ether8169.c 18 KB

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