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/error.h"
17 #include "../port/netif.h"
18 #include "../port/etherif.h"
19 #include "../port/ethermii.h"
21 typedef struct Ctlr Ctlr;
22 typedef struct D D; /* Transmit/Receive Descriptor */
23 typedef struct Dtcc Dtcc;
26 Debug = 0, /* beware: > 1 interferes with correct operation */
29 enum { /* registers */
30 Idr0 = 0x00, /* MAC address */
31 Mar0 = 0x08, /* Multicast address */
32 Dtccr = 0x10, /* Dump Tally Counter Command */
33 Tnpds = 0x20, /* Transmit Normal Priority Descriptors */
34 Thpds = 0x28, /* Transmit High Priority Descriptors */
35 Flash = 0x30, /* Flash Memory Read/Write */
36 Erbcr = 0x34, /* Early Receive Byte Count */
37 Ersr = 0x36, /* Early Receive Status */
38 Cr = 0x37, /* Command Register */
39 Tppoll = 0x38, /* Transmit Priority Polling */
40 Imr = 0x3C, /* Interrupt Mask */
41 Isr = 0x3E, /* Interrupt Status */
42 Tcr = 0x40, /* Transmit Configuration */
43 Rcr = 0x44, /* Receive Configuration */
44 Tctr = 0x48, /* Timer Count */
45 Mpc = 0x4C, /* Missed Packet Counter */
46 Cr9346 = 0x50, /* 9346 Command Register */
47 Config0 = 0x51, /* Configuration Register 0 */
48 Config1 = 0x52, /* Configuration Register 1 */
49 Config2 = 0x53, /* Configuration Register 2 */
50 Config3 = 0x54, /* Configuration Register 3 */
51 Config4 = 0x55, /* Configuration Register 4 */
52 Config5 = 0x56, /* Configuration Register 5 */
53 Timerint = 0x58, /* Timer Interrupt */
54 Mulint = 0x5C, /* Multiple Interrupt Select */
55 Phyar = 0x60, /* PHY Access */
56 Tbicsr0 = 0x64, /* TBI Control and Status */
57 Tbianar = 0x68, /* TBI Auto-Negotiation Advertisment */
58 Tbilpar = 0x6A, /* TBI Auto-Negotiation Link Partner */
59 Phystatus = 0x6C, /* PHY Status */
61 Rms = 0xDA, /* Receive Packet Maximum Size */
62 Cplusc = 0xE0, /* C+ Command */
63 Coal = 0xE2, /* Interrupt Mitigation (Coalesce) */
64 Rdsar = 0xE4, /* Receive Descriptor Start Address */
65 Etx = 0xEC, /* 8169: Early Tx Threshold; 32-byte units */
66 Mtps = 0xEC, /* 8168: Maximum Transmit Packet Size */
70 Cmd = 0x00000008, /* Command */
74 Te = 0x04, /* Transmitter Enable */
75 Re = 0x08, /* Receiver Enable */
76 Rst = 0x10, /* Software Reset */
80 Fswint = 0x01, /* Forced Software Interrupt */
81 Npq = 0x40, /* Normal Priority Queue polling */
82 Hpq = 0x80, /* High Priority Queue polling */
86 Rok = 0x0001, /* Receive OK */
87 Rer = 0x0002, /* Receive Error */
88 Tok = 0x0004, /* Transmit OK */
89 Ter = 0x0008, /* Transmit Error */
90 Rdu = 0x0010, /* Receive Descriptor Unavailable */
91 Punlc = 0x0020, /* Packet Underrun or Link Change */
92 Fovw = 0x0040, /* Receive FIFO Overflow */
93 Tdu = 0x0080, /* Transmit Descriptor Unavailable */
94 Swint = 0x0100, /* Software Interrupt */
95 Timeout = 0x4000, /* Timer */
96 Serr = 0x8000, /* System Error */
100 MtxdmaSHIFT = 8, /* Max. DMA Burst Size */
101 MtxdmaMASK = 0x00000700,
102 Mtxdmaunlimited = 0x00000700,
103 Acrc = 0x00010000, /* Append CRC (not) */
104 Lbk0 = 0x00020000, /* Loopback Test 0 */
105 Lbk1 = 0x00040000, /* Loopback Test 1 */
106 Ifg2 = 0x00080000, /* Interframe Gap 2 */
107 HwveridSHIFT = 23, /* Hardware Version ID */
108 HwveridMASK = 0x7C800000,
109 Macv01 = 0x00000000, /* RTL8169 */
110 Macv02 = 0x00800000, /* RTL8169S/8110S */
111 Macv03 = 0x04000000, /* RTL8169S/8110S */
112 Macv04 = 0x10000000, /* RTL8169SB/8110SB */
113 Macv05 = 0x18000000, /* RTL8169SC/8110SC */
114 Macv07 = 0x24800000, /* RTL8102e */
115 // Macv8103e = 0x24C00000,
116 Macv25 = 0x28000000, /* RTL8168D */
117 // Macv8168dp = 0x28800000,
118 // Macv8168e = 0x2C000000,
119 Macv11 = 0x30000000, /* RTL8168B/8111B */
120 Macv14 = 0x30800000, /* RTL8100E */
121 Macv13 = 0x34000000, /* RTL8101E */
122 Macv07a = 0x34800000, /* RTL8102e */
123 Macv12 = 0x38000000, /* RTL8169B/8111B */
124 // Macv8168spin3 = 0x38400000,
125 Macv15 = 0x38800000, /* RTL8100E */
126 Macv12a = 0x3c000000, /* RTL8169C/8111C */
127 // Macv19 = 0x3c000000, /* dup Macv12a: RTL8111c-gr */
128 // Macv8168cspin2 = 0x3c400000,
129 // Macv8168cp = 0x3c800000,
130 // Macv8139 = 0x60000000,
131 // Macv8139a = 0x70000000,
132 // Macv8139ag = 0x70800000,
133 // Macv8139b = 0x78000000,
134 // Macv8130 = 0x7C000000,
135 // Macv8139c = 0x74000000,
136 // Macv8139d = 0x74400000,
137 // Macv8139cplus = 0x74800000,
138 // Macv8101 = 0x74c00000,
139 // Macv8100 = 0x78800000,
140 // Macv8169_8110sbl= 0x7cc00000,
141 // Macv8169_8110sce= 0x98000000,
142 Ifg0 = 0x01000000, /* Interframe Gap 0 */
143 Ifg1 = 0x02000000, /* Interframe Gap 1 */
147 Aap = 0x00000001, /* Accept All Packets */
148 Apm = 0x00000002, /* Accept Physical Match */
149 Am = 0x00000004, /* Accept Multicast */
150 Ab = 0x00000008, /* Accept Broadcast */
151 Ar = 0x00000010, /* Accept Runt */
152 Aer = 0x00000020, /* Accept Error */
153 Sel9356 = 0x00000040, /* 9356 EEPROM used */
154 MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
155 MrxdmaMASK = 0x00000700,
156 Mrxdmaunlimited = 0x00000700,
157 RxfthSHIFT = 13, /* Receive Buffer Length */
158 RxfthMASK = 0x0000E000,
159 Rxfth256 = 0x00008000,
160 Rxfthnone = 0x0000E000,
161 Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
162 MulERINT = 0x01000000, /* Multiple Early Interrupt Select */
170 Eem0 = 0x40, /* Operating Mode */
175 DataMASK = 0x0000FFFF, /* 16-bit GMII/MII Register Data */
177 RegaddrMASK = 0x001F0000, /* 5-bit GMII/MII Register Address */
179 Flag = 0x80000000, /* */
182 enum { /* Phystatus */
183 Fd = 0x01, /* Full Duplex */
184 Linksts = 0x02, /* Link Status */
185 Speed10 = 0x04, /* */
186 Speed100 = 0x08, /* */
187 Speed1000 = 0x10, /* */
194 Init1 = 0x0001, /* 8168 */
195 Mulrw = 0x0008, /* PCI Multiple R/W Enable */
196 Dac = 0x0010, /* PCI Dual Address Cycle Enable */
197 Rxchksum = 0x0020, /* Receive Checksum Offload Enable */
198 Rxvlan = 0x0040, /* Receive VLAN De-tagging Enable */
199 Pktcntoff = 0x0080, /* 8168, 8101 */
200 Endian = 0x0200, /* Endian Mode */
210 enum { /* Transmit Descriptor control */
211 TxflMASK = 0x0000FFFF, /* Transmit Frame Length */
213 Tcps = 0x00010000, /* TCP Checksum Offload */
214 Udpcs = 0x00020000, /* UDP Checksum Offload */
215 Ipcs = 0x00040000, /* IP Checksum Offload */
216 Lgsen = 0x08000000, /* TSO; WARNING: contains lark's vomit */
219 enum { /* Receive Descriptor control */
220 RxflMASK = 0x00001FFF, /* Receive Frame Length */
221 Tcpf = 0x00004000, /* TCP Checksum Failure */
222 Udpf = 0x00008000, /* UDP Checksum Failure */
223 Ipf = 0x00010000, /* IP Checksum Failure */
224 Pid0 = 0x00020000, /* Protocol ID0 */
225 Pid1 = 0x00040000, /* Protocol ID1 */
226 Crce = 0x00080000, /* CRC Error */
227 Runt = 0x00100000, /* Runt Packet */
228 Res = 0x00200000, /* Receive Error Summary */
229 Rwt = 0x00400000, /* Receive Watchdog Timer Expired */
230 Fovf = 0x00800000, /* FIFO Overflow */
231 Bovf = 0x01000000, /* Buffer Overflow */
232 Bar = 0x02000000, /* Broadcast Address Received */
233 Pam = 0x04000000, /* Physical Address Matched */
234 Mar = 0x08000000, /* Multicast Address Received */
237 enum { /* General Descriptor control */
238 Ls = 0x10000000, /* Last Segment Descriptor */
239 Fs = 0x20000000, /* First Segment Descriptor */
240 Eor = 0x40000000, /* End of Descriptor Ring */
241 Own = 0x80000000, /* Ownership: belongs to hw */
246 enum { /* Ring sizes (<= 1024) */
247 Ntd = 1024, /* Transmit Ring */
248 /* at 1Gb/s, it only takes 12 ms. to fill a 1024-buffer ring */
249 Nrd = 1024, /* Receive Ring */
253 Mps = ROUNDUP(ETHERMAXTU+4, 128),
254 // Mps = Mtu + 8 + 14, /* if(mtu>ETHERMAXTU) */
273 enum { /* Variants */
274 Rtl8100e = (0x8136<<16)|0x10EC, /* RTL810[01]E: pci -e */
275 Rtl8169c = (0x0116<<16)|0x16EC, /* RTL8169C+ (USR997902) */
276 Rtl8169sc = (0x8167<<16)|0x10EC, /* RTL8169SC */
277 Rtl8168b = (0x8168<<16)|0x10EC, /* RTL8168B: pci-e */
278 Rtl8169 = (0x8169<<16)|0x10EC, /* RTL8169 */
280 * trimslice is 10ec/8168 (8168b) Macv25 (8168D) but
281 * compulab says 8111dl.
282 * oui 0x732 (aaeon) phyno 1, macv = 0x28000000 phyv = 0x0002
291 Ether* ether; /* point back */
294 QLock alock; /* attach */
295 Lock ilock; /* init */
299 int macv; /* MAC version */
300 int phyv; /* PHY version */
301 int pcie; /* flag: pci-express device? */
303 uvlong mchash; /* multicast hash */
307 // Lock tlock; /* transmit */
309 D* td; /* descriptor ring */
310 Block** tb; /* transmit buffers */
313 int tdh; /* head - producer index (host) */
314 int tdt; /* tail - consumer index (NIC) */
320 // Lock rlock; /* receive */
322 D* rd; /* descriptor ring */
323 Block** rb; /* receive buffers */
326 int rdh; /* head - producer index (NIC) */
327 int rdt; /* tail - consumer index (host) */
331 int tcr; /* transmit configuration register */
332 int rcr; /* receive configuration register */
334 int isr; /* sw copy for kprocs */
336 QLock slock; /* statistics */
349 uint frag; /* partial packets; rb was too small */
352 static Ctlr* rtl8169ctlrhead;
353 static Ctlr* rtl8169ctlrtail;
355 static Lock rblock; /* free receive Blocks */
356 static Block* rbpool;
358 #define csr8r(c, r) (*((uchar *) ((c)->nic)+(r)))
359 #define csr16r(c, r) (*((u16int *)((c)->nic)+((r)/2)))
360 #define csr32p(c, r) ((u32int *) ((c)->nic)+((r)/4))
361 #define csr32r(c, r) (*csr32p(c, r))
363 #define csr8w(c, r, b) (*((uchar *) ((c)->nic)+(r)) = (b), coherence())
364 #define csr16w(c, r, w) (*((u16int *)((c)->nic)+((r)/2)) = (w), coherence())
365 #define csr32w(c, r, v) (*csr32p(c, r) = (v), coherence())
368 rtl8169miimir(Mii* mii, int pa, int ra)
377 r = (ra<<16) & RegaddrMASK;
378 csr32w(ctlr, Phyar, r);
380 for(timeo = 0; timeo < 2000; timeo++){
381 if((r = csr32r(ctlr, Phyar)) & Flag)
388 return (r & DataMASK)>>DataSHIFT;
392 rtl8169miimiw(Mii* mii, int pa, int ra, int data)
401 r = Flag|((ra<<16) & RegaddrMASK)|((data<<DataSHIFT) & DataMASK);
402 csr32w(ctlr, Phyar, r);
404 for(timeo = 0; timeo < 2000; timeo++){
405 if(!((r = csr32r(ctlr, Phyar)) & Flag))
416 rtl8169mii(Ctlr* ctlr)
423 if((ctlr->mii = malloc(sizeof(Mii))) == nil)
425 ctlr->mii->mir = rtl8169miimir;
426 ctlr->mii->miw = rtl8169miimiw;
427 ctlr->mii->ctlr = ctlr;
430 * Get rev number out of Phyidr2 so can config properly.
431 * There's probably more special stuff for Macv0[234] needed here.
433 ilock(&ctlr->reglock);
434 ctlr->phyv = rtl8169miimir(ctlr->mii, 1, Phyidr2) & 0x0F;
435 if(ctlr->macv == Macv02){
436 csr8w(ctlr, 0x82, 1); /* magic */
437 rtl8169miimiw(ctlr->mii, 1, 0x0B, 0x0000); /* magic */
440 if(mii(ctlr->mii, (1<<1)) == 0 || (phy = ctlr->mii->curphy) == nil){
441 iunlock(&ctlr->reglock);
446 print("rtl8169: oui %#ux phyno %d, macv = %#8.8ux phyv = %#4.4ux\n",
447 phy->oui, phy->phyno, ctlr->macv, ctlr->phyv);
449 miiane(ctlr->mii, ~0, ~0, ~0);
450 iunlock(&ctlr->reglock);
461 if((bp = rbpool) != nil){
472 bp->wp = bp->rp = bp->lim - Mps;
473 bp->flag &= ~(Bipck | Budpck | Btcpck | Bpktck);
482 rtl8169promiscuous(void* arg, int on)
490 ilock(&ctlr->reglock);
496 csr32w(ctlr, Rcr, ctlr->rcr);
497 iunlock(&ctlr->reglock);
498 iunlock(&ctlr->ilock);
502 /* everyone else uses 0x04c11db7, but they both produce the same crc */
503 Etherpolybe = 0x04c11db6,
504 Bytemask = (1<<8) - 1,
508 ethercrcbe(uchar *addr, long len)
514 for (i = 0; i < len; i++) {
516 for (j = 0; j < 8; j++) {
517 carry = ((crc & (1UL << 31))? 1: 0) ^ (c & 1);
521 crc = (crc ^ Etherpolybe) | carry;
530 return l>>24 | (l>>8) & (Bytemask<<8) |
531 (l<<8) & (Bytemask<<16) | l<<24;
535 rtl8169multicast(void* ether, uchar *eaddr, int add)
541 return; /* ok to keep receiving on old mcast addrs */
546 ilock(&ctlr->reglock);
548 ctlr->mchash |= 1ULL << (ethercrcbe(eaddr, Eaddrlen) >> 26);
551 csr32w(ctlr, Rcr, ctlr->rcr);
553 /* pci-e variants reverse the order of the hash byte registers */
555 csr32w(ctlr, Mar0, swabl(ctlr->mchash>>32));
556 csr32w(ctlr, Mar0+4, swabl(ctlr->mchash));
558 csr32w(ctlr, Mar0, ctlr->mchash);
559 csr32w(ctlr, Mar0+4, ctlr->mchash>>32);
562 iunlock(&ctlr->reglock);
563 iunlock(&ctlr->ilock);
567 rtl8169ifstat(Ether* edev, void* a, long n, ulong offset)
579 qunlock(&ctlr->slock);
584 /* copy hw statistics into ctlr->dtcc */
586 allcache->invse(dtcc, sizeof *dtcc);
587 ilock(&ctlr->reglock);
588 csr32w(ctlr, Dtccr+4, 0);
589 csr32w(ctlr, Dtccr, PCIWADDR(dtcc)|Cmd); /* initiate dma? */
590 for(timeo = 0; timeo < 1000; timeo++){
591 if(!(csr32r(ctlr, Dtccr) & Cmd))
595 iunlock(&ctlr->reglock);
596 if(csr32r(ctlr, Dtccr) & Cmd)
599 edev->oerrs = dtcc->txer;
600 edev->crcs = dtcc->rxer;
601 edev->frames = dtcc->fae;
602 edev->buffs = dtcc->misspkt;
603 edev->overflows = ctlr->txdu + ctlr->rdu;
606 qunlock(&ctlr->slock);
611 if((p = malloc(READSTR)) == nil)
614 l = snprint(p, READSTR, "TxOk: %llud\n", dtcc->txok);
615 l += snprint(p+l, READSTR-l, "RxOk: %llud\n", dtcc->rxok);
616 l += snprint(p+l, READSTR-l, "TxEr: %llud\n", dtcc->txer);
617 l += snprint(p+l, READSTR-l, "RxEr: %ud\n", dtcc->rxer);
618 l += snprint(p+l, READSTR-l, "MissPkt: %ud\n", dtcc->misspkt);
619 l += snprint(p+l, READSTR-l, "FAE: %ud\n", dtcc->fae);
620 l += snprint(p+l, READSTR-l, "Tx1Col: %ud\n", dtcc->tx1col);
621 l += snprint(p+l, READSTR-l, "TxMCol: %ud\n", dtcc->txmcol);
622 l += snprint(p+l, READSTR-l, "RxOkPh: %llud\n", dtcc->rxokph);
623 l += snprint(p+l, READSTR-l, "RxOkBrd: %llud\n", dtcc->rxokbrd);
624 l += snprint(p+l, READSTR-l, "RxOkMu: %ud\n", dtcc->rxokmu);
625 l += snprint(p+l, READSTR-l, "TxAbt: %ud\n", dtcc->txabt);
626 l += snprint(p+l, READSTR-l, "TxUndrn: %ud\n", dtcc->txundrn);
628 l += snprint(p+l, READSTR-l, "txdu: %ud\n", ctlr->txdu);
629 l += snprint(p+l, READSTR-l, "tcpf: %ud\n", ctlr->tcpf);
630 l += snprint(p+l, READSTR-l, "udpf: %ud\n", ctlr->udpf);
631 l += snprint(p+l, READSTR-l, "ipf: %ud\n", ctlr->ipf);
632 l += snprint(p+l, READSTR-l, "fovf: %ud\n", ctlr->fovf);
633 l += snprint(p+l, READSTR-l, "ierrs: %ud\n", ctlr->ierrs);
634 l += snprint(p+l, READSTR-l, "rer: %ud\n", ctlr->rer);
635 l += snprint(p+l, READSTR-l, "rdu: %ud\n", ctlr->rdu);
636 l += snprint(p+l, READSTR-l, "punlc: %ud\n", ctlr->punlc);
637 l += snprint(p+l, READSTR-l, "fovw: %ud\n", ctlr->fovw);
639 l += snprint(p+l, READSTR-l, "tcr: %#8.8ux\n", ctlr->tcr);
640 l += snprint(p+l, READSTR-l, "rcr: %#8.8ux\n", ctlr->rcr);
641 l += snprint(p+l, READSTR-l, "multicast: %ud\n", ctlr->mcast);
643 if(ctlr->mii != nil && ctlr->mii->curphy != nil){
644 l += snprint(p+l, READSTR, "phy: ");
645 for(i = 0; i < NMiiPhyr; i++){
646 if(i && ((i & 0x07) == 0))
647 l += snprint(p+l, READSTR-l, "\n ");
648 r = miimir(ctlr->mii, i);
649 l += snprint(p+l, READSTR-l, " %4.4ux", r);
651 snprint(p+l, READSTR-l, "\n");
654 n = readstr(offset, a, n, p);
656 qunlock(&ctlr->slock);
664 rtl8169halt(Ctlr* ctlr)
666 ilock(&ctlr->reglock);
667 csr32w(ctlr, Timerint, 0);
669 csr16w(ctlr, Imr, 0);
670 csr16w(ctlr, Isr, ~0);
671 iunlock(&ctlr->reglock);
675 rtl8169reset(Ctlr* ctlr)
681 * Soft reset the controller.
683 ilock(&ctlr->reglock);
684 csr8w(ctlr, Cr, Rst);
685 for(r = timeo = 0; timeo < 1000; timeo++){
691 iunlock(&ctlr->reglock);
701 rtl8169shutdown(Ether *ether)
703 rtl8169reset(ether->ctlr);
707 rtl8169replenish(Ether *edev)
715 if (ctlr->nrd == 0) {
716 iprint("rtl8169replenish: not yet initialised\n");
722 while(NEXT(rdt, ctlr->nrd) != ctlr->rdh){
725 panic("rtl8169replenish: nil ctlr->rd[%d]", rdt);
726 if (d->control & Own) { /* ctlr owns it? shouldn't happen */
727 iprint("replenish: descriptor owned by hw\n");
730 if(ctlr->rb[rdt] == nil){
733 iprint("rtl8169: no available buffers\n");
739 d->addrlo = PCIWADDR(bp->rp);
742 iprint("8169: replenish: rx overrun\n");
743 d->control = (d->control & ~RxflMASK) | Mps | Own;
746 rdt = NEXT(rdt, ctlr->nrd);
755 ckrderrs(Ctlr *ctlr, Block *bp, ulong control)
762 switch(control & (Pid1|Pid0)){
765 iprint("8169: bad tcp checksum\n");
773 iprint("8169: bad udp checksum\n");
781 iprint("8169: bad ip checksum\n");
791 badpkt(Ether *edev, int rdh, ulong control)
796 /* Res is only valid if Fs is set */
798 iprint("8169: rcv error; d->control %#.8lux\n", control);
799 else if (control == 0) { /* buggered? */
801 iprint("8169: rcv: d->control==0 (wtf?)\n");
804 iprint("8169: rcv'd frag; d->control %#.8lux\n", control);
807 freeb(ctlr->rb[rdh]);
811 qpkt(Ether *edev, int rdh, ulong control)
818 len = (control & RxflMASK) - 4;
821 panic("8169: received pkt non-existent");
823 panic("8169: received pkt too big");
825 bp->wp = bp->rp + len;
828 allcache->invse(bp->rp, len); /* clear any stale cached packet */
829 ckrderrs(ctlr, bp, control);
841 return ctlr->isr & (Fovw|Rdu|Rer|Rok) &&
842 !(ctlr->rd[ctlr->rdh].control & Own);
859 /* wait for next interrupt */
860 ilock(&ctlr->reglock);
861 ctlr->imr |= Fovw|Rdu|Rer|Rok;
862 csr16w(ctlr, Imr, ctlr->imr);
863 iunlock(&ctlr->reglock);
865 sleep(&ctlr->rrendez, pktstoread, ctlr);
867 /* clear saved isr bits */
868 ilock(&ctlr->reglock);
869 ctlr->isr &= ~(Fovw|Rdu|Rer|Rok);
870 iunlock(&ctlr->reglock);
873 for (rd = &ctlr->rd[rdh]; !(rd->control & Own);
874 rd = &ctlr->rd[rdh]){
875 control = rd->control;
876 if((control & (Fs|Ls|Res)) == (Fs|Ls))
877 qpkt(edev, rdh, control);
879 badpkt(edev, rdh, control);
886 rdh = NEXT(rdh, ctlr->nrd);
887 if(ctlr->nrdfree < ctlr->nrd/2) {
888 /* replenish reads ctlr->rdh */
890 rtl8169replenish(edev);
891 /* if replenish called restart, rdh is reset */
903 Ctlr *ctlr = edev->ctlr;
905 return ctlr->isr & (Ter|Tok) &&
906 !(ctlr->td[ctlr->tdh].control & Own) && edev->link;
921 /* wait for next interrupt */
922 ilock(&ctlr->reglock);
923 ctlr->imr |= Ter|Tok;
924 csr16w(ctlr, Imr, ctlr->imr);
925 iunlock(&ctlr->reglock);
927 sleep(&ctlr->trendez, pktstosend, edev);
929 /* clear saved isr bits */
930 ilock(&ctlr->reglock);
931 ctlr->isr &= ~(Ter|Tok);
932 iunlock(&ctlr->reglock);
934 /* reclaim transmitted Blocks */
935 for(x = ctlr->tdh; ctlr->ntq > 0; x = NEXT(x, ctlr->ntd)){
937 if(d == nil || d->control & Own)
942 * Need to clean the descriptor here? Not really.
943 * Simple freeb for now (no chain and freeblist).
944 * Use ntq count for now.
956 csr8w(ctlr, Tppoll, Npq); /* kick xmiter to keep it going */
957 /* copy as much of my output q as possible into output ring */
959 while(ctlr->ntq < (ctlr->ntd-1)){
960 if((bp = qget(edev->oq)) == nil)
963 /* make sure the whole packet is in ram */
965 allcache->wbse(bp->rp, len);
969 assert(!(d->control & Own));
971 d->addrlo = PCIWADDR(bp->rp);
974 d->control = (d->control & ~TxflMASK) |
981 x = NEXT(x, ctlr->ntd);
986 csr8w(ctlr, Tppoll, Npq); /* kick xmiter again */
988 if(x != ctlr->tdt){ /* added new packet(s)? */
991 csr8w(ctlr, Tppoll, Npq);
993 else if(ctlr->ntq >= (ctlr->ntd-1))
999 rtl8169init(Ether* edev)
1006 ilock(&ctlr->ilock);
1009 ilock(&ctlr->reglock);
1016 /* 8168b manual says set c+ reg first, then command */
1017 csr16w(ctlr, Cplusc, 0x2000); /* magic */
1023 * MAC Address is not settable on some (all?) chips.
1024 * Must put chip into config register write enable mode.
1026 csr8w(ctlr, Cr9346, Eem1|Eem0);
1031 memset(ctlr->td, 0, sizeof(D)*ctlr->ntd);
1032 ctlr->tdh = ctlr->tdt = 0;
1034 ctlr->td[ctlr->ntd-1].control = Eor;
1038 * Need to do something here about the multicast filter.
1040 memset(ctlr->rd, 0, sizeof(D)*ctlr->nrd);
1041 ctlr->nrdfree = ctlr->rdh = ctlr->rdt = 0;
1042 ctlr->rd[ctlr->nrd-1].control = Eor;
1044 rtl8169replenish(edev);
1048 ctlr->rcr = Rxfthnone|Mrxdmaunlimited|Ab|Apm;
1052 ctlr->rcr = Rxfthnone|6<<MrxdmaSHIFT|Ab|Apm; /* DMA max 1024 */
1057 * Setting Mulrw in Cplusc disables the Tx/Rx DMA burst
1058 * settings in Tcr/Rcr; the (1<<14) is magic.
1060 cplusc = csr16r(ctlr, Cplusc) & ~(1<<14);
1064 cplusc |= Pktcntoff | Init1;
1067 cplusc |= /*Rxchksum|*/Mulrw;
1070 panic("ether8169: unknown macv %#08ux for vid %#ux did %#ux",
1071 ctlr->macv, ctlr->pcidev->vid, ctlr->pcidev->did);
1076 cplusc |= 1<<14; /* magic */
1080 * This is interpreted from clearly bogus code
1081 * in the manufacturer-supplied driver, it could
1082 * be wrong. Untested.
1084 r = csr8r(ctlr, Config2) & 0x07;
1085 if(r == 0x01) /* 66MHz PCI */
1086 csr32w(ctlr, 0x7C, 0x0007FFFF); /* magic */
1088 csr32w(ctlr, 0x7C, 0x0007FF00); /* magic */
1089 pciclrmwi(ctlr->pcidev);
1093 * This is interpreted from clearly bogus code
1094 * in the manufacturer-supplied driver, it could
1095 * be wrong. Untested.
1097 pcicfgw8(ctlr->pcidev, 0x68, 0x00); /* magic */
1098 pcicfgw8(ctlr->pcidev, 0x69, 0x08); /* magic */
1113 * Enable receiver/transmitter.
1114 * Need to do this first or some of the settings below
1119 csr8w(ctlr, Cr, Te|Re);
1120 csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
1121 csr32w(ctlr, Rcr, ctlr->rcr);
1128 csr32w(ctlr, Mar0, 0);
1129 csr32w(ctlr, Mar0+4, 0);
1133 * Disable Tdu for now, the transmit routine will tidy.
1134 * Tdu means the NIC ran out of descriptors to send (i.e., the
1135 * output ring is empty), so it doesn't really need to ever be on.
1137 * The timer runs at the PCI(-E) clock frequency, 125MHz for PCI-E,
1138 * presumably 66MHz for PCI. Thus the units for PCI-E controllers
1139 * (e.g., 8168) are 8ns, and only the buggy 8168 seems to need to use
1140 * timeouts to keep from stalling.
1142 csr32w(ctlr, Tctr, 0);
1143 /* Tok makes the whole system run faster */
1144 ctlr->imr = Serr|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok;
1148 /* alleged workaround for rx fifo overflow on 8168[bd] */
1152 csr16w(ctlr, Imr, ctlr->imr);
1155 * Clear missed-packet counter;
1156 * clear early transmit threshold value;
1157 * set the descriptor ring base addresses;
1158 * set the maximum receive packet size;
1159 * no early-receive interrupts.
1161 * note: the maximum rx size is a filter. the size of the buffer
1162 * in the descriptor ring is still honored. we will toss >Mtu
1163 * packets because they've been fragmented into multiple
1166 csr32w(ctlr, Mpc, 0);
1168 csr8w(ctlr, Mtps, Mps / 128);
1170 csr8w(ctlr, Etx, 0x3f); /* max; no early transmission */
1171 csr32w(ctlr, Tnpds+4, 0);
1172 csr32w(ctlr, Tnpds, PCIWADDR(ctlr->td));
1173 csr32w(ctlr, Rdsar+4, 0);
1174 csr32w(ctlr, Rdsar, PCIWADDR(ctlr->rd));
1175 csr16w(ctlr, Rms, 2048); /* was Mps; see above comment */
1176 r = csr16r(ctlr, Mulint) & 0xF000; /* no early rx interrupts */
1177 csr16w(ctlr, Mulint, r);
1178 csr16w(ctlr, Cplusc, cplusc);
1179 csr16w(ctlr, Coal, 0);
1182 * Set configuration.
1186 csr8w(ctlr, Cr, Te|Re);
1187 csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
1188 csr32w(ctlr, Rcr, ctlr->rcr);
1192 csr16w(ctlr, Cplusc, 0x2000); /* magic */
1193 csr8w(ctlr, Cr, Te|Re);
1194 csr32w(ctlr, Tcr, Ifg1|Ifg0|6<<MtxdmaSHIFT); /* DMA max 1024 */
1195 csr32w(ctlr, Rcr, ctlr->rcr);
1198 ctlr->tcr = csr32r(ctlr, Tcr);
1199 csr8w(ctlr, Cr9346, 0);
1201 iunlock(&ctlr->reglock);
1202 iunlock(&ctlr->ilock);
1204 // rtl8169mii(ctlr);
1210 rtl8169attach(Ether* edev)
1213 char name[KNAMELEN];
1219 qlock(&ctlr->alock);
1220 if(ctlr->init || waserror()) {
1221 qunlock(&ctlr->alock);
1225 ctlr->td = ucallocalign(sizeof(D)*Ntd, 256, 0);
1226 ctlr->tb = malloc(Ntd*sizeof(Block*));
1229 ctlr->rd = ucallocalign(sizeof(D)*Nrd, 256, 0);
1230 ctlr->rb = malloc(Nrd*sizeof(Block*));
1233 ctlr->dtcc = mallocalign(sizeof(Dtcc), 64, 0, 0);
1242 if(ctlr->td == nil || ctlr->tb == nil || ctlr->rd == nil ||
1243 ctlr->rb == nil || ctlr->dtcc == nil)
1246 /* allocate private receive-buffer pool */
1248 for(i = 0; i < Nrb; i++){
1249 if((bp = allocb(Mps)) == nil)
1257 qunlock(&ctlr->alock);
1259 poperror(); /* free */
1260 poperror(); /* qunlock */
1262 /* signal secondary cpus that l1 ptes are stable */
1263 l1ptstable.word = 1;
1264 allcache->wbse(&l1ptstable, sizeof l1ptstable);
1267 /* Don't wait long for link to be ready. */
1268 for(timeo = 0; timeo < 50 && miistatus(ctlr->mii) != 0; timeo++)
1269 // tsleep(&up->sleep, return0, 0, 100); /* fewer miistatus msgs */
1273 tsleep(&up->sleep, return0, 0, 10);
1276 snprint(name, KNAMELEN, "#l%drproc", edev->ctlrno);
1277 kproc(name, rproc, edev);
1279 snprint(name, KNAMELEN, "#l%dtproc", edev->ctlrno);
1280 kproc(name, tproc, edev);
1283 /* call with ctlr->reglock held */
1285 rtl8169link(Ether* edev)
1293 if(!((r = csr8r(ctlr, Phystatus)) & Linksts)){
1296 csr8w(ctlr, Cr, Re);
1297 iprint("#l%d: link down\n", edev->ctlrno);
1301 if (edev->link == 0) {
1303 csr8w(ctlr, Cr, Te|Re);
1304 iprint("#l%d: link up\n", edev->ctlrno);
1310 } else if(r & Speed100)
1312 else if(r & Speed1000)
1316 qsetlimit(edev->oq, limit);
1320 rtl8169transmit(Ether* edev)
1325 if (ctlr == nil || ctlr->ntd == 0) {
1326 iprint("rtl8169transmit: not yet initialised\n");
1329 wakeup(&ctlr->trendez);
1333 * the controller has lost its mind, so reset it.
1334 * call with ctlr->reglock held.
1337 restart(Ether *edev, char *why)
1341 static int inrestart;
1342 static Lock rstrtlck;
1344 /* keep other cpus out */
1353 if (ctlr == nil || !ctlr->init) {
1360 iprint("#l%d: restart due to %s\n", edev->ctlrno, why);
1363 /* process any pkts in the rings */
1364 wakeup(&ctlr->rrendez);
1366 rtl8169transmit(edev);
1367 /* allow time to drain 1024-buffer ring */
1368 for (del = 0; del < 13 && ctlr->ntq > 0; del++)
1371 iunlock(&ctlr->reglock);
1373 /* free any remaining unprocessed input buffers */
1374 for (i = 0; i < ctlr->nrd; i++) {
1379 ilock(&ctlr->reglock);
1382 rtl8169transmit(edev); /* drain any output queue */
1383 wakeup(&ctlr->rrendez);
1392 rcvdiag(Ether *edev, ulong isr)
1397 if(!(isr & (Punlc|Rok)))
1407 if (isr & (Fovw|Rdu|Rer)) {
1408 if (isr & ~(Tdu|Tok|Rok)) /* harmless */
1409 iprint("#l%d: isr %8.8#lux\n", edev->ctlrno, isr);
1410 restart(edev, "rcv error");
1417 rtl8169interrupt(Ureg*, void* arg)
1425 ilock(&ctlr->reglock);
1427 while((isr = csr16r(ctlr, Isr)) != 0 && isr != 0xFFFF){
1428 ctlr->isr |= isr; /* merge bits for [rt]proc */
1429 csr16w(ctlr, Isr, isr); /* dismiss? */
1430 if((isr & ctlr->imr) == 0)
1432 if(isr & Fovw && ctlr->pciv == Rtl8168b) {
1434 * Fovw means we got behind; relatively common on 8168.
1435 * this is a big hammer, but it gets things going again.
1438 restart(edev, "rx fifo overrun");
1441 if(isr & (Fovw|Punlc|Rdu|Rer|Rok)) {
1442 ctlr->imr &= ~(Fovw|Rdu|Rer|Rok);
1443 csr16w(ctlr, Imr, ctlr->imr);
1444 wakeup(&ctlr->rrendez);
1446 if (isr & (Fovw|Punlc|Rdu|Rer)) {
1447 isr = rcvdiag(edev, isr);
1449 break; /* restarted */
1451 isr &= ~(Fovw|Rdu|Rer|Rok);
1453 if(isr & (Ter|Tok)){
1454 ctlr->imr &= ~(Ter|Tok);
1455 csr16w(ctlr, Imr, ctlr->imr);
1456 wakeup(&ctlr->trendez);
1459 iprint("xmit err; isr %8.8#ux\n", isr);
1469 * Some of the reserved bits get set sometimes...
1471 if(isr & (Serr|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok))
1472 panic("rtl8169interrupt: imr %#4.4ux isr %#4.4ux",
1473 csr16r(ctlr, Imr), isr);
1475 if (edev->link && ctlr->ntq > 0)
1476 csr8w(ctlr, Tppoll, Npq); /* kick xmiter to keep it going */
1477 iunlock(&ctlr->reglock);
1479 * extinguish pci-e controller interrupt source.
1480 * should be done more cleanly.
1487 vetmacv(Ctlr *ctlr, uint *macv)
1489 *macv = csr32r(ctlr, Tcr) & HwveridMASK;
1522 while(p = pcimatch(p, 0, 0)){
1523 if(p->ccrb != 0x02 || p->ccru != 0)
1527 switch(i = ((p->did<<16)|p->vid)){
1530 case Rtl8100e: /* RTL810[01]E ? */
1531 case Rtl8168b: /* RTL8168B */
1534 case Rtl8169c: /* RTL8169C */
1535 case Rtl8169sc: /* RTL8169SC */
1536 case Rtl8169: /* RTL8169 */
1538 case (0xC107<<16)|0x1259: /* Corega CG-LAPCIGT */
1543 bar = p->mem[2].bar & ~0x0F;
1545 assert(!(p->mem[2].bar & Barioaddr));
1546 if(0) iprint("rtl8169: %d-bit register accesses\n",
1547 ((p->mem[2].bar >> Barwidthshift) & Barwidthmask) ==
1548 Barwidth32? 32: 64);
1549 mem = (void *)bar; /* don't need to vmap on trimslice */
1551 print("rtl8169: can't map %#ux\n", bar);
1554 ctlr = malloc(sizeof(Ctlr));
1563 if(vetmacv(ctlr, &macv) == -1){
1565 print("rtl8169: unknown mac %.4ux %.8ux\n", p->did, macv);
1569 if(pcigetpms(p) > 0){
1572 for(i = 0; i < 6; i++)
1573 pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
1574 pcicfgw8(p, PciINTL, p->intl);
1575 pcicfgw8(p, PciLTR, p->ltr);
1576 pcicfgw8(p, PciCLS, p->cls);
1577 pcicfgw16(p, PciPCR, p->pcr);
1580 if(rtl8169reset(ctlr)){
1586 * Extract the chip hardware version,
1587 * needed to configure each properly.
1594 if(rtl8169ctlrhead != nil)
1595 rtl8169ctlrtail->next = ctlr;
1597 rtl8169ctlrhead = ctlr;
1598 rtl8169ctlrtail = ctlr;
1603 rtl8169pnp(Ether* edev)
1616 * Any adapter matches if no edev->port is supplied,
1617 * otherwise the ports must match.
1619 for(ctlr = rtl8169ctlrhead; ctlr != nil; ctlr = ctlr->next){
1622 if(edev->port == 0 || edev->port == ctlr->port){
1632 edev->port = ctlr->port;
1633 // edev->irq = ctlr->pcidev->intl; /* incorrect on trimslice */
1634 edev->irq = Pcieirq; /* trimslice: non-msi pci-e intr */
1635 edev->tbdf = ctlr->pcidev->tbdf;
1640 * Check if the adapter's station address is to be overridden.
1641 * If not, read it from the device and set in edev->ea.
1643 memset(ea, 0, Eaddrlen);
1644 if(memcmp(ea, edev->ea, Eaddrlen) == 0){
1645 r = csr32r(ctlr, Idr0);
1648 edev->ea[2] = r>>16;
1649 edev->ea[3] = r>>24;
1650 r = csr32r(ctlr, Idr0+4);
1655 edev->attach = rtl8169attach;
1656 edev->transmit = rtl8169transmit;
1657 edev->ifstat = rtl8169ifstat;
1660 edev->promiscuous = rtl8169promiscuous;
1661 edev->multicast = rtl8169multicast;
1662 edev->shutdown = rtl8169shutdown;
1664 ilock(&ctlr->reglock);
1666 iunlock(&ctlr->reglock);
1668 intrenable(edev->irq, rtl8169interrupt, edev, 0, edev->name);
1676 addethercard("rtl8169", rtl8169pnp);