Home | History | Annotate | Line # | Download | only in pci
if_ti.c revision 1.111
      1  1.111   msaitoh /* $NetBSD: if_ti.c,v 1.111 2019/05/29 10:07:29 msaitoh Exp $ */
      2    1.1  drochner 
      3    1.1  drochner /*
      4    1.1  drochner  * Copyright (c) 1997, 1998, 1999
      5    1.1  drochner  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6    1.1  drochner  *
      7    1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8    1.1  drochner  * modification, are permitted provided that the following conditions
      9    1.1  drochner  * are met:
     10    1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11    1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     12    1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13    1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14    1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15    1.1  drochner  * 3. All advertising materials mentioning features or use of this software
     16    1.1  drochner  *    must display the following acknowledgement:
     17    1.1  drochner  *	This product includes software developed by Bill Paul.
     18    1.1  drochner  * 4. Neither the name of the author nor the names of any co-contributors
     19    1.1  drochner  *    may be used to endorse or promote products derived from this software
     20    1.1  drochner  *    without specific prior written permission.
     21    1.1  drochner  *
     22    1.1  drochner  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23    1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24    1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25    1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26    1.1  drochner  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27    1.1  drochner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28    1.1  drochner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29    1.1  drochner  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30    1.1  drochner  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31    1.1  drochner  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32    1.1  drochner  * THE POSSIBILITY OF SUCH DAMAGE.
     33    1.1  drochner  *
     34    1.1  drochner  *	FreeBSD Id: if_ti.c,v 1.15 1999/08/14 15:45:03 wpaul Exp
     35    1.1  drochner  */
     36    1.1  drochner 
     37    1.1  drochner /*
     38    1.1  drochner  * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
     39    1.1  drochner  * Manuals, sample driver and firmware source kits are available
     40    1.1  drochner  * from http://www.alteon.com/support/openkits.
     41   1.66     perry  *
     42    1.1  drochner  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     43    1.1  drochner  * Electrical Engineering Department
     44    1.1  drochner  * Columbia University, New York City
     45    1.1  drochner  */
     46    1.1  drochner 
     47    1.1  drochner /*
     48    1.1  drochner  * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
     49    1.1  drochner  * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
     50    1.1  drochner  * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
     51    1.1  drochner  * Tigon supports hardware IP, TCP and UCP checksumming, multicast
     52    1.1  drochner  * filtering and jumbo (9014 byte) frames. The hardware is largely
     53    1.1  drochner  * controlled by firmware, which must be loaded into the NIC during
     54    1.1  drochner  * initialization.
     55    1.1  drochner  *
     56    1.1  drochner  * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
     57    1.1  drochner  * revision, which supports new features such as extended commands,
     58    1.1  drochner  * extended jumbo receive ring desciptors and a mini receive ring.
     59    1.1  drochner  *
     60    1.1  drochner  * Alteon Networks is to be commended for releasing such a vast amount
     61    1.1  drochner  * of development material for the Tigon NIC without requiring an NDA
     62    1.1  drochner  * (although they really should have done it a long time ago). With
     63    1.1  drochner  * any luck, the other vendors will finally wise up and follow Alteon's
     64    1.1  drochner  * stellar example.
     65    1.1  drochner  *
     66    1.1  drochner  * The firmware for the Tigon 1 and 2 NICs is compiled directly into
     67    1.1  drochner  * this driver by #including it as a C header file. This bloats the
     68    1.1  drochner  * driver somewhat, but it's the easiest method considering that the
     69    1.1  drochner  * driver code and firmware code need to be kept in sync. The source
     70    1.1  drochner  * for the firmware is not provided with the FreeBSD distribution since
     71    1.1  drochner  * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
     72    1.1  drochner  *
     73    1.1  drochner  * The following people deserve special thanks:
     74    1.1  drochner  * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
     75    1.1  drochner  *   for testing
     76    1.1  drochner  * - Raymond Lee of Netgear, for providing a pair of Netgear
     77    1.1  drochner  *   GA620 Tigon 2 boards for testing
     78    1.3   thorpej  * - Ulf Zimmermann, for bringing the GA620 to my attention and
     79    1.1  drochner  *   convincing me to write this driver.
     80    1.1  drochner  * - Andrew Gallatin for providing FreeBSD/Alpha support.
     81    1.1  drochner  */
     82   1.43     lukem 
     83   1.43     lukem #include <sys/cdefs.h>
     84  1.111   msaitoh __KERNEL_RCSID(0, "$NetBSD: if_ti.c,v 1.111 2019/05/29 10:07:29 msaitoh Exp $");
     85    1.1  drochner 
     86    1.1  drochner #include "opt_inet.h"
     87    1.1  drochner 
     88    1.1  drochner #include <sys/param.h>
     89    1.1  drochner #include <sys/systm.h>
     90    1.1  drochner #include <sys/sockio.h>
     91    1.1  drochner #include <sys/mbuf.h>
     92    1.1  drochner #include <sys/malloc.h>
     93    1.1  drochner #include <sys/kernel.h>
     94    1.1  drochner #include <sys/socket.h>
     95    1.1  drochner #include <sys/queue.h>
     96    1.1  drochner #include <sys/device.h>
     97    1.9  jdolecek #include <sys/reboot.h>
     98    1.1  drochner 
     99    1.1  drochner #include <net/if.h>
    100    1.1  drochner #include <net/if_arp.h>
    101    1.1  drochner #include <net/if_ether.h>
    102    1.1  drochner #include <net/if_dl.h>
    103    1.1  drochner #include <net/if_media.h>
    104    1.1  drochner 
    105    1.1  drochner #include <net/bpf.h>
    106    1.1  drochner 
    107    1.1  drochner #ifdef INET
    108    1.1  drochner #include <netinet/in.h>
    109    1.1  drochner #include <netinet/if_inarp.h>
    110   1.21   thorpej #include <netinet/in_systm.h>
    111   1.21   thorpej #include <netinet/ip.h>
    112    1.1  drochner #endif
    113    1.1  drochner 
    114    1.2  drochner 
    115   1.78        ad #include <sys/bus.h>
    116    1.1  drochner 
    117    1.1  drochner #include <dev/pci/pcireg.h>
    118    1.1  drochner #include <dev/pci/pcivar.h>
    119    1.1  drochner #include <dev/pci/pcidevs.h>
    120    1.1  drochner 
    121    1.1  drochner #include <dev/pci/if_tireg.h>
    122   1.28   thorpej 
    123   1.28   thorpej #include <dev/microcode/tigon/ti_fw.h>
    124   1.28   thorpej #include <dev/microcode/tigon/ti_fw2.h>
    125    1.1  drochner 
    126    1.1  drochner /*
    127    1.1  drochner  * Various supported device vendors/types and their names.
    128    1.1  drochner  */
    129    1.1  drochner 
    130   1.19  jdolecek static const struct ti_type ti_devs[] = {
    131    1.1  drochner 	{ PCI_VENDOR_ALTEON,	PCI_PRODUCT_ALTEON_ACENIC,
    132   1.37   thorpej 		"Alteon AceNIC 1000BASE-SX Ethernet" },
    133   1.15    bouyer 	{ PCI_VENDOR_ALTEON,	PCI_PRODUCT_ALTEON_ACENIC_COPPER,
    134   1.37   thorpej 		"Alteon AceNIC 1000BASE-T Ethernet" },
    135    1.1  drochner 	{ PCI_VENDOR_3COM,	PCI_PRODUCT_3COM_3C985,
    136    1.1  drochner 		"3Com 3c985-SX Gigabit Ethernet" },
    137    1.1  drochner 	{ PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_GA620,
    138   1.37   thorpej 		"Netgear GA620 1000BASE-SX Ethernet" },
    139   1.15    bouyer 	{ PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_GA620T,
    140   1.37   thorpej 		"Netgear GA620 1000BASE-T Ethernet" },
    141    1.1  drochner 	{ PCI_VENDOR_SGI, PCI_PRODUCT_SGI_TIGON,
    142    1.1  drochner 		"Silicon Graphics Gigabit Ethernet" },
    143    1.1  drochner 	{ 0, 0, NULL }
    144    1.1  drochner };
    145    1.1  drochner 
    146   1.64     perry static const struct ti_type *ti_type_match(struct pci_attach_args *);
    147   1.84    cegger static int ti_probe(device_t, cfdata_t, void *);
    148   1.84    cegger static void ti_attach(device_t, device_t, void *);
    149   1.86   tsutsui static bool ti_shutdown(device_t, int);
    150   1.64     perry static void ti_txeof_tigon1(struct ti_softc *);
    151   1.64     perry static void ti_txeof_tigon2(struct ti_softc *);
    152   1.64     perry static void ti_rxeof(struct ti_softc *);
    153   1.64     perry 
    154   1.64     perry static void ti_stats_update(struct ti_softc *);
    155  1.108   msaitoh static int ti_encap_tigon1(struct ti_softc *, struct mbuf *, uint32_t *);
    156  1.108   msaitoh static int ti_encap_tigon2(struct ti_softc *, struct mbuf *, uint32_t *);
    157   1.64     perry 
    158   1.64     perry static int ti_intr(void *);
    159   1.64     perry static void ti_start(struct ifnet *);
    160   1.74  christos static int ti_ioctl(struct ifnet *, u_long, void *);
    161   1.64     perry static void ti_init(void *);
    162   1.64     perry static void ti_init2(struct ti_softc *);
    163   1.64     perry static void ti_stop(struct ti_softc *);
    164   1.64     perry static void ti_watchdog(struct ifnet *);
    165   1.64     perry static int ti_ifmedia_upd(struct ifnet *);
    166   1.64     perry static void ti_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    167   1.64     perry 
    168  1.108   msaitoh static uint32_t ti_eeprom_putbyte(struct ti_softc *, int);
    169  1.108   msaitoh static uint8_t	ti_eeprom_getbyte(struct ti_softc *, int, uint8_t *);
    170   1.74  christos static int ti_read_eeprom(struct ti_softc *, void *, int, int);
    171   1.64     perry 
    172   1.64     perry static void ti_add_mcast(struct ti_softc *, struct ether_addr *);
    173   1.64     perry static void ti_del_mcast(struct ti_softc *, struct ether_addr *);
    174   1.64     perry static void ti_setmulti(struct ti_softc *);
    175   1.64     perry 
    176  1.108   msaitoh static void ti_mem(struct ti_softc *, uint32_t, uint32_t, const void *);
    177   1.64     perry static void ti_loadfw(struct ti_softc *);
    178   1.64     perry static void ti_cmd(struct ti_softc *, struct ti_cmd_desc *);
    179   1.74  christos static void ti_cmd_ext(struct ti_softc *, struct ti_cmd_desc *, void *, int);
    180   1.64     perry static void ti_handle_events(struct ti_softc *);
    181   1.64     perry static int ti_alloc_jumbo_mem(struct ti_softc *);
    182   1.64     perry static void *ti_jalloc(struct ti_softc *);
    183   1.74  christos static void ti_jfree(struct mbuf *, void *, size_t, void *);
    184   1.64     perry static int ti_newbuf_std(struct ti_softc *, int, struct mbuf *, bus_dmamap_t);
    185   1.64     perry static int ti_newbuf_mini(struct ti_softc *, int, struct mbuf *, bus_dmamap_t);
    186   1.64     perry static int ti_newbuf_jumbo(struct ti_softc *, int, struct mbuf *);
    187   1.64     perry static int ti_init_rx_ring_std(struct ti_softc *);
    188   1.64     perry static void ti_free_rx_ring_std(struct ti_softc *);
    189   1.64     perry static int ti_init_rx_ring_jumbo(struct ti_softc *);
    190   1.64     perry static void ti_free_rx_ring_jumbo(struct ti_softc *);
    191   1.64     perry static int ti_init_rx_ring_mini(struct ti_softc *);
    192   1.64     perry static void ti_free_rx_ring_mini(struct ti_softc *);
    193   1.64     perry static void ti_free_tx_ring(struct ti_softc *);
    194   1.64     perry static int ti_init_tx_ring(struct ti_softc *);
    195   1.64     perry 
    196   1.64     perry static int ti_64bitslot_war(struct ti_softc *);
    197   1.64     perry static int ti_chipinit(struct ti_softc *);
    198   1.64     perry static int ti_gibinit(struct ti_softc *);
    199    1.1  drochner 
    200   1.74  christos static int ti_ether_ioctl(struct ifnet *, u_long, void *);
    201    1.1  drochner 
    202   1.91       chs CFATTACH_DECL_NEW(ti, sizeof(struct ti_softc),
    203   1.52   thorpej     ti_probe, ti_attach, NULL, NULL);
    204    1.1  drochner 
    205    1.1  drochner /*
    206    1.1  drochner  * Send an instruction or address to the EEPROM, check for ACK.
    207    1.1  drochner  */
    208  1.108   msaitoh static uint32_t
    209   1.77       tnn ti_eeprom_putbyte(struct ti_softc *sc, int byte)
    210    1.1  drochner {
    211   1.64     perry 	int i, ack = 0;
    212    1.1  drochner 
    213    1.1  drochner 	/*
    214    1.1  drochner 	 * Make sure we're in TX mode.
    215    1.1  drochner 	 */
    216    1.1  drochner 	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
    217    1.1  drochner 
    218    1.1  drochner 	/*
    219    1.1  drochner 	 * Feed in each bit and stobe the clock.
    220    1.1  drochner 	 */
    221    1.1  drochner 	for (i = 0x80; i; i >>= 1) {
    222    1.1  drochner 		if (byte & i) {
    223    1.1  drochner 			TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
    224    1.1  drochner 		} else {
    225    1.1  drochner 			TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
    226    1.1  drochner 		}
    227    1.1  drochner 		DELAY(1);
    228    1.1  drochner 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    229    1.1  drochner 		DELAY(1);
    230    1.1  drochner 		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    231    1.1  drochner 	}
    232    1.1  drochner 
    233    1.1  drochner 	/*
    234    1.1  drochner 	 * Turn off TX mode.
    235    1.1  drochner 	 */
    236    1.1  drochner 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
    237    1.1  drochner 
    238    1.1  drochner 	/*
    239    1.1  drochner 	 * Check for ack.
    240    1.1  drochner 	 */
    241    1.1  drochner 	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    242    1.1  drochner 	ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
    243    1.1  drochner 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    244    1.1  drochner 
    245   1.77       tnn 	return (ack);
    246    1.1  drochner }
    247    1.1  drochner 
    248    1.1  drochner /*
    249    1.1  drochner  * Read a byte of data stored in the EEPROM at address 'addr.'
    250    1.1  drochner  * We have to send two address bytes since the EEPROM can hold
    251    1.1  drochner  * more than 256 bytes of data.
    252    1.1  drochner  */
    253  1.108   msaitoh static uint8_t
    254  1.108   msaitoh ti_eeprom_getbyte(struct ti_softc *sc, int addr, uint8_t *dest)
    255    1.1  drochner {
    256    1.8  augustss 	int		i;
    257  1.108   msaitoh 	uint8_t		byte = 0;
    258    1.1  drochner 
    259   1.77       tnn 	EEPROM_START();
    260    1.1  drochner 
    261    1.1  drochner 	/*
    262    1.1  drochner 	 * Send write control code to EEPROM.
    263    1.1  drochner 	 */
    264    1.1  drochner 	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
    265    1.1  drochner 		printf("%s: failed to send write command, status: %x\n",
    266   1.91       chs 		    device_xname(sc->sc_dev), CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    267   1.77       tnn 		return (1);
    268    1.1  drochner 	}
    269    1.1  drochner 
    270    1.1  drochner 	/*
    271    1.1  drochner 	 * Send first byte of address of byte we want to read.
    272    1.1  drochner 	 */
    273    1.1  drochner 	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
    274    1.1  drochner 		printf("%s: failed to send address, status: %x\n",
    275   1.91       chs 		    device_xname(sc->sc_dev), CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    276   1.77       tnn 		return (1);
    277    1.1  drochner 	}
    278    1.1  drochner 	/*
    279    1.1  drochner 	 * Send second byte address of byte we want to read.
    280    1.1  drochner 	 */
    281    1.1  drochner 	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
    282    1.1  drochner 		printf("%s: failed to send address, status: %x\n",
    283   1.91       chs 		    device_xname(sc->sc_dev), CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    284   1.77       tnn 		return (1);
    285    1.1  drochner 	}
    286    1.1  drochner 
    287   1.77       tnn 	EEPROM_STOP();
    288   1.77       tnn 	EEPROM_START();
    289    1.1  drochner 	/*
    290    1.1  drochner 	 * Send read control code to EEPROM.
    291    1.1  drochner 	 */
    292    1.1  drochner 	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
    293    1.1  drochner 		printf("%s: failed to send read command, status: %x\n",
    294   1.91       chs 		    device_xname(sc->sc_dev), CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    295   1.77       tnn 		return (1);
    296    1.1  drochner 	}
    297    1.1  drochner 
    298    1.1  drochner 	/*
    299    1.1  drochner 	 * Start reading bits from EEPROM.
    300    1.1  drochner 	 */
    301    1.1  drochner 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
    302    1.1  drochner 	for (i = 0x80; i; i >>= 1) {
    303    1.1  drochner 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    304    1.1  drochner 		DELAY(1);
    305    1.1  drochner 		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
    306    1.1  drochner 			byte |= i;
    307    1.1  drochner 		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    308    1.1  drochner 		DELAY(1);
    309    1.1  drochner 	}
    310    1.1  drochner 
    311   1.77       tnn 	EEPROM_STOP();
    312    1.1  drochner 
    313    1.1  drochner 	/*
    314    1.1  drochner 	 * No ACK generated for read, so just return byte.
    315    1.1  drochner 	 */
    316    1.1  drochner 
    317    1.1  drochner 	*dest = byte;
    318    1.1  drochner 
    319   1.77       tnn 	return (0);
    320    1.1  drochner }
    321    1.1  drochner 
    322    1.1  drochner /*
    323    1.1  drochner  * Read a sequence of bytes from the EEPROM.
    324    1.1  drochner  */
    325   1.77       tnn static int
    326   1.77       tnn ti_read_eeprom(struct ti_softc *sc, void *destv, int off, int cnt)
    327    1.1  drochner {
    328   1.74  christos 	char *dest = destv;
    329   1.74  christos 	int err = 0, i;
    330  1.108   msaitoh 	uint8_t byte = 0;
    331    1.1  drochner 
    332    1.1  drochner 	for (i = 0; i < cnt; i++) {
    333    1.1  drochner 		err = ti_eeprom_getbyte(sc, off + i, &byte);
    334    1.1  drochner 		if (err)
    335    1.1  drochner 			break;
    336    1.1  drochner 		*(dest + i) = byte;
    337    1.1  drochner 	}
    338    1.1  drochner 
    339   1.77       tnn 	return (err ? 1 : 0);
    340    1.1  drochner }
    341    1.1  drochner 
    342    1.1  drochner /*
    343    1.1  drochner  * NIC memory access function. Can be used to either clear a section
    344   1.68  christos  * of NIC local memory or (if tbuf is non-NULL) copy data into it.
    345    1.1  drochner  */
    346   1.77       tnn static void
    347  1.108   msaitoh ti_mem(struct ti_softc *sc, uint32_t addr, uint32_t len, const void *xbuf)
    348    1.1  drochner {
    349    1.1  drochner 	int			segptr, segsize, cnt;
    350   1.68  christos 	const void		*ptr;
    351    1.1  drochner 
    352    1.1  drochner 	segptr = addr;
    353    1.1  drochner 	cnt = len;
    354   1.68  christos 	ptr = xbuf;
    355    1.1  drochner 
    356   1.77       tnn 	while (cnt) {
    357    1.1  drochner 		if (cnt < TI_WINLEN)
    358    1.1  drochner 			segsize = cnt;
    359    1.1  drochner 		else
    360    1.1  drochner 			segsize = TI_WINLEN - (segptr % TI_WINLEN);
    361    1.1  drochner 		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
    362   1.68  christos 		if (xbuf == NULL) {
    363    1.6    bouyer 			bus_space_set_region_4(sc->ti_btag, sc->ti_bhandle,
    364    1.6    bouyer 			    TI_WINDOW + (segptr & (TI_WINLEN - 1)), 0,
    365    1.6    bouyer 			    segsize / 4);
    366    1.6    bouyer 		} else {
    367   1.60    bouyer #ifdef __BUS_SPACE_HAS_STREAM_METHODS
    368   1.60    bouyer 			bus_space_write_region_stream_4(sc->ti_btag,
    369   1.60    bouyer 			    sc->ti_bhandle,
    370   1.60    bouyer 			    TI_WINDOW + (segptr & (TI_WINLEN - 1)),
    371  1.108   msaitoh 			    (const uint32_t *)ptr, segsize / 4);
    372   1.60    bouyer #else
    373    1.6    bouyer 			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
    374    1.6    bouyer 			    TI_WINDOW + (segptr & (TI_WINLEN - 1)),
    375  1.108   msaitoh 			    (const uint32_t *)ptr, segsize / 4);
    376   1.60    bouyer #endif
    377   1.68  christos 			ptr = (const char *)ptr + segsize;
    378    1.1  drochner 		}
    379    1.1  drochner 		segptr += segsize;
    380    1.1  drochner 		cnt -= segsize;
    381    1.1  drochner 	}
    382    1.1  drochner 
    383    1.1  drochner 	return;
    384    1.1  drochner }
    385    1.1  drochner 
    386    1.1  drochner /*
    387    1.1  drochner  * Load firmware image into the NIC. Check that the firmware revision
    388    1.1  drochner  * is acceptable and see if we want the firmware for the Tigon 1 or
    389    1.1  drochner  * Tigon 2.
    390    1.1  drochner  */
    391   1.77       tnn static void
    392   1.77       tnn ti_loadfw(struct ti_softc *sc)
    393    1.1  drochner {
    394   1.77       tnn 	switch (sc->ti_hwrev) {
    395    1.1  drochner 	case TI_HWREV_TIGON:
    396    1.1  drochner 		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
    397    1.1  drochner 		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
    398    1.1  drochner 		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
    399    1.1  drochner 			printf("%s: firmware revision mismatch; want "
    400   1.91       chs 			    "%d.%d.%d, got %d.%d.%d\n", device_xname(sc->sc_dev),
    401    1.1  drochner 			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
    402    1.1  drochner 			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
    403    1.1  drochner 			    tigonFwReleaseMinor, tigonFwReleaseFix);
    404    1.1  drochner 			return;
    405    1.1  drochner 		}
    406   1.68  christos 		ti_mem(sc, tigonFwTextAddr, tigonFwTextLen, tigonFwText);
    407   1.68  christos 		ti_mem(sc, tigonFwDataAddr, tigonFwDataLen, tigonFwData);
    408   1.68  christos 		ti_mem(sc, tigonFwRodataAddr, tigonFwRodataLen, tigonFwRodata);
    409    1.1  drochner 		ti_mem(sc, tigonFwBssAddr, tigonFwBssLen, NULL);
    410    1.1  drochner 		ti_mem(sc, tigonFwSbssAddr, tigonFwSbssLen, NULL);
    411    1.1  drochner 		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
    412    1.1  drochner 		break;
    413    1.1  drochner 	case TI_HWREV_TIGON_II:
    414    1.1  drochner 		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
    415    1.1  drochner 		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
    416    1.1  drochner 		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
    417    1.1  drochner 			printf("%s: firmware revision mismatch; want "
    418   1.91       chs 			    "%d.%d.%d, got %d.%d.%d\n", device_xname(sc->sc_dev),
    419    1.1  drochner 			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
    420    1.1  drochner 			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
    421    1.1  drochner 			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
    422    1.1  drochner 			return;
    423    1.1  drochner 		}
    424   1.68  christos 		ti_mem(sc, tigon2FwTextAddr, tigon2FwTextLen, tigon2FwText);
    425   1.68  christos 		ti_mem(sc, tigon2FwDataAddr, tigon2FwDataLen, tigon2FwData);
    426    1.1  drochner 		ti_mem(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
    427   1.68  christos 		    tigon2FwRodata);
    428    1.1  drochner 		ti_mem(sc, tigon2FwBssAddr, tigon2FwBssLen, NULL);
    429    1.1  drochner 		ti_mem(sc, tigon2FwSbssAddr, tigon2FwSbssLen, NULL);
    430    1.1  drochner 		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
    431    1.1  drochner 		break;
    432    1.1  drochner 	default:
    433    1.1  drochner 		printf("%s: can't load firmware: unknown hardware rev\n",
    434   1.91       chs 		    device_xname(sc->sc_dev));
    435    1.1  drochner 		break;
    436    1.1  drochner 	}
    437    1.1  drochner 
    438    1.1  drochner 	return;
    439    1.1  drochner }
    440    1.1  drochner 
    441    1.1  drochner /*
    442    1.1  drochner  * Send the NIC a command via the command ring.
    443    1.1  drochner  */
    444   1.77       tnn static void
    445   1.77       tnn ti_cmd(struct ti_softc *sc, struct ti_cmd_desc *cmd)
    446    1.1  drochner {
    447  1.108   msaitoh 	uint32_t		index;
    448    1.1  drochner 
    449    1.1  drochner 	index = sc->ti_cmd_saved_prodidx;
    450  1.108   msaitoh 	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(uint32_t *)(cmd));
    451    1.1  drochner 	TI_INC(index, TI_CMD_RING_CNT);
    452    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
    453    1.1  drochner 	sc->ti_cmd_saved_prodidx = index;
    454    1.1  drochner }
    455    1.1  drochner 
    456    1.1  drochner /*
    457    1.1  drochner  * Send the NIC an extended command. The 'len' parameter specifies the
    458    1.1  drochner  * number of command slots to include after the initial command.
    459    1.1  drochner  */
    460   1.77       tnn static void
    461   1.77       tnn ti_cmd_ext(struct ti_softc *sc, struct ti_cmd_desc *cmd, void *argv, int len)
    462    1.1  drochner {
    463  1.108   msaitoh 	char		*arg = argv;
    464  1.108   msaitoh 	uint32_t	index;
    465    1.8  augustss 	int		i;
    466    1.1  drochner 
    467    1.1  drochner 	index = sc->ti_cmd_saved_prodidx;
    468  1.108   msaitoh 	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(uint32_t *)(cmd));
    469    1.1  drochner 	TI_INC(index, TI_CMD_RING_CNT);
    470    1.1  drochner 	for (i = 0; i < len; i++) {
    471    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
    472  1.108   msaitoh 		    *(uint32_t *)(&arg[i * 4]));
    473    1.1  drochner 		TI_INC(index, TI_CMD_RING_CNT);
    474    1.1  drochner 	}
    475    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
    476    1.1  drochner 	sc->ti_cmd_saved_prodidx = index;
    477    1.1  drochner }
    478    1.1  drochner 
    479    1.1  drochner /*
    480    1.1  drochner  * Handle events that have triggered interrupts.
    481    1.1  drochner  */
    482   1.77       tnn static void
    483   1.77       tnn ti_handle_events(struct ti_softc *sc)
    484    1.1  drochner {
    485    1.1  drochner 	struct ti_event_desc	*e;
    486    1.1  drochner 
    487    1.1  drochner 	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
    488    1.1  drochner 		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
    489   1.77       tnn 		switch (TI_EVENT_EVENT(e)) {
    490    1.1  drochner 		case TI_EV_LINKSTAT_CHANGED:
    491   1.77       tnn 			sc->ti_linkstat = TI_EVENT_CODE(e);
    492   1.77       tnn 			if (sc->ti_linkstat == TI_EV_CODE_LINK_UP)
    493    1.1  drochner 				printf("%s: 10/100 link up\n",
    494   1.91       chs 				       device_xname(sc->sc_dev));
    495   1.77       tnn 			else if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP)
    496    1.1  drochner 				printf("%s: gigabit link up\n",
    497   1.91       chs 				       device_xname(sc->sc_dev));
    498   1.77       tnn 			else if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
    499    1.1  drochner 				printf("%s: link down\n",
    500   1.91       chs 				       device_xname(sc->sc_dev));
    501    1.1  drochner 			break;
    502    1.1  drochner 		case TI_EV_ERROR:
    503   1.77       tnn 			if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_INVAL_CMD)
    504    1.1  drochner 				printf("%s: invalid command\n",
    505   1.91       chs 				       device_xname(sc->sc_dev));
    506   1.77       tnn 			else if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_UNIMP_CMD)
    507    1.1  drochner 				printf("%s: unknown command\n",
    508   1.91       chs 				       device_xname(sc->sc_dev));
    509   1.77       tnn 			else if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_BADCFG)
    510    1.1  drochner 				printf("%s: bad config data\n",
    511   1.91       chs 				       device_xname(sc->sc_dev));
    512    1.1  drochner 			break;
    513    1.1  drochner 		case TI_EV_FIRMWARE_UP:
    514    1.1  drochner 			ti_init2(sc);
    515    1.1  drochner 			break;
    516    1.1  drochner 		case TI_EV_STATS_UPDATED:
    517    1.1  drochner 			ti_stats_update(sc);
    518    1.1  drochner 			break;
    519    1.1  drochner 		case TI_EV_RESET_JUMBO_RING:
    520    1.1  drochner 		case TI_EV_MCAST_UPDATED:
    521    1.1  drochner 			/* Who cares. */
    522    1.1  drochner 			break;
    523    1.1  drochner 		default:
    524    1.1  drochner 			printf("%s: unknown event: %d\n",
    525   1.91       chs 			    device_xname(sc->sc_dev), TI_EVENT_EVENT(e));
    526    1.1  drochner 			break;
    527    1.1  drochner 		}
    528    1.1  drochner 		/* Advance the consumer index. */
    529    1.1  drochner 		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
    530    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
    531    1.1  drochner 	}
    532    1.1  drochner 
    533    1.1  drochner 	return;
    534    1.1  drochner }
    535    1.1  drochner 
    536    1.1  drochner /*
    537    1.1  drochner  * Memory management for the jumbo receive ring is a pain in the
    538    1.1  drochner  * butt. We need to allocate at least 9018 bytes of space per frame,
    539    1.1  drochner  * _and_ it has to be contiguous (unless you use the extended
    540    1.1  drochner  * jumbo descriptor format). Using malloc() all the time won't
    541    1.1  drochner  * work: malloc() allocates memory in powers of two, which means we
    542    1.1  drochner  * would end up wasting a considerable amount of space by allocating
    543    1.1  drochner  * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
    544    1.1  drochner  * to do our own memory management.
    545    1.1  drochner  *
    546    1.1  drochner  * The driver needs to allocate a contiguous chunk of memory at boot
    547    1.1  drochner  * time. We then chop this up ourselves into 9K pieces and use them
    548    1.1  drochner  * as external mbuf storage.
    549    1.1  drochner  *
    550    1.1  drochner  * One issue here is how much memory to allocate. The jumbo ring has
    551    1.1  drochner  * 256 slots in it, but at 9K per slot than can consume over 2MB of
    552    1.1  drochner  * RAM. This is a bit much, especially considering we also need
    553    1.1  drochner  * RAM for the standard ring and mini ring (on the Tigon 2). To
    554    1.1  drochner  * save space, we only actually allocate enough memory for 64 slots
    555    1.1  drochner  * by default, which works out to between 500 and 600K. This can
    556    1.1  drochner  * be tuned by changing a #define in if_tireg.h.
    557    1.1  drochner  */
    558    1.1  drochner 
    559   1.77       tnn static int
    560   1.77       tnn ti_alloc_jumbo_mem(struct ti_softc *sc)
    561    1.1  drochner {
    562   1.74  christos 	char *ptr;
    563   1.74  christos 	int i;
    564  1.108   msaitoh 	struct ti_jpool_entry	*entry;
    565    1.1  drochner 	bus_dma_segment_t dmaseg;
    566    1.1  drochner 	int error, dmanseg;
    567    1.1  drochner 
    568    1.1  drochner 	/* Grab a big chunk o' storage. */
    569    1.1  drochner 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    570   1.13   thorpej 	    TI_JMEM, PAGE_SIZE, 0, &dmaseg, 1, &dmanseg,
    571    1.1  drochner 	    BUS_DMA_NOWAIT)) != 0) {
    572   1.99   msaitoh 		aprint_error_dev(sc->sc_dev,
    573   1.99   msaitoh 		    "can't allocate jumbo buffer, error = %d\n", error);
    574    1.1  drochner 		return (error);
    575    1.1  drochner 	}
    576    1.1  drochner 
    577    1.1  drochner 	if ((error = bus_dmamem_map(sc->sc_dmat, &dmaseg, dmanseg,
    578   1.74  christos 	    TI_JMEM, (void **)&sc->ti_cdata.ti_jumbo_buf,
    579  1.108   msaitoh 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    580   1.99   msaitoh 		aprint_error_dev(sc->sc_dev,
    581   1.99   msaitoh 		    "can't map jumbo buffer, error = %d\n", error);
    582    1.1  drochner 		return (error);
    583    1.1  drochner 	}
    584    1.1  drochner 
    585    1.1  drochner 	if ((error = bus_dmamap_create(sc->sc_dmat,
    586    1.1  drochner 	    TI_JMEM, 1,
    587    1.1  drochner 	    TI_JMEM, 0, BUS_DMA_NOWAIT,
    588    1.1  drochner 	    &sc->jumbo_dmamap)) != 0) {
    589   1.99   msaitoh 		aprint_error_dev(sc->sc_dev,
    590   1.99   msaitoh 		    "can't create jumbo buffer DMA map, error = %d\n", error);
    591    1.1  drochner 		return (error);
    592    1.1  drochner 	}
    593    1.1  drochner 
    594    1.1  drochner 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->jumbo_dmamap,
    595    1.1  drochner 	    sc->ti_cdata.ti_jumbo_buf, TI_JMEM, NULL,
    596    1.1  drochner 	    BUS_DMA_NOWAIT)) != 0) {
    597   1.99   msaitoh 		aprint_error_dev(sc->sc_dev,
    598   1.99   msaitoh 		    "can't load jumbo buffer DMA map, error = %d\n", error);
    599    1.1  drochner 		return (error);
    600    1.1  drochner 	}
    601    1.1  drochner 	sc->jumbo_dmaaddr = sc->jumbo_dmamap->dm_segs[0].ds_addr;
    602    1.1  drochner 
    603    1.1  drochner 	SIMPLEQ_INIT(&sc->ti_jfree_listhead);
    604    1.1  drochner 	SIMPLEQ_INIT(&sc->ti_jinuse_listhead);
    605    1.1  drochner 
    606    1.1  drochner 	/*
    607    1.1  drochner 	 * Now divide it up into 9K pieces and save the addresses
    608   1.15    bouyer 	 * in an array.
    609    1.1  drochner 	 */
    610    1.1  drochner 	ptr = sc->ti_cdata.ti_jumbo_buf;
    611    1.1  drochner 	for (i = 0; i < TI_JSLOTS; i++) {
    612   1.15    bouyer 		sc->ti_cdata.ti_jslots[i] = ptr;
    613   1.15    bouyer 		ptr += TI_JLEN;
    614   1.66     perry 		entry = malloc(sizeof(struct ti_jpool_entry),
    615    1.1  drochner 			       M_DEVBUF, M_NOWAIT);
    616    1.1  drochner 		if (entry == NULL) {
    617    1.1  drochner 			free(sc->ti_cdata.ti_jumbo_buf, M_DEVBUF);
    618    1.1  drochner 			sc->ti_cdata.ti_jumbo_buf = NULL;
    619    1.1  drochner 			printf("%s: no memory for jumbo "
    620   1.91       chs 			    "buffer queue!\n", device_xname(sc->sc_dev));
    621   1.77       tnn 			return (ENOBUFS);
    622    1.1  drochner 		}
    623    1.1  drochner 		entry->slot = i;
    624    1.1  drochner 		SIMPLEQ_INSERT_HEAD(&sc->ti_jfree_listhead, entry,
    625    1.1  drochner 				    jpool_entries);
    626    1.1  drochner 	}
    627    1.1  drochner 
    628   1.77       tnn 	return (0);
    629    1.1  drochner }
    630    1.1  drochner 
    631    1.1  drochner /*
    632    1.1  drochner  * Allocate a jumbo buffer.
    633    1.1  drochner  */
    634   1.77       tnn static void *
    635   1.77       tnn ti_jalloc(struct ti_softc *sc)
    636    1.1  drochner {
    637  1.108   msaitoh 	struct ti_jpool_entry	*entry;
    638   1.66     perry 
    639    1.1  drochner 	entry = SIMPLEQ_FIRST(&sc->ti_jfree_listhead);
    640   1.66     perry 
    641    1.1  drochner 	if (entry == NULL) {
    642   1.91       chs 		printf("%s: no free jumbo buffers\n", device_xname(sc->sc_dev));
    643   1.77       tnn 		return (NULL);
    644    1.1  drochner 	}
    645    1.1  drochner 
    646   1.48     lukem 	SIMPLEQ_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
    647    1.1  drochner 	SIMPLEQ_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
    648   1.77       tnn 
    649   1.77       tnn 	return (sc->ti_cdata.ti_jslots[entry->slot]);
    650    1.1  drochner }
    651    1.1  drochner 
    652    1.1  drochner /*
    653    1.1  drochner  * Release a jumbo buffer.
    654    1.1  drochner  */
    655   1.77       tnn static void
    656   1.77       tnn ti_jfree(struct mbuf *m, void *tbuf, size_t size, void *arg)
    657    1.1  drochner {
    658    1.1  drochner 	struct ti_softc		*sc;
    659  1.108   msaitoh 	int			i, s;
    660  1.108   msaitoh 	struct ti_jpool_entry	*entry;
    661    1.1  drochner 
    662    1.1  drochner 	/* Extract the softc struct pointer. */
    663   1.15    bouyer 	sc = (struct ti_softc *)arg;
    664    1.1  drochner 
    665    1.1  drochner 	if (sc == NULL)
    666   1.15    bouyer 		panic("ti_jfree: didn't get softc pointer!");
    667    1.1  drochner 
    668    1.1  drochner 	/* calculate the slot this buffer belongs to */
    669    1.1  drochner 
    670   1.74  christos 	i = ((char *)tbuf
    671   1.74  christos 	     - (char *)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
    672    1.1  drochner 
    673    1.1  drochner 	if ((i < 0) || (i >= TI_JSLOTS))
    674    1.1  drochner 		panic("ti_jfree: asked to free buffer that we don't manage!");
    675   1.47   thorpej 
    676   1.47   thorpej 	s = splvm();
    677   1.15    bouyer 	entry = SIMPLEQ_FIRST(&sc->ti_jinuse_listhead);
    678   1.15    bouyer 	if (entry == NULL)
    679   1.15    bouyer 		panic("ti_jfree: buffer not in use!");
    680   1.15    bouyer 	entry->slot = i;
    681   1.48     lukem 	SIMPLEQ_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
    682   1.48     lukem 	SIMPLEQ_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
    683    1.1  drochner 
    684   1.47   thorpej 	if (__predict_true(m != NULL))
    685   1.79        ad 		pool_cache_put(mb_cache, m);
    686   1.47   thorpej 	splx(s);
    687    1.1  drochner }
    688    1.1  drochner 
    689    1.1  drochner 
    690    1.1  drochner /*
    691  1.103  dholland  * Initialize a standard receive ring descriptor.
    692    1.1  drochner  */
    693   1.77       tnn static int
    694   1.77       tnn ti_newbuf_std(struct ti_softc *sc, int i, struct mbuf *m, bus_dmamap_t dmamap)
    695    1.1  drochner {
    696    1.1  drochner 	struct mbuf		*m_new = NULL;
    697    1.1  drochner 	struct ti_rx_desc	*r;
    698    1.1  drochner 	int error;
    699    1.1  drochner 
    700    1.1  drochner 	if (dmamap == NULL) {
    701    1.1  drochner 		/* if (m) panic() */
    702    1.1  drochner 
    703    1.1  drochner 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    704    1.1  drochner 					       MCLBYTES, 0, BUS_DMA_NOWAIT,
    705    1.1  drochner 					       &dmamap)) != 0) {
    706   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    707   1.99   msaitoh 			    "can't create recv map, error = %d\n", error);
    708   1.77       tnn 			return (ENOMEM);
    709    1.1  drochner 		}
    710    1.1  drochner 	}
    711    1.1  drochner 	sc->std_dmamap[i] = dmamap;
    712    1.1  drochner 
    713    1.1  drochner 	if (m == NULL) {
    714    1.1  drochner 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    715    1.1  drochner 		if (m_new == NULL) {
    716   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    717   1.99   msaitoh 			    "mbuf allocation failed -- packet dropped!\n");
    718   1.77       tnn 			return (ENOBUFS);
    719    1.1  drochner 		}
    720    1.1  drochner 
    721    1.1  drochner 		MCLGET(m_new, M_DONTWAIT);
    722    1.1  drochner 		if (!(m_new->m_flags & M_EXT)) {
    723   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    724   1.99   msaitoh 			    "cluster allocation failed -- packet dropped!\n");
    725    1.1  drochner 			m_freem(m_new);
    726   1.77       tnn 			return (ENOBUFS);
    727    1.1  drochner 		}
    728    1.1  drochner 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    729    1.1  drochner 		m_adj(m_new, ETHER_ALIGN);
    730    1.1  drochner 
    731    1.1  drochner 		if ((error = bus_dmamap_load(sc->sc_dmat, dmamap,
    732   1.74  christos 				mtod(m_new, void *), m_new->m_len, NULL,
    733  1.108   msaitoh 				BUS_DMA_READ | BUS_DMA_NOWAIT)) != 0) {
    734   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    735   1.99   msaitoh 			    "can't load recv map, error = %d\n", error);
    736   1.94  christos 			m_freem(m_new);
    737    1.1  drochner 			return (ENOMEM);
    738    1.1  drochner 		}
    739    1.1  drochner 	} else {
    740    1.1  drochner 		m_new = m;
    741    1.1  drochner 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    742    1.1  drochner 		m_new->m_data = m_new->m_ext.ext_buf;
    743    1.1  drochner 		m_adj(m_new, ETHER_ALIGN);
    744    1.1  drochner 
    745    1.1  drochner 		/* reuse the dmamap */
    746    1.1  drochner 	}
    747    1.1  drochner 
    748    1.1  drochner 	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
    749    1.1  drochner 	r = &sc->ti_rdata->ti_rx_std_ring[i];
    750    1.1  drochner 	TI_HOSTADDR(r->ti_addr) = dmamap->dm_segs[0].ds_addr;
    751    1.1  drochner 	r->ti_type = TI_BDTYPE_RECV_BD;
    752    1.1  drochner 	r->ti_flags = 0;
    753   1.67      yamt 	if (sc->ethercom.ec_if.if_capenable & IFCAP_CSUM_IPv4_Rx)
    754   1.21   thorpej 		r->ti_flags |= TI_BDFLAG_IP_CKSUM;
    755   1.21   thorpej 	if (sc->ethercom.ec_if.if_capenable &
    756   1.67      yamt 	    (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
    757   1.21   thorpej 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
    758    1.1  drochner 	r->ti_len = m_new->m_len; /* == ds_len */
    759    1.1  drochner 	r->ti_idx = i;
    760    1.1  drochner 
    761   1.77       tnn 	return (0);
    762    1.1  drochner }
    763    1.1  drochner 
    764    1.1  drochner /*
    765    1.1  drochner  * Intialize a mini receive ring descriptor. This only applies to
    766    1.1  drochner  * the Tigon 2.
    767    1.1  drochner  */
    768   1.77       tnn static int
    769   1.77       tnn ti_newbuf_mini(struct ti_softc *sc, int i, struct mbuf *m, bus_dmamap_t dmamap)
    770    1.1  drochner {
    771    1.1  drochner 	struct mbuf		*m_new = NULL;
    772    1.1  drochner 	struct ti_rx_desc	*r;
    773    1.1  drochner 	int error;
    774    1.1  drochner 
    775    1.1  drochner 	if (dmamap == NULL) {
    776    1.1  drochner 		/* if (m) panic() */
    777    1.1  drochner 
    778    1.1  drochner 		if ((error = bus_dmamap_create(sc->sc_dmat, MHLEN, 1,
    779    1.1  drochner 					       MHLEN, 0, BUS_DMA_NOWAIT,
    780    1.1  drochner 					       &dmamap)) != 0) {
    781   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    782   1.99   msaitoh 			    "can't create recv map, error = %d\n", error);
    783   1.77       tnn 			return (ENOMEM);
    784    1.1  drochner 		}
    785    1.1  drochner 	}
    786    1.1  drochner 	sc->mini_dmamap[i] = dmamap;
    787    1.1  drochner 
    788    1.1  drochner 	if (m == NULL) {
    789    1.1  drochner 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    790    1.1  drochner 		if (m_new == NULL) {
    791   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    792   1.99   msaitoh 			    "mbuf allocation failed -- packet dropped!\n");
    793   1.77       tnn 			return (ENOBUFS);
    794    1.1  drochner 		}
    795    1.1  drochner 		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
    796    1.1  drochner 		m_adj(m_new, ETHER_ALIGN);
    797    1.1  drochner 
    798    1.1  drochner 		if ((error = bus_dmamap_load(sc->sc_dmat, dmamap,
    799   1.74  christos 				mtod(m_new, void *), m_new->m_len, NULL,
    800  1.108   msaitoh 				BUS_DMA_READ | BUS_DMA_NOWAIT)) != 0) {
    801   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    802   1.99   msaitoh 			    "can't load recv map, error = %d\n", error);
    803   1.95      maxv 			m_freem(m_new);
    804    1.1  drochner 			return (ENOMEM);
    805    1.1  drochner 		}
    806    1.1  drochner 	} else {
    807    1.1  drochner 		m_new = m;
    808    1.1  drochner 		m_new->m_data = m_new->m_pktdat;
    809    1.1  drochner 		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
    810    1.1  drochner 		m_adj(m_new, ETHER_ALIGN);
    811    1.1  drochner 
    812    1.1  drochner 		/* reuse the dmamap */
    813    1.1  drochner 	}
    814    1.1  drochner 
    815    1.1  drochner 	r = &sc->ti_rdata->ti_rx_mini_ring[i];
    816    1.1  drochner 	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
    817    1.1  drochner 	TI_HOSTADDR(r->ti_addr) = dmamap->dm_segs[0].ds_addr;
    818    1.1  drochner 	r->ti_type = TI_BDTYPE_RECV_BD;
    819    1.1  drochner 	r->ti_flags = TI_BDFLAG_MINI_RING;
    820   1.67      yamt 	if (sc->ethercom.ec_if.if_capenable & IFCAP_CSUM_IPv4_Rx)
    821   1.21   thorpej 		r->ti_flags |= TI_BDFLAG_IP_CKSUM;
    822   1.21   thorpej 	if (sc->ethercom.ec_if.if_capenable &
    823   1.67      yamt 	    (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
    824   1.21   thorpej 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
    825    1.1  drochner 	r->ti_len = m_new->m_len; /* == ds_len */
    826    1.1  drochner 	r->ti_idx = i;
    827    1.1  drochner 
    828   1.77       tnn 	return (0);
    829    1.1  drochner }
    830    1.1  drochner 
    831    1.1  drochner /*
    832    1.1  drochner  * Initialize a jumbo receive ring descriptor. This allocates
    833    1.1  drochner  * a jumbo buffer from the pool managed internally by the driver.
    834    1.1  drochner  */
    835   1.77       tnn static int
    836   1.77       tnn ti_newbuf_jumbo(struct ti_softc *sc, int i, struct mbuf *m)
    837    1.1  drochner {
    838    1.1  drochner 	struct mbuf		*m_new = NULL;
    839    1.1  drochner 	struct ti_rx_desc	*r;
    840    1.1  drochner 
    841    1.1  drochner 	if (m == NULL) {
    842  1.108   msaitoh 		void *		tbuf = NULL;
    843    1.1  drochner 
    844    1.1  drochner 		/* Allocate the mbuf. */
    845    1.1  drochner 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    846    1.1  drochner 		if (m_new == NULL) {
    847   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    848   1.99   msaitoh 			    "mbuf allocation failed -- packet dropped!\n");
    849   1.77       tnn 			return (ENOBUFS);
    850    1.1  drochner 		}
    851    1.1  drochner 
    852    1.1  drochner 		/* Allocate the jumbo buffer */
    853   1.68  christos 		tbuf = ti_jalloc(sc);
    854   1.68  christos 		if (tbuf == NULL) {
    855    1.1  drochner 			m_freem(m_new);
    856   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
    857   1.99   msaitoh 			    "jumbo allocation failed -- packet dropped!\n");
    858   1.77       tnn 			return (ENOBUFS);
    859    1.1  drochner 		}
    860    1.1  drochner 
    861    1.1  drochner 		/* Attach the buffer to the mbuf. */
    862   1.68  christos 		MEXTADD(m_new, tbuf, ETHER_MAX_LEN_JUMBO,
    863   1.46   thorpej 		    M_DEVBUF, ti_jfree, sc);
    864   1.62      yamt 		m_new->m_flags |= M_EXT_RW;
    865   1.46   thorpej 		m_new->m_len = m_new->m_pkthdr.len = ETHER_MAX_LEN_JUMBO;
    866    1.1  drochner 	} else {
    867    1.1  drochner 		m_new = m;
    868    1.1  drochner 		m_new->m_data = m_new->m_ext.ext_buf;
    869   1.22   thorpej 		m_new->m_ext.ext_size = ETHER_MAX_LEN_JUMBO;
    870    1.1  drochner 	}
    871    1.1  drochner 
    872    1.1  drochner 	m_adj(m_new, ETHER_ALIGN);
    873    1.1  drochner 	/* Set up the descriptor. */
    874    1.1  drochner 	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
    875    1.1  drochner 	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
    876    1.1  drochner 	TI_HOSTADDR(r->ti_addr) = sc->jumbo_dmaaddr +
    877   1.74  christos 		(mtod(m_new, char *) - (char *)sc->ti_cdata.ti_jumbo_buf);
    878    1.1  drochner 	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
    879    1.1  drochner 	r->ti_flags = TI_BDFLAG_JUMBO_RING;
    880   1.67      yamt 	if (sc->ethercom.ec_if.if_capenable & IFCAP_CSUM_IPv4_Rx)
    881   1.21   thorpej 		r->ti_flags |= TI_BDFLAG_IP_CKSUM;
    882   1.21   thorpej 	if (sc->ethercom.ec_if.if_capenable &
    883   1.67      yamt 	    (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
    884   1.21   thorpej 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
    885    1.1  drochner 	r->ti_len = m_new->m_len;
    886    1.1  drochner 	r->ti_idx = i;
    887    1.1  drochner 
    888   1.77       tnn 	return (0);
    889    1.1  drochner }
    890    1.1  drochner 
    891    1.1  drochner /*
    892    1.1  drochner  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
    893    1.1  drochner  * that's 1MB or memory, which is a lot. For now, we fill only the first
    894    1.1  drochner  * 256 ring entries and hope that our CPU is fast enough to keep up with
    895    1.1  drochner  * the NIC.
    896    1.1  drochner  */
    897   1.77       tnn static int
    898   1.77       tnn ti_init_rx_ring_std(struct ti_softc *sc)
    899    1.1  drochner {
    900    1.8  augustss 	int		i;
    901    1.1  drochner 	struct ti_cmd_desc	cmd;
    902    1.1  drochner 
    903    1.1  drochner 	for (i = 0; i < TI_SSLOTS; i++) {
    904    1.1  drochner 		if (ti_newbuf_std(sc, i, NULL, 0) == ENOBUFS)
    905   1.77       tnn 			return (ENOBUFS);
    906    1.1  drochner 	};
    907    1.1  drochner 
    908    1.1  drochner 	TI_UPDATE_STDPROD(sc, i - 1);
    909    1.1  drochner 	sc->ti_std = i - 1;
    910    1.1  drochner 
    911   1.77       tnn 	return (0);
    912    1.1  drochner }
    913    1.1  drochner 
    914   1.77       tnn static void
    915   1.77       tnn ti_free_rx_ring_std(struct ti_softc *sc)
    916    1.1  drochner {
    917    1.8  augustss 	int		i;
    918    1.1  drochner 
    919    1.1  drochner 	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
    920    1.1  drochner 		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
    921    1.1  drochner 			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
    922    1.1  drochner 			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
    923    1.1  drochner 
    924    1.1  drochner 			/* if (sc->std_dmamap[i] == 0) panic() */
    925    1.1  drochner 			bus_dmamap_destroy(sc->sc_dmat, sc->std_dmamap[i]);
    926    1.1  drochner 			sc->std_dmamap[i] = 0;
    927    1.1  drochner 		}
    928   1.39   thorpej 		memset((char *)&sc->ti_rdata->ti_rx_std_ring[i], 0,
    929    1.1  drochner 		    sizeof(struct ti_rx_desc));
    930    1.1  drochner 	}
    931    1.1  drochner 
    932    1.1  drochner 	return;
    933    1.1  drochner }
    934    1.1  drochner 
    935   1.77       tnn static int
    936   1.77       tnn ti_init_rx_ring_jumbo(struct ti_softc *sc)
    937    1.1  drochner {
    938    1.8  augustss 	int		i;
    939    1.1  drochner 	struct ti_cmd_desc	cmd;
    940    1.1  drochner 
    941   1.61        he 	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
    942    1.1  drochner 		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
    943   1.77       tnn 			return (ENOBUFS);
    944    1.1  drochner 	};
    945    1.1  drochner 
    946    1.1  drochner 	TI_UPDATE_JUMBOPROD(sc, i - 1);
    947    1.1  drochner 	sc->ti_jumbo = i - 1;
    948    1.1  drochner 
    949   1.77       tnn 	return (0);
    950    1.1  drochner }
    951    1.1  drochner 
    952   1.77       tnn static void
    953   1.77       tnn ti_free_rx_ring_jumbo(struct ti_softc *sc)
    954    1.1  drochner {
    955    1.8  augustss 	int		i;
    956    1.1  drochner 
    957    1.1  drochner 	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
    958    1.1  drochner 		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
    959    1.1  drochner 			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
    960    1.1  drochner 			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
    961    1.1  drochner 		}
    962   1.39   thorpej 		memset((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i], 0,
    963    1.1  drochner 		    sizeof(struct ti_rx_desc));
    964    1.1  drochner 	}
    965    1.1  drochner 
    966    1.1  drochner 	return;
    967    1.1  drochner }
    968    1.1  drochner 
    969   1.77       tnn static int
    970   1.77       tnn ti_init_rx_ring_mini(struct ti_softc *sc)
    971    1.1  drochner {
    972    1.8  augustss 	int		i;
    973    1.1  drochner 
    974    1.1  drochner 	for (i = 0; i < TI_MSLOTS; i++) {
    975    1.1  drochner 		if (ti_newbuf_mini(sc, i, NULL, 0) == ENOBUFS)
    976   1.77       tnn 			return (ENOBUFS);
    977    1.1  drochner 	};
    978    1.1  drochner 
    979    1.1  drochner 	TI_UPDATE_MINIPROD(sc, i - 1);
    980    1.1  drochner 	sc->ti_mini = i - 1;
    981    1.1  drochner 
    982   1.77       tnn 	return (0);
    983    1.1  drochner }
    984    1.1  drochner 
    985   1.77       tnn static void
    986   1.77       tnn ti_free_rx_ring_mini(struct ti_softc *sc)
    987    1.1  drochner {
    988    1.8  augustss 	int		i;
    989    1.1  drochner 
    990    1.1  drochner 	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
    991    1.1  drochner 		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
    992    1.1  drochner 			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
    993    1.1  drochner 			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
    994    1.1  drochner 
    995    1.1  drochner 			/* if (sc->mini_dmamap[i] == 0) panic() */
    996    1.1  drochner 			bus_dmamap_destroy(sc->sc_dmat, sc->mini_dmamap[i]);
    997    1.1  drochner 			sc->mini_dmamap[i] = 0;
    998    1.1  drochner 		}
    999   1.39   thorpej 		memset((char *)&sc->ti_rdata->ti_rx_mini_ring[i], 0,
   1000    1.1  drochner 		    sizeof(struct ti_rx_desc));
   1001    1.1  drochner 	}
   1002    1.1  drochner 
   1003    1.1  drochner 	return;
   1004    1.1  drochner }
   1005    1.1  drochner 
   1006   1.77       tnn static void
   1007   1.77       tnn ti_free_tx_ring(struct ti_softc *sc)
   1008    1.1  drochner {
   1009    1.8  augustss 	int		i;
   1010    1.1  drochner 	struct txdmamap_pool_entry *dma;
   1011    1.1  drochner 
   1012    1.1  drochner 	for (i = 0; i < TI_TX_RING_CNT; i++) {
   1013    1.1  drochner 		if (sc->ti_cdata.ti_tx_chain[i] != NULL) {
   1014    1.1  drochner 			m_freem(sc->ti_cdata.ti_tx_chain[i]);
   1015    1.1  drochner 			sc->ti_cdata.ti_tx_chain[i] = NULL;
   1016    1.1  drochner 
   1017    1.1  drochner 			/* if (sc->txdma[i] == 0) panic() */
   1018    1.1  drochner 			SIMPLEQ_INSERT_HEAD(&sc->txdma_list, sc->txdma[i],
   1019    1.1  drochner 					    link);
   1020    1.1  drochner 			sc->txdma[i] = 0;
   1021    1.1  drochner 		}
   1022   1.39   thorpej 		memset((char *)&sc->ti_rdata->ti_tx_ring[i], 0,
   1023    1.1  drochner 		    sizeof(struct ti_tx_desc));
   1024    1.1  drochner 	}
   1025    1.1  drochner 
   1026    1.1  drochner 	while ((dma = SIMPLEQ_FIRST(&sc->txdma_list))) {
   1027   1.48     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->txdma_list, link);
   1028    1.1  drochner 		bus_dmamap_destroy(sc->sc_dmat, dma->dmamap);
   1029    1.1  drochner 		free(dma, M_DEVBUF);
   1030    1.1  drochner 	}
   1031    1.1  drochner 
   1032    1.1  drochner 	return;
   1033    1.1  drochner }
   1034    1.1  drochner 
   1035   1.77       tnn static int
   1036   1.77       tnn ti_init_tx_ring(struct ti_softc *sc)
   1037    1.1  drochner {
   1038    1.1  drochner 	int i, error;
   1039    1.1  drochner 	bus_dmamap_t dmamap;
   1040    1.1  drochner 	struct txdmamap_pool_entry *dma;
   1041    1.1  drochner 
   1042    1.1  drochner 	sc->ti_txcnt = 0;
   1043    1.1  drochner 	sc->ti_tx_saved_considx = 0;
   1044    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
   1045    1.1  drochner 
   1046    1.1  drochner 	SIMPLEQ_INIT(&sc->txdma_list);
   1047    1.1  drochner 	for (i = 0; i < TI_RSLOTS; i++) {
   1048    1.1  drochner 		/* I've seen mbufs with 30 fragments. */
   1049   1.99   msaitoh 		if ((error = bus_dmamap_create(sc->sc_dmat,
   1050   1.99   msaitoh 			    ETHER_MAX_LEN_JUMBO, 40, ETHER_MAX_LEN_JUMBO, 0,
   1051   1.99   msaitoh 			    BUS_DMA_NOWAIT, &dmamap)) != 0) {
   1052   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
   1053   1.99   msaitoh 			    "can't create tx map, error = %d\n", error);
   1054   1.77       tnn 			return (ENOMEM);
   1055    1.1  drochner 		}
   1056    1.1  drochner 		dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT);
   1057    1.1  drochner 		if (!dma) {
   1058   1.99   msaitoh 			aprint_error_dev(sc->sc_dev,
   1059   1.99   msaitoh 			    "can't alloc txdmamap_pool_entry\n");
   1060    1.1  drochner 			bus_dmamap_destroy(sc->sc_dmat, dmamap);
   1061    1.1  drochner 			return (ENOMEM);
   1062    1.1  drochner 		}
   1063    1.1  drochner 		dma->dmamap = dmamap;
   1064    1.1  drochner 		SIMPLEQ_INSERT_HEAD(&sc->txdma_list, dma, link);
   1065    1.1  drochner 	}
   1066    1.1  drochner 
   1067   1.77       tnn 	return (0);
   1068    1.1  drochner }
   1069    1.1  drochner 
   1070    1.1  drochner /*
   1071    1.1  drochner  * The Tigon 2 firmware has a new way to add/delete multicast addresses,
   1072    1.1  drochner  * but we have to support the old way too so that Tigon 1 cards will
   1073    1.1  drochner  * work.
   1074    1.1  drochner  */
   1075   1.77       tnn static void
   1076   1.77       tnn ti_add_mcast(struct ti_softc *sc, struct ether_addr *addr)
   1077    1.1  drochner {
   1078    1.1  drochner 	struct ti_cmd_desc	cmd;
   1079  1.108   msaitoh 	uint16_t		*m;
   1080  1.108   msaitoh 	uint32_t		ext[2] = {0, 0};
   1081    1.1  drochner 
   1082  1.108   msaitoh 	m = (uint16_t *)&addr->ether_addr_octet[0]; /* XXX */
   1083    1.1  drochner 
   1084   1.77       tnn 	switch (sc->ti_hwrev) {
   1085    1.1  drochner 	case TI_HWREV_TIGON:
   1086    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
   1087    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
   1088    1.1  drochner 		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
   1089    1.1  drochner 		break;
   1090    1.1  drochner 	case TI_HWREV_TIGON_II:
   1091    1.1  drochner 		ext[0] = htons(m[0]);
   1092    1.1  drochner 		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
   1093   1.74  christos 		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (void *)&ext, 2);
   1094    1.1  drochner 		break;
   1095    1.1  drochner 	default:
   1096   1.91       chs 		printf("%s: unknown hwrev\n", device_xname(sc->sc_dev));
   1097    1.1  drochner 		break;
   1098    1.1  drochner 	}
   1099    1.1  drochner 
   1100    1.1  drochner 	return;
   1101    1.1  drochner }
   1102    1.1  drochner 
   1103   1.77       tnn static void
   1104   1.77       tnn ti_del_mcast(struct ti_softc *sc, struct ether_addr *addr)
   1105    1.1  drochner {
   1106    1.1  drochner 	struct ti_cmd_desc	cmd;
   1107  1.108   msaitoh 	uint16_t		*m;
   1108  1.108   msaitoh 	uint32_t		ext[2] = {0, 0};
   1109    1.1  drochner 
   1110  1.108   msaitoh 	m = (uint16_t *)&addr->ether_addr_octet[0]; /* XXX */
   1111    1.1  drochner 
   1112   1.77       tnn 	switch (sc->ti_hwrev) {
   1113    1.1  drochner 	case TI_HWREV_TIGON:
   1114    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
   1115    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
   1116    1.1  drochner 		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
   1117    1.1  drochner 		break;
   1118    1.1  drochner 	case TI_HWREV_TIGON_II:
   1119    1.1  drochner 		ext[0] = htons(m[0]);
   1120    1.1  drochner 		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
   1121   1.74  christos 		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (void *)&ext, 2);
   1122    1.1  drochner 		break;
   1123    1.1  drochner 	default:
   1124   1.91       chs 		printf("%s: unknown hwrev\n", device_xname(sc->sc_dev));
   1125    1.1  drochner 		break;
   1126    1.1  drochner 	}
   1127    1.1  drochner 
   1128    1.1  drochner 	return;
   1129    1.1  drochner }
   1130    1.1  drochner 
   1131    1.1  drochner /*
   1132    1.1  drochner  * Configure the Tigon's multicast address filter.
   1133    1.1  drochner  *
   1134    1.1  drochner  * The actual multicast table management is a bit of a pain, thanks to
   1135    1.1  drochner  * slight brain damage on the part of both Alteon and us. With our
   1136    1.1  drochner  * multicast code, we are only alerted when the multicast address table
   1137    1.1  drochner  * changes and at that point we only have the current list of addresses:
   1138    1.1  drochner  * we only know the current state, not the previous state, so we don't
   1139    1.1  drochner  * actually know what addresses were removed or added. The firmware has
   1140    1.1  drochner  * state, but we can't get our grubby mits on it, and there is no 'delete
   1141    1.1  drochner  * all multicast addresses' command. Hence, we have to maintain our own
   1142    1.1  drochner  * state so we know what addresses have been programmed into the NIC at
   1143    1.1  drochner  * any given time.
   1144    1.1  drochner  */
   1145   1.77       tnn static void
   1146   1.77       tnn ti_setmulti(struct ti_softc *sc)
   1147    1.1  drochner {
   1148  1.109   msaitoh 	struct ethercom		*ec = &sc->ethercom;
   1149  1.109   msaitoh 	struct ifnet		*ifp = &ec->ec_if;
   1150    1.1  drochner 	struct ti_cmd_desc	cmd;
   1151    1.1  drochner 	struct ti_mc_entry	*mc;
   1152  1.108   msaitoh 	uint32_t		intrs;
   1153  1.108   msaitoh 	struct ether_multi	*enm;
   1154  1.108   msaitoh 	struct ether_multistep	step;
   1155    1.1  drochner 
   1156    1.1  drochner 	/* Disable interrupts. */
   1157    1.1  drochner 	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
   1158    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   1159    1.1  drochner 
   1160    1.1  drochner 	/* First, zot all the existing filters. */
   1161   1.20     enami 	while ((mc = SIMPLEQ_FIRST(&sc->ti_mc_listhead)) != NULL) {
   1162    1.1  drochner 		ti_del_mcast(sc, &mc->mc_addr);
   1163   1.48     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
   1164    1.1  drochner 		free(mc, M_DEVBUF);
   1165    1.1  drochner 	}
   1166    1.1  drochner 
   1167   1.20     enami 	/*
   1168   1.20     enami 	 * Remember all multicast addresses so that we can delete them
   1169   1.20     enami 	 * later.  Punt if there is a range of addresses or memory shortage.
   1170   1.20     enami 	 */
   1171  1.110   msaitoh 	ETHER_LOCK(ec);
   1172  1.109   msaitoh 	ETHER_FIRST_MULTI(step, ec, enm);
   1173    1.1  drochner 	while (enm != NULL) {
   1174   1.20     enami 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1175  1.110   msaitoh 		    ETHER_ADDR_LEN) != 0) {
   1176  1.110   msaitoh 			ETHER_UNLOCK(ec);
   1177   1.20     enami 			goto allmulti;
   1178  1.110   msaitoh 		}
   1179   1.20     enami 		if ((mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF,
   1180  1.110   msaitoh 		    M_NOWAIT)) == NULL) {
   1181  1.110   msaitoh 			ETHER_UNLOCK(ec);
   1182   1.20     enami 			goto allmulti;
   1183  1.110   msaitoh 		}
   1184   1.20     enami 		memcpy(&mc->mc_addr, enm->enm_addrlo, ETHER_ADDR_LEN);
   1185    1.1  drochner 		SIMPLEQ_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
   1186    1.1  drochner 		ETHER_NEXT_MULTI(step, enm);
   1187    1.1  drochner 	}
   1188  1.110   msaitoh 	ETHER_UNLOCK(ec);
   1189    1.1  drochner 
   1190   1.20     enami 	/* Accept only programmed multicast addresses */
   1191   1.20     enami 	ifp->if_flags &= ~IFF_ALLMULTI;
   1192   1.20     enami 	TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
   1193   1.20     enami 
   1194   1.20     enami 	/* Now program new ones. */
   1195   1.48     lukem 	SIMPLEQ_FOREACH(mc, &sc->ti_mc_listhead, mc_entries)
   1196   1.20     enami 		ti_add_mcast(sc, &mc->mc_addr);
   1197   1.20     enami 
   1198    1.1  drochner 	/* Re-enable interrupts. */
   1199    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
   1200    1.1  drochner 
   1201    1.1  drochner 	return;
   1202   1.20     enami 
   1203   1.20     enami allmulti:
   1204   1.20     enami 	/* No need to keep individual multicast addresses */
   1205   1.20     enami 	while ((mc = SIMPLEQ_FIRST(&sc->ti_mc_listhead)) != NULL) {
   1206   1.48     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
   1207   1.20     enami 		free(mc, M_DEVBUF);
   1208   1.20     enami 	}
   1209   1.20     enami 
   1210   1.20     enami 	/* Accept all multicast addresses */
   1211   1.20     enami 	ifp->if_flags |= IFF_ALLMULTI;
   1212   1.20     enami 	TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
   1213   1.20     enami 
   1214   1.20     enami 	/* Re-enable interrupts. */
   1215   1.20     enami 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
   1216    1.1  drochner }
   1217    1.1  drochner 
   1218    1.1  drochner /*
   1219    1.1  drochner  * Check to see if the BIOS has configured us for a 64 bit slot when
   1220    1.1  drochner  * we aren't actually in one. If we detect this condition, we can work
   1221    1.1  drochner  * around it on the Tigon 2 by setting a bit in the PCI state register,
   1222    1.1  drochner  * but for the Tigon 1 we must give up and abort the interface attach.
   1223    1.1  drochner  */
   1224   1.77       tnn static int
   1225   1.77       tnn ti_64bitslot_war(struct ti_softc *sc)
   1226    1.1  drochner {
   1227    1.1  drochner 	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
   1228    1.1  drochner 		CSR_WRITE_4(sc, 0x600, 0);
   1229    1.1  drochner 		CSR_WRITE_4(sc, 0x604, 0);
   1230    1.1  drochner 		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
   1231    1.1  drochner 		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
   1232    1.1  drochner 			if (sc->ti_hwrev == TI_HWREV_TIGON)
   1233   1.77       tnn 				return (EINVAL);
   1234    1.1  drochner 			else {
   1235    1.1  drochner 				TI_SETBIT(sc, TI_PCI_STATE,
   1236    1.1  drochner 				    TI_PCISTATE_32BIT_BUS);
   1237   1.77       tnn 				return (0);
   1238    1.1  drochner 			}
   1239    1.1  drochner 		}
   1240    1.1  drochner 	}
   1241    1.1  drochner 
   1242   1.77       tnn 	return (0);
   1243    1.1  drochner }
   1244    1.1  drochner 
   1245    1.1  drochner /*
   1246    1.1  drochner  * Do endian, PCI and DMA initialization. Also check the on-board ROM
   1247    1.1  drochner  * self-test results.
   1248    1.1  drochner  */
   1249   1.77       tnn static int
   1250   1.77       tnn ti_chipinit(struct ti_softc *sc)
   1251    1.1  drochner {
   1252  1.108   msaitoh 	uint32_t	cacheline;
   1253  1.108   msaitoh 	uint32_t	pci_writemax = 0;
   1254  1.108   msaitoh 	uint32_t	rev;
   1255    1.1  drochner 
   1256    1.1  drochner 	/* Initialize link to down state. */
   1257    1.1  drochner 	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
   1258    1.1  drochner 
   1259    1.1  drochner 	/* Set endianness before we access any non-PCI registers. */
   1260    1.1  drochner #if BYTE_ORDER == BIG_ENDIAN
   1261    1.1  drochner 	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
   1262    1.1  drochner 	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
   1263    1.1  drochner #else
   1264    1.1  drochner 	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
   1265    1.1  drochner 	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
   1266    1.1  drochner #endif
   1267    1.1  drochner 
   1268    1.1  drochner 	/* Check the ROM failed bit to see if self-tests passed. */
   1269    1.1  drochner 	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
   1270    1.1  drochner 		printf("%s: board self-diagnostics failed!\n",
   1271   1.91       chs 		       device_xname(sc->sc_dev));
   1272   1.77       tnn 		return (ENODEV);
   1273    1.1  drochner 	}
   1274    1.1  drochner 
   1275    1.1  drochner 	/* Halt the CPU. */
   1276    1.1  drochner 	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
   1277    1.1  drochner 
   1278    1.1  drochner 	/* Figure out the hardware revision. */
   1279   1.59    bouyer 	rev = CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK;
   1280   1.77       tnn 	switch (rev) {
   1281    1.1  drochner 	case TI_REV_TIGON_I:
   1282    1.1  drochner 		sc->ti_hwrev = TI_HWREV_TIGON;
   1283    1.1  drochner 		break;
   1284    1.1  drochner 	case TI_REV_TIGON_II:
   1285    1.1  drochner 		sc->ti_hwrev = TI_HWREV_TIGON_II;
   1286    1.1  drochner 		break;
   1287    1.1  drochner 	default:
   1288   1.59    bouyer 		printf("%s: unsupported chip revision 0x%x\n",
   1289   1.91       chs 		    device_xname(sc->sc_dev), rev);
   1290   1.77       tnn 		return (ENODEV);
   1291    1.1  drochner 	}
   1292    1.1  drochner 
   1293    1.1  drochner 	/* Do special setup for Tigon 2. */
   1294    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
   1295    1.1  drochner 		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
   1296    1.1  drochner 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_256K);
   1297    1.1  drochner 		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
   1298    1.1  drochner 	}
   1299    1.1  drochner 
   1300    1.1  drochner 	/* Set up the PCI state register. */
   1301  1.108   msaitoh 	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD | TI_PCI_WRITE_CMD);
   1302    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
   1303    1.1  drochner 		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
   1304    1.1  drochner 	}
   1305    1.1  drochner 
   1306    1.1  drochner 	/* Clear the read/write max DMA parameters. */
   1307  1.108   msaitoh 	TI_CLRBIT(sc, TI_PCI_STATE,
   1308  1.108   msaitoh 	    (TI_PCISTATE_WRITE_MAXDMA | TI_PCISTATE_READ_MAXDMA));
   1309    1.1  drochner 
   1310    1.1  drochner 	/* Get cache line size. */
   1311    1.1  drochner 	cacheline = PCI_CACHELINE(CSR_READ_4(sc, PCI_BHLC_REG));
   1312    1.1  drochner 
   1313    1.1  drochner 	/*
   1314    1.1  drochner 	 * If the system has set enabled the PCI memory write
   1315    1.1  drochner 	 * and invalidate command in the command register, set
   1316    1.1  drochner 	 * the write max parameter accordingly. This is necessary
   1317    1.1  drochner 	 * to use MWI with the Tigon 2.
   1318    1.1  drochner 	 */
   1319    1.1  drochner 	if (CSR_READ_4(sc, PCI_COMMAND_STATUS_REG)
   1320    1.1  drochner 	    & PCI_COMMAND_INVALIDATE_ENABLE) {
   1321   1.77       tnn 		switch (cacheline) {
   1322    1.1  drochner 		case 1:
   1323    1.1  drochner 		case 4:
   1324    1.1  drochner 		case 8:
   1325    1.1  drochner 		case 16:
   1326    1.1  drochner 		case 32:
   1327    1.1  drochner 		case 64:
   1328    1.1  drochner 			break;
   1329    1.1  drochner 		default:
   1330    1.1  drochner 		/* Disable PCI memory write and invalidate. */
   1331    1.1  drochner 			if (bootverbose)
   1332    1.1  drochner 				printf("%s: cache line size %d not "
   1333    1.1  drochner 				    "supported; disabling PCI MWI\n",
   1334   1.91       chs 				    device_xname(sc->sc_dev), cacheline);
   1335    1.1  drochner 			CSR_WRITE_4(sc, PCI_COMMAND_STATUS_REG,
   1336    1.1  drochner 				    CSR_READ_4(sc, PCI_COMMAND_STATUS_REG)
   1337    1.1  drochner 				    & ~PCI_COMMAND_INVALIDATE_ENABLE);
   1338    1.1  drochner 			break;
   1339    1.1  drochner 		}
   1340    1.1  drochner 	}
   1341    1.1  drochner 
   1342    1.1  drochner #ifdef __brokenalpha__
   1343    1.1  drochner 	/*
   1344    1.1  drochner 	 * From the Alteon sample driver:
   1345    1.1  drochner 	 * Must insure that we do not cross an 8K (bytes) boundary
   1346   1.66     perry 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
   1347   1.66     perry 	 * restriction on some ALPHA platforms with early revision
   1348   1.66     perry 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
   1349    1.1  drochner 	 */
   1350  1.108   msaitoh 	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax | TI_PCI_READMAX_1024);
   1351    1.1  drochner #else
   1352    1.1  drochner 	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
   1353    1.1  drochner #endif
   1354    1.1  drochner 
   1355    1.1  drochner 	/* This sets the min dma param all the way up (0xff). */
   1356    1.1  drochner 	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
   1357    1.1  drochner 
   1358    1.1  drochner 	/* Configure DMA variables. */
   1359    1.1  drochner #if BYTE_ORDER == BIG_ENDIAN
   1360    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
   1361    1.1  drochner 	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
   1362    1.1  drochner 	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
   1363    1.1  drochner 	    TI_OPMODE_DONT_FRAG_JUMBO);
   1364    1.1  drochner #else
   1365  1.108   msaitoh 	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA |
   1366  1.108   msaitoh 	    TI_OPMODE_WORDSWAP_BD | TI_OPMODE_DONT_FRAG_JUMBO |
   1367  1.108   msaitoh 	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB);
   1368    1.1  drochner #endif
   1369    1.1  drochner 
   1370    1.1  drochner 	/*
   1371    1.1  drochner 	 * Only allow 1 DMA channel to be active at a time.
   1372    1.1  drochner 	 * I don't think this is a good idea, but without it
   1373    1.1  drochner 	 * the firmware racks up lots of nicDmaReadRingFull
   1374    1.1  drochner 	 * errors.
   1375   1.24    bouyer 	 * Incompatible with hardware assisted checksums.
   1376    1.1  drochner 	 */
   1377   1.24    bouyer 	if ((sc->ethercom.ec_if.if_capenable &
   1378   1.67      yamt 	    (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1379   1.67      yamt 	     IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
   1380   1.67      yamt 	     IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx)) == 0)
   1381   1.24    bouyer 		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
   1382    1.1  drochner 
   1383    1.1  drochner 	/* Recommended settings from Tigon manual. */
   1384    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
   1385    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
   1386    1.1  drochner 
   1387    1.1  drochner 	if (ti_64bitslot_war(sc)) {
   1388    1.1  drochner 		printf("%s: bios thinks we're in a 64 bit slot, "
   1389   1.91       chs 		    "but we aren't", device_xname(sc->sc_dev));
   1390   1.77       tnn 		return (EINVAL);
   1391    1.1  drochner 	}
   1392    1.1  drochner 
   1393   1.77       tnn 	return (0);
   1394    1.1  drochner }
   1395    1.1  drochner 
   1396    1.1  drochner /*
   1397    1.1  drochner  * Initialize the general information block and firmware, and
   1398    1.1  drochner  * start the CPU(s) running.
   1399    1.1  drochner  */
   1400   1.77       tnn static int
   1401   1.77       tnn ti_gibinit(struct ti_softc *sc)
   1402    1.1  drochner {
   1403    1.1  drochner 	struct ti_rcb		*rcb;
   1404    1.1  drochner 	int			i;
   1405    1.1  drochner 	struct ifnet		*ifp;
   1406    1.1  drochner 
   1407    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   1408    1.1  drochner 
   1409    1.1  drochner 	/* Disable interrupts for now. */
   1410    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   1411    1.1  drochner 
   1412    1.1  drochner 	/* Tell the chip where to find the general information block. */
   1413    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
   1414   1.33   thorpej 	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, TI_CDGIBADDR(sc));
   1415    1.1  drochner 
   1416    1.1  drochner 	/* Load the firmware into SRAM. */
   1417    1.1  drochner 	ti_loadfw(sc);
   1418    1.1  drochner 
   1419    1.1  drochner 	/* Set up the contents of the general info and ring control blocks. */
   1420    1.1  drochner 
   1421    1.1  drochner 	/* Set up the event ring and producer pointer. */
   1422    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
   1423    1.1  drochner 
   1424   1.33   thorpej 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_CDEVENTADDR(sc, 0);
   1425    1.1  drochner 	rcb->ti_flags = 0;
   1426    1.1  drochner 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
   1427   1.33   thorpej 	    TI_CDEVPRODADDR(sc);
   1428   1.33   thorpej 
   1429    1.1  drochner 	sc->ti_ev_prodidx.ti_idx = 0;
   1430    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
   1431    1.1  drochner 	sc->ti_ev_saved_considx = 0;
   1432    1.1  drochner 
   1433    1.1  drochner 	/* Set up the command ring and producer mailbox. */
   1434    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
   1435    1.1  drochner 
   1436    1.1  drochner 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
   1437    1.1  drochner 	rcb->ti_flags = 0;
   1438    1.1  drochner 	rcb->ti_max_len = 0;
   1439    1.1  drochner 	for (i = 0; i < TI_CMD_RING_CNT; i++) {
   1440    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
   1441    1.1  drochner 	}
   1442    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
   1443    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
   1444    1.1  drochner 	sc->ti_cmd_saved_prodidx = 0;
   1445    1.1  drochner 
   1446    1.1  drochner 	/*
   1447    1.1  drochner 	 * Assign the address of the stats refresh buffer.
   1448    1.1  drochner 	 * We re-use the current stats buffer for this to
   1449    1.1  drochner 	 * conserve memory.
   1450    1.1  drochner 	 */
   1451    1.1  drochner 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
   1452   1.33   thorpej 	    TI_CDSTATSADDR(sc);
   1453    1.1  drochner 
   1454    1.1  drochner 	/* Set up the standard receive ring. */
   1455    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
   1456   1.33   thorpej 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_CDRXSTDADDR(sc, 0);
   1457   1.22   thorpej 	rcb->ti_max_len = ETHER_MAX_LEN;
   1458    1.1  drochner 	rcb->ti_flags = 0;
   1459   1.67      yamt 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   1460   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1461  1.108   msaitoh 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
   1462   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM;
   1463   1.65  jdolecek 	if (VLAN_ATTACHED(&sc->ethercom))
   1464   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1465    1.1  drochner 
   1466    1.1  drochner 	/* Set up the jumbo receive ring. */
   1467    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
   1468   1.33   thorpej 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_CDRXJUMBOADDR(sc, 0);
   1469   1.22   thorpej 	rcb->ti_max_len = ETHER_MAX_LEN_JUMBO;
   1470    1.1  drochner 	rcb->ti_flags = 0;
   1471   1.67      yamt 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   1472   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1473  1.108   msaitoh 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
   1474   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM;
   1475   1.65  jdolecek 	if (VLAN_ATTACHED(&sc->ethercom))
   1476   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1477    1.1  drochner 
   1478    1.1  drochner 	/*
   1479    1.1  drochner 	 * Set up the mini ring. Only activated on the
   1480    1.1  drochner 	 * Tigon 2 but the slot in the config block is
   1481    1.1  drochner 	 * still there on the Tigon 1.
   1482    1.1  drochner 	 */
   1483    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
   1484   1.33   thorpej 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_CDRXMINIADDR(sc, 0);
   1485    1.2  drochner 	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
   1486    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   1487    1.1  drochner 		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
   1488    1.1  drochner 	else
   1489    1.1  drochner 		rcb->ti_flags = 0;
   1490   1.67      yamt 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   1491   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1492  1.108   msaitoh 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
   1493   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM;
   1494   1.65  jdolecek 	if (VLAN_ATTACHED(&sc->ethercom))
   1495   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1496    1.1  drochner 
   1497    1.1  drochner 	/*
   1498    1.1  drochner 	 * Set up the receive return ring.
   1499    1.1  drochner 	 */
   1500    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
   1501   1.33   thorpej 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_CDRXRTNADDR(sc, 0);
   1502    1.1  drochner 	rcb->ti_flags = 0;
   1503    1.1  drochner 	rcb->ti_max_len = TI_RETURN_RING_CNT;
   1504    1.1  drochner 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
   1505   1.33   thorpej 	    TI_CDRTNPRODADDR(sc);
   1506    1.1  drochner 
   1507    1.1  drochner 	/*
   1508    1.1  drochner 	 * Set up the tx ring. Note: for the Tigon 2, we have the option
   1509    1.1  drochner 	 * of putting the transmit ring in the host's address space and
   1510    1.1  drochner 	 * letting the chip DMA it instead of leaving the ring in the NIC's
   1511    1.1  drochner 	 * memory and accessing it through the shared memory region. We
   1512    1.1  drochner 	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
   1513    1.1  drochner 	 * so we have to revert to the shared memory scheme if we detect
   1514    1.1  drochner 	 * a Tigon 1 chip.
   1515    1.1  drochner 	 */
   1516    1.1  drochner 	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
   1517    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON) {
   1518   1.30   thorpej 		sc->ti_tx_ring_nic =
   1519    1.1  drochner 		    (struct ti_tx_desc *)(sc->ti_vhandle + TI_WINDOW);
   1520    1.1  drochner 	}
   1521   1.39   thorpej 	memset((char *)sc->ti_rdata->ti_tx_ring, 0,
   1522    1.1  drochner 	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
   1523    1.1  drochner 	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
   1524    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   1525    1.1  drochner 		rcb->ti_flags = 0;
   1526    1.1  drochner 	else
   1527    1.1  drochner 		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
   1528   1.67      yamt 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx)
   1529   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1530   1.21   thorpej 	/*
   1531   1.21   thorpej 	 * When we get the packet, there is a pseudo-header seed already
   1532   1.21   thorpej 	 * in the th_sum or uh_sum field.  Make sure the firmware doesn't
   1533   1.21   thorpej 	 * compute the pseudo-header checksum again!
   1534   1.21   thorpej 	 */
   1535  1.108   msaitoh 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx))
   1536  1.108   msaitoh 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
   1537   1.21   thorpej 		    TI_RCB_FLAG_NO_PHDR_CKSUM;
   1538   1.65  jdolecek 	if (VLAN_ATTACHED(&sc->ethercom))
   1539   1.21   thorpej 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1540    1.1  drochner 	rcb->ti_max_len = TI_TX_RING_CNT;
   1541    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   1542    1.1  drochner 		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
   1543    1.1  drochner 	else
   1544   1.33   thorpej 		TI_HOSTADDR(rcb->ti_hostaddr) = TI_CDTXADDR(sc, 0);
   1545    1.1  drochner 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
   1546   1.33   thorpej 	    TI_CDTXCONSADDR(sc);
   1547    1.1  drochner 
   1548   1.34   thorpej 	/*
   1549   1.34   thorpej 	 * We're done frobbing the General Information Block.  Sync
   1550   1.34   thorpej 	 * it.  Note we take care of the first stats sync here, as
   1551   1.34   thorpej 	 * well.
   1552   1.34   thorpej 	 */
   1553  1.108   msaitoh 	TI_CDGIBSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1554   1.34   thorpej 
   1555    1.1  drochner 	/* Set up tuneables */
   1556   1.12    bouyer 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN) ||
   1557   1.12    bouyer 	    (sc->ethercom.ec_capenable & ETHERCAP_VLAN_MTU))
   1558    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
   1559    1.1  drochner 		    (sc->ti_rx_coal_ticks / 10));
   1560    1.1  drochner 	else
   1561    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
   1562    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
   1563    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
   1564    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
   1565    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
   1566    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
   1567    1.1  drochner 
   1568    1.1  drochner 	/* Turn interrupts on. */
   1569    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
   1570    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
   1571    1.1  drochner 
   1572    1.1  drochner 	/* Start CPU. */
   1573  1.108   msaitoh 	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT | TI_CPUSTATE_STEP));
   1574    1.1  drochner 
   1575   1.77       tnn 	return (0);
   1576    1.1  drochner }
   1577    1.1  drochner 
   1578    1.1  drochner /*
   1579    1.6    bouyer  * look for id in the device list, returning the first match
   1580    1.6    bouyer  */
   1581   1.19  jdolecek static const struct ti_type *
   1582   1.77       tnn ti_type_match(struct pci_attach_args *pa)
   1583    1.6    bouyer {
   1584  1.108   msaitoh 	const struct ti_type	      *t;
   1585    1.6    bouyer 
   1586    1.6    bouyer 	t = ti_devs;
   1587   1.77       tnn 	while (t->ti_name != NULL) {
   1588    1.6    bouyer 		if ((PCI_VENDOR(pa->pa_id) == t->ti_vid) &&
   1589    1.6    bouyer 		    (PCI_PRODUCT(pa->pa_id) == t->ti_did)) {
   1590    1.6    bouyer 			return (t);
   1591    1.6    bouyer 		}
   1592    1.6    bouyer 		t++;
   1593    1.6    bouyer 	}
   1594    1.6    bouyer 
   1595   1.77       tnn 	return (NULL);
   1596    1.6    bouyer }
   1597    1.6    bouyer 
   1598    1.6    bouyer /*
   1599    1.1  drochner  * Probe for a Tigon chip. Check the PCI vendor and device IDs
   1600    1.1  drochner  * against our list and return its name if we find a match.
   1601    1.1  drochner  */
   1602   1.72  christos static int
   1603   1.84    cegger ti_probe(device_t parent, cfdata_t match, void *aux)
   1604    1.1  drochner {
   1605  1.108   msaitoh 	struct pci_attach_args	*pa = aux;
   1606  1.108   msaitoh 	const struct ti_type	*t;
   1607    1.1  drochner 
   1608    1.6    bouyer 	t = ti_type_match(pa);
   1609    1.1  drochner 
   1610   1.77       tnn 	return ((t == NULL) ? 0 : 1);
   1611    1.1  drochner }
   1612    1.1  drochner 
   1613   1.72  christos static void
   1614   1.84    cegger ti_attach(device_t parent, device_t self, void *aux)
   1615    1.1  drochner {
   1616  1.108   msaitoh 	uint32_t		command;
   1617    1.1  drochner 	struct ifnet		*ifp;
   1618    1.1  drochner 	struct ti_softc		*sc;
   1619  1.108   msaitoh 	uint8_t eaddr[ETHER_ADDR_LEN];
   1620    1.1  drochner 	struct pci_attach_args *pa = aux;
   1621    1.1  drochner 	pci_chipset_tag_t pc = pa->pa_pc;
   1622    1.1  drochner 	pci_intr_handle_t ih;
   1623    1.1  drochner 	const char *intrstr = NULL;
   1624    1.1  drochner 	bus_dma_segment_t dmaseg;
   1625    1.6    bouyer 	int error, dmanseg, nolinear;
   1626   1.19  jdolecek 	const struct ti_type		*t;
   1627   1.93  christos 	char intrbuf[PCI_INTRSTR_LEN];
   1628    1.6    bouyer 
   1629    1.6    bouyer 	t = ti_type_match(pa);
   1630    1.6    bouyer 	if (t == NULL) {
   1631   1.99   msaitoh 		aprint_error("ti_attach: were did the card go ?\n");
   1632    1.6    bouyer 		return;
   1633    1.6    bouyer 	}
   1634    1.1  drochner 
   1635   1.99   msaitoh 	aprint_normal(": %s (rev. 0x%02x)\n", t->ti_name,
   1636   1.99   msaitoh 	    PCI_REVISION(pa->pa_class));
   1637    1.1  drochner 
   1638   1.85    cegger 	sc = device_private(self);
   1639   1.91       chs 	sc->sc_dev = self;
   1640    1.1  drochner 
   1641    1.1  drochner 	/*
   1642    1.1  drochner 	 * Map control/status registers.
   1643    1.1  drochner 	 */
   1644    1.6    bouyer 	nolinear = 0;
   1645    1.6    bouyer 	if (pci_mapreg_map(pa, 0x10,
   1646    1.6    bouyer 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
   1647    1.6    bouyer 	    BUS_SPACE_MAP_LINEAR , &sc->ti_btag, &sc->ti_bhandle,
   1648    1.6    bouyer 	    NULL, NULL)) {
   1649    1.6    bouyer 		nolinear = 1;
   1650    1.6    bouyer 		if (pci_mapreg_map(pa, 0x10,
   1651    1.6    bouyer 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
   1652    1.6    bouyer 		    0 , &sc->ti_btag, &sc->ti_bhandle, NULL, NULL)) {
   1653   1.99   msaitoh 			aprint_error_dev(self, "can't map memory space\n");
   1654    1.6    bouyer 			return;
   1655    1.6    bouyer 		}
   1656    1.1  drochner 	}
   1657    1.6    bouyer 	if (nolinear == 0)
   1658   1.45       eeh 		sc->ti_vhandle = bus_space_vaddr(sc->ti_btag, sc->ti_bhandle);
   1659   1.66     perry 	else
   1660    1.6    bouyer 		sc->ti_vhandle = NULL;
   1661    1.1  drochner 
   1662    1.1  drochner 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   1663    1.1  drochner 	command |= PCI_COMMAND_MASTER_ENABLE;
   1664    1.1  drochner 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
   1665    1.1  drochner 
   1666    1.1  drochner 	/* Allocate interrupt */
   1667   1.17  sommerfe 	if (pci_intr_map(pa, &ih)) {
   1668   1.91       chs 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
   1669   1.54    simonb 		return;
   1670    1.1  drochner 	}
   1671   1.93  christos 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
   1672  1.106  jdolecek 	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, ti_intr, sc,
   1673  1.106  jdolecek 	    device_xname(self));
   1674    1.1  drochner 	if (sc->sc_ih == NULL) {
   1675   1.91       chs 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
   1676    1.1  drochner 		if (intrstr != NULL)
   1677   1.87     njoly 			aprint_error(" at %s", intrstr);
   1678   1.87     njoly 		aprint_error("\n");
   1679   1.54    simonb 		return;
   1680    1.1  drochner 	}
   1681   1.91       chs 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
   1682    1.1  drochner 
   1683    1.1  drochner 	if (ti_chipinit(sc)) {
   1684   1.81    cegger 		aprint_error_dev(self, "chip initialization failed\n");
   1685    1.6    bouyer 		goto fail2;
   1686    1.6    bouyer 	}
   1687   1.31   thorpej 
   1688   1.31   thorpej 	/*
   1689   1.31   thorpej 	 * Deal with some chip diffrences.
   1690   1.31   thorpej 	 */
   1691   1.31   thorpej 	switch (sc->ti_hwrev) {
   1692   1.31   thorpej 	case TI_HWREV_TIGON:
   1693   1.31   thorpej 		sc->sc_tx_encap = ti_encap_tigon1;
   1694   1.32   thorpej 		sc->sc_tx_eof = ti_txeof_tigon1;
   1695   1.31   thorpej 		if (nolinear == 1)
   1696   1.98   msaitoh 			aprint_error_dev(self,
   1697   1.98   msaitoh 			    "memory space not mapped linear\n");
   1698   1.31   thorpej 		break;
   1699   1.31   thorpej 
   1700   1.31   thorpej 	case TI_HWREV_TIGON_II:
   1701   1.31   thorpej 		sc->sc_tx_encap = ti_encap_tigon2;
   1702   1.32   thorpej 		sc->sc_tx_eof = ti_txeof_tigon2;
   1703   1.31   thorpej 		break;
   1704   1.31   thorpej 
   1705   1.31   thorpej 	default:
   1706   1.99   msaitoh 		aprint_error_dev(self, "Unknown chip version: %d\n",
   1707   1.31   thorpej 		    sc->ti_hwrev);
   1708   1.31   thorpej 		goto fail2;
   1709    1.1  drochner 	}
   1710    1.1  drochner 
   1711    1.1  drochner 	/* Zero out the NIC's on-board SRAM. */
   1712    1.1  drochner 	ti_mem(sc, 0x2000, 0x100000 - 0x2000,  NULL);
   1713    1.1  drochner 
   1714    1.1  drochner 	/* Init again -- zeroing memory may have clobbered some registers. */
   1715    1.1  drochner 	if (ti_chipinit(sc)) {
   1716   1.81    cegger 		aprint_error_dev(self, "chip initialization failed\n");
   1717    1.6    bouyer 		goto fail2;
   1718    1.1  drochner 	}
   1719    1.1  drochner 
   1720    1.1  drochner 	/*
   1721    1.1  drochner 	 * Get station address from the EEPROM. Note: the manual states
   1722    1.1  drochner 	 * that the MAC address is at offset 0x8c, however the data is
   1723    1.1  drochner 	 * stored as two longwords (since that's how it's loaded into
   1724   1.42       wiz 	 * the NIC). This means the MAC address is actually preceded
   1725    1.1  drochner 	 * by two zero bytes. We need to skip over those.
   1726    1.1  drochner 	 */
   1727   1.74  christos 	if (ti_read_eeprom(sc, (void *)&eaddr,
   1728    1.1  drochner 				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
   1729   1.81    cegger 		aprint_error_dev(self, "failed to read station address\n");
   1730    1.6    bouyer 		goto fail2;
   1731    1.1  drochner 	}
   1732    1.1  drochner 
   1733    1.1  drochner 	/*
   1734    1.1  drochner 	 * A Tigon chip was detected. Inform the world.
   1735    1.1  drochner 	 */
   1736  1.109   msaitoh 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
   1737    1.1  drochner 
   1738    1.1  drochner 	sc->sc_dmat = pa->pa_dmat;
   1739    1.1  drochner 
   1740    1.1  drochner 	/* Allocate the general information block and ring buffers. */
   1741    1.1  drochner 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
   1742   1.13   thorpej 	    sizeof(struct ti_ring_data), PAGE_SIZE, 0, &dmaseg, 1, &dmanseg,
   1743    1.1  drochner 	    BUS_DMA_NOWAIT)) != 0) {
   1744   1.99   msaitoh 		aprint_error_dev(self,
   1745   1.98   msaitoh 		    "can't allocate ring buffer, error = %d\n", error);
   1746    1.6    bouyer 		goto fail2;
   1747    1.1  drochner 	}
   1748    1.1  drochner 
   1749    1.1  drochner 	if ((error = bus_dmamem_map(sc->sc_dmat, &dmaseg, dmanseg,
   1750   1.74  christos 	    sizeof(struct ti_ring_data), (void **)&sc->ti_rdata,
   1751  1.108   msaitoh 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
   1752   1.99   msaitoh 		aprint_error_dev(self,
   1753   1.98   msaitoh 		    "can't map ring buffer, error = %d\n", error);
   1754    1.6    bouyer 		goto fail2;
   1755    1.1  drochner 	}
   1756    1.1  drochner 
   1757    1.1  drochner 	if ((error = bus_dmamap_create(sc->sc_dmat,
   1758    1.1  drochner 	    sizeof(struct ti_ring_data), 1,
   1759    1.1  drochner 	    sizeof(struct ti_ring_data), 0, BUS_DMA_NOWAIT,
   1760    1.1  drochner 	    &sc->info_dmamap)) != 0) {
   1761   1.99   msaitoh 		aprint_error_dev(self,
   1762   1.98   msaitoh 		    "can't create ring buffer DMA map, error = %d\n", error);
   1763    1.6    bouyer 		goto fail2;
   1764    1.1  drochner 	}
   1765    1.1  drochner 
   1766    1.1  drochner 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->info_dmamap,
   1767    1.1  drochner 	    sc->ti_rdata, sizeof(struct ti_ring_data), NULL,
   1768    1.1  drochner 	    BUS_DMA_NOWAIT)) != 0) {
   1769   1.99   msaitoh 		aprint_error_dev(self,
   1770   1.98   msaitoh 		    "can't load ring buffer DMA map, error = %d\n", error);
   1771    1.6    bouyer 		goto fail2;
   1772    1.1  drochner 	}
   1773    1.1  drochner 
   1774    1.1  drochner 	sc->info_dmaaddr = sc->info_dmamap->dm_segs[0].ds_addr;
   1775    1.1  drochner 
   1776   1.39   thorpej 	memset(sc->ti_rdata, 0, sizeof(struct ti_ring_data));
   1777    1.1  drochner 
   1778    1.1  drochner 	/* Try to allocate memory for jumbo buffers. */
   1779    1.1  drochner 	if (ti_alloc_jumbo_mem(sc)) {
   1780   1.81    cegger 		aprint_error_dev(self, "jumbo buffer allocation failed\n");
   1781    1.6    bouyer 		goto fail2;
   1782    1.1  drochner 	}
   1783    1.1  drochner 
   1784   1.20     enami 	SIMPLEQ_INIT(&sc->ti_mc_listhead);
   1785   1.20     enami 
   1786   1.15    bouyer 	/*
   1787   1.36     bjh21 	 * We really need a better way to tell a 1000baseT card
   1788   1.15    bouyer 	 * from a 1000baseSX one, since in theory there could be
   1789   1.36     bjh21 	 * OEMed 1000baseT cards from lame vendors who aren't
   1790   1.15    bouyer 	 * clever enough to change the PCI ID. For the moment
   1791   1.15    bouyer 	 * though, the AceNIC is the only copper card available.
   1792   1.15    bouyer 	 */
   1793   1.15    bouyer 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALTEON &&
   1794   1.15    bouyer 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTEON_ACENIC_COPPER) ||
   1795   1.15    bouyer 	    (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NETGEAR &&
   1796   1.15    bouyer 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NETGEAR_GA620T))
   1797   1.15    bouyer 		sc->ti_copper = 1;
   1798   1.15    bouyer 	else
   1799   1.15    bouyer 		sc->ti_copper = 0;
   1800   1.15    bouyer 
   1801    1.1  drochner 	/* Set default tuneable values. */
   1802    1.1  drochner 	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
   1803    1.1  drochner 	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
   1804    1.1  drochner 	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
   1805    1.1  drochner 	sc->ti_rx_max_coal_bds = 64;
   1806    1.1  drochner 	sc->ti_tx_max_coal_bds = 128;
   1807    1.1  drochner 	sc->ti_tx_buf_ratio = 21;
   1808    1.1  drochner 
   1809    1.1  drochner 	/* Set up ifnet structure */
   1810    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   1811    1.1  drochner 	ifp->if_softc = sc;
   1812   1.91       chs 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
   1813    1.1  drochner 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1814    1.1  drochner 	ifp->if_ioctl = ti_ioctl;
   1815    1.1  drochner 	ifp->if_start = ti_start;
   1816    1.1  drochner 	ifp->if_watchdog = ti_watchdog;
   1817   1.16   thorpej 	IFQ_SET_READY(&ifp->if_snd);
   1818   1.16   thorpej 
   1819   1.16   thorpej #if 0
   1820   1.16   thorpej 	/*
   1821   1.16   thorpej 	 * XXX This is not really correct -- we don't necessarily
   1822   1.16   thorpej 	 * XXX want to queue up as many as we can transmit at the
   1823   1.16   thorpej 	 * XXX upper layer like that.  Someone with a board should
   1824   1.16   thorpej 	 * XXX check to see how this affects performance.
   1825   1.16   thorpej 	 */
   1826    1.1  drochner 	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
   1827   1.16   thorpej #endif
   1828    1.1  drochner 
   1829   1.12    bouyer 	/*
   1830   1.12    bouyer 	 * We can support 802.1Q VLAN-sized frames.
   1831   1.12    bouyer 	 */
   1832   1.15    bouyer 	sc->ethercom.ec_capabilities |=
   1833   1.15    bouyer 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
   1834   1.12    bouyer 
   1835   1.21   thorpej 	/*
   1836   1.21   thorpej 	 * We can do IPv4, TCPv4, and UDPv4 checksums in hardware.
   1837   1.21   thorpej 	 */
   1838   1.67      yamt 	ifp->if_capabilities |=
   1839   1.67      yamt 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
   1840   1.67      yamt 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1841   1.67      yamt 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
   1842   1.21   thorpej 
   1843    1.1  drochner 	/* Set up ifmedia support. */
   1844  1.111   msaitoh 	sc->ethercom.ec_ifmedia = &sc->ifmedia;
   1845    1.1  drochner 	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
   1846   1.15    bouyer 	if (sc->ti_copper) {
   1847  1.108   msaitoh 		/*
   1848  1.108   msaitoh 		 * Copper cards allow manual 10/100 mode selection,
   1849  1.108   msaitoh 		 * but not manual 1000baseT mode selection. Why?
   1850  1.108   msaitoh 		 * Because currently there's no way to specify the
   1851  1.108   msaitoh 		 * master/slave setting through the firmware interface,
   1852  1.108   msaitoh 		 * so Alteon decided to just bag it and handle it
   1853  1.108   msaitoh 		 * via autonegotiation.
   1854  1.108   msaitoh 		 */
   1855  1.108   msaitoh 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
   1856  1.108   msaitoh 		ifmedia_add(&sc->ifmedia,
   1857  1.108   msaitoh 		    IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
   1858  1.108   msaitoh 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL);
   1859  1.108   msaitoh 		ifmedia_add(&sc->ifmedia,
   1860  1.108   msaitoh 		    IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
   1861  1.108   msaitoh 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T, 0, NULL);
   1862  1.108   msaitoh 		ifmedia_add(&sc->ifmedia,
   1863  1.108   msaitoh 		    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
   1864   1.15    bouyer 	} else {
   1865   1.15    bouyer 		/* Fiber cards don't support 10/100 modes. */
   1866  1.108   msaitoh 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
   1867  1.108   msaitoh 		ifmedia_add(&sc->ifmedia,
   1868  1.108   msaitoh 		    IFM_ETHER | IFM_1000_SX | IFM_FDX, 0, NULL);
   1869   1.15    bouyer 	}
   1870  1.108   msaitoh 	ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
   1871  1.108   msaitoh 	ifmedia_set(&sc->ifmedia, IFM_ETHER | IFM_AUTO);
   1872    1.1  drochner 
   1873    1.1  drochner 	/*
   1874    1.1  drochner 	 * Call MI attach routines.
   1875    1.1  drochner 	 */
   1876    1.1  drochner 	if_attach(ifp);
   1877  1.100     ozaki 	if_deferred_start_init(ifp, NULL);
   1878    1.1  drochner 	ether_ifattach(ifp, eaddr);
   1879    1.1  drochner 
   1880   1.86   tsutsui 	/*
   1881   1.86   tsutsui 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
   1882   1.86   tsutsui 	 * doing do could allow DMA to corrupt kernel memory during the
   1883   1.86   tsutsui 	 * reboot before the driver initializes.
   1884   1.86   tsutsui 	 */
   1885   1.86   tsutsui 	if (pmf_device_register1(self, NULL, NULL, ti_shutdown))
   1886   1.86   tsutsui 		pmf_class_network_register(self, ifp);
   1887   1.86   tsutsui 	else
   1888   1.86   tsutsui 		aprint_error_dev(self, "couldn't establish power handler\n");
   1889   1.86   tsutsui 
   1890    1.6    bouyer 	return;
   1891    1.6    bouyer fail2:
   1892    1.6    bouyer 	pci_intr_disestablish(pc, sc->sc_ih);
   1893    1.6    bouyer 	return;
   1894    1.1  drochner }
   1895    1.1  drochner 
   1896    1.1  drochner /*
   1897    1.1  drochner  * Frame reception handling. This is called if there's a frame
   1898    1.1  drochner  * on the receive return list.
   1899    1.1  drochner  *
   1900    1.1  drochner  * Note: we have to be able to handle three possibilities here:
   1901    1.1  drochner  * 1) the frame is from the mini receive ring (can only happen)
   1902    1.1  drochner  *    on Tigon 2 boards)
   1903   1.25       wiz  * 2) the frame is from the jumbo receive ring
   1904    1.1  drochner  * 3) the frame is from the standard receive ring
   1905    1.1  drochner  */
   1906    1.1  drochner 
   1907   1.77       tnn static void
   1908   1.77       tnn ti_rxeof(struct ti_softc *sc)
   1909    1.1  drochner {
   1910    1.1  drochner 	struct ifnet		*ifp;
   1911    1.1  drochner 	struct ti_cmd_desc	cmd;
   1912    1.1  drochner 
   1913    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   1914    1.1  drochner 
   1915   1.77       tnn 	while (sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
   1916    1.1  drochner 		struct ti_rx_desc	*cur_rx;
   1917  1.108   msaitoh 		uint32_t		rxidx;
   1918    1.1  drochner 		struct mbuf		*m = NULL;
   1919   1.21   thorpej 		struct ether_header	*eh;
   1920    1.1  drochner 		bus_dmamap_t dmamap;
   1921    1.1  drochner 
   1922    1.1  drochner 		cur_rx =
   1923    1.1  drochner 		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
   1924    1.1  drochner 		rxidx = cur_rx->ti_idx;
   1925    1.1  drochner 		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
   1926    1.1  drochner 
   1927    1.1  drochner 		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
   1928    1.1  drochner 			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
   1929    1.1  drochner 			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
   1930    1.1  drochner 			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
   1931    1.1  drochner 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
   1932    1.1  drochner 				ifp->if_ierrors++;
   1933    1.1  drochner 				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
   1934    1.1  drochner 				continue;
   1935    1.1  drochner 			}
   1936    1.1  drochner 			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL)
   1937    1.1  drochner 			    == ENOBUFS) {
   1938    1.1  drochner 				ifp->if_ierrors++;
   1939    1.1  drochner 				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
   1940    1.1  drochner 				continue;
   1941    1.1  drochner 			}
   1942    1.1  drochner 		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
   1943    1.1  drochner 			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
   1944    1.1  drochner 			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
   1945    1.1  drochner 			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
   1946    1.1  drochner 			dmamap = sc->mini_dmamap[rxidx];
   1947    1.1  drochner 			sc->mini_dmamap[rxidx] = 0;
   1948    1.1  drochner 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
   1949    1.1  drochner 				ifp->if_ierrors++;
   1950    1.1  drochner 				ti_newbuf_mini(sc, sc->ti_mini, m, dmamap);
   1951    1.1  drochner 				continue;
   1952    1.1  drochner 			}
   1953    1.1  drochner 			if (ti_newbuf_mini(sc, sc->ti_mini, NULL, dmamap)
   1954    1.1  drochner 			    == ENOBUFS) {
   1955    1.1  drochner 				ifp->if_ierrors++;
   1956    1.1  drochner 				ti_newbuf_mini(sc, sc->ti_mini, m, dmamap);
   1957    1.1  drochner 				continue;
   1958    1.1  drochner 			}
   1959    1.1  drochner 		} else {
   1960    1.1  drochner 			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
   1961    1.1  drochner 			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
   1962    1.1  drochner 			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
   1963    1.1  drochner 			dmamap = sc->std_dmamap[rxidx];
   1964    1.1  drochner 			sc->std_dmamap[rxidx] = 0;
   1965    1.1  drochner 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
   1966    1.1  drochner 				ifp->if_ierrors++;
   1967    1.1  drochner 				ti_newbuf_std(sc, sc->ti_std, m, dmamap);
   1968    1.1  drochner 				continue;
   1969    1.1  drochner 			}
   1970    1.1  drochner 			if (ti_newbuf_std(sc, sc->ti_std, NULL, dmamap)
   1971    1.1  drochner 			    == ENOBUFS) {
   1972    1.1  drochner 				ifp->if_ierrors++;
   1973    1.1  drochner 				ti_newbuf_std(sc, sc->ti_std, m, dmamap);
   1974    1.1  drochner 				continue;
   1975    1.1  drochner 			}
   1976    1.1  drochner 		}
   1977    1.1  drochner 
   1978    1.1  drochner 		m->m_pkthdr.len = m->m_len = cur_rx->ti_len;
   1979   1.97     ozaki 		m_set_rcvif(m, ifp);
   1980    1.1  drochner 
   1981   1.21   thorpej 		eh = mtod(m, struct ether_header *);
   1982   1.21   thorpej 		switch (ntohs(eh->ether_type)) {
   1983   1.44    itojun #ifdef INET
   1984   1.21   thorpej 		case ETHERTYPE_IP:
   1985   1.21   thorpej 		    {
   1986   1.21   thorpej 			struct ip *ip = (struct ip *) (eh + 1);
   1987   1.21   thorpej 
   1988   1.21   thorpej 			/*
   1989   1.21   thorpej 			 * Note the Tigon firmware does not invert
   1990   1.21   thorpej 			 * the checksum for us, hence the XOR.
   1991   1.21   thorpej 			 */
   1992   1.21   thorpej 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1993   1.21   thorpej 			if ((cur_rx->ti_ip_cksum ^ 0xffff) != 0)
   1994   1.21   thorpej 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1995   1.21   thorpej 			/*
   1996   1.21   thorpej 			 * ntohs() the constant so the compiler can
   1997   1.21   thorpej 			 * optimize...
   1998   1.21   thorpej 			 *
   1999   1.21   thorpej 			 * XXX Figure out a sane way to deal with
   2000   1.21   thorpej 			 * fragmented packets.
   2001   1.21   thorpej 			 */
   2002  1.108   msaitoh 			if ((ip->ip_off & htons(IP_MF | IP_OFFMASK)) == 0) {
   2003   1.21   thorpej 				switch (ip->ip_p) {
   2004   1.21   thorpej 				case IPPROTO_TCP:
   2005   1.21   thorpej 					m->m_pkthdr.csum_data =
   2006   1.21   thorpej 					    cur_rx->ti_tcp_udp_cksum;
   2007   1.21   thorpej 					m->m_pkthdr.csum_flags |=
   2008  1.108   msaitoh 					    M_CSUM_TCPv4 | M_CSUM_DATA;
   2009   1.21   thorpej 					break;
   2010   1.21   thorpej 				case IPPROTO_UDP:
   2011   1.21   thorpej 					m->m_pkthdr.csum_data =
   2012   1.21   thorpej 					    cur_rx->ti_tcp_udp_cksum;
   2013   1.21   thorpej 					m->m_pkthdr.csum_flags |=
   2014  1.108   msaitoh 					    M_CSUM_UDPv4 | M_CSUM_DATA;
   2015   1.21   thorpej 					break;
   2016   1.21   thorpej 				default:
   2017   1.21   thorpej 					/* Nothing */;
   2018   1.21   thorpej 				}
   2019   1.21   thorpej 			}
   2020   1.21   thorpej 			break;
   2021   1.21   thorpej 		    }
   2022   1.44    itojun #endif
   2023   1.21   thorpej 		default:
   2024   1.21   thorpej 			/* Nothing. */
   2025   1.21   thorpej 			break;
   2026   1.21   thorpej 		}
   2027    1.1  drochner 
   2028   1.65  jdolecek 		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
   2029  1.102  knakahar 			/* ti_vlan_tag also has the priority, trim it */
   2030  1.102  knakahar 			vlan_set_tag(m, cur_rx->ti_vlan_tag & 0x0fff);
   2031   1.65  jdolecek 		}
   2032   1.53    itojun 
   2033   1.96     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
   2034    1.1  drochner 	}
   2035    1.1  drochner 
   2036    1.1  drochner 	/* Only necessary on the Tigon 1. */
   2037    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   2038    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
   2039    1.1  drochner 		    sc->ti_rx_saved_considx);
   2040    1.1  drochner 
   2041    1.1  drochner 	TI_UPDATE_STDPROD(sc, sc->ti_std);
   2042    1.1  drochner 	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
   2043    1.1  drochner 	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
   2044    1.1  drochner }
   2045    1.1  drochner 
   2046   1.77       tnn static void
   2047   1.77       tnn ti_txeof_tigon1(struct ti_softc *sc)
   2048    1.1  drochner {
   2049    1.1  drochner 	struct ti_tx_desc	*cur_tx = NULL;
   2050    1.1  drochner 	struct ifnet		*ifp;
   2051   1.29   thorpej 	struct txdmamap_pool_entry *dma;
   2052    1.1  drochner 
   2053    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   2054    1.1  drochner 
   2055    1.1  drochner 	/*
   2056    1.1  drochner 	 * Go through our tx ring and free mbufs for those
   2057    1.1  drochner 	 * frames that have been sent.
   2058    1.1  drochner 	 */
   2059    1.1  drochner 	while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
   2060  1.108   msaitoh 		uint32_t	idx = 0;
   2061    1.1  drochner 
   2062    1.1  drochner 		idx = sc->ti_tx_saved_considx;
   2063   1.32   thorpej 		if (idx > 383)
   2064   1.32   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2065   1.32   thorpej 			    TI_TX_RING_BASE + 6144);
   2066   1.32   thorpej 		else if (idx > 255)
   2067   1.32   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2068   1.32   thorpej 			    TI_TX_RING_BASE + 4096);
   2069   1.32   thorpej 		else if (idx > 127)
   2070   1.32   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2071   1.32   thorpej 			    TI_TX_RING_BASE + 2048);
   2072   1.32   thorpej 		else
   2073   1.32   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2074   1.32   thorpej 			    TI_TX_RING_BASE);
   2075   1.32   thorpej 		cur_tx = &sc->ti_tx_ring_nic[idx % 128];
   2076   1.32   thorpej 		if (cur_tx->ti_flags & TI_BDFLAG_END)
   2077   1.32   thorpej 			ifp->if_opackets++;
   2078   1.32   thorpej 		if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
   2079   1.32   thorpej 			m_freem(sc->ti_cdata.ti_tx_chain[idx]);
   2080   1.32   thorpej 			sc->ti_cdata.ti_tx_chain[idx] = NULL;
   2081   1.32   thorpej 
   2082   1.32   thorpej 			dma = sc->txdma[idx];
   2083   1.32   thorpej 			KDASSERT(dma != NULL);
   2084   1.32   thorpej 			bus_dmamap_sync(sc->sc_dmat, dma->dmamap, 0,
   2085   1.32   thorpej 			    dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2086   1.32   thorpej 			bus_dmamap_unload(sc->sc_dmat, dma->dmamap);
   2087   1.32   thorpej 
   2088   1.32   thorpej 			SIMPLEQ_INSERT_HEAD(&sc->txdma_list, dma, link);
   2089   1.32   thorpej 			sc->txdma[idx] = NULL;
   2090   1.32   thorpej 		}
   2091   1.32   thorpej 		sc->ti_txcnt--;
   2092   1.32   thorpej 		TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
   2093   1.32   thorpej 		ifp->if_timer = 0;
   2094   1.32   thorpej 	}
   2095   1.32   thorpej 
   2096   1.32   thorpej 	if (cur_tx != NULL)
   2097   1.32   thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
   2098   1.32   thorpej }
   2099   1.32   thorpej 
   2100   1.77       tnn static void
   2101   1.77       tnn ti_txeof_tigon2(struct ti_softc *sc)
   2102   1.32   thorpej {
   2103   1.32   thorpej 	struct ti_tx_desc	*cur_tx = NULL;
   2104   1.32   thorpej 	struct ifnet		*ifp;
   2105   1.32   thorpej 	struct txdmamap_pool_entry *dma;
   2106   1.35   thorpej 	int firstidx, cnt;
   2107   1.32   thorpej 
   2108   1.32   thorpej 	ifp = &sc->ethercom.ec_if;
   2109   1.32   thorpej 
   2110   1.32   thorpej 	/*
   2111   1.32   thorpej 	 * Go through our tx ring and free mbufs for those
   2112   1.32   thorpej 	 * frames that have been sent.
   2113   1.32   thorpej 	 */
   2114   1.35   thorpej 	firstidx = sc->ti_tx_saved_considx;
   2115   1.35   thorpej 	cnt = 0;
   2116   1.32   thorpej 	while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
   2117  1.108   msaitoh 		uint32_t	idx = 0;
   2118   1.32   thorpej 
   2119   1.32   thorpej 		idx = sc->ti_tx_saved_considx;
   2120   1.32   thorpej 		cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
   2121    1.1  drochner 		if (cur_tx->ti_flags & TI_BDFLAG_END)
   2122    1.1  drochner 			ifp->if_opackets++;
   2123    1.1  drochner 		if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
   2124    1.1  drochner 			m_freem(sc->ti_cdata.ti_tx_chain[idx]);
   2125    1.1  drochner 			sc->ti_cdata.ti_tx_chain[idx] = NULL;
   2126    1.1  drochner 
   2127   1.29   thorpej 			dma = sc->txdma[idx];
   2128   1.29   thorpej 			KDASSERT(dma != NULL);
   2129   1.29   thorpej 			bus_dmamap_sync(sc->sc_dmat, dma->dmamap, 0,
   2130   1.29   thorpej 			    dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   2131   1.29   thorpej 			bus_dmamap_unload(sc->sc_dmat, dma->dmamap);
   2132   1.29   thorpej 
   2133   1.29   thorpej 			SIMPLEQ_INSERT_HEAD(&sc->txdma_list, dma, link);
   2134   1.29   thorpej 			sc->txdma[idx] = NULL;
   2135    1.1  drochner 		}
   2136   1.35   thorpej 		cnt++;
   2137    1.1  drochner 		sc->ti_txcnt--;
   2138    1.1  drochner 		TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
   2139    1.1  drochner 		ifp->if_timer = 0;
   2140    1.1  drochner 	}
   2141    1.1  drochner 
   2142   1.35   thorpej 	if (cnt != 0)
   2143   1.35   thorpej 		TI_CDTXSYNC(sc, firstidx, cnt, BUS_DMASYNC_POSTWRITE);
   2144   1.35   thorpej 
   2145    1.1  drochner 	if (cur_tx != NULL)
   2146    1.1  drochner 		ifp->if_flags &= ~IFF_OACTIVE;
   2147    1.1  drochner }
   2148    1.1  drochner 
   2149   1.77       tnn static int
   2150   1.77       tnn ti_intr(void *xsc)
   2151    1.1  drochner {
   2152  1.108   msaitoh 	struct ti_softc	*sc;
   2153  1.108   msaitoh 	struct ifnet	*ifp;
   2154    1.1  drochner 
   2155    1.1  drochner 	sc = xsc;
   2156    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   2157    1.1  drochner 
   2158    1.1  drochner #ifdef notdef
   2159    1.1  drochner 	/* Avoid this for now -- checking this register is expensive. */
   2160    1.1  drochner 	/* Make sure this is really our interrupt. */
   2161    1.1  drochner 	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE))
   2162    1.1  drochner 		return (0);
   2163    1.1  drochner #endif
   2164    1.1  drochner 
   2165  1.103  dholland 	/* Ack interrupt and stop others from occurring. */
   2166    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   2167    1.1  drochner 
   2168    1.1  drochner 	if (ifp->if_flags & IFF_RUNNING) {
   2169    1.1  drochner 		/* Check RX return ring producer/consumer */
   2170    1.1  drochner 		ti_rxeof(sc);
   2171    1.1  drochner 
   2172    1.1  drochner 		/* Check TX ring producer/consumer */
   2173   1.32   thorpej 		(*sc->sc_tx_eof)(sc);
   2174    1.1  drochner 	}
   2175    1.1  drochner 
   2176    1.1  drochner 	ti_handle_events(sc);
   2177    1.1  drochner 
   2178    1.1  drochner 	/* Re-enable interrupts. */
   2179    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
   2180    1.1  drochner 
   2181  1.100     ozaki 	if ((ifp->if_flags & IFF_RUNNING) != 0)
   2182  1.100     ozaki 		if_schedule_deferred_start(ifp);
   2183    1.1  drochner 
   2184    1.1  drochner 	return (1);
   2185    1.1  drochner }
   2186    1.1  drochner 
   2187   1.77       tnn static void
   2188   1.77       tnn ti_stats_update(struct ti_softc *sc)
   2189    1.1  drochner {
   2190    1.1  drochner 	struct ifnet		*ifp;
   2191    1.1  drochner 
   2192    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   2193    1.1  drochner 
   2194   1.34   thorpej 	TI_CDSTATSSYNC(sc, BUS_DMASYNC_POSTREAD);
   2195   1.34   thorpej 
   2196    1.1  drochner 	ifp->if_collisions +=
   2197    1.1  drochner 	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
   2198    1.1  drochner 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
   2199    1.1  drochner 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
   2200    1.1  drochner 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
   2201    1.1  drochner 	   ifp->if_collisions;
   2202    1.1  drochner 
   2203   1.34   thorpej 	TI_CDSTATSSYNC(sc, BUS_DMASYNC_PREREAD);
   2204    1.1  drochner }
   2205    1.1  drochner 
   2206    1.1  drochner /*
   2207    1.1  drochner  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
   2208    1.1  drochner  * pointers to descriptors.
   2209    1.1  drochner  */
   2210   1.77       tnn static int
   2211  1.108   msaitoh ti_encap_tigon1(struct ti_softc *sc, struct mbuf *m_head, uint32_t *txidx)
   2212    1.1  drochner {
   2213    1.1  drochner 	struct ti_tx_desc	*f = NULL;
   2214  1.108   msaitoh 	uint32_t		frag, cur, cnt = 0;
   2215    1.1  drochner 	struct txdmamap_pool_entry *dma;
   2216    1.1  drochner 	bus_dmamap_t dmamap;
   2217    1.1  drochner 	int error, i;
   2218  1.108   msaitoh 	uint16_t csum_flags = 0;
   2219    1.1  drochner 
   2220    1.1  drochner 	dma = SIMPLEQ_FIRST(&sc->txdma_list);
   2221    1.6    bouyer 	if (dma == NULL) {
   2222    1.6    bouyer 		return ENOMEM;
   2223    1.6    bouyer 	}
   2224    1.1  drochner 	dmamap = dma->dmamap;
   2225    1.1  drochner 
   2226   1.40   thorpej 	error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m_head,
   2227   1.49    bouyer 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2228    1.1  drochner 	if (error) {
   2229    1.1  drochner 		struct mbuf *m;
   2230   1.68  christos 		int j = 0;
   2231    1.1  drochner 		for (m = m_head; m; m = m->m_next)
   2232   1.68  christos 			j++;
   2233    1.1  drochner 		printf("ti_encap: bus_dmamap_load_mbuf (len %d, %d frags) "
   2234   1.68  christos 		       "error %d\n", m_head->m_pkthdr.len, j, error);
   2235    1.1  drochner 		return (ENOMEM);
   2236    1.1  drochner 	}
   2237    1.1  drochner 
   2238    1.1  drochner 	cur = frag = *txidx;
   2239    1.1  drochner 
   2240   1.21   thorpej 	if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4) {
   2241   1.21   thorpej 		/* IP header checksum field must be 0! */
   2242   1.21   thorpej 		csum_flags |= TI_BDFLAG_IP_CKSUM;
   2243   1.21   thorpej 	}
   2244  1.108   msaitoh 	if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4))
   2245   1.21   thorpej 		csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
   2246   1.21   thorpej 
   2247   1.21   thorpej 	/* XXX fragmented packet checksum capability? */
   2248   1.21   thorpej 
   2249    1.1  drochner 	/*
   2250  1.108   msaitoh 	 * Start packing the mbufs in this chain into
   2251    1.1  drochner 	 * the fragment pointers. Stop when we run out
   2252  1.108   msaitoh 	 * of fragments or hit the end of the mbuf chain.
   2253    1.1  drochner 	 */
   2254    1.1  drochner 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   2255   1.31   thorpej 		if (frag > 383)
   2256   1.31   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2257   1.31   thorpej 			    TI_TX_RING_BASE + 6144);
   2258   1.31   thorpej 		else if (frag > 255)
   2259   1.31   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2260   1.31   thorpej 			    TI_TX_RING_BASE + 4096);
   2261   1.31   thorpej 		else if (frag > 127)
   2262   1.31   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2263   1.31   thorpej 			    TI_TX_RING_BASE + 2048);
   2264   1.31   thorpej 		else
   2265   1.31   thorpej 			CSR_WRITE_4(sc, TI_WINBASE,
   2266   1.31   thorpej 			    TI_TX_RING_BASE);
   2267   1.31   thorpej 		f = &sc->ti_tx_ring_nic[frag % 128];
   2268   1.31   thorpej 		if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
   2269   1.31   thorpej 			break;
   2270   1.31   thorpej 		TI_HOSTADDR(f->ti_addr) = dmamap->dm_segs[i].ds_addr;
   2271   1.31   thorpej 		f->ti_len = dmamap->dm_segs[i].ds_len;
   2272   1.31   thorpej 		f->ti_flags = csum_flags;
   2273  1.102  knakahar 		if (vlan_has_tag(m_head)) {
   2274   1.31   thorpej 			f->ti_flags |= TI_BDFLAG_VLAN_TAG;
   2275  1.102  knakahar 			f->ti_vlan_tag = vlan_get_tag(m_head);
   2276   1.31   thorpej 		} else {
   2277   1.31   thorpej 			f->ti_vlan_tag = 0;
   2278   1.31   thorpej 		}
   2279   1.31   thorpej 		/*
   2280   1.31   thorpej 		 * Sanity check: avoid coming within 16 descriptors
   2281   1.31   thorpej 		 * of the end of the ring.
   2282   1.31   thorpej 		 */
   2283   1.31   thorpej 		if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
   2284   1.77       tnn 			return (ENOBUFS);
   2285   1.31   thorpej 		cur = frag;
   2286   1.31   thorpej 		TI_INC(frag, TI_TX_RING_CNT);
   2287   1.31   thorpej 		cnt++;
   2288   1.31   thorpej 	}
   2289   1.31   thorpej 
   2290   1.31   thorpej 	if (i < dmamap->dm_nsegs)
   2291   1.77       tnn 		return (ENOBUFS);
   2292   1.31   thorpej 
   2293   1.31   thorpej 	if (frag == sc->ti_tx_saved_considx)
   2294   1.77       tnn 		return (ENOBUFS);
   2295   1.31   thorpej 
   2296   1.31   thorpej 	sc->ti_tx_ring_nic[cur % 128].ti_flags |=
   2297   1.31   thorpej 	    TI_BDFLAG_END;
   2298   1.31   thorpej 
   2299   1.31   thorpej 	/* Sync the packet's DMA map. */
   2300   1.31   thorpej 	bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   2301   1.31   thorpej 	    BUS_DMASYNC_PREWRITE);
   2302   1.31   thorpej 
   2303   1.31   thorpej 	sc->ti_cdata.ti_tx_chain[cur] = m_head;
   2304   1.48     lukem 	SIMPLEQ_REMOVE_HEAD(&sc->txdma_list, link);
   2305   1.31   thorpej 	sc->txdma[cur] = dma;
   2306   1.31   thorpej 	sc->ti_txcnt += cnt;
   2307   1.31   thorpej 
   2308   1.31   thorpej 	*txidx = frag;
   2309   1.31   thorpej 
   2310   1.77       tnn 	return (0);
   2311   1.31   thorpej }
   2312   1.31   thorpej 
   2313   1.77       tnn static int
   2314  1.108   msaitoh ti_encap_tigon2(struct ti_softc *sc, struct mbuf *m_head, uint32_t *txidx)
   2315   1.31   thorpej {
   2316   1.31   thorpej 	struct ti_tx_desc	*f = NULL;
   2317  1.108   msaitoh 	uint32_t		frag, firstfrag, cur, cnt = 0;
   2318   1.31   thorpej 	struct txdmamap_pool_entry *dma;
   2319   1.31   thorpej 	bus_dmamap_t dmamap;
   2320   1.31   thorpej 	int error, i;
   2321  1.108   msaitoh 	uint16_t csum_flags = 0;
   2322   1.31   thorpej 
   2323   1.31   thorpej 	dma = SIMPLEQ_FIRST(&sc->txdma_list);
   2324   1.31   thorpej 	if (dma == NULL) {
   2325   1.31   thorpej 		return ENOMEM;
   2326   1.31   thorpej 	}
   2327   1.31   thorpej 	dmamap = dma->dmamap;
   2328   1.31   thorpej 
   2329   1.40   thorpej 	error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m_head,
   2330   1.49    bouyer 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   2331   1.31   thorpej 	if (error) {
   2332   1.31   thorpej 		struct mbuf *m;
   2333   1.68  christos 		int j = 0;
   2334   1.31   thorpej 		for (m = m_head; m; m = m->m_next)
   2335   1.68  christos 			j++;
   2336   1.31   thorpej 		printf("ti_encap: bus_dmamap_load_mbuf (len %d, %d frags) "
   2337   1.68  christos 		       "error %d\n", m_head->m_pkthdr.len, j, error);
   2338   1.31   thorpej 		return (ENOMEM);
   2339   1.31   thorpej 	}
   2340   1.31   thorpej 
   2341   1.35   thorpej 	cur = firstfrag = frag = *txidx;
   2342   1.31   thorpej 
   2343   1.31   thorpej 	if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4) {
   2344   1.31   thorpej 		/* IP header checksum field must be 0! */
   2345   1.31   thorpej 		csum_flags |= TI_BDFLAG_IP_CKSUM;
   2346   1.31   thorpej 	}
   2347  1.108   msaitoh 	if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4))
   2348   1.31   thorpej 		csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
   2349   1.31   thorpej 
   2350   1.31   thorpej 	/* XXX fragmented packet checksum capability? */
   2351   1.31   thorpej 
   2352   1.31   thorpej 	/*
   2353  1.108   msaitoh 	 * Start packing the mbufs in this chain into
   2354   1.31   thorpej 	 * the fragment pointers. Stop when we run out
   2355  1.108   msaitoh 	 * of fragments or hit the end of the mbuf chain.
   2356   1.31   thorpej 	 */
   2357   1.31   thorpej 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   2358   1.31   thorpej 		f = &sc->ti_rdata->ti_tx_ring[frag];
   2359   1.31   thorpej 		if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
   2360   1.31   thorpej 			break;
   2361   1.31   thorpej 		TI_HOSTADDR(f->ti_addr) = dmamap->dm_segs[i].ds_addr;
   2362   1.31   thorpej 		f->ti_len = dmamap->dm_segs[i].ds_len;
   2363   1.31   thorpej 		f->ti_flags = csum_flags;
   2364  1.102  knakahar 		if (vlan_has_tag(m_head)) {
   2365   1.31   thorpej 			f->ti_flags |= TI_BDFLAG_VLAN_TAG;
   2366  1.102  knakahar 			f->ti_vlan_tag = vlan_get_tag(m_head);
   2367   1.31   thorpej 		} else {
   2368   1.31   thorpej 			f->ti_vlan_tag = 0;
   2369   1.31   thorpej 		}
   2370   1.31   thorpej 		/*
   2371   1.31   thorpej 		 * Sanity check: avoid coming within 16 descriptors
   2372   1.31   thorpej 		 * of the end of the ring.
   2373   1.31   thorpej 		 */
   2374   1.31   thorpej 		if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
   2375   1.77       tnn 			return (ENOBUFS);
   2376   1.31   thorpej 		cur = frag;
   2377   1.31   thorpej 		TI_INC(frag, TI_TX_RING_CNT);
   2378   1.31   thorpej 		cnt++;
   2379    1.1  drochner 	}
   2380    1.1  drochner 
   2381    1.1  drochner 	if (i < dmamap->dm_nsegs)
   2382   1.77       tnn 		return (ENOBUFS);
   2383    1.1  drochner 
   2384    1.1  drochner 	if (frag == sc->ti_tx_saved_considx)
   2385   1.77       tnn 		return (ENOBUFS);
   2386    1.1  drochner 
   2387   1.31   thorpej 	sc->ti_rdata->ti_tx_ring[cur].ti_flags |= TI_BDFLAG_END;
   2388   1.29   thorpej 
   2389   1.29   thorpej 	/* Sync the packet's DMA map. */
   2390   1.29   thorpej 	bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   2391   1.29   thorpej 	    BUS_DMASYNC_PREWRITE);
   2392   1.35   thorpej 
   2393   1.35   thorpej 	/* Sync the descriptors we are using. */
   2394   1.35   thorpej 	TI_CDTXSYNC(sc, firstfrag, cnt, BUS_DMASYNC_PREWRITE);
   2395   1.29   thorpej 
   2396    1.1  drochner 	sc->ti_cdata.ti_tx_chain[cur] = m_head;
   2397   1.48     lukem 	SIMPLEQ_REMOVE_HEAD(&sc->txdma_list, link);
   2398    1.1  drochner 	sc->txdma[cur] = dma;
   2399    1.1  drochner 	sc->ti_txcnt += cnt;
   2400    1.1  drochner 
   2401    1.1  drochner 	*txidx = frag;
   2402    1.1  drochner 
   2403   1.77       tnn 	return (0);
   2404    1.1  drochner }
   2405    1.1  drochner 
   2406    1.1  drochner /*
   2407    1.1  drochner  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
   2408    1.1  drochner  * to the mbuf data regions directly in the transmit descriptors.
   2409    1.1  drochner  */
   2410   1.77       tnn static void
   2411   1.77       tnn ti_start(struct ifnet *ifp)
   2412    1.1  drochner {
   2413  1.108   msaitoh 	struct ti_softc	*sc;
   2414  1.108   msaitoh 	struct mbuf	*m_head = NULL;
   2415  1.108   msaitoh 	uint32_t	prodidx = 0;
   2416    1.1  drochner 
   2417    1.1  drochner 	sc = ifp->if_softc;
   2418    1.1  drochner 
   2419    1.1  drochner 	prodidx = CSR_READ_4(sc, TI_MB_SENDPROD_IDX);
   2420    1.1  drochner 
   2421   1.16   thorpej 	while (sc->ti_cdata.ti_tx_chain[prodidx] == NULL) {
   2422   1.16   thorpej 		IFQ_POLL(&ifp->if_snd, m_head);
   2423    1.1  drochner 		if (m_head == NULL)
   2424    1.1  drochner 			break;
   2425    1.1  drochner 
   2426    1.1  drochner 		/*
   2427    1.1  drochner 		 * Pack the data into the transmit ring. If we
   2428    1.1  drochner 		 * don't have room, set the OACTIVE flag and wait
   2429    1.1  drochner 		 * for the NIC to drain the ring.
   2430    1.1  drochner 		 */
   2431   1.31   thorpej 		if ((*sc->sc_tx_encap)(sc, m_head, &prodidx)) {
   2432    1.1  drochner 			ifp->if_flags |= IFF_OACTIVE;
   2433    1.1  drochner 			break;
   2434    1.1  drochner 		}
   2435   1.16   thorpej 
   2436   1.16   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   2437    1.1  drochner 
   2438    1.1  drochner 		/*
   2439    1.1  drochner 		 * If there's a BPF listener, bounce a copy of this frame
   2440    1.1  drochner 		 * to him.
   2441    1.1  drochner 		 */
   2442  1.104   msaitoh 		bpf_mtap(ifp, m_head, BPF_D_OUT);
   2443    1.1  drochner 	}
   2444    1.1  drochner 
   2445    1.1  drochner 	/* Transmit */
   2446    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, prodidx);
   2447    1.1  drochner 
   2448  1.108   msaitoh 	/* Set a timeout in case the chip goes out to lunch. */
   2449    1.1  drochner 	ifp->if_timer = 5;
   2450    1.1  drochner }
   2451    1.1  drochner 
   2452   1.77       tnn static void
   2453   1.77       tnn ti_init(void *xsc)
   2454    1.1  drochner {
   2455    1.1  drochner 	struct ti_softc		*sc = xsc;
   2456  1.108   msaitoh 	int			s;
   2457    1.1  drochner 
   2458   1.18   thorpej 	s = splnet();
   2459    1.1  drochner 
   2460    1.1  drochner 	/* Cancel pending I/O and flush buffers. */
   2461    1.1  drochner 	ti_stop(sc);
   2462    1.1  drochner 
   2463    1.1  drochner 	/* Init the gen info block, ring control blocks and firmware. */
   2464    1.1  drochner 	if (ti_gibinit(sc)) {
   2465   1.91       chs 		aprint_error_dev(sc->sc_dev, "initialization failure\n");
   2466    1.1  drochner 		splx(s);
   2467    1.1  drochner 		return;
   2468    1.1  drochner 	}
   2469    1.1  drochner 
   2470    1.1  drochner 	splx(s);
   2471    1.1  drochner }
   2472    1.1  drochner 
   2473   1.77       tnn static void
   2474   1.77       tnn ti_init2(struct ti_softc *sc)
   2475    1.1  drochner {
   2476    1.1  drochner 	struct ti_cmd_desc	cmd;
   2477    1.1  drochner 	struct ifnet		*ifp;
   2478  1.108   msaitoh 	const uint8_t		*m;
   2479    1.1  drochner 	struct ifmedia		*ifm;
   2480    1.1  drochner 	int			tmp;
   2481    1.1  drochner 
   2482    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   2483    1.1  drochner 
   2484    1.1  drochner 	/* Specify MTU and interface index. */
   2485   1.91       chs 	CSR_WRITE_4(sc, TI_GCR_IFINDEX, device_unit(sc->sc_dev)); /* ??? */
   2486   1.23   thorpej 
   2487   1.23   thorpej 	tmp = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   2488   1.23   thorpej 	if (sc->ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   2489   1.23   thorpej 		tmp += ETHER_VLAN_ENCAP_LEN;
   2490   1.23   thorpej 	CSR_WRITE_4(sc, TI_GCR_IFMTU, tmp);
   2491   1.23   thorpej 
   2492    1.1  drochner 	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
   2493    1.1  drochner 
   2494    1.1  drochner 	/* Load our MAC address. */
   2495  1.108   msaitoh 	m = (const uint8_t *)CLLADDR(ifp->if_sadl);
   2496    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_PAR0, (m[0] << 8) | m[1]);
   2497    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_PAR1, (m[2] << 24) | (m[3] << 16)
   2498    1.1  drochner 		    | (m[4] << 8) | m[5]);
   2499    1.1  drochner 	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
   2500    1.1  drochner 
   2501    1.1  drochner 	/* Enable or disable promiscuous mode as needed. */
   2502    1.1  drochner 	if (ifp->if_flags & IFF_PROMISC) {
   2503    1.1  drochner 		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
   2504    1.1  drochner 	} else {
   2505    1.1  drochner 		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
   2506    1.1  drochner 	}
   2507    1.1  drochner 
   2508    1.1  drochner 	/* Program multicast filter. */
   2509    1.1  drochner 	ti_setmulti(sc);
   2510    1.1  drochner 
   2511    1.1  drochner 	/*
   2512    1.1  drochner 	 * If this is a Tigon 1, we should tell the
   2513    1.1  drochner 	 * firmware to use software packet filtering.
   2514    1.1  drochner 	 */
   2515    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON) {
   2516    1.1  drochner 		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
   2517    1.1  drochner 	}
   2518    1.1  drochner 
   2519    1.1  drochner 	/* Init RX ring. */
   2520    1.1  drochner 	ti_init_rx_ring_std(sc);
   2521    1.1  drochner 
   2522    1.1  drochner 	/* Init jumbo RX ring. */
   2523   1.12    bouyer 	if (ifp->if_mtu > (MCLBYTES - ETHER_HDR_LEN - ETHER_CRC_LEN))
   2524    1.1  drochner 		ti_init_rx_ring_jumbo(sc);
   2525    1.1  drochner 
   2526    1.1  drochner 	/*
   2527    1.1  drochner 	 * If this is a Tigon 2, we can also configure the
   2528    1.1  drochner 	 * mini ring.
   2529    1.1  drochner 	 */
   2530    1.1  drochner 	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
   2531    1.1  drochner 		ti_init_rx_ring_mini(sc);
   2532    1.1  drochner 
   2533    1.1  drochner 	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
   2534    1.1  drochner 	sc->ti_rx_saved_considx = 0;
   2535    1.1  drochner 
   2536    1.1  drochner 	/* Init TX ring. */
   2537    1.1  drochner 	ti_init_tx_ring(sc);
   2538    1.1  drochner 
   2539    1.1  drochner 	/* Tell firmware we're alive. */
   2540    1.1  drochner 	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
   2541    1.1  drochner 
   2542    1.1  drochner 	/* Enable host interrupts. */
   2543    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
   2544    1.1  drochner 
   2545    1.1  drochner 	ifp->if_flags |= IFF_RUNNING;
   2546    1.1  drochner 	ifp->if_flags &= ~IFF_OACTIVE;
   2547    1.1  drochner 
   2548    1.1  drochner 	/*
   2549    1.1  drochner 	 * Make sure to set media properly. We have to do this
   2550    1.1  drochner 	 * here since we have to issue commands in order to set
   2551    1.1  drochner 	 * the link negotiation and we can't issue commands until
   2552    1.1  drochner 	 * the firmware is running.
   2553    1.1  drochner 	 */
   2554    1.1  drochner 	ifm = &sc->ifmedia;
   2555    1.1  drochner 	tmp = ifm->ifm_media;
   2556    1.1  drochner 	ifm->ifm_media = ifm->ifm_cur->ifm_media;
   2557    1.1  drochner 	ti_ifmedia_upd(ifp);
   2558    1.1  drochner 	ifm->ifm_media = tmp;
   2559    1.1  drochner }
   2560    1.1  drochner 
   2561    1.1  drochner /*
   2562    1.1  drochner  * Set media options.
   2563    1.1  drochner  */
   2564   1.77       tnn static int
   2565   1.77       tnn ti_ifmedia_upd(struct ifnet *ifp)
   2566    1.1  drochner {
   2567    1.1  drochner 	struct ti_softc		*sc;
   2568    1.1  drochner 	struct ifmedia		*ifm;
   2569    1.1  drochner 	struct ti_cmd_desc	cmd;
   2570    1.1  drochner 
   2571    1.1  drochner 	sc = ifp->if_softc;
   2572    1.1  drochner 	ifm = &sc->ifmedia;
   2573    1.1  drochner 
   2574    1.1  drochner 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   2575   1.77       tnn 		return (EINVAL);
   2576    1.1  drochner 
   2577   1.77       tnn 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
   2578    1.1  drochner 	case IFM_AUTO:
   2579  1.108   msaitoh 		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF | TI_GLNK_1000MB |
   2580  1.108   msaitoh 		    TI_GLNK_FULL_DUPLEX | TI_GLNK_RX_FLOWCTL_Y |
   2581  1.108   msaitoh 		    TI_GLNK_AUTONEGENB | TI_GLNK_ENB);
   2582  1.108   msaitoh 		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB | TI_LNK_10MB |
   2583  1.108   msaitoh 		    TI_LNK_FULL_DUPLEX | TI_LNK_HALF_DUPLEX |
   2584  1.108   msaitoh 		    TI_LNK_AUTONEGENB | TI_LNK_ENB);
   2585    1.1  drochner 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
   2586    1.1  drochner 		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
   2587    1.1  drochner 		break;
   2588    1.3   thorpej 	case IFM_1000_SX:
   2589   1.36     bjh21 	case IFM_1000_T:
   2590  1.107   msaitoh 		if ((ifm->ifm_media & IFM_FDX) != 0) {
   2591   1.15    bouyer 			CSR_WRITE_4(sc, TI_GCR_GLINK,
   2592  1.108   msaitoh 			    TI_GLNK_PREF | TI_GLNK_1000MB | TI_GLNK_FULL_DUPLEX
   2593  1.108   msaitoh 			    | TI_GLNK_RX_FLOWCTL_Y | TI_GLNK_ENB);
   2594   1.15    bouyer 		} else {
   2595   1.15    bouyer 			CSR_WRITE_4(sc, TI_GCR_GLINK,
   2596  1.108   msaitoh 			    TI_GLNK_PREF | TI_GLNK_1000MB |
   2597  1.108   msaitoh 			    TI_GLNK_RX_FLOWCTL_Y | TI_GLNK_ENB);
   2598   1.15    bouyer 		}
   2599    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
   2600    1.1  drochner 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
   2601    1.1  drochner 		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
   2602    1.1  drochner 		break;
   2603    1.1  drochner 	case IFM_100_FX:
   2604    1.1  drochner 	case IFM_10_FL:
   2605   1.15    bouyer 	case IFM_100_TX:
   2606   1.15    bouyer 	case IFM_10_T:
   2607    1.1  drochner 		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
   2608  1.108   msaitoh 		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB | TI_LNK_PREF);
   2609   1.15    bouyer 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
   2610   1.15    bouyer 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
   2611    1.1  drochner 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
   2612    1.1  drochner 		} else {
   2613    1.1  drochner 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
   2614    1.1  drochner 		}
   2615  1.107   msaitoh 		if ((ifm->ifm_media & IFM_FDX) != 0) {
   2616    1.1  drochner 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
   2617    1.1  drochner 		} else {
   2618    1.1  drochner 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
   2619    1.1  drochner 		}
   2620    1.1  drochner 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
   2621    1.1  drochner 		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
   2622    1.1  drochner 		break;
   2623    1.1  drochner 	}
   2624    1.1  drochner 
   2625    1.5   thorpej 	sc->ethercom.ec_if.if_baudrate =
   2626    1.5   thorpej 	    ifmedia_baudrate(ifm->ifm_media);
   2627    1.5   thorpej 
   2628   1.77       tnn 	return (0);
   2629    1.1  drochner }
   2630    1.1  drochner 
   2631    1.1  drochner /*
   2632    1.1  drochner  * Report current media status.
   2633    1.1  drochner  */
   2634   1.77       tnn static void
   2635   1.77       tnn ti_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   2636    1.1  drochner {
   2637    1.1  drochner 	struct ti_softc		*sc;
   2638  1.108   msaitoh 	uint32_t		media = 0;
   2639    1.1  drochner 
   2640    1.1  drochner 	sc = ifp->if_softc;
   2641    1.1  drochner 
   2642    1.1  drochner 	ifmr->ifm_status = IFM_AVALID;
   2643    1.1  drochner 	ifmr->ifm_active = IFM_ETHER;
   2644    1.1  drochner 
   2645    1.1  drochner 	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
   2646    1.1  drochner 		return;
   2647    1.1  drochner 
   2648    1.1  drochner 	ifmr->ifm_status |= IFM_ACTIVE;
   2649    1.1  drochner 
   2650   1.15    bouyer 	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
   2651   1.15    bouyer 		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
   2652   1.15    bouyer 		if (sc->ti_copper)
   2653   1.36     bjh21 			ifmr->ifm_active |= IFM_1000_T;
   2654   1.15    bouyer 		else
   2655   1.15    bouyer 			ifmr->ifm_active |= IFM_1000_SX;
   2656   1.15    bouyer 		if (media & TI_GLNK_FULL_DUPLEX)
   2657   1.15    bouyer 			ifmr->ifm_active |= IFM_FDX;
   2658   1.15    bouyer 		else
   2659   1.15    bouyer 			ifmr->ifm_active |= IFM_HDX;
   2660   1.15    bouyer 	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
   2661    1.1  drochner 		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
   2662   1.15    bouyer 		if (sc->ti_copper) {
   2663   1.15    bouyer 			if (media & TI_LNK_100MB)
   2664   1.15    bouyer 				ifmr->ifm_active |= IFM_100_TX;
   2665   1.15    bouyer 			if (media & TI_LNK_10MB)
   2666   1.15    bouyer 				ifmr->ifm_active |= IFM_10_T;
   2667   1.15    bouyer 		} else {
   2668   1.15    bouyer 			if (media & TI_LNK_100MB)
   2669   1.15    bouyer 				ifmr->ifm_active |= IFM_100_FX;
   2670   1.15    bouyer 			if (media & TI_LNK_10MB)
   2671   1.15    bouyer 				ifmr->ifm_active |= IFM_10_FL;
   2672   1.15    bouyer 		}
   2673    1.1  drochner 		if (media & TI_LNK_FULL_DUPLEX)
   2674    1.1  drochner 			ifmr->ifm_active |= IFM_FDX;
   2675    1.1  drochner 		if (media & TI_LNK_HALF_DUPLEX)
   2676    1.1  drochner 			ifmr->ifm_active |= IFM_HDX;
   2677    1.1  drochner 	}
   2678    1.5   thorpej 
   2679    1.5   thorpej 	sc->ethercom.ec_if.if_baudrate =
   2680    1.5   thorpej 	    ifmedia_baudrate(sc->ifmedia.ifm_media);
   2681    1.1  drochner }
   2682    1.1  drochner 
   2683    1.1  drochner static int
   2684   1.77       tnn ti_ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   2685    1.1  drochner {
   2686    1.1  drochner 	struct ifaddr *ifa = (struct ifaddr *) data;
   2687    1.1  drochner 	struct ti_softc *sc = ifp->if_softc;
   2688    1.1  drochner 
   2689   1.26    bouyer 	if ((ifp->if_flags & IFF_UP) == 0) {
   2690   1.26    bouyer 		ifp->if_flags |= IFF_UP;
   2691   1.26    bouyer 		ti_init(sc);
   2692   1.26    bouyer 	}
   2693   1.66     perry 
   2694    1.1  drochner 	switch (cmd) {
   2695   1.82    dyoung 	case SIOCINITIFADDR:
   2696    1.1  drochner 
   2697    1.1  drochner 		switch (ifa->ifa_addr->sa_family) {
   2698    1.1  drochner #ifdef INET
   2699    1.1  drochner 		case AF_INET:
   2700    1.1  drochner 			arp_ifinit(ifp, ifa);
   2701    1.1  drochner 			break;
   2702    1.1  drochner #endif
   2703    1.1  drochner 		default:
   2704    1.1  drochner 			break;
   2705    1.1  drochner 		}
   2706    1.1  drochner 		break;
   2707    1.1  drochner 
   2708    1.1  drochner 	default:
   2709    1.1  drochner 		return (EINVAL);
   2710    1.1  drochner 	}
   2711    1.1  drochner 
   2712    1.1  drochner 	return (0);
   2713    1.1  drochner }
   2714    1.1  drochner 
   2715   1.77       tnn static int
   2716   1.77       tnn ti_ioctl(struct ifnet *ifp, u_long command, void *data)
   2717    1.1  drochner {
   2718    1.1  drochner 	struct ti_softc		*sc = ifp->if_softc;
   2719    1.1  drochner 	struct ifreq		*ifr = (struct ifreq *) data;
   2720    1.1  drochner 	int			s, error = 0;
   2721    1.1  drochner 	struct ti_cmd_desc	cmd;
   2722    1.1  drochner 
   2723   1.18   thorpej 	s = splnet();
   2724    1.1  drochner 
   2725   1.77       tnn 	switch (command) {
   2726   1.82    dyoung 	case SIOCINITIFADDR:
   2727    1.1  drochner 		error = ti_ether_ioctl(ifp, command, data);
   2728    1.1  drochner 		break;
   2729    1.1  drochner 	case SIOCSIFMTU:
   2730   1.80    dyoung 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU_JUMBO)
   2731    1.1  drochner 			error = EINVAL;
   2732  1.109   msaitoh 		else if ((error = ifioctl_common(ifp, command, data))
   2733  1.109   msaitoh 		    == ENETRESET) {
   2734    1.1  drochner 			ti_init(sc);
   2735   1.80    dyoung 			error = 0;
   2736    1.1  drochner 		}
   2737    1.1  drochner 		break;
   2738    1.1  drochner 	case SIOCSIFFLAGS:
   2739   1.82    dyoung 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   2740   1.82    dyoung 			break;
   2741    1.1  drochner 		if (ifp->if_flags & IFF_UP) {
   2742    1.1  drochner 			/*
   2743    1.1  drochner 			 * If only the state of the PROMISC flag changed,
   2744    1.1  drochner 			 * then just use the 'set promisc mode' command
   2745    1.1  drochner 			 * instead of reinitializing the entire NIC. Doing
   2746    1.1  drochner 			 * a full re-init means reloading the firmware and
   2747    1.1  drochner 			 * waiting for it to start up, which may take a
   2748    1.1  drochner 			 * second or two.
   2749    1.1  drochner 			 */
   2750    1.1  drochner 			if (ifp->if_flags & IFF_RUNNING &&
   2751    1.1  drochner 			    ifp->if_flags & IFF_PROMISC &&
   2752    1.1  drochner 			    !(sc->ti_if_flags & IFF_PROMISC)) {
   2753    1.1  drochner 				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
   2754    1.1  drochner 				    TI_CMD_CODE_PROMISC_ENB, 0);
   2755    1.1  drochner 			} else if (ifp->if_flags & IFF_RUNNING &&
   2756    1.1  drochner 			    !(ifp->if_flags & IFF_PROMISC) &&
   2757    1.1  drochner 			    sc->ti_if_flags & IFF_PROMISC) {
   2758    1.1  drochner 				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
   2759    1.1  drochner 				    TI_CMD_CODE_PROMISC_DIS, 0);
   2760    1.1  drochner 			} else
   2761    1.1  drochner 				ti_init(sc);
   2762    1.1  drochner 		} else {
   2763    1.1  drochner 			if (ifp->if_flags & IFF_RUNNING) {
   2764    1.1  drochner 				ti_stop(sc);
   2765    1.1  drochner 			}
   2766    1.1  drochner 		}
   2767    1.1  drochner 		sc->ti_if_flags = ifp->if_flags;
   2768    1.1  drochner 		error = 0;
   2769    1.1  drochner 		break;
   2770    1.1  drochner 	default:
   2771   1.80    dyoung 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   2772   1.80    dyoung 			break;
   2773   1.80    dyoung 
   2774   1.80    dyoung 		error = 0;
   2775   1.80    dyoung 
   2776   1.80    dyoung 		if (command == SIOCSIFCAP)
   2777   1.80    dyoung 			ti_init(sc);
   2778   1.80    dyoung 		else if (command != SIOCADDMULTI && command != SIOCDELMULTI)
   2779   1.80    dyoung 			;
   2780   1.80    dyoung 		else if (ifp->if_flags & IFF_RUNNING)
   2781   1.80    dyoung 			ti_setmulti(sc);
   2782    1.1  drochner 		break;
   2783    1.1  drochner 	}
   2784    1.1  drochner 
   2785    1.1  drochner 	(void)splx(s);
   2786    1.1  drochner 
   2787   1.77       tnn 	return (error);
   2788    1.1  drochner }
   2789    1.1  drochner 
   2790   1.77       tnn static void
   2791   1.77       tnn ti_watchdog(struct ifnet *ifp)
   2792    1.1  drochner {
   2793    1.1  drochner 	struct ti_softc		*sc;
   2794    1.1  drochner 
   2795    1.1  drochner 	sc = ifp->if_softc;
   2796    1.1  drochner 
   2797   1.91       chs 	aprint_error_dev(sc->sc_dev, "watchdog timeout -- resetting\n");
   2798    1.1  drochner 	ti_stop(sc);
   2799    1.1  drochner 	ti_init(sc);
   2800    1.1  drochner 
   2801    1.1  drochner 	ifp->if_oerrors++;
   2802    1.1  drochner }
   2803    1.1  drochner 
   2804    1.1  drochner /*
   2805    1.1  drochner  * Stop the adapter and free any mbufs allocated to the
   2806    1.1  drochner  * RX and TX lists.
   2807    1.1  drochner  */
   2808   1.77       tnn static void
   2809   1.77       tnn ti_stop(struct ti_softc *sc)
   2810    1.1  drochner {
   2811    1.1  drochner 	struct ifnet		*ifp;
   2812    1.1  drochner 	struct ti_cmd_desc	cmd;
   2813    1.1  drochner 
   2814    1.1  drochner 	ifp = &sc->ethercom.ec_if;
   2815    1.1  drochner 
   2816    1.1  drochner 	/* Disable host interrupts. */
   2817    1.1  drochner 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   2818    1.1  drochner 	/*
   2819    1.1  drochner 	 * Tell firmware we're shutting down.
   2820    1.1  drochner 	 */
   2821    1.1  drochner 	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
   2822    1.1  drochner 
   2823    1.1  drochner 	/* Halt and reinitialize. */
   2824    1.1  drochner 	ti_chipinit(sc);
   2825    1.1  drochner 	ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
   2826    1.1  drochner 	ti_chipinit(sc);
   2827    1.1  drochner 
   2828    1.1  drochner 	/* Free the RX lists. */
   2829    1.1  drochner 	ti_free_rx_ring_std(sc);
   2830    1.1  drochner 
   2831    1.1  drochner 	/* Free jumbo RX list. */
   2832    1.1  drochner 	ti_free_rx_ring_jumbo(sc);
   2833    1.1  drochner 
   2834    1.1  drochner 	/* Free mini RX list. */
   2835    1.1  drochner 	ti_free_rx_ring_mini(sc);
   2836    1.1  drochner 
   2837    1.1  drochner 	/* Free TX buffers. */
   2838    1.1  drochner 	ti_free_tx_ring(sc);
   2839    1.1  drochner 
   2840    1.1  drochner 	sc->ti_ev_prodidx.ti_idx = 0;
   2841    1.1  drochner 	sc->ti_return_prodidx.ti_idx = 0;
   2842    1.1  drochner 	sc->ti_tx_considx.ti_idx = 0;
   2843    1.1  drochner 	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
   2844    1.1  drochner 
   2845    1.1  drochner 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2846    1.1  drochner }
   2847    1.1  drochner 
   2848    1.1  drochner /*
   2849    1.1  drochner  * Stop all chip I/O so that the kernel's probe routines don't
   2850    1.1  drochner  * get confused by errant DMAs when rebooting.
   2851    1.1  drochner  */
   2852   1.86   tsutsui static bool
   2853   1.86   tsutsui ti_shutdown(device_t self, int howto)
   2854    1.1  drochner {
   2855   1.86   tsutsui 	struct ti_softc *sc;
   2856    1.1  drochner 
   2857   1.86   tsutsui 	sc = device_private(self);
   2858    1.1  drochner 	ti_chipinit(sc);
   2859   1.86   tsutsui 
   2860   1.86   tsutsui 	return true;
   2861    1.1  drochner }
   2862