ether8169.c 18 KB

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