ether8169.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. Ifg0 = 0x01000000, /* Interframe Gap 0 */
  93. Ifg1 = 0x02000000, /* Interframe Gap 1 */
  94. };
  95. enum { /* Rcr */
  96. Aap = 0x00000001, /* Accept All Packets */
  97. Apm = 0x00000002, /* Accept Physical Match */
  98. Am = 0x00000004, /* Accept Multicast */
  99. Ab = 0x00000008, /* Accept Broadcast */
  100. Ar = 0x00000010, /* Accept Runt */
  101. Aer = 0x00000020, /* Accept Error */
  102. Sel9356 = 0x00000040, /* 9356 EEPROM used */
  103. MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
  104. MrxdmaMASK = 0x00000700,
  105. Mrxdmaunlimited = 0x00000700,
  106. RxfthSHIFT = 13, /* Receive Buffer Length */
  107. RxfthMASK = 0x0000E000,
  108. Rxfth256 = 0x00008000,
  109. Rxfthnone = 0x0000E000,
  110. Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
  111. MulERINT = 0x01000000, /* Multiple Early Interrupt Select */
  112. };
  113. enum { /* Cr9346 */
  114. Eedo = 0x01, /* */
  115. Eedi = 0x02, /* */
  116. Eesk = 0x04, /* */
  117. Eecs = 0x08, /* */
  118. Eem0 = 0x40, /* Operating Mode */
  119. Eem1 = 0x80,
  120. };
  121. enum { /* Phyar */
  122. DataMASK = 0x0000FFFF, /* 16-bit GMII/MII Register Data */
  123. DataSHIFT = 0,
  124. RegaddrMASK = 0x001F0000, /* 5-bit GMII/MII Register Address */
  125. RegaddrSHIFT = 16,
  126. Flag = 0x80000000, /* */
  127. };
  128. enum { /* Phystatus */
  129. Fd = 0x01, /* Full Duplex */
  130. Linksts = 0x02, /* Link Status */
  131. Speed10 = 0x04, /* */
  132. Speed100 = 0x08, /* */
  133. Speed1000 = 0x10, /* */
  134. Rxflow = 0x20, /* */
  135. Txflow = 0x40, /* */
  136. Entbi = 0x80, /* */
  137. };
  138. enum { /* Cplusc */
  139. Mulrw = 0x0008, /* PCI Multiple R/W Enable */
  140. Dac = 0x0010, /* PCI Dual Address Cycle Enable */
  141. Rxchksum = 0x0020, /* Receive Checksum Offload Enable */
  142. Rxvlan = 0x0040, /* Receive VLAN De-tagging Enable */
  143. Endian = 0x0200, /* Endian Mode */
  144. };
  145. typedef struct D D; /* Transmit/Receive Descriptor */
  146. struct D {
  147. u32int control;
  148. u32int vlan;
  149. u32int addrlo;
  150. u32int addrhi;
  151. };
  152. enum { /* Transmit Descriptor control */
  153. TxflMASK = 0x0000FFFF, /* Transmit Frame Length */
  154. TxflSHIFT = 0,
  155. Tcps = 0x00010000, /* TCP Checksum Offload */
  156. Udpcs = 0x00020000, /* UDP Checksum Offload */
  157. Ipcs = 0x00040000, /* IP Checksum Offload */
  158. Lgsen = 0x08000000, /* Large Send */
  159. };
  160. enum { /* Receive Descriptor control */
  161. RxflMASK = 0x00003FFF, /* Receive Frame Length */
  162. RxflSHIFT = 0,
  163. Tcpf = 0x00004000, /* TCP Checksum Failure */
  164. Udpf = 0x00008000, /* UDP Checksum Failure */
  165. Ipf = 0x00010000, /* IP Checksum Failure */
  166. Pid0 = 0x00020000, /* Protocol ID0 */
  167. Pid1 = 0x00040000, /* Protocol ID1 */
  168. Crce = 0x00080000, /* CRC Error */
  169. Runt = 0x00100000, /* Runt Packet */
  170. Res = 0x00200000, /* Receive Error Summary */
  171. Rwt = 0x00400000, /* Receive Watchdog Timer Expired */
  172. Fovf = 0x00800000, /* FIFO Overflow */
  173. Bovf = 0x01000000, /* Buffer Overflow */
  174. Bar = 0x02000000, /* Broadcast Address Received */
  175. Pam = 0x04000000, /* Physical Address Matched */
  176. Mar = 0x08000000, /* Multicast Address Received */
  177. };
  178. enum { /* General Descriptor control */
  179. Ls = 0x10000000, /* Last Segment Descriptor */
  180. Fs = 0x20000000, /* First Segment Descriptor */
  181. Eor = 0x40000000, /* End of Descriptor Ring */
  182. Own = 0x80000000, /* Ownership */
  183. };
  184. /*
  185. */
  186. enum { /* Ring sizes (<= 1024) */
  187. Ntd = 128, /* Transmit Ring */
  188. Nrd = 64, /* Receive Ring */
  189. Mps = ROUNDUP(ETHERMAXTU+4, 128),
  190. };
  191. typedef struct Dtcc Dtcc;
  192. struct Dtcc {
  193. u64int txok;
  194. u64int rxok;
  195. u64int txer;
  196. u32int rxer;
  197. u16int misspkt;
  198. u16int fae;
  199. u32int tx1col;
  200. u32int txmcol;
  201. u64int rxokph;
  202. u64int rxokbrd;
  203. u32int rxokmu;
  204. u16int txabt;
  205. u16int txundrn;
  206. };
  207. typedef struct Ctlr Ctlr;
  208. typedef struct Ctlr {
  209. int port;
  210. Pcidev* pcidev;
  211. Ctlr* next;
  212. int active;
  213. uint id;
  214. QLock alock; /* attach */
  215. Lock ilock; /* init */
  216. int init; /* */
  217. Mii* mii;
  218. Lock tlock; /* transmit */
  219. D* td; /* descriptor ring */
  220. Block** tb; /* transmit buffers */
  221. int ntd;
  222. int tdh; /* head - producer index (host) */
  223. int tdt; /* tail - consumer index (NIC) */
  224. int ntdfree;
  225. int ntq;
  226. int mtps; /* Max. Transmit Packet Size */
  227. Lock rlock; /* receive */
  228. D* rd; /* descriptor ring */
  229. Block** rb; /* receive buffers */
  230. int nrd;
  231. int rdh; /* head - producer index (NIC) */
  232. int rdt; /* tail - consumer index (host) */
  233. int nrdfree;
  234. int tcr; /* transmit configuration register */
  235. int rcr; /* receive configuration register */
  236. QLock slock; /* statistics */
  237. Dtcc* dtcc;
  238. uint txdu;
  239. uint tcpf;
  240. uint udpf;
  241. uint ipf;
  242. uint fovf;
  243. uint ierrs;
  244. uint rer;
  245. uint rdu;
  246. uint punlc;
  247. uint fovw;
  248. } Ctlr;
  249. static Ctlr* ctlrhead;
  250. static Ctlr* ctlrtail;
  251. #define csr8r(c, r) (inb((c)->port+(r)))
  252. #define csr16r(c, r) (ins((c)->port+(r)))
  253. #define csr32r(c, r) (inl((c)->port+(r)))
  254. #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
  255. #define csr16w(c, r, w) (outs((c)->port+(r), (ushort)(w)))
  256. #define csr32w(c, r, l) (outl((c)->port+(r), (ulong)(l)))
  257. static int
  258. rtl8169miimir(Mii* mii, int pa, int ra)
  259. {
  260. uint r;
  261. int timeo;
  262. Ctlr *ctlr;
  263. if(pa != 1)
  264. return -1;
  265. ctlr = mii->ctlr;
  266. r = (ra<<16) & RegaddrMASK;
  267. csr32w(ctlr, Phyar, r);
  268. delay(1);
  269. for(timeo = 0; timeo < 2000; timeo++){
  270. if((r = csr32r(ctlr, Phyar)) & Flag)
  271. break;
  272. microdelay(100);
  273. }
  274. if(!(r & Flag))
  275. return -1;
  276. return (r & DataMASK)>>DataSHIFT;
  277. }
  278. static int
  279. rtl8169miimiw(Mii* mii, int pa, int ra, int data)
  280. {
  281. uint r;
  282. int timeo;
  283. Ctlr *ctlr;
  284. if(pa != 1)
  285. return -1;
  286. ctlr = mii->ctlr;
  287. r = Flag|((ra<<16) & RegaddrMASK)|((data<<DataSHIFT) & DataMASK);
  288. csr32w(ctlr, Phyar, r);
  289. delay(1);
  290. for(timeo = 0; timeo < 2000; timeo++){
  291. if(!((r = csr32r(ctlr, Phyar)) & Flag))
  292. break;
  293. microdelay(100);
  294. }
  295. if(r & Flag)
  296. return -1;
  297. return 0;
  298. }
  299. static int
  300. rtl8169mii(Ctlr* ctlr)
  301. {
  302. MiiPhy *phy;
  303. /*
  304. * Link management.
  305. */
  306. if((ctlr->mii = malloc(sizeof(Mii))) == nil)
  307. return -1;
  308. ctlr->mii->mir = rtl8169miimir;
  309. ctlr->mii->miw = rtl8169miimiw;
  310. ctlr->mii->ctlr = ctlr;
  311. rtl8169miimiw(ctlr->mii, 1, 0x0B, 0x0000); /* magic */
  312. if(mii(ctlr->mii, ~0) == 0 || (phy = ctlr->mii->curphy) == nil){
  313. free(ctlr->mii);
  314. ctlr->mii = nil;
  315. return -1;
  316. }
  317. USED(phy);
  318. // print("oui %X phyno %d\n", phy->oui, phy->phyno);
  319. miiane(ctlr->mii, ~0, ~0, ~0);
  320. return 0;
  321. }
  322. static void
  323. rtl8169promiscuous(void* arg, int on)
  324. {
  325. Ether *edev;
  326. Ctlr * ctlr;
  327. edev = arg;
  328. ctlr = edev->ctlr;
  329. ilock(&ctlr->ilock);
  330. if(on)
  331. ctlr->rcr |= Aap;
  332. else
  333. ctlr->rcr &= ~Aap;
  334. csr32w(ctlr, Rcr, ctlr->rcr);
  335. iunlock(&ctlr->ilock);
  336. }
  337. static long
  338. rtl8169ifstat(Ether* edev, void* a, long n, ulong offset)
  339. {
  340. char *p;
  341. Ctlr *ctlr;
  342. Dtcc *dtcc;
  343. int i, l, r, timeo;
  344. ctlr = edev->ctlr;
  345. qlock(&ctlr->slock);
  346. p = nil;
  347. if(waserror()){
  348. qunlock(&ctlr->slock);
  349. free(p);
  350. nexterror();
  351. }
  352. csr32w(ctlr, Dtccr+4, 0);
  353. csr32w(ctlr, Dtccr, PCIWADDR(ctlr->dtcc)|Cmd);
  354. for(timeo = 0; timeo < 1000; timeo++){
  355. if(!(csr32r(ctlr, Dtccr) & Cmd))
  356. break;
  357. delay(1);
  358. }
  359. if(csr32r(ctlr, Dtccr) & Cmd)
  360. error(Eio);
  361. dtcc = ctlr->dtcc;
  362. edev->oerrs = dtcc->txer;
  363. edev->crcs = dtcc->rxer;
  364. edev->frames = dtcc->fae;
  365. edev->buffs = dtcc->misspkt;
  366. edev->overflows = ctlr->txdu+ctlr->rdu;
  367. if(n == 0){
  368. qunlock(&ctlr->slock);
  369. poperror();
  370. return 0;
  371. }
  372. if((p = malloc(READSTR)) == nil)
  373. error(Enomem);
  374. l = snprint(p, READSTR, "TxOk: %llud\n", dtcc->txok);
  375. l += snprint(p+l, READSTR-l, "RxOk: %llud\n", dtcc->rxok);
  376. l += snprint(p+l, READSTR-l, "TxEr: %llud\n", dtcc->txer);
  377. l += snprint(p+l, READSTR-l, "RxEr: %ud\n", dtcc->rxer);
  378. l += snprint(p+l, READSTR-l, "MissPkt: %ud\n", dtcc->misspkt);
  379. l += snprint(p+l, READSTR-l, "FAE: %ud\n", dtcc->fae);
  380. l += snprint(p+l, READSTR-l, "Tx1Col: %ud\n", dtcc->tx1col);
  381. l += snprint(p+l, READSTR-l, "TxMCol: %ud\n", dtcc->txmcol);
  382. l += snprint(p+l, READSTR-l, "RxOkPh: %llud\n", dtcc->rxokph);
  383. l += snprint(p+l, READSTR-l, "RxOkBrd: %llud\n", dtcc->rxokbrd);
  384. l += snprint(p+l, READSTR-l, "RxOkMu: %ud\n", dtcc->rxokmu);
  385. l += snprint(p+l, READSTR-l, "TxAbt: %ud\n", dtcc->txabt);
  386. l += snprint(p+l, READSTR-l, "TxUndrn: %ud\n", dtcc->txundrn);
  387. l += snprint(p+l, READSTR-l, "txdu: %ud\n", ctlr->txdu);
  388. l += snprint(p+l, READSTR-l, "tcpf: %ud\n", ctlr->tcpf);
  389. l += snprint(p+l, READSTR-l, "udpf: %ud\n", ctlr->udpf);
  390. l += snprint(p+l, READSTR-l, "ipf: %ud\n", ctlr->ipf);
  391. l += snprint(p+l, READSTR-l, "fovf: %ud\n", ctlr->fovf);
  392. l += snprint(p+l, READSTR-l, "ierrs: %ud\n", ctlr->ierrs);
  393. l += snprint(p+l, READSTR-l, "rer: %ud\n", ctlr->rer);
  394. l += snprint(p+l, READSTR-l, "rdu: %ud\n", ctlr->rdu);
  395. l += snprint(p+l, READSTR-l, "punlc: %ud\n", ctlr->punlc);
  396. l += snprint(p+l, READSTR-l, "fovw: %ud\n", ctlr->fovw);
  397. l += snprint(p+l, READSTR-l, "tcr: %#8.8ux\n", ctlr->tcr);
  398. l += snprint(p+l, READSTR-l, "rcr: %#8.8ux\n", ctlr->rcr);
  399. if(ctlr->mii != nil && ctlr->mii->curphy != nil){
  400. l += snprint(p+l, READSTR, "phy: ");
  401. for(i = 0; i < NMiiPhyr; i++){
  402. if(i && ((i & 0x07) == 0))
  403. l += snprint(p+l, READSTR-l, "\n ");
  404. r = miimir(ctlr->mii, i);
  405. l += snprint(p+l, READSTR-l, " %4.4ux", r);
  406. }
  407. snprint(p+l, READSTR-l, "\n");
  408. }
  409. n = readstr(offset, a, n, p);
  410. qunlock(&ctlr->slock);
  411. poperror();
  412. free(p);
  413. return n;
  414. }
  415. static int
  416. rtl8169reset(Ctlr* ctlr)
  417. {
  418. int timeo;
  419. /*
  420. * Soft reset the controller.
  421. */
  422. csr8w(ctlr, Cr, Rst);
  423. for(timeo = 0; timeo < 1000; timeo++){
  424. if(!(csr8r(ctlr, Cr) & Rst))
  425. return 0;
  426. delay(1);
  427. }
  428. return -1;
  429. }
  430. static void
  431. rtl8169halt(Ctlr* ctlr)
  432. {
  433. csr8w(ctlr, Cr, 0);
  434. csr16w(ctlr, Imr, 0);
  435. csr16w(ctlr, Isr, ~0);
  436. }
  437. static void
  438. rtl8169replenish(Ctlr* ctlr)
  439. {
  440. D *d;
  441. int rdt;
  442. Block *bp;
  443. rdt = ctlr->rdt;
  444. while(NEXT(rdt, ctlr->nrd) != ctlr->rdh){
  445. d = &ctlr->rd[rdt];
  446. if(ctlr->rb[rdt] == nil){
  447. /*
  448. * simple allocation for now
  449. */
  450. bp = iallocb(Mps);
  451. if(bp == nil){
  452. iprint("no available buffers\n");
  453. break;
  454. }
  455. ctlr->rb[rdt] = bp;
  456. d->addrlo = PCIWADDR(bp->rp);
  457. d->addrhi = 0;
  458. }
  459. coherence();
  460. d->control |= Own|Mps;
  461. rdt = NEXT(rdt, ctlr->nrd);
  462. ctlr->nrdfree++;
  463. }
  464. ctlr->rdt = rdt;
  465. }
  466. static void
  467. rtl8169init(Ether* edev)
  468. {
  469. int i;
  470. uint r;
  471. Block *bp;
  472. Ctlr *ctlr;
  473. ctlr = edev->ctlr;
  474. ilock(&ctlr->ilock);
  475. rtl8169halt(ctlr);
  476. /*
  477. * Setting Mulrw in Cplusc disables the Tx/Rx DMA burst settings
  478. * in Tcr/Rcr.
  479. */
  480. csr16w(ctlr, Cplusc, (1<<14)|Rxchksum|Mulrw); /* magic (1<<14) */
  481. /*
  482. * MAC Address.
  483. * Must put chip into config register write enable mode.
  484. */
  485. csr8w(ctlr, Cr9346, Eem1|Eem0);
  486. r = (edev->ea[3]<<24)|(edev->ea[2]<<16)|(edev->ea[1]<<8)|edev->ea[0];
  487. csr32w(ctlr, Idr0, r);
  488. r = (edev->ea[5]<<8)|edev->ea[4];
  489. csr32w(ctlr, Idr0+4, r);
  490. /*
  491. * Enable receiver/transmitter.
  492. * Need to do this first or some of the settings below
  493. * won't take.
  494. */
  495. csr8w(ctlr, Cr, Te|Re);
  496. /*
  497. * Transmitter.
  498. * Mtps is in units of 128.
  499. */
  500. memset(ctlr->td, 0, sizeof(D)*ctlr->ntd);
  501. ctlr->tdh = ctlr->tdt = 0;
  502. ctlr->td[ctlr->ntd-1].control = Eor;
  503. ctlr->mtps = HOWMANY(Mps, 128);
  504. /*
  505. * Receiver.
  506. */
  507. memset(ctlr->rd, 0, sizeof(D)*ctlr->nrd);
  508. ctlr->rdh = ctlr->rdt = 0;
  509. ctlr->rd[ctlr->nrd-1].control = Eor;
  510. for(i = 0; i < ctlr->nrd; i++){
  511. if((bp = ctlr->rb[i]) != nil){
  512. ctlr->rb[i] = nil;
  513. freeb(bp);
  514. }
  515. }
  516. rtl8169replenish(ctlr);
  517. ctlr->rcr = Rxfth256|Mrxdmaunlimited|Ab|Apm;
  518. /*
  519. * Interrupts.
  520. * Disable Tdu|Tok for now, the transmit routine will tidy.
  521. * Tdu means the NIC ran out of descritors to send, so it
  522. * doesn't really need to ever be on.
  523. */
  524. csr32w(ctlr, Timerint, 0);
  525. csr16w(ctlr, Imr, Serr|Timeout/*|Tdu*/|Fovw|Punlc|Rdu|Ter/*|Tok*/|Rer|Rok);
  526. /*
  527. * Clear missed-packet counter;
  528. * initial early transmit threshold value;
  529. * set the descriptor ring base addresses;
  530. * set the maximum receive packet size - if it is
  531. * larger than 8191 the Rwt|Res bits may be set
  532. * in the receive descriptor control info even if
  533. * the packet is good;
  534. * no early-receive interrupts.
  535. */
  536. csr32w(ctlr, Mpc, 0);
  537. csr8w(ctlr, Mtps, ctlr->mtps);
  538. csr32w(ctlr, Tnpds+4, 0);
  539. csr32w(ctlr, Tnpds, PCIWADDR(ctlr->td));
  540. csr32w(ctlr, Rdsar+4, 0);
  541. csr32w(ctlr, Rdsar, PCIWADDR(ctlr->rd));
  542. csr16w(ctlr, Rms, Mps);
  543. csr16w(ctlr, Mulint, 0);
  544. /*
  545. * Set configuration.
  546. */
  547. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  548. ctlr->tcr = csr32r(ctlr, Tcr);
  549. csr32w(ctlr, Rcr, ctlr->rcr);
  550. csr16w(ctlr, 0xE2, 0); /* magic */
  551. csr8w(ctlr, Cr9346, 0);
  552. iunlock(&ctlr->ilock);
  553. // rtl8169mii(ctlr);
  554. }
  555. static void
  556. rtl8169attach(Ether* edev)
  557. {
  558. Ctlr *ctlr;
  559. ctlr = edev->ctlr;
  560. qlock(&ctlr->alock);
  561. if(ctlr->init == 0){
  562. /*
  563. * Handle allocation/init errors here.
  564. */
  565. ctlr->td = mallocalign(sizeof(D)*Ntd, 256, 0, 0);
  566. ctlr->tb = malloc(Ntd*sizeof(Block*));
  567. ctlr->ntd = Ntd;
  568. ctlr->rd = mallocalign(sizeof(D)*Nrd, 256, 0, 0);
  569. ctlr->rb = malloc(Nrd*sizeof(Block*));
  570. ctlr->nrd = Nrd;
  571. ctlr->dtcc = mallocalign(sizeof(Dtcc), 64, 0, 0);
  572. rtl8169init(edev);
  573. ctlr->init = 1;
  574. }
  575. qunlock(&ctlr->alock);
  576. /*
  577. * Should wait for link to be ready here.
  578. */
  579. }
  580. static void
  581. rtl8169link(Ether* edev)
  582. {
  583. uint r;
  584. int limit;
  585. Ctlr *ctlr;
  586. ctlr = edev->ctlr;
  587. /*
  588. * Maybe the link changed - do we care very much?
  589. * Could stall transmits if no link, maybe?
  590. */
  591. if(!((r = csr8r(ctlr, Phystatus)) & Linksts))
  592. return;
  593. limit = 256*1024;
  594. if(r & Speed10){
  595. edev->mbps = 10;
  596. limit = 65*1024;
  597. }
  598. else if(r & Speed100)
  599. edev->mbps = 100;
  600. else if(r & Speed1000)
  601. edev->mbps = 1000;
  602. if(edev->oq != nil)
  603. qsetlimit(edev->oq, limit);
  604. }
  605. static void
  606. rtl8169transmit(Ether* edev)
  607. {
  608. D *d;
  609. Block *bp;
  610. Ctlr *ctlr;
  611. int control, x;
  612. ctlr = edev->ctlr;
  613. ilock(&ctlr->tlock);
  614. for(x = ctlr->tdh; ctlr->ntq > 0; x = NEXT(x, ctlr->ntd)){
  615. d = &ctlr->td[x];
  616. if((control = d->control) & Own)
  617. break;
  618. /*
  619. * Check errors and log here.
  620. */
  621. USED(control);
  622. /*
  623. * Free it up.
  624. * Need to clean the descriptor here? Not really.
  625. * Simple freeb for now (no chain and freeblist).
  626. * Use ntq count for now.
  627. */
  628. freeb(ctlr->tb[x]);
  629. ctlr->tb[x] = nil;
  630. d->control &= Eor;
  631. ctlr->ntq--;
  632. }
  633. ctlr->tdh = x;
  634. x = ctlr->tdt;
  635. while(ctlr->ntq < (ctlr->ntd-1)){
  636. if((bp = qget(edev->oq)) == nil)
  637. break;
  638. d = &ctlr->td[x];
  639. d->addrlo = PCIWADDR(bp->rp);
  640. d->addrhi = 0;
  641. ctlr->tb[x] = bp;
  642. coherence();
  643. d->control |= Own|Fs|Ls|((BLEN(bp)<<TxflSHIFT) & TxflMASK);
  644. x = NEXT(x, ctlr->ntd);
  645. ctlr->ntq++;
  646. }
  647. if(x != ctlr->tdt){
  648. ctlr->tdt = x;
  649. csr8w(ctlr, Tppoll, Npq);
  650. }
  651. else if(ctlr->ntq >= (ctlr->ntd-1))
  652. ctlr->txdu++;
  653. iunlock(&ctlr->tlock);
  654. }
  655. static void
  656. rtl8169receive(Ether* edev)
  657. {
  658. D *d;
  659. int rdh;
  660. Block *bp;
  661. Ctlr *ctlr;
  662. u32int control;
  663. ctlr = edev->ctlr;
  664. rdh = ctlr->rdh;
  665. for(;;){
  666. d = &ctlr->rd[rdh];
  667. if(d->control & Own)
  668. break;
  669. control = d->control;
  670. if((control & (Fs|Ls|Res)) == (Fs|Ls)){
  671. bp = ctlr->rb[rdh];
  672. ctlr->rb[rdh] = nil;
  673. bp->wp = bp->rp + ((control & RxflMASK)>>RxflSHIFT) - 4;
  674. bp->next = nil;
  675. if(control & Fovf)
  676. ctlr->fovf++;
  677. switch(control & (Pid1|Pid0)){
  678. default:
  679. break;
  680. case Pid0:
  681. if(control & Tcpf){
  682. ctlr->tcpf++;
  683. break;
  684. }
  685. bp->flag |= Btcpck;
  686. break;
  687. case Pid1:
  688. if(control & Udpf){
  689. ctlr->udpf++;
  690. break;
  691. }
  692. bp->flag |= Budpck;
  693. break;
  694. case Pid1|Pid0:
  695. if(control & Ipf){
  696. ctlr->ipf++;
  697. break;
  698. }
  699. bp->flag |= Bipck;
  700. break;
  701. }
  702. etheriq(edev, bp, 1);
  703. }
  704. else{
  705. /*
  706. * Error stuff here.
  707. print("control %#8.8ux\n", control);
  708. */
  709. }
  710. d->control &= Eor;
  711. ctlr->nrdfree--;
  712. rdh = NEXT(rdh, ctlr->nrd);
  713. }
  714. ctlr->rdh = rdh;
  715. if(ctlr->nrdfree < ctlr->nrd/2)
  716. rtl8169replenish(ctlr);
  717. }
  718. static void
  719. rtl8169interrupt(Ureg*, void* arg)
  720. {
  721. Ctlr *ctlr;
  722. Ether *edev;
  723. u32int isr;
  724. edev = arg;
  725. ctlr = edev->ctlr;
  726. while((isr = csr16r(ctlr, Isr)) != 0){
  727. csr16w(ctlr, Isr, isr);
  728. if(isr & (Fovw|Punlc|Rdu|Rer|Rok)){
  729. rtl8169receive(edev);
  730. if(!(isr & (Punlc|Rok)))
  731. ctlr->ierrs++;
  732. if(isr & Rer)
  733. ctlr->rer++;
  734. if(isr & Rdu)
  735. ctlr->rdu++;
  736. if(isr & Punlc)
  737. ctlr->punlc++;
  738. if(isr & Fovw)
  739. ctlr->fovw++;
  740. isr &= ~(Fovw|Rdu|Rer|Rok);
  741. }
  742. if(isr & (Tdu|Ter|Tok)){
  743. rtl8169transmit(edev);
  744. isr &= ~(Tdu|Ter|Tok);
  745. }
  746. if(isr & Punlc){
  747. rtl8169link(edev);
  748. isr &= ~Punlc;
  749. }
  750. /*
  751. * Some of the reserved bits get set sometimes...
  752. */
  753. if(isr & (Serr|Timeout|Tdu|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok))
  754. panic("rtl8169interrupt: imr %#4.4ux isr %#4.4ux\n",
  755. csr16r(ctlr, Imr), isr);
  756. }
  757. }
  758. static Ctlr*
  759. rtl8169match(Ether* edev, int id)
  760. {
  761. Pcidev *p;
  762. Ctlr *ctlr;
  763. int i, port;
  764. /*
  765. * Any adapter matches if no edev->port is supplied,
  766. * otherwise the ports must match.
  767. */
  768. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  769. if(ctlr->active)
  770. continue;
  771. p = ctlr->pcidev;
  772. if(((p->did<<16)|p->vid) != id)
  773. continue;
  774. port = p->mem[0].bar & ~0x01;
  775. if(edev->port != 0 && edev->port != port)
  776. continue;
  777. if(ioalloc(port, p->mem[0].size, 0, "rtl8169") < 0){
  778. print("rtl8169: port %#ux in use\n", port);
  779. continue;
  780. }
  781. if(pcigetpms(p) > 0){
  782. pcisetpms(p, 0);
  783. for(i = 0; i < 6; i++)
  784. pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
  785. pcicfgw8(p, PciINTL, p->intl);
  786. pcicfgw8(p, PciLTR, p->ltr);
  787. pcicfgw8(p, PciCLS, p->cls);
  788. pcicfgw16(p, PciPCR, p->pcr);
  789. }
  790. ctlr->port = port;
  791. if(rtl8169reset(ctlr))
  792. continue;
  793. csr8w(ctlr, 0x82, 1); /* magic */
  794. rtl8169mii(ctlr);
  795. pcisetbme(p);
  796. ctlr->active = 1;
  797. return ctlr;
  798. }
  799. return nil;
  800. }
  801. static struct {
  802. char* name;
  803. int id;
  804. } rtl8169pci[] = {
  805. { "rtl8169", (0x8169<<16)|0x10EC, }, /* generic */
  806. { "CG-LAPCIGT", (0xC107<<16)|0x1259, }, /* Corega CG-LAPCIGT */
  807. { nil },
  808. };
  809. static int
  810. rtl8169pnp(Ether* edev)
  811. {
  812. Pcidev *p;
  813. Ctlr *ctlr;
  814. int i, id;
  815. uchar ea[Eaddrlen];
  816. /*
  817. * Make a list of all ethernet controllers
  818. * if not already done.
  819. */
  820. if(ctlrhead == nil){
  821. p = nil;
  822. while(p = pcimatch(p, 0, 0)){
  823. if(p->ccrb != 0x02 || p->ccru != 0)
  824. continue;
  825. ctlr = malloc(sizeof(Ctlr));
  826. ctlr->pcidev = p;
  827. ctlr->id = (p->did<<16)|p->vid;
  828. if(ctlrhead != nil)
  829. ctlrtail->next = ctlr;
  830. else
  831. ctlrhead = ctlr;
  832. ctlrtail = ctlr;
  833. }
  834. }
  835. /*
  836. * Is it an RTL8169 under a different name?
  837. * Normally a search is made through all the found controllers
  838. * for one which matches any of the known vid+did pairs.
  839. * If a vid+did pair is specified a search is made for that
  840. * specific controller only.
  841. */
  842. id = 0;
  843. for(i = 0; i < edev->nopt; i++){
  844. if(cistrncmp(edev->opt[i], "id=", 3) == 0)
  845. id = strtol(&edev->opt[i][3], nil, 0);
  846. }
  847. ctlr = nil;
  848. if(id != 0)
  849. ctlr = rtl8169match(edev, id);
  850. else for(i = 0; rtl8169pci[i].name; i++){
  851. if((ctlr = rtl8169match(edev, rtl8169pci[i].id)) != nil)
  852. break;
  853. }
  854. if(ctlr == nil)
  855. return -1;
  856. edev->ctlr = ctlr;
  857. edev->port = ctlr->port;
  858. edev->irq = ctlr->pcidev->intl;
  859. edev->tbdf = ctlr->pcidev->tbdf;
  860. /*
  861. * Check if the adapter's station address is to be overridden.
  862. * If not, read it from the device and set in edev->ea.
  863. */
  864. memset(ea, 0, Eaddrlen);
  865. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  866. i = csr32r(ctlr, Idr0);
  867. edev->ea[0] = i;
  868. edev->ea[1] = i>>8;
  869. edev->ea[2] = i>>16;
  870. edev->ea[3] = i>>24;
  871. i = csr32r(ctlr, Idr0+4);
  872. edev->ea[4] = i;
  873. edev->ea[5] = i>>8;
  874. }
  875. edev->attach = rtl8169attach;
  876. edev->transmit = rtl8169transmit;
  877. edev->interrupt = rtl8169interrupt;
  878. edev->ifstat = rtl8169ifstat;
  879. edev->arg = edev;
  880. edev->promiscuous = rtl8169promiscuous;
  881. rtl8169link(edev);
  882. return 0;
  883. }
  884. void
  885. ether8169link(void)
  886. {
  887. addethercard("rtl8169", rtl8169pnp);
  888. }