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