if_smsc.c revision 271832
1239275Sgonzo/*-
2239275Sgonzo * Copyright (c) 2012
3239275Sgonzo *	Ben Gray <bgray@freebsd.org>.
4239275Sgonzo * All rights reserved.
5239275Sgonzo *
6239275Sgonzo * Redistribution and use in source and binary forms, with or without
7239275Sgonzo * modification, are permitted provided that the following conditions
8239275Sgonzo * are met:
9239275Sgonzo * 1. Redistributions of source code must retain the above copyright
10239275Sgonzo *    notice, this list of conditions and the following disclaimer.
11239275Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12239275Sgonzo *    notice, this list of conditions and the following disclaimer in the
13239275Sgonzo *    documentation and/or other materials provided with the distribution.
14239275Sgonzo *
15239275Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16239275Sgonzo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17239275Sgonzo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18239275Sgonzo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19239275Sgonzo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20239275Sgonzo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21239275Sgonzo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22239275Sgonzo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23239275Sgonzo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24239275Sgonzo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25239275Sgonzo */
26239275Sgonzo
27239275Sgonzo#include <sys/cdefs.h>
28239275Sgonzo__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_smsc.c 271832 2014-09-18 21:09:22Z glebius $");
29239275Sgonzo
30239275Sgonzo/*
31239275Sgonzo * SMSC LAN9xxx devices (http://www.smsc.com/)
32239275Sgonzo *
33239275Sgonzo * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
34239275Sgonzo * support USB 2.0 and 10/100 Mbps Ethernet.
35239275Sgonzo *
36239275Sgonzo * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
37239275Sgonzo * The driver only covers the Ethernet part, the standard USB hub driver
38239275Sgonzo * supports the hub part.
39239275Sgonzo *
40239275Sgonzo * This driver is closely modelled on the Linux driver written and copyrighted
41239275Sgonzo * by SMSC.
42239275Sgonzo *
43239275Sgonzo *
44239275Sgonzo *
45239275Sgonzo *
46239275Sgonzo * H/W TCP & UDP Checksum Offloading
47239275Sgonzo * ---------------------------------
48239275Sgonzo * The chip supports both tx and rx offloading of UDP & TCP checksums, this
49239275Sgonzo * feature can be dynamically enabled/disabled.
50239275Sgonzo *
51239275Sgonzo * RX checksuming is performed across bytes after the IPv4 header to the end of
52239275Sgonzo * the Ethernet frame, this means if the frame is padded with non-zero values
53239275Sgonzo * the H/W checksum will be incorrect, however the rx code compensates for this.
54239275Sgonzo *
55239275Sgonzo * TX checksuming is more complicated, the device requires a special header to
56239275Sgonzo * be prefixed onto the start of the frame which indicates the start and end
57239275Sgonzo * positions of the UDP or TCP frame.  This requires the driver to manually
58239275Sgonzo * go through the packet data and decode the headers prior to sending.
59239275Sgonzo * On Linux they generally provide cues to the location of the csum and the
60239275Sgonzo * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
61239275Sgonzo * hence this is not as optimal and therefore h/w tX checksum is currently not
62239275Sgonzo * implemented.
63239275Sgonzo *
64239275Sgonzo */
65239275Sgonzo#include <sys/stdint.h>
66239275Sgonzo#include <sys/stddef.h>
67239275Sgonzo#include <sys/param.h>
68239275Sgonzo#include <sys/queue.h>
69239275Sgonzo#include <sys/types.h>
70239275Sgonzo#include <sys/systm.h>
71239275Sgonzo#include <sys/kernel.h>
72239275Sgonzo#include <sys/bus.h>
73239275Sgonzo#include <sys/module.h>
74239275Sgonzo#include <sys/lock.h>
75239275Sgonzo#include <sys/mutex.h>
76239275Sgonzo#include <sys/condvar.h>
77257241Sglebius#include <sys/socket.h>
78239275Sgonzo#include <sys/sysctl.h>
79239275Sgonzo#include <sys/sx.h>
80239275Sgonzo#include <sys/unistd.h>
81239275Sgonzo#include <sys/callout.h>
82239275Sgonzo#include <sys/malloc.h>
83239275Sgonzo#include <sys/priv.h>
84239275Sgonzo#include <sys/random.h>
85239275Sgonzo
86257241Sglebius#include <net/if.h>
87257241Sglebius#include <net/if_var.h>
88257241Sglebius
89265371Stuexen#include <netinet/in.h>
90265371Stuexen#include <netinet/ip.h>
91265371Stuexen
92243421Sgonzo#include "opt_platform.h"
93243421Sgonzo
94243421Sgonzo#ifdef FDT
95243421Sgonzo#include <dev/fdt/fdt_common.h>
96243421Sgonzo#include <dev/ofw/ofw_bus.h>
97243421Sgonzo#include <dev/ofw/ofw_bus_subr.h>
98243421Sgonzo#endif
99243421Sgonzo
100239275Sgonzo#include <dev/usb/usb.h>
101239275Sgonzo#include <dev/usb/usbdi.h>
102239275Sgonzo#include <dev/usb/usbdi_util.h>
103239275Sgonzo#include "usbdevs.h"
104239275Sgonzo
105239275Sgonzo#define	USB_DEBUG_VAR smsc_debug
106239275Sgonzo#include <dev/usb/usb_debug.h>
107239275Sgonzo#include <dev/usb/usb_process.h>
108239275Sgonzo
109239275Sgonzo#include <dev/usb/net/usb_ethernet.h>
110239275Sgonzo
111246615Shselasky#include <dev/usb/net/if_smscreg.h>
112246615Shselasky
113239275Sgonzo#ifdef USB_DEBUG
114239275Sgonzostatic int smsc_debug = 0;
115239275Sgonzo
116239275SgonzoSYSCTL_NODE(_hw_usb, OID_AUTO, smsc, CTLFLAG_RW, 0, "USB smsc");
117239275SgonzoSYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RW, &smsc_debug, 0,
118239275Sgonzo    "Debug level");
119239275Sgonzo#endif
120239275Sgonzo
121239275Sgonzo/*
122239275Sgonzo * Various supported device vendors/products.
123239275Sgonzo */
124239275Sgonzostatic const struct usb_device_id smsc_devs[] = {
125239275Sgonzo#define	SMSC_DEV(p,i) { USB_VPI(USB_VENDOR_SMC2, USB_PRODUCT_SMC2_##p, i) }
126239275Sgonzo	SMSC_DEV(LAN9514_ETH, 0),
127239275Sgonzo#undef SMSC_DEV
128239275Sgonzo};
129239275Sgonzo
130239275Sgonzo
131239275Sgonzo#ifdef USB_DEBUG
132239275Sgonzo#define smsc_dbg_printf(sc, fmt, args...) \
133239275Sgonzo	do { \
134239275Sgonzo		if (smsc_debug > 0) \
135239275Sgonzo			device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
136239275Sgonzo	} while(0)
137239275Sgonzo#else
138239275Sgonzo#define smsc_dbg_printf(sc, fmt, args...)
139239275Sgonzo#endif
140239275Sgonzo
141239275Sgonzo#define smsc_warn_printf(sc, fmt, args...) \
142239275Sgonzo	device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
143239275Sgonzo
144239275Sgonzo#define smsc_err_printf(sc, fmt, args...) \
145239275Sgonzo	device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
146239275Sgonzo
147239275Sgonzo
148239275Sgonzo#define ETHER_IS_ZERO(addr) \
149239275Sgonzo	(!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
150239275Sgonzo
151239275Sgonzo#define ETHER_IS_VALID(addr) \
152239275Sgonzo	(!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
153239275Sgonzo
154239275Sgonzostatic device_probe_t smsc_probe;
155239275Sgonzostatic device_attach_t smsc_attach;
156239275Sgonzostatic device_detach_t smsc_detach;
157239275Sgonzo
158239275Sgonzostatic usb_callback_t smsc_bulk_read_callback;
159239275Sgonzostatic usb_callback_t smsc_bulk_write_callback;
160239275Sgonzo
161239275Sgonzostatic miibus_readreg_t smsc_miibus_readreg;
162239275Sgonzostatic miibus_writereg_t smsc_miibus_writereg;
163239275Sgonzostatic miibus_statchg_t smsc_miibus_statchg;
164239275Sgonzo
165239275Sgonzo#if __FreeBSD_version > 1000000
166239275Sgonzostatic int smsc_attach_post_sub(struct usb_ether *ue);
167239275Sgonzo#endif
168239275Sgonzostatic uether_fn_t smsc_attach_post;
169239275Sgonzostatic uether_fn_t smsc_init;
170239275Sgonzostatic uether_fn_t smsc_stop;
171239275Sgonzostatic uether_fn_t smsc_start;
172239275Sgonzostatic uether_fn_t smsc_tick;
173239275Sgonzostatic uether_fn_t smsc_setmulti;
174239275Sgonzostatic uether_fn_t smsc_setpromisc;
175239275Sgonzo
176239275Sgonzostatic int	smsc_ifmedia_upd(struct ifnet *);
177239275Sgonzostatic void	smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
178239275Sgonzo
179239275Sgonzostatic int smsc_chip_init(struct smsc_softc *sc);
180239275Sgonzostatic int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
181239275Sgonzo
182239275Sgonzostatic const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
183239275Sgonzo
184239275Sgonzo	[SMSC_BULK_DT_WR] = {
185239275Sgonzo		.type = UE_BULK,
186239275Sgonzo		.endpoint = UE_ADDR_ANY,
187239275Sgonzo		.direction = UE_DIR_OUT,
188239275Sgonzo		.frames = 16,
189239275Sgonzo		.bufsize = 16 * (MCLBYTES + 16),
190239275Sgonzo		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
191239275Sgonzo		.callback = smsc_bulk_write_callback,
192239275Sgonzo		.timeout = 10000,	/* 10 seconds */
193239275Sgonzo	},
194239275Sgonzo
195239275Sgonzo	[SMSC_BULK_DT_RD] = {
196239275Sgonzo		.type = UE_BULK,
197239275Sgonzo		.endpoint = UE_ADDR_ANY,
198239275Sgonzo		.direction = UE_DIR_IN,
199239275Sgonzo		.bufsize = 20480,	/* bytes */
200239275Sgonzo		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
201239275Sgonzo		.callback = smsc_bulk_read_callback,
202239275Sgonzo		.timeout = 0,	/* no timeout */
203239275Sgonzo	},
204239275Sgonzo
205239275Sgonzo	/* The SMSC chip supports an interrupt endpoints, however they aren't
206239275Sgonzo	 * needed as we poll on the MII status.
207239275Sgonzo	 */
208239275Sgonzo};
209239275Sgonzo
210239275Sgonzostatic const struct usb_ether_methods smsc_ue_methods = {
211239275Sgonzo	.ue_attach_post = smsc_attach_post,
212239275Sgonzo#if __FreeBSD_version > 1000000
213239275Sgonzo	.ue_attach_post_sub = smsc_attach_post_sub,
214239275Sgonzo#endif
215239275Sgonzo	.ue_start = smsc_start,
216239275Sgonzo	.ue_ioctl = smsc_ioctl,
217239275Sgonzo	.ue_init = smsc_init,
218239275Sgonzo	.ue_stop = smsc_stop,
219239275Sgonzo	.ue_tick = smsc_tick,
220239275Sgonzo	.ue_setmulti = smsc_setmulti,
221239275Sgonzo	.ue_setpromisc = smsc_setpromisc,
222239275Sgonzo	.ue_mii_upd = smsc_ifmedia_upd,
223239275Sgonzo	.ue_mii_sts = smsc_ifmedia_sts,
224239275Sgonzo};
225239275Sgonzo
226239275Sgonzo/**
227239275Sgonzo *	smsc_read_reg - Reads a 32-bit register on the device
228239275Sgonzo *	@sc: driver soft context
229239275Sgonzo *	@off: offset of the register
230239275Sgonzo *	@data: pointer a value that will be populated with the register value
231239275Sgonzo *
232239275Sgonzo *	LOCKING:
233239275Sgonzo *	The device lock must be held before calling this function.
234239275Sgonzo *
235239275Sgonzo *	RETURNS:
236239275Sgonzo *	0 on success, a USB_ERR_?? error code on failure.
237239275Sgonzo */
238239275Sgonzostatic int
239239275Sgonzosmsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
240239275Sgonzo{
241239275Sgonzo	struct usb_device_request req;
242239275Sgonzo	uint32_t buf;
243239275Sgonzo	usb_error_t err;
244239275Sgonzo
245239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
246239275Sgonzo
247239275Sgonzo	req.bmRequestType = UT_READ_VENDOR_DEVICE;
248239275Sgonzo	req.bRequest = SMSC_UR_READ_REG;
249239275Sgonzo	USETW(req.wValue, 0);
250239275Sgonzo	USETW(req.wIndex, off);
251239275Sgonzo	USETW(req.wLength, 4);
252239275Sgonzo
253239275Sgonzo	err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
254239275Sgonzo	if (err != 0)
255239275Sgonzo		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
256239275Sgonzo
257239275Sgonzo	*data = le32toh(buf);
258239275Sgonzo
259239275Sgonzo	return (err);
260239275Sgonzo}
261239275Sgonzo
262239275Sgonzo/**
263239275Sgonzo *	smsc_write_reg - Writes a 32-bit register on the device
264239275Sgonzo *	@sc: driver soft context
265239275Sgonzo *	@off: offset of the register
266239275Sgonzo *	@data: the 32-bit value to write into the register
267239275Sgonzo *
268239275Sgonzo *	LOCKING:
269239275Sgonzo *	The device lock must be held before calling this function.
270239275Sgonzo *
271239275Sgonzo *	RETURNS:
272239275Sgonzo *	0 on success, a USB_ERR_?? error code on failure.
273239275Sgonzo */
274239275Sgonzostatic int
275239275Sgonzosmsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
276239275Sgonzo{
277239275Sgonzo	struct usb_device_request req;
278239275Sgonzo	uint32_t buf;
279239275Sgonzo	usb_error_t err;
280239275Sgonzo
281239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
282239275Sgonzo
283239275Sgonzo	buf = htole32(data);
284239275Sgonzo
285239275Sgonzo	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
286239275Sgonzo	req.bRequest = SMSC_UR_WRITE_REG;
287239275Sgonzo	USETW(req.wValue, 0);
288239275Sgonzo	USETW(req.wIndex, off);
289239275Sgonzo	USETW(req.wLength, 4);
290239275Sgonzo
291239275Sgonzo	err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
292239275Sgonzo	if (err != 0)
293239275Sgonzo		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
294239275Sgonzo
295239275Sgonzo	return (err);
296239275Sgonzo}
297239275Sgonzo
298239275Sgonzo/**
299239275Sgonzo *	smsc_wait_for_bits - Polls on a register value until bits are cleared
300239275Sgonzo *	@sc: soft context
301239275Sgonzo *	@reg: offset of the register
302239275Sgonzo *	@bits: if the bits are clear the function returns
303239275Sgonzo *
304239275Sgonzo *	LOCKING:
305239275Sgonzo *	The device lock must be held before calling this function.
306239275Sgonzo *
307239275Sgonzo *	RETURNS:
308239275Sgonzo *	0 on success, or a USB_ERR_?? error code on failure.
309239275Sgonzo */
310239275Sgonzostatic int
311239275Sgonzosmsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
312239275Sgonzo{
313239275Sgonzo	usb_ticks_t start_ticks;
314240804Shselasky	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
315239275Sgonzo	uint32_t val;
316239275Sgonzo	int err;
317239275Sgonzo
318239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
319239275Sgonzo
320239275Sgonzo	start_ticks = (usb_ticks_t)ticks;
321239275Sgonzo	do {
322239275Sgonzo		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
323239275Sgonzo			return (err);
324239275Sgonzo		if (!(val & bits))
325239275Sgonzo			return (0);
326239275Sgonzo
327239275Sgonzo		uether_pause(&sc->sc_ue, hz / 100);
328240804Shselasky	} while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
329239275Sgonzo
330239275Sgonzo	return (USB_ERR_TIMEOUT);
331239275Sgonzo}
332239275Sgonzo
333239275Sgonzo/**
334239275Sgonzo *	smsc_eeprom_read - Reads the attached EEPROM
335239275Sgonzo *	@sc: soft context
336239275Sgonzo *	@off: the eeprom address offset
337239275Sgonzo *	@buf: stores the bytes
338239275Sgonzo *	@buflen: the number of bytes to read
339239275Sgonzo *
340239275Sgonzo *	Simply reads bytes from an attached eeprom.
341239275Sgonzo *
342239275Sgonzo *	LOCKING:
343239275Sgonzo *	The function takes and releases the device lock if it is not already held.
344239275Sgonzo *
345239275Sgonzo *	RETURNS:
346239275Sgonzo *	0 on success, or a USB_ERR_?? error code on failure.
347239275Sgonzo */
348239275Sgonzostatic int
349239275Sgonzosmsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
350239275Sgonzo{
351239275Sgonzo	usb_ticks_t start_ticks;
352240806Shselasky	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
353239275Sgonzo	int err;
354239275Sgonzo	int locked;
355239275Sgonzo	uint32_t val;
356239275Sgonzo	uint16_t i;
357239275Sgonzo
358239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
359239275Sgonzo	if (!locked)
360239275Sgonzo		SMSC_LOCK(sc);
361239275Sgonzo
362239275Sgonzo	err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
363239275Sgonzo	if (err != 0) {
364239275Sgonzo		smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
365239275Sgonzo		goto done;
366239275Sgonzo	}
367239275Sgonzo
368239275Sgonzo	/* start reading the bytes, one at a time */
369239275Sgonzo	for (i = 0; i < buflen; i++) {
370239275Sgonzo
371239275Sgonzo		val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
372239275Sgonzo		if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
373239275Sgonzo			goto done;
374239275Sgonzo
375239275Sgonzo		start_ticks = (usb_ticks_t)ticks;
376239275Sgonzo		do {
377239275Sgonzo			if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
378239275Sgonzo				goto done;
379239275Sgonzo			if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
380239275Sgonzo				break;
381239275Sgonzo
382239275Sgonzo			uether_pause(&sc->sc_ue, hz / 100);
383240806Shselasky		} while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
384240806Shselasky
385239275Sgonzo		if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
386239275Sgonzo			smsc_warn_printf(sc, "eeprom command failed\n");
387239275Sgonzo			err = USB_ERR_IOERROR;
388239275Sgonzo			break;
389239275Sgonzo		}
390239275Sgonzo
391239275Sgonzo		if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
392239275Sgonzo			goto done;
393239275Sgonzo
394239275Sgonzo		buf[i] = (val & 0xff);
395239275Sgonzo	}
396239275Sgonzo
397239275Sgonzodone:
398239275Sgonzo	if (!locked)
399239275Sgonzo		SMSC_UNLOCK(sc);
400239275Sgonzo
401239275Sgonzo	return (err);
402239275Sgonzo}
403239275Sgonzo
404239275Sgonzo/**
405239275Sgonzo *	smsc_miibus_readreg - Reads a MII/MDIO register
406239275Sgonzo *	@dev: usb ether device
407239275Sgonzo *	@phy: the number of phy reading from
408239275Sgonzo *	@reg: the register address
409239275Sgonzo *
410239275Sgonzo *	Attempts to read a phy register over the MII bus.
411239275Sgonzo *
412239275Sgonzo *	LOCKING:
413239275Sgonzo *	Takes and releases the device mutex lock if not already held.
414239275Sgonzo *
415239275Sgonzo *	RETURNS:
416239275Sgonzo *	Returns the 16-bits read from the MII register, if this function fails 0
417239275Sgonzo *	is returned.
418239275Sgonzo */
419239275Sgonzostatic int
420239275Sgonzosmsc_miibus_readreg(device_t dev, int phy, int reg)
421239275Sgonzo{
422239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
423239275Sgonzo	int locked;
424239275Sgonzo	uint32_t addr;
425239275Sgonzo	uint32_t val = 0;
426239275Sgonzo
427239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
428239275Sgonzo	if (!locked)
429239275Sgonzo		SMSC_LOCK(sc);
430239275Sgonzo
431239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
432239275Sgonzo		smsc_warn_printf(sc, "MII is busy\n");
433239275Sgonzo		goto done;
434239275Sgonzo	}
435239275Sgonzo
436239275Sgonzo	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
437239275Sgonzo	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
438239275Sgonzo
439239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
440239275Sgonzo		smsc_warn_printf(sc, "MII read timeout\n");
441239275Sgonzo
442239275Sgonzo	smsc_read_reg(sc, SMSC_MII_DATA, &val);
443239275Sgonzo	val = le32toh(val);
444239275Sgonzo
445239275Sgonzodone:
446239275Sgonzo	if (!locked)
447239275Sgonzo		SMSC_UNLOCK(sc);
448239275Sgonzo
449239275Sgonzo	return (val & 0xFFFF);
450239275Sgonzo}
451239275Sgonzo
452239275Sgonzo/**
453239275Sgonzo *	smsc_miibus_writereg - Writes a MII/MDIO register
454239275Sgonzo *	@dev: usb ether device
455239275Sgonzo *	@phy: the number of phy writing to
456239275Sgonzo *	@reg: the register address
457239275Sgonzo *	@val: the value to write
458239275Sgonzo *
459239275Sgonzo *	Attempts to write a phy register over the MII bus.
460239275Sgonzo *
461239275Sgonzo *	LOCKING:
462239275Sgonzo *	Takes and releases the device mutex lock if not already held.
463239275Sgonzo *
464239275Sgonzo *	RETURNS:
465239275Sgonzo *	Always returns 0 regardless of success or failure.
466239275Sgonzo */
467239275Sgonzostatic int
468239275Sgonzosmsc_miibus_writereg(device_t dev, int phy, int reg, int val)
469239275Sgonzo{
470239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
471239275Sgonzo	int locked;
472239275Sgonzo	uint32_t addr;
473239275Sgonzo
474239275Sgonzo	if (sc->sc_phyno != phy)
475239275Sgonzo		return (0);
476239275Sgonzo
477239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
478239275Sgonzo	if (!locked)
479239275Sgonzo		SMSC_LOCK(sc);
480239275Sgonzo
481239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
482239275Sgonzo		smsc_warn_printf(sc, "MII is busy\n");
483239275Sgonzo		goto done;
484239275Sgonzo	}
485239275Sgonzo
486239275Sgonzo	val = htole32(val);
487239275Sgonzo	smsc_write_reg(sc, SMSC_MII_DATA, val);
488239275Sgonzo
489239275Sgonzo	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
490239275Sgonzo	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
491239275Sgonzo
492239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
493239275Sgonzo		smsc_warn_printf(sc, "MII write timeout\n");
494239275Sgonzo
495239275Sgonzodone:
496239275Sgonzo	if (!locked)
497239275Sgonzo		SMSC_UNLOCK(sc);
498239275Sgonzo	return (0);
499239275Sgonzo}
500239275Sgonzo
501239275Sgonzo
502239275Sgonzo
503239275Sgonzo/**
504239275Sgonzo *	smsc_miibus_statchg - Called to detect phy status change
505239275Sgonzo *	@dev: usb ether device
506239275Sgonzo *
507239275Sgonzo *	This function is called periodically by the system to poll for status
508239275Sgonzo *	changes of the link.
509239275Sgonzo *
510239275Sgonzo *	LOCKING:
511239275Sgonzo *	Takes and releases the device mutex lock if not already held.
512239275Sgonzo */
513239275Sgonzostatic void
514239275Sgonzosmsc_miibus_statchg(device_t dev)
515239275Sgonzo{
516239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
517239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
518239275Sgonzo	struct ifnet *ifp;
519239275Sgonzo	int locked;
520239275Sgonzo	int err;
521239275Sgonzo	uint32_t flow;
522239275Sgonzo	uint32_t afc_cfg;
523239275Sgonzo
524239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
525239275Sgonzo	if (!locked)
526239275Sgonzo		SMSC_LOCK(sc);
527239275Sgonzo
528239275Sgonzo	ifp = uether_getifp(&sc->sc_ue);
529239275Sgonzo	if (mii == NULL || ifp == NULL ||
530239275Sgonzo	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
531239275Sgonzo		goto done;
532239275Sgonzo
533239275Sgonzo	/* Use the MII status to determine link status */
534239275Sgonzo	sc->sc_flags &= ~SMSC_FLAG_LINK;
535239275Sgonzo	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
536239275Sgonzo	    (IFM_ACTIVE | IFM_AVALID)) {
537239275Sgonzo		switch (IFM_SUBTYPE(mii->mii_media_active)) {
538239275Sgonzo			case IFM_10_T:
539239275Sgonzo			case IFM_100_TX:
540239275Sgonzo				sc->sc_flags |= SMSC_FLAG_LINK;
541239275Sgonzo				break;
542239275Sgonzo			case IFM_1000_T:
543239275Sgonzo				/* Gigabit ethernet not supported by chipset */
544239275Sgonzo				break;
545239275Sgonzo			default:
546239275Sgonzo				break;
547239275Sgonzo		}
548239275Sgonzo	}
549239275Sgonzo
550239275Sgonzo	/* Lost link, do nothing. */
551239275Sgonzo	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
552239275Sgonzo		smsc_dbg_printf(sc, "link flag not set\n");
553239275Sgonzo		goto done;
554239275Sgonzo	}
555239275Sgonzo
556239275Sgonzo	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
557239275Sgonzo	if (err) {
558239275Sgonzo		smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
559239275Sgonzo		goto done;
560239275Sgonzo	}
561239275Sgonzo
562239275Sgonzo	/* Enable/disable full duplex operation and TX/RX pause */
563239275Sgonzo	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
564239275Sgonzo		smsc_dbg_printf(sc, "full duplex operation\n");
565239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
566239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
567239275Sgonzo
568239275Sgonzo		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
569239275Sgonzo			flow = 0xffff0002;
570239275Sgonzo		else
571239275Sgonzo			flow = 0;
572239275Sgonzo
573239275Sgonzo		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
574239275Sgonzo			afc_cfg |= 0xf;
575239275Sgonzo		else
576239275Sgonzo			afc_cfg &= ~0xf;
577239275Sgonzo
578239275Sgonzo	} else {
579239275Sgonzo		smsc_dbg_printf(sc, "half duplex operation\n");
580239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
581239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
582239275Sgonzo
583239275Sgonzo		flow = 0;
584239275Sgonzo		afc_cfg |= 0xf;
585239275Sgonzo	}
586239275Sgonzo
587239275Sgonzo	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
588239275Sgonzo	err += smsc_write_reg(sc, SMSC_FLOW, flow);
589239275Sgonzo	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
590239275Sgonzo	if (err)
591239275Sgonzo		smsc_warn_printf(sc, "media change failed, error %d\n", err);
592239275Sgonzo
593239275Sgonzodone:
594239275Sgonzo	if (!locked)
595239275Sgonzo		SMSC_UNLOCK(sc);
596239275Sgonzo}
597239275Sgonzo
598239275Sgonzo/**
599239275Sgonzo *	smsc_ifmedia_upd - Set media options
600239275Sgonzo *	@ifp: interface pointer
601239275Sgonzo *
602239275Sgonzo *	Basically boilerplate code that simply calls the mii functions to set the
603239275Sgonzo *	media options.
604239275Sgonzo *
605239275Sgonzo *	LOCKING:
606239275Sgonzo *	The device lock must be held before this function is called.
607239275Sgonzo *
608239275Sgonzo *	RETURNS:
609239275Sgonzo *	Returns 0 on success or a negative error code.
610239275Sgonzo */
611239275Sgonzostatic int
612239275Sgonzosmsc_ifmedia_upd(struct ifnet *ifp)
613239275Sgonzo{
614239275Sgonzo	struct smsc_softc *sc = ifp->if_softc;
615239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
616251734Skevlo	struct mii_softc *miisc;
617239275Sgonzo	int err;
618239275Sgonzo
619239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
620239275Sgonzo
621251734Skevlo	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
622251734Skevlo		PHY_RESET(miisc);
623239275Sgonzo	err = mii_mediachg(mii);
624239275Sgonzo	return (err);
625239275Sgonzo}
626239275Sgonzo
627239275Sgonzo/**
628239275Sgonzo *	smsc_ifmedia_sts - Report current media status
629239275Sgonzo *	@ifp: inet interface pointer
630239275Sgonzo *	@ifmr: interface media request
631239275Sgonzo *
632239275Sgonzo *	Basically boilerplate code that simply calls the mii functions to get the
633239275Sgonzo *	media status.
634239275Sgonzo *
635239275Sgonzo *	LOCKING:
636239275Sgonzo *	Internally takes and releases the device lock.
637239275Sgonzo */
638239275Sgonzostatic void
639239275Sgonzosmsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
640239275Sgonzo{
641239275Sgonzo	struct smsc_softc *sc = ifp->if_softc;
642239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
643239275Sgonzo
644239275Sgonzo	SMSC_LOCK(sc);
645239275Sgonzo	mii_pollstat(mii);
646239275Sgonzo	ifmr->ifm_active = mii->mii_media_active;
647239275Sgonzo	ifmr->ifm_status = mii->mii_media_status;
648251734Skevlo	SMSC_UNLOCK(sc);
649239275Sgonzo}
650239275Sgonzo
651239275Sgonzo/**
652239275Sgonzo *	smsc_hash - Calculate the hash of a mac address
653239275Sgonzo *	@addr: The mac address to calculate the hash on
654239275Sgonzo *
655239275Sgonzo *	This function is used when configuring a range of m'cast mac addresses to
656239275Sgonzo *	filter on.  The hash of the mac address is put in the device's mac hash
657239275Sgonzo *	table.
658239275Sgonzo *
659239275Sgonzo *	RETURNS:
660239275Sgonzo *	Returns a value from 0-63 value which is the hash of the mac address.
661239275Sgonzo */
662239275Sgonzostatic inline uint32_t
663239275Sgonzosmsc_hash(uint8_t addr[ETHER_ADDR_LEN])
664239275Sgonzo{
665239275Sgonzo	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
666239275Sgonzo}
667239275Sgonzo
668239275Sgonzo/**
669239275Sgonzo *	smsc_setmulti - Setup multicast
670239275Sgonzo *	@ue: usb ethernet device context
671239275Sgonzo *
672239275Sgonzo *	Tells the device to either accept frames with a multicast mac address, a
673239275Sgonzo *	select group of m'cast mac addresses or just the devices mac address.
674239275Sgonzo *
675239275Sgonzo *	LOCKING:
676239275Sgonzo *	Should be called with the SMSC lock held.
677239275Sgonzo */
678239275Sgonzostatic void
679239275Sgonzosmsc_setmulti(struct usb_ether *ue)
680239275Sgonzo{
681239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
682239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
683239275Sgonzo	struct ifmultiaddr *ifma;
684239275Sgonzo	uint32_t hashtbl[2] = { 0, 0 };
685239275Sgonzo	uint32_t hash;
686239275Sgonzo
687239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
688239275Sgonzo
689239275Sgonzo	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
690239275Sgonzo		smsc_dbg_printf(sc, "receive all multicast enabled\n");
691239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
692239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
693239275Sgonzo
694239275Sgonzo	} else {
695239275Sgonzo		/* Take the lock of the mac address list before hashing each of them */
696239275Sgonzo		if_maddr_rlock(ifp);
697239275Sgonzo
698239275Sgonzo		if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
699239275Sgonzo			/* We are filtering on a set of address so calculate hashes of each
700239275Sgonzo			 * of the address and set the corresponding bits in the register.
701239275Sgonzo			 */
702239275Sgonzo			sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
703239275Sgonzo			sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
704239275Sgonzo
705239275Sgonzo			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
706239275Sgonzo				if (ifma->ifma_addr->sa_family != AF_LINK)
707239275Sgonzo					continue;
708239275Sgonzo
709239275Sgonzo				hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
710239275Sgonzo				hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
711239275Sgonzo			}
712239275Sgonzo		} else {
713239275Sgonzo			/* Only receive packets with destination set to our mac address */
714239275Sgonzo			sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
715239275Sgonzo		}
716239275Sgonzo
717239275Sgonzo		if_maddr_runlock(ifp);
718239275Sgonzo
719239275Sgonzo		/* Debug */
720239275Sgonzo		if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
721239275Sgonzo			smsc_dbg_printf(sc, "receive select group of macs\n");
722239275Sgonzo		else
723239275Sgonzo			smsc_dbg_printf(sc, "receive own packets only\n");
724239275Sgonzo	}
725239275Sgonzo
726239275Sgonzo	/* Write the hash table and mac control registers */
727239275Sgonzo	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
728239275Sgonzo	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
729239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
730239275Sgonzo}
731239275Sgonzo
732239275Sgonzo
733239275Sgonzo/**
734239275Sgonzo *	smsc_setpromisc - Enables/disables promiscuous mode
735239275Sgonzo *	@ue: usb ethernet device context
736239275Sgonzo *
737239275Sgonzo *	LOCKING:
738239275Sgonzo *	Should be called with the SMSC lock held.
739239275Sgonzo */
740239275Sgonzostatic void
741239275Sgonzosmsc_setpromisc(struct usb_ether *ue)
742239275Sgonzo{
743239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
744239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
745239275Sgonzo
746239275Sgonzo	smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
747239275Sgonzo	                (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
748239275Sgonzo
749239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
750239275Sgonzo
751239275Sgonzo	if (ifp->if_flags & IFF_PROMISC)
752239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
753239275Sgonzo	else
754239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
755239275Sgonzo
756239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
757239275Sgonzo}
758239275Sgonzo
759239275Sgonzo
760239275Sgonzo/**
761239275Sgonzo *	smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
762239275Sgonzo *	@sc: driver soft context
763239275Sgonzo *
764239275Sgonzo *	LOCKING:
765239275Sgonzo *	Should be called with the SMSC lock held.
766239275Sgonzo *
767239275Sgonzo *	RETURNS:
768239275Sgonzo *	Returns 0 on success or a negative error code.
769239275Sgonzo */
770239275Sgonzostatic int smsc_sethwcsum(struct smsc_softc *sc)
771239275Sgonzo{
772239275Sgonzo	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
773239275Sgonzo	uint32_t val;
774239275Sgonzo	int err;
775239275Sgonzo
776239275Sgonzo	if (!ifp)
777239275Sgonzo		return (-EIO);
778239275Sgonzo
779239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
780239275Sgonzo
781239275Sgonzo	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
782239275Sgonzo	if (err != 0) {
783239275Sgonzo		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
784239275Sgonzo		return (err);
785239275Sgonzo	}
786239275Sgonzo
787239275Sgonzo	/* Enable/disable the Rx checksum */
788239275Sgonzo	if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
789239275Sgonzo		val |= SMSC_COE_CTRL_RX_EN;
790239275Sgonzo	else
791239275Sgonzo		val &= ~SMSC_COE_CTRL_RX_EN;
792239275Sgonzo
793239275Sgonzo	/* Enable/disable the Tx checksum (currently not supported) */
794239275Sgonzo	if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
795239275Sgonzo		val |= SMSC_COE_CTRL_TX_EN;
796239275Sgonzo	else
797239275Sgonzo		val &= ~SMSC_COE_CTRL_TX_EN;
798239275Sgonzo
799239275Sgonzo	err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
800239275Sgonzo	if (err != 0) {
801239275Sgonzo		smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
802239275Sgonzo		return (err);
803239275Sgonzo	}
804239275Sgonzo
805239275Sgonzo	return (0);
806239275Sgonzo}
807239275Sgonzo
808239275Sgonzo
809239275Sgonzo/**
810239275Sgonzo *	smsc_setmacaddress - Sets the mac address in the device
811239275Sgonzo *	@sc: driver soft context
812239275Sgonzo *	@addr: pointer to array contain at least 6 bytes of the mac
813239275Sgonzo *
814239275Sgonzo *	Writes the MAC address into the device, usually the MAC is programmed with
815239275Sgonzo *	values from the EEPROM.
816239275Sgonzo *
817239275Sgonzo *	LOCKING:
818239275Sgonzo *	Should be called with the SMSC lock held.
819239275Sgonzo *
820239275Sgonzo *	RETURNS:
821239275Sgonzo *	Returns 0 on success or a negative error code.
822239275Sgonzo */
823239275Sgonzostatic int
824239275Sgonzosmsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
825239275Sgonzo{
826239275Sgonzo	int err;
827239275Sgonzo	uint32_t val;
828239275Sgonzo
829239275Sgonzo	smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
830239275Sgonzo	                addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
831239275Sgonzo
832239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
833239275Sgonzo
834239275Sgonzo	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
835239275Sgonzo	if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
836239275Sgonzo		goto done;
837239275Sgonzo
838239275Sgonzo	val = (addr[5] << 8) | addr[4];
839239275Sgonzo	err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
840239275Sgonzo
841239275Sgonzodone:
842239275Sgonzo	return (err);
843239275Sgonzo}
844239275Sgonzo
845239275Sgonzo/**
846239275Sgonzo *	smsc_reset - Reset the SMSC chip
847239275Sgonzo *	@sc: device soft context
848239275Sgonzo *
849239275Sgonzo *	LOCKING:
850239275Sgonzo *	Should be called with the SMSC lock held.
851239275Sgonzo */
852239275Sgonzostatic void
853239275Sgonzosmsc_reset(struct smsc_softc *sc)
854239275Sgonzo{
855239275Sgonzo	struct usb_config_descriptor *cd;
856239275Sgonzo	usb_error_t err;
857239275Sgonzo
858239275Sgonzo	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
859239275Sgonzo
860239275Sgonzo	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
861239275Sgonzo	                          cd->bConfigurationValue);
862239275Sgonzo	if (err)
863239275Sgonzo		smsc_warn_printf(sc, "reset failed (ignored)\n");
864239275Sgonzo
865239275Sgonzo	/* Wait a little while for the chip to get its brains in order. */
866239275Sgonzo	uether_pause(&sc->sc_ue, hz / 100);
867239275Sgonzo
868239275Sgonzo	/* Reinitialize controller to achieve full reset. */
869239275Sgonzo	smsc_chip_init(sc);
870239275Sgonzo}
871239275Sgonzo
872239275Sgonzo
873239275Sgonzo/**
874239275Sgonzo *	smsc_init - Initialises the LAN95xx chip
875239275Sgonzo *	@ue: USB ether interface
876239275Sgonzo *
877239275Sgonzo *	Called when the interface is brought up (i.e. ifconfig ue0 up), this
878239275Sgonzo *	initialise the interface and the rx/tx pipes.
879239275Sgonzo *
880239275Sgonzo *	LOCKING:
881239275Sgonzo *	Should be called with the SMSC lock held.
882239275Sgonzo */
883239275Sgonzostatic void
884239275Sgonzosmsc_init(struct usb_ether *ue)
885239275Sgonzo{
886239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
887239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
888239275Sgonzo
889239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
890239275Sgonzo
891239275Sgonzo	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
892239275Sgonzo		return;
893239275Sgonzo
894239275Sgonzo	/* Cancel pending I/O */
895239275Sgonzo	smsc_stop(ue);
896239275Sgonzo
897239275Sgonzo#if __FreeBSD_version <= 1000000
898239275Sgonzo	/* On earlier versions this was the first place we could tell the system
899239275Sgonzo	 * that we supported h/w csuming, however this is only called after the
900239275Sgonzo	 * the interface has been brought up - not ideal.
901239275Sgonzo	 */
902239275Sgonzo	if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
903239275Sgonzo		ifp->if_capabilities |= IFCAP_RXCSUM;
904239275Sgonzo		ifp->if_capenable |= IFCAP_RXCSUM;
905239275Sgonzo		ifp->if_hwassist = 0;
906239275Sgonzo	}
907239275Sgonzo
908239275Sgonzo	/* TX checksuming is disabled for now
909239275Sgonzo	ifp->if_capabilities |= IFCAP_TXCSUM;
910239275Sgonzo	ifp->if_capenable |= IFCAP_TXCSUM;
911239275Sgonzo	ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
912239275Sgonzo	*/
913239275Sgonzo#endif
914239275Sgonzo
915239275Sgonzo	/* Reset the ethernet interface. */
916239275Sgonzo	smsc_reset(sc);
917239275Sgonzo
918239275Sgonzo	/* Load the multicast filter. */
919239275Sgonzo	smsc_setmulti(ue);
920239275Sgonzo
921239275Sgonzo	/* TCP/UDP checksum offload engines. */
922239275Sgonzo	smsc_sethwcsum(sc);
923239275Sgonzo
924239275Sgonzo	usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
925239275Sgonzo
926239275Sgonzo	/* Indicate we are up and running. */
927239275Sgonzo	ifp->if_drv_flags |= IFF_DRV_RUNNING;
928239275Sgonzo
929239275Sgonzo	/* Switch to selected media. */
930239275Sgonzo	smsc_ifmedia_upd(ifp);
931239275Sgonzo	smsc_start(ue);
932239275Sgonzo}
933239275Sgonzo
934239275Sgonzo/**
935239275Sgonzo *	smsc_bulk_read_callback - Read callback used to process the USB URB
936239275Sgonzo *	@xfer: the USB transfer
937239275Sgonzo *	@error:
938239275Sgonzo *
939239275Sgonzo *	Reads the URB data which can contain one or more ethernet frames, the
940239275Sgonzo *	frames are copyed into a mbuf and given to the system.
941239275Sgonzo *
942239275Sgonzo *	LOCKING:
943239275Sgonzo *	No locking required, doesn't access internal driver settings.
944239275Sgonzo */
945239275Sgonzostatic void
946239275Sgonzosmsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
947239275Sgonzo{
948239275Sgonzo	struct smsc_softc *sc = usbd_xfer_softc(xfer);
949239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
950239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
951239275Sgonzo	struct mbuf *m;
952239275Sgonzo	struct usb_page_cache *pc;
953239275Sgonzo	uint32_t rxhdr;
954239275Sgonzo	uint16_t pktlen;
955239275Sgonzo	int off;
956239275Sgonzo	int actlen;
957239275Sgonzo
958239275Sgonzo	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
959239275Sgonzo	smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
960239275Sgonzo
961239275Sgonzo	switch (USB_GET_STATE(xfer)) {
962239275Sgonzo	case USB_ST_TRANSFERRED:
963239275Sgonzo
964239275Sgonzo		/* There is always a zero length frame after bringing the IF up */
965239275Sgonzo		if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
966239275Sgonzo			goto tr_setup;
967239275Sgonzo
968239275Sgonzo		/* There maybe multiple packets in the USB frame, each will have a
969239275Sgonzo		 * header and each needs to have it's own mbuf allocated and populated
970239275Sgonzo		 * for it.
971239275Sgonzo		 */
972239275Sgonzo		pc = usbd_xfer_get_frame(xfer, 0);
973239275Sgonzo		off = 0;
974239275Sgonzo
975239275Sgonzo		while (off < actlen) {
976239275Sgonzo
977239275Sgonzo			/* The frame header is always aligned on a 4 byte boundary */
978239275Sgonzo			off = ((off + 0x3) & ~0x3);
979239275Sgonzo
980239275Sgonzo			usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
981239275Sgonzo			off += (sizeof(rxhdr) + ETHER_ALIGN);
982239275Sgonzo			rxhdr = le32toh(rxhdr);
983239275Sgonzo
984239275Sgonzo			pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
985239275Sgonzo
986239275Sgonzo			smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
987239275Sgonzo			                "off %d\n", rxhdr, pktlen, actlen, off);
988239275Sgonzo
989239275Sgonzo
990239275Sgonzo			if (rxhdr & SMSC_RX_STAT_ERROR) {
991239275Sgonzo				smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
992271832Sglebius				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
993239275Sgonzo				if (rxhdr & SMSC_RX_STAT_COLLISION)
994271832Sglebius					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
995239275Sgonzo			} else {
996239275Sgonzo
997239275Sgonzo				/* Check if the ethernet frame is too big or too small */
998239275Sgonzo				if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
999239275Sgonzo					goto tr_setup;
1000239275Sgonzo
1001239275Sgonzo				/* Create a new mbuf to store the packet in */
1002239275Sgonzo				m = uether_newbuf();
1003239275Sgonzo				if (m == NULL) {
1004239275Sgonzo					smsc_warn_printf(sc, "failed to create new mbuf\n");
1005271832Sglebius					if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1006239275Sgonzo					goto tr_setup;
1007239275Sgonzo				}
1008239275Sgonzo
1009239275Sgonzo				usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1010239275Sgonzo
1011239275Sgonzo				/* Check if RX TCP/UDP checksumming is being offloaded */
1012239275Sgonzo				if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1013246196Shselasky
1014246196Shselasky					struct ether_header *eh;
1015246196Shselasky
1016246196Shselasky					eh = mtod(m, struct ether_header *);
1017239275Sgonzo
1018239275Sgonzo					/* Remove the extra 2 bytes of the csum */
1019239275Sgonzo					pktlen -= 2;
1020239275Sgonzo
1021239275Sgonzo					/* The checksum appears to be simplistically calculated
1022239275Sgonzo					 * over the udp/tcp header and data up to the end of the
1023239275Sgonzo					 * eth frame.  Which means if the eth frame is padded
1024239275Sgonzo					 * the csum calculation is incorrectly performed over
1025239275Sgonzo					 * the padding bytes as well. Therefore to be safe we
1026239275Sgonzo					 * ignore the H/W csum on frames less than or equal to
1027239275Sgonzo					 * 64 bytes.
1028246196Shselasky					 *
1029246196Shselasky					 * Ignore H/W csum for non-IPv4 packets.
1030239275Sgonzo					 */
1031265371Stuexen					if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1032265371Stuexen					    (pktlen > ETHER_MIN_LEN)) {
1033265371Stuexen						struct ip *ip;
1034265371Stuexen
1035265371Stuexen						ip = (struct ip *)(eh + 1);
1036265371Stuexen						if ((ip->ip_v == IPVERSION) &&
1037265371Stuexen						    ((ip->ip_p == IPPROTO_TCP) ||
1038265371Stuexen						     (ip->ip_p == IPPROTO_UDP))) {
1039265371Stuexen							/* Indicate the UDP/TCP csum has been calculated */
1040265371Stuexen							m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1041265371Stuexen
1042265371Stuexen							/* Copy the TCP/UDP checksum from the last 2 bytes
1043265371Stuexen							 * of the transfer and put in the csum_data field.
1044265371Stuexen							 */
1045265371Stuexen							usbd_copy_out(pc, (off + pktlen),
1046265371Stuexen							              &m->m_pkthdr.csum_data, 2);
1047265371Stuexen
1048265371Stuexen							/* The data is copied in network order, but the
1049265371Stuexen							 * csum algorithm in the kernel expects it to be
1050265371Stuexen							 * in host network order.
1051265371Stuexen							 */
1052265371Stuexen							m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1053265371Stuexen
1054265371Stuexen							smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1055265371Stuexen							                m->m_pkthdr.csum_data);
1056265371Stuexen						}
1057239275Sgonzo					}
1058239275Sgonzo
1059239275Sgonzo					/* Need to adjust the offset as well or we'll be off
1060239275Sgonzo					 * by 2 because the csum is removed from the packet
1061239275Sgonzo					 * length.
1062239275Sgonzo					 */
1063239275Sgonzo					off += 2;
1064239275Sgonzo				}
1065239275Sgonzo
1066239275Sgonzo				/* Finally enqueue the mbuf on the receive queue */
1067241033Shselasky				/* Remove 4 trailing bytes */
1068241034Shselasky				if (pktlen < (4 + ETHER_HDR_LEN)) {
1069241034Shselasky					m_freem(m);
1070241034Shselasky					goto tr_setup;
1071241034Shselasky				}
1072241034Shselasky				uether_rxmbuf(ue, m, pktlen - 4);
1073239275Sgonzo			}
1074239275Sgonzo
1075239275Sgonzo			/* Update the offset to move to the next potential packet */
1076239275Sgonzo			off += pktlen;
1077239275Sgonzo		}
1078239275Sgonzo
1079239275Sgonzo		/* FALLTHROUGH */
1080239275Sgonzo
1081239275Sgonzo	case USB_ST_SETUP:
1082239275Sgonzotr_setup:
1083239275Sgonzo		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1084239275Sgonzo		usbd_transfer_submit(xfer);
1085239275Sgonzo		uether_rxflush(ue);
1086239275Sgonzo		return;
1087239275Sgonzo
1088239275Sgonzo	default:
1089239275Sgonzo		if (error != USB_ERR_CANCELLED) {
1090239275Sgonzo			smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1091239275Sgonzo			usbd_xfer_set_stall(xfer);
1092239275Sgonzo			goto tr_setup;
1093239275Sgonzo		}
1094239275Sgonzo		return;
1095239275Sgonzo	}
1096239275Sgonzo}
1097239275Sgonzo
1098239275Sgonzo/**
1099239275Sgonzo *	smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1100239275Sgonzo *	@xfer: the USB transfer
1101239275Sgonzo *	@error: error code if the transfers is in an errored state
1102239275Sgonzo *
1103239275Sgonzo *	The main write function that pulls ethernet frames off the queue and sends
1104239275Sgonzo *	them out.
1105239275Sgonzo *
1106239275Sgonzo *	LOCKING:
1107239275Sgonzo *
1108239275Sgonzo */
1109239275Sgonzostatic void
1110239275Sgonzosmsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1111239275Sgonzo{
1112239275Sgonzo	struct smsc_softc *sc = usbd_xfer_softc(xfer);
1113239275Sgonzo	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1114239275Sgonzo	struct usb_page_cache *pc;
1115239275Sgonzo	struct mbuf *m;
1116239275Sgonzo	uint32_t txhdr;
1117239275Sgonzo	uint32_t frm_len = 0;
1118239275Sgonzo	int nframes;
1119239275Sgonzo
1120239275Sgonzo	switch (USB_GET_STATE(xfer)) {
1121239275Sgonzo	case USB_ST_TRANSFERRED:
1122239275Sgonzo		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1123239275Sgonzo		/* FALLTHROUGH */
1124239275Sgonzo
1125239275Sgonzo	case USB_ST_SETUP:
1126239275Sgonzotr_setup:
1127239275Sgonzo		if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1128239275Sgonzo			(ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1129239275Sgonzo			/* Don't send anything if there is no link or controller is busy. */
1130239275Sgonzo			return;
1131239275Sgonzo		}
1132239275Sgonzo
1133239275Sgonzo		for (nframes = 0; nframes < 16 &&
1134239275Sgonzo		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1135239275Sgonzo			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1136239275Sgonzo			if (m == NULL)
1137239275Sgonzo				break;
1138239275Sgonzo			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1139239275Sgonzo			    nframes);
1140239275Sgonzo			frm_len = 0;
1141239275Sgonzo			pc = usbd_xfer_get_frame(xfer, nframes);
1142239275Sgonzo
1143239275Sgonzo			/* Each frame is prefixed with two 32-bit values describing the
1144239275Sgonzo			 * length of the packet and buffer.
1145239275Sgonzo			 */
1146239275Sgonzo			txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1147239275Sgonzo					SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1148239275Sgonzo			txhdr = htole32(txhdr);
1149239275Sgonzo			usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1150239275Sgonzo
1151239275Sgonzo			txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1152239275Sgonzo			txhdr = htole32(txhdr);
1153239275Sgonzo			usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1154239275Sgonzo
1155239275Sgonzo			frm_len += 8;
1156239275Sgonzo
1157239275Sgonzo			/* Next copy in the actual packet */
1158239275Sgonzo			usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1159239275Sgonzo			frm_len += m->m_pkthdr.len;
1160239275Sgonzo
1161271832Sglebius			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1162239275Sgonzo
1163239275Sgonzo			/* If there's a BPF listener, bounce a copy of this frame to him */
1164239275Sgonzo			BPF_MTAP(ifp, m);
1165239275Sgonzo
1166239275Sgonzo			m_freem(m);
1167239275Sgonzo
1168239275Sgonzo			/* Set frame length. */
1169239275Sgonzo			usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1170239275Sgonzo		}
1171239275Sgonzo		if (nframes != 0) {
1172239275Sgonzo			usbd_xfer_set_frames(xfer, nframes);
1173239275Sgonzo			usbd_transfer_submit(xfer);
1174239275Sgonzo			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1175239275Sgonzo		}
1176239275Sgonzo		return;
1177239275Sgonzo
1178239275Sgonzo	default:
1179271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1180239275Sgonzo		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1181239275Sgonzo
1182239275Sgonzo		if (error != USB_ERR_CANCELLED) {
1183239275Sgonzo			smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1184239275Sgonzo			usbd_xfer_set_stall(xfer);
1185239275Sgonzo			goto tr_setup;
1186239275Sgonzo		}
1187239275Sgonzo		return;
1188239275Sgonzo	}
1189239275Sgonzo}
1190239275Sgonzo
1191239275Sgonzo/**
1192239275Sgonzo *	smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1193239275Sgonzo *	@ue: USB ether interface
1194239275Sgonzo *
1195239275Sgonzo *	Simply calls the mii status functions to check the state of the link.
1196239275Sgonzo *
1197239275Sgonzo *	LOCKING:
1198239275Sgonzo *	Should be called with the SMSC lock held.
1199239275Sgonzo */
1200239275Sgonzostatic void
1201239275Sgonzosmsc_tick(struct usb_ether *ue)
1202239275Sgonzo{
1203239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1204241844Seadler	struct mii_data *mii = uether_getmii(&sc->sc_ue);
1205239275Sgonzo
1206239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1207239275Sgonzo
1208239275Sgonzo	mii_tick(mii);
1209239275Sgonzo	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1210239275Sgonzo		smsc_miibus_statchg(ue->ue_dev);
1211239275Sgonzo		if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1212239275Sgonzo			smsc_start(ue);
1213239275Sgonzo	}
1214239275Sgonzo}
1215239275Sgonzo
1216239275Sgonzo/**
1217239275Sgonzo *	smsc_start - Starts communication with the LAN95xx chip
1218239275Sgonzo *	@ue: USB ether interface
1219239275Sgonzo *
1220239275Sgonzo *
1221239275Sgonzo *
1222239275Sgonzo */
1223239275Sgonzostatic void
1224239275Sgonzosmsc_start(struct usb_ether *ue)
1225239275Sgonzo{
1226239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1227239275Sgonzo
1228239275Sgonzo	/*
1229239275Sgonzo	 * start the USB transfers, if not already started:
1230239275Sgonzo	 */
1231239275Sgonzo	usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1232239275Sgonzo	usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1233239275Sgonzo}
1234239275Sgonzo
1235239275Sgonzo/**
1236239275Sgonzo *	smsc_stop - Stops communication with the LAN95xx chip
1237239275Sgonzo *	@ue: USB ether interface
1238239275Sgonzo *
1239239275Sgonzo *
1240239275Sgonzo *
1241239275Sgonzo */
1242239275Sgonzostatic void
1243239275Sgonzosmsc_stop(struct usb_ether *ue)
1244239275Sgonzo{
1245239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1246239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
1247239275Sgonzo
1248239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1249239275Sgonzo
1250239275Sgonzo	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1251239275Sgonzo	sc->sc_flags &= ~SMSC_FLAG_LINK;
1252239275Sgonzo
1253239275Sgonzo	/*
1254239275Sgonzo	 * stop all the transfers, if not already stopped:
1255239275Sgonzo	 */
1256239275Sgonzo	usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1257239275Sgonzo	usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1258239275Sgonzo}
1259239275Sgonzo
1260239275Sgonzo/**
1261239275Sgonzo *	smsc_phy_init - Initialises the in-built SMSC phy
1262239275Sgonzo *	@sc: driver soft context
1263239275Sgonzo *
1264239275Sgonzo *	Resets the PHY part of the chip and then initialises it to default
1265239275Sgonzo *	values.  The 'link down' and 'auto-negotiation complete' interrupts
1266239275Sgonzo *	from the PHY are also enabled, however we don't monitor the interrupt
1267239275Sgonzo *	endpoints for the moment.
1268239275Sgonzo *
1269239275Sgonzo *	RETURNS:
1270239275Sgonzo *	Returns 0 on success or EIO if failed to reset the PHY.
1271239275Sgonzo */
1272239275Sgonzostatic int
1273239275Sgonzosmsc_phy_init(struct smsc_softc *sc)
1274239275Sgonzo{
1275239275Sgonzo	int bmcr;
1276239275Sgonzo	usb_ticks_t start_ticks;
1277240806Shselasky	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1278239275Sgonzo
1279239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1280239275Sgonzo
1281239275Sgonzo	/* Reset phy and wait for reset to complete */
1282239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1283239275Sgonzo
1284239275Sgonzo	start_ticks = ticks;
1285239275Sgonzo	do {
1286239275Sgonzo		uether_pause(&sc->sc_ue, hz / 100);
1287239275Sgonzo		bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1288239275Sgonzo	} while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1289239275Sgonzo
1290240806Shselasky	if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1291239275Sgonzo		smsc_err_printf(sc, "PHY reset timed-out");
1292239275Sgonzo		return (EIO);
1293239275Sgonzo	}
1294239275Sgonzo
1295239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1296239275Sgonzo	                     ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD |  /* all modes */
1297239275Sgonzo	                     ANAR_CSMA |
1298239275Sgonzo	                     ANAR_FC |
1299239275Sgonzo	                     ANAR_PAUSE_ASYM);
1300239275Sgonzo
1301239275Sgonzo	/* Setup the phy to interrupt when the link goes down or autoneg completes */
1302239275Sgonzo	smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1303239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1304239275Sgonzo	                     (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1305239275Sgonzo
1306239275Sgonzo	/* Restart auto-negotation */
1307239275Sgonzo	bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1308239275Sgonzo	bmcr |= BMCR_STARTNEG;
1309239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1310239275Sgonzo
1311239275Sgonzo	return (0);
1312239275Sgonzo}
1313239275Sgonzo
1314239275Sgonzo
1315239275Sgonzo/**
1316239275Sgonzo *	smsc_chip_init - Initialises the chip after power on
1317239275Sgonzo *	@sc: driver soft context
1318239275Sgonzo *
1319239275Sgonzo *	This initialisation sequence is modelled on the procedure in the Linux
1320239275Sgonzo *	driver.
1321239275Sgonzo *
1322239275Sgonzo *	RETURNS:
1323239275Sgonzo *	Returns 0 on success or an error code on failure.
1324239275Sgonzo */
1325239275Sgonzostatic int
1326239275Sgonzosmsc_chip_init(struct smsc_softc *sc)
1327239275Sgonzo{
1328239275Sgonzo	int err;
1329239275Sgonzo	int locked;
1330239275Sgonzo	uint32_t reg_val;
1331239275Sgonzo	int burst_cap;
1332239275Sgonzo
1333239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
1334239275Sgonzo	if (!locked)
1335239275Sgonzo		SMSC_LOCK(sc);
1336239275Sgonzo
1337239275Sgonzo	/* Enter H/W config mode */
1338239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1339239275Sgonzo
1340239275Sgonzo	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1341239275Sgonzo		smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1342239275Sgonzo		goto init_failed;
1343239275Sgonzo	}
1344239275Sgonzo
1345239275Sgonzo	/* Reset the PHY */
1346239275Sgonzo	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1347239275Sgonzo
1348239275Sgonzo	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST) != 0)) {
1349239275Sgonzo		smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1350239275Sgonzo		goto init_failed;
1351239275Sgonzo	}
1352239275Sgonzo
1353239275Sgonzo	/* Set the mac address */
1354239275Sgonzo	if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1355239275Sgonzo		smsc_warn_printf(sc, "failed to set the MAC address\n");
1356239275Sgonzo		goto init_failed;
1357239275Sgonzo	}
1358239275Sgonzo
1359239275Sgonzo	/* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1360239275Sgonzo	 * as used in the Linux driver.
1361239275Sgonzo	 */
1362239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
1363239275Sgonzo		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1364239275Sgonzo		goto init_failed;
1365239275Sgonzo	}
1366239275Sgonzo	reg_val |= SMSC_HW_CFG_BIR;
1367239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1368239275Sgonzo
1369239275Sgonzo	/* There is a so called 'turbo mode' that the linux driver supports, it
1370239275Sgonzo	 * seems to allow you to jam multiple frames per Rx transaction.  By default
1371239275Sgonzo	 * this driver supports that and therefore allows multiple frames per URB.
1372239275Sgonzo	 *
1373239275Sgonzo	 * The xfer buffer size needs to reflect this as well, therefore based on
1374239275Sgonzo	 * the calculations in the Linux driver the RX bufsize is set to 18944,
1375239275Sgonzo	 *     bufsz = (16 * 1024 + 5 * 512)
1376239275Sgonzo	 *
1377239275Sgonzo	 * Burst capability is the number of URBs that can be in a burst of data/
1378239275Sgonzo	 * ethernet frames.
1379239275Sgonzo	 */
1380239275Sgonzo	if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1381239275Sgonzo		burst_cap = 37;
1382239275Sgonzo	else
1383239275Sgonzo		burst_cap = 128;
1384239275Sgonzo
1385239275Sgonzo	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1386239275Sgonzo
1387239275Sgonzo	/* Set the default bulk in delay (magic value from Linux driver) */
1388239275Sgonzo	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1389239275Sgonzo
1390239275Sgonzo
1391239275Sgonzo
1392239275Sgonzo	/*
1393239275Sgonzo	 * Initialise the RX interface
1394239275Sgonzo	 */
1395239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
1396239275Sgonzo		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1397239275Sgonzo		goto init_failed;
1398239275Sgonzo	}
1399239275Sgonzo
1400239275Sgonzo	/* Adjust the packet offset in the buffer (designed to try and align IP
1401239275Sgonzo	 * header on 4 byte boundary)
1402239275Sgonzo	 */
1403239275Sgonzo	reg_val &= ~SMSC_HW_CFG_RXDOFF;
1404239275Sgonzo	reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1405239275Sgonzo
1406239275Sgonzo	/* The following setings are used for 'turbo mode', a.k.a multiple frames
1407239275Sgonzo	 * per Rx transaction (again info taken form Linux driver).
1408239275Sgonzo	 */
1409239275Sgonzo	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1410239275Sgonzo
1411239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1412239275Sgonzo
1413239275Sgonzo	/* Clear the status register ? */
1414239275Sgonzo	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1415239275Sgonzo
1416239275Sgonzo	/* Read and display the revision register */
1417239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1418239275Sgonzo		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1419239275Sgonzo		goto init_failed;
1420239275Sgonzo	}
1421239275Sgonzo
1422239275Sgonzo	device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1423239275Sgonzo	    (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1424239275Sgonzo	    (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1425239275Sgonzo
1426239275Sgonzo	/* GPIO/LED setup */
1427239275Sgonzo	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1428239275Sgonzo	          SMSC_LED_GPIO_CFG_FDX_LED;
1429239275Sgonzo	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1430239275Sgonzo
1431239275Sgonzo	/*
1432239275Sgonzo	 * Initialise the TX interface
1433239275Sgonzo	 */
1434239275Sgonzo	smsc_write_reg(sc, SMSC_FLOW, 0);
1435239275Sgonzo
1436239275Sgonzo	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1437239275Sgonzo
1438239275Sgonzo	/* Read the current MAC configuration */
1439239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1440239275Sgonzo		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1441239275Sgonzo		goto init_failed;
1442239275Sgonzo	}
1443239275Sgonzo
1444239275Sgonzo	/* Vlan */
1445239275Sgonzo	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1446239275Sgonzo
1447239275Sgonzo	/*
1448239275Sgonzo	 * Initialise the PHY
1449239275Sgonzo	 */
1450239275Sgonzo	if ((err = smsc_phy_init(sc)) != 0)
1451239275Sgonzo		goto init_failed;
1452239275Sgonzo
1453239275Sgonzo
1454239275Sgonzo	/*
1455239275Sgonzo	 * Start TX
1456239275Sgonzo	 */
1457239275Sgonzo	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1458239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1459239275Sgonzo	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1460239275Sgonzo
1461239275Sgonzo	/*
1462239275Sgonzo	 * Start RX
1463239275Sgonzo	 */
1464239275Sgonzo	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1465239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1466239275Sgonzo
1467239275Sgonzo	if (!locked)
1468239275Sgonzo		SMSC_UNLOCK(sc);
1469239275Sgonzo
1470239275Sgonzo	return (0);
1471239275Sgonzo
1472239275Sgonzoinit_failed:
1473239275Sgonzo	if (!locked)
1474239275Sgonzo		SMSC_UNLOCK(sc);
1475239275Sgonzo
1476239275Sgonzo	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1477239275Sgonzo	return (err);
1478239275Sgonzo}
1479239275Sgonzo
1480239275Sgonzo
1481239275Sgonzo/**
1482239275Sgonzo *	smsc_ioctl - ioctl function for the device
1483239275Sgonzo *	@ifp: interface pointer
1484239275Sgonzo *	@cmd: the ioctl command
1485239275Sgonzo *	@data: data passed in the ioctl call, typically a pointer to struct ifreq.
1486239275Sgonzo *
1487239275Sgonzo *	The ioctl routine is overridden to detect change requests for the H/W
1488239275Sgonzo *	checksum capabilities.
1489239275Sgonzo *
1490239275Sgonzo *	RETURNS:
1491239275Sgonzo *	0 on success and an error code on failure.
1492239275Sgonzo */
1493239275Sgonzostatic int
1494239275Sgonzosmsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1495239275Sgonzo{
1496239275Sgonzo	struct usb_ether *ue = ifp->if_softc;
1497239275Sgonzo	struct smsc_softc *sc;
1498239275Sgonzo	struct ifreq *ifr;
1499239275Sgonzo	int rc;
1500239275Sgonzo	int mask;
1501239275Sgonzo	int reinit;
1502239275Sgonzo
1503239275Sgonzo	if (cmd == SIOCSIFCAP) {
1504239275Sgonzo
1505239275Sgonzo		sc = uether_getsc(ue);
1506239275Sgonzo		ifr = (struct ifreq *)data;
1507239275Sgonzo
1508239275Sgonzo		SMSC_LOCK(sc);
1509239275Sgonzo
1510239275Sgonzo		rc = 0;
1511239275Sgonzo		reinit = 0;
1512239275Sgonzo
1513239275Sgonzo		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1514239275Sgonzo
1515239275Sgonzo		/* Modify the RX CSUM enable bits */
1516239275Sgonzo		if ((mask & IFCAP_RXCSUM) != 0 &&
1517239275Sgonzo		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1518239275Sgonzo			ifp->if_capenable ^= IFCAP_RXCSUM;
1519239275Sgonzo
1520239275Sgonzo			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1521239275Sgonzo				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1522239275Sgonzo				reinit = 1;
1523239275Sgonzo			}
1524239275Sgonzo		}
1525239275Sgonzo
1526239275Sgonzo		SMSC_UNLOCK(sc);
1527239275Sgonzo		if (reinit)
1528239275Sgonzo#if __FreeBSD_version > 1000000
1529239275Sgonzo			uether_init(ue);
1530239275Sgonzo#else
1531239275Sgonzo			ifp->if_init(ue);
1532239275Sgonzo#endif
1533239275Sgonzo
1534239275Sgonzo	} else {
1535239275Sgonzo		rc = uether_ioctl(ifp, cmd, data);
1536239275Sgonzo	}
1537239275Sgonzo
1538239275Sgonzo	return (rc);
1539239275Sgonzo}
1540239275Sgonzo
1541243421Sgonzo#ifdef FDT
1542243421Sgonzo/**
1543243421Sgonzo * Get MAC address from FDT blob. Firmware or loader should fill
1544243421Sgonzo * mac-address or local-mac-address property Returns 0 if MAC address
1545243421Sgonzo * obtained, error code otherwise
1546243421Sgonzo */
1547243421Sgonzostatic int
1548243421Sgonzosmsc_fdt_find_mac(unsigned char *mac)
1549243421Sgonzo{
1550243421Sgonzo	phandle_t child, parent, root;
1551243421Sgonzo	int len;
1552239275Sgonzo
1553243421Sgonzo	root = OF_finddevice("/");
1554243421Sgonzo	len = 0;
1555243421Sgonzo	parent = root;
1556243421Sgonzo
1557243421Sgonzo	/* Traverse through entire tree to find nodes usb ethernet nodes */
1558243421Sgonzo	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
1559243421Sgonzo
1560243421Sgonzo		/* Find a 'leaf'. Start the search from this node. */
1561243421Sgonzo		while (OF_child(child)) {
1562243421Sgonzo			parent = child;
1563243421Sgonzo			child = OF_child(child);
1564243421Sgonzo		}
1565243421Sgonzo
1566243421Sgonzo		if (fdt_is_compatible(child, "net,ethernet") &&
1567243421Sgonzo		    fdt_is_compatible(child, "usb,device")) {
1568243421Sgonzo
1569243421Sgonzo			/* Check if there is property */
1570243421Sgonzo			if ((len = OF_getproplen(child, "local-mac-address")) > 0) {
1571243421Sgonzo				if (len != ETHER_ADDR_LEN)
1572243421Sgonzo					return (EINVAL);
1573243421Sgonzo
1574243421Sgonzo				OF_getprop(child, "local-mac-address", mac,
1575243421Sgonzo				    ETHER_ADDR_LEN);
1576243421Sgonzo				return (0);
1577243421Sgonzo			}
1578243421Sgonzo
1579243421Sgonzo			if ((len = OF_getproplen(child, "mac-address")) > 0) {
1580243421Sgonzo				if (len != ETHER_ADDR_LEN)
1581243421Sgonzo					return (EINVAL);
1582243421Sgonzo
1583243421Sgonzo				OF_getprop(child, "mac-address", mac,
1584243421Sgonzo				    ETHER_ADDR_LEN);
1585243421Sgonzo				return (0);
1586243421Sgonzo			}
1587243421Sgonzo		}
1588243421Sgonzo
1589243421Sgonzo		if (OF_peer(child) == 0) {
1590243421Sgonzo			/* No more siblings. */
1591243421Sgonzo			child = parent;
1592243421Sgonzo			parent = OF_parent(child);
1593243421Sgonzo		}
1594243421Sgonzo	}
1595243421Sgonzo
1596243421Sgonzo	return (ENXIO);
1597243421Sgonzo}
1598243421Sgonzo#endif
1599243421Sgonzo
1600239275Sgonzo/**
1601239275Sgonzo *	smsc_attach_post - Called after the driver attached to the USB interface
1602239275Sgonzo *	@ue: the USB ethernet device
1603239275Sgonzo *
1604239275Sgonzo *	This is where the chip is intialised for the first time.  This is different
1605239275Sgonzo *	from the smsc_init() function in that that one is designed to setup the
1606239275Sgonzo *	H/W to match the UE settings and can be called after a reset.
1607239275Sgonzo *
1608239275Sgonzo *
1609239275Sgonzo */
1610239275Sgonzostatic void
1611239275Sgonzosmsc_attach_post(struct usb_ether *ue)
1612239275Sgonzo{
1613239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1614239275Sgonzo	uint32_t mac_h, mac_l;
1615239275Sgonzo	int err;
1616239275Sgonzo
1617239275Sgonzo	smsc_dbg_printf(sc, "smsc_attach_post\n");
1618239275Sgonzo
1619239275Sgonzo	/* Setup some of the basics */
1620239275Sgonzo	sc->sc_phyno = 1;
1621239275Sgonzo
1622239275Sgonzo
1623239275Sgonzo	/* Attempt to get the mac address, if an EEPROM is not attached this
1624239275Sgonzo	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1625239275Sgonzo	 * address based on urandom.
1626239275Sgonzo	 */
1627239275Sgonzo	memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1628239275Sgonzo
1629239275Sgonzo	/* Check if there is already a MAC address in the register */
1630239275Sgonzo	if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1631239275Sgonzo	    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1632239275Sgonzo		sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1633239275Sgonzo		sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1634239275Sgonzo		sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1635239275Sgonzo		sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1636239275Sgonzo		sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1637239275Sgonzo		sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1638239275Sgonzo	}
1639239275Sgonzo
1640239275Sgonzo	/* MAC address is not set so try to read from EEPROM, if that fails generate
1641239275Sgonzo	 * a random MAC address.
1642239275Sgonzo	 */
1643239275Sgonzo	if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1644239275Sgonzo
1645239275Sgonzo		err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1646243421Sgonzo#ifdef FDT
1647243421Sgonzo		if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1648243421Sgonzo			err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1649243421Sgonzo#endif
1650239275Sgonzo		if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1651239275Sgonzo			read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1652239275Sgonzo			sc->sc_ue.ue_eaddr[0] &= ~0x01;     /* unicast */
1653239275Sgonzo			sc->sc_ue.ue_eaddr[0] |=  0x02;     /* locally administered */
1654239275Sgonzo		}
1655239275Sgonzo	}
1656239275Sgonzo
1657239275Sgonzo	/* Initialise the chip for the first time */
1658239275Sgonzo	smsc_chip_init(sc);
1659239275Sgonzo}
1660239275Sgonzo
1661239275Sgonzo
1662239275Sgonzo/**
1663239275Sgonzo *	smsc_attach_post_sub - Called after the driver attached to the USB interface
1664239275Sgonzo *	@ue: the USB ethernet device
1665239275Sgonzo *
1666239275Sgonzo *	Most of this is boilerplate code and copied from the base USB ethernet
1667239275Sgonzo *	driver.  It has been overriden so that we can indicate to the system that
1668239275Sgonzo *	the chip supports H/W checksumming.
1669239275Sgonzo *
1670239275Sgonzo *	RETURNS:
1671239275Sgonzo *	Returns 0 on success or a negative error code.
1672239275Sgonzo */
1673239275Sgonzo#if __FreeBSD_version > 1000000
1674239275Sgonzostatic int
1675239275Sgonzosmsc_attach_post_sub(struct usb_ether *ue)
1676239275Sgonzo{
1677239275Sgonzo	struct smsc_softc *sc;
1678239275Sgonzo	struct ifnet *ifp;
1679239275Sgonzo	int error;
1680239275Sgonzo
1681239275Sgonzo	sc = uether_getsc(ue);
1682239275Sgonzo	ifp = ue->ue_ifp;
1683239275Sgonzo	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1684239275Sgonzo	ifp->if_start = uether_start;
1685239275Sgonzo	ifp->if_ioctl = smsc_ioctl;
1686239275Sgonzo	ifp->if_init = uether_init;
1687239275Sgonzo	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1688239275Sgonzo	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1689239275Sgonzo	IFQ_SET_READY(&ifp->if_snd);
1690239275Sgonzo
1691239275Sgonzo	/* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1692239275Sgonzo	 * currently only RX checksum is supported in the driver (see top of file).
1693239275Sgonzo	 */
1694239275Sgonzo	ifp->if_capabilities |= IFCAP_RXCSUM;
1695239275Sgonzo	ifp->if_hwassist = 0;
1696239275Sgonzo
1697239275Sgonzo	/* TX checksuming is disabled (for now?)
1698239275Sgonzo	ifp->if_capabilities |= IFCAP_TXCSUM;
1699239275Sgonzo	ifp->if_capenable |= IFCAP_TXCSUM;
1700239275Sgonzo	ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1701239275Sgonzo	*/
1702239275Sgonzo
1703239275Sgonzo	ifp->if_capenable = ifp->if_capabilities;
1704239275Sgonzo
1705239275Sgonzo	mtx_lock(&Giant);
1706239275Sgonzo	error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1707239275Sgonzo	    uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1708239275Sgonzo	    BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1709239275Sgonzo	mtx_unlock(&Giant);
1710239275Sgonzo
1711239275Sgonzo	return (error);
1712239275Sgonzo}
1713239275Sgonzo#endif /* __FreeBSD_version > 1000000 */
1714239275Sgonzo
1715239275Sgonzo
1716239275Sgonzo/**
1717239275Sgonzo *	smsc_probe - Probe the interface.
1718239275Sgonzo *	@dev: smsc device handle
1719239275Sgonzo *
1720239275Sgonzo *	Checks if the device is a match for this driver.
1721239275Sgonzo *
1722239275Sgonzo *	RETURNS:
1723239275Sgonzo *	Returns 0 on success or an error code on failure.
1724239275Sgonzo */
1725239275Sgonzostatic int
1726239275Sgonzosmsc_probe(device_t dev)
1727239275Sgonzo{
1728239275Sgonzo	struct usb_attach_arg *uaa = device_get_ivars(dev);
1729239275Sgonzo
1730239275Sgonzo	if (uaa->usb_mode != USB_MODE_HOST)
1731239275Sgonzo		return (ENXIO);
1732239275Sgonzo	if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1733239275Sgonzo		return (ENXIO);
1734239275Sgonzo	if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1735239275Sgonzo		return (ENXIO);
1736239275Sgonzo
1737239275Sgonzo	return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1738239275Sgonzo}
1739239275Sgonzo
1740239275Sgonzo
1741239275Sgonzo/**
1742239275Sgonzo *	smsc_attach - Attach the interface.
1743239275Sgonzo *	@dev: smsc device handle
1744239275Sgonzo *
1745239275Sgonzo *	Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1746239275Sgonzo *
1747239275Sgonzo *	RETURNS:
1748239275Sgonzo *	Returns 0 on success or a negative error code.
1749239275Sgonzo */
1750239275Sgonzostatic int
1751239275Sgonzosmsc_attach(device_t dev)
1752239275Sgonzo{
1753239275Sgonzo	struct usb_attach_arg *uaa = device_get_ivars(dev);
1754239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
1755239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
1756239275Sgonzo	uint8_t iface_index;
1757239275Sgonzo	int err;
1758239275Sgonzo
1759239275Sgonzo	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1760239275Sgonzo
1761239275Sgonzo	device_set_usb_desc(dev);
1762239275Sgonzo
1763239275Sgonzo	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1764239275Sgonzo
1765239275Sgonzo	/* Setup the endpoints for the SMSC LAN95xx device(s) */
1766239275Sgonzo	iface_index = SMSC_IFACE_IDX;
1767239275Sgonzo	err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1768239275Sgonzo	                          smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1769239275Sgonzo	if (err) {
1770239275Sgonzo		device_printf(dev, "error: allocating USB transfers failed\n");
1771239275Sgonzo		goto detach;
1772239275Sgonzo	}
1773239275Sgonzo
1774239275Sgonzo	ue->ue_sc = sc;
1775239275Sgonzo	ue->ue_dev = dev;
1776239275Sgonzo	ue->ue_udev = uaa->device;
1777239275Sgonzo	ue->ue_mtx = &sc->sc_mtx;
1778239275Sgonzo	ue->ue_methods = &smsc_ue_methods;
1779239275Sgonzo
1780239275Sgonzo	err = uether_ifattach(ue);
1781239275Sgonzo	if (err) {
1782239275Sgonzo		device_printf(dev, "error: could not attach interface\n");
1783239275Sgonzo		goto detach;
1784239275Sgonzo	}
1785239275Sgonzo	return (0);			/* success */
1786239275Sgonzo
1787239275Sgonzodetach:
1788239275Sgonzo	smsc_detach(dev);
1789239275Sgonzo	return (ENXIO);		/* failure */
1790239275Sgonzo}
1791239275Sgonzo
1792239275Sgonzo/**
1793239275Sgonzo *	smsc_detach - Detach the interface.
1794239275Sgonzo *	@dev: smsc device handle
1795239275Sgonzo *
1796239275Sgonzo *	RETURNS:
1797239275Sgonzo *	Returns 0.
1798239275Sgonzo */
1799239275Sgonzostatic int
1800239275Sgonzosmsc_detach(device_t dev)
1801239275Sgonzo{
1802239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
1803239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
1804239275Sgonzo
1805239275Sgonzo	usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1806239275Sgonzo	uether_ifdetach(ue);
1807239275Sgonzo	mtx_destroy(&sc->sc_mtx);
1808239275Sgonzo
1809239275Sgonzo	return (0);
1810239275Sgonzo}
1811239275Sgonzo
1812239275Sgonzostatic device_method_t smsc_methods[] = {
1813239275Sgonzo	/* Device interface */
1814239275Sgonzo	DEVMETHOD(device_probe, smsc_probe),
1815239275Sgonzo	DEVMETHOD(device_attach, smsc_attach),
1816239275Sgonzo	DEVMETHOD(device_detach, smsc_detach),
1817239275Sgonzo
1818239275Sgonzo	/* bus interface */
1819239275Sgonzo	DEVMETHOD(bus_print_child, bus_generic_print_child),
1820239275Sgonzo	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1821239275Sgonzo
1822239275Sgonzo	/* MII interface */
1823239275Sgonzo	DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1824239275Sgonzo	DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1825239275Sgonzo	DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1826239275Sgonzo
1827246128Ssbz	DEVMETHOD_END
1828239275Sgonzo};
1829239275Sgonzo
1830239275Sgonzostatic driver_t smsc_driver = {
1831239275Sgonzo	.name = "smsc",
1832239275Sgonzo	.methods = smsc_methods,
1833239275Sgonzo	.size = sizeof(struct smsc_softc),
1834239275Sgonzo};
1835239275Sgonzo
1836239275Sgonzostatic devclass_t smsc_devclass;
1837239275Sgonzo
1838239275SgonzoDRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1839239275SgonzoDRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1840239275SgonzoMODULE_DEPEND(smsc, uether, 1, 1, 1);
1841239275SgonzoMODULE_DEPEND(smsc, usb, 1, 1, 1);
1842239275SgonzoMODULE_DEPEND(smsc, ether, 1, 1, 1);
1843239275SgonzoMODULE_DEPEND(smsc, miibus, 1, 1, 1);
1844239275SgonzoMODULE_VERSION(smsc, 1);
1845