ether8169.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * Realtek RTL8110S/8169S.
  11. * Mostly there. There are some magic register values used
  12. * which are not described in any datasheet or driver but seem
  13. * to be necessary.
  14. * No tuning has been done. Only tested on an RTL8110S, there
  15. * are slight differences between the chips in the series so some
  16. * tweaks may be needed.
  17. */
  18. #include "u.h"
  19. #include "../port/lib.h"
  20. #include "mem.h"
  21. #include "dat.h"
  22. #include "fns.h"
  23. #include "io.h"
  24. #include "../port/error.h"
  25. #include "../port/ethermii.h"
  26. #include "../port/netif.h"
  27. #include "etherif.h"
  28. enum { /* registers */
  29. Idr0 = 0x00, /* MAC address */
  30. Mar0 = 0x08, /* Multicast address */
  31. Dtccr = 0x10, /* Dump Tally Counter Command */
  32. Tnpds = 0x20, /* Transmit Normal Priority Descriptors */
  33. Thpds = 0x28, /* Transmit High Priority Descriptors */
  34. Flash = 0x30, /* Flash Memory Read/Write */
  35. Erbcr = 0x34, /* Early Receive Byte Count */
  36. Ersr = 0x36, /* Early Receive Status */
  37. Cr = 0x37, /* Command Register */
  38. Tppoll = 0x38, /* Transmit Priority Polling */
  39. Imr = 0x3C, /* Interrupt Mask */
  40. Isr = 0x3E, /* Interrupt Status */
  41. Tcr = 0x40, /* Transmit Configuration */
  42. Rcr = 0x44, /* Receive Configuration */
  43. Tctr = 0x48, /* Timer Count */
  44. Mpc = 0x4C, /* Missed Packet Counter */
  45. Cr9346 = 0x50, /* 9346 Command Register */
  46. Config0 = 0x51, /* Configuration Register 0 */
  47. Config1 = 0x52, /* Configuration Register 1 */
  48. Config2 = 0x53, /* Configuration Register 2 */
  49. Config3 = 0x54, /* Configuration Register 3 */
  50. Config4 = 0x55, /* Configuration Register 4 */
  51. Config5 = 0x56, /* Configuration Register 5 */
  52. Timerint = 0x58, /* Timer Interrupt */
  53. Mulint = 0x5C, /* Multiple Interrupt Select */
  54. Phyar = 0x60, /* PHY Access */
  55. Tbicsr0 = 0x64, /* TBI Control and Status */
  56. Tbianar = 0x68, /* TBI Auto-Negotiation Advertisment */
  57. Tbilpar = 0x6A, /* TBI Auto-Negotiation Link Partner */
  58. Rms = 0xDA, /* Receive Packet Maximum Size */
  59. Cplusc = 0xE0, /* C+ Command */
  60. Rdsar = 0xE4, /* Receive Descriptor Start Address */
  61. Mtps = 0xEC, /* Max. Transmit Packet Size */
  62. };
  63. enum { /* Dtccr */
  64. Cmd = 0x00000008, /* Command */
  65. };
  66. enum { /* Cr */
  67. Te = 0x04, /* Transmitter Enable */
  68. Re = 0x08, /* Receiver Enable */
  69. Rst = 0x10, /* Software Reset */
  70. };
  71. enum { /* Tppoll */
  72. Fswint = 0x01, /* Forced Software Interrupt */
  73. Npq = 0x40, /* Normal Priority Queue polling */
  74. Hpq = 0x80, /* High Priority Queue polling */
  75. };
  76. enum { /* Imr/Isr */
  77. Rok = 0x0001, /* Receive OK */
  78. Rer = 0x0002, /* Receive Error */
  79. Tok = 0x0004, /* Transmit OK */
  80. Ter = 0x0008, /* Transmit Error */
  81. Rdu = 0x0010, /* Receive Descriptor Unavailable */
  82. Punlc = 0x0020, /* Packet Underrun or Link Change */
  83. Fovw = 0x0040, /* Receive FIFO Overflow */
  84. Tdu = 0x0080, /* Transmit Descriptor Unavailable */
  85. Swint = 0x0100, /* Software Interrupt */
  86. Timeout = 0x4000, /* Timer */
  87. Serr = 0x8000, /* System Error */
  88. };
  89. enum { /* Tcr */
  90. MtxdmaSHIFT = 8, /* Max. DMA Burst Size */
  91. MtxdmaMASK = 0x00000700,
  92. Mtxdmaunlimited = 0x00000700,
  93. Acrc = 0x00010000, /* Append CRC (not) */
  94. Lbk0 = 0x00020000, /* Loopback Test 0 */
  95. Lbk1 = 0x00040000, /* Loopback Test 1 */
  96. Ifg2 = 0x00080000, /* Interframe Gap 2 */
  97. HwveridSHIFT = 23, /* Hardware Version ID */
  98. HwveridMASK = 0x7C800000,
  99. Macv01 = 0x00000000, /* RTL8169 */
  100. Macv02 = 0x00800000, /* RTL8169S/8110S */
  101. Macv03 = 0x04000000, /* RTL8169S/8110S */
  102. Macv04 = 0x10000000, /* RTL8169SB/8110SB */
  103. Macv05 = 0x18000000, /* RTL8169SC/8110SC */
  104. Macv11 = 0x30000000, /* RTL8168B/8111B */
  105. Macv12 = 0x38000000, /* RTL8169B/8111B */
  106. Macv13 = 0x34000000, /* RTL8101E */
  107. Macv14 = 0x30800000, /* RTL8100E */
  108. Macv15 = 0x38800000, /* RTL8100E */
  109. Ifg0 = 0x01000000, /* Interframe Gap 0 */
  110. Ifg1 = 0x02000000, /* Interframe Gap 1 */
  111. };
  112. enum { /* Rcr */
  113. Aap = 0x00000001, /* Accept All Packets */
  114. Apm = 0x00000002, /* Accept Physical Match */
  115. Am = 0x00000004, /* Accept Multicast */
  116. Ab = 0x00000008, /* Accept Broadcast */
  117. Ar = 0x00000010, /* Accept Runt */
  118. Aer = 0x00000020, /* Accept Error */
  119. Sel9356 = 0x00000040, /* 9356 EEPROM used */
  120. MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
  121. MrxdmaMASK = 0x00000700,
  122. Mrxdmaunlimited = 0x00000700,
  123. RxfthSHIFT = 13, /* Receive Buffer Length */
  124. RxfthMASK = 0x0000E000,
  125. Rxfth256 = 0x00008000,
  126. Rxfthnone = 0x0000E000,
  127. Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
  128. MulERINT = 0x01000000, /* Multiple Early Interrupt Select */
  129. };
  130. enum { /* Cr9346 */
  131. Eedo = 0x01, /* */
  132. Eedi = 0x02, /* */
  133. Eesk = 0x04, /* */
  134. Eecs = 0x08, /* */
  135. Eem0 = 0x40, /* Operating Mode */
  136. Eem1 = 0x80,
  137. };
  138. enum { /* Phyar */
  139. DataMASK = 0x0000FFFF, /* 16-bit GMII/MII Register Data */
  140. DataSHIFT = 0,
  141. RegaddrMASK = 0x001F0000, /* 5-bit GMII/MII Register Address */
  142. RegaddrSHIFT = 16,
  143. Flag = 0x80000000, /* */
  144. };
  145. enum { /* Cplusc */
  146. Mulrw = 0x0008, /* PCI Multiple R/W Enable */
  147. Dac = 0x0010, /* PCI Dual Address Cycle Enable */
  148. Rxchksum = 0x0020, /* Receive Checksum Offload Enable */
  149. Rxvlan = 0x0040, /* Receive VLAN De-tagging Enable */
  150. Endian = 0x0200, /* Endian Mode */
  151. };
  152. typedef struct D D; /* Transmit/Receive Descriptor */
  153. struct D {
  154. u32int control;
  155. u32int vlan;
  156. u32int addrlo;
  157. u32int addrhi;
  158. };
  159. enum { /* Transmit Descriptor control */
  160. TxflMASK = 0x0000FFFF, /* Transmit Frame Length */
  161. TxflSHIFT = 0,
  162. Tcps = 0x00010000, /* TCP Checksum Offload */
  163. Udpcs = 0x00020000, /* UDP Checksum Offload */
  164. Ipcs = 0x00040000, /* IP Checksum Offload */
  165. Lgsen = 0x08000000, /* Large Send */
  166. };
  167. enum { /* Receive Descriptor control */
  168. RxflMASK = 0x00003FFF, /* Receive Frame Length */
  169. RxflSHIFT = 0,
  170. Tcpf = 0x00004000, /* TCP Checksum Failure */
  171. Udpf = 0x00008000, /* UDP Checksum Failure */
  172. Ipf = 0x00010000, /* IP Checksum Failure */
  173. Pid0 = 0x00020000, /* Protocol ID0 */
  174. Pid1 = 0x00040000, /* Protocol ID1 */
  175. Crce = 0x00080000, /* CRC Error */
  176. Runt = 0x00100000, /* Runt Packet */
  177. Res = 0x00200000, /* Receive Error Summary */
  178. Rwt = 0x00400000, /* Receive Watchdog Timer Expired */
  179. Fovf = 0x00800000, /* FIFO Overflow */
  180. Bovf = 0x01000000, /* Buffer Overflow */
  181. Bar = 0x02000000, /* Broadcast Address Received */
  182. Pam = 0x04000000, /* Physical Address Matched */
  183. Mar = 0x08000000, /* Multicast Address Received */
  184. };
  185. enum { /* General Descriptor control */
  186. Ls = 0x10000000, /* Last Segment Descriptor */
  187. Fs = 0x20000000, /* First Segment Descriptor */
  188. Eor = 0x40000000, /* End of Descriptor Ring */
  189. Own = 0x80000000, /* Ownership */
  190. };
  191. /*
  192. */
  193. enum { /* Ring sizes (<= 1024) */
  194. Ntd = 32, /* Transmit Ring */
  195. Nrd = 128, /* Receive Ring */
  196. Mps = ROUNDUP(ETHERMAXTU+4, 128),
  197. };
  198. typedef struct Dtcc Dtcc;
  199. struct Dtcc {
  200. u64int txok;
  201. u64int rxok;
  202. u64int txer;
  203. u32int rxer;
  204. u16int misspkt;
  205. u16int fae;
  206. u32int tx1col;
  207. u32int txmcol;
  208. u64int rxokph;
  209. u64int rxokbrd;
  210. u32int rxokmu;
  211. u16int txabt;
  212. u16int txundrn;
  213. };
  214. enum { /* Variants */
  215. Rtl8100e = (0x8136<<16)|0x10EC, /* RTL810[01]E: pci -e */
  216. Rtl8169c = (0x0116<<16)|0x16EC, /* RTL8169C+ (USR997902) */
  217. Rtl8169sc = (0x8167<<16)|0x10EC, /* RTL8169SC */
  218. Rtl8168b = (0x8168<<16)|0x10EC, /* RTL8168B: pci-e */
  219. Rtl8169 = (0x8169<<16)|0x10EC, /* RTL8169 */
  220. };
  221. typedef struct Ctlr Ctlr;
  222. typedef struct Ctlr {
  223. int port;
  224. Pcidev* pcidev;
  225. Ctlr* next;
  226. int active;
  227. QLock alock; /* attach */
  228. Lock ilock; /* init */
  229. int init; /* */
  230. int pciv; /* */
  231. int macv; /* MAC version */
  232. int phyv; /* PHY version */
  233. int pcie; /* flag: pci-express device? */
  234. uvlong mchash; /* multicast hash */
  235. Mii* mii;
  236. Lock tlock; /* transmit */
  237. D* td; /* descriptor ring */
  238. Block** tb; /* transmit buffers */
  239. int ntd;
  240. int tdh; /* head - producer index (host) */
  241. int tdt; /* tail - consumer index (NIC) */
  242. int ntdfree;
  243. int ntq;
  244. int mtps; /* Max. Transmit Packet Size */
  245. Lock rlock; /* receive */
  246. D* rd; /* descriptor ring */
  247. Block** rb; /* receive buffers */
  248. int nrd;
  249. int rdh; /* head - producer index (NIC) */
  250. int rdt; /* tail - consumer index (host) */
  251. int nrdfree;
  252. int tcr; /* transmit configuration register */
  253. int rcr; /* receive configuration register */
  254. int imr;
  255. QLock slock; /* statistics */
  256. Dtcc* dtcc;
  257. uint txdu;
  258. uint tcpf;
  259. uint udpf;
  260. uint ipf;
  261. uint fovf;
  262. uint ierrs;
  263. uint rer;
  264. uint rdu;
  265. uint punlc;
  266. uint fovw;
  267. uint mcast;
  268. } Ctlr;
  269. static Ctlr* rtl8169ctlrhead;
  270. static Ctlr* rtl8169ctlrtail;
  271. #define csr8r(c, r) (inb((c)->port+(r)))
  272. #define csr16r(c, r) (ins((c)->port+(r)))
  273. #define csr32r(c, r) (inl((c)->port+(r)))
  274. #define csr8w(c, r, b) (outb((c)->port+(r), (u8int)(b)))
  275. #define csr16w(c, r, w) (outs((c)->port+(r), (u16int)(w)))
  276. #define csr32w(c, r, l) (outl((c)->port+(r), (u32int)(l)))
  277. static int
  278. rtl8169miimir(Ctlr* ctlr, int pa, int ra)
  279. {
  280. uint r;
  281. int timeo;
  282. if(pa != 1)
  283. return -1;
  284. r = (ra<<16) & RegaddrMASK;
  285. csr32w(ctlr, Phyar, r);
  286. delay(1);
  287. for(timeo = 0; timeo < 2000; timeo++){
  288. if((r = csr32r(ctlr, Phyar)) & Flag)
  289. break;
  290. microdelay(100);
  291. }
  292. if(!(r & Flag))
  293. return -1;
  294. return (r & DataMASK)>>DataSHIFT;
  295. }
  296. static int
  297. rtl8169miimiw(Ctlr* ctlr, int pa, int ra, int data)
  298. {
  299. uint r;
  300. int timeo;
  301. if(pa != 1)
  302. return -1;
  303. r = Flag|((ra<<16) & RegaddrMASK)|((data<<DataSHIFT) & DataMASK);
  304. csr32w(ctlr, Phyar, r);
  305. delay(1);
  306. for(timeo = 0; timeo < 2000; timeo++){
  307. if(!((r = csr32r(ctlr, Phyar)) & Flag))
  308. break;
  309. microdelay(100);
  310. }
  311. if(r & Flag)
  312. return -1;
  313. return 0;
  314. }
  315. static int
  316. rtl8169miirw(Mii* mii, int write, int pa, int ra, int data)
  317. {
  318. if(write)
  319. return rtl8169miimiw(mii->ctlr, pa, ra, data);
  320. return rtl8169miimir(mii->ctlr, pa, ra);
  321. }
  322. static Mii*
  323. rtl8169mii(Ctlr* ctlr)
  324. {
  325. Mii* mii;
  326. MiiPhy *phy;
  327. /*
  328. * Link management.
  329. *
  330. * Get rev number out of Phyidr2 so can config properly.
  331. * There's probably more special stuff for Macv0[234] needed here.
  332. */
  333. ctlr->phyv = rtl8169miimir(ctlr, 1, Phyidr2) & 0x0F;
  334. if(ctlr->macv == Macv02){
  335. csr8w(ctlr, 0x82, 1); /* magic */
  336. rtl8169miimiw(ctlr, 1, 0x0B, 0x0000); /* magic */
  337. }
  338. if((mii = miiattach(ctlr, (1<<1), rtl8169miirw)) == nil)
  339. return nil;
  340. phy = mii->curphy;
  341. print("oui %#ux phyno %d, macv = %#8.8ux phyv = %#4.4ux\n",
  342. phy->oui, phy->phyno, ctlr->macv, ctlr->phyv);
  343. if(miistatus(mii) < 0){
  344. miireset(mii);
  345. miiane(mii, ~0, ~0, ~0);
  346. }
  347. return mii;
  348. }
  349. static void
  350. rtl8169promiscuous(void* arg, int on)
  351. {
  352. Ether *edev;
  353. Ctlr * ctlr;
  354. edev = arg;
  355. ctlr = edev->ctlr;
  356. ilock(&ctlr->ilock);
  357. if(on)
  358. ctlr->rcr |= Aap;
  359. else
  360. ctlr->rcr &= ~Aap;
  361. csr32w(ctlr, Rcr, ctlr->rcr);
  362. iunlock(&ctlr->ilock);
  363. }
  364. enum {
  365. /* everyone else uses 0x04c11db7, but they both produce the same crc */
  366. Etherpolybe = 0x04c11db6,
  367. Bytemask = (1<<8) - 1,
  368. };
  369. static ulong
  370. ethercrcbe(uchar *addr, long len)
  371. {
  372. int i, j;
  373. ulong c, crc, carry;
  374. crc = ~0UL;
  375. for (i = 0; i < len; i++) {
  376. c = addr[i];
  377. for (j = 0; j < 8; j++) {
  378. carry = ((crc & (1UL << 31))? 1: 0) ^ (c & 1);
  379. crc <<= 1;
  380. c >>= 1;
  381. if (carry)
  382. crc = (crc ^ Etherpolybe) | carry;
  383. }
  384. }
  385. return crc;
  386. }
  387. static ulong
  388. swabl(ulong l)
  389. {
  390. return l>>24 | (l>>8) & (Bytemask<<8) |
  391. (l<<8) & (Bytemask<<16) | l<<24;
  392. }
  393. static void
  394. rtl8169multicast(void* ether, uchar *eaddr, int add)
  395. {
  396. Ether *edev;
  397. Ctlr *ctlr;
  398. if (!add)
  399. return; /* ok to keep receiving on old mcast addrs */
  400. edev = ether;
  401. ctlr = edev->ctlr;
  402. ilock(&ctlr->ilock);
  403. ctlr->mchash |= 1ULL << (ethercrcbe(eaddr, Eaddrlen) >> 26);
  404. ctlr->rcr |= Am;
  405. csr32w(ctlr, Rcr, ctlr->rcr);
  406. /* pci-e variants reverse the order of the hash byte registers */
  407. if (ctlr->pcie) {
  408. csr32w(ctlr, Mar0, swabl(ctlr->mchash>>32));
  409. csr32w(ctlr, Mar0+4, swabl(ctlr->mchash));
  410. } else {
  411. csr32w(ctlr, Mar0, ctlr->mchash);
  412. csr32w(ctlr, Mar0+4, ctlr->mchash>>32);
  413. }
  414. iunlock(&ctlr->ilock);
  415. }
  416. static long
  417. rtl8169ifstat(Ether* edev, void* a, long n, ulong offset)
  418. {
  419. Ctlr *ctlr;
  420. Dtcc *dtcc;
  421. int timeo;
  422. char *alloc, *e, *p;
  423. ctlr = edev->ctlr;
  424. qlock(&ctlr->slock);
  425. alloc = nil;
  426. if(waserror()){
  427. qunlock(&ctlr->slock);
  428. free(alloc);
  429. nexterror();
  430. }
  431. csr32w(ctlr, Dtccr+4, 0);
  432. csr32w(ctlr, Dtccr, PCIWADDR(ctlr->dtcc)|Cmd);
  433. for(timeo = 0; timeo < 1000; timeo++){
  434. if(!(csr32r(ctlr, Dtccr) & Cmd))
  435. break;
  436. delay(1);
  437. }
  438. if(csr32r(ctlr, Dtccr) & Cmd)
  439. error(Eio);
  440. dtcc = ctlr->dtcc;
  441. edev->oerrs = dtcc->txer;
  442. edev->crcs = dtcc->rxer;
  443. edev->frames = dtcc->fae;
  444. edev->buffs = dtcc->misspkt;
  445. edev->overflows = ctlr->txdu+ctlr->rdu;
  446. if(n == 0){
  447. qunlock(&ctlr->slock);
  448. poperror();
  449. return 0;
  450. }
  451. if((alloc = malloc(READSTR)) == nil)
  452. error(Enomem);
  453. e = alloc+READSTR;
  454. p = seprint(alloc, e, "TxOk: %llud\n", dtcc->txok);
  455. p = seprint(p, e, "RxOk: %llud\n", dtcc->rxok);
  456. p = seprint(p, e, "TxEr: %llud\n", dtcc->txer);
  457. p = seprint(p, e, "RxEr: %ud\n", dtcc->rxer);
  458. p = seprint(p, e, "MissPkt: %ud\n", dtcc->misspkt);
  459. p = seprint(p, e, "FAE: %ud\n", dtcc->fae);
  460. p = seprint(p, e, "Tx1Col: %ud\n", dtcc->tx1col);
  461. p = seprint(p, e, "TxMCol: %ud\n", dtcc->txmcol);
  462. p = seprint(p, e, "RxOkPh: %llud\n", dtcc->rxokph);
  463. p = seprint(p, e, "RxOkBrd: %llud\n", dtcc->rxokbrd);
  464. p = seprint(p, e, "RxOkMu: %ud\n", dtcc->rxokmu);
  465. p = seprint(p, e, "TxAbt: %ud\n", dtcc->txabt);
  466. p = seprint(p, e, "TxUndrn: %ud\n", dtcc->txundrn);
  467. p = seprint(p, e, "txdu: %ud\n", ctlr->txdu);
  468. p = seprint(p, e, "tcpf: %ud\n", ctlr->tcpf);
  469. p = seprint(p, e, "udpf: %ud\n", ctlr->udpf);
  470. p = seprint(p, e, "ipf: %ud\n", ctlr->ipf);
  471. p = seprint(p, e, "fovf: %ud\n", ctlr->fovf);
  472. p = seprint(p, e, "ierrs: %ud\n", ctlr->ierrs);
  473. p = seprint(p, e, "rer: %ud\n", ctlr->rer);
  474. p = seprint(p, e, "rdu: %ud\n", ctlr->rdu);
  475. p = seprint(p, e, "punlc: %ud\n", ctlr->punlc);
  476. p = seprint(p, e, "fovw: %ud\n", ctlr->fovw);
  477. p = seprint(p, e, "tcr: %#8.8ux\n", ctlr->tcr);
  478. p = seprint(p, e, "rcr: %#8.8ux\n", ctlr->rcr);
  479. p = seprint(p, e, "multicast: %ud\n", ctlr->mcast);
  480. if(ctlr->mii != nil && ctlr->mii->curphy != nil)
  481. miidumpphy(ctlr->mii, p, e);
  482. n = readstr(offset, a, n, alloc);
  483. qunlock(&ctlr->slock);
  484. poperror();
  485. free(alloc);
  486. return n;
  487. }
  488. static void
  489. rtl8169halt(Ctlr* ctlr)
  490. {
  491. csr8w(ctlr, Cr, 0);
  492. csr16w(ctlr, Imr, 0);
  493. csr16w(ctlr, Isr, ~0);
  494. }
  495. static int
  496. rtl8169reset(Ctlr* ctlr)
  497. {
  498. u32int r;
  499. int timeo;
  500. /*
  501. * Soft reset the controller.
  502. */
  503. csr8w(ctlr, Cr, Rst);
  504. for(r = timeo = 0; timeo < 1000; timeo++){
  505. r = csr8r(ctlr, Cr);
  506. if(!(r & Rst))
  507. break;
  508. delay(1);
  509. }
  510. rtl8169halt(ctlr);
  511. if(r & Rst)
  512. return -1;
  513. return 0;
  514. }
  515. static void
  516. rtl8169replenish(Ctlr* ctlr)
  517. {
  518. D *d;
  519. int rdt;
  520. Block *bp;
  521. rdt = ctlr->rdt;
  522. while(NEXT(rdt, ctlr->nrd) != ctlr->rdh){
  523. d = &ctlr->rd[rdt];
  524. if(ctlr->rb[rdt] == nil){
  525. /*
  526. * Simple allocation for now.
  527. * This better be aligned on 8.
  528. */
  529. bp = iallocb(Mps);
  530. if(bp == nil){
  531. iprint("no available buffers\n");
  532. break;
  533. }
  534. ctlr->rb[rdt] = bp;
  535. d->addrlo = PCIWADDR(bp->rp);
  536. d->addrhi = 0;
  537. }
  538. coherence();
  539. d->control |= Own|Mps;
  540. rdt = NEXT(rdt, ctlr->nrd);
  541. ctlr->nrdfree++;
  542. }
  543. ctlr->rdt = rdt;
  544. }
  545. static int
  546. rtl8169init(Ether* edev)
  547. {
  548. int i;
  549. u32int r;
  550. Block *bp;
  551. Ctlr *ctlr;
  552. u8int cplusc;
  553. ctlr = edev->ctlr;
  554. ilock(&ctlr->ilock);
  555. rtl8169halt(ctlr);
  556. /*
  557. * MAC Address.
  558. * Must put chip into config register write enable mode.
  559. */
  560. csr8w(ctlr, Cr9346, Eem1|Eem0);
  561. r = (edev->ea[3]<<24)|(edev->ea[2]<<16)|(edev->ea[1]<<8)|edev->ea[0];
  562. csr32w(ctlr, Idr0, r);
  563. r = (edev->ea[5]<<8)|edev->ea[4];
  564. csr32w(ctlr, Idr0+4, r);
  565. /*
  566. * Transmitter.
  567. */
  568. memset(ctlr->td, 0, sizeof(D)*ctlr->ntd);
  569. ctlr->tdh = ctlr->tdt = 0;
  570. ctlr->td[ctlr->ntd-1].control = Eor;
  571. /*
  572. * Receiver.
  573. * Need to do something here about the multicast filter.
  574. */
  575. memset(ctlr->rd, 0, sizeof(D)*ctlr->nrd);
  576. ctlr->nrdfree = ctlr->rdh = ctlr->rdt = 0;
  577. ctlr->rd[ctlr->nrd-1].control = Eor;
  578. for(i = 0; i < ctlr->nrd; i++){
  579. if((bp = ctlr->rb[i]) != nil){
  580. ctlr->rb[i] = nil;
  581. freeb(bp);
  582. }
  583. }
  584. rtl8169replenish(ctlr);
  585. ctlr->rcr = Rxfthnone|Mrxdmaunlimited|Ab|Am|Apm;
  586. /*
  587. * Mtps is in units of 128 except for the RTL8169
  588. * where is is 32. If using jumbo frames should be
  589. * set to 0x3F.
  590. * Setting Mulrw in Cplusc disables the Tx/Rx DMA burst
  591. * settings in Tcr/Rcr; the (1<<14) is magic.
  592. */
  593. ctlr->mtps = HOWMANY(Mps, 128);
  594. cplusc = csr16r(ctlr, Cplusc) & ~(1<<14);
  595. cplusc |= /*Rxchksum|*/Mulrw;
  596. switch(ctlr->macv){
  597. default:
  598. return -1;
  599. case Macv01:
  600. ctlr->mtps = HOWMANY(Mps, 32);
  601. break;
  602. case Macv02:
  603. case Macv03:
  604. cplusc |= (1<<14); /* magic */
  605. break;
  606. case Macv05:
  607. /*
  608. * This is interpreted from clearly bogus code
  609. * in the manufacturer-supplied driver, it could
  610. * be wrong. Untested.
  611. */
  612. r = csr8r(ctlr, Config2) & 0x07;
  613. if(r == 0x01) /* 66MHz PCI */
  614. csr32w(ctlr, 0x7C, 0x0007FFFF); /* magic */
  615. else
  616. csr32w(ctlr, 0x7C, 0x0007FF00); /* magic */
  617. pciclrmwi(ctlr->pcidev);
  618. break;
  619. case Macv13:
  620. /*
  621. * This is interpreted from clearly bogus code
  622. * in the manufacturer-supplied driver, it could
  623. * be wrong. Untested.
  624. */
  625. pcicfgw8(ctlr->pcidev, 0x68, 0x00); /* magic */
  626. pcicfgw8(ctlr->pcidev, 0x69, 0x08); /* magic */
  627. break;
  628. case Macv04:
  629. case Macv11:
  630. case Macv12:
  631. case Macv14:
  632. case Macv15:
  633. break;
  634. }
  635. /*
  636. * Enable receiver/transmitter.
  637. * Need to do this first or some of the settings below
  638. * won't take.
  639. */
  640. switch(ctlr->pciv){
  641. default:
  642. csr8w(ctlr, Cr, Te|Re);
  643. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  644. csr32w(ctlr, Rcr, ctlr->rcr);
  645. csr32w(ctlr, Mar0, 0);
  646. csr32w(ctlr, Mar0+4, 0);
  647. ctlr->mchash = 0;
  648. case Rtl8169sc:
  649. case Rtl8168b:
  650. break;
  651. }
  652. /*
  653. * Interrupts.
  654. * Disable Tdu|Tok for now, the transmit routine will tidy.
  655. * Tdu means the NIC ran out of descriptors to send, so it
  656. * doesn't really need to ever be on.
  657. */
  658. csr32w(ctlr, Timerint, 0);
  659. ctlr->imr = Serr|Timeout|Fovw|Punlc|Rdu|Ter|Rer|Rok;
  660. csr16w(ctlr, Imr, ctlr->imr);
  661. /*
  662. * Clear missed-packet counter;
  663. * initial early transmit threshold value;
  664. * set the descriptor ring base addresses;
  665. * set the maximum receive packet size;
  666. * no early-receive interrupts.
  667. */
  668. csr32w(ctlr, Mpc, 0);
  669. csr8w(ctlr, Mtps, ctlr->mtps);
  670. csr32w(ctlr, Tnpds+4, 0);
  671. csr32w(ctlr, Tnpds, PCIWADDR(ctlr->td));
  672. csr32w(ctlr, Rdsar+4, 0);
  673. csr32w(ctlr, Rdsar, PCIWADDR(ctlr->rd));
  674. csr16w(ctlr, Rms, Mps);
  675. r = csr16r(ctlr, Mulint) & 0xF000;
  676. csr16w(ctlr, Mulint, r);
  677. csr16w(ctlr, Cplusc, cplusc);
  678. /*
  679. * Set configuration.
  680. */
  681. switch(ctlr->pciv){
  682. default:
  683. break;
  684. case Rtl8169sc:
  685. csr16w(ctlr, 0xE2, 0); /* magic */
  686. csr8w(ctlr, Cr, Te|Re);
  687. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  688. csr32w(ctlr, Rcr, ctlr->rcr);
  689. break;
  690. case Rtl8168b:
  691. case Rtl8169c:
  692. csr16w(ctlr, 0xE2, 0); /* magic */
  693. csr16w(ctlr, Cplusc, 0x2000); /* magic */
  694. csr8w(ctlr, Cr, Te|Re);
  695. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  696. csr32w(ctlr, Rcr, ctlr->rcr);
  697. csr16w(ctlr, Rms, 0x0800);
  698. csr8w(ctlr, Mtps, 0x3F);
  699. break;
  700. }
  701. ctlr->tcr = csr32r(ctlr, Tcr);
  702. csr8w(ctlr, Cr9346, 0);
  703. iunlock(&ctlr->ilock);
  704. // rtl8169mii(ctlr);
  705. return 0;
  706. }
  707. static void
  708. rtl8169attach(Ether* edev)
  709. {
  710. int timeo;
  711. Ctlr *ctlr;
  712. MiiPhy *phy;
  713. ctlr = edev->ctlr;
  714. qlock(&ctlr->alock);
  715. if(ctlr->init == 0){
  716. /*
  717. * Handle allocation/init errors here.
  718. */
  719. ctlr->td = mallocalign(sizeof(D)*Ntd, 256, 0, 0);
  720. ctlr->tb = malloc(Ntd*sizeof(Block*));
  721. ctlr->ntd = Ntd;
  722. ctlr->rd = mallocalign(sizeof(D)*Nrd, 256, 0, 0);
  723. ctlr->rb = malloc(Nrd*sizeof(Block*));
  724. ctlr->nrd = Nrd;
  725. ctlr->dtcc = mallocalign(sizeof(Dtcc), 64, 0, 0);
  726. rtl8169init(edev);
  727. ctlr->init = 1;
  728. }
  729. qunlock(&ctlr->alock);
  730. /*
  731. * Wait for link to be ready.
  732. */
  733. for(timeo = 0; timeo < 350; timeo++){
  734. if(miistatus(ctlr->mii) == 0)
  735. break;
  736. tsleep(&up->sleep, return0, 0, 10);
  737. }
  738. phy = ctlr->mii->curphy;
  739. print("%s: speed %d fd %d link %d rfc %d tfc %d\n",
  740. edev->name, phy->speed, phy->fd, phy->link, phy->rfc, phy->tfc);
  741. }
  742. static void
  743. rtl8169link(Ether* edev)
  744. {
  745. int limit;
  746. Ctlr *ctlr;
  747. MiiPhy *phy;
  748. ctlr = edev->ctlr;
  749. /*
  750. * Maybe the link changed - do we care very much?
  751. * Could stall transmits if no link, maybe?
  752. */
  753. if(ctlr->mii == nil || ctlr->mii->curphy == nil)
  754. return;
  755. phy = ctlr->mii->curphy;
  756. if(miistatus(ctlr->mii) < 0){
  757. iprint("%slink n: speed %d fd %d link %d rfc %d tfc %d\n",
  758. edev->name, phy->speed, phy->fd, phy->link,
  759. phy->rfc, phy->tfc);
  760. edev->link = 0;
  761. return;
  762. }
  763. edev->link = 1;
  764. limit = 256*1024;
  765. if(phy->speed == 10){
  766. edev->mbps = 10;
  767. limit = 65*1024;
  768. }
  769. else if(phy->speed == 100)
  770. edev->mbps = 100;
  771. else if(phy->speed == 1000)
  772. edev->mbps = 1000;
  773. iprint("%slink y: speed %d fd %d link %d rfc %d tfc %d\n",
  774. edev->name, phy->speed, phy->fd, phy->link,
  775. phy->rfc, phy->tfc);
  776. if(edev->oq != nil)
  777. qsetlimit(edev->oq, limit);
  778. }
  779. static void
  780. rtl8169transmit(Ether* edev)
  781. {
  782. D *d;
  783. Block *bp;
  784. Ctlr *ctlr;
  785. int control, x;
  786. ctlr = edev->ctlr;
  787. ilock(&ctlr->tlock);
  788. for(x = ctlr->tdh; ctlr->ntq > 0; x = NEXT(x, ctlr->ntd)){
  789. d = &ctlr->td[x];
  790. if((control = d->control) & Own)
  791. break;
  792. /*
  793. * Check errors and log here.
  794. */
  795. USED(control);
  796. /*
  797. * Free it up.
  798. * Need to clean the descriptor here? Not really.
  799. * Simple freeb for now (no chain and freeblist).
  800. * Use ntq count for now.
  801. */
  802. freeb(ctlr->tb[x]);
  803. ctlr->tb[x] = nil;
  804. d->control &= Eor;
  805. ctlr->ntq--;
  806. }
  807. ctlr->tdh = x;
  808. x = ctlr->tdt;
  809. while(ctlr->ntq < (ctlr->ntd-1)){
  810. if((bp = qget(edev->oq)) == nil)
  811. break;
  812. d = &ctlr->td[x];
  813. d->addrlo = PCIWADDR(bp->rp);
  814. d->addrhi = 0;
  815. ctlr->tb[x] = bp;
  816. coherence();
  817. d->control |= Own|Fs|Ls|((BLEN(bp)<<TxflSHIFT) & TxflMASK);
  818. x = NEXT(x, ctlr->ntd);
  819. ctlr->ntq++;
  820. }
  821. if(x != ctlr->tdt){
  822. ctlr->tdt = x;
  823. csr8w(ctlr, Tppoll, Npq);
  824. }
  825. else if(ctlr->ntq >= (ctlr->ntd-1))
  826. ctlr->txdu++;
  827. iunlock(&ctlr->tlock);
  828. }
  829. static void
  830. rtl8169receive(Ether* edev)
  831. {
  832. D *d;
  833. int rdh;
  834. Block *bp;
  835. Ctlr *ctlr;
  836. u32int control;
  837. ctlr = edev->ctlr;
  838. rdh = ctlr->rdh;
  839. for(;;){
  840. d = &ctlr->rd[rdh];
  841. if(d->control & Own)
  842. break;
  843. control = d->control;
  844. if((control & (Fs|Ls|Res)) == (Fs|Ls)){
  845. bp = ctlr->rb[rdh];
  846. ctlr->rb[rdh] = nil;
  847. bp->wp = bp->rp + ((control & RxflMASK)>>RxflSHIFT)-4;
  848. bp->next = nil;
  849. if(control & Fovf)
  850. ctlr->fovf++;
  851. if(control & Mar)
  852. ctlr->mcast++;
  853. switch(control & (Pid1|Pid0)){
  854. default:
  855. break;
  856. case Pid0:
  857. if(control & Tcpf){
  858. ctlr->tcpf++;
  859. break;
  860. }
  861. bp->flag |= Btcpck;
  862. break;
  863. case Pid1:
  864. if(control & Udpf){
  865. ctlr->udpf++;
  866. break;
  867. }
  868. bp->flag |= Budpck;
  869. break;
  870. case Pid1|Pid0:
  871. if(control & Ipf){
  872. ctlr->ipf++;
  873. break;
  874. }
  875. bp->flag |= Bipck;
  876. break;
  877. }
  878. etheriq(edev, bp, 1);
  879. }
  880. else{
  881. /*
  882. * Error stuff here.
  883. print("control %#8.8ux\n", control);
  884. */
  885. }
  886. d->control &= Eor;
  887. ctlr->nrdfree--;
  888. rdh = NEXT(rdh, ctlr->nrd);
  889. if(ctlr->nrdfree < ctlr->nrd/2)
  890. rtl8169replenish(ctlr);
  891. }
  892. ctlr->rdh = rdh;
  893. }
  894. static void
  895. rtl8169interrupt(Ureg*, void* arg)
  896. {
  897. Ctlr *ctlr;
  898. Ether *edev;
  899. u32int isr;
  900. edev = arg;
  901. ctlr = edev->ctlr;
  902. while((isr = csr16r(ctlr, Isr)) != 0 && isr != 0xFFFF){
  903. csr16w(ctlr, Isr, isr);
  904. if((isr & ctlr->imr) == 0)
  905. break;
  906. if(isr & (Fovw|Punlc|Rdu|Rer|Rok)){
  907. rtl8169receive(edev);
  908. if(!(isr & (Punlc|Rok)))
  909. ctlr->ierrs++;
  910. if(isr & Rer)
  911. ctlr->rer++;
  912. if(isr & Rdu)
  913. ctlr->rdu++;
  914. if(isr & Punlc)
  915. ctlr->punlc++;
  916. if(isr & Fovw)
  917. ctlr->fovw++;
  918. isr &= ~(Fovw|Rdu|Rer|Rok);
  919. }
  920. if(isr & (Tdu|Ter|Tok)){
  921. rtl8169transmit(edev);
  922. isr &= ~(Tdu|Ter|Tok);
  923. }
  924. if(isr & Punlc){
  925. rtl8169link(edev);
  926. isr &= ~Punlc;
  927. }
  928. /*
  929. * Some of the reserved bits get set sometimes...
  930. */
  931. if(isr & (Serr|Timeout|Tdu|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok))
  932. panic("rtl8169interrupt: imr %#4.4ux isr %#4.4ux\n",
  933. csr16r(ctlr, Imr), isr);
  934. }
  935. }
  936. static void
  937. rtl8169pci(void)
  938. {
  939. Pcidev *p;
  940. Ctlr *ctlr;
  941. int i, port, pcie;
  942. p = nil;
  943. while(p = pcimatch(p, 0, 0)){
  944. if(p->ccrb != 0x02 || p->ccru != 0)
  945. continue;
  946. pcie = 0;
  947. switch(i = ((p->did<<16)|p->vid)){
  948. default:
  949. continue;
  950. case Rtl8100e: /* RTL810[01]E ? */
  951. case Rtl8168b: /* RTL8168B */
  952. pcie = 1;
  953. break;
  954. case Rtl8169c: /* RTL8169C */
  955. case Rtl8169sc: /* RTL8169SC */
  956. case Rtl8169: /* RTL8169 */
  957. break;
  958. case (0xC107<<16)|0x1259: /* Corega CG-LAPCIGT */
  959. i = Rtl8169;
  960. break;
  961. }
  962. port = p->mem[0].bar & ~0x01;
  963. if(ioalloc(port, p->mem[0].size, 0, "rtl8169") < 0){
  964. print("rtl8169: port %#ux in use\n", port);
  965. continue;
  966. }
  967. ctlr = malloc(sizeof(Ctlr));
  968. ctlr->port = port;
  969. ctlr->pcidev = p;
  970. ctlr->pciv = i;
  971. ctlr->pcie = pcie;
  972. if(pcigetpms(p) > 0){
  973. pcisetpms(p, 0);
  974. for(i = 0; i < 6; i++)
  975. pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
  976. pcicfgw8(p, PciINTL, p->intl);
  977. pcicfgw8(p, PciLTR, p->ltr);
  978. pcicfgw8(p, PciCLS, p->cls);
  979. pcicfgw16(p, PciPCR, p->pcr);
  980. }
  981. if(rtl8169reset(ctlr)){
  982. iofree(port);
  983. free(ctlr);
  984. continue;
  985. }
  986. /*
  987. * Extract the chip hardware version,
  988. * needed to configure each properly.
  989. */
  990. ctlr->macv = csr32r(ctlr, Tcr) & HwveridMASK;
  991. if((ctlr->mii = rtl8169mii(ctlr)) == nil){
  992. iofree(port);
  993. free(ctlr);
  994. continue;
  995. }
  996. pcisetbme(p);
  997. if(rtl8169ctlrhead != nil)
  998. rtl8169ctlrtail->next = ctlr;
  999. else
  1000. rtl8169ctlrhead = ctlr;
  1001. rtl8169ctlrtail = ctlr;
  1002. }
  1003. }
  1004. static int
  1005. rtl8169pnp(Ether* edev)
  1006. {
  1007. u32int r;
  1008. Ctlr *ctlr;
  1009. uchar ea[Eaddrlen];
  1010. if(rtl8169ctlrhead == nil)
  1011. rtl8169pci();
  1012. /*
  1013. * Any adapter matches if no edev->port is supplied,
  1014. * otherwise the ports must match.
  1015. */
  1016. for(ctlr = rtl8169ctlrhead; ctlr != nil; ctlr = ctlr->next){
  1017. if(ctlr->active)
  1018. continue;
  1019. if(edev->port == 0 || edev->port == ctlr->port){
  1020. ctlr->active = 1;
  1021. break;
  1022. }
  1023. }
  1024. if(ctlr == nil)
  1025. return -1;
  1026. edev->ctlr = ctlr;
  1027. edev->port = ctlr->port;
  1028. edev->irq = ctlr->pcidev->intl;
  1029. edev->tbdf = ctlr->pcidev->tbdf;
  1030. edev->mbps = 100;
  1031. /*
  1032. * Check if the adapter's station address is to be overridden.
  1033. * If not, read it from the device and set in edev->ea.
  1034. */
  1035. memset(ea, 0, Eaddrlen);
  1036. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  1037. r = csr32r(ctlr, Idr0);
  1038. edev->ea[0] = r;
  1039. edev->ea[1] = r>>8;
  1040. edev->ea[2] = r>>16;
  1041. edev->ea[3] = r>>24;
  1042. r = csr32r(ctlr, Idr0+4);
  1043. edev->ea[4] = r;
  1044. edev->ea[5] = r>>8;
  1045. }
  1046. edev->attach = rtl8169attach;
  1047. edev->transmit = rtl8169transmit;
  1048. edev->interrupt = rtl8169interrupt;
  1049. edev->ifstat = rtl8169ifstat;
  1050. edev->arg = edev;
  1051. edev->promiscuous = rtl8169promiscuous;
  1052. edev->multicast = rtl8169multicast;
  1053. // edev->shutdown = rtl8169shutdown;
  1054. rtl8169link(edev);
  1055. return 0;
  1056. }
  1057. void
  1058. ether8169link(void)
  1059. {
  1060. addethercard("rtl8169", rtl8169pnp);
  1061. }