Home | History | Annotate | Line # | Download | only in pci
if_lii.c revision 1.1
      1 /*	$NetBSD: if_lii.c,v 1.1 2008/03/29 00:16:26 cube Exp $	*/
      2 
      3 /*
      4  *  Copyright (c) 2008 The NetBSD Foundation.
      5  *  All rights reserved.
      6  *
      7  *  Redistribution and use in source and binary forms, with or without
      8  *  modification, are permitted provided that the following conditions
      9  *  are met:
     10  *  1. Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  *  2. Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  *  3. Neither the name of The NetBSD Foundation nor the names of its
     16  *     contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  *  POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Driver for Attansic/Atheros's L2 Fast Ethernet controller
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: if_lii.c,v 1.1 2008/03/29 00:16:26 cube Exp $");
     38 
     39 #include "bpfilter.h"
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/types.h>
     44 #include <sys/device.h>
     45 #include <sys/endian.h>
     46 #include <sys/kernel.h>
     47 #include <sys/sockio.h>
     48 
     49 #include <net/if.h>
     50 #include <net/if_media.h>
     51 #include <net/if_ether.h>
     52 
     53 #if NBPFILTER > 0
     54 #include <net/bpf.h>
     55 #endif
     56 
     57 #include <dev/mii/mii.h>
     58 #include <dev/mii/miivar.h>
     59 
     60 #include <dev/pci/pcireg.h>
     61 #include <dev/pci/pcivar.h>
     62 #include <dev/pci/pcidevs.h>
     63 
     64 #include <dev/pci/if_liireg.h>
     65 
     66 /* #define LII_DEBUG */
     67 #ifdef LII_DEBUG
     68 #define DPRINTF(x)	printf x
     69 #else
     70 #define DPRINTF(x)
     71 #endif
     72 
     73 struct lii_softc {
     74 	device_t		sc_dev;
     75 	pci_chipset_tag_t	sc_pc;
     76 	pcitag_t		sc_tag;
     77 
     78 	bus_space_tag_t		sc_mmiot;
     79 	bus_space_handle_t	sc_mmioh;
     80 
     81 	/*
     82 	 * We allocate a big chunk of DMA-safe memory for all data exchanges.
     83 	 * It is unfortunate that this chip doesn't seem to do scatter-gather.
     84 	 */
     85 	bus_dma_tag_t		sc_dmat;
     86 	bus_dmamap_t		sc_ringmap;
     87 	bus_dma_segment_t	sc_ringseg;
     88 
     89 	uint8_t			*sc_ring; /* the whole area */
     90 	size_t			sc_ringsize;
     91 
     92 	struct rx_pkt		*sc_rxp; /* the part used for RX */
     93 	struct tx_pkt_status	*sc_txs; /* the parts used for TX */
     94 	bus_addr_t		sc_txsp;
     95 	char			*sc_txdbase;
     96 	bus_addr_t		sc_txdp;
     97 
     98 	unsigned int		sc_rxcur;
     99 	/* the active area is [ack; cur[ */
    100 	int			sc_txs_cur;
    101 	int			sc_txs_ack;
    102 	int			sc_txd_cur;
    103 	int			sc_txd_ack;
    104 	bool			sc_free_tx_slots;
    105 
    106 	void			*sc_ih;
    107 
    108 	struct ethercom		sc_ec;
    109 	struct mii_data		sc_mii;
    110 	callout_t		sc_tick_ch;
    111 	uint8_t			sc_eaddr[ETHER_ADDR_LEN];
    112 
    113 	int			(*sc_memread)(struct lii_softc *, uint32_t,
    114 				     uint32_t *);
    115 };
    116 
    117 static int	lii_match(device_t, cfdata_t, void *);
    118 static void	lii_attach(device_t, device_t, void *);
    119 
    120 static int	lii_reset(struct lii_softc *);
    121 static bool	lii_eeprom_present(struct lii_softc *);
    122 static int	lii_read_macaddr(struct lii_softc *, uint8_t *);
    123 static int	lii_eeprom_read(struct lii_softc *, uint32_t, uint32_t *);
    124 static void	lii_spi_configure(struct lii_softc *);
    125 static int	lii_spi_read(struct lii_softc *, uint32_t, uint32_t *);
    126 static void	lii_setmulti(struct lii_softc *);
    127 static void	lii_tick(void *);
    128 
    129 static int	lii_alloc_rings(struct lii_softc *);
    130 static int	lii_free_tx_space(struct lii_softc *);
    131 
    132 static int	lii_mii_readreg(device_t, int, int);
    133 static void	lii_mii_writereg(device_t, int, int, int);
    134 static void	lii_mii_statchg(device_t);
    135 
    136 static int	lii_media_change(struct ifnet *);
    137 static void	lii_media_status(struct ifnet *, struct ifmediareq *);
    138 
    139 static int	lii_init(struct ifnet *);
    140 static void	lii_start(struct ifnet *);
    141 static void	lii_stop(struct ifnet *, int);
    142 static void	lii_watchdog(struct ifnet *);
    143 static int	lii_ioctl(struct ifnet *, u_long, void *);
    144 
    145 static int	lii_intr(void *);
    146 static void	lii_rxintr(struct lii_softc *);
    147 static void	lii_txintr(struct lii_softc *);
    148 
    149 CFATTACH_DECL_NEW(lii, sizeof(struct lii_softc),
    150     lii_match, lii_attach, NULL, NULL);
    151 
    152 /* #define LII_DEBUG_REGS */
    153 #ifndef LII_DEBUG_REGS
    154 #define AT_READ_4(sc,reg) \
    155     bus_space_read_4((sc)->sc_mmiot, (sc)->sc_mmioh, (reg))
    156 #define AT_READ_2(sc,reg) \
    157     bus_space_read_2((sc)->sc_mmiot, (sc)->sc_mmioh, (reg))
    158 #define AT_READ_1(sc,reg) \
    159     bus_space_read_1((sc)->sc_mmiot, (sc)->sc_mmioh, (reg))
    160 #define AT_WRITE_4(sc,reg,val) \
    161     bus_space_write_4((sc)->sc_mmiot, (sc)->sc_mmioh, (reg), (val))
    162 #define AT_WRITE_2(sc,reg,val) \
    163     bus_space_write_2((sc)->sc_mmiot, (sc)->sc_mmioh, (reg), (val))
    164 #define AT_WRITE_1(sc,reg,val) \
    165     bus_space_write_1((sc)->sc_mmiot, (sc)->sc_mmioh, (reg), (val))
    166 #else
    167 static inline uint32_t
    168 AT_READ_4(struct lii_softc *sc, bus_size_t reg)
    169 {
    170 	uint32_t r = bus_space_read_4(sc->sc_mmiot, sc->sc_mmioh, reg);
    171 	printf("AT_READ_4(%x) = %x\n", (unsigned int)reg, r);
    172 	return r;
    173 }
    174 
    175 static inline uint16_t
    176 AT_READ_2(struct lii_softc *sc, bus_size_t reg)
    177 {
    178 	uint16_t r = bus_space_read_2(sc->sc_mmiot, sc->sc_mmioh, reg);
    179 	printf("AT_READ_2(%x) = %x\n", (unsigned int)reg, r);
    180 	return r;
    181 }
    182 
    183 static inline uint8_t
    184 AT_READ_1(struct lii_softc *sc, bus_size_t reg)
    185 {
    186 	uint8_t r = bus_space_read_1(sc->sc_mmiot, sc->sc_mmioh, reg);
    187 	printf("AT_READ_1(%x) = %x\n", (unsigned int)reg, r);
    188 	return r;
    189 }
    190 
    191 static inline void
    192 AT_WRITE_4(struct lii_softc *sc, bus_size_t reg, uint32_t val)
    193 {
    194 	printf("AT_WRITE_4(%x, %x)\n", (unsigned int)reg, val);
    195 	bus_space_write_4(sc->sc_mmiot, sc->sc_mmioh, reg, val);
    196 }
    197 
    198 static inline void
    199 AT_WRITE_2(struct lii_softc *sc, bus_size_t reg, uint16_t val)
    200 {
    201 	printf("AT_WRITE_2(%x, %x)\n", (unsigned int)reg, val);
    202 	bus_space_write_2(sc->sc_mmiot, sc->sc_mmioh, reg, val);
    203 }
    204 
    205 static inline void
    206 AT_WRITE_1(struct lii_softc *sc, bus_size_t reg, uint8_t val)
    207 {
    208 	printf("AT_WRITE_1(%x, %x)\n", (unsigned int)reg, val);
    209 	bus_space_write_1(sc->sc_mmiot, sc->sc_mmioh, reg, val);
    210 }
    211 #endif
    212 
    213 /*
    214  * Those are the default Linux parameters.
    215  */
    216 
    217 #define AT_TXD_NUM		64
    218 #define AT_TXD_BUFFER_SIZE	8192
    219 #define AT_RXD_NUM		64
    220 
    221 /*
    222  * Assuming (you know what that word makes of you) the chunk of memory
    223  * bus_dmamem_alloc returns us is 128-byte aligned, we won't use the
    224  * first 120 bytes of it, so that the space for the packets, and not the
    225  * whole descriptors themselves, are on a 128-byte boundary.
    226  */
    227 
    228 #define AT_RXD_PADDING		120
    229 
    230 static int
    231 lii_match(device_t parent, cfdata_t cfmatch, void *aux)
    232 {
    233 	struct pci_attach_args *pa = aux;
    234 
    235 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ATTANSIC &&
    236 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ATTANSIC_ETHERNET_100);
    237 }
    238 
    239 static void
    240 lii_attach(device_t parent, device_t self, void *aux)
    241 {
    242 	struct lii_softc *sc = device_private(self);
    243 	struct pci_attach_args *pa = aux;
    244 	uint8_t eaddr[ETHER_ADDR_LEN];
    245 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    246 	pci_intr_handle_t ih;
    247 	const char *intrstr;
    248 	pcireg_t cmd;
    249 
    250 	aprint_naive("\n");
    251 	aprint_normal(": Attansic/Atheros L2 Fast Ethernet\n");
    252 
    253 	sc->sc_dev = self;
    254 	sc->sc_pc = pa->pa_pc;
    255 	sc->sc_tag = pa->pa_tag;
    256 	sc->sc_dmat = pa->pa_dmat;
    257 
    258 	cmd = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
    259 	cmd |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    260 	cmd &= ~PCI_COMMAND_IO_ENABLE;
    261 	pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG, cmd);
    262 
    263 	switch (cmd = pci_mapreg_type(sc->sc_pc, sc->sc_tag, PCI_MAPREG_START)) {
    264 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    265 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT_1M:
    266 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    267 		break;
    268 	default:
    269 		aprint_error_dev(self, "invalid base address register\n");
    270 		break;
    271 	}
    272 	if (pci_mapreg_map(pa, PCI_MAPREG_START, cmd, 0,
    273 	    &sc->sc_mmiot, &sc->sc_mmioh, NULL, NULL) != 0) {
    274 		aprint_error_dev(self, "failed to map registers\n");
    275 		return;
    276 	}
    277 
    278 	if (lii_reset(sc))
    279 		return;
    280 
    281 	lii_spi_configure(sc);
    282 
    283 	if (lii_eeprom_present(sc))
    284 		sc->sc_memread = lii_eeprom_read;
    285 	else
    286 		sc->sc_memread = lii_spi_read;
    287 
    288 	if (lii_read_macaddr(sc, eaddr))
    289 		return;
    290 	memcpy(sc->sc_eaddr, eaddr, ETHER_ADDR_LEN);
    291 
    292 	aprint_normal_dev(self, "Ethernet address %s\n",
    293 	    ether_sprintf(eaddr));
    294 
    295 	if (pci_intr_map(pa, &ih) != 0) {
    296 		aprint_error_dev(self, "failed to map interrupt\n");
    297 		return;
    298 	}
    299 	intrstr = pci_intr_string(sc->sc_pc, ih);
    300 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, lii_intr, sc);
    301 	if (sc->sc_ih == NULL) {
    302 		aprint_error_dev(self, "failed to establish interrupt");
    303 		if (intrstr != NULL)
    304 			aprint_error(" at %s", intrstr);
    305 		aprint_error("\n");
    306 		return;
    307 	}
    308 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    309 
    310 	if (lii_alloc_rings(sc)) {
    311 		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    312 		return;
    313 	}
    314 
    315 	callout_init(&sc->sc_tick_ch, 0);
    316 	callout_setfunc(&sc->sc_tick_ch, lii_tick, sc);
    317 
    318 	sc->sc_mii.mii_ifp = ifp;
    319 	sc->sc_mii.mii_readreg = lii_mii_readreg;
    320 	sc->sc_mii.mii_writereg = lii_mii_writereg;
    321 	sc->sc_mii.mii_statchg = lii_mii_statchg;
    322 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, lii_media_change,
    323 	    lii_media_status);
    324 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
    325 	    MII_OFFSET_ANY, 0);
    326 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    327 
    328 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    329 	ifp->if_softc = sc;
    330 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    331 	ifp->if_ioctl = lii_ioctl;
    332 	ifp->if_start = lii_start;
    333 	ifp->if_watchdog = lii_watchdog;
    334 	ifp->if_init = lii_init;
    335 	ifp->if_stop = lii_stop;
    336 	IFQ_SET_READY(&ifp->if_snd);
    337 
    338 	/*
    339 	 * While the device does support HW VLAN tagging, there is no
    340 	 * real point using that feature.
    341 	 */
    342 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
    343 
    344 	if_attach(ifp);
    345 	ether_ifattach(ifp, eaddr);
    346 
    347 	return;
    348 }
    349 
    350 static int
    351 lii_reset(struct lii_softc *sc)
    352 {
    353 	int i;
    354 
    355 	DPRINTF(("lii_reset\n"));
    356 
    357 	AT_WRITE_4(sc, ATL2_SMC, SMC_SOFT_RST);
    358 	DELAY(1000);
    359 
    360 	for (i = 0; i < 10; ++i) {
    361 		if (AT_READ_4(sc, ATL2_BIS) == 0)
    362 			break;
    363 		DELAY(1000);
    364 	}
    365 
    366 	if (i == 10) {
    367 		aprint_error_dev(sc->sc_dev, "reset failed\n");
    368 		return 1;
    369 	}
    370 
    371 	AT_WRITE_4(sc, ATL2_PHYC, PHYC_ENABLE);
    372 	DELAY(10);
    373 
    374 	/* Init PCI-Express module */
    375 	/* Magic Numbers Warning */
    376 	AT_WRITE_4(sc, ATL2_PCELTM, PCELTM_DEF);
    377 	AT_WRITE_4(sc, ATL2_PCEDTXC, PCEDTX_DEF);
    378 
    379 	return 0;
    380 }
    381 
    382 static bool
    383 lii_eeprom_present(struct lii_softc *sc)
    384 {
    385 	/*
    386 	 * The Linux driver does this, but then it has a very weird way of
    387 	 * checking whether the PCI configuration space exposes the Vital
    388 	 * Product Data capability, so maybe it's not really needed.
    389 	 */
    390 
    391 #ifdef weirdloonix
    392 	uint32_t val;
    393 
    394 	val = AT_READ_4(sc, ATL2_SFC);
    395 	if (val & SFC_EN_VPD)
    396 		AT_WRITE_4(sc, ATL2_SFC, val & ~(SFC_EN_VPD));
    397 #endif
    398 
    399 	return pci_get_capability(sc->sc_pc, sc->sc_tag, PCI_CAP_VPD,
    400 	    NULL, NULL) == 1;
    401 }
    402 
    403 static int
    404 lii_eeprom_read(struct lii_softc *sc, uint32_t reg, uint32_t *val)
    405 {
    406 	int r = pci_vpd_read(sc->sc_pc, sc->sc_tag, reg, 1, (pcireg_t *)val);
    407 
    408 	DPRINTF(("lii_eeprom_read(%x) = %x\n", reg, *val));
    409 
    410 	return r;
    411 }
    412 
    413 static void
    414 lii_spi_configure(struct lii_softc *sc)
    415 {
    416 	/*
    417 	 * We don't offer a way to configure the SPI Flash vendor parameter, so
    418 	 * the table is given for reference
    419 	 */
    420 	static const struct lii_spi_flash_vendor {
    421 	    const char *sfv_name;
    422 	    const uint8_t sfv_opcodes[9];
    423 	} lii_sfv[] = {
    424 	    { "Atmel", { 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62 } },
    425 	    { "SST",   { 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60 } },
    426 	    { "ST",    { 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xab, 0xd8, 0xc7 } },
    427 	};
    428 #define SF_OPCODE_WRSR	0
    429 #define SF_OPCODE_READ	1
    430 #define SF_OPCODE_PRGM	2
    431 #define SF_OPCODE_WREN	3
    432 #define SF_OPCODE_WRDI	4
    433 #define SF_OPCODE_RDSR	5
    434 #define SF_OPCODE_RDID	6
    435 #define SF_OPCODE_SECT_ER	7
    436 #define SF_OPCODE_CHIP_ER	8
    437 
    438 #define SF_DEFAULT_VENDOR	0
    439 	static const uint8_t vendor = SF_DEFAULT_VENDOR;
    440 
    441 	/*
    442 	 * Why isn't WRDI used?  Heck if I know.
    443 	 */
    444 
    445 	AT_WRITE_1(sc, ATL2_SFOP_WRSR,
    446 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_WRSR]);
    447 	AT_WRITE_1(sc, ATL2_SFOP_READ,
    448 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_READ]);
    449 	AT_WRITE_1(sc, ATL2_SFOP_PROGRAM,
    450 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_PRGM]);
    451 	AT_WRITE_1(sc, ATL2_SFOP_WREN,
    452 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_WREN]);
    453 	AT_WRITE_1(sc, ATL2_SFOP_RDSR,
    454 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_RDSR]);
    455 	AT_WRITE_1(sc, ATL2_SFOP_RDID,
    456 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_RDID]);
    457 	AT_WRITE_1(sc, ATL2_SFOP_SC_ERASE,
    458 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_SECT_ER]);
    459 	AT_WRITE_1(sc, ATL2_SFOP_CHIP_ERASE,
    460 	    lii_sfv[vendor].sfv_opcodes[SF_OPCODE_CHIP_ER]);
    461 }
    462 
    463 #define MAKE_SFC(cssetup, clkhi, clklo, cshold, cshi, ins) \
    464     ( (((cssetup) & SFC_CS_SETUP_MASK)	\
    465         << SFC_CS_SETUP_SHIFT)		\
    466     | (((clkhi) & SFC_CLK_HI_MASK)	\
    467         << SFC_CLK_HI_SHIFT)		\
    468     | (((clklo) & SFC_CLK_LO_MASK)	\
    469         << SFC_CLK_LO_SHIFT)		\
    470     | (((cshold) & SFC_CS_HOLD_MASK)	\
    471         << SFC_CS_HOLD_SHIFT)		\
    472     | (((cshi) & SFC_CS_HI_MASK)	\
    473         << SFC_CS_HI_SHIFT)		\
    474     | (((ins) & SFC_INS_MASK)		\
    475         << SFC_INS_SHIFT))
    476 
    477 /* Magic settings from the Linux driver */
    478 
    479 #define CUSTOM_SPI_CS_SETUP	2
    480 #define CUSTOM_SPI_CLK_HI	2
    481 #define CUSTOM_SPI_CLK_LO	2
    482 #define CUSTOM_SPI_CS_HOLD	2
    483 #define CUSTOM_SPI_CS_HI	3
    484 
    485 static int
    486 lii_spi_read(struct lii_softc *sc, uint32_t reg, uint32_t *val)
    487 {
    488 	uint32_t v;
    489 	int i;
    490 
    491 	AT_WRITE_4(sc, ATL2_SF_DATA, 0);
    492 	AT_WRITE_4(sc, ATL2_SF_ADDR, reg);
    493 
    494 	v = SFC_WAIT_READY |
    495 	    MAKE_SFC(CUSTOM_SPI_CS_SETUP, CUSTOM_SPI_CLK_HI,
    496 	         CUSTOM_SPI_CLK_LO, CUSTOM_SPI_CS_HOLD, CUSTOM_SPI_CS_HI, 1);
    497 
    498 	AT_WRITE_4(sc, ATL2_SFC, v);
    499 	v |= SFC_START;
    500 	AT_WRITE_4(sc, ATL2_SFC, v);
    501 
    502 	for (i = 0; i < 10; ++i) {
    503 		DELAY(1000);
    504 		if (!(AT_READ_4(sc, ATL2_SFC) & SFC_START))
    505 			break;
    506 	}
    507 	if (i == 10)
    508 		return EBUSY;
    509 
    510 	*val = AT_READ_4(sc, ATL2_SF_DATA);
    511 	return 0;
    512 }
    513 
    514 static int
    515 lii_read_macaddr(struct lii_softc *sc, uint8_t *ea)
    516 {
    517 	uint32_t offset = 0x100;
    518 	uint32_t val, val1, addr0 = 0, addr1 = 0;
    519 	uint8_t found = 0;
    520 
    521 	while ((*sc->sc_memread)(sc, offset, &val) == 0) {
    522 		offset += 4;
    523 
    524 		/* Each chunk of data starts with a signature */
    525 		if ((val & 0xff) != 0x5a)
    526 			break;
    527 		if ((*sc->sc_memread)(sc, offset, &val1))
    528 			break;
    529 
    530 		offset += 4;
    531 
    532 		val >>= 16;
    533 		switch (val) {
    534 		case ATL2_MAC_ADDR_0:
    535 			addr0 = val1;
    536 			++found;
    537 			break;
    538 		case ATL2_MAC_ADDR_1:
    539 			addr1 = val1;
    540 			++found;
    541 			break;
    542 		default:
    543 			continue;
    544 		}
    545 	}
    546 
    547 	if (found < 2) {
    548 		aprint_error_dev(sc->sc_dev, "error reading MAC address\n");
    549 		return 1;
    550 	}
    551 
    552 	addr0 = htole32(addr0);
    553 	addr1 = htole32(addr1);
    554 
    555 	if ((addr0 == 0xffffff && (addr1 & 0xffff) == 0xffff) ||
    556 	    (addr0 == 0 && (addr1 & 0xffff) == 0)) {
    557 		addr0 = htole32(AT_READ_4(sc, ATL2_MAC_ADDR_0));
    558 		addr1 = htole32(AT_READ_4(sc, ATL2_MAC_ADDR_1));
    559 	}
    560 
    561 	ea[0] = (addr1 & 0x0000ff00) >> 8;
    562 	ea[1] = (addr1 & 0x000000ff);
    563 	ea[2] = (addr0 & 0xff000000) >> 24;
    564 	ea[3] = (addr0 & 0x00ff0000) >> 16;
    565 	ea[4] = (addr0 & 0x0000ff00) >> 8;
    566 	ea[5] = (addr0 & 0x000000ff);
    567 
    568 	return 0;
    569 }
    570 
    571 static int
    572 lii_mii_readreg(device_t dev, int phy, int reg)
    573 {
    574 	struct lii_softc *sc = device_private(dev);
    575 	uint32_t val;
    576 	int i;
    577 
    578 	val = (reg & MDIOC_REG_MASK) << MDIOC_REG_SHIFT;
    579 
    580 	val |= MDIOC_START | MDIOC_SUP_PREAMBLE;
    581 	val |= MDIOC_CLK_25_4 << MDIOC_CLK_SEL_SHIFT;
    582 
    583 	val |= MDIOC_READ;
    584 
    585 	AT_WRITE_4(sc, ATL2_MDIOC, val);
    586 
    587 	for (i = 0; i < MDIO_WAIT_TIMES; ++i) {
    588 		DELAY(2);
    589 		val = AT_READ_4(sc, ATL2_MDIOC);
    590 		if ((val & (MDIOC_START | MDIOC_BUSY)) == 0)
    591 			break;
    592 	}
    593 
    594 	if (i == MDIO_WAIT_TIMES)
    595 		aprint_error_dev(dev, "timeout reading PHY %d reg %d\n", phy,
    596 		    reg);
    597 
    598 	return (val & 0x0000ffff);
    599 }
    600 
    601 static void
    602 lii_mii_writereg(device_t dev, int phy, int reg, int data)
    603 {
    604 	struct lii_softc *sc = device_private(dev);
    605 	uint32_t val;
    606 	int i;
    607 
    608 	val = (reg & MDIOC_REG_MASK) << MDIOC_REG_SHIFT;
    609 	val |= (data & MDIOC_DATA_MASK) << MDIOC_DATA_SHIFT;
    610 
    611 	val |= MDIOC_START | MDIOC_SUP_PREAMBLE;
    612 	val |= MDIOC_CLK_25_4 << MDIOC_CLK_SEL_SHIFT;
    613 
    614 	/* val |= MDIOC_WRITE; */
    615 
    616 	AT_WRITE_4(sc, ATL2_MDIOC, val);
    617 
    618 	for (i = 0; i < MDIO_WAIT_TIMES; ++i) {
    619 		DELAY(2);
    620 		val = AT_READ_4(sc, ATL2_MDIOC);
    621 		if ((val & (MDIOC_START | MDIOC_BUSY)) == 0)
    622 			break;
    623 	}
    624 
    625 	if (i == MDIO_WAIT_TIMES)
    626 		aprint_error_dev(dev, "timeout writing PHY %d reg %d\n", phy,
    627 		    reg);
    628 }
    629 
    630 static void
    631 lii_mii_statchg(device_t dev)
    632 {
    633 	struct lii_softc *sc = device_private(dev);
    634 	uint32_t val;
    635 
    636 	DPRINTF(("lii_mii_statchg\n"));
    637 
    638 	val = AT_READ_4(sc, ATL2_MACC);
    639 
    640 	if ((sc->sc_mii.mii_media_active & IFM_GMASK) == IFM_FDX)
    641 		val |= MACC_FDX;
    642 	else
    643 		val &= ~MACC_FDX;
    644 
    645 	AT_WRITE_4(sc, ATL2_MACC, val);
    646 }
    647 
    648 static int
    649 lii_media_change(struct ifnet *ifp)
    650 {
    651 	struct lii_softc *sc = ifp->if_softc;
    652 
    653 	DPRINTF(("lii_media_change\n"));
    654 
    655 	if (ifp->if_flags & IFF_UP)
    656 		mii_mediachg(&sc->sc_mii);
    657 	return 0;
    658 }
    659 
    660 static void
    661 lii_media_status(struct ifnet *ifp, struct ifmediareq *imr)
    662 {
    663 	struct lii_softc *sc = ifp->if_softc;
    664 
    665 	DPRINTF(("lii_media_status\n"));
    666 
    667 	mii_pollstat(&sc->sc_mii);
    668 	imr->ifm_status = sc->sc_mii.mii_media_status;
    669 	imr->ifm_active = sc->sc_mii.mii_media_active;
    670 }
    671 
    672 static int
    673 lii_init(struct ifnet *ifp)
    674 {
    675 	struct lii_softc *sc = ifp->if_softc;
    676 	uint32_t val;
    677 	int error;
    678 
    679 	DPRINTF(("lii_init\n"));
    680 
    681 	lii_stop(ifp, 0);
    682 
    683 	memset(sc->sc_ring, 0, sc->sc_ringsize);
    684 
    685 	/* Disable all interrupts */
    686 	AT_WRITE_4(sc, ATL2_ISR, 0xffffffff);
    687 
    688 	/* XXX endianness */
    689 	AT_WRITE_4(sc, ATL2_MAC_ADDR_0,
    690 	    sc->sc_eaddr[2] << 24 |
    691 	    sc->sc_eaddr[3] << 16 |
    692 	    sc->sc_eaddr[4] << 8 |
    693 	    sc->sc_eaddr[5]);
    694 	AT_WRITE_4(sc, ATL2_MAC_ADDR_1,
    695 	    sc->sc_eaddr[0] << 8 |
    696 	    sc->sc_eaddr[1]);
    697 
    698 	AT_WRITE_4(sc, ATL2_DESC_BASE_ADDR_HI, 0);
    699 /* XXX
    700 	    sc->sc_ringmap->dm_segs[0].ds_addr >> 32);
    701 */
    702 	AT_WRITE_4(sc, ATL2_RXD_BASE_ADDR_LO,
    703 	    (sc->sc_ringmap->dm_segs[0].ds_addr & 0xffffffff)
    704 	    + AT_RXD_PADDING);
    705 	AT_WRITE_4(sc, ATL2_TXS_BASE_ADDR_LO,
    706 	    sc->sc_txsp & 0xffffffff);
    707 	AT_WRITE_4(sc, ATL2_TXD_BASE_ADDR_LO,
    708 	    sc->sc_txdp & 0xffffffff);
    709 
    710 	AT_WRITE_2(sc, ATL2_TXD_BUFFER_SIZE, AT_TXD_BUFFER_SIZE / 4);
    711 	AT_WRITE_2(sc, ATL2_TXS_NUM_ENTRIES, AT_TXD_NUM);
    712 	AT_WRITE_2(sc, ATL2_RXD_NUM_ENTRIES, AT_RXD_NUM);
    713 
    714 	/*
    715 	 * Inter Paket Gap Time = 0x60 (IPGT)
    716 	 * Minimum inter-frame gap for RX = 0x50 (MIFG)
    717 	 * 64-bit Carrier-Sense window = 0x40 (IPGR1)
    718 	 * 96-bit IPG window = 0x60 (IPGR2)
    719 	 */
    720 	AT_WRITE_4(sc, ATL2_MIPFG, 0x60405060);
    721 
    722 	/*
    723 	 * Collision window = 0x37 (LCOL)
    724 	 * Maximum # of retrans = 0xf (RETRY)
    725 	 * Maximum binary expansion # = 0xa (ABEBT)
    726 	 * IPG to start jam = 0x7 (JAMIPG)
    727 	*/
    728 	AT_WRITE_4(sc, ATL2_MHDC, 0x07a0f037 |
    729 	     MHDC_EXC_DEF_EN);
    730 
    731 	/* 100 means 200us */
    732 	AT_WRITE_2(sc, ATL2_IMTIV, 100);
    733 	AT_WRITE_2(sc, ATL2_SMC, SMC_ITIMER_EN);
    734 
    735 	/* 500000 means 100ms */
    736 	AT_WRITE_2(sc, ATL2_IALTIV, 50000);
    737 
    738 	AT_WRITE_4(sc, ATL2_MTU, ifp->if_mtu + ETHER_HDR_LEN
    739 	    + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
    740 
    741 	/* unit unknown for TX cur-through threshold */
    742 	AT_WRITE_4(sc, ATL2_TX_CUT_THRESH, 0x177);
    743 
    744 	AT_WRITE_2(sc, ATL2_PAUSE_ON_TH, AT_RXD_NUM * 7 / 8);
    745 	AT_WRITE_2(sc, ATL2_PAUSE_OFF_TH, AT_RXD_NUM / 12);
    746 
    747 	sc->sc_rxcur = 0;
    748 	sc->sc_txs_cur = sc->sc_txs_ack = 0;
    749 	sc->sc_txd_cur = sc->sc_txd_ack = 0;
    750 	sc->sc_free_tx_slots = true;
    751 	AT_WRITE_2(sc, ATL2_MB_TXD_WR_IDX, sc->sc_txd_cur);
    752 	AT_WRITE_2(sc, ATL2_MB_RXD_RD_IDX, sc->sc_rxcur);
    753 
    754 	AT_WRITE_1(sc, ATL2_DMAR, DMAR_EN);
    755 	AT_WRITE_1(sc, ATL2_DMAW, DMAW_EN);
    756 
    757 	AT_WRITE_4(sc, ATL2_SMC, AT_READ_4(sc, ATL2_SMC) | SMC_MANUAL_INT);
    758 
    759 	error = ((AT_READ_4(sc, ATL2_ISR) & ISR_PHY_LINKDOWN) != 0);
    760 	AT_WRITE_4(sc, ATL2_ISR, 0x3fffffff);
    761 	AT_WRITE_4(sc, ATL2_ISR, 0);
    762 	if (error) {
    763 		aprint_error_dev(sc->sc_dev, "init failed\n");
    764 		goto out;
    765 	}
    766 
    767 	lii_setmulti(sc);
    768 
    769 	val = AT_READ_4(sc, ATL2_MACC) & MACC_FDX;
    770 
    771 	val |= MACC_RX_EN | MACC_TX_EN | MACC_MACLP_CLK_PHY |
    772 	    MACC_TX_FLOW_EN | MACC_RX_FLOW_EN |
    773 	    MACC_ADD_CRC | MACC_PAD | MACC_BCAST_EN;
    774 
    775 	if (ifp->if_flags & IFF_PROMISC)
    776 		val |= MACC_PROMISC_EN;
    777 	else if (ifp->if_flags & IFF_ALLMULTI)
    778 		val |= MACC_ALLMULTI_EN;
    779 
    780 	val |= 7 << MACC_PREAMBLE_LEN_SHIFT;
    781 	val |= 2 << MACC_HDX_LEFT_BUF_SHIFT;
    782 
    783 	AT_WRITE_4(sc, ATL2_MACC, val);
    784 
    785 	mii_mediachg(&sc->sc_mii);
    786 
    787 	AT_WRITE_4(sc, ATL2_IMR, IMR_NORMAL_MASK);
    788 
    789 	callout_schedule(&sc->sc_tick_ch, hz);
    790 
    791 	ifp->if_flags |= IFF_RUNNING;
    792 	ifp->if_flags &= ~IFF_OACTIVE;
    793 
    794 out:
    795 	return error;
    796 }
    797 
    798 static void
    799 lii_tx_put(struct lii_softc *sc, struct mbuf *m)
    800 {
    801 	int left;
    802 	struct tx_pkt_header *tph =
    803 	    (struct tx_pkt_header *)(sc->sc_txdbase + sc->sc_txd_cur);
    804 
    805 	memset(tph, 0, sizeof *tph);
    806 	tph->txph_size = m->m_pkthdr.len;
    807 
    808 	sc->sc_txd_cur = (sc->sc_txd_cur + 4) % AT_TXD_BUFFER_SIZE;
    809 
    810 	/*
    811 	 * We already know we have enough space, so if there is a part of the
    812 	 * space ahead of txd_cur that is active, it doesn't matter because
    813 	 * left will be large enough even without it.
    814 	 */
    815 	left  = AT_TXD_BUFFER_SIZE - sc->sc_txd_cur;
    816 
    817 	if (left > m->m_pkthdr.len) {
    818 		m_copydata(m, 0, m->m_pkthdr.len,
    819 		    sc->sc_txdbase + sc->sc_txd_cur);
    820 		sc->sc_txd_cur += m->m_pkthdr.len;
    821 	} else {
    822 		m_copydata(m, 0, left, sc->sc_txdbase + sc->sc_txd_cur);
    823 		m_copydata(m, left, m->m_pkthdr.len - left, sc->sc_txdbase);
    824 		sc->sc_txd_cur = m->m_pkthdr.len - left;
    825 	}
    826 
    827 	/* Round to a 32-bit boundary */
    828 	sc->sc_txd_cur = (sc->sc_txd_cur + 3) & ~3;
    829 	if (sc->sc_txd_cur == sc->sc_txd_ack)
    830 		sc->sc_free_tx_slots = false;
    831 }
    832 
    833 static int
    834 lii_free_tx_space(struct lii_softc *sc)
    835 {
    836 	int space;
    837 
    838 	if (sc->sc_txd_cur >= sc->sc_txd_ack)
    839 		space = (AT_TXD_BUFFER_SIZE - sc->sc_txd_cur) +
    840 		    sc->sc_txd_ack;
    841 	else
    842 		space = sc->sc_txd_ack - sc->sc_txd_cur;
    843 
    844 	/* Account for the tx_pkt_header */
    845 	return (space - 4);
    846 }
    847 
    848 static void
    849 lii_start(struct ifnet *ifp)
    850 {
    851 	struct lii_softc *sc = ifp->if_softc;
    852 	struct mbuf *m0;
    853 
    854 	DPRINTF(("lii_start\n"));
    855 
    856 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    857 		return;
    858 
    859 	for (;;) {
    860 		IFQ_POLL(&ifp->if_snd, m0);
    861 		if (m0 == NULL)
    862 			break;
    863 
    864 		if (!sc->sc_free_tx_slots ||
    865 		    lii_free_tx_space(sc) < m0->m_pkthdr.len) {
    866 			ifp->if_flags |= IFF_OACTIVE;
    867 			break;
    868 		}
    869 
    870 		lii_tx_put(sc, m0);
    871 
    872 		DPRINTF(("lii_start: put %d\n", sc->sc_txs_cur));
    873 
    874 		sc->sc_txs[sc->sc_txs_cur].txps_update = 0;
    875 		sc->sc_txs_cur = (sc->sc_txs_cur + 1) % AT_TXD_NUM;
    876 		if (sc->sc_txs_cur == sc->sc_txs_ack)
    877 			sc->sc_free_tx_slots = false;
    878 
    879 		AT_WRITE_2(sc, ATL2_MB_TXD_WR_IDX, sc->sc_txd_cur/4);
    880 
    881 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    882 
    883 #if NBPFILTER > 0
    884 		if (ifp->if_bpf != NULL)
    885 			bpf_mtap(ifp->if_bpf, m0);
    886 #endif
    887 		m_freem(m0);
    888 	}
    889 }
    890 
    891 static void
    892 lii_stop(struct ifnet *ifp, int disable)
    893 {
    894 	struct lii_softc *sc = ifp->if_softc;
    895 
    896 	callout_stop(&sc->sc_tick_ch);
    897 
    898 	ifp->if_timer = 0;
    899 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    900 
    901 	mii_down(&sc->sc_mii);
    902 
    903 	lii_reset(sc);
    904 
    905 	AT_WRITE_4(sc, ATL2_IMR, 0);
    906 }
    907 
    908 static int
    909 lii_intr(void *v)
    910 {
    911 	struct lii_softc *sc = v;
    912 	uint32_t status;
    913 
    914 	status = AT_READ_4(sc, ATL2_ISR);
    915 	if (status == 0)
    916 		return 0;
    917 
    918 	DPRINTF(("lii_intr (%x)\n", status));
    919 
    920 	/* Clear the interrupt and disable them */
    921 	AT_WRITE_4(sc, ATL2_ISR, status | ISR_DIS_INT);
    922 
    923 	if (status & (ISR_PHY | ISR_MANUAL)) {
    924 		/* Ack PHY interrupt.  Magic register */
    925 		if (status & ISR_PHY)
    926 			(void)lii_mii_readreg(sc->sc_dev, 1, 19);
    927 		mii_mediachg(&sc->sc_mii);
    928 	}
    929 
    930 	if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST | ISR_PHY_LINKDOWN)) {
    931 		lii_init(&sc->sc_ec.ec_if);
    932 		return 1;
    933 	}
    934 
    935 	if (status & ISR_RX_EVENT) {
    936 #ifdef LII_DEBUG
    937 		if (!(status & ISR_RS_UPDATE))
    938 			printf("rxintr %08x\n", status);
    939 #endif
    940 		lii_rxintr(sc);
    941 	}
    942 
    943 	if (status & ISR_TX_EVENT)
    944 		lii_txintr(sc);
    945 
    946 	/* Re-enable interrupts */
    947 	AT_WRITE_4(sc, ATL2_ISR, 0);
    948 
    949 	return 1;
    950 }
    951 
    952 static void
    953 lii_rxintr(struct lii_softc *sc)
    954 {
    955 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    956 	struct rx_pkt *rxp;
    957 	struct mbuf *m;
    958 	uint16_t size;
    959 
    960 	DPRINTF(("lii_rxintr\n"));
    961 
    962 	for (;;) {
    963 		rxp = &sc->sc_rxp[sc->sc_rxcur];
    964 		if (rxp->rxp_update == 0)
    965 			break;
    966 
    967 		DPRINTF(("lii_rxintr: getting %u (%u) [%x]\n", sc->sc_rxcur,
    968 		    rxp->rxp_size, rxp->rxp_flags));
    969 		sc->sc_rxcur = (sc->sc_rxcur + 1) % AT_RXD_NUM;
    970 		rxp->rxp_update = 0;
    971 		if (!(rxp->rxp_flags & ATL2_RXF_SUCCESS)) {
    972 			++ifp->if_ierrors;
    973 			continue;
    974 		}
    975 
    976 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    977 		if (m == NULL) {
    978 			++ifp->if_ierrors;
    979 			continue;
    980 		}
    981 		size = rxp->rxp_size - ETHER_CRC_LEN;
    982 		if (size > MHLEN) {
    983 			MCLGET(m, M_DONTWAIT);
    984 			if ((m->m_flags & M_EXT) == 0) {
    985 				m_freem(m);
    986 				++ifp->if_ierrors;
    987 				continue;
    988 			}
    989 		}
    990 
    991 		m->m_pkthdr.rcvif = ifp;
    992 		/* Copy the packet withhout the FCS */
    993 		m->m_pkthdr.len = m->m_len = size;
    994 		memcpy(mtod(m, void *), &rxp->rxp_data[0], size);
    995 		++ifp->if_ipackets;
    996 
    997 #if NBPFILTER > 0
    998 		if (ifp->if_bpf)
    999 			bpf_mtap(ifp->if_bpf, m);
   1000 #endif
   1001 
   1002 		(*ifp->if_input)(ifp, m);
   1003 	}
   1004 
   1005 	AT_WRITE_4(sc, ATL2_MB_RXD_RD_IDX, sc->sc_rxcur);
   1006 }
   1007 
   1008 static void
   1009 lii_txintr(struct lii_softc *sc)
   1010 {
   1011 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1012 	struct tx_pkt_status *txs;
   1013 	struct tx_pkt_header *txph;
   1014 
   1015 	DPRINTF(("lii_txintr\n"));
   1016 
   1017 	for (;;) {
   1018 		txs = &sc->sc_txs[sc->sc_txs_ack];
   1019 		if (txs->txps_update == 0)
   1020 			break;
   1021 		DPRINTF(("lii_txintr: ack'd %d\n", sc->sc_txs_ack));
   1022 		sc->sc_txs_ack = (sc->sc_txs_ack + 1) % AT_TXD_NUM;
   1023 		sc->sc_free_tx_slots = true;
   1024 
   1025 		txs->txps_update = 0;
   1026 
   1027 		txph =  (struct tx_pkt_header *)
   1028 		    (sc->sc_txdbase + sc->sc_txd_ack);
   1029 
   1030 		if (txph->txph_size != txs->txps_size)
   1031 			aprint_error_dev(sc->sc_dev,
   1032 			    "mismatched status and packet\n");
   1033 		/*
   1034 		 * Move ack by the packet size, taking the packet header in
   1035 		 * account and round to the next 32-bit boundary
   1036 		 * (7 = sizeof(header) + 3)
   1037 		 */
   1038 		sc->sc_txd_ack = (sc->sc_txd_ack + txph->txph_size + 7 ) & ~3;
   1039 		sc->sc_txd_ack %= AT_TXD_BUFFER_SIZE;
   1040 
   1041 		if (txs->txps_flags & ATL2_TXF_SUCCESS)
   1042 			++ifp->if_opackets;
   1043 		else
   1044 			++ifp->if_oerrors;
   1045 		ifp->if_flags &= ~IFF_OACTIVE;
   1046 	}
   1047 
   1048 	if (sc->sc_free_tx_slots)
   1049 		lii_start(ifp);
   1050 }
   1051 
   1052 static int
   1053 lii_alloc_rings(struct lii_softc *sc)
   1054 {
   1055 	int nsegs;
   1056 	bus_size_t bs;
   1057 
   1058 	/*
   1059 	 * We need a big chunk of DMA-friendly memory because descriptors
   1060 	 * are not separate from data on that crappy hardware, which means
   1061 	 * we'll have to copy data from and to that memory zone to and from
   1062 	 * the mbufs.
   1063 	 *
   1064 	 * How lame is that?  Using the default values from the Linux driver,
   1065 	 * we allocate space for receiving up to 64 full-size Ethernet frames,
   1066 	 * and only 8kb for transmitting up to 64 Ethernet frames.
   1067 	 */
   1068 
   1069 	sc->sc_ringsize = bs = AT_RXD_PADDING
   1070 	    + AT_RXD_NUM * sizeof(struct rx_pkt)
   1071 	    + AT_TXD_NUM * sizeof(struct tx_pkt_status)
   1072 	    + AT_TXD_BUFFER_SIZE;
   1073 
   1074 	if (bus_dmamap_create(sc->sc_dmat, bs, 1, bs, (1<<30),
   1075 	    BUS_DMA_NOWAIT, &sc->sc_ringmap) != 0) {
   1076 		aprint_error_dev(sc->sc_dev, "bus_dmamap_create failed\n");
   1077 		return 1;
   1078 	}
   1079 
   1080 	if (bus_dmamem_alloc(sc->sc_dmat, bs, PAGE_SIZE, (1<<30),
   1081 	    &sc->sc_ringseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0) {
   1082 		aprint_error_dev(sc->sc_dev, "bus_dmamem_alloc failed\n");
   1083 		goto fail;
   1084 	}
   1085 
   1086 	if (bus_dmamem_map(sc->sc_dmat, &sc->sc_ringseg, nsegs, bs,
   1087 	    (void **)&sc->sc_ring, BUS_DMA_NOWAIT) != 0) {
   1088 		aprint_error_dev(sc->sc_dev, "bus_dmamem_map failed\n");
   1089 		goto fail1;
   1090 	}
   1091 
   1092 	if (bus_dmamap_load(sc->sc_dmat, sc->sc_ringmap, sc->sc_ring,
   1093 	    bs, NULL, BUS_DMA_NOWAIT) != 0) {
   1094 		aprint_error_dev(sc->sc_dev, "bus_dmamap_load failed\n");
   1095 		goto fail2;
   1096 	}
   1097 
   1098 	sc->sc_rxp = (void *)(sc->sc_ring + AT_RXD_PADDING);
   1099 	sc->sc_txs = (void *)(sc->sc_ring + AT_RXD_PADDING
   1100 	    + AT_RXD_NUM * sizeof(struct rx_pkt));
   1101 	sc->sc_txdbase = ((char *)sc->sc_txs)
   1102 	    + AT_TXD_NUM * sizeof(struct tx_pkt_status);
   1103 	sc->sc_txsp = sc->sc_ringmap->dm_segs[0].ds_addr
   1104 	    + ((char *)sc->sc_txs - (char *)sc->sc_ring);
   1105 	sc->sc_txdp = sc->sc_ringmap->dm_segs[0].ds_addr
   1106 	    + ((char *)sc->sc_txdbase - (char *)sc->sc_ring);
   1107 
   1108 	return 0;
   1109 
   1110 fail2:
   1111 	bus_dmamem_unmap(sc->sc_dmat, sc->sc_ring, bs);
   1112 fail1:
   1113 	bus_dmamem_free(sc->sc_dmat, &sc->sc_ringseg, nsegs);
   1114 fail:
   1115 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_ringmap);
   1116 	return 1;
   1117 }
   1118 
   1119 static void
   1120 lii_watchdog(struct ifnet *ifp)
   1121 {
   1122 	struct lii_softc *sc = ifp->if_softc;
   1123 
   1124 	aprint_error_dev(sc->sc_dev, "watchdog timeout\n");
   1125 	++ifp->if_oerrors;
   1126 	lii_init(ifp);
   1127 }
   1128 
   1129 static int
   1130 lii_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1131 {
   1132 	struct lii_softc *sc = ifp->if_softc;
   1133 	int s, error;
   1134 
   1135 	s = splnet();
   1136 
   1137 	switch(cmd) {
   1138 	case SIOCADDMULTI:
   1139 	case SIOCDELMULTI:
   1140 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1141 			if (ifp->if_flags & IFF_RUNNING)
   1142 				lii_setmulti(sc);
   1143 			error = 0;
   1144 		}
   1145 		break;
   1146 	case SIOCSIFMEDIA:
   1147 	case SIOCGIFMEDIA:
   1148 		error = ifmedia_ioctl(ifp, (struct ifreq *)data,
   1149 		    &sc->sc_mii.mii_media, cmd);
   1150 		break;
   1151 	default:
   1152 		error = ether_ioctl(ifp, cmd, data);
   1153 		if (error == ENETRESET) {
   1154 			if (ifp->if_flags & IFF_RUNNING)
   1155 				lii_setmulti(sc);
   1156 			error = 0;
   1157 		}
   1158 		break;
   1159 	}
   1160 
   1161 	splx(s);
   1162 
   1163 	return error;
   1164 }
   1165 
   1166 static void
   1167 lii_setmulti(struct lii_softc *sc)
   1168 {
   1169 	struct ethercom *ec = &sc->sc_ec;
   1170 	struct ifnet *ifp = &ec->ec_if;
   1171 	uint32_t mht0 = 0, mht1 = 0, crc;
   1172 	struct ether_multi *enm;
   1173 	struct ether_multistep step;
   1174 
   1175 	/* Clear multicast hash table */
   1176 	AT_WRITE_4(sc, ATL2_MHT, 0);
   1177 	AT_WRITE_4(sc, ATL2_MHT + 4, 0);
   1178 
   1179 	ifp->if_flags &= ~IFF_ALLMULTI;
   1180 
   1181 	ETHER_FIRST_MULTI(step, ec, enm);
   1182 	while (enm != NULL) {
   1183 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1184 			ifp->if_flags |= IFF_ALLMULTI;
   1185 			mht0 = mht1 = 0;
   1186 			goto alldone;
   1187 		}
   1188 
   1189 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1190 
   1191 		if (crc & (1 << 31))
   1192 			mht1 |= (1 << (crc & 0x0000001f));
   1193 		else
   1194 			mht0 |= (1 << (crc & 0x0000001f));
   1195 
   1196 	     ETHER_NEXT_MULTI(step, enm);
   1197 	}
   1198 
   1199 alldone:
   1200 	AT_WRITE_4(sc, ATL2_MHT, mht0);
   1201 	AT_WRITE_4(sc, ATL2_MHT+4, mht1);
   1202 }
   1203 
   1204 static void
   1205 lii_tick(void *v)
   1206 {
   1207 	struct lii_softc *sc = v;
   1208 	int s;
   1209 
   1210 	s = splnet();
   1211 	mii_tick(&sc->sc_mii);
   1212 	splx(s);
   1213 
   1214 	callout_schedule(&sc->sc_tick_ch, hz);
   1215 }
   1216