if_ex.c revision 55882
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 55882 2000-01-13 06:52:51Z 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 *
3821769Sjkh * 30-Oct-1996: first beta version. Inet and BPF supported, but no multicast.
3921769Sjkh */
4021769Sjkh
4121769Sjkh#include "ex.h"
4221769Sjkh#if NEX > 0
4321769Sjkh
4421769Sjkh#include <sys/param.h>
4521769Sjkh#include <sys/systm.h>
4652286Smdodd#include <sys/kernel.h>
4721769Sjkh#include <sys/conf.h>
4824204Sbde#include <sys/sockio.h>
4921769Sjkh#include <sys/mbuf.h>
5021769Sjkh#include <sys/socket.h>
5121769Sjkh
5252286Smdodd#include <sys/module.h>
5352286Smdodd#include <sys/bus.h>
5452286Smdodd
5552286Smdodd#include <machine/bus.h>
5652286Smdodd#include <machine/resource.h>
5752286Smdodd#include <sys/rman.h>
5852286Smdodd
5950026Smdodd#include <net/ethernet.h>
6021769Sjkh#include <net/if.h>
6121769Sjkh
6250026Smdodd#include <netinet/in.h>
6350026Smdodd#include <netinet/if_ether.h>
6450026Smdodd
6521769Sjkh#include <net/bpf.h>
6621769Sjkh
6721769Sjkh#include <machine/clock.h>
6821769Sjkh
6952286Smdodd#include <isa/isavar.h>
7052286Smdodd#include <isa/pnpvar.h>
7152286Smdodd
7221769Sjkh#include <i386/isa/if_exreg.h>
7321769Sjkh
7421769Sjkh#ifdef EXDEBUG
7552286Smdodd# define Start_End 1
7652286Smdodd# define Rcvd_Pkts 2
7752286Smdodd# define Sent_Pkts 4
7852286Smdodd# define Status    8
7921769Sjkhstatic int debug_mask = 0;
8021769Sjkhstatic int exintr_count = 0;
8152286Smdodd# define DODEBUG(level, action) if (level & debug_mask) action
8221769Sjkh#else
8352286Smdodd# define DODEBUG(level, action)
8421769Sjkh#endif
8521769Sjkh
8621769Sjkh#define Conn_BNC 1
8721769Sjkh#define Conn_TPE 2
8821769Sjkh#define Conn_AUI 3
8921769Sjkh
9052286Smdodd#define CARD_TYPE_EX_10		1
9152286Smdodd#define CARD_TYPE_EX_10_PLUS	2
9252286Smdodd
9321769Sjkhstruct ex_softc {
9452286Smdodd  	struct arpcom	arpcom;		/* Ethernet common data */
9552286Smdodd
9652286Smdodd	device_t	dev;
9752286Smdodd	struct resource *ioport;
9852286Smdodd	struct resource *irq;
9952286Smdodd
10052286Smdodd	u_int		iobase;		/* I/O base address. */
10152286Smdodd	u_short		irq_no;		/* IRQ number. */
10252286Smdodd
10352286Smdodd	char *		irq2ee;		/* irq <-> internal		*/
10452286Smdodd	u_char *	ee2irq;		/* representation conversion	*/
10552286Smdodd
10652286Smdodd	u_short		connector;	/* Connector type. */
10752286Smdodd
10852286Smdodd	u_int		mem_size;	/* Total memory size, in bytes. */
10952286Smdodd	u_int		rx_mem_size;	/* Rx memory size (by default,	*/
11052286Smdodd					/* first 3/4 of total memory).	*/
11152286Smdodd
11252286Smdodd	u_int		rx_lower_limit;	/* Lower and upper limits of	*/
11352286Smdodd	u_int		rx_upper_limit;	/* receive buffer.		*/
11452286Smdodd
11552286Smdodd	u_int		rx_head;	/* Head of receive ring buffer. */
11652286Smdodd	u_int		tx_mem_size;	/* Tx memory size (by default,	*/
11752286Smdodd					/* last quarter of total memory).*/
11852286Smdodd
11952286Smdodd	u_int		tx_lower_limit;	/* Lower and upper limits of	*/
12052286Smdodd	u_int		tx_upper_limit;	/* transmit buffer.		*/
12152286Smdodd
12252286Smdodd	u_int		tx_head;	/* Head and tail of 		*/
12352286Smdodd	u_int		tx_tail;	/* transmit ring buffer.	*/
12452286Smdodd
12552286Smdodd	u_int		tx_last;	/* Pointer to beginning of last	*/
12652286Smdodd					/* frame in the chain.		*/
12721769Sjkh};
12821769Sjkh
12952286Smdoddstatic char irq2eemap[] =
13052286Smdodd	{ -1, -1, 0, 1, -1, 2, -1, -1, -1, 0, 3, 4, -1, -1, -1, -1 };
13152286Smdoddstatic u_char ee2irqmap[] =
13252286Smdodd	{ 9, 3, 5, 10, 11, 0, 0, 0 };
13321769Sjkh
13452286Smdoddstatic char plus_irq2eemap[] =
13552286Smdodd	{ -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, -1, -1, -1 };
13652286Smdoddstatic u_char plus_ee2irqmap[] =
13752286Smdodd	{ 3, 4, 5, 7, 9, 10, 11, 12 };
13821769Sjkh
13952286Smdodd/* Bus Front End Functions */
14055882Smdoddstatic void	ex_isa_identify	__P((driver_t *, device_t));
14152286Smdoddstatic int	ex_isa_probe	__P((device_t));
14252286Smdoddstatic int	ex_attach	__P((device_t));
14321769Sjkh
14452286Smdodd/* Network Interface Functions */
14552286Smdoddstatic void	ex_init		__P((void *));
14652286Smdoddstatic void	ex_start	__P((struct ifnet *));
14752286Smdoddstatic int	ex_ioctl	__P((struct ifnet *, u_long, caddr_t));
14852286Smdoddstatic void	ex_watchdog	__P((struct ifnet *));
14921769Sjkh
15052286Smdoddstatic void	ex_stop		__P((struct ex_softc *));
15152286Smdoddstatic void	ex_reset	__P((struct ex_softc *));
15252286Smdodd
15352286Smdoddstatic driver_intr_t	exintr;
15452286Smdoddstatic void	ex_tx_intr	__P((struct ex_softc *));
15552286Smdoddstatic void	ex_rx_intr	__P((struct ex_softc *));
15652286Smdodd
15752286Smdoddstatic u_short	eeprom_read	__P((int, int));
15852286Smdodd
15952286Smdoddstatic device_method_t ex_methods[] = {
16052286Smdodd	/* Device interface */
16155882Smdodd	DEVMETHOD(device_identify,	ex_isa_identify),
16252286Smdodd	DEVMETHOD(device_probe,		ex_isa_probe),
16352286Smdodd	DEVMETHOD(device_attach,	ex_attach),
16452286Smdodd
16552286Smdodd	{ 0, 0 }
16652286Smdodd};
16752286Smdodd
16852286Smdoddstatic driver_t ex_driver = {
16921769Sjkh	"ex",
17052286Smdodd	ex_methods,
17152286Smdodd	sizeof(struct ex_softc),
17221769Sjkh};
17321769Sjkh
17452286Smdoddstatic devclass_t ex_devclass;
17552286Smdodd
17652286SmdoddDRIVER_MODULE(ex, isa, ex_driver, ex_devclass, 0, 0);
17752286Smdodd
17852286Smdoddstatic struct isa_pnp_id ex_ids[] = {
17952286Smdodd	{ 0x3110d425,	NULL },	/* INT1031 */
18052286Smdodd	{ 0x3010d425,	NULL },	/* INT1030 */
18152286Smdodd	{ 0,		NULL },
18252286Smdodd};
18352286Smdodd
18421769Sjkhstatic int look_for_card(u_int iobase)
18521769Sjkh{
18621769Sjkh	int count1, count2;
18721769Sjkh
18821769Sjkh	/*
18921769Sjkh	 * Check for the i82595 signature, and check that the round robin
19021769Sjkh	 * counter actually advances.
19121769Sjkh	 */
19221769Sjkh	if (((count1 = inb(iobase + ID_REG)) & Id_Mask) != Id_Sig)
19321769Sjkh		return(0);
19421769Sjkh	count2 = inb(iobase + ID_REG);
19521769Sjkh	count2 = inb(iobase + ID_REG);
19621769Sjkh	count2 = inb(iobase + ID_REG);
19752286Smdodd
19821769Sjkh	return((count2 & Counter_bits) == ((count1 + 0xc0) & Counter_bits));
19921769Sjkh}
20021769Sjkh
20152286Smdoddstatic int
20252286Smdoddex_get_media (u_int32_t iobase)
20352286Smdodd{
20452286Smdodd	int	tmp;
20521769Sjkh
20652286Smdodd	outb(iobase + CMD_REG, Bank2_Sel);
20752286Smdodd	tmp = inb(iobase + REG3);
20852286Smdodd	outb(iobase + CMD_REG, Bank0_Sel);
20952286Smdodd
21052286Smdodd	if (tmp & TPE_bit)
21152286Smdodd		return(Conn_TPE);
21252286Smdodd	if (tmp & BNC_bit)
21352286Smdodd		return(Conn_BNC);
21452286Smdodd
21552286Smdodd	return (Conn_AUI);
21652286Smdodd}
21752286Smdodd
21852286Smdoddstatic void
21952286Smdoddex_get_address (u_int32_t iobase, u_char *enaddr)
22021769Sjkh{
22152286Smdodd	u_int16_t	eaddr_tmp;
22221769Sjkh
22352286Smdodd	eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Lo);
22452286Smdodd	enaddr[5] = eaddr_tmp & 0xff;
22552286Smdodd	enaddr[4] = eaddr_tmp >> 8;
22652286Smdodd	eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Mid);
22752286Smdodd	enaddr[3] = eaddr_tmp & 0xff;
22852286Smdodd	enaddr[2] = eaddr_tmp >> 8;
22952286Smdodd	eaddr_tmp = eeprom_read(iobase, EE_Eth_Addr_Hi);
23052286Smdodd	enaddr[1] = eaddr_tmp & 0xff;
23152286Smdodd	enaddr[0] = eaddr_tmp >> 8;
23252286Smdodd
23352286Smdodd	return;
23452286Smdodd}
23521769Sjkh
23652286Smdoddstatic int
23752286Smdoddex_card_type (u_char *enaddr)
23852286Smdodd{
23952286Smdodd	if ((enaddr[0] == 0x00) && (enaddr[1] == 0xA0) && (enaddr[2] == 0xC9))
24052286Smdodd		return (CARD_TYPE_EX_10_PLUS);
24152286Smdodd
24252286Smdodd	return (CARD_TYPE_EX_10);
24352286Smdodd}
24452286Smdodd
24555882Smdodd/*
24655882Smdodd * Non-destructive identify.
24755882Smdodd */
24855882Smdoddstatic void
24955882Smdoddex_isa_identify (driver_t *driver, device_t parent)
25055882Smdodd{
25155882Smdodd	device_t	child;
25255882Smdodd	u_int32_t	ioport;
25355882Smdodd	u_char 		enaddr[6];
25455882Smdodd	u_int		irq;
25555882Smdodd	int		tmp;
25655882Smdodd	const char *	desc;
25755882Smdodd
25855882Smdodd	for (ioport = 0x200; ioport < 0x3a0; ioport += 0x10) {
25955882Smdodd
26055882Smdodd		/* No board found at address */
26155882Smdodd		if (!look_for_card(ioport)) {
26255882Smdodd			continue;
26355882Smdodd		}
26455882Smdodd
26555882Smdodd		/* Board in PnP mode */
26655882Smdodd		if (eeprom_read(ioport, 0) & 0x01) {
26755882Smdodd			continue;
26855882Smdodd		}
26955882Smdodd
27055882Smdodd		bzero(enaddr, sizeof(enaddr));
27155882Smdodd
27255882Smdodd		/* Reset the card. */
27355882Smdodd		outb(ioport + CMD_REG, Reset_CMD);
27455882Smdodd		DELAY(400);
27555882Smdodd
27655882Smdodd		ex_get_address(ioport, enaddr);
27755882Smdodd		tmp = eeprom_read(ioport, EE_IRQ_No) & IRQ_No_Mask;
27855882Smdodd
27955882Smdodd		/* work out which set of irq <-> internal tables to use */
28055882Smdodd		if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
28155882Smdodd			irq  = plus_ee2irqmap[tmp];
28255882Smdodd			desc = "Intel Pro/10+";
28355882Smdodd		} else {
28455882Smdodd			irq = ee2irqmap[tmp];
28555882Smdodd			desc = "Intel Pro/10";
28655882Smdodd		}
28755882Smdodd
28855882Smdodd		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ex", -1);
28955882Smdodd		device_set_desc_copy(child, desc);
29055882Smdodd		device_set_driver(child, driver);
29155882Smdodd		bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
29255882Smdodd		bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EX_IOSIZE);
29355882Smdodd	}
29455882Smdodd
29555882Smdodd	return;
29655882Smdodd}
29755882Smdodd
29852286Smdoddstatic int
29952286Smdoddex_isa_probe(device_t dev)
30052286Smdodd{
30152286Smdodd	u_int		iobase;
30252286Smdodd	u_int		irq;
30352286Smdodd	char *		irq2ee;
30452286Smdodd	u_char *	ee2irq;
30552286Smdodd	u_char 		enaddr[6];
30652286Smdodd	int		tmp;
30752286Smdodd	int		error;
30852286Smdodd
30952286Smdodd	DODEBUG(Start_End, printf("ex_probe: start\n"););
31052286Smdodd
31152286Smdodd	/* Check isapnp ids */
31252286Smdodd	error = ISA_PNP_PROBE(device_get_parent(dev), dev, ex_ids);
31352286Smdodd
31452286Smdodd	/* If the card had a PnP ID that didn't match any we know about */
31552286Smdodd	if (error == ENXIO) {
31652286Smdodd		return(error);
31752286Smdodd	}
31852286Smdodd
31952286Smdodd	/* If we had some other problem. */
32052286Smdodd	if (!(error == 0 || error == ENOENT)) {
32152286Smdodd		return(error);
32252286Smdodd	}
32352286Smdodd
32452286Smdodd	iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0);
32555882Smdodd	if (iobase && !look_for_card(iobase)) {
32655882Smdodd		printf("ex: no card found at 0x%03x\n", iobase);
32755882Smdodd		return(ENXIO);
32821769Sjkh	}
32921769Sjkh
33021769Sjkh	/*
33121769Sjkh	 * Reset the card.
33221769Sjkh	 */
33321769Sjkh	outb(iobase + CMD_REG, Reset_CMD);
33429313Smsmith	DELAY(400);
33521769Sjkh
33652286Smdodd	ex_get_address(iobase, enaddr);
33752286Smdodd
33852286Smdodd	/* work out which set of irq <-> internal tables to use */
33952286Smdodd	if (ex_card_type(enaddr) == CARD_TYPE_EX_10_PLUS) {
34052286Smdodd		irq2ee = plus_irq2eemap;
34152286Smdodd		ee2irq = plus_ee2irqmap;
34252286Smdodd	} else {
34352286Smdodd		irq2ee = irq2eemap;
34452286Smdodd		ee2irq = ee2irqmap;
34552286Smdodd	}
34652286Smdodd
34752286Smdodd	tmp = eeprom_read(iobase, EE_IRQ_No) & IRQ_No_Mask;
34852286Smdodd	irq = bus_get_resource_start(dev, SYS_RES_IRQ, 0);
34952286Smdodd
35052286Smdodd	if (irq > 0) {
35155882Smdodd		/* This will happen if board is in PnP mode. */
35252286Smdodd		if (ee2irq[tmp] != irq) {
35352286Smdodd			printf("ex: WARNING: board's EEPROM is configured"
35452286Smdodd				" for IRQ %d, using %d\n",
35552286Smdodd				ee2irq[tmp], irq);
35652286Smdodd		}
35752286Smdodd	} else {
35852286Smdodd		irq = ee2irq[tmp];
35952286Smdodd		bus_set_resource(dev, SYS_RES_IRQ, 0, irq, 1);
36052286Smdodd	}
36152286Smdodd
36252286Smdodd	if (irq == 0) {
36352286Smdodd		printf("ex: invalid IRQ.\n");
36452286Smdodd		return(ENXIO);
36552286Smdodd	}
36652286Smdodd
36752286Smdodd	DODEBUG(Start_End, printf("ex_probe: finish\n"););
36852286Smdodd
36952286Smdodd	return(0);
37052286Smdodd}
37152286Smdodd
37252286Smdodd
37352286Smdoddint ex_attach(device_t dev)
37452286Smdodd{
37552286Smdodd	struct ex_softc *	sc = device_get_softc(dev);
37652286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
37752286Smdodd	int			unit = device_get_unit(dev);
37852286Smdodd	int			error;
37952286Smdodd	int			rid;
38052286Smdodd	void *			ih;
38152286Smdodd
38252286Smdodd	DODEBUG(Start_End, device_printf(dev, "start\n"););
38352286Smdodd
38452286Smdodd	rid = 0;
38552286Smdodd	sc->ioport  = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
38652286Smdodd					 0, ~0, 1, RF_ACTIVE);
38752286Smdodd
38852286Smdodd	if (!sc->ioport) {
38952286Smdodd		device_printf(dev, "No I/O space?!\n");
39052286Smdodd		goto bad;
39152286Smdodd	}
39252286Smdodd
39352286Smdodd	rid = 0;
39452286Smdodd	sc->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
39552286Smdodd				     0, ~0, 1, RF_ACTIVE);
39652286Smdodd
39752286Smdodd	if (!sc->irq) {
39852286Smdodd		device_printf(dev, "No IRQ?!\n");
39952286Smdodd		goto bad;
40052286Smdodd	}
40152286Smdodd
40252286Smdodd	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET,
40352842Smdodd			       exintr, (void *)sc, &ih);
40452286Smdodd
40552286Smdodd	if (error) {
40652286Smdodd		device_printf(dev, "bus_setup_intr() failed!\n");
40752286Smdodd		goto bad;
40852286Smdodd	}
40952286Smdodd
41021769Sjkh	/*
41121769Sjkh	 * Fill in several fields of the softc structure:
41221769Sjkh	 *	- I/O base address.
41321769Sjkh	 *	- Hardware Ethernet address.
41421769Sjkh	 *	- IRQ number (if not supplied in config file, read it from EEPROM).
41521769Sjkh	 *	- Connector type.
41621769Sjkh	 */
41752286Smdodd	sc->dev = dev;
41852286Smdodd	sc->iobase = rman_get_start(sc->ioport);
41952286Smdodd	sc->irq_no = rman_get_start(sc->irq);
42029877Smsmith
42152286Smdodd	ex_get_address(sc->iobase, sc->arpcom.ac_enaddr);
42252286Smdodd
42329877Smsmith	/* work out which set of irq <-> internal tables to use */
42452286Smdodd	if (ex_card_type(sc->arpcom.ac_enaddr) == CARD_TYPE_EX_10_PLUS) {
42529877Smsmith		sc->irq2ee = plus_irq2eemap;
42629877Smsmith		sc->ee2irq = plus_ee2irqmap;
42752286Smdodd	} else {
42829877Smsmith		sc->irq2ee = irq2eemap;
42929877Smsmith		sc->ee2irq = ee2irqmap;
43029877Smsmith	}
43129877Smsmith
43252286Smdodd	sc->connector = ex_get_media(sc->iobase);
43321769Sjkh	sc->mem_size = CARD_RAM_SIZE;	/* XXX This should be read from the card itself. */
43421769Sjkh
43521769Sjkh	/*
43621769Sjkh	 * Initialize the ifnet structure.
43721769Sjkh	 */
43821769Sjkh	ifp->if_softc = sc;
43921769Sjkh	ifp->if_unit = unit;
44021769Sjkh	ifp->if_name = "ex";
44121769Sjkh	ifp->if_init = ex_init;
44221769Sjkh	ifp->if_output = ether_output;
44321769Sjkh	ifp->if_start = ex_start;
44421769Sjkh	ifp->if_ioctl = ex_ioctl;
44521769Sjkh	ifp->if_watchdog = ex_watchdog;
44621769Sjkh	ifp->if_mtu = ETHERMTU;
44752286Smdodd	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST
44852286Smdodd					/* XXX not done yet. | IFF_MULTICAST */;
44921769Sjkh
45021769Sjkh	/*
45121769Sjkh	 * Attach the interface.
45221769Sjkh	 */
45321769Sjkh	if_attach(ifp);
45421769Sjkh	ether_ifattach(ifp);
45521769Sjkh
45652286Smdodd	if (ex_card_type(sc->arpcom.ac_enaddr) == CARD_TYPE_EX_10_PLUS) {
45752286Smdodd		printf("ex%d: Intel EtherExpress Pro/10+, address %6D, connector ", unit, sc->arpcom.ac_enaddr, ":");
45829313Smsmith	} else {
45952286Smdodd		printf("ex%d: Intel EtherExpress Pro/10, address %6D, connector ", unit, sc->arpcom.ac_enaddr, ":");
46029313Smsmith	}
46121769Sjkh	switch(sc->connector) {
46221769Sjkh		case Conn_TPE: printf("TPE\n"); break;
46321769Sjkh		case Conn_BNC: printf("BNC\n"); break;
46421769Sjkh		case Conn_AUI: printf("AUI\n"); break;
46521769Sjkh		default: printf("???\n");
46621769Sjkh	}
46721769Sjkh
46821769Sjkh	/*
46921769Sjkh	 * If BPF is in the kernel, call the attach for it
47021769Sjkh	 */
47121769Sjkh	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
47221769Sjkh	DODEBUG(Start_End, printf("ex_attach%d: finish\n", unit););
47346347Speter	sc->arpcom.ac_if.if_snd.ifq_maxlen = ifqmaxlen;
47452286Smdodd
47552286Smdodd	return(0);
47652286Smdodd
47752286Smdoddbad:
47852286Smdodd
47952286Smdodd	if (sc->ioport)
48052286Smdodd		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->ioport);
48152286Smdodd	if (sc->irq)
48252286Smdodd		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
48352286Smdodd
48452286Smdodd	return (-1);
48521769Sjkh}
48621769Sjkh
48721769Sjkh
48821769Sjkhvoid ex_init(void *xsc)
48921769Sjkh{
49052286Smdodd	struct ex_softc *	sc = (struct ex_softc *) xsc;
49152286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
49252286Smdodd	int			s;
49352286Smdodd	int			i;
49452286Smdodd	register int		iobase = sc->iobase;
49552286Smdodd	unsigned short		temp_reg;
49621769Sjkh
49721769Sjkh	DODEBUG(Start_End, printf("ex_init%d: start\n", ifp->if_unit););
49821769Sjkh
49952286Smdodd	if (ifp->if_addrhead.tqh_first == NULL) {
50052286Smdodd		return;
50152286Smdodd	}
50221769Sjkh	s = splimp();
50321769Sjkh	sc->arpcom.ac_if.if_timer = 0;
50421769Sjkh
50521769Sjkh	/*
50621769Sjkh	 * Load the ethernet address into the card.
50721769Sjkh	 */
50821769Sjkh	outb(iobase + CMD_REG, Bank2_Sel);
50921769Sjkh	temp_reg = inb(iobase + EEPROM_REG);
51052286Smdodd	if (temp_reg & Trnoff_Enable) {
51152286Smdodd		outb(iobase + EEPROM_REG, temp_reg & ~Trnoff_Enable);
51252286Smdodd	}
51352286Smdodd	for (i = 0; i < ETHER_ADDR_LEN; i++) {
51452286Smdodd		outb(iobase + I_ADDR_REG0 + i, sc->arpcom.ac_enaddr[i]);
51552286Smdodd	}
51621769Sjkh	/*
51721769Sjkh	 * - Setup transmit chaining and discard bad received frames.
51821769Sjkh	 * - Match broadcast.
51921769Sjkh	 * - Clear test mode.
52021769Sjkh	 * - Set receiving mode.
52121769Sjkh	 * - Set IRQ number.
52221769Sjkh	 */
52321769Sjkh	outb(iobase + REG1, inb(iobase + REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
52421769Sjkh	outb(iobase + REG2, inb(iobase + REG2) | No_SA_Ins | RX_CRC_InMem);
52526545Sgibbs	outb(iobase + REG3, inb(iobase + REG3) & 0x3f /* XXX constants. */ );
52621769Sjkh	outb(iobase + CMD_REG, Bank1_Sel);
52729877Smsmith	outb(iobase + INT_NO_REG, (inb(iobase + INT_NO_REG) & 0xf8) | sc->irq2ee[sc->irq_no]);
52821769Sjkh
52921769Sjkh	/*
53021769Sjkh	 * Divide the available memory in the card into rcv and xmt buffers.
53121769Sjkh	 * By default, I use the first 3/4 of the memory for the rcv buffer,
53221769Sjkh	 * and the remaining 1/4 of the memory for the xmt buffer.
53321769Sjkh	 */
53421769Sjkh	sc->rx_mem_size = sc->mem_size * 3 / 4;
53521769Sjkh	sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
53621769Sjkh	sc->rx_lower_limit = 0x0000;
53721769Sjkh	sc->rx_upper_limit = sc->rx_mem_size - 2;
53821769Sjkh	sc->tx_lower_limit = sc->rx_mem_size;
53921769Sjkh	sc->tx_upper_limit = sc->mem_size - 2;
54021769Sjkh	outb(iobase + RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
54121769Sjkh	outb(iobase + RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
54221769Sjkh	outb(iobase + XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
54321769Sjkh	outb(iobase + XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
54421769Sjkh
54521769Sjkh	/*
54621769Sjkh	 * Enable receive and transmit interrupts, and clear any pending int.
54721769Sjkh	 */
54821769Sjkh	outb(iobase + REG1, inb(iobase + REG1) | TriST_INT);
54921769Sjkh	outb(iobase + CMD_REG, Bank0_Sel);
55021769Sjkh	outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
55121769Sjkh	outb(iobase + STATUS_REG, All_Int);
55221769Sjkh
55321769Sjkh	/*
55421769Sjkh	 * Initialize receive and transmit ring buffers.
55521769Sjkh	 */
55621769Sjkh	outw(iobase + RCV_BAR, sc->rx_lower_limit);
55721769Sjkh	sc->rx_head = sc->rx_lower_limit;
55826545Sgibbs	outw(iobase + RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
55921769Sjkh	outw(iobase + XMT_BAR, sc->tx_lower_limit);
56021769Sjkh	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
56121769Sjkh
56221769Sjkh	ifp->if_flags |= IFF_RUNNING;
56321769Sjkh	ifp->if_flags &= ~IFF_OACTIVE;
56421769Sjkh	DODEBUG(Status, printf("OIDLE init\n"););
56521769Sjkh
56621769Sjkh	/*
56721769Sjkh	 * Final reset of the board, and enable operation.
56821769Sjkh	 */
56921769Sjkh	outb(iobase + CMD_REG, Sel_Reset_CMD);
57021769Sjkh	DELAY(2);
57121769Sjkh	outb(iobase + CMD_REG, Rcv_Enable_CMD);
57221769Sjkh
57321769Sjkh	ex_start(ifp);
57421769Sjkh	splx(s);
57521769Sjkh
57621769Sjkh	DODEBUG(Start_End, printf("ex_init%d: finish\n", ifp->if_unit););
57721769Sjkh}
57821769Sjkh
57921769Sjkh
58052286Smdoddvoid
58152286Smdoddex_start(struct ifnet *ifp)
58221769Sjkh{
58352286Smdodd	struct ex_softc *	sc = ifp->if_softc;
58452286Smdodd	int			iobase = sc->iobase;
58552286Smdodd	int			i, s, len, data_len, avail, dest, next;
58652286Smdodd	unsigned char		tmp16[2];
58752286Smdodd	struct mbuf *		opkt;
58852286Smdodd	struct mbuf *		m;
58921769Sjkh
59052286Smdodd	DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
59121769Sjkh
59252286Smdodd	s = splimp();
59321769Sjkh
59452286Smdodd	/*
59552286Smdodd	 * Main loop: send outgoing packets to network card until there are no
59652286Smdodd	 * more packets left, or the card cannot accept any more yet.
59752286Smdodd	 */
59852286Smdodd	while (((opkt = ifp->if_snd.ifq_head) != NULL) &&
59952286Smdodd	       !(ifp->if_flags & IFF_OACTIVE)) {
60021769Sjkh
60152286Smdodd		/*
60252286Smdodd		 * Ensure there is enough free transmit buffer space for
60352286Smdodd		 * this packet, including its header. Note: the header
60452286Smdodd		 * cannot wrap around the end of the transmit buffer and
60552286Smdodd		 * must be kept together, so we allow space for twice the
60652286Smdodd		 * length of the header, just in case.
60752286Smdodd		 */
60821769Sjkh
60952286Smdodd		for (len = 0, m = opkt; m != NULL; m = m->m_next) {
61052286Smdodd			len += m->m_len;
61152286Smdodd		}
61252286Smdodd
61352286Smdodd		data_len = len;
61452286Smdodd
61552286Smdodd		DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
61652286Smdodd
61752286Smdodd		if (len & 1) {
61852286Smdodd			len += XMT_HEADER_LEN + 1;
61952286Smdodd		} else {
62052286Smdodd			len += XMT_HEADER_LEN;
62152286Smdodd		}
62252286Smdodd
62352286Smdodd		if ((i = sc->tx_tail - sc->tx_head) >= 0) {
62452286Smdodd			avail = sc->tx_mem_size - i;
62552286Smdodd		} else {
62652286Smdodd			avail = -i;
62752286Smdodd		}
62852286Smdodd
62952286Smdodd		DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
63052286Smdodd
63152286Smdodd		if (avail >= len + XMT_HEADER_LEN) {
63252286Smdodd			IF_DEQUEUE(&ifp->if_snd, opkt);
63352286Smdodd
63421769Sjkh#ifdef EX_PSA_INTR
63552286Smdodd			/*
63652286Smdodd			 * Disable rx and tx interrupts, to avoid corruption
63752286Smdodd			 * of the host address register by interrupt service
63852286Smdodd			 * routines.
63952286Smdodd			 * XXX Is this necessary with splimp() enabled?
64052286Smdodd			 */
64152286Smdodd			outb(iobase + MASK_REG, All_Int);
64221769Sjkh#endif
64321769Sjkh
64452286Smdodd			/*
64552286Smdodd			 * Compute the start and end addresses of this
64652286Smdodd			 * frame in the tx buffer.
64752286Smdodd			 */
64852286Smdodd			dest = sc->tx_tail;
64952286Smdodd			next = dest + len;
65021769Sjkh
65152286Smdodd			if (next > sc->tx_upper_limit) {
65252286Smdodd				if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
65352286Smdodd				    XMT_HEADER_LEN) {
65452286Smdodd					dest = sc->tx_lower_limit;
65552286Smdodd					next = dest + len;
65655881Smdodd				} else {
65755881Smdodd					next = sc->tx_lower_limit +
65855881Smdodd						next - sc->tx_upper_limit - 2;
65952286Smdodd				}
66052286Smdodd			}
66121769Sjkh
66252286Smdodd			/*
66352286Smdodd			 * Build the packet frame in the card's ring buffer.
66452286Smdodd			 */
66552286Smdodd			DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
66621769Sjkh
66752286Smdodd			outw(iobase + HOST_ADDR_REG, dest);
66852286Smdodd			outw(iobase + IO_PORT_REG, Transmit_CMD);
66952286Smdodd			outw(iobase + IO_PORT_REG, 0);
67052286Smdodd			outw(iobase + IO_PORT_REG, next);
67152286Smdodd			outw(iobase + IO_PORT_REG, data_len);
67221769Sjkh
67352286Smdodd			/*
67452286Smdodd			 * Output the packet data to the card. Ensure all
67552286Smdodd			 * transfers are 16-bit wide, even if individual
67652286Smdodd			 * mbufs have odd length.
67752286Smdodd			 */
67821769Sjkh
67952286Smdodd			for (m = opkt, i = 0; m != NULL; m = m->m_next) {
68052286Smdodd				DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
68152286Smdodd				if (i) {
68252286Smdodd					tmp16[1] = *(mtod(m, caddr_t));
68352286Smdodd					outsw(iobase + IO_PORT_REG, tmp16, 1);
68452286Smdodd				}
68552286Smdodd				outsw(iobase + IO_PORT_REG,
68652286Smdodd				      mtod(m, caddr_t) + i, (m->m_len - i) / 2);
68752286Smdodd
68852286Smdodd				if ((i = (m->m_len - i) & 1) != 0) {
68952286Smdodd					tmp16[0] = *(mtod(m, caddr_t) +
69052286Smdodd						   m->m_len - 1);
69152286Smdodd				}
69252286Smdodd			}
69352286Smdodd			if (i) {
69452286Smdodd				outsw(iobase + IO_PORT_REG, tmp16, 1);
69555881Smdodd			}
69652286Smdodd
69755881Smdodd			/*
69855881Smdodd			 * If there were other frames chained, update the
69955881Smdodd			 * chain in the last one.
70055881Smdodd			 */
70155881Smdodd			if (sc->tx_head != sc->tx_tail) {
70255881Smdodd				if (sc->tx_tail != dest) {
70352286Smdodd					outw(iobase + HOST_ADDR_REG,
70455881Smdodd					     sc->tx_last + XMT_Chain_Point);
70555881Smdodd					outw(iobase + IO_PORT_REG, dest);
70652286Smdodd				}
70755881Smdodd				outw(iobase + HOST_ADDR_REG,
70855881Smdodd				     sc->tx_last + XMT_Byte_Count);
70955881Smdodd				i = inw(iobase + IO_PORT_REG);
71055881Smdodd				outw(iobase + HOST_ADDR_REG,
71155881Smdodd				     sc->tx_last + XMT_Byte_Count);
71255881Smdodd				outw(iobase + IO_PORT_REG, i | Ch_bit);
71355881Smdodd			}
71455881Smdodd
71555881Smdodd			/*
71655881Smdodd			 * Resume normal operation of the card:
71755881Smdodd			 * - Make a dummy read to flush the DRAM write
71855881Smdodd			 *   pipeline.
71955881Smdodd			 * - Enable receive and transmit interrupts.
72055881Smdodd			 * - Send Transmit or Resume_XMT command, as
72155881Smdodd			 *   appropriate.
72255881Smdodd			 */
72355881Smdodd			inw(iobase + IO_PORT_REG);
72421769Sjkh#ifdef EX_PSA_INTR
72555881Smdodd			outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
72621769Sjkh#endif
72755881Smdodd			if (sc->tx_head == sc->tx_tail) {
72855881Smdodd				outw(iobase + XMT_BAR, dest);
72955881Smdodd				outb(iobase + CMD_REG, Transmit_CMD);
73055881Smdodd				sc->tx_head = dest;
73155881Smdodd				DODEBUG(Sent_Pkts, printf("Transmit\n"););
73252286Smdodd			} else {
73355881Smdodd				outb(iobase + CMD_REG, Resume_XMT_List_CMD);
73455881Smdodd				DODEBUG(Sent_Pkts, printf("Resume\n"););
73552286Smdodd			}
73655881Smdodd
73755881Smdodd			sc->tx_last = dest;
73855881Smdodd			sc->tx_tail = next;
73955881Smdodd
74055881Smdodd			if (ifp->if_bpf != NULL) {
74155881Smdodd				bpf_mtap(ifp, opkt);
74255881Smdodd			}
74355881Smdodd
74455881Smdodd			ifp->if_timer = 2;
74555881Smdodd			ifp->if_opackets++;
74655881Smdodd			m_freem(opkt);
74755881Smdodd		} else {
74855881Smdodd			ifp->if_flags |= IFF_OACTIVE;
74955881Smdodd			DODEBUG(Status, printf("OACTIVE start\n"););
75052286Smdodd		}
75121769Sjkh	}
75221769Sjkh
75352286Smdodd	splx(s);
75421769Sjkh
75552286Smdodd	DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
75621769Sjkh}
75721769Sjkh
75852286Smdoddstatic void
75952286Smdoddex_stop(struct ex_softc *sc)
76021769Sjkh{
76152286Smdodd	int iobase = sc->iobase;
76221769Sjkh
76352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
76421769Sjkh
76552286Smdodd	/*
76652286Smdodd	 * Disable card operation:
76752286Smdodd	 * - Disable the interrupt line.
76852286Smdodd	 * - Flush transmission and disable reception.
76952286Smdodd	 * - Mask and clear all interrupts.
77052286Smdodd	 * - Reset the 82595.
77152286Smdodd	 */
77252286Smdodd	outb(iobase + CMD_REG, Bank1_Sel);
77352286Smdodd	outb(iobase + REG1, inb(iobase + REG1) & ~TriST_INT);
77452286Smdodd	outb(iobase + CMD_REG, Bank0_Sel);
77552286Smdodd	outb(iobase + CMD_REG, Rcv_Stop);
77652286Smdodd	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
77752286Smdodd	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. */
77852286Smdodd	outb(iobase + MASK_REG, All_Int);
77952286Smdodd	outb(iobase + STATUS_REG, All_Int);
78052286Smdodd	outb(iobase + CMD_REG, Reset_CMD);
78152286Smdodd	DELAY(200);
78221769Sjkh
78352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
78452286Smdodd
78552286Smdodd	return;
78621769Sjkh}
78721769Sjkh
78821769Sjkh
78952286Smdoddstatic void
79052286Smdoddexintr(void *arg)
79121769Sjkh{
79252286Smdodd	struct ex_softc *	sc = (struct ex_softc *)arg;
79352286Smdodd	struct ifnet *	ifp = &sc->arpcom.ac_if;
79452286Smdodd	int			iobase = sc->iobase;
79552286Smdodd	int			int_status, send_pkts;
79621769Sjkh
79752286Smdodd	DODEBUG(Start_End, printf("exintr%d: start\n", unit););
79821769Sjkh
79921769Sjkh#ifdef EXDEBUG
80021769Sjkh	if (++exintr_count != 1)
80121769Sjkh		printf("WARNING: nested interrupt (%d). Mail the author.\n", exintr_count);
80221769Sjkh#endif
80321769Sjkh
80452286Smdodd	send_pkts = 0;
80552286Smdodd	while ((int_status = inb(iobase + STATUS_REG)) & (Tx_Int | Rx_Int)) {
80652286Smdodd		if (int_status & Rx_Int) {
80752286Smdodd			outb(iobase + STATUS_REG, Rx_Int);
80821769Sjkh
80952286Smdodd			ex_rx_intr(sc);
81052286Smdodd		} else if (int_status & Tx_Int) {
81152286Smdodd			outb(iobase + STATUS_REG, Tx_Int);
81221769Sjkh
81352286Smdodd			ex_tx_intr(sc);
81452286Smdodd			send_pkts = 1;
81552286Smdodd		}
81652286Smdodd	}
81721769Sjkh
81852286Smdodd	/*
81952286Smdodd	 * If any packet has been transmitted, and there are queued packets to
82052286Smdodd	 * be sent, attempt to send more packets to the network card.
82152286Smdodd	 */
82252286Smdodd
82352286Smdodd	if (send_pkts && (ifp->if_snd.ifq_head != NULL)) {
82452286Smdodd		ex_start(ifp);
82552286Smdodd	}
82652286Smdodd
82721769Sjkh#ifdef EXDEBUG
82821769Sjkh	exintr_count--;
82921769Sjkh#endif
83021769Sjkh
83152286Smdodd	DODEBUG(Start_End, printf("exintr%d: finish\n", unit););
83252286Smdodd
83352286Smdodd	return;
83421769Sjkh}
83521769Sjkh
83652286Smdoddstatic void
83752286Smdoddex_tx_intr(struct ex_softc *sc)
83821769Sjkh{
83952286Smdodd	struct ifnet *	ifp = &sc->arpcom.ac_if;
84052286Smdodd	int		iobase = sc->iobase;
84152286Smdodd	int		tx_status;
84221769Sjkh
84352286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
84421769Sjkh
84552286Smdodd	/*
84652286Smdodd	 * - Cancel the watchdog.
84752286Smdodd	 * For all packets transmitted since last transmit interrupt:
84852286Smdodd	 * - Advance chain pointer to next queued packet.
84952286Smdodd	 * - Update statistics.
85052286Smdodd	 */
85121769Sjkh
85252286Smdodd	ifp->if_timer = 0;
85321769Sjkh
85452286Smdodd	while (sc->tx_head != sc->tx_tail) {
85552286Smdodd		outw(iobase + HOST_ADDR_REG, sc->tx_head);
85621769Sjkh
85752286Smdodd		if (! inw(iobase + IO_PORT_REG) & Done_bit)
85852286Smdodd			break;
85921769Sjkh
86052286Smdodd		tx_status = inw(iobase + IO_PORT_REG);
86152286Smdodd		sc->tx_head = inw(iobase + IO_PORT_REG);
86252286Smdodd
86352286Smdodd		if (tx_status & TX_OK_bit) {
86452286Smdodd			ifp->if_opackets++;
86552286Smdodd		} else {
86652286Smdodd			ifp->if_oerrors++;
86752286Smdodd		}
86852286Smdodd
86952286Smdodd		ifp->if_collisions += tx_status & No_Collisions_bits;
87052286Smdodd	}
87152286Smdodd
87252286Smdodd	/*
87352286Smdodd	 * The card should be ready to accept more packets now.
87452286Smdodd	 */
87552286Smdodd
87652286Smdodd	ifp->if_flags &= ~IFF_OACTIVE;
87752286Smdodd
87852286Smdodd	DODEBUG(Status, printf("OIDLE tx_intr\n"););
87952286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
88052286Smdodd
88152286Smdodd	return;
88221769Sjkh}
88321769Sjkh
88452286Smdoddstatic void
88552286Smdoddex_rx_intr(struct ex_softc *sc)
88621769Sjkh{
88752286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
88852286Smdodd	int			iobase = sc->iobase;
88952286Smdodd	int			rx_status;
89052286Smdodd	int			pkt_len;
89152286Smdodd	int			QQQ;
89252286Smdodd	struct mbuf *		m;
89352286Smdodd	struct mbuf *		ipkt;
89452286Smdodd	struct ether_header *	eh;
89521769Sjkh
89652286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
89721769Sjkh
89852286Smdodd	/*
89952286Smdodd	 * For all packets received since last receive interrupt:
90052286Smdodd	 * - If packet ok, read it into a new mbuf and queue it to interface,
90152286Smdodd	 *   updating statistics.
90252286Smdodd	 * - If packet bad, just discard it, and update statistics.
90352286Smdodd	 * Finally, advance receive stop limit in card's memory to new location.
90452286Smdodd	 */
90521769Sjkh
90652286Smdodd	outw(iobase + HOST_ADDR_REG, sc->rx_head);
90721769Sjkh
90852286Smdodd	while (inw(iobase + IO_PORT_REG) == RCV_Done) {
90952286Smdodd
91052286Smdodd		rx_status = inw(iobase + IO_PORT_REG);
91152286Smdodd		sc->rx_head = inw(iobase + IO_PORT_REG);
91252286Smdodd		QQQ = pkt_len = inw(iobase + IO_PORT_REG);
91352286Smdodd
91452286Smdodd		if (rx_status & RCV_OK_bit) {
91552286Smdodd			MGETHDR(m, M_DONTWAIT, MT_DATA);
91652286Smdodd			ipkt = m;
91752286Smdodd			if (ipkt == NULL) {
91852286Smdodd				ifp->if_iqdrops++;
91952286Smdodd			} else {
92052286Smdodd				ipkt->m_pkthdr.rcvif = ifp;
92152286Smdodd				ipkt->m_pkthdr.len = pkt_len;
92252286Smdodd				ipkt->m_len = MHLEN;
92352286Smdodd
92452286Smdodd				while (pkt_len > 0) {
92552286Smdodd					if (pkt_len > MINCLSIZE) {
92652286Smdodd						MCLGET(m, M_DONTWAIT);
92752286Smdodd						if (m->m_flags & M_EXT) {
92852286Smdodd							m->m_len = MCLBYTES;
92952286Smdodd						} else {
93052286Smdodd							m_freem(ipkt);
93152286Smdodd							ifp->if_iqdrops++;
93252286Smdodd							goto rx_another;
93352286Smdodd						}
93452286Smdodd					}
93552286Smdodd					m->m_len = min(m->m_len, pkt_len);
93652286Smdodd
93721769Sjkh	  /*
93821769Sjkh	   * NOTE: I'm assuming that all mbufs allocated are of even length,
93921769Sjkh	   * except for the last one in an odd-length packet.
94021769Sjkh	   */
94152286Smdodd
94252286Smdodd					insw(iobase + IO_PORT_REG,
94352286Smdodd					     mtod(m, caddr_t), m->m_len / 2);
94452286Smdodd
94552286Smdodd					if (m->m_len & 1) {
94652286Smdodd						*(mtod(m, caddr_t) + m->m_len - 1) = inb(iobase + IO_PORT_REG);
94752286Smdodd					}
94852286Smdodd					pkt_len -= m->m_len;
94952286Smdodd
95052286Smdodd					if (pkt_len > 0) {
95152286Smdodd						MGET(m->m_next, M_DONTWAIT, MT_DATA);
95252286Smdodd						if (m->m_next == NULL) {
95352286Smdodd							m_freem(ipkt);
95452286Smdodd							ifp->if_iqdrops++;
95552286Smdodd							goto rx_another;
95652286Smdodd						}
95752286Smdodd						m = m->m_next;
95852286Smdodd						m->m_len = MLEN;
95952286Smdodd					}
96052286Smdodd				}
96152286Smdodd				eh = mtod(ipkt, struct ether_header *);
96252286Smdodd#ifdef EXDEBUG
96352286Smdodd	if (debug_mask & Rcvd_Pkts) {
96452286Smdodd		if ((eh->ether_dhost[5] != 0xff) || (eh->ether_dhost[0] != 0xff)) {
96552286Smdodd			printf("Receive packet with %d data bytes: %6D -> ", QQQ, eh->ether_shost, ":");
96652286Smdodd			printf("%6D\n", eh->ether_dhost, ":");
96752286Smdodd		} /* QQQ */
96821769Sjkh	}
96921769Sjkh#endif
97052286Smdodd				if (ifp->if_bpf != NULL) {
97152286Smdodd					bpf_mtap(ifp, ipkt);
97221769Sjkh
97321769Sjkh		/*
97452286Smdodd		 * Note that the interface cannot be in promiscuous mode
97552286Smdodd		 * if there are no BPF listeners. And if we are in
97652286Smdodd		 * promiscuous mode, we have to check if this packet is
97752286Smdodd		 * really ours.
97821769Sjkh		 */
97952286Smdodd					if ((ifp->if_flags & IFF_PROMISC) &&
98052286Smdodd					    (eh->ether_dhost[0] & 1) == 0 &&
98152286Smdodd					    bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, sizeof(eh->ether_dhost)) != 0 &&
98252286Smdodd					    bcmp(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost)) != 0) {
98352286Smdodd						m_freem(ipkt);
98452286Smdodd						goto rx_another;
98552286Smdodd					}
98652286Smdodd				}
98752286Smdodd				m_adj(ipkt, sizeof(struct ether_header));
98852286Smdodd				ether_input(ifp, eh, ipkt);
98952286Smdodd				ifp->if_ipackets++;
99052286Smdodd			}
99152286Smdodd		} else {
99252286Smdodd			ifp->if_ierrors++;
99321769Sjkh		}
99452286Smdodd		outw(iobase + HOST_ADDR_REG, sc->rx_head);
99552286Smdoddrx_another: ;
99621769Sjkh	}
99721769Sjkh
99852286Smdodd	if (sc->rx_head < sc->rx_lower_limit + 2)
99952286Smdodd		outw(iobase + RCV_STOP_REG, sc->rx_upper_limit);
100052286Smdodd	else
100152286Smdodd		outw(iobase + RCV_STOP_REG, sc->rx_head - 2);
100252286Smdodd
100352286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
100452286Smdodd
100552286Smdodd	return;
100621769Sjkh}
100721769Sjkh
100821769Sjkh
100936735Sdfrint ex_ioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
101021769Sjkh{
101152286Smdodd	struct ex_softc *	sc = ifp->if_softc;
101252286Smdodd	int			s;
101352286Smdodd	int			error = 0;
101421769Sjkh
101552286Smdodd	DODEBUG(Start_End, printf("ex_ioctl%d: start ", ifp->if_unit););
101621769Sjkh
101752286Smdodd	s = splimp();
101821769Sjkh
101952286Smdodd	switch(cmd) {
102052286Smdodd		case SIOCSIFADDR:
102152286Smdodd		case SIOCGIFADDR:
102252286Smdodd		case SIOCSIFMTU:
102352286Smdodd			error = ether_ioctl(ifp, cmd, data);
102452286Smdodd			break;
102521769Sjkh
102652286Smdodd		case SIOCSIFFLAGS:
102752286Smdodd			DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
102852286Smdodd			if ((ifp->if_flags & IFF_UP) == 0 &&
102952286Smdodd			    (ifp->if_flags & IFF_RUNNING)) {
103052286Smdodd
103152286Smdodd				ifp->if_flags &= ~IFF_RUNNING;
103252286Smdodd				ex_stop(sc);
103352286Smdodd			} else {
103452286Smdodd      				ex_init(sc);
103552286Smdodd			}
103652286Smdodd			break;
103721769Sjkh#ifdef NODEF
103852286Smdodd		case SIOCGHWADDR:
103952286Smdodd			DODEBUG(Start_End, printf("SIOCGHWADDR"););
104052286Smdodd			bcopy((caddr_t)sc->sc_addr, (caddr_t)&ifr->ifr_data,
104152286Smdodd			      sizeof(sc->sc_addr));
104252286Smdodd			break;
104321769Sjkh#endif
104452286Smdodd		case SIOCADDMULTI:
104552286Smdodd			DODEBUG(Start_End, printf("SIOCADDMULTI"););
104652286Smdodd		case SIOCDELMULTI:
104752286Smdodd			DODEBUG(Start_End, printf("SIOCDELMULTI"););
104852286Smdodd			/* XXX Support not done yet. */
104952286Smdodd			error = EINVAL;
105052286Smdodd			break;
105152286Smdodd		default:
105252286Smdodd			DODEBUG(Start_End, printf("unknown"););
105352286Smdodd			error = EINVAL;
105452286Smdodd	}
105521769Sjkh
105652286Smdodd	splx(s);
105721769Sjkh
105852286Smdodd	DODEBUG(Start_End, printf("\nex_ioctl%d: finish\n", ifp->if_unit););
105952286Smdodd
106052286Smdodd	return(error);
106121769Sjkh}
106221769Sjkh
106321769Sjkh
106452286Smdoddstatic void
106552286Smdoddex_reset(struct ex_softc *sc)
106621769Sjkh{
106752286Smdodd	int s;
106821769Sjkh
106952286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
107021769Sjkh
107152286Smdodd	s = splimp();
107221769Sjkh
107352286Smdodd	ex_stop(sc);
107452286Smdodd	ex_init(sc);
107521769Sjkh
107652286Smdodd	splx(s);
107721769Sjkh
107852286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
107952286Smdodd
108052286Smdodd	return;
108121769Sjkh}
108221769Sjkh
108321769Sjkh
108452286Smdoddstatic void
108552286Smdoddex_watchdog(struct ifnet *ifp)
108621769Sjkh{
108752286Smdodd	struct ex_softc *	sc = ifp->if_softc;
108821769Sjkh
108952286Smdodd	DODEBUG(Start_End, printf("ex_watchdog%d: start\n", ifp->if_unit););
109021769Sjkh
109152286Smdodd	ifp->if_flags &= ~IFF_OACTIVE;
109221769Sjkh
109352286Smdodd	DODEBUG(Status, printf("OIDLE watchdog\n"););
109452286Smdodd
109552286Smdodd	ifp->if_oerrors++;
109652286Smdodd	ex_reset(sc);
109752286Smdodd	ex_start(ifp);
109852286Smdodd
109952286Smdodd	DODEBUG(Start_End, printf("ex_watchdog%d: finish\n", ifp->if_unit););
110052286Smdodd
110152286Smdodd	return;
110221769Sjkh}
110321769Sjkh
110421769Sjkh
110552286Smdoddstatic u_short
110652286Smdoddeeprom_read(int iobase, int location)
110721769Sjkh{
110821769Sjkh	int i;
110921769Sjkh	u_short data = 0;
111021769Sjkh	int ee_addr;
111121769Sjkh	int read_cmd = location | EE_READ_CMD;
111221769Sjkh	short ctrl_val = EECS;
111321769Sjkh
111421769Sjkh	ee_addr = iobase + EEPROM_REG;
111521769Sjkh	outb(iobase + CMD_REG, Bank2_Sel);
111621769Sjkh	outb(ee_addr, EECS);
111721769Sjkh	for (i = 8; i >= 0; i--) {
111821769Sjkh		short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
111921769Sjkh		outb(ee_addr, outval);
112021769Sjkh		outb(ee_addr, outval | EESK);
112121769Sjkh		DELAY(3);
112221769Sjkh		outb(ee_addr, outval);
112321769Sjkh		DELAY(2);
112421769Sjkh	}
112521769Sjkh	outb(ee_addr, ctrl_val);
112621769Sjkh
112721769Sjkh	for (i = 16; i > 0; i--) {
112821769Sjkh		outb(ee_addr, ctrl_val | EESK);
112921769Sjkh		DELAY(3);
113021769Sjkh		data = (data << 1) | ((inb(ee_addr) & EEDO) ? 1 : 0);
113121769Sjkh		outb(ee_addr, ctrl_val);
113221769Sjkh		DELAY(2);
113321769Sjkh	}
113421769Sjkh
113521769Sjkh	ctrl_val &= ~EECS;
113621769Sjkh	outb(ee_addr, ctrl_val | EESK);
113721769Sjkh	DELAY(3);
113821769Sjkh	outb(ee_addr, ctrl_val);
113921769Sjkh	DELAY(2);
114021769Sjkh	outb(iobase + CMD_REG, Bank0_Sel);
114121769Sjkh	return(data);
114221769Sjkh}
114321769Sjkh
114421769Sjkh#endif /* NEX > 0 */
1145