if_ex.c revision 243857
1139749Simp/*-
2230132Suqs * 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 243857 2012-12-04 09:32:43Z glebius $");
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));
235179775Sjhb	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
23621769Sjkh	ifp->if_start = ex_start;
23721769Sjkh	ifp->if_ioctl = ex_ioctl;
23855883Smdodd	ifp->if_init = ex_init;
239207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
24021769Sjkh
24157987Smdodd	ifmedia_init(&sc->ifmedia, 0, ex_ifmedia_upd, ex_ifmedia_sts);
242179775Sjhb	mtx_init(&sc->lock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
243179775Sjhb	    MTX_DEF);
244179775Sjhb	callout_init_mtx(&sc->timer, &sc->lock, 0);
24557987Smdodd
246131192Simp	temp = ex_eeprom_read(sc, EE_W5);
24757987Smdodd	if (temp & EE_W5_PORT_TPE)
24857987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
24957987Smdodd	if (temp & EE_W5_PORT_BNC)
25057987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
25157987Smdodd	if (temp & EE_W5_PORT_AUI)
25257987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
25357987Smdodd
254112764Smdodd	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
255112764Smdodd	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
256131192Simp	ifmedia_set(&sc->ifmedia, ex_get_media(sc));
25757987Smdodd
25857987Smdodd	ifm = &sc->ifmedia;
259179775Sjhb	ifm->ifm_media = ifm->ifm_cur->ifm_media;
26057987Smdodd	ex_ifmedia_upd(ifp);
26157987Smdodd
26221769Sjkh	/*
26321769Sjkh	 * Attach the interface.
26421769Sjkh	 */
265147256Sbrooks	ether_ifattach(ifp, sc->enaddr);
26621769Sjkh
267179775Sjhb	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
268179775Sjhb				NULL, ex_intr, (void *)sc, &sc->ih);
269179775Sjhb	if (error) {
270179775Sjhb		device_printf(dev, "bus_setup_intr() failed!\n");
271179775Sjhb		ether_ifdetach(ifp);
272179775Sjhb		mtx_destroy(&sc->lock);
273179775Sjhb		return (error);
274179775Sjhb	}
275179775Sjhb
27652286Smdodd	return(0);
27721769Sjkh}
27821769Sjkh
279112800Smdoddint
280131192Simpex_detach(device_t dev)
281112800Smdodd{
282112800Smdodd	struct ex_softc	*sc;
283112800Smdodd	struct ifnet	*ifp;
284112800Smdodd
285112800Smdodd	sc = device_get_softc(dev);
286147256Sbrooks	ifp = sc->ifp;
287112800Smdodd
288179775Sjhb	EX_LOCK(sc);
289112800Smdodd        ex_stop(sc);
290179775Sjhb	EX_UNLOCK(sc);
291112800Smdodd
292112800Smdodd	ether_ifdetach(ifp);
293179775Sjhb	callout_drain(&sc->timer);
294112800Smdodd
295112800Smdodd	ex_release_resources(dev);
296179775Sjhb	mtx_destroy(&sc->lock);
297112800Smdodd
298112800Smdodd	return (0);
299112800Smdodd}
300112800Smdodd
30155883Smdoddstatic void
30255883Smdoddex_init(void *xsc)
30321769Sjkh{
30452286Smdodd	struct ex_softc *	sc = (struct ex_softc *) xsc;
305179775Sjhb
306179775Sjhb	EX_LOCK(sc);
307179775Sjhb	ex_init_locked(sc);
308179775Sjhb	EX_UNLOCK(sc);
309179775Sjhb}
310179775Sjhb
311179775Sjhbstatic void
312179775Sjhbex_init_locked(struct ex_softc *sc)
313179775Sjhb{
314147256Sbrooks	struct ifnet *		ifp = sc->ifp;
31552286Smdodd	int			i;
31652286Smdodd	unsigned short		temp_reg;
31721769Sjkh
318121816Sbrooks	DODEBUG(Start_End, printf("%s: ex_init: start\n", ifp->if_xname););
31921769Sjkh
320179775Sjhb	sc->tx_timeout = 0;
32121769Sjkh
32221769Sjkh	/*
32321769Sjkh	 * Load the ethernet address into the card.
32421769Sjkh	 */
325131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
326131192Simp	temp_reg = CSR_READ_1(sc, EEPROM_REG);
327182088Simp	if (temp_reg & Trnoff_Enable)
328131192Simp		CSR_WRITE_1(sc, EEPROM_REG, temp_reg & ~Trnoff_Enable);
329182088Simp	for (i = 0; i < ETHER_ADDR_LEN; i++)
330152315Sru		CSR_WRITE_1(sc, I_ADDR_REG0 + i, IF_LLADDR(sc->ifp)[i]);
331182088Simp
33221769Sjkh	/*
33321769Sjkh	 * - Setup transmit chaining and discard bad received frames.
33421769Sjkh	 * - Match broadcast.
33521769Sjkh	 * - Clear test mode.
33621769Sjkh	 * - Set receiving mode.
33721769Sjkh	 */
338131192Simp	CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
339131192Simp	CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | No_SA_Ins | RX_CRC_InMem);
340131192Simp	CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3) & 0x3f /* XXX constants. */ );
341182088Simp	/*
342182088Simp	 * - Set IRQ number, if this part has it.  ISA devices have this,
343182088Simp	 * while PC Card devices don't seem to.  Either way, we have to
344182088Simp	 * switch to Bank1 as the rest of this code relies on that.
345182088Simp	 */
346131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank1_Sel);
347182088Simp	if (sc->flags & HAS_INT_NO_REG)
348182088Simp		CSR_WRITE_1(sc, INT_NO_REG,
349182088Simp		    (CSR_READ_1(sc, INT_NO_REG) & 0xf8) |
350182088Simp		    sc->irq2ee[sc->irq_no]);
35121769Sjkh
35221769Sjkh	/*
35321769Sjkh	 * Divide the available memory in the card into rcv and xmt buffers.
35421769Sjkh	 * By default, I use the first 3/4 of the memory for the rcv buffer,
35521769Sjkh	 * and the remaining 1/4 of the memory for the xmt buffer.
35621769Sjkh	 */
35721769Sjkh	sc->rx_mem_size = sc->mem_size * 3 / 4;
35821769Sjkh	sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
35921769Sjkh	sc->rx_lower_limit = 0x0000;
36021769Sjkh	sc->rx_upper_limit = sc->rx_mem_size - 2;
36121769Sjkh	sc->tx_lower_limit = sc->rx_mem_size;
36221769Sjkh	sc->tx_upper_limit = sc->mem_size - 2;
363131192Simp	CSR_WRITE_1(sc, RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
364131192Simp	CSR_WRITE_1(sc, RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
365131192Simp	CSR_WRITE_1(sc, XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
366131192Simp	CSR_WRITE_1(sc, XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
36721769Sjkh
36821769Sjkh	/*
36921769Sjkh	 * Enable receive and transmit interrupts, and clear any pending int.
37021769Sjkh	 */
371131192Simp	CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) | TriST_INT);
372131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
373131192Simp	CSR_WRITE_1(sc, MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
374131192Simp	CSR_WRITE_1(sc, STATUS_REG, All_Int);
37521769Sjkh
37621769Sjkh	/*
37721769Sjkh	 * Initialize receive and transmit ring buffers.
37821769Sjkh	 */
379131192Simp	CSR_WRITE_2(sc, RCV_BAR, sc->rx_lower_limit);
38021769Sjkh	sc->rx_head = sc->rx_lower_limit;
381131192Simp	CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
382131192Simp	CSR_WRITE_2(sc, XMT_BAR, sc->tx_lower_limit);
38321769Sjkh	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
38421769Sjkh
385148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
386148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
38721769Sjkh	DODEBUG(Status, printf("OIDLE init\n"););
388179775Sjhb	callout_reset(&sc->timer, hz, ex_watchdog, sc);
38921769Sjkh
390112731Smdodd	ex_setmulti(sc);
391112731Smdodd
39221769Sjkh	/*
39321769Sjkh	 * Final reset of the board, and enable operation.
39421769Sjkh	 */
395131192Simp	CSR_WRITE_1(sc, CMD_REG, Sel_Reset_CMD);
39621769Sjkh	DELAY(2);
397131192Simp	CSR_WRITE_1(sc, CMD_REG, Rcv_Enable_CMD);
39821769Sjkh
399179775Sjhb	ex_start_locked(ifp);
40021769Sjkh
401121816Sbrooks	DODEBUG(Start_End, printf("%s: ex_init: finish\n", ifp->if_xname););
40221769Sjkh}
40321769Sjkh
40455883Smdoddstatic void
40552286Smdoddex_start(struct ifnet *ifp)
40621769Sjkh{
40752286Smdodd	struct ex_softc *	sc = ifp->if_softc;
408179775Sjhb
409179775Sjhb	EX_LOCK(sc);
410179775Sjhb	ex_start_locked(ifp);
411179775Sjhb	EX_UNLOCK(sc);
412179775Sjhb}
413179775Sjhb
414179775Sjhbstatic void
415179775Sjhbex_start_locked(struct ifnet *ifp)
416179775Sjhb{
417179775Sjhb	struct ex_softc *	sc = ifp->if_softc;
418179775Sjhb	int			i, len, data_len, avail, dest, next;
41952286Smdodd	unsigned char		tmp16[2];
42052286Smdodd	struct mbuf *		opkt;
42152286Smdodd	struct mbuf *		m;
42221769Sjkh
42352286Smdodd	DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
42421769Sjkh
42552286Smdodd	/*
42652286Smdodd	 * Main loop: send outgoing packets to network card until there are no
42752286Smdodd	 * more packets left, or the card cannot accept any more yet.
42852286Smdodd	 */
42952286Smdodd	while (((opkt = ifp->if_snd.ifq_head) != NULL) &&
430148887Srwatson	       !(ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
43121769Sjkh
43252286Smdodd		/*
43352286Smdodd		 * Ensure there is enough free transmit buffer space for
43452286Smdodd		 * this packet, including its header. Note: the header
43552286Smdodd		 * cannot wrap around the end of the transmit buffer and
43652286Smdodd		 * must be kept together, so we allow space for twice the
43752286Smdodd		 * length of the header, just in case.
43852286Smdodd		 */
43921769Sjkh
44052286Smdodd		for (len = 0, m = opkt; m != NULL; m = m->m_next) {
44152286Smdodd			len += m->m_len;
44252286Smdodd		}
44352286Smdodd
44452286Smdodd		data_len = len;
44552286Smdodd
44652286Smdodd		DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
44752286Smdodd
44852286Smdodd		if (len & 1) {
44952286Smdodd			len += XMT_HEADER_LEN + 1;
45052286Smdodd		} else {
45152286Smdodd			len += XMT_HEADER_LEN;
45252286Smdodd		}
45352286Smdodd
45452286Smdodd		if ((i = sc->tx_tail - sc->tx_head) >= 0) {
45552286Smdodd			avail = sc->tx_mem_size - i;
45652286Smdodd		} else {
45752286Smdodd			avail = -i;
45852286Smdodd		}
45952286Smdodd
46052286Smdodd		DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
46152286Smdodd
46252286Smdodd		if (avail >= len + XMT_HEADER_LEN) {
46352286Smdodd			IF_DEQUEUE(&ifp->if_snd, opkt);
46452286Smdodd
46521769Sjkh#ifdef EX_PSA_INTR
46652286Smdodd			/*
46752286Smdodd			 * Disable rx and tx interrupts, to avoid corruption
46852286Smdodd			 * of the host address register by interrupt service
46952286Smdodd			 * routines.
47052286Smdodd			 * XXX Is this necessary with splimp() enabled?
47152286Smdodd			 */
472131192Simp			CSR_WRITE_1(sc, MASK_REG, All_Int);
47321769Sjkh#endif
47421769Sjkh
47552286Smdodd			/*
47652286Smdodd			 * Compute the start and end addresses of this
47752286Smdodd			 * frame in the tx buffer.
47852286Smdodd			 */
47952286Smdodd			dest = sc->tx_tail;
48052286Smdodd			next = dest + len;
48121769Sjkh
48252286Smdodd			if (next > sc->tx_upper_limit) {
48352286Smdodd				if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
48452286Smdodd				    XMT_HEADER_LEN) {
48552286Smdodd					dest = sc->tx_lower_limit;
48652286Smdodd					next = dest + len;
48755881Smdodd				} else {
48855881Smdodd					next = sc->tx_lower_limit +
48955881Smdodd						next - sc->tx_upper_limit - 2;
49052286Smdodd				}
49152286Smdodd			}
49221769Sjkh
49352286Smdodd			/*
49452286Smdodd			 * Build the packet frame in the card's ring buffer.
49552286Smdodd			 */
49652286Smdodd			DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
49721769Sjkh
498131192Simp			CSR_WRITE_2(sc, HOST_ADDR_REG, dest);
499131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, Transmit_CMD);
500131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, 0);
501131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, next);
502131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, data_len);
50321769Sjkh
50452286Smdodd			/*
50552286Smdodd			 * Output the packet data to the card. Ensure all
50652286Smdodd			 * transfers are 16-bit wide, even if individual
50752286Smdodd			 * mbufs have odd length.
50852286Smdodd			 */
50952286Smdodd			for (m = opkt, i = 0; m != NULL; m = m->m_next) {
51052286Smdodd				DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
51152286Smdodd				if (i) {
51252286Smdodd					tmp16[1] = *(mtod(m, caddr_t));
513131192Simp					CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
514131192Simp					    (uint16_t *) tmp16, 1);
51552286Smdodd				}
516131192Simp				CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
517131192Simp				    (uint16_t *) (mtod(m, caddr_t) + i),
518131192Simp				    (m->m_len - i) / 2);
51952286Smdodd				if ((i = (m->m_len - i) & 1) != 0) {
52052286Smdodd					tmp16[0] = *(mtod(m, caddr_t) +
52152286Smdodd						   m->m_len - 1);
52252286Smdodd				}
52352286Smdodd			}
524131192Simp			if (i)
525131192Simp				CSR_WRITE_MULTI_2(sc, IO_PORT_REG,
526131192Simp				    (uint16_t *) tmp16, 1);
52755881Smdodd			/*
52855881Smdodd			 * If there were other frames chained, update the
52955881Smdodd			 * chain in the last one.
53055881Smdodd			 */
53155881Smdodd			if (sc->tx_head != sc->tx_tail) {
53255881Smdodd				if (sc->tx_tail != dest) {
533131192Simp					CSR_WRITE_2(sc, HOST_ADDR_REG,
53455881Smdodd					     sc->tx_last + XMT_Chain_Point);
535131192Simp					CSR_WRITE_2(sc, IO_PORT_REG, dest);
53652286Smdodd				}
537131192Simp				CSR_WRITE_2(sc, HOST_ADDR_REG,
53855881Smdodd				     sc->tx_last + XMT_Byte_Count);
539131192Simp				i = CSR_READ_2(sc, IO_PORT_REG);
540131192Simp				CSR_WRITE_2(sc, HOST_ADDR_REG,
54155881Smdodd				     sc->tx_last + XMT_Byte_Count);
542131192Simp				CSR_WRITE_2(sc, IO_PORT_REG, i | Ch_bit);
54355881Smdodd			}
54455881Smdodd
54555881Smdodd			/*
54655881Smdodd			 * Resume normal operation of the card:
54755881Smdodd			 * - Make a dummy read to flush the DRAM write
54855881Smdodd			 *   pipeline.
54955881Smdodd			 * - Enable receive and transmit interrupts.
55055881Smdodd			 * - Send Transmit or Resume_XMT command, as
55155881Smdodd			 *   appropriate.
55255881Smdodd			 */
553131192Simp			CSR_READ_2(sc, IO_PORT_REG);
55421769Sjkh#ifdef EX_PSA_INTR
555131192Simp			CSR_WRITE_1(sc, MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
55621769Sjkh#endif
55755881Smdodd			if (sc->tx_head == sc->tx_tail) {
558131192Simp				CSR_WRITE_2(sc, XMT_BAR, dest);
559131192Simp				CSR_WRITE_1(sc, CMD_REG, Transmit_CMD);
56055881Smdodd				sc->tx_head = dest;
56155881Smdodd				DODEBUG(Sent_Pkts, printf("Transmit\n"););
56252286Smdodd			} else {
563131192Simp				CSR_WRITE_1(sc, CMD_REG, Resume_XMT_List_CMD);
56455881Smdodd				DODEBUG(Sent_Pkts, printf("Resume\n"););
56552286Smdodd			}
56655881Smdodd
56755881Smdodd			sc->tx_last = dest;
56855881Smdodd			sc->tx_tail = next;
56955881Smdodd
570106937Ssam			BPF_MTAP(ifp, opkt);
57155881Smdodd
572179775Sjhb			sc->tx_timeout = 2;
57355881Smdodd			ifp->if_opackets++;
57455881Smdodd			m_freem(opkt);
57555881Smdodd		} else {
576148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
57755881Smdodd			DODEBUG(Status, printf("OACTIVE start\n"););
57852286Smdodd		}
57921769Sjkh	}
58021769Sjkh
58152286Smdodd	DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
58221769Sjkh}
58321769Sjkh
58466440Simpvoid
58552286Smdoddex_stop(struct ex_softc *sc)
58621769Sjkh{
587131192Simp
58852286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
58921769Sjkh
590179775Sjhb	EX_ASSERT_LOCKED(sc);
59152286Smdodd	/*
59252286Smdodd	 * Disable card operation:
59352286Smdodd	 * - Disable the interrupt line.
59452286Smdodd	 * - Flush transmission and disable reception.
59552286Smdodd	 * - Mask and clear all interrupts.
59652286Smdodd	 * - Reset the 82595.
59752286Smdodd	 */
598131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank1_Sel);
599131192Simp	CSR_WRITE_1(sc, REG1, CSR_READ_1(sc, REG1) & ~TriST_INT);
600131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
601131192Simp	CSR_WRITE_1(sc, CMD_REG, Rcv_Stop);
60252286Smdodd	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
60352286Smdodd	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. */
604131192Simp	CSR_WRITE_1(sc, MASK_REG, All_Int);
605131192Simp	CSR_WRITE_1(sc, STATUS_REG, All_Int);
606131192Simp	CSR_WRITE_1(sc, CMD_REG, Reset_CMD);
60752286Smdodd	DELAY(200);
608179775Sjhb	sc->ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
609179775Sjhb	sc->tx_timeout = 0;
610179775Sjhb	callout_stop(&sc->timer);
61121769Sjkh
61252286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
61352286Smdodd
61452286Smdodd	return;
61521769Sjkh}
61621769Sjkh
61759816Smdoddvoid
61855883Smdoddex_intr(void *arg)
61921769Sjkh{
620131192Simp	struct ex_softc *sc = (struct ex_softc *)arg;
621147256Sbrooks	struct ifnet 	*ifp = sc->ifp;
622131192Simp	int		int_status, send_pkts;
623131192Simp	int		loops = 100;
62421769Sjkh
62555883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: start\n", unit););
62621769Sjkh
627179775Sjhb	EX_LOCK(sc);
62852286Smdodd	send_pkts = 0;
629131192Simp	while (loops-- > 0 &&
630131192Simp	    (int_status = CSR_READ_1(sc, STATUS_REG)) & (Tx_Int | Rx_Int)) {
631131192Simp		/* don't loop forever */
632131192Simp		if (int_status == 0xff)
633131192Simp			break;
63452286Smdodd		if (int_status & Rx_Int) {
635131192Simp			CSR_WRITE_1(sc, STATUS_REG, Rx_Int);
63652286Smdodd			ex_rx_intr(sc);
63752286Smdodd		} else if (int_status & Tx_Int) {
638131192Simp			CSR_WRITE_1(sc, STATUS_REG, Tx_Int);
63952286Smdodd			ex_tx_intr(sc);
64052286Smdodd			send_pkts = 1;
64152286Smdodd		}
64252286Smdodd	}
643131192Simp	if (loops == 0)
644131192Simp		printf("100 loops are not enough\n");
64521769Sjkh
64652286Smdodd	/*
64752286Smdodd	 * If any packet has been transmitted, and there are queued packets to
64852286Smdodd	 * be sent, attempt to send more packets to the network card.
64952286Smdodd	 */
650131192Simp	if (send_pkts && (ifp->if_snd.ifq_head != NULL))
651179775Sjhb		ex_start_locked(ifp);
652179775Sjhb	EX_UNLOCK(sc);
65352286Smdodd
65455883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: finish\n", unit););
65552286Smdodd
65652286Smdodd	return;
65721769Sjkh}
65821769Sjkh
65952286Smdoddstatic void
66052286Smdoddex_tx_intr(struct ex_softc *sc)
66121769Sjkh{
662147256Sbrooks	struct ifnet *	ifp = sc->ifp;
66352286Smdodd	int		tx_status;
66421769Sjkh
66552286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
66621769Sjkh
66752286Smdodd	/*
66852286Smdodd	 * - Cancel the watchdog.
66952286Smdodd	 * For all packets transmitted since last transmit interrupt:
67052286Smdodd	 * - Advance chain pointer to next queued packet.
67152286Smdodd	 * - Update statistics.
67252286Smdodd	 */
67321769Sjkh
674179775Sjhb	sc->tx_timeout = 0;
67521769Sjkh
67652286Smdodd	while (sc->tx_head != sc->tx_tail) {
677131192Simp		CSR_WRITE_2(sc, HOST_ADDR_REG, sc->tx_head);
67821769Sjkh
679201794Strasz		if (!(CSR_READ_2(sc, IO_PORT_REG) & Done_bit))
68052286Smdodd			break;
68121769Sjkh
682131192Simp		tx_status = CSR_READ_2(sc, IO_PORT_REG);
683131192Simp		sc->tx_head = CSR_READ_2(sc, IO_PORT_REG);
68452286Smdodd
68552286Smdodd		if (tx_status & TX_OK_bit) {
68652286Smdodd			ifp->if_opackets++;
68752286Smdodd		} else {
68852286Smdodd			ifp->if_oerrors++;
68952286Smdodd		}
69052286Smdodd
69152286Smdodd		ifp->if_collisions += tx_status & No_Collisions_bits;
69252286Smdodd	}
69352286Smdodd
69452286Smdodd	/*
69552286Smdodd	 * The card should be ready to accept more packets now.
69652286Smdodd	 */
69752286Smdodd
698148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
69952286Smdodd
70052286Smdodd	DODEBUG(Status, printf("OIDLE tx_intr\n"););
70152286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
70252286Smdodd
70352286Smdodd	return;
70421769Sjkh}
70521769Sjkh
70652286Smdoddstatic void
70752286Smdoddex_rx_intr(struct ex_softc *sc)
70821769Sjkh{
709147256Sbrooks	struct ifnet *		ifp = sc->ifp;
71052286Smdodd	int			rx_status;
71152286Smdodd	int			pkt_len;
71252286Smdodd	int			QQQ;
71352286Smdodd	struct mbuf *		m;
71452286Smdodd	struct mbuf *		ipkt;
71552286Smdodd	struct ether_header *	eh;
71621769Sjkh
71752286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
71821769Sjkh
71952286Smdodd	/*
72052286Smdodd	 * For all packets received since last receive interrupt:
72152286Smdodd	 * - If packet ok, read it into a new mbuf and queue it to interface,
72252286Smdodd	 *   updating statistics.
72352286Smdodd	 * - If packet bad, just discard it, and update statistics.
72452286Smdodd	 * Finally, advance receive stop limit in card's memory to new location.
72552286Smdodd	 */
72621769Sjkh
727131192Simp	CSR_WRITE_2(sc, HOST_ADDR_REG, sc->rx_head);
72821769Sjkh
729131192Simp	while (CSR_READ_2(sc, IO_PORT_REG) == RCV_Done) {
73052286Smdodd
731131192Simp		rx_status = CSR_READ_2(sc, IO_PORT_REG);
732131192Simp		sc->rx_head = CSR_READ_2(sc, IO_PORT_REG);
733131192Simp		QQQ = pkt_len = CSR_READ_2(sc, IO_PORT_REG);
73452286Smdodd
73552286Smdodd		if (rx_status & RCV_OK_bit) {
736243857Sglebius			MGETHDR(m, M_NOWAIT, MT_DATA);
73752286Smdodd			ipkt = m;
73852286Smdodd			if (ipkt == NULL) {
73952286Smdodd				ifp->if_iqdrops++;
74052286Smdodd			} else {
74152286Smdodd				ipkt->m_pkthdr.rcvif = ifp;
74252286Smdodd				ipkt->m_pkthdr.len = pkt_len;
74352286Smdodd				ipkt->m_len = MHLEN;
74452286Smdodd
74552286Smdodd				while (pkt_len > 0) {
746136625Sglebius					if (pkt_len >= MINCLSIZE) {
747243857Sglebius						MCLGET(m, M_NOWAIT);
74852286Smdodd						if (m->m_flags & M_EXT) {
74952286Smdodd							m->m_len = MCLBYTES;
75052286Smdodd						} else {
75152286Smdodd							m_freem(ipkt);
75252286Smdodd							ifp->if_iqdrops++;
75352286Smdodd							goto rx_another;
75452286Smdodd						}
75552286Smdodd					}
75652286Smdodd					m->m_len = min(m->m_len, pkt_len);
75752286Smdodd
75821769Sjkh	  /*
75921769Sjkh	   * NOTE: I'm assuming that all mbufs allocated are of even length,
76021769Sjkh	   * except for the last one in an odd-length packet.
76121769Sjkh	   */
76252286Smdodd
763131192Simp					CSR_READ_MULTI_2(sc, IO_PORT_REG,
764131192Simp					    mtod(m, uint16_t *), m->m_len / 2);
76552286Smdodd
76652286Smdodd					if (m->m_len & 1) {
767131192Simp						*(mtod(m, caddr_t) + m->m_len - 1) = CSR_READ_1(sc, IO_PORT_REG);
76852286Smdodd					}
76952286Smdodd					pkt_len -= m->m_len;
77052286Smdodd
77152286Smdodd					if (pkt_len > 0) {
772243857Sglebius						MGET(m->m_next, M_NOWAIT, MT_DATA);
77352286Smdodd						if (m->m_next == NULL) {
77452286Smdodd							m_freem(ipkt);
77552286Smdodd							ifp->if_iqdrops++;
77652286Smdodd							goto rx_another;
77752286Smdodd						}
77852286Smdodd						m = m->m_next;
77952286Smdodd						m->m_len = MLEN;
78052286Smdodd					}
78152286Smdodd				}
78252286Smdodd				eh = mtod(ipkt, struct ether_header *);
78352286Smdodd#ifdef EXDEBUG
78452286Smdodd	if (debug_mask & Rcvd_Pkts) {
78552286Smdodd		if ((eh->ether_dhost[5] != 0xff) || (eh->ether_dhost[0] != 0xff)) {
78652286Smdodd			printf("Receive packet with %d data bytes: %6D -> ", QQQ, eh->ether_shost, ":");
78752286Smdodd			printf("%6D\n", eh->ether_dhost, ":");
78852286Smdodd		} /* QQQ */
78921769Sjkh	}
79021769Sjkh#endif
791179775Sjhb				EX_UNLOCK(sc);
792106937Ssam				(*ifp->if_input)(ifp, ipkt);
793179775Sjhb				EX_LOCK(sc);
79452286Smdodd				ifp->if_ipackets++;
79552286Smdodd			}
79652286Smdodd		} else {
79752286Smdodd			ifp->if_ierrors++;
79821769Sjkh		}
799131192Simp		CSR_WRITE_2(sc, HOST_ADDR_REG, sc->rx_head);
80052286Smdoddrx_another: ;
80121769Sjkh	}
80221769Sjkh
80352286Smdodd	if (sc->rx_head < sc->rx_lower_limit + 2)
804131192Simp		CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_upper_limit);
80552286Smdodd	else
806131192Simp		CSR_WRITE_2(sc, RCV_STOP_REG, sc->rx_head - 2);
80752286Smdodd
80852286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
80952286Smdodd
81052286Smdodd	return;
81121769Sjkh}
81221769Sjkh
81321769Sjkh
81455883Smdoddstatic int
81555883Smdoddex_ioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
81621769Sjkh{
81752286Smdodd	struct ex_softc *	sc = ifp->if_softc;
81857987Smdodd	struct ifreq *		ifr = (struct ifreq *)data;
81952286Smdodd	int			error = 0;
82021769Sjkh
821121816Sbrooks	DODEBUG(Start_End, printf("%s: ex_ioctl: start ", ifp->if_xname););
82221769Sjkh
82352286Smdodd	switch(cmd) {
82452286Smdodd		case SIOCSIFADDR:
82552286Smdodd		case SIOCGIFADDR:
82652286Smdodd		case SIOCSIFMTU:
82752286Smdodd			error = ether_ioctl(ifp, cmd, data);
82852286Smdodd			break;
82921769Sjkh
83052286Smdodd		case SIOCSIFFLAGS:
83152286Smdodd			DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
832179775Sjhb			EX_LOCK(sc);
83352286Smdodd			if ((ifp->if_flags & IFF_UP) == 0 &&
834148887Srwatson			    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
83552286Smdodd				ex_stop(sc);
83652286Smdodd			} else {
837179775Sjhb      				ex_init_locked(sc);
83852286Smdodd			}
839179775Sjhb			EX_UNLOCK(sc);
84052286Smdodd			break;
84152286Smdodd		case SIOCADDMULTI:
84252286Smdodd		case SIOCDELMULTI:
843112731Smdodd			ex_init(sc);
844112731Smdodd			error = 0;
84552286Smdodd			break;
84657987Smdodd		case SIOCSIFMEDIA:
84757987Smdodd		case SIOCGIFMEDIA:
84857987Smdodd			error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
84957987Smdodd			break;
85052286Smdodd		default:
85152286Smdodd			DODEBUG(Start_End, printf("unknown"););
85252286Smdodd			error = EINVAL;
85352286Smdodd	}
85421769Sjkh
855121816Sbrooks	DODEBUG(Start_End, printf("\n%s: ex_ioctl: finish\n", ifp->if_xname););
85652286Smdodd
85752286Smdodd	return(error);
85821769Sjkh}
85921769Sjkh
860112731Smdoddstatic void
861112731Smdoddex_setmulti(struct ex_softc *sc)
862112731Smdodd{
863112731Smdodd	struct ifnet *ifp;
864112731Smdodd	struct ifmultiaddr *maddr;
865131192Simp	uint16_t *addr;
866112731Smdodd	int count;
867112731Smdodd	int timeout, status;
868112731Smdodd
869147256Sbrooks	ifp = sc->ifp;
87021769Sjkh
871112731Smdodd	count = 0;
872195049Srwatson	if_maddr_rlock(ifp);
873112731Smdodd	TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
874112731Smdodd		if (maddr->ifma_addr->sa_family != AF_LINK)
875112731Smdodd			continue;
876112731Smdodd		count++;
877112731Smdodd	}
878195049Srwatson	if_maddr_runlock(ifp);
879112731Smdodd
880112731Smdodd	if ((ifp->if_flags & IFF_PROMISC) || (ifp->if_flags & IFF_ALLMULTI)
881112731Smdodd			|| count > 63) {
882112731Smdodd		/* Interface is in promiscuous mode or there are too many
883112731Smdodd		 * multicast addresses for the card to handle */
884131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
885131192Simp		CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | Promisc_Mode);
886131192Simp		CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
887131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
888112731Smdodd	}
889112731Smdodd	else if ((ifp->if_flags & IFF_MULTICAST) && (count > 0)) {
890112731Smdodd		/* Program multicast addresses plus our MAC address
891112731Smdodd		 * into the filter */
892131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
893131192Simp		CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) | Multi_IA);
894131192Simp		CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
895131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
896112731Smdodd
897112731Smdodd		/* Borrow space from TX buffer; this should be safe
898112731Smdodd		 * as this is only called from ex_init */
899112731Smdodd
900131192Simp		CSR_WRITE_2(sc, HOST_ADDR_REG, sc->tx_lower_limit);
901131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, MC_Setup_CMD);
902131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, 0);
903131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, 0);
904131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, (count + 1) * 6);
905148654Srwatson
906195049Srwatson		if_maddr_rlock(ifp);
907112731Smdodd		TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
908112731Smdodd			if (maddr->ifma_addr->sa_family != AF_LINK)
909112731Smdodd				continue;
910112731Smdodd
911131192Simp			addr = (uint16_t*)LLADDR((struct sockaddr_dl *)
912112731Smdodd					maddr->ifma_addr);
913131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
914131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
915131192Simp			CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
916112731Smdodd		}
917195049Srwatson		if_maddr_runlock(ifp);
918112731Smdodd
919112731Smdodd		/* Program our MAC address as well */
920112731Smdodd		/* XXX: Is this necessary?  The Linux driver does this
921112731Smdodd		 * but the NetBSD driver does not */
922152315Sru		addr = (uint16_t*)IF_LLADDR(sc->ifp);
923131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
924131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
925131192Simp		CSR_WRITE_2(sc, IO_PORT_REG, *addr++);
926112731Smdodd
927131192Simp		CSR_READ_2(sc, IO_PORT_REG);
928131192Simp		CSR_WRITE_2(sc, XMT_BAR, sc->tx_lower_limit);
929131192Simp		CSR_WRITE_1(sc, CMD_REG, MC_Setup_CMD);
930112731Smdodd
931112731Smdodd		sc->tx_head = sc->tx_lower_limit;
932112731Smdodd		sc->tx_tail = sc->tx_head + XMT_HEADER_LEN + (count + 1) * 6;
933112731Smdodd
934112731Smdodd		for (timeout=0; timeout<100; timeout++) {
935112731Smdodd			DELAY(2);
936131192Simp			if ((CSR_READ_1(sc, STATUS_REG) & Exec_Int) == 0)
937112731Smdodd				continue;
938112731Smdodd
939131192Simp			status = CSR_READ_1(sc, CMD_REG);
940131192Simp			CSR_WRITE_1(sc, STATUS_REG, Exec_Int);
941112731Smdodd			break;
942112731Smdodd		}
943112731Smdodd
944112731Smdodd		sc->tx_head = sc->tx_tail;
945112731Smdodd	}
946112731Smdodd	else
947112731Smdodd	{
948112731Smdodd		/* No multicast or promiscuous mode */
949131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
950131192Simp		CSR_WRITE_1(sc, REG2, CSR_READ_1(sc, REG2) & 0xDE);
951112731Smdodd			/* ~(Multi_IA | Promisc_Mode) */
952131192Simp		CSR_WRITE_1(sc, REG3, CSR_READ_1(sc, REG3));
953131192Simp		CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
954112731Smdodd	}
955112731Smdodd}
956112731Smdodd
95752286Smdoddstatic void
95852286Smdoddex_reset(struct ex_softc *sc)
95921769Sjkh{
96021769Sjkh
96152286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
96221769Sjkh
963179775Sjhb	EX_ASSERT_LOCKED(sc);
96452286Smdodd	ex_stop(sc);
965179775Sjhb	ex_init_locked(sc);
96621769Sjkh
96752286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
96852286Smdodd
96952286Smdodd	return;
97021769Sjkh}
97121769Sjkh
97252286Smdoddstatic void
973179775Sjhbex_watchdog(void *arg)
97421769Sjkh{
975179775Sjhb	struct ex_softc *	sc = arg;
976179775Sjhb	struct ifnet *ifp = sc->ifp;
97721769Sjkh
978179775Sjhb	if (sc->tx_timeout && --sc->tx_timeout == 0) {
979179775Sjhb		DODEBUG(Start_End, if_printf(ifp, "ex_watchdog: start\n"););
98021769Sjkh
981179775Sjhb		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
98221769Sjkh
983179775Sjhb		DODEBUG(Status, printf("OIDLE watchdog\n"););
98452286Smdodd
985179775Sjhb		ifp->if_oerrors++;
986179775Sjhb		ex_reset(sc);
987179775Sjhb		ex_start_locked(ifp);
98852286Smdodd
989179775Sjhb		DODEBUG(Start_End, if_printf(ifp, "ex_watchdog: finish\n"););
990179775Sjhb	}
99152286Smdodd
992179775Sjhb	callout_reset(&sc->timer, hz, ex_watchdog, sc);
99321769Sjkh}
99421769Sjkh
99557987Smdoddstatic int
996131192Simpex_get_media(struct ex_softc *sc)
99759816Smdodd{
998112764Smdodd	int	current;
999112764Smdodd	int	media;
100059816Smdodd
1001131192Simp	media = ex_eeprom_read(sc, EE_W5);
1002112764Smdodd
1003131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
1004131192Simp	current = CSR_READ_1(sc, REG3);
1005131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
100659816Smdodd
1007112764Smdodd	if ((current & TPE_bit) && (media & EE_W5_PORT_TPE))
100859816Smdodd		return(IFM_ETHER|IFM_10_T);
1009112764Smdodd	if ((current & BNC_bit) && (media & EE_W5_PORT_BNC))
101059816Smdodd		return(IFM_ETHER|IFM_10_2);
101159816Smdodd
1012112764Smdodd	if (media & EE_W5_PORT_AUI)
1013112764Smdodd		return (IFM_ETHER|IFM_10_5);
1014112764Smdodd
1015112764Smdodd	return (IFM_ETHER|IFM_AUTO);
101659816Smdodd}
101759816Smdodd
101859816Smdoddstatic int
1019131192Simpex_ifmedia_upd(ifp)
102057987Smdodd	struct ifnet *		ifp;
102157987Smdodd{
1022112764Smdodd	struct ex_softc *       sc = ifp->if_softc;
102321769Sjkh
1024112764Smdodd	if (IFM_TYPE(sc->ifmedia.ifm_media) != IFM_ETHER)
1025112764Smdodd		return EINVAL;
1026112764Smdodd
102757987Smdodd	return (0);
102857987Smdodd}
102957987Smdodd
103057987Smdoddstatic void
103157987Smdoddex_ifmedia_sts(ifp, ifmr)
103257987Smdodd	struct ifnet *          ifp;
103357987Smdodd	struct ifmediareq *     ifmr;
103457987Smdodd{
103557987Smdodd	struct ex_softc *       sc = ifp->if_softc;
103657987Smdodd
1037179775Sjhb	EX_LOCK(sc);
1038131192Simp	ifmr->ifm_active = ex_get_media(sc);
1039112764Smdodd	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
1040179775Sjhb	EX_UNLOCK(sc);
104157987Smdodd
104257987Smdodd	return;
104357987Smdodd}
104457987Smdodd
104559816Smdoddu_short
1046131192Simpex_eeprom_read(struct ex_softc *sc, int location)
104721769Sjkh{
104821769Sjkh	int i;
104921769Sjkh	u_short data = 0;
105021769Sjkh	int read_cmd = location | EE_READ_CMD;
105121769Sjkh	short ctrl_val = EECS;
105221769Sjkh
1053131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank2_Sel);
1054131192Simp	CSR_WRITE_1(sc, EEPROM_REG, EECS);
105521769Sjkh	for (i = 8; i >= 0; i--) {
105621769Sjkh		short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
1057131192Simp		CSR_WRITE_1(sc, EEPROM_REG, outval);
1058131192Simp		CSR_WRITE_1(sc, EEPROM_REG, outval | EESK);
105921769Sjkh		DELAY(3);
1060131192Simp		CSR_WRITE_1(sc, EEPROM_REG, outval);
106121769Sjkh		DELAY(2);
106221769Sjkh	}
1063131192Simp	CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
106421769Sjkh
106521769Sjkh	for (i = 16; i > 0; i--) {
1066131192Simp		CSR_WRITE_1(sc, EEPROM_REG, ctrl_val | EESK);
106721769Sjkh		DELAY(3);
1068131192Simp		data = (data << 1) |
1069131192Simp		    ((CSR_READ_1(sc, EEPROM_REG) & EEDO) ? 1 : 0);
1070131192Simp		CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
107121769Sjkh		DELAY(2);
107221769Sjkh	}
107321769Sjkh
107421769Sjkh	ctrl_val &= ~EECS;
1075131192Simp	CSR_WRITE_1(sc, EEPROM_REG, ctrl_val | EESK);
107621769Sjkh	DELAY(3);
1077131192Simp	CSR_WRITE_1(sc, EEPROM_REG, ctrl_val);
107821769Sjkh	DELAY(2);
1079131192Simp	CSR_WRITE_1(sc, CMD_REG, Bank0_Sel);
108021769Sjkh	return(data);
108121769Sjkh}
1082