ether82557.c 30 KB

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