ether8139.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Realtek 8139 (but not the 8129).
  3. * Error recovery for the various over/under -flow conditions
  4. * may need work.
  5. */
  6. #include "u.h"
  7. #include "../port/lib.h"
  8. #include "mem.h"
  9. #include "dat.h"
  10. #include "fns.h"
  11. #include "io.h"
  12. #include "../port/error.h"
  13. #include "../port/netif.h"
  14. #include "etherif.h"
  15. enum { /* registers */
  16. Idr0 = 0x0000, /* MAC address */
  17. Mar0 = 0x0008, /* Multicast address */
  18. Tsd0 = 0x0010, /* Transmit Status Descriptor0 */
  19. Tsad0 = 0x0020, /* Transmit Start Address Descriptor0 */
  20. Rbstart = 0x0030, /* Receive Buffer Start Address */
  21. Erbcr = 0x0034, /* Early Receive Byte Count */
  22. Ersr = 0x0036, /* Early Receive Status */
  23. Cr = 0x0037, /* Command Register */
  24. Capr = 0x0038, /* Current Address of Packet Read */
  25. Cbr = 0x003A, /* Current Buffer Address */
  26. Imr = 0x003C, /* Interrupt Mask */
  27. Isr = 0x003E, /* Interrupt Status */
  28. Tcr = 0x0040, /* Transmit Configuration */
  29. Rcr = 0x0044, /* Receive Configuration */
  30. Tctr = 0x0048, /* Timer Count */
  31. Mpc = 0x004C, /* Missed Packet Counter */
  32. Cr9346 = 0x0050, /* 9346 Command Register */
  33. Config0 = 0x0051, /* Configuration Register 0 */
  34. Config1 = 0x0052, /* Configuration Register 1 */
  35. TimerInt = 0x0054, /* Timer Interrupt */
  36. Msr = 0x0058, /* Media Status */
  37. Config3 = 0x0059, /* Configuration Register 3 */
  38. Config4 = 0x005A, /* Configuration Register 4 */
  39. Mulint = 0x005C, /* Multiple Interrupt Select */
  40. RerID = 0x005E, /* PCI Revision ID */
  41. Tsad = 0x0060, /* Transmit Status of all Descriptors */
  42. Bmcr = 0x0062, /* Basic Mode Control */
  43. Bmsr = 0x0064, /* Basic Mode Status */
  44. Anar = 0x0066, /* Auto-Negotiation Advertisment */
  45. Anlpar = 0x0068, /* Auto-Negotiation Link Partner */
  46. Aner = 0x006A, /* Auto-Negotiation Expansion */
  47. Dis = 0x006C, /* Disconnect Counter */
  48. Fcsc = 0x006E, /* False Carrier Sense Counter */
  49. Nwaytr = 0x0070, /* N-way Test */
  50. Rec = 0x0072, /* RX_ER Counter */
  51. Cscr = 0x0074, /* CS Configuration */
  52. Phy1parm = 0x0078, /* PHY Parameter 1 */
  53. Twparm = 0x007C, /* Twister Parameter */
  54. Phy2parm = 0x0080, /* PHY Parameter 2 */
  55. };
  56. enum { /* Cr */
  57. Bufe = 0x01, /* Rx Buffer Empty */
  58. Te = 0x04, /* Transmitter Enable */
  59. Re = 0x08, /* Receiver Enable */
  60. Rst = 0x10, /* Software Reset */
  61. };
  62. enum { /* Imr/Isr */
  63. Rok = 0x0001, /* Receive OK */
  64. Rer = 0x0002, /* Receive Error */
  65. Tok = 0x0004, /* Transmit OK */
  66. Ter = 0x0008, /* Transmit Error */
  67. Rxovw = 0x0010, /* Receive Buffer Overflow */
  68. PunLc = 0x0020, /* Packet Underrun or Link Change */
  69. Fovw = 0x0040, /* Receive FIFO Overflow */
  70. Clc = 0x2000, /* Cable Length Change */
  71. Timerbit = 0x4000, /* Timer */
  72. Serr = 0x8000, /* System Error */
  73. };
  74. enum { /* Tcr */
  75. Clrabt = 0x00000001, /* Clear Abort */
  76. TxrrSHIFT = 4, /* Transmit Retry Count */
  77. TxrrMASK = 0x000000F0,
  78. MtxdmaSHIFT = 8, /* Max. DMA Burst Size */
  79. MtxdmaMASK = 0x00000700,
  80. Mtxdma2048 = 0x00000700,
  81. Acrc = 0x00010000, /* Append CRC (not) */
  82. LbkSHIFT = 17, /* Loopback Test */
  83. LbkMASK = 0x00060000,
  84. Rtl8139ArevG = 0x00800000, /* RTL8139A Rev. G ID */
  85. IfgSHIFT = 24, /* Interframe Gap */
  86. IfgMASK = 0x03000000,
  87. HwveridSHIFT = 26, /* Hardware Version ID */
  88. HwveridMASK = 0x7C000000,
  89. };
  90. enum { /* Rcr */
  91. Aap = 0x00000001, /* Accept All Packets */
  92. Apm = 0x00000002, /* Accept Physical Match */
  93. Am = 0x00000004, /* Accept Multicast */
  94. Ab = 0x00000008, /* Accept Broadcast */
  95. Ar = 0x00000010, /* Accept Runt */
  96. Aer = 0x00000020, /* Accept Error */
  97. Sel9356 = 0x00000040, /* 9356 EEPROM used */
  98. Wrap = 0x00000080, /* Rx Buffer Wrap Control */
  99. MrxdmaSHIFT = 8, /* Max. DMA Burst Size */
  100. MrxdmaMASK = 0x00000700,
  101. Mrxdmaunlimited = 0x00000700,
  102. RblenSHIFT = 11, /* Receive Buffer Length */
  103. RblenMASK = 0x00001800,
  104. Rblen8K = 0x00000000, /* 8KB+16 */
  105. Rblen16K = 0x00000800, /* 16KB+16 */
  106. Rblen32K = 0x00001000, /* 32KB+16 */
  107. Rblen64K = 0x00001800, /* 64KB+16 */
  108. RxfthSHIFT = 13, /* Receive Buffer Length */
  109. RxfthMASK = 0x0000E000,
  110. Rxfth256 = 0x00008000,
  111. Rxfthnone = 0x0000E000,
  112. Rer8 = 0x00010000, /* Accept Error Packets > 8 bytes */
  113. MulERINT = 0x00020000, /* Multiple Early Interrupt Select */
  114. ErxthSHIFT = 24, /* Early Rx Threshold */
  115. ErxthMASK = 0x0F000000,
  116. Erxthnone = 0x00000000,
  117. };
  118. enum { /* Received Packet Status */
  119. Rcok = 0x0001, /* Receive Completed OK */
  120. Fae = 0x0002, /* Frame Alignment Error */
  121. Crc = 0x0004, /* CRC Error */
  122. Long = 0x0008, /* Long Packet */
  123. Runt = 0x0010, /* Runt Packet Received */
  124. Ise = 0x0020, /* Invalid Symbol Error */
  125. Bar = 0x2000, /* Broadcast Address Received */
  126. Pam = 0x4000, /* Physical Address Matched */
  127. Mar = 0x8000, /* Multicast Address Received */
  128. };
  129. enum { /* Media Status Register */
  130. Rxpf = 0x01, /* Pause Flag */
  131. Txpf = 0x02, /* Pause Flag */
  132. Linkb = 0x04, /* Inverse of Link Status */
  133. Speed10 = 0x08, /* 10Mbps */
  134. Auxstatus = 0x10, /* Aux. Power Present Status */
  135. Rxfce = 0x40, /* Receive Flow Control Enable */
  136. Txfce = 0x80, /* Transmit Flow Control Enable */
  137. };
  138. typedef struct Td Td;
  139. struct Td { /* Soft Transmit Descriptor */
  140. int tsd;
  141. int tsad;
  142. uchar* data;
  143. Block* bp;
  144. };
  145. enum { /* Tsd0 */
  146. SizeSHIFT = 0, /* Descriptor Size */
  147. SizeMASK = 0x00001FFF,
  148. Own = 0x00002000,
  149. Tun = 0x00004000, /* Transmit FIFO Underrun */
  150. Tcok = 0x00008000, /* Transmit COmpleted OK */
  151. EtxthSHIFT = 16, /* Early Tx Threshold */
  152. EtxthMASK = 0x001F0000,
  153. NccSHIFT = 24, /* Number of Collisions Count */
  154. NccMASK = 0x0F000000,
  155. Cdh = 0x10000000, /* CD Heartbeat */
  156. Owc = 0x20000000, /* Out of Window Collision */
  157. Tabt = 0x40000000, /* Transmit Abort */
  158. Crs = 0x80000000, /* Carrier Sense Lost */
  159. };
  160. enum {
  161. Rblen = Rblen64K, /* Receive Buffer Length */
  162. Ntd = 4, /* Number of Transmit Descriptors */
  163. Tdbsz = ROUNDUP(sizeof(Etherpkt), 4),
  164. };
  165. typedef struct Ctlr Ctlr;
  166. typedef struct Ctlr {
  167. int port;
  168. Pcidev* pcidev;
  169. Ctlr* next;
  170. int active;
  171. int id;
  172. QLock alock; /* attach */
  173. Lock ilock; /* init */
  174. void* alloc; /* base of per-Ctlr allocated data */
  175. int rcr; /* receive configuration register */
  176. uchar* rbstart; /* receive buffer */
  177. int rblen; /* receive buffer length */
  178. int ierrs; /* receive errors */
  179. Lock tlock; /* transmit */
  180. Td td[Ntd];
  181. int ntd; /* descriptors active */
  182. int tdh; /* host index into td */
  183. int tdi; /* interface index into td */
  184. int etxth; /* early transmit threshold */
  185. int taligned; /* packet required no alignment */
  186. int tunaligned; /* packet required alignment */
  187. int dis; /* disconnect counter */
  188. int fcsc; /* false carrier sense counter */
  189. int rec; /* RX_ER counter */
  190. } Ctlr;
  191. static Ctlr* ctlrhead;
  192. static Ctlr* ctlrtail;
  193. #define csr8r(c, r) (inb((c)->port+(r)))
  194. #define csr16r(c, r) (ins((c)->port+(r)))
  195. #define csr32r(c, r) (inl((c)->port+(r)))
  196. #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
  197. #define csr16w(c, r, w) (outs((c)->port+(r), (ushort)(w)))
  198. #define csr32w(c, r, l) (outl((c)->port+(r), (ulong)(l)))
  199. static void
  200. rtl8139promiscuous(void* arg, int on)
  201. {
  202. Ether *edev;
  203. Ctlr * ctlr;
  204. edev = arg;
  205. ctlr = edev->ctlr;
  206. ilock(&ctlr->ilock);
  207. if(on)
  208. ctlr->rcr |= Aap;
  209. else
  210. ctlr->rcr &= ~Aap;
  211. csr32w(ctlr, Rcr, ctlr->rcr);
  212. iunlock(&ctlr->ilock);
  213. }
  214. static long
  215. rtl8139ifstat(Ether* edev, void* a, long n, ulong offset)
  216. {
  217. int l;
  218. char *p;
  219. Ctlr *ctlr;
  220. ctlr = edev->ctlr;
  221. p = malloc(READSTR);
  222. l = snprint(p, READSTR, "rcr %#8.8ux\n", ctlr->rcr);
  223. l += snprint(p+l, READSTR-l, "ierrs %d\n", ctlr->ierrs);
  224. l += snprint(p+l, READSTR-l, "etxth %d\n", ctlr->etxth);
  225. l += snprint(p+l, READSTR-l, "taligned %d\n", ctlr->taligned);
  226. l += snprint(p+l, READSTR-l, "tunaligned %d\n", ctlr->tunaligned);
  227. ctlr->dis += csr16r(ctlr, Dis);
  228. l += snprint(p+l, READSTR-l, "dis %d\n", ctlr->dis);
  229. ctlr->fcsc += csr16r(ctlr, Fcsc);
  230. l += snprint(p+l, READSTR-l, "fcscnt %d\n", ctlr->fcsc);
  231. ctlr->rec += csr16r(ctlr, Rec);
  232. l += snprint(p+l, READSTR-l, "rec %d\n", ctlr->rec);
  233. l += snprint(p+l, READSTR-l, "Tcr %#8.8lux\n", csr32r(ctlr, Tcr));
  234. l += snprint(p+l, READSTR-l, "Config0 %#2.2ux\n", csr8r(ctlr, Config0));
  235. l += snprint(p+l, READSTR-l, "Config1 %#2.2ux\n", csr8r(ctlr, Config1));
  236. l += snprint(p+l, READSTR-l, "Msr %#2.2ux\n", csr8r(ctlr, Msr));
  237. l += snprint(p+l, READSTR-l, "Config3 %#2.2ux\n", csr8r(ctlr, Config3));
  238. l += snprint(p+l, READSTR-l, "Config4 %#2.2ux\n", csr8r(ctlr, Config4));
  239. l += snprint(p+l, READSTR-l, "Bmcr %#4.4ux\n", csr16r(ctlr, Bmcr));
  240. l += snprint(p+l, READSTR-l, "Bmsr %#4.4ux\n", csr16r(ctlr, Bmsr));
  241. l += snprint(p+l, READSTR-l, "Anar %#4.4ux\n", csr16r(ctlr, Anar));
  242. l += snprint(p+l, READSTR-l, "Anlpar %#4.4ux\n", csr16r(ctlr, Anlpar));
  243. l += snprint(p+l, READSTR-l, "Aner %#4.4ux\n", csr16r(ctlr, Aner));
  244. l += snprint(p+l, READSTR-l, "Nwaytr %#4.4ux\n", csr16r(ctlr, Nwaytr));
  245. snprint(p+l, READSTR-l, "Cscr %#4.4ux\n", csr16r(ctlr, Cscr));
  246. n = readstr(offset, a, n, p);
  247. free(p);
  248. return n;
  249. }
  250. static int
  251. rtl8139reset(Ctlr* ctlr)
  252. {
  253. int timeo;
  254. /*
  255. * Soft reset the controller.
  256. */
  257. csr8w(ctlr, Cr, Rst);
  258. for(timeo = 0; timeo < 1000; timeo++){
  259. if(!(csr8r(ctlr, Cr) & Rst))
  260. return 0;
  261. delay(1);
  262. }
  263. return -1;
  264. }
  265. static void
  266. rtl8139halt(Ctlr* ctlr)
  267. {
  268. int i;
  269. csr8w(ctlr, Cr, 0);
  270. csr16w(ctlr, Imr, 0);
  271. csr16w(ctlr, Isr, ~0);
  272. for(i = 0; i < Ntd; i++){
  273. if(ctlr->td[i].bp == nil)
  274. continue;
  275. freeb(ctlr->td[i].bp);
  276. ctlr->td[i].bp = nil;
  277. }
  278. }
  279. static void
  280. rtl8139init(Ether* edev)
  281. {
  282. int i;
  283. ulong r;
  284. Ctlr *ctlr;
  285. uchar *alloc;
  286. ctlr = edev->ctlr;
  287. ilock(&ctlr->ilock);
  288. rtl8139halt(ctlr);
  289. /*
  290. * MAC Address.
  291. */
  292. r = (edev->ea[3]<<24)|(edev->ea[2]<<16)|(edev->ea[1]<<8)|edev->ea[0];
  293. csr32w(ctlr, Idr0, r);
  294. r = (edev->ea[5]<<8)|edev->ea[4];
  295. csr32w(ctlr, Idr0+4, r);
  296. /*
  297. * Receiver
  298. */
  299. alloc = (uchar*)ROUNDUP((ulong)ctlr->alloc, 32);
  300. ctlr->rbstart = alloc;
  301. alloc += ctlr->rblen+16;
  302. memset(ctlr->rbstart, 0, ctlr->rblen+16);
  303. csr32w(ctlr, Rbstart, PCIWADDR(ctlr->rbstart));
  304. ctlr->rcr = Rxfth256|Rblen|Mrxdmaunlimited|Ab|Apm;
  305. /*
  306. * Transmitter.
  307. */
  308. for(i = 0; i < Ntd; i++){
  309. ctlr->td[i].tsd = Tsd0+i*4;
  310. ctlr->td[i].tsad = Tsad0+i*4;
  311. ctlr->td[i].data = alloc;
  312. alloc += Tdbsz;
  313. ctlr->td[i].bp = nil;
  314. }
  315. ctlr->ntd = ctlr->tdh = ctlr->tdi = 0;
  316. ctlr->etxth = 128/32;
  317. /*
  318. * Interrupts.
  319. */
  320. csr32w(ctlr, TimerInt, 0);
  321. csr16w(ctlr, Imr, Serr|Timerbit|Fovw|PunLc|Rxovw|Ter|Tok|Rer|Rok);
  322. csr32w(ctlr, Mpc, 0);
  323. /*
  324. * Enable receiver/transmitter.
  325. * Need to enable before writing the Rcr or it won't take.
  326. */
  327. csr8w(ctlr, Cr, Te|Re);
  328. csr32w(ctlr, Tcr, Mtxdma2048);
  329. csr32w(ctlr, Rcr, ctlr->rcr);
  330. iunlock(&ctlr->ilock);
  331. }
  332. static void
  333. rtl8139attach(Ether* edev)
  334. {
  335. Ctlr *ctlr;
  336. ctlr = edev->ctlr;
  337. qlock(&ctlr->alock);
  338. if(ctlr->alloc == nil){
  339. ctlr->rblen = 1<<((Rblen>>RblenSHIFT)+13);
  340. ctlr->alloc = mallocz(ctlr->rblen+16 + Ntd*Tdbsz + 32, 0);
  341. rtl8139init(edev);
  342. }
  343. qunlock(&ctlr->alock);
  344. }
  345. static void
  346. rtl8139txstart(Ether* edev)
  347. {
  348. Td *td;
  349. int size;
  350. Block *bp;
  351. Ctlr *ctlr;
  352. ctlr = edev->ctlr;
  353. while(ctlr->ntd < Ntd){
  354. bp = qget(edev->oq);
  355. if(bp == nil)
  356. break;
  357. size = BLEN(bp);
  358. td = &ctlr->td[ctlr->tdh];
  359. if(((int)bp->rp) & 0x03){
  360. memmove(td->data, bp->rp, size);
  361. freeb(bp);
  362. csr32w(ctlr, td->tsad, PCIWADDR(td->data));
  363. ctlr->tunaligned++;
  364. }
  365. else{
  366. td->bp = bp;
  367. csr32w(ctlr, td->tsad, PCIWADDR(bp->rp));
  368. ctlr->taligned++;
  369. }
  370. csr32w(ctlr, td->tsd, (ctlr->etxth<<EtxthSHIFT)|size);
  371. ctlr->ntd++;
  372. ctlr->tdh = NEXT(ctlr->tdh, Ntd);
  373. }
  374. }
  375. static void
  376. rtl8139transmit(Ether* edev)
  377. {
  378. Ctlr *ctlr;
  379. ctlr = edev->ctlr;
  380. ilock(&ctlr->tlock);
  381. rtl8139txstart(edev);
  382. iunlock(&ctlr->tlock);
  383. }
  384. static void
  385. rtl8139receive(Ether* edev)
  386. {
  387. Block *bp;
  388. Ctlr *ctlr;
  389. ushort capr;
  390. uchar cr, *p;
  391. int l, length, status;
  392. ctlr = edev->ctlr;
  393. /*
  394. * Capr is where the host is reading from,
  395. * Cbr is where the NIC is currently writing.
  396. */
  397. capr = (csr16r(ctlr, Capr)+16) % ctlr->rblen;
  398. while(!(csr8r(ctlr, Cr) & Bufe)){
  399. p = ctlr->rbstart+capr;
  400. /*
  401. * Apparently the packet length may be 0xFFF0 if
  402. * the NIC is still copying the packet into memory.
  403. */
  404. length = (*(p+3)<<8)|*(p+2);
  405. if(length == 0xFFF0)
  406. break;
  407. status = (*(p+1)<<8)|*p;
  408. if(!(status & Rcok)){
  409. if(status & (Ise|Fae))
  410. edev->frames++;
  411. if(status & Crc)
  412. edev->crcs++;
  413. if(status & (Runt|Long))
  414. edev->buffs++;
  415. /*
  416. * Reset the receiver.
  417. * Also may have to restore the multicast list
  418. * here too if it ever gets used.
  419. */
  420. cr = csr8r(ctlr, Cr);
  421. csr8w(ctlr, Cr, cr & ~Re);
  422. csr32w(ctlr, Rbstart, PCIWADDR(ctlr->rbstart));
  423. csr8w(ctlr, Cr, cr);
  424. csr32w(ctlr, Rcr, ctlr->rcr);
  425. continue;
  426. }
  427. /*
  428. * Receive Completed OK.
  429. * Very simplistic; there are ways this could be done
  430. * without copying, but the juice probably isn't worth
  431. * the squeeze.
  432. * The packet length includes a 4 byte CRC on the end.
  433. */
  434. capr = (capr+4) % ctlr->rblen;
  435. p = ctlr->rbstart+capr;
  436. capr = (capr+length) % ctlr->rblen;
  437. if((bp = iallocb(length)) != nil){
  438. if(p+length >= ctlr->rbstart+ctlr->rblen){
  439. l = ctlr->rbstart+ctlr->rblen - p;
  440. memmove(bp->wp, p, l);
  441. bp->wp += l;
  442. length -= l;
  443. p = ctlr->rbstart;
  444. }
  445. if(length > 0){
  446. memmove(bp->wp, p, length);
  447. bp->wp += length;
  448. }
  449. bp->wp -= 4;
  450. etheriq(edev, bp, 1);
  451. }
  452. capr = ROUNDUP(capr, 4);
  453. csr16w(ctlr, Capr, capr-16);
  454. }
  455. }
  456. static void
  457. rtl8139interrupt(Ureg*, void* arg)
  458. {
  459. Td *td;
  460. Ctlr *ctlr;
  461. Ether *edev;
  462. int isr, msr, tsd;
  463. edev = arg;
  464. ctlr = edev->ctlr;
  465. while((isr = csr16r(ctlr, Isr)) != 0){
  466. csr16w(ctlr, Isr, isr);
  467. if(isr & (Fovw|PunLc|Rxovw|Rer|Rok)){
  468. rtl8139receive(edev);
  469. if(!(isr & Rok))
  470. ctlr->ierrs++;
  471. isr &= ~(Fovw|Rxovw|Rer|Rok);
  472. }
  473. if(isr & (Ter|Tok)){
  474. ilock(&ctlr->tlock);
  475. while(ctlr->ntd){
  476. td = &ctlr->td[ctlr->tdi];
  477. tsd = csr32r(ctlr, td->tsd);
  478. if(!(tsd & (Tabt|Tun|Tcok)))
  479. break;
  480. if(!(tsd & Tcok)){
  481. if(tsd & Tun){
  482. if(ctlr->etxth < ETHERMAXTU/32)
  483. ctlr->etxth++;
  484. }
  485. edev->oerrs++;
  486. }
  487. if(td->bp != nil){
  488. freeb(td->bp);
  489. td->bp = nil;
  490. }
  491. ctlr->ntd--;
  492. ctlr->tdi = NEXT(ctlr->tdi, Ntd);
  493. }
  494. rtl8139txstart(edev);
  495. iunlock(&ctlr->tlock);
  496. isr &= ~(Ter|Tok);
  497. }
  498. if(isr & PunLc){
  499. /*
  500. * Maybe the link changed - do we care very much?
  501. */
  502. msr = csr8r(ctlr, Msr);
  503. if(!(msr & Linkb)){
  504. if(!(msr & Speed10) && edev->mbps != 100){
  505. edev->mbps = 100;
  506. qsetlimit(edev->oq, 256*1024);
  507. }
  508. else if((msr & Speed10) && edev->mbps != 10){
  509. edev->mbps = 10;
  510. qsetlimit(edev->oq, 65*1024);
  511. }
  512. }
  513. isr &= ~(Clc|PunLc);
  514. }
  515. /*
  516. * Only Serr|Timerbit should be left by now.
  517. * Should anything be done to tidy up? TimerInt isn't
  518. * used so that can be cleared. A PCI bus error is indicated
  519. * by Serr, that's pretty serious; is there anyhing to do
  520. * other than try to reinitialise the chip?
  521. */
  522. if((isr & (Serr|Timerbit)) != 0){
  523. iprint("rtl8139interrupt: imr %#4.4ux isr %#4.4ux\n",
  524. csr16r(ctlr, Imr), isr);
  525. if(isr & Timerbit)
  526. csr32w(ctlr, TimerInt, 0);
  527. if(isr & Serr)
  528. rtl8139init(edev);
  529. }
  530. }
  531. }
  532. static Ctlr*
  533. rtl8139match(Ether* edev, int id)
  534. {
  535. Pcidev *p;
  536. Ctlr *ctlr;
  537. int i, port;
  538. /*
  539. * Any adapter matches if no edev->port is supplied,
  540. * otherwise the ports must match.
  541. */
  542. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  543. if(ctlr->active)
  544. continue;
  545. p = ctlr->pcidev;
  546. if(((p->did<<16)|p->vid) != id)
  547. continue;
  548. port = p->mem[0].bar & ~0x01;
  549. if(edev->port != 0 && edev->port != port)
  550. continue;
  551. if(ioalloc(port, p->mem[0].size, 0, "rtl8139") < 0){
  552. print("rtl8139: port %#ux in use\n", port);
  553. continue;
  554. }
  555. if(pcigetpms(p) > 0){
  556. pcisetpms(p, 0);
  557. for(i = 0; i < 6; i++)
  558. pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
  559. pcicfgw8(p, PciINTL, p->intl);
  560. pcicfgw8(p, PciLTR, p->ltr);
  561. pcicfgw8(p, PciCLS, p->cls);
  562. pcicfgw16(p, PciPCR, p->pcr);
  563. }
  564. ctlr->port = port;
  565. if(rtl8139reset(ctlr))
  566. continue;
  567. pcisetbme(p);
  568. ctlr->active = 1;
  569. return ctlr;
  570. }
  571. return nil;
  572. }
  573. static struct {
  574. char* name;
  575. int id;
  576. } rtl8139pci[] = {
  577. { "rtl8139", (0x8139<<16)|0x10EC, }, /* generic */
  578. { "smc1211", (0x1211<<16)|0x1113, }, /* SMC EZ-Card */
  579. { "dfe-538tx", (0x1300<<16)|0x1186, }, /* D-Link DFE-538TX */
  580. { "dfe-560txd", (0x1340<<16)|0x1186, }, /* D-Link DFE-560TXD */
  581. { nil },
  582. };
  583. static int
  584. rtl8139pnp(Ether* edev)
  585. {
  586. int i, id;
  587. Pcidev *p;
  588. Ctlr *ctlr;
  589. uchar ea[Eaddrlen];
  590. /*
  591. * Make a list of all ethernet controllers
  592. * if not already done.
  593. */
  594. if(ctlrhead == nil){
  595. p = nil;
  596. while(p = pcimatch(p, 0, 0)){
  597. if(p->ccrb != 0x02 || p->ccru != 0)
  598. continue;
  599. ctlr = malloc(sizeof(Ctlr));
  600. ctlr->pcidev = p;
  601. ctlr->id = (p->did<<16)|p->vid;
  602. if(ctlrhead != nil)
  603. ctlrtail->next = ctlr;
  604. else
  605. ctlrhead = ctlr;
  606. ctlrtail = ctlr;
  607. }
  608. }
  609. /*
  610. * Is it an RTL8139 under a different name?
  611. * Normally a search is made through all the found controllers
  612. * for one which matches any of the known vid+did pairs.
  613. * If a vid+did pair is specified a search is made for that
  614. * specific controller only.
  615. */
  616. id = 0;
  617. for(i = 0; i < edev->nopt; i++){
  618. if(cistrncmp(edev->opt[i], "id=", 3) == 0)
  619. id = strtol(&edev->opt[i][3], nil, 0);
  620. }
  621. ctlr = nil;
  622. if(id != 0)
  623. ctlr = rtl8139match(edev, id);
  624. else for(i = 0; rtl8139pci[i].name; i++){
  625. if((ctlr = rtl8139match(edev, rtl8139pci[i].id)) != nil)
  626. break;
  627. }
  628. if(ctlr == nil)
  629. return -1;
  630. edev->ctlr = ctlr;
  631. edev->port = ctlr->port;
  632. edev->irq = ctlr->pcidev->intl;
  633. edev->tbdf = ctlr->pcidev->tbdf;
  634. /*
  635. * Check if the adapter's station address is to be overridden.
  636. * If not, read it from the device and set in edev->ea.
  637. */
  638. memset(ea, 0, Eaddrlen);
  639. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  640. i = csr32r(ctlr, Idr0);
  641. edev->ea[0] = i;
  642. edev->ea[1] = i>>8;
  643. edev->ea[2] = i>>16;
  644. edev->ea[3] = i>>24;
  645. i = csr32r(ctlr, Idr0+4);
  646. edev->ea[4] = i;
  647. edev->ea[5] = i>>8;
  648. }
  649. edev->attach = rtl8139attach;
  650. edev->transmit = rtl8139transmit;
  651. edev->interrupt = rtl8139interrupt;
  652. edev->ifstat = rtl8139ifstat;
  653. edev->arg = edev;
  654. edev->promiscuous = rtl8139promiscuous;
  655. /*
  656. * This should be much more dynamic but will do for now.
  657. */
  658. if((csr8r(ctlr, Msr) & (Speed10|Linkb)) == 0)
  659. edev->mbps = 100;
  660. return 0;
  661. }
  662. void
  663. ether8139link(void)
  664. {
  665. addethercard("rtl8139", rtl8139pnp);
  666. }