if_ex.c revision 182088
1139749Simp/*-
221769Sjkh * Copyright (c) 1996, Javier Mart�n Rueda (jmrueda@diatel.upm.es)
321769Sjkh * All rights reserved.
421769Sjkh *
521769Sjkh * Redistribution and use in source and binary forms, with or without
621769Sjkh * modification, are permitted provided that the following conditions
721769Sjkh * are met:
821769Sjkh * 1. Redistributions of source code must retain the above copyright
921769Sjkh *    notice unmodified, this list of conditions, and the following
1021769Sjkh *    disclaimer.
1121769Sjkh * 2. Redistributions in binary form must reproduce the above copyright
1221769Sjkh *    notice, this list of conditions and the following disclaimer in the
1321769Sjkh *    documentation and/or other materials provided with the distribution.
1421769Sjkh *
1521769Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1621769Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1721769Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1821769Sjkh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1921769Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2021769Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2121769Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2221769Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2321769Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2421769Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2521769Sjkh * SUCH DAMAGE.
2629877Smsmith *
2752286Smdodd *
2852286Smdodd * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
2952286Smdodd *                             <mdodd@FreeBSD.org>
3021769Sjkh */
3121769Sjkh
32119418Sobrien#include <sys/cdefs.h>
33119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/ex/if_ex.c 182088 2008-08-24 00:22:42Z imp $");
34119418Sobrien
3521769Sjkh/*
3629877Smsmith * Intel EtherExpress Pro/10, Pro/10+ Ethernet driver
3721769Sjkh *
3821769Sjkh * Revision history:
3921769Sjkh *
40112731Smdodd * dd-mmm-yyyy: Multicast support ported from NetBSD's if_iy driver.
4121769Sjkh * 30-Oct-1996: first beta version. Inet and BPF supported, but no multicast.
4221769Sjkh */
4321769Sjkh
4421769Sjkh#include <sys/param.h>
4521769Sjkh#include <sys/systm.h>
4652286Smdodd#include <sys/kernel.h>
4724204Sbde#include <sys/sockio.h>
4821769Sjkh#include <sys/mbuf.h>
4921769Sjkh#include <sys/socket.h>
5021769Sjkh
5152286Smdodd#include <sys/module.h>
5252286Smdodd#include <sys/bus.h>
5352286Smdodd
5452286Smdodd#include <machine/bus.h>
5552286Smdodd#include <machine/resource.h>
5652286Smdodd#include <sys/rman.h>
5752286Smdodd
5857987Smdodd#include <net/if.h>
5957987Smdodd#include <net/if_arp.h>
60112731Smdodd#include <net/if_dl.h>
6157987Smdodd#include <net/if_media.h>
62147256Sbrooks#include <net/if_types.h>
6350026Smdodd#include <net/ethernet.h>
6457987Smdodd#include <net/bpf.h>
6521769Sjkh
6650026Smdodd#include <netinet/in.h>
6750026Smdodd#include <netinet/if_ether.h>
6850026Smdodd
6921769Sjkh
7052286Smdodd#include <isa/isavar.h>
7152286Smdodd#include <isa/pnpvar.h>
7252286Smdodd
7355953Speter#include <dev/ex/if_exreg.h>
7459816Smdodd#include <dev/ex/if_exvar.h>
7521769Sjkh
7621769Sjkh#ifdef EXDEBUG
7752286Smdodd# define Start_End 1
7852286Smdodd# define Rcvd_Pkts 2
7952286Smdodd# define Sent_Pkts 4
8052286Smdodd# define Status    8
8121769Sjkhstatic int debug_mask = 0;
8252286Smdodd# define DODEBUG(level, action) if (level & debug_mask) action
8321769Sjkh#else
8452286Smdodd# define DODEBUG(level, action)
8521769Sjkh#endif
8621769Sjkh
87112801Smdodddevclass_t ex_devclass;
88112801Smdodd
8959816Smdoddchar irq2eemap[] =
9052286Smdodd	{ -1, -1, 0, 1, -1, 2, -1, -1, -1, 0, 3, 4, -1, -1, -1, -1 };
9159816Smdoddu_char ee2irqmap[] =
9252286Smdodd	{ 9, 3, 5, 10, 11, 0, 0, 0 };
9359816Smdodd
9459816Smdoddchar plus_irq2eemap[] =
9552286Smdodd	{ -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, -1, -1, -1 };
9659816Smdoddu_char plus_ee2irqmap[] =
9752286Smdodd	{ 3, 4, 5, 7, 9, 10, 11, 12 };
9821769Sjkh
9952286Smdodd/* Network Interface Functions */
100131192Simpstatic void	ex_init(void *);
101179775Sjhbstatic void	ex_init_locked(struct ex_softc *);
102131192Simpstatic void	ex_start(struct ifnet *);
103179775Sjhbstatic void	ex_start_locked(struct ifnet *);
104131192Simpstatic int	ex_ioctl(struct ifnet *, u_long, caddr_t);
105179775Sjhbstatic void	ex_watchdog(void *);
10621769Sjkh
10757987Smdodd/* ifmedia Functions	*/
108131192Simpstatic int	ex_ifmedia_upd(struct ifnet *);
109131192Simpstatic void	ex_ifmedia_sts(struct ifnet *, struct ifmediareq *);
11057987Smdodd
111131192Simpstatic int	ex_get_media(struct ex_softc *);
11259816Smdodd
113131192Simpstatic void	ex_reset(struct ex_softc *);
114131192Simpstatic void	ex_setmulti(struct ex_softc *);
11552286Smdodd
116131192Simpstatic void	ex_tx_intr(struct ex_softc *);
117131192Simpstatic void	ex_rx_intr(struct ex_softc *);
11852286Smdodd
11959816Smdoddvoid
120131192Simpex_get_address(struct ex_softc *sc, u_char *enaddr)
12121769Sjkh{
122131192Simp	uint16_t	eaddr_tmp;
12321769Sjkh
124131192Simp	eaddr_tmp = ex_eeprom_read(sc, EE_Eth_Addr_Lo);
12552286Smdodd	enaddr[5] = eaddr_tmp & 0xff;
12652286Smdodd	enaddr[4] = eaddr_tmp >> 8;
127131192Simp	eaddr_tmp = ex_eeprom_read(sc, EE_Eth_Addr_Mid);
12852286Smdodd	enaddr[3] = eaddr_tmp & 0xff;
12952286Smdodd	enaddr[2] = eaddr_tmp >> 8;
130131192Simp	eaddr_tmp = ex_eeprom_read(sc, EE_Eth_Addr_Hi);
13152286Smdodd	enaddr[1] = eaddr_tmp & 0xff;
13252286Smdodd	enaddr[0] = eaddr_tmp >> 8;
13352286Smdodd
13452286Smdodd	return;
13552286Smdodd}
13621769Sjkh
13759816Smdoddint
138131192Simpex_card_type(u_char *enaddr)
13952286Smdodd{
14052286Smdodd	if ((enaddr[0] == 0x00) && (enaddr[1] == 0xA0) && (enaddr[2] == 0xC9))
14152286Smdodd		return (CARD_TYPE_EX_10_PLUS);
14252286Smdodd
14352286Smdodd	return (CARD_TYPE_EX_10);
14452286Smdodd}
14552286Smdodd
14655882Smdodd/*
14759816Smdodd * Caller is responsible for eventually calling
14859816Smdodd * ex_release_resources() on failure.
14955882Smdodd */
15059816Smdoddint
151131192Simpex_alloc_resources(device_t dev)
15255882Smdodd{
15359816Smdodd	struct ex_softc *	sc = device_get_softc(dev);
15459816Smdodd	int			error = 0;
15555882Smdodd
156127135Snjl	sc->ioport = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
157127135Snjl					    &sc->ioport_rid, RF_ACTIVE);
15859816Smdodd	if (!sc->ioport) {
15959816Smdodd		device_printf(dev, "No I/O space?!\n");
16059816Smdodd		error = ENOMEM;
16159816Smdodd		goto bad;
16259816Smdodd	}
16357989Smdodd
164127135Snjl	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
165127135Snjl					RF_ACTIVE);
16655882Smdodd
16759816Smdodd	if (!sc->irq) {
16859816Smdodd		device_printf(dev, "No IRQ?!\n");
16959816Smdodd		error = ENOMEM;
17059816Smdodd		goto bad;
17155882Smdodd	}
17255882Smdodd
17359816Smdoddbad:
17459816Smdodd	return (error);
17555882Smdodd}
17655882Smdodd
17759816Smdoddvoid
178131192Simpex_release_resources(device_t dev)
17952286Smdodd{
18059816Smdodd	struct ex_softc *	sc = device_get_softc(dev);
18152286Smdodd
18259816Smdodd	if (sc->ih) {
18359816Smdodd		bus_teardown_intr(dev, sc->irq, sc->ih);
18459816Smdodd		sc->ih = NULL;
18552286Smdodd	}
18652286Smdodd
18759816Smdodd	if (sc->ioport) {
18859816Smdodd		bus_release_resource(dev, SYS_RES_IOPORT,
18959816Smdodd					sc->ioport_rid, sc->ioport);
19059816Smdodd		sc->ioport = NULL;
19152286Smdodd	}
19252286Smdodd
19359816Smdodd	if (sc->irq) {
19459816Smdodd		bus_release_resource(dev, SYS_RES_IRQ,
19559816Smdodd					sc->irq_rid, sc->irq);
19659816Smdodd		sc->irq = NULL;
19757989Smdodd	}
19857989Smdodd
199150215Sru	if (sc->ifp)
200150215Sru		if_free(sc->ifp);
201150215Sru
20259816Smdodd	return;
20352286Smdodd}
20452286Smdodd
20559816Smdoddint
20659816Smdoddex_attach(device_t dev)
20752286Smdodd{
20852286Smdodd	struct ex_softc *	sc = device_get_softc(dev);
209147256Sbrooks	struct ifnet *		ifp;
21057987Smdodd	struct ifmedia *	ifm;
211179775Sjhb	int			error;
212131192Simp	uint16_t		temp;
21352286Smdodd
214147256Sbrooks	ifp = sc->ifp = if_alloc(IFT_ETHER);
215147256Sbrooks	if (ifp == NULL) {
216147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
217147256Sbrooks		return (ENOSPC);
218147256Sbrooks	}
21929877Smsmith	/* work out which set of irq <-> internal tables to use */
220147256Sbrooks	if (ex_card_type(sc->enaddr) == CARD_TYPE_EX_10_PLUS) {
22129877Smsmith		sc->irq2ee = plus_irq2eemap;
22229877Smsmith		sc->ee2irq = plus_ee2irqmap;
22352286Smdodd	} else {
22429877Smsmith		sc->irq2ee = irq2eemap;
22529877Smsmith		sc->ee2irq = ee2irqmap;
22629877Smsmith	}
22729877Smsmith
22821769Sjkh	sc->mem_size = CARD_RAM_SIZE;	/* XXX This should be read from the card itself. */
22921769Sjkh
23021769Sjkh	/*
23121769Sjkh	 * Initialize the ifnet structure.
23221769Sjkh	 */
23321769Sjkh	ifp->if_softc = sc;
234121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23555883Smdodd	ifp->if_mtu = ETHERMTU;
236179775Sjhb	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
23721769Sjkh	ifp->if_start = ex_start;
23821769Sjkh	ifp->if_ioctl = ex_ioctl;
23955883Smdodd	ifp->if_init = ex_init;
240179775Sjhb	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
24121769Sjkh
24257987Smdodd	ifmedia_init(&sc->ifmedia, 0, ex_ifmedia_upd, ex_ifmedia_sts);
243179775Sjhb	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
244179775Sjhb	    MTX_DEF);
245179775Sjhb	callout_init_mtx(&sc->timer, &sc->lock, 0);
24657987Smdodd
247131192Simp	temp = ex_eeprom_read(sc, EE_W5);
24857987Smdodd	if (temp & EE_W5_PORT_TPE)
24957987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
25057987Smdodd	if (temp & EE_W5_PORT_BNC)
25157987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
25257987Smdodd	if (temp & EE_W5_PORT_AUI)
25357987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
25457987Smdodd
255112764Smdodd	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
256112764Smdodd	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
257131192Simp	ifmedia_set(&sc->ifmedia, ex_get_media(sc));
25857987Smdodd
25957987Smdodd	ifm = &sc->ifmedia;
260179775Sjhb	ifm->ifm_media = ifm->ifm_cur->ifm_media;
26157987Smdodd	ex_ifmedia_upd(ifp);
26257987Smdodd
26321769Sjkh	/*
26421769Sjkh	 * Attach the interface.
26521769Sjkh	 */
266147256Sbrooks	ether_ifattach(ifp, sc->enaddr);
26721769Sjkh
268179775Sjhb	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
269179775Sjhb				NULL, ex_intr, (void *)sc, &sc->ih);
270179775Sjhb	if (error) {
271179775Sjhb		device_printf(dev, "bus_setup_intr() failed!\n");
272179775Sjhb		ether_ifdetach(ifp);
273179775Sjhb		mtx_destroy(&sc->lock);
274179775Sjhb		return (error);
275179775Sjhb	}
276179775Sjhb
27752286Smdodd	return(0);
27821769Sjkh}
27921769Sjkh
280112800Smdoddint
281131192Simpex_detach(device_t dev)
282112800Smdodd{
283112800Smdodd	struct ex_softc	*sc;
284112800Smdodd	struct ifnet	*ifp;
285112800Smdodd
286112800Smdodd	sc = device_get_softc(dev);
287147256Sbrooks	ifp = sc->ifp;
288112800Smdodd
289179775Sjhb	EX_LOCK(sc);
290112800Smdodd        ex_stop(sc);
291179775Sjhb	EX_UNLOCK(sc);
292112800Smdodd
293112800Smdodd	ether_ifdetach(ifp);
294179775Sjhb	callout_drain(&sc->timer);
295112800Smdodd
296112800Smdodd	ex_release_resources(dev);
297179775Sjhb	mtx_destroy(&sc->lock);
298112800Smdodd
299112800Smdodd	return (0);
300112800Smdodd}
301112800Smdodd
30255883Smdoddstatic void
30355883Smdoddex_init(void *xsc)
30421769Sjkh{
30552286Smdodd	struct ex_softc *	sc = (struct ex_softc *) xsc;
306179775Sjhb
307179775Sjhb	EX_LOCK(sc);
308179775Sjhb	ex_init_locked(sc);
309179775Sjhb	EX_UNLOCK(sc);
310179775Sjhb}
311179775Sjhb
312179775Sjhbstatic void
313179775Sjhbex_init_locked(struct ex_softc *sc)
314179775Sjhb{
315147256Sbrooks	struct ifnet *		ifp = sc->ifp;
31652286Smdodd	int			i;
31752286Smdodd	unsigned short		temp_reg;
31821769Sjkh
319121816Sbrooks	DODEBUG(Start_End, printf("%s: ex_init: start\n", ifp->if_xname););
32021769Sjkh
321179775Sjhb	sc->tx_timeout = 0;
32221769Sjkh
32321769Sjkh	/*
32421769Sjkh	 * Load the ethernet address into the card.
32521769Sjkh	 */
326131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
327131192Simp	temp_reg = CSR_READ_1(sc, EEPROM_REG);
328182088Simp	if (temp_reg & Trnoff_Enable)
329131192Simp		CSR_WRITE_1(sc, EEPROM_REG, temp_reg & ~Trnoff_Enable);
330182088Simp	for (i = 0; i < ETHER_ADDR_LEN; i++)
331152315Sru		CSR_WRITE_1(sc, I_ADDR_REG0 + i, IF_LLADDR(sc->ifp)[i]);
332182088Simp
33321769Sjkh	/*
33421769Sjkh	 * - Setup transmit chaining and discard bad received frames.
33521769Sjkh	 * - Match broadcast.
33621769Sjkh	 * - Clear test mode.
33721769Sjkh	 * - Set receiving mode.
33821769Sjkh	 */
339131192Simp	CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
340131192Simp	CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | No_SA_Ins | RX_CRC_InMem);
341131192Simp	CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3) & 0x3f /* XXX constants. */ );
342182088Simp	/*
343182088Simp	 * - Set IRQ number, if this part has it.  ISA devices have this,
344182088Simp	 * while PC Card devices don't seem to.  Either way, we have to
345182088Simp	 * switch to Bank1 as the rest of this code relies on that.
346182088Simp	 */
347131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank1_Sel);
348182088Simp	if (sc->flags & HAS_INT_NO_REG)
349182088Simp		CSR_WRITE_1(sc, INT_NO_REG,
350182088Simp		    (CSR_READ_1(sc, INT_NO_REG) & 0xf8) |
351182088Simp		    sc->irq2ee[sc->irq_no]);
35221769Sjkh
35321769Sjkh	/*
35421769Sjkh	 * Divide the available memory in the card into rcv and xmt buffers.
35521769Sjkh	 * By default, I use the first 3/4 of the memory for the rcv buffer,
35621769Sjkh	 * and the remaining 1/4 of the memory for the xmt buffer.
35721769Sjkh	 */
35821769Sjkh	sc->rx_mem_size = sc->mem_size * 3 / 4;
35921769Sjkh	sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
36021769Sjkh	sc->rx_lower_limit = 0x0000;
36121769Sjkh	sc->rx_upper_limit = sc->rx_mem_size - 2;
36221769Sjkh	sc->tx_lower_limit = sc->rx_mem_size;
36321769Sjkh	sc->tx_upper_limit = sc->mem_size - 2;
364131192Simp	CSR_WRITE_1(sc, RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
365131192Simp	CSR_WRITE_1(sc, RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
366131192Simp	CSR_WRITE_1(sc, XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
367131192Simp	CSR_WRITE_1(sc, XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
36821769Sjkh
36921769Sjkh	/*
37021769Sjkh	 * Enable receive and transmit interrupts, and clear any pending int.
37121769Sjkh	 */
372131192Simp	CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) | TriST_INT);
373131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
374131192Simp	CSR_WRITE_1(sc, MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
375131192Simp	CSR_WRITE_1(sc, STATUS_REG, All_Int);
37621769Sjkh
37721769Sjkh	/*
37821769Sjkh	 * Initialize receive and transmit ring buffers.
37921769Sjkh	 */
380131192Simp	CSR_WRITE_2(sc, RCV_BAR, sc->rx_lower_limit);
38121769Sjkh	sc->rx_head = sc->rx_lower_limit;
382131192Simp	CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
383131192Simp	CSR_WRITE_2(sc, XMT_BAR, sc->tx_lower_limit);
38421769Sjkh	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
38521769Sjkh
386148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
387148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
38821769Sjkh	DODEBUG(Status, printf("OIDLE init\n"););
389179775Sjhb	callout_reset(&sc->timer, hz, ex_watchdog, sc);
39021769Sjkh
391112731Smdodd	ex_setmulti(sc);
392112731Smdodd
39321769Sjkh	/*
39421769Sjkh	 * Final reset of the board, and enable operation.
39521769Sjkh	 */
396131192Simp	CSR_WRITE_1(sc, CMD_REG, Sel_Reset_CMD);
39721769Sjkh	DELAY(2);
398131192Simp	CSR_WRITE_1(sc, CMD_REG, Rcv_Enable_CMD);
39921769Sjkh
400179775Sjhb	ex_start_locked(ifp);
40121769Sjkh
402121816Sbrooks	DODEBUG(Start_End, printf("%s: ex_init: finish\n", ifp->if_xname););
40321769Sjkh}
40421769Sjkh
40555883Smdoddstatic void
40652286Smdoddex_start(struct ifnet *ifp)
40721769Sjkh{
40852286Smdodd	struct ex_softc *	sc = ifp->if_softc;
409179775Sjhb
410179775Sjhb	EX_LOCK(sc);
411179775Sjhb	ex_start_locked(ifp);
412179775Sjhb	EX_UNLOCK(sc);
413179775Sjhb}
414179775Sjhb
415179775Sjhbstatic void
416179775Sjhbex_start_locked(struct ifnet *ifp)
417179775Sjhb{
418179775Sjhb	struct ex_softc *	sc = ifp->if_softc;
419179775Sjhb	int			i, len, data_len, avail, dest, next;
42052286Smdodd	unsigned char		tmp16[2];
42152286Smdodd	struct mbuf *		opkt;
42252286Smdodd	struct mbuf *		m;
42321769Sjkh
42452286Smdodd	DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
42521769Sjkh
42652286Smdodd	/*
42752286Smdodd	 * Main loop: send outgoing packets to network card until there are no
42852286Smdodd	 * more packets left, or the card cannot accept any more yet.
42952286Smdodd	 */
43052286Smdodd	while (((opkt = ifp->if_snd.ifq_head) != NULL) &&
431148887Srwatson	       !(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
43221769Sjkh
43352286Smdodd		/*
43452286Smdodd		 * Ensure there is enough free transmit buffer space for
43552286Smdodd		 * this packet, including its header. Note: the header
43652286Smdodd		 * cannot wrap around the end of the transmit buffer and
43752286Smdodd		 * must be kept together, so we allow space for twice the
43852286Smdodd		 * length of the header, just in case.
43952286Smdodd		 */
44021769Sjkh
44152286Smdodd		for (len = 0, m = opkt; m != NULL; m = m->m_next) {
44252286Smdodd			len += m->m_len;
44352286Smdodd		}
44452286Smdodd
44552286Smdodd		data_len = len;
44652286Smdodd
44752286Smdodd		DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
44852286Smdodd
44952286Smdodd		if (len & 1) {
45052286Smdodd			len += XMT_HEADER_LEN + 1;
45152286Smdodd		} else {
45252286Smdodd			len += XMT_HEADER_LEN;
45352286Smdodd		}
45452286Smdodd
45552286Smdodd		if ((i = sc->tx_tail - sc->tx_head) >= 0) {
45652286Smdodd			avail = sc->tx_mem_size - i;
45752286Smdodd		} else {
45852286Smdodd			avail = -i;
45952286Smdodd		}
46052286Smdodd
46152286Smdodd		DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
46252286Smdodd
46352286Smdodd		if (avail >= len + XMT_HEADER_LEN) {
46452286Smdodd			IF_DEQUEUE(&ifp->if_snd, opkt);
46552286Smdodd
46621769Sjkh#ifdef EX_PSA_INTR
46752286Smdodd			/*
46852286Smdodd			 * Disable rx and tx interrupts, to avoid corruption
46952286Smdodd			 * of the host address register by interrupt service
47052286Smdodd			 * routines.
47152286Smdodd			 * XXX Is this necessary with splimp() enabled?
47252286Smdodd			 */
473131192Simp			CSR_WRITE_1(sc, MASK_REG, All_Int);
47421769Sjkh#endif
47521769Sjkh
47652286Smdodd			/*
47752286Smdodd			 * Compute the start and end addresses of this
47852286Smdodd			 * frame in the tx buffer.
47952286Smdodd			 */
48052286Smdodd			dest = sc->tx_tail;
48152286Smdodd			next = dest + len;
48221769Sjkh
48352286Smdodd			if (next > sc->tx_upper_limit) {
48452286Smdodd				if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
48552286Smdodd				    XMT_HEADER_LEN) {
48652286Smdodd					dest = sc->tx_lower_limit;
48752286Smdodd					next = dest + len;
48855881Smdodd				} else {
48955881Smdodd					next = sc->tx_lower_limit +
49055881Smdodd						next - sc->tx_upper_limit - 2;
49152286Smdodd				}
49252286Smdodd			}
49321769Sjkh
49452286Smdodd			/*
49552286Smdodd			 * Build the packet frame in the card's ring buffer.
49652286Smdodd			 */
49752286Smdodd			DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
49821769Sjkh
499131192Simp			CSR_WRITE_2(sc, HOST_ADDR_REG, dest);
500131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, Transmit_CMD);
501131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, 0);
502131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, next);
503131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, data_len);
50421769Sjkh
50552286Smdodd			/*
50652286Smdodd			 * Output the packet data to the card. Ensure all
50752286Smdodd			 * transfers are 16-bit wide, even if individual
50852286Smdodd			 * mbufs have odd length.
50952286Smdodd			 */
51052286Smdodd			for (m = opkt, i = 0; m != NULL; m = m->m_next) {
51152286Smdodd				DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
51252286Smdodd				if (i) {
51352286Smdodd					tmp16[1] = *(mtod(m, caddr_t));
514131192Simp					CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
515131192Simp					    (uint16_t *) tmp16, 1);
51652286Smdodd				}
517131192Simp				CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
518131192Simp				    (uint16_t *) (mtod(m, caddr_t) + i),
519131192Simp				    (m->m_len - i) / 2);
52052286Smdodd				if ((i = (m->m_len - i) & 1) != 0) {
52152286Smdodd					tmp16[0] = *(mtod(m, caddr_t) +
52252286Smdodd						   m->m_len - 1);
52352286Smdodd				}
52452286Smdodd			}
525131192Simp			if (i)
526131192Simp				CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
527131192Simp				    (uint16_t *) tmp16, 1);
52855881Smdodd			/*
52955881Smdodd			 * If there were other frames chained, update the
53055881Smdodd			 * chain in the last one.
53155881Smdodd			 */
53255881Smdodd			if (sc->tx_head != sc->tx_tail) {
53355881Smdodd				if (sc->tx_tail != dest) {
534131192Simp					CSR_WRITE_2(sc, HOST_ADDR_REG,
53555881Smdodd					     sc->tx_last + XMT_Chain_Point);
536131192Simp					CSR_WRITE_2(sc, IO_PORT_REG, dest);
53752286Smdodd				}
538131192Simp				CSR_WRITE_2(sc, HOST_ADDR_REG,
53955881Smdodd				     sc->tx_last + XMT_Byte_Count);
540131192Simp				i = CSR_READ_2(sc, IO_PORT_REG);
541131192Simp				CSR_WRITE_2(sc, HOST_ADDR_REG,
54255881Smdodd				     sc->tx_last + XMT_Byte_Count);
543131192Simp				CSR_WRITE_2(sc, IO_PORT_REG, i | Ch_bit);
54455881Smdodd			}
54555881Smdodd
54655881Smdodd			/*
54755881Smdodd			 * Resume normal operation of the card:
54855881Smdodd			 * - Make a dummy read to flush the DRAM write
54955881Smdodd			 *   pipeline.
55055881Smdodd			 * - Enable receive and transmit interrupts.
55155881Smdodd			 * - Send Transmit or Resume_XMT command, as
55255881Smdodd			 *   appropriate.
55355881Smdodd			 */
554131192Simp			CSR_READ_2(sc, IO_PORT_REG);
55521769Sjkh#ifdef EX_PSA_INTR
556131192Simp			CSR_WRITE_1(sc, MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
55721769Sjkh#endif
55855881Smdodd			if (sc->tx_head == sc->tx_tail) {
559131192Simp				CSR_WRITE_2(sc, XMT_BAR, dest);
560131192Simp				CSR_WRITE_1(sc, CMD_REG, Transmit_CMD);
56155881Smdodd				sc->tx_head = dest;
56255881Smdodd				DODEBUG(Sent_Pkts, printf("Transmit\n"););
56352286Smdodd			} else {
564131192Simp				CSR_WRITE_1(sc, CMD_REG, Resume_XMT_List_CMD);
56555881Smdodd				DODEBUG(Sent_Pkts, printf("Resume\n"););
56652286Smdodd			}
56755881Smdodd
56855881Smdodd			sc->tx_last = dest;
56955881Smdodd			sc->tx_tail = next;
57055881Smdodd
571106937Ssam			BPF_MTAP(ifp, opkt);
57255881Smdodd
573179775Sjhb			sc->tx_timeout = 2;
57455881Smdodd			ifp->if_opackets++;
57555881Smdodd			m_freem(opkt);
57655881Smdodd		} else {
577148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
57855881Smdodd			DODEBUG(Status, printf("OACTIVE start\n"););
57952286Smdodd		}
58021769Sjkh	}
58121769Sjkh
58252286Smdodd	DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
58321769Sjkh}
58421769Sjkh
58566440Simpvoid
58652286Smdoddex_stop(struct ex_softc *sc)
58721769Sjkh{
588131192Simp
58952286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
59021769Sjkh
591179775Sjhb	EX_ASSERT_LOCKED(sc);
59252286Smdodd	/*
59352286Smdodd	 * Disable card operation:
59452286Smdodd	 * - Disable the interrupt line.
59552286Smdodd	 * - Flush transmission and disable reception.
59652286Smdodd	 * - Mask and clear all interrupts.
59752286Smdodd	 * - Reset the 82595.
59852286Smdodd	 */
599131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank1_Sel);
600131192Simp	CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) & ~TriST_INT);
601131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
602131192Simp	CSR_WRITE_1(sc, CMD_REG, Rcv_Stop);
60352286Smdodd	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
60452286Smdodd	sc->tx_last = 0; /* XXX I think these two lines are not necessary, because ex_init will always be called again to reinit the interface. */
605131192Simp	CSR_WRITE_1(sc, MASK_REG, All_Int);
606131192Simp	CSR_WRITE_1(sc, STATUS_REG, All_Int);
607131192Simp	CSR_WRITE_1(sc, CMD_REG, Reset_CMD);
60852286Smdodd	DELAY(200);
609179775Sjhb	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
610179775Sjhb	sc->tx_timeout = 0;
611179775Sjhb	callout_stop(&sc->timer);
61221769Sjkh
61352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
61452286Smdodd
61552286Smdodd	return;
61621769Sjkh}
61721769Sjkh
61859816Smdoddvoid
61955883Smdoddex_intr(void *arg)
62021769Sjkh{
621131192Simp	struct ex_softc *sc = (struct ex_softc *)arg;
622147256Sbrooks	struct ifnet 	*ifp = sc->ifp;
623131192Simp	int		int_status, send_pkts;
624131192Simp	int		loops = 100;
62521769Sjkh
62655883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: start\n", unit););
62721769Sjkh
628179775Sjhb	EX_LOCK(sc);
62952286Smdodd	send_pkts = 0;
630131192Simp	while (loops-- > 0 &&
631131192Simp	    (int_status = CSR_READ_1(sc, STATUS_REG)) & (Tx_Int | Rx_Int)) {
632131192Simp		/* don't loop forever */
633131192Simp		if (int_status == 0xff)
634131192Simp			break;
63552286Smdodd		if (int_status & Rx_Int) {
636131192Simp			CSR_WRITE_1(sc, STATUS_REG, Rx_Int);
63752286Smdodd			ex_rx_intr(sc);
63852286Smdodd		} else if (int_status & Tx_Int) {
639131192Simp			CSR_WRITE_1(sc, STATUS_REG, Tx_Int);
64052286Smdodd			ex_tx_intr(sc);
64152286Smdodd			send_pkts = 1;
64252286Smdodd		}
64352286Smdodd	}
644131192Simp	if (loops == 0)
645131192Simp		printf("100 loops are not enough\n");
64621769Sjkh
64752286Smdodd	/*
64852286Smdodd	 * If any packet has been transmitted, and there are queued packets to
64952286Smdodd	 * be sent, attempt to send more packets to the network card.
65052286Smdodd	 */
651131192Simp	if (send_pkts && (ifp->if_snd.ifq_head != NULL))
652179775Sjhb		ex_start_locked(ifp);
653179775Sjhb	EX_UNLOCK(sc);
65452286Smdodd
65555883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: finish\n", unit););
65652286Smdodd
65752286Smdodd	return;
65821769Sjkh}
65921769Sjkh
66052286Smdoddstatic void
66152286Smdoddex_tx_intr(struct ex_softc *sc)
66221769Sjkh{
663147256Sbrooks	struct ifnet *	ifp = sc->ifp;
66452286Smdodd	int		tx_status;
66521769Sjkh
66652286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
66721769Sjkh
66852286Smdodd	/*
66952286Smdodd	 * - Cancel the watchdog.
67052286Smdodd	 * For all packets transmitted since last transmit interrupt:
67152286Smdodd	 * - Advance chain pointer to next queued packet.
67252286Smdodd	 * - Update statistics.
67352286Smdodd	 */
67421769Sjkh
675179775Sjhb	sc->tx_timeout = 0;
67621769Sjkh
67752286Smdodd	while (sc->tx_head != sc->tx_tail) {
678131192Simp		CSR_WRITE_2(sc, HOST_ADDR_REG, sc->tx_head);
67921769Sjkh
680131192Simp		if (! CSR_READ_2(sc, IO_PORT_REG) & Done_bit)
68152286Smdodd			break;
68221769Sjkh
683131192Simp		tx_status = CSR_READ_2(sc, IO_PORT_REG);
684131192Simp		sc->tx_head = CSR_READ_2(sc, IO_PORT_REG);
68552286Smdodd
68652286Smdodd		if (tx_status & TX_OK_bit) {
68752286Smdodd			ifp->if_opackets++;
68852286Smdodd		} else {
68952286Smdodd			ifp->if_oerrors++;
69052286Smdodd		}
69152286Smdodd
69252286Smdodd		ifp->if_collisions += tx_status & No_Collisions_bits;
69352286Smdodd	}
69452286Smdodd
69552286Smdodd	/*
69652286Smdodd	 * The card should be ready to accept more packets now.
69752286Smdodd	 */
69852286Smdodd
699148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
70052286Smdodd
70152286Smdodd	DODEBUG(Status, printf("OIDLE tx_intr\n"););
70252286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
70352286Smdodd
70452286Smdodd	return;
70521769Sjkh}
70621769Sjkh
70752286Smdoddstatic void
70852286Smdoddex_rx_intr(struct ex_softc *sc)
70921769Sjkh{
710147256Sbrooks	struct ifnet *		ifp = sc->ifp;
71152286Smdodd	int			rx_status;
71252286Smdodd	int			pkt_len;
71352286Smdodd	int			QQQ;
71452286Smdodd	struct mbuf *		m;
71552286Smdodd	struct mbuf *		ipkt;
71652286Smdodd	struct ether_header *	eh;
71721769Sjkh
71852286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
71921769Sjkh
72052286Smdodd	/*
72152286Smdodd	 * For all packets received since last receive interrupt:
72252286Smdodd	 * - If packet ok, read it into a new mbuf and queue it to interface,
72352286Smdodd	 *   updating statistics.
72452286Smdodd	 * - If packet bad, just discard it, and update statistics.
72552286Smdodd	 * Finally, advance receive stop limit in card's memory to new location.
72652286Smdodd	 */
72721769Sjkh
728131192Simp	CSR_WRITE_2(sc, HOST_ADDR_REG, sc->rx_head);
72921769Sjkh
730131192Simp	while (CSR_READ_2(sc, IO_PORT_REG) == RCV_Done) {
73152286Smdodd
732131192Simp		rx_status = CSR_READ_2(sc, IO_PORT_REG);
733131192Simp		sc->rx_head = CSR_READ_2(sc, IO_PORT_REG);
734131192Simp		QQQ = pkt_len = CSR_READ_2(sc, IO_PORT_REG);
73552286Smdodd
73652286Smdodd		if (rx_status & RCV_OK_bit) {
737111119Simp			MGETHDR(m, M_DONTWAIT, MT_DATA);
73852286Smdodd			ipkt = m;
73952286Smdodd			if (ipkt == NULL) {
74052286Smdodd				ifp->if_iqdrops++;
74152286Smdodd			} else {
74252286Smdodd				ipkt->m_pkthdr.rcvif = ifp;
74352286Smdodd				ipkt->m_pkthdr.len = pkt_len;
74452286Smdodd				ipkt->m_len = MHLEN;
74552286Smdodd
74652286Smdodd				while (pkt_len > 0) {
747136625Sglebius					if (pkt_len >= MINCLSIZE) {
748111119Simp						MCLGET(m, M_DONTWAIT);
74952286Smdodd						if (m->m_flags & M_EXT) {
75052286Smdodd							m->m_len = MCLBYTES;
75152286Smdodd						} else {
75252286Smdodd							m_freem(ipkt);
75352286Smdodd							ifp->if_iqdrops++;
75452286Smdodd							goto rx_another;
75552286Smdodd						}
75652286Smdodd					}
75752286Smdodd					m->m_len = min(m->m_len, pkt_len);
75852286Smdodd
75921769Sjkh	  /*
76021769Sjkh	   * NOTE: I'm assuming that all mbufs allocated are of even length,
76121769Sjkh	   * except for the last one in an odd-length packet.
76221769Sjkh	   */
76352286Smdodd
764131192Simp					CSR_READ_MULTI_2(sc, IO_PORT_REG,
765131192Simp					    mtod(m, uint16_t *), m->m_len / 2);
76652286Smdodd
76752286Smdodd					if (m->m_len & 1) {
768131192Simp						*(mtod(m, caddr_t) + m->m_len - 1) = CSR_READ_1(sc, IO_PORT_REG);
76952286Smdodd					}
77052286Smdodd					pkt_len -= m->m_len;
77152286Smdodd
77252286Smdodd					if (pkt_len > 0) {
773111119Simp						MGET(m->m_next, M_DONTWAIT, MT_DATA);
77452286Smdodd						if (m->m_next == NULL) {
77552286Smdodd							m_freem(ipkt);
77652286Smdodd							ifp->if_iqdrops++;
77752286Smdodd							goto rx_another;
77852286Smdodd						}
77952286Smdodd						m = m->m_next;
78052286Smdodd						m->m_len = MLEN;
78152286Smdodd					}
78252286Smdodd				}
78352286Smdodd				eh = mtod(ipkt, struct ether_header *);
78452286Smdodd#ifdef EXDEBUG
78552286Smdodd	if (debug_mask & Rcvd_Pkts) {
78652286Smdodd		if ((eh->ether_dhost[5] != 0xff) || (eh->ether_dhost[0] != 0xff)) {
78752286Smdodd			printf("Receive packet with %d data bytes: %6D -> ", QQQ, eh->ether_shost, ":");
78852286Smdodd			printf("%6D\n", eh->ether_dhost, ":");
78952286Smdodd		} /* QQQ */
79021769Sjkh	}
79121769Sjkh#endif
792179775Sjhb				EX_UNLOCK(sc);
793106937Ssam				(*ifp->if_input)(ifp, ipkt);
794179775Sjhb				EX_LOCK(sc);
79552286Smdodd				ifp->if_ipackets++;
79652286Smdodd			}
79752286Smdodd		} else {
79852286Smdodd			ifp->if_ierrors++;
79921769Sjkh		}
800131192Simp		CSR_WRITE_2(sc, HOST_ADDR_REG, sc->rx_head);
80152286Smdoddrx_another: ;
80221769Sjkh	}
80321769Sjkh
80452286Smdodd	if (sc->rx_head < sc->rx_lower_limit + 2)
805131192Simp		CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_upper_limit);
80652286Smdodd	else
807131192Simp		CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_head - 2);
80852286Smdodd
80952286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
81052286Smdodd
81152286Smdodd	return;
81221769Sjkh}
81321769Sjkh
81421769Sjkh
81555883Smdoddstatic int
81655883Smdoddex_ioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
81721769Sjkh{
81852286Smdodd	struct ex_softc *	sc = ifp->if_softc;
81957987Smdodd	struct ifreq *		ifr = (struct ifreq *)data;
82052286Smdodd	int			error = 0;
82121769Sjkh
822121816Sbrooks	DODEBUG(Start_End, printf("%s: ex_ioctl: start ", ifp->if_xname););
82321769Sjkh
82452286Smdodd	switch(cmd) {
82552286Smdodd		case SIOCSIFADDR:
82652286Smdodd		case SIOCGIFADDR:
82752286Smdodd		case SIOCSIFMTU:
82852286Smdodd			error = ether_ioctl(ifp, cmd, data);
82952286Smdodd			break;
83021769Sjkh
83152286Smdodd		case SIOCSIFFLAGS:
83252286Smdodd			DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
833179775Sjhb			EX_LOCK(sc);
83452286Smdodd			if ((ifp->if_flags & IFF_UP) == 0 &&
835148887Srwatson			    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
83652286Smdodd				ex_stop(sc);
83752286Smdodd			} else {
838179775Sjhb      				ex_init_locked(sc);
83952286Smdodd			}
840179775Sjhb			EX_UNLOCK(sc);
84152286Smdodd			break;
84252286Smdodd		case SIOCADDMULTI:
84352286Smdodd		case SIOCDELMULTI:
844112731Smdodd			ex_init(sc);
845112731Smdodd			error = 0;
84652286Smdodd			break;
84757987Smdodd		case SIOCSIFMEDIA:
84857987Smdodd		case SIOCGIFMEDIA:
84957987Smdodd			error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
85057987Smdodd			break;
85152286Smdodd		default:
85252286Smdodd			DODEBUG(Start_End, printf("unknown"););
85352286Smdodd			error = EINVAL;
85452286Smdodd	}
85521769Sjkh
856121816Sbrooks	DODEBUG(Start_End, printf("\n%s: ex_ioctl: finish\n", ifp->if_xname););
85752286Smdodd
85852286Smdodd	return(error);
85921769Sjkh}
86021769Sjkh
861112731Smdoddstatic void
862112731Smdoddex_setmulti(struct ex_softc *sc)
863112731Smdodd{
864112731Smdodd	struct ifnet *ifp;
865112731Smdodd	struct ifmultiaddr *maddr;
866131192Simp	uint16_t *addr;
867112731Smdodd	int count;
868112731Smdodd	int timeout, status;
869112731Smdodd
870147256Sbrooks	ifp = sc->ifp;
87121769Sjkh
872112731Smdodd	count = 0;
873148654Srwatson	IF_ADDR_LOCK(ifp);
874112731Smdodd	TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
875112731Smdodd		if (maddr->ifma_addr->sa_family != AF_LINK)
876112731Smdodd			continue;
877112731Smdodd		count++;
878112731Smdodd	}
879148654Srwatson	IF_ADDR_UNLOCK(ifp);
880112731Smdodd
881112731Smdodd	if ((ifp->if_flags & IFF_PROMISC) || (ifp->if_flags & IFF_ALLMULTI)
882112731Smdodd			|| count > 63) {
883112731Smdodd		/* Interface is in promiscuous mode or there are too many
884112731Smdodd		 * multicast addresses for the card to handle */
885131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
886131192Simp		CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | Promisc_Mode);
887131192Simp		CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
888131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
889112731Smdodd	}
890112731Smdodd	else if ((ifp->if_flags & IFF_MULTICAST) && (count > 0)) {
891112731Smdodd		/* Program multicast addresses plus our MAC address
892112731Smdodd		 * into the filter */
893131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
894131192Simp		CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | Multi_IA);
895131192Simp		CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
896131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
897112731Smdodd
898112731Smdodd		/* Borrow space from TX buffer; this should be safe
899112731Smdodd		 * as this is only called from ex_init */
900112731Smdodd
901131192Simp		CSR_WRITE_2(sc, HOST_ADDR_REG, sc->tx_lower_limit);
902131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, MC_Setup_CMD);
903131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, 0);
904131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, 0);
905131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, (count + 1) * 6);
906148654Srwatson
907148654Srwatson		IF_ADDR_LOCK(ifp);
908112731Smdodd		TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
909112731Smdodd			if (maddr->ifma_addr->sa_family != AF_LINK)
910112731Smdodd				continue;
911112731Smdodd
912131192Simp			addr = (uint16_t*)LLADDR((struct sockaddr_dl *)
913112731Smdodd					maddr->ifma_addr);
914131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
915131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
916131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
917112731Smdodd		}
918148654Srwatson		IF_ADDR_UNLOCK(ifp);
919112731Smdodd
920112731Smdodd		/* Program our MAC address as well */
921112731Smdodd		/* XXX: Is this necessary?  The Linux driver does this
922112731Smdodd		 * but the NetBSD driver does not */
923152315Sru		addr = (uint16_t*)IF_LLADDR(sc->ifp);
924131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
925131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
926131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
927112731Smdodd
928131192Simp		CSR_READ_2(sc, IO_PORT_REG);
929131192Simp		CSR_WRITE_2(sc, XMT_BAR, sc->tx_lower_limit);
930131192Simp		CSR_WRITE_1(sc, CMD_REG, MC_Setup_CMD);
931112731Smdodd
932112731Smdodd		sc->tx_head = sc->tx_lower_limit;
933112731Smdodd		sc->tx_tail = sc->tx_head + XMT_HEADER_LEN + (count + 1) * 6;
934112731Smdodd
935112731Smdodd		for (timeout=0; timeout<100; timeout++) {
936112731Smdodd			DELAY(2);
937131192Simp			if ((CSR_READ_1(sc, STATUS_REG) & Exec_Int) == 0)
938112731Smdodd				continue;
939112731Smdodd
940131192Simp			status = CSR_READ_1(sc, CMD_REG);
941131192Simp			CSR_WRITE_1(sc, STATUS_REG, Exec_Int);
942112731Smdodd			break;
943112731Smdodd		}
944112731Smdodd
945112731Smdodd		sc->tx_head = sc->tx_tail;
946112731Smdodd	}
947112731Smdodd	else
948112731Smdodd	{
949112731Smdodd		/* No multicast or promiscuous mode */
950131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
951131192Simp		CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) & 0xDE);
952112731Smdodd			/* ~(Multi_IA | Promisc_Mode) */
953131192Simp		CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
954131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
955112731Smdodd	}
956112731Smdodd}
957112731Smdodd
95852286Smdoddstatic void
95952286Smdoddex_reset(struct ex_softc *sc)
96021769Sjkh{
96121769Sjkh
96252286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
96321769Sjkh
964179775Sjhb	EX_ASSERT_LOCKED(sc);
96552286Smdodd	ex_stop(sc);
966179775Sjhb	ex_init_locked(sc);
96721769Sjkh
96852286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
96952286Smdodd
97052286Smdodd	return;
97121769Sjkh}
97221769Sjkh
97352286Smdoddstatic void
974179775Sjhbex_watchdog(void *arg)
97521769Sjkh{
976179775Sjhb	struct ex_softc *	sc = arg;
977179775Sjhb	struct ifnet *ifp = sc->ifp;
97821769Sjkh
979179775Sjhb	if (sc->tx_timeout && --sc->tx_timeout == 0) {
980179775Sjhb		DODEBUG(Start_End, if_printf(ifp, "ex_watchdog: start\n"););
98121769Sjkh
982179775Sjhb		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
98321769Sjkh
984179775Sjhb		DODEBUG(Status, printf("OIDLE watchdog\n"););
98552286Smdodd
986179775Sjhb		ifp->if_oerrors++;
987179775Sjhb		ex_reset(sc);
988179775Sjhb		ex_start_locked(ifp);
98952286Smdodd
990179775Sjhb		DODEBUG(Start_End, if_printf(ifp, "ex_watchdog: finish\n"););
991179775Sjhb	}
99252286Smdodd
993179775Sjhb	callout_reset(&sc->timer, hz, ex_watchdog, sc);
99421769Sjkh}
99521769Sjkh
99657987Smdoddstatic int
997131192Simpex_get_media(struct ex_softc *sc)
99859816Smdodd{
999112764Smdodd	int	current;
1000112764Smdodd	int	media;
100159816Smdodd
1002131192Simp	media = ex_eeprom_read(sc, EE_W5);
1003112764Smdodd
1004131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
1005131192Simp	current = CSR_READ_1(sc, REG3);
1006131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
100759816Smdodd
1008112764Smdodd	if ((current & TPE_bit) && (media & EE_W5_PORT_TPE))
100959816Smdodd		return(IFM_ETHER|IFM_10_T);
1010112764Smdodd	if ((current & BNC_bit) && (media & EE_W5_PORT_BNC))
101159816Smdodd		return(IFM_ETHER|IFM_10_2);
101259816Smdodd
1013112764Smdodd	if (media & EE_W5_PORT_AUI)
1014112764Smdodd		return (IFM_ETHER|IFM_10_5);
1015112764Smdodd
1016112764Smdodd	return (IFM_ETHER|IFM_AUTO);
101759816Smdodd}
101859816Smdodd
101959816Smdoddstatic int
1020131192Simpex_ifmedia_upd(ifp)
102157987Smdodd	struct ifnet *		ifp;
102257987Smdodd{
1023112764Smdodd	struct ex_softc *       sc = ifp->if_softc;
102421769Sjkh
1025112764Smdodd	if (IFM_TYPE(sc->ifmedia.ifm_media) != IFM_ETHER)
1026112764Smdodd		return EINVAL;
1027112764Smdodd
102857987Smdodd	return (0);
102957987Smdodd}
103057987Smdodd
103157987Smdoddstatic void
103257987Smdoddex_ifmedia_sts(ifp, ifmr)
103357987Smdodd	struct ifnet *          ifp;
103457987Smdodd	struct ifmediareq *     ifmr;
103557987Smdodd{
103657987Smdodd	struct ex_softc *       sc = ifp->if_softc;
103757987Smdodd
1038179775Sjhb	EX_LOCK(sc);
1039131192Simp	ifmr->ifm_active = ex_get_media(sc);
1040112764Smdodd	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
1041179775Sjhb	EX_UNLOCK(sc);
104257987Smdodd
104357987Smdodd	return;
104457987Smdodd}
104557987Smdodd
104659816Smdoddu_short
1047131192Simpex_eeprom_read(struct ex_softc *sc, int location)
104821769Sjkh{
104921769Sjkh	int i;
105021769Sjkh	u_short data = 0;
105121769Sjkh	int read_cmd = location | EE_READ_CMD;
105221769Sjkh	short ctrl_val = EECS;
105321769Sjkh
1054131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
1055131192Simp	CSR_WRITE_1(sc, EEPROM_REG, EECS);
105621769Sjkh	for (i = 8; i >= 0; i--) {
105721769Sjkh		short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
1058131192Simp		CSR_WRITE_1(sc, EEPROM_REG, outval);
1059131192Simp		CSR_WRITE_1(sc, EEPROM_REG, outval | EESK);
106021769Sjkh		DELAY(3);
1061131192Simp		CSR_WRITE_1(sc, EEPROM_REG, outval);
106221769Sjkh		DELAY(2);
106321769Sjkh	}
1064131192Simp	CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
106521769Sjkh
106621769Sjkh	for (i = 16; i > 0; i--) {
1067131192Simp		CSR_WRITE_1(sc, EEPROM_REG, ctrl_val | EESK);
106821769Sjkh		DELAY(3);
1069131192Simp		data = (data << 1) |
1070131192Simp		    ((CSR_READ_1(sc, EEPROM_REG) & EEDO) ? 1 : 0);
1071131192Simp		CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
107221769Sjkh		DELAY(2);
107321769Sjkh	}
107421769Sjkh
107521769Sjkh	ctrl_val &= ~EECS;
1076131192Simp	CSR_WRITE_1(sc, EEPROM_REG, ctrl_val | EESK);
107721769Sjkh	DELAY(3);
1078131192Simp	CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
107921769Sjkh	DELAY(2);
1080131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
108121769Sjkh	return(data);
108221769Sjkh}
1083