ether8139.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. Timer = 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. #ifndef FS
  207. static void
  208. rtl8139promiscuous(void* arg, int on)
  209. {
  210. Ether *edev;
  211. Ctlr * ctlr;
  212. edev = arg;
  213. ctlr = edev->ctlr;
  214. ilock(&ctlr->ilock);
  215. if(on)
  216. ctlr->rcr |= Aap;
  217. else
  218. ctlr->rcr &= ~Aap;
  219. csr32w(ctlr, Rcr, ctlr->rcr);
  220. iunlock(&ctlr->ilock);
  221. }
  222. static long
  223. rtl8139ifstat(Ether* edev, void* a, long n, ulong offset)
  224. {
  225. int l;
  226. char *p;
  227. Ctlr *ctlr;
  228. ctlr = edev->ctlr;
  229. p = malloc(READSTR);
  230. l = snprint(p, READSTR, "rcr %8.8uX\n", ctlr->rcr);
  231. l += snprint(p+l, READSTR-l, "ierrs %d\n", ctlr->ierrs);
  232. l += snprint(p+l, READSTR-l, "etxth %d\n", ctlr->etxth);
  233. l += snprint(p+l, READSTR-l, "taligned %d\n", ctlr->taligned);
  234. l += snprint(p+l, READSTR-l, "tunaligned %d\n", ctlr->tunaligned);
  235. ctlr->dis += csr16r(ctlr, Dis);
  236. l += snprint(p+l, READSTR-l, "dis %d\n", ctlr->dis);
  237. ctlr->fcsc += csr16r(ctlr, Fcsc);
  238. l += snprint(p+l, READSTR-l, "fcscnt %d\n", ctlr->fcsc);
  239. ctlr->rec += csr16r(ctlr, Rec);
  240. l += snprint(p+l, READSTR-l, "rec %d\n", ctlr->rec);
  241. l += snprint(p+l, READSTR-l, "Tcr %8.8luX\n", csr32r(ctlr, Tcr));
  242. l += snprint(p+l, READSTR-l, "Config0 %2.2uX\n", csr8r(ctlr, Config0));
  243. l += snprint(p+l, READSTR-l, "Config1 %2.2uX\n", csr8r(ctlr, Config1));
  244. l += snprint(p+l, READSTR-l, "Msr %2.2uX\n", csr8r(ctlr, Msr));
  245. l += snprint(p+l, READSTR-l, "Config3 %2.2uX\n", csr8r(ctlr, Config3));
  246. l += snprint(p+l, READSTR-l, "Config4 %2.2uX\n", csr8r(ctlr, Config4));
  247. l += snprint(p+l, READSTR-l, "Bmcr %4.4uX\n", csr16r(ctlr, Bmcr));
  248. l += snprint(p+l, READSTR-l, "Bmsr %4.4uX\n", csr16r(ctlr, Bmsr));
  249. l += snprint(p+l, READSTR-l, "Anar %4.4uX\n", csr16r(ctlr, Anar));
  250. l += snprint(p+l, READSTR-l, "Anlpar %4.4uX\n", csr16r(ctlr, Anlpar));
  251. l += snprint(p+l, READSTR-l, "Aner %4.4uX\n", csr16r(ctlr, Aner));
  252. l += snprint(p+l, READSTR-l, "Nwaytr %4.4uX\n", csr16r(ctlr, Nwaytr));
  253. snprint(p+l, READSTR-l, "Cscr %4.4uX\n", csr16r(ctlr, Cscr));
  254. n = readstr(offset, a, n, p);
  255. free(p);
  256. return n;
  257. }
  258. #endif
  259. static int
  260. rtl8139reset(Ctlr* ctlr)
  261. {
  262. /*
  263. * Soft reset the controller.
  264. */
  265. csr8w(ctlr, Cr, Rst);
  266. while(csr8r(ctlr, Cr) & Rst)
  267. ;
  268. return 0;
  269. }
  270. static void
  271. rtl8139halt(Ctlr* ctlr)
  272. {
  273. int i;
  274. csr8w(ctlr, Cr, 0);
  275. csr16w(ctlr, Imr, 0);
  276. csr16w(ctlr, Isr, ~0);
  277. for(i = 0; i < Ntd; i++){
  278. if(ctlr->td[i].bp == nil)
  279. continue;
  280. freeb(ctlr->td[i].bp);
  281. ctlr->td[i].bp = nil;
  282. }
  283. }
  284. static void
  285. rtl8139init(Ether* edev)
  286. {
  287. int i;
  288. ulong r;
  289. Ctlr *ctlr;
  290. uchar *alloc;
  291. ctlr = edev->ctlr;
  292. ilock(&ctlr->ilock);
  293. rtl8139halt(ctlr);
  294. /*
  295. * MAC Address.
  296. */
  297. r = (edev->ea[3]<<24)|(edev->ea[2]<<16)|(edev->ea[1]<<8)|edev->ea[0];
  298. csr32w(ctlr, Idr0, r);
  299. r = (edev->ea[5]<<8)|edev->ea[4];
  300. csr32w(ctlr, Idr0+4, r);
  301. /*
  302. * Receiver
  303. */
  304. alloc = (uchar*)ROUNDUP((ulong)ctlr->alloc, 32);
  305. ctlr->rbstart = alloc;
  306. alloc += ctlr->rblen+16;
  307. memset(ctlr->rbstart, 0, ctlr->rblen+16);
  308. csr32w(ctlr, Rbstart, PCIWADDR(ctlr->rbstart));
  309. ctlr->rcr = Rxfth256|Rblen|Mrxdmaunlimited|Ab|Apm;
  310. /*
  311. * Transmitter.
  312. */
  313. for(i = 0; i < Ntd; i++){
  314. ctlr->td[i].tsd = Tsd0+i*4;
  315. ctlr->td[i].tsad = Tsad0+i*4;
  316. ctlr->td[i].data = alloc;
  317. alloc += Tdbsz;
  318. ctlr->td[i].bp = nil;
  319. }
  320. ctlr->ntd = ctlr->tdh = ctlr->tdi = 0;
  321. ctlr->etxth = 128/32;
  322. /*
  323. * Interrupts.
  324. */
  325. csr32w(ctlr, TimerInt, 0);
  326. csr16w(ctlr, Imr, Serr|Timer|Fovw|PunLc|Rxovw|Ter|Tok|Rer|Rok);
  327. csr32w(ctlr, Mpc, 0);
  328. /*
  329. * Enable receiver/transmitter.
  330. * Need to enable before writing the Rcr or it won't take.
  331. */
  332. csr8w(ctlr, Cr, Te|Re);
  333. csr32w(ctlr, Tcr, Mtxdma2048);
  334. csr32w(ctlr, Rcr, ctlr->rcr);
  335. iunlock(&ctlr->ilock);
  336. }
  337. static void
  338. rtl8139attach(Ether* edev)
  339. {
  340. Ctlr *ctlr;
  341. ctlr = edev->ctlr;
  342. qlock(&ctlr->alock);
  343. if(ctlr->alloc == nil){
  344. ctlr->rblen = 1<<((Rblen>>RblenSHIFT)+13);
  345. ctlr->alloc = mallocz(ctlr->rblen+16 + Ntd*Tdbsz + 32, 0);
  346. rtl8139init(edev);
  347. }
  348. qunlock(&ctlr->alock);
  349. }
  350. static void
  351. rtl8139txstart(Ether* edev)
  352. {
  353. Td *td;
  354. int size;
  355. Block *bp;
  356. Ctlr *ctlr;
  357. ctlr = edev->ctlr;
  358. while(ctlr->ntd < Ntd){
  359. bp = etheroq(edev);
  360. if(bp == nil)
  361. break;
  362. size = BLEN(bp);
  363. td = &ctlr->td[ctlr->tdh];
  364. if(((int)bp->rp) & 0x03){
  365. memmove(td->data, bp->rp, size);
  366. freeb(bp);
  367. csr32w(ctlr, td->tsad, PCIWADDR(td->data));
  368. ctlr->tunaligned++;
  369. }
  370. else{
  371. td->bp = bp;
  372. csr32w(ctlr, td->tsad, PCIWADDR(bp->rp));
  373. ctlr->taligned++;
  374. }
  375. csr32w(ctlr, td->tsd, (ctlr->etxth<<EtxthSHIFT)|size);
  376. ctlr->ntd++;
  377. ctlr->tdh = NEXT(ctlr->tdh, Ntd);
  378. }
  379. }
  380. static void
  381. rtl8139transmit(Ether* edev)
  382. {
  383. Ctlr *ctlr;
  384. ctlr = edev->ctlr;
  385. ilock(&ctlr->tlock);
  386. rtl8139txstart(edev);
  387. iunlock(&ctlr->tlock);
  388. }
  389. static void
  390. rtl8139receive(Ether* edev)
  391. {
  392. Block *bp;
  393. Ctlr *ctlr;
  394. ushort capr;
  395. uchar cr, *p;
  396. int l, length, status;
  397. ctlr = edev->ctlr;
  398. /*
  399. * Capr is where the host is reading from,
  400. * Cbr is where the NIC is currently writing.
  401. */
  402. capr = (csr16r(ctlr, Capr)+16) % ctlr->rblen;
  403. while(!(csr8r(ctlr, Cr) & Bufe)){
  404. p = ctlr->rbstart+capr;
  405. /*
  406. * Apparently the packet length may be 0xFFF0 if
  407. * the NIC is still copying the packet into memory.
  408. */
  409. length = (*(p+3)<<8)|*(p+2);
  410. if(length == 0xFFF0)
  411. break;
  412. status = (*(p+1)<<8)|*p;
  413. if(!(status & Rcok)){
  414. #ifndef FS
  415. if(status & (Ise|Fae))
  416. edev->frames++;
  417. if(status & Crc)
  418. edev->crcs++;
  419. if(status & (Runt|Long))
  420. edev->buffs++;
  421. #endif
  422. /*
  423. * Reset the receiver.
  424. * Also may have to restore the multicast list
  425. * here too if it ever gets used.
  426. */
  427. cr = csr8r(ctlr, Cr);
  428. csr8w(ctlr, Cr, cr & ~Re);
  429. csr32w(ctlr, Rbstart, PCIWADDR(ctlr->rbstart));
  430. csr8w(ctlr, Cr, cr);
  431. csr32w(ctlr, Rcr, ctlr->rcr);
  432. continue;
  433. }
  434. /*
  435. * Receive Completed OK.
  436. * Very simplistic; there are ways this could be done
  437. * without copying, but the juice probably isn't worth
  438. * the squeeze.
  439. * The packet length includes a 4 byte CRC on the end.
  440. */
  441. capr = (capr+4) % ctlr->rblen;
  442. p = ctlr->rbstart+capr;
  443. capr = (capr+length) % ctlr->rblen;
  444. if((bp = iallocb(length)) != nil){
  445. SETWPCNT(bp, 0);
  446. if(p+length >= ctlr->rbstart+ctlr->rblen){
  447. l = ctlr->rbstart+ctlr->rblen - p;
  448. memmove(ENDDATA(bp), p, l);
  449. INCRPTR(bp, l);
  450. length -= l;
  451. p = ctlr->rbstart;
  452. }
  453. if(length > 0){
  454. memmove(ENDDATA(bp), p, length);
  455. INCRPTR(bp, length);
  456. }
  457. INCRPTR(bp, -4);
  458. if (BLEN(bp) < 0) {
  459. print("rtl8139receive: input packet of negative length\n");
  460. SETWPCNT(bp, 0);
  461. freeb(bp);
  462. } else
  463. ETHERIQ(edev, bp, 1);
  464. }
  465. capr = ROUNDUP(capr, 4);
  466. csr16w(ctlr, Capr, capr-16);
  467. }
  468. }
  469. static void
  470. rtl8139interrupt(Ureg*, void* arg)
  471. {
  472. Td *td;
  473. Ctlr *ctlr;
  474. Ether *edev;
  475. int isr, msr, tsd;
  476. edev = arg;
  477. ctlr = edev->ctlr;
  478. while((isr = csr16r(ctlr, Isr)) != 0){
  479. csr16w(ctlr, Isr, isr);
  480. if(isr & (Fovw|PunLc|Rxovw|Rer|Rok)){
  481. rtl8139receive(edev);
  482. if(!(isr & Rok))
  483. ctlr->ierrs++;
  484. isr &= ~(Fovw|Rxovw|Rer|Rok);
  485. }
  486. if(isr & (Ter|Tok)){
  487. ilock(&ctlr->tlock);
  488. while(ctlr->ntd){
  489. td = &ctlr->td[ctlr->tdi];
  490. tsd = csr32r(ctlr, td->tsd);
  491. if(!(tsd & (Tabt|Tun|Tcok)))
  492. break;
  493. if(!(tsd & Tcok)){
  494. if(tsd & Tun){
  495. if(ctlr->etxth < ETHERMAXTU/32)
  496. ctlr->etxth++;
  497. }
  498. #ifndef FS
  499. edev->oerrs++;
  500. #endif
  501. }
  502. if(td->bp != nil){
  503. freeb(td->bp);
  504. td->bp = nil;
  505. }
  506. ctlr->ntd--;
  507. ctlr->tdi = NEXT(ctlr->tdi, Ntd);
  508. }
  509. rtl8139txstart(edev);
  510. iunlock(&ctlr->tlock);
  511. isr &= ~(Ter|Tok);
  512. }
  513. if(isr & PunLc){
  514. /*
  515. * Maybe the link changed - do we care very much?
  516. */
  517. msr = csr8r(ctlr, Msr);
  518. if(!(msr & Linkb)){
  519. if(!(msr & Speed10) && edev->mbps != 100){
  520. edev->mbps = 100;
  521. qsetlimit(edev->oq, 256*1024);
  522. }
  523. else if((msr & Speed10) && edev->mbps != 10){
  524. edev->mbps = 10;
  525. qsetlimit(edev->oq, 65*1024);
  526. }
  527. }
  528. isr &= ~(Clc|PunLc);
  529. }
  530. /*
  531. * Only Serr|Timer should be left by now.
  532. * Should anything be done to tidy up? TimerInt isn't
  533. * used so that can be cleared. A PCI bus error is indicated
  534. * by Serr, that's pretty serious; is there anyhing to do
  535. * other than try to reinitialise the chip?
  536. */
  537. if(isr != 0){
  538. iprint("rtl8139interrupt: imr %4.4ux isr %4.4ux\n",
  539. csr16r(ctlr, Imr), isr);
  540. if(isr & Timer)
  541. csr32w(ctlr, TimerInt, 0);
  542. if(isr & Serr)
  543. rtl8139init(edev);
  544. }
  545. }
  546. }
  547. static Ctlr*
  548. rtl8139match(Ether* edev, int id)
  549. {
  550. int port;
  551. Pcidev *p;
  552. Ctlr *ctlr;
  553. /*
  554. * Any adapter matches if no edev->port is supplied,
  555. * otherwise the ports must match.
  556. */
  557. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  558. if(ctlr->active)
  559. continue;
  560. p = ctlr->pcidev;
  561. if(((p->did<<16)|p->vid) != id)
  562. continue;
  563. port = p->mem[0].bar & ~0x01;
  564. if(edev->port != 0 && edev->port != port)
  565. continue;
  566. if(ioalloc(port, p->mem[0].size, 0, "rtl8139") < 0){
  567. print("rtl8139: port 0x%ux in use\n", port);
  568. continue;
  569. }
  570. ctlr->port = port;
  571. if(rtl8139reset(ctlr))
  572. continue;
  573. pcisetbme(p);
  574. ctlr->active = 1;
  575. return ctlr;
  576. }
  577. return nil;
  578. }
  579. static struct {
  580. char* name;
  581. int id;
  582. } rtl8139pci[] = {
  583. { "rtl8139", (0x8139<<16)|0x10EC, }, /* generic */
  584. { "smc1211", (0x1211<<16)|0x1113, }, /* SMC EZ-Card */
  585. { nil },
  586. };
  587. int
  588. rtl8139pnp(Ether* edev)
  589. {
  590. int i, id;
  591. Pcidev *p;
  592. Ctlr *ctlr;
  593. uchar ea[Eaddrlen];
  594. /*
  595. * Make a list of all ethernet controllers
  596. * if not already done.
  597. */
  598. if(ctlrhead == nil){
  599. p = nil;
  600. while(p = pcimatch(p, 0, 0)){
  601. #ifdef FS
  602. if(p->ccru != ((0x02<<8)|0x00))
  603. #else
  604. if(p->ccrb != 0x02 || p->ccru != 0)
  605. #endif
  606. continue;
  607. ctlr = malloc(sizeof(Ctlr));
  608. ctlr->pcidev = p;
  609. ctlr->id = (p->did<<16)|p->vid;
  610. if(ctlrhead != nil)
  611. ctlrtail->next = ctlr;
  612. else
  613. ctlrhead = ctlr;
  614. ctlrtail = ctlr;
  615. }
  616. }
  617. /*
  618. * Is it an RTL8139 under a different name?
  619. * Normally a search is made through all the found controllers
  620. * for one which matches any of the known vid+did pairs.
  621. * If a vid+did pair is specified a search is made for that
  622. * specific controller only.
  623. */
  624. id = 0;
  625. for(i = 0; i < edev->nopt; i++){
  626. if(cistrncmp(edev->opt[i], "id=", 3) == 0)
  627. id = strtol(&edev->opt[i][3], nil, 0);
  628. }
  629. ctlr = nil;
  630. if(id != 0)
  631. ctlr = rtl8139match(edev, id);
  632. else for(i = 0; rtl8139pci[i].name; i++){
  633. if((ctlr = rtl8139match(edev, rtl8139pci[i].id)) != nil)
  634. break;
  635. }
  636. if(ctlr == nil)
  637. return -1;
  638. edev->ctlr = ctlr;
  639. edev->port = ctlr->port;
  640. edev->irq = ctlr->pcidev->intl;
  641. edev->tbdf = ctlr->pcidev->tbdf;
  642. /*
  643. * Check if the adapter's station address is to be overridden.
  644. * If not, read it from the device and set in edev->ea.
  645. */
  646. memset(ea, 0, Eaddrlen);
  647. if(memcmp(ea, edev->ea, Eaddrlen) == 0){
  648. i = csr32r(ctlr, Idr0);
  649. edev->ea[0] = i;
  650. edev->ea[1] = i>>8;
  651. edev->ea[2] = i>>16;
  652. edev->ea[3] = i>>24;
  653. i = csr32r(ctlr, Idr0+4);
  654. edev->ea[4] = i;
  655. edev->ea[5] = i>>8;
  656. }
  657. edev->attach = rtl8139attach;
  658. edev->transmit = rtl8139transmit;
  659. edev->interrupt = rtl8139interrupt;
  660. #ifndef FS
  661. edev->ifstat = rtl8139ifstat;
  662. edev->arg = edev;
  663. edev->promiscuous = rtl8139promiscuous;
  664. #endif
  665. /*
  666. * This should be much more dynamic but will do for now.
  667. */
  668. if((csr8r(ctlr, Msr) & (Speed10|Linkb)) == 0)
  669. edev->mbps = 100;
  670. return 0;
  671. }
  672. #ifndef FS
  673. void
  674. ether8139link(void)
  675. {
  676. addethercard("rtl8139", rtl8139pnp);
  677. }
  678. #endif