ether8139.c 18 KB

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