if_ex.c revision 55953
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 55953 2000-01-14 07:14:17Z peter $
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
4321769Sjkh#include <sys/param.h>
4421769Sjkh#include <sys/systm.h>
4552286Smdodd#include <sys/kernel.h>
4621769Sjkh#include <sys/conf.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
5850026Smdodd#include <net/ethernet.h>
5921769Sjkh#include <net/if.h>
6021769Sjkh
6150026Smdodd#include <netinet/in.h>
6250026Smdodd#include <netinet/if_ether.h>
6350026Smdodd
6421769Sjkh#include <net/bpf.h>
6521769Sjkh
6621769Sjkh#include <machine/clock.h>
6721769Sjkh
6852286Smdodd#include <isa/isavar.h>
6952286Smdodd#include <isa/pnpvar.h>
7052286Smdodd
7155953Speter#include <dev/ex/if_exreg.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
8521769Sjkh#define Conn_BNC 1
8621769Sjkh#define Conn_TPE 2
8721769Sjkh#define Conn_AUI 3
8821769Sjkh
8952286Smdodd#define CARD_TYPE_EX_10		1
9052286Smdodd#define CARD_TYPE_EX_10_PLUS	2
9152286Smdodd
9221769Sjkhstruct ex_softc {
9352286Smdodd  	struct arpcom	arpcom;		/* Ethernet common data */
9452286Smdodd
9552286Smdodd	device_t	dev;
9652286Smdodd	struct resource *ioport;
9752286Smdodd	struct resource *irq;
9852286Smdodd
9952286Smdodd	u_int		iobase;		/* I/O base address. */
10052286Smdodd	u_short		irq_no;		/* IRQ number. */
10152286Smdodd
10252286Smdodd	char *		irq2ee;		/* irq <-> internal		*/
10352286Smdodd	u_char *	ee2irq;		/* representation conversion	*/
10452286Smdodd
10552286Smdodd	u_short		connector;	/* Connector type. */
10652286Smdodd
10752286Smdodd	u_int		mem_size;	/* Total memory size, in bytes. */
10852286Smdodd	u_int		rx_mem_size;	/* Rx memory size (by default,	*/
10952286Smdodd					/* first 3/4 of total memory).	*/
11052286Smdodd
11152286Smdodd	u_int		rx_lower_limit;	/* Lower and upper limits of	*/
11252286Smdodd	u_int		rx_upper_limit;	/* receive buffer.		*/
11352286Smdodd
11452286Smdodd	u_int		rx_head;	/* Head of receive ring buffer. */
11552286Smdodd	u_int		tx_mem_size;	/* Tx memory size (by default,	*/
11652286Smdodd					/* last quarter of total memory).*/
11752286Smdodd
11852286Smdodd	u_int		tx_lower_limit;	/* Lower and upper limits of	*/
11952286Smdodd	u_int		tx_upper_limit;	/* transmit buffer.		*/
12052286Smdodd
12152286Smdodd	u_int		tx_head;	/* Head and tail of 		*/
12252286Smdodd	u_int		tx_tail;	/* transmit ring buffer.	*/
12352286Smdodd
12452286Smdodd	u_int		tx_last;	/* Pointer to beginning of last	*/
12552286Smdodd					/* frame in the chain.		*/
12621769Sjkh};
12721769Sjkh
12852286Smdoddstatic char irq2eemap[] =
12952286Smdodd	{ -1, -1, 0, 1, -1, 2, -1, -1, -1, 0, 3, 4, -1, -1, -1, -1 };
13052286Smdoddstatic u_char ee2irqmap[] =
13152286Smdodd	{ 9, 3, 5, 10, 11, 0, 0, 0 };
13221769Sjkh
13352286Smdoddstatic char plus_irq2eemap[] =
13452286Smdodd	{ -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, -1, -1, -1 };
13552286Smdoddstatic u_char plus_ee2irqmap[] =
13652286Smdodd	{ 3, 4, 5, 7, 9, 10, 11, 12 };
13721769Sjkh
13852286Smdodd/* Bus Front End Functions */
13955882Smdoddstatic void	ex_isa_identify	__P((driver_t *, device_t));
14052286Smdoddstatic int	ex_isa_probe	__P((device_t));
14155883Smdoddstatic int	ex_isa_attach	__P((device_t));
14221769Sjkh
14352286Smdodd/* Network Interface Functions */
14452286Smdoddstatic void	ex_init		__P((void *));
14552286Smdoddstatic void	ex_start	__P((struct ifnet *));
14652286Smdoddstatic int	ex_ioctl	__P((struct ifnet *, u_long, caddr_t));
14752286Smdoddstatic void	ex_watchdog	__P((struct ifnet *));
14821769Sjkh
14952286Smdoddstatic void	ex_stop		__P((struct ex_softc *));
15052286Smdoddstatic void	ex_reset	__P((struct ex_softc *));
15152286Smdodd
15255883Smdoddstatic driver_intr_t	ex_intr;
15352286Smdoddstatic void	ex_tx_intr	__P((struct ex_softc *));
15452286Smdoddstatic void	ex_rx_intr	__P((struct ex_softc *));
15552286Smdodd
15652286Smdoddstatic u_short	eeprom_read	__P((int, int));
15752286Smdodd
15852286Smdoddstatic device_method_t ex_methods[] = {
15952286Smdodd	/* Device interface */
16055882Smdodd	DEVMETHOD(device_identify,	ex_isa_identify),
16152286Smdodd	DEVMETHOD(device_probe,		ex_isa_probe),
16255883Smdodd	DEVMETHOD(device_attach,	ex_isa_attach),
16352286Smdodd
16452286Smdodd	{ 0, 0 }
16552286Smdodd};
16652286Smdodd
16752286Smdoddstatic driver_t ex_driver = {
16821769Sjkh	"ex",
16952286Smdodd	ex_methods,
17052286Smdodd	sizeof(struct ex_softc),
17121769Sjkh};
17221769Sjkh
17352286Smdoddstatic devclass_t ex_devclass;
17452286Smdodd
17552286SmdoddDRIVER_MODULE(ex, isa, ex_driver, ex_devclass, 0, 0);
17652286Smdodd
17752286Smdoddstatic struct isa_pnp_id ex_ids[] = {
17852286Smdodd	{ 0x3110d425,	NULL },	/* INT1031 */
17952286Smdodd	{ 0x3010d425,	NULL },	/* INT1030 */
18052286Smdodd	{ 0,		NULL },
18152286Smdodd};
18252286Smdodd
18355883Smdoddstatic int
18455883Smdoddlook_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
37255883Smdoddstatic int
37355883Smdoddex_isa_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,
40355883Smdodd			       ex_intr, (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";
44155883Smdodd	ifp->if_mtu = ETHERMTU;
44255883Smdodd	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST /* XXX not done yet. | IFF_MULTICAST */;
44321769Sjkh	ifp->if_output = ether_output;
44421769Sjkh	ifp->if_start = ex_start;
44521769Sjkh	ifp->if_ioctl = ex_ioctl;
44621769Sjkh	ifp->if_watchdog = ex_watchdog;
44755883Smdodd	ifp->if_init = ex_init;
44855883Smdodd	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
44921769Sjkh
45021769Sjkh	/*
45121769Sjkh	 * Attach the interface.
45221769Sjkh	 */
45321769Sjkh	if_attach(ifp);
45421769Sjkh	ether_ifattach(ifp);
45521769Sjkh
45655883Smdodd	device_printf(sc->dev, "Ethernet address %6D\n",
45755883Smdodd			sc->arpcom.ac_enaddr, ":");
45821769Sjkh
45921769Sjkh	/*
46021769Sjkh	 * If BPF is in the kernel, call the attach for it
46121769Sjkh	 */
46221769Sjkh	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
46355883Smdodd	DODEBUG(Start_End, printf("ex_isa_attach%d: finish\n", unit););
46452286Smdodd
46552286Smdodd	return(0);
46652286Smdoddbad:
46752286Smdodd
46852286Smdodd	if (sc->ioport)
46952286Smdodd		bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->ioport);
47052286Smdodd	if (sc->irq)
47152286Smdodd		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
47252286Smdodd
47352286Smdodd	return (-1);
47421769Sjkh}
47521769Sjkh
47621769Sjkh
47755883Smdoddstatic void
47855883Smdoddex_init(void *xsc)
47921769Sjkh{
48052286Smdodd	struct ex_softc *	sc = (struct ex_softc *) xsc;
48152286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
48252286Smdodd	int			s;
48352286Smdodd	int			i;
48452286Smdodd	register int		iobase = sc->iobase;
48552286Smdodd	unsigned short		temp_reg;
48621769Sjkh
48721769Sjkh	DODEBUG(Start_End, printf("ex_init%d: start\n", ifp->if_unit););
48821769Sjkh
48952286Smdodd	if (ifp->if_addrhead.tqh_first == NULL) {
49052286Smdodd		return;
49152286Smdodd	}
49221769Sjkh	s = splimp();
49321769Sjkh	sc->arpcom.ac_if.if_timer = 0;
49421769Sjkh
49521769Sjkh	/*
49621769Sjkh	 * Load the ethernet address into the card.
49721769Sjkh	 */
49821769Sjkh	outb(iobase + CMD_REG, Bank2_Sel);
49921769Sjkh	temp_reg = inb(iobase + EEPROM_REG);
50052286Smdodd	if (temp_reg & Trnoff_Enable) {
50152286Smdodd		outb(iobase + EEPROM_REG, temp_reg & ~Trnoff_Enable);
50252286Smdodd	}
50352286Smdodd	for (i = 0; i < ETHER_ADDR_LEN; i++) {
50452286Smdodd		outb(iobase + I_ADDR_REG0 + i, sc->arpcom.ac_enaddr[i]);
50552286Smdodd	}
50621769Sjkh	/*
50721769Sjkh	 * - Setup transmit chaining and discard bad received frames.
50821769Sjkh	 * - Match broadcast.
50921769Sjkh	 * - Clear test mode.
51021769Sjkh	 * - Set receiving mode.
51121769Sjkh	 * - Set IRQ number.
51221769Sjkh	 */
51321769Sjkh	outb(iobase + REG1, inb(iobase + REG1) | Tx_Chn_Int_Md | Tx_Chn_ErStp | Disc_Bad_Fr);
51421769Sjkh	outb(iobase + REG2, inb(iobase + REG2) | No_SA_Ins | RX_CRC_InMem);
51526545Sgibbs	outb(iobase + REG3, inb(iobase + REG3) & 0x3f /* XXX constants. */ );
51621769Sjkh	outb(iobase + CMD_REG, Bank1_Sel);
51729877Smsmith	outb(iobase + INT_NO_REG, (inb(iobase + INT_NO_REG) & 0xf8) | sc->irq2ee[sc->irq_no]);
51821769Sjkh
51921769Sjkh	/*
52021769Sjkh	 * Divide the available memory in the card into rcv and xmt buffers.
52121769Sjkh	 * By default, I use the first 3/4 of the memory for the rcv buffer,
52221769Sjkh	 * and the remaining 1/4 of the memory for the xmt buffer.
52321769Sjkh	 */
52421769Sjkh	sc->rx_mem_size = sc->mem_size * 3 / 4;
52521769Sjkh	sc->tx_mem_size = sc->mem_size - sc->rx_mem_size;
52621769Sjkh	sc->rx_lower_limit = 0x0000;
52721769Sjkh	sc->rx_upper_limit = sc->rx_mem_size - 2;
52821769Sjkh	sc->tx_lower_limit = sc->rx_mem_size;
52921769Sjkh	sc->tx_upper_limit = sc->mem_size - 2;
53021769Sjkh	outb(iobase + RCV_LOWER_LIMIT_REG, sc->rx_lower_limit >> 8);
53121769Sjkh	outb(iobase + RCV_UPPER_LIMIT_REG, sc->rx_upper_limit >> 8);
53221769Sjkh	outb(iobase + XMT_LOWER_LIMIT_REG, sc->tx_lower_limit >> 8);
53321769Sjkh	outb(iobase + XMT_UPPER_LIMIT_REG, sc->tx_upper_limit >> 8);
53421769Sjkh
53521769Sjkh	/*
53621769Sjkh	 * Enable receive and transmit interrupts, and clear any pending int.
53721769Sjkh	 */
53821769Sjkh	outb(iobase + REG1, inb(iobase + REG1) | TriST_INT);
53921769Sjkh	outb(iobase + CMD_REG, Bank0_Sel);
54021769Sjkh	outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
54121769Sjkh	outb(iobase + STATUS_REG, All_Int);
54221769Sjkh
54321769Sjkh	/*
54421769Sjkh	 * Initialize receive and transmit ring buffers.
54521769Sjkh	 */
54621769Sjkh	outw(iobase + RCV_BAR, sc->rx_lower_limit);
54721769Sjkh	sc->rx_head = sc->rx_lower_limit;
54826545Sgibbs	outw(iobase + RCV_STOP_REG, sc->rx_upper_limit | 0xfe);
54921769Sjkh	outw(iobase + XMT_BAR, sc->tx_lower_limit);
55021769Sjkh	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
55121769Sjkh
55221769Sjkh	ifp->if_flags |= IFF_RUNNING;
55321769Sjkh	ifp->if_flags &= ~IFF_OACTIVE;
55421769Sjkh	DODEBUG(Status, printf("OIDLE init\n"););
55521769Sjkh
55621769Sjkh	/*
55721769Sjkh	 * Final reset of the board, and enable operation.
55821769Sjkh	 */
55921769Sjkh	outb(iobase + CMD_REG, Sel_Reset_CMD);
56021769Sjkh	DELAY(2);
56121769Sjkh	outb(iobase + CMD_REG, Rcv_Enable_CMD);
56221769Sjkh
56321769Sjkh	ex_start(ifp);
56421769Sjkh	splx(s);
56521769Sjkh
56621769Sjkh	DODEBUG(Start_End, printf("ex_init%d: finish\n", ifp->if_unit););
56721769Sjkh}
56821769Sjkh
56921769Sjkh
57055883Smdoddstatic void
57152286Smdoddex_start(struct ifnet *ifp)
57221769Sjkh{
57352286Smdodd	struct ex_softc *	sc = ifp->if_softc;
57452286Smdodd	int			iobase = sc->iobase;
57552286Smdodd	int			i, s, len, data_len, avail, dest, next;
57652286Smdodd	unsigned char		tmp16[2];
57752286Smdodd	struct mbuf *		opkt;
57852286Smdodd	struct mbuf *		m;
57921769Sjkh
58052286Smdodd	DODEBUG(Start_End, printf("ex_start%d: start\n", unit););
58121769Sjkh
58252286Smdodd	s = splimp();
58321769Sjkh
58452286Smdodd	/*
58552286Smdodd	 * Main loop: send outgoing packets to network card until there are no
58652286Smdodd	 * more packets left, or the card cannot accept any more yet.
58752286Smdodd	 */
58852286Smdodd	while (((opkt = ifp->if_snd.ifq_head) != NULL) &&
58952286Smdodd	       !(ifp->if_flags & IFF_OACTIVE)) {
59021769Sjkh
59152286Smdodd		/*
59252286Smdodd		 * Ensure there is enough free transmit buffer space for
59352286Smdodd		 * this packet, including its header. Note: the header
59452286Smdodd		 * cannot wrap around the end of the transmit buffer and
59552286Smdodd		 * must be kept together, so we allow space for twice the
59652286Smdodd		 * length of the header, just in case.
59752286Smdodd		 */
59821769Sjkh
59952286Smdodd		for (len = 0, m = opkt; m != NULL; m = m->m_next) {
60052286Smdodd			len += m->m_len;
60152286Smdodd		}
60252286Smdodd
60352286Smdodd		data_len = len;
60452286Smdodd
60552286Smdodd		DODEBUG(Sent_Pkts, printf("1. Sending packet with %d data bytes. ", data_len););
60652286Smdodd
60752286Smdodd		if (len & 1) {
60852286Smdodd			len += XMT_HEADER_LEN + 1;
60952286Smdodd		} else {
61052286Smdodd			len += XMT_HEADER_LEN;
61152286Smdodd		}
61252286Smdodd
61352286Smdodd		if ((i = sc->tx_tail - sc->tx_head) >= 0) {
61452286Smdodd			avail = sc->tx_mem_size - i;
61552286Smdodd		} else {
61652286Smdodd			avail = -i;
61752286Smdodd		}
61852286Smdodd
61952286Smdodd		DODEBUG(Sent_Pkts, printf("i=%d, avail=%d\n", i, avail););
62052286Smdodd
62152286Smdodd		if (avail >= len + XMT_HEADER_LEN) {
62252286Smdodd			IF_DEQUEUE(&ifp->if_snd, opkt);
62352286Smdodd
62421769Sjkh#ifdef EX_PSA_INTR
62552286Smdodd			/*
62652286Smdodd			 * Disable rx and tx interrupts, to avoid corruption
62752286Smdodd			 * of the host address register by interrupt service
62852286Smdodd			 * routines.
62952286Smdodd			 * XXX Is this necessary with splimp() enabled?
63052286Smdodd			 */
63152286Smdodd			outb(iobase + MASK_REG, All_Int);
63221769Sjkh#endif
63321769Sjkh
63452286Smdodd			/*
63552286Smdodd			 * Compute the start and end addresses of this
63652286Smdodd			 * frame in the tx buffer.
63752286Smdodd			 */
63852286Smdodd			dest = sc->tx_tail;
63952286Smdodd			next = dest + len;
64021769Sjkh
64152286Smdodd			if (next > sc->tx_upper_limit) {
64252286Smdodd				if ((sc->tx_upper_limit + 2 - sc->tx_tail) <=
64352286Smdodd				    XMT_HEADER_LEN) {
64452286Smdodd					dest = sc->tx_lower_limit;
64552286Smdodd					next = dest + len;
64655881Smdodd				} else {
64755881Smdodd					next = sc->tx_lower_limit +
64855881Smdodd						next - sc->tx_upper_limit - 2;
64952286Smdodd				}
65052286Smdodd			}
65121769Sjkh
65252286Smdodd			/*
65352286Smdodd			 * Build the packet frame in the card's ring buffer.
65452286Smdodd			 */
65552286Smdodd			DODEBUG(Sent_Pkts, printf("2. dest=%d, next=%d. ", dest, next););
65621769Sjkh
65752286Smdodd			outw(iobase + HOST_ADDR_REG, dest);
65852286Smdodd			outw(iobase + IO_PORT_REG, Transmit_CMD);
65952286Smdodd			outw(iobase + IO_PORT_REG, 0);
66052286Smdodd			outw(iobase + IO_PORT_REG, next);
66152286Smdodd			outw(iobase + IO_PORT_REG, data_len);
66221769Sjkh
66352286Smdodd			/*
66452286Smdodd			 * Output the packet data to the card. Ensure all
66552286Smdodd			 * transfers are 16-bit wide, even if individual
66652286Smdodd			 * mbufs have odd length.
66752286Smdodd			 */
66821769Sjkh
66952286Smdodd			for (m = opkt, i = 0; m != NULL; m = m->m_next) {
67052286Smdodd				DODEBUG(Sent_Pkts, printf("[%d]", m->m_len););
67152286Smdodd				if (i) {
67252286Smdodd					tmp16[1] = *(mtod(m, caddr_t));
67352286Smdodd					outsw(iobase + IO_PORT_REG, tmp16, 1);
67452286Smdodd				}
67552286Smdodd				outsw(iobase + IO_PORT_REG,
67652286Smdodd				      mtod(m, caddr_t) + i, (m->m_len - i) / 2);
67752286Smdodd
67852286Smdodd				if ((i = (m->m_len - i) & 1) != 0) {
67952286Smdodd					tmp16[0] = *(mtod(m, caddr_t) +
68052286Smdodd						   m->m_len - 1);
68152286Smdodd				}
68252286Smdodd			}
68352286Smdodd			if (i) {
68452286Smdodd				outsw(iobase + IO_PORT_REG, tmp16, 1);
68555881Smdodd			}
68652286Smdodd
68755881Smdodd			/*
68855881Smdodd			 * If there were other frames chained, update the
68955881Smdodd			 * chain in the last one.
69055881Smdodd			 */
69155881Smdodd			if (sc->tx_head != sc->tx_tail) {
69255881Smdodd				if (sc->tx_tail != dest) {
69352286Smdodd					outw(iobase + HOST_ADDR_REG,
69455881Smdodd					     sc->tx_last + XMT_Chain_Point);
69555881Smdodd					outw(iobase + IO_PORT_REG, dest);
69652286Smdodd				}
69755881Smdodd				outw(iobase + HOST_ADDR_REG,
69855881Smdodd				     sc->tx_last + XMT_Byte_Count);
69955881Smdodd				i = inw(iobase + IO_PORT_REG);
70055881Smdodd				outw(iobase + HOST_ADDR_REG,
70155881Smdodd				     sc->tx_last + XMT_Byte_Count);
70255881Smdodd				outw(iobase + IO_PORT_REG, i | Ch_bit);
70355881Smdodd			}
70455881Smdodd
70555881Smdodd			/*
70655881Smdodd			 * Resume normal operation of the card:
70755881Smdodd			 * - Make a dummy read to flush the DRAM write
70855881Smdodd			 *   pipeline.
70955881Smdodd			 * - Enable receive and transmit interrupts.
71055881Smdodd			 * - Send Transmit or Resume_XMT command, as
71155881Smdodd			 *   appropriate.
71255881Smdodd			 */
71355881Smdodd			inw(iobase + IO_PORT_REG);
71421769Sjkh#ifdef EX_PSA_INTR
71555881Smdodd			outb(iobase + MASK_REG, All_Int & ~(Rx_Int | Tx_Int));
71621769Sjkh#endif
71755881Smdodd			if (sc->tx_head == sc->tx_tail) {
71855881Smdodd				outw(iobase + XMT_BAR, dest);
71955881Smdodd				outb(iobase + CMD_REG, Transmit_CMD);
72055881Smdodd				sc->tx_head = dest;
72155881Smdodd				DODEBUG(Sent_Pkts, printf("Transmit\n"););
72252286Smdodd			} else {
72355881Smdodd				outb(iobase + CMD_REG, Resume_XMT_List_CMD);
72455881Smdodd				DODEBUG(Sent_Pkts, printf("Resume\n"););
72552286Smdodd			}
72655881Smdodd
72755881Smdodd			sc->tx_last = dest;
72855881Smdodd			sc->tx_tail = next;
72955881Smdodd
73055881Smdodd			if (ifp->if_bpf != NULL) {
73155881Smdodd				bpf_mtap(ifp, opkt);
73255881Smdodd			}
73355881Smdodd
73455881Smdodd			ifp->if_timer = 2;
73555881Smdodd			ifp->if_opackets++;
73655881Smdodd			m_freem(opkt);
73755881Smdodd		} else {
73855881Smdodd			ifp->if_flags |= IFF_OACTIVE;
73955881Smdodd			DODEBUG(Status, printf("OACTIVE start\n"););
74052286Smdodd		}
74121769Sjkh	}
74221769Sjkh
74352286Smdodd	splx(s);
74421769Sjkh
74552286Smdodd	DODEBUG(Start_End, printf("ex_start%d: finish\n", unit););
74621769Sjkh}
74721769Sjkh
74852286Smdoddstatic void
74952286Smdoddex_stop(struct ex_softc *sc)
75021769Sjkh{
75152286Smdodd	int iobase = sc->iobase;
75221769Sjkh
75352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: start\n", unit););
75421769Sjkh
75552286Smdodd	/*
75652286Smdodd	 * Disable card operation:
75752286Smdodd	 * - Disable the interrupt line.
75852286Smdodd	 * - Flush transmission and disable reception.
75952286Smdodd	 * - Mask and clear all interrupts.
76052286Smdodd	 * - Reset the 82595.
76152286Smdodd	 */
76252286Smdodd	outb(iobase + CMD_REG, Bank1_Sel);
76352286Smdodd	outb(iobase + REG1, inb(iobase + REG1) & ~TriST_INT);
76452286Smdodd	outb(iobase + CMD_REG, Bank0_Sel);
76552286Smdodd	outb(iobase + CMD_REG, Rcv_Stop);
76652286Smdodd	sc->tx_head = sc->tx_tail = sc->tx_lower_limit;
76752286Smdodd	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. */
76852286Smdodd	outb(iobase + MASK_REG, All_Int);
76952286Smdodd	outb(iobase + STATUS_REG, All_Int);
77052286Smdodd	outb(iobase + CMD_REG, Reset_CMD);
77152286Smdodd	DELAY(200);
77221769Sjkh
77352286Smdodd	DODEBUG(Start_End, printf("ex_stop%d: finish\n", unit););
77452286Smdodd
77552286Smdodd	return;
77621769Sjkh}
77721769Sjkh
77821769Sjkh
77952286Smdoddstatic void
78055883Smdoddex_intr(void *arg)
78121769Sjkh{
78252286Smdodd	struct ex_softc *	sc = (struct ex_softc *)arg;
78352286Smdodd	struct ifnet *	ifp = &sc->arpcom.ac_if;
78452286Smdodd	int			iobase = sc->iobase;
78552286Smdodd	int			int_status, send_pkts;
78621769Sjkh
78755883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: start\n", unit););
78821769Sjkh
78921769Sjkh#ifdef EXDEBUG
79021769Sjkh	if (++exintr_count != 1)
79121769Sjkh		printf("WARNING: nested interrupt (%d). Mail the author.\n", exintr_count);
79221769Sjkh#endif
79321769Sjkh
79452286Smdodd	send_pkts = 0;
79552286Smdodd	while ((int_status = inb(iobase + STATUS_REG)) & (Tx_Int | Rx_Int)) {
79652286Smdodd		if (int_status & Rx_Int) {
79752286Smdodd			outb(iobase + STATUS_REG, Rx_Int);
79821769Sjkh
79952286Smdodd			ex_rx_intr(sc);
80052286Smdodd		} else if (int_status & Tx_Int) {
80152286Smdodd			outb(iobase + STATUS_REG, Tx_Int);
80221769Sjkh
80352286Smdodd			ex_tx_intr(sc);
80452286Smdodd			send_pkts = 1;
80552286Smdodd		}
80652286Smdodd	}
80721769Sjkh
80852286Smdodd	/*
80952286Smdodd	 * If any packet has been transmitted, and there are queued packets to
81052286Smdodd	 * be sent, attempt to send more packets to the network card.
81152286Smdodd	 */
81252286Smdodd
81352286Smdodd	if (send_pkts && (ifp->if_snd.ifq_head != NULL)) {
81452286Smdodd		ex_start(ifp);
81552286Smdodd	}
81652286Smdodd
81721769Sjkh#ifdef EXDEBUG
81821769Sjkh	exintr_count--;
81921769Sjkh#endif
82021769Sjkh
82155883Smdodd	DODEBUG(Start_End, printf("ex_intr%d: finish\n", unit););
82252286Smdodd
82352286Smdodd	return;
82421769Sjkh}
82521769Sjkh
82652286Smdoddstatic void
82752286Smdoddex_tx_intr(struct ex_softc *sc)
82821769Sjkh{
82952286Smdodd	struct ifnet *	ifp = &sc->arpcom.ac_if;
83052286Smdodd	int		iobase = sc->iobase;
83152286Smdodd	int		tx_status;
83221769Sjkh
83352286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: start\n", unit););
83421769Sjkh
83552286Smdodd	/*
83652286Smdodd	 * - Cancel the watchdog.
83752286Smdodd	 * For all packets transmitted since last transmit interrupt:
83852286Smdodd	 * - Advance chain pointer to next queued packet.
83952286Smdodd	 * - Update statistics.
84052286Smdodd	 */
84121769Sjkh
84252286Smdodd	ifp->if_timer = 0;
84321769Sjkh
84452286Smdodd	while (sc->tx_head != sc->tx_tail) {
84552286Smdodd		outw(iobase + HOST_ADDR_REG, sc->tx_head);
84621769Sjkh
84752286Smdodd		if (! inw(iobase + IO_PORT_REG) & Done_bit)
84852286Smdodd			break;
84921769Sjkh
85052286Smdodd		tx_status = inw(iobase + IO_PORT_REG);
85152286Smdodd		sc->tx_head = inw(iobase + IO_PORT_REG);
85252286Smdodd
85352286Smdodd		if (tx_status & TX_OK_bit) {
85452286Smdodd			ifp->if_opackets++;
85552286Smdodd		} else {
85652286Smdodd			ifp->if_oerrors++;
85752286Smdodd		}
85852286Smdodd
85952286Smdodd		ifp->if_collisions += tx_status & No_Collisions_bits;
86052286Smdodd	}
86152286Smdodd
86252286Smdodd	/*
86352286Smdodd	 * The card should be ready to accept more packets now.
86452286Smdodd	 */
86552286Smdodd
86652286Smdodd	ifp->if_flags &= ~IFF_OACTIVE;
86752286Smdodd
86852286Smdodd	DODEBUG(Status, printf("OIDLE tx_intr\n"););
86952286Smdodd	DODEBUG(Start_End, printf("ex_tx_intr%d: finish\n", unit););
87052286Smdodd
87152286Smdodd	return;
87221769Sjkh}
87321769Sjkh
87452286Smdoddstatic void
87552286Smdoddex_rx_intr(struct ex_softc *sc)
87621769Sjkh{
87752286Smdodd	struct ifnet *		ifp = &sc->arpcom.ac_if;
87852286Smdodd	int			iobase = sc->iobase;
87952286Smdodd	int			rx_status;
88052286Smdodd	int			pkt_len;
88152286Smdodd	int			QQQ;
88252286Smdodd	struct mbuf *		m;
88352286Smdodd	struct mbuf *		ipkt;
88452286Smdodd	struct ether_header *	eh;
88521769Sjkh
88652286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: start\n", unit););
88721769Sjkh
88852286Smdodd	/*
88952286Smdodd	 * For all packets received since last receive interrupt:
89052286Smdodd	 * - If packet ok, read it into a new mbuf and queue it to interface,
89152286Smdodd	 *   updating statistics.
89252286Smdodd	 * - If packet bad, just discard it, and update statistics.
89352286Smdodd	 * Finally, advance receive stop limit in card's memory to new location.
89452286Smdodd	 */
89521769Sjkh
89652286Smdodd	outw(iobase + HOST_ADDR_REG, sc->rx_head);
89721769Sjkh
89852286Smdodd	while (inw(iobase + IO_PORT_REG) == RCV_Done) {
89952286Smdodd
90052286Smdodd		rx_status = inw(iobase + IO_PORT_REG);
90152286Smdodd		sc->rx_head = inw(iobase + IO_PORT_REG);
90252286Smdodd		QQQ = pkt_len = inw(iobase + IO_PORT_REG);
90352286Smdodd
90452286Smdodd		if (rx_status & RCV_OK_bit) {
90552286Smdodd			MGETHDR(m, M_DONTWAIT, MT_DATA);
90652286Smdodd			ipkt = m;
90752286Smdodd			if (ipkt == NULL) {
90852286Smdodd				ifp->if_iqdrops++;
90952286Smdodd			} else {
91052286Smdodd				ipkt->m_pkthdr.rcvif = ifp;
91152286Smdodd				ipkt->m_pkthdr.len = pkt_len;
91252286Smdodd				ipkt->m_len = MHLEN;
91352286Smdodd
91452286Smdodd				while (pkt_len > 0) {
91552286Smdodd					if (pkt_len > MINCLSIZE) {
91652286Smdodd						MCLGET(m, M_DONTWAIT);
91752286Smdodd						if (m->m_flags & M_EXT) {
91852286Smdodd							m->m_len = MCLBYTES;
91952286Smdodd						} else {
92052286Smdodd							m_freem(ipkt);
92152286Smdodd							ifp->if_iqdrops++;
92252286Smdodd							goto rx_another;
92352286Smdodd						}
92452286Smdodd					}
92552286Smdodd					m->m_len = min(m->m_len, pkt_len);
92652286Smdodd
92721769Sjkh	  /*
92821769Sjkh	   * NOTE: I'm assuming that all mbufs allocated are of even length,
92921769Sjkh	   * except for the last one in an odd-length packet.
93021769Sjkh	   */
93152286Smdodd
93252286Smdodd					insw(iobase + IO_PORT_REG,
93352286Smdodd					     mtod(m, caddr_t), m->m_len / 2);
93452286Smdodd
93552286Smdodd					if (m->m_len & 1) {
93652286Smdodd						*(mtod(m, caddr_t) + m->m_len - 1) = inb(iobase + IO_PORT_REG);
93752286Smdodd					}
93852286Smdodd					pkt_len -= m->m_len;
93952286Smdodd
94052286Smdodd					if (pkt_len > 0) {
94152286Smdodd						MGET(m->m_next, M_DONTWAIT, MT_DATA);
94252286Smdodd						if (m->m_next == NULL) {
94352286Smdodd							m_freem(ipkt);
94452286Smdodd							ifp->if_iqdrops++;
94552286Smdodd							goto rx_another;
94652286Smdodd						}
94752286Smdodd						m = m->m_next;
94852286Smdodd						m->m_len = MLEN;
94952286Smdodd					}
95052286Smdodd				}
95152286Smdodd				eh = mtod(ipkt, struct ether_header *);
95252286Smdodd#ifdef EXDEBUG
95352286Smdodd	if (debug_mask & Rcvd_Pkts) {
95452286Smdodd		if ((eh->ether_dhost[5] != 0xff) || (eh->ether_dhost[0] != 0xff)) {
95552286Smdodd			printf("Receive packet with %d data bytes: %6D -> ", QQQ, eh->ether_shost, ":");
95652286Smdodd			printf("%6D\n", eh->ether_dhost, ":");
95752286Smdodd		} /* QQQ */
95821769Sjkh	}
95921769Sjkh#endif
96052286Smdodd				if (ifp->if_bpf != NULL) {
96152286Smdodd					bpf_mtap(ifp, ipkt);
96221769Sjkh
96321769Sjkh		/*
96452286Smdodd		 * Note that the interface cannot be in promiscuous mode
96552286Smdodd		 * if there are no BPF listeners. And if we are in
96652286Smdodd		 * promiscuous mode, we have to check if this packet is
96752286Smdodd		 * really ours.
96821769Sjkh		 */
96952286Smdodd					if ((ifp->if_flags & IFF_PROMISC) &&
97052286Smdodd					    (eh->ether_dhost[0] & 1) == 0 &&
97152286Smdodd					    bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, sizeof(eh->ether_dhost)) != 0 &&
97252286Smdodd					    bcmp(eh->ether_dhost, etherbroadcastaddr, sizeof(eh->ether_dhost)) != 0) {
97352286Smdodd						m_freem(ipkt);
97452286Smdodd						goto rx_another;
97552286Smdodd					}
97652286Smdodd				}
97752286Smdodd				m_adj(ipkt, sizeof(struct ether_header));
97852286Smdodd				ether_input(ifp, eh, ipkt);
97952286Smdodd				ifp->if_ipackets++;
98052286Smdodd			}
98152286Smdodd		} else {
98252286Smdodd			ifp->if_ierrors++;
98321769Sjkh		}
98452286Smdodd		outw(iobase + HOST_ADDR_REG, sc->rx_head);
98552286Smdoddrx_another: ;
98621769Sjkh	}
98721769Sjkh
98852286Smdodd	if (sc->rx_head < sc->rx_lower_limit + 2)
98952286Smdodd		outw(iobase + RCV_STOP_REG, sc->rx_upper_limit);
99052286Smdodd	else
99152286Smdodd		outw(iobase + RCV_STOP_REG, sc->rx_head - 2);
99252286Smdodd
99352286Smdodd	DODEBUG(Start_End, printf("ex_rx_intr%d: finish\n", unit););
99452286Smdodd
99552286Smdodd	return;
99621769Sjkh}
99721769Sjkh
99821769Sjkh
99955883Smdoddstatic int
100055883Smdoddex_ioctl(register struct ifnet *ifp, u_long cmd, caddr_t data)
100121769Sjkh{
100252286Smdodd	struct ex_softc *	sc = ifp->if_softc;
100352286Smdodd	int			s;
100452286Smdodd	int			error = 0;
100521769Sjkh
100652286Smdodd	DODEBUG(Start_End, printf("ex_ioctl%d: start ", ifp->if_unit););
100721769Sjkh
100852286Smdodd	s = splimp();
100921769Sjkh
101052286Smdodd	switch(cmd) {
101152286Smdodd		case SIOCSIFADDR:
101252286Smdodd		case SIOCGIFADDR:
101352286Smdodd		case SIOCSIFMTU:
101452286Smdodd			error = ether_ioctl(ifp, cmd, data);
101552286Smdodd			break;
101621769Sjkh
101752286Smdodd		case SIOCSIFFLAGS:
101852286Smdodd			DODEBUG(Start_End, printf("SIOCSIFFLAGS"););
101952286Smdodd			if ((ifp->if_flags & IFF_UP) == 0 &&
102052286Smdodd			    (ifp->if_flags & IFF_RUNNING)) {
102152286Smdodd
102252286Smdodd				ifp->if_flags &= ~IFF_RUNNING;
102352286Smdodd				ex_stop(sc);
102452286Smdodd			} else {
102552286Smdodd      				ex_init(sc);
102652286Smdodd			}
102752286Smdodd			break;
102821769Sjkh#ifdef NODEF
102952286Smdodd		case SIOCGHWADDR:
103052286Smdodd			DODEBUG(Start_End, printf("SIOCGHWADDR"););
103152286Smdodd			bcopy((caddr_t)sc->sc_addr, (caddr_t)&ifr->ifr_data,
103252286Smdodd			      sizeof(sc->sc_addr));
103352286Smdodd			break;
103421769Sjkh#endif
103552286Smdodd		case SIOCADDMULTI:
103652286Smdodd			DODEBUG(Start_End, printf("SIOCADDMULTI"););
103752286Smdodd		case SIOCDELMULTI:
103852286Smdodd			DODEBUG(Start_End, printf("SIOCDELMULTI"););
103952286Smdodd			/* XXX Support not done yet. */
104052286Smdodd			error = EINVAL;
104152286Smdodd			break;
104252286Smdodd		default:
104352286Smdodd			DODEBUG(Start_End, printf("unknown"););
104452286Smdodd			error = EINVAL;
104552286Smdodd	}
104621769Sjkh
104752286Smdodd	splx(s);
104821769Sjkh
104952286Smdodd	DODEBUG(Start_End, printf("\nex_ioctl%d: finish\n", ifp->if_unit););
105052286Smdodd
105152286Smdodd	return(error);
105221769Sjkh}
105321769Sjkh
105421769Sjkh
105552286Smdoddstatic void
105652286Smdoddex_reset(struct ex_softc *sc)
105721769Sjkh{
105852286Smdodd	int s;
105921769Sjkh
106052286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: start\n", unit););
106121769Sjkh
106252286Smdodd	s = splimp();
106321769Sjkh
106452286Smdodd	ex_stop(sc);
106552286Smdodd	ex_init(sc);
106621769Sjkh
106752286Smdodd	splx(s);
106821769Sjkh
106952286Smdodd	DODEBUG(Start_End, printf("ex_reset%d: finish\n", unit););
107052286Smdodd
107152286Smdodd	return;
107221769Sjkh}
107321769Sjkh
107421769Sjkh
107552286Smdoddstatic void
107652286Smdoddex_watchdog(struct ifnet *ifp)
107721769Sjkh{
107852286Smdodd	struct ex_softc *	sc = ifp->if_softc;
107921769Sjkh
108052286Smdodd	DODEBUG(Start_End, printf("ex_watchdog%d: start\n", ifp->if_unit););
108121769Sjkh
108252286Smdodd	ifp->if_flags &= ~IFF_OACTIVE;
108321769Sjkh
108452286Smdodd	DODEBUG(Status, printf("OIDLE watchdog\n"););
108552286Smdodd
108652286Smdodd	ifp->if_oerrors++;
108752286Smdodd	ex_reset(sc);
108852286Smdodd	ex_start(ifp);
108952286Smdodd
109052286Smdodd	DODEBUG(Start_End, printf("ex_watchdog%d: finish\n", ifp->if_unit););
109152286Smdodd
109252286Smdodd	return;
109321769Sjkh}
109421769Sjkh
109521769Sjkh
109652286Smdoddstatic u_short
109752286Smdoddeeprom_read(int iobase, int location)
109821769Sjkh{
109921769Sjkh	int i;
110021769Sjkh	u_short data = 0;
110121769Sjkh	int ee_addr;
110221769Sjkh	int read_cmd = location | EE_READ_CMD;
110321769Sjkh	short ctrl_val = EECS;
110421769Sjkh
110521769Sjkh	ee_addr = iobase + EEPROM_REG;
110621769Sjkh	outb(iobase + CMD_REG, Bank2_Sel);
110721769Sjkh	outb(ee_addr, EECS);
110821769Sjkh	for (i = 8; i >= 0; i--) {
110921769Sjkh		short outval = (read_cmd & (1 << i)) ? ctrl_val | EEDI : ctrl_val;
111021769Sjkh		outb(ee_addr, outval);
111121769Sjkh		outb(ee_addr, outval | EESK);
111221769Sjkh		DELAY(3);
111321769Sjkh		outb(ee_addr, outval);
111421769Sjkh		DELAY(2);
111521769Sjkh	}
111621769Sjkh	outb(ee_addr, ctrl_val);
111721769Sjkh
111821769Sjkh	for (i = 16; i > 0; i--) {
111921769Sjkh		outb(ee_addr, ctrl_val | EESK);
112021769Sjkh		DELAY(3);
112121769Sjkh		data = (data << 1) | ((inb(ee_addr) & EEDO) ? 1 : 0);
112221769Sjkh		outb(ee_addr, ctrl_val);
112321769Sjkh		DELAY(2);
112421769Sjkh	}
112521769Sjkh
112621769Sjkh	ctrl_val &= ~EECS;
112721769Sjkh	outb(ee_addr, ctrl_val | EESK);
112821769Sjkh	DELAY(3);
112921769Sjkh	outb(ee_addr, ctrl_val);
113021769Sjkh	DELAY(2);
113121769Sjkh	outb(iobase + CMD_REG, Bank0_Sel);
113221769Sjkh	return(data);
113321769Sjkh}
1134