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.
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).
12 #include "../port/lib.h"
17 #include "../port/error.h"
18 #include "../port/netif.h"
23 Nrfd = 64, /* receive frame area */
24 Ncb = 64, /* maximum control blocks queued */
26 NullPointer = 0xFFFFFFFF, /* 82557 NULL pointer */
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 */
45 RUnoresources = 0x0008,
47 RUrbd = 0x0020, /* bit */
48 RUstatus = 0x003F, /* mask */
53 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 */
64 enum { /* Command (byte) */
68 LoadDCA = 0x40, /* Load Dump Counters Address */
69 DumpSC = 0x50, /* Dump Statistical Counters */
70 LoadCUB = 0x60, /* Load CU Base */
71 ResetSA = 0x70, /* Dump and Reset Statistical Counters */
76 LoadHDS = 0x05, /* Load Header Data Size */
77 LoadRUB = 0x06, /* Load RU Base */
78 RBDresume = 0x07, /* Resume frame reception */
81 enum { /* Interrupt (byte) */
82 InterruptM = 0x01, /* interrupt Mask */
83 InterruptSI = 0x02, /* Software generated Interrupt */
87 EEsk = 0x01, /* serial clock */
88 EEcs = 0x02, /* chip select */
89 EEdi = 0x04, /* serial data in */
90 EEdo = 0x08, /* serial data out */
92 EEstart = 0x04, /* start bit */
93 EEread = 0x02, /* read opcode */
97 MDIread = 0x08000000, /* read opcode */
98 MDIwrite = 0x04000000, /* write opcode */
99 MDIready = 0x10000000, /* ready bit */
100 MDIie = 0x20000000, /* interrupt enable */
114 RfdCollision = 0x00000001,
115 RfdIA = 0x00000002, /* IA match */
116 RfdRxerr = 0x00000010, /* PHY character error */
117 RfdType = 0x00000020, /* Type frame */
118 RfdRunt = 0x00000080,
119 RfdOverrun = 0x00000100,
120 RfdBuffer = 0x00000200,
121 RfdAlignment = 0x00000400,
124 RfdOK = 0x00002000, /* frame received OK */
125 RfdC = 0x00008000, /* reception Complete */
126 RfdSF = 0x00080000, /* Simplified or Flexible (1) Rfd */
127 RfdH = 0x00100000, /* Header RFD */
129 RfdI = 0x20000000, /* Interrupt after completion */
130 RfdS = 0x40000000, /* Suspend after completion */
131 RfdEL = 0x80000000, /* End of List */
139 typedef struct Cb Cb;
145 uchar data[24]; /* CbIAS + CbConfigure */
162 enum { /* action command */
163 CbU = 0x1000, /* transmit underrun */
164 CbOK = 0x2000, /* DMA completed OK */
165 CbC = 0x8000, /* execution Complete */
168 CbIAS = 0x0001, /* Individual Address Setup */
169 CbConfigure = 0x0002,
170 CbMAS = 0x0003, /* Multicast Address Setup */
174 CbCommand = 0x0007, /* mask */
176 CbSF = 0x0008, /* Flexible-mode CbTransmit */
178 CbI = 0x2000, /* Interrupt after completion */
179 CbS = 0x4000, /* Suspend after completion */
180 CbEL = 0x8000, /* End of List */
183 enum { /* CbTransmit count */
187 typedef struct Ctlr Ctlr;
188 typedef struct Ctlr {
189 Lock slock; /* attach */
197 int eepromsz; /* address size in bits */
204 Lock rlock; /* registers */
205 int command; /* last command issued */
207 Block* rfdhead; /* receive side */
211 Lock cblock; /* transmit side */
214 uchar configdata[24];
224 Lock dlock; /* dump statistical counters */
228 static Ctlr* ctlrhead;
229 static Ctlr* ctlrtail;
231 static uchar configdata[24] = {
232 0x16, /* byte count */
233 0x08, /* Rx/Tx FIFO limit */
234 0x00, /* adaptive IFS */
236 0x00, /* Rx DMA maximum byte count */
237 // 0x80, /* Tx DMA maximum byte count */
238 0x00, /* Tx DMA maximum byte count */
239 0x32, /* !late SCB, CNA interrupts */
240 0x03, /* discard short Rx frames */
244 0x2E, /* normal operation, NSAI */
245 0x00, /* linear priority */
246 0x60, /* inter-frame spacing */
249 0xC8, /* 503, promiscuous mode off */
252 0xF3, /* transmit padding enable */
253 0x80, /* full duplex pin enable */
254 0x3F, /* no Multi IA */
255 0x05, /* no Multi Cast ALL */
258 #define csr8r(c, r) (inb((c)->port+(r)))
259 #define csr16r(c, r) (ins((c)->port+(r)))
260 #define csr32r(c, r) (inl((c)->port+(r)))
261 #define csr8w(c, r, b) (outb((c)->port+(r), (int)(b)))
262 #define csr16w(c, r, w) (outs((c)->port+(r), (ushort)(w)))
263 #define csr32w(c, r, l) (outl((c)->port+(r), (ulong)(l)))
266 command(Ctlr* ctlr, int c, int v)
273 * Only back-to-back CUresume can be done
274 * without waiting for any previous command to complete.
275 * This should be the common case.
276 * Unfortunately there's a chip errata where back-to-back
277 * CUresumes can be lost, the fix is to always wait.
278 if(c == CUresume && ctlr->command == CUresume){
279 csr8w(ctlr, CommandR, c);
280 iunlock(&ctlr->rlock);
285 for(timeo = 0; timeo < 100; timeo++){
286 if(!csr8r(ctlr, CommandR))
292 iunlock(&ctlr->rlock);
293 iprint("i82557: command %#ux %#ux timeout\n", c, v);
305 csr32w(ctlr, General, v);
319 csr8w(ctlr, CommandR, c);
322 iunlock(&ctlr->rlock);
331 if(bp = iallocb(sizeof(Rfd))){
335 rfd->rbd = NullPointer;
337 rfd->size = sizeof(Etherpkt);
348 static void txstart(Ether*);
352 tsleep(&up->sleep, return0, 0, 4000);
355 * Hmmm. This doesn't seem right. Currently
356 * the device can't be disabled but it may be in
360 if(ctlr == nil || ctlr->state == 0){
361 print("%s: exiting\n", up->text);
362 pexit("disabled", 0);
365 ilock(&ctlr->cblock);
367 ctlr->action = CbMAS;
370 iunlock(&ctlr->cblock);
382 if(ctlr->state == 0){
384 csr8w(ctlr, Interrupt, 0);
385 iunlock(&ctlr->rlock);
386 command(ctlr, RUstart, PADDR(ctlr->rfdhead->rp));
390 * Start the watchdog timer for the receive lockup errata
391 * unless the EEPROM compatibility word indicates it may be
394 if((ctlr->eeprom[0x03] & 0x0003) != 0x0003){
395 snprint(name, KNAMELEN, "#l%dwatchdog", ether->ctlrno);
396 kproc(name, watchdog, ether);
399 unlock(&ctlr->slock);
403 ifstat(Ether* ether, void* a, long n, ulong offset)
413 unlock(&ctlr->dlock);
418 * Start the command then
419 * wait for completion status,
423 command(ctlr, DumpSC, 0);
424 for(i = 0; i < 1000 && ctlr->dump[16] == 0; i++)
427 error("command timeout");
429 memmove(dump, ctlr->dump, sizeof(dump));
431 ether->oerrs = dump[1]+dump[2]+dump[3];
432 ether->crcs = dump[10];
433 ether->frames = dump[11];
434 ether->buffs = dump[12]+dump[15];
435 ether->overflows = dump[13];
438 unlock(&ctlr->dlock);
443 p = smalloc(READSTR);
444 len = snprint(p, READSTR, "transmit good frames: %lud\n", dump[0]);
445 len += snprint(p+len, READSTR-len, "transmit maximum collisions errors: %lud\n", dump[1]);
446 len += snprint(p+len, READSTR-len, "transmit late collisions errors: %lud\n", dump[2]);
447 len += snprint(p+len, READSTR-len, "transmit underrun errors: %lud\n", dump[3]);
448 len += snprint(p+len, READSTR-len, "transmit lost carrier sense: %lud\n", dump[4]);
449 len += snprint(p+len, READSTR-len, "transmit deferred: %lud\n", dump[5]);
450 len += snprint(p+len, READSTR-len, "transmit single collisions: %lud\n", dump[6]);
451 len += snprint(p+len, READSTR-len, "transmit multiple collisions: %lud\n", dump[7]);
452 len += snprint(p+len, READSTR-len, "transmit total collisions: %lud\n", dump[8]);
453 len += snprint(p+len, READSTR-len, "receive good frames: %lud\n", dump[9]);
454 len += snprint(p+len, READSTR-len, "receive CRC errors: %lud\n", dump[10]);
455 len += snprint(p+len, READSTR-len, "receive alignment errors: %lud\n", dump[11]);
456 len += snprint(p+len, READSTR-len, "receive resource errors: %lud\n", dump[12]);
457 len += snprint(p+len, READSTR-len, "receive overrun errors: %lud\n", dump[13]);
458 len += snprint(p+len, READSTR-len, "receive collision detect errors: %lud\n", dump[14]);
459 len += snprint(p+len, READSTR-len, "receive short frame errors: %lud\n", dump[15]);
460 len += snprint(p+len, READSTR-len, "nop: %d\n", ctlr->nop);
461 if(ctlr->cbqmax > ctlr->cbqmaxhw)
462 ctlr->cbqmaxhw = ctlr->cbqmax;
463 len += snprint(p+len, READSTR-len, "cbqmax: %d\n", ctlr->cbqmax);
465 len += snprint(p+len, READSTR-len, "threshold: %d\n", ctlr->threshold);
467 len += snprint(p+len, READSTR-len, "eeprom:");
468 for(i = 0; i < (1<<ctlr->eepromsz); i++){
469 if(i && ((i & 0x07) == 0))
470 len += snprint(p+len, READSTR-len, "\n ");
471 len += snprint(p+len, READSTR-len, " %4.4ux", ctlr->eeprom[i]);
474 if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000)){
475 phyaddr = ctlr->eeprom[6] & 0x00FF;
476 len += snprint(p+len, READSTR-len, "\nphy %2d:", phyaddr);
477 for(i = 0; i < 6; i++){
478 static int miir(Ctlr*, int, int);
480 len += snprint(p+len, READSTR-len, " %4.4ux",
481 miir(ctlr, phyaddr, i));
485 snprint(p+len, READSTR-len, "\n");
486 n = readstr(offset, a, n, p);
493 txstart(Ether* ether)
500 while(ctlr->cbq < (ctlr->ncb-1)){
501 cb = ctlr->cbhead->next;
502 if(ctlr->action == 0){
503 bp = qget(ether->oq);
507 cb->command = CbS|CbSF|CbTransmit;
508 cb->tbd = PADDR(&cb->tba);
510 cb->threshold = ctlr->threshold;
512 cb->tba = PADDR(bp->rp);
514 cb->tbasz = BLEN(bp);
516 else if(ctlr->action == CbConfigure){
517 cb->command = CbS|CbConfigure;
518 memmove(cb->data, ctlr->configdata, sizeof(ctlr->configdata));
521 else if(ctlr->action == CbIAS){
522 cb->command = CbS|CbIAS;
523 memmove(cb->data, ether->ea, Eaddrlen);
526 else if(ctlr->action == CbMAS){
527 cb->command = CbS|CbMAS;
528 memset(cb->data, 0, sizeof(cb->data));
532 print("#l%d: action %#ux\n", ether->ctlrno, ctlr->action);
539 ctlr->cbhead->command &= ~CbS;
545 * Workaround for some broken HUB chips
546 * when connected at 10Mb/s half-duplex.
549 command(ctlr, CUnop, 0);
552 command(ctlr, CUresume, 0);
554 if(ctlr->cbq > ctlr->cbqmax)
555 ctlr->cbqmax = ctlr->cbq;
559 configure(Ether* ether, int promiscuous)
564 ilock(&ctlr->cblock);
566 ctlr->configdata[6] |= 0x80; /* Save Bad Frames */
567 //ctlr->configdata[6] &= ~0x40; /* !Discard Overrun Rx Frames */
568 ctlr->configdata[7] &= ~0x01; /* !Discard Short Rx Frames */
569 ctlr->configdata[15] |= 0x01; /* Promiscuous mode */
570 ctlr->configdata[18] &= ~0x01; /* (!Padding enable?), !stripping enable */
571 ctlr->configdata[21] |= 0x08; /* Multi Cast ALL */
574 ctlr->configdata[6] &= ~0x80;
575 //ctlr->configdata[6] |= 0x40;
576 ctlr->configdata[7] |= 0x01;
577 ctlr->configdata[15] &= ~0x01;
578 ctlr->configdata[18] |= 0x01; /* 0x03? */
579 ctlr->configdata[21] &= ~0x08;
581 ctlr->action = CbConfigure;
583 iunlock(&ctlr->cblock);
587 promiscuous(void* arg, int on)
593 multicast(void* ether, uchar *addr, int add)
597 * TODO: if (add) add addr to list of mcast addrs in controller
598 * else remove addr from list of mcast addrs in controller
599 * enable multicast input (see CbMAS) instead of promiscuous mode.
606 transmit(Ether* ether)
611 ilock(&ctlr->cblock);
613 iunlock(&ctlr->cblock);
617 receive(Ether* ether)
622 Block *bp, *pbp, *xbp;
626 for(rfd = (Rfd*)bp->rp; rfd->field & RfdC; rfd = (Rfd*)bp->rp){
628 * If it's an OK receive frame
630 * 2) if it's small, try to allocate a block and copy
631 * the data, then adjust the necessary fields for reuse;
632 * 3) if it's big, try to allocate a new Rfd and if
634 * adjust the received buffer pointers for the
635 * actual data received;
636 * initialise the replacement buffer to point to
637 * the next in the ring;
638 * initialise bp to point to the replacement;
639 * 4) if there's a good packet, pass it on for disposal.
641 if(rfd->field & RfdOK){
643 count = rfd->count & 0x3FFF;
644 if((count < ETHERMAXTU/4) && (pbp = iallocb(count))){
645 memmove(pbp->rp, bp->rp+offsetof(Rfd, data[0]), count);
646 pbp->wp = pbp->rp + count;
651 else if(xbp = rfdalloc(rfd->link)){
652 bp->rp += offsetof(Rfd, data[0]);
653 bp->wp = bp->rp + count;
655 xbp->next = bp->next;
662 etheriq(ether, pbp, 1);
670 * The ring tail pointer follows the head with with one
671 * unused buffer in between to defeat hardware prefetch;
672 * once the tail pointer has been bumped on to the next
673 * and the new tail has the Suspend bit set, it can be
674 * removed from the old tail buffer.
675 * As a replacement for the current head buffer may have
676 * been allocated above, ensure that the new tail points
677 * to it (next and link).
679 rfd = (Rfd*)ctlr->rfdtail->rp;
680 ctlr->rfdtail = ctlr->rfdtail->next;
681 ctlr->rfdtail->next = bp;
682 ((Rfd*)ctlr->rfdtail->rp)->link = PADDR(bp->rp);
683 ((Rfd*)ctlr->rfdtail->rp)->field |= RfdS;
688 * Finally done with the current (possibly replaced)
689 * head, move on to the next and maintain the sentinel
690 * between tail and head.
692 ctlr->rfdhead = bp->next;
698 interrupt(Ureg*, void* arg)
710 status = csr16r(ctlr, Status);
711 csr8w(ctlr, Ack, (status>>8) & 0xFF);
712 iunlock(&ctlr->rlock);
714 if(!(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI)))
718 * If the watchdog timer for the receiver lockup errata is running,
719 * let it know the receiver is active.
721 if(status & (StatFR|StatRNR)){
722 ilock(&ctlr->cblock);
724 iunlock(&ctlr->cblock);
732 if(status & StatRNR){
733 command(ctlr, RUresume, 0);
737 if(status & StatCNA){
738 ilock(&ctlr->cblock);
742 if(!(cb->status & CbC))
748 if((cb->status & CbU) && ctlr->threshold < 0xE0)
757 iunlock(&ctlr->cblock);
762 if(status & (StatCX|StatFR|StatCNA|StatRNR|StatMDI|StatSWI))
763 panic("#l%d: status %#ux", ether->ctlrno, status);
776 * Create the Receive Frame Area (RFA) as a ring of allocated
778 * A sentinel buffer is maintained between the last buffer in
779 * the ring (marked with RfdS) and the head buffer to defeat the
780 * hardware prefetch of the next RFD and allow dynamic buffer
784 for(i = 0; i < Nrfd; i++){
787 panic("i82557: can't allocate rfd buffer");
788 if(ctlr->rfdhead == nil)
790 bp->next = ctlr->rfdhead;
792 link = PADDR(bp->rp);
794 ctlr->rfdtail->next = ctlr->rfdhead;
795 rfd = (Rfd*)ctlr->rfdtail->rp;
796 rfd->link = PADDR(ctlr->rfdhead->rp);
798 ctlr->rfdhead = ctlr->rfdhead->next;
801 * Create a ring of control blocks for the
804 ilock(&ctlr->cblock);
805 ctlr->cbr = malloc(ctlr->ncb*sizeof(Cb));
807 panic("i82557: can't allocate cbr");
808 for(i = 0; i < ctlr->ncb; i++){
809 ctlr->cbr[i].status = CbC|CbOK;
810 ctlr->cbr[i].command = CbS|CbNOP;
811 ctlr->cbr[i].link = PADDR(&ctlr->cbr[NEXT(i, ctlr->ncb)].status);
812 ctlr->cbr[i].next = &ctlr->cbr[NEXT(i, ctlr->ncb)];
814 ctlr->cbhead = ctlr->cbr;
815 ctlr->cbtail = ctlr->cbr;
818 memmove(ctlr->configdata, configdata, sizeof(configdata));
819 ctlr->threshold = 80;
822 iunlock(&ctlr->cblock);
826 miir(Ctlr* ctlr, int phyadd, int regadd)
830 lock(&ctlr->miilock);
831 csr32w(ctlr, Mcr, MDIread|(phyadd<<21)|(regadd<<16));
833 for(timo = 64; timo; timo--){
834 mcr = csr32r(ctlr, Mcr);
839 unlock(&ctlr->miilock);
848 miiw(Ctlr* ctlr, int phyadd, int regadd, int data)
852 lock(&ctlr->miilock);
853 csr32w(ctlr, Mcr, MDIwrite|(phyadd<<21)|(regadd<<16)|(data & 0xFFFF));
855 for(timo = 64; timo; timo--){
856 mcr = csr32r(ctlr, Mcr);
861 unlock(&ctlr->miilock);
870 hy93c46r(Ctlr* ctlr, int r)
872 int data, i, op, size;
875 * Hyundai HY93C46 or equivalent serial EEPROM.
876 * This sequence for reading a 16-bit register 'r'
877 * in the EEPROM is taken straight from Section
878 * 3.3.4.2 of the Intel 82557 User's Guide.
881 csr16w(ctlr, Ecr, EEcs);
883 for(i = 2; i >= 0; i--){
884 data = (((op>>i) & 0x01)<<2)|EEcs;
885 csr16w(ctlr, Ecr, data);
886 csr16w(ctlr, Ecr, data|EEsk);
888 csr16w(ctlr, Ecr, data);
893 * First time through must work out the EEPROM size.
895 if((size = ctlr->eepromsz) == 0)
898 for(size = size-1; size >= 0; size--){
899 data = (((r>>size) & 0x01)<<2)|EEcs;
900 csr16w(ctlr, Ecr, data);
901 csr16w(ctlr, Ecr, data|EEsk);
903 csr16w(ctlr, Ecr, data);
905 if(!(csr16r(ctlr, Ecr) & EEdo))
910 for(i = 15; i >= 0; i--){
911 csr16w(ctlr, Ecr, EEcs|EEsk);
913 if(csr16r(ctlr, Ecr) & EEdo)
915 csr16w(ctlr, Ecr, EEcs);
919 csr16w(ctlr, Ecr, 0);
921 if(ctlr->eepromsz == 0){
922 ctlr->eepromsz = 8-size;
923 ctlr->eeprom = malloc((1<<ctlr->eepromsz)*sizeof(ushort));
924 if(ctlr->eeprom == nil)
925 panic("i82557: can't allocate eeprom");
941 while(p = pcimatch(p, 0x8086, 0)){
945 case 0x1031: /* Intel 82562EM */
946 case 0x103B: /* Intel 82562EM */
947 case 0x103C: /* Intel 82562EM */
948 case 0x1050: /* Intel 82562EZ */
949 case 0x1039: /* Intel 82801BD PRO/100 VE */
950 case 0x103A: /* Intel 82562 PRO/100 VE */
951 case 0x103D: /* Intel 82562 PRO/100 VE */
952 case 0x1064: /* Intel 82562 PRO/100 VE */
953 case 0x2449: /* Intel 82562ET */
954 case 0x27DC: /* Intel 82801G PRO/100 VE */
957 case 0x1209: /* Intel 82559ER */
958 case 0x1229: /* Intel 8255[789] */
959 case 0x1030: /* Intel 82559 InBusiness 10/100 */
963 if(pcigetpms(p) > 0){
966 for(i = 0; i < 6; i++)
967 pcicfgw32(p, PciBAR0+i*4, p->mem[i].bar);
968 pcicfgw8(p, PciINTL, p->intl);
969 pcicfgw8(p, PciLTR, p->ltr);
970 pcicfgw8(p, PciCLS, p->cls);
971 pcicfgw16(p, PciPCR, p->pcr);
975 * bar[0] is the memory-mapped register address (4KB),
976 * bar[1] is the I/O port register address (32 bytes) and
977 * bar[2] is for the flash ROM (1MB).
979 port = p->mem[1].bar & ~0x01;
980 if(ioalloc(port, p->mem[1].size, 0, "i82557") < 0){
981 print("i82557: port %#ux in use\n", port);
985 ctlr = malloc(sizeof(Ctlr));
987 print("i82557: can't allocate memory\n");
996 ctlrtail->next = ctlr;
1005 static char* mediatable[9] = {
1006 "10BASE-T", /* TP */
1007 "10BASE-2", /* BNC */
1008 "10BASE-5", /* AUI */
1022 for(i = 0; i < 32; i++){
1023 if((oui = miir(ctlr, i, 2)) == -1 || oui == 0 || oui == 0xFFFF)
1026 x = miir(ctlr, i, 3);
1028 //print("phy%d: oui %#ux reg1 %#ux\n", i, oui, miir(ctlr, i, 1));
1030 ctlr->eeprom[6] = i;
1032 ctlr->eeprom[6] |= 0x07<<8;
1033 else if(oui == 0x80017){
1035 ctlr->eeprom[6] |= 0x0A<<8;
1037 ctlr->eeprom[6] |= 0x04<<8;
1045 shutdown(Ether* ether)
1047 Ctlr *ctlr = ether->ctlr;
1049 print("ether82557 shutting down\n");
1050 csr32w(ctlr, Port, 0);
1052 csr8w(ctlr, Interrupt, InterruptM);
1059 int anar, anlpar, bmcr, bmsr, i, k, medium, phyaddr, x;
1068 * Any adapter matches if no ether->port is supplied,
1069 * otherwise the ports must match.
1071 for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
1074 if(ether->port == 0 || ether->port == ctlr->port){
1083 * Initialise the Ctlr structure.
1084 * Perform a software reset after which should ensure busmastering
1085 * is still enabled. The EtherExpress PRO/100B appears to leave
1086 * the PCI configuration alone (see the 'To do' list above) so punt
1088 * Load the RUB and CUB registers for linear addressing (0).
1091 ether->port = ctlr->port;
1092 ether->irq = ctlr->pcidev->intl;
1093 ether->tbdf = ctlr->pcidev->tbdf;
1095 ilock(&ctlr->rlock);
1096 csr32w(ctlr, Port, 0);
1098 csr8w(ctlr, Interrupt, InterruptM);
1099 iunlock(&ctlr->rlock);
1101 command(ctlr, LoadRUB, 0);
1102 command(ctlr, LoadCUB, 0);
1103 command(ctlr, LoadDCA, PADDR(ctlr->dump));
1106 * Initialise the receive frame, transmit ring and configuration areas.
1113 * Do a dummy read first to get the size
1114 * and allocate ctlr->eeprom.
1118 for(i = 0; i < (1<<ctlr->eepromsz); i++){
1119 x = hy93c46r(ctlr, i);
1120 ctlr->eeprom[i] = x;
1124 print("#l%d: EEPROM checksum - %#4.4ux\n", ether->ctlrno, sum);
1127 * Eeprom[6] indicates whether there is a PHY and whether
1128 * it's not 10Mb-only, in which case use the given PHY address
1129 * to set any PHY specific options and determine the speed.
1130 * Unfortunately, sometimes the EEPROM is blank except for
1131 * the ether address and checksum; in this case look at the
1132 * controller type and if it's am 82558 or 82559 it has an
1133 * embedded PHY so scan for that.
1134 * If no PHY, assume 82503 (serial) operation.
1136 if((ctlr->eeprom[6] & 0x1F00) && !(ctlr->eeprom[6] & 0x8000))
1137 phyaddr = ctlr->eeprom[6] & 0x00FF;
1139 switch(ctlr->pcidev->rid){
1140 case 0x01: /* 82557 A-step */
1141 case 0x02: /* 82557 B-step */
1142 case 0x03: /* 82557 C-step */
1146 case 0x04: /* 82558 A-step */
1147 case 0x05: /* 82558 B-step */
1148 case 0x06: /* 82559 A-step */
1149 case 0x07: /* 82559 B-step */
1150 case 0x08: /* 82559 C-step */
1151 case 0x09: /* 82559ER A-step */
1152 phyaddr = scanphy(ctlr);
1157 * Resolve the highest common ability of the two
1158 * link partners. In descending order:
1159 * 0x0100 100BASE-TX Full Duplex
1162 * 0x0040 10BASE-T Full Duplex
1165 anar = miir(ctlr, phyaddr, 0x04);
1166 anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
1174 switch((ctlr->eeprom[6]>>8) & 0x001F){
1176 case 0x04: /* DP83840 */
1177 case 0x0A: /* DP83840A */
1179 * The DP83840[A] requires some tweaking for
1180 * reliable operation.
1181 * The manual says bit 10 should be unconditionally
1182 * set although it supposedly only affects full-duplex
1183 * operation (an & 0x0140).
1185 x = miir(ctlr, phyaddr, 0x17) & ~0x0520;
1187 for(i = 0; i < ether->nopt; i++){
1188 if(cistrcmp(ether->opt[i], "congestioncontrol"))
1193 miiw(ctlr, phyaddr, 0x17, x);
1196 * If the link partner can't autonegotiate, determine
1197 * the speed from elsewhere.
1200 miir(ctlr, phyaddr, 0x01);
1201 bmsr = miir(ctlr, phyaddr, 0x01);
1202 x = miir(ctlr, phyaddr, 0x19);
1203 if((bmsr & 0x0004) && !(x & 0x0040))
1208 case 0x07: /* Intel 82555 */
1210 * Auto-negotiation may fail if the other end is
1211 * a DP83840A and the cable is short.
1213 miir(ctlr, phyaddr, 0x01);
1214 bmsr = miir(ctlr, phyaddr, 0x01);
1215 if((miir(ctlr, phyaddr, 0) & 0x1000) && (bmsr & 0x0020))
1217 miiw(ctlr, phyaddr, 0x1A, 0x2010);
1218 x = miir(ctlr, phyaddr, 0);
1219 miiw(ctlr, phyaddr, 0, 0x1200|x);
1220 for(i = 0; i < 3000; i++){
1222 if(miir(ctlr, phyaddr, 0x01) & 0x0020)
1225 miiw(ctlr, phyaddr, 0x1A, 0x2000);
1227 anar = miir(ctlr, phyaddr, 0x04);
1228 anlpar = miir(ctlr, phyaddr, 0x05) & 0x03E0;
1239 * Force speed and duplex if no auto-negotiation.
1243 for(i = 0; i < ether->nopt; i++){
1244 for(k = 0; k < nelem(mediatable); k++){
1245 if(cistrcmp(mediatable[k], ether->opt[i]))
1255 case 0x00: /* 10BASE-T */
1256 case 0x01: /* 10BASE-2 */
1257 case 0x02: /* 10BASE-5 */
1258 bmcr &= ~(0x2000|0x0100);
1259 ctlr->configdata[19] &= ~0x40;
1262 case 0x03: /* 100BASE-TX */
1263 case 0x06: /* 100BASE-T4 */
1264 case 0x07: /* 100BASE-FX */
1265 ctlr->configdata[19] &= ~0x40;
1269 case 0x04: /* 10BASE-TFD */
1270 bmcr = (bmcr & ~0x2000)|0x0100;
1271 ctlr->configdata[19] |= 0x40;
1274 case 0x05: /* 100BASE-TXFD */
1275 case 0x08: /* 100BASE-FXFD */
1276 bmcr |= 0x2000|0x0100;
1277 ctlr->configdata[19] |= 0x40;
1282 miiw(ctlr, phyaddr, 0x00, bmcr);
1288 ctlr->configdata[8] = 1;
1289 ctlr->configdata[15] &= ~0x80;
1292 ctlr->configdata[8] = 0;
1293 ctlr->configdata[15] |= 0x80;
1297 * Workaround for some broken HUB chips when connected at 10Mb/s
1299 * This is a band-aid, but as there's no dynamic auto-negotiation
1300 * code at the moment, only deactivate the workaround code in txstart
1301 * if the link is 100Mb/s.
1303 if(ether->mbps != 10)
1307 * Load the chip configuration and start it off.
1310 ether->oq = qopen(256*1024, Qmsg, 0, 0);
1311 configure(ether, 0);
1312 command(ctlr, CUstart, PADDR(&ctlr->cbr->status));
1315 * Check if the adapter's station address is to be overridden.
1316 * If not, read it from the EEPROM and set in ether->ea prior to loading
1317 * the station address with the Individual Address Setup command.
1319 memset(ea, 0, Eaddrlen);
1320 if(memcmp(ea, ether->ea, Eaddrlen) == 0){
1321 for(i = 0; i < Eaddrlen/2; i++){
1322 x = ctlr->eeprom[i];
1324 ether->ea[2*i+1] = x>>8;
1328 ilock(&ctlr->cblock);
1329 ctlr->action = CbIAS;
1331 iunlock(&ctlr->cblock);
1334 * Linkage to the generic ethernet driver.
1336 ether->attach = attach;
1337 ether->transmit = transmit;
1338 ether->interrupt = interrupt;
1339 ether->ifstat = ifstat;
1340 ether->shutdown = shutdown;
1342 ether->promiscuous = promiscuous;
1343 ether->multicast = multicast;
1350 ether82557link(void)
1352 addethercard("i82557", reset);