if_ep.c revision 52549
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 52549 1999-10-27 06:25:16Z mdodd $
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/*
60 * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
61 *                             <mdodd@FreeBSD.org>
62 */
63
64#include <sys/param.h>
65#include <sys/kernel.h>
66#include <sys/systm.h>
67#include <sys/malloc.h>
68#include <sys/mbuf.h>
69#include <sys/socket.h>
70#include <sys/sockio.h>
71
72#include <sys/module.h>
73#include <sys/bus.h>
74
75#include <machine/bus.h>
76#include <machine/resource.h>
77#include <sys/rman.h>
78
79#include <net/if.h>
80#include <net/if_arp.h>
81#include <net/if_media.h>
82#include <net/ethernet.h>
83#include <net/bpf.h>
84
85#include <netinet/in.h>
86#include <netinet/if_ether.h>
87
88#include <machine/clock.h>
89
90#include <dev/ep/if_epreg.h>
91#include <dev/ep/if_epvar.h>
92#include <i386/isa/elink.h>
93
94/* Exported variables */
95devclass_t ep_devclass;
96
97#if 0
98static char *	ep_conn_type[] = {"UTP", "AUI", "???", "BNC"};
99static int	if_media2ep_media[] = { 0, 0, 0, UTP, BNC, AUI };
100#endif
101
102static int	ep_media2if_media[] =
103	{ IFM_10_T, IFM_10_5, IFM_NONE, IFM_10_2, IFM_NONE };
104
105/* if functions */
106static void	ep_if_init	__P((void *));
107static int	ep_if_ioctl	__P((struct ifnet *, u_long, caddr_t));
108static void	ep_if_start	__P((struct ifnet *));
109static void	ep_if_watchdog	__P((struct ifnet *));
110
111/* if_media functions */
112static int	ep_ifmedia_upd	__P((struct ifnet *));
113static void	ep_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
114
115static void	ep_get_macaddr	__P((struct ep_softc *, u_char *));
116static void	epstop		__P((struct ep_softc *));
117static void	epread		__P((struct ep_softc *));
118static int	eeprom_rdy	__P((struct ep_softc *));
119
120#define EP_FTST(sc, f)	(sc->stat &   (f))
121#define EP_FSET(sc, f)	(sc->stat |=  (f))
122#define EP_FRST(sc, f)	(sc->stat &= ~(f))
123
124static int
125eeprom_rdy(sc)
126    struct ep_softc *sc;
127{
128    int i;
129
130    for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
131	continue;
132    if (i >= MAX_EEPROMBUSY) {
133	printf("ep%d: eeprom failed to come ready.\n", sc->unit);
134	return (0);
135    }
136    return (1);
137}
138
139/*
140 * get_e: gets a 16 bits word from the EEPROM. we must have set the window
141 * before
142 */
143u_int16_t
144get_e(sc, offset)
145    struct ep_softc *sc;
146    int offset;
147{
148    if (!eeprom_rdy(sc))
149	return (0xffff);
150    outw(BASE + EP_W0_EEPROM_COMMAND, (EEPROM_CMD_RD << sc->epb.cmd_off) | offset);
151    if (!eeprom_rdy(sc))
152	return (0xffff);
153    return (inw(BASE + EP_W0_EEPROM_DATA));
154}
155
156static void
157ep_get_macaddr(sc, addr)
158	struct ep_softc	*	sc;
159	u_char *		addr;
160{
161	int			i;
162	u_int16_t * 		macaddr = (u_int16_t *)addr;
163
164	GO_WINDOW(0);
165        for(i = EEPROM_NODE_ADDR_0; i <= EEPROM_NODE_ADDR_2; i++) {
166		macaddr[i] = htons(get_e(sc, i));
167        }
168
169	return;
170}
171
172int
173ep_alloc(device_t dev)
174{
175	struct ep_softc	*	sc = device_get_softc(dev);
176	int			rid;
177	int			error = 0;
178
179        rid = 0;
180        sc->iobase = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
181                                        0, ~0, 1, RF_ACTIVE);
182        if (!sc->iobase) {
183                device_printf(dev, "No I/O space?!\n");
184		error = ENXIO;
185                goto bad;
186        }
187
188        rid = 0;
189        sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
190                                     0, ~0, 1, RF_ACTIVE);
191        if (!sc->irq) {
192                device_printf(dev, "No irq?!\n");
193		error = ENXIO;
194                goto bad;
195        }
196
197        sc->dev = dev;
198        sc->unit = device_get_unit(dev);
199        sc->stat = 0;   /* 16 bit access */
200
201        sc->ep_io_addr = rman_get_start(sc->iobase);
202
203        sc->ep_btag = rman_get_bustag(sc->iobase);
204        sc->ep_bhandle = rman_get_bushandle(sc->iobase);
205
206	sc->ep_connectors = 0;
207	sc->ep_connector = 0;
208
209	sc->epb.cmd_off = 0;
210	sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
211	sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
212
213bad:
214	return (error);
215}
216
217void
218ep_get_media(sc)
219	struct ep_softc	*	sc;
220{
221	u_int16_t		config;
222
223        GO_WINDOW(0);
224        config = inw(BASE + EP_W0_CONFIG_CTRL);
225        if (config & IS_AUI)
226                sc->ep_connectors |= AUI;
227        if (config & IS_BNC)
228                sc->ep_connectors |= BNC;
229        if (config & IS_UTP)
230                sc->ep_connectors |= UTP;
231
232        if (!(sc->ep_connectors & 7)) {
233		if (bootverbose)
234                	device_printf(sc->dev, "no connectors!\n");
235        }
236
237	/*
238	 * This works for most of the cards so we'll do it here.
239	 * The cards that require something different can override
240	 * this later on.
241	 */
242	sc->ep_connector = inw(BASE + EP_W0_ADDRESS_CFG) >> ACF_CONNECTOR_BITS;
243
244	return;
245}
246
247void
248ep_free(device_t dev)
249{
250	struct ep_softc	*	sc = device_get_softc(dev);
251
252	if (sc->iobase)
253		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->iobase);
254	if (sc->irq)
255		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
256
257	return;
258}
259
260int
261ep_attach(sc)
262	struct ep_softc *	sc;
263{
264	struct ifnet *		ifp = NULL;
265	struct ifmedia *	ifm = NULL;
266	u_short *		p;
267	int			i;
268	int			attached;
269
270	sc->gone = 0;
271
272	ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
273
274	/*
275	 * Setup the station address
276	 */
277	p = (u_short *)&sc->arpcom.ac_enaddr;
278	GO_WINDOW(2);
279	for (i = 0; i < 3; i++) {
280		outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
281	}
282
283	device_printf(sc->dev, "Ethernet address %6D\n",
284			sc->arpcom.ac_enaddr, ":");
285
286	ifp = &sc->arpcom.ac_if;
287	attached = (ifp->if_softc != 0);
288
289	ifp->if_softc = sc;
290	ifp->if_unit = sc->unit;
291	ifp->if_name = "ep";
292	ifp->if_mtu = ETHERMTU;
293	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
294	ifp->if_output = ether_output;
295	ifp->if_start = ep_if_start;
296	ifp->if_ioctl = ep_if_ioctl;
297	ifp->if_watchdog = ep_if_watchdog;
298	ifp->if_init = ep_if_init;
299	ifp->if_snd.ifq_maxlen = 8;
300
301	if (!sc->epb.mii_trans) {
302		ifmedia_init(&sc->ifmedia, 0, ep_ifmedia_upd, ep_ifmedia_sts);
303
304		if (sc->ep_connectors & AUI)
305			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
306		if (sc->ep_connectors & UTP)
307			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
308		if (sc->ep_connectors & BNC)
309			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
310		if (!sc->ep_connectors)
311			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
312
313		ifmedia_set(&sc->ifmedia, IFM_ETHER|ep_media2if_media[sc->ep_connector]);
314
315		ifm = &sc->ifmedia;
316		ifm->ifm_media = ifm->ifm_cur->ifm_media;
317		ep_ifmedia_upd(ifp);
318	}
319
320	if (!attached) {
321		if_attach(ifp);
322		ether_ifattach(ifp);
323		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
324	}
325
326#ifdef EP_LOCAL_STATS
327	sc->rx_no_first = sc->rx_no_mbuf = sc->rx_bpf_disc =
328		sc->rx_overrunf = sc->rx_overrunl = sc->tx_underrun = 0;
329#endif
330	EP_FSET(sc, F_RX_FIRST);
331	sc->top = sc->mcur = 0;
332
333	return 0;
334}
335
336/*
337 * The order in here seems important. Otherwise we may not receive
338 * interrupts. ?!
339 */
340static void
341ep_if_init(xsc)
342    void *xsc;
343{
344    struct ep_softc *sc = xsc;
345    register struct ifnet *ifp = &sc->arpcom.ac_if;
346    int s, i;
347
348    if (sc->gone)
349	return;
350
351	/*
352    if (ifp->if_addrlist == (struct ifaddr *) 0)
353	return;
354	*/
355
356    s = splimp();
357    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
358
359    GO_WINDOW(0);
360    outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
361    GO_WINDOW(4);
362    outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
363    GO_WINDOW(0);
364
365    /* Disable the card */
366    outw(BASE + EP_W0_CONFIG_CTRL, 0);
367
368    /* Enable the card */
369    outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
370
371    GO_WINDOW(2);
372
373    /* Reload the ether_addr. */
374    for (i = 0; i < 6; i++)
375	outb(BASE + EP_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
376
377    outw(BASE + EP_COMMAND, RX_RESET);
378    outw(BASE + EP_COMMAND, TX_RESET);
379    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
380
381    /* Window 1 is operating window */
382    GO_WINDOW(1);
383    for (i = 0; i < 31; i++)
384	inb(BASE + EP_W1_TX_STATUS);
385
386    /* get rid of stray intr's */
387    outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
388
389    outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
390
391    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
392
393    if (ifp->if_flags & IFF_PROMISC)
394	outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
395	 FIL_GROUP | FIL_BRDCST | FIL_ALL);
396    else
397	outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
398	 FIL_GROUP | FIL_BRDCST);
399
400    ep_ifmedia_upd(ifp);
401
402    outw(BASE + EP_COMMAND, RX_ENABLE);
403    outw(BASE + EP_COMMAND, TX_ENABLE);
404
405    ifp->if_flags |= IFF_RUNNING;
406    ifp->if_flags &= ~IFF_OACTIVE;	/* just in case */
407
408#ifdef EP_LOCAL_STATS
409    sc->rx_no_first = sc->rx_no_mbuf =
410	sc->rx_bpf_disc = sc->rx_overrunf = sc->rx_overrunl =
411	sc->tx_underrun = 0;
412#endif
413    EP_FSET(sc, F_RX_FIRST);
414    if (sc->top) {
415	m_freem(sc->top);
416	sc->top = sc->mcur = 0;
417    }
418    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
419    outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
420
421    /*
422     * Store up a bunch of mbuf's for use later. (MAX_MBS). First we free up
423     * any that we had in case we're being called from intr or somewhere
424     * else.
425     */
426
427    GO_WINDOW(1);
428    ep_if_start(ifp);
429
430    splx(s);
431}
432
433static const char padmap[] = {0, 3, 2, 1};
434
435static void
436ep_if_start(ifp)
437    struct ifnet *ifp;
438{
439    register struct ep_softc *sc = ifp->if_softc;
440    register u_int len;
441    register struct mbuf *m;
442    struct mbuf *top;
443    int s, pad;
444
445    if (sc->gone) {
446	return;
447    }
448
449    s = splimp();
450    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
451    if (ifp->if_flags & IFF_OACTIVE) {
452	splx(s);
453	return;
454    }
455startagain:
456    /* Sneak a peek at the next packet */
457    m = ifp->if_snd.ifq_head;
458    if (m == 0) {
459	splx(s);
460	return;
461    }
462    for (len = 0, top = m; m; m = m->m_next)
463	len += m->m_len;
464
465    pad = padmap[len & 3];
466
467    /*
468     * The 3c509 automatically pads short packets to minimum ethernet length,
469     * but we drop packets that are too large. Perhaps we should truncate
470     * them instead?
471     */
472    if (len + pad > ETHER_MAX_LEN) {
473	/* packet is obviously too large: toss it */
474	++ifp->if_oerrors;
475	IF_DEQUEUE(&ifp->if_snd, m);
476	m_freem(m);
477	goto readcheck;
478    }
479    if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
480	/* no room in FIFO */
481	outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | (len + pad + 4));
482	/* make sure */
483	if (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
484	    ifp->if_flags |= IFF_OACTIVE;
485	    splx(s);
486	    return;
487	}
488    }
489    IF_DEQUEUE(&ifp->if_snd, m);
490
491    outw(BASE + EP_W1_TX_PIO_WR_1, len);
492    outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);	/* Second dword meaningless */
493
494    for (top = m; m != 0; m = m->m_next)
495	if (EP_FTST(sc, F_ACCESS_32_BITS)) {
496	    outsl(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t),
497		  m->m_len / 4);
498	    if (m->m_len & 3)
499		outsb(BASE + EP_W1_TX_PIO_WR_1,
500		      mtod(m, caddr_t) + (m->m_len & (~3)),
501		      m->m_len & 3);
502	} else {
503	    outsw(BASE + EP_W1_TX_PIO_WR_1, mtod(m, caddr_t), m->m_len / 2);
504	    if (m->m_len & 1)
505		outb(BASE + EP_W1_TX_PIO_WR_1,
506		     *(mtod(m, caddr_t) + m->m_len - 1));
507	}
508
509    while (pad--)
510	outb(BASE + EP_W1_TX_PIO_WR_1, 0);	/* Padding */
511
512    if (ifp->if_bpf) {
513	bpf_mtap(ifp, top);
514    }
515
516    ifp->if_timer = 2;
517    ifp->if_opackets++;
518    m_freem(top);
519
520    /*
521     * Is another packet coming in? We don't want to overflow the tiny RX
522     * fifo.
523     */
524readcheck:
525    if (inw(BASE + EP_W1_RX_STATUS) & RX_BYTES_MASK) {
526	/*
527	 * we check if we have packets left, in that case we prepare to come
528	 * back later
529	 */
530	if (ifp->if_snd.ifq_head) {
531	    outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
532	}
533	splx(s);
534	return;
535    }
536    goto startagain;
537}
538
539void
540ep_intr(arg)
541    void *arg;
542{
543    struct ep_softc *sc;
544    register int status;
545    struct ifnet *ifp;
546    int x;
547
548    x = splbio();
549
550    sc = (struct ep_softc *)arg;
551
552    if (sc->gone)
553	    return;
554
555    ifp = &sc->arpcom.ac_if;
556
557    outw(BASE + EP_COMMAND, SET_INTR_MASK); /* disable all Ints */
558
559rescan:
560
561    while ((status = inw(BASE + EP_STATUS)) & S_5_INTS) {
562
563	/* first acknowledge all interrupt sources */
564	outw(BASE + EP_COMMAND, ACK_INTR | (status & S_MASK));
565
566	if (status & (S_RX_COMPLETE | S_RX_EARLY)) {
567	    epread(sc);
568	    continue;
569	}
570	if (status & S_TX_AVAIL) {
571	    /* we need ACK */
572	    ifp->if_timer = 0;
573	    ifp->if_flags &= ~IFF_OACTIVE;
574	    GO_WINDOW(1);
575	    inw(BASE + EP_W1_FREE_TX);
576	    ep_if_start(ifp);
577	}
578	if (status & S_CARD_FAILURE) {
579	    ifp->if_timer = 0;
580#ifdef EP_LOCAL_STATS
581	    printf("\nep%d:\n\tStatus: %x\n", sc->unit, status);
582	    GO_WINDOW(4);
583	    printf("\tFIFO Diagnostic: %x\n", inw(BASE + EP_W4_FIFO_DIAG));
584	    printf("\tStat: %x\n", sc->stat);
585	    printf("\tIpackets=%d, Opackets=%d\n",
586		ifp->if_ipackets, ifp->if_opackets);
587	    printf("\tNOF=%d, NOMB=%d, BPFD=%d, RXOF=%d, RXOL=%d, TXU=%d\n",
588		   sc->rx_no_first, sc->rx_no_mbuf, sc->rx_bpf_disc, sc->rx_overrunf,
589		   sc->rx_overrunl, sc->tx_underrun);
590#else
591
592#ifdef DIAGNOSTIC
593	    printf("ep%d: Status: %x (input buffer overflow)\n", sc->unit, status);
594#else
595	    ++ifp->if_ierrors;
596#endif
597
598#endif
599	    ep_if_init(sc);
600	    splx(x);
601	    return;
602	}
603	if (status & S_TX_COMPLETE) {
604	    ifp->if_timer = 0;
605	    /* we  need ACK. we do it at the end */
606	    /*
607	     * We need to read TX_STATUS until we get a 0 status in order to
608	     * turn off the interrupt flag.
609	     */
610	    while ((status = inb(BASE + EP_W1_TX_STATUS)) & TXS_COMPLETE) {
611		if (status & TXS_SUCCES_INTR_REQ);
612		else if (status & (TXS_UNDERRUN | TXS_JABBER | TXS_MAX_COLLISION)) {
613		    outw(BASE + EP_COMMAND, TX_RESET);
614		    if (status & TXS_UNDERRUN) {
615#ifdef EP_LOCAL_STATS
616			sc->tx_underrun++;
617#endif
618		    } else {
619			if (status & TXS_JABBER);
620			else	/* TXS_MAX_COLLISION - we shouldn't get here */
621			    ++ifp->if_collisions;
622		    }
623		    ++ifp->if_oerrors;
624		    outw(BASE + EP_COMMAND, TX_ENABLE);
625		    /*
626		     * To have a tx_avail_int but giving the chance to the
627		     * Reception
628		     */
629		    if (ifp->if_snd.ifq_head) {
630			outw(BASE + EP_COMMAND, SET_TX_AVAIL_THRESH | 8);
631		    }
632		}
633		outb(BASE + EP_W1_TX_STATUS, 0x0);	/* pops up the next
634							 * status */
635	    }			/* while */
636	    ifp->if_flags &= ~IFF_OACTIVE;
637	    GO_WINDOW(1);
638	    inw(BASE + EP_W1_FREE_TX);
639	    ep_if_start(ifp);
640	}			/* end TX_COMPLETE */
641    }
642
643    outw(BASE + EP_COMMAND, C_INTR_LATCH);	/* ACK int Latch */
644
645    if ((status = inw(BASE + EP_STATUS)) & S_5_INTS)
646	goto rescan;
647
648    /* re-enable Ints */
649    outw(BASE + EP_COMMAND, SET_INTR_MASK | S_5_INTS);
650
651    splx(x);
652}
653
654static void
655epread(sc)
656    register struct ep_softc *sc;
657{
658    struct ether_header *eh;
659    struct mbuf *top, *mcur, *m;
660    struct ifnet *ifp;
661    int lenthisone;
662
663    short rx_fifo2, status;
664    register short rx_fifo;
665
666    ifp = &sc->arpcom.ac_if;
667    status = inw(BASE + EP_W1_RX_STATUS);
668
669read_again:
670
671    if (status & ERR_RX) {
672	++ifp->if_ierrors;
673	if (status & ERR_RX_OVERRUN) {
674	    /*
675	     * we can think the rx latency is actually greather than we
676	     * expect
677	     */
678#ifdef EP_LOCAL_STATS
679	    if (EP_FTST(sc, F_RX_FIRST))
680		sc->rx_overrunf++;
681	    else
682		sc->rx_overrunl++;
683#endif
684	}
685	goto out;
686    }
687    rx_fifo = rx_fifo2 = status & RX_BYTES_MASK;
688
689    if (EP_FTST(sc, F_RX_FIRST)) {
690	MGETHDR(m, M_DONTWAIT, MT_DATA);
691	if (!m)
692	    goto out;
693	if (rx_fifo >= MINCLSIZE)
694	    MCLGET(m, M_DONTWAIT);
695	sc->top = sc->mcur = top = m;
696#define EROUND  ((sizeof(struct ether_header) + 3) & ~3)
697#define EOFF    (EROUND - sizeof(struct ether_header))
698	top->m_data += EOFF;
699
700	/* Read what should be the header. */
701	insw(BASE + EP_W1_RX_PIO_RD_1,
702	     mtod(top, caddr_t), sizeof(struct ether_header) / 2);
703	top->m_len = sizeof(struct ether_header);
704	rx_fifo -= sizeof(struct ether_header);
705	sc->cur_len = rx_fifo2;
706    } else {
707	/* come here if we didn't have a complete packet last time */
708	top = sc->top;
709	m = sc->mcur;
710	sc->cur_len += rx_fifo2;
711    }
712
713    /* Reads what is left in the RX FIFO */
714    while (rx_fifo > 0) {
715	lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
716	if (lenthisone == 0) {	/* no room in this one */
717	    mcur = m;
718	    MGET(m, M_DONTWAIT, MT_DATA);
719	    if (!m)
720		goto out;
721	    if (rx_fifo >= MINCLSIZE)
722		MCLGET(m, M_DONTWAIT);
723	    m->m_len = 0;
724	    mcur->m_next = m;
725	    lenthisone = min(rx_fifo, M_TRAILINGSPACE(m));
726	}
727	if (EP_FTST(sc, F_ACCESS_32_BITS)) { /* default for EISA configured cards*/
728	    insl(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
729		 lenthisone / 4);
730	    m->m_len += (lenthisone & ~3);
731	    if (lenthisone & 3)
732		insb(BASE + EP_W1_RX_PIO_RD_1,
733		     mtod(m, caddr_t) + m->m_len,
734		     lenthisone & 3);
735	    m->m_len += (lenthisone & 3);
736	} else {
737	    insw(BASE + EP_W1_RX_PIO_RD_1, mtod(m, caddr_t) + m->m_len,
738		 lenthisone / 2);
739	    m->m_len += lenthisone;
740	    if (lenthisone & 1)
741		*(mtod(m, caddr_t) + m->m_len - 1) = inb(BASE + EP_W1_RX_PIO_RD_1);
742	}
743	rx_fifo -= lenthisone;
744    }
745
746    if (status & ERR_RX_INCOMPLETE) {	/* we haven't received the complete
747					 * packet */
748	sc->mcur = m;
749#ifdef EP_LOCAL_STATS
750	sc->rx_no_first++;	/* to know how often we come here */
751#endif
752	EP_FRST(sc, F_RX_FIRST);
753	if (!((status = inw(BASE + EP_W1_RX_STATUS)) & ERR_RX_INCOMPLETE)) {
754	    /* we see if by now, the packet has completly arrived */
755	    goto read_again;
756	}
757	outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_NEXT_EARLY_THRESH);
758	return;
759    }
760    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
761    ++ifp->if_ipackets;
762    EP_FSET(sc, F_RX_FIRST);
763    top->m_pkthdr.rcvif = &sc->arpcom.ac_if;
764    top->m_pkthdr.len = sc->cur_len;
765
766    if (ifp->if_bpf) {
767	bpf_mtap(ifp, top);
768
769	/*
770	 * Note that the interface cannot be in promiscuous mode if there are
771	 * no BPF listeners.  And if we are in promiscuous mode, we have to
772	 * check if this packet is really ours.
773	 */
774	eh = mtod(top, struct ether_header *);
775	if ((ifp->if_flags & IFF_PROMISC) &&
776	    (eh->ether_dhost[0] & 1) == 0 &&
777	    bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
778		 sizeof(eh->ether_dhost)) != 0 &&
779	    bcmp(eh->ether_dhost, etherbroadcastaddr,
780		 sizeof(eh->ether_dhost)) != 0) {
781	    if (sc->top) {
782		m_freem(sc->top);
783		sc->top = 0;
784	    }
785	    EP_FSET(sc, F_RX_FIRST);
786#ifdef EP_LOCAL_STATS
787	    sc->rx_bpf_disc++;
788#endif
789	    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
790	    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
791	    return;
792	}
793    }
794
795    eh = mtod(top, struct ether_header *);
796    m_adj(top, sizeof(struct ether_header));
797    ether_input(ifp, eh, top);
798    sc->top = 0;
799    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
800    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
801    return;
802
803out:
804    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
805    if (sc->top) {
806	m_freem(sc->top);
807	sc->top = 0;
808#ifdef EP_LOCAL_STATS
809	sc->rx_no_mbuf++;
810#endif
811    }
812    EP_FSET(sc, F_RX_FIRST);
813    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
814    outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | RX_INIT_EARLY_THRESH);
815}
816
817static int
818ep_ifmedia_upd(ifp)
819	struct ifnet *		ifp;
820{
821	struct ep_softc *	sc = ifp->if_softc;
822	int			i = 0, j;
823
824	GO_WINDOW(0);
825	outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
826	GO_WINDOW(4);
827	outw(BASE + EP_W4_MEDIA_TYPE, DISABLE_UTP);
828	GO_WINDOW(0);
829
830	switch (IFM_SUBTYPE(sc->ifmedia.ifm_media)) {
831		case IFM_10_T:
832			if (sc->ep_connectors & UTP) {
833				i = ACF_CONNECTOR_UTP;
834				GO_WINDOW(4);
835				outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
836			}
837			break;
838		case IFM_10_2:
839			if (sc->ep_connectors & BNC) {
840				i = ACF_CONNECTOR_BNC;
841				outw(BASE + EP_COMMAND, START_TRANSCEIVER);
842				DELAY(DELAY_MULTIPLE * 1000);
843			}
844			break;
845		case IFM_10_5:
846			if (sc->ep_connectors & AUI)
847				i = ACF_CONNECTOR_AUI;
848			break;
849		default:
850			i = sc->ep_connector;
851			device_printf(sc->dev,
852				"strange connector type in EEPROM: assuming AUI\n");
853	}
854
855	GO_WINDOW(0);
856	j = inw(BASE + EP_W0_ADDRESS_CFG) & 0x3fff;
857	outw(BASE + EP_W0_ADDRESS_CFG, j | (i << ACF_CONNECTOR_BITS));
858
859	return (0);
860}
861
862static void
863ep_ifmedia_sts(ifp, ifmr)
864	struct ifnet *		ifp;
865	struct ifmediareq *	ifmr;
866{
867	struct ep_softc *	sc = ifp->if_softc;
868
869	ifmr->ifm_active = sc->ifmedia.ifm_media;
870
871	return;
872}
873
874static int
875ep_if_ioctl(ifp, cmd, data)
876	struct ifnet *		ifp;
877	u_long			cmd;
878	caddr_t			data;
879{
880	struct ep_softc *	sc = ifp->if_softc;
881	struct ifreq *		ifr = (struct ifreq *)data;
882	int s, error = 0;
883
884	s = splimp();
885
886	switch (cmd) {
887	case SIOCSIFADDR:
888	case SIOCGIFADDR:
889	case SIOCSIFMTU:
890		error = ether_ioctl(ifp, cmd, data);
891	break;
892
893	case SIOCSIFFLAGS:
894		if (((ifp->if_flags & IFF_UP) == 0) &&
895		    (ifp->if_flags & IFF_RUNNING)) {
896			ifp->if_flags &= ~IFF_RUNNING;
897			epstop(sc);
898		} else {
899			/* reinitialize card on any parameter change */
900			ep_if_init(sc);
901		}
902		break;
903#ifdef notdef
904	case SIOCGHWADDR:
905		bcopy((caddr_t) sc->sc_addr, (caddr_t) & ifr->ifr_data,
906		      sizeof(sc->sc_addr));
907		break;
908#endif
909	case SIOCADDMULTI:
910	case SIOCDELMULTI:
911		/*
912		 * The Etherlink III has no programmable multicast
913		 * filter.  We always initialize the card to be
914		 * promiscuous to multicast, since we're always a
915		 * member of the ALL-SYSTEMS group, so there's no
916		 * need to process SIOC*MULTI requests.
917		 */
918		error = 0;
919		break;
920	case SIOCSIFMEDIA:
921	case SIOCGIFMEDIA:
922		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
923		break;
924	default:
925		error = EINVAL;
926		break;
927	}
928
929	(void)splx(s);
930
931	return (error);
932}
933
934static void
935ep_if_watchdog(ifp)
936    struct ifnet *ifp;
937{
938    struct ep_softc *sc = ifp->if_softc;
939
940    /*
941    printf("ep: watchdog\n");
942
943    log(LOG_ERR, "ep%d: watchdog\n", ifp->if_unit);
944    ifp->if_oerrors++;
945    */
946
947    if (sc->gone) {
948	return;
949    }
950
951    ifp->if_flags &= ~IFF_OACTIVE;
952    ep_if_start(ifp);
953    ep_intr(ifp->if_softc);
954}
955
956static void
957epstop(sc)
958    struct ep_softc *sc;
959{
960    if (sc->gone) {
961	return;
962    }
963
964    outw(BASE + EP_COMMAND, RX_DISABLE);
965    outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
966    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
967    outw(BASE + EP_COMMAND, TX_DISABLE);
968    outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
969    outw(BASE + EP_COMMAND, RX_RESET);
970    outw(BASE + EP_COMMAND, TX_RESET);
971    while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
972    outw(BASE + EP_COMMAND, C_INTR_LATCH);
973    outw(BASE + EP_COMMAND, SET_RD_0_MASK);
974    outw(BASE + EP_COMMAND, SET_INTR_MASK);
975    outw(BASE + EP_COMMAND, SET_RX_FILTER);
976}
977