if_axe.c revision 199816
1184610Salfred/*-
2184610Salfred * Copyright (c) 1997, 1998, 1999, 2000-2003
3184610Salfred *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred * 3. All advertising materials mentioning features or use of this software
14184610Salfred *    must display the following acknowledgement:
15184610Salfred *	This product includes software developed by Bill Paul.
16184610Salfred * 4. Neither the name of the author nor the names of any co-contributors
17184610Salfred *    may be used to endorse or promote products derived from this software
18184610Salfred *    without specific prior written permission.
19184610Salfred *
20184610Salfred * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30184610Salfred * THE POSSIBILITY OF SUCH DAMAGE.
31184610Salfred */
32184610Salfred
33184610Salfred#include <sys/cdefs.h>
34184610Salfred__FBSDID("$FreeBSD: head/sys/dev/usb/net/if_axe.c 199816 2009-11-26 00:43:17Z thompsa $");
35184610Salfred
36184610Salfred/*
37188412Sthompsa * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
38188412Sthompsa * Used in the LinkSys USB200M and various other adapters.
39184610Salfred *
40184610Salfred * Manuals available from:
41184610Salfred * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
42184610Salfred * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
43184610Salfred * controller) to find the definitions for the RX control register.
44184610Salfred * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
45184610Salfred *
46184610Salfred * Written by Bill Paul <wpaul@windriver.com>
47184610Salfred * Senior Engineer
48184610Salfred * Wind River Systems
49184610Salfred */
50184610Salfred
51184610Salfred/*
52184610Salfred * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
53184610Salfred * It uses an external PHY (reference designs use a RealTek chip),
54184610Salfred * and has a 64-bit multicast hash filter. There is some information
55184610Salfred * missing from the manual which one needs to know in order to make
56184610Salfred * the chip function:
57184610Salfred *
58184610Salfred * - You must set bit 7 in the RX control register, otherwise the
59184610Salfred *   chip won't receive any packets.
60184610Salfred * - You must initialize all 3 IPG registers, or you won't be able
61184610Salfred *   to send any packets.
62184610Salfred *
63184610Salfred * Note that this device appears to only support loading the station
64184610Salfred * address via autload from the EEPROM (i.e. there's no way to manaully
65184610Salfred * set it).
66184610Salfred *
67184610Salfred * (Adam Weinberger wanted me to name this driver if_gir.c.)
68184610Salfred */
69184610Salfred
70184610Salfred/*
71184610Salfred * Ax88178 and Ax88772 support backported from the OpenBSD driver.
72184610Salfred * 2007/02/12, J.R. Oldroyd, fbsd@opal.com
73184610Salfred *
74184610Salfred * Manual here:
75184610Salfred * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
76184610Salfred * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
77184610Salfred */
78184610Salfred
79194677Sthompsa#include <sys/stdint.h>
80194677Sthompsa#include <sys/stddef.h>
81194677Sthompsa#include <sys/param.h>
82194677Sthompsa#include <sys/queue.h>
83194677Sthompsa#include <sys/types.h>
84194677Sthompsa#include <sys/systm.h>
85194677Sthompsa#include <sys/kernel.h>
86194677Sthompsa#include <sys/bus.h>
87194677Sthompsa#include <sys/linker_set.h>
88194677Sthompsa#include <sys/module.h>
89194677Sthompsa#include <sys/lock.h>
90194677Sthompsa#include <sys/mutex.h>
91194677Sthompsa#include <sys/condvar.h>
92194677Sthompsa#include <sys/sysctl.h>
93194677Sthompsa#include <sys/sx.h>
94194677Sthompsa#include <sys/unistd.h>
95194677Sthompsa#include <sys/callout.h>
96194677Sthompsa#include <sys/malloc.h>
97194677Sthompsa#include <sys/priv.h>
98194677Sthompsa
99194677Sthompsa#include <dev/usb/usb.h>
100194677Sthompsa#include <dev/usb/usbdi.h>
101194677Sthompsa#include <dev/usb/usbdi_util.h>
102188746Sthompsa#include "usbdevs.h"
103184610Salfred
104184610Salfred#define	USB_DEBUG_VAR axe_debug
105194677Sthompsa#include <dev/usb/usb_debug.h>
106188942Sthompsa#include <dev/usb/usb_process.h>
107184610Salfred
108188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
109188942Sthompsa#include <dev/usb/net/if_axereg.h>
110184610Salfred
111188412Sthompsa/*
112188412Sthompsa * AXE_178_MAX_FRAME_BURST
113188412Sthompsa * max frame burst size for Ax88178 and Ax88772
114188412Sthompsa *	0	2048 bytes
115188412Sthompsa *	1	4096 bytes
116188412Sthompsa *	2	8192 bytes
117188412Sthompsa *	3	16384 bytes
118188412Sthompsa * use the largest your system can handle without USB stalling.
119188412Sthompsa *
120188412Sthompsa * NB: 88772 parts appear to generate lots of input errors with
121188412Sthompsa * a 2K rx buffer and 8K is only slightly faster than 4K on an
122188412Sthompsa * EHCI port on a T42 so change at your own risk.
123188412Sthompsa */
124188412Sthompsa#define AXE_178_MAX_FRAME_BURST	1
125184610Salfred
126184610Salfred#if USB_DEBUG
127184610Salfredstatic int axe_debug = 0;
128184610Salfred
129192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
130192502SthompsaSYSCTL_INT(_hw_usb_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
131184610Salfred    "Debug level");
132184610Salfred#endif
133184610Salfred
134184610Salfred/*
135184610Salfred * Various supported device vendors/products.
136184610Salfred */
137192984Sthompsastatic const struct usb_device_id axe_devs[] = {
138184610Salfred	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200, 0)},
139184610Salfred	{USB_VPI(USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2, 0)},
140184610Salfred	{USB_VPI(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET, AXE_FLAG_772)},
141184610Salfred	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172, 0)},
142184610Salfred	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178, AXE_FLAG_178)},
143184610Salfred	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772, AXE_FLAG_772)},
144184610Salfred	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T, 0)},
145184610Salfred	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055, AXE_FLAG_178)},
146184610Salfred	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR, 0)},
147184610Salfred	{USB_VPI(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2, AXE_FLAG_772)},
148184610Salfred	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX, 0)},
149184610Salfred	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100, 0)},
150184610Salfred	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1, AXE_FLAG_772)},
151184610Salfred	{USB_VPI(USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E, 0)},
152184610Salfred	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2, AXE_FLAG_178)},
153184610Salfred	{USB_VPI(USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1, 0)},
154184610Salfred	{USB_VPI(USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M, 0)},
155184610Salfred	{USB_VPI(USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000, AXE_FLAG_178)},
156184610Salfred	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX, 0)},
157184610Salfred	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120, 0)},
158184610Salfred	{USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS, AXE_FLAG_772)},
159184610Salfred	{USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T, AXE_FLAG_178)},
160184610Salfred	{USB_VPI(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029, 0)},
161184610Salfred	{USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028, AXE_FLAG_178)},
162184610Salfred	{USB_VPI(USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL, 0)},
163184610Salfred};
164184610Salfred
165184610Salfredstatic device_probe_t axe_probe;
166184610Salfredstatic device_attach_t axe_attach;
167184610Salfredstatic device_detach_t axe_detach;
168184610Salfred
169193045Sthompsastatic usb_callback_t axe_intr_callback;
170193045Sthompsastatic usb_callback_t axe_bulk_read_callback;
171193045Sthompsastatic usb_callback_t axe_bulk_write_callback;
172184610Salfred
173188412Sthompsastatic miibus_readreg_t axe_miibus_readreg;
174188412Sthompsastatic miibus_writereg_t axe_miibus_writereg;
175188412Sthompsastatic miibus_statchg_t axe_miibus_statchg;
176184610Salfred
177193045Sthompsastatic uether_fn_t axe_attach_post;
178193045Sthompsastatic uether_fn_t axe_init;
179193045Sthompsastatic uether_fn_t axe_stop;
180193045Sthompsastatic uether_fn_t axe_start;
181193045Sthompsastatic uether_fn_t axe_tick;
182193045Sthompsastatic uether_fn_t axe_setmulti;
183193045Sthompsastatic uether_fn_t axe_setpromisc;
184184610Salfred
185188412Sthompsastatic int	axe_ifmedia_upd(struct ifnet *);
186188412Sthompsastatic void	axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
187188412Sthompsastatic int	axe_cmd(struct axe_softc *, int, int, int, void *);
188188412Sthompsastatic void	axe_ax88178_init(struct axe_softc *);
189188412Sthompsastatic void	axe_ax88772_init(struct axe_softc *);
190186730Salfredstatic int	axe_get_phyno(struct axe_softc *, int);
191184610Salfred
192192984Sthompsastatic const struct usb_config axe_config[AXE_N_TRANSFER] = {
193184610Salfred
194187259Sthompsa	[AXE_BULK_DT_WR] = {
195184610Salfred		.type = UE_BULK,
196184610Salfred		.endpoint = UE_ADDR_ANY,
197184610Salfred		.direction = UE_DIR_OUT,
198190734Sthompsa		.bufsize = AXE_BULK_BUF_SIZE,
199190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
200190734Sthompsa		.callback = axe_bulk_write_callback,
201190734Sthompsa		.timeout = 10000,	/* 10 seconds */
202184610Salfred	},
203184610Salfred
204187259Sthompsa	[AXE_BULK_DT_RD] = {
205184610Salfred		.type = UE_BULK,
206184610Salfred		.endpoint = UE_ADDR_ANY,
207184610Salfred		.direction = UE_DIR_IN,
208197566Sthompsa		.bufsize = 16384,	/* bytes */
209190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
210190734Sthompsa		.callback = axe_bulk_read_callback,
211190734Sthompsa		.timeout = 0,	/* no timeout */
212184610Salfred	},
213184610Salfred
214187259Sthompsa	[AXE_INTR_DT_RD] = {
215184610Salfred		.type = UE_INTERRUPT,
216184610Salfred		.endpoint = UE_ADDR_ANY,
217184610Salfred		.direction = UE_DIR_IN,
218190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
219190734Sthompsa		.bufsize = 0,	/* use wMaxPacketSize */
220190734Sthompsa		.callback = axe_intr_callback,
221184610Salfred	},
222184610Salfred};
223184610Salfred
224184610Salfredstatic device_method_t axe_methods[] = {
225184610Salfred	/* Device interface */
226184610Salfred	DEVMETHOD(device_probe, axe_probe),
227184610Salfred	DEVMETHOD(device_attach, axe_attach),
228184610Salfred	DEVMETHOD(device_detach, axe_detach),
229184610Salfred
230184610Salfred	/* bus interface */
231184610Salfred	DEVMETHOD(bus_print_child, bus_generic_print_child),
232184610Salfred	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
233184610Salfred
234184610Salfred	/* MII interface */
235188412Sthompsa	DEVMETHOD(miibus_readreg, axe_miibus_readreg),
236188412Sthompsa	DEVMETHOD(miibus_writereg, axe_miibus_writereg),
237188412Sthompsa	DEVMETHOD(miibus_statchg, axe_miibus_statchg),
238184610Salfred
239184610Salfred	{0, 0}
240184610Salfred};
241184610Salfred
242184610Salfredstatic driver_t axe_driver = {
243184610Salfred	.name = "axe",
244184610Salfred	.methods = axe_methods,
245184610Salfred	.size = sizeof(struct axe_softc),
246184610Salfred};
247184610Salfred
248184610Salfredstatic devclass_t axe_devclass;
249184610Salfred
250189275SthompsaDRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, 0);
251184610SalfredDRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
252188942SthompsaMODULE_DEPEND(axe, uether, 1, 1, 1);
253188942SthompsaMODULE_DEPEND(axe, usb, 1, 1, 1);
254188412SthompsaMODULE_DEPEND(axe, ether, 1, 1, 1);
255188412SthompsaMODULE_DEPEND(axe, miibus, 1, 1, 1);
256184610Salfred
257192984Sthompsastatic const struct usb_ether_methods axe_ue_methods = {
258188412Sthompsa	.ue_attach_post = axe_attach_post,
259188412Sthompsa	.ue_start = axe_start,
260188412Sthompsa	.ue_init = axe_init,
261188412Sthompsa	.ue_stop = axe_stop,
262188412Sthompsa	.ue_tick = axe_tick,
263188412Sthompsa	.ue_setmulti = axe_setmulti,
264188412Sthompsa	.ue_setpromisc = axe_setpromisc,
265188412Sthompsa	.ue_mii_upd = axe_ifmedia_upd,
266188412Sthompsa	.ue_mii_sts = axe_ifmedia_sts,
267188412Sthompsa};
268188412Sthompsa
269188412Sthompsastatic int
270188412Sthompsaaxe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
271184610Salfred{
272192984Sthompsa	struct usb_device_request req;
273193045Sthompsa	usb_error_t err;
274184610Salfred
275188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
276188412Sthompsa
277184610Salfred	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
278184610Salfred	    UT_WRITE_VENDOR_DEVICE :
279184610Salfred	    UT_READ_VENDOR_DEVICE);
280184610Salfred	req.bRequest = AXE_CMD_CMD(cmd);
281184610Salfred	USETW(req.wValue, val);
282184610Salfred	USETW(req.wIndex, index);
283188412Sthompsa	USETW(req.wLength, AXE_CMD_LEN(cmd));
284184610Salfred
285194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, buf, 1000);
286184610Salfred
287188412Sthompsa	return (err);
288184610Salfred}
289184610Salfred
290184610Salfredstatic int
291188412Sthompsaaxe_miibus_readreg(device_t dev, int phy, int reg)
292184610Salfred{
293184610Salfred	struct axe_softc *sc = device_get_softc(dev);
294184610Salfred	uint16_t val;
295188412Sthompsa	int locked;
296184610Salfred
297188412Sthompsa	if (sc->sc_phyno != phy)
298188412Sthompsa		return (0);
299184610Salfred
300188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
301188412Sthompsa	if (!locked)
302188412Sthompsa		AXE_LOCK(sc);
303186730Salfred
304188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
305188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
306188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
307184610Salfred
308184610Salfred	val = le16toh(val);
309186730Salfred	if ((sc->sc_flags & AXE_FLAG_772) != 0 && reg == MII_BMSR) {
310186730Salfred		/*
311186730Salfred		 * BMSR of AX88772 indicates that it supports extended
312186730Salfred		 * capability but the extended status register is
313186730Salfred		 * revered for embedded ethernet PHY. So clear the
314186730Salfred		 * extended capability bit of BMSR.
315186730Salfred		 */
316186730Salfred		val &= ~BMSR_EXTCAP;
317186730Salfred	}
318184610Salfred
319188412Sthompsa	if (!locked)
320188412Sthompsa		AXE_UNLOCK(sc);
321184610Salfred	return (val);
322184610Salfred}
323184610Salfred
324184610Salfredstatic int
325188412Sthompsaaxe_miibus_writereg(device_t dev, int phy, int reg, int val)
326184610Salfred{
327184610Salfred	struct axe_softc *sc = device_get_softc(dev);
328188412Sthompsa	int locked;
329184610Salfred
330189522Sthompsa	val = htole32(val);
331184610Salfred
332186730Salfred	if (sc->sc_phyno != phy)
333188412Sthompsa		return (0);
334186730Salfred
335188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
336188412Sthompsa	if (!locked)
337188412Sthompsa		AXE_LOCK(sc);
338184610Salfred
339188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
340188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
341188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
342188412Sthompsa
343188412Sthompsa	if (!locked)
344188412Sthompsa		AXE_UNLOCK(sc);
345184610Salfred	return (0);
346184610Salfred}
347184610Salfred
348184610Salfredstatic void
349188412Sthompsaaxe_miibus_statchg(device_t dev)
350184610Salfred{
351184610Salfred	struct axe_softc *sc = device_get_softc(dev);
352184610Salfred	struct mii_data *mii = GET_MII(sc);
353188553Sthompsa	struct ifnet *ifp;
354184610Salfred	uint16_t val;
355188412Sthompsa	int err, locked;
356184610Salfred
357188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
358188412Sthompsa	if (!locked)
359188412Sthompsa		AXE_LOCK(sc);
360184610Salfred
361194228Sthompsa	ifp = uether_getifp(&sc->sc_ue);
362188553Sthompsa	if (mii == NULL || ifp == NULL ||
363188553Sthompsa	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
364188553Sthompsa		goto done;
365188553Sthompsa
366188553Sthompsa	sc->sc_flags &= ~AXE_FLAG_LINK;
367188553Sthompsa	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
368188553Sthompsa	    (IFM_ACTIVE | IFM_AVALID)) {
369188553Sthompsa		switch (IFM_SUBTYPE(mii->mii_media_active)) {
370188553Sthompsa		case IFM_10_T:
371188553Sthompsa		case IFM_100_TX:
372188553Sthompsa			sc->sc_flags |= AXE_FLAG_LINK;
373188553Sthompsa			break;
374188553Sthompsa		case IFM_1000_T:
375188553Sthompsa			if ((sc->sc_flags & AXE_FLAG_178) == 0)
376188553Sthompsa				break;
377188553Sthompsa			sc->sc_flags |= AXE_FLAG_LINK;
378188553Sthompsa			break;
379188553Sthompsa		default:
380188553Sthompsa			break;
381188553Sthompsa		}
382188553Sthompsa	}
383188553Sthompsa
384188553Sthompsa	/* Lost link, do nothing. */
385188553Sthompsa	if ((sc->sc_flags & AXE_FLAG_LINK) == 0)
386188553Sthompsa		goto done;
387188553Sthompsa
388188553Sthompsa	val = 0;
389188553Sthompsa	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
390188553Sthompsa		val |= AXE_MEDIA_FULL_DUPLEX;
391186730Salfred	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
392186730Salfred		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
393188553Sthompsa		if ((sc->sc_flags & AXE_FLAG_178) != 0)
394188553Sthompsa			val |= AXE_178_MEDIA_ENCK;
395184610Salfred		switch (IFM_SUBTYPE(mii->mii_media_active)) {
396184610Salfred		case IFM_1000_T:
397184610Salfred			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
398184610Salfred			break;
399184610Salfred		case IFM_100_TX:
400184610Salfred			val |= AXE_178_MEDIA_100TX;
401184610Salfred			break;
402184610Salfred		case IFM_10_T:
403184610Salfred			/* doesn't need to be handled */
404184610Salfred			break;
405184610Salfred		}
406184610Salfred	}
407188412Sthompsa	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
408188412Sthompsa	if (err)
409188412Sthompsa		device_printf(dev, "media change failed, error %d\n", err);
410188553Sthompsadone:
411188412Sthompsa	if (!locked)
412188412Sthompsa		AXE_UNLOCK(sc);
413184610Salfred}
414184610Salfred
415184610Salfred/*
416184610Salfred * Set media options.
417184610Salfred */
418184610Salfredstatic int
419188412Sthompsaaxe_ifmedia_upd(struct ifnet *ifp)
420184610Salfred{
421184610Salfred	struct axe_softc *sc = ifp->if_softc;
422184610Salfred	struct mii_data *mii = GET_MII(sc);
423188553Sthompsa	int error;
424184610Salfred
425188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
426184610Salfred
427184610Salfred	if (mii->mii_instance) {
428184610Salfred		struct mii_softc *miisc;
429184610Salfred
430188412Sthompsa		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
431184610Salfred			mii_phy_reset(miisc);
432184610Salfred	}
433188553Sthompsa	error = mii_mediachg(mii);
434188553Sthompsa	return (error);
435184610Salfred}
436184610Salfred
437184610Salfred/*
438184610Salfred * Report current media status.
439184610Salfred */
440184610Salfredstatic void
441188412Sthompsaaxe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
442184610Salfred{
443184610Salfred	struct axe_softc *sc = ifp->if_softc;
444188412Sthompsa	struct mii_data *mii = GET_MII(sc);
445184610Salfred
446188412Sthompsa	AXE_LOCK(sc);
447188412Sthompsa	mii_pollstat(mii);
448188412Sthompsa	AXE_UNLOCK(sc);
449188412Sthompsa	ifmr->ifm_active = mii->mii_media_active;
450188412Sthompsa	ifmr->ifm_status = mii->mii_media_status;
451184610Salfred}
452184610Salfred
453184610Salfredstatic void
454192984Sthompsaaxe_setmulti(struct usb_ether *ue)
455184610Salfred{
456194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
457194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
458188412Sthompsa	struct ifmultiaddr *ifma;
459188412Sthompsa	uint32_t h = 0;
460184610Salfred	uint16_t rxmode;
461188412Sthompsa	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
462184610Salfred
463188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
464184610Salfred
465188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
466184610Salfred	rxmode = le16toh(rxmode);
467184610Salfred
468188412Sthompsa	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
469184610Salfred		rxmode |= AXE_RXCMD_ALLMULTI;
470188412Sthompsa		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
471184610Salfred		return;
472184610Salfred	}
473184610Salfred	rxmode &= ~AXE_RXCMD_ALLMULTI;
474184610Salfred
475195049Srwatson	if_maddr_rlock(ifp);
476188412Sthompsa	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
477188412Sthompsa	{
478188412Sthompsa		if (ifma->ifma_addr->sa_family != AF_LINK)
479188412Sthompsa			continue;
480188412Sthompsa		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
481188412Sthompsa		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
482188412Sthompsa		hashtbl[h / 8] |= 1 << (h % 8);
483188412Sthompsa	}
484195049Srwatson	if_maddr_runlock(ifp);
485184610Salfred
486188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
487188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
488184610Salfred}
489184610Salfred
490186730Salfredstatic int
491186730Salfredaxe_get_phyno(struct axe_softc *sc, int sel)
492186730Salfred{
493188412Sthompsa	int phyno;
494186730Salfred
495186730Salfred	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
496186730Salfred	case PHY_TYPE_100_HOME:
497186730Salfred	case PHY_TYPE_GIG:
498188412Sthompsa		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
499186730Salfred		break;
500186730Salfred	case PHY_TYPE_SPECIAL:
501186730Salfred		/* FALLTHROUGH */
502186730Salfred	case PHY_TYPE_RSVD:
503186730Salfred		/* FALLTHROUGH */
504186730Salfred	case PHY_TYPE_NON_SUP:
505186730Salfred		/* FALLTHROUGH */
506186730Salfred	default:
507186730Salfred		phyno = -1;
508186730Salfred		break;
509186730Salfred	}
510186730Salfred
511186730Salfred	return (phyno);
512186730Salfred}
513186730Salfred
514184610Salfredstatic void
515188412Sthompsaaxe_ax88178_init(struct axe_softc *sc)
516184610Salfred{
517188412Sthompsa	int gpio0 = 0, phymode = 0;
518184610Salfred	uint16_t eeprom;
519184610Salfred
520188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
521184610Salfred	/* XXX magic */
522188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
523184610Salfred	eeprom = le16toh(eeprom);
524188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
525184610Salfred
526184610Salfred	/* if EEPROM is invalid we have to use to GPIO0 */
527184610Salfred	if (eeprom == 0xffff) {
528184610Salfred		phymode = 0;
529184610Salfred		gpio0 = 1;
530184610Salfred	} else {
531188412Sthompsa		phymode = eeprom & 7;
532184610Salfred		gpio0 = (eeprom & 0x80) ? 0 : 1;
533184610Salfred	}
534184610Salfred
535188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL);
536194228Sthompsa	uether_pause(&sc->sc_ue, hz / 16);
537184610Salfred
538184610Salfred	if ((eeprom >> 8) != 0x01) {
539188412Sthompsa		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
540194228Sthompsa		uether_pause(&sc->sc_ue, hz / 32);
541184610Salfred
542188412Sthompsa		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL);
543194228Sthompsa		uether_pause(&sc->sc_ue, hz / 3);
544184610Salfred
545188412Sthompsa		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
546194228Sthompsa		uether_pause(&sc->sc_ue, hz / 32);
547184610Salfred	} else {
548188412Sthompsa		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL);
549194228Sthompsa		uether_pause(&sc->sc_ue, hz / 32);
550184610Salfred
551188412Sthompsa		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL);
552194228Sthompsa		uether_pause(&sc->sc_ue, hz / 32);
553184610Salfred	}
554184610Salfred
555184610Salfred	/* soft reset */
556188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
557194228Sthompsa	uether_pause(&sc->sc_ue, hz / 4);
558184610Salfred
559188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
560184610Salfred	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
561194228Sthompsa	uether_pause(&sc->sc_ue, hz / 4);
562186730Salfred	/* Enable MII/GMII/RGMII interface to work with external PHY. */
563188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
564194228Sthompsa	uether_pause(&sc->sc_ue, hz / 4);
565184610Salfred
566188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
567184610Salfred}
568184610Salfred
569184610Salfredstatic void
570188412Sthompsaaxe_ax88772_init(struct axe_softc *sc)
571184610Salfred{
572188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
573194228Sthompsa	uether_pause(&sc->sc_ue, hz / 16);
574184610Salfred
575186730Salfred	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
576184610Salfred		/* ask for the embedded PHY */
577188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
578194228Sthompsa		uether_pause(&sc->sc_ue, hz / 64);
579184610Salfred
580184610Salfred		/* power down and reset state, pin reset state */
581188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
582184610Salfred		    AXE_SW_RESET_CLEAR, NULL);
583194228Sthompsa		uether_pause(&sc->sc_ue, hz / 16);
584184610Salfred
585184610Salfred		/* power down/reset state, pin operating state */
586188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
587184610Salfred		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
588194228Sthompsa		uether_pause(&sc->sc_ue, hz / 4);
589184610Salfred
590184610Salfred		/* power up, reset */
591188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
592184610Salfred
593184610Salfred		/* power up, operating */
594188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
595184610Salfred		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
596184610Salfred	} else {
597184610Salfred		/* ask for external PHY */
598188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
599194228Sthompsa		uether_pause(&sc->sc_ue, hz / 64);
600184610Salfred
601184610Salfred		/* power down internal PHY */
602188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
603184610Salfred		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
604184610Salfred	}
605184610Salfred
606194228Sthompsa	uether_pause(&sc->sc_ue, hz / 4);
607188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
608184610Salfred}
609184610Salfred
610184610Salfredstatic void
611188412Sthompsaaxe_reset(struct axe_softc *sc)
612184610Salfred{
613192984Sthompsa	struct usb_config_descriptor *cd;
614193045Sthompsa	usb_error_t err;
615184610Salfred
616194228Sthompsa	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
617184610Salfred
618194228Sthompsa	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
619188412Sthompsa	    cd->bConfigurationValue);
620188412Sthompsa	if (err)
621188412Sthompsa		DPRINTF("reset failed (ignored)\n");
622188412Sthompsa
623188412Sthompsa	/* Wait a little while for the chip to get its brains in order. */
624194228Sthompsa	uether_pause(&sc->sc_ue, hz / 100);
625188412Sthompsa}
626188412Sthompsa
627188412Sthompsastatic void
628192984Sthompsaaxe_attach_post(struct usb_ether *ue)
629188412Sthompsa{
630194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
631188412Sthompsa
632184610Salfred	/*
633184610Salfred	 * Load PHY indexes first. Needed by axe_xxx_init().
634184610Salfred	 */
635188412Sthompsa	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
636186730Salfred#if 1
637188412Sthompsa	device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
638186730Salfred	    sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
639186730Salfred#endif
640186730Salfred	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
641186730Salfred	if (sc->sc_phyno == -1)
642186730Salfred		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
643186730Salfred	if (sc->sc_phyno == -1) {
644188412Sthompsa		device_printf(sc->sc_ue.ue_dev,
645188412Sthompsa		    "no valid PHY address found, assuming PHY address 0\n");
646186730Salfred		sc->sc_phyno = 0;
647186730Salfred	}
648184610Salfred
649188412Sthompsa	if (sc->sc_flags & AXE_FLAG_178)
650188412Sthompsa		axe_ax88178_init(sc);
651188412Sthompsa	else if (sc->sc_flags & AXE_FLAG_772)
652188412Sthompsa		axe_ax88772_init(sc);
653188412Sthompsa
654184610Salfred	/*
655184610Salfred	 * Get station address.
656184610Salfred	 */
657184610Salfred	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772))
658188412Sthompsa		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
659184610Salfred	else
660188412Sthompsa		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
661184610Salfred
662184610Salfred	/*
663184610Salfred	 * Fetch IPG values.
664184610Salfred	 */
665188412Sthompsa	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
666188412Sthompsa}
667184610Salfred
668188412Sthompsa/*
669188412Sthompsa * Probe for a AX88172 chip.
670188412Sthompsa */
671188412Sthompsastatic int
672188412Sthompsaaxe_probe(device_t dev)
673188412Sthompsa{
674192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
675184610Salfred
676192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
677188412Sthompsa		return (ENXIO);
678188412Sthompsa	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
679188412Sthompsa		return (ENXIO);
680188412Sthompsa	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
681188412Sthompsa		return (ENXIO);
682184610Salfred
683194228Sthompsa	return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
684188412Sthompsa}
685184610Salfred
686188412Sthompsa/*
687188412Sthompsa * Attach the interface. Allocate softc structures, do ifmedia
688188412Sthompsa * setup and ethernet/BPF attach.
689188412Sthompsa */
690188412Sthompsastatic int
691188412Sthompsaaxe_attach(device_t dev)
692188412Sthompsa{
693192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
694188412Sthompsa	struct axe_softc *sc = device_get_softc(dev);
695192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
696188412Sthompsa	uint8_t iface_index;
697188412Sthompsa	int error;
698184610Salfred
699188412Sthompsa	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
700184610Salfred
701194228Sthompsa	device_set_usb_desc(dev);
702184610Salfred
703188412Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
704184610Salfred
705188412Sthompsa	iface_index = AXE_IFACE_IDX;
706194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
707188412Sthompsa	    axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
708188412Sthompsa	if (error) {
709199816Sthompsa		device_printf(dev, "allocating USB transfers failed\n");
710188412Sthompsa		goto detach;
711188412Sthompsa	}
712184610Salfred
713188412Sthompsa	ue->ue_sc = sc;
714188412Sthompsa	ue->ue_dev = dev;
715188412Sthompsa	ue->ue_udev = uaa->device;
716188412Sthompsa	ue->ue_mtx = &sc->sc_mtx;
717188412Sthompsa	ue->ue_methods = &axe_ue_methods;
718184610Salfred
719194228Sthompsa	error = uether_ifattach(ue);
720184610Salfred	if (error) {
721188412Sthompsa		device_printf(dev, "could not attach interface\n");
722188412Sthompsa		goto detach;
723184610Salfred	}
724188412Sthompsa	return (0);			/* success */
725184610Salfred
726188412Sthompsadetach:
727188412Sthompsa	axe_detach(dev);
728188412Sthompsa	return (ENXIO);			/* failure */
729184610Salfred}
730184610Salfred
731184610Salfredstatic int
732184610Salfredaxe_detach(device_t dev)
733184610Salfred{
734184610Salfred	struct axe_softc *sc = device_get_softc(dev);
735192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
736184610Salfred
737194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
738194228Sthompsa	uether_ifdetach(ue);
739184610Salfred	mtx_destroy(&sc->sc_mtx);
740184610Salfred
741184610Salfred	return (0);
742184610Salfred}
743184610Salfred
744184610Salfredstatic void
745194677Sthompsaaxe_intr_callback(struct usb_xfer *xfer, usb_error_t error)
746184610Salfred{
747184610Salfred	switch (USB_GET_STATE(xfer)) {
748184610Salfred	case USB_ST_TRANSFERRED:
749184610Salfred	case USB_ST_SETUP:
750188412Sthompsatr_setup:
751194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
752194228Sthompsa		usbd_transfer_submit(xfer);
753184610Salfred		return;
754184610Salfred
755184610Salfred	default:			/* Error */
756194677Sthompsa		if (error != USB_ERR_CANCELLED) {
757188412Sthompsa			/* try to clear stall first */
758194677Sthompsa			usbd_xfer_set_stall(xfer);
759188412Sthompsa			goto tr_setup;
760184610Salfred		}
761184610Salfred		return;
762184610Salfred	}
763184610Salfred}
764184610Salfred
765184610Salfred#if (AXE_BULK_BUF_SIZE >= 0x10000)
766184610Salfred#error "Please update axe_bulk_read_callback()!"
767184610Salfred#endif
768184610Salfred
769184610Salfredstatic void
770194677Sthompsaaxe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
771184610Salfred{
772194677Sthompsa	struct axe_softc *sc = usbd_xfer_softc(xfer);
773192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
774194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
775184610Salfred	struct axe_sframe_hdr hdr;
776194677Sthompsa	struct usb_page_cache *pc;
777197566Sthompsa	int err, pos, len;
778194677Sthompsa	int actlen;
779184610Salfred
780194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
781194677Sthompsa
782184610Salfred	switch (USB_GET_STATE(xfer)) {
783184610Salfred	case USB_ST_TRANSFERRED:
784184610Salfred		pos = 0;
785197566Sthompsa		len = 0;
786197566Sthompsa		err = 0;
787197566Sthompsa
788194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
789197566Sthompsa		if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
790197566Sthompsa			while (pos < actlen) {
791197566Sthompsa				if ((pos + sizeof(hdr)) > actlen) {
792184610Salfred					/* too little data */
793197566Sthompsa					err = EINVAL;
794184610Salfred					break;
795184610Salfred				}
796194677Sthompsa				usbd_copy_out(pc, pos, &hdr, sizeof(hdr));
797184610Salfred
798184610Salfred				if ((hdr.len ^ hdr.ilen) != 0xFFFF) {
799184610Salfred					/* we lost sync */
800197566Sthompsa					err = EINVAL;
801184610Salfred					break;
802184610Salfred				}
803184610Salfred				pos += sizeof(hdr);
804184610Salfred
805184610Salfred				len = le16toh(hdr.len);
806197566Sthompsa				if ((pos + len) > actlen) {
807184610Salfred					/* invalid length */
808197566Sthompsa					err = EINVAL;
809184610Salfred					break;
810184610Salfred				}
811197566Sthompsa				err = uether_rxbuf(ue, pc, pos, len);
812184610Salfred
813197566Sthompsa				pos += len + (len % 2);
814184610Salfred			}
815197566Sthompsa		} else {
816197566Sthompsa			err = uether_rxbuf(ue, pc, 0, actlen);
817184610Salfred		}
818184610Salfred
819197566Sthompsa		if (err != 0)
820197566Sthompsa			ifp->if_ierrors++;
821184610Salfred
822188412Sthompsa		/* FALLTHROUGH */
823184610Salfred	case USB_ST_SETUP:
824184610Salfredtr_setup:
825194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
826194228Sthompsa		usbd_transfer_submit(xfer);
827194228Sthompsa		uether_rxflush(ue);
828184610Salfred		return;
829184610Salfred
830184610Salfred	default:			/* Error */
831194677Sthompsa		DPRINTF("bulk read error, %s\n", usbd_errstr(error));
832188412Sthompsa
833194677Sthompsa		if (error != USB_ERR_CANCELLED) {
834184610Salfred			/* try to clear stall first */
835194677Sthompsa			usbd_xfer_set_stall(xfer);
836188412Sthompsa			goto tr_setup;
837184610Salfred		}
838184610Salfred		return;
839184610Salfred
840184610Salfred	}
841184610Salfred}
842184610Salfred
843184610Salfred#if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
844184610Salfred#error "Please update axe_bulk_write_callback()!"
845184610Salfred#endif
846184610Salfred
847184610Salfredstatic void
848194677Sthompsaaxe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
849184610Salfred{
850194677Sthompsa	struct axe_softc *sc = usbd_xfer_softc(xfer);
851184610Salfred	struct axe_sframe_hdr hdr;
852194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
853194677Sthompsa	struct usb_page_cache *pc;
854184610Salfred	struct mbuf *m;
855188412Sthompsa	int pos;
856184610Salfred
857184610Salfred	switch (USB_GET_STATE(xfer)) {
858184610Salfred	case USB_ST_TRANSFERRED:
859184610Salfred		DPRINTFN(11, "transfer complete\n");
860184610Salfred		ifp->if_opackets++;
861188412Sthompsa		/* FALLTHROUGH */
862184610Salfred	case USB_ST_SETUP:
863188412Sthompsatr_setup:
864186730Salfred		if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
865184610Salfred			/*
866184610Salfred			 * don't send anything if there is no link !
867184610Salfred			 */
868188412Sthompsa			return;
869184610Salfred		}
870184610Salfred		pos = 0;
871194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
872184610Salfred
873184610Salfred		while (1) {
874184610Salfred
875184610Salfred			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
876184610Salfred
877184610Salfred			if (m == NULL) {
878184610Salfred				if (pos > 0)
879184610Salfred					break;	/* send out data */
880188412Sthompsa				return;
881184610Salfred			}
882184610Salfred			if (m->m_pkthdr.len > MCLBYTES) {
883184610Salfred				m->m_pkthdr.len = MCLBYTES;
884184610Salfred			}
885184610Salfred			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
886184610Salfred
887184610Salfred				hdr.len = htole16(m->m_pkthdr.len);
888184610Salfred				hdr.ilen = ~hdr.len;
889184610Salfred
890194677Sthompsa				usbd_copy_in(pc, pos, &hdr, sizeof(hdr));
891184610Salfred
892184610Salfred				pos += sizeof(hdr);
893184610Salfred
894184610Salfred				/*
895184610Salfred				 * NOTE: Some drivers force a short packet
896184610Salfred				 * by appending a dummy header with zero
897184610Salfred				 * length at then end of the USB transfer.
898184610Salfred				 * This driver uses the
899184610Salfred				 * USB_FORCE_SHORT_XFER flag instead.
900184610Salfred				 */
901184610Salfred			}
902194677Sthompsa			usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
903184610Salfred			pos += m->m_pkthdr.len;
904184610Salfred
905184610Salfred			/*
906188412Sthompsa			 * if there's a BPF listener, bounce a copy
907188412Sthompsa			 * of this frame to him:
908188412Sthompsa			 */
909184610Salfred			BPF_MTAP(ifp, m);
910184610Salfred
911184610Salfred			m_freem(m);
912184610Salfred
913184610Salfred			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
914184610Salfred				if (pos > (AXE_BULK_BUF_SIZE - MCLBYTES - sizeof(hdr))) {
915184610Salfred					/* send out frame(s) */
916184610Salfred					break;
917184610Salfred				}
918184610Salfred			} else {
919184610Salfred				/* send out frame */
920184610Salfred				break;
921184610Salfred			}
922184610Salfred		}
923184610Salfred
924194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, pos);
925194228Sthompsa		usbd_transfer_submit(xfer);
926184610Salfred		return;
927184610Salfred
928184610Salfred	default:			/* Error */
929184610Salfred		DPRINTFN(11, "transfer error, %s\n",
930194677Sthompsa		    usbd_errstr(error));
931184610Salfred
932188412Sthompsa		ifp->if_oerrors++;
933188412Sthompsa
934194677Sthompsa		if (error != USB_ERR_CANCELLED) {
935184610Salfred			/* try to clear stall first */
936194677Sthompsa			usbd_xfer_set_stall(xfer);
937188412Sthompsa			goto tr_setup;
938184610Salfred		}
939184610Salfred		return;
940184610Salfred
941184610Salfred	}
942184610Salfred}
943184610Salfred
944184610Salfredstatic void
945192984Sthompsaaxe_tick(struct usb_ether *ue)
946184610Salfred{
947194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
948184610Salfred	struct mii_data *mii = GET_MII(sc);
949184610Salfred
950188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
951188412Sthompsa
952184610Salfred	mii_tick(mii);
953188553Sthompsa	if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
954188553Sthompsa		axe_miibus_statchg(ue->ue_dev);
955188553Sthompsa		if ((sc->sc_flags & AXE_FLAG_LINK) != 0)
956188553Sthompsa			axe_start(ue);
957186730Salfred	}
958184610Salfred}
959184610Salfred
960184610Salfredstatic void
961192984Sthompsaaxe_start(struct usb_ether *ue)
962184610Salfred{
963194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
964184610Salfred
965188412Sthompsa	/*
966188412Sthompsa	 * start the USB transfers, if not already started:
967188412Sthompsa	 */
968194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_INTR_DT_RD]);
969194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
970194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
971184610Salfred}
972184610Salfred
973184610Salfredstatic void
974192984Sthompsaaxe_init(struct usb_ether *ue)
975184610Salfred{
976194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
977194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
978184610Salfred	uint16_t rxmode;
979184610Salfred
980188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
981184610Salfred
982188412Sthompsa	/* Cancel pending I/O */
983188412Sthompsa	axe_stop(ue);
984184610Salfred
985197567Sthompsa	/* Set MAC address. */
986197567Sthompsa	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772))
987197567Sthompsa		axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
988197567Sthompsa	else
989197567Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
990184610Salfred
991184610Salfred	/* Set transmitter IPG values */
992184610Salfred	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
993188412Sthompsa		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
994184610Salfred		    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
995184610Salfred	} else {
996188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
997188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
998188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
999184610Salfred	}
1000184610Salfred
1001184610Salfred	/* Enable receiver, set RX mode */
1002184610Salfred	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1003184610Salfred	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
1004197566Sthompsa#if 0
1005184610Salfred		rxmode |= AXE_178_RXCMD_MFB_2048;	/* chip default */
1006197566Sthompsa#else
1007197566Sthompsa		/*
1008197566Sthompsa		 * Default Rx buffer size is too small to get
1009197566Sthompsa		 * maximum performance.
1010197566Sthompsa		 */
1011197566Sthompsa		rxmode |= AXE_178_RXCMD_MFB_16384;
1012197566Sthompsa#endif
1013184610Salfred	} else {
1014184610Salfred		rxmode |= AXE_172_RXCMD_UNICAST;
1015184610Salfred	}
1016184610Salfred
1017184610Salfred	/* If we want promiscuous mode, set the allframes bit. */
1018188412Sthompsa	if (ifp->if_flags & IFF_PROMISC)
1019184610Salfred		rxmode |= AXE_RXCMD_PROMISC;
1020188412Sthompsa
1021188412Sthompsa	if (ifp->if_flags & IFF_BROADCAST)
1022184610Salfred		rxmode |= AXE_RXCMD_BROADCAST;
1023184610Salfred
1024188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1025188412Sthompsa
1026184610Salfred	/* Load the multicast filter. */
1027188412Sthompsa	axe_setmulti(ue);
1028184610Salfred
1029194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
1030184610Salfred
1031188412Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1032188412Sthompsa	axe_start(ue);
1033184610Salfred}
1034184610Salfred
1035184610Salfredstatic void
1036192984Sthompsaaxe_setpromisc(struct usb_ether *ue)
1037184610Salfred{
1038194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1039194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1040184610Salfred	uint16_t rxmode;
1041184610Salfred
1042188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
1043184610Salfred
1044184610Salfred	rxmode = le16toh(rxmode);
1045184610Salfred
1046188412Sthompsa	if (ifp->if_flags & IFF_PROMISC) {
1047184610Salfred		rxmode |= AXE_RXCMD_PROMISC;
1048184610Salfred	} else {
1049184610Salfred		rxmode &= ~AXE_RXCMD_PROMISC;
1050184610Salfred	}
1051184610Salfred
1052188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1053184610Salfred
1054188412Sthompsa	axe_setmulti(ue);
1055184610Salfred}
1056184610Salfred
1057184610Salfredstatic void
1058192984Sthompsaaxe_stop(struct usb_ether *ue)
1059184610Salfred{
1060194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1061194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1062184610Salfred
1063188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1064184610Salfred
1065188412Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1066186730Salfred	sc->sc_flags &= ~AXE_FLAG_LINK;
1067184610Salfred
1068184610Salfred	/*
1069184610Salfred	 * stop all the transfers, if not already stopped:
1070184610Salfred	 */
1071194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1072194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
1073194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_INTR_DT_RD]);
1074184610Salfred
1075188412Sthompsa	axe_reset(sc);
1076184610Salfred}
1077