2 * Intel RS-82543GC Gigabit Ethernet Controller
3 * as found on the Intel PRO/1000[FT] Server Adapter.
4 * The older non-[FT] cards use the 82542 (LSI L2A1157) chip; no attempt
5 * is made to handle the older chip although it should be possible.
6 * The datasheet is not very clear about running on a big-endian system
7 * and this driver assumes little-endian throughout.
14 #include "../port/lib.h"
19 #include "../port/pci.h"
20 #include "../port/error.h"
21 #include "../port/netif.h"
22 #include "../port/etherif.h"
25 Ctrl = 0x00000000, /* Device Control */
26 Status = 0x00000008, /* Device Status */
27 Eecd = 0x00000010, /* EEPROM/Flash Control/Data */
28 Ctrlext = 0x00000018, /* Extended Device Control */
29 Mdic = 0x00000020, /* MDI Control */
30 Fcal = 0x00000028, /* Flow Control Address Low */
31 Fcah = 0x0000002C, /* Flow Control Address High */
32 Fct = 0x00000030, /* Flow Control Type */
33 Icr = 0x000000C0, /* Interrupt Cause Read */
34 Ics = 0x000000C8, /* Interrupt Cause Set */
35 Ims = 0x000000D0, /* Interrupt Mask Set/Read */
36 Imc = 0x000000D8, /* Interrupt mask Clear */
37 Rctl = 0x00000100, /* Receive Control */
38 Fcttv = 0x00000170, /* Flow Control Transmit Timer Value */
39 Txcw = 0x00000178, /* Transmit configuration word reg. */
40 Rxcw = 0x00000180, /* Receive configuration word reg. */
41 Tctl = 0x00000400, /* Transmit Control */
42 Tipg = 0x00000410, /* Transmit IPG */
43 Tbt = 0x00000448, /* Transmit Burst Timer */
44 Ait = 0x00000458, /* Adaptive IFS Throttle */
45 Fcrtl = 0x00002160, /* Flow Control RX Threshold Low */
46 Fcrth = 0x00002168, /* Flow Control Rx Threshold High */
47 Rdfh = 0x00002410, /* Receive data fifo head */
48 Rdft = 0x00002418, /* Receive data fifo tail */
49 Rdfhs = 0x00002420, /* Receive data fifo head saved */
50 Rdfts = 0x00002428, /* Receive data fifo tail saved */
51 Rdfpc = 0x00002430, /* Receive data fifo packet count */
52 Rdbal = 0x00002800, /* Rdesc Base Address Low */
53 Rdbah = 0x00002804, /* Rdesc Base Address High */
54 Rdlen = 0x00002808, /* Receive Descriptor Length */
55 Rdh = 0x00002810, /* Receive Descriptor Head */
56 Rdt = 0x00002818, /* Receive Descriptor Tail */
57 Rdtr = 0x00002820, /* Receive Descriptor Timer Ring */
58 Rxdctl = 0x00002828, /* Receive Descriptor Control */
59 Txdmac = 0x00003000, /* Transfer DMA Control */
60 Ett = 0x00003008, /* Early Transmit Control */
61 Tdfh = 0x00003410, /* Transmit data fifo head */
62 Tdft = 0x00003418, /* Transmit data fifo tail */
63 Tdfhs = 0x00003420, /* Transmit data Fifo Head saved */
64 Tdfts = 0x00003428, /* Transmit data fifo tail saved */
65 Tdfpc = 0x00003430, /* Trasnmit data Fifo packet count */
66 Tdbal = 0x00003800, /* Tdesc Base Address Low */
67 Tdbah = 0x00003804, /* Tdesc Base Address High */
68 Tdlen = 0x00003808, /* Transmit Descriptor Length */
69 Tdh = 0x00003810, /* Transmit Descriptor Head */
70 Tdt = 0x00003818, /* Transmit Descriptor Tail */
71 Tidv = 0x00003820, /* Transmit Interrupt Delay Value */
72 Txdctl = 0x00003828, /* Transmit Descriptor Control */
74 Statistics = 0x00004000, /* Start of Statistics Area */
75 Gorcl = 0x88/4, /* Good Octets Received Count */
76 Gotcl = 0x90/4, /* Good Octets Transmitted Count */
77 Torl = 0xC0/4, /* Total Octets Received */
78 Totl = 0xC8/4, /* Total Octets Transmitted */
81 Rxcsum = 0x00005000, /* Receive Checksum Control */
82 Mta = 0x00005200, /* Multicast Table Array */
83 Ral = 0x00005400, /* Receive Address Low */
84 Rah = 0x00005404, /* Receive Address High */
88 Bem = 0x00000002, /* Big Endian Mode */
89 Prior = 0x00000004, /* Priority on the PCI bus */
90 Lrst = 0x00000008, /* Link Reset */
91 Asde = 0x00000020, /* Auto-Speed Detection Enable */
92 Slu = 0x00000040, /* Set Link Up */
93 Ilos = 0x00000080, /* Invert Loss of Signal (LOS) */
94 Frcspd = 0x00000800, /* Force Speed */
95 Frcdplx = 0x00001000, /* Force Duplex */
96 Swdpinslo = 0x003C0000, /* Software Defined Pins - lo nibble */
100 Swdpin3 = 0x00200000,
101 Swdpiolo = 0x03C00000, /* Software Defined I/O Pins */
102 Swdpio0 = 0x00400000,
103 Swdpio1 = 0x00800000,
104 Swdpio2 = 0x01000000,
105 Swdpio3 = 0x02000000,
106 Devrst = 0x04000000, /* Device Reset */
107 Rfce = 0x08000000, /* Receive Flow Control Enable */
108 Tfce = 0x10000000, /* Transmit Flow Control Enable */
109 Vme = 0x40000000, /* VLAN Mode Enable */
113 Lu = 0x00000002, /* Link Up */
114 Tckok = 0x00000004, /* Transmit clock is running */
115 Rbcok = 0x00000008, /* Receive clock is running */
116 Txoff = 0x00000010, /* Transmission Paused */
117 Tbimode = 0x00000020, /* TBI Mode Indication */
118 SpeedMASK = 0x000000C0,
119 Speed10 = 0x00000000, /* 10Mb/s */
120 Speed100 = 0x00000040, /* 100Mb/s */
121 Speed1000 = 0x00000080, /* 1000Mb/s */
122 Mtxckok = 0x00000400, /* MTX clock is running */
123 Pci66 = 0x00000800, /* PCI Bus speed indication */
124 Bus64 = 0x00001000, /* PCI Bus width indication */
127 enum { /* Ctrl and Status */
128 Fd = 0x00000001, /* Full-Duplex */
129 AsdvMASK = 0x00000300,
130 Asdv10 = 0x00000000, /* 10Mb/s */
131 Asdv100 = 0x00000100, /* 100Mb/s */
132 Asdv1000 = 0x00000200, /* 1000Mb/s */
136 Sk = 0x00000001, /* Clock input to the EEPROM */
137 Cs = 0x00000002, /* Chip Select */
138 Di = 0x00000004, /* Data Input to the EEPROM */
139 Do = 0x00000008, /* Data Output from the EEPROM */
143 Gpien = 0x0000000F, /* General Purpose Interrupt Enables */
144 Swdpinshi = 0x000000F0, /* Software Defined Pins - hi nibble */
145 Swdpiohi = 0x00000F00, /* Software Defined Pins - I or O */
146 Asdchk = 0x00001000, /* ASD Check */
147 Eerst = 0x00002000, /* EEPROM Reset */
148 Ips = 0x00004000, /* Invert Power State */
149 Spdbyps = 0x00008000, /* Speed Select Bypass */
152 enum { /* EEPROM content offsets */
153 Ea = 0x00, /* Ethernet Address */
154 Cf = 0x03, /* Compatibility Field */
155 Pba = 0x08, /* Printed Board Assembly number */
156 Icw1 = 0x0A, /* Initialization Control Word 1 */
157 Sid = 0x0B, /* Subsystem ID */
158 Svid = 0x0C, /* Subsystem Vendor ID */
159 Did = 0x0D, /* Device ID */
160 Vid = 0x0E, /* Vendor ID */
161 Icw2 = 0x0F, /* Initialization Control Word 2 */
165 MDIdMASK = 0x0000FFFF, /* Data */
167 MDIrMASK = 0x001F0000, /* PHY Register Address */
169 MDIpMASK = 0x03E00000, /* PHY Address */
171 MDIwop = 0x04000000, /* Write Operation */
172 MDIrop = 0x08000000, /* Read Operation */
173 MDIready = 0x10000000, /* End of Transaction */
174 MDIie = 0x20000000, /* Interrupt Enable */
175 MDIe = 0x40000000, /* Error */
178 enum { /* Icr, Ics, Ims, Imc */
179 Txdw = 0x00000001, /* Transmit Descriptor Written Back */
180 Txqe = 0x00000002, /* Transmit Queue Empty */
181 Lsc = 0x00000004, /* Link Status Change */
182 Rxseq = 0x00000008, /* Receive Sequence Error */
183 Rxdmt0 = 0x00000010, /* Rdesc Minimum Threshold Reached */
184 Rxo = 0x00000040, /* Receiver Overrun */
185 Rxt0 = 0x00000080, /* Receiver Timer Interrupt */
186 Mdac = 0x00000200, /* MDIO Access Completed */
187 Rxcfg = 0x00000400, /* Receiving /C/ ordered sets */
188 Gpi0 = 0x00000800, /* General Purpose Interrupts */
195 Ane = 0x80000000, /* Autonegotiate enable */
196 Np = 0x00008000, /* Next Page */
197 As = 0x00000100, /* Asymmetric Flow control desired */
198 Ps = 0x00000080, /* Pause supported */
199 Hd = 0x00000040, /* Half duplex supported */
200 TxcwFd = 0x00000020, /* Full Duplex supported */
204 Rxword = 0x0000FFFF, /* Data from auto-negotiation process */
205 Rxnocarrier = 0x04000000, /* Carrier Sense indication */
206 Rxinvalid = 0x08000000, /* Invalid Symbol during configuration */
207 Rxchange = 0x10000000, /* Change to the Rxword indication */
208 Rxconfig = 0x20000000, /* /C/ order set reception indication */
209 Rxsync = 0x40000000, /* Lost bit synchronization indication */
210 Anc = 0x80000000, /* Auto Negotiation Complete */
214 Rrst = 0x00000001, /* Receiver Software Reset */
215 Ren = 0x00000002, /* Receiver Enable */
216 Sbp = 0x00000004, /* Store Bad Packets */
217 Upe = 0x00000008, /* Unicast Promiscuous Enable */
218 Mpe = 0x00000010, /* Multicast Promiscuous Enable */
219 Lpe = 0x00000020, /* Long Packet Reception Enable */
220 LbmMASK = 0x000000C0, /* Loopback Mode */
221 LbmOFF = 0x00000000, /* No Loopback */
222 LbmTBI = 0x00000040, /* TBI Loopback */
223 LbmMII = 0x00000080, /* GMII/MII Loopback */
224 LbmXCVR = 0x000000C0, /* Transceiver Loopback */
225 RdtmsMASK = 0x00000300, /* Rdesc Minimum Threshold Size */
226 RdtmsHALF = 0x00000000, /* Threshold is 1/2 Rdlen */
227 RdtmsQUARTER = 0x00000100, /* Threshold is 1/4 Rdlen */
228 RdtmsEIGHTH = 0x00000200, /* Threshold is 1/8 Rdlen */
229 MoMASK = 0x00003000, /* Multicast Offset */
230 Bam = 0x00008000, /* Broadcast Accept Mode */
231 BsizeMASK = 0x00030000, /* Receive Buffer Size */
232 Bsize2048 = 0x00000000, /* Bsex = 0 */
233 Bsize1024 = 0x00010000, /* Bsex = 0 */
234 Bsize512 = 0x00020000, /* Bsex = 0 */
235 Bsize256 = 0x00030000, /* Bsex = 0 */
236 Bsize16384 = 0x00010000, /* Bsex = 1 */
237 Vfe = 0x00040000, /* VLAN Filter Enable */
238 Cfien = 0x00080000, /* Canonical Form Indicator Enable */
239 Cfi = 0x00100000, /* Canonical Form Indicator value */
240 Dpf = 0x00400000, /* Discard Pause Frames */
241 Pmcf = 0x00800000, /* Pass MAC Control Frames */
242 Bsex = 0x02000000, /* Buffer Size Extension */
243 Secrc = 0x04000000, /* Strip CRC from incoming packet */
247 Trst = 0x00000001, /* Transmitter Software Reset */
248 Ten = 0x00000002, /* Transmit Enable */
249 Psp = 0x00000008, /* Pad Short Packets */
250 CtMASK = 0x00000FF0, /* Collision Threshold */
252 ColdMASK = 0x003FF000, /* Collision Distance */
254 Swxoff = 0x00400000, /* Sofware XOFF Transmission */
255 Pbe = 0x00800000, /* Packet Burst Enable */
256 Rtlc = 0x01000000, /* Re-transmit on Late Collision */
257 Nrtu = 0x02000000, /* No Re-transmit on Underrrun */
260 enum { /* [RT]xdctl */
261 PthreshMASK = 0x0000003F, /* Prefetch Threshold */
263 HthreshMASK = 0x00003F00, /* Host Threshold */
265 WthreshMASK = 0x003F0000, /* Writeback Threshold */
267 Gran = 0x00000000, /* Granularity */
268 RxGran = 0x01000000, /* Granularity */
272 PcssMASK = 0x000000FF, /* Packet Checksum Start */
274 Ipofl = 0x00000100, /* IP Checksum Off-load Enable */
275 Tuofl = 0x00000200, /* TCP/UDP Checksum Off-load Enable */
278 enum { /* Receive Delay Timer Ring */
279 Fpd = 0x80000000, /* Flush partial Descriptor Block */
282 typedef struct Rdesc { /* Receive Descriptor */
291 enum { /* Rdesc status */
292 Rdd = 0x01, /* Descriptor Done */
293 Reop = 0x02, /* End of Packet */
294 Ixsm = 0x04, /* Ignore Checksum Indication */
295 Vp = 0x08, /* Packet is 802.1Q (matched VET) */
296 Tcpcs = 0x20, /* TCP Checksum Calculated on Packet */
297 Ipcs = 0x40, /* IP Checksum Calculated on Packet */
298 Pif = 0x80, /* Passed in-exact filter */
301 enum { /* Rdesc errors */
302 Ce = 0x01, /* CRC Error or Alignment Error */
303 Se = 0x02, /* Symbol Error */
304 Seq = 0x04, /* Sequence Error */
305 Cxe = 0x10, /* Carrier Extension Error */
306 Tcpe = 0x20, /* TCP/UDP Checksum Error */
307 Ipe = 0x40, /* IP Checksum Error */
308 Rxe = 0x80, /* RX Data Error */
311 typedef struct Tdesc { /* Legacy+Normal Transmit Descriptor */
313 uint control; /* varies with descriptor type */
314 uint status; /* varies with descriptor type */
317 enum { /* Tdesc control */
318 CsoMASK = 0x00000F00, /* Checksum Offset */
320 Teop = 0x01000000, /* End of Packet */
321 Ifcs = 0x02000000, /* Insert FCS */
322 Ic = 0x04000000, /* Insert Checksum (Dext == 0) */
323 Tse = 0x04000000, /* TCP Segmentaion Enable (Dext == 1) */
324 Rs = 0x08000000, /* Report Status */
325 Rps = 0x10000000, /* Report Status Sent */
326 Dext = 0x20000000, /* Extension (!legacy) */
327 Vle = 0x40000000, /* VLAN Packet Enable */
328 Ide = 0x80000000, /* Interrupt Delay Enable */
331 enum { /* Tdesc status */
332 Tdd = 0x00000001, /* Descriptor Done */
333 Ec = 0x00000002, /* Excess Collisions */
334 Lc = 0x00000004, /* Late Collision */
335 Tu = 0x00000008, /* Transmit Underrun */
336 CssMASK = 0x0000FF00, /* Checksum Start Field */
341 Nrdesc = 256, /* multiple of 8 */
342 Ntdesc = 256, /* multiple of 8 */
343 Nblocks = 4098, /* total number of blocks to use */
352 typedef struct Ctlr Ctlr;
353 typedef struct Ctlr {
363 int im; /* interrupt mask */
366 uint statistics[Nstatistics];
369 Rdesc* rdba; /* receive descriptor base address */
370 Block* rb[Nrdesc]; /* receive buffers */
371 int rdh; /* receive descriptor head */
372 int rdt; /* receive descriptor tail */
373 Block** freehead; /* points to long or short head */
376 Tdesc* tdba; /* transmit descriptor base address */
377 Block* tb[Ntdesc]; /* transmit buffers */
378 int tdh; /* transmit descriptor head */
379 int tdt; /* transmit descriptor tail */
380 int txstalled; /* count of times unable to send */
386 ulong multimask[128]; /* bit mask for multicast addresses */
389 static Ctlr* gc82543ctlrhead;
390 static Ctlr* gc82543ctlrtail;
392 static Lock freelistlock;
393 static Block* freeShortHead;
394 static Block* freeJumboHead;
396 #define csr32r(c, r) (*((c)->nic+((r)/4)))
397 #define csr32w(c, r, v) (*((c)->nic+((r)/4)) = (v))
399 static void gc82543watchdog(void* arg);
402 gc82543attach(Ether* edev)
411 * adjust queue length depending on speed;
413 * more needed here...
418 unlock(&ctlr->slock);
422 ctl = csr32r(ctlr, Rctl)|Ren;
423 csr32w(ctlr, Rctl, ctl);
424 ctl = csr32r(ctlr, Tctl)|Ten;
425 csr32w(ctlr, Tctl, ctl);
427 csr32w(ctlr, Ims, ctlr->im);
430 unlock(&ctlr->slock);
432 snprint(name, KNAMELEN, "#l%d82543", edev->ctlrno);
433 kproc(name, gc82543watchdog, edev);
436 static char* statistics[Nstatistics] = {
443 "Excessive Collisions",
444 "Multiple Collision",
452 "Carrier Extension Error",
453 "Receive Error Length",
459 "FC Received Unsupported",
460 "Packets Received (64 Bytes)",
461 "Packets Received (65-127 Bytes)",
462 "Packets Received (128-255 Bytes)",
463 "Packets Received (256-511 Bytes)",
464 "Packets Received (512-1023 Bytes)",
465 "Packets Received (1024-1522 Bytes)",
466 "Good Packets Received",
467 "Broadcast Packets Received",
468 "Multicast Packets Received",
469 "Good Packets Transmitted",
471 "Good Octets Received",
473 "Good Octets Transmitted",
477 "Receive No Buffers",
485 "Total Octets Received",
487 "Total Octets Transmitted",
489 "Total Packets Received",
490 "Total Packets Transmitted",
491 "Packets Transmitted (64 Bytes)",
492 "Packets Transmitted (65-127 Bytes)",
493 "Packets Transmitted (128-255 Bytes)",
494 "Packets Transmitted (256-511 Bytes)",
495 "Packets Transmitted (512-1023 Bytes)",
496 "Packets Transmitted (1024-1522 Bytes)",
497 "Multicast Packets Transmitted",
498 "Broadcast Packets Transmitted",
499 "TCP Segmentation Context Transmitted",
500 "TCP Segmentation Context Fail",
504 gc82543ifstat(Ether* edev, void* a, long n, ulong offset)
511 p = smalloc(READSTR);
516 for(i = 0; i < Nstatistics; i++){
517 r = csr32r(ctlr, Statistics+i*4);
518 if((s = statistics[i]) == nil)
526 ruvl += ((uvlong)csr32r(ctlr, Statistics+(i+1)*4))<<32;
528 tuvl += ctlr->statistics[i];
529 tuvl += ((uvlong)ctlr->statistics[i+1])<<32;
532 ctlr->statistics[i] = tuvl;
533 ctlr->statistics[i+1] = tuvl>>32;
534 l += snprint(p+l, READSTR-l, "%s: %llud %llud\n",
540 ctlr->statistics[i] += r;
541 if(ctlr->statistics[i] == 0)
543 l += snprint(p+l, READSTR-l, "%s: %ud %ud\n",
544 s, ctlr->statistics[i], r);
549 l += snprint(p+l, READSTR-l, "eeprom:");
550 for(i = 0; i < 0x40; i++){
551 if(i && ((i & 0x07) == 0))
552 l += snprint(p+l, READSTR-l, "\n ");
553 l += snprint(p+l, READSTR-l, " %4.4uX", ctlr->eeprom[i]);
556 snprint(p+l, READSTR-l, "\ntxstalled %d\n", ctlr->txstalled);
557 n = readstr(offset, a, n, p);
559 unlock(&ctlr->slock);
565 gc82543promiscuous(void* arg, int on)
574 rctl = csr32r(ctlr, Rctl);
575 rctl &= ~MoMASK; /* make sure we're using bits 47:36 */
580 csr32w(ctlr, Rctl, rctl);
584 gc82543multicast(void* arg, uchar* addr, int on)
593 bit = ((addr[5] & 1)<<4)|(addr[4]>>4);
595 ctlr->multimask[x] |= 1<<bit;
597 ctlr->multimask[x] &= ~(1<<bit);
599 csr32w(ctlr, Mta+x*4, ctlr->multimask[x]);
603 gc82543ctl(Ether* edev, void* buf, long n)
615 cb = parsecmd(buf, n);
618 else if(cistrcmp(cb->f[0], "auto") == 0){
619 ctrl = csr32r(ctlr, Ctrl);
620 if(cistrcmp(cb->f[1], "off") == 0){
621 csr32w(ctlr, Txcw, ctlr->txcw & ~Ane);
627 csr32w(ctlr, Ctrl, ctrl);
629 else if(cistrcmp(cb->f[1], "on") == 0){
630 csr32w(ctlr, Txcw, ctlr->txcw);
632 csr32w(ctlr, Ctrl, ctrl);
637 else if(cistrcmp(cb->f[0], "clear") == 0){
638 if(cistrcmp(cb->f[1], "stats") == 0){
639 for(i = 0; i < Nstatistics; i++)
640 ctlr->statistics[i] = 0;
647 unlock(&ctlr->slock);
650 return (r == 0) ? n : r;
654 gc82543txinit(Ctlr* ctlr)
660 tdsize = ROUND(Ntdesc*sizeof(Tdesc), 4096);
662 if(ctlr->tdba == nil)
663 ctlr->tdba = xspanalloc(tdsize, 32, 0);
665 for(i = 0; i < Ntdesc; i++){
672 memset(&ctlr->tdba[i], 0, sizeof(Tdesc));
675 csr32w(ctlr, Tdbal, PCIWADDR(ctlr->tdba));
676 csr32w(ctlr, Tdbah, 0);
677 csr32w(ctlr, Tdlen, Ntdesc*sizeof(Tdesc));
680 * set the ring head and tail pointers.
683 csr32w(ctlr, Tdh, ctlr->tdh);
685 csr32w(ctlr, Tdt, ctlr->tdt);
687 csr32w(ctlr, Tipg, (6<<20)|(8<<10)|6);
688 csr32w(ctlr, Tidv, 128);
689 csr32w(ctlr, Ait, 0);
690 csr32w(ctlr, Txdmac, 0);
691 csr32w(ctlr, Txdctl, Gran|(4<<WthreshSHIFT)|(1<<HthreshSHIFT)|16);
692 csr32w(ctlr, Tctl, (0x0F<<CtSHIFT)|Psp|(6<<ColdSHIFT));
698 gc82543transmit(Ether* edev)
707 ilock(&ctlr->tdlock);
711 * Free any completed packets
713 tdesc = &ctlr->tdba[tdh];
714 if(!(tdesc->status & Tdd))
716 memset(tdesc, 0, sizeof(Tdesc));
717 bpp = &ctlr->tb[tdh];
723 tdh = NEXT(tdh, Ntdesc);
726 s = csr32r(ctlr, Status);
729 * Try to fill the ring back up
730 * but only if link is up and transmission isn't paused.
732 if((s & (Txoff|Lu)) == Lu){
734 while(NEXT(tdt, Ntdesc) != tdh){
735 if((bp = qget(edev->oq)) == nil)
738 tdesc = &ctlr->tdba[tdt];
739 tdesc->addr[0] = PCIWADDR(bp->rp);
740 tdesc->control = Ide|Rs|Ifcs|Teop|BLEN(bp);
742 tdt = NEXT(tdt, Ntdesc);
745 if(tdt != ctlr->tdt){
747 csr32w(ctlr, Tdt, tdt);
753 iunlock(&ctlr->tdlock);
757 gc82543allocb(Ctlr* ctlr)
761 ilock(&freelistlock);
762 if((bp = *(ctlr->freehead)) != nil){
763 *(ctlr->freehead) = bp->next;
766 iunlock(&freelistlock);
771 gc82543replenish(Ctlr* ctlr)
777 ilock(&ctlr->rdlock);
779 while(NEXT(rdt, Nrdesc) != ctlr->rdh){
780 rdesc = &ctlr->rdba[rdt];
781 if(ctlr->rb[rdt] == nil){
782 bp = gc82543allocb(ctlr);
784 iprint("no available buffers\n");
788 rdesc->addr[0] = PCIWADDR(bp->rp);
793 rdt = NEXT(rdt, Nrdesc);
796 csr32w(ctlr, Rdt, rdt);
797 iunlock(&ctlr->rdlock);
801 gc82543rxinit(Ctlr* ctlr)
805 csr32w(ctlr, Rctl, Dpf|Bsize2048|Bam|RdtmsHALF);
808 * Allocate the descriptor ring and load its
809 * address and length into the NIC.
811 rdsize = ROUND(Nrdesc*sizeof(Rdesc), 4096);
812 if(ctlr->rdba == nil)
813 ctlr->rdba = xspanalloc(rdsize, 32, 0);
814 memset(ctlr->rdba, 0, rdsize);
819 csr32w(ctlr, Rdtr, Fpd|64);
820 csr32w(ctlr, Rdbal, PCIWADDR(ctlr->rdba));
821 csr32w(ctlr, Rdbah, 0);
822 csr32w(ctlr, Rdlen, Nrdesc*sizeof(Rdesc));
823 csr32w(ctlr, Rdh, 0);
824 csr32w(ctlr, Rdt, 0);
825 for(i = 0; i < Nrdesc; i++){
826 if(ctlr->rb[i] != nil){
831 gc82543replenish(ctlr);
833 csr32w(ctlr, Rxdctl, RxGran|(8<<WthreshSHIFT)|(4<<HthreshSHIFT)|1);
834 ctlr->im |= Rxt0|Rxo|Rxdmt0|Rxseq;
838 gc82543recv(Ether* edev, int icr)
849 rdesc = &ctlr->rdba[rdh];
851 if(!(rdesc->status & Rdd))
854 if((rdesc->status & Reop) && rdesc->errors == 0){
857 bp->wp += rdesc->length;
862 if(ctlr->rb[rdh] != nil){
863 /* either non eop packet, or error */
864 freeb(ctlr->rb[rdh]);
867 memset(rdesc, 0, sizeof(Rdesc));
869 rdh = NEXT(rdh, Nrdesc);
874 gc82543replenish(ctlr);
878 freegc82543short(Block *bp)
880 ilock(&freelistlock);
881 /* reset read/write pointer to proper positions */
882 bp->rp = bp->lim - ROUND(SBLOCKSIZE, BLOCKALIGN);
884 bp->next = freeShortHead;
886 iunlock(&freelistlock);
890 freegc82532jumbo(Block *bp)
892 ilock(&freelistlock);
893 /* reset read/write pointer to proper positions */
894 bp->rp = bp->lim - ROUND(JBLOCKSIZE, BLOCKALIGN);
896 bp->next = freeJumboHead;
898 iunlock(&freelistlock);
906 ctrl = csr32r(ctlr, Ctrl);
908 if((ctrl & Swdpin1) ||
909 ((csr32r(ctlr, Rxcw) & Rxconfig) && !(csr32r(ctlr, Txcw) & Ane))){
910 csr32w(ctlr, Txcw, ctlr->txcw);
911 ctrl &= ~(Slu|Fd|Frcdplx);
912 csr32w(ctlr, Ctrl, ctrl);
917 gc82543interrupt(Ureg*, void* arg)
926 while((icr = csr32r(ctlr, Icr) & ctlr->im) != 0){
928 * Link status changed.
930 if(icr & (Lsc|Rxseq))
934 * Process recv buffers.
936 gc82543recv(edev, icr);
939 * Refill transmit ring and free packets.
941 gc82543transmit(edev);
946 gc82543init(Ether* edev)
955 * Allocate private buffer pool to use for receiving packets.
957 ilock(&freelistlock);
958 if (ctlr->freehead == nil){
959 for(i = 0; i < Nblocks; i++){
960 bp = iallocb(SBLOCKSIZE);
962 bp->next = freeShortHead;
963 bp->free = freegc82543short;
967 print("82543gc: no memory\n");
971 ctlr->freehead = &freeShortHead;
973 iunlock(&freelistlock);
976 * Set up the receive addresses.
977 * There are 16 addresses. The first should be the MAC address.
978 * The others are cleared and not marked valid (MS bit of Rah).
980 csr = (edev->ea[3]<<24)|(edev->ea[2]<<16)|(edev->ea[1]<<8)|edev->ea[0];
981 csr32w(ctlr, Ral, csr);
982 csr = 0x80000000|(edev->ea[5]<<8)|edev->ea[4];
983 csr32w(ctlr, Rah, csr);
984 for(i = 1; i < 16; i++){
985 csr32w(ctlr, Ral+i*8, 0);
986 csr32w(ctlr, Rah+i*8, 0);
990 * Clear the Multicast Table Array.
991 * It's a 4096 bit vector accessed as 128 32-bit registers.
993 for(i = 0; i < 128; i++)
994 csr32w(ctlr, Mta+i*4, 0);
1003 at93c46io(Ctlr* ctlr, char* op, int data)
1006 int i, loop, eecd, r;
1008 eecd = csr32r(ctlr, Eecd);
1013 for(p = op; *p != '\0'; p++){
1019 case ':': /* start of loop */
1021 if(p != (lp+1) || loop != 7)
1030 case ';': /* end of loop */
1039 case 'C': /* assert clock */
1042 case 'c': /* deassert clock */
1045 case 'D': /* next bit in 'data' byte */
1048 if(data & (1<<loop))
1053 case 'O': /* collect data output */
1054 i = (csr32r(ctlr, Eecd) & Do) != 0;
1060 case 'I': /* assert data input */
1063 case 'i': /* deassert data input */
1066 case 'S': /* enable chip select */
1069 case 's': /* disable chip select */
1073 csr32w(ctlr, Eecd, eecd);
1082 at93c46r(Ctlr* ctlr)
1088 for(addr = 0; addr < 0x40; addr++){
1090 * Read a word at address 'addr' from the Atmel AT93C46
1091 * 3-Wire Serial EEPROM or compatible. The EEPROM access is
1092 * controlled by 4 bits in Eecd. See the AT93C46 datasheet
1093 * for protocol details.
1095 if(at93c46io(ctlr, "S ICc :DCc;", (0x02<<6)|addr) != 0)
1097 data = at93c46io(ctlr, "::COc;", 0);
1098 at93c46io(ctlr, "sic", 0);
1099 ctlr->eeprom[addr] = data;
1107 gc82543detach(Ctlr* ctlr)
1110 * Perform a device reset to get the chip back to the
1111 * power-on state, followed by an EEPROM reset to read
1112 * the defaults for some internal registers.
1114 csr32w(ctlr, Imc, ~0);
1115 csr32w(ctlr, Rctl, 0);
1116 csr32w(ctlr, Tctl, 0);
1120 csr32w(ctlr, Ctrl, Devrst);
1121 while(csr32r(ctlr, Ctrl) & Devrst)
1124 csr32w(ctlr, Ctrlext, Eerst);
1125 while(csr32r(ctlr, Ctrlext) & Eerst)
1128 csr32w(ctlr, Imc, ~0);
1129 while(csr32r(ctlr, Icr))
1134 gc82543checklink(Ctlr* ctlr)
1136 int ctrl, status, rxcw;
1138 ctrl = csr32r(ctlr, Ctrl);
1139 status = csr32r(ctlr, Status);
1140 rxcw = csr32r(ctlr, Rxcw);
1143 if(!(ctrl & (Swdpin1|Slu)) && !(rxcw & Rxconfig)){
1144 csr32w(ctlr, Txcw, ctlr->txcw & ~Ane);
1150 csr32w(ctlr, Ctrl, ctrl);
1153 else if((ctrl & Slu) && (rxcw & Rxconfig)){
1154 csr32w(ctlr, Txcw, ctlr->txcw);
1156 csr32w(ctlr, Ctrl, ctrl);
1161 gc82543shutdown(Ether* ether)
1163 gc82543detach(ether->ctlr);
1167 gc82543reset(Ctlr* ctlr)
1173 * Read the EEPROM, validate the checksum
1174 * then get the device back to a power-on state.
1176 if(at93c46r(ctlr) != 0xBABA)
1179 gc82543detach(ctlr);
1181 te = ctlr->eeprom[Icw2];
1182 if((te & 0x3000) == 0){
1183 ctlr->fcrtl = 0x00002000;
1184 ctlr->fcrth = 0x00004000;
1185 ctlr->txcw = Ane|TxcwFd;
1187 else if((te & 0x3000) == 0x2000){
1190 ctlr->txcw = Ane|TxcwFd|As;
1193 ctlr->fcrtl = 0x00002000;
1194 ctlr->fcrth = 0x00004000;
1195 ctlr->txcw = Ane|TxcwFd|As|Ps;
1198 csr32w(ctlr, Txcw, ctlr->txcw);
1200 csr32w(ctlr, Ctrlext, (te & 0x00f0)<<4);
1202 csr32w(ctlr, Tctl, csr32r(ctlr, Tctl)|(64<<ColdSHIFT));
1204 te = ctlr->eeprom[Icw1];
1205 ctl = ((te & 0x01E0)<<17)|(te & 0x0010)<<3;
1206 csr32w(ctlr, Ctrl, ctl);
1211 * Flow control - values from the datasheet.
1213 csr32w(ctlr, Fcal, 0x00C28001);
1214 csr32w(ctlr, Fcah, 0x00000100);
1215 csr32w(ctlr, Fct, 0x00008808);
1216 csr32w(ctlr, Fcttv, 0x00000100);
1218 csr32w(ctlr, Fcrtl, ctlr->fcrtl);
1219 csr32w(ctlr, Fcrth, ctlr->fcrth);
1222 gc82543checklink(ctlr);
1228 gc82543watchdog(void* arg)
1237 tsleep(&up->sleep, return0, 0, 1000);
1242 gc82543checklink(ctlr);
1243 gc82543replenish(ctlr);
1245 print("%s: exiting\n", up->text);
1246 pexit("disabled", 1);
1259 while(p = pcimatch(p, 0, 0)){
1260 if(p->ccrb != 0x02 || p->ccru != 0)
1262 if(p->mem[0].bar & 1)
1265 switch((p->did<<16)|p->vid){
1266 case (0x1000<<16)|0x8086: /* LSI L2A1157 (82542) */
1267 case (0x1004<<16)|0x8086: /* Intel PRO/1000 T */
1268 case (0x1008<<16)|0x8086: /* Intel PRO/1000 XT */
1271 case (0x1001<<16)|0x8086: /* Intel PRO/1000 F */
1275 ctlr = malloc(sizeof(Ctlr));
1277 print("82543gc: can't allocate memory\n");
1280 mem = vmap(p->mem[0].bar & ~0x0F, p->mem[0].size);
1282 print("82543gc: can't map %8.8luX\n", p->mem[0].bar);
1291 print("82543gc: p->cls %#ux, setting to 0x10\n", p->cls);
1293 pcicfgw8(p, PciCLS, p->cls);
1295 ctlr->port = p->mem[0].bar & ~0x0F;
1297 ctlr->id = (p->did<<16)|p->vid;
1300 if(gc82543ctlrhead != nil)
1301 gc82543ctlrtail->next = ctlr;
1303 gc82543ctlrhead = ctlr;
1304 gc82543ctlrtail = ctlr;
1309 gc82543pnp(Ether* edev)
1315 if(gc82543ctlrhead == nil)
1319 * Any adapter matches if no edev->port is supplied,
1320 * otherwise the ports must match.
1322 for(ctlr = gc82543ctlrhead; ctlr != nil; ctlr = ctlr->next){
1325 if(edev->port == 0 || edev->port == ctlr->port){
1333 pcienable(ctlr->pcidev);
1337 edev->port = ctlr->port;
1338 edev->irq = ctlr->pcidev->intl;
1339 edev->tbdf = ctlr->pcidev->tbdf;
1343 * Check if the adapter's station address is to be overridden.
1344 * If not, read it from the EEPROM and set in ether->ea prior to
1345 * loading the station address in the hardware.
1347 memset(ea, 0, Eaddrlen);
1348 if(memcmp(ea, edev->ea, Eaddrlen) == 0){
1349 for(i = Ea; i < Eaddrlen/2; i++){
1350 edev->ea[2*i] = ctlr->eeprom[i];
1351 edev->ea[2*i+1] = ctlr->eeprom[i]>>8;
1355 pcisetbme(ctlr->pcidev);
1358 * Linkage to the generic ethernet driver.
1360 edev->attach = gc82543attach;
1361 edev->transmit = gc82543transmit;
1362 edev->interrupt = gc82543interrupt;
1363 edev->ifstat = gc82543ifstat;
1364 edev->shutdown = gc82543shutdown;
1365 edev->ctl = gc82543ctl;
1367 edev->promiscuous = gc82543promiscuous;
1368 edev->multicast = gc82543multicast;
1374 ether82543gclink(void)
1376 addethercard("82543GC", gc82543pnp);