xref: /freebsd/sys/dev/my/if_my.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Written by: yen_cw@myson.com.tw
5  * Copyright (c) 2002 Myson Technology Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Myson fast ethernet PCI NIC driver, available at: http://www.myson.com.tw/
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sockio.h>
38 #include <sys/mbuf.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/module.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 
48 #define NBPFILTER	1
49 
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
56 #include <net/if_dl.h>
57 #include <net/bpf.h>
58 
59 #include <vm/vm.h>		/* for vtophys */
60 #include <vm/pmap.h>		/* for vtophys */
61 #include <machine/bus.h>
62 #include <machine/resource.h>
63 #include <sys/bus.h>
64 #include <sys/rman.h>
65 
66 #include <dev/pci/pcireg.h>
67 #include <dev/pci/pcivar.h>
68 
69 /*
70  * #define MY_USEIOSPACE
71  */
72 
73 static int      MY_USEIOSPACE = 1;
74 
75 #ifdef MY_USEIOSPACE
76 #define MY_RES                  SYS_RES_IOPORT
77 #define MY_RID                  MY_PCI_LOIO
78 #else
79 #define MY_RES                  SYS_RES_MEMORY
80 #define MY_RID                  MY_PCI_LOMEM
81 #endif
82 
83 #include <dev/my/if_myreg.h>
84 
85 /*
86  * Various supported device vendors/types and their names.
87  */
88 struct my_type *my_info_tmp;
89 static struct my_type my_devs[] = {
90 	{MYSONVENDORID, MTD800ID, "Myson MTD80X Based Fast Ethernet Card"},
91 	{MYSONVENDORID, MTD803ID, "Myson MTD80X Based Fast Ethernet Card"},
92 	{MYSONVENDORID, MTD891ID, "Myson MTD89X Based Giga Ethernet Card"},
93 	{0, 0, NULL}
94 };
95 
96 /*
97  * Various supported PHY vendors/types and their names. Note that this driver
98  * will work with pretty much any MII-compliant PHY, so failure to positively
99  * identify the chip is not a fatal error.
100  */
101 static struct my_type my_phys[] = {
102 	{MysonPHYID0, MysonPHYID0, "<MYSON MTD981>"},
103 	{SeeqPHYID0, SeeqPHYID0, "<SEEQ 80225>"},
104 	{AhdocPHYID0, AhdocPHYID0, "<AHDOC 101>"},
105 	{MarvellPHYID0, MarvellPHYID0, "<MARVELL 88E1000>"},
106 	{LevelOnePHYID0, LevelOnePHYID0, "<LevelOne LXT1000>"},
107 	{0, 0, "<MII-compliant physical interface>"}
108 };
109 
110 static int      my_probe(device_t);
111 static int      my_attach(device_t);
112 static int      my_detach(device_t);
113 static int      my_newbuf(struct my_softc *, struct my_chain_onefrag *);
114 static int      my_encap(struct my_softc *, struct my_chain *, struct mbuf *);
115 static void     my_rxeof(struct my_softc *);
116 static void     my_txeof(struct my_softc *);
117 static void     my_txeoc(struct my_softc *);
118 static void     my_intr(void *);
119 static void     my_start(if_t);
120 static void     my_start_locked(if_t);
121 static int      my_ioctl(if_t, u_long, caddr_t);
122 static void     my_init(void *);
123 static void     my_init_locked(struct my_softc *);
124 static void     my_stop(struct my_softc *);
125 static void     my_autoneg_timeout(void *);
126 static void     my_watchdog(void *);
127 static int      my_shutdown(device_t);
128 static int      my_ifmedia_upd(if_t);
129 static void     my_ifmedia_sts(if_t, struct ifmediareq *);
130 static u_int16_t my_phy_readreg(struct my_softc *, int);
131 static void     my_phy_writereg(struct my_softc *, int, int);
132 static void     my_autoneg_xmit(struct my_softc *);
133 static void     my_autoneg_mii(struct my_softc *, int, int);
134 static void     my_setmode_mii(struct my_softc *, int);
135 static void     my_getmode_mii(struct my_softc *);
136 static void     my_setcfg(struct my_softc *, int);
137 static void     my_setmulti(struct my_softc *);
138 static void     my_reset(struct my_softc *);
139 static int      my_list_rx_init(struct my_softc *);
140 static int      my_list_tx_init(struct my_softc *);
141 static long     my_send_cmd_to_phy(struct my_softc *, int, int);
142 
143 #define MY_SETBIT(sc, reg, x) CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
144 #define MY_CLRBIT(sc, reg, x) CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
145 
146 static device_method_t my_methods[] = {
147 	/* Device interface */
148 	DEVMETHOD(device_probe, my_probe),
149 	DEVMETHOD(device_attach, my_attach),
150 	DEVMETHOD(device_detach, my_detach),
151 	DEVMETHOD(device_shutdown, my_shutdown),
152 
153 	DEVMETHOD_END
154 };
155 
156 static driver_t my_driver = {
157 	"my",
158 	my_methods,
159 	sizeof(struct my_softc)
160 };
161 
162 DRIVER_MODULE(my, pci, my_driver, 0, 0);
163 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, my, my_devs,
164     nitems(my_devs) - 1);
165 MODULE_DEPEND(my, pci, 1, 1, 1);
166 MODULE_DEPEND(my, ether, 1, 1, 1);
167 
168 static long
169 my_send_cmd_to_phy(struct my_softc * sc, int opcode, int regad)
170 {
171 	long            miir;
172 	int             i;
173 	int             mask, data;
174 
175 	MY_LOCK_ASSERT(sc);
176 
177 	/* enable MII output */
178 	miir = CSR_READ_4(sc, MY_MANAGEMENT);
179 	miir &= 0xfffffff0;
180 
181 	miir |= MY_MASK_MIIR_MII_WRITE + MY_MASK_MIIR_MII_MDO;
182 
183 	/* send 32 1's preamble */
184 	for (i = 0; i < 32; i++) {
185 		/* low MDC; MDO is already high (miir) */
186 		miir &= ~MY_MASK_MIIR_MII_MDC;
187 		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
188 
189 		/* high MDC */
190 		miir |= MY_MASK_MIIR_MII_MDC;
191 		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
192 	}
193 
194 	/* calculate ST+OP+PHYAD+REGAD+TA */
195 	data = opcode | (sc->my_phy_addr << 7) | (regad << 2);
196 
197 	/* sent out */
198 	mask = 0x8000;
199 	while (mask) {
200 		/* low MDC, prepare MDO */
201 		miir &= ~(MY_MASK_MIIR_MII_MDC + MY_MASK_MIIR_MII_MDO);
202 		if (mask & data)
203 			miir |= MY_MASK_MIIR_MII_MDO;
204 
205 		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
206 		/* high MDC */
207 		miir |= MY_MASK_MIIR_MII_MDC;
208 		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
209 		DELAY(30);
210 
211 		/* next */
212 		mask >>= 1;
213 		if (mask == 0x2 && opcode == MY_OP_READ)
214 			miir &= ~MY_MASK_MIIR_MII_WRITE;
215 	}
216 
217 	return miir;
218 }
219 
220 static u_int16_t
221 my_phy_readreg(struct my_softc * sc, int reg)
222 {
223 	long            miir;
224 	int             mask, data;
225 
226 	MY_LOCK_ASSERT(sc);
227 
228 	if (sc->my_info->my_did == MTD803ID)
229 		data = CSR_READ_2(sc, MY_PHYBASE + reg * 2);
230 	else {
231 		miir = my_send_cmd_to_phy(sc, MY_OP_READ, reg);
232 
233 		/* read data */
234 		mask = 0x8000;
235 		data = 0;
236 		while (mask) {
237 			/* low MDC */
238 			miir &= ~MY_MASK_MIIR_MII_MDC;
239 			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
240 
241 			/* read MDI */
242 			miir = CSR_READ_4(sc, MY_MANAGEMENT);
243 			if (miir & MY_MASK_MIIR_MII_MDI)
244 				data |= mask;
245 
246 			/* high MDC, and wait */
247 			miir |= MY_MASK_MIIR_MII_MDC;
248 			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
249 			DELAY(30);
250 
251 			/* next */
252 			mask >>= 1;
253 		}
254 
255 		/* low MDC */
256 		miir &= ~MY_MASK_MIIR_MII_MDC;
257 		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
258 	}
259 
260 	return (u_int16_t) data;
261 }
262 
263 static void
264 my_phy_writereg(struct my_softc * sc, int reg, int data)
265 {
266 	long            miir;
267 	int             mask;
268 
269 	MY_LOCK_ASSERT(sc);
270 
271 	if (sc->my_info->my_did == MTD803ID)
272 		CSR_WRITE_2(sc, MY_PHYBASE + reg * 2, data);
273 	else {
274 		miir = my_send_cmd_to_phy(sc, MY_OP_WRITE, reg);
275 
276 		/* write data */
277 		mask = 0x8000;
278 		while (mask) {
279 			/* low MDC, prepare MDO */
280 			miir &= ~(MY_MASK_MIIR_MII_MDC + MY_MASK_MIIR_MII_MDO);
281 			if (mask & data)
282 				miir |= MY_MASK_MIIR_MII_MDO;
283 			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
284 			DELAY(1);
285 
286 			/* high MDC */
287 			miir |= MY_MASK_MIIR_MII_MDC;
288 			CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
289 			DELAY(1);
290 
291 			/* next */
292 			mask >>= 1;
293 		}
294 
295 		/* low MDC */
296 		miir &= ~MY_MASK_MIIR_MII_MDC;
297 		CSR_WRITE_4(sc, MY_MANAGEMENT, miir);
298 	}
299 	return;
300 }
301 
302 static u_int
303 my_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
304 {
305 	uint32_t *hashes = arg;
306 	int h;
307 
308 	h = ~ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
309 	if (h < 32)
310 		hashes[0] |= (1 << h);
311 	else
312 		hashes[1] |= (1 << (h - 32));
313 
314 	return (1);
315 }
316 /*
317  * Program the 64-bit multicast hash filter.
318  */
319 static void
320 my_setmulti(struct my_softc * sc)
321 {
322 	if_t		ifp;
323 	u_int32_t       hashes[2] = {0, 0};
324 	u_int32_t       rxfilt;
325 
326 	MY_LOCK_ASSERT(sc);
327 
328 	ifp = sc->my_ifp;
329 
330 	rxfilt = CSR_READ_4(sc, MY_TCRRCR);
331 
332 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
333 		rxfilt |= MY_AM;
334 		CSR_WRITE_4(sc, MY_TCRRCR, rxfilt);
335 		CSR_WRITE_4(sc, MY_MAR0, 0xFFFFFFFF);
336 		CSR_WRITE_4(sc, MY_MAR1, 0xFFFFFFFF);
337 
338 		return;
339 	}
340 	/* first, zot all the existing hash bits */
341 	CSR_WRITE_4(sc, MY_MAR0, 0);
342 	CSR_WRITE_4(sc, MY_MAR1, 0);
343 
344 	/* now program new ones */
345 	if (if_foreach_llmaddr(ifp, my_hash_maddr, hashes) > 0)
346 		rxfilt |= MY_AM;
347 	else
348 		rxfilt &= ~MY_AM;
349 	CSR_WRITE_4(sc, MY_MAR0, hashes[0]);
350 	CSR_WRITE_4(sc, MY_MAR1, hashes[1]);
351 	CSR_WRITE_4(sc, MY_TCRRCR, rxfilt);
352 }
353 
354 /*
355  * Initiate an autonegotiation session.
356  */
357 static void
358 my_autoneg_xmit(struct my_softc * sc)
359 {
360 	u_int16_t       phy_sts = 0;
361 
362 	MY_LOCK_ASSERT(sc);
363 
364 	my_phy_writereg(sc, PHY_BMCR, PHY_BMCR_RESET);
365 	DELAY(500);
366 	while (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_RESET);
367 
368 	phy_sts = my_phy_readreg(sc, PHY_BMCR);
369 	phy_sts |= PHY_BMCR_AUTONEGENBL | PHY_BMCR_AUTONEGRSTR;
370 	my_phy_writereg(sc, PHY_BMCR, phy_sts);
371 
372 	return;
373 }
374 
375 static void
376 my_autoneg_timeout(void *arg)
377 {
378 	struct my_softc *sc;
379 
380 	sc = arg;
381 	MY_LOCK_ASSERT(sc);
382 	my_autoneg_mii(sc, MY_FLAG_DELAYTIMEO, 1);
383 }
384 
385 /*
386  * Invoke autonegotiation on a PHY.
387  */
388 static void
389 my_autoneg_mii(struct my_softc * sc, int flag, int verbose)
390 {
391 	u_int16_t       phy_sts = 0, media, advert, ability;
392 	u_int16_t       ability2 = 0;
393 	if_t		ifp;
394 	struct ifmedia *ifm;
395 
396 	MY_LOCK_ASSERT(sc);
397 
398 	ifm = &sc->ifmedia;
399 	ifp = sc->my_ifp;
400 
401 	ifm->ifm_media = IFM_ETHER | IFM_AUTO;
402 
403 #ifndef FORCE_AUTONEG_TFOUR
404 	/*
405 	 * First, see if autoneg is supported. If not, there's no point in
406 	 * continuing.
407 	 */
408 	phy_sts = my_phy_readreg(sc, PHY_BMSR);
409 	if (!(phy_sts & PHY_BMSR_CANAUTONEG)) {
410 		if (verbose)
411 			device_printf(sc->my_dev,
412 			    "autonegotiation not supported\n");
413 		ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX;
414 		return;
415 	}
416 #endif
417 	switch (flag) {
418 	case MY_FLAG_FORCEDELAY:
419 		/*
420 		 * XXX Never use this option anywhere but in the probe
421 		 * routine: making the kernel stop dead in its tracks for
422 		 * three whole seconds after we've gone multi-user is really
423 		 * bad manners.
424 		 */
425 		my_autoneg_xmit(sc);
426 		DELAY(5000000);
427 		break;
428 	case MY_FLAG_SCHEDDELAY:
429 		/*
430 		 * Wait for the transmitter to go idle before starting an
431 		 * autoneg session, otherwise my_start() may clobber our
432 		 * timeout, and we don't want to allow transmission during an
433 		 * autoneg session since that can screw it up.
434 		 */
435 		if (sc->my_cdata.my_tx_head != NULL) {
436 			sc->my_want_auto = 1;
437 			MY_UNLOCK(sc);
438 			return;
439 		}
440 		my_autoneg_xmit(sc);
441 		callout_reset(&sc->my_autoneg_timer, hz * 5, my_autoneg_timeout,
442 		    sc);
443 		sc->my_autoneg = 1;
444 		sc->my_want_auto = 0;
445 		return;
446 	case MY_FLAG_DELAYTIMEO:
447 		callout_stop(&sc->my_autoneg_timer);
448 		sc->my_autoneg = 0;
449 		break;
450 	default:
451 		device_printf(sc->my_dev, "invalid autoneg flag: %d\n", flag);
452 		return;
453 	}
454 
455 	if (my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_AUTONEGCOMP) {
456 		if (verbose)
457 			device_printf(sc->my_dev, "autoneg complete, ");
458 		phy_sts = my_phy_readreg(sc, PHY_BMSR);
459 	} else {
460 		if (verbose)
461 			device_printf(sc->my_dev, "autoneg not complete, ");
462 	}
463 
464 	media = my_phy_readreg(sc, PHY_BMCR);
465 
466 	/* Link is good. Report modes and set duplex mode. */
467 	if (my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT) {
468 		if (verbose)
469 			device_printf(sc->my_dev, "link status good. ");
470 		advert = my_phy_readreg(sc, PHY_ANAR);
471 		ability = my_phy_readreg(sc, PHY_LPAR);
472 		if ((sc->my_pinfo->my_vid == MarvellPHYID0) ||
473 		    (sc->my_pinfo->my_vid == LevelOnePHYID0)) {
474 			ability2 = my_phy_readreg(sc, PHY_1000SR);
475 			if (ability2 & PHY_1000SR_1000BTXFULL) {
476 				advert = 0;
477 				ability = 0;
478 				/*
479 				 * this version did not support 1000M,
480 				 * ifm->ifm_media =
481 				 * IFM_ETHER|IFM_1000_T|IFM_FDX;
482 				 */
483 				ifm->ifm_media =
484 				    IFM_ETHER | IFM_100_TX | IFM_FDX;
485 				media &= ~PHY_BMCR_SPEEDSEL;
486 				media |= PHY_BMCR_1000;
487 				media |= PHY_BMCR_DUPLEX;
488 				printf("(full-duplex, 1000Mbps)\n");
489 			} else if (ability2 & PHY_1000SR_1000BTXHALF) {
490 				advert = 0;
491 				ability = 0;
492 				/*
493 				 * this version did not support 1000M,
494 				 * ifm->ifm_media = IFM_ETHER|IFM_1000_T;
495 				 */
496 				ifm->ifm_media = IFM_ETHER | IFM_100_TX;
497 				media &= ~PHY_BMCR_SPEEDSEL;
498 				media &= ~PHY_BMCR_DUPLEX;
499 				media |= PHY_BMCR_1000;
500 				printf("(half-duplex, 1000Mbps)\n");
501 			}
502 		}
503 		if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4) {
504 			ifm->ifm_media = IFM_ETHER | IFM_100_T4;
505 			media |= PHY_BMCR_SPEEDSEL;
506 			media &= ~PHY_BMCR_DUPLEX;
507 			printf("(100baseT4)\n");
508 		} else if (advert & PHY_ANAR_100BTXFULL &&
509 			   ability & PHY_ANAR_100BTXFULL) {
510 			ifm->ifm_media = IFM_ETHER | IFM_100_TX | IFM_FDX;
511 			media |= PHY_BMCR_SPEEDSEL;
512 			media |= PHY_BMCR_DUPLEX;
513 			printf("(full-duplex, 100Mbps)\n");
514 		} else if (advert & PHY_ANAR_100BTXHALF &&
515 			   ability & PHY_ANAR_100BTXHALF) {
516 			ifm->ifm_media = IFM_ETHER | IFM_100_TX | IFM_HDX;
517 			media |= PHY_BMCR_SPEEDSEL;
518 			media &= ~PHY_BMCR_DUPLEX;
519 			printf("(half-duplex, 100Mbps)\n");
520 		} else if (advert & PHY_ANAR_10BTFULL &&
521 			   ability & PHY_ANAR_10BTFULL) {
522 			ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_FDX;
523 			media &= ~PHY_BMCR_SPEEDSEL;
524 			media |= PHY_BMCR_DUPLEX;
525 			printf("(full-duplex, 10Mbps)\n");
526 		} else if (advert) {
527 			ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX;
528 			media &= ~PHY_BMCR_SPEEDSEL;
529 			media &= ~PHY_BMCR_DUPLEX;
530 			printf("(half-duplex, 10Mbps)\n");
531 		}
532 		media &= ~PHY_BMCR_AUTONEGENBL;
533 
534 		/* Set ASIC's duplex mode to match the PHY. */
535 		my_phy_writereg(sc, PHY_BMCR, media);
536 		my_setcfg(sc, media);
537 	} else {
538 		if (verbose)
539 			device_printf(sc->my_dev, "no carrier\n");
540 	}
541 
542 	my_init_locked(sc);
543 	if (sc->my_tx_pend) {
544 		sc->my_autoneg = 0;
545 		sc->my_tx_pend = 0;
546 		my_start_locked(ifp);
547 	}
548 	return;
549 }
550 
551 /*
552  * To get PHY ability.
553  */
554 static void
555 my_getmode_mii(struct my_softc * sc)
556 {
557 	u_int16_t       bmsr;
558 	if_t		ifp;
559 
560 	MY_LOCK_ASSERT(sc);
561 	ifp = sc->my_ifp;
562 	bmsr = my_phy_readreg(sc, PHY_BMSR);
563 	if (bootverbose)
564 		device_printf(sc->my_dev, "PHY status word: %x\n", bmsr);
565 
566 	/* fallback */
567 	sc->ifmedia.ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX;
568 
569 	if (bmsr & PHY_BMSR_10BTHALF) {
570 		if (bootverbose)
571 			device_printf(sc->my_dev,
572 			    "10Mbps half-duplex mode supported\n");
573 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T | IFM_HDX,
574 		    0, NULL);
575 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
576 	}
577 	if (bmsr & PHY_BMSR_10BTFULL) {
578 		if (bootverbose)
579 			device_printf(sc->my_dev,
580 			    "10Mbps full-duplex mode supported\n");
581 
582 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX,
583 		    0, NULL);
584 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_10_T | IFM_FDX;
585 	}
586 	if (bmsr & PHY_BMSR_100BTXHALF) {
587 		if (bootverbose)
588 			device_printf(sc->my_dev,
589 			    "100Mbps half-duplex mode supported\n");
590 		if_setbaudrate(ifp, 100000000);
591 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL);
592 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX | IFM_HDX,
593 			    0, NULL);
594 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_TX | IFM_HDX;
595 	}
596 	if (bmsr & PHY_BMSR_100BTXFULL) {
597 		if (bootverbose)
598 			device_printf(sc->my_dev,
599 			    "100Mbps full-duplex mode supported\n");
600 		if_setbaudrate(ifp, 100000000);
601 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX,
602 		    0, NULL);
603 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_TX | IFM_FDX;
604 	}
605 	/* Some also support 100BaseT4. */
606 	if (bmsr & PHY_BMSR_100BT4) {
607 		if (bootverbose)
608 			device_printf(sc->my_dev, "100baseT4 mode supported\n");
609 		if_setbaudrate(ifp, 100000000);
610 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_T4, 0, NULL);
611 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_T4;
612 #ifdef FORCE_AUTONEG_TFOUR
613 		if (bootverbose)
614 			device_printf(sc->my_dev,
615 			    "forcing on autoneg support for BT4\n");
616 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0 NULL):
617 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_AUTO;
618 #endif
619 	}
620 #if 0				/* this version did not support 1000M, */
621 	if (sc->my_pinfo->my_vid == MarvellPHYID0) {
622 		if (bootverbose)
623 			device_printf(sc->my_dev,
624 			    "1000Mbps half-duplex mode supported\n");
625 
626 		if_setbaudrate(ifp, 1000000000);
627 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T, 0, NULL);
628 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T | IFM_HDX,
629 		    0, NULL);
630 		if (bootverbose)
631 			device_printf(sc->my_dev,
632 			    "1000Mbps full-duplex mode supported\n");
633 		if_setbaudrate(ifp, 1000000000);
634 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T | IFM_FDX,
635 		    0, NULL);
636 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_1000_T | IFM_FDX;
637 	}
638 #endif
639 	if (bmsr & PHY_BMSR_CANAUTONEG) {
640 		if (bootverbose)
641 			device_printf(sc->my_dev, "autoneg supported\n");
642 		ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
643 		sc->ifmedia.ifm_media = IFM_ETHER | IFM_AUTO;
644 	}
645 	return;
646 }
647 
648 /*
649  * Set speed and duplex mode.
650  */
651 static void
652 my_setmode_mii(struct my_softc * sc, int media)
653 {
654 	u_int16_t       bmcr;
655 
656 	MY_LOCK_ASSERT(sc);
657 	/*
658 	 * If an autoneg session is in progress, stop it.
659 	 */
660 	if (sc->my_autoneg) {
661 		device_printf(sc->my_dev, "canceling autoneg session\n");
662 		callout_stop(&sc->my_autoneg_timer);
663 		sc->my_autoneg = sc->my_want_auto = 0;
664 		bmcr = my_phy_readreg(sc, PHY_BMCR);
665 		bmcr &= ~PHY_BMCR_AUTONEGENBL;
666 		my_phy_writereg(sc, PHY_BMCR, bmcr);
667 	}
668 	device_printf(sc->my_dev, "selecting MII, ");
669 	bmcr = my_phy_readreg(sc, PHY_BMCR);
670 	bmcr &= ~(PHY_BMCR_AUTONEGENBL | PHY_BMCR_SPEEDSEL | PHY_BMCR_1000 |
671 		  PHY_BMCR_DUPLEX | PHY_BMCR_LOOPBK);
672 
673 #if 0				/* this version did not support 1000M, */
674 	if (IFM_SUBTYPE(media) == IFM_1000_T) {
675 		printf("1000Mbps/T4, half-duplex\n");
676 		bmcr &= ~PHY_BMCR_SPEEDSEL;
677 		bmcr &= ~PHY_BMCR_DUPLEX;
678 		bmcr |= PHY_BMCR_1000;
679 	}
680 #endif
681 	if (IFM_SUBTYPE(media) == IFM_100_T4) {
682 		printf("100Mbps/T4, half-duplex\n");
683 		bmcr |= PHY_BMCR_SPEEDSEL;
684 		bmcr &= ~PHY_BMCR_DUPLEX;
685 	}
686 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
687 		printf("100Mbps, ");
688 		bmcr |= PHY_BMCR_SPEEDSEL;
689 	}
690 	if (IFM_SUBTYPE(media) == IFM_10_T) {
691 		printf("10Mbps, ");
692 		bmcr &= ~PHY_BMCR_SPEEDSEL;
693 	}
694 	if ((media & IFM_GMASK) == IFM_FDX) {
695 		printf("full duplex\n");
696 		bmcr |= PHY_BMCR_DUPLEX;
697 	} else {
698 		printf("half duplex\n");
699 		bmcr &= ~PHY_BMCR_DUPLEX;
700 	}
701 	my_phy_writereg(sc, PHY_BMCR, bmcr);
702 	my_setcfg(sc, bmcr);
703 	return;
704 }
705 
706 /*
707  * The Myson manual states that in order to fiddle with the 'full-duplex' and
708  * '100Mbps' bits in the netconfig register, we first have to put the
709  * transmit and/or receive logic in the idle state.
710  */
711 static void
712 my_setcfg(struct my_softc * sc, int bmcr)
713 {
714 	int             i, restart = 0;
715 
716 	MY_LOCK_ASSERT(sc);
717 	if (CSR_READ_4(sc, MY_TCRRCR) & (MY_TE | MY_RE)) {
718 		restart = 1;
719 		MY_CLRBIT(sc, MY_TCRRCR, (MY_TE | MY_RE));
720 		for (i = 0; i < MY_TIMEOUT; i++) {
721 			DELAY(10);
722 			if (!(CSR_READ_4(sc, MY_TCRRCR) &
723 			    (MY_TXRUN | MY_RXRUN)))
724 				break;
725 		}
726 		if (i == MY_TIMEOUT)
727 			device_printf(sc->my_dev,
728 			    "failed to force tx and rx to idle \n");
729 	}
730 	MY_CLRBIT(sc, MY_TCRRCR, MY_PS1000);
731 	MY_CLRBIT(sc, MY_TCRRCR, MY_PS10);
732 	if (bmcr & PHY_BMCR_1000)
733 		MY_SETBIT(sc, MY_TCRRCR, MY_PS1000);
734 	else if (!(bmcr & PHY_BMCR_SPEEDSEL))
735 		MY_SETBIT(sc, MY_TCRRCR, MY_PS10);
736 	if (bmcr & PHY_BMCR_DUPLEX)
737 		MY_SETBIT(sc, MY_TCRRCR, MY_FD);
738 	else
739 		MY_CLRBIT(sc, MY_TCRRCR, MY_FD);
740 	if (restart)
741 		MY_SETBIT(sc, MY_TCRRCR, MY_TE | MY_RE);
742 	return;
743 }
744 
745 static void
746 my_reset(struct my_softc * sc)
747 {
748 	int    i;
749 
750 	MY_LOCK_ASSERT(sc);
751 	MY_SETBIT(sc, MY_BCR, MY_SWR);
752 	for (i = 0; i < MY_TIMEOUT; i++) {
753 		DELAY(10);
754 		if (!(CSR_READ_4(sc, MY_BCR) & MY_SWR))
755 			break;
756 	}
757 	if (i == MY_TIMEOUT)
758 		device_printf(sc->my_dev, "reset never completed!\n");
759 
760 	/* Wait a little while for the chip to get its brains in order. */
761 	DELAY(1000);
762 	return;
763 }
764 
765 /*
766  * Probe for a Myson chip. Check the PCI vendor and device IDs against our
767  * list and return a device name if we find a match.
768  */
769 static int
770 my_probe(device_t dev)
771 {
772 	struct my_type *t;
773 
774 	t = my_devs;
775 	while (t->my_name != NULL) {
776 		if ((pci_get_vendor(dev) == t->my_vid) &&
777 		    (pci_get_device(dev) == t->my_did)) {
778 			device_set_desc(dev, t->my_name);
779 			my_info_tmp = t;
780 			return (BUS_PROBE_DEFAULT);
781 		}
782 		t++;
783 	}
784 	return (ENXIO);
785 }
786 
787 /*
788  * Attach the interface. Allocate softc structures, do ifmedia setup and
789  * ethernet/BPF attach.
790  */
791 static int
792 my_attach(device_t dev)
793 {
794 	int             i;
795 	u_char          eaddr[ETHER_ADDR_LEN];
796 	u_int32_t       iobase;
797 	struct my_softc *sc;
798 	if_t		ifp;
799 	int             media = IFM_ETHER | IFM_100_TX | IFM_FDX;
800 	unsigned int    round;
801 	caddr_t         roundptr;
802 	struct my_type *p;
803 	u_int16_t       phy_vid, phy_did, phy_sts = 0;
804 	int             rid, error = 0;
805 
806 	sc = device_get_softc(dev);
807 	sc->my_dev = dev;
808 	mtx_init(&sc->my_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
809 	    MTX_DEF);
810 	callout_init_mtx(&sc->my_autoneg_timer, &sc->my_mtx, 0);
811 	callout_init_mtx(&sc->my_watchdog, &sc->my_mtx, 0);
812 
813 	/*
814 	 * Map control/status registers.
815 	 */
816 	pci_enable_busmaster(dev);
817 
818 	if (my_info_tmp->my_did == MTD800ID) {
819 		iobase = pci_read_config(dev, MY_PCI_LOIO, 4);
820 		if (iobase & 0x300)
821 			MY_USEIOSPACE = 0;
822 	}
823 
824 	rid = MY_RID;
825 	sc->my_res = bus_alloc_resource_any(dev, MY_RES, &rid, RF_ACTIVE);
826 
827 	if (sc->my_res == NULL) {
828 		device_printf(dev, "couldn't map ports/memory\n");
829 		error = ENXIO;
830 		goto destroy_mutex;
831 	}
832 	sc->my_btag = rman_get_bustag(sc->my_res);
833 	sc->my_bhandle = rman_get_bushandle(sc->my_res);
834 
835 	rid = 0;
836 	sc->my_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
837 					    RF_SHAREABLE | RF_ACTIVE);
838 
839 	if (sc->my_irq == NULL) {
840 		device_printf(dev, "couldn't map interrupt\n");
841 		error = ENXIO;
842 		goto release_io;
843 	}
844 
845 	sc->my_info = my_info_tmp;
846 
847 	/* Reset the adapter. */
848 	MY_LOCK(sc);
849 	my_reset(sc);
850 	MY_UNLOCK(sc);
851 
852 	/*
853 	 * Get station address
854 	 */
855 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
856 		eaddr[i] = CSR_READ_1(sc, MY_PAR0 + i);
857 
858 	sc->my_ldata_ptr = malloc(sizeof(struct my_list_data) + 8,
859 				  M_DEVBUF, M_NOWAIT);
860 	if (sc->my_ldata_ptr == NULL) {
861 		device_printf(dev, "no memory for list buffers!\n");
862 		error = ENXIO;
863 		goto release_irq;
864 	}
865 	sc->my_ldata = (struct my_list_data *) sc->my_ldata_ptr;
866 	round = (uintptr_t)sc->my_ldata_ptr & 0xF;
867 	roundptr = sc->my_ldata_ptr;
868 	for (i = 0; i < 8; i++) {
869 		if (round % 8) {
870 			round++;
871 			roundptr++;
872 		} else
873 			break;
874 	}
875 	sc->my_ldata = (struct my_list_data *) roundptr;
876 	bzero(sc->my_ldata, sizeof(struct my_list_data));
877 
878 	ifp = sc->my_ifp = if_alloc(IFT_ETHER);
879 	if (ifp == NULL) {
880 		device_printf(dev, "can not if_alloc()\n");
881 		error = ENOSPC;
882 		goto free_ldata;
883 	}
884 	if_setsoftc(ifp, sc);
885 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
886 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
887 	if_setioctlfn(ifp, my_ioctl);
888 	if_setstartfn(ifp, my_start);
889 	if_setinitfn(ifp, my_init);
890 	if_setbaudrate(ifp, 10000000);
891 	if_setsendqlen(ifp, ifqmaxlen);
892 	if_setsendqready(ifp);
893 
894 	if (sc->my_info->my_did == MTD803ID)
895 		sc->my_pinfo = my_phys;
896 	else {
897 		if (bootverbose)
898 			device_printf(dev, "probing for a PHY\n");
899 		MY_LOCK(sc);
900 		for (i = MY_PHYADDR_MIN; i < MY_PHYADDR_MAX + 1; i++) {
901 			if (bootverbose)
902 				device_printf(dev, "checking address: %d\n", i);
903 			sc->my_phy_addr = i;
904 			phy_sts = my_phy_readreg(sc, PHY_BMSR);
905 			if ((phy_sts != 0) && (phy_sts != 0xffff))
906 				break;
907 			else
908 				phy_sts = 0;
909 		}
910 		if (phy_sts) {
911 			phy_vid = my_phy_readreg(sc, PHY_VENID);
912 			phy_did = my_phy_readreg(sc, PHY_DEVID);
913 			if (bootverbose) {
914 				device_printf(dev, "found PHY at address %d, ",
915 				    sc->my_phy_addr);
916 				printf("vendor id: %x device id: %x\n",
917 				    phy_vid, phy_did);
918 			}
919 			p = my_phys;
920 			while (p->my_vid) {
921 				if (phy_vid == p->my_vid) {
922 					sc->my_pinfo = p;
923 					break;
924 				}
925 				p++;
926 			}
927 			if (sc->my_pinfo == NULL)
928 				sc->my_pinfo = &my_phys[PHY_UNKNOWN];
929 			if (bootverbose)
930 				device_printf(dev, "PHY type: %s\n",
931 				       sc->my_pinfo->my_name);
932 		} else {
933 			MY_UNLOCK(sc);
934 			device_printf(dev, "MII without any phy!\n");
935 			error = ENXIO;
936 			goto free_if;
937 		}
938 		MY_UNLOCK(sc);
939 	}
940 
941 	/* Do ifmedia setup. */
942 	ifmedia_init(&sc->ifmedia, 0, my_ifmedia_upd, my_ifmedia_sts);
943 	MY_LOCK(sc);
944 	my_getmode_mii(sc);
945 	my_autoneg_mii(sc, MY_FLAG_FORCEDELAY, 1);
946 	media = sc->ifmedia.ifm_media;
947 	my_stop(sc);
948 	MY_UNLOCK(sc);
949 	ifmedia_set(&sc->ifmedia, media);
950 
951 	ether_ifattach(ifp, eaddr);
952 
953 	error = bus_setup_intr(dev, sc->my_irq, INTR_TYPE_NET | INTR_MPSAFE,
954 			       NULL, my_intr, sc, &sc->my_intrhand);
955 
956 	if (error) {
957 		device_printf(dev, "couldn't set up irq\n");
958 		goto detach_if;
959 	}
960 
961 	return (0);
962 
963 detach_if:
964 	ether_ifdetach(ifp);
965 free_if:
966 	if_free(ifp);
967 free_ldata:
968 	free(sc->my_ldata_ptr, M_DEVBUF);
969 release_irq:
970 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->my_irq);
971 release_io:
972 	bus_release_resource(dev, MY_RES, MY_RID, sc->my_res);
973 destroy_mutex:
974 	mtx_destroy(&sc->my_mtx);
975 	return (error);
976 }
977 
978 static int
979 my_detach(device_t dev)
980 {
981 	struct my_softc *sc;
982 	if_t		ifp;
983 
984 	sc = device_get_softc(dev);
985 	ifp = sc->my_ifp;
986 	ether_ifdetach(ifp);
987 	MY_LOCK(sc);
988 	my_stop(sc);
989 	MY_UNLOCK(sc);
990 	bus_teardown_intr(dev, sc->my_irq, sc->my_intrhand);
991 	callout_drain(&sc->my_watchdog);
992 	callout_drain(&sc->my_autoneg_timer);
993 
994 	if_free(ifp);
995 	free(sc->my_ldata_ptr, M_DEVBUF);
996 
997 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->my_irq);
998 	bus_release_resource(dev, MY_RES, MY_RID, sc->my_res);
999 	mtx_destroy(&sc->my_mtx);
1000 	return (0);
1001 }
1002 
1003 /*
1004  * Initialize the transmit descriptors.
1005  */
1006 static int
1007 my_list_tx_init(struct my_softc * sc)
1008 {
1009 	struct my_chain_data *cd;
1010 	struct my_list_data *ld;
1011 	int             i;
1012 
1013 	MY_LOCK_ASSERT(sc);
1014 	cd = &sc->my_cdata;
1015 	ld = sc->my_ldata;
1016 	for (i = 0; i < MY_TX_LIST_CNT; i++) {
1017 		cd->my_tx_chain[i].my_ptr = &ld->my_tx_list[i];
1018 		if (i == (MY_TX_LIST_CNT - 1))
1019 			cd->my_tx_chain[i].my_nextdesc = &cd->my_tx_chain[0];
1020 		else
1021 			cd->my_tx_chain[i].my_nextdesc =
1022 			    &cd->my_tx_chain[i + 1];
1023 	}
1024 	cd->my_tx_free = &cd->my_tx_chain[0];
1025 	cd->my_tx_tail = cd->my_tx_head = NULL;
1026 	return (0);
1027 }
1028 
1029 /*
1030  * Initialize the RX descriptors and allocate mbufs for them. Note that we
1031  * arrange the descriptors in a closed ring, so that the last descriptor
1032  * points back to the first.
1033  */
1034 static int
1035 my_list_rx_init(struct my_softc * sc)
1036 {
1037 	struct my_chain_data *cd;
1038 	struct my_list_data *ld;
1039 	int             i;
1040 
1041 	MY_LOCK_ASSERT(sc);
1042 	cd = &sc->my_cdata;
1043 	ld = sc->my_ldata;
1044 	for (i = 0; i < MY_RX_LIST_CNT; i++) {
1045 		cd->my_rx_chain[i].my_ptr =
1046 		    (struct my_desc *) & ld->my_rx_list[i];
1047 		if (my_newbuf(sc, &cd->my_rx_chain[i]) == ENOBUFS) {
1048 			MY_UNLOCK(sc);
1049 			return (ENOBUFS);
1050 		}
1051 		if (i == (MY_RX_LIST_CNT - 1)) {
1052 			cd->my_rx_chain[i].my_nextdesc = &cd->my_rx_chain[0];
1053 			ld->my_rx_list[i].my_next = vtophys(&ld->my_rx_list[0]);
1054 		} else {
1055 			cd->my_rx_chain[i].my_nextdesc =
1056 			    &cd->my_rx_chain[i + 1];
1057 			ld->my_rx_list[i].my_next =
1058 			    vtophys(&ld->my_rx_list[i + 1]);
1059 		}
1060 	}
1061 	cd->my_rx_head = &cd->my_rx_chain[0];
1062 	return (0);
1063 }
1064 
1065 /*
1066  * Initialize an RX descriptor and attach an MBUF cluster.
1067  */
1068 static int
1069 my_newbuf(struct my_softc * sc, struct my_chain_onefrag * c)
1070 {
1071 	struct mbuf    *m_new = NULL;
1072 
1073 	MY_LOCK_ASSERT(sc);
1074 	MGETHDR(m_new, M_NOWAIT, MT_DATA);
1075 	if (m_new == NULL) {
1076 		device_printf(sc->my_dev,
1077 		    "no memory for rx list -- packet dropped!\n");
1078 		return (ENOBUFS);
1079 	}
1080 	if (!(MCLGET(m_new, M_NOWAIT))) {
1081 		device_printf(sc->my_dev,
1082 		    "no memory for rx list -- packet dropped!\n");
1083 		m_freem(m_new);
1084 		return (ENOBUFS);
1085 	}
1086 	c->my_mbuf = m_new;
1087 	c->my_ptr->my_data = vtophys(mtod(m_new, caddr_t));
1088 	c->my_ptr->my_ctl = (MCLBYTES - 1) << MY_RBSShift;
1089 	c->my_ptr->my_status = MY_OWNByNIC;
1090 	return (0);
1091 }
1092 
1093 /*
1094  * A frame has been uploaded: pass the resulting mbuf chain up to the higher
1095  * level protocols.
1096  */
1097 static void
1098 my_rxeof(struct my_softc * sc)
1099 {
1100 	struct ether_header *eh;
1101 	struct mbuf    *m;
1102 	if_t		ifp;
1103 	struct my_chain_onefrag *cur_rx;
1104 	int             total_len = 0;
1105 	u_int32_t       rxstat;
1106 
1107 	MY_LOCK_ASSERT(sc);
1108 	ifp = sc->my_ifp;
1109 	while (!((rxstat = sc->my_cdata.my_rx_head->my_ptr->my_status)
1110 	    & MY_OWNByNIC)) {
1111 		cur_rx = sc->my_cdata.my_rx_head;
1112 		sc->my_cdata.my_rx_head = cur_rx->my_nextdesc;
1113 
1114 		if (rxstat & MY_ES) {	/* error summary: give up this rx pkt */
1115 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1116 			cur_rx->my_ptr->my_status = MY_OWNByNIC;
1117 			continue;
1118 		}
1119 		/* No errors; receive the packet. */
1120 		total_len = (rxstat & MY_FLNGMASK) >> MY_FLNGShift;
1121 		total_len -= ETHER_CRC_LEN;
1122 
1123 		if (total_len < MINCLSIZE) {
1124 			m = m_devget(mtod(cur_rx->my_mbuf, char *),
1125 			    total_len, 0, ifp, NULL);
1126 			cur_rx->my_ptr->my_status = MY_OWNByNIC;
1127 			if (m == NULL) {
1128 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1129 				continue;
1130 			}
1131 		} else {
1132 			m = cur_rx->my_mbuf;
1133 			/*
1134 			 * Try to conjure up a new mbuf cluster. If that
1135 			 * fails, it means we have an out of memory condition
1136 			 * and should leave the buffer in place and continue.
1137 			 * This will result in a lost packet, but there's
1138 			 * little else we can do in this situation.
1139 			 */
1140 			if (my_newbuf(sc, cur_rx) == ENOBUFS) {
1141 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1142 				cur_rx->my_ptr->my_status = MY_OWNByNIC;
1143 				continue;
1144 			}
1145 			m->m_pkthdr.rcvif = ifp;
1146 			m->m_pkthdr.len = m->m_len = total_len;
1147 		}
1148 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1149 		eh = mtod(m, struct ether_header *);
1150 #if NBPFILTER > 0
1151 		/*
1152 		 * Handle BPF listeners. Let the BPF user see the packet, but
1153 		 * don't pass it up to the ether_input() layer unless it's a
1154 		 * broadcast packet, multicast packet, matches our ethernet
1155 		 * address or the interface is in promiscuous mode.
1156 		 */
1157 		if (bpf_peers_present(if_getbpf(ifp))) {
1158 			bpf_mtap_if(ifp, m);
1159 			if (if_getflags(ifp) & IFF_PROMISC &&
1160 			    (bcmp(eh->ether_dhost, if_getlladdr(sc->my_ifp),
1161 				ETHER_ADDR_LEN) &&
1162 			     (eh->ether_dhost[0] & 1) == 0)) {
1163 				m_freem(m);
1164 				continue;
1165 			}
1166 		}
1167 #endif
1168 		MY_UNLOCK(sc);
1169 		if_input(ifp, m);
1170 		MY_LOCK(sc);
1171 	}
1172 	return;
1173 }
1174 
1175 /*
1176  * A frame was downloaded to the chip. It's safe for us to clean up the list
1177  * buffers.
1178  */
1179 static void
1180 my_txeof(struct my_softc * sc)
1181 {
1182 	struct my_chain *cur_tx;
1183 	if_t		ifp;
1184 
1185 	MY_LOCK_ASSERT(sc);
1186 	ifp = sc->my_ifp;
1187 	/* Clear the timeout timer. */
1188 	sc->my_timer = 0;
1189 	if (sc->my_cdata.my_tx_head == NULL) {
1190 		return;
1191 	}
1192 	/*
1193 	 * Go through our tx list and free mbufs for those frames that have
1194 	 * been transmitted.
1195 	 */
1196 	while (sc->my_cdata.my_tx_head->my_mbuf != NULL) {
1197 		u_int32_t       txstat;
1198 
1199 		cur_tx = sc->my_cdata.my_tx_head;
1200 		txstat = MY_TXSTATUS(cur_tx);
1201 		if ((txstat & MY_OWNByNIC) || txstat == MY_UNSENT)
1202 			break;
1203 		if (!(CSR_READ_4(sc, MY_TCRRCR) & MY_Enhanced)) {
1204 			if (txstat & MY_TXERR) {
1205 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1206 				if (txstat & MY_EC) /* excessive collision */
1207 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1208 				if (txstat & MY_LC)	/* late collision */
1209 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1210 			}
1211 			if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
1212 			    (txstat & MY_NCRMASK) >> MY_NCRShift);
1213 		}
1214 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1215 		m_freem(cur_tx->my_mbuf);
1216 		cur_tx->my_mbuf = NULL;
1217 		if (sc->my_cdata.my_tx_head == sc->my_cdata.my_tx_tail) {
1218 			sc->my_cdata.my_tx_head = NULL;
1219 			sc->my_cdata.my_tx_tail = NULL;
1220 			break;
1221 		}
1222 		sc->my_cdata.my_tx_head = cur_tx->my_nextdesc;
1223 	}
1224 	if (CSR_READ_4(sc, MY_TCRRCR) & MY_Enhanced) {
1225 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (CSR_READ_4(sc, MY_TSR) & MY_NCRMask));
1226 	}
1227 	return;
1228 }
1229 
1230 /*
1231  * TX 'end of channel' interrupt handler.
1232  */
1233 static void
1234 my_txeoc(struct my_softc * sc)
1235 {
1236 	if_t		ifp;
1237 
1238 	MY_LOCK_ASSERT(sc);
1239 	ifp = sc->my_ifp;
1240 	sc->my_timer = 0;
1241 	if (sc->my_cdata.my_tx_head == NULL) {
1242 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1243 		sc->my_cdata.my_tx_tail = NULL;
1244 		if (sc->my_want_auto)
1245 			my_autoneg_mii(sc, MY_FLAG_SCHEDDELAY, 1);
1246 	} else {
1247 		if (MY_TXOWN(sc->my_cdata.my_tx_head) == MY_UNSENT) {
1248 			MY_TXOWN(sc->my_cdata.my_tx_head) = MY_OWNByNIC;
1249 			sc->my_timer = 5;
1250 			CSR_WRITE_4(sc, MY_TXPDR, 0xFFFFFFFF);
1251 		}
1252 	}
1253 	return;
1254 }
1255 
1256 static void
1257 my_intr(void *arg)
1258 {
1259 	struct my_softc *sc;
1260 	if_t		ifp;
1261 	u_int32_t       status;
1262 
1263 	sc = arg;
1264 	MY_LOCK(sc);
1265 	ifp = sc->my_ifp;
1266 	if (!(if_getflags(ifp) & IFF_UP)) {
1267 		MY_UNLOCK(sc);
1268 		return;
1269 	}
1270 	/* Disable interrupts. */
1271 	CSR_WRITE_4(sc, MY_IMR, 0x00000000);
1272 
1273 	for (;;) {
1274 		status = CSR_READ_4(sc, MY_ISR);
1275 		status &= MY_INTRS;
1276 		if (status)
1277 			CSR_WRITE_4(sc, MY_ISR, status);
1278 		else
1279 			break;
1280 
1281 		if (status & MY_RI)	/* receive interrupt */
1282 			my_rxeof(sc);
1283 
1284 		if ((status & MY_RBU) || (status & MY_RxErr)) {
1285 			/* rx buffer unavailable or rx error */
1286 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1287 #ifdef foo
1288 			my_stop(sc);
1289 			my_reset(sc);
1290 			my_init_locked(sc);
1291 #endif
1292 		}
1293 		if (status & MY_TI)	/* tx interrupt */
1294 			my_txeof(sc);
1295 		if (status & MY_ETI)	/* tx early interrupt */
1296 			my_txeof(sc);
1297 		if (status & MY_TBU)	/* tx buffer unavailable */
1298 			my_txeoc(sc);
1299 
1300 #if 0				/* 90/1/18 delete */
1301 		if (status & MY_FBE) {
1302 			my_reset(sc);
1303 			my_init_locked(sc);
1304 		}
1305 #endif
1306 	}
1307 
1308 	/* Re-enable interrupts. */
1309 	CSR_WRITE_4(sc, MY_IMR, MY_INTRS);
1310 	if (!if_sendq_empty(ifp))
1311 		my_start_locked(ifp);
1312 	MY_UNLOCK(sc);
1313 	return;
1314 }
1315 
1316 /*
1317  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1318  * pointers to the fragment pointers.
1319  */
1320 static int
1321 my_encap(struct my_softc * sc, struct my_chain * c, struct mbuf * m_head)
1322 {
1323 	struct my_desc *f = NULL;
1324 	int             total_len;
1325 	struct mbuf    *m, *m_new = NULL;
1326 
1327 	MY_LOCK_ASSERT(sc);
1328 	/* calculate the total tx pkt length */
1329 	total_len = 0;
1330 	for (m = m_head; m != NULL; m = m->m_next)
1331 		total_len += m->m_len;
1332 	/*
1333 	 * Start packing the mbufs in this chain into the fragment pointers.
1334 	 * Stop when we run out of fragments or hit the end of the mbuf
1335 	 * chain.
1336 	 */
1337 	m = m_head;
1338 	MGETHDR(m_new, M_NOWAIT, MT_DATA);
1339 	if (m_new == NULL) {
1340 		device_printf(sc->my_dev, "no memory for tx list");
1341 		return (1);
1342 	}
1343 	if (m_head->m_pkthdr.len > MHLEN) {
1344 		if (!(MCLGET(m_new, M_NOWAIT))) {
1345 			m_freem(m_new);
1346 			device_printf(sc->my_dev, "no memory for tx list");
1347 			return (1);
1348 		}
1349 	}
1350 	m_copydata(m_head, 0, m_head->m_pkthdr.len, mtod(m_new, caddr_t));
1351 	m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1352 	m_freem(m_head);
1353 	m_head = m_new;
1354 	f = &c->my_ptr->my_frag[0];
1355 	f->my_status = 0;
1356 	f->my_data = vtophys(mtod(m_new, caddr_t));
1357 	total_len = m_new->m_len;
1358 	f->my_ctl = MY_TXFD | MY_TXLD | MY_CRCEnable | MY_PADEnable;
1359 	f->my_ctl |= total_len << MY_PKTShift;	/* pkt size */
1360 	f->my_ctl |= total_len;	/* buffer size */
1361 	/* 89/12/29 add, for mtd891 *//* [ 89? ] */
1362 	if (sc->my_info->my_did == MTD891ID)
1363 		f->my_ctl |= MY_ETIControl | MY_RetryTxLC;
1364 	c->my_mbuf = m_head;
1365 	c->my_lastdesc = 0;
1366 	MY_TXNEXT(c) = vtophys(&c->my_nextdesc->my_ptr->my_frag[0]);
1367 	return (0);
1368 }
1369 
1370 /*
1371  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1372  * to the mbuf data regions directly in the transmit lists. We also save a
1373  * copy of the pointers since the transmit list fragment pointers are
1374  * physical addresses.
1375  */
1376 static void
1377 my_start(if_t ifp)
1378 {
1379 	struct my_softc *sc;
1380 
1381 	sc = if_getsoftc(ifp);
1382 	MY_LOCK(sc);
1383 	my_start_locked(ifp);
1384 	MY_UNLOCK(sc);
1385 }
1386 
1387 static void
1388 my_start_locked(if_t ifp)
1389 {
1390 	struct my_softc *sc;
1391 	struct mbuf    *m_head = NULL;
1392 	struct my_chain *cur_tx = NULL, *start_tx;
1393 
1394 	sc = if_getsoftc(ifp);
1395 	MY_LOCK_ASSERT(sc);
1396 	if (sc->my_autoneg) {
1397 		sc->my_tx_pend = 1;
1398 		return;
1399 	}
1400 	/*
1401 	 * Check for an available queue slot. If there are none, punt.
1402 	 */
1403 	if (sc->my_cdata.my_tx_free->my_mbuf != NULL) {
1404 		if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
1405 		return;
1406 	}
1407 	start_tx = sc->my_cdata.my_tx_free;
1408 	while (sc->my_cdata.my_tx_free->my_mbuf == NULL) {
1409 		m_head = if_dequeue(ifp);
1410 		if (m_head == NULL)
1411 			break;
1412 
1413 		/* Pick a descriptor off the free list. */
1414 		cur_tx = sc->my_cdata.my_tx_free;
1415 		sc->my_cdata.my_tx_free = cur_tx->my_nextdesc;
1416 
1417 		/* Pack the data into the descriptor. */
1418 		my_encap(sc, cur_tx, m_head);
1419 
1420 		if (cur_tx != start_tx)
1421 			MY_TXOWN(cur_tx) = MY_OWNByNIC;
1422 #if NBPFILTER > 0
1423 		/*
1424 		 * If there's a BPF listener, bounce a copy of this frame to
1425 		 * him.
1426 		 */
1427 		BPF_MTAP(ifp, cur_tx->my_mbuf);
1428 #endif
1429 	}
1430 	/*
1431 	 * If there are no packets queued, bail.
1432 	 */
1433 	if (cur_tx == NULL) {
1434 		return;
1435 	}
1436 	/*
1437 	 * Place the request for the upload interrupt in the last descriptor
1438 	 * in the chain. This way, if we're chaining several packets at once,
1439 	 * we'll only get an interrupt once for the whole chain rather than
1440 	 * once for each packet.
1441 	 */
1442 	MY_TXCTL(cur_tx) |= MY_TXIC;
1443 	cur_tx->my_ptr->my_frag[0].my_ctl |= MY_TXIC;
1444 	sc->my_cdata.my_tx_tail = cur_tx;
1445 	if (sc->my_cdata.my_tx_head == NULL)
1446 		sc->my_cdata.my_tx_head = start_tx;
1447 	MY_TXOWN(start_tx) = MY_OWNByNIC;
1448 	CSR_WRITE_4(sc, MY_TXPDR, 0xFFFFFFFF);	/* tx polling demand */
1449 
1450 	/*
1451 	 * Set a timeout in case the chip goes out to lunch.
1452 	 */
1453 	sc->my_timer = 5;
1454 	return;
1455 }
1456 
1457 static void
1458 my_init(void *xsc)
1459 {
1460 	struct my_softc *sc = xsc;
1461 
1462 	MY_LOCK(sc);
1463 	my_init_locked(sc);
1464 	MY_UNLOCK(sc);
1465 }
1466 
1467 static void
1468 my_init_locked(struct my_softc *sc)
1469 {
1470 	if_t		ifp = sc->my_ifp;
1471 	u_int16_t       phy_bmcr = 0;
1472 
1473 	MY_LOCK_ASSERT(sc);
1474 	if (sc->my_autoneg) {
1475 		return;
1476 	}
1477 	if (sc->my_pinfo != NULL)
1478 		phy_bmcr = my_phy_readreg(sc, PHY_BMCR);
1479 	/*
1480 	 * Cancel pending I/O and free all RX/TX buffers.
1481 	 */
1482 	my_stop(sc);
1483 	my_reset(sc);
1484 
1485 	/*
1486 	 * Set cache alignment and burst length.
1487 	 */
1488 #if 0				/* 89/9/1 modify,  */
1489 	CSR_WRITE_4(sc, MY_BCR, MY_RPBLE512);
1490 	CSR_WRITE_4(sc, MY_TCRRCR, MY_TFTSF);
1491 #endif
1492 	CSR_WRITE_4(sc, MY_BCR, MY_PBL8);
1493 	CSR_WRITE_4(sc, MY_TCRRCR, MY_TFTSF | MY_RBLEN | MY_RPBLE512);
1494 	/*
1495 	 * 89/12/29 add, for mtd891,
1496 	 */
1497 	if (sc->my_info->my_did == MTD891ID) {
1498 		MY_SETBIT(sc, MY_BCR, MY_PROG);
1499 		MY_SETBIT(sc, MY_TCRRCR, MY_Enhanced);
1500 	}
1501 	my_setcfg(sc, phy_bmcr);
1502 	/* Init circular RX list. */
1503 	if (my_list_rx_init(sc) == ENOBUFS) {
1504 		device_printf(sc->my_dev, "init failed: no memory for rx buffers\n");
1505 		my_stop(sc);
1506 		return;
1507 	}
1508 	/* Init TX descriptors. */
1509 	my_list_tx_init(sc);
1510 
1511 	/* If we want promiscuous mode, set the allframes bit. */
1512 	if (if_getflags(ifp) & IFF_PROMISC)
1513 		MY_SETBIT(sc, MY_TCRRCR, MY_PROM);
1514 	else
1515 		MY_CLRBIT(sc, MY_TCRRCR, MY_PROM);
1516 
1517 	/*
1518 	 * Set capture broadcast bit to capture broadcast frames.
1519 	 */
1520 	if (if_getflags(ifp) & IFF_BROADCAST)
1521 		MY_SETBIT(sc, MY_TCRRCR, MY_AB);
1522 	else
1523 		MY_CLRBIT(sc, MY_TCRRCR, MY_AB);
1524 
1525 	/*
1526 	 * Program the multicast filter, if necessary.
1527 	 */
1528 	my_setmulti(sc);
1529 
1530 	/*
1531 	 * Load the address of the RX list.
1532 	 */
1533 	MY_CLRBIT(sc, MY_TCRRCR, MY_RE);
1534 	CSR_WRITE_4(sc, MY_RXLBA, vtophys(&sc->my_ldata->my_rx_list[0]));
1535 
1536 	/*
1537 	 * Enable interrupts.
1538 	 */
1539 	CSR_WRITE_4(sc, MY_IMR, MY_INTRS);
1540 	CSR_WRITE_4(sc, MY_ISR, 0xFFFFFFFF);
1541 
1542 	/* Enable receiver and transmitter. */
1543 	MY_SETBIT(sc, MY_TCRRCR, MY_RE);
1544 	MY_CLRBIT(sc, MY_TCRRCR, MY_TE);
1545 	CSR_WRITE_4(sc, MY_TXLBA, vtophys(&sc->my_ldata->my_tx_list[0]));
1546 	MY_SETBIT(sc, MY_TCRRCR, MY_TE);
1547 
1548 	/* Restore state of BMCR */
1549 	if (sc->my_pinfo != NULL)
1550 		my_phy_writereg(sc, PHY_BMCR, phy_bmcr);
1551 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
1552 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
1553 
1554 	callout_reset(&sc->my_watchdog, hz, my_watchdog, sc);
1555 	return;
1556 }
1557 
1558 /*
1559  * Set media options.
1560  */
1561 
1562 static int
1563 my_ifmedia_upd(if_t ifp)
1564 {
1565 	struct my_softc *sc;
1566 	struct ifmedia *ifm;
1567 
1568 	sc = if_getsoftc(ifp);
1569 	MY_LOCK(sc);
1570 	ifm = &sc->ifmedia;
1571 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
1572 		MY_UNLOCK(sc);
1573 		return (EINVAL);
1574 	}
1575 	if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO)
1576 		my_autoneg_mii(sc, MY_FLAG_SCHEDDELAY, 1);
1577 	else
1578 		my_setmode_mii(sc, ifm->ifm_media);
1579 	MY_UNLOCK(sc);
1580 	return (0);
1581 }
1582 
1583 /*
1584  * Report current media status.
1585  */
1586 
1587 static void
1588 my_ifmedia_sts(if_t ifp, struct ifmediareq * ifmr)
1589 {
1590 	struct my_softc *sc;
1591 	u_int16_t advert = 0, ability = 0;
1592 
1593 	sc = if_getsoftc(ifp);
1594 	MY_LOCK(sc);
1595 	ifmr->ifm_active = IFM_ETHER;
1596 	if (!(my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_AUTONEGENBL)) {
1597 #if 0				/* this version did not support 1000M, */
1598 		if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_1000)
1599 			ifmr->ifm_active = IFM_ETHER | IFM_1000TX;
1600 #endif
1601 		if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_SPEEDSEL)
1602 			ifmr->ifm_active = IFM_ETHER | IFM_100_TX;
1603 		else
1604 			ifmr->ifm_active = IFM_ETHER | IFM_10_T;
1605 		if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_DUPLEX)
1606 			ifmr->ifm_active |= IFM_FDX;
1607 		else
1608 			ifmr->ifm_active |= IFM_HDX;
1609 
1610 		MY_UNLOCK(sc);
1611 		return;
1612 	}
1613 	ability = my_phy_readreg(sc, PHY_LPAR);
1614 	advert = my_phy_readreg(sc, PHY_ANAR);
1615 
1616 #if 0				/* this version did not support 1000M, */
1617 	if (sc->my_pinfo->my_vid = MarvellPHYID0) {
1618 		ability2 = my_phy_readreg(sc, PHY_1000SR);
1619 		if (ability2 & PHY_1000SR_1000BTXFULL) {
1620 			advert = 0;
1621 			ability = 0;
1622 	  		ifmr->ifm_active = IFM_ETHER|IFM_1000_T|IFM_FDX;
1623 	  	} else if (ability & PHY_1000SR_1000BTXHALF) {
1624 			advert = 0;
1625 			ability = 0;
1626 			ifmr->ifm_active = IFM_ETHER|IFM_1000_T|IFM_HDX;
1627 		}
1628 	}
1629 #endif
1630 	if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4)
1631 		ifmr->ifm_active = IFM_ETHER | IFM_100_T4;
1632 	else if (advert & PHY_ANAR_100BTXFULL && ability & PHY_ANAR_100BTXFULL)
1633 		ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1634 	else if (advert & PHY_ANAR_100BTXHALF && ability & PHY_ANAR_100BTXHALF)
1635 		ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1636 	else if (advert & PHY_ANAR_10BTFULL && ability & PHY_ANAR_10BTFULL)
1637 		ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1638 	else if (advert & PHY_ANAR_10BTHALF && ability & PHY_ANAR_10BTHALF)
1639 		ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1640 	MY_UNLOCK(sc);
1641 	return;
1642 }
1643 
1644 static int
1645 my_ioctl(if_t ifp, u_long command, caddr_t data)
1646 {
1647 	struct my_softc *sc = if_getsoftc(ifp);
1648 	struct ifreq   *ifr = (struct ifreq *) data;
1649 	int             error;
1650 
1651 	switch (command) {
1652 	case SIOCSIFFLAGS:
1653 		MY_LOCK(sc);
1654 		if (if_getflags(ifp) & IFF_UP)
1655 			my_init_locked(sc);
1656 		else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1657 			my_stop(sc);
1658 		MY_UNLOCK(sc);
1659 		error = 0;
1660 		break;
1661 	case SIOCADDMULTI:
1662 	case SIOCDELMULTI:
1663 		MY_LOCK(sc);
1664 		my_setmulti(sc);
1665 		MY_UNLOCK(sc);
1666 		error = 0;
1667 		break;
1668 	case SIOCGIFMEDIA:
1669 	case SIOCSIFMEDIA:
1670 		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
1671 		break;
1672 	default:
1673 		error = ether_ioctl(ifp, command, data);
1674 		break;
1675 	}
1676 	return (error);
1677 }
1678 
1679 static void
1680 my_watchdog(void *arg)
1681 {
1682 	struct my_softc *sc;
1683 	if_t		ifp;
1684 
1685 	sc = arg;
1686 	MY_LOCK_ASSERT(sc);
1687 	callout_reset(&sc->my_watchdog, hz, my_watchdog, sc);
1688 	if (sc->my_timer == 0 || --sc->my_timer > 0)
1689 		return;
1690 
1691 	ifp = sc->my_ifp;
1692 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1693 	if_printf(ifp, "watchdog timeout\n");
1694 	if (!(my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
1695 		if_printf(ifp, "no carrier - transceiver cable problem?\n");
1696 	my_stop(sc);
1697 	my_reset(sc);
1698 	my_init_locked(sc);
1699 	if (!if_sendq_empty(ifp))
1700 		my_start_locked(ifp);
1701 }
1702 
1703 /*
1704  * Stop the adapter and free any mbufs allocated to the RX and TX lists.
1705  */
1706 static void
1707 my_stop(struct my_softc * sc)
1708 {
1709 	int    i;
1710 	if_t   ifp;
1711 
1712 	MY_LOCK_ASSERT(sc);
1713 	ifp = sc->my_ifp;
1714 
1715 	callout_stop(&sc->my_autoneg_timer);
1716 	callout_stop(&sc->my_watchdog);
1717 
1718 	MY_CLRBIT(sc, MY_TCRRCR, (MY_RE | MY_TE));
1719 	CSR_WRITE_4(sc, MY_IMR, 0x00000000);
1720 	CSR_WRITE_4(sc, MY_TXLBA, 0x00000000);
1721 	CSR_WRITE_4(sc, MY_RXLBA, 0x00000000);
1722 
1723 	/*
1724 	 * Free data in the RX lists.
1725 	 */
1726 	for (i = 0; i < MY_RX_LIST_CNT; i++) {
1727 		if (sc->my_cdata.my_rx_chain[i].my_mbuf != NULL) {
1728 			m_freem(sc->my_cdata.my_rx_chain[i].my_mbuf);
1729 			sc->my_cdata.my_rx_chain[i].my_mbuf = NULL;
1730 		}
1731 	}
1732 	bzero((char *)&sc->my_ldata->my_rx_list,
1733 	    sizeof(sc->my_ldata->my_rx_list));
1734 	/*
1735 	 * Free the TX list buffers.
1736 	 */
1737 	for (i = 0; i < MY_TX_LIST_CNT; i++) {
1738 		if (sc->my_cdata.my_tx_chain[i].my_mbuf != NULL) {
1739 			m_freem(sc->my_cdata.my_tx_chain[i].my_mbuf);
1740 			sc->my_cdata.my_tx_chain[i].my_mbuf = NULL;
1741 		}
1742 	}
1743 	bzero((char *)&sc->my_ldata->my_tx_list,
1744 	    sizeof(sc->my_ldata->my_tx_list));
1745 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
1746 	return;
1747 }
1748 
1749 /*
1750  * Stop all chip I/O so that the kernel's probe routines don't get confused
1751  * by errant DMAs when rebooting.
1752  */
1753 static int
1754 my_shutdown(device_t dev)
1755 {
1756 	struct my_softc *sc;
1757 
1758 	sc = device_get_softc(dev);
1759 	MY_LOCK(sc);
1760 	my_stop(sc);
1761 	MY_UNLOCK(sc);
1762 	return 0;
1763 }
1764