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