ether8169.c 25 KB

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