xref: /openbsd-src/sys/dev/usb/if_smsc.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: if_smsc.c,v 1.13 2014/07/13 15:52:49 mpi Exp $	*/
2 /* $FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
3 /*-
4  * Copyright (c) 2012
5  *	Ben Gray <bgray@freebsd.org>.
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  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * SMSC LAN9xxx devices (http://www.smsc.com/)
31  *
32  * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
33  * support USB 2.0 and 10/100 Mbps Ethernet.
34  *
35  * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
36  * The driver only covers the Ethernet part, the standard USB hub driver
37  * supports the hub part.
38  *
39  * This driver is closely modelled on the Linux driver written and copyrighted
40  * by SMSC.
41  *
42  * H/W TCP & UDP Checksum Offloading
43  * ---------------------------------
44  * The chip supports both tx and rx offloading of UDP & TCP checksums, this
45  * feature can be dynamically enabled/disabled.
46  *
47  * RX checksuming is performed across bytes after the IPv4 header to the end of
48  * the Ethernet frame, this means if the frame is padded with non-zero values
49  * the H/W checksum will be incorrect, however the rx code compensates for this.
50  *
51  * TX checksuming is more complicated, the device requires a special header to
52  * be prefixed onto the start of the frame which indicates the start and end
53  * positions of the UDP or TCP frame.  This requires the driver to manually
54  * go through the packet data and decode the headers prior to sending.
55  * On Linux they generally provide cues to the location of the csum and the
56  * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
57  * hence this is not as optimal and therefore h/w tX checksum is currently not
58  * implemented.
59  */
60 
61 #include "bpfilter.h"
62 #include "vlan.h"
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/sockio.h>
67 #include <sys/rwlock.h>
68 #include <sys/mbuf.h>
69 #include <sys/kernel.h>
70 #include <sys/socket.h>
71 
72 #include <sys/device.h>
73 
74 #include <machine/bus.h>
75 
76 #include <net/if.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
79 
80 #if NBPFILTER > 0
81 #include <net/bpf.h>
82 #endif
83 
84 #ifdef INET
85 #include <netinet/in.h>
86 #include <netinet/if_ether.h>
87 #endif
88 
89 #include <dev/mii/mii.h>
90 #include <dev/mii/miivar.h>
91 
92 #include <dev/usb/usb.h>
93 #include <dev/usb/usbdi.h>
94 #include <dev/usb/usbdi_util.h>
95 #include <dev/usb/usbdivar.h>
96 #include <dev/usb/usbdevs.h>
97 
98 #include "if_smscreg.h"
99 
100 /*
101  * Various supported device vendors/products.
102  */
103 static const struct usb_devno smsc_devs[] = {
104 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_LAN89530 },
105 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_LAN9530 },
106 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_LAN9730 },
107 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500 },
108 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500A },
109 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500A_ALT },
110 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500A_HAL },
111 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500A_SAL10 },
112 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500_ALT },
113 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9500_SAL10 },
114 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9505 },
115 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9505A },
116 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9505A_HAL },
117 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9505A_SAL10 },
118 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9505_SAL10 },
119 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9512_14 },
120 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9512_14_ALT },
121 	{ USB_VENDOR_SMC2,	USB_PRODUCT_SMC2_SMSC9512_14_SAL10 }
122 };
123 
124 #ifdef SMSC_DEBUG
125 static int smsc_debug = 0;
126 #define smsc_dbg_printf(sc, fmt, args...) \
127 	do { \
128 		if (smsc_debug > 0) \
129 			printf("debug: " fmt, ##args); \
130 	} while(0)
131 #else
132 #define smsc_dbg_printf(sc, fmt, args...)
133 #endif
134 
135 #define smsc_warn_printf(sc, fmt, args...) \
136 	printf("%s: warning: " fmt, (sc)->sc_dev.dv_xname, ##args)
137 
138 #define smsc_err_printf(sc, fmt, args...) \
139 	printf("%s: error: " fmt, (sc)->sc_dev.dv_xname, ##args)
140 
141 int		 smsc_chip_init(struct smsc_softc *sc);
142 int		 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
143 void		 smsc_iff(struct smsc_softc *);
144 int		 smsc_setmacaddress(struct smsc_softc *, const uint8_t *);
145 
146 int		 smsc_match(struct device *, void *, void *);
147 void		 smsc_attach(struct device *, struct device *, void *);
148 int		 smsc_detach(struct device *, int);
149 
150 void		 smsc_init(void *);
151 void		 smsc_stop(struct smsc_softc *);
152 void		 smsc_start(struct ifnet *);
153 void		 smsc_reset(struct smsc_softc *);
154 struct mbuf	*smsc_newbuf(void);
155 
156 void		 smsc_tick(void *);
157 void		 smsc_tick_task(void *);
158 void		 smsc_miibus_statchg(struct device *);
159 int		 smsc_miibus_readreg(struct device *, int, int);
160 void		 smsc_miibus_writereg(struct device *, int, int, int);
161 int		 smsc_ifmedia_upd(struct ifnet *);
162 void		 smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
163 void		 smsc_lock_mii(struct smsc_softc *sc);
164 void		 smsc_unlock_mii(struct smsc_softc *sc);
165 
166 int		 smsc_tx_list_init(struct smsc_softc *);
167 int		 smsc_rx_list_init(struct smsc_softc *);
168 int		 smsc_encap(struct smsc_softc *, struct mbuf *, int);
169 void		 smsc_rxeof(struct usbd_xfer *, void *, usbd_status);
170 void		 smsc_txeof(struct usbd_xfer *, void *, usbd_status);
171 
172 int		 smsc_read_reg(struct smsc_softc *, uint32_t, uint32_t *);
173 int		 smsc_write_reg(struct smsc_softc *, uint32_t, uint32_t);
174 int		 smsc_wait_for_bits(struct smsc_softc *, uint32_t, uint32_t);
175 int		 smsc_sethwcsum(struct smsc_softc *);
176 
177 struct cfdriver smsc_cd = {
178 	NULL, "smsc", DV_IFNET
179 };
180 
181 const struct cfattach smsc_ca = {
182 	sizeof(struct smsc_softc), smsc_match, smsc_attach, smsc_detach,
183 };
184 
185 int
186 smsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
187 {
188 	usb_device_request_t req;
189 	uint32_t buf;
190 	usbd_status err;
191 
192 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
193 	req.bRequest = SMSC_UR_READ_REG;
194 	USETW(req.wValue, 0);
195 	USETW(req.wIndex, off);
196 	USETW(req.wLength, 4);
197 
198 	err = usbd_do_request(sc->sc_udev, &req, &buf);
199 	if (err != 0)
200 		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
201 
202 	*data = letoh32(buf);
203 
204 	return (err);
205 }
206 
207 int
208 smsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
209 {
210 	usb_device_request_t req;
211 	uint32_t buf;
212 	usbd_status err;
213 
214 	buf = htole32(data);
215 
216 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
217 	req.bRequest = SMSC_UR_WRITE_REG;
218 	USETW(req.wValue, 0);
219 	USETW(req.wIndex, off);
220 	USETW(req.wLength, 4);
221 
222 	err = usbd_do_request(sc->sc_udev, &req, &buf);
223 	if (err != 0)
224 		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
225 
226 	return (err);
227 }
228 
229 int
230 smsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
231 {
232 	uint32_t val;
233 	int err, i;
234 
235 	for (i = 0; i < 100; i++) {
236 		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
237 			return (err);
238 		if (!(val & bits))
239 			return (0);
240 		DELAY(5);
241 	}
242 
243 	return (1);
244 }
245 
246 int
247 smsc_miibus_readreg(struct device *dev, int phy, int reg)
248 {
249 	struct smsc_softc *sc = (struct smsc_softc *)dev;
250 	uint32_t addr;
251 	uint32_t val = 0;
252 
253 	smsc_lock_mii(sc);
254 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
255 		smsc_warn_printf(sc, "MII is busy\n");
256 		goto done;
257 	}
258 
259 	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
260 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
261 
262 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
263 		smsc_warn_printf(sc, "MII read timeout\n");
264 
265 	smsc_read_reg(sc, SMSC_MII_DATA, &val);
266 	smsc_unlock_mii(sc);
267 
268 done:
269 	return (val & 0xFFFF);
270 }
271 
272 void
273 smsc_miibus_writereg(struct device *dev, int phy, int reg, int val)
274 {
275 	struct smsc_softc *sc = (struct smsc_softc *)dev;
276 	uint32_t addr;
277 
278 	if (sc->sc_phyno != phy)
279 		return;
280 
281 	smsc_lock_mii(sc);
282 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
283 		smsc_warn_printf(sc, "MII is busy\n");
284 		return;
285 	}
286 
287 	smsc_write_reg(sc, SMSC_MII_DATA, val);
288 
289 	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
290 	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
291 	smsc_unlock_mii(sc);
292 
293 	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
294 		smsc_warn_printf(sc, "MII write timeout\n");
295 }
296 
297 void
298 smsc_miibus_statchg(struct device *dev)
299 {
300 	struct smsc_softc *sc = (struct smsc_softc *)dev;
301 	struct mii_data *mii = &sc->sc_mii;
302 	struct ifnet *ifp = &sc->sc_ac.ac_if;
303 	int err;
304 	uint32_t flow;
305 	uint32_t afc_cfg;
306 
307 	if (mii == NULL || ifp == NULL ||
308 	    (ifp->if_flags & IFF_RUNNING) == 0)
309 		return;
310 
311 	/* Use the MII status to determine link status */
312 	sc->sc_flags &= ~SMSC_FLAG_LINK;
313 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
314 	    (IFM_ACTIVE | IFM_AVALID)) {
315 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
316 			case IFM_10_T:
317 			case IFM_100_TX:
318 				sc->sc_flags |= SMSC_FLAG_LINK;
319 				break;
320 			case IFM_1000_T:
321 				/* Gigabit ethernet not supported by chipset */
322 				break;
323 			default:
324 				break;
325 		}
326 	}
327 
328 	/* Lost link, do nothing. */
329 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
330 		smsc_dbg_printf(sc, "link flag not set\n");
331 		return;
332 	}
333 
334 	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
335 	if (err) {
336 		smsc_warn_printf(sc, "failed to read initial AFC_CFG, "
337 		    "error %d\n", err);
338 		return;
339 	}
340 
341 	/* Enable/disable full duplex operation and TX/RX pause */
342 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
343 		smsc_dbg_printf(sc, "full duplex operation\n");
344 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
345 		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
346 
347 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
348 			flow = 0xffff0002;
349 		else
350 			flow = 0;
351 
352 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
353 			afc_cfg |= 0xf;
354 		else
355 			afc_cfg &= ~0xf;
356 
357 	} else {
358 		smsc_dbg_printf(sc, "half duplex operation\n");
359 		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
360 		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
361 
362 		flow = 0;
363 		afc_cfg |= 0xf;
364 	}
365 
366 	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
367 	err += smsc_write_reg(sc, SMSC_FLOW, flow);
368 	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
369 	if (err)
370 		smsc_warn_printf(sc, "media change failed, error %d\n", err);
371 }
372 
373 int
374 smsc_ifmedia_upd(struct ifnet *ifp)
375 {
376 	struct smsc_softc *sc = ifp->if_softc;
377 	struct mii_data *mii = &sc->sc_mii;
378 	int err;
379 
380 	if (mii->mii_instance) {
381 		struct mii_softc *miisc;
382 
383 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
384 			mii_phy_reset(miisc);
385 	}
386 	err = mii_mediachg(mii);
387 	return (err);
388 }
389 
390 void
391 smsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
392 {
393 	struct smsc_softc *sc = ifp->if_softc;
394 	struct mii_data *mii = &sc->sc_mii;
395 
396 	mii_pollstat(mii);
397 
398 	ifmr->ifm_active = mii->mii_media_active;
399 	ifmr->ifm_status = mii->mii_media_status;
400 }
401 
402 static inline uint32_t
403 smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
404 {
405 	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
406 }
407 
408 void
409 smsc_iff(struct smsc_softc *sc)
410 {
411 	struct ifnet		*ifp = &sc->sc_ac.ac_if;
412 	struct arpcom		*ac = &sc->sc_ac;
413 	struct ether_multi	*enm;
414 	struct ether_multistep	 step;
415 	uint32_t		 hashtbl[2] = { 0, 0 };
416 	uint32_t		 hash;
417 
418 	if (usbd_is_dying(sc->sc_udev))
419 		return;
420 
421 	sc->sc_mac_csr &= ~(SMSC_MAC_CSR_HPFILT | SMSC_MAC_CSR_MCPAS |
422 	    SMSC_MAC_CSR_PRMS);
423 	ifp->if_flags &= ~IFF_ALLMULTI;
424 
425 	if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
426 		ifp->if_flags |= IFF_ALLMULTI;
427 		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
428 		if (ifp->if_flags & IFF_PROMISC)
429 			sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
430 	} else {
431 		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
432 
433 		ETHER_FIRST_MULTI(step, ac, enm);
434 		while (enm != NULL) {
435 			hash = smsc_hash(enm->enm_addrlo);
436 
437 			hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
438 
439 			ETHER_NEXT_MULTI(step, enm);
440 		}
441 	}
442 
443 	/* Debug */
444 	if (sc->sc_mac_csr & SMSC_MAC_CSR_MCPAS)
445 		smsc_dbg_printf(sc, "receive all multicast enabled\n");
446 	else if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
447 		smsc_dbg_printf(sc, "receive select group of macs\n");
448 
449 	/* Write the hash table and mac control registers */
450 	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
451 	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
452 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
453 }
454 
455 int
456 smsc_sethwcsum(struct smsc_softc *sc)
457 {
458 	struct ifnet *ifp = &sc->sc_ac.ac_if;
459 	uint32_t val;
460 	int err;
461 
462 	if (!ifp)
463 		return (-EIO);
464 
465 	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
466 	if (err != 0) {
467 		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n",
468 		    err);
469 		return (err);
470 	}
471 
472 	/* Enable/disable the Rx checksum */
473 	if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
474 		val |= SMSC_COE_CTRL_RX_EN;
475 	else
476 		val &= ~SMSC_COE_CTRL_RX_EN;
477 
478 	/* Enable/disable the Tx checksum (currently not supported) */
479 	if (ifp->if_capabilities & IFCAP_CSUM_IPv4)
480 		val |= SMSC_COE_CTRL_TX_EN;
481 	else
482 		val &= ~SMSC_COE_CTRL_TX_EN;
483 
484 	err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
485 	if (err != 0) {
486 		smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n",
487 		    err);
488 		return (err);
489 	}
490 
491 	return (0);
492 }
493 
494 int
495 smsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
496 {
497 	int err;
498 	uint32_t val;
499 
500 	smsc_dbg_printf(sc, "setting mac address to "
501 	    "%02x:%02x:%02x:%02x:%02x:%02x\n",
502 	    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
503 
504 	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
505 	if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
506 		goto done;
507 
508 	val = (addr[5] << 8) | addr[4];
509 	err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
510 
511 done:
512 	return (err);
513 }
514 
515 void
516 smsc_reset(struct smsc_softc *sc)
517 {
518 	if (usbd_is_dying(sc->sc_udev))
519 		return;
520 
521 	/* Wait a little while for the chip to get its brains in order. */
522 	DELAY(1000);
523 
524 	/* Reinitialize controller to achieve full reset. */
525 	smsc_chip_init(sc);
526 }
527 
528 void
529 smsc_init(void *xsc)
530 {
531 	struct smsc_softc	*sc = xsc;
532 	struct ifnet		*ifp = &sc->sc_ac.ac_if;
533 	struct smsc_chain	*c;
534 	usbd_status		 err;
535 	int			 s, i;
536 
537 	s = splnet();
538 
539 	/* Cancel pending I/O */
540 	smsc_stop(sc);
541 
542 	/* Reset the ethernet interface. */
543 	smsc_reset(sc);
544 
545 	/* Init RX ring. */
546 	if (smsc_rx_list_init(sc) == ENOBUFS) {
547 		printf("%s: rx list init failed\n", sc->sc_dev.dv_xname);
548 		splx(s);
549 		return;
550 	}
551 
552 	/* Init TX ring. */
553 	if (smsc_tx_list_init(sc) == ENOBUFS) {
554 		printf("%s: tx list init failed\n", sc->sc_dev.dv_xname);
555 		splx(s);
556 		return;
557 	}
558 
559 	/* Program promiscuous mode and multicast filters. */
560 	smsc_iff(sc);
561 
562 	/* Open RX and TX pipes. */
563 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_RX],
564 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_RX]);
565 	if (err) {
566 		printf("%s: open rx pipe failed: %s\n",
567 		    sc->sc_dev.dv_xname, usbd_errstr(err));
568 		splx(s);
569 		return;
570 	}
571 
572 	err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[SMSC_ENDPT_TX],
573 	    USBD_EXCLUSIVE_USE, &sc->sc_ep[SMSC_ENDPT_TX]);
574 	if (err) {
575 		printf("%s: open tx pipe failed: %s\n",
576 		    sc->sc_dev.dv_xname, usbd_errstr(err));
577 		splx(s);
578 		return;
579 	}
580 
581 	/* Start up the receive pipe. */
582 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
583 		c = &sc->sc_cdata.rx_chain[i];
584 		usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_RX],
585 		    c, c->sc_buf, sc->sc_bufsz,
586 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
587 		    USBD_NO_TIMEOUT, smsc_rxeof);
588 		usbd_transfer(c->sc_xfer);
589 	}
590 
591 	/* TCP/UDP checksum offload engines. */
592 	smsc_sethwcsum(sc);
593 
594 	/* Indicate we are up and running. */
595 	ifp->if_flags |= IFF_RUNNING;
596 	ifp->if_flags &= ~IFF_OACTIVE;
597 
598 	timeout_add_sec(&sc->sc_stat_ch, 1);
599 
600 	splx(s);
601 }
602 
603 void
604 smsc_start(struct ifnet *ifp)
605 {
606 	struct smsc_softc	*sc = ifp->if_softc;
607 	struct mbuf		*m_head = NULL;
608 
609 	/* Don't send anything if there is no link or controller is busy. */
610 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
611 		(ifp->if_flags & IFF_OACTIVE) != 0) {
612 		return;
613 	}
614 
615 	IFQ_POLL(&ifp->if_snd, m_head);
616 	if (m_head == NULL)
617 		return;
618 
619 	if (smsc_encap(sc, m_head, 0)) {
620 		ifp->if_flags |= IFF_OACTIVE;
621 		return;
622 	}
623 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
624 
625 #if NBPFILTER > 0
626 	if (ifp->if_bpf)
627 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
628 #endif
629 	ifp->if_flags |= IFF_OACTIVE;
630 }
631 
632 void
633 smsc_tick(void *xsc)
634 {
635 	struct smsc_softc *sc = xsc;
636 
637 	if (sc == NULL)
638 		return;
639 
640 	if (usbd_is_dying(sc->sc_udev))
641 		return;
642 
643 	usb_add_task(sc->sc_udev, &sc->sc_tick_task);
644 }
645 
646 void
647 smsc_stop(struct smsc_softc *sc)
648 {
649 	usbd_status		err;
650 	struct ifnet		*ifp;
651 	int			i;
652 
653 	smsc_reset(sc);
654 
655 	ifp = &sc->sc_ac.ac_if;
656 	ifp->if_timer = 0;
657 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
658 
659 	timeout_del(&sc->sc_stat_ch);
660 
661 	/* Stop transfers. */
662 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL) {
663 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
664 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
665 		if (err) {
666 			printf("%s: close rx pipe failed: %s\n",
667 			    sc->sc_dev.dv_xname, usbd_errstr(err));
668 		}
669 		sc->sc_ep[SMSC_ENDPT_RX] = NULL;
670 	}
671 
672 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL) {
673 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
674 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
675 		if (err) {
676 			printf("%s: close tx pipe failed: %s\n",
677 			    sc->sc_dev.dv_xname, usbd_errstr(err));
678 		}
679 		sc->sc_ep[SMSC_ENDPT_TX] = NULL;
680 	}
681 
682 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL) {
683 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
684 		err = usbd_close_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
685 		if (err) {
686 			printf("%s: close intr pipe failed: %s\n",
687 			    sc->sc_dev.dv_xname, usbd_errstr(err));
688 		}
689 		sc->sc_ep[SMSC_ENDPT_INTR] = NULL;
690 	}
691 
692 	/* Free RX resources. */
693 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
694 		if (sc->sc_cdata.rx_chain[i].sc_mbuf != NULL) {
695 			m_freem(sc->sc_cdata.rx_chain[i].sc_mbuf);
696 			sc->sc_cdata.rx_chain[i].sc_mbuf = NULL;
697 		}
698 		if (sc->sc_cdata.rx_chain[i].sc_xfer != NULL) {
699 			usbd_free_xfer(sc->sc_cdata.rx_chain[i].sc_xfer);
700 			sc->sc_cdata.rx_chain[i].sc_xfer = NULL;
701 		}
702 	}
703 
704 	/* Free TX resources. */
705 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
706 		if (sc->sc_cdata.tx_chain[i].sc_mbuf != NULL) {
707 			m_freem(sc->sc_cdata.tx_chain[i].sc_mbuf);
708 			sc->sc_cdata.tx_chain[i].sc_mbuf = NULL;
709 		}
710 		if (sc->sc_cdata.tx_chain[i].sc_xfer != NULL) {
711 			usbd_free_xfer(sc->sc_cdata.tx_chain[i].sc_xfer);
712 			sc->sc_cdata.tx_chain[i].sc_xfer = NULL;
713 		}
714 	}
715 }
716 
717 int
718 smsc_chip_init(struct smsc_softc *sc)
719 {
720 	int err;
721 	uint32_t reg_val;
722 	int burst_cap;
723 
724 	/* Enter H/W config mode */
725 	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
726 
727 	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG,
728 	    SMSC_HW_CFG_LRST)) != 0) {
729 		smsc_warn_printf(sc, "timed-out waiting for reset to "
730 		    "complete\n");
731 		goto init_failed;
732 	}
733 
734 	/* Reset the PHY */
735 	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
736 
737 	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL,
738 	    SMSC_PM_CTRL_PHY_RST) != 0)) {
739 		smsc_warn_printf(sc, "timed-out waiting for phy reset to "
740 		    "complete\n");
741 		goto init_failed;
742 	}
743 	usbd_delay_ms(sc->sc_udev, 40);
744 
745 	/* Set the mac address */
746 	if ((err = smsc_setmacaddress(sc, sc->sc_ac.ac_enaddr)) != 0) {
747 		smsc_warn_printf(sc, "failed to set the MAC address\n");
748 		goto init_failed;
749 	}
750 
751 	/*
752 	 * Don't know what the HW_CFG_BIR bit is, but following the reset
753 	 * sequence as used in the Linux driver.
754 	 */
755 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
756 		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
757 		goto init_failed;
758 	}
759 	reg_val |= SMSC_HW_CFG_BIR;
760 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
761 
762 	/*
763 	 * There is a so called 'turbo mode' that the linux driver supports, it
764 	 * seems to allow you to jam multiple frames per Rx transaction.
765 	 * By default this driver supports that and therefore allows multiple
766 	 * frames per URB.
767 	 *
768 	 * The xfer buffer size needs to reflect this as well, therefore based
769 	 * on the calculations in the Linux driver the RX bufsize is set to
770 	 * 18944,
771 	 *     bufsz = (16 * 1024 + 5 * 512)
772 	 *
773 	 * Burst capability is the number of URBs that can be in a burst of
774 	 * data/ethernet frames.
775 	 */
776 #ifdef SMSC_TURBO
777 	if (sc->sc_udev->speed == USB_SPEED_HIGH)
778 		burst_cap = 37;
779 	else
780 		burst_cap = 128;
781 #else
782 	burst_cap = 0;
783 #endif
784 
785 	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
786 
787 	/* Set the default bulk in delay (magic value from Linux driver) */
788 	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
789 
790 
791 
792 	/*
793 	 * Initialise the RX interface
794 	 */
795 	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
796 		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n",
797 		    err);
798 		goto init_failed;
799 	}
800 
801 	/*
802 	 * The following setings are used for 'turbo mode', a.k.a multiple
803 	 * frames per Rx transaction (again info taken form Linux driver).
804 	 */
805 #ifdef SMSC_TURBO
806 	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
807 #endif
808 
809 	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
810 
811 	/* Clear the status register ? */
812 	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
813 
814 	/* Read and display the revision register */
815 	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
816 		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
817 		goto init_failed;
818 	}
819 
820 	/* GPIO/LED setup */
821 	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
822 	          SMSC_LED_GPIO_CFG_FDX_LED;
823 	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
824 
825 	/*
826 	 * Initialise the TX interface
827 	 */
828 	smsc_write_reg(sc, SMSC_FLOW, 0);
829 
830 	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
831 
832 	/* Read the current MAC configuration */
833 	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
834 		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
835 		goto init_failed;
836 	}
837 
838 	/* Vlan */
839 	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
840 
841 	/*
842 	 * Start TX
843 	 */
844 	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
845 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
846 	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
847 
848 	/*
849 	 * Start RX
850 	 */
851 	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
852 	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
853 
854 	return (0);
855 
856 init_failed:
857 	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
858 	return (err);
859 }
860 
861 int
862 smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
863 {
864 	struct smsc_softc	*sc = ifp->if_softc;
865 	struct ifreq		*ifr = (struct ifreq *)data;
866 	struct ifaddr		*ifa = (struct ifaddr *)data;
867 	int			s, error = 0;
868 
869 	s = splnet();
870 
871 	switch(cmd) {
872 	case SIOCSIFADDR:
873 		ifp->if_flags |= IFF_UP;
874 		if (!(ifp->if_flags & IFF_RUNNING))
875 			smsc_init(sc);
876 #ifdef INET
877 		if (ifa->ifa_addr->sa_family == AF_INET)
878 			arp_ifinit(&sc->sc_ac, ifa);
879 #endif
880 		break;
881 
882 	case SIOCSIFFLAGS:
883 		if (ifp->if_flags & IFF_UP) {
884 			if (ifp->if_flags & IFF_RUNNING)
885 				error = ENETRESET;
886 			else
887 				smsc_init(sc);
888 		} else {
889 			if (ifp->if_flags & IFF_RUNNING)
890 				smsc_stop(sc);
891 		}
892 		break;
893 
894 	case SIOCGIFMEDIA:
895 	case SIOCSIFMEDIA:
896 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
897 		break;
898 
899 	default:
900 		error = ether_ioctl(ifp, &sc->sc_ac, cmd, data);
901 	}
902 
903 	if (error == ENETRESET) {
904 		if (ifp->if_flags & IFF_RUNNING)
905 			smsc_iff(sc);
906 		error = 0;
907 	}
908 
909 	splx(s);
910 	return(error);
911 }
912 
913 int
914 smsc_match(struct device *parent, void *match, void *aux)
915 {
916 	struct usb_attach_arg *uaa = aux;
917 
918 	if (uaa->iface != NULL)
919 		return UMATCH_NONE;
920 
921 	return (usb_lookup(smsc_devs, uaa->vendor, uaa->product) != NULL) ?
922 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
923 }
924 
925 void
926 smsc_attach(struct device *parent, struct device *self, void *aux)
927 {
928 	struct smsc_softc *sc = (struct smsc_softc *)self;
929 	struct usb_attach_arg *uaa = aux;
930 	struct usbd_device *dev = uaa->device;
931 	usb_interface_descriptor_t *id;
932 	usb_endpoint_descriptor_t *ed;
933 	struct mii_data *mii;
934 	struct ifnet *ifp;
935 	int err, s, i;
936 	uint32_t mac_h, mac_l;
937 
938 	sc->sc_udev = dev;
939 
940 	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
941 
942 	/* Setup the endpoints for the SMSC LAN95xx device(s) */
943 	usb_init_task(&sc->sc_tick_task, smsc_tick_task, sc,
944 	    USB_TASK_TYPE_GENERIC);
945 	rw_init(&sc->sc_mii_lock, "smscmii");
946 	usb_init_task(&sc->sc_stop_task, (void (*)(void *))smsc_stop, sc,
947 	    USB_TASK_TYPE_GENERIC);
948 
949 	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &sc->sc_iface);
950 	if (err) {
951 		printf("%s: getting interface handle failed\n",
952 		    sc->sc_dev.dv_xname);
953 		return;
954 	}
955 
956 	id = usbd_get_interface_descriptor(sc->sc_iface);
957 
958 	if (sc->sc_udev->speed >= USB_SPEED_HIGH)
959 		sc->sc_bufsz = SMSC_MAX_BUFSZ;
960 	else
961 		sc->sc_bufsz = SMSC_MIN_BUFSZ;
962 
963 	/* Find endpoints. */
964 	for (i = 0; i < id->bNumEndpoints; i++) {
965 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
966 		if (!ed) {
967 			printf("%s: couldn't get ep %d\n",
968 			    sc->sc_dev.dv_xname, i);
969 			return;
970 		}
971 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
972 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
973 			sc->sc_ed[SMSC_ENDPT_RX] = ed->bEndpointAddress;
974 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
975 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
976 			sc->sc_ed[SMSC_ENDPT_TX] = ed->bEndpointAddress;
977 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
978 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
979 			sc->sc_ed[SMSC_ENDPT_INTR] = ed->bEndpointAddress;
980 		}
981 	}
982 
983 	s = splnet();
984 
985 	ifp = &sc->sc_ac.ac_if;
986 	ifp->if_softc = sc;
987 	strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
988 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
989 	ifp->if_ioctl = smsc_ioctl;
990 	ifp->if_start = smsc_start;
991 	ifp->if_capabilities = IFCAP_VLAN_MTU;
992 
993 	/* Setup some of the basics */
994 	sc->sc_phyno = 1;
995 
996 	/*
997 	 * Attempt to get the mac address, if an EEPROM is not attached this
998 	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
999 	 * address based on urandom.
1000 	 */
1001 	memset(sc->sc_ac.ac_enaddr, 0xff, ETHER_ADDR_LEN);
1002 
1003 	/* Check if there is already a MAC address in the register */
1004 	if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1005 	    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1006 		sc->sc_ac.ac_enaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1007 		sc->sc_ac.ac_enaddr[4] = (uint8_t)((mac_h) & 0xff);
1008 		sc->sc_ac.ac_enaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1009 		sc->sc_ac.ac_enaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1010 		sc->sc_ac.ac_enaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1011 		sc->sc_ac.ac_enaddr[0] = (uint8_t)((mac_l) & 0xff);
1012 	}
1013 
1014 	printf("%s: address %s\n", sc->sc_dev.dv_xname,
1015 	    ether_sprintf(sc->sc_ac.ac_enaddr));
1016 
1017 	/* Initialise the chip for the first time */
1018 	smsc_chip_init(sc);
1019 
1020 	IFQ_SET_READY(&ifp->if_snd);
1021 
1022 	/* Initialize MII/media info. */
1023 	mii = &sc->sc_mii;
1024 	mii->mii_ifp = ifp;
1025 	mii->mii_readreg = smsc_miibus_readreg;
1026 	mii->mii_writereg = smsc_miibus_writereg;
1027 	mii->mii_statchg = smsc_miibus_statchg;
1028 	mii->mii_flags = MIIF_AUTOTSLEEP;
1029 
1030 	ifmedia_init(&mii->mii_media, 0, smsc_ifmedia_upd, smsc_ifmedia_sts);
1031 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
1032 
1033 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
1034 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1035 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1036 	} else
1037 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1038 
1039 	if_attach(ifp);
1040 	ether_ifattach(ifp);
1041 
1042 	timeout_set(&sc->sc_stat_ch, smsc_tick, sc);
1043 
1044 	splx(s);
1045 }
1046 
1047 int
1048 smsc_detach(struct device *self, int flags)
1049 {
1050 	struct smsc_softc *sc = (struct smsc_softc *)self;
1051 	struct ifnet *ifp = &sc->sc_ac.ac_if;
1052 	int s;
1053 
1054 	if (timeout_initialized(&sc->sc_stat_ch))
1055 		timeout_del(&sc->sc_stat_ch);
1056 
1057 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL)
1058 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_TX]);
1059 	if (sc->sc_ep[SMSC_ENDPT_RX] != NULL)
1060 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_RX]);
1061 	if (sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1062 		usbd_abort_pipe(sc->sc_ep[SMSC_ENDPT_INTR]);
1063 
1064 	/*
1065 	 * Remove any pending tasks.  They cannot be executing because they run
1066 	 * in the same thread as detach.
1067 	 */
1068 	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
1069 	usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
1070 
1071 	s = splusb();
1072 
1073 	if (--sc->sc_refcnt >= 0) {
1074 		/* Wait for processes to go away */
1075 		usb_detach_wait(&sc->sc_dev);
1076 	}
1077 
1078 	if (ifp->if_flags & IFF_RUNNING)
1079 		smsc_stop(sc);
1080 
1081 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1082 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1083 	if (ifp->if_softc != NULL) {
1084 		ether_ifdetach(ifp);
1085 		if_detach(ifp);
1086 	}
1087 
1088 #ifdef DIAGNOSTIC
1089 	if (sc->sc_ep[SMSC_ENDPT_TX] != NULL ||
1090 	    sc->sc_ep[SMSC_ENDPT_RX] != NULL ||
1091 	    sc->sc_ep[SMSC_ENDPT_INTR] != NULL)
1092 		printf("%s: detach has active endpoints\n",
1093 		    sc->sc_dev.dv_xname);
1094 #endif
1095 
1096 	if (--sc->sc_refcnt >= 0) {
1097 		/* Wait for processes to go away. */
1098 		usb_detach_wait(&sc->sc_dev);
1099 	}
1100 	splx(s);
1101 
1102 	return (0);
1103 }
1104 
1105 void
1106 smsc_tick_task(void *xsc)
1107 {
1108 	int			 s;
1109 	struct smsc_softc	*sc = xsc;
1110 	struct ifnet		*ifp;
1111 	struct mii_data		*mii;
1112 
1113 	if (sc == NULL)
1114 		return;
1115 
1116 	if (usbd_is_dying(sc->sc_udev))
1117 		return;
1118 	ifp = &sc->sc_ac.ac_if;
1119 	mii = &sc->sc_mii;
1120 	if (mii == NULL)
1121 		return;
1122 
1123 	s = splnet();
1124 
1125 	mii_tick(mii);
1126 	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0)
1127 		smsc_miibus_statchg(&sc->sc_dev);
1128 	timeout_add_sec(&sc->sc_stat_ch, 1);
1129 
1130 	splx(s);
1131 }
1132 
1133 void
1134 smsc_lock_mii(struct smsc_softc *sc)
1135 {
1136 	sc->sc_refcnt++;
1137 	rw_enter_write(&sc->sc_mii_lock);
1138 }
1139 
1140 void
1141 smsc_unlock_mii(struct smsc_softc *sc)
1142 {
1143 	rw_exit_write(&sc->sc_mii_lock);
1144 	if (--sc->sc_refcnt < 0)
1145 		usb_detach_wakeup(&sc->sc_dev);
1146 }
1147 
1148 void
1149 smsc_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1150 {
1151 	struct smsc_chain	*c = (struct smsc_chain *)priv;
1152 	struct smsc_softc	*sc = c->sc_sc;
1153 	struct ifnet		*ifp = &sc->sc_ac.ac_if;
1154 	u_char			*buf = c->sc_buf;
1155 	uint32_t		total_len;
1156 	uint16_t		pktlen = 0;
1157 	struct mbuf		*m;
1158 	int			s;
1159 	uint32_t		rxhdr;
1160 
1161 	if (usbd_is_dying(sc->sc_udev))
1162 		return;
1163 
1164 	if (!(ifp->if_flags & IFF_RUNNING))
1165 		return;
1166 
1167 	if (status != USBD_NORMAL_COMPLETION) {
1168 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1169 			return;
1170 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
1171 			printf("%s: usb errors on rx: %s\n",
1172 			    sc->sc_dev.dv_xname, usbd_errstr(status));
1173 		}
1174 		if (status == USBD_STALLED)
1175 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_RX]);
1176 		goto done;
1177 	}
1178 
1179 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1180 	smsc_dbg_printf(sc, "xfer status total_len %d\n", total_len);
1181 
1182 	do {
1183 		if (total_len < sizeof(rxhdr)) {
1184 			smsc_dbg_printf(sc, "total_len %d < sizeof(rxhdr) %d\n",
1185 			    total_len, sizeof(rxhdr));
1186 			ifp->if_ierrors++;
1187 			goto done;
1188 		}
1189 
1190 		buf += pktlen;
1191 
1192 		memcpy(&rxhdr, buf, sizeof(rxhdr));
1193 		rxhdr = letoh32(rxhdr);
1194 		total_len -= sizeof(rxhdr);
1195 
1196 		if (rxhdr & SMSC_RX_STAT_ERROR) {
1197 			smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1198 			ifp->if_ierrors++;
1199 			goto done;
1200 		}
1201 
1202 		pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1203 		smsc_dbg_printf(sc, "rxeof total_len %d pktlen %d rxhdr "
1204 		    "0x%08x\n", total_len, pktlen, rxhdr);
1205 		if (pktlen > total_len) {
1206 			smsc_dbg_printf(sc, "pktlen %d > total_len %d\n",
1207 			    pktlen, total_len);
1208 			ifp->if_ierrors++;
1209 			goto done;
1210 		}
1211 
1212 		buf += sizeof(rxhdr);
1213 
1214 		if (total_len < pktlen)
1215 			total_len = 0;
1216 		else
1217 			total_len -= pktlen;
1218 
1219 		m = smsc_newbuf();
1220 		if (m == NULL) {
1221 			smsc_dbg_printf(sc, "smc_newbuf returned NULL\n");
1222 			ifp->if_ierrors++;
1223 			goto done;
1224 		}
1225 
1226 		ifp->if_ipackets++;
1227 		m->m_pkthdr.rcvif = ifp;
1228 		m->m_pkthdr.len = m->m_len = pktlen;
1229 		m_adj(m, ETHER_ALIGN);
1230 
1231 		memcpy(mtod(m, char *), buf, pktlen);
1232 
1233 		/* push the packet up */
1234 		s = splnet();
1235 #if NBPFILTER > 0
1236 		if (ifp->if_bpf)
1237 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1238 #endif
1239 		ether_input_mbuf(ifp, m);
1240 
1241 		splx(s);
1242 	} while (total_len > 0);
1243 
1244 done:
1245 	memset(c->sc_buf, 0, sc->sc_bufsz);
1246 
1247 	/* Setup new transfer. */
1248 	usbd_setup_xfer(xfer, sc->sc_ep[SMSC_ENDPT_RX],
1249 	    c, c->sc_buf, sc->sc_bufsz,
1250 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1251 	    USBD_NO_TIMEOUT, smsc_rxeof);
1252 	usbd_transfer(xfer);
1253 
1254 	return;
1255 }
1256 
1257 void
1258 smsc_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1259 {
1260 	struct smsc_softc	*sc;
1261 	struct smsc_chain	*c;
1262 	struct ifnet		*ifp;
1263 	int			s;
1264 
1265 	c = priv;
1266 	sc = c->sc_sc;
1267 	ifp = &sc->sc_ac.ac_if;
1268 
1269 	if (usbd_is_dying(sc->sc_udev))
1270 		return;
1271 
1272 	s = splnet();
1273 
1274 	if (status != USBD_NORMAL_COMPLETION) {
1275 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1276 			splx(s);
1277 			return;
1278 		}
1279 		ifp->if_oerrors++;
1280 		printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname,
1281 		    usbd_errstr(status));
1282 		if (status == USBD_STALLED)
1283 			usbd_clear_endpoint_stall_async(sc->sc_ep[SMSC_ENDPT_TX]);
1284 		splx(s);
1285 		return;
1286 	}
1287 
1288 	ifp->if_timer = 0;
1289 	ifp->if_flags &= ~IFF_OACTIVE;
1290 
1291 	m_freem(c->sc_mbuf);
1292 	c->sc_mbuf = NULL;
1293 
1294 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1295 		smsc_start(ifp);
1296 
1297 	ifp->if_opackets++;
1298 	splx(s);
1299 }
1300 
1301 int
1302 smsc_tx_list_init(struct smsc_softc *sc)
1303 {
1304 	struct smsc_cdata *cd;
1305 	struct smsc_chain *c;
1306 	int i;
1307 
1308 	cd = &sc->sc_cdata;
1309 	for (i = 0; i < SMSC_TX_LIST_CNT; i++) {
1310 		c = &cd->tx_chain[i];
1311 		c->sc_sc = sc;
1312 		c->sc_idx = i;
1313 		c->sc_mbuf = NULL;
1314 		if (c->sc_xfer == NULL) {
1315 			c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1316 			if (c->sc_xfer == NULL)
1317 				return (ENOBUFS);
1318 			c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1319 			    sc->sc_bufsz);
1320 			if (c->sc_buf == NULL) {
1321 				usbd_free_xfer(c->sc_xfer);
1322 				return (ENOBUFS);
1323 			}
1324 		}
1325 	}
1326 
1327 	return (0);
1328 }
1329 
1330 int
1331 smsc_rx_list_init(struct smsc_softc *sc)
1332 {
1333 	struct smsc_cdata *cd;
1334 	struct smsc_chain *c;
1335 	int i;
1336 
1337 	cd = &sc->sc_cdata;
1338 	for (i = 0; i < SMSC_RX_LIST_CNT; i++) {
1339 		c = &cd->rx_chain[i];
1340 		c->sc_sc = sc;
1341 		c->sc_idx = i;
1342 		c->sc_mbuf = NULL;
1343 		if (c->sc_xfer == NULL) {
1344 			c->sc_xfer = usbd_alloc_xfer(sc->sc_udev);
1345 			if (c->sc_xfer == NULL)
1346 				return (ENOBUFS);
1347 			c->sc_buf = usbd_alloc_buffer(c->sc_xfer,
1348 			    sc->sc_bufsz);
1349 			if (c->sc_buf == NULL) {
1350 				usbd_free_xfer(c->sc_xfer);
1351 				return (ENOBUFS);
1352 			}
1353 		}
1354 	}
1355 
1356 	return (0);
1357 }
1358 
1359 struct mbuf *
1360 smsc_newbuf(void)
1361 {
1362 	struct mbuf	*m;
1363 
1364 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1365 	if (m == NULL)
1366 		return (NULL);
1367 
1368 	MCLGET(m, M_DONTWAIT);
1369 	if (!(m->m_flags & M_EXT)) {
1370 		m_freem(m);
1371 		return (NULL);
1372 	}
1373 
1374 	return (m);
1375 }
1376 
1377 int
1378 smsc_encap(struct smsc_softc *sc, struct mbuf *m, int idx)
1379 {
1380 	struct smsc_chain	*c;
1381 	usbd_status		 err;
1382 	uint32_t		 txhdr;
1383 	uint32_t		 frm_len = 0;
1384 
1385 	c = &sc->sc_cdata.tx_chain[idx];
1386 
1387 	/*
1388 	 * Each frame is prefixed with two 32-bit values describing the
1389 	 * length of the packet and buffer.
1390 	 */
1391 	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1392 			SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1393 	txhdr = htole32(txhdr);
1394 	memcpy(c->sc_buf, &txhdr, sizeof(txhdr));
1395 
1396 	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1397 	txhdr = htole32(txhdr);
1398 	memcpy(c->sc_buf + 4, &txhdr, sizeof(txhdr));
1399 
1400 	frm_len += 8;
1401 
1402 	/* Next copy in the actual packet */
1403 	m_copydata(m, 0, m->m_pkthdr.len, c->sc_buf + frm_len);
1404 	frm_len += m->m_pkthdr.len;
1405 
1406 	c->sc_mbuf = m;
1407 
1408 	usbd_setup_xfer(c->sc_xfer, sc->sc_ep[SMSC_ENDPT_TX],
1409 	    c, c->sc_buf, frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1410 	    10000, smsc_txeof);
1411 
1412 	err = usbd_transfer(c->sc_xfer);
1413 	if (err != USBD_IN_PROGRESS) {
1414 		smsc_stop(sc);
1415 		return (EIO);
1416 	}
1417 
1418 	sc->sc_cdata.tx_cnt++;
1419 
1420 	return (0);
1421 }
1422