2 * Realtek RTL8110/8168/8169 Gigabit Ethernet Controllers.
3 * There are some magic register values used which are not described in
4 * any datasheet or driver but seem to be necessary.
5 * There are slight differences between the chips in the series so some
6 * tweaks may be needed.
8 * we use l1 and l2 cache ops; data must reach ram for dma.
11 #include "../port/lib.h"
16 #include "../port/pci.h"
17 #include "../port/error.h"
18 #include "../port/netif.h"
19 #include "../port/etherif.h"
20 #include "../port/ethermii.h"
22 typedef struct Ctlr Ctlr;
23 typedef struct D D; /* Transmit/Receive Descriptor */
24 typedef struct Dtcc Dtcc;
27 Debug = 0, /* beware: > 1 interferes with correct operation */
30 enum { /* registers */
31 Idr0 = 0x00, /* MAC address */
32 Mar0 = 0x08, /* Multicast address */
33 Dtccr = 0x10, /* Dump Tally Counter Command */
34 Tnpds = 0x20, /* Transmit Normal Priority Descriptors */
35 Thpds = 0x28, /* Transmit High Priority Descriptors */
36 Flash = 0x30, /* Flash Memory Read/Write */
37 Erbcr = 0x34, /* Early Receive Byte Count */
38 Ersr = 0x36, /* Early Receive Status */
39 Cr = 0x37, /* Command Register */
40 Tppoll = 0x38, /* Transmit Priority Polling */
41 Imr = 0x3C, /* Interrupt Mask */
42 Isr = 0x3E, /* Interrupt Status */
43 Tcr = 0x40, /* Transmit Configuration */
44 Rcr = 0x44, /* Receive Configuration */
45 Tctr = 0x48, /* Timer Count */
46 Mpc = 0x4C, /* Missed Packet Counter */
47 Cr9346 = 0x50, /* 9346 Command Register */
48 Config0 = 0x51, /* Configuration Register 0 */
49 Config1 = 0x52, /* Configuration Register 1 */
50 Config2 = 0x53, /* Configuration Register 2 */
51 Config3 = 0x54, /* Configuration Register 3 */
52 Config4 = 0x55, /* Configuration Register 4 */
53 Config5 = 0x56, /* Configuration Register 5 */
54 Timerint = 0x58, /* Timer Interrupt */
55 Mulint = 0x5C, /* Multiple Interrupt Select */
56 Phyar = 0x60, /* PHY Access */
57 Tbicsr0 = 0x64, /* TBI Control and Status */
58 Tbianar = 0x68, /* TBI Auto-Negotiation Advertisment */
59 Tbilpar = 0x6A, /* TBI Auto-Negotiation Link Partner */
60 Phystatus = 0x6C, /* PHY Status */
62 Rms = 0xDA, /* Receive Packet Maximum Size */
63 Cplusc = 0xE0, /* C+ Command */
64 Coal = 0xE2, /* Interrupt Mitigation (Coalesce) */
65 Rdsar = 0xE4, /* Receive Descriptor Start Address */
66 Etx = 0xEC, /* 8169: Early Tx Threshold; 32-byte units */
67 Mtps = 0xEC, /* 8168: Maximum Transmit Packet Size */
71 Cmd = 0x00000008, /* Command */
75 Te = 0x04, /* Transmitter Enable */
76 Re = 0x08, /* Receiver Enable */
77 Rst = 0x10, /* Software Reset */
81 Fswint = 0x01, /* Forced Software Interrupt */
82 Npq = 0x40, /* Normal Priority Queue polling */
83 Hpq = 0x80, /* High Priority Queue polling */
87 Rok = 0x0001, /* Receive OK */
88 Rer = 0x0002, /* Receive Error */
89 Tok = 0x0004, /* Transmit OK */
90 Ter = 0x0008, /* Transmit Error */
91 Rdu = 0x0010, /* Receive Descriptor Unavailable */
92 Punlc = 0x0020, /* Packet Underrun or Link Change */
93 Fovw = 0x0040, /* Receive FIFO Overflow */
94 Tdu = 0x0080, /* Transmit Descriptor Unavailable */
95 Swint = 0x0100, /* Software Interrupt */
96 Timeout = 0x4000, /* Timer */
97 Serr = 0x8000, /* System Error */
101 MtxdmaSHIFT = 8, /* Max. DMA Burst Size */
102 MtxdmaMASK = 0x00000700,
103 Mtxdmaunlimited = 0x00000700,
104 Acrc = 0x00010000, /* Append CRC (not) */
105 Lbk0 = 0x00020000, /* Loopback Test 0 */
106 Lbk1 = 0x00040000, /* Loopback Test 1 */
107 Ifg2 = 0x00080000, /* Interframe Gap 2 */
108 HwveridSHIFT = 23, /* Hardware Version ID */
109 HwveridMASK = 0x7C800000,
110 Macv01 = 0x00000000, /* RTL8169 */
111 Macv02 = 0x00800000, /* RTL8169S/8110S */
112 Macv03 = 0x04000000, /* RTL8169S/8110S */
113 Macv04 = 0x10000000, /* RTL8169SB/8110SB */
114 Macv05 = 0x18000000, /* RTL8169SC/8110SC */
115 Macv07 = 0x24800000, /* RTL8102e */
116 // Macv8103e = 0x24C00000,
117 Macv25 = 0x28000000, /* RTL8168D */
118 // Macv8168dp = 0x28800000,
119 // Macv8168e = 0x2C000000,
120 Macv11 = 0x30000000, /* RTL8168B/8111B */
121 Macv14 = 0x30800000, /* RTL8100E */
122 Macv13 = 0x34000000, /* RTL8101E */
123 Macv07a = 0x34800000, /* RTL8102e */
124 Macv12 = 0x38000000, /* RTL8169B/8111B */
125 // Macv8168spin3 = 0x38400000,
126 Macv15 = 0x38800000, /* RTL8100E */
127 Macv12a = 0x3c000000, /* RTL8169C/8111C */
128 // Macv19 = 0x3c000000, /* dup Macv12a: RTL8111c-gr */
129 // Macv8168cspin2 = 0x3c400000,
130 // Macv8168cp = 0x3c800000,
131 // Macv8139 = 0x60000000,
132 // Macv8139a = 0x70000000,
133 // Macv8139ag = 0x70800000,
134 // Macv8139b = 0x78000000,
135 // Macv8130 = 0x7C000000,
136 // Macv8139c = 0x74000000,
137 // Macv8139d = 0x74400000,
138 // Macv8139cplus = 0x74800000,
139 // Macv8101 = 0x74c00000,
140 // Macv8100 = 0x78800000,
141 // Macv8169_8110sbl= 0x7cc00000,
142 // Macv8169_8110sce= 0x98000000,
143 Ifg0 = 0x01000000, /* Interframe Gap 0 */
144 Ifg1 = 0x02000000, /* Interframe Gap 1 */
148 Aap = 0x00000001, /* Accept All Packets */
149 Apm = 0x00000002, /* Accept Physical Match */
150 Am = 0x00000004, /* Accept Multicast */
151 Ab = 0x00000008, /* Accept Broadcast */
152 Ar = 0x00000010, /* Accept Runt */
153 Aer = 0x00000020, /* Accept Error */
154 Sel9356 = 0x00000040, /* 9356 EEPROM used */
155 MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
156 MrxdmaMASK = 0x00000700,
157 Mrxdmaunlimited = 0x00000700,
158 RxfthSHIFT = 13, /* Receive Buffer Length */
159 RxfthMASK = 0x0000E000,
160 Rxfth256 = 0x00008000,
161 Rxfthnone = 0x0000E000,
162 Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
163 MulERINT = 0x01000000, /* Multiple Early Interrupt Select */
171 Eem0 = 0x40, /* Operating Mode */
176 DataMASK = 0x0000FFFF, /* 16-bit GMII/MII Register Data */
178 RegaddrMASK = 0x001F0000, /* 5-bit GMII/MII Register Address */
180 Flag = 0x80000000, /* */
183 enum { /* Phystatus */
184 Fd = 0x01, /* Full Duplex */
185 Linksts = 0x02, /* Link Status */
186 Speed10 = 0x04, /* */
187 Speed100 = 0x08, /* */
188 Speed1000 = 0x10, /* */
195 Init1 = 0x0001, /* 8168 */
196 Mulrw = 0x0008, /* PCI Multiple R/W Enable */
197 Dac = 0x0010, /* PCI Dual Address Cycle Enable */
198 Rxchksum = 0x0020, /* Receive Checksum Offload Enable */
199 Rxvlan = 0x0040, /* Receive VLAN De-tagging Enable */
200 Pktcntoff = 0x0080, /* 8168, 8101 */
201 Endian = 0x0200, /* Endian Mode */
211 enum { /* Transmit Descriptor control */
212 TxflMASK = 0x0000FFFF, /* Transmit Frame Length */
214 Tcps = 0x00010000, /* TCP Checksum Offload */
215 Udpcs = 0x00020000, /* UDP Checksum Offload */
216 Ipcs = 0x00040000, /* IP Checksum Offload */
217 Lgsen = 0x08000000, /* TSO; WARNING: contains lark's vomit */
220 enum { /* Receive Descriptor control */
221 RxflMASK = 0x00001FFF, /* Receive Frame Length */
222 Tcpf = 0x00004000, /* TCP Checksum Failure */
223 Udpf = 0x00008000, /* UDP Checksum Failure */
224 Ipf = 0x00010000, /* IP Checksum Failure */
225 Pid0 = 0x00020000, /* Protocol ID0 */
226 Pid1 = 0x00040000, /* Protocol ID1 */
227 Crce = 0x00080000, /* CRC Error */
228 Runt = 0x00100000, /* Runt Packet */
229 Res = 0x00200000, /* Receive Error Summary */
230 Rwt = 0x00400000, /* Receive Watchdog Timer Expired */
231 Fovf = 0x00800000, /* FIFO Overflow */
232 Bovf = 0x01000000, /* Buffer Overflow */
233 Bar = 0x02000000, /* Broadcast Address Received */
234 Pam = 0x04000000, /* Physical Address Matched */
235 Mar = 0x08000000, /* Multicast Address Received */
238 enum { /* General Descriptor control */
239 Ls = 0x10000000, /* Last Segment Descriptor */
240 Fs = 0x20000000, /* First Segment Descriptor */
241 Eor = 0x40000000, /* End of Descriptor Ring */
242 Own = 0x80000000, /* Ownership: belongs to hw */
247 enum { /* Ring sizes (<= 1024) */
248 Ntd = 1024, /* Transmit Ring */
249 /* at 1Gb/s, it only takes 12 ms. to fill a 1024-buffer ring */
250 Nrd = 1024, /* Receive Ring */
254 Mps = ROUNDUP(ETHERMAXTU+4, 128),
255 // Mps = Mtu + 8 + 14, /* if(mtu>ETHERMAXTU) */
274 enum { /* Variants */
275 Rtl8100e = (0x8136<<16)|0x10EC, /* RTL810[01]E: pci -e */
276 Rtl8169c = (0x0116<<16)|0x16EC, /* RTL8169C+ (USR997902) */
277 Rtl8169sc = (0x8167<<16)|0x10EC, /* RTL8169SC */
278 Rtl8168b = (0x8168<<16)|0x10EC, /* RTL8168B: pci-e */
279 Rtl8169 = (0x8169<<16)|0x10EC, /* RTL8169 */
281 * trimslice is 10ec/8168 (8168b) Macv25 (8168D) but
282 * compulab says 8111dl.
283 * oui 0x732 (aaeon) phyno 1, macv = 0x28000000 phyv = 0x0002
292 Ether* ether; /* point back */
295 QLock alock; /* attach */
296 Lock ilock; /* init */
300 int macv; /* MAC version */
301 int phyv; /* PHY version */
302 int pcie; /* flag: pci-express device? */
304 uvlong mchash; /* multicast hash */
308 // Lock tlock; /* transmit */
310 D* td; /* descriptor ring */
311 Block** tb; /* transmit buffers */
314 int tdh; /* head - producer index (host) */
315 int tdt; /* tail - consumer index (NIC) */
321 // Lock rlock; /* receive */
323 D* rd; /* descriptor ring */
324 Block** rb; /* receive buffers */
327 int rdh; /* head - producer index (NIC) */
328 int rdt; /* tail - consumer index (host) */
332 int tcr; /* transmit configuration register */
333 int rcr; /* receive configuration register */
335 int isr; /* sw copy for kprocs */
337 QLock slock; /* statistics */
350 uint frag; /* partial packets; rb was too small */
353 static Ctlr* rtl8169ctlrhead;
354 static Ctlr* rtl8169ctlrtail;
356 static Lock rblock; /* free receive Blocks */
357 static Block* rbpool;
359 #define csr8r(c, r) (*((uchar *) ((c)->nic)+(r)))
360 #define csr16r(c, r) (*((u16int *)((c)->nic)+((r)/2)))
361 #define csr32p(c, r) ((u32int *) ((c)->nic)+((r)/4))
362 #define csr32r(c, r) (*csr32p(c, r))
364 #define csr8w(c, r, b) (*((uchar *) ((c)->nic)+(r)) = (b), coherence())
365 #define csr16w(c, r, w) (*((u16int *)((c)->nic)+((r)/2)) = (w), coherence())
366 #define csr32w(c, r, v) (*csr32p(c, r) = (v), coherence())
369 rtl8169miimir(Mii* mii, int pa, int ra)
378 r = (ra<<16) & RegaddrMASK;
379 csr32w(ctlr, Phyar, r);
381 for(timeo = 0; timeo < 2000; timeo++){
382 if((r = csr32r(ctlr, Phyar)) & Flag)
389 return (r & DataMASK)>>DataSHIFT;
393 rtl8169miimiw(Mii* mii, int pa, int ra, int data)
402 r = Flag|((ra<<16) & RegaddrMASK)|((data<<DataSHIFT) & DataMASK);
403 csr32w(ctlr, Phyar, r);
405 for(timeo = 0; timeo < 2000; timeo++){
406 if(!((r = csr32r(ctlr, Phyar)) & Flag))
417 rtl8169mii(Ctlr* ctlr)
424 if((ctlr->mii = malloc(sizeof(Mii))) == nil)
426 ctlr->mii->mir = rtl8169miimir;
427 ctlr->mii->miw = rtl8169miimiw;
428 ctlr->mii->ctlr = ctlr;
431 * Get rev number out of Phyidr2 so can config properly.
432 * There's probably more special stuff for Macv0[234] needed here.
434 ilock(&ctlr->reglock);
435 ctlr->phyv = rtl8169miimir(ctlr->mii, 1, Phyidr2) & 0x0F;
436 if(ctlr->macv == Macv02){
437 csr8w(ctlr, 0x82, 1); /* magic */
438 rtl8169miimiw(ctlr->mii, 1, 0x0B, 0x0000); /* magic */
441 if(mii(ctlr->mii, (1<<1)) == 0 || (phy = ctlr->mii->curphy) == nil){
442 iunlock(&ctlr->reglock);
447 print("rtl8169: oui %#ux phyno %d, macv = %#8.8ux phyv = %#4.4ux\n",
448 phy->oui, phy->phyno, ctlr->macv, ctlr->phyv);
450 miiane(ctlr->mii, ~0, ~0, ~0);
451 iunlock(&ctlr->reglock);
462 if((bp = rbpool) != nil){
473 bp->wp = bp->rp = bp->lim - Mps;
474 bp->flag &= ~(Bipck | Budpck | Btcpck | Bpktck);
483 rtl8169promiscuous(void* arg, int on)
491 ilock(&ctlr->reglock);
497 csr32w(ctlr, Rcr, ctlr->rcr);
498 iunlock(&ctlr->reglock);
499 iunlock(&ctlr->ilock);
503 /* everyone else uses 0x04c11db7, but they both produce the same crc */
504 Etherpolybe = 0x04c11db6,
505 Bytemask = (1<<8) - 1,
509 ethercrcbe(uchar *addr, long len)
515 for (i = 0; i < len; i++) {
517 for (j = 0; j < 8; j++) {
518 carry = ((crc & (1UL << 31))? 1: 0) ^ (c & 1);
522 crc = (crc ^ Etherpolybe) | carry;
531 return l>>24 | (l>>8) & (Bytemask<<8) |
532 (l<<8) & (Bytemask<<16) | l<<24;
536 rtl8169multicast(void* ether, uchar *eaddr, int add)
542 return; /* ok to keep receiving on old mcast addrs */
547 ilock(&ctlr->reglock);
549 ctlr->mchash |= 1ULL << (ethercrcbe(eaddr, Eaddrlen) >> 26);
552 csr32w(ctlr, Rcr, ctlr->rcr);
554 /* pci-e variants reverse the order of the hash byte registers */
556 csr32w(ctlr, Mar0, swabl(ctlr->mchash>>32));
557 csr32w(ctlr, Mar0+4, swabl(ctlr->mchash));
559 csr32w(ctlr, Mar0, ctlr->mchash);
560 csr32w(ctlr, Mar0+4, ctlr->mchash>>32);
563 iunlock(&ctlr->reglock);
564 iunlock(&ctlr->ilock);
568 rtl8169ifstat(Ether* edev, void* a, long n, ulong offset)
580 qunlock(&ctlr->slock);
585 /* copy hw statistics into ctlr->dtcc */
587 allcache->invse(dtcc, sizeof *dtcc);
588 ilock(&ctlr->reglock);
589 csr32w(ctlr, Dtccr+4, 0);
590 csr32w(ctlr, Dtccr, PCIWADDR(dtcc)|Cmd); /* initiate dma? */
591 for(timeo = 0; timeo < 1000; timeo++){
592 if(!(csr32r(ctlr, Dtccr) & Cmd))
596 iunlock(&ctlr->reglock);
597 if(csr32r(ctlr, Dtccr) & Cmd)
600 edev->oerrs = dtcc->txer;
601 edev->crcs = dtcc->rxer;
602 edev->frames = dtcc->fae;
603 edev->buffs = dtcc->misspkt;
604 edev->overflows = ctlr->txdu + ctlr->rdu;
607 qunlock(&ctlr->slock);
612 if((p = malloc(READSTR)) == nil)
615 l = snprint(p, READSTR, "TxOk: %llud\n", dtcc->txok);
616 l += snprint(p+l, READSTR-l, "RxOk: %llud\n", dtcc->rxok);
617 l += snprint(p+l, READSTR-l, "TxEr: %llud\n", dtcc->txer);
618 l += snprint(p+l, READSTR-l, "RxEr: %ud\n", dtcc->rxer);
619 l += snprint(p+l, READSTR-l, "MissPkt: %ud\n", dtcc->misspkt);
620 l += snprint(p+l, READSTR-l, "FAE: %ud\n", dtcc->fae);
621 l += snprint(p+l, READSTR-l, "Tx1Col: %ud\n", dtcc->tx1col);
622 l += snprint(p+l, READSTR-l, "TxMCol: %ud\n", dtcc->txmcol);
623 l += snprint(p+l, READSTR-l, "RxOkPh: %llud\n", dtcc->rxokph);
624 l += snprint(p+l, READSTR-l, "RxOkBrd: %llud\n", dtcc->rxokbrd);
625 l += snprint(p+l, READSTR-l, "RxOkMu: %ud\n", dtcc->rxokmu);
626 l += snprint(p+l, READSTR-l, "TxAbt: %ud\n", dtcc->txabt);
627 l += snprint(p+l, READSTR-l, "TxUndrn: %ud\n", dtcc->txundrn);
629 l += snprint(p+l, READSTR-l, "txdu: %ud\n", ctlr->txdu);
630 l += snprint(p+l, READSTR-l, "tcpf: %ud\n", ctlr->tcpf);
631 l += snprint(p+l, READSTR-l, "udpf: %ud\n", ctlr->udpf);
632 l += snprint(p+l, READSTR-l, "ipf: %ud\n", ctlr->ipf);
633 l += snprint(p+l, READSTR-l, "fovf: %ud\n", ctlr->fovf);
634 l += snprint(p+l, READSTR-l, "ierrs: %ud\n", ctlr->ierrs);
635 l += snprint(p+l, READSTR-l, "rer: %ud\n", ctlr->rer);
636 l += snprint(p+l, READSTR-l, "rdu: %ud\n", ctlr->rdu);
637 l += snprint(p+l, READSTR-l, "punlc: %ud\n", ctlr->punlc);
638 l += snprint(p+l, READSTR-l, "fovw: %ud\n", ctlr->fovw);
640 l += snprint(p+l, READSTR-l, "tcr: %#8.8ux\n", ctlr->tcr);
641 l += snprint(p+l, READSTR-l, "rcr: %#8.8ux\n", ctlr->rcr);
642 l += snprint(p+l, READSTR-l, "multicast: %ud\n", ctlr->mcast);
644 if(ctlr->mii != nil && ctlr->mii->curphy != nil){
645 l += snprint(p+l, READSTR, "phy: ");
646 for(i = 0; i < NMiiPhyr; i++){
647 if(i && ((i & 0x07) == 0))
648 l += snprint(p+l, READSTR-l, "\n ");
649 r = miimir(ctlr->mii, i);
650 l += snprint(p+l, READSTR-l, " %4.4ux", r);
652 snprint(p+l, READSTR-l, "\n");
655 n = readstr(offset, a, n, p);
657 qunlock(&ctlr->slock);
665 rtl8169halt(Ctlr* ctlr)
667 ilock(&ctlr->reglock);
668 csr32w(ctlr, Timerint, 0);
670 csr16w(ctlr, Imr, 0);
671 csr16w(ctlr, Isr, ~0);
672 iunlock(&ctlr->reglock);
676 rtl8169reset(Ctlr* ctlr)
682 * Soft reset the controller.
684 ilock(&ctlr->reglock);
685 csr8w(ctlr, Cr, Rst);
686 for(r = timeo = 0; timeo < 1000; timeo++){
692 iunlock(&ctlr->reglock);
702 rtl8169shutdown(Ether *ether)
704 rtl8169reset(ether->ctlr);
708 rtl8169replenish(Ether *edev)
716 if (ctlr->nrd == 0) {
717 iprint("rtl8169replenish: not yet initialised\n");
723 while(NEXT(rdt, ctlr->nrd) != ctlr->rdh){
726 panic("rtl8169replenish: nil ctlr->rd[%d]", rdt);
727 if (d->control & Own) { /* ctlr owns it? shouldn't happen */
728 iprint("replenish: descriptor owned by hw\n");
731 if(ctlr->rb[rdt] == nil){
734 iprint("rtl8169: no available buffers\n");
740 d->addrlo = PCIWADDR(bp->rp);
743 iprint("8169: replenish: rx overrun\n");
744 d->control = (d->control & ~RxflMASK) | Mps | Own;
747 rdt = NEXT(rdt, ctlr->nrd);
756 ckrderrs(Ctlr *ctlr, Block *bp, ulong control)
763 switch(control & (Pid1|Pid0)){
766 iprint("8169: bad tcp checksum\n");
774 iprint("8169: bad udp checksum\n");
782 iprint("8169: bad ip checksum\n");
792 badpkt(Ether *edev, int rdh, ulong control)
797 /* Res is only valid if Fs is set */
799 iprint("8169: rcv error; d->control %#.8lux\n", control);
800 else if (control == 0) { /* buggered? */
802 iprint("8169: rcv: d->control==0 (wtf?)\n");
805 iprint("8169: rcv'd frag; d->control %#.8lux\n", control);
808 freeb(ctlr->rb[rdh]);
812 qpkt(Ether *edev, int rdh, ulong control)
819 len = (control & RxflMASK) - 4;
822 panic("8169: received pkt non-existent");
824 panic("8169: received pkt too big");
826 bp->wp = bp->rp + len;
829 allcache->invse(bp->rp, len); /* clear any stale cached packet */
830 ckrderrs(ctlr, bp, control);
842 return ctlr->isr & (Fovw|Rdu|Rer|Rok) &&
843 !(ctlr->rd[ctlr->rdh].control & Own);
860 /* wait for next interrupt */
861 ilock(&ctlr->reglock);
862 ctlr->imr |= Fovw|Rdu|Rer|Rok;
863 csr16w(ctlr, Imr, ctlr->imr);
864 iunlock(&ctlr->reglock);
866 sleep(&ctlr->rrendez, pktstoread, ctlr);
868 /* clear saved isr bits */
869 ilock(&ctlr->reglock);
870 ctlr->isr &= ~(Fovw|Rdu|Rer|Rok);
871 iunlock(&ctlr->reglock);
874 for (rd = &ctlr->rd[rdh]; !(rd->control & Own);
875 rd = &ctlr->rd[rdh]){
876 control = rd->control;
877 if((control & (Fs|Ls|Res)) == (Fs|Ls))
878 qpkt(edev, rdh, control);
880 badpkt(edev, rdh, control);
887 rdh = NEXT(rdh, ctlr->nrd);
888 if(ctlr->nrdfree < ctlr->nrd/2) {
889 /* replenish reads ctlr->rdh */
891 rtl8169replenish(edev);
892 /* if replenish called restart, rdh is reset */
904 Ctlr *ctlr = edev->ctlr;
906 return ctlr->isr & (Ter|Tok) &&
907 !(ctlr->td[ctlr->tdh].control & Own) && edev->link;
922 /* wait for next interrupt */
923 ilock(&ctlr->reglock);
924 ctlr->imr |= Ter|Tok;
925 csr16w(ctlr, Imr, ctlr->imr);
926 iunlock(&ctlr->reglock);
928 sleep(&ctlr->trendez, pktstosend, edev);
930 /* clear saved isr bits */
931 ilock(&ctlr->reglock);
932 ctlr->isr &= ~(Ter|Tok);
933 iunlock(&ctlr->reglock);
935 /* reclaim transmitted Blocks */
936 for(x = ctlr->tdh; ctlr->ntq > 0; x = NEXT(x, ctlr->ntd)){
938 if(d == nil || d->control & Own)
943 * Need to clean the descriptor here? Not really.
944 * Simple freeb for now (no chain and freeblist).
945 * Use ntq count for now.
957 csr8w(ctlr, Tppoll, Npq); /* kick xmiter to keep it going */
958 /* copy as much of my output q as possible into output ring */
960 while(ctlr->ntq < (ctlr->ntd-1)){
961 if((bp = qget(edev->oq)) == nil)
964 /* make sure the whole packet is in ram */
966 allcache->wbse(bp->rp, len);
970 assert(!(d->control & Own));
972 d->addrlo = PCIWADDR(bp->rp);
975 d->control = (d->control & ~TxflMASK) |
982 x = NEXT(x, ctlr->ntd);
987 csr8w(ctlr, Tppoll, Npq); /* kick xmiter again */
989 if(x != ctlr->tdt){ /* added new packet(s)? */
992 csr8w(ctlr, Tppoll, Npq);
994 else if(ctlr->ntq >= (ctlr->ntd-1))
1000 rtl8169init(Ether* edev)
1007 ilock(&ctlr->ilock);
1010 ilock(&ctlr->reglock);
1017 /* 8168b manual says set c+ reg first, then command */
1018 csr16w(ctlr, Cplusc, 0x2000); /* magic */
1024 * MAC Address is not settable on some (all?) chips.
1025 * Must put chip into config register write enable mode.
1027 csr8w(ctlr, Cr9346, Eem1|Eem0);
1032 memset(ctlr->td, 0, sizeof(D)*ctlr->ntd);
1033 ctlr->tdh = ctlr->tdt = 0;
1035 ctlr->td[ctlr->ntd-1].control = Eor;
1039 * Need to do something here about the multicast filter.
1041 memset(ctlr->rd, 0, sizeof(D)*ctlr->nrd);
1042 ctlr->nrdfree = ctlr->rdh = ctlr->rdt = 0;
1043 ctlr->rd[ctlr->nrd-1].control = Eor;
1045 rtl8169replenish(edev);
1049 ctlr->rcr = Rxfthnone|Mrxdmaunlimited|Ab|Apm;
1053 ctlr->rcr = Rxfthnone|6<<MrxdmaSHIFT|Ab|Apm; /* DMA max 1024 */
1058 * Setting Mulrw in Cplusc disables the Tx/Rx DMA burst
1059 * settings in Tcr/Rcr; the (1<<14) is magic.
1061 cplusc = csr16r(ctlr, Cplusc) & ~(1<<14);
1065 cplusc |= Pktcntoff | Init1;
1068 cplusc |= /*Rxchksum|*/Mulrw;
1071 panic("ether8169: unknown macv %#08ux for vid %#ux did %#ux",
1072 ctlr->macv, ctlr->pcidev->vid, ctlr->pcidev->did);
1077 cplusc |= 1<<14; /* magic */
1081 * This is interpreted from clearly bogus code
1082 * in the manufacturer-supplied driver, it could
1083 * be wrong. Untested.
1085 r = csr8r(ctlr, Config2) & 0x07;
1086 if(r == 0x01) /* 66MHz PCI */
1087 csr32w(ctlr, 0x7C, 0x0007FFFF); /* magic */
1089 csr32w(ctlr, 0x7C, 0x0007FF00); /* magic */
1090 pciclrmwi(ctlr->pcidev);
1094 * This is interpreted from clearly bogus code
1095 * in the manufacturer-supplied driver, it could
1096 * be wrong. Untested.
1098 pcicfgw8(ctlr->pcidev, 0x68, 0x00); /* magic */
1099 pcicfgw8(ctlr->pcidev, 0x69, 0x08); /* magic */
1114 * Enable receiver/transmitter.
1115 * Need to do this first or some of the settings below
1120 csr8w(ctlr, Cr, Te|Re);
1121 csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
1122 csr32w(ctlr, Rcr, ctlr->rcr);
1129 csr32w(ctlr, Mar0, 0);
1130 csr32w(ctlr, Mar0+4, 0);
1134 * Disable Tdu for now, the transmit routine will tidy.
1135 * Tdu means the NIC ran out of descriptors to send (i.e., the
1136 * output ring is empty), so it doesn't really need to ever be on.
1138 * The timer runs at the PCI(-E) clock frequency, 125MHz for PCI-E,
1139 * presumably 66MHz for PCI. Thus the units for PCI-E controllers
1140 * (e.g., 8168) are 8ns, and only the buggy 8168 seems to need to use
1141 * timeouts to keep from stalling.
1143 csr32w(ctlr, Tctr, 0);
1144 /* Tok makes the whole system run faster */
1145 ctlr->imr = Serr|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok;
1149 /* alleged workaround for rx fifo overflow on 8168[bd] */
1153 csr16w(ctlr, Imr, ctlr->imr);
1156 * Clear missed-packet counter;
1157 * clear early transmit threshold value;
1158 * set the descriptor ring base addresses;
1159 * set the maximum receive packet size;
1160 * no early-receive interrupts.
1162 * note: the maximum rx size is a filter. the size of the buffer
1163 * in the descriptor ring is still honored. we will toss >Mtu
1164 * packets because they've been fragmented into multiple
1167 csr32w(ctlr, Mpc, 0);
1169 csr8w(ctlr, Mtps, Mps / 128);
1171 csr8w(ctlr, Etx, 0x3f); /* max; no early transmission */
1172 csr32w(ctlr, Tnpds+4, 0);
1173 csr32w(ctlr, Tnpds, PCIWADDR(ctlr->td));
1174 csr32w(ctlr, Rdsar+4, 0);
1175 csr32w(ctlr, Rdsar, PCIWADDR(ctlr->rd));
1176 csr16w(ctlr, Rms, 2048); /* was Mps; see above comment */
1177 r = csr16r(ctlr, Mulint) & 0xF000; /* no early rx interrupts */
1178 csr16w(ctlr, Mulint, r);
1179 csr16w(ctlr, Cplusc, cplusc);
1180 csr16w(ctlr, Coal, 0);
1183 * Set configuration.
1187 csr8w(ctlr, Cr, Te|Re);
1188 csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
1189 csr32w(ctlr, Rcr, ctlr->rcr);
1193 csr16w(ctlr, Cplusc, 0x2000); /* magic */
1194 csr8w(ctlr, Cr, Te|Re);
1195 csr32w(ctlr, Tcr, Ifg1|Ifg0|6<<MtxdmaSHIFT); /* DMA max 1024 */
1196 csr32w(ctlr, Rcr, ctlr->rcr);
1199 ctlr->tcr = csr32r(ctlr, Tcr);
1200 csr8w(ctlr, Cr9346, 0);
1202 iunlock(&ctlr->reglock);
1203 iunlock(&ctlr->ilock);
1205 // rtl8169mii(ctlr);
1211 rtl8169attach(Ether* edev)
1214 char name[KNAMELEN];
1220 qlock(&ctlr->alock);
1221 if(ctlr->init || waserror()) {
1222 qunlock(&ctlr->alock);
1226 ctlr->td = ucallocalign(sizeof(D)*Ntd, 256, 0);
1227 ctlr->tb = malloc(Ntd*sizeof(Block*));
1230 ctlr->rd = ucallocalign(sizeof(D)*Nrd, 256, 0);
1231 ctlr->rb = malloc(Nrd*sizeof(Block*));
1234 ctlr->dtcc = mallocalign(sizeof(Dtcc), 64, 0, 0);
1243 if(ctlr->td == nil || ctlr->tb == nil || ctlr->rd == nil ||
1244 ctlr->rb == nil || ctlr->dtcc == nil)
1247 /* allocate private receive-buffer pool */
1249 for(i = 0; i < Nrb; i++){
1250 if((bp = allocb(Mps)) == nil)
1258 qunlock(&ctlr->alock);
1260 poperror(); /* free */
1261 poperror(); /* qunlock */
1263 /* signal secondary cpus that l1 ptes are stable */
1264 l1ptstable.word = 1;
1265 allcache->wbse(&l1ptstable, sizeof l1ptstable);
1268 /* Don't wait long for link to be ready. */
1269 for(timeo = 0; timeo < 50 && miistatus(ctlr->mii) != 0; timeo++)
1270 // tsleep(&up->sleep, return0, 0, 100); /* fewer miistatus msgs */
1274 tsleep(&up->sleep, return0, 0, 10);
1277 snprint(name, KNAMELEN, "#l%drproc", edev->ctlrno);
1278 kproc(name, rproc, edev);
1280 snprint(name, KNAMELEN, "#l%dtproc", edev->ctlrno);
1281 kproc(name, tproc, edev);
1284 /* call with ctlr->reglock held */
1286 rtl8169link(Ether* edev)
1294 if(!((r = csr8r(ctlr, Phystatus)) & Linksts)){
1297 csr8w(ctlr, Cr, Re);
1298 iprint("#l%d: link down\n", edev->ctlrno);
1302 if (edev->link == 0) {
1304 csr8w(ctlr, Cr, Te|Re);
1305 iprint("#l%d: link up\n", edev->ctlrno);
1311 } else if(r & Speed100)
1313 else if(r & Speed1000)
1317 qsetlimit(edev->oq, limit);
1321 rtl8169transmit(Ether* edev)
1326 if (ctlr == nil || ctlr->ntd == 0) {
1327 iprint("rtl8169transmit: not yet initialised\n");
1330 wakeup(&ctlr->trendez);
1334 * the controller has lost its mind, so reset it.
1335 * call with ctlr->reglock held.
1338 restart(Ether *edev, char *why)
1342 static int inrestart;
1343 static Lock rstrtlck;
1345 /* keep other cpus out */
1354 if (ctlr == nil || !ctlr->init) {
1361 iprint("#l%d: restart due to %s\n", edev->ctlrno, why);
1364 /* process any pkts in the rings */
1365 wakeup(&ctlr->rrendez);
1367 rtl8169transmit(edev);
1368 /* allow time to drain 1024-buffer ring */
1369 for (del = 0; del < 13 && ctlr->ntq > 0; del++)
1372 iunlock(&ctlr->reglock);
1374 /* free any remaining unprocessed input buffers */
1375 for (i = 0; i < ctlr->nrd; i++) {
1380 ilock(&ctlr->reglock);
1383 rtl8169transmit(edev); /* drain any output queue */
1384 wakeup(&ctlr->rrendez);
1393 rcvdiag(Ether *edev, ulong isr)
1398 if(!(isr & (Punlc|Rok)))
1408 if (isr & (Fovw|Rdu|Rer)) {
1409 if (isr & ~(Tdu|Tok|Rok)) /* harmless */
1410 iprint("#l%d: isr %8.8#lux\n", edev->ctlrno, isr);
1411 restart(edev, "rcv error");
1418 rtl8169interrupt(Ureg*, void* arg)
1426 ilock(&ctlr->reglock);
1428 while((isr = csr16r(ctlr, Isr)) != 0 && isr != 0xFFFF){
1429 ctlr->isr |= isr; /* merge bits for [rt]proc */
1430 csr16w(ctlr, Isr, isr); /* dismiss? */
1431 if((isr & ctlr->imr) == 0)
1433 if(isr & Fovw && ctlr->pciv == Rtl8168b) {
1435 * Fovw means we got behind; relatively common on 8168.
1436 * this is a big hammer, but it gets things going again.
1439 restart(edev, "rx fifo overrun");
1442 if(isr & (Fovw|Punlc|Rdu|Rer|Rok)) {
1443 ctlr->imr &= ~(Fovw|Rdu|Rer|Rok);
1444 csr16w(ctlr, Imr, ctlr->imr);
1445 wakeup(&ctlr->rrendez);
1447 if (isr & (Fovw|Punlc|Rdu|Rer)) {
1448 isr = rcvdiag(edev, isr);
1450 break; /* restarted */
1452 isr &= ~(Fovw|Rdu|Rer|Rok);
1454 if(isr & (Ter|Tok)){
1455 ctlr->imr &= ~(Ter|Tok);
1456 csr16w(ctlr, Imr, ctlr->imr);
1457 wakeup(&ctlr->trendez);
1460 iprint("xmit err; isr %8.8#ux\n", isr);
1470 * Some of the reserved bits get set sometimes...
1472 if(isr & (Serr|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok))
1473 panic("rtl8169interrupt: imr %#4.4ux isr %#4.4ux",
1474 csr16r(ctlr, Imr), isr);
1476 if (edev->link && ctlr->ntq > 0)
1477 csr8w(ctlr, Tppoll, Npq); /* kick xmiter to keep it going */
1478 iunlock(&ctlr->reglock);
1480 * extinguish pci-e controller interrupt source.
1481 * should be done more cleanly.
1488 vetmacv(Ctlr *ctlr, uint *macv)
1490 *macv = csr32r(ctlr, Tcr) & HwveridMASK;
1523 while(p = pcimatch(p, 0, 0)){
1524 if(p->ccrb != 0x02 || p->ccru != 0)
1528 switch(i = ((p->did<<16)|p->vid)){
1531 case Rtl8100e: /* RTL810[01]E ? */
1532 case Rtl8168b: /* RTL8168B */
1535 case Rtl8169c: /* RTL8169C */
1536 case Rtl8169sc: /* RTL8169SC */
1537 case Rtl8169: /* RTL8169 */
1539 case (0xC107<<16)|0x1259: /* Corega CG-LAPCIGT */
1544 bar = p->mem[2].bar & ~0x0F;
1546 assert(!(p->mem[2].bar & Barioaddr));
1547 if(0) iprint("rtl8169: %d-bit register accesses\n",
1548 ((p->mem[2].bar >> Barwidthshift) & Barwidthmask) ==
1549 Barwidth32? 32: 64);
1550 mem = (void *)bar; /* don't need to vmap on trimslice */
1552 print("rtl8169: can't map %#ux\n", bar);
1555 ctlr = malloc(sizeof(Ctlr));
1564 if(vetmacv(ctlr, &macv) == -1){
1566 print("rtl8169: unknown mac %.4ux %.8ux\n", p->did, macv);
1572 if(rtl8169reset(ctlr)){
1578 * Extract the chip hardware version,
1579 * needed to configure each properly.
1586 if(rtl8169ctlrhead != nil)
1587 rtl8169ctlrtail->next = ctlr;
1589 rtl8169ctlrhead = ctlr;
1590 rtl8169ctlrtail = ctlr;
1595 rtl8169pnp(Ether* edev)
1608 * Any adapter matches if no edev->port is supplied,
1609 * otherwise the ports must match.
1611 for(ctlr = rtl8169ctlrhead; ctlr != nil; ctlr = ctlr->next){
1614 if(edev->port == 0 || edev->port == ctlr->port){
1624 edev->port = ctlr->port;
1625 // edev->irq = ctlr->pcidev->intl; /* incorrect on trimslice */
1626 edev->irq = Pcieirq; /* trimslice: non-msi pci-e intr */
1627 edev->tbdf = ctlr->pcidev->tbdf;
1632 * Check if the adapter's station address is to be overridden.
1633 * If not, read it from the device and set in edev->ea.
1635 memset(ea, 0, Eaddrlen);
1636 if(memcmp(ea, edev->ea, Eaddrlen) == 0){
1637 r = csr32r(ctlr, Idr0);
1640 edev->ea[2] = r>>16;
1641 edev->ea[3] = r>>24;
1642 r = csr32r(ctlr, Idr0+4);
1647 edev->attach = rtl8169attach;
1648 edev->transmit = rtl8169transmit;
1649 edev->ifstat = rtl8169ifstat;
1652 edev->promiscuous = rtl8169promiscuous;
1653 edev->multicast = rtl8169multicast;
1654 edev->shutdown = rtl8169shutdown;
1656 ilock(&ctlr->reglock);
1658 iunlock(&ctlr->reglock);
1660 intrenable(edev->irq, rtl8169interrupt, edev, 0, edev->name);
1668 addethercard("rtl8169", rtl8169pnp);