if_ep.c revision 51630
1/*
2 * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by Herb Peyerl.
16 * 4. The name of Herb Peyerl may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *	if_ep.c,v 1.19 1995/01/24 20:53:45 davidg Exp
31 */
32
33/*
34 *	Modified from the FreeBSD 1.1.5.1 version by:
35 *		 	Andres Vega Garcia
36 *			INRIA - Sophia Antipolis, France
37 *			avega@sophia.inria.fr
38 */
39
40/*
41 * $FreeBSD: head/sys/dev/ep/if_ep.c 51630 1999-09-24 16:27:32Z obrien $
42 *
43 *  Promiscuous mode added and interrupt logic slightly changed
44 *  to reduce the number of adapter failures. Transceiver select
45 *  logic changed to use value from EEPROM. Autoconfiguration
46 *  features added.
47 *  Done by:
48 *          Serge Babkin
49 *          Chelindbank (Chelyabinsk, Russia)
50 *          babkin@hq.icb.chel.su
51 */
52
53/*
54 * Pccard support for 3C589 by:
55 *		HAMADA Naoki
56 *		nao@tom-yam.or.jp
57 */
58
59#include "ep.h"
60#if NEP > 0
61
62#include "bpf.h"
63#include "opt_inet.h"
64#include "opt_ipx.h"
65
66#include <sys/param.h>
67#if defined(__FreeBSD__)
68#include <sys/kernel.h>
69#include <sys/systm.h>
70#endif
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/socket.h>
74#include <sys/sockio.h>
75#if defined(__NetBSD__)
76#include <sys/select.h>
77#endif
78
79#include <net/ethernet.h>
80#include <net/if.h>
81
82#include <netinet/in.h>
83#include <netinet/if_ether.h>
84
85#if NBPF > 0
86#include <net/bpf.h>
87#endif
88
89#if defined(__FreeBSD__)
90#include <machine/clock.h>
91#endif
92
93#include <i386/isa/isa_device.h>
94#include <i386/isa/if_epreg.h>
95#include <i386/isa/elink.h>
96
97/* DELAY_MULTIPLE: How much to boost "base" delays, except
98 * for the inter-bit delays in get_eeprom_data.  A cyrix Media GX needed this.
99 */
100#define DELAY_MULTIPLE 10
101#define BIT_DELAY_MULTIPLE 10
102
103/* Exported variables */
104u_long	ep_unit;
105int	ep_boards;
106struct	ep_board ep_board[EP_MAX_BOARDS + 1];
107
108static	int eeprom_rdy __P((struct ep_softc *sc));
109
110static	int ep_isa_probe __P((struct isa_device *));
111static struct ep_board * ep_look_for_board_at __P((struct isa_device *is));
112static	int ep_isa_attach __P((struct isa_device *));
113static	int epioctl __P((struct ifnet * ifp, u_long, caddr_t));
114
115static	void epinit __P((void *));
116static	ointhand2_t epintr;
117static	void epread __P((struct ep_softc *));
118void	epreset __P((int));
119static	void epstart __P((struct ifnet *));
120static	void epstop __P((struct ep_softc *));
121static	void epwatchdog __P((struct ifnet *));
122
123#if 0
124static	int send_ID_sequence __P((int));
125#endif
126static	int get_eeprom_data __P((int, int));
127
128static	struct ep_softc* ep_softc[NEP];
129static	int ep_current_tag = EP_LAST_TAG + 1;
130static	char *ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
131
132#define ep_ftst(f) (sc->stat&(f))
133#define ep_fset(f) (sc->stat|=(f))
134#define ep_frst(f) (sc->stat&=~(f))
135
136struct isa_driver epdriver = {
137    ep_isa_probe,
138    ep_isa_attach,
139    "ep",
140    0
141};
142
143#include "card.h"
144
145#if NCARD > 0
146#include <sys/select.h>
147#include <sys/module.h>
148#include <pccard/cardinfo.h>
149#include <pccard/slot.h>
150
151/*
152 * PC-Card (PCMCIA) specific code.
153 */
154static int ep_pccard_init __P((struct pccard_devinfo *));
155static int ep_pccard_attach  __P((struct pccard_devinfo *));
156static void ep_unload __P((struct pccard_devinfo *));
157static int card_intr __P((struct pccard_devinfo *));
158static int ep_pccard_identify (struct ep_board *epb, int unit);
159
160PCCARD_MODULE(ep, ep_pccard_init, ep_unload, card_intr, 0, net_imask);
161
162/*
163 * Initialize the device - called from Slot manager.
164 */
165static int
166ep_pccard_init(devi)
167    struct pccard_devinfo *devi;
168{
169    struct isa_device *is = &devi->isahd;
170    struct ep_softc *sc = ep_softc[is->id_unit];
171    struct ep_board *epb;
172    int i;
173
174    epb = &ep_board[is->id_unit];
175
176    if (sc == 0) {
177	if ((sc = ep_alloc(is->id_unit, epb)) == 0) {
178	    return (ENXIO);
179	}
180	ep_unit++;
181    }
182
183    /* get_e() requires these. */
184    sc->ep_io_addr = is->id_iobase;
185    sc->unit = is->id_unit;
186    epb->epb_addr = is->id_iobase;
187    epb->epb_used = 1;
188
189    /*
190     * XXX - Certain (newer?) 3Com cards need epb->cmd_off == 2. Sadly,
191     * you need to have a correct cmd_off in order to identify the card.
192     * So we have to hit it with both and cross our virtual fingers. There's
193     * got to be a better way to do this. jyoung@accessus.net 09/11/1999
194     */
195
196    epb->cmd_off = 0;
197    epb->prod_id = get_e(sc, EEPROM_PROD_ID);
198    if (!ep_pccard_identify(epb, is->id_unit)) {
199	if (bootverbose) printf("ep%d: Pass 1 of 2 detection failed (nonfatal)\n", is->id_unit);
200	epb->cmd_off = 2;
201	epb->prod_id = get_e(sc, EEPROM_PROD_ID);
202	if (!ep_pccard_identify(epb, is->id_unit)) {
203	    if (bootverbose) printf("ep%d: Pass 2 of 2 detection failed (fatal!)\n", is->id_unit);
204	    printf("ep%d: Unit failed to come ready or product ID unknown! (id 0x%x)\n", is->id_unit, epb->prod_id);
205	    return (ENXIO);
206	}
207    }
208
209    epb->res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
210    for (i = 0; i < 3; i++)
211	sc->epb->eth_addr[i] = get_e(sc, EEPROM_NODE_ADDR_0 + i);
212
213    if (ep_pccard_attach(devi) == 0)
214	return (ENXIO);
215
216    sc->arpcom.ac_if.if_snd.ifq_maxlen = ifqmaxlen;
217    return (0);
218}
219
220static int
221ep_pccard_identify(epb, unit)
222    struct ep_board *epb;
223    int unit;
224{
225    /* Determine device type and associated MII capabilities  */
226    switch (epb->prod_id) {
227	case 0x6055: /* 3C556 */
228	    if (bootverbose) printf("ep%d: 3Com 3C556\n", unit);
229	    epb->mii_trans = 1;
230	    return (1);
231	    break; /* NOTREACHED */
232	case 0x4057: /* 3C574 */
233	    if (bootverbose) printf("ep%d: 3Com 3C574\n", unit);
234	    epb->mii_trans = 1;
235	    return (1);
236	    break; /* NOTREACHED */
237	case 0x4b57: /* 3C574B */
238	    if (bootverbose) printf("ep%d: 3Com 3C574B, Megahertz 3CCFE574BT or Fast Etherlink 3C574-TX\n", unit);
239	    epb->mii_trans = 1;
240	    return (1);
241	    break; /* NOTREACHED */
242	case 0x9058: /* 3C589 */
243	    if (bootverbose) printf("ep%d: 3Com Etherlink III 3C589[B/C/D]\n", unit);
244	    epb->mii_trans = 0;
245	    return (1);
246	    break; /* NOTREACHED */
247    }
248    return (0);
249}
250
251static int
252ep_pccard_attach(devi)
253    struct pccard_devinfo *devi;
254{
255    struct isa_device *is = &devi->isahd;
256    struct ep_softc *sc = ep_softc[is->id_unit];
257    u_short config;
258
259    sc->ep_connectors = 0;
260    config = inw(IS_BASE + EP_W0_CONFIG_CTRL);
261    if (config & IS_BNC) {
262	sc->ep_connectors |= BNC;
263    }
264    if (config & IS_UTP) {
265	sc->ep_connectors |= UTP;
266    }
267    if (!(sc->ep_connectors & 7))
268	/* (Apparently) non-fatal */
269	if(bootverbose) printf("ep%d: No connectors or MII.\n", is->id_unit);
270
271    sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
272
273    /* ROM size = 0, ROM base = 0 */
274    /* For now, ignore AUTO SELECT feature of 3C589B and later. */
275    outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
276
277    /* Fake IRQ must be 3 */
278    outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb->res_cfg & 0x0fff) | 0x3000);
279
280    outw(BASE + EP_W0_PRODUCT_ID, sc->epb->prod_id);
281
282    if (sc->epb->mii_trans) {
283	/*
284	 * turn on the MII transciever
285	 */
286	GO_WINDOW(3);
287	outw(BASE + EP_W3_OPTIONS, 0x8040);
288	DELAY(1000);
289	outw(BASE + EP_W3_OPTIONS, 0xc040);
290	outw(BASE + EP_COMMAND, RX_RESET);
291	outw(BASE + EP_COMMAND, TX_RESET);
292	while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
293	DELAY(1000);
294	outw(BASE + EP_W3_OPTIONS, 0x8040);
295    }
296
297    ep_attach(sc);
298
299    return 1;
300}
301
302static void
303ep_unload(devi)
304    struct pccard_devinfo *devi;
305{
306    struct ep_softc *sc = ep_softc[devi->isahd.id_unit];
307
308    if (sc->gone) {
309        printf("ep%d: already unloaded\n", devi->isahd.id_unit);
310	return;
311    }
312    sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING;
313    sc->gone = 1;
314    printf("ep%d: unload\n", devi->isahd.id_unit);
315}
316
317/*
318 * card_intr - Shared interrupt called from
319 * front end of PC-Card handler.
320 */
321static int
322card_intr(devi)
323    struct pccard_devinfo *devi;
324{
325    epintr(devi->isahd.id_unit);
326    return(1);
327}
328#endif /* NCARD > 0 */
329
330static int
331eeprom_rdy(sc)
332    struct ep_softc *sc;
333{
334    int i;
335
336    for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
337	continue;
338    if (i >= MAX_EEPROMBUSY) {
339	printf("ep%d: eeprom failed to come ready.\n", sc->unit);
340	return (0);
341    }
342    return (1);
343}
344
345static struct ep_board *
346ep_look_for_board_at(is)
347    struct isa_device *is;
348{
349    int data, i, j, id_port = ELINK_ID_PORT;
350    int count = 0;
351
352    if (ep_current_tag == (EP_LAST_TAG + 1)) {
353	/* Come here just one time */
354
355	ep_current_tag--;
356
357        /* Look for the ISA boards. Init and leave them actived */
358	outb(id_port, 0);
359	outb(id_port, 0);
360
361	elink_idseq(0xCF);
362
363	elink_reset();
364	DELAY(DELAY_MULTIPLE * 10000);
365	for (i = 0; i < EP_MAX_BOARDS; i++) {
366	    outb(id_port, 0);
367	    outb(id_port, 0);
368	    elink_idseq(0xCF);
369
370	    data = get_eeprom_data(id_port, EEPROM_MFG_ID);
371	    if (data != MFG_ID)
372		break;
373
374	    /* resolve contention using the Ethernet address */
375
376	    for (j = 0; j < 3; j++)
377		 get_eeprom_data(id_port, j);
378
379	    /* and save this address for later use */
380
381	    for (j = 0; j < 3; j++)
382		 ep_board[ep_boards].eth_addr[j] = get_eeprom_data(id_port, j);
383
384	    ep_board[ep_boards].res_cfg =
385		get_eeprom_data(id_port, EEPROM_RESOURCE_CFG);
386
387	    ep_board[ep_boards].prod_id =
388		get_eeprom_data(id_port, EEPROM_PROD_ID);
389
390	    ep_board[ep_boards].epb_used = 0;
391#ifdef PC98
392	    ep_board[ep_boards].epb_addr =
393			(get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x100 + 0x40d0;
394#else
395	    ep_board[ep_boards].epb_addr =
396			(get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x10 + 0x200;
397
398	    if (ep_board[ep_boards].epb_addr > 0x3E0)
399		/* Board in EISA configuration mode */
400		continue;
401#endif /* PC98 */
402
403	    outb(id_port, ep_current_tag);	/* tags board */
404	    outb(id_port, ACTIVATE_ADAPTER_TO_CONFIG);
405	    ep_boards++;
406	    count++;
407	    ep_current_tag--;
408	}
409
410	ep_board[ep_boards].epb_addr = 0;
411	if (count) {
412	    printf("%d 3C5x9 board(s) on ISA found at", count);
413	    for (j = 0; ep_board[j].epb_addr; j++)
414		if (ep_board[j].epb_addr <= 0x3E0)
415		    printf(" 0x%x", ep_board[j].epb_addr);
416	    printf("\n");
417	}
418    }
419
420    /* we have two cases:
421     *
422     *  1. Device was configured with 'port ?'
423     *      In this case we search for the first unused card in list
424     *
425     *  2. Device was configured with 'port xxx'
426     *      In this case we search for the unused card with that address
427     *
428     */
429
430    if (IS_BASE == -1) { /* port? */
431	for (i = 0; ep_board[i].epb_addr && ep_board[i].epb_used; i++)
432	    ;
433	if (ep_board[i].epb_addr == 0)
434	    return 0;
435
436	IS_BASE = ep_board[i].epb_addr;
437	ep_board[i].epb_used = 1;
438
439	return &ep_board[i];
440    } else {
441	for (i = 0;
442	     ep_board[i].epb_addr && ep_board[i].epb_addr != IS_BASE;
443	     i++)
444	    ;
445
446	if (ep_board[i].epb_used || ep_board[i].epb_addr != IS_BASE)
447	    return 0;
448
449	if (inw(IS_BASE + EP_W0_EEPROM_COMMAND) & EEPROM_TST_MODE) {
450	    printf("ep%d: 3c5x9 at 0x%x in PnP mode. Disable PnP mode!\n",
451		   is->id_unit, IS_BASE);
452	}
453	ep_board[i].epb_used = 1;
454
455	return &ep_board[i];
456    }
457}
458
459/*
460 * get_e: gets a 16 bits word from the EEPROM. we must have set the window
461 * before
462 */
463u_int16_t
464get_e(sc, offset)
465    struct ep_softc *sc;
466    int offset;
467{
468    if (!eeprom_rdy(sc))
469	return (0xffff);
470    outw(BASE + EP_W0_EEPROM_COMMAND, (EEPROM_CMD_RD << sc->epb->cmd_off) | offset);
471    if (!eeprom_rdy(sc))
472	return (0xffff);
473    return (inw(BASE + EP_W0_EEPROM_DATA));
474}
475
476struct ep_softc *
477ep_alloc(unit, epb)
478    int	unit;
479    struct	ep_board *epb;
480{
481    struct	ep_softc *sc;
482
483    if (unit >= NEP) {
484	printf("ep: unit number (%d) too high\n", unit);
485	return NULL;
486    }
487
488    /*
489     * Allocate a storage area for us
490     */
491    if (ep_softc[unit]) {
492	printf("ep%d: unit number already allocated to another "
493	       "adaptor\n", unit);
494	return NULL;
495    }
496
497    sc = malloc(sizeof(struct ep_softc), M_DEVBUF, M_NOWAIT);
498    if (!sc) {
499	printf("ep%d: cannot malloc!\n", unit);
500	return NULL;
501    }
502    bzero(sc, sizeof(struct ep_softc));
503    ep_softc[unit] = sc;
504    sc->unit = unit;
505    sc->ep_io_addr = epb->epb_addr;
506    sc->epb = epb;
507
508    return(sc);
509}
510
511void
512ep_free(sc)
513    struct ep_softc *sc;
514{
515    ep_softc[sc->unit] = NULL;
516    free(sc, M_DEVBUF);
517    return;
518}
519
520int
521ep_isa_probe(is)
522    struct isa_device *is;
523{
524    struct ep_softc *sc;
525    struct ep_board *epb;
526    u_short k;
527
528    if ((epb = ep_look_for_board_at(is)) == 0)
529	return (0);
530
531    /*
532     * Allocate a storage area for us
533     */
534    sc = ep_alloc(ep_unit, epb);
535    if (!sc)
536	return (0);
537
538    is->id_unit = ep_unit++;
539
540    /*
541     * The iobase was found and MFG_ID was 0x6d50. PROD_ID should be
542     * 0x9[0-f]50	(IBM-PC)
543     * 0x9[0-f]5[0-f]	(PC-98)
544     */
545    GO_WINDOW(0);
546    k = sc->epb->prod_id;
547#ifdef PC98
548    if ((k & 0xf0f0) != (PROD_ID & 0xf0f0)) {
549#else
550    if ((k & 0xf0ff) != (PROD_ID & 0xf0ff)) {
551#endif
552	printf("ep_isa_probe: ignoring model %04x\n", k);
553	ep_free(sc);
554	return (0);
555    }
556
557    k = sc->epb->res_cfg;
558
559    k >>= 12;
560
561    /* Now we have two cases again:
562     *
563     *  1. Device was configured with 'irq?'
564     *      In this case we use irq read from the board
565     *
566     *  2. Device was configured with 'irq xxx'
567     *      In this case we set up the board to use specified interrupt
568     *
569     */
570
571    if (is->id_irq == 0) { /* irq? */
572	is->id_irq = 1 << ((k == 2) ? 9 : k);
573    }
574
575    sc->stat = 0;	/* 16 bit access */
576
577    /* By now, the adapter is already activated */
578
579    return (EP_IOSIZE);		/* 16 bytes of I/O space used. */
580}
581
582static int
583ep_isa_attach(is)
584    struct isa_device *is;
585{
586    struct ep_softc *sc = ep_softc[is->id_unit];
587    u_short config;
588    int irq;
589
590    is->id_ointr = epintr;
591    sc->ep_connectors = 0;
592    config = inw(IS_BASE + EP_W0_CONFIG_CTRL);
593    if (config & IS_AUI) {
594	sc->ep_connectors |= AUI;
595    }
596    if (config & IS_BNC) {
597	sc->ep_connectors |= BNC;
598    }
599    if (config & IS_UTP) {
600	sc->ep_connectors |= UTP;
601    }
602    if (!(sc->ep_connectors & 7))
603	printf("no connectors!");
604    sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
605    /*
606     * Write IRQ value to board
607     */
608
609    irq = ffs(is->id_irq) - 1;
610    if (irq == -1) {
611	printf(" invalid irq... cannot attach\n");
612	return 0;
613    }
614
615    GO_WINDOW(0);
616    SET_IRQ(BASE, irq);
617
618    ep_attach(sc);
619    return 1;
620}
621
622int
623ep_attach(sc)
624    struct ep_softc *sc;
625{
626    struct ifnet *ifp = &sc->arpcom.ac_if;
627    u_short *p;
628    int i;
629    int attached;
630
631    sc->gone = 0;
632    attached = (ifp->if_softc != 0);
633
634    printf("ep%d: ", sc->unit);
635    /*
636     * Current media type
637     */
638    if (sc->ep_connectors & AUI) {
639	printf("aui");
640	if (sc->ep_connectors & ~AUI)
641		printf("/");
642    }
643    if (sc->ep_connectors & UTP) {
644	printf("utp");
645	if (sc->ep_connectors & BNC)
646		printf("/");
647    }
648    if (sc->ep_connectors & BNC) {
649	printf("bnc");
650    }
651
652    printf("[*%s*]", ep_conn_type[sc->ep_connector]);
653
654    /*
655     * Setup the station address
656     */
657    p = (u_short *) & sc->arpcom.ac_enaddr;
658    GO_WINDOW(2);
659    for (i = 0; i < 3; i++) {
660	p[i] = htons(sc->epb->eth_addr[i]);
661	outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
662    }
663    printf(" address %6D\n", sc->arpcom.ac_enaddr, ":");
664
665    ifp->if_softc = sc;
666    ifp->if_unit = sc->unit;
667    ifp->if_name = "ep";
668    ifp->if_mtu = ETHERMTU;
669    ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
670    ifp->if_output = ether_output;
671    ifp->if_start = epstart;
672    ifp->if_ioctl = epioctl;
673    ifp->if_watchdog = epwatchdog;
674    ifp->if_init = epinit;
675
676    if (!attached) {
677	if_attach(ifp);
678	ether_ifattach(ifp);
679    }
680
681#ifdef EP_LOCAL_STATS
682    sc->rx_no_first = sc->rx_no_mbuf =
683	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
684	sc->tx_underrun = 0;
685#endif
686    ep_fset(F_RX_FIRST);
687    sc->top = sc->mcur = 0;
688
689#if NBPF > 0
690    if (!attached) {
691	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
692    }
693#endif
694    return 0;
695}
696
697
698/*
699 * The order in here seems important. Otherwise we may not receive
700 * interrupts. ?!
701 */
702static void
703epinit(xsc)
704    void *xsc;
705{
706    struct ep_softc *sc = xsc;
707    register struct ifnet *ifp = &sc->arpcom.ac_if;
708    int s, i, j;
709
710    if (sc->gone)
711	return;
712
713	/*
714    if (ifp->if_addrlist == (struct ifaddr *) 0)
715	return;
716	*/
717
718    s = splimp();
719    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
720
721    GO_WINDOW(0);
722    outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
723    GO_WINDOW(4);
724    outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
725    GO_WINDOW(0);
726
727    /* Disable the card */
728    outw(BASE + EP_W0_CONFIG_CTRL, 0);
729
730    /* Enable the card */
731    outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
732
733    GO_WINDOW(2);
734
735    /* Reload the ether_addr. */
736    for (i = 0; i < 6; i++)
737	outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
738
739    outw(BASE + EP_COMMAND, RX_RESET);
740    outw(BASE + EP_COMMAND, TX_RESET);
741    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
742
743    /* Window 1 is operating window */
744    GO_WINDOW(1);
745    for (i = 0; i < 31; i++)
746	inb(BASE + EP_W1_TX_STATUS);
747
748    /* get rid of stray intr's */
749    outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
750
751    outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
752
753    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
754
755    if (ifp->if_flags & IFF_PROMISC)
756	outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
757	 FIL_GROUP | FIL_BRDCST | FIL_ALL);
758    else
759	outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
760	 FIL_GROUP | FIL_BRDCST);
761
762     /*
763      * S.B.
764      *
765      * Now behavior was slightly changed:
766      *
767      * if any of flags link[0-2] is used and its connector is
768      * physically present the following connectors are used:
769      *
770      *   link0 - AUI * highest precedence
771      *   link1 - BNC
772      *   link2 - UTP * lowest precedence
773      *
774      * If none of them is specified then
775      * connector specified in the EEPROM is used
776      * (if present on card or AUI if not).
777      *
778      */
779
780    /* Set the xcvr. */
781    if (ifp->if_flags & IFF_LINK0 && sc->ep_connectors & AUI) {
782	i = ACF_CONNECTOR_AUI;
783    } else if (ifp->if_flags & IFF_LINK1 && sc->ep_connectors & BNC) {
784	i = ACF_CONNECTOR_BNC;
785    } else if (ifp->if_flags & IFF_LINK2 && sc->ep_connectors & UTP) {
786	i = ACF_CONNECTOR_UTP;
787    } else {
788	i = sc->ep_connector;
789    }
790    GO_WINDOW(0);
791    j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
792    outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
793
794    switch(i) {
795      case ACF_CONNECTOR_UTP:
796	if (sc->ep_connectors & UTP) {
797	    GO_WINDOW(4);
798	    outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
799	}
800	break;
801      case ACF_CONNECTOR_BNC:
802	if (sc->ep_connectors & BNC) {
803	    outw(BASE + EP_COMMAND, START_TRANSCEIVER);
804	    DELAY(DELAY_MULTIPLE * 1000);
805	}
806	break;
807      case ACF_CONNECTOR_AUI:
808	/* nothing to do */
809	break;
810      default:
811	printf("ep%d: strange connector type in EEPROM: assuming AUI\n",
812	       sc->unit);
813	break;
814    }
815
816    outw(BASE + EP_COMMAND, RX_ENABLE);
817    outw(BASE + EP_COMMAND, TX_ENABLE);
818
819    ifp->if_flags |= IFF_RUNNING;
820    ifp->if_flags &= ~IFF_OACTIVE;	/* just in case */
821
822#ifdef EP_LOCAL_STATS
823    sc->rx_no_first = sc->rx_no_mbuf =
824	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
825	sc->tx_underrun = 0;
826#endif
827    ep_fset(F_RX_FIRST);
828    if (sc->top) {
829	m_freem(sc->top);
830	sc->top = sc->mcur = 0;
831    }
832    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
833    outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
834
835    /*
836     * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
837     * any that we had in case we're being called from intr or somewhere
838     * else.
839     */
840
841    GO_WINDOW(1);
842    epstart(ifp);
843
844    splx(s);
845}
846
847static const char padmap[] = {0, 3, 2, 1};
848
849static void
850epstart(ifp)
851    struct ifnet *ifp;
852{
853    register struct ep_softc *sc = ifp->if_softc;
854    register u_int len;
855    register struct mbuf *m;
856    struct mbuf *top;
857    int s, pad;
858
859    if (sc->gone) {
860	return;
861    }
862
863    s = splimp();
864    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
865    if (ifp->if_flags & IFF_OACTIVE) {
866	splx(s);
867	return;
868    }
869startagain:
870    /* Sneak a peek at the next packet */
871    m = ifp->if_snd.ifq_head;
872    if (m == 0) {
873	splx(s);
874	return;
875    }
876    for (len = 0, top = m; m; m = m->m_next)
877	len += m->m_len;
878
879    pad = padmap[len & 3];
880
881    /*
882     * The 3c509 automatically pads short packets to minimum ethernet length,
883     * but we drop packets that are too large. Perhaps we should truncate
884     * them instead?
885     */
886    if (len + pad > ETHER_MAX_LEN) {
887	/* packet is obviously too large: toss it */
888	++ifp->if_oerrors;
889	IF_DEQUEUE(&ifp->if_snd, m);
890	m_freem(m);
891	goto readcheck;
892    }
893    if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
894	/* no room in FIFO */
895	outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
896	/* make sure */
897	if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
898	    ifp->if_flags |= IFF_OACTIVE;
899	    splx(s);
900	    return;
901	}
902    }
903    IF_DEQUEUE(&ifp->if_snd, m);
904
905    outw(BASE + EP_W1_TX_PIO_WR_1, len);
906    outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);	/* Second dword meaningless */
907
908    for (top = m; m != 0; m = m->m_next)
909	if (ep_ftst(F_ACCESS_32_BITS)) {
910	    outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
911		  m->m_len / 4);
912	    if (m->m_len & 3)
913		outsb(BASE + EP_W1_TX_PIO_WR_1,
914		      mtod(m, caddr_t) + (m->m_len & (~3)),
915		      m->m_len & 3);
916	} else {
917	    outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
918	    if (m->m_len & 1)
919		outb(BASE + EP_W1_TX_PIO_WR_1,
920		     *(mtod(m, caddr_t) + m->m_len - 1));
921	}
922
923    while (pad--)
924	outb(BASE + EP_W1_TX_PIO_WR_1, 0);	/* Padding */
925
926#if NBPF > 0
927    if (ifp->if_bpf) {
928	bpf_mtap(ifp, top);
929    }
930#endif
931
932    ifp->if_timer = 2;
933    ifp->if_opackets++;
934    m_freem(top);
935
936    /*
937     * Is another packet coming in? We don't want to overflow the tiny RX
938     * fifo.
939     */
940readcheck:
941    if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
942	/*
943	 * we check if we have packets left, in that case we prepare to come
944	 * back later
945	 */
946	if (ifp->if_snd.ifq_head) {
947	    outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
948	}
949	splx(s);
950	return;
951    }
952    goto startagain;
953}
954
955static void
956epintr(unit)
957    int unit;
958{
959    register struct ep_softc *sc = ep_softc[unit];
960
961    if (sc->gone) {
962	return;
963    }
964
965    ep_intr(sc);
966}
967
968void
969ep_intr(arg)
970    void *arg;
971{
972    struct ep_softc *sc;
973    register int status;
974    struct ifnet *ifp;
975    int x;
976
977    x = splbio();
978
979    sc = (struct ep_softc *)arg;
980
981    ifp = &sc->arpcom.ac_if;
982
983    outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
984
985rescan:
986
987    while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
988
989	/* first acknowledge all interrupt sources */
990	outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
991
992	if (status & (S_RX_COMPLETE | S_RX_EARLY)) {
993	    epread(sc);
994	    continue;
995	}
996	if (status & S_TX_AVAIL) {
997	    /* we need ACK */
998	    ifp->if_timer = 0;
999	    ifp->if_flags &= ~IFF_OACTIVE;
1000	    GO_WINDOW(1);
1001	    inw(BASE + EP_W1_FREE_TX);
1002	    epstart(ifp);
1003	}
1004	if (status & S_CARD_FAILURE) {
1005	    ifp->if_timer = 0;
1006#ifdef EP_LOCAL_STATS
1007	    printf("\nep%d:\n\tStatus: %x\n", sc->unit, status);
1008	    GO_WINDOW(4);
1009	    printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
1010	    printf("\tStat: %x\n", sc->stat);
1011	    printf("\tIpackets=%d, Opackets=%d\n",
1012		ifp->if_ipackets, ifp->if_opackets);
1013	    printf("\tNOF=%d, NOMB=%d, BPFD=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
1014		   sc->rx_no_first, sc->rx_no_mbuf, sc->rx_bpf_disc, sc->rx_overrunf,
1015		   sc->rx_overrunl, sc->tx_underrun);
1016#else
1017
1018#ifdef DIAGNOSTIC
1019	    printf("ep%d: Status: %x (input buffer overflow)\n", sc->unit, status);
1020#else
1021	    ++ifp->if_ierrors;
1022#endif
1023
1024#endif
1025	    epinit(sc);
1026	    splx(x);
1027	    return;
1028	}
1029	if (status & S_TX_COMPLETE) {
1030	    ifp->if_timer = 0;
1031	    /* we  need ACK. we do it at the end */
1032	    /*
1033	     * We need to read TX_STATUS until we get a 0 status in order to
1034	     * turn off the interrupt flag.
1035	     */
1036	    while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
1037		if (status & TXS_SUCCES_INTR_REQ);
1038		else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
1039		    outw(BASE + EP_COMMAND, TX_RESET);
1040		    if (status & TXS_UNDERRUN) {
1041#ifdef EP_LOCAL_STATS
1042			sc->tx_underrun++;
1043#endif
1044		    } else {
1045			if (status & TXS_JABBER);
1046			else	/* TXS_MAX_COLLISION - we shouldn't get here */
1047			    ++ifp->if_collisions;
1048		    }
1049		    ++ifp->if_oerrors;
1050		    outw(BASE + EP_COMMAND, TX_ENABLE);
1051		    /*
1052		     * To have a tx_avail_int but giving the chance to the
1053		     * Reception
1054		     */
1055		    if (ifp->if_snd.ifq_head) {
1056			outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
1057		    }
1058		}
1059		outb(BASE + EP_W1_TX_STATUS, 0x0);	/* pops up the next
1060							 * status */
1061	    }			/* while */
1062	    ifp->if_flags &= ~IFF_OACTIVE;
1063	    GO_WINDOW(1);
1064	    inw(BASE + EP_W1_FREE_TX);
1065	    epstart(ifp);
1066	}			/* end TX_COMPLETE */
1067    }
1068
1069    outw(BASE + EP_COMMAND, C_INTR_LATCH);	/* ACK int Latch */
1070
1071    if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
1072	goto rescan;
1073
1074    /* re-enable Ints */
1075    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
1076
1077    splx(x);
1078}
1079
1080static void
1081epread(sc)
1082    register struct ep_softc *sc;
1083{
1084    struct ether_header *eh;
1085    struct mbuf *top, *mcur, *m;
1086    struct ifnet *ifp;
1087    int lenthisone;
1088
1089    short rx_fifo2, status;
1090    register short rx_fifo;
1091
1092    ifp = &sc->arpcom.ac_if;
1093    status = inw(BASE + EP_W1_RX_STATUS);
1094
1095read_again:
1096
1097    if (status & ERR_RX) {
1098	++ifp->if_ierrors;
1099	if (status & ERR_RX_OVERRUN) {
1100	    /*
1101	     * we can think the rx latency is actually greather than we
1102	     * expect
1103	     */
1104#ifdef EP_LOCAL_STATS
1105	    if (ep_ftst(F_RX_FIRST))
1106		sc->rx_overrunf++;
1107	    else
1108		sc->rx_overrunl++;
1109#endif
1110	}
1111	goto out;
1112    }
1113    rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
1114
1115    if (ep_ftst(F_RX_FIRST)) {
1116	MGETHDR(m, M_DONTWAIT, MT_DATA);
1117	if (!m)
1118	    goto out;
1119	if (rx_fifo >= MINCLSIZE)
1120	    MCLGET(m, M_DONTWAIT);
1121	sc->top = sc->mcur = top = m;
1122#define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
1123#define EOFF    (EROUND - sizeof(struct ether_header))
1124	top->m_data += EOFF;
1125
1126	/* Read what should be the header. */
1127	insw(BASE + EP_W1_RX_PIO_RD_1,
1128	     mtod(top, caddr_t), sizeof(struct ether_header) / 2);
1129	top->m_len = sizeof(struct ether_header);
1130	rx_fifo -= sizeof(struct ether_header);
1131	sc->cur_len = rx_fifo2;
1132    } else {
1133	/* come here if we didn't have a complete packet last time */
1134	top = sc->top;
1135	m = sc->mcur;
1136	sc->cur_len += rx_fifo2;
1137    }
1138
1139    /* Reads what is left in the RX FIFO */
1140    while (rx_fifo > 0) {
1141	lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
1142	if (lenthisone == 0) {	/* no room in this one */
1143	    mcur = m;
1144	    MGET(m, M_DONTWAIT, MT_DATA);
1145	    if (!m)
1146		goto out;
1147	    if (rx_fifo >= MINCLSIZE)
1148		MCLGET(m, M_DONTWAIT);
1149	    m->m_len = 0;
1150	    mcur->m_next = m;
1151	    lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
1152	}
1153	if (ep_ftst(F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
1154	    insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
1155		 lenthisone / 4);
1156	    m->m_len += (lenthisone & ~3);
1157	    if (lenthisone & 3)
1158		insb(BASE + EP_W1_RX_PIO_RD_1,
1159		     mtod(m, caddr_t) + m->m_len,
1160		     lenthisone & 3);
1161	    m->m_len += (lenthisone & 3);
1162	} else {
1163	    insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
1164		 lenthisone / 2);
1165	    m->m_len += lenthisone;
1166	    if (lenthisone & 1)
1167		*(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
1168	}
1169	rx_fifo -= lenthisone;
1170    }
1171
1172    if (status & ERR_RX_INCOMPLETE) {	/* we haven't received the complete
1173					 * packet */
1174	sc->mcur = m;
1175#ifdef EP_LOCAL_STATS
1176	sc->rx_no_first++;	/* to know how often we come here */
1177#endif
1178	ep_frst(F_RX_FIRST);
1179	if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
1180	    /* we see if by now, the packet has completly arrived */
1181	    goto read_again;
1182	}
1183	outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
1184	return;
1185    }
1186    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1187    ++ifp->if_ipackets;
1188    ep_fset(F_RX_FIRST);
1189    top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1190    top->m_pkthdr.len = sc->cur_len;
1191
1192#if NBPF > 0
1193    if (ifp->if_bpf) {
1194	bpf_mtap(ifp, top);
1195
1196	/*
1197	 * Note that the interface cannot be in promiscuous mode if there are
1198	 * no BPF listeners.  And if we are in promiscuous mode, we have to
1199	 * check if this packet is really ours.
1200	 */
1201	eh = mtod(top, struct ether_header *);
1202	if ((ifp->if_flags & IFF_PROMISC) &&
1203	    (eh->ether_dhost[0] & 1) == 0 &&
1204	    bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1205		 sizeof(eh->ether_dhost)) != 0 &&
1206	    bcmp(eh->ether_dhost, etherbroadcastaddr,
1207		 sizeof(eh->ether_dhost)) != 0) {
1208	    if (sc->top) {
1209		m_freem(sc->top);
1210		sc->top = 0;
1211	    }
1212	    ep_fset(F_RX_FIRST);
1213#ifdef EP_LOCAL_STATS
1214	    sc->rx_bpf_disc++;
1215#endif
1216	    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1217	    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
1218	    return;
1219	}
1220    }
1221#endif
1222
1223    eh = mtod(top, struct ether_header *);
1224    m_adj(top, sizeof(struct ether_header));
1225    ether_input(ifp, eh, top);
1226    sc->top = 0;
1227    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1228    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
1229    return;
1230
1231out:
1232    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1233    if (sc->top) {
1234	m_freem(sc->top);
1235	sc->top = 0;
1236#ifdef EP_LOCAL_STATS
1237	sc->rx_no_mbuf++;
1238#endif
1239    }
1240    ep_fset(F_RX_FIRST);
1241    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1242    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
1243}
1244
1245/*
1246 * Look familiar?
1247 */
1248static int
1249epioctl(ifp, cmd, data)
1250    register struct ifnet *ifp;
1251    u_long cmd;
1252    caddr_t data;
1253{
1254    struct ep_softc *sc = ifp->if_softc;
1255    int s, error = 0;
1256
1257    s = splimp();
1258
1259    switch (cmd) {
1260      case SIOCSIFADDR:
1261      case SIOCGIFADDR:
1262      case SIOCSIFMTU:
1263        error = ether_ioctl(ifp, cmd, data);
1264        break;
1265
1266      case SIOCSIFFLAGS:
1267
1268	if ((ifp->if_flags & IFF_UP) == 0 && ifp->if_flags & IFF_RUNNING) {
1269	    ifp->if_flags &= ~IFF_RUNNING;
1270	    epstop(sc);
1271	    break;
1272	} else {
1273	    /* reinitialize card on any parameter change */
1274	    epinit(sc);
1275	    break;
1276	}
1277
1278	/* NOTREACHED */
1279	break;
1280#ifdef notdef
1281      case SIOCGHWADDR:
1282	bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
1283	      sizeof(sc->sc_addr));
1284	break;
1285#endif
1286	case SIOCADDMULTI:
1287	case SIOCDELMULTI:
1288	    /*
1289	     * The Etherlink III has no programmable multicast
1290	     * filter.  We always initialize the card to be
1291	     * promiscuous to multicast, since we're always a
1292	     * member of the ALL-SYSTEMS group, so there's no
1293	     * need to process SIOC*MULTI requests.
1294	     */
1295	    error = 0;
1296	    break;
1297      default:
1298		error = EINVAL;
1299    }
1300
1301    splx(s);
1302
1303    return (error);
1304}
1305
1306static void
1307epwatchdog(ifp)
1308    struct ifnet *ifp;
1309{
1310    struct ep_softc *sc = ifp->if_softc;
1311
1312    /*
1313    printf("ep: watchdog\n");
1314
1315    log(LOG_ERR, "ep%d: watchdog\n", ifp->if_unit);
1316    ifp->if_oerrors++;
1317    */
1318
1319    if (sc->gone) {
1320	return;
1321    }
1322
1323    ifp->if_flags &= ~IFF_OACTIVE;
1324    epstart(ifp);
1325    ep_intr(ifp->if_softc);
1326}
1327
1328static void
1329epstop(sc)
1330    struct ep_softc *sc;
1331{
1332    if (sc->gone) {
1333	return;
1334    }
1335
1336    outw(BASE + EP_COMMAND, RX_DISABLE);
1337    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
1338    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1339    outw(BASE + EP_COMMAND, TX_DISABLE);
1340    outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
1341    outw(BASE + EP_COMMAND, RX_RESET);
1342    outw(BASE + EP_COMMAND, TX_RESET);
1343    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
1344    outw(BASE + EP_COMMAND, C_INTR_LATCH);
1345    outw(BASE + EP_COMMAND, SET_RD_0_MASK);
1346    outw(BASE + EP_COMMAND, SET_INTR_MASK);
1347    outw(BASE + EP_COMMAND, SET_RX_FILTER);
1348}
1349
1350
1351#if 0
1352static int
1353send_ID_sequence(port)
1354    int port;
1355{
1356    int cx, al;
1357
1358    for (al = 0xff, cx = 0; cx < 255; cx++) {
1359	outb(port, al);
1360	al <<= 1;
1361	if (al & 0x100)
1362	    al ^= 0xcf;
1363    }
1364    return (1);
1365}
1366#endif
1367
1368
1369/*
1370 * We get eeprom data from the id_port given an offset into the eeprom.
1371 * Basically; after the ID_sequence is sent to all of the cards; they enter
1372 * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
1373 * the eeprom data.  We then read the port 16 times and with every read; the
1374 * cards check for contention (ie: if one card writes a 0 bit and another
1375 * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
1376 * compares the data on the bus; if there is a difference then that card goes
1377 * into ID_WAIT state again). In the meantime; one bit of data is returned in
1378 * the AX register which is conveniently returned to us by inb().  Hence; we
1379 * read 16 times getting one bit of data with each read.
1380 */
1381
1382static int
1383get_eeprom_data(id_port, offset)
1384    int id_port;
1385    int offset;
1386{
1387    int i, data = 0;
1388    outb(id_port, 0x80 + offset);
1389    for (i = 0; i < 16; i++) {
1390    	DELAY(BIT_DELAY_MULTIPLE * 1000);
1391	data = (data << 1) | (inw(id_port) & 1);
1392    }
1393    return (data);
1394}
1395
1396#endif				/* NEP > 0 */
1397