if_tl.c revision 127135
136270Swpaul/*
236270Swpaul * Copyright (c) 1997, 1998
336270Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
436270Swpaul *
536270Swpaul * Redistribution and use in source and binary forms, with or without
636270Swpaul * modification, are permitted provided that the following conditions
736270Swpaul * are met:
836270Swpaul * 1. Redistributions of source code must retain the above copyright
936270Swpaul *    notice, this list of conditions and the following disclaimer.
1036270Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1136270Swpaul *    notice, this list of conditions and the following disclaimer in the
1236270Swpaul *    documentation and/or other materials provided with the distribution.
1336270Swpaul * 3. All advertising materials mentioning features or use of this software
1436270Swpaul *    must display the following acknowledgement:
1536270Swpaul *	This product includes software developed by Bill Paul.
1636270Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1736270Swpaul *    may be used to endorse or promote products derived from this software
1836270Swpaul *    without specific prior written permission.
1936270Swpaul *
2036270Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2136270Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2236270Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2336270Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2436270Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2536270Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2636270Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2736270Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2836270Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2936270Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3036270Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3136270Swpaul */
3236270Swpaul
33122678Sobrien#include <sys/cdefs.h>
34122678Sobrien__FBSDID("$FreeBSD: head/sys/pci/if_tl.c 127135 2004-03-17 17:50:55Z njl $");
35122678Sobrien
3636270Swpaul/*
3736270Swpaul * Texas Instruments ThunderLAN driver for FreeBSD 2.2.6 and 3.x.
3836270Swpaul * Supports many Compaq PCI NICs based on the ThunderLAN ethernet controller,
3936270Swpaul * the National Semiconductor DP83840A physical interface and the
4036270Swpaul * Microchip Technology 24Cxx series serial EEPROM.
4136270Swpaul *
4239583Swpaul * Written using the following four documents:
4336270Swpaul *
4436270Swpaul * Texas Instruments ThunderLAN Programmer's Guide (www.ti.com)
4536270Swpaul * National Semiconductor DP83840A data sheet (www.national.com)
4636270Swpaul * Microchip Technology 24C02C data sheet (www.microchip.com)
4739583Swpaul * Micro Linear ML6692 100BaseTX only PHY data sheet (www.microlinear.com)
4836270Swpaul *
4936270Swpaul * Written by Bill Paul <wpaul@ctr.columbia.edu>
5036270Swpaul * Electrical Engineering Department
5136270Swpaul * Columbia University, New York City
5236270Swpaul */
5336270Swpaul/*
5436270Swpaul * Some notes about the ThunderLAN:
5536270Swpaul *
5636270Swpaul * The ThunderLAN controller is a single chip containing PCI controller
5736270Swpaul * logic, approximately 3K of on-board SRAM, a LAN controller, and media
5839583Swpaul * independent interface (MII) bus. The MII allows the ThunderLAN chip to
5936270Swpaul * control up to 32 different physical interfaces (PHYs). The ThunderLAN
6036270Swpaul * also has a built-in 10baseT PHY, allowing a single ThunderLAN controller
6136270Swpaul * to act as a complete ethernet interface.
6236270Swpaul *
6336270Swpaul * Other PHYs may be attached to the ThunderLAN; the Compaq 10/100 cards
6436270Swpaul * use a National Semiconductor DP83840A PHY that supports 10 or 100Mb/sec
6536270Swpaul * in full or half duplex. Some of the Compaq Deskpro machines use a
6639583Swpaul * Level 1 LXT970 PHY with the same capabilities. Certain Olicom adapters
6739583Swpaul * use a Micro Linear ML6692 100BaseTX only PHY, which can be used in
6839583Swpaul * concert with the ThunderLAN's internal PHY to provide full 10/100
6939583Swpaul * support. This is cheaper than using a standalone external PHY for both
7039583Swpaul * 10/100 modes and letting the ThunderLAN's internal PHY go to waste.
7139583Swpaul * A serial EEPROM is also attached to the ThunderLAN chip to provide
7239583Swpaul * power-up default register settings and for storing the adapter's
7339583Swpaul * station address. Although not supported by this driver, the ThunderLAN
7439583Swpaul * chip can also be connected to token ring PHYs.
7536270Swpaul *
7636270Swpaul * The ThunderLAN has a set of registers which can be used to issue
7739583Swpaul * commands, acknowledge interrupts, and to manipulate other internal
7836270Swpaul * registers on its DIO bus. The primary registers can be accessed
7936270Swpaul * using either programmed I/O (inb/outb) or via PCI memory mapping,
8036270Swpaul * depending on how the card is configured during the PCI probing
8136270Swpaul * phase. It is even possible to have both PIO and memory mapped
8236270Swpaul * access turned on at the same time.
8336270Swpaul *
8436270Swpaul * Frame reception and transmission with the ThunderLAN chip is done
8536270Swpaul * using frame 'lists.' A list structure looks more or less like this:
8636270Swpaul *
8736270Swpaul * struct tl_frag {
8836270Swpaul *	u_int32_t		fragment_address;
8936270Swpaul *	u_int32_t		fragment_size;
9036270Swpaul * };
9136270Swpaul * struct tl_list {
9236270Swpaul *	u_int32_t		forward_pointer;
9336270Swpaul *	u_int16_t		cstat;
9436270Swpaul *	u_int16_t		frame_size;
9536270Swpaul *	struct tl_frag		fragments[10];
9636270Swpaul * };
9736270Swpaul *
9836270Swpaul * The forward pointer in the list header can be either a 0 or the address
9936270Swpaul * of another list, which allows several lists to be linked together. Each
10036270Swpaul * list contains up to 10 fragment descriptors. This means the chip allows
10136270Swpaul * ethernet frames to be broken up into up to 10 chunks for transfer to
10236270Swpaul * and from the SRAM. Note that the forward pointer and fragment buffer
10336270Swpaul * addresses are physical memory addresses, not virtual. Note also that
10436270Swpaul * a single ethernet frame can not span lists: if the host wants to
10536270Swpaul * transmit a frame and the frame data is split up over more than 10
10636270Swpaul * buffers, the frame has to collapsed before it can be transmitted.
10736270Swpaul *
10836270Swpaul * To receive frames, the driver sets up a number of lists and populates
10936270Swpaul * the fragment descriptors, then it sends an RX GO command to the chip.
11036270Swpaul * When a frame is received, the chip will DMA it into the memory regions
11136270Swpaul * specified by the fragment descriptors and then trigger an RX 'end of
11236270Swpaul * frame interrupt' when done. The driver may choose to use only one
11336270Swpaul * fragment per list; this may result is slighltly less efficient use
11436270Swpaul * of memory in exchange for improving performance.
11536270Swpaul *
11636270Swpaul * To transmit frames, the driver again sets up lists and fragment
11736270Swpaul * descriptors, only this time the buffers contain frame data that
11836270Swpaul * is to be DMA'ed into the chip instead of out of it. Once the chip
11936270Swpaul * has transfered the data into its on-board SRAM, it will trigger a
12036270Swpaul * TX 'end of frame' interrupt. It will also generate an 'end of channel'
12136270Swpaul * interrupt when it reaches the end of the list.
12236270Swpaul */
12336270Swpaul/*
12436270Swpaul * Some notes about this driver:
12536270Swpaul *
12636270Swpaul * The ThunderLAN chip provides a couple of different ways to organize
12736270Swpaul * reception, transmission and interrupt handling. The simplest approach
12836270Swpaul * is to use one list each for transmission and reception. In this mode,
12936270Swpaul * the ThunderLAN will generate two interrupts for every received frame
13036270Swpaul * (one RX EOF and one RX EOC) and two for each transmitted frame (one
13136270Swpaul * TX EOF and one TX EOC). This may make the driver simpler but it hurts
13236270Swpaul * performance to have to handle so many interrupts.
13336270Swpaul *
13436270Swpaul * Initially I wanted to create a circular list of receive buffers so
13536270Swpaul * that the ThunderLAN chip would think there was an infinitely long
13636270Swpaul * receive channel and never deliver an RXEOC interrupt. However this
13736270Swpaul * doesn't work correctly under heavy load: while the manual says the
13836270Swpaul * chip will trigger an RXEOF interrupt each time a frame is copied into
13936270Swpaul * memory, you can't count on the chip waiting around for you to acknowledge
14036270Swpaul * the interrupt before it starts trying to DMA the next frame. The result
14136270Swpaul * is that the chip might traverse the entire circular list and then wrap
14236270Swpaul * around before you have a chance to do anything about it. Consequently,
14336270Swpaul * the receive list is terminated (with a 0 in the forward pointer in the
14436270Swpaul * last element). Each time an RXEOF interrupt arrives, the used list
14536270Swpaul * is shifted to the end of the list. This gives the appearance of an
14636270Swpaul * infinitely large RX chain so long as the driver doesn't fall behind
14736270Swpaul * the chip and allow all of the lists to be filled up.
14836270Swpaul *
14936270Swpaul * If all the lists are filled, the adapter will deliver an RX 'end of
15036270Swpaul * channel' interrupt when it hits the 0 forward pointer at the end of
15136270Swpaul * the chain. The RXEOC handler then cleans out the RX chain and resets
15236270Swpaul * the list head pointer in the ch_parm register and restarts the receiver.
15336270Swpaul *
15436270Swpaul * For frame transmission, it is possible to program the ThunderLAN's
15536270Swpaul * transmit interrupt threshold so that the chip can acknowledge multiple
15636270Swpaul * lists with only a single TX EOF interrupt. This allows the driver to
15736270Swpaul * queue several frames in one shot, and only have to handle a total
15836270Swpaul * two interrupts (one TX EOF and one TX EOC) no matter how many frames
15936270Swpaul * are transmitted. Frame transmission is done directly out of the
16036270Swpaul * mbufs passed to the tl_start() routine via the interface send queue.
16136270Swpaul * The driver simply sets up the fragment descriptors in the transmit
16236270Swpaul * lists to point to the mbuf data regions and sends a TX GO command.
16336270Swpaul *
16436270Swpaul * Note that since the RX and TX lists themselves are always used
16536270Swpaul * only by the driver, the are malloc()ed once at driver initialization
16636270Swpaul * time and never free()ed.
16736270Swpaul *
16836270Swpaul * Also, in order to remain as platform independent as possible, this
16936270Swpaul * driver uses memory mapped register access to manipulate the card
17036270Swpaul * as opposed to programmed I/O. This avoids the use of the inb/outb
17136270Swpaul * (and related) instructions which are specific to the i386 platform.
17236270Swpaul *
17336270Swpaul * Using these techniques, this driver achieves very high performance
17436270Swpaul * by minimizing the amount of interrupts generated during large
17536270Swpaul * transfers and by completely avoiding buffer copies. Frame transfer
17636270Swpaul * to and from the ThunderLAN chip is performed entirely by the chip
17736270Swpaul * itself thereby reducing the load on the host CPU.
17836270Swpaul */
17936270Swpaul
18036270Swpaul#include <sys/param.h>
18136270Swpaul#include <sys/systm.h>
18236270Swpaul#include <sys/sockio.h>
18336270Swpaul#include <sys/mbuf.h>
18436270Swpaul#include <sys/malloc.h>
18536270Swpaul#include <sys/kernel.h>
18636270Swpaul#include <sys/socket.h>
18736270Swpaul
18836270Swpaul#include <net/if.h>
18936270Swpaul#include <net/if_arp.h>
19036270Swpaul#include <net/ethernet.h>
19136270Swpaul#include <net/if_dl.h>
19236270Swpaul#include <net/if_media.h>
19336270Swpaul
19436270Swpaul#include <net/bpf.h>
19536270Swpaul
19636270Swpaul#include <vm/vm.h>              /* for vtophys */
19736270Swpaul#include <vm/pmap.h>            /* for vtophys */
19845155Swpaul#include <machine/bus_memio.h>
19945155Swpaul#include <machine/bus_pio.h>
20045155Swpaul#include <machine/bus.h>
20148992Swpaul#include <machine/resource.h>
20248992Swpaul#include <sys/bus.h>
20348992Swpaul#include <sys/rman.h>
20436270Swpaul
20550462Swpaul#include <dev/mii/mii.h>
20650462Swpaul#include <dev/mii/miivar.h>
20750462Swpaul
208119288Simp#include <dev/pci/pcireg.h>
209119288Simp#include <dev/pci/pcivar.h>
21036270Swpaul
21139957Swpaul/*
21239957Swpaul * Default to using PIO register access mode to pacify certain
21339957Swpaul * laptop docking stations with built-in ThunderLAN chips that
21439957Swpaul * don't seem to handle memory mapped mode properly.
21539957Swpaul */
21639957Swpaul#define TL_USEIOSPACE
21739957Swpaul
21836270Swpaul#include <pci/if_tlreg.h>
21936270Swpaul
220113506SmdoddMODULE_DEPEND(tl, pci, 1, 1, 1);
221113506SmdoddMODULE_DEPEND(tl, ether, 1, 1, 1);
22259758SpeterMODULE_DEPEND(tl, miibus, 1, 1, 1);
22359758Speter
22451089Speter/* "controller miibus0" required.  See GENERIC if you get errors here. */
22550462Swpaul#include "miibus_if.h"
22650462Swpaul
22736270Swpaul/*
22836270Swpaul * Various supported device vendors/types and their names.
22936270Swpaul */
23036270Swpaul
23136270Swpaulstatic struct tl_type tl_devs[] = {
23236270Swpaul	{ TI_VENDORID,	TI_DEVICEID_THUNDERLAN,
23336270Swpaul		"Texas Instruments ThunderLAN" },
23436270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10,
23536270Swpaul		"Compaq Netelligent 10" },
23636270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100,
23736270Swpaul		"Compaq Netelligent 10/100" },
23836270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_PROLIANT,
23936270Swpaul		"Compaq Netelligent 10/100 Proliant" },
24036270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_DUAL,
24136270Swpaul		"Compaq Netelligent 10/100 Dual Port" },
24236270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETFLEX_3P_INTEGRATED,
24336270Swpaul		"Compaq NetFlex-3/P Integrated" },
24436270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETFLEX_3P,
24536270Swpaul		"Compaq NetFlex-3/P" },
24636270Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETFLEX_3P_BNC,
24736270Swpaul		"Compaq NetFlex 3/P w/ BNC" },
24837626Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_EMBEDDED,
24937626Swpaul		"Compaq Netelligent 10/100 TX Embedded UTP" },
25037626Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_T2_UTP_COAX,
25137626Swpaul		"Compaq Netelligent 10 T/2 PCI UTP/Coax" },
25237626Swpaul	{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_TX_UTP,
25337626Swpaul		"Compaq Netelligent 10/100 TX UTP" },
25437626Swpaul	{ OLICOM_VENDORID, OLICOM_DEVICEID_OC2183,
25537626Swpaul		"Olicom OC-2183/2185" },
25637626Swpaul	{ OLICOM_VENDORID, OLICOM_DEVICEID_OC2325,
25737626Swpaul		"Olicom OC-2325" },
25837626Swpaul	{ OLICOM_VENDORID, OLICOM_DEVICEID_OC2326,
25937626Swpaul		"Olicom OC-2326 10/100 TX UTP" },
26036270Swpaul	{ 0, 0, NULL }
26136270Swpaul};
26236270Swpaul
26392739Salfredstatic int tl_probe		(device_t);
26492739Salfredstatic int tl_attach		(device_t);
26592739Salfredstatic int tl_detach		(device_t);
26692739Salfredstatic int tl_intvec_rxeoc	(void *, u_int32_t);
26792739Salfredstatic int tl_intvec_txeoc	(void *, u_int32_t);
26892739Salfredstatic int tl_intvec_txeof	(void *, u_int32_t);
26992739Salfredstatic int tl_intvec_rxeof	(void *, u_int32_t);
27092739Salfredstatic int tl_intvec_adchk	(void *, u_int32_t);
27192739Salfredstatic int tl_intvec_netsts	(void *, u_int32_t);
27236270Swpaul
27392739Salfredstatic int tl_newbuf		(struct tl_softc *, struct tl_chain_onefrag *);
27492739Salfredstatic void tl_stats_update	(void *);
27592739Salfredstatic int tl_encap		(struct tl_softc *, struct tl_chain *,
27692739Salfred						struct mbuf *);
27736270Swpaul
27892739Salfredstatic void tl_intr		(void *);
27992739Salfredstatic void tl_start		(struct ifnet *);
28092739Salfredstatic int tl_ioctl		(struct ifnet *, u_long, caddr_t);
28192739Salfredstatic void tl_init		(void *);
28292739Salfredstatic void tl_stop		(struct tl_softc *);
28392739Salfredstatic void tl_watchdog		(struct ifnet *);
28492739Salfredstatic void tl_shutdown		(device_t);
28592739Salfredstatic int tl_ifmedia_upd	(struct ifnet *);
28692739Salfredstatic void tl_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
28736270Swpaul
28892739Salfredstatic u_int8_t tl_eeprom_putbyte	(struct tl_softc *, int);
28992739Salfredstatic u_int8_t	tl_eeprom_getbyte	(struct tl_softc *, int, u_int8_t *);
29092739Salfredstatic int tl_read_eeprom	(struct tl_softc *, caddr_t, int, int);
29136270Swpaul
29292739Salfredstatic void tl_mii_sync		(struct tl_softc *);
29392739Salfredstatic void tl_mii_send		(struct tl_softc *, u_int32_t, int);
29492739Salfredstatic int tl_mii_readreg	(struct tl_softc *, struct tl_mii_frame *);
29592739Salfredstatic int tl_mii_writereg	(struct tl_softc *, struct tl_mii_frame *);
29692739Salfredstatic int tl_miibus_readreg	(device_t, int, int);
29792739Salfredstatic int tl_miibus_writereg	(device_t, int, int, int);
29892739Salfredstatic void tl_miibus_statchg	(device_t);
29936270Swpaul
30092739Salfredstatic void tl_setmode		(struct tl_softc *, int);
301123289Sobrienstatic uint32_t tl_mchash	(const uint8_t *);
30292739Salfredstatic void tl_setmulti		(struct tl_softc *);
30392739Salfredstatic void tl_setfilt		(struct tl_softc *, caddr_t, int);
30492739Salfredstatic void tl_softreset	(struct tl_softc *, int);
30592739Salfredstatic void tl_hardreset	(device_t);
30692739Salfredstatic int tl_list_rx_init	(struct tl_softc *);
30792739Salfredstatic int tl_list_tx_init	(struct tl_softc *);
30836270Swpaul
30992739Salfredstatic u_int8_t tl_dio_read8	(struct tl_softc *, int);
31092739Salfredstatic u_int16_t tl_dio_read16	(struct tl_softc *, int);
31192739Salfredstatic u_int32_t tl_dio_read32	(struct tl_softc *, int);
31292739Salfredstatic void tl_dio_write8	(struct tl_softc *, int, int);
31392739Salfredstatic void tl_dio_write16	(struct tl_softc *, int, int);
31492739Salfredstatic void tl_dio_write32	(struct tl_softc *, int, int);
31592739Salfredstatic void tl_dio_setbit	(struct tl_softc *, int, int);
31692739Salfredstatic void tl_dio_clrbit	(struct tl_softc *, int, int);
31792739Salfredstatic void tl_dio_setbit16	(struct tl_softc *, int, int);
31892739Salfredstatic void tl_dio_clrbit16	(struct tl_softc *, int, int);
31939583Swpaul
32049010Swpaul#ifdef TL_USEIOSPACE
32149010Swpaul#define TL_RES		SYS_RES_IOPORT
32249010Swpaul#define TL_RID		TL_PCI_LOIO
32349010Swpaul#else
32449010Swpaul#define TL_RES		SYS_RES_MEMORY
32549010Swpaul#define TL_RID		TL_PCI_LOMEM
32649010Swpaul#endif
32749010Swpaul
32848992Swpaulstatic device_method_t tl_methods[] = {
32948992Swpaul	/* Device interface */
33048992Swpaul	DEVMETHOD(device_probe,		tl_probe),
33148992Swpaul	DEVMETHOD(device_attach,	tl_attach),
33248992Swpaul	DEVMETHOD(device_detach,	tl_detach),
33348992Swpaul	DEVMETHOD(device_shutdown,	tl_shutdown),
33450462Swpaul
33550462Swpaul	/* bus interface */
33650462Swpaul	DEVMETHOD(bus_print_child,	bus_generic_print_child),
33750462Swpaul	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
33850462Swpaul
33950462Swpaul	/* MII interface */
34050462Swpaul	DEVMETHOD(miibus_readreg,	tl_miibus_readreg),
34150462Swpaul	DEVMETHOD(miibus_writereg,	tl_miibus_writereg),
34250462Swpaul	DEVMETHOD(miibus_statchg,	tl_miibus_statchg),
34350462Swpaul
34448992Swpaul	{ 0, 0 }
34548992Swpaul};
34648992Swpaul
34748992Swpaulstatic driver_t tl_driver = {
34851455Swpaul	"tl",
34948992Swpaul	tl_methods,
35048992Swpaul	sizeof(struct tl_softc)
35148992Swpaul};
35248992Swpaul
35348992Swpaulstatic devclass_t tl_devclass;
35448992Swpaul
355113506SmdoddDRIVER_MODULE(tl, pci, tl_driver, tl_devclass, 0, 0);
35651473SwpaulDRIVER_MODULE(miibus, tl, miibus_driver, miibus_devclass, 0, 0);
35748992Swpaul
35839583Swpaulstatic u_int8_t tl_dio_read8(sc, reg)
35941656Swpaul	struct tl_softc		*sc;
36041656Swpaul	int			reg;
36139583Swpaul{
36239583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
36339583Swpaul	return(CSR_READ_1(sc, TL_DIO_DATA + (reg & 3)));
36439583Swpaul}
36539583Swpaul
36639583Swpaulstatic u_int16_t tl_dio_read16(sc, reg)
36741656Swpaul	struct tl_softc		*sc;
36841656Swpaul	int			reg;
36939583Swpaul{
37039583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
37139583Swpaul	return(CSR_READ_2(sc, TL_DIO_DATA + (reg & 3)));
37239583Swpaul}
37339583Swpaul
37439583Swpaulstatic u_int32_t tl_dio_read32(sc, reg)
37541656Swpaul	struct tl_softc		*sc;
37641656Swpaul	int			reg;
37739583Swpaul{
37839583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
37939583Swpaul	return(CSR_READ_4(sc, TL_DIO_DATA + (reg & 3)));
38039583Swpaul}
38139583Swpaul
38239583Swpaulstatic void tl_dio_write8(sc, reg, val)
38341656Swpaul	struct tl_softc		*sc;
38441656Swpaul	int			reg;
38541656Swpaul	int			val;
38639583Swpaul{
38739583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
38839583Swpaul	CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), val);
38939583Swpaul	return;
39039583Swpaul}
39139583Swpaul
39239583Swpaulstatic void tl_dio_write16(sc, reg, val)
39341656Swpaul	struct tl_softc		*sc;
39441656Swpaul	int			reg;
39541656Swpaul	int			val;
39639583Swpaul{
39739583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
39839583Swpaul	CSR_WRITE_2(sc, TL_DIO_DATA + (reg & 3), val);
39939583Swpaul	return;
40039583Swpaul}
40139583Swpaul
40239583Swpaulstatic void tl_dio_write32(sc, reg, val)
40341656Swpaul	struct tl_softc		*sc;
40441656Swpaul	int			reg;
40541656Swpaul	int			val;
40639583Swpaul{
40739583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
40839583Swpaul	CSR_WRITE_4(sc, TL_DIO_DATA + (reg & 3), val);
40939583Swpaul	return;
41039583Swpaul}
41139583Swpaul
412102336Salfredstatic void
413102336Salfredtl_dio_setbit(sc, reg, bit)
41441656Swpaul	struct tl_softc		*sc;
41541656Swpaul	int			reg;
41641656Swpaul	int			bit;
41739583Swpaul{
41839583Swpaul	u_int8_t			f;
41939583Swpaul
42039583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
42139583Swpaul	f = CSR_READ_1(sc, TL_DIO_DATA + (reg & 3));
42239583Swpaul	f |= bit;
42339583Swpaul	CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), f);
42439583Swpaul
42539583Swpaul	return;
42639583Swpaul}
42739583Swpaul
428102336Salfredstatic void
429102336Salfredtl_dio_clrbit(sc, reg, bit)
43041656Swpaul	struct tl_softc		*sc;
43141656Swpaul	int			reg;
43241656Swpaul	int			bit;
43339583Swpaul{
43439583Swpaul	u_int8_t			f;
43539583Swpaul
43639583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
43739583Swpaul	f = CSR_READ_1(sc, TL_DIO_DATA + (reg & 3));
43839583Swpaul	f &= ~bit;
43939583Swpaul	CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), f);
44039583Swpaul
44139583Swpaul	return;
44239583Swpaul}
44339583Swpaul
44439583Swpaulstatic void tl_dio_setbit16(sc, reg, bit)
44541656Swpaul	struct tl_softc		*sc;
44641656Swpaul	int			reg;
44741656Swpaul	int			bit;
44839583Swpaul{
44939583Swpaul	u_int16_t			f;
45039583Swpaul
45139583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
45239583Swpaul	f = CSR_READ_2(sc, TL_DIO_DATA + (reg & 3));
45339583Swpaul	f |= bit;
45439583Swpaul	CSR_WRITE_2(sc, TL_DIO_DATA + (reg & 3), f);
45539583Swpaul
45639583Swpaul	return;
45739583Swpaul}
45839583Swpaul
45939583Swpaulstatic void tl_dio_clrbit16(sc, reg, bit)
46041656Swpaul	struct tl_softc		*sc;
46141656Swpaul	int			reg;
46241656Swpaul	int			bit;
46339583Swpaul{
46439583Swpaul	u_int16_t			f;
46539583Swpaul
46639583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
46739583Swpaul	f = CSR_READ_2(sc, TL_DIO_DATA + (reg & 3));
46839583Swpaul	f &= ~bit;
46939583Swpaul	CSR_WRITE_2(sc, TL_DIO_DATA + (reg & 3), f);
47039583Swpaul
47139583Swpaul	return;
47239583Swpaul}
47339583Swpaul
47436270Swpaul/*
47536270Swpaul * Send an instruction or address to the EEPROM, check for ACK.
47636270Swpaul */
47739583Swpaulstatic u_int8_t tl_eeprom_putbyte(sc, byte)
47839583Swpaul	struct tl_softc		*sc;
47941656Swpaul	int			byte;
48036270Swpaul{
48136270Swpaul	register int		i, ack = 0;
48236270Swpaul
48336270Swpaul	/*
48436270Swpaul	 * Make sure we're in TX mode.
48536270Swpaul	 */
48639583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ETXEN);
48736270Swpaul
48836270Swpaul	/*
48936270Swpaul	 * Feed in each bit and stobe the clock.
49036270Swpaul	 */
49136270Swpaul	for (i = 0x80; i; i >>= 1) {
49236270Swpaul		if (byte & i) {
49339583Swpaul			tl_dio_setbit(sc, TL_NETSIO, TL_SIO_EDATA);
49436270Swpaul		} else {
49539583Swpaul			tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_EDATA);
49636270Swpaul		}
49739583Swpaul		DELAY(1);
49839583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ECLOK);
49939583Swpaul		DELAY(1);
50039583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ECLOK);
50136270Swpaul	}
50236270Swpaul
50336270Swpaul	/*
50436270Swpaul	 * Turn off TX mode.
50536270Swpaul	 */
50639583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ETXEN);
50736270Swpaul
50836270Swpaul	/*
50936270Swpaul	 * Check for ack.
51036270Swpaul	 */
51139583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ECLOK);
51239583Swpaul	ack = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_EDATA;
51339583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ECLOK);
51436270Swpaul
51536270Swpaul	return(ack);
51636270Swpaul}
51736270Swpaul
51836270Swpaul/*
51936270Swpaul * Read a byte of data stored in the EEPROM at address 'addr.'
52036270Swpaul */
52139583Swpaulstatic u_int8_t tl_eeprom_getbyte(sc, addr, dest)
52239583Swpaul	struct tl_softc		*sc;
52341656Swpaul	int			addr;
52436270Swpaul	u_int8_t		*dest;
52536270Swpaul{
52636270Swpaul	register int		i;
52736270Swpaul	u_int8_t		byte = 0;
528105599Sbrooks	struct ifnet		*ifp = &sc->arpcom.ac_if;
52936270Swpaul
53039583Swpaul	tl_dio_write8(sc, TL_NETSIO, 0);
53139583Swpaul
53236270Swpaul	EEPROM_START;
53339583Swpaul
53436270Swpaul	/*
53536270Swpaul	 * Send write control code to EEPROM.
53636270Swpaul	 */
53739583Swpaul	if (tl_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
538105599Sbrooks		if_printf(ifp, "failed to send write command, status: %x\n",
539105599Sbrooks		    tl_dio_read8(sc, TL_NETSIO));
54036270Swpaul		return(1);
54139583Swpaul	}
54236270Swpaul
54336270Swpaul	/*
54436270Swpaul	 * Send address of byte we want to read.
54536270Swpaul	 */
54639583Swpaul	if (tl_eeprom_putbyte(sc, addr)) {
547105599Sbrooks		if_printf(ifp, "failed to send address, status: %x\n",
548105599Sbrooks		    tl_dio_read8(sc, TL_NETSIO));
54936270Swpaul		return(1);
55039583Swpaul	}
55136270Swpaul
55236270Swpaul	EEPROM_STOP;
55336270Swpaul	EEPROM_START;
55436270Swpaul	/*
55536270Swpaul	 * Send read control code to EEPROM.
55636270Swpaul	 */
55739583Swpaul	if (tl_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
558105599Sbrooks		if_printf(ifp, "failed to send write command, status: %x\n",
559105599Sbrooks		    tl_dio_read8(sc, TL_NETSIO));
56036270Swpaul		return(1);
56139583Swpaul	}
56236270Swpaul
56336270Swpaul	/*
56436270Swpaul	 * Start reading bits from EEPROM.
56536270Swpaul	 */
56639583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ETXEN);
56736270Swpaul	for (i = 0x80; i; i >>= 1) {
56839583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ECLOK);
56939583Swpaul		DELAY(1);
57039583Swpaul		if (tl_dio_read8(sc, TL_NETSIO) & TL_SIO_EDATA)
57136270Swpaul			byte |= i;
57239583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ECLOK);
57336501Swpaul		DELAY(1);
57436270Swpaul	}
57536270Swpaul
57636270Swpaul	EEPROM_STOP;
57736270Swpaul
57836270Swpaul	/*
57936270Swpaul	 * No ACK generated for read, so just return byte.
58036270Swpaul	 */
58136270Swpaul
58236270Swpaul	*dest = byte;
58336270Swpaul
58436270Swpaul	return(0);
58536270Swpaul}
58636270Swpaul
58739583Swpaul/*
58839583Swpaul * Read a sequence of bytes from the EEPROM.
58939583Swpaul */
590102336Salfredstatic int
591102336Salfredtl_read_eeprom(sc, dest, off, cnt)
59239583Swpaul	struct tl_softc		*sc;
59339583Swpaul	caddr_t			dest;
59439583Swpaul	int			off;
59539583Swpaul	int			cnt;
59636270Swpaul{
59739583Swpaul	int			err = 0, i;
59839583Swpaul	u_int8_t		byte = 0;
59939583Swpaul
60039583Swpaul	for (i = 0; i < cnt; i++) {
60139583Swpaul		err = tl_eeprom_getbyte(sc, off + i, &byte);
60239583Swpaul		if (err)
60339583Swpaul			break;
60439583Swpaul		*(dest + i) = byte;
60539583Swpaul	}
60639583Swpaul
60739583Swpaul	return(err ? 1 : 0);
60839583Swpaul}
60939583Swpaul
610102336Salfredstatic void
611102336Salfredtl_mii_sync(sc)
61239583Swpaul	struct tl_softc		*sc;
61339583Swpaul{
61436270Swpaul	register int		i;
61536270Swpaul
61639583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN);
61736270Swpaul
61836270Swpaul	for (i = 0; i < 32; i++) {
61939583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
62039583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
62136270Swpaul	}
62236270Swpaul
62336270Swpaul	return;
62436270Swpaul}
62536270Swpaul
626102336Salfredstatic void
627102336Salfredtl_mii_send(sc, bits, cnt)
62839583Swpaul	struct tl_softc		*sc;
62936270Swpaul	u_int32_t		bits;
63036270Swpaul	int			cnt;
63136270Swpaul{
63236270Swpaul	int			i;
63336270Swpaul
63436270Swpaul	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
63539583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
63636270Swpaul		if (bits & i) {
63739583Swpaul			tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MDATA);
63836270Swpaul		} else {
63939583Swpaul			tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MDATA);
64036270Swpaul		}
64139583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
64236270Swpaul	}
64336270Swpaul}
64436270Swpaul
645102336Salfredstatic int
646102336Salfredtl_mii_readreg(sc, frame)
64739583Swpaul	struct tl_softc		*sc;
64836270Swpaul	struct tl_mii_frame	*frame;
64936270Swpaul
65036270Swpaul{
65167087Swpaul	int			i, ack;
65236270Swpaul	int			minten = 0;
65336270Swpaul
65467087Swpaul	TL_LOCK(sc);
65536270Swpaul
65639583Swpaul	tl_mii_sync(sc);
65736270Swpaul
65836270Swpaul	/*
65936270Swpaul	 * Set up frame for RX.
66036270Swpaul	 */
66136270Swpaul	frame->mii_stdelim = TL_MII_STARTDELIM;
66236270Swpaul	frame->mii_opcode = TL_MII_READOP;
66336270Swpaul	frame->mii_turnaround = 0;
66436270Swpaul	frame->mii_data = 0;
66536270Swpaul
66636270Swpaul	/*
66736270Swpaul	 * Turn off MII interrupt by forcing MINTEN low.
66836270Swpaul	 */
66939583Swpaul	minten = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MINTEN;
67036270Swpaul	if (minten) {
67139583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MINTEN);
67236270Swpaul	}
67336270Swpaul
67436270Swpaul	/*
67536270Swpaul 	 * Turn on data xmit.
67636270Swpaul	 */
67739583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MTXEN);
67836270Swpaul
67936270Swpaul	/*
68036270Swpaul	 * Send command/address info.
68136270Swpaul	 */
68239583Swpaul	tl_mii_send(sc, frame->mii_stdelim, 2);
68339583Swpaul	tl_mii_send(sc, frame->mii_opcode, 2);
68439583Swpaul	tl_mii_send(sc, frame->mii_phyaddr, 5);
68539583Swpaul	tl_mii_send(sc, frame->mii_regaddr, 5);
68636270Swpaul
68736270Swpaul	/*
68836270Swpaul	 * Turn off xmit.
68936270Swpaul	 */
69039583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN);
69136270Swpaul
69236270Swpaul	/* Idle bit */
69339583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
69439583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
69536270Swpaul
69636270Swpaul	/* Check for ack */
69739583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
69839583Swpaul	ack = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MDATA;
69936270Swpaul
70036270Swpaul	/* Complete the cycle */
70139583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
70236270Swpaul
70336270Swpaul	/*
70436270Swpaul	 * Now try reading data bits. If the ack failed, we still
70536270Swpaul	 * need to clock through 16 cycles to keep the PHYs in sync.
70636270Swpaul	 */
70736270Swpaul	if (ack) {
70836270Swpaul		for(i = 0; i < 16; i++) {
70939583Swpaul			tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
71039583Swpaul			tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
71136270Swpaul		}
71236270Swpaul		goto fail;
71336270Swpaul	}
71436270Swpaul
71536270Swpaul	for (i = 0x8000; i; i >>= 1) {
71639583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
71736270Swpaul		if (!ack) {
71839583Swpaul			if (tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MDATA)
71936270Swpaul				frame->mii_data |= i;
72036270Swpaul		}
72139583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
72236270Swpaul	}
72336270Swpaul
72436270Swpaulfail:
72536270Swpaul
72639583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
72739583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
72836270Swpaul
72936270Swpaul	/* Reenable interrupts */
73036270Swpaul	if (minten) {
73139583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MINTEN);
73236270Swpaul	}
73336270Swpaul
73467087Swpaul	TL_UNLOCK(sc);
73536270Swpaul
73636270Swpaul	if (ack)
73736270Swpaul		return(1);
73836270Swpaul	return(0);
73936270Swpaul}
74036270Swpaul
741102336Salfredstatic int
742102336Salfredtl_mii_writereg(sc, frame)
74339583Swpaul	struct tl_softc		*sc;
74436270Swpaul	struct tl_mii_frame	*frame;
74536270Swpaul
74636270Swpaul{
74736270Swpaul	int			minten;
74836270Swpaul
74967087Swpaul	TL_LOCK(sc);
75067087Swpaul
75139583Swpaul	tl_mii_sync(sc);
75236270Swpaul
75336270Swpaul	/*
75436270Swpaul	 * Set up frame for TX.
75536270Swpaul	 */
75636270Swpaul
75736270Swpaul	frame->mii_stdelim = TL_MII_STARTDELIM;
75836270Swpaul	frame->mii_opcode = TL_MII_WRITEOP;
75936270Swpaul	frame->mii_turnaround = TL_MII_TURNAROUND;
76036270Swpaul
76136270Swpaul	/*
76236270Swpaul	 * Turn off MII interrupt by forcing MINTEN low.
76336270Swpaul	 */
76439583Swpaul	minten = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MINTEN;
76536270Swpaul	if (minten) {
76639583Swpaul		tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MINTEN);
76736270Swpaul	}
76836270Swpaul
76936270Swpaul	/*
77036270Swpaul 	 * Turn on data output.
77136270Swpaul	 */
77239583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MTXEN);
77336270Swpaul
77439583Swpaul	tl_mii_send(sc, frame->mii_stdelim, 2);
77539583Swpaul	tl_mii_send(sc, frame->mii_opcode, 2);
77639583Swpaul	tl_mii_send(sc, frame->mii_phyaddr, 5);
77739583Swpaul	tl_mii_send(sc, frame->mii_regaddr, 5);
77839583Swpaul	tl_mii_send(sc, frame->mii_turnaround, 2);
77939583Swpaul	tl_mii_send(sc, frame->mii_data, 16);
78036270Swpaul
78139583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
78239583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
78336270Swpaul
78436270Swpaul	/*
78536270Swpaul	 * Turn off xmit.
78636270Swpaul	 */
78739583Swpaul	tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN);
78836270Swpaul
78936270Swpaul	/* Reenable interrupts */
79036270Swpaul	if (minten)
79139583Swpaul		tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MINTEN);
79236270Swpaul
79367087Swpaul	TL_UNLOCK(sc);
79436270Swpaul
79536270Swpaul	return(0);
79636270Swpaul}
79736270Swpaul
798102336Salfredstatic int
799102336Salfredtl_miibus_readreg(dev, phy, reg)
80050462Swpaul	device_t		dev;
80150462Swpaul	int			phy, reg;
80250462Swpaul{
80336270Swpaul	struct tl_softc		*sc;
80436270Swpaul	struct tl_mii_frame	frame;
80536270Swpaul
80650462Swpaul	sc = device_get_softc(dev);
80736270Swpaul	bzero((char *)&frame, sizeof(frame));
80836270Swpaul
80950462Swpaul	frame.mii_phyaddr = phy;
81036270Swpaul	frame.mii_regaddr = reg;
81139583Swpaul	tl_mii_readreg(sc, &frame);
81236270Swpaul
81336270Swpaul	return(frame.mii_data);
81436270Swpaul}
81536270Swpaul
816102336Salfredstatic int
817102336Salfredtl_miibus_writereg(dev, phy, reg, data)
81850462Swpaul	device_t		dev;
81950462Swpaul	int			phy, reg, data;
82050462Swpaul{
82136270Swpaul	struct tl_softc		*sc;
82236270Swpaul	struct tl_mii_frame	frame;
82336270Swpaul
82450462Swpaul	sc = device_get_softc(dev);
82536270Swpaul	bzero((char *)&frame, sizeof(frame));
82636270Swpaul
82750462Swpaul	frame.mii_phyaddr = phy;
82836270Swpaul	frame.mii_regaddr = reg;
82936270Swpaul	frame.mii_data = data;
83036270Swpaul
83139583Swpaul	tl_mii_writereg(sc, &frame);
83236270Swpaul
83350462Swpaul	return(0);
83436270Swpaul}
83536270Swpaul
836102336Salfredstatic void
837102336Salfredtl_miibus_statchg(dev)
83850462Swpaul	device_t		dev;
83950462Swpaul{
84036270Swpaul	struct tl_softc		*sc;
84150462Swpaul	struct mii_data		*mii;
84236270Swpaul
84350462Swpaul	sc = device_get_softc(dev);
84467087Swpaul	TL_LOCK(sc);
84550462Swpaul	mii = device_get_softc(sc->tl_miibus);
84636270Swpaul
84750462Swpaul	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
84850462Swpaul		tl_dio_setbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
84936270Swpaul	} else {
85050462Swpaul		tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
85136270Swpaul	}
85267087Swpaul	TL_UNLOCK(sc);
85336270Swpaul
85436270Swpaul	return;
85536270Swpaul}
85636270Swpaul
85736270Swpaul/*
85850462Swpaul * Set modes for bitrate devices.
85936270Swpaul */
860102336Salfredstatic void
861102336Salfredtl_setmode(sc, media)
86236270Swpaul	struct tl_softc		*sc;
86336270Swpaul	int			media;
86436270Swpaul{
86550462Swpaul	if (IFM_SUBTYPE(media) == IFM_10_5)
86650462Swpaul		tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_MTXD1);
86736270Swpaul	if (IFM_SUBTYPE(media) == IFM_10_T) {
86850462Swpaul		tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_MTXD1);
86936270Swpaul		if ((media & IFM_GMASK) == IFM_FDX) {
87050462Swpaul			tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_MTXD3);
87139583Swpaul			tl_dio_setbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
87236270Swpaul		} else {
87350462Swpaul			tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_MTXD3);
87439583Swpaul			tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
87536270Swpaul		}
87636270Swpaul	}
87736270Swpaul
87836270Swpaul	return;
87936270Swpaul}
88036270Swpaul
88136464Swpaul/*
88236464Swpaul * Calculate the hash of a MAC address for programming the multicast hash
88336464Swpaul * table.  This hash is simply the address split into 6-bit chunks
88436464Swpaul * XOR'd, e.g.
88536464Swpaul * byte: 000000|00 1111|1111 22|222222|333333|33 4444|4444 55|555555
88636464Swpaul * bit:  765432|10 7654|3210 76|543210|765432|10 7654|3210 76|543210
88736464Swpaul * Bytes 0-2 and 3-5 are symmetrical, so are folded together.  Then
88836464Swpaul * the folded 24-bit value is split into 6-bit portions and XOR'd.
88936464Swpaul */
890123289Sobrienstatic uint32_t
891122625Sobrientl_mchash(addr)
892123289Sobrien	const uint8_t *addr;
89336270Swpaul{
894123289Sobrien	int t;
89536270Swpaul
89636464Swpaul	t = (addr[0] ^ addr[3]) << 16 | (addr[1] ^ addr[4]) << 8 |
89736464Swpaul		(addr[2] ^ addr[5]);
89836464Swpaul	return ((t >> 18) ^ (t >> 12) ^ (t >> 6) ^ t) & 0x3f;
89936270Swpaul}
90036270Swpaul
90139583Swpaul/*
90239583Swpaul * The ThunderLAN has a perfect MAC address filter in addition to
90339583Swpaul * the multicast hash filter. The perfect filter can be programmed
90439583Swpaul * with up to four MAC addresses. The first one is always used to
90539583Swpaul * hold the station address, which leaves us free to use the other
90639583Swpaul * three for multicast addresses.
90739583Swpaul */
908102336Salfredstatic void
909102336Salfredtl_setfilt(sc, addr, slot)
91039583Swpaul	struct tl_softc		*sc;
91141656Swpaul	caddr_t			addr;
91239583Swpaul	int			slot;
91339583Swpaul{
91439583Swpaul	int			i;
91539583Swpaul	u_int16_t		regaddr;
91639583Swpaul
91739583Swpaul	regaddr = TL_AREG0_B5 + (slot * ETHER_ADDR_LEN);
91839583Swpaul
91939583Swpaul	for (i = 0; i < ETHER_ADDR_LEN; i++)
92039583Swpaul		tl_dio_write8(sc, regaddr + i, *(addr + i));
92139583Swpaul
92239583Swpaul	return;
92339583Swpaul}
92439583Swpaul
92539583Swpaul/*
92639583Swpaul * XXX In FreeBSD 3.0, multicast addresses are managed using a doubly
92739583Swpaul * linked list. This is fine, except addresses are added from the head
92839583Swpaul * end of the list. We want to arrange for 224.0.0.1 (the "all hosts")
92939583Swpaul * group to always be in the perfect filter, but as more groups are added,
93039583Swpaul * the 224.0.0.1 entry (which is always added first) gets pushed down
93139583Swpaul * the list and ends up at the tail. So after 3 or 4 multicast groups
93239583Swpaul * are added, the all-hosts entry gets pushed out of the perfect filter
93339583Swpaul * and into the hash table.
93439583Swpaul *
93539583Swpaul * Because the multicast list is a doubly-linked list as opposed to a
93639583Swpaul * circular queue, we don't have the ability to just grab the tail of
93739583Swpaul * the list and traverse it backwards. Instead, we have to traverse
93839583Swpaul * the list once to find the tail, then traverse it again backwards to
93939583Swpaul * update the multicast filter.
94039583Swpaul */
941102336Salfredstatic void
942102336Salfredtl_setmulti(sc)
94336270Swpaul	struct tl_softc		*sc;
94436270Swpaul{
94536270Swpaul	struct ifnet		*ifp;
94636270Swpaul	u_int32_t		hashes[2] = { 0, 0 };
94739583Swpaul	int			h, i;
94836270Swpaul	struct ifmultiaddr	*ifma;
94939583Swpaul	u_int8_t		dummy[] = { 0, 0, 0, 0, 0 ,0 };
95036270Swpaul	ifp = &sc->arpcom.ac_if;
95136270Swpaul
95239583Swpaul	/* First, zot all the existing filters. */
95339583Swpaul	for (i = 1; i < 4; i++)
95441656Swpaul		tl_setfilt(sc, (caddr_t)&dummy, i);
95539583Swpaul	tl_dio_write32(sc, TL_HASH1, 0);
95639583Swpaul	tl_dio_write32(sc, TL_HASH2, 0);
95739583Swpaul
95839583Swpaul	/* Now program new ones. */
95939583Swpaul	if (ifp->if_flags & IFF_ALLMULTI) {
96036270Swpaul		hashes[0] = 0xFFFFFFFF;
96136270Swpaul		hashes[1] = 0xFFFFFFFF;
96236270Swpaul	} else {
96339583Swpaul		i = 1;
96472084Sphk		TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) {
96536270Swpaul			if (ifma->ifma_addr->sa_family != AF_LINK)
96636270Swpaul				continue;
96739583Swpaul			/*
96839583Swpaul			 * Program the first three multicast groups
96939583Swpaul			 * into the perfect filter. For all others,
97039583Swpaul			 * use the hash table.
97139583Swpaul			 */
97239583Swpaul			if (i < 4) {
97339583Swpaul				tl_setfilt(sc,
97439583Swpaul			LLADDR((struct sockaddr_dl *)ifma->ifma_addr), i);
97539583Swpaul				i++;
97639583Swpaul				continue;
97739583Swpaul			}
97839583Swpaul
979122625Sobrien			h = tl_mchash(
98036270Swpaul				LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
98136270Swpaul			if (h < 32)
98236270Swpaul				hashes[0] |= (1 << h);
98336270Swpaul			else
98436317Swpaul				hashes[1] |= (1 << (h - 32));
98536270Swpaul		}
98636270Swpaul	}
98736270Swpaul
98839583Swpaul	tl_dio_write32(sc, TL_HASH1, hashes[0]);
98939583Swpaul	tl_dio_write32(sc, TL_HASH2, hashes[1]);
99036270Swpaul
99136270Swpaul	return;
99236270Swpaul}
99336270Swpaul
99439583Swpaul/*
99539583Swpaul * This routine is recommended by the ThunderLAN manual to insure that
99639583Swpaul * the internal PHY is powered up correctly. It also recommends a one
99739583Swpaul * second pause at the end to 'wait for the clocks to start' but in my
99839583Swpaul * experience this isn't necessary.
99939583Swpaul */
1000102336Salfredstatic void
1001102336Salfredtl_hardreset(dev)
100250468Swpaul	device_t		dev;
100350468Swpaul{
100439583Swpaul	struct tl_softc		*sc;
100539583Swpaul	int			i;
100650468Swpaul	u_int16_t		flags;
100739583Swpaul
100850468Swpaul	sc = device_get_softc(dev);
100939583Swpaul
101050468Swpaul	tl_mii_sync(sc);
101139583Swpaul
101250468Swpaul	flags = BMCR_LOOP|BMCR_ISO|BMCR_PDOWN;
101339583Swpaul
101450468Swpaul	for (i = 0; i < MII_NPHY; i++)
101550468Swpaul		tl_miibus_writereg(dev, i, MII_BMCR, flags);
101639583Swpaul
101750468Swpaul	tl_miibus_writereg(dev, 31, MII_BMCR, BMCR_ISO);
101839583Swpaul	DELAY(50000);
101950468Swpaul	tl_miibus_writereg(dev, 31, MII_BMCR, BMCR_LOOP|BMCR_ISO);
102039583Swpaul	tl_mii_sync(sc);
102150468Swpaul	while(tl_miibus_readreg(dev, 31, MII_BMCR) & BMCR_RESET);
102239583Swpaul
102350468Swpaul	DELAY(50000);
102439583Swpaul	return;
102539583Swpaul}
102639583Swpaul
1027102336Salfredstatic void
1028102336Salfredtl_softreset(sc, internal)
102939583Swpaul	struct tl_softc		*sc;
103036270Swpaul	int			internal;
103136270Swpaul{
103239583Swpaul        u_int32_t               cmd, dummy, i;
103336270Swpaul
103436270Swpaul        /* Assert the adapter reset bit. */
103539583Swpaul	CMD_SET(sc, TL_CMD_ADRST);
103650468Swpaul
103736270Swpaul        /* Turn off interrupts */
103839583Swpaul	CMD_SET(sc, TL_CMD_INTSOFF);
103936270Swpaul
104036270Swpaul	/* First, clear the stats registers. */
104139583Swpaul	for (i = 0; i < 5; i++)
104239583Swpaul		dummy = tl_dio_read32(sc, TL_TXGOODFRAMES);
104336270Swpaul
104436270Swpaul        /* Clear Areg and Hash registers */
104539583Swpaul	for (i = 0; i < 8; i++)
104639583Swpaul		tl_dio_write32(sc, TL_AREG0_B5, 0x00000000);
104736270Swpaul
104836270Swpaul        /*
104936270Swpaul	 * Set up Netconfig register. Enable one channel and
105036270Swpaul	 * one fragment mode.
105136270Swpaul	 */
105239583Swpaul	tl_dio_setbit16(sc, TL_NETCONFIG, TL_CFG_ONECHAN|TL_CFG_ONEFRAG);
105345155Swpaul	if (internal && !sc->tl_bitrate) {
105439583Swpaul		tl_dio_setbit16(sc, TL_NETCONFIG, TL_CFG_PHYEN);
105536270Swpaul	} else {
105639583Swpaul		tl_dio_clrbit16(sc, TL_NETCONFIG, TL_CFG_PHYEN);
105736270Swpaul	}
105836270Swpaul
105945155Swpaul	/* Handle cards with bitrate devices. */
106045155Swpaul	if (sc->tl_bitrate)
106145155Swpaul		tl_dio_setbit16(sc, TL_NETCONFIG, TL_CFG_BITRATE);
106245155Swpaul
106336270Swpaul	/*
106436270Swpaul	 * Load adapter irq pacing timer and tx threshold.
106536270Swpaul	 * We make the transmit threshold 1 initially but we may
106636270Swpaul	 * change that later.
106736270Swpaul	 */
106839583Swpaul	cmd = CSR_READ_4(sc, TL_HOSTCMD);
106936270Swpaul	cmd |= TL_CMD_NES;
107036270Swpaul	cmd &= ~(TL_CMD_RT|TL_CMD_EOC|TL_CMD_ACK_MASK|TL_CMD_CHSEL_MASK);
107139583Swpaul	CMD_PUT(sc, cmd | (TL_CMD_LDTHR | TX_THR));
107239583Swpaul	CMD_PUT(sc, cmd | (TL_CMD_LDTMR | 0x00000003));
107336270Swpaul
107436270Swpaul        /* Unreset the MII */
107539583Swpaul	tl_dio_setbit(sc, TL_NETSIO, TL_SIO_NMRST);
107636270Swpaul
107736270Swpaul	/* Take the adapter out of reset */
107839583Swpaul	tl_dio_setbit(sc, TL_NETCMD, TL_CMD_NRESET|TL_CMD_NWRAP);
107936270Swpaul
108036270Swpaul	/* Wait for things to settle down a little. */
108136270Swpaul	DELAY(500);
108236270Swpaul
108336270Swpaul        return;
108436270Swpaul}
108536270Swpaul
108636270Swpaul/*
108736270Swpaul * Probe for a ThunderLAN chip. Check the PCI vendor and device IDs
108839583Swpaul * against our list and return its name if we find a match.
108936270Swpaul */
1090102336Salfredstatic int
1091102336Salfredtl_probe(dev)
109248992Swpaul	device_t		dev;
109336270Swpaul{
109436270Swpaul	struct tl_type		*t;
109536270Swpaul
109636270Swpaul	t = tl_devs;
109736270Swpaul
109836270Swpaul	while(t->tl_name != NULL) {
109948992Swpaul		if ((pci_get_vendor(dev) == t->tl_vid) &&
110048992Swpaul		    (pci_get_device(dev) == t->tl_did)) {
110148992Swpaul			device_set_desc(dev, t->tl_name);
110248992Swpaul			return(0);
110348992Swpaul		}
110436270Swpaul		t++;
110536270Swpaul	}
110636270Swpaul
110748992Swpaul	return(ENXIO);
110836270Swpaul}
110936270Swpaul
1110102336Salfredstatic int
1111102336Salfredtl_attach(dev)
111248992Swpaul	device_t		dev;
111336270Swpaul{
111467087Swpaul	int			i;
111539583Swpaul	u_int16_t		did, vid;
111639583Swpaul	struct tl_type		*t;
111739583Swpaul	struct ifnet		*ifp;
111839583Swpaul	struct tl_softc		*sc;
111948992Swpaul	int			unit, error = 0, rid;
112036270Swpaul
112148992Swpaul	vid = pci_get_vendor(dev);
112248992Swpaul	did = pci_get_device(dev);
112348992Swpaul	sc = device_get_softc(dev);
112448992Swpaul	unit = device_get_unit(dev);
112539583Swpaul
112639583Swpaul	t = tl_devs;
112739583Swpaul	while(t->tl_name != NULL) {
112839583Swpaul		if (vid == t->tl_vid && did == t->tl_did)
112936270Swpaul			break;
113039583Swpaul		t++;
113139583Swpaul	}
113236270Swpaul
113339583Swpaul	if (t->tl_name == NULL) {
1134105599Sbrooks		device_printf(dev, "unknown device!?\n");
1135112878Sjhb		return (ENXIO);
113636270Swpaul	}
113736270Swpaul
113893818Sjhb	mtx_init(&sc->tl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
113993818Sjhb	    MTX_DEF | MTX_RECURSE);
114069583Swpaul
114136270Swpaul	/*
114236270Swpaul	 * Map control/status registers.
114336270Swpaul	 */
114472813Swpaul	pci_enable_busmaster(dev);
114536270Swpaul
114639583Swpaul#ifdef TL_USEIOSPACE
114739583Swpaul
114848992Swpaul	rid = TL_PCI_LOIO;
1149127135Snjl	sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
1150127135Snjl		RF_ACTIVE);
115148992Swpaul
115248992Swpaul	/*
115348992Swpaul	 * Some cards have the I/O and memory mapped address registers
115448992Swpaul	 * reversed. Try both combinations before giving up.
115548992Swpaul	 */
115648992Swpaul	if (sc->tl_res == NULL) {
115748992Swpaul		rid = TL_PCI_LOMEM;
1158127135Snjl		sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
1159127135Snjl		    RF_ACTIVE);
116045155Swpaul	}
116139583Swpaul#else
116248992Swpaul	rid = TL_PCI_LOMEM;
1163127135Snjl	sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1164127135Snjl	    RF_ACTIVE);
116548992Swpaul	if (sc->tl_res == NULL) {
116648992Swpaul		rid = TL_PCI_LOIO;
1167127135Snjl		sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1168127135Snjl		    RF_ACTIVE);
116936270Swpaul	}
117039583Swpaul#endif
117136270Swpaul
117248992Swpaul	if (sc->tl_res == NULL) {
1173105599Sbrooks		device_printf(dev, "couldn't map ports/memory\n");
117448992Swpaul		error = ENXIO;
117548992Swpaul		goto fail;
117648992Swpaul	}
117748992Swpaul
117848992Swpaul	sc->tl_btag = rman_get_bustag(sc->tl_res);
117948992Swpaul	sc->tl_bhandle = rman_get_bushandle(sc->tl_res);
118048992Swpaul
118139583Swpaul#ifdef notdef
118239583Swpaul	/*
118339583Swpaul	 * The ThunderLAN manual suggests jacking the PCI latency
118439583Swpaul	 * timer all the way up to its maximum value. I'm not sure
118539583Swpaul	 * if this is really necessary, but what the manual wants,
118639583Swpaul	 * the manual gets.
118739583Swpaul	 */
118848992Swpaul	command = pci_read_config(dev, TL_PCI_LATENCY_TIMER, 4);
118939583Swpaul	command |= 0x0000FF00;
119048992Swpaul	pci_write_config(dev, TL_PCI_LATENCY_TIMER, command, 4);
119139583Swpaul#endif
119236270Swpaul
119336270Swpaul	/* Allocate interrupt */
119448992Swpaul	rid = 0;
1195127135Snjl	sc->tl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
119648992Swpaul	    RF_SHAREABLE | RF_ACTIVE);
119748992Swpaul
119848992Swpaul	if (sc->tl_irq == NULL) {
1199105599Sbrooks		device_printf(dev, "couldn't map interrupt\n");
120048992Swpaul		error = ENXIO;
120136270Swpaul		goto fail;
120236270Swpaul	}
120336270Swpaul
120436270Swpaul	/*
120551439Swpaul	 * Now allocate memory for the TX and RX lists.
120636270Swpaul	 */
120751439Swpaul	sc->tl_ldata = contigmalloc(sizeof(struct tl_list_data), M_DEVBUF,
120851657Swpaul	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
120939583Swpaul
121051439Swpaul	if (sc->tl_ldata == NULL) {
1211105599Sbrooks		device_printf(dev, "no memory for list buffers!\n");
121248992Swpaul		error = ENXIO;
121336270Swpaul		goto fail;
121436270Swpaul	}
121536270Swpaul
121639583Swpaul	bzero(sc->tl_ldata, sizeof(struct tl_list_data));
121739583Swpaul
121839583Swpaul	sc->tl_dinfo = t;
121943235Swpaul	if (t->tl_vid == COMPAQ_VENDORID || t->tl_vid == TI_VENDORID)
122039583Swpaul		sc->tl_eeaddr = TL_EEPROM_EADDR;
122139583Swpaul	if (t->tl_vid == OLICOM_VENDORID)
122239583Swpaul		sc->tl_eeaddr = TL_EEPROM_EADDR_OC;
122339583Swpaul
122439583Swpaul	/* Reset the adapter. */
122539583Swpaul	tl_softreset(sc, 1);
122650468Swpaul	tl_hardreset(dev);
122739583Swpaul	tl_softreset(sc, 1);
122839583Swpaul
122938030Swpaul	/*
123039583Swpaul	 * Get station address from the EEPROM.
123139583Swpaul	 */
123239583Swpaul	if (tl_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
123339583Swpaul				sc->tl_eeaddr, ETHER_ADDR_LEN)) {
1234105599Sbrooks		device_printf(dev, "failed to read station address\n");
123548992Swpaul		error = ENXIO;
123639583Swpaul		goto fail;
123739583Swpaul	}
123839583Swpaul
123939583Swpaul        /*
124039583Swpaul         * XXX Olicom, in its desire to be different from the
124139583Swpaul         * rest of the world, has done strange things with the
124239583Swpaul         * encoding of the station address in the EEPROM. First
124339583Swpaul         * of all, they store the address at offset 0xF8 rather
124439583Swpaul         * than at 0x83 like the ThunderLAN manual suggests.
124539583Swpaul         * Second, they store the address in three 16-bit words in
124639583Swpaul         * network byte order, as opposed to storing it sequentially
124739583Swpaul         * like all the other ThunderLAN cards. In order to get
124839583Swpaul         * the station address in a form that matches what the Olicom
124939583Swpaul         * diagnostic utility specifies, we have to byte-swap each
125039583Swpaul         * word. To make things even more confusing, neither 00:00:28
125139583Swpaul         * nor 00:00:24 appear in the IEEE OUI database.
125239583Swpaul         */
125339583Swpaul        if (sc->tl_dinfo->tl_vid == OLICOM_VENDORID) {
125439583Swpaul                for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
125539583Swpaul                        u_int16_t               *p;
125639583Swpaul                        p = (u_int16_t *)&sc->arpcom.ac_enaddr[i];
125739583Swpaul                        *p = ntohs(*p);
125839583Swpaul                }
125939583Swpaul        }
126039583Swpaul
126139583Swpaul	ifp = &sc->arpcom.ac_if;
126239583Swpaul	ifp->if_softc = sc;
1263121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
126439583Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
126539583Swpaul	ifp->if_ioctl = tl_ioctl;
126639583Swpaul	ifp->if_start = tl_start;
126739583Swpaul	ifp->if_watchdog = tl_watchdog;
126839583Swpaul	ifp->if_init = tl_init;
126939583Swpaul	ifp->if_mtu = ETHERMTU;
127051439Swpaul	ifp->if_snd.ifq_maxlen = TL_TX_LIST_CNT - 1;
127139583Swpaul	callout_handle_init(&sc->tl_stat_ch);
127239583Swpaul
127339583Swpaul	/* Reset the adapter again. */
127439583Swpaul	tl_softreset(sc, 1);
127550468Swpaul	tl_hardreset(dev);
127639583Swpaul	tl_softreset(sc, 1);
127739583Swpaul
127836270Swpaul	/*
127950462Swpaul	 * Do MII setup. If no PHYs are found, then this is a
128050462Swpaul	 * bitrate ThunderLAN chip that only supports 10baseT
128150462Swpaul	 * and AUI/BNC.
128236270Swpaul	 */
128350462Swpaul	if (mii_phy_probe(dev, &sc->tl_miibus,
128450462Swpaul	    tl_ifmedia_upd, tl_ifmedia_sts)) {
128545155Swpaul		struct ifmedia		*ifm;
128645155Swpaul		sc->tl_bitrate = 1;
128745155Swpaul		ifmedia_init(&sc->ifmedia, 0, tl_ifmedia_upd, tl_ifmedia_sts);
128845155Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
128945155Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
129045155Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
129145155Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
129245166Swpaul		ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_10_T);
129345155Swpaul		/* Reset again, this time setting bitrate mode. */
129445155Swpaul		tl_softreset(sc, 1);
129545155Swpaul		ifm = &sc->ifmedia;
129645155Swpaul		ifm->ifm_media = ifm->ifm_cur->ifm_media;
129745155Swpaul		tl_ifmedia_upd(ifp);
129836270Swpaul	}
129936270Swpaul
130039583Swpaul	/*
130163090Sarchie	 * Call MI attach routine.
130239583Swpaul	 */
1303106936Ssam	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
130438030Swpaul
1305113609Snjl	/* Hook interrupt last to avoid having to lock softc */
1306112872Snjl	error = bus_setup_intr(dev, sc->tl_irq, INTR_TYPE_NET,
1307112872Snjl	    tl_intr, sc, &sc->tl_intrhand);
1308112872Snjl
1309112872Snjl	if (error) {
1310112872Snjl		device_printf(dev, "couldn't set up irq\n");
1311113609Snjl		ether_ifdetach(ifp);
1312112872Snjl		goto fail;
1313112872Snjl	}
1314112872Snjl
131536270Swpaulfail:
1316112872Snjl	if (error)
1317112872Snjl		tl_detach(dev);
1318112872Snjl
131948992Swpaul	return(error);
132036270Swpaul}
132136270Swpaul
1322113609Snjl/*
1323113609Snjl * Shutdown hardware and free up resources. This can be called any
1324113609Snjl * time after the mutex has been initialized. It is called in both
1325113609Snjl * the error case in attach and the normal detach case so it needs
1326113609Snjl * to be careful about only freeing resources that have actually been
1327113609Snjl * allocated.
1328113609Snjl */
1329102336Salfredstatic int
1330102336Salfredtl_detach(dev)
133148992Swpaul	device_t		dev;
133248992Swpaul{
133348992Swpaul	struct tl_softc		*sc;
133448992Swpaul	struct ifnet		*ifp;
133548992Swpaul
133648992Swpaul	sc = device_get_softc(dev);
1337112880Sjhb	KASSERT(mtx_initialized(&sc->tl_mtx), ("tl mutex not initialized"));
133867087Swpaul	TL_LOCK(sc);
133948992Swpaul	ifp = &sc->arpcom.ac_if;
134048992Swpaul
1341113609Snjl	/* These should only be active if attach succeeded */
1342113812Simp	if (device_is_attached(dev)) {
1343113609Snjl		tl_stop(sc);
1344112872Snjl		ether_ifdetach(ifp);
1345113609Snjl	}
1346113609Snjl	if (sc->tl_miibus)
1347112872Snjl		device_delete_child(dev, sc->tl_miibus);
1348113609Snjl	bus_generic_detach(dev);
134948992Swpaul
1350112872Snjl	if (sc->tl_ldata)
1351112872Snjl		contigfree(sc->tl_ldata, sizeof(struct tl_list_data), M_DEVBUF);
135250462Swpaul	if (sc->tl_bitrate)
135350462Swpaul		ifmedia_removeall(&sc->ifmedia);
135448992Swpaul
1355112872Snjl	if (sc->tl_intrhand)
1356112872Snjl		bus_teardown_intr(dev, sc->tl_irq, sc->tl_intrhand);
1357112872Snjl	if (sc->tl_irq)
1358112872Snjl		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->tl_irq);
1359112872Snjl	if (sc->tl_res)
1360112872Snjl		bus_release_resource(dev, TL_RES, TL_RID, sc->tl_res);
136148992Swpaul
136267087Swpaul	TL_UNLOCK(sc);
136367087Swpaul	mtx_destroy(&sc->tl_mtx);
136448992Swpaul
136548992Swpaul	return(0);
136648992Swpaul}
136748992Swpaul
136836270Swpaul/*
136936270Swpaul * Initialize the transmit lists.
137036270Swpaul */
1371102336Salfredstatic int
1372102336Salfredtl_list_tx_init(sc)
137336270Swpaul	struct tl_softc		*sc;
137436270Swpaul{
137536270Swpaul	struct tl_chain_data	*cd;
137636270Swpaul	struct tl_list_data	*ld;
137736270Swpaul	int			i;
137836270Swpaul
137936270Swpaul	cd = &sc->tl_cdata;
138036270Swpaul	ld = sc->tl_ldata;
138136270Swpaul	for (i = 0; i < TL_TX_LIST_CNT; i++) {
138236270Swpaul		cd->tl_tx_chain[i].tl_ptr = &ld->tl_tx_list[i];
138336270Swpaul		if (i == (TL_TX_LIST_CNT - 1))
138436270Swpaul			cd->tl_tx_chain[i].tl_next = NULL;
138536270Swpaul		else
138636270Swpaul			cd->tl_tx_chain[i].tl_next = &cd->tl_tx_chain[i + 1];
138736270Swpaul	}
138836270Swpaul
138936270Swpaul	cd->tl_tx_free = &cd->tl_tx_chain[0];
139036270Swpaul	cd->tl_tx_tail = cd->tl_tx_head = NULL;
139136270Swpaul	sc->tl_txeoc = 1;
139236270Swpaul
139336270Swpaul	return(0);
139436270Swpaul}
139536270Swpaul
139636270Swpaul/*
139736270Swpaul * Initialize the RX lists and allocate mbufs for them.
139836270Swpaul */
1399102336Salfredstatic int
1400102336Salfredtl_list_rx_init(sc)
140136270Swpaul	struct tl_softc		*sc;
140236270Swpaul{
140336270Swpaul	struct tl_chain_data	*cd;
140436270Swpaul	struct tl_list_data	*ld;
140536270Swpaul	int			i;
140636270Swpaul
140736270Swpaul	cd = &sc->tl_cdata;
140836270Swpaul	ld = sc->tl_ldata;
140936270Swpaul
141040795Swpaul	for (i = 0; i < TL_RX_LIST_CNT; i++) {
141136270Swpaul		cd->tl_rx_chain[i].tl_ptr =
141237626Swpaul			(struct tl_list_onefrag *)&ld->tl_rx_list[i];
141339583Swpaul		if (tl_newbuf(sc, &cd->tl_rx_chain[i]) == ENOBUFS)
141439583Swpaul			return(ENOBUFS);
141540795Swpaul		if (i == (TL_RX_LIST_CNT - 1)) {
141636270Swpaul			cd->tl_rx_chain[i].tl_next = NULL;
141736270Swpaul			ld->tl_rx_list[i].tlist_fptr = 0;
141836270Swpaul		} else {
141936270Swpaul			cd->tl_rx_chain[i].tl_next = &cd->tl_rx_chain[i + 1];
142036270Swpaul			ld->tl_rx_list[i].tlist_fptr =
142136270Swpaul					vtophys(&ld->tl_rx_list[i + 1]);
142236270Swpaul		}
142336270Swpaul	}
142436270Swpaul
142536270Swpaul	cd->tl_rx_head = &cd->tl_rx_chain[0];
142636270Swpaul	cd->tl_rx_tail = &cd->tl_rx_chain[TL_RX_LIST_CNT - 1];
142736270Swpaul
142836270Swpaul	return(0);
142936270Swpaul}
143036270Swpaul
1431102336Salfredstatic int
1432102336Salfredtl_newbuf(sc, c)
143336270Swpaul	struct tl_softc		*sc;
143437626Swpaul	struct tl_chain_onefrag	*c;
143536270Swpaul{
143636270Swpaul	struct mbuf		*m_new = NULL;
143736270Swpaul
1438111119Simp	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
143987846Sluigi	if (m_new == NULL)
144036270Swpaul		return(ENOBUFS);
144136270Swpaul
1442111119Simp	MCLGET(m_new, M_DONTWAIT);
144336270Swpaul	if (!(m_new->m_flags & M_EXT)) {
144436270Swpaul		m_freem(m_new);
144536270Swpaul		return(ENOBUFS);
144636270Swpaul	}
144736270Swpaul
144845155Swpaul#ifdef __alpha__
144945155Swpaul	m_new->m_data += 2;
145045155Swpaul#endif
145145155Swpaul
145236270Swpaul	c->tl_mbuf = m_new;
145336270Swpaul	c->tl_next = NULL;
145436270Swpaul	c->tl_ptr->tlist_frsize = MCLBYTES;
145536270Swpaul	c->tl_ptr->tlist_fptr = 0;
145637626Swpaul	c->tl_ptr->tl_frag.tlist_dadr = vtophys(mtod(m_new, caddr_t));
145737626Swpaul	c->tl_ptr->tl_frag.tlist_dcnt = MCLBYTES;
145856060Swpaul	c->tl_ptr->tlist_cstat = TL_CSTAT_READY;
145936270Swpaul
146036270Swpaul	return(0);
146136270Swpaul}
146236270Swpaul/*
146336270Swpaul * Interrupt handler for RX 'end of frame' condition (EOF). This
146436270Swpaul * tells us that a full ethernet frame has been captured and we need
146536270Swpaul * to handle it.
146636270Swpaul *
146736270Swpaul * Reception is done using 'lists' which consist of a header and a
146836270Swpaul * series of 10 data count/data address pairs that point to buffers.
146936270Swpaul * Initially you're supposed to create a list, populate it with pointers
147036270Swpaul * to buffers, then load the physical address of the list into the
147136270Swpaul * ch_parm register. The adapter is then supposed to DMA the received
147236270Swpaul * frame into the buffers for you.
147336270Swpaul *
147436270Swpaul * To make things as fast as possible, we have the chip DMA directly
147536270Swpaul * into mbufs. This saves us from having to do a buffer copy: we can
147636270Swpaul * just hand the mbufs directly to ether_input(). Once the frame has
147736270Swpaul * been sent on its way, the 'list' structure is assigned a new buffer
147836270Swpaul * and moved to the end of the RX chain. As long we we stay ahead of
147936270Swpaul * the chip, it will always think it has an endless receive channel.
148036270Swpaul *
148136270Swpaul * If we happen to fall behind and the chip manages to fill up all of
148236270Swpaul * the buffers, it will generate an end of channel interrupt and wait
148336270Swpaul * for us to empty the chain and restart the receiver.
148436270Swpaul */
1485102336Salfredstatic int
1486102336Salfredtl_intvec_rxeof(xsc, type)
148736270Swpaul	void			*xsc;
148836270Swpaul	u_int32_t		type;
148936270Swpaul{
149036270Swpaul	struct tl_softc		*sc;
149136270Swpaul	int			r = 0, total_len = 0;
149236270Swpaul	struct ether_header	*eh;
149336270Swpaul	struct mbuf		*m;
149436270Swpaul	struct ifnet		*ifp;
149537626Swpaul	struct tl_chain_onefrag	*cur_rx;
149636270Swpaul
149736270Swpaul	sc = xsc;
149836270Swpaul	ifp = &sc->arpcom.ac_if;
149936270Swpaul
1500122689Ssam	TL_LOCK_ASSERT(sc);
1501122689Ssam
150256060Swpaul	while(sc->tl_cdata.tl_rx_head != NULL) {
150356060Swpaul		cur_rx = sc->tl_cdata.tl_rx_head;
150456060Swpaul		if (!(cur_rx->tl_ptr->tlist_cstat & TL_CSTAT_FRAMECMP))
150556060Swpaul			break;
150636270Swpaul		r++;
150736270Swpaul		sc->tl_cdata.tl_rx_head = cur_rx->tl_next;
150836270Swpaul		m = cur_rx->tl_mbuf;
150936270Swpaul		total_len = cur_rx->tl_ptr->tlist_frsize;
151036270Swpaul
151139583Swpaul		if (tl_newbuf(sc, cur_rx) == ENOBUFS) {
151239583Swpaul			ifp->if_ierrors++;
151339583Swpaul			cur_rx->tl_ptr->tlist_frsize = MCLBYTES;
151439583Swpaul			cur_rx->tl_ptr->tlist_cstat = TL_CSTAT_READY;
151539583Swpaul			cur_rx->tl_ptr->tl_frag.tlist_dcnt = MCLBYTES;
151639583Swpaul			continue;
151739583Swpaul		}
151836270Swpaul
151936270Swpaul		sc->tl_cdata.tl_rx_tail->tl_ptr->tlist_fptr =
152036270Swpaul						vtophys(cur_rx->tl_ptr);
152136270Swpaul		sc->tl_cdata.tl_rx_tail->tl_next = cur_rx;
152236270Swpaul		sc->tl_cdata.tl_rx_tail = cur_rx;
152336270Swpaul
152437626Swpaul		/*
152537626Swpaul		 * Note: when the ThunderLAN chip is in 'capture all
152637626Swpaul		 * frames' mode, it will receive its own transmissions.
152737626Swpaul		 * We drop don't need to process our own transmissions,
152837626Swpaul		 * so we drop them here and continue.
152937626Swpaul		 */
1530106936Ssam		eh = mtod(m, struct ether_header *);
153139583Swpaul		/*if (ifp->if_flags & IFF_PROMISC && */
153239583Swpaul		if (!bcmp(eh->ether_shost, sc->arpcom.ac_enaddr,
153337626Swpaul		 					ETHER_ADDR_LEN)) {
153437626Swpaul				m_freem(m);
153537626Swpaul				continue;
153637626Swpaul		}
153737626Swpaul
1538106936Ssam		m->m_pkthdr.rcvif = ifp;
1539106936Ssam		m->m_pkthdr.len = m->m_len = total_len;
1540106936Ssam
1541122689Ssam		TL_UNLOCK(sc);
1542106936Ssam		(*ifp->if_input)(ifp, m);
1543122689Ssam		TL_LOCK(sc);
154436270Swpaul	}
154536270Swpaul
154636270Swpaul	return(r);
154736270Swpaul}
154836270Swpaul
154936270Swpaul/*
155036270Swpaul * The RX-EOC condition hits when the ch_parm address hasn't been
155136270Swpaul * initialized or the adapter reached a list with a forward pointer
155236270Swpaul * of 0 (which indicates the end of the chain). In our case, this means
155336270Swpaul * the card has hit the end of the receive buffer chain and we need to
155436270Swpaul * empty out the buffers and shift the pointer back to the beginning again.
155536270Swpaul */
1556102336Salfredstatic int
1557102336Salfredtl_intvec_rxeoc(xsc, type)
155836270Swpaul	void			*xsc;
155936270Swpaul	u_int32_t		type;
156036270Swpaul{
156136270Swpaul	struct tl_softc		*sc;
156236270Swpaul	int			r;
156356060Swpaul	struct tl_chain_data	*cd;
156436270Swpaul
156556060Swpaul
156636270Swpaul	sc = xsc;
156756060Swpaul	cd = &sc->tl_cdata;
156836270Swpaul
156936270Swpaul	/* Flush out the receive queue and ack RXEOF interrupts. */
157036270Swpaul	r = tl_intvec_rxeof(xsc, type);
157139583Swpaul	CMD_PUT(sc, TL_CMD_ACK | r | (type & ~(0x00100000)));
157236270Swpaul	r = 1;
157356060Swpaul	cd->tl_rx_head = &cd->tl_rx_chain[0];
157456060Swpaul	cd->tl_rx_tail = &cd->tl_rx_chain[TL_RX_LIST_CNT - 1];
157539583Swpaul	CSR_WRITE_4(sc, TL_CH_PARM, vtophys(sc->tl_cdata.tl_rx_head->tl_ptr));
157636270Swpaul	r |= (TL_CMD_GO|TL_CMD_RT);
157736270Swpaul	return(r);
157836270Swpaul}
157936270Swpaul
1580102336Salfredstatic int
1581102336Salfredtl_intvec_txeof(xsc, type)
158236270Swpaul	void			*xsc;
158336270Swpaul	u_int32_t		type;
158436270Swpaul{
158536270Swpaul	struct tl_softc		*sc;
158636270Swpaul	int			r = 0;
158736270Swpaul	struct tl_chain		*cur_tx;
158836270Swpaul
158936270Swpaul	sc = xsc;
159036270Swpaul
159136270Swpaul	/*
159236270Swpaul	 * Go through our tx list and free mbufs for those
159336270Swpaul	 * frames that have been sent.
159436270Swpaul	 */
159536270Swpaul	while (sc->tl_cdata.tl_tx_head != NULL) {
159636270Swpaul		cur_tx = sc->tl_cdata.tl_tx_head;
159736270Swpaul		if (!(cur_tx->tl_ptr->tlist_cstat & TL_CSTAT_FRAMECMP))
159836270Swpaul			break;
159936270Swpaul		sc->tl_cdata.tl_tx_head = cur_tx->tl_next;
160036270Swpaul
160136270Swpaul		r++;
160236270Swpaul		m_freem(cur_tx->tl_mbuf);
160336270Swpaul		cur_tx->tl_mbuf = NULL;
160436270Swpaul
160536270Swpaul		cur_tx->tl_next = sc->tl_cdata.tl_tx_free;
160636270Swpaul		sc->tl_cdata.tl_tx_free = cur_tx;
160737626Swpaul		if (!cur_tx->tl_ptr->tlist_fptr)
160837626Swpaul			break;
160936270Swpaul	}
161036270Swpaul
161136270Swpaul	return(r);
161236270Swpaul}
161336270Swpaul
161436270Swpaul/*
161536270Swpaul * The transmit end of channel interrupt. The adapter triggers this
161636270Swpaul * interrupt to tell us it hit the end of the current transmit list.
161736270Swpaul *
161836270Swpaul * A note about this: it's possible for a condition to arise where
161936270Swpaul * tl_start() may try to send frames between TXEOF and TXEOC interrupts.
162036270Swpaul * You have to avoid this since the chip expects things to go in a
162136270Swpaul * particular order: transmit, acknowledge TXEOF, acknowledge TXEOC.
162236270Swpaul * When the TXEOF handler is called, it will free all of the transmitted
162336270Swpaul * frames and reset the tx_head pointer to NULL. However, a TXEOC
162436270Swpaul * interrupt should be received and acknowledged before any more frames
162536270Swpaul * are queued for transmission. If tl_statrt() is called after TXEOF
162636270Swpaul * resets the tx_head pointer but _before_ the TXEOC interrupt arrives,
162736270Swpaul * it could attempt to issue a transmit command prematurely.
162836270Swpaul *
162936270Swpaul * To guard against this, tl_start() will only issue transmit commands
163036270Swpaul * if the tl_txeoc flag is set, and only the TXEOC interrupt handler
163136270Swpaul * can set this flag once tl_start() has cleared it.
163236270Swpaul */
1633102336Salfredstatic int
1634102336Salfredtl_intvec_txeoc(xsc, type)
163536270Swpaul	void			*xsc;
163636270Swpaul	u_int32_t		type;
163736270Swpaul{
163836270Swpaul	struct tl_softc		*sc;
163936270Swpaul	struct ifnet		*ifp;
164036270Swpaul	u_int32_t		cmd;
164136270Swpaul
164236270Swpaul	sc = xsc;
164336270Swpaul	ifp = &sc->arpcom.ac_if;
164436270Swpaul
164536270Swpaul	/* Clear the timeout timer. */
164636270Swpaul	ifp->if_timer = 0;
164736270Swpaul
164836270Swpaul	if (sc->tl_cdata.tl_tx_head == NULL) {
164936270Swpaul		ifp->if_flags &= ~IFF_OACTIVE;
165036270Swpaul		sc->tl_cdata.tl_tx_tail = NULL;
165136270Swpaul		sc->tl_txeoc = 1;
165236270Swpaul	} else {
165336270Swpaul		sc->tl_txeoc = 0;
165436270Swpaul		/* First we have to ack the EOC interrupt. */
165539583Swpaul		CMD_PUT(sc, TL_CMD_ACK | 0x00000001 | type);
165636270Swpaul		/* Then load the address of the next TX list. */
165739583Swpaul		CSR_WRITE_4(sc, TL_CH_PARM,
165851439Swpaul		    vtophys(sc->tl_cdata.tl_tx_head->tl_ptr));
165936270Swpaul		/* Restart TX channel. */
166039583Swpaul		cmd = CSR_READ_4(sc, TL_HOSTCMD);
166136270Swpaul		cmd &= ~TL_CMD_RT;
166236270Swpaul		cmd |= TL_CMD_GO|TL_CMD_INTSON;
166339583Swpaul		CMD_PUT(sc, cmd);
166436270Swpaul		return(0);
166536270Swpaul	}
166636270Swpaul
166736270Swpaul	return(1);
166836270Swpaul}
166936270Swpaul
1670102336Salfredstatic int
1671102336Salfredtl_intvec_adchk(xsc, type)
167236270Swpaul	void			*xsc;
167336270Swpaul	u_int32_t		type;
167436270Swpaul{
167536270Swpaul	struct tl_softc		*sc;
167636270Swpaul
167736270Swpaul	sc = xsc;
167836270Swpaul
167939627Swpaul	if (type)
1680105599Sbrooks		if_printf(&sc->arpcom.ac_if, "adapter check: %x\n",
168141656Swpaul			(unsigned int)CSR_READ_4(sc, TL_CH_PARM));
168236270Swpaul
168339583Swpaul	tl_softreset(sc, 1);
168437626Swpaul	tl_stop(sc);
168536270Swpaul	tl_init(sc);
168639583Swpaul	CMD_SET(sc, TL_CMD_INTSON);
168736270Swpaul
168836270Swpaul	return(0);
168936270Swpaul}
169036270Swpaul
1691102336Salfredstatic int
1692102336Salfredtl_intvec_netsts(xsc, type)
169336270Swpaul	void			*xsc;
169436270Swpaul	u_int32_t		type;
169536270Swpaul{
169636270Swpaul	struct tl_softc		*sc;
169736270Swpaul	u_int16_t		netsts;
169836270Swpaul
169936270Swpaul	sc = xsc;
170036270Swpaul
170139583Swpaul	netsts = tl_dio_read16(sc, TL_NETSTS);
170239583Swpaul	tl_dio_write16(sc, TL_NETSTS, netsts);
170336270Swpaul
1704105599Sbrooks	if_printf(&sc->arpcom.ac_if, "network status: %x\n", netsts);
170536270Swpaul
170636270Swpaul	return(1);
170736270Swpaul}
170836270Swpaul
1709102336Salfredstatic void
1710102336Salfredtl_intr(xsc)
171139583Swpaul	void			*xsc;
171236270Swpaul{
171336270Swpaul	struct tl_softc		*sc;
171436270Swpaul	struct ifnet		*ifp;
171536270Swpaul	int			r = 0;
171636270Swpaul	u_int32_t		type = 0;
171736270Swpaul	u_int16_t		ints = 0;
171836270Swpaul	u_int8_t		ivec = 0;
171936270Swpaul
172039583Swpaul	sc = xsc;
172167087Swpaul	TL_LOCK(sc);
172236270Swpaul
172336270Swpaul	/* Disable interrupts */
172439583Swpaul	ints = CSR_READ_2(sc, TL_HOST_INT);
172539583Swpaul	CSR_WRITE_2(sc, TL_HOST_INT, ints);
172636270Swpaul	type = (ints << 16) & 0xFFFF0000;
172736270Swpaul	ivec = (ints & TL_VEC_MASK) >> 5;
172836270Swpaul	ints = (ints & TL_INT_MASK) >> 2;
172936270Swpaul
173036270Swpaul	ifp = &sc->arpcom.ac_if;
173136270Swpaul
173236270Swpaul	switch(ints) {
173336270Swpaul	case (TL_INTR_INVALID):
173439583Swpaul#ifdef DIAGNOSTIC
1735105599Sbrooks		if_printf(ifp, "got an invalid interrupt!\n");
173639583Swpaul#endif
173739583Swpaul		/* Re-enable interrupts but don't ack this one. */
173839583Swpaul		CMD_PUT(sc, type);
173939583Swpaul		r = 0;
174036270Swpaul		break;
174136270Swpaul	case (TL_INTR_TXEOF):
174236270Swpaul		r = tl_intvec_txeof((void *)sc, type);
174336270Swpaul		break;
174436270Swpaul	case (TL_INTR_TXEOC):
174536270Swpaul		r = tl_intvec_txeoc((void *)sc, type);
174636270Swpaul		break;
174736270Swpaul	case (TL_INTR_STATOFLOW):
174839583Swpaul		tl_stats_update(sc);
174939583Swpaul		r = 1;
175036270Swpaul		break;
175136270Swpaul	case (TL_INTR_RXEOF):
175236270Swpaul		r = tl_intvec_rxeof((void *)sc, type);
175336270Swpaul		break;
175436270Swpaul	case (TL_INTR_DUMMY):
1755105599Sbrooks		if_printf(ifp, "got a dummy interrupt\n");
175639583Swpaul		r = 1;
175736270Swpaul		break;
175836270Swpaul	case (TL_INTR_ADCHK):
175936270Swpaul		if (ivec)
176036270Swpaul			r = tl_intvec_adchk((void *)sc, type);
176136270Swpaul		else
176236270Swpaul			r = tl_intvec_netsts((void *)sc, type);
176336270Swpaul		break;
176436270Swpaul	case (TL_INTR_RXEOC):
176536270Swpaul		r = tl_intvec_rxeoc((void *)sc, type);
176636270Swpaul		break;
176736270Swpaul	default:
1768105599Sbrooks		if_printf(ifp, "bogus interrupt type\n");
176936270Swpaul		break;
177036270Swpaul	}
177136270Swpaul
177236270Swpaul	/* Re-enable interrupts */
177337626Swpaul	if (r) {
177439583Swpaul		CMD_PUT(sc, TL_CMD_ACK | r | type);
177537626Swpaul	}
177636270Swpaul
177737626Swpaul	if (ifp->if_snd.ifq_head != NULL)
177837626Swpaul		tl_start(ifp);
177937626Swpaul
178067087Swpaul	TL_UNLOCK(sc);
178167087Swpaul
178236270Swpaul	return;
178336270Swpaul}
178436270Swpaul
1785102336Salfredstatic void
1786102336Salfredtl_stats_update(xsc)
178736270Swpaul	void			*xsc;
178836270Swpaul{
178936270Swpaul	struct tl_softc		*sc;
179036270Swpaul	struct ifnet		*ifp;
179136270Swpaul	struct tl_stats		tl_stats;
179250462Swpaul	struct mii_data		*mii;
179336270Swpaul	u_int32_t		*p;
179436270Swpaul
179536270Swpaul	bzero((char *)&tl_stats, sizeof(struct tl_stats));
179636270Swpaul
179736270Swpaul	sc = xsc;
179867087Swpaul	TL_LOCK(sc);
179936270Swpaul	ifp = &sc->arpcom.ac_if;
180036270Swpaul
180136270Swpaul	p = (u_int32_t *)&tl_stats;
180236270Swpaul
180339583Swpaul	CSR_WRITE_2(sc, TL_DIO_ADDR, TL_TXGOODFRAMES|TL_DIO_ADDR_INC);
180439583Swpaul	*p++ = CSR_READ_4(sc, TL_DIO_DATA);
180539583Swpaul	*p++ = CSR_READ_4(sc, TL_DIO_DATA);
180639583Swpaul	*p++ = CSR_READ_4(sc, TL_DIO_DATA);
180739583Swpaul	*p++ = CSR_READ_4(sc, TL_DIO_DATA);
180839583Swpaul	*p++ = CSR_READ_4(sc, TL_DIO_DATA);
180936270Swpaul
181036270Swpaul	ifp->if_opackets += tl_tx_goodframes(tl_stats);
181136270Swpaul	ifp->if_collisions += tl_stats.tl_tx_single_collision +
181236270Swpaul				tl_stats.tl_tx_multi_collision;
181336270Swpaul	ifp->if_ipackets += tl_rx_goodframes(tl_stats);
181436270Swpaul	ifp->if_ierrors += tl_stats.tl_crc_errors + tl_stats.tl_code_errors +
181536270Swpaul			    tl_rx_overrun(tl_stats);
181636270Swpaul	ifp->if_oerrors += tl_tx_underrun(tl_stats);
181736270Swpaul
181851439Swpaul	if (tl_tx_underrun(tl_stats)) {
181951439Swpaul		u_int8_t		tx_thresh;
182051439Swpaul		tx_thresh = tl_dio_read8(sc, TL_ACOMMIT) & TL_AC_TXTHRESH;
182151439Swpaul		if (tx_thresh != TL_AC_TXTHRESH_WHOLEPKT) {
182251439Swpaul			tx_thresh >>= 4;
182351439Swpaul			tx_thresh++;
1824105599Sbrooks			if_printf(ifp, "tx underrun -- increasing "
1825105599Sbrooks			    "tx threshold to %d bytes\n",
182651439Swpaul			    (64 * (tx_thresh * 4)));
182751439Swpaul			tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_TXTHRESH);
182851439Swpaul			tl_dio_setbit(sc, TL_ACOMMIT, tx_thresh << 4);
182951439Swpaul		}
183051439Swpaul	}
183151439Swpaul
183236270Swpaul	sc->tl_stat_ch = timeout(tl_stats_update, sc, hz);
183336302Swpaul
183450462Swpaul	if (!sc->tl_bitrate) {
183550462Swpaul		mii = device_get_softc(sc->tl_miibus);
183650462Swpaul		mii_tick(mii);
183750462Swpaul	}
183850462Swpaul
183967087Swpaul	TL_UNLOCK(sc);
184048992Swpaul
184136302Swpaul	return;
184236270Swpaul}
184336270Swpaul
184436270Swpaul/*
184536270Swpaul * Encapsulate an mbuf chain in a list by coupling the mbuf data
184636270Swpaul * pointers to the fragment pointers.
184736270Swpaul */
1848102336Salfredstatic int
1849102336Salfredtl_encap(sc, c, m_head)
185036270Swpaul	struct tl_softc		*sc;
185136270Swpaul	struct tl_chain		*c;
185236270Swpaul	struct mbuf		*m_head;
185336270Swpaul{
185436270Swpaul	int			frag = 0;
185536270Swpaul	struct tl_frag		*f = NULL;
185636270Swpaul	int			total_len;
185736270Swpaul	struct mbuf		*m;
1858105599Sbrooks	struct ifnet		*ifp = &sc->arpcom.ac_if;
185936270Swpaul
186036270Swpaul	/*
186136270Swpaul 	 * Start packing the mbufs in this chain into
186236270Swpaul	 * the fragment pointers. Stop when we run out
186336270Swpaul 	 * of fragments or hit the end of the mbuf chain.
186436270Swpaul	 */
186536270Swpaul	m = m_head;
186636270Swpaul	total_len = 0;
186736270Swpaul
186836270Swpaul	for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
186936270Swpaul		if (m->m_len != 0) {
187036270Swpaul			if (frag == TL_MAXFRAGS)
187136270Swpaul				break;
187236270Swpaul			total_len+= m->m_len;
187336270Swpaul			c->tl_ptr->tl_frag[frag].tlist_dadr =
187436270Swpaul				vtophys(mtod(m, vm_offset_t));
187536270Swpaul			c->tl_ptr->tl_frag[frag].tlist_dcnt = m->m_len;
187636270Swpaul			frag++;
187736270Swpaul		}
187836270Swpaul	}
187936270Swpaul
188036270Swpaul	/*
188136270Swpaul	 * Handle special cases.
188236270Swpaul	 * Special case #1: we used up all 10 fragments, but
188336270Swpaul	 * we have more mbufs left in the chain. Copy the
188436270Swpaul	 * data into an mbuf cluster. Note that we don't
188536270Swpaul	 * bother clearing the values in the other fragment
188636270Swpaul	 * pointers/counters; it wouldn't gain us anything,
188736270Swpaul	 * and would waste cycles.
188836270Swpaul	 */
188936270Swpaul	if (m != NULL) {
189036270Swpaul		struct mbuf		*m_new = NULL;
189136270Swpaul
1892111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
189336270Swpaul		if (m_new == NULL) {
1894105599Sbrooks			if_printf(ifp, "no memory for tx list\n");
189536270Swpaul			return(1);
189636270Swpaul		}
189736270Swpaul		if (m_head->m_pkthdr.len > MHLEN) {
1898111119Simp			MCLGET(m_new, M_DONTWAIT);
189936270Swpaul			if (!(m_new->m_flags & M_EXT)) {
190036270Swpaul				m_freem(m_new);
1901105599Sbrooks				if_printf(ifp, "no memory for tx list\n");
190236270Swpaul				return(1);
190336270Swpaul			}
190436270Swpaul		}
190536270Swpaul		m_copydata(m_head, 0, m_head->m_pkthdr.len,
190636270Swpaul					mtod(m_new, caddr_t));
190736270Swpaul		m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
190836270Swpaul		m_freem(m_head);
190936270Swpaul		m_head = m_new;
191036270Swpaul		f = &c->tl_ptr->tl_frag[0];
191136270Swpaul		f->tlist_dadr = vtophys(mtod(m_new, caddr_t));
191236270Swpaul		f->tlist_dcnt = total_len = m_new->m_len;
191336270Swpaul		frag = 1;
191436270Swpaul	}
191536270Swpaul
191636270Swpaul	/*
191736270Swpaul	 * Special case #2: the frame is smaller than the minimum
191836270Swpaul	 * frame size. We have to pad it to make the chip happy.
191936270Swpaul	 */
192036270Swpaul	if (total_len < TL_MIN_FRAMELEN) {
192136270Swpaul		if (frag == TL_MAXFRAGS)
1922105599Sbrooks			if_printf(ifp,
1923105599Sbrooks			    "all frags filled but frame still to small!\n");
192436270Swpaul		f = &c->tl_ptr->tl_frag[frag];
192536270Swpaul		f->tlist_dcnt = TL_MIN_FRAMELEN - total_len;
192636270Swpaul		f->tlist_dadr = vtophys(&sc->tl_ldata->tl_pad);
192736270Swpaul		total_len += f->tlist_dcnt;
192836270Swpaul		frag++;
192936270Swpaul	}
193036270Swpaul
193136270Swpaul	c->tl_mbuf = m_head;
193236270Swpaul	c->tl_ptr->tl_frag[frag - 1].tlist_dcnt |= TL_LAST_FRAG;
193336270Swpaul	c->tl_ptr->tlist_frsize = total_len;
193436270Swpaul	c->tl_ptr->tlist_cstat = TL_CSTAT_READY;
193536270Swpaul	c->tl_ptr->tlist_fptr = 0;
193636270Swpaul
193736270Swpaul	return(0);
193836270Swpaul}
193936270Swpaul
194036270Swpaul/*
194136270Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
194236270Swpaul * to the mbuf data regions directly in the transmit lists. We also save a
194336270Swpaul * copy of the pointers since the transmit list fragment pointers are
194436270Swpaul * physical addresses.
194536270Swpaul */
1946102336Salfredstatic void
1947102336Salfredtl_start(ifp)
194836270Swpaul	struct ifnet		*ifp;
194936270Swpaul{
195036270Swpaul	struct tl_softc		*sc;
195136270Swpaul	struct mbuf		*m_head = NULL;
195236270Swpaul	u_int32_t		cmd;
195336270Swpaul	struct tl_chain		*prev = NULL, *cur_tx = NULL, *start_tx;
195436270Swpaul
195536270Swpaul	sc = ifp->if_softc;
195667087Swpaul	TL_LOCK(sc);
195736270Swpaul
195836270Swpaul	/*
195936270Swpaul	 * Check for an available queue slot. If there are none,
196036270Swpaul	 * punt.
196136270Swpaul	 */
196236270Swpaul	if (sc->tl_cdata.tl_tx_free == NULL) {
196336270Swpaul		ifp->if_flags |= IFF_OACTIVE;
196467087Swpaul		TL_UNLOCK(sc);
196536270Swpaul		return;
196636270Swpaul	}
196736270Swpaul
196836270Swpaul	start_tx = sc->tl_cdata.tl_tx_free;
196936270Swpaul
197036270Swpaul	while(sc->tl_cdata.tl_tx_free != NULL) {
197136270Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
197236270Swpaul		if (m_head == NULL)
197336270Swpaul			break;
197436270Swpaul
197536270Swpaul		/* Pick a chain member off the free list. */
197636270Swpaul		cur_tx = sc->tl_cdata.tl_tx_free;
197736270Swpaul		sc->tl_cdata.tl_tx_free = cur_tx->tl_next;
197836270Swpaul
197936270Swpaul		cur_tx->tl_next = NULL;
198036270Swpaul
198136270Swpaul		/* Pack the data into the list. */
198236270Swpaul		tl_encap(sc, cur_tx, m_head);
198336270Swpaul
198436270Swpaul		/* Chain it together */
198536270Swpaul		if (prev != NULL) {
198636270Swpaul			prev->tl_next = cur_tx;
198736270Swpaul			prev->tl_ptr->tlist_fptr = vtophys(cur_tx->tl_ptr);
198836270Swpaul		}
198936270Swpaul		prev = cur_tx;
199036270Swpaul
199136270Swpaul		/*
199236270Swpaul		 * If there's a BPF listener, bounce a copy of this frame
199336270Swpaul		 * to him.
199436270Swpaul		 */
1995106936Ssam		BPF_MTAP(ifp, cur_tx->tl_mbuf);
199636270Swpaul	}
199736270Swpaul
199836270Swpaul	/*
199941526Swpaul	 * If there are no packets queued, bail.
200041526Swpaul	 */
200167087Swpaul	if (cur_tx == NULL) {
200267087Swpaul		TL_UNLOCK(sc);
200341526Swpaul		return;
200467087Swpaul	}
200541526Swpaul
200641526Swpaul	/*
200736270Swpaul	 * That's all we can stands, we can't stands no more.
200836270Swpaul	 * If there are no other transfers pending, then issue the
200936270Swpaul	 * TX GO command to the adapter to start things moving.
201036270Swpaul	 * Otherwise, just leave the data in the queue and let
201136270Swpaul	 * the EOF/EOC interrupt handler send.
201236270Swpaul	 */
201336270Swpaul	if (sc->tl_cdata.tl_tx_head == NULL) {
201436270Swpaul		sc->tl_cdata.tl_tx_head = start_tx;
201536270Swpaul		sc->tl_cdata.tl_tx_tail = cur_tx;
201639583Swpaul
201736270Swpaul		if (sc->tl_txeoc) {
201836270Swpaul			sc->tl_txeoc = 0;
201939583Swpaul			CSR_WRITE_4(sc, TL_CH_PARM, vtophys(start_tx->tl_ptr));
202039583Swpaul			cmd = CSR_READ_4(sc, TL_HOSTCMD);
202136270Swpaul			cmd &= ~TL_CMD_RT;
202236270Swpaul			cmd |= TL_CMD_GO|TL_CMD_INTSON;
202339583Swpaul			CMD_PUT(sc, cmd);
202436270Swpaul		}
202536270Swpaul	} else {
202636270Swpaul		sc->tl_cdata.tl_tx_tail->tl_next = start_tx;
202742146Swpaul		sc->tl_cdata.tl_tx_tail = cur_tx;
202836270Swpaul	}
202936270Swpaul
203036270Swpaul	/*
203136270Swpaul	 * Set a timeout in case the chip goes out to lunch.
203236270Swpaul	 */
203336270Swpaul	ifp->if_timer = 5;
203467087Swpaul	TL_UNLOCK(sc);
203536270Swpaul
203636270Swpaul	return;
203736270Swpaul}
203836270Swpaul
2039102336Salfredstatic void
2040102336Salfredtl_init(xsc)
204136270Swpaul	void			*xsc;
204236270Swpaul{
204336270Swpaul	struct tl_softc		*sc = xsc;
204436270Swpaul	struct ifnet		*ifp = &sc->arpcom.ac_if;
204550462Swpaul	struct mii_data		*mii;
204636270Swpaul
204767087Swpaul	TL_LOCK(sc);
204836270Swpaul
204936270Swpaul	ifp = &sc->arpcom.ac_if;
205036270Swpaul
205136270Swpaul	/*
205236270Swpaul	 * Cancel pending I/O.
205336270Swpaul	 */
205436270Swpaul	tl_stop(sc);
205536270Swpaul
205651439Swpaul	/* Initialize TX FIFO threshold */
205751439Swpaul	tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_TXTHRESH);
205851439Swpaul	tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_TXTHRESH_16LONG);
205951439Swpaul
206051439Swpaul        /* Set PCI burst size */
206151439Swpaul	tl_dio_write8(sc, TL_BSIZEREG, TL_RXBURST_16LONG|TL_TXBURST_16LONG);
206251439Swpaul
206336270Swpaul	/*
206436270Swpaul	 * Set 'capture all frames' bit for promiscuous mode.
206536270Swpaul	 */
206639583Swpaul	if (ifp->if_flags & IFF_PROMISC)
206739583Swpaul		tl_dio_setbit(sc, TL_NETCMD, TL_CMD_CAF);
206839583Swpaul	else
206939583Swpaul		tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_CAF);
207036270Swpaul
207136270Swpaul	/*
207236270Swpaul	 * Set capture broadcast bit to capture broadcast frames.
207336270Swpaul	 */
207439583Swpaul	if (ifp->if_flags & IFF_BROADCAST)
207539583Swpaul		tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_NOBRX);
207639583Swpaul	else
207739583Swpaul		tl_dio_setbit(sc, TL_NETCMD, TL_CMD_NOBRX);
207836270Swpaul
207950468Swpaul	tl_dio_write16(sc, TL_MAXRX, MCLBYTES);
208050468Swpaul
208136270Swpaul	/* Init our MAC address */
208241656Swpaul	tl_setfilt(sc, (caddr_t)&sc->arpcom.ac_enaddr, 0);
208336270Swpaul
208439583Swpaul	/* Init multicast filter, if needed. */
208539583Swpaul	tl_setmulti(sc);
208639583Swpaul
208736270Swpaul	/* Init circular RX list. */
208839583Swpaul	if (tl_list_rx_init(sc) == ENOBUFS) {
2089105599Sbrooks		if_printf(ifp,
2090105599Sbrooks		    "initialization failed: no memory for rx buffers\n");
209139583Swpaul		tl_stop(sc);
209267087Swpaul		TL_UNLOCK(sc);
209336270Swpaul		return;
209436270Swpaul	}
209536270Swpaul
209636270Swpaul	/* Init TX pointers. */
209736270Swpaul	tl_list_tx_init(sc);
209836270Swpaul
209939583Swpaul	/* Enable PCI interrupts. */
210039583Swpaul	CMD_SET(sc, TL_CMD_INTSON);
210136270Swpaul
210236270Swpaul	/* Load the address of the rx list */
210339583Swpaul	CMD_SET(sc, TL_CMD_RT);
210439583Swpaul	CSR_WRITE_4(sc, TL_CH_PARM, vtophys(&sc->tl_ldata->tl_rx_list[0]));
210536270Swpaul
210650462Swpaul	if (!sc->tl_bitrate) {
210750462Swpaul		if (sc->tl_miibus != NULL) {
210850462Swpaul			mii = device_get_softc(sc->tl_miibus);
210950462Swpaul			mii_mediachg(mii);
211050462Swpaul		}
2111113548Smdodd	} else {
2112113548Smdodd		tl_ifmedia_upd(ifp);
211350462Swpaul	}
211438030Swpaul
211536270Swpaul	/* Send the RX go command */
211650468Swpaul	CMD_SET(sc, TL_CMD_GO|TL_CMD_NES|TL_CMD_RT);
211736270Swpaul
211836270Swpaul	ifp->if_flags |= IFF_RUNNING;
211936270Swpaul	ifp->if_flags &= ~IFF_OACTIVE;
212036270Swpaul
212136270Swpaul	/* Start the stats update counter */
212236270Swpaul	sc->tl_stat_ch = timeout(tl_stats_update, sc, hz);
212367087Swpaul	TL_UNLOCK(sc);
212436270Swpaul
212536270Swpaul	return;
212636270Swpaul}
212736270Swpaul
212836270Swpaul/*
212936270Swpaul * Set media options.
213036270Swpaul */
2131102336Salfredstatic int
2132102336Salfredtl_ifmedia_upd(ifp)
213336270Swpaul	struct ifnet		*ifp;
213436270Swpaul{
213536270Swpaul	struct tl_softc		*sc;
213650462Swpaul	struct mii_data		*mii = NULL;
213736270Swpaul
213836270Swpaul	sc = ifp->if_softc;
213936270Swpaul
214050462Swpaul	if (sc->tl_bitrate)
214150462Swpaul		tl_setmode(sc, sc->ifmedia.ifm_media);
214250462Swpaul	else {
214350462Swpaul		mii = device_get_softc(sc->tl_miibus);
214450462Swpaul		mii_mediachg(mii);
214550462Swpaul	}
214636270Swpaul
214736270Swpaul	return(0);
214836270Swpaul}
214936270Swpaul
215036270Swpaul/*
215136270Swpaul * Report current media status.
215236270Swpaul */
2153102336Salfredstatic void
2154102336Salfredtl_ifmedia_sts(ifp, ifmr)
215536270Swpaul	struct ifnet		*ifp;
215636270Swpaul	struct ifmediareq	*ifmr;
215736270Swpaul{
215836270Swpaul	struct tl_softc		*sc;
215950462Swpaul	struct mii_data		*mii;
216036270Swpaul
216136270Swpaul	sc = ifp->if_softc;
216236270Swpaul
216336270Swpaul	ifmr->ifm_active = IFM_ETHER;
216436270Swpaul
216545155Swpaul	if (sc->tl_bitrate) {
216645155Swpaul		if (tl_dio_read8(sc, TL_ACOMMIT) & TL_AC_MTXD1)
216745155Swpaul			ifmr->ifm_active = IFM_ETHER|IFM_10_5;
216845155Swpaul		else
216945155Swpaul			ifmr->ifm_active = IFM_ETHER|IFM_10_T;
217045155Swpaul		if (tl_dio_read8(sc, TL_ACOMMIT) & TL_AC_MTXD3)
217145155Swpaul			ifmr->ifm_active |= IFM_HDX;
217245155Swpaul		else
217345155Swpaul			ifmr->ifm_active |= IFM_FDX;
217445155Swpaul		return;
217536270Swpaul	} else {
217650462Swpaul		mii = device_get_softc(sc->tl_miibus);
217750462Swpaul		mii_pollstat(mii);
217850462Swpaul		ifmr->ifm_active = mii->mii_media_active;
217950462Swpaul		ifmr->ifm_status = mii->mii_media_status;
218036270Swpaul	}
218136270Swpaul
218236270Swpaul	return;
218336270Swpaul}
218436270Swpaul
2185102336Salfredstatic int
2186102336Salfredtl_ioctl(ifp, command, data)
218736270Swpaul	struct ifnet		*ifp;
218836735Sdfr	u_long			command;
218936270Swpaul	caddr_t			data;
219036270Swpaul{
219136270Swpaul	struct tl_softc		*sc = ifp->if_softc;
219236270Swpaul	struct ifreq		*ifr = (struct ifreq *) data;
219336270Swpaul	int			s, error = 0;
219436270Swpaul
219536270Swpaul	s = splimp();
219636270Swpaul
219736270Swpaul	switch(command) {
219836270Swpaul	case SIOCSIFFLAGS:
219936270Swpaul		if (ifp->if_flags & IFF_UP) {
220050462Swpaul			if (ifp->if_flags & IFF_RUNNING &&
220150462Swpaul			    ifp->if_flags & IFF_PROMISC &&
220250462Swpaul			    !(sc->tl_if_flags & IFF_PROMISC)) {
220350462Swpaul				tl_dio_setbit(sc, TL_NETCMD, TL_CMD_CAF);
220450462Swpaul				tl_setmulti(sc);
220550462Swpaul			} else if (ifp->if_flags & IFF_RUNNING &&
220650462Swpaul			    !(ifp->if_flags & IFF_PROMISC) &&
220750462Swpaul			    sc->tl_if_flags & IFF_PROMISC) {
220850462Swpaul				tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_CAF);
220950462Swpaul				tl_setmulti(sc);
221050462Swpaul			} else
221150462Swpaul				tl_init(sc);
221236270Swpaul		} else {
221336270Swpaul			if (ifp->if_flags & IFF_RUNNING) {
221436270Swpaul				tl_stop(sc);
221536270Swpaul			}
221636270Swpaul		}
221750462Swpaul		sc->tl_if_flags = ifp->if_flags;
221836270Swpaul		error = 0;
221936270Swpaul		break;
222036270Swpaul	case SIOCADDMULTI:
222136270Swpaul	case SIOCDELMULTI:
222236270Swpaul		tl_setmulti(sc);
222336270Swpaul		error = 0;
222436270Swpaul		break;
222536270Swpaul	case SIOCSIFMEDIA:
222636270Swpaul	case SIOCGIFMEDIA:
222750462Swpaul		if (sc->tl_bitrate)
222850462Swpaul			error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
222950462Swpaul		else {
223050462Swpaul			struct mii_data		*mii;
223150462Swpaul			mii = device_get_softc(sc->tl_miibus);
223250462Swpaul			error = ifmedia_ioctl(ifp, ifr,
223350462Swpaul			    &mii->mii_media, command);
223450462Swpaul		}
223536270Swpaul		break;
223636270Swpaul	default:
2237106936Ssam		error = ether_ioctl(ifp, command, data);
223836270Swpaul		break;
223936270Swpaul	}
224036270Swpaul
224136270Swpaul	(void)splx(s);
224236270Swpaul
224336270Swpaul	return(error);
224436270Swpaul}
224536270Swpaul
2246102336Salfredstatic void
2247102336Salfredtl_watchdog(ifp)
224836270Swpaul	struct ifnet		*ifp;
224936270Swpaul{
225036270Swpaul	struct tl_softc		*sc;
225136270Swpaul
225236270Swpaul	sc = ifp->if_softc;
225336270Swpaul
2254105599Sbrooks	if_printf(ifp, "device timeout\n");
225536270Swpaul
225636270Swpaul	ifp->if_oerrors++;
225736270Swpaul
225850468Swpaul	tl_softreset(sc, 1);
225936270Swpaul	tl_init(sc);
226036270Swpaul
226136270Swpaul	return;
226236270Swpaul}
226336270Swpaul
226436270Swpaul/*
226536270Swpaul * Stop the adapter and free any mbufs allocated to the
226636270Swpaul * RX and TX lists.
226736270Swpaul */
2268102336Salfredstatic void
2269102336Salfredtl_stop(sc)
227036270Swpaul	struct tl_softc		*sc;
227136270Swpaul{
227236270Swpaul	register int		i;
227336270Swpaul	struct ifnet		*ifp;
227436270Swpaul
227567087Swpaul	TL_LOCK(sc);
227667087Swpaul
227736270Swpaul	ifp = &sc->arpcom.ac_if;
227836270Swpaul
227936270Swpaul	/* Stop the stats updater. */
228036270Swpaul	untimeout(tl_stats_update, sc, sc->tl_stat_ch);
228136270Swpaul
228236270Swpaul	/* Stop the transmitter */
228339583Swpaul	CMD_CLR(sc, TL_CMD_RT);
228439583Swpaul	CMD_SET(sc, TL_CMD_STOP);
228539583Swpaul	CSR_WRITE_4(sc, TL_CH_PARM, 0);
228636270Swpaul
228736270Swpaul	/* Stop the receiver */
228839583Swpaul	CMD_SET(sc, TL_CMD_RT);
228939583Swpaul	CMD_SET(sc, TL_CMD_STOP);
229039583Swpaul	CSR_WRITE_4(sc, TL_CH_PARM, 0);
229136270Swpaul
229236270Swpaul	/*
229336270Swpaul	 * Disable host interrupts.
229436270Swpaul	 */
229539583Swpaul	CMD_SET(sc, TL_CMD_INTSOFF);
229636270Swpaul
229736270Swpaul	/*
229836270Swpaul	 * Clear list pointer.
229936270Swpaul	 */
230039583Swpaul	CSR_WRITE_4(sc, TL_CH_PARM, 0);
230136270Swpaul
230236270Swpaul	/*
230336270Swpaul	 * Free the RX lists.
230436270Swpaul	 */
230536270Swpaul	for (i = 0; i < TL_RX_LIST_CNT; i++) {
230636270Swpaul		if (sc->tl_cdata.tl_rx_chain[i].tl_mbuf != NULL) {
230736270Swpaul			m_freem(sc->tl_cdata.tl_rx_chain[i].tl_mbuf);
230836270Swpaul			sc->tl_cdata.tl_rx_chain[i].tl_mbuf = NULL;
230936270Swpaul		}
231036270Swpaul	}
231136270Swpaul	bzero((char *)&sc->tl_ldata->tl_rx_list,
231236270Swpaul		sizeof(sc->tl_ldata->tl_rx_list));
231336270Swpaul
231436270Swpaul	/*
231536270Swpaul	 * Free the TX list buffers.
231636270Swpaul	 */
231736270Swpaul	for (i = 0; i < TL_TX_LIST_CNT; i++) {
231836270Swpaul		if (sc->tl_cdata.tl_tx_chain[i].tl_mbuf != NULL) {
231936270Swpaul			m_freem(sc->tl_cdata.tl_tx_chain[i].tl_mbuf);
232036270Swpaul			sc->tl_cdata.tl_tx_chain[i].tl_mbuf = NULL;
232136270Swpaul		}
232236270Swpaul	}
232336270Swpaul	bzero((char *)&sc->tl_ldata->tl_tx_list,
232436270Swpaul		sizeof(sc->tl_ldata->tl_tx_list));
232536270Swpaul
232636270Swpaul	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
232767087Swpaul	TL_UNLOCK(sc);
232836270Swpaul
232936270Swpaul	return;
233036270Swpaul}
233136270Swpaul
233236270Swpaul/*
233336270Swpaul * Stop all chip I/O so that the kernel's probe routines don't
233436270Swpaul * get confused by errant DMAs when rebooting.
233536270Swpaul */
2336102336Salfredstatic void
2337102336Salfredtl_shutdown(dev)
233848992Swpaul	device_t		dev;
233936270Swpaul{
234039583Swpaul	struct tl_softc		*sc;
234136270Swpaul
234248992Swpaul	sc = device_get_softc(dev);
234336270Swpaul
234439583Swpaul	tl_stop(sc);
234536270Swpaul
234636270Swpaul	return;
234736270Swpaul}
2348