ether8169.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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 void
  366. rtl8169multicast(void* arg, uchar*, int)
  367. {
  368. rtl8169promiscuous(arg, 1);
  369. }
  370. static long
  371. rtl8169ifstat(Ether* edev, void* a, long n, ulong offset)
  372. {
  373. char *p;
  374. Ctlr *ctlr;
  375. Dtcc *dtcc;
  376. int i, l, r, timeo;
  377. ctlr = edev->ctlr;
  378. qlock(&ctlr->slock);
  379. p = nil;
  380. if(waserror()){
  381. qunlock(&ctlr->slock);
  382. free(p);
  383. nexterror();
  384. }
  385. csr32w(ctlr, Dtccr+4, 0);
  386. csr32w(ctlr, Dtccr, PCIWADDR(ctlr->dtcc)|Cmd);
  387. for(timeo = 0; timeo < 1000; timeo++){
  388. if(!(csr32r(ctlr, Dtccr) & Cmd))
  389. break;
  390. delay(1);
  391. }
  392. if(csr32r(ctlr, Dtccr) & Cmd)
  393. error(Eio);
  394. dtcc = ctlr->dtcc;
  395. edev->oerrs = dtcc->txer;
  396. edev->crcs = dtcc->rxer;
  397. edev->frames = dtcc->fae;
  398. edev->buffs = dtcc->misspkt;
  399. edev->overflows = ctlr->txdu+ctlr->rdu;
  400. if(n == 0){
  401. qunlock(&ctlr->slock);
  402. poperror();
  403. return 0;
  404. }
  405. if((p = malloc(READSTR)) == nil)
  406. error(Enomem);
  407. l = snprint(p, READSTR, "TxOk: %llud\n", dtcc->txok);
  408. l += snprint(p+l, READSTR-l, "RxOk: %llud\n", dtcc->rxok);
  409. l += snprint(p+l, READSTR-l, "TxEr: %llud\n", dtcc->txer);
  410. l += snprint(p+l, READSTR-l, "RxEr: %ud\n", dtcc->rxer);
  411. l += snprint(p+l, READSTR-l, "MissPkt: %ud\n", dtcc->misspkt);
  412. l += snprint(p+l, READSTR-l, "FAE: %ud\n", dtcc->fae);
  413. l += snprint(p+l, READSTR-l, "Tx1Col: %ud\n", dtcc->tx1col);
  414. l += snprint(p+l, READSTR-l, "TxMCol: %ud\n", dtcc->txmcol);
  415. l += snprint(p+l, READSTR-l, "RxOkPh: %llud\n", dtcc->rxokph);
  416. l += snprint(p+l, READSTR-l, "RxOkBrd: %llud\n", dtcc->rxokbrd);
  417. l += snprint(p+l, READSTR-l, "RxOkMu: %ud\n", dtcc->rxokmu);
  418. l += snprint(p+l, READSTR-l, "TxAbt: %ud\n", dtcc->txabt);
  419. l += snprint(p+l, READSTR-l, "TxUndrn: %ud\n", dtcc->txundrn);
  420. l += snprint(p+l, READSTR-l, "txdu: %ud\n", ctlr->txdu);
  421. l += snprint(p+l, READSTR-l, "tcpf: %ud\n", ctlr->tcpf);
  422. l += snprint(p+l, READSTR-l, "udpf: %ud\n", ctlr->udpf);
  423. l += snprint(p+l, READSTR-l, "ipf: %ud\n", ctlr->ipf);
  424. l += snprint(p+l, READSTR-l, "fovf: %ud\n", ctlr->fovf);
  425. l += snprint(p+l, READSTR-l, "ierrs: %ud\n", ctlr->ierrs);
  426. l += snprint(p+l, READSTR-l, "rer: %ud\n", ctlr->rer);
  427. l += snprint(p+l, READSTR-l, "rdu: %ud\n", ctlr->rdu);
  428. l += snprint(p+l, READSTR-l, "punlc: %ud\n", ctlr->punlc);
  429. l += snprint(p+l, READSTR-l, "fovw: %ud\n", ctlr->fovw);
  430. l += snprint(p+l, READSTR-l, "tcr: %#8.8ux\n", ctlr->tcr);
  431. l += snprint(p+l, READSTR-l, "rcr: %#8.8ux\n", ctlr->rcr);
  432. if(ctlr->mii != nil && ctlr->mii->curphy != nil){
  433. l += snprint(p+l, READSTR, "phy: ");
  434. for(i = 0; i < NMiiPhyr; i++){
  435. if(i && ((i & 0x07) == 0))
  436. l += snprint(p+l, READSTR-l, "\n ");
  437. r = miimir(ctlr->mii, i);
  438. l += snprint(p+l, READSTR-l, " %4.4ux", r);
  439. }
  440. snprint(p+l, READSTR-l, "\n");
  441. }
  442. n = readstr(offset, a, n, p);
  443. qunlock(&ctlr->slock);
  444. poperror();
  445. free(p);
  446. return n;
  447. }
  448. static void
  449. rtl8169halt(Ctlr* ctlr)
  450. {
  451. csr8w(ctlr, Cr, 0);
  452. csr16w(ctlr, Imr, 0);
  453. csr16w(ctlr, Isr, ~0);
  454. }
  455. static int
  456. rtl8169reset(Ctlr* ctlr)
  457. {
  458. u32int r;
  459. int timeo;
  460. /*
  461. * Soft reset the controller.
  462. */
  463. csr8w(ctlr, Cr, Rst);
  464. for(r = timeo = 0; timeo < 1000; timeo++){
  465. r = csr8r(ctlr, Cr);
  466. if(!(r & Rst))
  467. break;
  468. delay(1);
  469. }
  470. rtl8169halt(ctlr);
  471. if(r & Rst)
  472. return -1;
  473. return 0;
  474. }
  475. static void
  476. rtl8169replenish(Ctlr* ctlr)
  477. {
  478. D *d;
  479. int rdt;
  480. Block *bp;
  481. rdt = ctlr->rdt;
  482. while(NEXT(rdt, ctlr->nrd) != ctlr->rdh){
  483. d = &ctlr->rd[rdt];
  484. if(ctlr->rb[rdt] == nil){
  485. /*
  486. * Simple allocation for now.
  487. * This better be aligned on 8.
  488. */
  489. bp = iallocb(Mps);
  490. if(bp == nil){
  491. iprint("no available buffers\n");
  492. break;
  493. }
  494. ctlr->rb[rdt] = bp;
  495. d->addrlo = PCIWADDR(bp->rp);
  496. d->addrhi = 0;
  497. }
  498. coherence();
  499. d->control |= Own|Mps;
  500. rdt = NEXT(rdt, ctlr->nrd);
  501. ctlr->nrdfree++;
  502. }
  503. ctlr->rdt = rdt;
  504. }
  505. static int
  506. rtl8169init(Ether* edev)
  507. {
  508. int i;
  509. u32int r;
  510. Block *bp;
  511. Ctlr *ctlr;
  512. u8int cplusc;
  513. ctlr = edev->ctlr;
  514. ilock(&ctlr->ilock);
  515. rtl8169halt(ctlr);
  516. /*
  517. * MAC Address.
  518. * Must put chip into config register write enable mode.
  519. */
  520. csr8w(ctlr, Cr9346, Eem1|Eem0);
  521. r = (edev->ea[3]<<24)|(edev->ea[2]<<16)|(edev->ea[1]<<8)|edev->ea[0];
  522. csr32w(ctlr, Idr0, r);
  523. r = (edev->ea[5]<<8)|edev->ea[4];
  524. csr32w(ctlr, Idr0+4, r);
  525. /*
  526. * Transmitter.
  527. */
  528. memset(ctlr->td, 0, sizeof(D)*ctlr->ntd);
  529. ctlr->tdh = ctlr->tdt = 0;
  530. ctlr->td[ctlr->ntd-1].control = Eor;
  531. /*
  532. * Receiver.
  533. * Need to do something here about the multicast filter.
  534. */
  535. memset(ctlr->rd, 0, sizeof(D)*ctlr->nrd);
  536. ctlr->nrdfree = ctlr->rdh = ctlr->rdt = 0;
  537. ctlr->rd[ctlr->nrd-1].control = Eor;
  538. for(i = 0; i < ctlr->nrd; i++){
  539. if((bp = ctlr->rb[i]) != nil){
  540. ctlr->rb[i] = nil;
  541. freeb(bp);
  542. }
  543. }
  544. rtl8169replenish(ctlr);
  545. ctlr->rcr = Rxfthnone|Mrxdmaunlimited|Ab|Apm;
  546. /*
  547. * Mtps is in units of 128 except for the RTL8169
  548. * where is is 32. If using jumbo frames should be
  549. * set to 0x3F.
  550. * Setting Mulrw in Cplusc disables the Tx/Rx DMA burst
  551. * settings in Tcr/Rcr; the (1<<14) is magic.
  552. */
  553. ctlr->mtps = HOWMANY(Mps, 128);
  554. cplusc = csr16r(ctlr, Cplusc) & ~(1<<14);
  555. cplusc |= /*Rxchksum|*/Mulrw;
  556. switch(ctlr->macv){
  557. default:
  558. return -1;
  559. case Macv01:
  560. ctlr->mtps = HOWMANY(Mps, 32);
  561. break;
  562. case Macv02:
  563. case Macv03:
  564. cplusc |= (1<<14); /* magic */
  565. break;
  566. case Macv05:
  567. /*
  568. * This is interpreted from clearly bogus code
  569. * in the manufacturer-supplied driver, it could
  570. * be wrong. Untested.
  571. */
  572. r = csr8r(ctlr, Config2) & 0x07;
  573. if(r == 0x01) /* 66MHz PCI */
  574. csr32w(ctlr, 0x7C, 0x0007FFFF); /* magic */
  575. else
  576. csr32w(ctlr, 0x7C, 0x0007FF00); /* magic */
  577. pciclrmwi(ctlr->pcidev);
  578. break;
  579. case Macv13:
  580. /*
  581. * This is interpreted from clearly bogus code
  582. * in the manufacturer-supplied driver, it could
  583. * be wrong. Untested.
  584. */
  585. pcicfgw8(ctlr->pcidev, 0x68, 0x00); /* magic */
  586. pcicfgw8(ctlr->pcidev, 0x69, 0x08); /* magic */
  587. break;
  588. case Macv04:
  589. case Macv11:
  590. case Macv12:
  591. case Macv14:
  592. case Macv15:
  593. break;
  594. }
  595. /*
  596. * Enable receiver/transmitter.
  597. * Need to do this first or some of the settings below
  598. * won't take.
  599. */
  600. switch(ctlr->pciv){
  601. default:
  602. csr8w(ctlr, Cr, Te|Re);
  603. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  604. csr32w(ctlr, Rcr, ctlr->rcr);
  605. case Rtl8169sc:
  606. case Rtl8168b:
  607. break;
  608. }
  609. /*
  610. * Interrupts.
  611. * Disable Tdu|Tok for now, the transmit routine will tidy.
  612. * Tdu means the NIC ran out of descriptors to send, so it
  613. * doesn't really need to ever be on.
  614. */
  615. csr32w(ctlr, Timerint, 0);
  616. ctlr->imr = Serr|Timeout|Fovw|Punlc|Rdu|Ter|Rer|Rok;
  617. csr16w(ctlr, Imr, ctlr->imr);
  618. /*
  619. * Clear missed-packet counter;
  620. * initial early transmit threshold value;
  621. * set the descriptor ring base addresses;
  622. * set the maximum receive packet size;
  623. * no early-receive interrupts.
  624. */
  625. csr32w(ctlr, Mpc, 0);
  626. csr8w(ctlr, Mtps, ctlr->mtps);
  627. csr32w(ctlr, Tnpds+4, 0);
  628. csr32w(ctlr, Tnpds, PCIWADDR(ctlr->td));
  629. csr32w(ctlr, Rdsar+4, 0);
  630. csr32w(ctlr, Rdsar, PCIWADDR(ctlr->rd));
  631. csr16w(ctlr, Rms, Mps);
  632. r = csr16r(ctlr, Mulint) & 0xF000;
  633. csr16w(ctlr, Mulint, r);
  634. csr16w(ctlr, Cplusc, cplusc);
  635. /*
  636. * Set configuration.
  637. */
  638. switch(ctlr->pciv){
  639. default:
  640. break;
  641. case Rtl8169sc:
  642. csr16w(ctlr, 0xE2, 0); /* magic */
  643. csr8w(ctlr, Cr, Te|Re);
  644. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  645. csr32w(ctlr, Rcr, ctlr->rcr);
  646. break;
  647. case Rtl8168b:
  648. case Rtl8169c:
  649. csr16w(ctlr, 0xE2, 0); /* magic */
  650. csr16w(ctlr, Cplusc, 0x2000); /* magic */
  651. csr8w(ctlr, Cr, Te|Re);
  652. csr32w(ctlr, Tcr, Ifg1|Ifg0|Mtxdmaunlimited);
  653. csr32w(ctlr, Rcr, ctlr->rcr);
  654. csr16w(ctlr, Rms, 0x0800);
  655. csr8w(ctlr, Mtps, 0x3F);
  656. break;
  657. }
  658. ctlr->tcr = csr32r(ctlr, Tcr);
  659. csr8w(ctlr, Cr9346, 0);
  660. iunlock(&ctlr->ilock);
  661. // rtl8169mii(ctlr);
  662. return 0;
  663. }
  664. static void
  665. rtl8169attach(Ether* edev)
  666. {
  667. int timeo;
  668. Ctlr *ctlr;
  669. ctlr = edev->ctlr;
  670. qlock(&ctlr->alock);
  671. if(ctlr->init == 0){
  672. /*
  673. * Handle allocation/init errors here.
  674. */
  675. ctlr->td = mallocalign(sizeof(D)*Ntd, 256, 0, 0);
  676. ctlr->tb = malloc(Ntd*sizeof(Block*));
  677. ctlr->ntd = Ntd;
  678. ctlr->rd = mallocalign(sizeof(D)*Nrd, 256, 0, 0);
  679. ctlr->rb = malloc(Nrd*sizeof(Block*));
  680. ctlr->nrd = Nrd;
  681. ctlr->dtcc = mallocalign(sizeof(Dtcc), 64, 0, 0);
  682. rtl8169init(edev);
  683. ctlr->init = 1;
  684. }
  685. qunlock(&ctlr->alock);
  686. /*
  687. * Wait for link to be ready.
  688. */
  689. for(timeo = 0; timeo < 350; timeo++){
  690. if(miistatus(ctlr->mii) == 0)
  691. break;
  692. delay(100); /* print fewer miistatus messages */
  693. }
  694. }
  695. static void
  696. rtl8169link(Ether* edev)
  697. {
  698. uint r;
  699. int limit;
  700. Ctlr *ctlr;
  701. ctlr = edev->ctlr;
  702. /*
  703. * Maybe the link changed - do we care very much?
  704. * Could stall transmits if no link, maybe?
  705. */
  706. if(!((r = csr8r(ctlr, Phystatus)) & Linksts))
  707. return;
  708. limit = 256*1024;
  709. if(r & Speed10){
  710. edev->mbps = 10;
  711. limit = 65*1024;
  712. }
  713. else if(r & Speed100)
  714. edev->mbps = 100;
  715. else if(r & Speed1000)
  716. edev->mbps = 1000;
  717. if(edev->oq != nil)
  718. qsetlimit(edev->oq, limit);
  719. }
  720. static void
  721. rtl8169transmit(Ether* edev)
  722. {
  723. D *d;
  724. Block *bp;
  725. Ctlr *ctlr;
  726. int control, x;
  727. ctlr = edev->ctlr;
  728. ilock(&ctlr->tlock);
  729. for(x = ctlr->tdh; ctlr->ntq > 0; x = NEXT(x, ctlr->ntd)){
  730. d = &ctlr->td[x];
  731. if((control = d->control) & Own)
  732. break;
  733. /*
  734. * Check errors and log here.
  735. */
  736. USED(control);
  737. /*
  738. * Free it up.
  739. * Need to clean the descriptor here? Not really.
  740. * Simple freeb for now (no chain and freeblist).
  741. * Use ntq count for now.
  742. */
  743. freeb(ctlr->tb[x]);
  744. ctlr->tb[x] = nil;
  745. d->control &= Eor;
  746. ctlr->ntq--;
  747. }
  748. ctlr->tdh = x;
  749. x = ctlr->tdt;
  750. while(ctlr->ntq < (ctlr->ntd-1)){
  751. if((bp = qget(edev->oq)) == nil)
  752. break;
  753. d = &ctlr->td[x];
  754. d->addrlo = PCIWADDR(bp->rp);
  755. d->addrhi = 0;
  756. ctlr->tb[x] = bp;
  757. coherence();
  758. d->control |= Own|Fs|Ls|((BLEN(bp)<<TxflSHIFT) & TxflMASK);
  759. x = NEXT(x, ctlr->ntd);
  760. ctlr->ntq++;
  761. }
  762. if(x != ctlr->tdt){
  763. ctlr->tdt = x;
  764. csr8w(ctlr, Tppoll, Npq);
  765. }
  766. else if(ctlr->ntq >= (ctlr->ntd-1))
  767. ctlr->txdu++;
  768. iunlock(&ctlr->tlock);
  769. }
  770. static void
  771. rtl8169receive(Ether* edev)
  772. {
  773. D *d;
  774. int rdh;
  775. Block *bp;
  776. Ctlr *ctlr;
  777. u32int control;
  778. ctlr = edev->ctlr;
  779. rdh = ctlr->rdh;
  780. for(;;){
  781. d = &ctlr->rd[rdh];
  782. if(d->control & Own)
  783. break;
  784. control = d->control;
  785. if((control & (Fs|Ls|Res)) == (Fs|Ls)){
  786. bp = ctlr->rb[rdh];
  787. ctlr->rb[rdh] = nil;
  788. bp->wp = bp->rp + ((control & RxflMASK)>>RxflSHIFT)-4;
  789. bp->next = nil;
  790. if(control & Fovf)
  791. ctlr->fovf++;
  792. switch(control & (Pid1|Pid0)){
  793. default:
  794. break;
  795. case Pid0:
  796. if(control & Tcpf){
  797. ctlr->tcpf++;
  798. break;
  799. }
  800. bp->flag |= Btcpck;
  801. break;
  802. case Pid1:
  803. if(control & Udpf){
  804. ctlr->udpf++;
  805. break;
  806. }
  807. bp->flag |= Budpck;
  808. break;
  809. case Pid1|Pid0:
  810. if(control & Ipf){
  811. ctlr->ipf++;
  812. break;
  813. }
  814. bp->flag |= Bipck;
  815. break;
  816. }
  817. etheriq(edev, bp, 1);
  818. }
  819. else{
  820. /*
  821. * Error stuff here.
  822. print("control %#8.8ux\n", control);
  823. */
  824. }
  825. d->control &= Eor;
  826. ctlr->nrdfree--;
  827. rdh = NEXT(rdh, ctlr->nrd);
  828. if(ctlr->nrdfree < ctlr->nrd/2)
  829. rtl8169replenish(ctlr);
  830. }
  831. ctlr->rdh = rdh;
  832. }
  833. static void
  834. rtl8169interrupt(Ureg*, void* arg)
  835. {
  836. Ctlr *ctlr;
  837. Ether *edev;
  838. u32int isr;
  839. edev = arg;
  840. ctlr = edev->ctlr;
  841. while((isr = csr16r(ctlr, Isr)) != 0 && isr != 0xFFFF){
  842. csr16w(ctlr, Isr, isr);
  843. if((isr & ctlr->imr) == 0)
  844. break;
  845. if(isr & (Fovw|Punlc|Rdu|Rer|Rok)){
  846. rtl8169receive(edev);
  847. if(!(isr & (Punlc|Rok)))
  848. ctlr->ierrs++;
  849. if(isr & Rer)
  850. ctlr->rer++;
  851. if(isr & Rdu)
  852. ctlr->rdu++;
  853. if(isr & Punlc)
  854. ctlr->punlc++;
  855. if(isr & Fovw)
  856. ctlr->fovw++;
  857. isr &= ~(Fovw|Rdu|Rer|Rok);
  858. }
  859. if(isr & (Tdu|Ter|Tok)){
  860. rtl8169transmit(edev);
  861. isr &= ~(Tdu|Ter|Tok);
  862. }
  863. if(isr & Punlc){
  864. rtl8169link(edev);
  865. isr &= ~Punlc;
  866. }
  867. /*
  868. * Some of the reserved bits get set sometimes...
  869. */
  870. if(isr & (Serr|Timeout|Tdu|Fovw|Punlc|Rdu|Ter|Tok|Rer|Rok))
  871. panic("rtl8169interrupt: imr %#4.4ux isr %#4.4ux\n",
  872. csr16r(ctlr, Imr), isr);
  873. }
  874. }
  875. static void
  876. rtl8169pci(void)
  877. {
  878. Pcidev *p;
  879. Ctlr *ctlr;
  880. int i, port;
  881. p = nil;
  882. while(p = pcimatch(p, 0, 0)){
  883. if(p->ccrb != 0x02 || p->ccru != 0)
  884. continue;
  885. switch(i = ((p->did<<16)|p->vid)){
  886. default:
  887. continue;
  888. case Rtl8100e: /* RTL810[01]E ? */
  889. case Rtl8169c: /* RTL8169C */
  890. case Rtl8169sc: /* RTL8169SC */
  891. case Rtl8168b: /* RTL8168B */
  892. case Rtl8169: /* RTL8169 */
  893. break;
  894. case (0xC107<<16)|0x1259: /* Corega CG-LAPCIGT */
  895. i = Rtl8169;
  896. break;
  897. }
  898. port = p->mem[0].bar & ~0x01;
  899. if(ioalloc(port, p->mem[0].size, 0, "rtl8169") < 0){
  900. print("rtl8169: port %#ux in use\n", port);
  901. continue;
  902. }
  903. ctlr = malloc(sizeof(Ctlr));
  904. ctlr->port = port;
  905. ctlr->pcidev = p;
  906. ctlr->pciv = i;
  907. if(pcigetpms(p) > 0){
  908. pcisetpms(p, 0);
  909. for(i = 0; i < 6; i++)
  910. pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
  911. pcicfgw8(p, PciINTL, p->intl);
  912. pcicfgw8(p, PciLTR, p->ltr);
  913. pcicfgw8(p, PciCLS, p->cls);
  914. pcicfgw16(p, PciPCR, p->pcr);
  915. }
  916. if(rtl8169reset(ctlr)){
  917. iofree(port);
  918. free(ctlr);
  919. continue;
  920. }
  921. /*
  922. * Extract the chip hardware version,
  923. * needed to configure each properly.
  924. */
  925. ctlr->macv = csr32r(ctlr, Tcr) & HwveridMASK;
  926. rtl8169mii(ctlr);
  927. pcisetbme(p);
  928. if(rtl8169ctlrhead != nil)
  929. rtl8169ctlrtail->next = ctlr;
  930. else
  931. rtl8169ctlrhead = ctlr;
  932. rtl8169ctlrtail = ctlr;
  933. }
  934. }
  935. static int
  936. rtl8169pnp(Ether* edev)
  937. {
  938. u32int r;
  939. Ctlr *ctlr;
  940. uchar ea[Eaddrlen];
  941. if(rtl8169ctlrhead == nil)
  942. rtl8169pci();
  943. /*
  944. * Any adapter matches if no edev->port is supplied,
  945. * otherwise the ports must match.
  946. */
  947. for(ctlr = rtl8169ctlrhead; ctlr != nil; ctlr = ctlr->next){
  948. if(ctlr->active)
  949. continue;
  950. if(edev->port == 0 || edev->port == ctlr->port){
  951. ctlr->active = 1;
  952. break;
  953. }
  954. }
  955. if(ctlr == nil)
  956. return -1;
  957. edev->ctlr = ctlr;
  958. edev->port = ctlr->port;
  959. edev->irq = ctlr->pcidev->intl;
  960. edev->tbdf = ctlr->pcidev->tbdf;
  961. edev->mbps = 100;
  962. /*
  963. * Check if the adapter's station address is to be overridden.
  964. * If not, read it from the device and set in edev->ea.
  965. */
  966. memset(ea, 0, Eaddrlen);
  967. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  968. r = csr32r(ctlr, Idr0);
  969. edev->ea[0] = r;
  970. edev->ea[1] = r>>8;
  971. edev->ea[2] = r>>16;
  972. edev->ea[3] = r>>24;
  973. r = csr32r(ctlr, Idr0+4);
  974. edev->ea[4] = r;
  975. edev->ea[5] = r>>8;
  976. }
  977. edev->attach = rtl8169attach;
  978. edev->transmit = rtl8169transmit;
  979. edev->interrupt = rtl8169interrupt;
  980. edev->ifstat = rtl8169ifstat;
  981. edev->arg = edev;
  982. edev->promiscuous = rtl8169promiscuous;
  983. edev->multicast = rtl8169multicast;
  984. // edev->shutdown = rtl8169shutdown;
  985. rtl8169link(edev);
  986. return 0;
  987. }
  988. void
  989. ether8169link(void)
  990. {
  991. addethercard("rtl8169", rtl8169pnp);
  992. }