ether8169.c 21 KB

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