xref: /netbsd-src/sys/dev/pci/if_tl.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: if_tl.c,v 1.102 2015/04/13 16:33:25 riastradh Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * Texas Instruments ThunderLAN ethernet controller
29  * ThunderLAN Programmer's Guide (TI Literature Number SPWU013A)
30  * available from www.ti.com
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_tl.c,v 1.102 2015/04/13 16:33:25 riastradh Exp $");
35 
36 #undef TLDEBUG
37 #define TL_PRIV_STATS
38 #undef TLDEBUG_RX
39 #undef TLDEBUG_TX
40 #undef TLDEBUG_ADDR
41 
42 #include "opt_inet.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
54 #include <sys/device.h>
55 
56 #include <net/if.h>
57 #if defined(SIOCSIFMEDIA)
58 #include <net/if_media.h>
59 #endif
60 #include <net/if_types.h>
61 #include <net/if_dl.h>
62 #include <net/route.h>
63 #include <net/netisr.h>
64 
65 #include <net/bpf.h>
66 #include <net/bpfdesc.h>
67 
68 #include <sys/rndsource.h>
69 
70 #ifdef INET
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75 #endif
76 
77 
78 #if defined(__NetBSD__)
79 #include <net/if_ether.h>
80 #if defined(INET)
81 #include <netinet/if_inarp.h>
82 #endif
83 
84 #include <sys/bus.h>
85 #include <sys/intr.h>
86 
87 #include <dev/pci/pcireg.h>
88 #include <dev/pci/pcivar.h>
89 #include <dev/pci/pcidevs.h>
90 
91 #include <dev/i2c/i2cvar.h>
92 #include <dev/i2c/i2c_bitbang.h>
93 #include <dev/i2c/at24cxxvar.h>
94 
95 #include <dev/mii/mii.h>
96 #include <dev/mii/miivar.h>
97 
98 #include <dev/mii/tlphyvar.h>
99 
100 #include <dev/pci/if_tlregs.h>
101 #include <dev/pci/if_tlvar.h>
102 #endif /* __NetBSD__ */
103 
104 /* number of transmit/receive buffers */
105 #ifndef TL_NBUF
106 #define TL_NBUF 32
107 #endif
108 
109 static int tl_pci_match(device_t, cfdata_t, void *);
110 static void tl_pci_attach(device_t, device_t, void *);
111 static int tl_intr(void *);
112 
113 static int tl_ifioctl(struct ifnet *, ioctl_cmd_t, void *);
114 static int tl_mediachange(struct ifnet *);
115 static void tl_ifwatchdog(struct ifnet *);
116 static bool tl_shutdown(device_t, int);
117 
118 static void tl_ifstart(struct ifnet *);
119 static void tl_reset(tl_softc_t *);
120 static int  tl_init(struct ifnet *);
121 static void tl_stop(struct ifnet *, int);
122 static void tl_restart(void *);
123 static int  tl_add_RxBuff(tl_softc_t *, struct Rx_list *, struct mbuf *);
124 static void tl_read_stats(tl_softc_t *);
125 static void tl_ticks(void *);
126 static int tl_multicast_hash(uint8_t *);
127 static void tl_addr_filter(tl_softc_t *);
128 
129 static uint32_t tl_intreg_read(tl_softc_t *, uint32_t);
130 static void tl_intreg_write(tl_softc_t *, uint32_t, uint32_t);
131 static uint8_t tl_intreg_read_byte(tl_softc_t *, uint32_t);
132 static void tl_intreg_write_byte(tl_softc_t *, uint32_t, uint8_t);
133 
134 void	tl_mii_sync(struct tl_softc *);
135 void	tl_mii_sendbits(struct tl_softc *, uint32_t, int);
136 
137 
138 #if defined(TLDEBUG_RX)
139 static void ether_printheader(struct ether_header *);
140 #endif
141 
142 int tl_mii_read(device_t, int, int);
143 void tl_mii_write(device_t, int, int, int);
144 
145 void tl_statchg(struct ifnet *);
146 
147 	/* I2C glue */
148 static int tl_i2c_acquire_bus(void *, int);
149 static void tl_i2c_release_bus(void *, int);
150 static int tl_i2c_send_start(void *, int);
151 static int tl_i2c_send_stop(void *, int);
152 static int tl_i2c_initiate_xfer(void *, i2c_addr_t, int);
153 static int tl_i2c_read_byte(void *, uint8_t *, int);
154 static int tl_i2c_write_byte(void *, uint8_t, int);
155 
156 	/* I2C bit-bang glue */
157 static void tl_i2cbb_set_bits(void *, uint32_t);
158 static void tl_i2cbb_set_dir(void *, uint32_t);
159 static uint32_t tl_i2cbb_read(void *);
160 static const struct i2c_bitbang_ops tl_i2cbb_ops = {
161 	tl_i2cbb_set_bits,
162 	tl_i2cbb_set_dir,
163 	tl_i2cbb_read,
164 	{
165 		TL_NETSIO_EDATA,	/* SDA */
166 		TL_NETSIO_ECLOCK,	/* SCL */
167 		TL_NETSIO_ETXEN,	/* SDA is output */
168 		0,			/* SDA is input */
169 	}
170 };
171 
172 static inline void netsio_clr(tl_softc_t *, uint8_t);
173 static inline void netsio_set(tl_softc_t *, uint8_t);
174 static inline uint8_t netsio_read(tl_softc_t *, uint8_t);
175 
176 static inline void
177 netsio_clr(tl_softc_t *sc, uint8_t bits)
178 {
179 
180 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio,
181 	    tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & (~bits));
182 }
183 
184 static inline void
185 netsio_set(tl_softc_t *sc, uint8_t bits)
186 {
187 
188 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio,
189 	    tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) | bits);
190 }
191 
192 static inline uint8_t
193 netsio_read(tl_softc_t *sc, uint8_t bits)
194 {
195 
196 	return tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & bits;
197 }
198 
199 CFATTACH_DECL_NEW(tl, sizeof(tl_softc_t),
200     tl_pci_match, tl_pci_attach, NULL, NULL);
201 
202 static const struct tl_product_desc tl_compaq_products[] = {
203 	{ PCI_PRODUCT_COMPAQ_N100TX, TLPHY_MEDIA_NO_10_T,
204 	  "Compaq Netelligent 10/100 TX" },
205 	{ PCI_PRODUCT_COMPAQ_INT100TX, TLPHY_MEDIA_NO_10_T,
206 	  "Integrated Compaq Netelligent 10/100 TX" },
207 	{ PCI_PRODUCT_COMPAQ_N10T, TLPHY_MEDIA_10_5,
208 	  "Compaq Netelligent 10 T" },
209 	{ PCI_PRODUCT_COMPAQ_N10T2, TLPHY_MEDIA_10_2,
210 	  "Compaq Netelligent 10 T/2 UTP/Coax" },
211 	{ PCI_PRODUCT_COMPAQ_IntNF3P, TLPHY_MEDIA_10_2,
212 	  "Compaq Integrated NetFlex 3/P" },
213 	{ PCI_PRODUCT_COMPAQ_IntPL100TX, TLPHY_MEDIA_10_2|TLPHY_MEDIA_NO_10_T,
214 	  "Compaq ProLiant Integrated Netelligent 10/100 TX" },
215 	{ PCI_PRODUCT_COMPAQ_DPNet100TX, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T,
216 	  "Compaq Dual Port Netelligent 10/100 TX" },
217 	{ PCI_PRODUCT_COMPAQ_DP4000, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T,
218 	  "Compaq Deskpro 4000 5233MMX" },
219 	{ PCI_PRODUCT_COMPAQ_NF3P_BNC, TLPHY_MEDIA_10_2,
220 	  "Compaq NetFlex 3/P w/ BNC" },
221 	{ PCI_PRODUCT_COMPAQ_NF3P, TLPHY_MEDIA_10_5,
222 	  "Compaq NetFlex 3/P" },
223 	{ 0, 0, NULL },
224 };
225 
226 static const struct tl_product_desc tl_ti_products[] = {
227 	/*
228 	 * Built-in Ethernet on the TI TravelMate 5000
229 	 * docking station; better product description?
230 	 */
231 	{ PCI_PRODUCT_TI_TLAN, 0,
232 	  "Texas Instruments ThunderLAN" },
233 	{ 0, 0, NULL },
234 };
235 
236 struct tl_vendor_desc {
237 	uint32_t tv_vendor;
238 	const struct tl_product_desc *tv_products;
239 };
240 
241 const struct tl_vendor_desc tl_vendors[] = {
242 	{ PCI_VENDOR_COMPAQ, tl_compaq_products },
243 	{ PCI_VENDOR_TI, tl_ti_products },
244 	{ 0, NULL },
245 };
246 
247 static const struct tl_product_desc *tl_lookup_product(uint32_t);
248 
249 static const struct tl_product_desc *
250 tl_lookup_product(uint32_t id)
251 {
252 	const struct tl_product_desc *tp;
253 	const struct tl_vendor_desc *tv;
254 
255 	for (tv = tl_vendors; tv->tv_products != NULL; tv++)
256 		if (PCI_VENDOR(id) == tv->tv_vendor)
257 			break;
258 
259 	if ((tp = tv->tv_products) == NULL)
260 		return NULL;
261 
262 	for (; tp->tp_desc != NULL; tp++)
263 		if (PCI_PRODUCT(id) == tp->tp_product)
264 			break;
265 
266 	if (tp->tp_desc == NULL)
267 		return NULL;
268 
269 	return tp;
270 }
271 
272 static int
273 tl_pci_match(device_t parent, cfdata_t cf, void *aux)
274 {
275 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
276 
277 	if (tl_lookup_product(pa->pa_id) != NULL)
278 		return 1;
279 
280 	return 0;
281 }
282 
283 static void
284 tl_pci_attach(device_t parent, device_t self, void *aux)
285 {
286 	tl_softc_t *sc = device_private(self);
287 	struct pci_attach_args * const pa = (struct pci_attach_args *)aux;
288 	const struct tl_product_desc *tp;
289 	struct ifnet * const ifp = &sc->tl_if;
290 	bus_space_tag_t iot, memt;
291 	bus_space_handle_t ioh, memh;
292 	pci_intr_handle_t intrhandle;
293 	const char *intrstr;
294 	int ioh_valid, memh_valid;
295 	int reg_io, reg_mem;
296 	pcireg_t reg10, reg14;
297 	pcireg_t csr;
298 	char intrbuf[PCI_INTRSTR_LEN];
299 
300 	sc->sc_dev = self;
301 	aprint_normal("\n");
302 
303 	callout_init(&sc->tl_tick_ch, 0);
304 	callout_init(&sc->tl_restart_ch, 0);
305 
306 	tp = tl_lookup_product(pa->pa_id);
307 	if (tp == NULL)
308 		panic("%s: impossible", __func__);
309 	sc->tl_product = tp;
310 
311 	/*
312 	 * Map the card space. First we have to find the I/O and MEM
313 	 * registers. I/O is supposed to be at 0x10, MEM at 0x14,
314 	 * but some boards (Compaq Netflex 3/P PCI) seem to have it reversed.
315 	 * The ThunderLAN manual is not consistent about this either (there
316 	 * are both cases in code examples).
317 	 */
318 	reg10 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x10);
319 	reg14 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x14);
320 	if (PCI_MAPREG_TYPE(reg10) == PCI_MAPREG_TYPE_IO)
321 		reg_io = 0x10;
322 	else if (PCI_MAPREG_TYPE(reg14) == PCI_MAPREG_TYPE_IO)
323 		reg_io = 0x14;
324 	else
325 		reg_io = 0;
326 	if (PCI_MAPREG_TYPE(reg10) == PCI_MAPREG_TYPE_MEM)
327 		reg_mem = 0x10;
328 	else if (PCI_MAPREG_TYPE(reg14) == PCI_MAPREG_TYPE_MEM)
329 		reg_mem = 0x14;
330 	else
331 		reg_mem = 0;
332 
333 	if (reg_io != 0)
334 		ioh_valid = (pci_mapreg_map(pa, reg_io, PCI_MAPREG_TYPE_IO,
335 		    0, &iot, &ioh, NULL, NULL) == 0);
336 	else
337 		ioh_valid = 0;
338 	if (reg_mem != 0)
339 		memh_valid = (pci_mapreg_map(pa, PCI_CBMA,
340 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
341 		    0, &memt, &memh, NULL, NULL) == 0);
342 	else
343 		memh_valid = 0;
344 
345 	if (ioh_valid) {
346 		sc->tl_bustag = iot;
347 		sc->tl_bushandle = ioh;
348 	} else if (memh_valid) {
349 		sc->tl_bustag = memt;
350 		sc->tl_bushandle = memh;
351 	} else {
352 		aprint_error_dev(self, "unable to map device registers\n");
353 		return;
354 	}
355 	sc->tl_dmatag = pa->pa_dmat;
356 
357 	/* Enable the device. */
358 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
359 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
360 	    csr | PCI_COMMAND_MASTER_ENABLE);
361 
362 	aprint_normal_dev(self, "%s\n", tp->tp_desc);
363 
364 	tl_reset(sc);
365 
366 	/* fill in the i2c tag */
367 	sc->sc_i2c.ic_cookie = sc;
368 	sc->sc_i2c.ic_acquire_bus = tl_i2c_acquire_bus;
369 	sc->sc_i2c.ic_release_bus = tl_i2c_release_bus;
370 	sc->sc_i2c.ic_send_start = tl_i2c_send_start;
371 	sc->sc_i2c.ic_send_stop = tl_i2c_send_stop;
372 	sc->sc_i2c.ic_initiate_xfer = tl_i2c_initiate_xfer;
373 	sc->sc_i2c.ic_read_byte = tl_i2c_read_byte;
374 	sc->sc_i2c.ic_write_byte = tl_i2c_write_byte;
375 
376 #ifdef TLDEBUG
377 	aprint_debug_dev(self, "default values of INTreg: 0x%x\n",
378 	    tl_intreg_read(sc, TL_INT_Defaults));
379 #endif
380 
381 	/* read mac addr */
382 	if (seeprom_bootstrap_read(&sc->sc_i2c, 0x50, 0x83, 256 /* 2kbit */,
383 	    sc->tl_enaddr, ETHER_ADDR_LEN)) {
384 		aprint_error_dev(self, "error reading Ethernet address\n");
385 		return;
386 	}
387 	aprint_normal_dev(self, "Ethernet address %s\n",
388 	    ether_sprintf(sc->tl_enaddr));
389 
390 	/* Map and establish interrupts */
391 	if (pci_intr_map(pa, &intrhandle)) {
392 		aprint_error_dev(self, "couldn't map interrupt\n");
393 		return;
394 	}
395 	intrstr = pci_intr_string(pa->pa_pc, intrhandle, intrbuf, sizeof(intrbuf));
396 	sc->tl_if.if_softc = sc;
397 	sc->tl_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
398 	    tl_intr, sc);
399 	if (sc->tl_ih == NULL) {
400 		aprint_error_dev(self, "couldn't establish interrupt");
401 		if (intrstr != NULL)
402 			aprint_error(" at %s", intrstr);
403 		aprint_error("\n");
404 		return;
405 	}
406 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
407 
408 	/* init these pointers, so that tl_shutdown won't try to read them */
409 	sc->Rx_list = NULL;
410 	sc->Tx_list = NULL;
411 
412 	/* allocate DMA-safe memory for control structs */
413 	if (bus_dmamem_alloc(sc->tl_dmatag, PAGE_SIZE, 0, PAGE_SIZE,
414 	    &sc->ctrl_segs, 1, &sc->ctrl_nsegs, BUS_DMA_NOWAIT) != 0 ||
415 	    bus_dmamem_map(sc->tl_dmatag, &sc->ctrl_segs,
416 	    sc->ctrl_nsegs, PAGE_SIZE, (void **)&sc->ctrl,
417 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0) {
418 		aprint_error_dev(self, "can't allocate DMA memory for lists\n");
419 		return;
420 	}
421 
422 	/*
423 	 * Initialize our media structures and probe the MII.
424 	 *
425 	 * Note that we don't care about the media instance.  We
426 	 * are expecting to have multiple PHYs on the 10/100 cards,
427 	 * and on those cards we exclude the internal PHY from providing
428 	 * 10baseT.  By ignoring the instance, it allows us to not have
429 	 * to specify it on the command line when switching media.
430 	 */
431 	sc->tl_mii.mii_ifp = ifp;
432 	sc->tl_mii.mii_readreg = tl_mii_read;
433 	sc->tl_mii.mii_writereg = tl_mii_write;
434 	sc->tl_mii.mii_statchg = tl_statchg;
435 	sc->tl_ec.ec_mii = &sc->tl_mii;
436 	ifmedia_init(&sc->tl_mii.mii_media, IFM_IMASK, tl_mediachange,
437 	    ether_mediastatus);
438 	mii_attach(self, &sc->tl_mii, 0xffffffff, MII_PHY_ANY,
439 	    MII_OFFSET_ANY, 0);
440 	if (LIST_FIRST(&sc->tl_mii.mii_phys) == NULL) {
441 		ifmedia_add(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
442 		ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE);
443 	} else
444 		ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_AUTO);
445 
446 	/*
447 	 * We can support 802.1Q VLAN-sized frames.
448 	 */
449 	sc->tl_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
450 
451 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
452 	ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
453 	ifp->if_ioctl = tl_ifioctl;
454 	ifp->if_start = tl_ifstart;
455 	ifp->if_watchdog = tl_ifwatchdog;
456 	ifp->if_init = tl_init;
457 	ifp->if_stop = tl_stop;
458 	ifp->if_timer = 0;
459 	IFQ_SET_READY(&ifp->if_snd);
460 	if_attach(ifp);
461 	ether_ifattach(&(sc)->tl_if, (sc)->tl_enaddr);
462 
463 	/*
464 	 * Add shutdown hook so that DMA is disabled prior to reboot.
465 	 * Not doing reboot before the driver initializes.
466 	 */
467 	if (pmf_device_register1(self, NULL, NULL, tl_shutdown))
468 		pmf_class_network_register(self, ifp);
469 	else
470 		aprint_error_dev(self, "couldn't establish power handler\n");
471 
472 	rnd_attach_source(&sc->rnd_source, device_xname(self),
473 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
474 }
475 
476 static void
477 tl_reset(tl_softc_t *sc)
478 {
479 	int i;
480 
481 	/* read stats */
482 	if (sc->tl_if.if_flags & IFF_RUNNING) {
483 		callout_stop(&sc->tl_tick_ch);
484 		tl_read_stats(sc);
485 	}
486 	/* Reset adapter */
487 	TL_HR_WRITE(sc, TL_HOST_CMD,
488 	    TL_HR_READ(sc, TL_HOST_CMD) | HOST_CMD_Ad_Rst);
489 	DELAY(100000);
490 	/* Disable interrupts */
491 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
492 	/* setup aregs & hash */
493 	for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
494 		tl_intreg_write(sc, i, 0);
495 #ifdef TLDEBUG_ADDR
496 	printf("Areg & hash registers: \n");
497 	for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
498 		printf("    reg %x: %x\n", i, tl_intreg_read(sc, i));
499 #endif
500 	/* Setup NetConfig */
501 	tl_intreg_write(sc, TL_INT_NetConfig,
502 	    TL_NETCONFIG_1F | TL_NETCONFIG_1chn | TL_NETCONFIG_PHY_EN);
503 	/* Bsize: accept default */
504 	/* TX commit in Acommit: accept default */
505 	/* Load Ld_tmr and Ld_thr */
506 	/* Ld_tmr = 3 */
507 	TL_HR_WRITE(sc, TL_HOST_CMD, 0x3 | HOST_CMD_LdTmr);
508 	/* Ld_thr = 0 */
509 	TL_HR_WRITE(sc, TL_HOST_CMD, 0x0 | HOST_CMD_LdThr);
510 	/* Unreset MII */
511 	netsio_set(sc, TL_NETSIO_NMRST);
512 	DELAY(100000);
513 	sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
514 }
515 
516 static bool
517 tl_shutdown(device_t self, int howto)
518 {
519 	tl_softc_t *sc = device_private(self);
520 	struct ifnet *ifp = &sc->tl_if;
521 
522 	tl_stop(ifp, 1);
523 
524 	return true;
525 }
526 
527 static void
528 tl_stop(struct ifnet *ifp, int disable)
529 {
530 	tl_softc_t *sc = ifp->if_softc;
531 	struct Tx_list *Tx;
532 	int i;
533 
534 	if ((ifp->if_flags & IFF_RUNNING) == 0)
535 		return;
536 	/* disable interrupts */
537 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
538 	/* stop TX and RX channels */
539 	TL_HR_WRITE(sc, TL_HOST_CMD,
540 	    HOST_CMD_STOP | HOST_CMD_RT | HOST_CMD_Nes);
541 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_STOP);
542 	DELAY(100000);
543 
544 	/* stop statistics reading loop, read stats */
545 	callout_stop(&sc->tl_tick_ch);
546 	tl_read_stats(sc);
547 
548 	/* Down the MII. */
549 	mii_down(&sc->tl_mii);
550 
551 	/* deallocate memory allocations */
552 	if (sc->Rx_list) {
553 		for (i = 0; i< TL_NBUF; i++) {
554 			if (sc->Rx_list[i].m) {
555 				bus_dmamap_unload(sc->tl_dmatag,
556 				    sc->Rx_list[i].m_dmamap);
557 				m_freem(sc->Rx_list[i].m);
558 			}
559 			bus_dmamap_destroy(sc->tl_dmatag,
560 			    sc->Rx_list[i].m_dmamap);
561 			sc->Rx_list[i].m = NULL;
562 		}
563 		free(sc->Rx_list, M_DEVBUF);
564 		sc->Rx_list = NULL;
565 		bus_dmamap_unload(sc->tl_dmatag, sc->Rx_dmamap);
566 		bus_dmamap_destroy(sc->tl_dmatag, sc->Rx_dmamap);
567 		sc->hw_Rx_list = NULL;
568 		while ((Tx = sc->active_Tx) != NULL) {
569 			Tx->hw_list->stat = 0;
570 			bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap);
571 			bus_dmamap_destroy(sc->tl_dmatag, Tx->m_dmamap);
572 			m_freem(Tx->m);
573 			sc->active_Tx = Tx->next;
574 			Tx->next = sc->Free_Tx;
575 			sc->Free_Tx = Tx;
576 		}
577 		sc->last_Tx = NULL;
578 		free(sc->Tx_list, M_DEVBUF);
579 		sc->Tx_list = NULL;
580 		bus_dmamap_unload(sc->tl_dmatag, sc->Tx_dmamap);
581 		bus_dmamap_destroy(sc->tl_dmatag, sc->Tx_dmamap);
582 		sc->hw_Tx_list = NULL;
583 	}
584 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
585 	ifp->if_timer = 0;
586 	sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
587 }
588 
589 static void
590 tl_restart(void *v)
591 {
592 
593 	tl_init(v);
594 }
595 
596 static int
597 tl_init(struct ifnet *ifp)
598 {
599 	tl_softc_t *sc = ifp->if_softc;
600 	int i, s, error;
601 	bus_size_t boundary;
602 	prop_number_t prop_boundary;
603 	const char *errstring;
604 	char *nullbuf;
605 
606 	s = splnet();
607 	/* cancel any pending IO */
608 	tl_stop(ifp, 1);
609 	tl_reset(sc);
610 	if ((sc->tl_if.if_flags & IFF_UP) == 0) {
611 		splx(s);
612 		return 0;
613 	}
614 	/* Set various register to reasonable value */
615 	/* setup NetCmd in promisc mode if needed */
616 	i = (ifp->if_flags & IFF_PROMISC) ? TL_NETCOMMAND_CAF : 0;
617 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
618 	    TL_NETCOMMAND_NRESET | TL_NETCOMMAND_NWRAP | i);
619 	/* Max receive size : MCLBYTES */
620 	tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxL, MCLBYTES & 0xff);
621 	tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxH,
622 	    (MCLBYTES >> 8) & 0xff);
623 
624 	/* init MAC addr */
625 	for (i = 0; i < ETHER_ADDR_LEN; i++)
626 		tl_intreg_write_byte(sc, TL_INT_Areg0 + i , sc->tl_enaddr[i]);
627 	/* add multicast filters */
628 	tl_addr_filter(sc);
629 #ifdef TLDEBUG_ADDR
630 	printf("Wrote Mac addr, Areg & hash registers are now: \n");
631 	for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
632 		printf("    reg %x: %x\n", i, tl_intreg_read(sc, i));
633 #endif
634 
635 	/* Pre-allocate receivers mbuf, make the lists */
636 	sc->Rx_list = malloc(sizeof(struct Rx_list) * TL_NBUF, M_DEVBUF,
637 	    M_NOWAIT|M_ZERO);
638 	sc->Tx_list = malloc(sizeof(struct Tx_list) * TL_NBUF, M_DEVBUF,
639 	    M_NOWAIT|M_ZERO);
640 	if (sc->Rx_list == NULL || sc->Tx_list == NULL) {
641 		errstring = "out of memory for lists";
642 		error = ENOMEM;
643 		goto bad;
644 	}
645 
646 	/*
647 	 * Some boards (Set Engineering GFE) do not permit DMA transfers
648 	 * across page boundaries.
649 	 */
650 	prop_boundary = prop_dictionary_get(device_properties(sc->sc_dev),
651 	    "tl-dma-page-boundary");
652 	if (prop_boundary != NULL) {
653 		KASSERT(prop_object_type(prop_boundary) == PROP_TYPE_NUMBER);
654 		boundary = (bus_size_t)prop_number_integer_value(prop_boundary);
655 	} else {
656 		boundary = 0;
657 	}
658 
659 	error = bus_dmamap_create(sc->tl_dmatag,
660 	    sizeof(struct tl_Rx_list) * TL_NBUF, 1,
661 	    sizeof(struct tl_Rx_list) * TL_NBUF, 0, BUS_DMA_WAITOK,
662 	    &sc->Rx_dmamap);
663 	if (error == 0)
664 		error = bus_dmamap_create(sc->tl_dmatag,
665 		    sizeof(struct tl_Tx_list) * TL_NBUF, 1,
666 		    sizeof(struct tl_Tx_list) * TL_NBUF, boundary,
667 		    BUS_DMA_WAITOK, &sc->Tx_dmamap);
668 	if (error == 0)
669 		error = bus_dmamap_create(sc->tl_dmatag, ETHER_MIN_TX, 1,
670 		    ETHER_MIN_TX, boundary, BUS_DMA_WAITOK,
671 		    &sc->null_dmamap);
672 	if (error) {
673 		errstring = "can't allocate DMA maps for lists";
674 		goto bad;
675 	}
676 	memset(sc->ctrl, 0, PAGE_SIZE);
677 	sc->hw_Rx_list = (void *)sc->ctrl;
678 	sc->hw_Tx_list =
679 	    (void *)(sc->ctrl + sizeof(struct tl_Rx_list) * TL_NBUF);
680 	nullbuf = sc->ctrl + sizeof(struct tl_Rx_list) * TL_NBUF +
681 	    sizeof(struct tl_Tx_list) * TL_NBUF;
682 	error = bus_dmamap_load(sc->tl_dmatag, sc->Rx_dmamap,
683 	    sc->hw_Rx_list, sizeof(struct tl_Rx_list) * TL_NBUF, NULL,
684 	    BUS_DMA_WAITOK);
685 	if (error == 0)
686 		error = bus_dmamap_load(sc->tl_dmatag, sc->Tx_dmamap,
687 		    sc->hw_Tx_list, sizeof(struct tl_Tx_list) * TL_NBUF, NULL,
688 		    BUS_DMA_WAITOK);
689 	if (error == 0)
690 		error = bus_dmamap_load(sc->tl_dmatag, sc->null_dmamap,
691 		    nullbuf, ETHER_MIN_TX, NULL, BUS_DMA_WAITOK);
692 	if (error) {
693 		errstring = "can't DMA map DMA memory for lists";
694 		goto bad;
695 	}
696 	for (i = 0; i < TL_NBUF; i++) {
697 		error = bus_dmamap_create(sc->tl_dmatag, MCLBYTES,
698 		    1, MCLBYTES, boundary, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
699 		    &sc->Rx_list[i].m_dmamap);
700 		if (error == 0) {
701 			error = bus_dmamap_create(sc->tl_dmatag, MCLBYTES,
702 			    TL_NSEG, MCLBYTES, boundary,
703 			    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
704 			    &sc->Tx_list[i].m_dmamap);
705 		}
706 		if (error) {
707 			errstring = "can't allocate DMA maps for mbufs";
708 			goto bad;
709 		}
710 		sc->Rx_list[i].hw_list = &sc->hw_Rx_list[i];
711 		sc->Rx_list[i].hw_listaddr = sc->Rx_dmamap->dm_segs[0].ds_addr
712 		    + sizeof(struct tl_Rx_list) * i;
713 		sc->Tx_list[i].hw_list = &sc->hw_Tx_list[i];
714 		sc->Tx_list[i].hw_listaddr = sc->Tx_dmamap->dm_segs[0].ds_addr
715 		    + sizeof(struct tl_Tx_list) * i;
716 		if (tl_add_RxBuff(sc, &sc->Rx_list[i], NULL) == 0) {
717 			errstring = "out of mbuf for receive list";
718 			error = ENOMEM;
719 			goto bad;
720 		}
721 		if (i > 0) { /* chain the list */
722 			sc->Rx_list[i - 1].next = &sc->Rx_list[i];
723 			sc->hw_Rx_list[i - 1].fwd =
724 			    htole32(sc->Rx_list[i].hw_listaddr);
725 			sc->Tx_list[i - 1].next = &sc->Tx_list[i];
726 		}
727 	}
728 	sc->hw_Rx_list[TL_NBUF - 1].fwd = 0;
729 	sc->Rx_list[TL_NBUF - 1].next = NULL;
730 	sc->hw_Tx_list[TL_NBUF - 1].fwd = 0;
731 	sc->Tx_list[TL_NBUF - 1].next = NULL;
732 
733 	sc->active_Rx = &sc->Rx_list[0];
734 	sc->last_Rx   = &sc->Rx_list[TL_NBUF - 1];
735 	sc->active_Tx = sc->last_Tx = NULL;
736 	sc->Free_Tx   = &sc->Tx_list[0];
737 	bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
738 	    sizeof(struct tl_Rx_list) * TL_NBUF,
739 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
740 	bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
741 	    sizeof(struct tl_Tx_list) * TL_NBUF,
742 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
743 	bus_dmamap_sync(sc->tl_dmatag, sc->null_dmamap, 0, ETHER_MIN_TX,
744 	    BUS_DMASYNC_PREWRITE);
745 
746 	/* set media */
747 	if ((error = mii_mediachg(&sc->tl_mii)) == ENXIO)
748 		error = 0;
749 	else if (error != 0) {
750 		errstring = "could not set media";
751 		goto bad;
752 	}
753 
754 	/* start ticks calls */
755 	callout_reset(&sc->tl_tick_ch, hz, tl_ticks, sc);
756 	/* write address of Rx list and enable interrupts */
757 	TL_HR_WRITE(sc, TL_HOST_CH_PARM, sc->Rx_list[0].hw_listaddr);
758 	TL_HR_WRITE(sc, TL_HOST_CMD,
759 	    HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | HOST_CMD_IntOn);
760 	sc->tl_if.if_flags |= IFF_RUNNING;
761 	sc->tl_if.if_flags &= ~IFF_OACTIVE;
762 	splx(s);
763 	return 0;
764 bad:
765 	printf("%s: %s\n", device_xname(sc->sc_dev), errstring);
766 	splx(s);
767 	return error;
768 }
769 
770 
771 static uint32_t
772 tl_intreg_read(tl_softc_t *sc, uint32_t reg)
773 {
774 
775 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
776 	return TL_HR_READ(sc, TL_HOST_DIO_DATA);
777 }
778 
779 static uint8_t
780 tl_intreg_read_byte(tl_softc_t *sc, uint32_t reg)
781 {
782 
783 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
784 	    (reg & (~0x07)) & TL_HOST_DIOADR_MASK);
785 	return TL_HR_READ_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x07));
786 }
787 
788 static void
789 tl_intreg_write(tl_softc_t *sc, uint32_t reg, uint32_t val)
790 {
791 
792 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
793 	TL_HR_WRITE(sc, TL_HOST_DIO_DATA, val);
794 }
795 
796 static void
797 tl_intreg_write_byte(tl_softc_t *sc, uint32_t reg, uint8_t val)
798 {
799 
800 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
801 	    (reg & (~0x03)) & TL_HOST_DIOADR_MASK);
802 	TL_HR_WRITE_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x03), val);
803 }
804 
805 void
806 tl_mii_sync(struct tl_softc *sc)
807 {
808 	int i;
809 
810 	netsio_clr(sc, TL_NETSIO_MTXEN);
811 	for (i = 0; i < 32; i++) {
812 		netsio_clr(sc, TL_NETSIO_MCLK);
813 		netsio_set(sc, TL_NETSIO_MCLK);
814 	}
815 }
816 
817 void
818 tl_mii_sendbits(struct tl_softc *sc, uint32_t data, int nbits)
819 {
820 	int i;
821 
822 	netsio_set(sc, TL_NETSIO_MTXEN);
823 	for (i = 1 << (nbits - 1); i; i = i >>  1) {
824 		netsio_clr(sc, TL_NETSIO_MCLK);
825 		netsio_read(sc, TL_NETSIO_MCLK);
826 		if (data & i)
827 			netsio_set(sc, TL_NETSIO_MDATA);
828 		else
829 			netsio_clr(sc, TL_NETSIO_MDATA);
830 		netsio_set(sc, TL_NETSIO_MCLK);
831 		netsio_read(sc, TL_NETSIO_MCLK);
832 	}
833 }
834 
835 int
836 tl_mii_read(device_t self, int phy, int reg)
837 {
838 	struct tl_softc *sc = device_private(self);
839 	int val = 0, i, err;
840 
841 	/*
842 	 * Read the PHY register by manually driving the MII control lines.
843 	 */
844 
845 	tl_mii_sync(sc);
846 	tl_mii_sendbits(sc, MII_COMMAND_START, 2);
847 	tl_mii_sendbits(sc, MII_COMMAND_READ, 2);
848 	tl_mii_sendbits(sc, phy, 5);
849 	tl_mii_sendbits(sc, reg, 5);
850 
851 	netsio_clr(sc, TL_NETSIO_MTXEN);
852 	netsio_clr(sc, TL_NETSIO_MCLK);
853 	netsio_set(sc, TL_NETSIO_MCLK);
854 	netsio_clr(sc, TL_NETSIO_MCLK);
855 
856 	err = netsio_read(sc, TL_NETSIO_MDATA);
857 	netsio_set(sc, TL_NETSIO_MCLK);
858 
859 	/* Even if an error occurs, must still clock out the cycle. */
860 	for (i = 0; i < 16; i++) {
861 		val <<= 1;
862 		netsio_clr(sc, TL_NETSIO_MCLK);
863 		if (err == 0 && netsio_read(sc, TL_NETSIO_MDATA))
864 			val |= 1;
865 		netsio_set(sc, TL_NETSIO_MCLK);
866 	}
867 	netsio_clr(sc, TL_NETSIO_MCLK);
868 	netsio_set(sc, TL_NETSIO_MCLK);
869 
870 	return err ? 0 : val;
871 }
872 
873 void
874 tl_mii_write(device_t self, int phy, int reg, int val)
875 {
876 	struct tl_softc *sc = device_private(self);
877 
878 	/*
879 	 * Write the PHY register by manually driving the MII control lines.
880 	 */
881 
882 	tl_mii_sync(sc);
883 	tl_mii_sendbits(sc, MII_COMMAND_START, 2);
884 	tl_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
885 	tl_mii_sendbits(sc, phy, 5);
886 	tl_mii_sendbits(sc, reg, 5);
887 	tl_mii_sendbits(sc, MII_COMMAND_ACK, 2);
888 	tl_mii_sendbits(sc, val, 16);
889 
890 	netsio_clr(sc, TL_NETSIO_MCLK);
891 	netsio_set(sc, TL_NETSIO_MCLK);
892 }
893 
894 void
895 tl_statchg(struct ifnet *ifp)
896 {
897 	tl_softc_t *sc = ifp->if_softc;
898 	uint32_t reg;
899 
900 #ifdef TLDEBUG
901 	printf("%s: media %x\n", __func__, sc->tl_mii.mii_media.ifm_media);
902 #endif
903 
904 	/*
905 	 * We must keep the ThunderLAN and the PHY in sync as
906 	 * to the status of full-duplex!
907 	 */
908 	reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetCmd);
909 	if (sc->tl_mii.mii_media_active & IFM_FDX)
910 		reg |= TL_NETCOMMAND_DUPLEX;
911 	else
912 		reg &= ~TL_NETCOMMAND_DUPLEX;
913 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd, reg);
914 }
915 
916 /********** I2C glue **********/
917 
918 static int
919 tl_i2c_acquire_bus(void *cookie, int flags)
920 {
921 
922 	/* private bus */
923 	return 0;
924 }
925 
926 static void
927 tl_i2c_release_bus(void *cookie, int flags)
928 {
929 
930 	/* private bus */
931 }
932 
933 static int
934 tl_i2c_send_start(void *cookie, int flags)
935 {
936 
937 	return i2c_bitbang_send_start(cookie, flags, &tl_i2cbb_ops);
938 }
939 
940 static int
941 tl_i2c_send_stop(void *cookie, int flags)
942 {
943 
944 	return i2c_bitbang_send_stop(cookie, flags, &tl_i2cbb_ops);
945 }
946 
947 static int
948 tl_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
949 {
950 
951 	return i2c_bitbang_initiate_xfer(cookie, addr, flags, &tl_i2cbb_ops);
952 }
953 
954 static int
955 tl_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
956 {
957 
958 	return i2c_bitbang_read_byte(cookie, valp, flags, &tl_i2cbb_ops);
959 }
960 
961 static int
962 tl_i2c_write_byte(void *cookie, uint8_t val, int flags)
963 {
964 
965 	return i2c_bitbang_write_byte(cookie, val, flags, &tl_i2cbb_ops);
966 }
967 
968 /********** I2C bit-bang glue **********/
969 
970 static void
971 tl_i2cbb_set_bits(void *cookie, uint32_t bits)
972 {
973 	struct tl_softc *sc = cookie;
974 	uint8_t reg;
975 
976 	reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio);
977 	reg = (reg & ~(TL_NETSIO_EDATA|TL_NETSIO_ECLOCK)) | bits;
978 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio, reg);
979 }
980 
981 static void
982 tl_i2cbb_set_dir(void *cookie, uint32_t bits)
983 {
984 	struct tl_softc *sc = cookie;
985 	uint8_t reg;
986 
987 	reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio);
988 	reg = (reg & ~TL_NETSIO_ETXEN) | bits;
989 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio, reg);
990 }
991 
992 static uint32_t
993 tl_i2cbb_read(void *cookie)
994 {
995 
996 	return tl_intreg_read_byte(cookie, TL_INT_NET + TL_INT_NetSio);
997 }
998 
999 /********** End of I2C stuff **********/
1000 
1001 static int
1002 tl_intr(void *v)
1003 {
1004 	tl_softc_t *sc = v;
1005 	struct ifnet *ifp = &sc->tl_if;
1006 	struct Rx_list *Rx;
1007 	struct Tx_list *Tx;
1008 	struct mbuf *m;
1009 	uint32_t int_type, int_reg;
1010 	int ack = 0;
1011 	int size;
1012 
1013 	int_reg = TL_HR_READ(sc, TL_HOST_INTR_DIOADR);
1014 	int_type = int_reg  & TL_INTR_MASK;
1015 	if (int_type == 0)
1016 		return 0;
1017 #if defined(TLDEBUG_RX) || defined(TLDEBUG_TX)
1018 	printf("%s: interrupt type %x, intr_reg %x\n", device_xname(sc->sc_dev),
1019 	    int_type, int_reg);
1020 #endif
1021 	/* disable interrupts */
1022 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
1023 	switch(int_type & TL_INTR_MASK) {
1024 	case TL_INTR_RxEOF:
1025 		bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
1026 		    sizeof(struct tl_Rx_list) * TL_NBUF,
1027 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1028 		while(le32toh(sc->active_Rx->hw_list->stat) &
1029 		    TL_RX_CSTAT_CPLT) {
1030 			/* dequeue and requeue at end of list */
1031 			ack++;
1032 			Rx = sc->active_Rx;
1033 			sc->active_Rx = Rx->next;
1034 			bus_dmamap_sync(sc->tl_dmatag, Rx->m_dmamap, 0,
1035 			    Rx->m_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
1036 			bus_dmamap_unload(sc->tl_dmatag, Rx->m_dmamap);
1037 			m = Rx->m;
1038 			size = le32toh(Rx->hw_list->stat) >> 16;
1039 #ifdef TLDEBUG_RX
1040 			printf("%s: RX list complete, Rx %p, size=%d\n",
1041 			    __func__, Rx, size);
1042 #endif
1043 			if (tl_add_RxBuff(sc, Rx, m) == 0) {
1044 				/*
1045 				 * No new mbuf, reuse the same. This means
1046 				 * that this packet
1047 				 * is lost
1048 				 */
1049 				m = NULL;
1050 #ifdef TL_PRIV_STATS
1051 				sc->ierr_nomem++;
1052 #endif
1053 #ifdef TLDEBUG
1054 				printf("%s: out of mbuf, lost input packet\n",
1055 				    device_xname(sc->sc_dev));
1056 #endif
1057 			}
1058 			Rx->next = NULL;
1059 			Rx->hw_list->fwd = 0;
1060 			sc->last_Rx->hw_list->fwd = htole32(Rx->hw_listaddr);
1061 			sc->last_Rx->next = Rx;
1062 			sc->last_Rx = Rx;
1063 
1064 			/* deliver packet */
1065 			if (m) {
1066 				if (size < sizeof(struct ether_header)) {
1067 					m_freem(m);
1068 					continue;
1069 				}
1070 				m->m_pkthdr.rcvif = ifp;
1071 				m->m_pkthdr.len = m->m_len = size;
1072 #ifdef TLDEBUG_RX
1073 				{
1074 					struct ether_header *eh =
1075 					    mtod(m, struct ether_header *);
1076 					printf("%s: Rx packet:\n", __func__);
1077 					ether_printheader(eh);
1078 				}
1079 #endif
1080 				bpf_mtap(ifp, m);
1081 				(*ifp->if_input)(ifp, m);
1082 			}
1083 		}
1084 		bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
1085 		    sizeof(struct tl_Rx_list) * TL_NBUF,
1086 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1087 #ifdef TLDEBUG_RX
1088 		printf("TL_INTR_RxEOF: ack %d\n", ack);
1089 #else
1090 		if (ack == 0) {
1091 			printf("%s: EOF intr without anything to read !\n",
1092 			    device_xname(sc->sc_dev));
1093 			tl_reset(sc);
1094 			/* schedule reinit of the board */
1095 			callout_reset(&sc->tl_restart_ch, 1, tl_restart, ifp);
1096 			return 1;
1097 		}
1098 #endif
1099 		break;
1100 	case TL_INTR_RxEOC:
1101 		ack++;
1102 		bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
1103 		    sizeof(struct tl_Rx_list) * TL_NBUF,
1104 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1105 #ifdef TLDEBUG_RX
1106 		printf("TL_INTR_RxEOC: ack %d\n", ack);
1107 #endif
1108 #ifdef DIAGNOSTIC
1109 		if (le32toh(sc->active_Rx->hw_list->stat) & TL_RX_CSTAT_CPLT) {
1110 			printf("%s: Rx EOC interrupt and active Tx list not "
1111 			    "cleared\n", device_xname(sc->sc_dev));
1112 			return 0;
1113 		} else
1114 #endif
1115 		{
1116 		/*
1117 		 * write address of Rx list and send Rx GO command, ack
1118 		 * interrupt and enable interrupts in one command
1119 		 */
1120 		TL_HR_WRITE(sc, TL_HOST_CH_PARM, sc->active_Rx->hw_listaddr);
1121 		TL_HR_WRITE(sc, TL_HOST_CMD,
1122 		    HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | ack | int_type |
1123 		    HOST_CMD_ACK | HOST_CMD_IntOn);
1124 		return 1;
1125 		}
1126 	case TL_INTR_TxEOF:
1127 	case TL_INTR_TxEOC:
1128 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
1129 		    sizeof(struct tl_Tx_list) * TL_NBUF,
1130 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1131 		while ((Tx = sc->active_Tx) != NULL) {
1132 			if((le32toh(Tx->hw_list->stat) & TL_TX_CSTAT_CPLT) == 0)
1133 				break;
1134 			ack++;
1135 #ifdef TLDEBUG_TX
1136 			printf("TL_INTR_TxEOC: list 0x%x done\n",
1137 			    (int)Tx->hw_listaddr);
1138 #endif
1139 			Tx->hw_list->stat = 0;
1140 			bus_dmamap_sync(sc->tl_dmatag, Tx->m_dmamap, 0,
1141 			    Tx->m_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1142 			bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap);
1143 			m_freem(Tx->m);
1144 			Tx->m = NULL;
1145 			sc->active_Tx = Tx->next;
1146 			if (sc->active_Tx == NULL)
1147 				sc->last_Tx = NULL;
1148 			Tx->next = sc->Free_Tx;
1149 			sc->Free_Tx = Tx;
1150 		}
1151 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
1152 		    sizeof(struct tl_Tx_list) * TL_NBUF,
1153 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1154 		/* if this was an EOC, ACK immediatly */
1155 		if (ack)
1156 			sc->tl_if.if_flags &= ~IFF_OACTIVE;
1157 		if (int_type == TL_INTR_TxEOC) {
1158 #ifdef TLDEBUG_TX
1159 			printf("TL_INTR_TxEOC: ack %d (will be set to 1)\n",
1160 			    ack);
1161 #endif
1162 			TL_HR_WRITE(sc, TL_HOST_CMD, 1 | int_type |
1163 			    HOST_CMD_ACK | HOST_CMD_IntOn);
1164 			if (sc->active_Tx != NULL) {
1165 				/* needs a Tx go command */
1166 				TL_HR_WRITE(sc, TL_HOST_CH_PARM,
1167 				    sc->active_Tx->hw_listaddr);
1168 				TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1169 			}
1170 			sc->tl_if.if_timer = 0;
1171 			if (IFQ_IS_EMPTY(&sc->tl_if.if_snd) == 0)
1172 				tl_ifstart(&sc->tl_if);
1173 			return 1;
1174 		}
1175 #ifdef TLDEBUG
1176 		else {
1177 			printf("TL_INTR_TxEOF: ack %d\n", ack);
1178 		}
1179 #endif
1180 		sc->tl_if.if_timer = 0;
1181 		if (IFQ_IS_EMPTY(&sc->tl_if.if_snd) == 0)
1182 			tl_ifstart(&sc->tl_if);
1183 		break;
1184 	case TL_INTR_Stat:
1185 		ack++;
1186 #ifdef TLDEBUG
1187 		printf("TL_INTR_Stat: ack %d\n", ack);
1188 #endif
1189 		tl_read_stats(sc);
1190 		break;
1191 	case TL_INTR_Adc:
1192 		if (int_reg & TL_INTVec_MASK) {
1193 			/* adapter check conditions */
1194 			printf("%s: check condition, intvect=0x%x, "
1195 			    "ch_param=0x%x\n", device_xname(sc->sc_dev),
1196 			    int_reg & TL_INTVec_MASK,
1197 			    TL_HR_READ(sc, TL_HOST_CH_PARM));
1198 			tl_reset(sc);
1199 			/* schedule reinit of the board */
1200 			callout_reset(&sc->tl_restart_ch, 1, tl_restart, ifp);
1201 			return 1;
1202 		} else {
1203 			uint8_t netstat;
1204 			/* Network status */
1205 			netstat =
1206 			    tl_intreg_read_byte(sc, TL_INT_NET+TL_INT_NetSts);
1207 			printf("%s: network status, NetSts=%x\n",
1208 			    device_xname(sc->sc_dev), netstat);
1209 			/* Ack interrupts */
1210 			tl_intreg_write_byte(sc, TL_INT_NET+TL_INT_NetSts,
1211 			    netstat);
1212 			ack++;
1213 		}
1214 		break;
1215 	default:
1216 		printf("%s: unhandled interrupt code %x!\n",
1217 		    device_xname(sc->sc_dev), int_type);
1218 		ack++;
1219 	}
1220 
1221 	if (ack) {
1222 		/* Ack the interrupt and enable interrupts */
1223 		TL_HR_WRITE(sc, TL_HOST_CMD, ack | int_type | HOST_CMD_ACK |
1224 		    HOST_CMD_IntOn);
1225 		rnd_add_uint32(&sc->rnd_source, int_reg);
1226 		return 1;
1227 	}
1228 	/* ack = 0 ; interrupt was perhaps not our. Just enable interrupts */
1229 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOn);
1230 	return 0;
1231 }
1232 
1233 static int
1234 tl_ifioctl(struct ifnet *ifp, unsigned long cmd, void *data)
1235 {
1236 	struct tl_softc *sc = ifp->if_softc;
1237 	int s, error;
1238 
1239 	s = splnet();
1240 	error = ether_ioctl(ifp, cmd, data);
1241 	if (error == ENETRESET) {
1242 		if (ifp->if_flags & IFF_RUNNING)
1243 			tl_addr_filter(sc);
1244 		error = 0;
1245 	}
1246 	splx(s);
1247 	return error;
1248 }
1249 
1250 static void
1251 tl_ifstart(struct ifnet *ifp)
1252 {
1253 	tl_softc_t *sc = ifp->if_softc;
1254 	struct mbuf *mb_head;
1255 	struct Tx_list *Tx;
1256 	int segment, size;
1257 	int again, error;
1258 
1259 	if ((sc->tl_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
1260 		return;
1261 txloop:
1262 	/* If we don't have more space ... */
1263 	if (sc->Free_Tx == NULL) {
1264 #ifdef TLDEBUG
1265 		printf("%s: No free TX list\n", __func__);
1266 #endif
1267 		sc->tl_if.if_flags |= IFF_OACTIVE;
1268 		return;
1269 	}
1270 	/* Grab a paquet for output */
1271 	IFQ_DEQUEUE(&ifp->if_snd, mb_head);
1272 	if (mb_head == NULL) {
1273 #ifdef TLDEBUG_TX
1274 		printf("%s: nothing to send\n", __func__);
1275 #endif
1276 		return;
1277 	}
1278 	Tx = sc->Free_Tx;
1279 	sc->Free_Tx = Tx->next;
1280 	Tx->next = NULL;
1281 	again = 0;
1282 	/*
1283 	 * Go through each of the mbufs in the chain and initialize
1284 	 * the transmit list descriptors with the physical address
1285 	 * and size of the mbuf.
1286 	 */
1287 tbdinit:
1288 	memset(Tx->hw_list, 0, sizeof(struct tl_Tx_list));
1289 	Tx->m = mb_head;
1290 	size = mb_head->m_pkthdr.len;
1291 	if ((error = bus_dmamap_load_mbuf(sc->tl_dmatag, Tx->m_dmamap, mb_head,
1292 	    BUS_DMA_NOWAIT)) || (size < ETHER_MIN_TX &&
1293 	    Tx->m_dmamap->dm_nsegs == TL_NSEG)) {
1294 		struct mbuf *mn;
1295 		/*
1296 		 * We ran out of segments, or we will. We have to recopy this
1297 		 * mbuf chain first.
1298 		 */
1299 		 if (error == 0)
1300 			bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap);
1301 		 if (again) {
1302 			/* already copyed, can't do much more */
1303 			m_freem(mb_head);
1304 			goto bad;
1305 		}
1306 		again = 1;
1307 #ifdef TLDEBUG_TX
1308 		printf("%s: need to copy mbuf\n", __func__);
1309 #endif
1310 #ifdef TL_PRIV_STATS
1311 		sc->oerr_mcopy++;
1312 #endif
1313 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
1314 		if (mn == NULL) {
1315 			m_freem(mb_head);
1316 			goto bad;
1317 		}
1318 		if (mb_head->m_pkthdr.len > MHLEN) {
1319 			MCLGET(mn, M_DONTWAIT);
1320 			if ((mn->m_flags & M_EXT) == 0) {
1321 				m_freem(mn);
1322 				m_freem(mb_head);
1323 				goto bad;
1324 			}
1325 		}
1326 		m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1327 		    mtod(mn, void *));
1328 		mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
1329 		m_freem(mb_head);
1330 		mb_head = mn;
1331 		goto tbdinit;
1332 	}
1333 	for (segment = 0; segment < Tx->m_dmamap->dm_nsegs; segment++) {
1334 		Tx->hw_list->seg[segment].data_addr =
1335 		    htole32(Tx->m_dmamap->dm_segs[segment].ds_addr);
1336 		Tx->hw_list->seg[segment].data_count =
1337 		    htole32(Tx->m_dmamap->dm_segs[segment].ds_len);
1338 	}
1339 	bus_dmamap_sync(sc->tl_dmatag, Tx->m_dmamap, 0,
1340 	    Tx->m_dmamap->dm_mapsize,
1341 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1342 	/* We are at end of mbuf chain. check the size and
1343 	 * see if it needs to be extended
1344 	 */
1345 	if (size < ETHER_MIN_TX) {
1346 #ifdef DIAGNOSTIC
1347 		if (segment >= TL_NSEG) {
1348 			panic("%s: to much segmets (%d)", __func__, segment);
1349 		}
1350 #endif
1351 		/*
1352 	 	 * add the nullbuf in the seg
1353 	 	 */
1354 		Tx->hw_list->seg[segment].data_count =
1355 		    htole32(ETHER_MIN_TX - size);
1356 		Tx->hw_list->seg[segment].data_addr =
1357 		    htole32(sc->null_dmamap->dm_segs[0].ds_addr);
1358 		size = ETHER_MIN_TX;
1359 		segment++;
1360 	}
1361 	/* The list is done, finish the list init */
1362 	Tx->hw_list->seg[segment - 1].data_count |=
1363 	    htole32(TL_LAST_SEG);
1364 	Tx->hw_list->stat = htole32((size << 16) | 0x3000);
1365 #ifdef TLDEBUG_TX
1366 	printf("%s: sending, Tx : stat = 0x%x\n", device_xname(sc->sc_dev),
1367 	    le32toh(Tx->hw_list->stat));
1368 #if 0
1369 	for (segment = 0; segment < TL_NSEG; segment++) {
1370 		printf("    seg %d addr 0x%x len 0x%x\n",
1371 		    segment,
1372 		    le32toh(Tx->hw_list->seg[segment].data_addr),
1373 		    le32toh(Tx->hw_list->seg[segment].data_count));
1374 	}
1375 #endif
1376 #endif
1377 	if (sc->active_Tx == NULL) {
1378 		sc->active_Tx = sc->last_Tx = Tx;
1379 #ifdef TLDEBUG_TX
1380 		printf("%s: Tx GO, addr=0x%ux\n", device_xname(sc->sc_dev),
1381 		    (int)Tx->hw_listaddr);
1382 #endif
1383 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
1384 		    sizeof(struct tl_Tx_list) * TL_NBUF,
1385 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1386 		TL_HR_WRITE(sc, TL_HOST_CH_PARM, Tx->hw_listaddr);
1387 		TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1388 	} else {
1389 #ifdef TLDEBUG_TX
1390 		printf("%s: Tx addr=0x%ux queued\n", device_xname(sc->sc_dev),
1391 		    (int)Tx->hw_listaddr);
1392 #endif
1393 		sc->last_Tx->hw_list->fwd = htole32(Tx->hw_listaddr);
1394 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
1395 		    sizeof(struct tl_Tx_list) * TL_NBUF,
1396 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1397 		sc->last_Tx->next = Tx;
1398 		sc->last_Tx = Tx;
1399 #ifdef DIAGNOSTIC
1400 		if (sc->last_Tx->hw_list->fwd & 0x7)
1401 			printf("%s: physical addr 0x%x of list not properly "
1402 			    "aligned\n",
1403 			    device_xname(sc->sc_dev),
1404 			    sc->last_Rx->hw_list->fwd);
1405 #endif
1406 	}
1407 	/* Pass packet to bpf if there is a listener */
1408 	bpf_mtap(ifp, mb_head);
1409 	/*
1410 	 * Set a 5 second timer just in case we don't hear from the card again.
1411 	 */
1412 	ifp->if_timer = 5;
1413 	goto txloop;
1414 bad:
1415 #ifdef TLDEBUG
1416 	printf("%s: Out of mbuf, Tx pkt lost\n", __func__);
1417 #endif
1418 	Tx->next = sc->Free_Tx;
1419 	sc->Free_Tx = Tx;
1420 }
1421 
1422 static void
1423 tl_ifwatchdog(struct ifnet *ifp)
1424 {
1425 	tl_softc_t *sc = ifp->if_softc;
1426 
1427 	if ((ifp->if_flags & IFF_RUNNING) == 0)
1428 		return;
1429 	printf("%s: device timeout\n", device_xname(sc->sc_dev));
1430 	ifp->if_oerrors++;
1431 	tl_init(ifp);
1432 }
1433 
1434 static int
1435 tl_mediachange(struct ifnet *ifp)
1436 {
1437 
1438 	if (ifp->if_flags & IFF_UP)
1439 		tl_init(ifp);
1440 	return 0;
1441 }
1442 
1443 static int
1444 tl_add_RxBuff(tl_softc_t *sc, struct Rx_list *Rx, struct mbuf *oldm)
1445 {
1446 	struct mbuf *m;
1447 	int error;
1448 
1449 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1450 	if (m != NULL) {
1451 		MCLGET(m, M_DONTWAIT);
1452 		if ((m->m_flags & M_EXT) == 0) {
1453 			m_freem(m);
1454 			if (oldm == NULL)
1455 				return 0;
1456 			m = oldm;
1457 			m->m_data = m->m_ext.ext_buf;
1458 		}
1459 	} else {
1460 		if (oldm == NULL)
1461 			return 0;
1462 		m = oldm;
1463 		m->m_data = m->m_ext.ext_buf;
1464 	}
1465 
1466 	/* (re)init the Rx_list struct */
1467 
1468 	Rx->m = m;
1469 	if ((error = bus_dmamap_load(sc->tl_dmatag, Rx->m_dmamap,
1470 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT)) != 0) {
1471 		printf("%s: bus_dmamap_load() failed (error %d) for "
1472 		    "tl_add_RxBuff ", device_xname(sc->sc_dev), error);
1473 		printf("size %d (%d)\n", m->m_pkthdr.len, MCLBYTES);
1474 		m_freem(m);
1475 		Rx->m = NULL;
1476 		return 0;
1477 	}
1478 	bus_dmamap_sync(sc->tl_dmatag, Rx->m_dmamap, 0,
1479 	    Rx->m_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1480 	/*
1481 	 * Move the data pointer up so that the incoming data packet
1482 	 * will be 32-bit aligned.
1483 	 */
1484 	m->m_data += 2;
1485 
1486 	Rx->hw_list->stat =
1487 	    htole32(((Rx->m_dmamap->dm_segs[0].ds_len - 2) << 16) | 0x3000);
1488 	Rx->hw_list->seg.data_count =
1489 	    htole32(Rx->m_dmamap->dm_segs[0].ds_len - 2);
1490 	Rx->hw_list->seg.data_addr =
1491 	    htole32(Rx->m_dmamap->dm_segs[0].ds_addr + 2);
1492 	return (m != oldm);
1493 }
1494 
1495 static void
1496 tl_ticks(void *v)
1497 {
1498 	tl_softc_t *sc = v;
1499 
1500 	tl_read_stats(sc);
1501 
1502 	/* Tick the MII. */
1503 	mii_tick(&sc->tl_mii);
1504 
1505 	/* read statistics every seconds */
1506 	callout_reset(&sc->tl_tick_ch, hz, tl_ticks, sc);
1507 }
1508 
1509 static void
1510 tl_read_stats(tl_softc_t *sc)
1511 {
1512 	uint32_t reg;
1513 	int ierr_overr;
1514 	int ierr_code;
1515 	int ierr_crc;
1516 	int oerr_underr;
1517 	int oerr_deferred;
1518 	int oerr_coll;
1519 	int oerr_multicoll;
1520 	int oerr_exesscoll;
1521 	int oerr_latecoll;
1522 	int oerr_carrloss;
1523 	struct ifnet *ifp = &sc->tl_if;
1524 
1525 	reg =  tl_intreg_read(sc, TL_INT_STATS_TX);
1526 	ifp->if_opackets += reg & 0x00ffffff;
1527 	oerr_underr = reg >> 24;
1528 
1529 	reg =  tl_intreg_read(sc, TL_INT_STATS_RX);
1530 	ifp->if_ipackets += reg & 0x00ffffff;
1531 	ierr_overr = reg >> 24;
1532 
1533 	reg =  tl_intreg_read(sc, TL_INT_STATS_FERR);
1534 	ierr_crc = (reg & TL_FERR_CRC) >> 16;
1535 	ierr_code = (reg & TL_FERR_CODE) >> 24;
1536 	oerr_deferred = (reg & TL_FERR_DEF);
1537 
1538 	reg =  tl_intreg_read(sc, TL_INT_STATS_COLL);
1539 	oerr_multicoll = (reg & TL_COL_MULTI);
1540 	oerr_coll = (reg & TL_COL_SINGLE) >> 16;
1541 
1542 	reg =  tl_intreg_read(sc, TL_INT_LERR);
1543 	oerr_exesscoll = (reg & TL_LERR_ECOLL);
1544 	oerr_latecoll = (reg & TL_LERR_LCOLL) >> 8;
1545 	oerr_carrloss = (reg & TL_LERR_CL) >> 16;
1546 
1547 
1548 	ifp->if_oerrors += oerr_underr + oerr_exesscoll + oerr_latecoll +
1549 	   oerr_carrloss;
1550 	ifp->if_collisions += oerr_coll + oerr_multicoll;
1551 	ifp->if_ierrors += ierr_overr + ierr_code + ierr_crc;
1552 
1553 	if (ierr_overr)
1554 		printf("%s: receiver ring buffer overrun\n",
1555 		    device_xname(sc->sc_dev));
1556 	if (oerr_underr)
1557 		printf("%s: transmit buffer underrun\n",
1558 		    device_xname(sc->sc_dev));
1559 #ifdef TL_PRIV_STATS
1560 	sc->ierr_overr		+= ierr_overr;
1561 	sc->ierr_code		+= ierr_code;
1562 	sc->ierr_crc		+= ierr_crc;
1563 	sc->oerr_underr		+= oerr_underr;
1564 	sc->oerr_deferred	+= oerr_deferred;
1565 	sc->oerr_coll		+= oerr_coll;
1566 	sc->oerr_multicoll	+= oerr_multicoll;
1567 	sc->oerr_exesscoll	+= oerr_exesscoll;
1568 	sc->oerr_latecoll	+= oerr_latecoll;
1569 	sc->oerr_carrloss	+= oerr_carrloss;
1570 #endif
1571 }
1572 
1573 static void
1574 tl_addr_filter(tl_softc_t *sc)
1575 {
1576 	struct ether_multistep step;
1577 	struct ether_multi *enm;
1578 	uint32_t hash[2] = {0, 0};
1579 	int i;
1580 
1581 	sc->tl_if.if_flags &= ~IFF_ALLMULTI;
1582 	ETHER_FIRST_MULTI(step, &sc->tl_ec, enm);
1583 	while (enm != NULL) {
1584 #ifdef TLDEBUG
1585 		printf("%s: addrs %s %s\n", __func__,
1586 		   ether_sprintf(enm->enm_addrlo),
1587 		   ether_sprintf(enm->enm_addrhi));
1588 #endif
1589 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
1590 			i = tl_multicast_hash(enm->enm_addrlo);
1591 			hash[i / 32] |= 1 << (i%32);
1592 		} else {
1593 			hash[0] = hash[1] = 0xffffffff;
1594 			sc->tl_if.if_flags |= IFF_ALLMULTI;
1595 			break;
1596 		}
1597 		ETHER_NEXT_MULTI(step, enm);
1598 	}
1599 #ifdef TLDEBUG
1600 	printf("%s: hash1 %x has2 %x\n", __func__, hash[0], hash[1]);
1601 #endif
1602 	tl_intreg_write(sc, TL_INT_HASH1, hash[0]);
1603 	tl_intreg_write(sc, TL_INT_HASH2, hash[1]);
1604 }
1605 
1606 static int
1607 tl_multicast_hash(uint8_t *a)
1608 {
1609 	int hash;
1610 
1611 #define DA(addr,bit) (addr[5 - (bit / 8)] & (1 << (bit % 8)))
1612 #define xor8(a,b,c,d,e,f,g,h)						\
1613 	(((a != 0) + (b != 0) + (c != 0) + (d != 0) + 			\
1614 	  (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1)
1615 
1616 	hash  = xor8(DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30),
1617 	    DA(a,36), DA(a,42));
1618 	hash |= xor8(DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31),
1619 	    DA(a,37), DA(a,43)) << 1;
1620 	hash |= xor8(DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32),
1621 	    DA(a,38), DA(a,44)) << 2;
1622 	hash |= xor8(DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33),
1623 	    DA(a,39), DA(a,45)) << 3;
1624 	hash |= xor8(DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34),
1625 	    DA(a,40), DA(a,46)) << 4;
1626 	hash |= xor8(DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35),
1627 	    DA(a,41), DA(a,47)) << 5;
1628 
1629 	return hash;
1630 }
1631 
1632 #if defined(TLDEBUG_RX)
1633 void
1634 ether_printheader(struct ether_header *eh)
1635 {
1636 	uint8_t *c = (uint8_t *)eh;
1637 	int i;
1638 
1639 	for (i = 0; i < sizeof(struct ether_header); i++)
1640 		printf("%02x ", (u_int)c[i]);
1641 	printf("\n");
1642 }
1643 #endif
1644