This source file includes following definitions.
- sbmac_mii_sync
- sbmac_mii_senddata
- sbmac_mii_read
- sbmac_mii_write
- sbdma_initctx
- sbdma_channel_start
- sbdma_channel_stop
- sbdma_align_skb
- sbdma_add_rcvbuffer
- sbdma_add_txbuffer
- sbdma_emptyring
- sbdma_fillring
- sbmac_netpoll
- sbdma_rx_process
- sbdma_tx_process
- sbmac_initctx
- sbdma_uninitctx
- sbmac_uninitctx
- sbmac_channel_start
- sbmac_channel_stop
- sbmac_set_channel_state
- sbmac_promiscuous_mode
- sbmac_set_iphdr_offset
- sbmac_addr2reg
- sbmac_set_speed
- sbmac_set_duplex
- sbmac_intr
- sbmac_start_tx
- sbmac_setmulti
- sbmac_init
- sbmac_open
- sbmac_mii_probe
- sbmac_mii_poll
- sbmac_tx_timeout
- sbmac_set_rx_mode
- sbmac_mii_ioctl
- sbmac_close
- sbmac_poll
- sbmac_probe
- sbmac_remove
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <linux/bug.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/timer.h>
18 #include <linux/errno.h>
19 #include <linux/ioport.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/bitops.h>
26 #include <linux/err.h>
27 #include <linux/ethtool.h>
28 #include <linux/mii.h>
29 #include <linux/phy.h>
30 #include <linux/platform_device.h>
31 #include <linux/prefetch.h>
32
33 #include <asm/cache.h>
34 #include <asm/io.h>
35 #include <asm/processor.h>
36
37
38
39 #define CONFIG_SBMAC_COALESCE
40
41
42 #define TX_TIMEOUT (2*HZ)
43
44
45 MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
46 MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
47
48
49
50
51
52 static int debug = 1;
53 module_param(debug, int, 0444);
54 MODULE_PARM_DESC(debug, "Debug messages");
55
56 #ifdef CONFIG_SBMAC_COALESCE
57 static int int_pktcnt_tx = 255;
58 module_param(int_pktcnt_tx, int, 0444);
59 MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
60
61 static int int_timeout_tx = 255;
62 module_param(int_timeout_tx, int, 0444);
63 MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
64
65 static int int_pktcnt_rx = 64;
66 module_param(int_pktcnt_rx, int, 0444);
67 MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
68
69 static int int_timeout_rx = 64;
70 module_param(int_timeout_rx, int, 0444);
71 MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
72 #endif
73
74 #include <asm/sibyte/board.h>
75 #include <asm/sibyte/sb1250.h>
76 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
77 #include <asm/sibyte/bcm1480_regs.h>
78 #include <asm/sibyte/bcm1480_int.h>
79 #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
80 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
81 #include <asm/sibyte/sb1250_regs.h>
82 #include <asm/sibyte/sb1250_int.h>
83 #else
84 #error invalid SiByte MAC configuration
85 #endif
86 #include <asm/sibyte/sb1250_scd.h>
87 #include <asm/sibyte/sb1250_mac.h>
88 #include <asm/sibyte/sb1250_dma.h>
89
90 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
91 #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
92 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
93 #define UNIT_INT(n) (K_INT_MAC_0 + (n))
94 #else
95 #error invalid SiByte MAC configuration
96 #endif
97
98 #ifdef K_INT_PHY
99 #define SBMAC_PHY_INT K_INT_PHY
100 #else
101 #define SBMAC_PHY_INT PHY_POLL
102 #endif
103
104
105
106
107
108 enum sbmac_speed {
109 sbmac_speed_none = 0,
110 sbmac_speed_10 = SPEED_10,
111 sbmac_speed_100 = SPEED_100,
112 sbmac_speed_1000 = SPEED_1000,
113 };
114
115 enum sbmac_duplex {
116 sbmac_duplex_none = -1,
117 sbmac_duplex_half = DUPLEX_HALF,
118 sbmac_duplex_full = DUPLEX_FULL,
119 };
120
121 enum sbmac_fc {
122 sbmac_fc_none,
123 sbmac_fc_disabled,
124 sbmac_fc_frame,
125 sbmac_fc_collision,
126 sbmac_fc_carrier,
127 };
128
129 enum sbmac_state {
130 sbmac_state_uninit,
131 sbmac_state_off,
132 sbmac_state_on,
133 sbmac_state_broken,
134 };
135
136
137
138
139
140
141
142 #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
143 (d)->sbdma_dscrtable : (d)->f+1)
144
145
146 #define NUMCACHEBLKS(x) DIV_ROUND_UP(x, SMP_CACHE_BYTES)
147
148 #define SBMAC_MAX_TXDESCR 256
149 #define SBMAC_MAX_RXDESCR 256
150
151 #define ENET_PACKET_SIZE 1518
152
153
154
155
156
157
158 struct sbdmadscr {
159 uint64_t dscr_a;
160 uint64_t dscr_b;
161 };
162
163
164
165
166
167 struct sbmacdma {
168
169
170
171
172
173 struct sbmac_softc *sbdma_eth;
174
175 int sbdma_channel;
176 int sbdma_txdir;
177 int sbdma_maxdescr;
178
179 #ifdef CONFIG_SBMAC_COALESCE
180 int sbdma_int_pktcnt;
181
182
183 int sbdma_int_timeout;
184
185 #endif
186 void __iomem *sbdma_config0;
187 void __iomem *sbdma_config1;
188 void __iomem *sbdma_dscrbase;
189
190 void __iomem *sbdma_dscrcnt;
191 void __iomem *sbdma_curdscr;
192
193 void __iomem *sbdma_oodpktlost;
194
195
196
197
198
199 void *sbdma_dscrtable_unaligned;
200 struct sbdmadscr *sbdma_dscrtable;
201
202 struct sbdmadscr *sbdma_dscrtable_end;
203
204 struct sk_buff **sbdma_ctxtable;
205
206
207 dma_addr_t sbdma_dscrtable_phys;
208
209 struct sbdmadscr *sbdma_addptr;
210 struct sbdmadscr *sbdma_remptr;
211
212 };
213
214
215
216
217
218
219 struct sbmac_softc {
220
221
222
223
224 struct net_device *sbm_dev;
225 struct napi_struct napi;
226 struct phy_device *phy_dev;
227 struct mii_bus *mii_bus;
228 spinlock_t sbm_lock;
229 int sbm_devflags;
230
231
232
233
234 void __iomem *sbm_base;
235 enum sbmac_state sbm_state;
236
237 void __iomem *sbm_macenable;
238 void __iomem *sbm_maccfg;
239 void __iomem *sbm_fifocfg;
240 void __iomem *sbm_framecfg;
241 void __iomem *sbm_rxfilter;
242 void __iomem *sbm_isr;
243 void __iomem *sbm_imr;
244 void __iomem *sbm_mdio;
245
246 enum sbmac_speed sbm_speed;
247 enum sbmac_duplex sbm_duplex;
248 enum sbmac_fc sbm_fc;
249 int sbm_pause;
250 int sbm_link;
251
252 unsigned char sbm_hwaddr[ETH_ALEN];
253
254 struct sbmacdma sbm_txdma;
255 struct sbmacdma sbm_rxdma;
256 int rx_hw_checksum;
257 int sbe_idx;
258 };
259
260
261
262
263
264
265
266
267
268
269 static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
270 int txrx, int maxdescr);
271 static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
272 static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
273 struct sk_buff *m);
274 static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
275 static void sbdma_emptyring(struct sbmacdma *d);
276 static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
277 static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
278 int work_to_do, int poll);
279 static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
280 int poll);
281 static int sbmac_initctx(struct sbmac_softc *s);
282 static void sbmac_channel_start(struct sbmac_softc *s);
283 static void sbmac_channel_stop(struct sbmac_softc *s);
284 static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
285 enum sbmac_state);
286 static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
287 static uint64_t sbmac_addr2reg(unsigned char *ptr);
288 static irqreturn_t sbmac_intr(int irq, void *dev_instance);
289 static netdev_tx_t sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
290 static void sbmac_setmulti(struct sbmac_softc *sc);
291 static int sbmac_init(struct platform_device *pldev, long long base);
292 static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
293 static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
294 enum sbmac_fc fc);
295
296 static int sbmac_open(struct net_device *dev);
297 static void sbmac_tx_timeout (struct net_device *dev);
298 static void sbmac_set_rx_mode(struct net_device *dev);
299 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
300 static int sbmac_close(struct net_device *dev);
301 static int sbmac_poll(struct napi_struct *napi, int budget);
302
303 static void sbmac_mii_poll(struct net_device *dev);
304 static int sbmac_mii_probe(struct net_device *dev);
305
306 static void sbmac_mii_sync(void __iomem *sbm_mdio);
307 static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
308 int bitcnt);
309 static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
310 static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
311 u16 val);
312
313
314
315
316
317
318 static char sbmac_string[] = "sb1250-mac";
319
320 static char sbmac_mdio_string[] = "sb1250-mac-mdio";
321
322
323
324
325
326
327 #define MII_COMMAND_START 0x01
328 #define MII_COMMAND_READ 0x02
329 #define MII_COMMAND_WRITE 0x01
330 #define MII_COMMAND_ACK 0x02
331
332 #define M_MAC_MDIO_DIR_OUTPUT 0
333
334 #define ENABLE 1
335 #define DISABLE 0
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 static void sbmac_mii_sync(void __iomem *sbm_mdio)
351 {
352 int cnt;
353 uint64_t bits;
354 int mac_mdio_genc;
355
356 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
357
358 bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
359
360 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
361
362 for (cnt = 0; cnt < 32; cnt++) {
363 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
364 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
365 }
366 }
367
368
369
370
371
372
373
374
375
376
377
378
379
380 static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
381 int bitcnt)
382 {
383 int i;
384 uint64_t bits;
385 unsigned int curmask;
386 int mac_mdio_genc;
387
388 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
389
390 bits = M_MAC_MDIO_DIR_OUTPUT;
391 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
392
393 curmask = 1 << (bitcnt - 1);
394
395 for (i = 0; i < bitcnt; i++) {
396 if (data & curmask)
397 bits |= M_MAC_MDIO_OUT;
398 else bits &= ~M_MAC_MDIO_OUT;
399 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
400 __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
401 __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
402 curmask >>= 1;
403 }
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
422 {
423 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
424 void __iomem *sbm_mdio = sc->sbm_mdio;
425 int idx;
426 int error;
427 int regval;
428 int mac_mdio_genc;
429
430
431
432
433
434 sbmac_mii_sync(sbm_mdio);
435
436
437
438
439
440
441
442
443 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
444 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
445 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
446 sbmac_mii_senddata(sbm_mdio, regidx, 5);
447
448 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
449
450
451
452
453 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
454
455
456
457
458 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
459 sbm_mdio);
460 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
461
462
463
464
465 error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
466
467
468
469
470
471 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
472 sbm_mdio);
473 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
474
475 regval = 0;
476
477 for (idx = 0; idx < 16; idx++) {
478 regval <<= 1;
479
480 if (error == 0) {
481 if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
482 regval |= 1;
483 }
484
485 __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
486 sbm_mdio);
487 __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
488 }
489
490
491 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
492
493 if (error == 0)
494 return regval;
495 return 0xffff;
496 }
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514 static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
515 u16 regval)
516 {
517 struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
518 void __iomem *sbm_mdio = sc->sbm_mdio;
519 int mac_mdio_genc;
520
521 sbmac_mii_sync(sbm_mdio);
522
523 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
524 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
525 sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
526 sbmac_mii_senddata(sbm_mdio, regidx, 5);
527 sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
528 sbmac_mii_senddata(sbm_mdio, regval, 16);
529
530 mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
531
532 __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
533
534 return 0;
535 }
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
558 int txrx, int maxdescr)
559 {
560 #ifdef CONFIG_SBMAC_COALESCE
561 int int_pktcnt, int_timeout;
562 #endif
563
564
565
566
567
568 d->sbdma_eth = s;
569 d->sbdma_channel = chan;
570 d->sbdma_txdir = txrx;
571
572 #if 0
573
574 s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
575 #endif
576
577 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
578 __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
579 __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
580 __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
581 __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
582 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
583 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
584 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
585 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
586 __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
587 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
588 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
589 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
590 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
591 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
592 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
593 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
594 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
595 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
596 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
597 __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
598
599
600
601
602
603 d->sbdma_config0 =
604 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
605 d->sbdma_config1 =
606 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
607 d->sbdma_dscrbase =
608 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
609 d->sbdma_dscrcnt =
610 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
611 d->sbdma_curdscr =
612 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
613 if (d->sbdma_txdir)
614 d->sbdma_oodpktlost = NULL;
615 else
616 d->sbdma_oodpktlost =
617 s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
618
619
620
621
622
623 d->sbdma_maxdescr = maxdescr;
624
625 d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
626 sizeof(*d->sbdma_dscrtable),
627 GFP_KERNEL);
628
629
630
631
632
633 d->sbdma_dscrtable = (struct sbdmadscr *)
634 ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
635 sizeof(*d->sbdma_dscrtable));
636
637 d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
638
639 d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
640
641
642
643
644
645 d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
646 sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
647
648 #ifdef CONFIG_SBMAC_COALESCE
649
650
651
652
653 int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
654 if ( int_pktcnt ) {
655 d->sbdma_int_pktcnt = int_pktcnt;
656 } else {
657 d->sbdma_int_pktcnt = 1;
658 }
659
660 int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
661 if ( int_timeout ) {
662 d->sbdma_int_timeout = int_timeout;
663 } else {
664 d->sbdma_int_timeout = 0;
665 }
666 #endif
667
668 }
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683 static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
684 {
685
686
687
688
689 #ifdef CONFIG_SBMAC_COALESCE
690 __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
691 0, d->sbdma_config1);
692 __raw_writeq(M_DMA_EOP_INT_EN |
693 V_DMA_RINGSZ(d->sbdma_maxdescr) |
694 V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
695 0, d->sbdma_config0);
696 #else
697 __raw_writeq(0, d->sbdma_config1);
698 __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
699 0, d->sbdma_config0);
700 #endif
701
702 __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
703
704
705
706
707
708 d->sbdma_addptr = d->sbdma_dscrtable;
709 d->sbdma_remptr = d->sbdma_dscrtable;
710 }
711
712
713
714
715
716
717
718
719
720
721
722
723
724 static void sbdma_channel_stop(struct sbmacdma *d)
725 {
726
727
728
729
730 __raw_writeq(0, d->sbdma_config1);
731
732 __raw_writeq(0, d->sbdma_dscrbase);
733
734 __raw_writeq(0, d->sbdma_config0);
735
736
737
738
739
740 d->sbdma_addptr = NULL;
741 d->sbdma_remptr = NULL;
742 }
743
744 static inline void sbdma_align_skb(struct sk_buff *skb,
745 unsigned int power2, unsigned int offset)
746 {
747 unsigned char *addr = skb->data;
748 unsigned char *newaddr = PTR_ALIGN(addr, power2);
749
750 skb_reserve(skb, newaddr - addr + offset);
751 }
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771 static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
772 struct sk_buff *sb)
773 {
774 struct net_device *dev = sc->sbm_dev;
775 struct sbdmadscr *dsc;
776 struct sbdmadscr *nextdsc;
777 struct sk_buff *sb_new = NULL;
778 int pktsize = ENET_PACKET_SIZE;
779
780
781
782 dsc = d->sbdma_addptr;
783 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
784
785
786
787
788
789
790
791 if (nextdsc == d->sbdma_remptr) {
792 return -ENOSPC;
793 }
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814 if (sb == NULL) {
815 sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
816 SMP_CACHE_BYTES * 2 +
817 NET_IP_ALIGN);
818 if (sb_new == NULL)
819 return -ENOBUFS;
820
821 sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
822 }
823 else {
824 sb_new = sb;
825
826
827
828
829 }
830
831
832
833
834
835 #ifdef CONFIG_SBMAC_COALESCE
836
837
838
839 dsc->dscr_a = virt_to_phys(sb_new->data) |
840 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
841 #else
842 dsc->dscr_a = virt_to_phys(sb_new->data) |
843 V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
844 M_DMA_DSCRA_INTERRUPT;
845 #endif
846
847
848 dsc->dscr_b = 0;
849
850
851
852
853
854 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
855
856
857
858
859
860 d->sbdma_addptr = nextdsc;
861
862
863
864
865
866 __raw_writeq(1, d->sbdma_dscrcnt);
867
868 return 0;
869 }
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887 static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
888 {
889 struct sbdmadscr *dsc;
890 struct sbdmadscr *nextdsc;
891 uint64_t phys;
892 uint64_t ncb;
893 int length;
894
895
896
897 dsc = d->sbdma_addptr;
898 nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
899
900
901
902
903
904
905
906 if (nextdsc == d->sbdma_remptr) {
907 return -ENOSPC;
908 }
909
910
911
912
913
914
915
916 length = sb->len;
917
918
919
920
921
922
923
924
925 phys = virt_to_phys(sb->data);
926 ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
927
928 dsc->dscr_a = phys |
929 V_DMA_DSCRA_A_SIZE(ncb) |
930 #ifndef CONFIG_SBMAC_COALESCE
931 M_DMA_DSCRA_INTERRUPT |
932 #endif
933 M_DMA_ETHTX_SOP;
934
935
936
937 dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
938 V_DMA_DSCRB_PKT_SIZE(length);
939
940
941
942
943
944 d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
945
946
947
948
949
950 d->sbdma_addptr = nextdsc;
951
952
953
954
955
956 __raw_writeq(1, d->sbdma_dscrcnt);
957
958 return 0;
959 }
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976 static void sbdma_emptyring(struct sbmacdma *d)
977 {
978 int idx;
979 struct sk_buff *sb;
980
981 for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
982 sb = d->sbdma_ctxtable[idx];
983 if (sb) {
984 dev_kfree_skb(sb);
985 d->sbdma_ctxtable[idx] = NULL;
986 }
987 }
988 }
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005 static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
1006 {
1007 int idx;
1008
1009 for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
1010 if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
1011 break;
1012 }
1013 }
1014
1015 #ifdef CONFIG_NET_POLL_CONTROLLER
1016 static void sbmac_netpoll(struct net_device *netdev)
1017 {
1018 struct sbmac_softc *sc = netdev_priv(netdev);
1019 int irq = sc->sbm_dev->irq;
1020
1021 __raw_writeq(0, sc->sbm_imr);
1022
1023 sbmac_intr(irq, netdev);
1024
1025 #ifdef CONFIG_SBMAC_COALESCE
1026 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1027 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1028 sc->sbm_imr);
1029 #else
1030 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1031 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1032 #endif
1033 }
1034 #endif
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1053 int work_to_do, int poll)
1054 {
1055 struct net_device *dev = sc->sbm_dev;
1056 int curidx;
1057 int hwidx;
1058 struct sbdmadscr *dsc;
1059 struct sk_buff *sb;
1060 int len;
1061 int work_done = 0;
1062 int dropped = 0;
1063
1064 prefetch(d);
1065
1066 again:
1067
1068 dev->stats.rx_fifo_errors
1069 += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1070 __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1071
1072 while (work_to_do-- > 0) {
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084 dsc = d->sbdma_remptr;
1085 curidx = dsc - d->sbdma_dscrtable;
1086
1087 prefetch(dsc);
1088 prefetch(&d->sbdma_ctxtable[curidx]);
1089
1090 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1091 d->sbdma_dscrtable_phys) /
1092 sizeof(*d->sbdma_dscrtable);
1093
1094
1095
1096
1097
1098
1099
1100 if (curidx == hwidx)
1101 goto done;
1102
1103
1104
1105
1106
1107 sb = d->sbdma_ctxtable[curidx];
1108 d->sbdma_ctxtable[curidx] = NULL;
1109
1110 len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
1111
1112
1113
1114
1115
1116
1117
1118 if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
1119
1120
1121
1122
1123
1124
1125
1126 if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
1127 -ENOBUFS)) {
1128 dev->stats.rx_dropped++;
1129
1130 sbdma_add_rcvbuffer(sc, d, sb);
1131
1132 printk(KERN_ERR "dropped packet (1)\n");
1133 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1134 goto done;
1135 } else {
1136
1137
1138
1139 skb_put(sb,len);
1140
1141
1142
1143
1144
1145
1146 sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1147
1148 if (sc->rx_hw_checksum == ENABLE) {
1149 if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1150 !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1151 sb->ip_summed = CHECKSUM_UNNECESSARY;
1152
1153 } else {
1154 skb_checksum_none_assert(sb);
1155 }
1156 }
1157 prefetch(sb->data);
1158 prefetch((const void *)(((char *)sb->data)+32));
1159 if (poll)
1160 dropped = netif_receive_skb(sb);
1161 else
1162 dropped = netif_rx(sb);
1163
1164 if (dropped == NET_RX_DROP) {
1165 dev->stats.rx_dropped++;
1166 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1167 goto done;
1168 }
1169 else {
1170 dev->stats.rx_bytes += len;
1171 dev->stats.rx_packets++;
1172 }
1173 }
1174 } else {
1175
1176
1177
1178
1179 dev->stats.rx_errors++;
1180 sbdma_add_rcvbuffer(sc, d, sb);
1181 }
1182
1183
1184
1185
1186
1187
1188 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1189 work_done++;
1190 }
1191 if (!poll) {
1192 work_to_do = 32;
1193 goto again;
1194 }
1195 done:
1196 return work_done;
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217 static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1218 int poll)
1219 {
1220 struct net_device *dev = sc->sbm_dev;
1221 int curidx;
1222 int hwidx;
1223 struct sbdmadscr *dsc;
1224 struct sk_buff *sb;
1225 unsigned long flags;
1226 int packets_handled = 0;
1227
1228 spin_lock_irqsave(&(sc->sbm_lock), flags);
1229
1230 if (d->sbdma_remptr == d->sbdma_addptr)
1231 goto end_unlock;
1232
1233 hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1234 d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
1235
1236 for (;;) {
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248 curidx = d->sbdma_remptr - d->sbdma_dscrtable;
1249
1250
1251
1252
1253
1254
1255
1256 if (curidx == hwidx)
1257 break;
1258
1259
1260
1261
1262
1263 dsc = &(d->sbdma_dscrtable[curidx]);
1264 sb = d->sbdma_ctxtable[curidx];
1265 d->sbdma_ctxtable[curidx] = NULL;
1266
1267
1268
1269
1270
1271 dev->stats.tx_bytes += sb->len;
1272 dev->stats.tx_packets++;
1273
1274
1275
1276
1277
1278 dev_consume_skb_irq(sb);
1279
1280
1281
1282
1283
1284 d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1285
1286 packets_handled++;
1287
1288 }
1289
1290
1291
1292
1293
1294
1295
1296 if (packets_handled)
1297 netif_wake_queue(d->sbdma_eth->sbm_dev);
1298
1299 end_unlock:
1300 spin_unlock_irqrestore(&(sc->sbm_lock), flags);
1301
1302 }
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321 static int sbmac_initctx(struct sbmac_softc *s)
1322 {
1323
1324
1325
1326
1327
1328 s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1329 s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1330 s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1331 s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1332 s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1333 s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1334 s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1335 s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1336
1337
1338
1339
1340
1341
1342 sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1343 sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
1344
1345
1346
1347
1348
1349 s->sbm_state = sbmac_state_off;
1350
1351 return 0;
1352 }
1353
1354
1355 static void sbdma_uninitctx(struct sbmacdma *d)
1356 {
1357 kfree(d->sbdma_dscrtable_unaligned);
1358 d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
1359
1360 kfree(d->sbdma_ctxtable);
1361 d->sbdma_ctxtable = NULL;
1362 }
1363
1364
1365 static void sbmac_uninitctx(struct sbmac_softc *sc)
1366 {
1367 sbdma_uninitctx(&(sc->sbm_txdma));
1368 sbdma_uninitctx(&(sc->sbm_rxdma));
1369 }
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384 static void sbmac_channel_start(struct sbmac_softc *s)
1385 {
1386 uint64_t reg;
1387 void __iomem *port;
1388 uint64_t cfg,fifo,framecfg;
1389 int idx, th_value;
1390
1391
1392
1393
1394
1395 if (s->sbm_state == sbmac_state_on)
1396 return;
1397
1398
1399
1400
1401
1402 __raw_writeq(0, s->sbm_macenable);
1403
1404
1405
1406
1407
1408 __raw_writeq(0, s->sbm_rxfilter);
1409
1410
1411
1412
1413
1414 cfg = M_MAC_RETRY_EN |
1415 M_MAC_TX_HOLD_SOP_EN |
1416 V_MAC_TX_PAUSE_CNT_16K |
1417 M_MAC_AP_STAT_EN |
1418 M_MAC_FAST_SYNC |
1419 M_MAC_SS_EN |
1420 0;
1421
1422
1423
1424
1425
1426
1427 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
1428 th_value = 28;
1429 else
1430 th_value = 64;
1431
1432 fifo = V_MAC_TX_WR_THRSH(4) |
1433 ((s->sbm_speed == sbmac_speed_1000)
1434 ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1435 V_MAC_TX_RL_THRSH(4) |
1436 V_MAC_RX_PL_THRSH(4) |
1437 V_MAC_RX_RD_THRSH(4) |
1438 V_MAC_RX_RL_THRSH(8) |
1439 0;
1440
1441 framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1442 V_MAC_MAX_FRAMESZ_DEFAULT |
1443 V_MAC_BACKOFF_SEL(1);
1444
1445
1446
1447
1448
1449 port = s->sbm_base + R_MAC_HASH_BASE;
1450 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
1451 __raw_writeq(0, port);
1452 port += sizeof(uint64_t);
1453 }
1454
1455
1456
1457
1458
1459 port = s->sbm_base + R_MAC_ADDR_BASE;
1460 for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
1461 __raw_writeq(0, port);
1462 port += sizeof(uint64_t);
1463 }
1464
1465
1466
1467
1468
1469 port = s->sbm_base + R_MAC_CHUP0_BASE;
1470 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1471 __raw_writeq(0, port);
1472 port += sizeof(uint64_t);
1473 }
1474
1475
1476 port = s->sbm_base + R_MAC_CHLO0_BASE;
1477 for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1478 __raw_writeq(0, port);
1479 port += sizeof(uint64_t);
1480 }
1481
1482
1483
1484
1485
1486
1487 reg = sbmac_addr2reg(s->sbm_hwaddr);
1488
1489 port = s->sbm_base + R_MAC_ADDR_BASE;
1490 __raw_writeq(reg, port);
1491 port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1492
1493 __raw_writeq(reg, port);
1494
1495
1496
1497
1498
1499
1500 __raw_writeq(0, s->sbm_rxfilter);
1501 __raw_writeq(0, s->sbm_imr);
1502 __raw_writeq(framecfg, s->sbm_framecfg);
1503 __raw_writeq(fifo, s->sbm_fifocfg);
1504 __raw_writeq(cfg, s->sbm_maccfg);
1505
1506
1507
1508
1509
1510 sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1511 sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
1512
1513
1514
1515
1516
1517 sbmac_set_speed(s,s->sbm_speed);
1518 sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
1519
1520
1521
1522
1523
1524 sbdma_fillring(s, &(s->sbm_rxdma));
1525
1526
1527
1528
1529
1530 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
1531 __raw_writeq(M_MAC_RXDMA_EN0 |
1532 M_MAC_TXDMA_EN0, s->sbm_macenable);
1533 #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1534 __raw_writeq(M_MAC_RXDMA_EN0 |
1535 M_MAC_TXDMA_EN0 |
1536 M_MAC_RX_ENABLE |
1537 M_MAC_TX_ENABLE, s->sbm_macenable);
1538 #else
1539 #error invalid SiByte MAC configuration
1540 #endif
1541
1542 #ifdef CONFIG_SBMAC_COALESCE
1543 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1544 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
1545 #else
1546 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1547 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
1548 #endif
1549
1550
1551
1552
1553
1554 __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1555
1556
1557
1558
1559
1560 s->sbm_state = sbmac_state_on;
1561
1562
1563
1564
1565
1566 sbmac_setmulti(s);
1567
1568
1569
1570
1571
1572 if (s->sbm_devflags & IFF_PROMISC) {
1573 sbmac_promiscuous_mode(s,1);
1574 }
1575
1576 }
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591 static void sbmac_channel_stop(struct sbmac_softc *s)
1592 {
1593
1594
1595 if (s->sbm_state == sbmac_state_off)
1596 return;
1597
1598
1599
1600 __raw_writeq(0, s->sbm_rxfilter);
1601 __raw_writeq(0, s->sbm_imr);
1602
1603
1604
1605
1606
1607
1608
1609 __raw_writeq(0, s->sbm_macenable);
1610
1611
1612
1613 s->sbm_state = sbmac_state_off;
1614
1615
1616
1617
1618
1619 sbdma_channel_stop(&(s->sbm_rxdma));
1620 sbdma_channel_stop(&(s->sbm_txdma));
1621
1622
1623
1624 sbdma_emptyring(&(s->sbm_rxdma));
1625 sbdma_emptyring(&(s->sbm_txdma));
1626
1627 }
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640 static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1641 enum sbmac_state state)
1642 {
1643 enum sbmac_state oldstate = sc->sbm_state;
1644
1645
1646
1647
1648
1649 if (state == oldstate) {
1650 return oldstate;
1651 }
1652
1653
1654
1655
1656
1657 if (state == sbmac_state_on) {
1658 sbmac_channel_start(sc);
1659 }
1660 else {
1661 sbmac_channel_stop(sc);
1662 }
1663
1664
1665
1666
1667
1668 return oldstate;
1669 }
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685 static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1686 {
1687 uint64_t reg;
1688
1689 if (sc->sbm_state != sbmac_state_on)
1690 return;
1691
1692 if (onoff) {
1693 reg = __raw_readq(sc->sbm_rxfilter);
1694 reg |= M_MAC_ALLPKT_EN;
1695 __raw_writeq(reg, sc->sbm_rxfilter);
1696 }
1697 else {
1698 reg = __raw_readq(sc->sbm_rxfilter);
1699 reg &= ~M_MAC_ALLPKT_EN;
1700 __raw_writeq(reg, sc->sbm_rxfilter);
1701 }
1702 }
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716 static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1717 {
1718 uint64_t reg;
1719
1720
1721 reg = __raw_readq(sc->sbm_rxfilter);
1722 reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
1723 __raw_writeq(reg, sc->sbm_rxfilter);
1724
1725
1726
1727 if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
1728 sc->rx_hw_checksum = DISABLE;
1729 } else {
1730 sc->rx_hw_checksum = ENABLE;
1731 }
1732 }
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748 static uint64_t sbmac_addr2reg(unsigned char *ptr)
1749 {
1750 uint64_t reg = 0;
1751
1752 ptr += 6;
1753
1754 reg |= (uint64_t) *(--ptr);
1755 reg <<= 8;
1756 reg |= (uint64_t) *(--ptr);
1757 reg <<= 8;
1758 reg |= (uint64_t) *(--ptr);
1759 reg <<= 8;
1760 reg |= (uint64_t) *(--ptr);
1761 reg <<= 8;
1762 reg |= (uint64_t) *(--ptr);
1763 reg <<= 8;
1764 reg |= (uint64_t) *(--ptr);
1765
1766 return reg;
1767 }
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785 static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
1786 {
1787 uint64_t cfg;
1788 uint64_t framecfg;
1789
1790
1791
1792
1793
1794 s->sbm_speed = speed;
1795
1796 if (s->sbm_state == sbmac_state_on)
1797 return 0;
1798
1799
1800
1801
1802
1803 cfg = __raw_readq(s->sbm_maccfg);
1804 framecfg = __raw_readq(s->sbm_framecfg);
1805
1806
1807
1808
1809
1810 cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1811 framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1812 M_MAC_SLOT_SIZE);
1813
1814
1815
1816
1817
1818 switch (speed) {
1819 case sbmac_speed_10:
1820 framecfg |= V_MAC_IFG_RX_10 |
1821 V_MAC_IFG_TX_10 |
1822 K_MAC_IFG_THRSH_10 |
1823 V_MAC_SLOT_SIZE_10;
1824 cfg |= V_MAC_SPEED_SEL_10MBPS;
1825 break;
1826
1827 case sbmac_speed_100:
1828 framecfg |= V_MAC_IFG_RX_100 |
1829 V_MAC_IFG_TX_100 |
1830 V_MAC_IFG_THRSH_100 |
1831 V_MAC_SLOT_SIZE_100;
1832 cfg |= V_MAC_SPEED_SEL_100MBPS ;
1833 break;
1834
1835 case sbmac_speed_1000:
1836 framecfg |= V_MAC_IFG_RX_1000 |
1837 V_MAC_IFG_TX_1000 |
1838 V_MAC_IFG_THRSH_1000 |
1839 V_MAC_SLOT_SIZE_1000;
1840 cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1841 break;
1842
1843 default:
1844 return 0;
1845 }
1846
1847
1848
1849
1850
1851 __raw_writeq(framecfg, s->sbm_framecfg);
1852 __raw_writeq(cfg, s->sbm_maccfg);
1853
1854 return 1;
1855 }
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873 static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1874 enum sbmac_fc fc)
1875 {
1876 uint64_t cfg;
1877
1878
1879
1880
1881
1882 s->sbm_duplex = duplex;
1883 s->sbm_fc = fc;
1884
1885 if (s->sbm_state == sbmac_state_on)
1886 return 0;
1887
1888
1889
1890
1891
1892 cfg = __raw_readq(s->sbm_maccfg);
1893
1894
1895
1896
1897
1898 cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
1899
1900
1901 switch (duplex) {
1902 case sbmac_duplex_half:
1903 switch (fc) {
1904 case sbmac_fc_disabled:
1905 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1906 break;
1907
1908 case sbmac_fc_collision:
1909 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1910 break;
1911
1912 case sbmac_fc_carrier:
1913 cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1914 break;
1915
1916 case sbmac_fc_frame:
1917 default:
1918 return 0;
1919 }
1920 break;
1921
1922 case sbmac_duplex_full:
1923 switch (fc) {
1924 case sbmac_fc_disabled:
1925 cfg |= V_MAC_FC_CMD_DISABLED;
1926 break;
1927
1928 case sbmac_fc_frame:
1929 cfg |= V_MAC_FC_CMD_ENABLED;
1930 break;
1931
1932 case sbmac_fc_collision:
1933 case sbmac_fc_carrier:
1934 default:
1935 return 0;
1936 }
1937 break;
1938 default:
1939 return 0;
1940 }
1941
1942
1943
1944
1945
1946 __raw_writeq(cfg, s->sbm_maccfg);
1947
1948 return 1;
1949 }
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965 static irqreturn_t sbmac_intr(int irq,void *dev_instance)
1966 {
1967 struct net_device *dev = (struct net_device *) dev_instance;
1968 struct sbmac_softc *sc = netdev_priv(dev);
1969 uint64_t isr;
1970 int handled = 0;
1971
1972
1973
1974
1975
1976
1977 isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
1978
1979 if (isr == 0)
1980 return IRQ_RETVAL(0);
1981 handled = 1;
1982
1983
1984
1985
1986
1987 if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
1988 sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
1989
1990 if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
1991 if (napi_schedule_prep(&sc->napi)) {
1992 __raw_writeq(0, sc->sbm_imr);
1993 __napi_schedule(&sc->napi);
1994
1995 }
1996 else {
1997
1998 sbdma_rx_process(sc,&(sc->sbm_rxdma),
1999 SBMAC_MAX_RXDESCR * 2, 0);
2000 }
2001 }
2002 return IRQ_RETVAL(handled);
2003 }
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018 static netdev_tx_t sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2019 {
2020 struct sbmac_softc *sc = netdev_priv(dev);
2021 unsigned long flags;
2022
2023
2024 spin_lock_irqsave(&sc->sbm_lock, flags);
2025
2026
2027
2028
2029
2030
2031 if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2032
2033 netif_stop_queue(dev);
2034 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2035
2036 return NETDEV_TX_BUSY;
2037 }
2038
2039 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2040
2041 return NETDEV_TX_OK;
2042 }
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058 static void sbmac_setmulti(struct sbmac_softc *sc)
2059 {
2060 uint64_t reg;
2061 void __iomem *port;
2062 int idx;
2063 struct netdev_hw_addr *ha;
2064 struct net_device *dev = sc->sbm_dev;
2065
2066
2067
2068
2069
2070
2071
2072 for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2073 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
2074 __raw_writeq(0, port);
2075 }
2076
2077 for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2078 port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
2079 __raw_writeq(0, port);
2080 }
2081
2082
2083
2084
2085
2086 reg = __raw_readq(sc->sbm_rxfilter);
2087 reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2088 __raw_writeq(reg, sc->sbm_rxfilter);
2089
2090 if (dev->flags & IFF_ALLMULTI) {
2091
2092
2093
2094
2095 reg = __raw_readq(sc->sbm_rxfilter);
2096 reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2097 __raw_writeq(reg, sc->sbm_rxfilter);
2098 return;
2099 }
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111 idx = 1;
2112 netdev_for_each_mc_addr(ha, dev) {
2113 if (idx == MAC_ADDR_COUNT)
2114 break;
2115 reg = sbmac_addr2reg(ha->addr);
2116 port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
2117 __raw_writeq(reg, port);
2118 idx++;
2119 }
2120
2121
2122
2123
2124
2125
2126 if (idx > 1) {
2127 reg = __raw_readq(sc->sbm_rxfilter);
2128 reg |= M_MAC_MCAST_EN;
2129 __raw_writeq(reg, sc->sbm_rxfilter);
2130 }
2131 }
2132
2133 static const struct net_device_ops sbmac_netdev_ops = {
2134 .ndo_open = sbmac_open,
2135 .ndo_stop = sbmac_close,
2136 .ndo_start_xmit = sbmac_start_tx,
2137 .ndo_set_rx_mode = sbmac_set_rx_mode,
2138 .ndo_tx_timeout = sbmac_tx_timeout,
2139 .ndo_do_ioctl = sbmac_mii_ioctl,
2140 .ndo_validate_addr = eth_validate_addr,
2141 .ndo_set_mac_address = eth_mac_addr,
2142 #ifdef CONFIG_NET_POLL_CONTROLLER
2143 .ndo_poll_controller = sbmac_netpoll,
2144 #endif
2145 };
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159 static int sbmac_init(struct platform_device *pldev, long long base)
2160 {
2161 struct net_device *dev = platform_get_drvdata(pldev);
2162 int idx = pldev->id;
2163 struct sbmac_softc *sc = netdev_priv(dev);
2164 unsigned char *eaddr;
2165 uint64_t ea_reg;
2166 int i;
2167 int err;
2168
2169 sc->sbm_dev = dev;
2170 sc->sbe_idx = idx;
2171
2172 eaddr = sc->sbm_hwaddr;
2173
2174
2175
2176
2177
2178
2179 ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2180 __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
2181 for (i = 0; i < 6; i++) {
2182 eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2183 ea_reg >>= 8;
2184 }
2185
2186 for (i = 0; i < 6; i++) {
2187 dev->dev_addr[i] = eaddr[i];
2188 }
2189
2190
2191
2192
2193
2194
2195 sbmac_initctx(sc);
2196
2197
2198
2199
2200
2201 spin_lock_init(&(sc->sbm_lock));
2202
2203 dev->netdev_ops = &sbmac_netdev_ops;
2204 dev->watchdog_timeo = TX_TIMEOUT;
2205 dev->min_mtu = 0;
2206 dev->max_mtu = ENET_PACKET_SIZE;
2207
2208 netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
2209
2210 dev->irq = UNIT_INT(idx);
2211
2212
2213 sbmac_set_iphdr_offset(sc);
2214
2215 sc->mii_bus = mdiobus_alloc();
2216 if (sc->mii_bus == NULL) {
2217 err = -ENOMEM;
2218 goto uninit_ctx;
2219 }
2220
2221 sc->mii_bus->name = sbmac_mdio_string;
2222 snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2223 pldev->name, idx);
2224 sc->mii_bus->priv = sc;
2225 sc->mii_bus->read = sbmac_mii_read;
2226 sc->mii_bus->write = sbmac_mii_write;
2227
2228 sc->mii_bus->parent = &pldev->dev;
2229
2230
2231
2232 err = mdiobus_register(sc->mii_bus);
2233 if (err) {
2234 printk(KERN_ERR "%s: unable to register MDIO bus\n",
2235 dev->name);
2236 goto free_mdio;
2237 }
2238 platform_set_drvdata(pldev, sc->mii_bus);
2239
2240 err = register_netdev(dev);
2241 if (err) {
2242 printk(KERN_ERR "%s.%d: unable to register netdev\n",
2243 sbmac_string, idx);
2244 goto unreg_mdio;
2245 }
2246
2247 pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2248
2249 if (sc->rx_hw_checksum == ENABLE)
2250 pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2251
2252
2253
2254
2255
2256
2257 pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
2258 dev->name, base, eaddr);
2259
2260 return 0;
2261 unreg_mdio:
2262 mdiobus_unregister(sc->mii_bus);
2263 free_mdio:
2264 mdiobus_free(sc->mii_bus);
2265 uninit_ctx:
2266 sbmac_uninitctx(sc);
2267 return err;
2268 }
2269
2270
2271 static int sbmac_open(struct net_device *dev)
2272 {
2273 struct sbmac_softc *sc = netdev_priv(dev);
2274 int err;
2275
2276 if (debug > 1)
2277 pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2278
2279
2280
2281
2282
2283
2284
2285 __raw_readq(sc->sbm_isr);
2286 err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
2287 if (err) {
2288 printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2289 dev->irq);
2290 goto out_err;
2291 }
2292
2293 sc->sbm_speed = sbmac_speed_none;
2294 sc->sbm_duplex = sbmac_duplex_none;
2295 sc->sbm_fc = sbmac_fc_none;
2296 sc->sbm_pause = -1;
2297 sc->sbm_link = 0;
2298
2299
2300
2301
2302 err = sbmac_mii_probe(dev);
2303 if (err)
2304 goto out_unregister;
2305
2306
2307
2308
2309
2310 sbmac_set_channel_state(sc,sbmac_state_on);
2311
2312 netif_start_queue(dev);
2313
2314 sbmac_set_rx_mode(dev);
2315
2316 phy_start(sc->phy_dev);
2317
2318 napi_enable(&sc->napi);
2319
2320 return 0;
2321
2322 out_unregister:
2323 free_irq(dev->irq, dev);
2324 out_err:
2325 return err;
2326 }
2327
2328 static int sbmac_mii_probe(struct net_device *dev)
2329 {
2330 struct sbmac_softc *sc = netdev_priv(dev);
2331 struct phy_device *phy_dev;
2332
2333 phy_dev = phy_find_first(sc->mii_bus);
2334 if (!phy_dev) {
2335 printk(KERN_ERR "%s: no PHY found\n", dev->name);
2336 return -ENXIO;
2337 }
2338
2339 phy_dev = phy_connect(dev, dev_name(&phy_dev->mdio.dev),
2340 &sbmac_mii_poll, PHY_INTERFACE_MODE_GMII);
2341 if (IS_ERR(phy_dev)) {
2342 printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2343 return PTR_ERR(phy_dev);
2344 }
2345
2346
2347 phy_set_max_speed(phy_dev, SPEED_1000);
2348 phy_support_asym_pause(phy_dev);
2349
2350 phy_attached_info(phy_dev);
2351
2352 sc->phy_dev = phy_dev;
2353
2354 return 0;
2355 }
2356
2357
2358 static void sbmac_mii_poll(struct net_device *dev)
2359 {
2360 struct sbmac_softc *sc = netdev_priv(dev);
2361 struct phy_device *phy_dev = sc->phy_dev;
2362 unsigned long flags;
2363 enum sbmac_fc fc;
2364 int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2365
2366 link_chg = (sc->sbm_link != phy_dev->link);
2367 speed_chg = (sc->sbm_speed != phy_dev->speed);
2368 duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2369 pause_chg = (sc->sbm_pause != phy_dev->pause);
2370
2371 if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2372 return;
2373
2374 if (!phy_dev->link) {
2375 if (link_chg) {
2376 sc->sbm_link = phy_dev->link;
2377 sc->sbm_speed = sbmac_speed_none;
2378 sc->sbm_duplex = sbmac_duplex_none;
2379 sc->sbm_fc = sbmac_fc_disabled;
2380 sc->sbm_pause = -1;
2381 pr_info("%s: link unavailable\n", dev->name);
2382 }
2383 return;
2384 }
2385
2386 if (phy_dev->duplex == DUPLEX_FULL) {
2387 if (phy_dev->pause)
2388 fc = sbmac_fc_frame;
2389 else
2390 fc = sbmac_fc_disabled;
2391 } else
2392 fc = sbmac_fc_collision;
2393 fc_chg = (sc->sbm_fc != fc);
2394
2395 pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2396 phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2397
2398 spin_lock_irqsave(&sc->sbm_lock, flags);
2399
2400 sc->sbm_speed = phy_dev->speed;
2401 sc->sbm_duplex = phy_dev->duplex;
2402 sc->sbm_fc = fc;
2403 sc->sbm_pause = phy_dev->pause;
2404 sc->sbm_link = phy_dev->link;
2405
2406 if ((speed_chg || duplex_chg || fc_chg) &&
2407 sc->sbm_state != sbmac_state_off) {
2408
2409
2410
2411 if (debug > 1)
2412 pr_debug("%s: restarting channel "
2413 "because PHY state changed\n", dev->name);
2414 sbmac_channel_stop(sc);
2415 sbmac_channel_start(sc);
2416 }
2417
2418 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2419 }
2420
2421
2422 static void sbmac_tx_timeout (struct net_device *dev)
2423 {
2424 struct sbmac_softc *sc = netdev_priv(dev);
2425 unsigned long flags;
2426
2427 spin_lock_irqsave(&sc->sbm_lock, flags);
2428
2429
2430 netif_trans_update(dev);
2431 dev->stats.tx_errors++;
2432
2433 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2434
2435 printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2436 }
2437
2438
2439
2440
2441 static void sbmac_set_rx_mode(struct net_device *dev)
2442 {
2443 unsigned long flags;
2444 struct sbmac_softc *sc = netdev_priv(dev);
2445
2446 spin_lock_irqsave(&sc->sbm_lock, flags);
2447 if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2448
2449
2450
2451
2452 if (dev->flags & IFF_PROMISC) {
2453 sbmac_promiscuous_mode(sc,1);
2454 }
2455 else {
2456 sbmac_promiscuous_mode(sc,0);
2457 }
2458 }
2459 spin_unlock_irqrestore(&sc->sbm_lock, flags);
2460
2461
2462
2463
2464
2465 sbmac_setmulti(sc);
2466
2467 }
2468
2469 static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2470 {
2471 struct sbmac_softc *sc = netdev_priv(dev);
2472
2473 if (!netif_running(dev) || !sc->phy_dev)
2474 return -EINVAL;
2475
2476 return phy_mii_ioctl(sc->phy_dev, rq, cmd);
2477 }
2478
2479 static int sbmac_close(struct net_device *dev)
2480 {
2481 struct sbmac_softc *sc = netdev_priv(dev);
2482
2483 napi_disable(&sc->napi);
2484
2485 phy_stop(sc->phy_dev);
2486
2487 sbmac_set_channel_state(sc, sbmac_state_off);
2488
2489 netif_stop_queue(dev);
2490
2491 if (debug > 1)
2492 pr_debug("%s: Shutting down ethercard\n", dev->name);
2493
2494 phy_disconnect(sc->phy_dev);
2495 sc->phy_dev = NULL;
2496 free_irq(dev->irq, dev);
2497
2498 sbdma_emptyring(&(sc->sbm_txdma));
2499 sbdma_emptyring(&(sc->sbm_rxdma));
2500
2501 return 0;
2502 }
2503
2504 static int sbmac_poll(struct napi_struct *napi, int budget)
2505 {
2506 struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2507 int work_done;
2508
2509 work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
2510 sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2511
2512 if (work_done < budget) {
2513 napi_complete_done(napi, work_done);
2514
2515 #ifdef CONFIG_SBMAC_COALESCE
2516 __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2517 ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2518 sc->sbm_imr);
2519 #else
2520 __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2521 (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2522 #endif
2523 }
2524
2525 return work_done;
2526 }
2527
2528
2529 static int sbmac_probe(struct platform_device *pldev)
2530 {
2531 struct net_device *dev;
2532 struct sbmac_softc *sc;
2533 void __iomem *sbm_base;
2534 struct resource *res;
2535 u64 sbmac_orig_hwaddr;
2536 int err;
2537
2538 res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
2539 BUG_ON(!res);
2540 sbm_base = ioremap_nocache(res->start, resource_size(res));
2541 if (!sbm_base) {
2542 printk(KERN_ERR "%s: unable to map device registers\n",
2543 dev_name(&pldev->dev));
2544 err = -ENOMEM;
2545 goto out_out;
2546 }
2547
2548
2549
2550
2551
2552
2553 sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2554 pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
2555 sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2556 if (sbmac_orig_hwaddr == 0) {
2557 err = 0;
2558 goto out_unmap;
2559 }
2560
2561
2562
2563
2564 dev = alloc_etherdev(sizeof(struct sbmac_softc));
2565 if (!dev) {
2566 err = -ENOMEM;
2567 goto out_unmap;
2568 }
2569
2570 platform_set_drvdata(pldev, dev);
2571 SET_NETDEV_DEV(dev, &pldev->dev);
2572
2573 sc = netdev_priv(dev);
2574 sc->sbm_base = sbm_base;
2575
2576 err = sbmac_init(pldev, res->start);
2577 if (err)
2578 goto out_kfree;
2579
2580 return 0;
2581
2582 out_kfree:
2583 free_netdev(dev);
2584 __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2585
2586 out_unmap:
2587 iounmap(sbm_base);
2588
2589 out_out:
2590 return err;
2591 }
2592
2593 static int sbmac_remove(struct platform_device *pldev)
2594 {
2595 struct net_device *dev = platform_get_drvdata(pldev);
2596 struct sbmac_softc *sc = netdev_priv(dev);
2597
2598 unregister_netdev(dev);
2599 sbmac_uninitctx(sc);
2600 mdiobus_unregister(sc->mii_bus);
2601 mdiobus_free(sc->mii_bus);
2602 iounmap(sc->sbm_base);
2603 free_netdev(dev);
2604
2605 return 0;
2606 }
2607
2608 static struct platform_driver sbmac_driver = {
2609 .probe = sbmac_probe,
2610 .remove = sbmac_remove,
2611 .driver = {
2612 .name = sbmac_string,
2613 },
2614 };
2615
2616 module_platform_driver(sbmac_driver);
2617 MODULE_LICENSE("GPL");