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