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