ether82557.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*
  2. * Intel 82557 Fast Ethernet PCI Bus LAN Controller
  3. * as found on the Intel EtherExpress PRO/100B. This chip is full
  4. * of smarts, unfortunately they're not all in the right place.
  5. * To do:
  6. * the PCI scanning code could be made common to other adapters;
  7. * auto-negotiation, full-duplex;
  8. * optionally use memory-mapped registers;
  9. * detach for PCI reset problems (also towards loadable drivers).
  10. */
  11. #include "u.h"
  12. #include "../port/lib.h"
  13. #include "mem.h"
  14. #include "dat.h"
  15. #include "fns.h"
  16. #include "io.h"
  17. #include "../port/error.h"
  18. #include "../port/netif.h"
  19. #include "etherif.h"
  20. enum {
  21. /*
  22. * these were both 64. increased them to try to improve lookout's
  23. * reliability as a pxe booter.
  24. */
  25. Nrfd = 128, /* receive frame area */
  26. Ncb = 128, /* maximum control blocks queued */
  27. NullPointer = 0xFFFFFFFF, /* 82557 NULL pointer */
  28. };
  29. enum { /* CSR */
  30. Status = 0x00, /* byte or word (word includes Ack) */
  31. Ack = 0x01, /* byte */
  32. CommandR = 0x02, /* byte or word (word includes Interrupt) */
  33. Interrupt = 0x03, /* byte */
  34. General = 0x04, /* dword */
  35. Port = 0x08, /* dword */
  36. Fcr = 0x0C, /* Flash control register */
  37. Ecr = 0x0E, /* EEPROM control register */
  38. Mcr = 0x10, /* MDI control register */
  39. Gstatus = 0x1D, /* General status register */
  40. };
  41. enum { /* Status */
  42. RUidle = 0x0000,
  43. RUsuspended = 0x0004,
  44. RUnoresources = 0x0008,
  45. RUready = 0x0010,
  46. RUrbd = 0x0020, /* bit */
  47. RUstatus = 0x003F, /* mask */
  48. CUidle = 0x0000,
  49. CUsuspended = 0x0040,
  50. CUactive = 0x0080,
  51. CUstatus = 0x00C0, /* mask */
  52. StatSWI = 0x0400, /* SoftWare generated Interrupt */
  53. StatMDI = 0x0800, /* MDI r/w done */
  54. StatRNR = 0x1000, /* Receive unit Not Ready */
  55. StatCNA = 0x2000, /* Command unit Not Active (Active->Idle) */
  56. StatFR = 0x4000, /* Finished Receiving */
  57. StatCX = 0x8000, /* Command eXecuted */
  58. StatTNO = 0x8000, /* Transmit NOT OK */
  59. };
  60. enum { /* Command (byte) */
  61. CUnop = 0x00,
  62. CUstart = 0x10,
  63. CUresume = 0x20,
  64. LoadDCA = 0x40, /* Load Dump Counters Address */
  65. DumpSC = 0x50, /* Dump Statistical Counters */
  66. LoadCUB = 0x60, /* Load CU Base */
  67. ResetSA = 0x70, /* Dump and Reset Statistical Counters */
  68. RUstart = 0x01,
  69. RUresume = 0x02,
  70. RUabort = 0x04,
  71. LoadHDS = 0x05, /* Load Header Data Size */
  72. LoadRUB = 0x06, /* Load RU Base */
  73. RBDresume = 0x07, /* Resume frame reception */
  74. };
  75. enum { /* Interrupt (byte) */
  76. InterruptM = 0x01, /* interrupt Mask */
  77. InterruptSI = 0x02, /* Software generated Interrupt */
  78. };
  79. enum { /* Ecr */
  80. EEsk = 0x01, /* serial clock */
  81. EEcs = 0x02, /* chip select */
  82. EEdi = 0x04, /* serial data in */
  83. EEdo = 0x08, /* serial data out */
  84. EEstart = 0x04, /* start bit */
  85. EEread = 0x02, /* read opcode */
  86. };
  87. enum { /* Mcr */
  88. MDIread = 0x08000000, /* read opcode */
  89. MDIwrite = 0x04000000, /* write opcode */
  90. MDIready = 0x10000000, /* ready bit */
  91. MDIie = 0x20000000, /* interrupt enable */
  92. };
  93. typedef struct Rfd {
  94. int field;
  95. ulong link;
  96. ulong rbd;
  97. ushort count;
  98. ushort size;
  99. uchar data[1700];
  100. } Rfd;
  101. enum { /* field */
  102. RfdCollision = 0x00000001,
  103. RfdIA = 0x00000002, /* IA match */
  104. RfdRxerr = 0x00000010, /* PHY character error */
  105. RfdType = 0x00000020, /* Type frame */
  106. RfdRunt = 0x00000080,
  107. RfdOverrun = 0x00000100,
  108. RfdBuffer = 0x00000200,
  109. RfdAlignment = 0x00000400,
  110. RfdCRC = 0x00000800,
  111. RfdOK = 0x00002000, /* frame received OK */
  112. RfdC = 0x00008000, /* reception Complete */
  113. RfdSF = 0x00080000, /* Simplified or Flexible (1) Rfd */
  114. RfdH = 0x00100000, /* Header RFD */
  115. RfdI = 0x20000000, /* Interrupt after completion */
  116. RfdS = 0x40000000, /* Suspend after completion */
  117. RfdEL = 0x80000000, /* End of List */
  118. };
  119. enum { /* count */
  120. RfdF = 0x4000,
  121. RfdEOF = 0x8000,
  122. };
  123. typedef struct Cb Cb;
  124. typedef struct Cb {
  125. ushort status;
  126. ushort command;
  127. ulong link;
  128. union {
  129. uchar data[24]; /* CbIAS + CbConfigure */
  130. struct {
  131. ulong tbd;
  132. ushort count;
  133. uchar threshold;
  134. uchar number;
  135. ulong tba;
  136. ushort tbasz;
  137. ushort pad;
  138. };
  139. };
  140. Block* bp;
  141. Cb* next;
  142. } Cb;
  143. enum { /* action command */
  144. CbU = 0x1000, /* transmit underrun */
  145. CbOK = 0x2000, /* DMA completed OK */
  146. CbC = 0x8000, /* execution Complete */
  147. CbNOP = 0x0000,
  148. CbIAS = 0x0001, /* Individual Address Setup */
  149. CbConfigure = 0x0002,
  150. CbMAS = 0x0003, /* Multicast Address Setup */
  151. CbTransmit = 0x0004,
  152. CbDump = 0x0006,
  153. CbDiagnose = 0x0007,
  154. CbCommand = 0x0007, /* mask */
  155. CbSF = 0x0008, /* Flexible-mode CbTransmit */
  156. CbI = 0x2000, /* Interrupt after completion */
  157. CbS = 0x4000, /* Suspend after completion */
  158. CbEL = 0x8000, /* End of List */
  159. };
  160. enum { /* CbTransmit count */
  161. CbEOF = 0x8000,
  162. };
  163. typedef struct Ctlr Ctlr;
  164. typedef struct Ctlr {
  165. Lock slock; /* attach */
  166. int state;
  167. int port;
  168. Pcidev* pcidev;
  169. Ctlr* next;
  170. int active;
  171. int eepromsz; /* address size in bits */
  172. ushort* eeprom;
  173. Lock miilock;
  174. int tick;
  175. Lock rlock; /* registers */
  176. int command; /* last command issued */
  177. Block* rfdhead; /* receive side */
  178. Block* rfdtail;
  179. int nrfd;
  180. Lock cblock; /* transmit side */
  181. int action;
  182. int nop;
  183. uchar configdata[24];
  184. int threshold;
  185. int ncb;
  186. Cb* cbr;
  187. Cb* cbhead;
  188. Cb* cbtail;
  189. int cbq;
  190. int cbqmax;
  191. int cbqmaxhw;
  192. Lock dlock; /* dump statistical counters */
  193. ulong dump[17];
  194. } Ctlr;
  195. static Ctlr* ctlrhead;
  196. static Ctlr* ctlrtail;
  197. static uchar configdata[24] = {
  198. 0x16, /* byte count */
  199. 0x08, /* Rx/Tx FIFO limit */
  200. 0x00, /* adaptive IFS */
  201. 0x00,
  202. 0x00, /* Rx DMA maximum byte count */
  203. // 0x80, /* Tx DMA maximum byte count */
  204. 0x00, /* Tx DMA maximum byte count */
  205. 0x32, /* !late SCB, CNA interrupts */
  206. 0x03, /* discard short Rx frames */
  207. 0x00, /* 503/MII */
  208. 0x00,
  209. 0x2E, /* normal operation, NSAI */
  210. 0x00, /* linear priority */
  211. 0x60, /* inter-frame spacing */
  212. 0x00,
  213. 0xF2,
  214. 0xC8, /* 503, promiscuous mode off */
  215. 0x00,
  216. 0x40,
  217. 0xF3, /* transmit padding enable */
  218. 0x80, /* full duplex pin enable */
  219. 0x3F, /* no Multi IA */
  220. 0x05, /* no Multi Cast ALL */
  221. };
  222. #define csr8r(c, r) (inb((c)->port+(r)))
  223. #define csr16r(c, r) (ins((c)->port+(r)))
  224. #define csr32r(c, r) (inl((c)->port+(r)))
  225. #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
  226. #define csr16w(c, r, w) (outs((c)->port+(r), (ushort)(w)))
  227. #define csr32w(c, r, l) (outl((c)->port+(r), (ulong)(l)))
  228. static void
  229. command(Ctlr* ctlr, int c, int v)
  230. {
  231. int timeo;
  232. ilock(&ctlr->rlock);
  233. /*
  234. * Only back-to-back CUresume can be done
  235. * without waiting for any previous command to complete.
  236. * This should be the common case.
  237. * Unfortunately there's a chip errata where back-to-back
  238. * CUresumes can be lost, the fix is to always wait.
  239. if(c == CUresume && ctlr->command == CUresume){
  240. csr8w(ctlr, CommandR, c);
  241. iunlock(&ctlr->rlock);
  242. return;
  243. }
  244. */
  245. for(timeo = 0; timeo < 100; timeo++){
  246. if(!csr8r(ctlr, CommandR))
  247. break;
  248. microdelay(1);
  249. }
  250. if(timeo >= 100){
  251. ctlr->command = -1;
  252. iunlock(&ctlr->rlock);
  253. iprint("i82557: command %#ux %#ux timeout\n", c, v);
  254. return;
  255. }
  256. switch(c){
  257. case CUstart:
  258. case LoadDCA:
  259. case LoadCUB:
  260. case RUstart:
  261. case LoadHDS:
  262. case LoadRUB:
  263. csr32w(ctlr, General, v);
  264. break;
  265. /*
  266. case CUnop:
  267. case CUresume:
  268. case DumpSC:
  269. case ResetSA:
  270. case RUresume:
  271. case RUabort:
  272. */
  273. default:
  274. break;
  275. }
  276. csr8w(ctlr, CommandR, c);
  277. ctlr->command = c;
  278. iunlock(&ctlr->rlock);
  279. }
  280. static Block*
  281. rfdalloc(ulong link)
  282. {
  283. Block *bp;
  284. Rfd *rfd;
  285. if(bp = iallocb(sizeof(Rfd))){
  286. rfd = (Rfd*)bp->rp;
  287. rfd->field = 0;
  288. rfd->link = link;
  289. rfd->rbd = NullPointer;
  290. rfd->count = 0;
  291. rfd->size = sizeof(Etherpkt);
  292. }
  293. return bp;
  294. }
  295. static void
  296. ethwatchdog(void* arg)
  297. {
  298. Ether *ether;
  299. Ctlr *ctlr;
  300. static void txstart(Ether*);
  301. ether = arg;
  302. for(;;){
  303. tsleep(&up->sleep, return0, 0, 4000);
  304. /*
  305. * Hmmm. This doesn't seem right. Currently
  306. * the device can't be disabled but it may be in
  307. * the future.
  308. */
  309. ctlr = ether->ctlr;
  310. if(ctlr == nil || ctlr->state == 0){
  311. print("%s: exiting\n", up->text);
  312. pexit("disabled", 0);
  313. }
  314. ilock(&ctlr->cblock);
  315. if(ctlr->tick++){
  316. ctlr->action = CbMAS;
  317. txstart(ether);
  318. }
  319. iunlock(&ctlr->cblock);
  320. }
  321. }
  322. static void
  323. attach(Ether* ether)
  324. {
  325. Ctlr *ctlr;
  326. char name[KNAMELEN];
  327. ctlr = ether->ctlr;
  328. lock(&ctlr->slock);
  329. if(ctlr->state == 0){
  330. ilock(&ctlr->rlock);
  331. csr8w(ctlr, Interrupt, 0);
  332. iunlock(&ctlr->rlock);
  333. command(ctlr, RUstart, PADDR(ctlr->rfdhead->rp));
  334. ctlr->state = 1;
  335. /*
  336. * Start the watchdog timer for the receive lockup errata
  337. * unless the EEPROM compatibility word indicates it may be
  338. * omitted.
  339. */
  340. if((ctlr->eeprom[0x03] & 0x0003) != 0x0003){
  341. snprint(name, KNAMELEN, "#l%dwatchdog", ether->ctlrno);
  342. kproc(name, ethwatchdog, ether);
  343. }
  344. }
  345. unlock(&ctlr->slock);
  346. }
  347. static long
  348. ifstat(Ether* ether, void* a, long n, ulong offset)
  349. {
  350. char *p;
  351. int i, len, phyaddr;
  352. Ctlr *ctlr;
  353. ulong dump[17];
  354. ctlr = ether->ctlr;
  355. lock(&ctlr->dlock);
  356. /*
  357. * Start the command then
  358. * wait for completion status,
  359. * should be 0xA005.
  360. */
  361. ctlr->dump[16] = 0;
  362. command(ctlr, DumpSC, 0);
  363. while(ctlr->dump[16] == 0)
  364. ;
  365. ether->oerrs = ctlr->dump[1]+ctlr->dump[2]+ctlr->dump[3];
  366. ether->crcs = ctlr->dump[10];
  367. ether->frames = ctlr->dump[11];
  368. ether->buffs = ctlr->dump[12]+ctlr->dump[15];
  369. ether->overflows = ctlr->dump[13];
  370. if(n == 0){
  371. unlock(&ctlr->dlock);
  372. return 0;
  373. }
  374. memmove(dump, ctlr->dump, sizeof(dump));
  375. unlock(&ctlr->dlock);
  376. p = malloc(READSTR);
  377. if(p == nil)
  378. error(Enomem);
  379. len = snprint(p, READSTR, "transmit good frames: %lud\n", dump[0]);
  380. len += snprint(p+len, READSTR-len, "transmit maximum collisions errors: %lud\n", dump[1]);
  381. len += snprint(p+len, READSTR-len, "transmit late collisions errors: %lud\n", dump[2]);
  382. len += snprint(p+len, READSTR-len, "transmit underrun errors: %lud\n", dump[3]);
  383. len += snprint(p+len, READSTR-len, "transmit lost carrier sense: %lud\n", dump[4]);
  384. len += snprint(p+len, READSTR-len, "transmit deferred: %lud\n", dump[5]);
  385. len += snprint(p+len, READSTR-len, "transmit single collisions: %lud\n", dump[6]);
  386. len += snprint(p+len, READSTR-len, "transmit multiple collisions: %lud\n", dump[7]);
  387. len += snprint(p+len, READSTR-len, "transmit total collisions: %lud\n", dump[8]);
  388. len += snprint(p+len, READSTR-len, "receive good frames: %lud\n", dump[9]);
  389. len += snprint(p+len, READSTR-len, "receive CRC errors: %lud\n", dump[10]);
  390. len += snprint(p+len, READSTR-len, "receive alignment errors: %lud\n", dump[11]);
  391. len += snprint(p+len, READSTR-len, "receive resource errors: %lud\n", dump[12]);
  392. len += snprint(p+len, READSTR-len, "receive overrun errors: %lud\n", dump[13]);
  393. len += snprint(p+len, READSTR-len, "receive collision detect errors: %lud\n", dump[14]);
  394. len += snprint(p+len, READSTR-len, "receive short frame errors: %lud\n", dump[15]);
  395. len += snprint(p+len, READSTR-len, "nop: %d\n", ctlr->nop);
  396. if(ctlr->cbqmax > ctlr->cbqmaxhw)
  397. ctlr->cbqmaxhw = ctlr->cbqmax;
  398. len += snprint(p+len, READSTR-len, "cbqmax: %d\n", ctlr->cbqmax);
  399. ctlr->cbqmax = 0;
  400. len += snprint(p+len, READSTR-len, "threshold: %d\n", ctlr->threshold);
  401. len += snprint(p+len, READSTR-len, "eeprom:");
  402. for(i = 0; i < (1<<ctlr->eepromsz); i++){
  403. if(i && ((i & 0x07) == 0))
  404. len += snprint(p+len, READSTR-len, "\n ");
  405. len += snprint(p+len, READSTR-len, " %4.4ux", ctlr->eeprom[i]);
  406. }
  407. if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000)){
  408. phyaddr = ctlr->eeprom[6] & 0x00FF;
  409. len += snprint(p+len, READSTR-len, "\nphy %2d:", phyaddr);
  410. for(i = 0; i < 6; i++){
  411. static int miir(Ctlr*, int, int);
  412. len += snprint(p+len, READSTR-len, " %4.4ux",
  413. miir(ctlr, phyaddr, i));
  414. }
  415. }
  416. snprint(p+len, READSTR-len, "\n");
  417. n = readstr(offset, a, n, p);
  418. free(p);
  419. return n;
  420. }
  421. static void
  422. txstart(Ether* ether)
  423. {
  424. Ctlr *ctlr;
  425. Block *bp;
  426. Cb *cb;
  427. ctlr = ether->ctlr;
  428. while(ctlr->cbq < (ctlr->ncb-1)){
  429. cb = ctlr->cbhead->next;
  430. if(ctlr->action == 0){
  431. bp = qget(ether->oq);
  432. if(bp == nil)
  433. break;
  434. cb->command = CbS|CbSF|CbTransmit;
  435. cb->tbd = PADDR(&cb->tba);
  436. cb->count = 0;
  437. cb->threshold = ctlr->threshold;
  438. cb->number = 1;
  439. cb->tba = PADDR(bp->rp);
  440. cb->bp = bp;
  441. cb->tbasz = BLEN(bp);
  442. }
  443. else if(ctlr->action == CbConfigure){
  444. cb->command = CbS|CbConfigure;
  445. memmove(cb->data, ctlr->configdata, sizeof(ctlr->configdata));
  446. ctlr->action = 0;
  447. }
  448. else if(ctlr->action == CbIAS){
  449. cb->command = CbS|CbIAS;
  450. memmove(cb->data, ether->ea, Eaddrlen);
  451. ctlr->action = 0;
  452. }
  453. else if(ctlr->action == CbMAS){
  454. cb->command = CbS|CbMAS;
  455. memset(cb->data, 0, sizeof(cb->data));
  456. ctlr->action = 0;
  457. }
  458. else{
  459. print("#l%d: action %#ux\n", ether->ctlrno, ctlr->action);
  460. ctlr->action = 0;
  461. break;
  462. }
  463. cb->status = 0;
  464. coherence();
  465. ctlr->cbhead->command &= ~CbS;
  466. ctlr->cbhead = cb;
  467. ctlr->cbq++;
  468. }
  469. /*
  470. * Workaround for some broken HUB chips
  471. * when connected at 10Mb/s half-duplex.
  472. */
  473. if(ctlr->nop){
  474. command(ctlr, CUnop, 0);
  475. microdelay(1);
  476. }
  477. command(ctlr, CUresume, 0);
  478. if(ctlr->cbq > ctlr->cbqmax)
  479. ctlr->cbqmax = ctlr->cbq;
  480. }
  481. static void
  482. configure(Ether* ether, int promiscuous)
  483. {
  484. Ctlr *ctlr;
  485. ctlr = ether->ctlr;
  486. ilock(&ctlr->cblock);
  487. if(promiscuous){
  488. ctlr->configdata[6] |= 0x80; /* Save Bad Frames */
  489. //ctlr->configdata[6] &= ~0x40; /* !Discard Overrun Rx Frames */
  490. ctlr->configdata[7] &= ~0x01; /* !Discard Short Rx Frames */
  491. ctlr->configdata[15] |= 0x01; /* Promiscuous mode */
  492. ctlr->configdata[18] &= ~0x01; /* (!Padding enable?), !stripping enable */
  493. ctlr->configdata[21] |= 0x08; /* Multi Cast ALL */
  494. }
  495. else{
  496. ctlr->configdata[6] &= ~0x80;
  497. //ctlr->configdata[6] |= 0x40;
  498. ctlr->configdata[7] |= 0x01;
  499. ctlr->configdata[15] &= ~0x01;
  500. ctlr->configdata[18] |= 0x01; /* 0x03? */
  501. ctlr->configdata[21] &= ~0x08;
  502. }
  503. ctlr->action = CbConfigure;
  504. txstart(ether);
  505. iunlock(&ctlr->cblock);
  506. }
  507. static void
  508. promiscuous(void* arg, int on)
  509. {
  510. configure(arg, on);
  511. }
  512. static void
  513. multicast(void* ether, uchar *addr, int add)
  514. {
  515. USED(addr);
  516. /*
  517. * TODO: if (add) add addr to list of mcast addrs in controller
  518. * else remove addr from list of mcast addrs in controller
  519. * enable multicast input (see CbMAS) instead of promiscuous mode.
  520. */
  521. if (add)
  522. configure(ether, 1);
  523. }
  524. static void
  525. transmit(Ether* ether)
  526. {
  527. Ctlr *ctlr;
  528. ctlr = ether->ctlr;
  529. ilock(&ctlr->cblock);
  530. txstart(ether);
  531. iunlock(&ctlr->cblock);
  532. }
  533. static void
  534. receive(Ether* ether)
  535. {
  536. Rfd *rfd;
  537. Ctlr *ctlr;
  538. int count;
  539. Block *bp, *pbp, *xbp;
  540. ctlr = ether->ctlr;
  541. bp = ctlr->rfdhead;
  542. for(rfd = (Rfd*)bp->rp; rfd->field & RfdC; rfd = (Rfd*)bp->rp){
  543. /*
  544. * If it's an OK receive frame
  545. * 1) save the count
  546. * 2) if it's small, try to allocate a block and copy
  547. * the data, then adjust the necessary fields for reuse;
  548. * 3) if it's big, try to allocate a new Rfd and if
  549. * successful
  550. * adjust the received buffer pointers for the
  551. * actual data received;
  552. * initialise the replacement buffer to point to
  553. * the next in the ring;
  554. * initialise bp to point to the replacement;
  555. * 4) if there's a good packet, pass it on for disposal.
  556. */
  557. if(rfd->field & RfdOK){
  558. pbp = nil;
  559. count = rfd->count & 0x3FFF;
  560. if((count < ETHERMAXTU/4) && (pbp = iallocb(count))){
  561. memmove(pbp->rp, bp->rp+offsetof(Rfd, data[0]), count);
  562. pbp->wp = pbp->rp + count;
  563. rfd->count = 0;
  564. rfd->field = 0;
  565. }
  566. else if(xbp = rfdalloc(rfd->link)){
  567. bp->rp += offsetof(Rfd, data[0]);
  568. bp->wp = bp->rp + count;
  569. xbp->next = bp->next;
  570. bp->next = 0;
  571. pbp = bp;
  572. bp = xbp;
  573. }
  574. if(pbp != nil)
  575. etheriq(ether, pbp, 1);
  576. }
  577. else{
  578. rfd->count = 0;
  579. rfd->field = 0;
  580. }
  581. /*
  582. * The ring tail pointer follows the head with with one
  583. * unused buffer in between to defeat hardware prefetch;
  584. * once the tail pointer has been bumped on to the next
  585. * and the new tail has the Suspend bit set, it can be
  586. * removed from the old tail buffer.
  587. * As a replacement for the current head buffer may have
  588. * been allocated above, ensure that the new tail points
  589. * to it (next and link).
  590. */
  591. rfd = (Rfd*)ctlr->rfdtail->rp;
  592. ctlr->rfdtail = ctlr->rfdtail->next;
  593. ctlr->rfdtail->next = bp;
  594. ((Rfd*)ctlr->rfdtail->rp)->link = PADDR(bp->rp);
  595. ((Rfd*)ctlr->rfdtail->rp)->field |= RfdS;
  596. coherence();
  597. rfd->field &= ~RfdS;
  598. /*
  599. * Finally done with the current (possibly replaced)
  600. * head, move on to the next and maintain the sentinel
  601. * between tail and head.
  602. */
  603. ctlr->rfdhead = bp->next;
  604. bp = ctlr->rfdhead;
  605. }
  606. }
  607. static void
  608. interrupt(Ureg*, void* arg)
  609. {
  610. Cb* cb;
  611. Ctlr *ctlr;
  612. Ether *ether;
  613. int status;
  614. ether = arg;
  615. ctlr = ether->ctlr;
  616. for(;;){
  617. ilock(&ctlr->rlock);
  618. status = csr16r(ctlr, Status);
  619. csr8w(ctlr, Ack, (status>>8) & 0xFF);
  620. iunlock(&ctlr->rlock);
  621. if(!(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI)))
  622. break;
  623. /*
  624. * If the watchdog timer for the receiver lockup errata is running,
  625. * let it know the receiver is active.
  626. */
  627. if(status & (StatFR|StatRNR)){
  628. ilock(&ctlr->cblock);
  629. ctlr->tick = 0;
  630. iunlock(&ctlr->cblock);
  631. }
  632. if(status & StatFR){
  633. receive(ether);
  634. status &= ~StatFR;
  635. }
  636. if(status & StatRNR){
  637. command(ctlr, RUresume, 0);
  638. status &= ~StatRNR;
  639. }
  640. if(status & StatCNA){
  641. ilock(&ctlr->cblock);
  642. cb = ctlr->cbtail;
  643. while(ctlr->cbq){
  644. if(!(cb->status & CbC))
  645. break;
  646. if(cb->bp){
  647. freeb(cb->bp);
  648. cb->bp = nil;
  649. }
  650. if((cb->status & CbU) && ctlr->threshold < 0xE0)
  651. ctlr->threshold++;
  652. ctlr->cbq--;
  653. cb = cb->next;
  654. }
  655. ctlr->cbtail = cb;
  656. txstart(ether);
  657. iunlock(&ctlr->cblock);
  658. status &= ~StatCNA;
  659. }
  660. if(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI))
  661. panic("#l%d: status %#ux\n", ether->ctlrno, status);
  662. }
  663. }
  664. static void
  665. ctlrinit(Ctlr* ctlr)
  666. {
  667. int i;
  668. Block *bp;
  669. Rfd *rfd;
  670. ulong link;
  671. /*
  672. * Create the Receive Frame Area (RFA) as a ring of allocated
  673. * buffers.
  674. * A sentinel buffer is maintained between the last buffer in
  675. * the ring (marked with RfdS) and the head buffer to defeat the
  676. * hardware prefetch of the next RFD and allow dynamic buffer
  677. * allocation.
  678. */
  679. link = NullPointer;
  680. for(i = 0; i < Nrfd; i++){
  681. bp = rfdalloc(link);
  682. if(ctlr->rfdhead == nil)
  683. ctlr->rfdtail = bp;
  684. bp->next = ctlr->rfdhead;
  685. ctlr->rfdhead = bp;
  686. link = PADDR(bp->rp);
  687. }
  688. ctlr->rfdtail->next = ctlr->rfdhead;
  689. rfd = (Rfd*)ctlr->rfdtail->rp;
  690. rfd->link = PADDR(ctlr->rfdhead->rp);
  691. rfd->field |= RfdS;
  692. ctlr->rfdhead = ctlr->rfdhead->next;
  693. /*
  694. * Create a ring of control blocks for the
  695. * transmit side.
  696. */
  697. ilock(&ctlr->cblock);
  698. ctlr->cbr = malloc(ctlr->ncb*sizeof(Cb));
  699. if(ctlr->cbr == nil) {
  700. iunlock(&ctlr->cblock);
  701. error(Enomem);
  702. }
  703. for(i = 0; i < ctlr->ncb; i++){
  704. ctlr->cbr[i].status = CbC|CbOK;
  705. ctlr->cbr[i].command = CbS|CbNOP;
  706. ctlr->cbr[i].link = PADDR(&ctlr->cbr[NEXT(i, ctlr->ncb)].status);
  707. ctlr->cbr[i].next = &ctlr->cbr[NEXT(i, ctlr->ncb)];
  708. }
  709. ctlr->cbhead = ctlr->cbr;
  710. ctlr->cbtail = ctlr->cbr;
  711. ctlr->cbq = 0;
  712. memmove(ctlr->configdata, configdata, sizeof(configdata));
  713. ctlr->threshold = 80;
  714. ctlr->tick = 0;
  715. iunlock(&ctlr->cblock);
  716. }
  717. static int
  718. miir(Ctlr* ctlr, int phyadd, int regadd)
  719. {
  720. int mcr, timo;
  721. lock(&ctlr->miilock);
  722. csr32w(ctlr, Mcr, MDIread|(phyadd<<21)|(regadd<<16));
  723. mcr = 0;
  724. for(timo = 64; timo; timo--){
  725. mcr = csr32r(ctlr, Mcr);
  726. if(mcr & MDIready)
  727. break;
  728. microdelay(1);
  729. }
  730. unlock(&ctlr->miilock);
  731. if(mcr & MDIready)
  732. return mcr & 0xFFFF;
  733. return -1;
  734. }
  735. static int
  736. miiw(Ctlr* ctlr, int phyadd, int regadd, int data)
  737. {
  738. int mcr, timo;
  739. lock(&ctlr->miilock);
  740. csr32w(ctlr, Mcr, MDIwrite|(phyadd<<21)|(regadd<<16)|(data & 0xFFFF));
  741. mcr = 0;
  742. for(timo = 64; timo; timo--){
  743. mcr = csr32r(ctlr, Mcr);
  744. if(mcr & MDIready)
  745. break;
  746. microdelay(1);
  747. }
  748. unlock(&ctlr->miilock);
  749. if(mcr & MDIready)
  750. return 0;
  751. return -1;
  752. }
  753. static int
  754. hy93c46r(Ctlr* ctlr, int r)
  755. {
  756. int data, i, op, size;
  757. /*
  758. * Hyundai HY93C46 or equivalent serial EEPROM.
  759. * This sequence for reading a 16-bit register 'r'
  760. * in the EEPROM is taken straight from Section
  761. * 3.3.4.2 of the Intel 82557 User's Guide.
  762. */
  763. reread:
  764. csr16w(ctlr, Ecr, EEcs);
  765. op = EEstart|EEread;
  766. for(i = 2; i >= 0; i--){
  767. data = (((op>>i) & 0x01)<<2)|EEcs;
  768. csr16w(ctlr, Ecr, data);
  769. csr16w(ctlr, Ecr, data|EEsk);
  770. microdelay(1);
  771. csr16w(ctlr, Ecr, data);
  772. microdelay(1);
  773. }
  774. /*
  775. * First time through must work out the EEPROM size.
  776. */
  777. if((size = ctlr->eepromsz) == 0)
  778. size = 8;
  779. for(size = size-1; size >= 0; size--){
  780. data = (((r>>size) & 0x01)<<2)|EEcs;
  781. csr16w(ctlr, Ecr, data);
  782. csr16w(ctlr, Ecr, data|EEsk);
  783. delay(1);
  784. csr16w(ctlr, Ecr, data);
  785. microdelay(1);
  786. if(!(csr16r(ctlr, Ecr) & EEdo))
  787. break;
  788. }
  789. data = 0;
  790. for(i = 15; i >= 0; i--){
  791. csr16w(ctlr, Ecr, EEcs|EEsk);
  792. microdelay(1);
  793. if(csr16r(ctlr, Ecr) & EEdo)
  794. data |= (1<<i);
  795. csr16w(ctlr, Ecr, EEcs);
  796. microdelay(1);
  797. }
  798. csr16w(ctlr, Ecr, 0);
  799. if(ctlr->eepromsz == 0){
  800. ctlr->eepromsz = 8-size;
  801. ctlr->eeprom = malloc((1<<ctlr->eepromsz)*sizeof(ushort));
  802. if(ctlr->eeprom == nil)
  803. error(Enomem);
  804. goto reread;
  805. }
  806. return data;
  807. }
  808. static void
  809. i82557pci(void)
  810. {
  811. Pcidev *p;
  812. Ctlr *ctlr;
  813. int i, nop, port;
  814. p = nil;
  815. nop = 0;
  816. while(p = pcimatch(p, 0x8086, 0)){
  817. switch(p->did){
  818. default:
  819. continue;
  820. case 0x1031: /* Intel 82562EM */
  821. case 0x103B: /* Intel 82562EM */
  822. case 0x103C: /* Intel 82562EM */
  823. case 0x1050: /* Intel 82562EZ */
  824. case 0x1039: /* Intel 82801BD PRO/100 VE */
  825. case 0x103A: /* Intel 82562 PRO/100 VE */
  826. case 0x103D: /* Intel 82562 PRO/100 VE */
  827. case 0x1064: /* Intel 82562 PRO/100 VE */
  828. case 0x2449: /* Intel 82562ET */
  829. case 0x27DC: /* Intel 82801G PRO/100 VE */
  830. nop = 1;
  831. /*FALLTHROUGH*/
  832. case 0x1209: /* Intel 82559ER */
  833. case 0x1229: /* Intel 8255[789] */
  834. case 0x1030: /* Intel 82559 InBusiness 10/100 */
  835. break;
  836. }
  837. if(pcigetpms(p) > 0){
  838. pcisetpms(p, 0);
  839. for(i = 0; i < 6; i++)
  840. pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
  841. pcicfgw8(p, PciINTL, p->intl);
  842. pcicfgw8(p, PciLTR, p->ltr);
  843. pcicfgw8(p, PciCLS, p->cls);
  844. pcicfgw16(p, PciPCR, p->pcr);
  845. }
  846. /*
  847. * bar[0] is the memory-mapped register address (4KB),
  848. * bar[1] is the I/O port register address (32 bytes) and
  849. * bar[2] is for the flash ROM (1MB).
  850. */
  851. port = p->mem[1].bar & ~0x01;
  852. if(ioalloc(port, p->mem[1].size, 0, "i82557") < 0){
  853. print("i82557: port %#ux in use\n", port);
  854. continue;
  855. }
  856. ctlr = malloc(sizeof(Ctlr));
  857. if(ctlr == nil)
  858. error(Enomem);
  859. ctlr->port = port;
  860. ctlr->pcidev = p;
  861. ctlr->nop = nop;
  862. if(ctlrhead != nil)
  863. ctlrtail->next = ctlr;
  864. else
  865. ctlrhead = ctlr;
  866. ctlrtail = ctlr;
  867. pcisetbme(p);
  868. }
  869. }
  870. static char* mediatable[9] = {
  871. "10BASE-T", /* TP */
  872. "10BASE-2", /* BNC */
  873. "10BASE-5", /* AUI */
  874. "100BASE-TX",
  875. "10BASE-TFD",
  876. "100BASE-TXFD",
  877. "100BASE-T4",
  878. "100BASE-FX",
  879. "100BASE-FXFD",
  880. };
  881. static int
  882. scanphy(Ctlr* ctlr)
  883. {
  884. int i, oui, x;
  885. for(i = 0; i < 32; i++){
  886. if((oui = miir(ctlr, i, 2)) == -1 || oui == 0 || oui == 0xFFFF)
  887. continue;
  888. oui <<= 6;
  889. x = miir(ctlr, i, 3);
  890. oui |= x>>10;
  891. //print("phy%d: oui %#ux reg1 %#ux\n", i, oui, miir(ctlr, i, 1));
  892. ctlr->eeprom[6] = i;
  893. if(oui == 0xAA00)
  894. ctlr->eeprom[6] |= 0x07<<8;
  895. else if(oui == 0x80017){
  896. if(x & 0x01)
  897. ctlr->eeprom[6] |= 0x0A<<8;
  898. else
  899. ctlr->eeprom[6] |= 0x04<<8;
  900. }
  901. return i;
  902. }
  903. return -1;
  904. }
  905. static void
  906. shutdown(Ether* ether)
  907. {
  908. Ctlr *ctlr = ether->ctlr;
  909. print("ether82557 shutting down\n");
  910. csr32w(ctlr, Port, 0);
  911. delay(1);
  912. csr8w(ctlr, Interrupt, InterruptM);
  913. }
  914. static int
  915. reset(Ether* ether)
  916. {
  917. int anar, anlpar, bmcr, bmsr, i, k, medium, phyaddr, x;
  918. unsigned short sum;
  919. uchar ea[Eaddrlen];
  920. Ctlr *ctlr;
  921. if(ctlrhead == nil)
  922. i82557pci();
  923. /*
  924. * Any adapter matches if no ether->port is supplied,
  925. * otherwise the ports must match.
  926. */
  927. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  928. if(ctlr->active)
  929. continue;
  930. if(ether->port == 0 || ether->port == ctlr->port){
  931. ctlr->active = 1;
  932. break;
  933. }
  934. }
  935. if(ctlr == nil)
  936. return -1;
  937. /*
  938. * Initialise the Ctlr structure.
  939. * Perform a software reset after which should ensure busmastering
  940. * is still enabled. The EtherExpress PRO/100B appears to leave
  941. * the PCI configuration alone (see the 'To do' list above) so punt
  942. * for now.
  943. * Load the RUB and CUB registers for linear addressing (0).
  944. */
  945. ether->ctlr = ctlr;
  946. ether->port = ctlr->port;
  947. ether->irq = ctlr->pcidev->intl;
  948. ether->tbdf = ctlr->pcidev->tbdf;
  949. ilock(&ctlr->rlock);
  950. csr32w(ctlr, Port, 0);
  951. delay(1);
  952. csr8w(ctlr, Interrupt, InterruptM);
  953. iunlock(&ctlr->rlock);
  954. command(ctlr, LoadRUB, 0);
  955. command(ctlr, LoadCUB, 0);
  956. command(ctlr, LoadDCA, PADDR(ctlr->dump));
  957. /*
  958. * Initialise the receive frame, transmit ring and configuration areas.
  959. */
  960. ctlr->ncb = Ncb;
  961. ctlrinit(ctlr);
  962. /*
  963. * Read the EEPROM.
  964. * Do a dummy read first to get the size
  965. * and allocate ctlr->eeprom.
  966. */
  967. hy93c46r(ctlr, 0);
  968. sum = 0;
  969. for(i = 0; i < (1<<ctlr->eepromsz); i++){
  970. x = hy93c46r(ctlr, i);
  971. ctlr->eeprom[i] = x;
  972. sum += x;
  973. }
  974. if(sum != 0xBABA)
  975. print("#l%d: EEPROM checksum - %#4.4ux\n", ether->ctlrno, sum);
  976. /*
  977. * Eeprom[6] indicates whether there is a PHY and whether
  978. * it's not 10Mb-only, in which case use the given PHY address
  979. * to set any PHY specific options and determine the speed.
  980. * Unfortunately, sometimes the EEPROM is blank except for
  981. * the ether address and checksum; in this case look at the
  982. * controller type and if it's am 82558 or 82559 it has an
  983. * embedded PHY so scan for that.
  984. * If no PHY, assume 82503 (serial) operation.
  985. */
  986. if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000))
  987. phyaddr = ctlr->eeprom[6] & 0x00FF;
  988. else
  989. switch(ctlr->pcidev->rid){
  990. case 0x01: /* 82557 A-step */
  991. case 0x02: /* 82557 B-step */
  992. case 0x03: /* 82557 C-step */
  993. default:
  994. phyaddr = -1;
  995. break;
  996. case 0x04: /* 82558 A-step */
  997. case 0x05: /* 82558 B-step */
  998. case 0x06: /* 82559 A-step */
  999. case 0x07: /* 82559 B-step */
  1000. case 0x08: /* 82559 C-step */
  1001. case 0x09: /* 82559ER A-step */
  1002. phyaddr = scanphy(ctlr);
  1003. break;
  1004. }
  1005. if(phyaddr >= 0){
  1006. /*
  1007. * Resolve the highest common ability of the two
  1008. * link partners. In descending order:
  1009. * 0x0100 100BASE-TX Full Duplex
  1010. * 0x0200 100BASE-T4
  1011. * 0x0080 100BASE-TX
  1012. * 0x0040 10BASE-T Full Duplex
  1013. * 0x0020 10BASE-T
  1014. */
  1015. anar = miir(ctlr, phyaddr, 0x04);
  1016. anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
  1017. anar &= anlpar;
  1018. bmcr = 0;
  1019. if(anar & 0x380)
  1020. bmcr = 0x2000;
  1021. if(anar & 0x0140)
  1022. bmcr |= 0x0100;
  1023. switch((ctlr->eeprom[6]>>8) & 0x001F){
  1024. case 0x04: /* DP83840 */
  1025. case 0x0A: /* DP83840A */
  1026. /*
  1027. * The DP83840[A] requires some tweaking for
  1028. * reliable operation.
  1029. * The manual says bit 10 should be unconditionally
  1030. * set although it supposedly only affects full-duplex
  1031. * operation (an & 0x0140).
  1032. */
  1033. x = miir(ctlr, phyaddr, 0x17) & ~0x0520;
  1034. x |= 0x0420;
  1035. for(i = 0; i < ether->nopt; i++){
  1036. if(cistrcmp(ether->opt[i], "congestioncontrol"))
  1037. continue;
  1038. x |= 0x0100;
  1039. break;
  1040. }
  1041. miiw(ctlr, phyaddr, 0x17, x);
  1042. /*
  1043. * If the link partner can't autonegotiate, determine
  1044. * the speed from elsewhere.
  1045. */
  1046. if(anlpar == 0){
  1047. miir(ctlr, phyaddr, 0x01);
  1048. bmsr = miir(ctlr, phyaddr, 0x01);
  1049. x = miir(ctlr, phyaddr, 0x19);
  1050. if((bmsr & 0x0004) && !(x & 0x0040))
  1051. bmcr = 0x2000;
  1052. }
  1053. break;
  1054. case 0x07: /* Intel 82555 */
  1055. /*
  1056. * Auto-negotiation may fail if the other end is
  1057. * a DP83840A and the cable is short.
  1058. */
  1059. miir(ctlr, phyaddr, 0x01);
  1060. bmsr = miir(ctlr, phyaddr, 0x01);
  1061. if((miir(ctlr, phyaddr, 0) & 0x1000) && !(bmsr & 0x0020)){
  1062. miiw(ctlr, phyaddr, 0x1A, 0x2010);
  1063. x = miir(ctlr, phyaddr, 0);
  1064. miiw(ctlr, phyaddr, 0, 0x0200|x);
  1065. for(i = 0; i < 3000; i++){
  1066. delay(1);
  1067. if(miir(ctlr, phyaddr, 0x01) & 0x0020)
  1068. break;
  1069. }
  1070. miiw(ctlr, phyaddr, 0x1A, 0x2000);
  1071. anar = miir(ctlr, phyaddr, 0x04);
  1072. anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
  1073. anar &= anlpar;
  1074. bmcr = 0;
  1075. if(anar & 0x380)
  1076. bmcr = 0x2000;
  1077. if(anar & 0x0140)
  1078. bmcr |= 0x0100;
  1079. }
  1080. break;
  1081. }
  1082. /*
  1083. * Force speed and duplex if no auto-negotiation.
  1084. */
  1085. if(anlpar == 0){
  1086. medium = -1;
  1087. for(i = 0; i < ether->nopt; i++){
  1088. for(k = 0; k < nelem(mediatable); k++){
  1089. if(cistrcmp(mediatable[k], ether->opt[i]))
  1090. continue;
  1091. medium = k;
  1092. break;
  1093. }
  1094. switch(medium){
  1095. default:
  1096. break;
  1097. case 0x00: /* 10BASE-T */
  1098. case 0x01: /* 10BASE-2 */
  1099. case 0x02: /* 10BASE-5 */
  1100. bmcr &= ~(0x2000|0x0100);
  1101. ctlr->configdata[19] &= ~0x40;
  1102. break;
  1103. case 0x03: /* 100BASE-TX */
  1104. case 0x06: /* 100BASE-T4 */
  1105. case 0x07: /* 100BASE-FX */
  1106. ctlr->configdata[19] &= ~0x40;
  1107. bmcr |= 0x2000;
  1108. break;
  1109. case 0x04: /* 10BASE-TFD */
  1110. bmcr = (bmcr & ~0x2000)|0x0100;
  1111. ctlr->configdata[19] |= 0x40;
  1112. break;
  1113. case 0x05: /* 100BASE-TXFD */
  1114. case 0x08: /* 100BASE-FXFD */
  1115. bmcr |= 0x2000|0x0100;
  1116. ctlr->configdata[19] |= 0x40;
  1117. break;
  1118. }
  1119. }
  1120. if(medium != -1)
  1121. miiw(ctlr, phyaddr, 0x00, bmcr);
  1122. }
  1123. if(bmcr & 0x2000)
  1124. ether->mbps = 100;
  1125. ctlr->configdata[8] = 1;
  1126. ctlr->configdata[15] &= ~0x80;
  1127. }
  1128. else{
  1129. ctlr->configdata[8] = 0;
  1130. ctlr->configdata[15] |= 0x80;
  1131. }
  1132. /*
  1133. * Workaround for some broken HUB chips when connected at 10Mb/s
  1134. * half-duplex.
  1135. * This is a band-aid, but as there's no dynamic auto-negotiation
  1136. * code at the moment, only deactivate the workaround code in txstart
  1137. * if the link is 100Mb/s.
  1138. */
  1139. if(ether->mbps != 10)
  1140. ctlr->nop = 0;
  1141. /*
  1142. * Load the chip configuration and start it off.
  1143. */
  1144. if(ether->oq == 0)
  1145. ether->oq = qopen(64*1024, Qmsg, 0, 0);
  1146. configure(ether, 0);
  1147. command(ctlr, CUstart, PADDR(&ctlr->cbr->status));
  1148. /*
  1149. * Check if the adapter's station address is to be overridden.
  1150. * If not, read it from the EEPROM and set in ether->ea prior to loading
  1151. * the station address with the Individual Address Setup command.
  1152. */
  1153. memset(ea, 0, Eaddrlen);
  1154. if(memcmp(ea, ether->ea, Eaddrlen) == 0){
  1155. for(i = 0; i < Eaddrlen/2; i++){
  1156. x = ctlr->eeprom[i];
  1157. ether->ea[2*i] = x;
  1158. ether->ea[2*i+1] = x>>8;
  1159. }
  1160. }
  1161. ilock(&ctlr->cblock);
  1162. ctlr->action = CbIAS;
  1163. txstart(ether);
  1164. iunlock(&ctlr->cblock);
  1165. /*
  1166. * Linkage to the generic ethernet driver.
  1167. */
  1168. ether->attach = attach;
  1169. ether->transmit = transmit;
  1170. ether->interrupt = interrupt;
  1171. ether->ifstat = ifstat;
  1172. ether->shutdown = shutdown;
  1173. ether->promiscuous = promiscuous;
  1174. ether->multicast = multicast;
  1175. ether->arg = ether;
  1176. return 0;
  1177. }
  1178. void
  1179. ether82557link(void)
  1180. {
  1181. addethercard("i82557", reset);
  1182. }