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: releng/11.0/sys/dev/usb/net/if_smsc.c 295608 2016-02-14 07:20:07Z hselasky $");
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");
117276701ShselaskySYSCTL_INT(_hw_usb_smsc, OID_AUTO, debug, CTLFLAG_RWTUN, &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) }
126272160Sgavin	SMSC_DEV(LAN89530_ETH, 0),
127272160Sgavin	SMSC_DEV(LAN9500_ETH, 0),
128272160Sgavin	SMSC_DEV(LAN9500_ETH_2, 0),
129272160Sgavin	SMSC_DEV(LAN9500A_ETH, 0),
130272160Sgavin	SMSC_DEV(LAN9500A_ETH_2, 0),
131272160Sgavin	SMSC_DEV(LAN9505_ETH, 0),
132272160Sgavin	SMSC_DEV(LAN9505A_ETH, 0),
133239275Sgonzo	SMSC_DEV(LAN9514_ETH, 0),
134272160Sgavin	SMSC_DEV(LAN9514_ETH_2, 0),
135272160Sgavin	SMSC_DEV(LAN9530_ETH, 0),
136272160Sgavin	SMSC_DEV(LAN9730_ETH, 0),
137272160Sgavin	SMSC_DEV(LAN9500_SAL10, 0),
138272160Sgavin	SMSC_DEV(LAN9505_SAL10, 0),
139272160Sgavin	SMSC_DEV(LAN9500A_SAL10, 0),
140272160Sgavin	SMSC_DEV(LAN9505A_SAL10, 0),
141272160Sgavin	SMSC_DEV(LAN9514_SAL10, 0),
142272160Sgavin	SMSC_DEV(LAN9500A_HAL, 0),
143272160Sgavin	SMSC_DEV(LAN9505A_HAL, 0),
144239275Sgonzo#undef SMSC_DEV
145239275Sgonzo};
146239275Sgonzo
147239275Sgonzo
148239275Sgonzo#ifdef USB_DEBUG
149239275Sgonzo#define smsc_dbg_printf(sc, fmt, args...) \
150239275Sgonzo	do { \
151239275Sgonzo		if (smsc_debug > 0) \
152239275Sgonzo			device_printf((sc)->sc_ue.ue_dev, "debug: " fmt, ##args); \
153239275Sgonzo	} while(0)
154239275Sgonzo#else
155239275Sgonzo#define smsc_dbg_printf(sc, fmt, args...)
156239275Sgonzo#endif
157239275Sgonzo
158239275Sgonzo#define smsc_warn_printf(sc, fmt, args...) \
159239275Sgonzo	device_printf((sc)->sc_ue.ue_dev, "warning: " fmt, ##args)
160239275Sgonzo
161239275Sgonzo#define smsc_err_printf(sc, fmt, args...) \
162239275Sgonzo	device_printf((sc)->sc_ue.ue_dev, "error: " fmt, ##args)
163239275Sgonzo
164239275Sgonzo
165239275Sgonzo#define ETHER_IS_ZERO(addr) \
166239275Sgonzo	(!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
167239275Sgonzo
168239275Sgonzo#define ETHER_IS_VALID(addr) \
169239275Sgonzo	(!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
170239275Sgonzo
171239275Sgonzostatic device_probe_t smsc_probe;
172239275Sgonzostatic device_attach_t smsc_attach;
173239275Sgonzostatic device_detach_t smsc_detach;
174239275Sgonzo
175239275Sgonzostatic usb_callback_t smsc_bulk_read_callback;
176239275Sgonzostatic usb_callback_t smsc_bulk_write_callback;
177239275Sgonzo
178239275Sgonzostatic miibus_readreg_t smsc_miibus_readreg;
179239275Sgonzostatic miibus_writereg_t smsc_miibus_writereg;
180239275Sgonzostatic miibus_statchg_t smsc_miibus_statchg;
181239275Sgonzo
182239275Sgonzo#if __FreeBSD_version > 1000000
183239275Sgonzostatic int smsc_attach_post_sub(struct usb_ether *ue);
184239275Sgonzo#endif
185239275Sgonzostatic uether_fn_t smsc_attach_post;
186239275Sgonzostatic uether_fn_t smsc_init;
187239275Sgonzostatic uether_fn_t smsc_stop;
188239275Sgonzostatic uether_fn_t smsc_start;
189239275Sgonzostatic uether_fn_t smsc_tick;
190239275Sgonzostatic uether_fn_t smsc_setmulti;
191239275Sgonzostatic uether_fn_t smsc_setpromisc;
192239275Sgonzo
193239275Sgonzostatic int	smsc_ifmedia_upd(struct ifnet *);
194239275Sgonzostatic void	smsc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
195239275Sgonzo
196239275Sgonzostatic int smsc_chip_init(struct smsc_softc *sc);
197239275Sgonzostatic int smsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
198239275Sgonzo
199239275Sgonzostatic const struct usb_config smsc_config[SMSC_N_TRANSFER] = {
200239275Sgonzo
201239275Sgonzo	[SMSC_BULK_DT_WR] = {
202239275Sgonzo		.type = UE_BULK,
203239275Sgonzo		.endpoint = UE_ADDR_ANY,
204239275Sgonzo		.direction = UE_DIR_OUT,
205239275Sgonzo		.frames = 16,
206239275Sgonzo		.bufsize = 16 * (MCLBYTES + 16),
207239275Sgonzo		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
208239275Sgonzo		.callback = smsc_bulk_write_callback,
209239275Sgonzo		.timeout = 10000,	/* 10 seconds */
210239275Sgonzo	},
211239275Sgonzo
212239275Sgonzo	[SMSC_BULK_DT_RD] = {
213239275Sgonzo		.type = UE_BULK,
214239275Sgonzo		.endpoint = UE_ADDR_ANY,
215239275Sgonzo		.direction = UE_DIR_IN,
216239275Sgonzo		.bufsize = 20480,	/* bytes */
217239275Sgonzo		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
218239275Sgonzo		.callback = smsc_bulk_read_callback,
219239275Sgonzo		.timeout = 0,	/* no timeout */
220239275Sgonzo	},
221239275Sgonzo
222239275Sgonzo	/* The SMSC chip supports an interrupt endpoints, however they aren't
223239275Sgonzo	 * needed as we poll on the MII status.
224239275Sgonzo	 */
225239275Sgonzo};
226239275Sgonzo
227239275Sgonzostatic const struct usb_ether_methods smsc_ue_methods = {
228239275Sgonzo	.ue_attach_post = smsc_attach_post,
229239275Sgonzo#if __FreeBSD_version > 1000000
230239275Sgonzo	.ue_attach_post_sub = smsc_attach_post_sub,
231239275Sgonzo#endif
232239275Sgonzo	.ue_start = smsc_start,
233239275Sgonzo	.ue_ioctl = smsc_ioctl,
234239275Sgonzo	.ue_init = smsc_init,
235239275Sgonzo	.ue_stop = smsc_stop,
236239275Sgonzo	.ue_tick = smsc_tick,
237239275Sgonzo	.ue_setmulti = smsc_setmulti,
238239275Sgonzo	.ue_setpromisc = smsc_setpromisc,
239239275Sgonzo	.ue_mii_upd = smsc_ifmedia_upd,
240239275Sgonzo	.ue_mii_sts = smsc_ifmedia_sts,
241239275Sgonzo};
242239275Sgonzo
243239275Sgonzo/**
244239275Sgonzo *	smsc_read_reg - Reads a 32-bit register on the device
245239275Sgonzo *	@sc: driver soft context
246239275Sgonzo *	@off: offset of the register
247239275Sgonzo *	@data: pointer a value that will be populated with the register value
248239275Sgonzo *
249239275Sgonzo *	LOCKING:
250239275Sgonzo *	The device lock must be held before calling this function.
251239275Sgonzo *
252239275Sgonzo *	RETURNS:
253239275Sgonzo *	0 on success, a USB_ERR_?? error code on failure.
254239275Sgonzo */
255239275Sgonzostatic int
256239275Sgonzosmsc_read_reg(struct smsc_softc *sc, uint32_t off, uint32_t *data)
257239275Sgonzo{
258239275Sgonzo	struct usb_device_request req;
259239275Sgonzo	uint32_t buf;
260239275Sgonzo	usb_error_t err;
261239275Sgonzo
262239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
263239275Sgonzo
264239275Sgonzo	req.bmRequestType = UT_READ_VENDOR_DEVICE;
265239275Sgonzo	req.bRequest = SMSC_UR_READ_REG;
266239275Sgonzo	USETW(req.wValue, 0);
267239275Sgonzo	USETW(req.wIndex, off);
268239275Sgonzo	USETW(req.wLength, 4);
269239275Sgonzo
270239275Sgonzo	err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
271239275Sgonzo	if (err != 0)
272239275Sgonzo		smsc_warn_printf(sc, "Failed to read register 0x%0x\n", off);
273239275Sgonzo
274239275Sgonzo	*data = le32toh(buf);
275239275Sgonzo
276239275Sgonzo	return (err);
277239275Sgonzo}
278239275Sgonzo
279239275Sgonzo/**
280239275Sgonzo *	smsc_write_reg - Writes a 32-bit register on the device
281239275Sgonzo *	@sc: driver soft context
282239275Sgonzo *	@off: offset of the register
283239275Sgonzo *	@data: the 32-bit value to write into the register
284239275Sgonzo *
285239275Sgonzo *	LOCKING:
286239275Sgonzo *	The device lock must be held before calling this function.
287239275Sgonzo *
288239275Sgonzo *	RETURNS:
289239275Sgonzo *	0 on success, a USB_ERR_?? error code on failure.
290239275Sgonzo */
291239275Sgonzostatic int
292239275Sgonzosmsc_write_reg(struct smsc_softc *sc, uint32_t off, uint32_t data)
293239275Sgonzo{
294239275Sgonzo	struct usb_device_request req;
295239275Sgonzo	uint32_t buf;
296239275Sgonzo	usb_error_t err;
297239275Sgonzo
298239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
299239275Sgonzo
300239275Sgonzo	buf = htole32(data);
301239275Sgonzo
302239275Sgonzo	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
303239275Sgonzo	req.bRequest = SMSC_UR_WRITE_REG;
304239275Sgonzo	USETW(req.wValue, 0);
305239275Sgonzo	USETW(req.wIndex, off);
306239275Sgonzo	USETW(req.wLength, 4);
307239275Sgonzo
308239275Sgonzo	err = uether_do_request(&sc->sc_ue, &req, &buf, 1000);
309239275Sgonzo	if (err != 0)
310239275Sgonzo		smsc_warn_printf(sc, "Failed to write register 0x%0x\n", off);
311239275Sgonzo
312239275Sgonzo	return (err);
313239275Sgonzo}
314239275Sgonzo
315239275Sgonzo/**
316239275Sgonzo *	smsc_wait_for_bits - Polls on a register value until bits are cleared
317239275Sgonzo *	@sc: soft context
318239275Sgonzo *	@reg: offset of the register
319239275Sgonzo *	@bits: if the bits are clear the function returns
320239275Sgonzo *
321239275Sgonzo *	LOCKING:
322239275Sgonzo *	The device lock must be held before calling this function.
323239275Sgonzo *
324239275Sgonzo *	RETURNS:
325239275Sgonzo *	0 on success, or a USB_ERR_?? error code on failure.
326239275Sgonzo */
327239275Sgonzostatic int
328239275Sgonzosmsc_wait_for_bits(struct smsc_softc *sc, uint32_t reg, uint32_t bits)
329239275Sgonzo{
330239275Sgonzo	usb_ticks_t start_ticks;
331240804Shselasky	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
332239275Sgonzo	uint32_t val;
333239275Sgonzo	int err;
334239275Sgonzo
335239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
336239275Sgonzo
337239275Sgonzo	start_ticks = (usb_ticks_t)ticks;
338239275Sgonzo	do {
339239275Sgonzo		if ((err = smsc_read_reg(sc, reg, &val)) != 0)
340239275Sgonzo			return (err);
341239275Sgonzo		if (!(val & bits))
342239275Sgonzo			return (0);
343239275Sgonzo
344239275Sgonzo		uether_pause(&sc->sc_ue, hz / 100);
345240804Shselasky	} while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
346239275Sgonzo
347239275Sgonzo	return (USB_ERR_TIMEOUT);
348239275Sgonzo}
349239275Sgonzo
350239275Sgonzo/**
351239275Sgonzo *	smsc_eeprom_read - Reads the attached EEPROM
352239275Sgonzo *	@sc: soft context
353239275Sgonzo *	@off: the eeprom address offset
354239275Sgonzo *	@buf: stores the bytes
355239275Sgonzo *	@buflen: the number of bytes to read
356239275Sgonzo *
357239275Sgonzo *	Simply reads bytes from an attached eeprom.
358239275Sgonzo *
359239275Sgonzo *	LOCKING:
360239275Sgonzo *	The function takes and releases the device lock if it is not already held.
361239275Sgonzo *
362239275Sgonzo *	RETURNS:
363239275Sgonzo *	0 on success, or a USB_ERR_?? error code on failure.
364239275Sgonzo */
365239275Sgonzostatic int
366239275Sgonzosmsc_eeprom_read(struct smsc_softc *sc, uint16_t off, uint8_t *buf, uint16_t buflen)
367239275Sgonzo{
368239275Sgonzo	usb_ticks_t start_ticks;
369240806Shselasky	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
370239275Sgonzo	int err;
371239275Sgonzo	int locked;
372239275Sgonzo	uint32_t val;
373239275Sgonzo	uint16_t i;
374239275Sgonzo
375239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
376239275Sgonzo	if (!locked)
377239275Sgonzo		SMSC_LOCK(sc);
378239275Sgonzo
379239275Sgonzo	err = smsc_wait_for_bits(sc, SMSC_EEPROM_CMD, SMSC_EEPROM_CMD_BUSY);
380239275Sgonzo	if (err != 0) {
381239275Sgonzo		smsc_warn_printf(sc, "eeprom busy, failed to read data\n");
382239275Sgonzo		goto done;
383239275Sgonzo	}
384239275Sgonzo
385239275Sgonzo	/* start reading the bytes, one at a time */
386239275Sgonzo	for (i = 0; i < buflen; i++) {
387239275Sgonzo
388239275Sgonzo		val = SMSC_EEPROM_CMD_BUSY | (SMSC_EEPROM_CMD_ADDR_MASK & (off + i));
389239275Sgonzo		if ((err = smsc_write_reg(sc, SMSC_EEPROM_CMD, val)) != 0)
390239275Sgonzo			goto done;
391239275Sgonzo
392239275Sgonzo		start_ticks = (usb_ticks_t)ticks;
393239275Sgonzo		do {
394239275Sgonzo			if ((err = smsc_read_reg(sc, SMSC_EEPROM_CMD, &val)) != 0)
395239275Sgonzo				goto done;
396239275Sgonzo			if (!(val & SMSC_EEPROM_CMD_BUSY) || (val & SMSC_EEPROM_CMD_TIMEOUT))
397239275Sgonzo				break;
398239275Sgonzo
399239275Sgonzo			uether_pause(&sc->sc_ue, hz / 100);
400240806Shselasky		} while (((usb_ticks_t)(ticks - start_ticks)) < max_ticks);
401240806Shselasky
402239275Sgonzo		if (val & (SMSC_EEPROM_CMD_BUSY | SMSC_EEPROM_CMD_TIMEOUT)) {
403239275Sgonzo			smsc_warn_printf(sc, "eeprom command failed\n");
404239275Sgonzo			err = USB_ERR_IOERROR;
405239275Sgonzo			break;
406239275Sgonzo		}
407239275Sgonzo
408239275Sgonzo		if ((err = smsc_read_reg(sc, SMSC_EEPROM_DATA, &val)) != 0)
409239275Sgonzo			goto done;
410239275Sgonzo
411239275Sgonzo		buf[i] = (val & 0xff);
412239275Sgonzo	}
413239275Sgonzo
414239275Sgonzodone:
415239275Sgonzo	if (!locked)
416239275Sgonzo		SMSC_UNLOCK(sc);
417239275Sgonzo
418239275Sgonzo	return (err);
419239275Sgonzo}
420239275Sgonzo
421239275Sgonzo/**
422239275Sgonzo *	smsc_miibus_readreg - Reads a MII/MDIO register
423239275Sgonzo *	@dev: usb ether device
424239275Sgonzo *	@phy: the number of phy reading from
425239275Sgonzo *	@reg: the register address
426239275Sgonzo *
427239275Sgonzo *	Attempts to read a phy register over the MII bus.
428239275Sgonzo *
429239275Sgonzo *	LOCKING:
430239275Sgonzo *	Takes and releases the device mutex lock if not already held.
431239275Sgonzo *
432239275Sgonzo *	RETURNS:
433239275Sgonzo *	Returns the 16-bits read from the MII register, if this function fails 0
434239275Sgonzo *	is returned.
435239275Sgonzo */
436239275Sgonzostatic int
437239275Sgonzosmsc_miibus_readreg(device_t dev, int phy, int reg)
438239275Sgonzo{
439239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
440239275Sgonzo	int locked;
441239275Sgonzo	uint32_t addr;
442239275Sgonzo	uint32_t val = 0;
443239275Sgonzo
444239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
445239275Sgonzo	if (!locked)
446239275Sgonzo		SMSC_LOCK(sc);
447239275Sgonzo
448239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
449239275Sgonzo		smsc_warn_printf(sc, "MII is busy\n");
450239275Sgonzo		goto done;
451239275Sgonzo	}
452239275Sgonzo
453239275Sgonzo	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
454239275Sgonzo	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
455239275Sgonzo
456239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
457239275Sgonzo		smsc_warn_printf(sc, "MII read timeout\n");
458239275Sgonzo
459239275Sgonzo	smsc_read_reg(sc, SMSC_MII_DATA, &val);
460239275Sgonzo	val = le32toh(val);
461239275Sgonzo
462239275Sgonzodone:
463239275Sgonzo	if (!locked)
464239275Sgonzo		SMSC_UNLOCK(sc);
465239275Sgonzo
466239275Sgonzo	return (val & 0xFFFF);
467239275Sgonzo}
468239275Sgonzo
469239275Sgonzo/**
470239275Sgonzo *	smsc_miibus_writereg - Writes a MII/MDIO register
471239275Sgonzo *	@dev: usb ether device
472239275Sgonzo *	@phy: the number of phy writing to
473239275Sgonzo *	@reg: the register address
474239275Sgonzo *	@val: the value to write
475239275Sgonzo *
476239275Sgonzo *	Attempts to write a phy register over the MII bus.
477239275Sgonzo *
478239275Sgonzo *	LOCKING:
479239275Sgonzo *	Takes and releases the device mutex lock if not already held.
480239275Sgonzo *
481239275Sgonzo *	RETURNS:
482239275Sgonzo *	Always returns 0 regardless of success or failure.
483239275Sgonzo */
484239275Sgonzostatic int
485239275Sgonzosmsc_miibus_writereg(device_t dev, int phy, int reg, int val)
486239275Sgonzo{
487239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
488239275Sgonzo	int locked;
489239275Sgonzo	uint32_t addr;
490239275Sgonzo
491239275Sgonzo	if (sc->sc_phyno != phy)
492239275Sgonzo		return (0);
493239275Sgonzo
494239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
495239275Sgonzo	if (!locked)
496239275Sgonzo		SMSC_LOCK(sc);
497239275Sgonzo
498239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
499239275Sgonzo		smsc_warn_printf(sc, "MII is busy\n");
500239275Sgonzo		goto done;
501239275Sgonzo	}
502239275Sgonzo
503239275Sgonzo	val = htole32(val);
504239275Sgonzo	smsc_write_reg(sc, SMSC_MII_DATA, val);
505239275Sgonzo
506239275Sgonzo	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
507239275Sgonzo	smsc_write_reg(sc, SMSC_MII_ADDR, addr);
508239275Sgonzo
509239275Sgonzo	if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
510239275Sgonzo		smsc_warn_printf(sc, "MII write timeout\n");
511239275Sgonzo
512239275Sgonzodone:
513239275Sgonzo	if (!locked)
514239275Sgonzo		SMSC_UNLOCK(sc);
515239275Sgonzo	return (0);
516239275Sgonzo}
517239275Sgonzo
518239275Sgonzo
519239275Sgonzo
520239275Sgonzo/**
521239275Sgonzo *	smsc_miibus_statchg - Called to detect phy status change
522239275Sgonzo *	@dev: usb ether device
523239275Sgonzo *
524239275Sgonzo *	This function is called periodically by the system to poll for status
525239275Sgonzo *	changes of the link.
526239275Sgonzo *
527239275Sgonzo *	LOCKING:
528239275Sgonzo *	Takes and releases the device mutex lock if not already held.
529239275Sgonzo */
530239275Sgonzostatic void
531239275Sgonzosmsc_miibus_statchg(device_t dev)
532239275Sgonzo{
533239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
534239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
535239275Sgonzo	struct ifnet *ifp;
536239275Sgonzo	int locked;
537239275Sgonzo	int err;
538239275Sgonzo	uint32_t flow;
539239275Sgonzo	uint32_t afc_cfg;
540239275Sgonzo
541239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
542239275Sgonzo	if (!locked)
543239275Sgonzo		SMSC_LOCK(sc);
544239275Sgonzo
545239275Sgonzo	ifp = uether_getifp(&sc->sc_ue);
546239275Sgonzo	if (mii == NULL || ifp == NULL ||
547239275Sgonzo	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
548239275Sgonzo		goto done;
549239275Sgonzo
550239275Sgonzo	/* Use the MII status to determine link status */
551239275Sgonzo	sc->sc_flags &= ~SMSC_FLAG_LINK;
552239275Sgonzo	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
553239275Sgonzo	    (IFM_ACTIVE | IFM_AVALID)) {
554239275Sgonzo		switch (IFM_SUBTYPE(mii->mii_media_active)) {
555239275Sgonzo			case IFM_10_T:
556239275Sgonzo			case IFM_100_TX:
557239275Sgonzo				sc->sc_flags |= SMSC_FLAG_LINK;
558239275Sgonzo				break;
559239275Sgonzo			case IFM_1000_T:
560239275Sgonzo				/* Gigabit ethernet not supported by chipset */
561239275Sgonzo				break;
562239275Sgonzo			default:
563239275Sgonzo				break;
564239275Sgonzo		}
565239275Sgonzo	}
566239275Sgonzo
567239275Sgonzo	/* Lost link, do nothing. */
568239275Sgonzo	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
569239275Sgonzo		smsc_dbg_printf(sc, "link flag not set\n");
570239275Sgonzo		goto done;
571239275Sgonzo	}
572239275Sgonzo
573239275Sgonzo	err = smsc_read_reg(sc, SMSC_AFC_CFG, &afc_cfg);
574239275Sgonzo	if (err) {
575239275Sgonzo		smsc_warn_printf(sc, "failed to read initial AFC_CFG, error %d\n", err);
576239275Sgonzo		goto done;
577239275Sgonzo	}
578239275Sgonzo
579239275Sgonzo	/* Enable/disable full duplex operation and TX/RX pause */
580239275Sgonzo	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
581239275Sgonzo		smsc_dbg_printf(sc, "full duplex operation\n");
582239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
583239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;
584239275Sgonzo
585239275Sgonzo		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
586239275Sgonzo			flow = 0xffff0002;
587239275Sgonzo		else
588239275Sgonzo			flow = 0;
589239275Sgonzo
590239275Sgonzo		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
591239275Sgonzo			afc_cfg |= 0xf;
592239275Sgonzo		else
593239275Sgonzo			afc_cfg &= ~0xf;
594239275Sgonzo
595239275Sgonzo	} else {
596239275Sgonzo		smsc_dbg_printf(sc, "half duplex operation\n");
597239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
598239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;
599239275Sgonzo
600239275Sgonzo		flow = 0;
601239275Sgonzo		afc_cfg |= 0xf;
602239275Sgonzo	}
603239275Sgonzo
604239275Sgonzo	err = smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
605239275Sgonzo	err += smsc_write_reg(sc, SMSC_FLOW, flow);
606239275Sgonzo	err += smsc_write_reg(sc, SMSC_AFC_CFG, afc_cfg);
607239275Sgonzo	if (err)
608239275Sgonzo		smsc_warn_printf(sc, "media change failed, error %d\n", err);
609239275Sgonzo
610239275Sgonzodone:
611239275Sgonzo	if (!locked)
612239275Sgonzo		SMSC_UNLOCK(sc);
613239275Sgonzo}
614239275Sgonzo
615239275Sgonzo/**
616239275Sgonzo *	smsc_ifmedia_upd - Set media options
617239275Sgonzo *	@ifp: interface pointer
618239275Sgonzo *
619239275Sgonzo *	Basically boilerplate code that simply calls the mii functions to set the
620239275Sgonzo *	media options.
621239275Sgonzo *
622239275Sgonzo *	LOCKING:
623239275Sgonzo *	The device lock must be held before this function is called.
624239275Sgonzo *
625239275Sgonzo *	RETURNS:
626239275Sgonzo *	Returns 0 on success or a negative error code.
627239275Sgonzo */
628239275Sgonzostatic int
629239275Sgonzosmsc_ifmedia_upd(struct ifnet *ifp)
630239275Sgonzo{
631239275Sgonzo	struct smsc_softc *sc = ifp->if_softc;
632239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
633251734Skevlo	struct mii_softc *miisc;
634239275Sgonzo	int err;
635239275Sgonzo
636239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
637239275Sgonzo
638251734Skevlo	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
639251734Skevlo		PHY_RESET(miisc);
640239275Sgonzo	err = mii_mediachg(mii);
641239275Sgonzo	return (err);
642239275Sgonzo}
643239275Sgonzo
644239275Sgonzo/**
645239275Sgonzo *	smsc_ifmedia_sts - Report current media status
646239275Sgonzo *	@ifp: inet interface pointer
647239275Sgonzo *	@ifmr: interface media request
648239275Sgonzo *
649239275Sgonzo *	Basically boilerplate code that simply calls the mii functions to get the
650239275Sgonzo *	media status.
651239275Sgonzo *
652239275Sgonzo *	LOCKING:
653239275Sgonzo *	Internally takes and releases the device lock.
654239275Sgonzo */
655239275Sgonzostatic void
656239275Sgonzosmsc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
657239275Sgonzo{
658239275Sgonzo	struct smsc_softc *sc = ifp->if_softc;
659239275Sgonzo	struct mii_data *mii = uether_getmii(&sc->sc_ue);
660239275Sgonzo
661239275Sgonzo	SMSC_LOCK(sc);
662239275Sgonzo	mii_pollstat(mii);
663239275Sgonzo	ifmr->ifm_active = mii->mii_media_active;
664239275Sgonzo	ifmr->ifm_status = mii->mii_media_status;
665251734Skevlo	SMSC_UNLOCK(sc);
666239275Sgonzo}
667239275Sgonzo
668239275Sgonzo/**
669239275Sgonzo *	smsc_hash - Calculate the hash of a mac address
670239275Sgonzo *	@addr: The mac address to calculate the hash on
671239275Sgonzo *
672239275Sgonzo *	This function is used when configuring a range of m'cast mac addresses to
673239275Sgonzo *	filter on.  The hash of the mac address is put in the device's mac hash
674239275Sgonzo *	table.
675239275Sgonzo *
676239275Sgonzo *	RETURNS:
677239275Sgonzo *	Returns a value from 0-63 value which is the hash of the mac address.
678239275Sgonzo */
679239275Sgonzostatic inline uint32_t
680239275Sgonzosmsc_hash(uint8_t addr[ETHER_ADDR_LEN])
681239275Sgonzo{
682239275Sgonzo	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
683239275Sgonzo}
684239275Sgonzo
685239275Sgonzo/**
686239275Sgonzo *	smsc_setmulti - Setup multicast
687239275Sgonzo *	@ue: usb ethernet device context
688239275Sgonzo *
689239275Sgonzo *	Tells the device to either accept frames with a multicast mac address, a
690239275Sgonzo *	select group of m'cast mac addresses or just the devices mac address.
691239275Sgonzo *
692239275Sgonzo *	LOCKING:
693239275Sgonzo *	Should be called with the SMSC lock held.
694239275Sgonzo */
695239275Sgonzostatic void
696239275Sgonzosmsc_setmulti(struct usb_ether *ue)
697239275Sgonzo{
698239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
699239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
700239275Sgonzo	struct ifmultiaddr *ifma;
701239275Sgonzo	uint32_t hashtbl[2] = { 0, 0 };
702239275Sgonzo	uint32_t hash;
703239275Sgonzo
704239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
705239275Sgonzo
706239275Sgonzo	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
707239275Sgonzo		smsc_dbg_printf(sc, "receive all multicast enabled\n");
708239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
709239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
710239275Sgonzo
711239275Sgonzo	} else {
712239275Sgonzo		/* Take the lock of the mac address list before hashing each of them */
713239275Sgonzo		if_maddr_rlock(ifp);
714239275Sgonzo
715239275Sgonzo		if (!TAILQ_EMPTY(&ifp->if_multiaddrs)) {
716239275Sgonzo			/* We are filtering on a set of address so calculate hashes of each
717239275Sgonzo			 * of the address and set the corresponding bits in the register.
718239275Sgonzo			 */
719239275Sgonzo			sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
720239275Sgonzo			sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
721239275Sgonzo
722239275Sgonzo			TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
723239275Sgonzo				if (ifma->ifma_addr->sa_family != AF_LINK)
724239275Sgonzo					continue;
725239275Sgonzo
726239275Sgonzo				hash = smsc_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
727239275Sgonzo				hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
728239275Sgonzo			}
729239275Sgonzo		} else {
730239275Sgonzo			/* Only receive packets with destination set to our mac address */
731239275Sgonzo			sc->sc_mac_csr &= ~(SMSC_MAC_CSR_MCPAS | SMSC_MAC_CSR_HPFILT);
732239275Sgonzo		}
733239275Sgonzo
734239275Sgonzo		if_maddr_runlock(ifp);
735239275Sgonzo
736239275Sgonzo		/* Debug */
737239275Sgonzo		if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT)
738239275Sgonzo			smsc_dbg_printf(sc, "receive select group of macs\n");
739239275Sgonzo		else
740239275Sgonzo			smsc_dbg_printf(sc, "receive own packets only\n");
741239275Sgonzo	}
742239275Sgonzo
743239275Sgonzo	/* Write the hash table and mac control registers */
744239275Sgonzo	smsc_write_reg(sc, SMSC_HASHH, hashtbl[1]);
745239275Sgonzo	smsc_write_reg(sc, SMSC_HASHL, hashtbl[0]);
746239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
747239275Sgonzo}
748239275Sgonzo
749239275Sgonzo
750239275Sgonzo/**
751239275Sgonzo *	smsc_setpromisc - Enables/disables promiscuous mode
752239275Sgonzo *	@ue: usb ethernet device context
753239275Sgonzo *
754239275Sgonzo *	LOCKING:
755239275Sgonzo *	Should be called with the SMSC lock held.
756239275Sgonzo */
757239275Sgonzostatic void
758239275Sgonzosmsc_setpromisc(struct usb_ether *ue)
759239275Sgonzo{
760239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
761239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
762239275Sgonzo
763239275Sgonzo	smsc_dbg_printf(sc, "promiscuous mode %sabled\n",
764239275Sgonzo	                (ifp->if_flags & IFF_PROMISC) ? "en" : "dis");
765239275Sgonzo
766239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
767239275Sgonzo
768239275Sgonzo	if (ifp->if_flags & IFF_PROMISC)
769239275Sgonzo		sc->sc_mac_csr |= SMSC_MAC_CSR_PRMS;
770239275Sgonzo	else
771239275Sgonzo		sc->sc_mac_csr &= ~SMSC_MAC_CSR_PRMS;
772239275Sgonzo
773239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
774239275Sgonzo}
775239275Sgonzo
776239275Sgonzo
777239275Sgonzo/**
778239275Sgonzo *	smsc_sethwcsum - Enable or disable H/W UDP and TCP checksumming
779239275Sgonzo *	@sc: driver soft context
780239275Sgonzo *
781239275Sgonzo *	LOCKING:
782239275Sgonzo *	Should be called with the SMSC lock held.
783239275Sgonzo *
784239275Sgonzo *	RETURNS:
785239275Sgonzo *	Returns 0 on success or a negative error code.
786239275Sgonzo */
787239275Sgonzostatic int smsc_sethwcsum(struct smsc_softc *sc)
788239275Sgonzo{
789239275Sgonzo	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
790239275Sgonzo	uint32_t val;
791239275Sgonzo	int err;
792239275Sgonzo
793239275Sgonzo	if (!ifp)
794239275Sgonzo		return (-EIO);
795239275Sgonzo
796239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
797239275Sgonzo
798239275Sgonzo	err = smsc_read_reg(sc, SMSC_COE_CTRL, &val);
799239275Sgonzo	if (err != 0) {
800239275Sgonzo		smsc_warn_printf(sc, "failed to read SMSC_COE_CTRL (err=%d)\n", err);
801239275Sgonzo		return (err);
802239275Sgonzo	}
803239275Sgonzo
804239275Sgonzo	/* Enable/disable the Rx checksum */
805239275Sgonzo	if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_RXCSUM)
806239275Sgonzo		val |= SMSC_COE_CTRL_RX_EN;
807239275Sgonzo	else
808239275Sgonzo		val &= ~SMSC_COE_CTRL_RX_EN;
809239275Sgonzo
810239275Sgonzo	/* Enable/disable the Tx checksum (currently not supported) */
811239275Sgonzo	if ((ifp->if_capabilities & ifp->if_capenable) & IFCAP_TXCSUM)
812239275Sgonzo		val |= SMSC_COE_CTRL_TX_EN;
813239275Sgonzo	else
814239275Sgonzo		val &= ~SMSC_COE_CTRL_TX_EN;
815239275Sgonzo
816239275Sgonzo	err = smsc_write_reg(sc, SMSC_COE_CTRL, val);
817239275Sgonzo	if (err != 0) {
818239275Sgonzo		smsc_warn_printf(sc, "failed to write SMSC_COE_CTRL (err=%d)\n", err);
819239275Sgonzo		return (err);
820239275Sgonzo	}
821239275Sgonzo
822239275Sgonzo	return (0);
823239275Sgonzo}
824239275Sgonzo
825239275Sgonzo
826239275Sgonzo/**
827239275Sgonzo *	smsc_setmacaddress - Sets the mac address in the device
828239275Sgonzo *	@sc: driver soft context
829239275Sgonzo *	@addr: pointer to array contain at least 6 bytes of the mac
830239275Sgonzo *
831239275Sgonzo *	Writes the MAC address into the device, usually the MAC is programmed with
832239275Sgonzo *	values from the EEPROM.
833239275Sgonzo *
834239275Sgonzo *	LOCKING:
835239275Sgonzo *	Should be called with the SMSC lock held.
836239275Sgonzo *
837239275Sgonzo *	RETURNS:
838239275Sgonzo *	Returns 0 on success or a negative error code.
839239275Sgonzo */
840239275Sgonzostatic int
841239275Sgonzosmsc_setmacaddress(struct smsc_softc *sc, const uint8_t *addr)
842239275Sgonzo{
843239275Sgonzo	int err;
844239275Sgonzo	uint32_t val;
845239275Sgonzo
846239275Sgonzo	smsc_dbg_printf(sc, "setting mac address to %02x:%02x:%02x:%02x:%02x:%02x\n",
847239275Sgonzo	                addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
848239275Sgonzo
849239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
850239275Sgonzo
851239275Sgonzo	val = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
852239275Sgonzo	if ((err = smsc_write_reg(sc, SMSC_MAC_ADDRL, val)) != 0)
853239275Sgonzo		goto done;
854239275Sgonzo
855239275Sgonzo	val = (addr[5] << 8) | addr[4];
856239275Sgonzo	err = smsc_write_reg(sc, SMSC_MAC_ADDRH, val);
857239275Sgonzo
858239275Sgonzodone:
859239275Sgonzo	return (err);
860239275Sgonzo}
861239275Sgonzo
862239275Sgonzo/**
863239275Sgonzo *	smsc_reset - Reset the SMSC chip
864239275Sgonzo *	@sc: device soft context
865239275Sgonzo *
866239275Sgonzo *	LOCKING:
867239275Sgonzo *	Should be called with the SMSC lock held.
868239275Sgonzo */
869239275Sgonzostatic void
870239275Sgonzosmsc_reset(struct smsc_softc *sc)
871239275Sgonzo{
872239275Sgonzo	struct usb_config_descriptor *cd;
873239275Sgonzo	usb_error_t err;
874239275Sgonzo
875239275Sgonzo	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
876239275Sgonzo
877239275Sgonzo	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
878239275Sgonzo	                          cd->bConfigurationValue);
879239275Sgonzo	if (err)
880239275Sgonzo		smsc_warn_printf(sc, "reset failed (ignored)\n");
881239275Sgonzo
882239275Sgonzo	/* Wait a little while for the chip to get its brains in order. */
883239275Sgonzo	uether_pause(&sc->sc_ue, hz / 100);
884239275Sgonzo
885239275Sgonzo	/* Reinitialize controller to achieve full reset. */
886239275Sgonzo	smsc_chip_init(sc);
887239275Sgonzo}
888239275Sgonzo
889239275Sgonzo
890239275Sgonzo/**
891239275Sgonzo *	smsc_init - Initialises the LAN95xx chip
892239275Sgonzo *	@ue: USB ether interface
893239275Sgonzo *
894239275Sgonzo *	Called when the interface is brought up (i.e. ifconfig ue0 up), this
895239275Sgonzo *	initialise the interface and the rx/tx pipes.
896239275Sgonzo *
897239275Sgonzo *	LOCKING:
898239275Sgonzo *	Should be called with the SMSC lock held.
899239275Sgonzo */
900239275Sgonzostatic void
901239275Sgonzosmsc_init(struct usb_ether *ue)
902239275Sgonzo{
903239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
904239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
905239275Sgonzo
906239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
907239275Sgonzo
908239275Sgonzo	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
909239275Sgonzo		return;
910239275Sgonzo
911239275Sgonzo	/* Cancel pending I/O */
912239275Sgonzo	smsc_stop(ue);
913239275Sgonzo
914239275Sgonzo#if __FreeBSD_version <= 1000000
915239275Sgonzo	/* On earlier versions this was the first place we could tell the system
916239275Sgonzo	 * that we supported h/w csuming, however this is only called after the
917239275Sgonzo	 * the interface has been brought up - not ideal.
918239275Sgonzo	 */
919239275Sgonzo	if (!(ifp->if_capabilities & IFCAP_RXCSUM)) {
920239275Sgonzo		ifp->if_capabilities |= IFCAP_RXCSUM;
921239275Sgonzo		ifp->if_capenable |= IFCAP_RXCSUM;
922239275Sgonzo		ifp->if_hwassist = 0;
923239275Sgonzo	}
924239275Sgonzo
925239275Sgonzo	/* TX checksuming is disabled for now
926239275Sgonzo	ifp->if_capabilities |= IFCAP_TXCSUM;
927239275Sgonzo	ifp->if_capenable |= IFCAP_TXCSUM;
928239275Sgonzo	ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
929239275Sgonzo	*/
930239275Sgonzo#endif
931239275Sgonzo
932239275Sgonzo	/* Reset the ethernet interface. */
933239275Sgonzo	smsc_reset(sc);
934239275Sgonzo
935239275Sgonzo	/* Load the multicast filter. */
936239275Sgonzo	smsc_setmulti(ue);
937239275Sgonzo
938239275Sgonzo	/* TCP/UDP checksum offload engines. */
939239275Sgonzo	smsc_sethwcsum(sc);
940239275Sgonzo
941239275Sgonzo	usbd_xfer_set_stall(sc->sc_xfer[SMSC_BULK_DT_WR]);
942239275Sgonzo
943239275Sgonzo	/* Indicate we are up and running. */
944239275Sgonzo	ifp->if_drv_flags |= IFF_DRV_RUNNING;
945239275Sgonzo
946239275Sgonzo	/* Switch to selected media. */
947239275Sgonzo	smsc_ifmedia_upd(ifp);
948239275Sgonzo	smsc_start(ue);
949239275Sgonzo}
950239275Sgonzo
951239275Sgonzo/**
952239275Sgonzo *	smsc_bulk_read_callback - Read callback used to process the USB URB
953239275Sgonzo *	@xfer: the USB transfer
954239275Sgonzo *	@error:
955239275Sgonzo *
956239275Sgonzo *	Reads the URB data which can contain one or more ethernet frames, the
957239275Sgonzo *	frames are copyed into a mbuf and given to the system.
958239275Sgonzo *
959239275Sgonzo *	LOCKING:
960239275Sgonzo *	No locking required, doesn't access internal driver settings.
961239275Sgonzo */
962239275Sgonzostatic void
963239275Sgonzosmsc_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
964239275Sgonzo{
965239275Sgonzo	struct smsc_softc *sc = usbd_xfer_softc(xfer);
966239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
967239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
968239275Sgonzo	struct mbuf *m;
969239275Sgonzo	struct usb_page_cache *pc;
970239275Sgonzo	uint32_t rxhdr;
971239275Sgonzo	uint16_t pktlen;
972239275Sgonzo	int off;
973239275Sgonzo	int actlen;
974239275Sgonzo
975239275Sgonzo	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
976239275Sgonzo	smsc_dbg_printf(sc, "rx : actlen %d\n", actlen);
977239275Sgonzo
978239275Sgonzo	switch (USB_GET_STATE(xfer)) {
979239275Sgonzo	case USB_ST_TRANSFERRED:
980239275Sgonzo
981239275Sgonzo		/* There is always a zero length frame after bringing the IF up */
982239275Sgonzo		if (actlen < (sizeof(rxhdr) + ETHER_CRC_LEN))
983239275Sgonzo			goto tr_setup;
984239275Sgonzo
985239275Sgonzo		/* There maybe multiple packets in the USB frame, each will have a
986239275Sgonzo		 * header and each needs to have it's own mbuf allocated and populated
987239275Sgonzo		 * for it.
988239275Sgonzo		 */
989239275Sgonzo		pc = usbd_xfer_get_frame(xfer, 0);
990239275Sgonzo		off = 0;
991239275Sgonzo
992239275Sgonzo		while (off < actlen) {
993239275Sgonzo
994239275Sgonzo			/* The frame header is always aligned on a 4 byte boundary */
995239275Sgonzo			off = ((off + 0x3) & ~0x3);
996239275Sgonzo
997239275Sgonzo			usbd_copy_out(pc, off, &rxhdr, sizeof(rxhdr));
998239275Sgonzo			off += (sizeof(rxhdr) + ETHER_ALIGN);
999239275Sgonzo			rxhdr = le32toh(rxhdr);
1000239275Sgonzo
1001239275Sgonzo			pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
1002239275Sgonzo
1003239275Sgonzo			smsc_dbg_printf(sc, "rx : rxhdr 0x%08x : pktlen %d : actlen %d : "
1004239275Sgonzo			                "off %d\n", rxhdr, pktlen, actlen, off);
1005239275Sgonzo
1006239275Sgonzo
1007239275Sgonzo			if (rxhdr & SMSC_RX_STAT_ERROR) {
1008239275Sgonzo				smsc_dbg_printf(sc, "rx error (hdr 0x%08x)\n", rxhdr);
1009271832Sglebius				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1010239275Sgonzo				if (rxhdr & SMSC_RX_STAT_COLLISION)
1011271832Sglebius					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1012239275Sgonzo			} else {
1013239275Sgonzo
1014239275Sgonzo				/* Check if the ethernet frame is too big or too small */
1015239275Sgonzo				if ((pktlen < ETHER_HDR_LEN) || (pktlen > (actlen - off)))
1016239275Sgonzo					goto tr_setup;
1017239275Sgonzo
1018239275Sgonzo				/* Create a new mbuf to store the packet in */
1019239275Sgonzo				m = uether_newbuf();
1020239275Sgonzo				if (m == NULL) {
1021239275Sgonzo					smsc_warn_printf(sc, "failed to create new mbuf\n");
1022271832Sglebius					if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1023239275Sgonzo					goto tr_setup;
1024239275Sgonzo				}
1025239275Sgonzo
1026239275Sgonzo				usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
1027239275Sgonzo
1028239275Sgonzo				/* Check if RX TCP/UDP checksumming is being offloaded */
1029239275Sgonzo				if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1030246196Shselasky
1031246196Shselasky					struct ether_header *eh;
1032246196Shselasky
1033246196Shselasky					eh = mtod(m, struct ether_header *);
1034239275Sgonzo
1035239275Sgonzo					/* Remove the extra 2 bytes of the csum */
1036239275Sgonzo					pktlen -= 2;
1037239275Sgonzo
1038239275Sgonzo					/* The checksum appears to be simplistically calculated
1039239275Sgonzo					 * over the udp/tcp header and data up to the end of the
1040239275Sgonzo					 * eth frame.  Which means if the eth frame is padded
1041239275Sgonzo					 * the csum calculation is incorrectly performed over
1042239275Sgonzo					 * the padding bytes as well. Therefore to be safe we
1043239275Sgonzo					 * ignore the H/W csum on frames less than or equal to
1044239275Sgonzo					 * 64 bytes.
1045246196Shselasky					 *
1046246196Shselasky					 * Ignore H/W csum for non-IPv4 packets.
1047239275Sgonzo					 */
1048265371Stuexen					if ((be16toh(eh->ether_type) == ETHERTYPE_IP) &&
1049265371Stuexen					    (pktlen > ETHER_MIN_LEN)) {
1050265371Stuexen						struct ip *ip;
1051265371Stuexen
1052265371Stuexen						ip = (struct ip *)(eh + 1);
1053265371Stuexen						if ((ip->ip_v == IPVERSION) &&
1054265371Stuexen						    ((ip->ip_p == IPPROTO_TCP) ||
1055265371Stuexen						     (ip->ip_p == IPPROTO_UDP))) {
1056265371Stuexen							/* Indicate the UDP/TCP csum has been calculated */
1057265371Stuexen							m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1058265371Stuexen
1059265371Stuexen							/* Copy the TCP/UDP checksum from the last 2 bytes
1060265371Stuexen							 * of the transfer and put in the csum_data field.
1061265371Stuexen							 */
1062265371Stuexen							usbd_copy_out(pc, (off + pktlen),
1063265371Stuexen							              &m->m_pkthdr.csum_data, 2);
1064265371Stuexen
1065265371Stuexen							/* The data is copied in network order, but the
1066265371Stuexen							 * csum algorithm in the kernel expects it to be
1067265371Stuexen							 * in host network order.
1068265371Stuexen							 */
1069265371Stuexen							m->m_pkthdr.csum_data = ntohs(m->m_pkthdr.csum_data);
1070265371Stuexen
1071265371Stuexen							smsc_dbg_printf(sc, "RX checksum offloaded (0x%04x)\n",
1072265371Stuexen							                m->m_pkthdr.csum_data);
1073265371Stuexen						}
1074239275Sgonzo					}
1075239275Sgonzo
1076239275Sgonzo					/* Need to adjust the offset as well or we'll be off
1077239275Sgonzo					 * by 2 because the csum is removed from the packet
1078239275Sgonzo					 * length.
1079239275Sgonzo					 */
1080239275Sgonzo					off += 2;
1081239275Sgonzo				}
1082239275Sgonzo
1083239275Sgonzo				/* Finally enqueue the mbuf on the receive queue */
1084241033Shselasky				/* Remove 4 trailing bytes */
1085241034Shselasky				if (pktlen < (4 + ETHER_HDR_LEN)) {
1086241034Shselasky					m_freem(m);
1087241034Shselasky					goto tr_setup;
1088241034Shselasky				}
1089241034Shselasky				uether_rxmbuf(ue, m, pktlen - 4);
1090239275Sgonzo			}
1091239275Sgonzo
1092239275Sgonzo			/* Update the offset to move to the next potential packet */
1093239275Sgonzo			off += pktlen;
1094239275Sgonzo		}
1095239275Sgonzo
1096239275Sgonzo		/* FALLTHROUGH */
1097239275Sgonzo
1098239275Sgonzo	case USB_ST_SETUP:
1099239275Sgonzotr_setup:
1100239275Sgonzo		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1101239275Sgonzo		usbd_transfer_submit(xfer);
1102239275Sgonzo		uether_rxflush(ue);
1103239275Sgonzo		return;
1104239275Sgonzo
1105239275Sgonzo	default:
1106239275Sgonzo		if (error != USB_ERR_CANCELLED) {
1107239275Sgonzo			smsc_warn_printf(sc, "bulk read error, %s\n", usbd_errstr(error));
1108239275Sgonzo			usbd_xfer_set_stall(xfer);
1109239275Sgonzo			goto tr_setup;
1110239275Sgonzo		}
1111239275Sgonzo		return;
1112239275Sgonzo	}
1113239275Sgonzo}
1114239275Sgonzo
1115239275Sgonzo/**
1116239275Sgonzo *	smsc_bulk_write_callback - Write callback used to send ethernet frame(s)
1117239275Sgonzo *	@xfer: the USB transfer
1118239275Sgonzo *	@error: error code if the transfers is in an errored state
1119239275Sgonzo *
1120239275Sgonzo *	The main write function that pulls ethernet frames off the queue and sends
1121239275Sgonzo *	them out.
1122239275Sgonzo *
1123239275Sgonzo *	LOCKING:
1124239275Sgonzo *
1125239275Sgonzo */
1126239275Sgonzostatic void
1127239275Sgonzosmsc_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1128239275Sgonzo{
1129239275Sgonzo	struct smsc_softc *sc = usbd_xfer_softc(xfer);
1130239275Sgonzo	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1131239275Sgonzo	struct usb_page_cache *pc;
1132239275Sgonzo	struct mbuf *m;
1133239275Sgonzo	uint32_t txhdr;
1134239275Sgonzo	uint32_t frm_len = 0;
1135239275Sgonzo	int nframes;
1136239275Sgonzo
1137239275Sgonzo	switch (USB_GET_STATE(xfer)) {
1138239275Sgonzo	case USB_ST_TRANSFERRED:
1139239275Sgonzo		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1140239275Sgonzo		/* FALLTHROUGH */
1141239275Sgonzo
1142239275Sgonzo	case USB_ST_SETUP:
1143239275Sgonzotr_setup:
1144239275Sgonzo		if ((sc->sc_flags & SMSC_FLAG_LINK) == 0 ||
1145239275Sgonzo			(ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1146239275Sgonzo			/* Don't send anything if there is no link or controller is busy. */
1147239275Sgonzo			return;
1148239275Sgonzo		}
1149239275Sgonzo
1150239275Sgonzo		for (nframes = 0; nframes < 16 &&
1151239275Sgonzo		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1152239275Sgonzo			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1153239275Sgonzo			if (m == NULL)
1154239275Sgonzo				break;
1155239275Sgonzo			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1156239275Sgonzo			    nframes);
1157239275Sgonzo			frm_len = 0;
1158239275Sgonzo			pc = usbd_xfer_get_frame(xfer, nframes);
1159239275Sgonzo
1160239275Sgonzo			/* Each frame is prefixed with two 32-bit values describing the
1161239275Sgonzo			 * length of the packet and buffer.
1162239275Sgonzo			 */
1163239275Sgonzo			txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
1164239275Sgonzo					SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
1165239275Sgonzo			txhdr = htole32(txhdr);
1166239275Sgonzo			usbd_copy_in(pc, 0, &txhdr, sizeof(txhdr));
1167239275Sgonzo
1168239275Sgonzo			txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
1169239275Sgonzo			txhdr = htole32(txhdr);
1170239275Sgonzo			usbd_copy_in(pc, 4, &txhdr, sizeof(txhdr));
1171239275Sgonzo
1172239275Sgonzo			frm_len += 8;
1173239275Sgonzo
1174239275Sgonzo			/* Next copy in the actual packet */
1175239275Sgonzo			usbd_m_copy_in(pc, frm_len, m, 0, m->m_pkthdr.len);
1176239275Sgonzo			frm_len += m->m_pkthdr.len;
1177239275Sgonzo
1178271832Sglebius			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1179239275Sgonzo
1180239275Sgonzo			/* If there's a BPF listener, bounce a copy of this frame to him */
1181239275Sgonzo			BPF_MTAP(ifp, m);
1182239275Sgonzo
1183239275Sgonzo			m_freem(m);
1184239275Sgonzo
1185239275Sgonzo			/* Set frame length. */
1186239275Sgonzo			usbd_xfer_set_frame_len(xfer, nframes, frm_len);
1187239275Sgonzo		}
1188239275Sgonzo		if (nframes != 0) {
1189239275Sgonzo			usbd_xfer_set_frames(xfer, nframes);
1190239275Sgonzo			usbd_transfer_submit(xfer);
1191239275Sgonzo			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1192239275Sgonzo		}
1193239275Sgonzo		return;
1194239275Sgonzo
1195239275Sgonzo	default:
1196271832Sglebius		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1197239275Sgonzo		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1198239275Sgonzo
1199239275Sgonzo		if (error != USB_ERR_CANCELLED) {
1200239275Sgonzo			smsc_err_printf(sc, "usb error on tx: %s\n", usbd_errstr(error));
1201239275Sgonzo			usbd_xfer_set_stall(xfer);
1202239275Sgonzo			goto tr_setup;
1203239275Sgonzo		}
1204239275Sgonzo		return;
1205239275Sgonzo	}
1206239275Sgonzo}
1207239275Sgonzo
1208239275Sgonzo/**
1209239275Sgonzo *	smsc_tick - Called periodically to monitor the state of the LAN95xx chip
1210239275Sgonzo *	@ue: USB ether interface
1211239275Sgonzo *
1212239275Sgonzo *	Simply calls the mii status functions to check the state of the link.
1213239275Sgonzo *
1214239275Sgonzo *	LOCKING:
1215239275Sgonzo *	Should be called with the SMSC lock held.
1216239275Sgonzo */
1217239275Sgonzostatic void
1218239275Sgonzosmsc_tick(struct usb_ether *ue)
1219239275Sgonzo{
1220239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1221241844Seadler	struct mii_data *mii = uether_getmii(&sc->sc_ue);
1222239275Sgonzo
1223239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1224239275Sgonzo
1225239275Sgonzo	mii_tick(mii);
1226239275Sgonzo	if ((sc->sc_flags & SMSC_FLAG_LINK) == 0) {
1227239275Sgonzo		smsc_miibus_statchg(ue->ue_dev);
1228239275Sgonzo		if ((sc->sc_flags & SMSC_FLAG_LINK) != 0)
1229239275Sgonzo			smsc_start(ue);
1230239275Sgonzo	}
1231239275Sgonzo}
1232239275Sgonzo
1233239275Sgonzo/**
1234239275Sgonzo *	smsc_start - Starts communication with the LAN95xx chip
1235239275Sgonzo *	@ue: USB ether interface
1236239275Sgonzo *
1237239275Sgonzo *
1238239275Sgonzo *
1239239275Sgonzo */
1240239275Sgonzostatic void
1241239275Sgonzosmsc_start(struct usb_ether *ue)
1242239275Sgonzo{
1243239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1244239275Sgonzo
1245239275Sgonzo	/*
1246239275Sgonzo	 * start the USB transfers, if not already started:
1247239275Sgonzo	 */
1248239275Sgonzo	usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_RD]);
1249239275Sgonzo	usbd_transfer_start(sc->sc_xfer[SMSC_BULK_DT_WR]);
1250239275Sgonzo}
1251239275Sgonzo
1252239275Sgonzo/**
1253239275Sgonzo *	smsc_stop - Stops communication with the LAN95xx chip
1254239275Sgonzo *	@ue: USB ether interface
1255239275Sgonzo *
1256239275Sgonzo *
1257239275Sgonzo *
1258239275Sgonzo */
1259239275Sgonzostatic void
1260239275Sgonzosmsc_stop(struct usb_ether *ue)
1261239275Sgonzo{
1262239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1263239275Sgonzo	struct ifnet *ifp = uether_getifp(ue);
1264239275Sgonzo
1265239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1266239275Sgonzo
1267239275Sgonzo	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1268239275Sgonzo	sc->sc_flags &= ~SMSC_FLAG_LINK;
1269239275Sgonzo
1270239275Sgonzo	/*
1271239275Sgonzo	 * stop all the transfers, if not already stopped:
1272239275Sgonzo	 */
1273239275Sgonzo	usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_WR]);
1274239275Sgonzo	usbd_transfer_stop(sc->sc_xfer[SMSC_BULK_DT_RD]);
1275239275Sgonzo}
1276239275Sgonzo
1277239275Sgonzo/**
1278239275Sgonzo *	smsc_phy_init - Initialises the in-built SMSC phy
1279239275Sgonzo *	@sc: driver soft context
1280239275Sgonzo *
1281239275Sgonzo *	Resets the PHY part of the chip and then initialises it to default
1282239275Sgonzo *	values.  The 'link down' and 'auto-negotiation complete' interrupts
1283239275Sgonzo *	from the PHY are also enabled, however we don't monitor the interrupt
1284239275Sgonzo *	endpoints for the moment.
1285239275Sgonzo *
1286239275Sgonzo *	RETURNS:
1287239275Sgonzo *	Returns 0 on success or EIO if failed to reset the PHY.
1288239275Sgonzo */
1289239275Sgonzostatic int
1290239275Sgonzosmsc_phy_init(struct smsc_softc *sc)
1291239275Sgonzo{
1292239275Sgonzo	int bmcr;
1293239275Sgonzo	usb_ticks_t start_ticks;
1294240806Shselasky	const usb_ticks_t max_ticks = USB_MS_TO_TICKS(1000);
1295239275Sgonzo
1296239275Sgonzo	SMSC_LOCK_ASSERT(sc, MA_OWNED);
1297239275Sgonzo
1298239275Sgonzo	/* Reset phy and wait for reset to complete */
1299239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, BMCR_RESET);
1300239275Sgonzo
1301239275Sgonzo	start_ticks = ticks;
1302239275Sgonzo	do {
1303239275Sgonzo		uether_pause(&sc->sc_ue, hz / 100);
1304239275Sgonzo		bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1305239275Sgonzo	} while ((bmcr & MII_BMCR) && ((ticks - start_ticks) < max_ticks));
1306239275Sgonzo
1307240806Shselasky	if (((usb_ticks_t)(ticks - start_ticks)) >= max_ticks) {
1308239275Sgonzo		smsc_err_printf(sc, "PHY reset timed-out");
1309239275Sgonzo		return (EIO);
1310239275Sgonzo	}
1311239275Sgonzo
1312239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_ANAR,
1313239275Sgonzo	                     ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD |  /* all modes */
1314239275Sgonzo	                     ANAR_CSMA |
1315239275Sgonzo	                     ANAR_FC |
1316239275Sgonzo	                     ANAR_PAUSE_ASYM);
1317239275Sgonzo
1318239275Sgonzo	/* Setup the phy to interrupt when the link goes down or autoneg completes */
1319239275Sgonzo	smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_STAT);
1320239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, SMSC_PHY_INTR_MASK,
1321239275Sgonzo	                     (SMSC_PHY_INTR_ANEG_COMP | SMSC_PHY_INTR_LINK_DOWN));
1322239275Sgonzo
1323239275Sgonzo	/* Restart auto-negotation */
1324239275Sgonzo	bmcr = smsc_miibus_readreg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR);
1325239275Sgonzo	bmcr |= BMCR_STARTNEG;
1326239275Sgonzo	smsc_miibus_writereg(sc->sc_ue.ue_dev, sc->sc_phyno, MII_BMCR, bmcr);
1327239275Sgonzo
1328239275Sgonzo	return (0);
1329239275Sgonzo}
1330239275Sgonzo
1331239275Sgonzo
1332239275Sgonzo/**
1333239275Sgonzo *	smsc_chip_init - Initialises the chip after power on
1334239275Sgonzo *	@sc: driver soft context
1335239275Sgonzo *
1336239275Sgonzo *	This initialisation sequence is modelled on the procedure in the Linux
1337239275Sgonzo *	driver.
1338239275Sgonzo *
1339239275Sgonzo *	RETURNS:
1340239275Sgonzo *	Returns 0 on success or an error code on failure.
1341239275Sgonzo */
1342239275Sgonzostatic int
1343239275Sgonzosmsc_chip_init(struct smsc_softc *sc)
1344239275Sgonzo{
1345239275Sgonzo	int err;
1346239275Sgonzo	int locked;
1347239275Sgonzo	uint32_t reg_val;
1348239275Sgonzo	int burst_cap;
1349239275Sgonzo
1350239275Sgonzo	locked = mtx_owned(&sc->sc_mtx);
1351239275Sgonzo	if (!locked)
1352239275Sgonzo		SMSC_LOCK(sc);
1353239275Sgonzo
1354239275Sgonzo	/* Enter H/W config mode */
1355239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST);
1356239275Sgonzo
1357239275Sgonzo	if ((err = smsc_wait_for_bits(sc, SMSC_HW_CFG, SMSC_HW_CFG_LRST)) != 0) {
1358239275Sgonzo		smsc_warn_printf(sc, "timed-out waiting for reset to complete\n");
1359239275Sgonzo		goto init_failed;
1360239275Sgonzo	}
1361239275Sgonzo
1362239275Sgonzo	/* Reset the PHY */
1363239275Sgonzo	smsc_write_reg(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);
1364239275Sgonzo
1365295608Shselasky	if ((err = smsc_wait_for_bits(sc, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST)) != 0) {
1366239275Sgonzo		smsc_warn_printf(sc, "timed-out waiting for phy reset to complete\n");
1367239275Sgonzo		goto init_failed;
1368239275Sgonzo	}
1369239275Sgonzo
1370239275Sgonzo	/* Set the mac address */
1371239275Sgonzo	if ((err = smsc_setmacaddress(sc, sc->sc_ue.ue_eaddr)) != 0) {
1372239275Sgonzo		smsc_warn_printf(sc, "failed to set the MAC address\n");
1373239275Sgonzo		goto init_failed;
1374239275Sgonzo	}
1375239275Sgonzo
1376239275Sgonzo	/* Don't know what the HW_CFG_BIR bit is, but following the reset sequence
1377239275Sgonzo	 * as used in the Linux driver.
1378239275Sgonzo	 */
1379239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) != 0) {
1380239275Sgonzo		smsc_warn_printf(sc, "failed to read HW_CFG: %d\n", err);
1381239275Sgonzo		goto init_failed;
1382239275Sgonzo	}
1383239275Sgonzo	reg_val |= SMSC_HW_CFG_BIR;
1384239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1385239275Sgonzo
1386239275Sgonzo	/* There is a so called 'turbo mode' that the linux driver supports, it
1387239275Sgonzo	 * seems to allow you to jam multiple frames per Rx transaction.  By default
1388239275Sgonzo	 * this driver supports that and therefore allows multiple frames per URB.
1389239275Sgonzo	 *
1390239275Sgonzo	 * The xfer buffer size needs to reflect this as well, therefore based on
1391239275Sgonzo	 * the calculations in the Linux driver the RX bufsize is set to 18944,
1392239275Sgonzo	 *     bufsz = (16 * 1024 + 5 * 512)
1393239275Sgonzo	 *
1394239275Sgonzo	 * Burst capability is the number of URBs that can be in a burst of data/
1395239275Sgonzo	 * ethernet frames.
1396239275Sgonzo	 */
1397239275Sgonzo	if (usbd_get_speed(sc->sc_ue.ue_udev) == USB_SPEED_HIGH)
1398239275Sgonzo		burst_cap = 37;
1399239275Sgonzo	else
1400239275Sgonzo		burst_cap = 128;
1401239275Sgonzo
1402239275Sgonzo	smsc_write_reg(sc, SMSC_BURST_CAP, burst_cap);
1403239275Sgonzo
1404239275Sgonzo	/* Set the default bulk in delay (magic value from Linux driver) */
1405239275Sgonzo	smsc_write_reg(sc, SMSC_BULK_IN_DLY, 0x00002000);
1406239275Sgonzo
1407239275Sgonzo
1408239275Sgonzo
1409239275Sgonzo	/*
1410239275Sgonzo	 * Initialise the RX interface
1411239275Sgonzo	 */
1412239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_HW_CFG, &reg_val)) < 0) {
1413239275Sgonzo		smsc_warn_printf(sc, "failed to read HW_CFG: (err = %d)\n", err);
1414239275Sgonzo		goto init_failed;
1415239275Sgonzo	}
1416239275Sgonzo
1417239275Sgonzo	/* Adjust the packet offset in the buffer (designed to try and align IP
1418239275Sgonzo	 * header on 4 byte boundary)
1419239275Sgonzo	 */
1420239275Sgonzo	reg_val &= ~SMSC_HW_CFG_RXDOFF;
1421239275Sgonzo	reg_val |= (ETHER_ALIGN << 9) & SMSC_HW_CFG_RXDOFF;
1422239275Sgonzo
1423239275Sgonzo	/* The following setings are used for 'turbo mode', a.k.a multiple frames
1424239275Sgonzo	 * per Rx transaction (again info taken form Linux driver).
1425239275Sgonzo	 */
1426239275Sgonzo	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);
1427239275Sgonzo
1428239275Sgonzo	smsc_write_reg(sc, SMSC_HW_CFG, reg_val);
1429239275Sgonzo
1430239275Sgonzo	/* Clear the status register ? */
1431239275Sgonzo	smsc_write_reg(sc, SMSC_INTR_STATUS, 0xffffffff);
1432239275Sgonzo
1433239275Sgonzo	/* Read and display the revision register */
1434239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
1435239275Sgonzo		smsc_warn_printf(sc, "failed to read ID_REV (err = %d)\n", err);
1436239275Sgonzo		goto init_failed;
1437239275Sgonzo	}
1438239275Sgonzo
1439239275Sgonzo	device_printf(sc->sc_ue.ue_dev, "chip 0x%04lx, rev. %04lx\n",
1440239275Sgonzo	    (sc->sc_rev_id & SMSC_ID_REV_CHIP_ID_MASK) >> 16,
1441239275Sgonzo	    (sc->sc_rev_id & SMSC_ID_REV_CHIP_REV_MASK));
1442239275Sgonzo
1443239275Sgonzo	/* GPIO/LED setup */
1444239275Sgonzo	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
1445239275Sgonzo	          SMSC_LED_GPIO_CFG_FDX_LED;
1446239275Sgonzo	smsc_write_reg(sc, SMSC_LED_GPIO_CFG, reg_val);
1447239275Sgonzo
1448239275Sgonzo	/*
1449239275Sgonzo	 * Initialise the TX interface
1450239275Sgonzo	 */
1451239275Sgonzo	smsc_write_reg(sc, SMSC_FLOW, 0);
1452239275Sgonzo
1453239275Sgonzo	smsc_write_reg(sc, SMSC_AFC_CFG, AFC_CFG_DEFAULT);
1454239275Sgonzo
1455239275Sgonzo	/* Read the current MAC configuration */
1456239275Sgonzo	if ((err = smsc_read_reg(sc, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
1457239275Sgonzo		smsc_warn_printf(sc, "failed to read MAC_CSR (err=%d)\n", err);
1458239275Sgonzo		goto init_failed;
1459239275Sgonzo	}
1460239275Sgonzo
1461239275Sgonzo	/* Vlan */
1462239275Sgonzo	smsc_write_reg(sc, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);
1463239275Sgonzo
1464239275Sgonzo	/*
1465239275Sgonzo	 * Initialise the PHY
1466239275Sgonzo	 */
1467239275Sgonzo	if ((err = smsc_phy_init(sc)) != 0)
1468239275Sgonzo		goto init_failed;
1469239275Sgonzo
1470239275Sgonzo
1471239275Sgonzo	/*
1472239275Sgonzo	 * Start TX
1473239275Sgonzo	 */
1474239275Sgonzo	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
1475239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1476239275Sgonzo	smsc_write_reg(sc, SMSC_TX_CFG, SMSC_TX_CFG_ON);
1477239275Sgonzo
1478239275Sgonzo	/*
1479239275Sgonzo	 * Start RX
1480239275Sgonzo	 */
1481239275Sgonzo	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
1482239275Sgonzo	smsc_write_reg(sc, SMSC_MAC_CSR, sc->sc_mac_csr);
1483239275Sgonzo
1484239275Sgonzo	if (!locked)
1485239275Sgonzo		SMSC_UNLOCK(sc);
1486239275Sgonzo
1487239275Sgonzo	return (0);
1488239275Sgonzo
1489239275Sgonzoinit_failed:
1490239275Sgonzo	if (!locked)
1491239275Sgonzo		SMSC_UNLOCK(sc);
1492239275Sgonzo
1493239275Sgonzo	smsc_err_printf(sc, "smsc_chip_init failed (err=%d)\n", err);
1494239275Sgonzo	return (err);
1495239275Sgonzo}
1496239275Sgonzo
1497239275Sgonzo
1498239275Sgonzo/**
1499239275Sgonzo *	smsc_ioctl - ioctl function for the device
1500239275Sgonzo *	@ifp: interface pointer
1501239275Sgonzo *	@cmd: the ioctl command
1502239275Sgonzo *	@data: data passed in the ioctl call, typically a pointer to struct ifreq.
1503239275Sgonzo *
1504239275Sgonzo *	The ioctl routine is overridden to detect change requests for the H/W
1505239275Sgonzo *	checksum capabilities.
1506239275Sgonzo *
1507239275Sgonzo *	RETURNS:
1508239275Sgonzo *	0 on success and an error code on failure.
1509239275Sgonzo */
1510239275Sgonzostatic int
1511239275Sgonzosmsc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1512239275Sgonzo{
1513239275Sgonzo	struct usb_ether *ue = ifp->if_softc;
1514239275Sgonzo	struct smsc_softc *sc;
1515239275Sgonzo	struct ifreq *ifr;
1516239275Sgonzo	int rc;
1517239275Sgonzo	int mask;
1518239275Sgonzo	int reinit;
1519239275Sgonzo
1520239275Sgonzo	if (cmd == SIOCSIFCAP) {
1521239275Sgonzo
1522239275Sgonzo		sc = uether_getsc(ue);
1523239275Sgonzo		ifr = (struct ifreq *)data;
1524239275Sgonzo
1525239275Sgonzo		SMSC_LOCK(sc);
1526239275Sgonzo
1527239275Sgonzo		rc = 0;
1528239275Sgonzo		reinit = 0;
1529239275Sgonzo
1530239275Sgonzo		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1531239275Sgonzo
1532239275Sgonzo		/* Modify the RX CSUM enable bits */
1533239275Sgonzo		if ((mask & IFCAP_RXCSUM) != 0 &&
1534239275Sgonzo		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1535239275Sgonzo			ifp->if_capenable ^= IFCAP_RXCSUM;
1536239275Sgonzo
1537239275Sgonzo			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1538239275Sgonzo				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1539239275Sgonzo				reinit = 1;
1540239275Sgonzo			}
1541239275Sgonzo		}
1542239275Sgonzo
1543239275Sgonzo		SMSC_UNLOCK(sc);
1544239275Sgonzo		if (reinit)
1545239275Sgonzo#if __FreeBSD_version > 1000000
1546239275Sgonzo			uether_init(ue);
1547239275Sgonzo#else
1548239275Sgonzo			ifp->if_init(ue);
1549239275Sgonzo#endif
1550239275Sgonzo
1551239275Sgonzo	} else {
1552239275Sgonzo		rc = uether_ioctl(ifp, cmd, data);
1553239275Sgonzo	}
1554239275Sgonzo
1555239275Sgonzo	return (rc);
1556239275Sgonzo}
1557239275Sgonzo
1558243421Sgonzo#ifdef FDT
1559273546Sloosstatic phandle_t
1560273546Sloossmsc_fdt_find_eth_node(phandle_t start)
1561273546Sloos{
1562273546Sloos	phandle_t child, node;
1563273546Sloos
1564273546Sloos	/* Traverse through entire tree to find usb ethernet nodes. */
1565273546Sloos	for (node = OF_child(start); node != 0; node = OF_peer(node)) {
1566273546Sloos		if (fdt_is_compatible(node, "net,ethernet") &&
1567273546Sloos		    fdt_is_compatible(node, "usb,device"))
1568273546Sloos			return (node);
1569273546Sloos		child = smsc_fdt_find_eth_node(node);
1570273546Sloos		if (child != 0)
1571273546Sloos			return (child);
1572273546Sloos	}
1573273546Sloos
1574273546Sloos	return (0);
1575273546Sloos}
1576273546Sloos
1577243421Sgonzo/**
1578273546Sloos * Get MAC address from FDT blob.  Firmware or loader should fill
1579273546Sloos * mac-address or local-mac-address property.  Returns 0 if MAC address
1580273546Sloos * obtained, error code otherwise.
1581243421Sgonzo */
1582243421Sgonzostatic int
1583243421Sgonzosmsc_fdt_find_mac(unsigned char *mac)
1584243421Sgonzo{
1585273546Sloos	phandle_t node, root;
1586243421Sgonzo	int len;
1587239275Sgonzo
1588243421Sgonzo	root = OF_finddevice("/");
1589273546Sloos	node = smsc_fdt_find_eth_node(root);
1590273546Sloos	if (node != 0) {
1591243421Sgonzo
1592273546Sloos		/* Check if there is property */
1593273546Sloos		if ((len = OF_getproplen(node, "local-mac-address")) > 0) {
1594273546Sloos			if (len != ETHER_ADDR_LEN)
1595273546Sloos				return (EINVAL);
1596243421Sgonzo
1597273546Sloos			OF_getprop(node, "local-mac-address", mac,
1598273546Sloos			    ETHER_ADDR_LEN);
1599273546Sloos			return (0);
1600243421Sgonzo		}
1601243421Sgonzo
1602273546Sloos		if ((len = OF_getproplen(node, "mac-address")) > 0) {
1603273546Sloos			if (len != ETHER_ADDR_LEN)
1604273546Sloos				return (EINVAL);
1605243421Sgonzo
1606273546Sloos			OF_getprop(node, "mac-address", mac,
1607273546Sloos			    ETHER_ADDR_LEN);
1608273546Sloos			return (0);
1609243421Sgonzo		}
1610243421Sgonzo	}
1611243421Sgonzo
1612243421Sgonzo	return (ENXIO);
1613243421Sgonzo}
1614243421Sgonzo#endif
1615243421Sgonzo
1616239275Sgonzo/**
1617239275Sgonzo *	smsc_attach_post - Called after the driver attached to the USB interface
1618239275Sgonzo *	@ue: the USB ethernet device
1619239275Sgonzo *
1620239275Sgonzo *	This is where the chip is intialised for the first time.  This is different
1621239275Sgonzo *	from the smsc_init() function in that that one is designed to setup the
1622239275Sgonzo *	H/W to match the UE settings and can be called after a reset.
1623239275Sgonzo *
1624239275Sgonzo *
1625239275Sgonzo */
1626239275Sgonzostatic void
1627239275Sgonzosmsc_attach_post(struct usb_ether *ue)
1628239275Sgonzo{
1629239275Sgonzo	struct smsc_softc *sc = uether_getsc(ue);
1630239275Sgonzo	uint32_t mac_h, mac_l;
1631239275Sgonzo	int err;
1632239275Sgonzo
1633239275Sgonzo	smsc_dbg_printf(sc, "smsc_attach_post\n");
1634239275Sgonzo
1635239275Sgonzo	/* Setup some of the basics */
1636239275Sgonzo	sc->sc_phyno = 1;
1637239275Sgonzo
1638239275Sgonzo
1639239275Sgonzo	/* Attempt to get the mac address, if an EEPROM is not attached this
1640239275Sgonzo	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
1641239275Sgonzo	 * address based on urandom.
1642239275Sgonzo	 */
1643239275Sgonzo	memset(sc->sc_ue.ue_eaddr, 0xff, ETHER_ADDR_LEN);
1644239275Sgonzo
1645239275Sgonzo	/* Check if there is already a MAC address in the register */
1646239275Sgonzo	if ((smsc_read_reg(sc, SMSC_MAC_ADDRL, &mac_l) == 0) &&
1647239275Sgonzo	    (smsc_read_reg(sc, SMSC_MAC_ADDRH, &mac_h) == 0)) {
1648239275Sgonzo		sc->sc_ue.ue_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
1649239275Sgonzo		sc->sc_ue.ue_eaddr[4] = (uint8_t)((mac_h) & 0xff);
1650239275Sgonzo		sc->sc_ue.ue_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
1651239275Sgonzo		sc->sc_ue.ue_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
1652239275Sgonzo		sc->sc_ue.ue_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
1653239275Sgonzo		sc->sc_ue.ue_eaddr[0] = (uint8_t)((mac_l) & 0xff);
1654239275Sgonzo	}
1655239275Sgonzo
1656239275Sgonzo	/* MAC address is not set so try to read from EEPROM, if that fails generate
1657239275Sgonzo	 * a random MAC address.
1658239275Sgonzo	 */
1659239275Sgonzo	if (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)) {
1660239275Sgonzo
1661239275Sgonzo		err = smsc_eeprom_read(sc, 0x01, sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1662243421Sgonzo#ifdef FDT
1663243421Sgonzo		if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr)))
1664243421Sgonzo			err = smsc_fdt_find_mac(sc->sc_ue.ue_eaddr);
1665243421Sgonzo#endif
1666239275Sgonzo		if ((err != 0) || (!ETHER_IS_VALID(sc->sc_ue.ue_eaddr))) {
1667239275Sgonzo			read_random(sc->sc_ue.ue_eaddr, ETHER_ADDR_LEN);
1668239275Sgonzo			sc->sc_ue.ue_eaddr[0] &= ~0x01;     /* unicast */
1669239275Sgonzo			sc->sc_ue.ue_eaddr[0] |=  0x02;     /* locally administered */
1670239275Sgonzo		}
1671239275Sgonzo	}
1672239275Sgonzo
1673239275Sgonzo	/* Initialise the chip for the first time */
1674239275Sgonzo	smsc_chip_init(sc);
1675239275Sgonzo}
1676239275Sgonzo
1677239275Sgonzo
1678239275Sgonzo/**
1679239275Sgonzo *	smsc_attach_post_sub - Called after the driver attached to the USB interface
1680239275Sgonzo *	@ue: the USB ethernet device
1681239275Sgonzo *
1682239275Sgonzo *	Most of this is boilerplate code and copied from the base USB ethernet
1683239275Sgonzo *	driver.  It has been overriden so that we can indicate to the system that
1684239275Sgonzo *	the chip supports H/W checksumming.
1685239275Sgonzo *
1686239275Sgonzo *	RETURNS:
1687239275Sgonzo *	Returns 0 on success or a negative error code.
1688239275Sgonzo */
1689239275Sgonzo#if __FreeBSD_version > 1000000
1690239275Sgonzostatic int
1691239275Sgonzosmsc_attach_post_sub(struct usb_ether *ue)
1692239275Sgonzo{
1693239275Sgonzo	struct smsc_softc *sc;
1694239275Sgonzo	struct ifnet *ifp;
1695239275Sgonzo	int error;
1696239275Sgonzo
1697239275Sgonzo	sc = uether_getsc(ue);
1698239275Sgonzo	ifp = ue->ue_ifp;
1699239275Sgonzo	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1700239275Sgonzo	ifp->if_start = uether_start;
1701239275Sgonzo	ifp->if_ioctl = smsc_ioctl;
1702239275Sgonzo	ifp->if_init = uether_init;
1703239275Sgonzo	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
1704239275Sgonzo	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
1705239275Sgonzo	IFQ_SET_READY(&ifp->if_snd);
1706239275Sgonzo
1707239275Sgonzo	/* The chip supports TCP/UDP checksum offloading on TX and RX paths, however
1708239275Sgonzo	 * currently only RX checksum is supported in the driver (see top of file).
1709239275Sgonzo	 */
1710291953Shselasky	ifp->if_capabilities |= IFCAP_RXCSUM | IFCAP_VLAN_MTU;
1711239275Sgonzo	ifp->if_hwassist = 0;
1712239275Sgonzo
1713239275Sgonzo	/* TX checksuming is disabled (for now?)
1714239275Sgonzo	ifp->if_capabilities |= IFCAP_TXCSUM;
1715239275Sgonzo	ifp->if_capenable |= IFCAP_TXCSUM;
1716239275Sgonzo	ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1717239275Sgonzo	*/
1718239275Sgonzo
1719239275Sgonzo	ifp->if_capenable = ifp->if_capabilities;
1720239275Sgonzo
1721239275Sgonzo	mtx_lock(&Giant);
1722239275Sgonzo	error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
1723239275Sgonzo	    uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
1724239275Sgonzo	    BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0);
1725239275Sgonzo	mtx_unlock(&Giant);
1726239275Sgonzo
1727239275Sgonzo	return (error);
1728239275Sgonzo}
1729239275Sgonzo#endif /* __FreeBSD_version > 1000000 */
1730239275Sgonzo
1731239275Sgonzo
1732239275Sgonzo/**
1733239275Sgonzo *	smsc_probe - Probe the interface.
1734239275Sgonzo *	@dev: smsc device handle
1735239275Sgonzo *
1736239275Sgonzo *	Checks if the device is a match for this driver.
1737239275Sgonzo *
1738239275Sgonzo *	RETURNS:
1739239275Sgonzo *	Returns 0 on success or an error code on failure.
1740239275Sgonzo */
1741239275Sgonzostatic int
1742239275Sgonzosmsc_probe(device_t dev)
1743239275Sgonzo{
1744239275Sgonzo	struct usb_attach_arg *uaa = device_get_ivars(dev);
1745239275Sgonzo
1746239275Sgonzo	if (uaa->usb_mode != USB_MODE_HOST)
1747239275Sgonzo		return (ENXIO);
1748239275Sgonzo	if (uaa->info.bConfigIndex != SMSC_CONFIG_INDEX)
1749239275Sgonzo		return (ENXIO);
1750239275Sgonzo	if (uaa->info.bIfaceIndex != SMSC_IFACE_IDX)
1751239275Sgonzo		return (ENXIO);
1752239275Sgonzo
1753239275Sgonzo	return (usbd_lookup_id_by_uaa(smsc_devs, sizeof(smsc_devs), uaa));
1754239275Sgonzo}
1755239275Sgonzo
1756239275Sgonzo
1757239275Sgonzo/**
1758239275Sgonzo *	smsc_attach - Attach the interface.
1759239275Sgonzo *	@dev: smsc device handle
1760239275Sgonzo *
1761239275Sgonzo *	Allocate softc structures, do ifmedia setup and ethernet/BPF attach.
1762239275Sgonzo *
1763239275Sgonzo *	RETURNS:
1764239275Sgonzo *	Returns 0 on success or a negative error code.
1765239275Sgonzo */
1766239275Sgonzostatic int
1767239275Sgonzosmsc_attach(device_t dev)
1768239275Sgonzo{
1769239275Sgonzo	struct usb_attach_arg *uaa = device_get_ivars(dev);
1770239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
1771239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
1772239275Sgonzo	uint8_t iface_index;
1773239275Sgonzo	int err;
1774239275Sgonzo
1775239275Sgonzo	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
1776239275Sgonzo
1777239275Sgonzo	device_set_usb_desc(dev);
1778239275Sgonzo
1779239275Sgonzo	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1780239275Sgonzo
1781239275Sgonzo	/* Setup the endpoints for the SMSC LAN95xx device(s) */
1782239275Sgonzo	iface_index = SMSC_IFACE_IDX;
1783239275Sgonzo	err = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
1784239275Sgonzo	                          smsc_config, SMSC_N_TRANSFER, sc, &sc->sc_mtx);
1785239275Sgonzo	if (err) {
1786239275Sgonzo		device_printf(dev, "error: allocating USB transfers failed\n");
1787239275Sgonzo		goto detach;
1788239275Sgonzo	}
1789239275Sgonzo
1790239275Sgonzo	ue->ue_sc = sc;
1791239275Sgonzo	ue->ue_dev = dev;
1792239275Sgonzo	ue->ue_udev = uaa->device;
1793239275Sgonzo	ue->ue_mtx = &sc->sc_mtx;
1794239275Sgonzo	ue->ue_methods = &smsc_ue_methods;
1795239275Sgonzo
1796239275Sgonzo	err = uether_ifattach(ue);
1797239275Sgonzo	if (err) {
1798239275Sgonzo		device_printf(dev, "error: could not attach interface\n");
1799239275Sgonzo		goto detach;
1800239275Sgonzo	}
1801239275Sgonzo	return (0);			/* success */
1802239275Sgonzo
1803239275Sgonzodetach:
1804239275Sgonzo	smsc_detach(dev);
1805239275Sgonzo	return (ENXIO);		/* failure */
1806239275Sgonzo}
1807239275Sgonzo
1808239275Sgonzo/**
1809239275Sgonzo *	smsc_detach - Detach the interface.
1810239275Sgonzo *	@dev: smsc device handle
1811239275Sgonzo *
1812239275Sgonzo *	RETURNS:
1813239275Sgonzo *	Returns 0.
1814239275Sgonzo */
1815239275Sgonzostatic int
1816239275Sgonzosmsc_detach(device_t dev)
1817239275Sgonzo{
1818239275Sgonzo	struct smsc_softc *sc = device_get_softc(dev);
1819239275Sgonzo	struct usb_ether *ue = &sc->sc_ue;
1820239275Sgonzo
1821239275Sgonzo	usbd_transfer_unsetup(sc->sc_xfer, SMSC_N_TRANSFER);
1822239275Sgonzo	uether_ifdetach(ue);
1823239275Sgonzo	mtx_destroy(&sc->sc_mtx);
1824239275Sgonzo
1825239275Sgonzo	return (0);
1826239275Sgonzo}
1827239275Sgonzo
1828239275Sgonzostatic device_method_t smsc_methods[] = {
1829239275Sgonzo	/* Device interface */
1830239275Sgonzo	DEVMETHOD(device_probe, smsc_probe),
1831239275Sgonzo	DEVMETHOD(device_attach, smsc_attach),
1832239275Sgonzo	DEVMETHOD(device_detach, smsc_detach),
1833239275Sgonzo
1834239275Sgonzo	/* bus interface */
1835239275Sgonzo	DEVMETHOD(bus_print_child, bus_generic_print_child),
1836239275Sgonzo	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
1837239275Sgonzo
1838239275Sgonzo	/* MII interface */
1839239275Sgonzo	DEVMETHOD(miibus_readreg, smsc_miibus_readreg),
1840239275Sgonzo	DEVMETHOD(miibus_writereg, smsc_miibus_writereg),
1841239275Sgonzo	DEVMETHOD(miibus_statchg, smsc_miibus_statchg),
1842239275Sgonzo
1843246128Ssbz	DEVMETHOD_END
1844239275Sgonzo};
1845239275Sgonzo
1846239275Sgonzostatic driver_t smsc_driver = {
1847239275Sgonzo	.name = "smsc",
1848239275Sgonzo	.methods = smsc_methods,
1849239275Sgonzo	.size = sizeof(struct smsc_softc),
1850239275Sgonzo};
1851239275Sgonzo
1852239275Sgonzostatic devclass_t smsc_devclass;
1853239275Sgonzo
1854239275SgonzoDRIVER_MODULE(smsc, uhub, smsc_driver, smsc_devclass, NULL, 0);
1855239275SgonzoDRIVER_MODULE(miibus, smsc, miibus_driver, miibus_devclass, 0, 0);
1856239275SgonzoMODULE_DEPEND(smsc, uether, 1, 1, 1);
1857239275SgonzoMODULE_DEPEND(smsc, usb, 1, 1, 1);
1858239275SgonzoMODULE_DEPEND(smsc, ether, 1, 1, 1);
1859239275SgonzoMODULE_DEPEND(smsc, miibus, 1, 1, 1);
1860239275SgonzoMODULE_VERSION(smsc, 1);
1861292080SimpUSB_PNP_HOST_INFO(smsc_devs);
1862