if_ex.c revision 112800
121769Sjkh/*
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 *
2750477Speter * $FreeBSD: head/sys/dev/ex/if_ex.c 112800 2003-03-29 15:34:26Z mdodd $
2852286Smdodd *
2952286Smdodd * MAINTAINER: Matthew N. Dodd <winter@jurai.net>
3052286Smdodd *                             <mdodd@FreeBSD.org>
3121769Sjkh */
3221769Sjkh
3321769Sjkh/*
3429877Smsmith * Intel EtherExpress Pro/10, Pro/10+ Ethernet driver
3521769Sjkh *
3621769Sjkh * Revision history:
3721769Sjkh *
38112731Smdodd * dd-mmm-yyyy: Multicast support ported from NetBSD's if_iy driver.
3921769Sjkh * 30-Oct-1996: first beta version. Inet and BPF supported, but no multicast.
4021769Sjkh */
4121769Sjkh
4221769Sjkh#include <sys/param.h>
4321769Sjkh#include <sys/systm.h>
4452286Smdodd#include <sys/kernel.h>
4524204Sbde#include <sys/sockio.h>
4621769Sjkh#include <sys/mbuf.h>
4721769Sjkh#include <sys/socket.h>
4821769Sjkh
4952286Smdodd#include <sys/module.h>
5052286Smdodd#include <sys/bus.h>
5152286Smdodd
5252286Smdodd#include <machine/bus.h>
5352286Smdodd#include <machine/resource.h>
5452286Smdodd#include <sys/rman.h>
5552286Smdodd
5657987Smdodd#include <net/if.h>
5757987Smdodd#include <net/if_arp.h>
58112731Smdodd#include <net/if_dl.h>
5957987Smdodd#include <net/if_media.h>
6050026Smdodd#include <net/ethernet.h>
6157987Smdodd#include <net/bpf.h>
6221769Sjkh
6350026Smdodd#include <netinet/in.h>
6450026Smdodd#include <netinet/if_ether.h>
6550026Smdodd
6621769Sjkh
6752286Smdodd#include <isa/isavar.h>
6852286Smdodd#include <isa/pnpvar.h>
6952286Smdodd
7055953Speter#include <dev/ex/if_exreg.h>
7159816Smdodd#include <dev/ex/if_exvar.h>
7221769Sjkh
7321769Sjkh#ifdef EXDEBUG
7452286Smdodd# define Start_End 1
7552286Smdodd# define Rcvd_Pkts 2
7652286Smdodd# define Sent_Pkts 4
7752286Smdodd# define Status    8
7821769Sjkhstatic int debug_mask = 0;
7921769Sjkhstatic int exintr_count = 0;
8052286Smdodd# define DODEBUG(level, action) if (level & debug_mask) action
8121769Sjkh#else
8252286Smdodd# define DODEBUG(level, action)
8321769Sjkh#endif
8421769Sjkh
8559816Smdoddchar irq2eemap[] =
8652286Smdodd	{ -1, -1, 0, 1, -1, 2, -1, -1, -1, 0, 3, 4, -1, -1, -1, -1 };
8759816Smdoddu_char ee2irqmap[] =
8852286Smdodd	{ 9, 3, 5, 10, 11, 0, 0, 0 };
8959816Smdodd
9059816Smdoddchar plus_irq2eemap[] =
9152286Smdodd	{ -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, -1, -1, -1 };
9259816Smdoddu_char plus_ee2irqmap[] =
9352286Smdodd	{ 3, 4, 5, 7, 9, 10, 11, 12 };
9421769Sjkh
9552286Smdodd/* Network Interface Functions */
9692739Salfredstatic void	ex_init		(void *);
9792739Salfredstatic void	ex_start	(struct ifnet *);
9892739Salfredstatic int	ex_ioctl	(struct ifnet *, u_long, caddr_t);
9992739Salfredstatic void	ex_watchdog	(struct ifnet *);
10021769Sjkh
10157987Smdodd/* ifmedia Functions	*/
10292739Salfredstatic int	ex_ifmedia_upd	(struct ifnet *);
10392739Salfredstatic void	ex_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
10457987Smdodd
10592739Salfredstatic int	ex_get_media	(u_int32_t iobase);
10659816Smdodd
10792739Salfredstatic void	ex_reset	(struct ex_softc *);
108112731Smdoddstatic void	ex_setmulti	(struct ex_softc *);
10952286Smdodd
11092739Salfredstatic void	ex_tx_intr	(struct ex_softc *);
11192739Salfredstatic void	ex_rx_intr	(struct ex_softc *);
11252286Smdodd
11359816Smdoddint
11459816Smdoddlook_for_card (u_int32_t iobase)
11521769Sjkh{
11621769Sjkh	int count1, count2;
11721769Sjkh
11821769Sjkh	/*
11921769Sjkh	 * Check for the i82595 signature, and check that the round robin
12021769Sjkh	 * counter actually advances.
12121769Sjkh	 */
12221769Sjkh	if (((count1 = inb(iobase + ID_REG)) & Id_Mask) != Id_Sig)
12321769Sjkh		return(0);
12421769Sjkh	count2 = inb(iobase + ID_REG);
12521769Sjkh	count2 = inb(iobase + ID_REG);
12621769Sjkh	count2 = inb(iobase + ID_REG);
12752286Smdodd
12821769Sjkh	return((count2 & Counter_bits) == ((count1 + 0xc0) & Counter_bits));
12921769Sjkh}
13021769Sjkh
13159816Smdoddvoid
13252286Smdoddex_get_address (u_int32_t iobase, u_char *enaddr)
13321769Sjkh{
13452286Smdodd	u_int16_t	eaddr_tmp;
13521769Sjkh
13652286Smdodd	eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Lo);
13752286Smdodd	enaddr[5] = eaddr_tmp & 0xff;
13852286Smdodd	enaddr[4] = eaddr_tmp >> 8;
13952286Smdodd	eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Mid);
14052286Smdodd	enaddr[3] = eaddr_tmp & 0xff;
14152286Smdodd	enaddr[2] = eaddr_tmp >> 8;
14252286Smdodd	eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Hi);
14352286Smdodd	enaddr[1] = eaddr_tmp & 0xff;
14452286Smdodd	enaddr[0] = eaddr_tmp >> 8;
14552286Smdodd
14652286Smdodd	return;
14752286Smdodd}
14821769Sjkh
14959816Smdoddint
15052286Smdoddex_card_type (u_char *enaddr)
15152286Smdodd{
15252286Smdodd	if ((enaddr[0] == 0x00) && (enaddr[1] == 0xA0) && (enaddr[2] == 0xC9))
15352286Smdodd		return (CARD_TYPE_EX_10_PLUS);
15452286Smdodd
15552286Smdodd	return (CARD_TYPE_EX_10);
15652286Smdodd}
15752286Smdodd
15855882Smdodd/*
15959816Smdodd * Caller is responsible for eventually calling
16059816Smdodd * ex_release_resources() on failure.
16155882Smdodd */
16259816Smdoddint
16359816Smdoddex_alloc_resources (device_t dev)
16455882Smdodd{
16559816Smdodd	struct ex_softc *	sc = device_get_softc(dev);
16659816Smdodd	int			error = 0;
16755882Smdodd
16859816Smdodd	sc->ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, &sc->ioport_rid,
16959816Smdodd					0, ~0, 1, RF_ACTIVE);
17059816Smdodd	if (!sc->ioport) {
17159816Smdodd		device_printf(dev, "No I/O space?!\n");
17259816Smdodd		error = ENOMEM;
17359816Smdodd		goto bad;
17459816Smdodd	}
17557989Smdodd
17659816Smdodd	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid,
17759816Smdodd					0, ~0, 1, RF_ACTIVE);
17855882Smdodd
17959816Smdodd	if (!sc->irq) {
18059816Smdodd		device_printf(dev, "No IRQ?!\n");
18159816Smdodd		error = ENOMEM;
18259816Smdodd		goto bad;
18355882Smdodd	}
18455882Smdodd
18559816Smdoddbad:
18659816Smdodd	return (error);
18755882Smdodd}
18855882Smdodd
18959816Smdoddvoid
19059816Smdoddex_release_resources (device_t dev)
19152286Smdodd{
19259816Smdodd	struct ex_softc *	sc = device_get_softc(dev);
19352286Smdodd
19459816Smdodd	if (sc->ih) {
19559816Smdodd		bus_teardown_intr(dev, sc->irq, sc->ih);
19659816Smdodd		sc->ih = NULL;
19752286Smdodd	}
19852286Smdodd
19959816Smdodd	if (sc->ioport) {
20059816Smdodd		bus_release_resource(dev, SYS_RES_IOPORT,
20159816Smdodd					sc->ioport_rid, sc->ioport);
20259816Smdodd		sc->ioport = NULL;
20352286Smdodd	}
20452286Smdodd
20559816Smdodd	if (sc->irq) {
20659816Smdodd		bus_release_resource(dev, SYS_RES_IRQ,
20759816Smdodd					sc->irq_rid, sc->irq);
20859816Smdodd		sc->irq = NULL;
20957989Smdodd	}
21057989Smdodd
21159816Smdodd	return;
21252286Smdodd}
21352286Smdodd
21459816Smdoddint
21559816Smdoddex_attach(device_t dev)
21652286Smdodd{
21752286Smdodd	struct ex_softc *	sc = device_get_softc(dev);
21852286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
21957987Smdodd	struct ifmedia *	ifm;
22052286Smdodd	int			unit = device_get_unit(dev);
22157987Smdodd	u_int16_t		temp;
22252286Smdodd
22329877Smsmith	/* work out which set of irq <-> internal tables to use */
22452286Smdodd	if (ex_card_type(sc->arpcom.ac_enaddr) == CARD_TYPE_EX_10_PLUS) {
22529877Smsmith		sc->irq2ee = plus_irq2eemap;
22629877Smsmith		sc->ee2irq = plus_ee2irqmap;
22752286Smdodd	} else {
22829877Smsmith		sc->irq2ee = irq2eemap;
22929877Smsmith		sc->ee2irq = ee2irqmap;
23029877Smsmith	}
23129877Smsmith
23221769Sjkh	sc->mem_size = CARD_RAM_SIZE;	/* XXX This should be read from the card itself. */
23321769Sjkh
23421769Sjkh	/*
23521769Sjkh	 * Initialize the ifnet structure.
23621769Sjkh	 */
23721769Sjkh	ifp->if_softc = sc;
23821769Sjkh	ifp->if_unit = unit;
23921769Sjkh	ifp->if_name = "ex";
24055883Smdodd	ifp->if_mtu = ETHERMTU;
241112731Smdodd	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
24221769Sjkh	ifp->if_output = ether_output;
24321769Sjkh	ifp->if_start = ex_start;
24421769Sjkh	ifp->if_ioctl = ex_ioctl;
24521769Sjkh	ifp->if_watchdog = ex_watchdog;
24655883Smdodd	ifp->if_init = ex_init;
24755883Smdodd	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
24821769Sjkh
24957987Smdodd	ifmedia_init(&sc->ifmedia, 0, ex_ifmedia_upd, ex_ifmedia_sts);
25057987Smdodd
25157987Smdodd	temp = eeprom_read(sc->iobase, EE_W5);
25257987Smdodd	if (temp & EE_W5_PORT_TPE)
25357987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
25457987Smdodd	if (temp & EE_W5_PORT_BNC)
25557987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
25657987Smdodd	if (temp & EE_W5_PORT_AUI)
25757987Smdodd		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
25857987Smdodd
259112764Smdodd	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
260112764Smdodd	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_NONE, 0, NULL);
26157987Smdodd	ifmedia_set(&sc->ifmedia, ex_get_media(sc->iobase));
26257987Smdodd
26357987Smdodd	ifm = &sc->ifmedia;
26457987Smdodd	ifm->ifm_media = ifm->ifm_cur->ifm_media;
26557987Smdodd	ex_ifmedia_upd(ifp);
26657987Smdodd
26721769Sjkh	/*
26821769Sjkh	 * Attach the interface.
26921769Sjkh	 */
270106937Ssam	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
27121769Sjkh
27255883Smdodd	device_printf(sc->dev, "Ethernet address %6D\n",
27355883Smdodd			sc->arpcom.ac_enaddr, ":");
27452286Smdodd
27552286Smdodd	return(0);
27621769Sjkh}
27721769Sjkh
278112800Smdoddint
279112800Smdoddex_detach (device_t dev)
280112800Smdodd{
281112800Smdodd	struct ex_softc	*sc;
282112800Smdodd	struct ifnet	*ifp;
283112800Smdodd
284112800Smdodd	sc = device_get_softc(dev);
285112800Smdodd	ifp = &sc->arpcom.ac_if;
286112800Smdodd
287112800Smdodd        ex_stop(sc);
288112800Smdodd
289112800Smdodd        ifp->if_flags &= ~IFF_RUNNING;
290112800Smdodd	ether_ifdetach(ifp);
291112800Smdodd
292112800Smdodd	ex_release_resources(dev);
293112800Smdodd
294112800Smdodd	return (0);
295112800Smdodd}
296112800Smdodd
29755883Smdoddstatic void
29855883Smdoddex_init(void *xsc)
29921769Sjkh{
30052286Smdodd	struct ex_softc *	sc = (struct ex_softc *) xsc;
30152286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
30252286Smdodd	int			s;
30352286Smdodd	int			i;
30452286Smdodd	register int		iobase = sc->iobase;
30552286Smdodd	unsigned short		temp_reg;
30621769Sjkh
30721769Sjkh	DODEBUG(Start_End, printf("ex_init%d: start\n", ifp->if_unit););
30821769Sjkh
30971999Sphk	if (TAILQ_FIRST(&ifp->if_addrhead) == NULL) {
31052286Smdodd		return;
31152286Smdodd	}
31221769Sjkh	s = splimp();
31359816Smdodd	ifp->if_timer = 0;
31421769Sjkh
31521769Sjkh	/*
31621769Sjkh	 * Load the ethernet address into the card.
31721769Sjkh	 */
31821769Sjkh	outb(iobase + CMD_REG, Bank2_Sel);
31921769Sjkh	temp_reg = inb(iobase + EEPROM_REG);
32052286Smdodd	if (temp_reg & Trnoff_Enable) {
32152286Smdodd		outb(iobase + EEPROM_REG, temp_reg & ~Trnoff_Enable);
32252286Smdodd	}
32352286Smdodd	for (i = 0; i < ETHER_ADDR_LEN; i++) {
32452286Smdodd		outb(iobase + I_ADDR_REG0 + i, sc->arpcom.ac_enaddr[i]);
32552286Smdodd	}
32621769Sjkh	/*
32721769Sjkh	 * - Setup transmit chaining and discard bad received frames.
32821769Sjkh	 * - Match broadcast.
32921769Sjkh	 * - Clear test mode.
33021769Sjkh	 * - Set receiving mode.
33121769Sjkh	 * - Set IRQ number.
33221769Sjkh	 */
33321769Sjkh	outb(iobase + REG1, inb(iobase + REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
33421769Sjkh	outb(iobase + REG2, inb(iobase + REG2) | No_SA_Ins | RX_CRC_InMem);
33526545Sgibbs	outb(iobase + REG3, inb(iobase + REG3) & 0x3f /* XXX constants. */ );
33621769Sjkh	outb(iobase + CMD_REG, Bank1_Sel);
33729877Smsmith	outb(iobase + INT_NO_REG, (inb(iobase + INT_NO_REG) & 0xf8) | sc->irq2ee[sc->irq_no]);
33821769Sjkh
33921769Sjkh	/*
34021769Sjkh	 * Divide the available memory in the card into rcv and xmt buffers.
34121769Sjkh	 * By default, I use the first 3/4 of the memory for the rcv buffer,
34221769Sjkh	 * and the remaining 1/4 of the memory for the xmt buffer.
34321769Sjkh	 */
34421769Sjkh	sc->rx_mem_size = sc->mem_size * 3 / 4;
34521769Sjkh	sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
34621769Sjkh	sc->rx_lower_limit = 0x0000;
34721769Sjkh	sc->rx_upper_limit = sc->rx_mem_size - 2;
34821769Sjkh	sc->tx_lower_limit = sc->rx_mem_size;
34921769Sjkh	sc->tx_upper_limit = sc->mem_size - 2;
35021769Sjkh	outb(iobase + RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
35121769Sjkh	outb(iobase + RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
35221769Sjkh	outb(iobase + XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
35321769Sjkh	outb(iobase + XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
35421769Sjkh
35521769Sjkh	/*
35621769Sjkh	 * Enable receive and transmit interrupts, and clear any pending int.
35721769Sjkh	 */
35821769Sjkh	outb(iobase + REG1, inb(iobase + REG1) | TriST_INT);
35921769Sjkh	outb(iobase + CMD_REG, Bank0_Sel);
36021769Sjkh	outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
36121769Sjkh	outb(iobase + STATUS_REG, All_Int);
36221769Sjkh
36321769Sjkh	/*
36421769Sjkh	 * Initialize receive and transmit ring buffers.
36521769Sjkh	 */
36621769Sjkh	outw(iobase + RCV_BAR, sc->rx_lower_limit);
36721769Sjkh	sc->rx_head = sc->rx_lower_limit;
36826545Sgibbs	outw(iobase + RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
36921769Sjkh	outw(iobase + XMT_BAR, sc->tx_lower_limit);
37021769Sjkh	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
37121769Sjkh
37221769Sjkh	ifp->if_flags |= IFF_RUNNING;
37321769Sjkh	ifp->if_flags &= ~IFF_OACTIVE;
37421769Sjkh	DODEBUG(Status, printf("OIDLE init\n"););
37521769Sjkh
376112731Smdodd	ex_setmulti(sc);
377112731Smdodd
37821769Sjkh	/*
37921769Sjkh	 * Final reset of the board, and enable operation.
38021769Sjkh	 */
38121769Sjkh	outb(iobase + CMD_REG, Sel_Reset_CMD);
38221769Sjkh	DELAY(2);
38321769Sjkh	outb(iobase + CMD_REG, Rcv_Enable_CMD);
38421769Sjkh
38521769Sjkh	ex_start(ifp);
38621769Sjkh	splx(s);
38721769Sjkh
38821769Sjkh	DODEBUG(Start_End, printf("ex_init%d: finish\n", ifp->if_unit););
38921769Sjkh}
39021769Sjkh
39121769Sjkh
39255883Smdoddstatic void
39352286Smdoddex_start(struct ifnet *ifp)
39421769Sjkh{
39552286Smdodd	struct ex_softc *	sc = ifp->if_softc;
39652286Smdodd	int			iobase = sc->iobase;
39752286Smdodd	int			i, s, len, data_len, avail, dest, next;
39852286Smdodd	unsigned char		tmp16[2];
39952286Smdodd	struct mbuf *		opkt;
40052286Smdodd	struct mbuf *		m;
40121769Sjkh
40252286Smdodd	DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
40321769Sjkh
40452286Smdodd	s = splimp();
40521769Sjkh
40652286Smdodd	/*
40752286Smdodd	 * Main loop: send outgoing packets to network card until there are no
40852286Smdodd	 * more packets left, or the card cannot accept any more yet.
40952286Smdodd	 */
41052286Smdodd	while (((opkt = ifp->if_snd.ifq_head) != NULL) &&
41152286Smdodd	       !(ifp->if_flags & IFF_OACTIVE)) {
41221769Sjkh
41352286Smdodd		/*
41452286Smdodd		 * Ensure there is enough free transmit buffer space for
41552286Smdodd		 * this packet, including its header. Note: the header
41652286Smdodd		 * cannot wrap around the end of the transmit buffer and
41752286Smdodd		 * must be kept together, so we allow space for twice the
41852286Smdodd		 * length of the header, just in case.
41952286Smdodd		 */
42021769Sjkh
42152286Smdodd		for (len = 0, m = opkt; m != NULL; m = m->m_next) {
42252286Smdodd			len += m->m_len;
42352286Smdodd		}
42452286Smdodd
42552286Smdodd		data_len = len;
42652286Smdodd
42752286Smdodd		DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
42852286Smdodd
42952286Smdodd		if (len & 1) {
43052286Smdodd			len += XMT_HEADER_LEN + 1;
43152286Smdodd		} else {
43252286Smdodd			len += XMT_HEADER_LEN;
43352286Smdodd		}
43452286Smdodd
43552286Smdodd		if ((i = sc->tx_tail - sc->tx_head) >= 0) {
43652286Smdodd			avail = sc->tx_mem_size - i;
43752286Smdodd		} else {
43852286Smdodd			avail = -i;
43952286Smdodd		}
44052286Smdodd
44152286Smdodd		DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
44252286Smdodd
44352286Smdodd		if (avail >= len + XMT_HEADER_LEN) {
44452286Smdodd			IF_DEQUEUE(&ifp->if_snd, opkt);
44552286Smdodd
44621769Sjkh#ifdef EX_PSA_INTR
44752286Smdodd			/*
44852286Smdodd			 * Disable rx and tx interrupts, to avoid corruption
44952286Smdodd			 * of the host address register by interrupt service
45052286Smdodd			 * routines.
45152286Smdodd			 * XXX Is this necessary with splimp() enabled?
45252286Smdodd			 */
45352286Smdodd			outb(iobase + MASK_REG, All_Int);
45421769Sjkh#endif
45521769Sjkh
45652286Smdodd			/*
45752286Smdodd			 * Compute the start and end addresses of this
45852286Smdodd			 * frame in the tx buffer.
45952286Smdodd			 */
46052286Smdodd			dest = sc->tx_tail;
46152286Smdodd			next = dest + len;
46221769Sjkh
46352286Smdodd			if (next > sc->tx_upper_limit) {
46452286Smdodd				if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
46552286Smdodd				    XMT_HEADER_LEN) {
46652286Smdodd					dest = sc->tx_lower_limit;
46752286Smdodd					next = dest + len;
46855881Smdodd				} else {
46955881Smdodd					next = sc->tx_lower_limit +
47055881Smdodd						next - sc->tx_upper_limit - 2;
47152286Smdodd				}
47252286Smdodd			}
47321769Sjkh
47452286Smdodd			/*
47552286Smdodd			 * Build the packet frame in the card's ring buffer.
47652286Smdodd			 */
47752286Smdodd			DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
47821769Sjkh
47952286Smdodd			outw(iobase + HOST_ADDR_REG, dest);
48052286Smdodd			outw(iobase + IO_PORT_REG, Transmit_CMD);
48152286Smdodd			outw(iobase + IO_PORT_REG, 0);
48252286Smdodd			outw(iobase + IO_PORT_REG, next);
48352286Smdodd			outw(iobase + IO_PORT_REG, data_len);
48421769Sjkh
48552286Smdodd			/*
48652286Smdodd			 * Output the packet data to the card. Ensure all
48752286Smdodd			 * transfers are 16-bit wide, even if individual
48852286Smdodd			 * mbufs have odd length.
48952286Smdodd			 */
49021769Sjkh
49152286Smdodd			for (m = opkt, i = 0; m != NULL; m = m->m_next) {
49252286Smdodd				DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
49352286Smdodd				if (i) {
49452286Smdodd					tmp16[1] = *(mtod(m, caddr_t));
49552286Smdodd					outsw(iobase + IO_PORT_REG, tmp16, 1);
49652286Smdodd				}
49752286Smdodd				outsw(iobase + IO_PORT_REG,
49852286Smdodd				      mtod(m, caddr_t) + i, (m->m_len - i) / 2);
49952286Smdodd
50052286Smdodd				if ((i = (m->m_len - i) & 1) != 0) {
50152286Smdodd					tmp16[0] = *(mtod(m, caddr_t) +
50252286Smdodd						   m->m_len - 1);
50352286Smdodd				}
50452286Smdodd			}
50552286Smdodd			if (i) {
50652286Smdodd				outsw(iobase + IO_PORT_REG, tmp16, 1);
50755881Smdodd			}
50852286Smdodd
50955881Smdodd			/*
51055881Smdodd			 * If there were other frames chained, update the
51155881Smdodd			 * chain in the last one.
51255881Smdodd			 */
51355881Smdodd			if (sc->tx_head != sc->tx_tail) {
51455881Smdodd				if (sc->tx_tail != dest) {
51552286Smdodd					outw(iobase + HOST_ADDR_REG,
51655881Smdodd					     sc->tx_last + XMT_Chain_Point);
51755881Smdodd					outw(iobase + IO_PORT_REG, dest);
51852286Smdodd				}
51955881Smdodd				outw(iobase + HOST_ADDR_REG,
52055881Smdodd				     sc->tx_last + XMT_Byte_Count);
52155881Smdodd				i = inw(iobase + IO_PORT_REG);
52255881Smdodd				outw(iobase + HOST_ADDR_REG,
52355881Smdodd				     sc->tx_last + XMT_Byte_Count);
52455881Smdodd				outw(iobase + IO_PORT_REG, i | Ch_bit);
52555881Smdodd			}
52655881Smdodd
52755881Smdodd			/*
52855881Smdodd			 * Resume normal operation of the card:
52955881Smdodd			 * - Make a dummy read to flush the DRAM write
53055881Smdodd			 *   pipeline.
53155881Smdodd			 * - Enable receive and transmit interrupts.
53255881Smdodd			 * - Send Transmit or Resume_XMT command, as
53355881Smdodd			 *   appropriate.
53455881Smdodd			 */
53555881Smdodd			inw(iobase + IO_PORT_REG);
53621769Sjkh#ifdef EX_PSA_INTR
53755881Smdodd			outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
53821769Sjkh#endif
53955881Smdodd			if (sc->tx_head == sc->tx_tail) {
54055881Smdodd				outw(iobase + XMT_BAR, dest);
54155881Smdodd				outb(iobase + CMD_REG, Transmit_CMD);
54255881Smdodd				sc->tx_head = dest;
54355881Smdodd				DODEBUG(Sent_Pkts, printf("Transmit\n"););
54452286Smdodd			} else {
54555881Smdodd				outb(iobase + CMD_REG, Resume_XMT_List_CMD);
54655881Smdodd				DODEBUG(Sent_Pkts, printf("Resume\n"););
54752286Smdodd			}
54855881Smdodd
54955881Smdodd			sc->tx_last = dest;
55055881Smdodd			sc->tx_tail = next;
55155881Smdodd
552106937Ssam			BPF_MTAP(ifp, opkt);
55355881Smdodd
55455881Smdodd			ifp->if_timer = 2;
55555881Smdodd			ifp->if_opackets++;
55655881Smdodd			m_freem(opkt);
55755881Smdodd		} else {
55855881Smdodd			ifp->if_flags |= IFF_OACTIVE;
55955881Smdodd			DODEBUG(Status, printf("OACTIVE start\n"););
56052286Smdodd		}
56121769Sjkh	}
56221769Sjkh
56352286Smdodd	splx(s);
56421769Sjkh
56552286Smdodd	DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
56621769Sjkh}
56721769Sjkh
56866440Simpvoid
56952286Smdoddex_stop(struct ex_softc *sc)
57021769Sjkh{
57152286Smdodd	int iobase = sc->iobase;
57221769Sjkh
57352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
57421769Sjkh
57552286Smdodd	/*
57652286Smdodd	 * Disable card operation:
57752286Smdodd	 * - Disable the interrupt line.
57852286Smdodd	 * - Flush transmission and disable reception.
57952286Smdodd	 * - Mask and clear all interrupts.
58052286Smdodd	 * - Reset the 82595.
58152286Smdodd	 */
58252286Smdodd	outb(iobase + CMD_REG, Bank1_Sel);
58352286Smdodd	outb(iobase + REG1, inb(iobase + REG1) & ~TriST_INT);
58452286Smdodd	outb(iobase + CMD_REG, Bank0_Sel);
58552286Smdodd	outb(iobase + CMD_REG, Rcv_Stop);
58652286Smdodd	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
58752286Smdodd	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. */
58852286Smdodd	outb(iobase + MASK_REG, All_Int);
58952286Smdodd	outb(iobase + STATUS_REG, All_Int);
59052286Smdodd	outb(iobase + CMD_REG, Reset_CMD);
59152286Smdodd	DELAY(200);
59221769Sjkh
59352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
59452286Smdodd
59552286Smdodd	return;
59621769Sjkh}
59721769Sjkh
59859816Smdoddvoid
59955883Smdoddex_intr(void *arg)
60021769Sjkh{
60152286Smdodd	struct ex_softc *	sc = (struct ex_softc *)arg;
60252286Smdodd	struct ifnet *	ifp = &sc->arpcom.ac_if;
60352286Smdodd	int			iobase = sc->iobase;
60452286Smdodd	int			int_status, send_pkts;
60521769Sjkh
60655883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: start\n", unit););
60721769Sjkh
60821769Sjkh#ifdef EXDEBUG
60921769Sjkh	if (++exintr_count != 1)
61021769Sjkh		printf("WARNING: nested interrupt (%d). Mail the author.\n", exintr_count);
61121769Sjkh#endif
61221769Sjkh
61352286Smdodd	send_pkts = 0;
61452286Smdodd	while ((int_status = inb(iobase + STATUS_REG)) & (Tx_Int | Rx_Int)) {
61552286Smdodd		if (int_status & Rx_Int) {
61652286Smdodd			outb(iobase + STATUS_REG, Rx_Int);
61721769Sjkh
61852286Smdodd			ex_rx_intr(sc);
61952286Smdodd		} else if (int_status & Tx_Int) {
62052286Smdodd			outb(iobase + STATUS_REG, Tx_Int);
62121769Sjkh
62252286Smdodd			ex_tx_intr(sc);
62352286Smdodd			send_pkts = 1;
62452286Smdodd		}
62552286Smdodd	}
62621769Sjkh
62752286Smdodd	/*
62852286Smdodd	 * If any packet has been transmitted, and there are queued packets to
62952286Smdodd	 * be sent, attempt to send more packets to the network card.
63052286Smdodd	 */
63152286Smdodd
63252286Smdodd	if (send_pkts && (ifp->if_snd.ifq_head != NULL)) {
63352286Smdodd		ex_start(ifp);
63452286Smdodd	}
63552286Smdodd
63621769Sjkh#ifdef EXDEBUG
63721769Sjkh	exintr_count--;
63821769Sjkh#endif
63921769Sjkh
64055883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: finish\n", unit););
64152286Smdodd
64252286Smdodd	return;
64321769Sjkh}
64421769Sjkh
64552286Smdoddstatic void
64652286Smdoddex_tx_intr(struct ex_softc *sc)
64721769Sjkh{
64852286Smdodd	struct ifnet *	ifp = &sc->arpcom.ac_if;
64952286Smdodd	int		iobase = sc->iobase;
65052286Smdodd	int		tx_status;
65121769Sjkh
65252286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
65321769Sjkh
65452286Smdodd	/*
65552286Smdodd	 * - Cancel the watchdog.
65652286Smdodd	 * For all packets transmitted since last transmit interrupt:
65752286Smdodd	 * - Advance chain pointer to next queued packet.
65852286Smdodd	 * - Update statistics.
65952286Smdodd	 */
66021769Sjkh
66152286Smdodd	ifp->if_timer = 0;
66221769Sjkh
66352286Smdodd	while (sc->tx_head != sc->tx_tail) {
66452286Smdodd		outw(iobase + HOST_ADDR_REG, sc->tx_head);
66521769Sjkh
66652286Smdodd		if (! inw(iobase + IO_PORT_REG) & Done_bit)
66752286Smdodd			break;
66821769Sjkh
66952286Smdodd		tx_status = inw(iobase + IO_PORT_REG);
67052286Smdodd		sc->tx_head = inw(iobase + IO_PORT_REG);
67152286Smdodd
67252286Smdodd		if (tx_status & TX_OK_bit) {
67352286Smdodd			ifp->if_opackets++;
67452286Smdodd		} else {
67552286Smdodd			ifp->if_oerrors++;
67652286Smdodd		}
67752286Smdodd
67852286Smdodd		ifp->if_collisions += tx_status & No_Collisions_bits;
67952286Smdodd	}
68052286Smdodd
68152286Smdodd	/*
68252286Smdodd	 * The card should be ready to accept more packets now.
68352286Smdodd	 */
68452286Smdodd
68552286Smdodd	ifp->if_flags &= ~IFF_OACTIVE;
68652286Smdodd
68752286Smdodd	DODEBUG(Status, printf("OIDLE tx_intr\n"););
68852286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
68952286Smdodd
69052286Smdodd	return;
69121769Sjkh}
69221769Sjkh
69352286Smdoddstatic void
69452286Smdoddex_rx_intr(struct ex_softc *sc)
69521769Sjkh{
69652286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
69752286Smdodd	int			iobase = sc->iobase;
69852286Smdodd	int			rx_status;
69952286Smdodd	int			pkt_len;
70052286Smdodd	int			QQQ;
70152286Smdodd	struct mbuf *		m;
70252286Smdodd	struct mbuf *		ipkt;
70352286Smdodd	struct ether_header *	eh;
70421769Sjkh
70552286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
70621769Sjkh
70752286Smdodd	/*
70852286Smdodd	 * For all packets received since last receive interrupt:
70952286Smdodd	 * - If packet ok, read it into a new mbuf and queue it to interface,
71052286Smdodd	 *   updating statistics.
71152286Smdodd	 * - If packet bad, just discard it, and update statistics.
71252286Smdodd	 * Finally, advance receive stop limit in card's memory to new location.
71352286Smdodd	 */
71421769Sjkh
71552286Smdodd	outw(iobase + HOST_ADDR_REG, sc->rx_head);
71621769Sjkh
71752286Smdodd	while (inw(iobase + IO_PORT_REG) == RCV_Done) {
71852286Smdodd
71952286Smdodd		rx_status = inw(iobase + IO_PORT_REG);
72052286Smdodd		sc->rx_head = inw(iobase + IO_PORT_REG);
72152286Smdodd		QQQ = pkt_len = inw(iobase + IO_PORT_REG);
72252286Smdodd
72352286Smdodd		if (rx_status & RCV_OK_bit) {
724111119Simp			MGETHDR(m, M_DONTWAIT, MT_DATA);
72552286Smdodd			ipkt = m;
72652286Smdodd			if (ipkt == NULL) {
72752286Smdodd				ifp->if_iqdrops++;
72852286Smdodd			} else {
72952286Smdodd				ipkt->m_pkthdr.rcvif = ifp;
73052286Smdodd				ipkt->m_pkthdr.len = pkt_len;
73152286Smdodd				ipkt->m_len = MHLEN;
73252286Smdodd
73352286Smdodd				while (pkt_len > 0) {
73452286Smdodd					if (pkt_len > MINCLSIZE) {
735111119Simp						MCLGET(m, M_DONTWAIT);
73652286Smdodd						if (m->m_flags & M_EXT) {
73752286Smdodd							m->m_len = MCLBYTES;
73852286Smdodd						} else {
73952286Smdodd							m_freem(ipkt);
74052286Smdodd							ifp->if_iqdrops++;
74152286Smdodd							goto rx_another;
74252286Smdodd						}
74352286Smdodd					}
74452286Smdodd					m->m_len = min(m->m_len, pkt_len);
74552286Smdodd
74621769Sjkh	  /*
74721769Sjkh	   * NOTE: I'm assuming that all mbufs allocated are of even length,
74821769Sjkh	   * except for the last one in an odd-length packet.
74921769Sjkh	   */
75052286Smdodd
75152286Smdodd					insw(iobase + IO_PORT_REG,
75252286Smdodd					     mtod(m, caddr_t), m->m_len / 2);
75352286Smdodd
75452286Smdodd					if (m->m_len & 1) {
75552286Smdodd						*(mtod(m, caddr_t) + m->m_len - 1) = inb(iobase + IO_PORT_REG);
75652286Smdodd					}
75752286Smdodd					pkt_len -= m->m_len;
75852286Smdodd
75952286Smdodd					if (pkt_len > 0) {
760111119Simp						MGET(m->m_next, M_DONTWAIT, MT_DATA);
76152286Smdodd						if (m->m_next == NULL) {
76252286Smdodd							m_freem(ipkt);
76352286Smdodd							ifp->if_iqdrops++;
76452286Smdodd							goto rx_another;
76552286Smdodd						}
76652286Smdodd						m = m->m_next;
76752286Smdodd						m->m_len = MLEN;
76852286Smdodd					}
76952286Smdodd				}
77052286Smdodd				eh = mtod(ipkt, struct ether_header *);
77152286Smdodd#ifdef EXDEBUG
77252286Smdodd	if (debug_mask & Rcvd_Pkts) {
77352286Smdodd		if ((eh->ether_dhost[5] != 0xff) || (eh->ether_dhost[0] != 0xff)) {
77452286Smdodd			printf("Receive packet with %d data bytes: %6D -> ", QQQ, eh->ether_shost, ":");
77552286Smdodd			printf("%6D\n", eh->ether_dhost, ":");
77652286Smdodd		} /* QQQ */
77721769Sjkh	}
77821769Sjkh#endif
779106937Ssam				(*ifp->if_input)(ifp, ipkt);
78052286Smdodd				ifp->if_ipackets++;
78152286Smdodd			}
78252286Smdodd		} else {
78352286Smdodd			ifp->if_ierrors++;
78421769Sjkh		}
78552286Smdodd		outw(iobase + HOST_ADDR_REG, sc->rx_head);
78652286Smdoddrx_another: ;
78721769Sjkh	}
78821769Sjkh
78952286Smdodd	if (sc->rx_head < sc->rx_lower_limit + 2)
79052286Smdodd		outw(iobase + RCV_STOP_REG, sc->rx_upper_limit);
79152286Smdodd	else
79252286Smdodd		outw(iobase + RCV_STOP_REG, sc->rx_head - 2);
79352286Smdodd
79452286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
79552286Smdodd
79652286Smdodd	return;
79721769Sjkh}
79821769Sjkh
79921769Sjkh
80055883Smdoddstatic int
80155883Smdoddex_ioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
80221769Sjkh{
80352286Smdodd	struct ex_softc *	sc = ifp->if_softc;
80457987Smdodd	struct ifreq *		ifr = (struct ifreq *)data;
80552286Smdodd	int			s;
80652286Smdodd	int			error = 0;
80721769Sjkh
80852286Smdodd	DODEBUG(Start_End, printf("ex_ioctl%d: start ", ifp->if_unit););
80921769Sjkh
81052286Smdodd	s = splimp();
81121769Sjkh
81252286Smdodd	switch(cmd) {
81352286Smdodd		case SIOCSIFADDR:
81452286Smdodd		case SIOCGIFADDR:
81552286Smdodd		case SIOCSIFMTU:
81652286Smdodd			error = ether_ioctl(ifp, cmd, data);
81752286Smdodd			break;
81821769Sjkh
81952286Smdodd		case SIOCSIFFLAGS:
82052286Smdodd			DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
82152286Smdodd			if ((ifp->if_flags & IFF_UP) == 0 &&
82252286Smdodd			    (ifp->if_flags & IFF_RUNNING)) {
82352286Smdodd
82452286Smdodd				ifp->if_flags &= ~IFF_RUNNING;
82552286Smdodd				ex_stop(sc);
82652286Smdodd			} else {
82752286Smdodd      				ex_init(sc);
82852286Smdodd			}
82952286Smdodd			break;
83021769Sjkh#ifdef NODEF
83152286Smdodd		case SIOCGHWADDR:
83252286Smdodd			DODEBUG(Start_End, printf("SIOCGHWADDR"););
83352286Smdodd			bcopy((caddr_t)sc->sc_addr, (caddr_t)&ifr->ifr_data,
83452286Smdodd			      sizeof(sc->sc_addr));
83552286Smdodd			break;
83621769Sjkh#endif
83752286Smdodd		case SIOCADDMULTI:
83852286Smdodd		case SIOCDELMULTI:
839112731Smdodd			ex_init(sc);
840112731Smdodd			error = 0;
84152286Smdodd			break;
84257987Smdodd		case SIOCSIFMEDIA:
84357987Smdodd		case SIOCGIFMEDIA:
84457987Smdodd			error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, cmd);
84557987Smdodd			break;
84652286Smdodd		default:
84752286Smdodd			DODEBUG(Start_End, printf("unknown"););
84852286Smdodd			error = EINVAL;
84952286Smdodd	}
85021769Sjkh
85152286Smdodd	splx(s);
85221769Sjkh
85352286Smdodd	DODEBUG(Start_End, printf("\nex_ioctl%d: finish\n", ifp->if_unit););
85452286Smdodd
85552286Smdodd	return(error);
85621769Sjkh}
85721769Sjkh
858112731Smdoddstatic void
859112731Smdoddex_setmulti(struct ex_softc *sc)
860112731Smdodd{
861112731Smdodd	struct ifnet *ifp;
862112731Smdodd	struct ifmultiaddr *maddr;
863112731Smdodd	u_int16_t *addr;
864112731Smdodd	int iobase = sc->iobase;
865112731Smdodd	int count;
866112731Smdodd	int timeout, status;
867112731Smdodd
868112731Smdodd	ifp = &sc->arpcom.ac_if;
86921769Sjkh
870112731Smdodd	count = 0;
871112731Smdodd	TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
872112731Smdodd		if (maddr->ifma_addr->sa_family != AF_LINK)
873112731Smdodd			continue;
874112731Smdodd		count++;
875112731Smdodd	}
876112731Smdodd
877112731Smdodd	if ((ifp->if_flags & IFF_PROMISC) || (ifp->if_flags & IFF_ALLMULTI)
878112731Smdodd			|| count > 63) {
879112731Smdodd		/* Interface is in promiscuous mode or there are too many
880112731Smdodd		 * multicast addresses for the card to handle */
881112731Smdodd		outb(iobase + CMD_REG, Bank2_Sel);
882112731Smdodd		outb(iobase + REG2, inb(iobase + REG2) | Promisc_Mode);
883112731Smdodd		outb(iobase + REG3, inb(iobase + REG3));
884112731Smdodd		outb(iobase + CMD_REG, Bank0_Sel);
885112731Smdodd	}
886112731Smdodd	else if ((ifp->if_flags & IFF_MULTICAST) && (count > 0)) {
887112731Smdodd		/* Program multicast addresses plus our MAC address
888112731Smdodd		 * into the filter */
889112731Smdodd		outb(iobase + CMD_REG, Bank2_Sel);
890112731Smdodd		outb(iobase + REG2, inb(iobase + REG2) | Multi_IA);
891112731Smdodd		outb(iobase + REG3, inb(iobase + REG3));
892112731Smdodd		outb(iobase + CMD_REG, Bank0_Sel);
893112731Smdodd
894112731Smdodd		/* Borrow space from TX buffer; this should be safe
895112731Smdodd		 * as this is only called from ex_init */
896112731Smdodd
897112731Smdodd		outw(iobase + HOST_ADDR_REG, sc->tx_lower_limit);
898112731Smdodd		outw(iobase + IO_PORT_REG, MC_Setup_CMD);
899112731Smdodd		outw(iobase + IO_PORT_REG, 0);
900112731Smdodd		outw(iobase + IO_PORT_REG, 0);
901112731Smdodd		outw(iobase + IO_PORT_REG, (count + 1) * 6);
902112731Smdodd
903112731Smdodd		TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
904112731Smdodd			if (maddr->ifma_addr->sa_family != AF_LINK)
905112731Smdodd				continue;
906112731Smdodd
907112731Smdodd			addr = (u_int16_t*)LLADDR((struct sockaddr_dl *)
908112731Smdodd					maddr->ifma_addr);
909112731Smdodd			outw(iobase + IO_PORT_REG, *addr++);
910112731Smdodd			outw(iobase + IO_PORT_REG, *addr++);
911112731Smdodd			outw(iobase + IO_PORT_REG, *addr++);
912112731Smdodd		}
913112731Smdodd
914112731Smdodd		/* Program our MAC address as well */
915112731Smdodd		/* XXX: Is this necessary?  The Linux driver does this
916112731Smdodd		 * but the NetBSD driver does not */
917112731Smdodd		addr = (u_int16_t*)(&sc->arpcom.ac_enaddr);
918112731Smdodd		outw(iobase + IO_PORT_REG, *addr++);
919112731Smdodd		outw(iobase + IO_PORT_REG, *addr++);
920112731Smdodd		outw(iobase + IO_PORT_REG, *addr++);
921112731Smdodd
922112731Smdodd		inw(iobase + IO_PORT_REG);
923112731Smdodd		outw(iobase + XMT_BAR, sc->tx_lower_limit);
924112731Smdodd		outb(iobase + CMD_REG, MC_Setup_CMD);
925112731Smdodd
926112731Smdodd		sc->tx_head = sc->tx_lower_limit;
927112731Smdodd		sc->tx_tail = sc->tx_head + XMT_HEADER_LEN + (count + 1) * 6;
928112731Smdodd
929112731Smdodd		for (timeout=0; timeout<100; timeout++) {
930112731Smdodd			DELAY(2);
931112731Smdodd			if ((inb(iobase + STATUS_REG) & Exec_Int) == 0)
932112731Smdodd				continue;
933112731Smdodd
934112731Smdodd			status = inb(iobase + CMD_REG);
935112731Smdodd			outb(iobase + STATUS_REG, Exec_Int);
936112731Smdodd			break;
937112731Smdodd		}
938112731Smdodd
939112731Smdodd		sc->tx_head = sc->tx_tail;
940112731Smdodd	}
941112731Smdodd	else
942112731Smdodd	{
943112731Smdodd		/* No multicast or promiscuous mode */
944112731Smdodd		outb(iobase + CMD_REG, Bank2_Sel);
945112731Smdodd		outb(iobase + REG2, inb(iobase + REG2) & 0xDE);
946112731Smdodd			/* ~(Multi_IA | Promisc_Mode) */
947112731Smdodd		outb(iobase + REG3, inb(iobase + REG3));
948112731Smdodd		outb(iobase + CMD_REG, Bank0_Sel);
949112731Smdodd	}
950112731Smdodd}
951112731Smdodd
95252286Smdoddstatic void
95352286Smdoddex_reset(struct ex_softc *sc)
95421769Sjkh{
95552286Smdodd	int s;
95621769Sjkh
95752286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
95821769Sjkh
95952286Smdodd	s = splimp();
96021769Sjkh
96152286Smdodd	ex_stop(sc);
96252286Smdodd	ex_init(sc);
96321769Sjkh
96452286Smdodd	splx(s);
96521769Sjkh
96652286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
96752286Smdodd
96852286Smdodd	return;
96921769Sjkh}
97021769Sjkh
97152286Smdoddstatic void
97252286Smdoddex_watchdog(struct ifnet *ifp)
97321769Sjkh{
97452286Smdodd	struct ex_softc *	sc = ifp->if_softc;
97521769Sjkh
97652286Smdodd	DODEBUG(Start_End, printf("ex_watchdog%d: start\n", ifp->if_unit););
97721769Sjkh
97852286Smdodd	ifp->if_flags &= ~IFF_OACTIVE;
97921769Sjkh
98052286Smdodd	DODEBUG(Status, printf("OIDLE watchdog\n"););
98152286Smdodd
98252286Smdodd	ifp->if_oerrors++;
98352286Smdodd	ex_reset(sc);
98452286Smdodd	ex_start(ifp);
98552286Smdodd
98652286Smdodd	DODEBUG(Start_End, printf("ex_watchdog%d: finish\n", ifp->if_unit););
98752286Smdodd
98852286Smdodd	return;
98921769Sjkh}
99021769Sjkh
99157987Smdoddstatic int
99259816Smdoddex_get_media (u_int32_t iobase)
99359816Smdodd{
994112764Smdodd	int	current;
995112764Smdodd	int	media;
99659816Smdodd
997112764Smdodd	media = eeprom_read(iobase, EE_W5);
998112764Smdodd
99959816Smdodd	outb(iobase + CMD_REG, Bank2_Sel);
1000112764Smdodd	current = inb(iobase + REG3);
100159816Smdodd	outb(iobase + CMD_REG, Bank0_Sel);
100259816Smdodd
1003112764Smdodd	if ((current & TPE_bit) && (media & EE_W5_PORT_TPE))
100459816Smdodd		return(IFM_ETHER|IFM_10_T);
1005112764Smdodd	if ((current & BNC_bit) && (media & EE_W5_PORT_BNC))
100659816Smdodd		return(IFM_ETHER|IFM_10_2);
100759816Smdodd
1008112764Smdodd	if (media & EE_W5_PORT_AUI)
1009112764Smdodd		return (IFM_ETHER|IFM_10_5);
1010112764Smdodd
1011112764Smdodd	return (IFM_ETHER|IFM_AUTO);
101259816Smdodd}
101359816Smdodd
101459816Smdoddstatic int
101557987Smdoddex_ifmedia_upd (ifp)
101657987Smdodd	struct ifnet *		ifp;
101757987Smdodd{
1018112764Smdodd	struct ex_softc *       sc = ifp->if_softc;
101921769Sjkh
1020112764Smdodd	if (IFM_TYPE(sc->ifmedia.ifm_media) != IFM_ETHER)
1021112764Smdodd		return EINVAL;
1022112764Smdodd
102357987Smdodd	return (0);
102457987Smdodd}
102557987Smdodd
102657987Smdoddstatic void
102757987Smdoddex_ifmedia_sts(ifp, ifmr)
102857987Smdodd	struct ifnet *          ifp;
102957987Smdodd	struct ifmediareq *     ifmr;
103057987Smdodd{
103157987Smdodd	struct ex_softc *       sc = ifp->if_softc;
103257987Smdodd
103357987Smdodd	ifmr->ifm_active = ex_get_media(sc->iobase);
1034112764Smdodd	ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
103557987Smdodd
103657987Smdodd	return;
103757987Smdodd}
103857987Smdodd
103959816Smdoddu_short
104059816Smdoddeeprom_read(u_int32_t iobase, int location)
104121769Sjkh{
104221769Sjkh	int i;
104321769Sjkh	u_short data = 0;
104421769Sjkh	int ee_addr;
104521769Sjkh	int read_cmd = location | EE_READ_CMD;
104621769Sjkh	short ctrl_val = EECS;
104721769Sjkh
104821769Sjkh	ee_addr = iobase + EEPROM_REG;
104921769Sjkh	outb(iobase + CMD_REG, Bank2_Sel);
105021769Sjkh	outb(ee_addr, EECS);
105121769Sjkh	for (i = 8; i >= 0; i--) {
105221769Sjkh		short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
105321769Sjkh		outb(ee_addr, outval);
105421769Sjkh		outb(ee_addr, outval | EESK);
105521769Sjkh		DELAY(3);
105621769Sjkh		outb(ee_addr, outval);
105721769Sjkh		DELAY(2);
105821769Sjkh	}
105921769Sjkh	outb(ee_addr, ctrl_val);
106021769Sjkh
106121769Sjkh	for (i = 16; i > 0; i--) {
106221769Sjkh		outb(ee_addr, ctrl_val | EESK);
106321769Sjkh		DELAY(3);
106421769Sjkh		data = (data << 1) | ((inb(ee_addr) & EEDO) ? 1 : 0);
106521769Sjkh		outb(ee_addr, ctrl_val);
106621769Sjkh		DELAY(2);
106721769Sjkh	}
106821769Sjkh
106921769Sjkh	ctrl_val &= ~EECS;
107021769Sjkh	outb(ee_addr, ctrl_val | EESK);
107121769Sjkh	DELAY(3);
107221769Sjkh	outb(ee_addr, ctrl_val);
107321769Sjkh	DELAY(2);
107421769Sjkh	outb(iobase + CMD_REG, Bank0_Sel);
107521769Sjkh	return(data);
107621769Sjkh}
1077