if_axe.c revision 223486
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 223486 2011-06-24 02:30:02Z hselasky $");
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/module.h>
88194677Sthompsa#include <sys/lock.h>
89194677Sthompsa#include <sys/mutex.h>
90194677Sthompsa#include <sys/condvar.h>
91194677Sthompsa#include <sys/sysctl.h>
92194677Sthompsa#include <sys/sx.h>
93194677Sthompsa#include <sys/unistd.h>
94194677Sthompsa#include <sys/callout.h>
95194677Sthompsa#include <sys/malloc.h>
96194677Sthompsa#include <sys/priv.h>
97194677Sthompsa
98194677Sthompsa#include <dev/usb/usb.h>
99194677Sthompsa#include <dev/usb/usbdi.h>
100194677Sthompsa#include <dev/usb/usbdi_util.h>
101188746Sthompsa#include "usbdevs.h"
102184610Salfred
103184610Salfred#define	USB_DEBUG_VAR axe_debug
104194677Sthompsa#include <dev/usb/usb_debug.h>
105188942Sthompsa#include <dev/usb/usb_process.h>
106184610Salfred
107188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
108188942Sthompsa#include <dev/usb/net/if_axereg.h>
109184610Salfred
110188412Sthompsa/*
111188412Sthompsa * AXE_178_MAX_FRAME_BURST
112188412Sthompsa * max frame burst size for Ax88178 and Ax88772
113188412Sthompsa *	0	2048 bytes
114188412Sthompsa *	1	4096 bytes
115188412Sthompsa *	2	8192 bytes
116188412Sthompsa *	3	16384 bytes
117188412Sthompsa * use the largest your system can handle without USB stalling.
118188412Sthompsa *
119188412Sthompsa * NB: 88772 parts appear to generate lots of input errors with
120188412Sthompsa * a 2K rx buffer and 8K is only slightly faster than 4K on an
121188412Sthompsa * EHCI port on a T42 so change at your own risk.
122188412Sthompsa */
123188412Sthompsa#define AXE_178_MAX_FRAME_BURST	1
124184610Salfred
125207077Sthompsa#ifdef USB_DEBUG
126184610Salfredstatic int axe_debug = 0;
127184610Salfred
128192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
129192502SthompsaSYSCTL_INT(_hw_usb_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
130184610Salfred    "Debug level");
131184610Salfred#endif
132184610Salfred
133184610Salfred/*
134184610Salfred * Various supported device vendors/products.
135184610Salfred */
136223486Shselaskystatic const STRUCT_USB_HOST_ID axe_devs[] = {
137201028Sthompsa#define	AXE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
138201028Sthompsa	AXE_DEV(ABOCOM, UF200, 0),
139201028Sthompsa	AXE_DEV(ACERCM, EP1427X2, 0),
140201028Sthompsa	AXE_DEV(APPLE, ETHERNET, AXE_FLAG_772),
141201028Sthompsa	AXE_DEV(ASIX, AX88172, 0),
142201028Sthompsa	AXE_DEV(ASIX, AX88178, AXE_FLAG_178),
143201028Sthompsa	AXE_DEV(ASIX, AX88772, AXE_FLAG_772),
144215969Syongari	AXE_DEV(ASIX, AX88772A, AXE_FLAG_772A),
145201028Sthompsa	AXE_DEV(ATEN, UC210T, 0),
146201028Sthompsa	AXE_DEV(BELKIN, F5D5055, AXE_FLAG_178),
147201028Sthompsa	AXE_DEV(BILLIONTON, USB2AR, 0),
148215969Syongari	AXE_DEV(CISCOLINKSYS, USB200MV2, AXE_FLAG_772A),
149201028Sthompsa	AXE_DEV(COREGA, FETHER_USB2_TX, 0),
150201028Sthompsa	AXE_DEV(DLINK, DUBE100, 0),
151201028Sthompsa	AXE_DEV(DLINK, DUBE100B1, AXE_FLAG_772),
152201028Sthompsa	AXE_DEV(GOODWAY, GWUSB2E, 0),
153201028Sthompsa	AXE_DEV(IODATA, ETGUS2, AXE_FLAG_178),
154201028Sthompsa	AXE_DEV(JVC, MP_PRX1, 0),
155201028Sthompsa	AXE_DEV(LINKSYS2, USB200M, 0),
156201028Sthompsa	AXE_DEV(LINKSYS4, USB1000, AXE_FLAG_178),
157212980Ssanpei	AXE_DEV(LOGITEC, LAN_GTJU2A, AXE_FLAG_178),
158201028Sthompsa	AXE_DEV(MELCO, LUAU2KTX, 0),
159212980Ssanpei	AXE_DEV(MELCO, LUA3U2AGT, AXE_FLAG_178),
160201028Sthompsa	AXE_DEV(NETGEAR, FA120, 0),
161201028Sthompsa	AXE_DEV(OQO, ETHER01PLUS, AXE_FLAG_772),
162201028Sthompsa	AXE_DEV(PLANEX3, GU1000T, AXE_FLAG_178),
163201028Sthompsa	AXE_DEV(SITECOM, LN029, 0),
164201028Sthompsa	AXE_DEV(SITECOMEU, LN028, AXE_FLAG_178),
165201028Sthompsa	AXE_DEV(SYSTEMTALKS, SGCX2UL, 0),
166201028Sthompsa#undef AXE_DEV
167184610Salfred};
168184610Salfred
169184610Salfredstatic device_probe_t axe_probe;
170184610Salfredstatic device_attach_t axe_attach;
171184610Salfredstatic device_detach_t axe_detach;
172184610Salfred
173193045Sthompsastatic usb_callback_t axe_bulk_read_callback;
174193045Sthompsastatic usb_callback_t axe_bulk_write_callback;
175184610Salfred
176188412Sthompsastatic miibus_readreg_t axe_miibus_readreg;
177188412Sthompsastatic miibus_writereg_t axe_miibus_writereg;
178188412Sthompsastatic miibus_statchg_t axe_miibus_statchg;
179184610Salfred
180193045Sthompsastatic uether_fn_t axe_attach_post;
181193045Sthompsastatic uether_fn_t axe_init;
182193045Sthompsastatic uether_fn_t axe_stop;
183193045Sthompsastatic uether_fn_t axe_start;
184193045Sthompsastatic uether_fn_t axe_tick;
185193045Sthompsastatic uether_fn_t axe_setmulti;
186193045Sthompsastatic uether_fn_t axe_setpromisc;
187184610Salfred
188188412Sthompsastatic int	axe_ifmedia_upd(struct ifnet *);
189188412Sthompsastatic void	axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
190188412Sthompsastatic int	axe_cmd(struct axe_softc *, int, int, int, void *);
191188412Sthompsastatic void	axe_ax88178_init(struct axe_softc *);
192188412Sthompsastatic void	axe_ax88772_init(struct axe_softc *);
193215969Syongaristatic void	axe_ax88772a_init(struct axe_softc *);
194186730Salfredstatic int	axe_get_phyno(struct axe_softc *, int);
195184610Salfred
196192984Sthompsastatic const struct usb_config axe_config[AXE_N_TRANSFER] = {
197184610Salfred
198187259Sthompsa	[AXE_BULK_DT_WR] = {
199184610Salfred		.type = UE_BULK,
200184610Salfred		.endpoint = UE_ADDR_ANY,
201184610Salfred		.direction = UE_DIR_OUT,
202216284Syongari		.frames = 16,
203216284Syongari		.bufsize = 16 * MCLBYTES,
204190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
205190734Sthompsa		.callback = axe_bulk_write_callback,
206190734Sthompsa		.timeout = 10000,	/* 10 seconds */
207184610Salfred	},
208184610Salfred
209187259Sthompsa	[AXE_BULK_DT_RD] = {
210184610Salfred		.type = UE_BULK,
211184610Salfred		.endpoint = UE_ADDR_ANY,
212184610Salfred		.direction = UE_DIR_IN,
213197566Sthompsa		.bufsize = 16384,	/* bytes */
214190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
215190734Sthompsa		.callback = axe_bulk_read_callback,
216190734Sthompsa		.timeout = 0,	/* no timeout */
217184610Salfred	},
218184610Salfred};
219184610Salfred
220184610Salfredstatic device_method_t axe_methods[] = {
221184610Salfred	/* Device interface */
222184610Salfred	DEVMETHOD(device_probe, axe_probe),
223184610Salfred	DEVMETHOD(device_attach, axe_attach),
224184610Salfred	DEVMETHOD(device_detach, axe_detach),
225184610Salfred
226184610Salfred	/* bus interface */
227184610Salfred	DEVMETHOD(bus_print_child, bus_generic_print_child),
228184610Salfred	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
229184610Salfred
230184610Salfred	/* MII interface */
231188412Sthompsa	DEVMETHOD(miibus_readreg, axe_miibus_readreg),
232188412Sthompsa	DEVMETHOD(miibus_writereg, axe_miibus_writereg),
233188412Sthompsa	DEVMETHOD(miibus_statchg, axe_miibus_statchg),
234184610Salfred
235184610Salfred	{0, 0}
236184610Salfred};
237184610Salfred
238184610Salfredstatic driver_t axe_driver = {
239184610Salfred	.name = "axe",
240184610Salfred	.methods = axe_methods,
241184610Salfred	.size = sizeof(struct axe_softc),
242184610Salfred};
243184610Salfred
244184610Salfredstatic devclass_t axe_devclass;
245184610Salfred
246189275SthompsaDRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, 0);
247184610SalfredDRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
248188942SthompsaMODULE_DEPEND(axe, uether, 1, 1, 1);
249188942SthompsaMODULE_DEPEND(axe, usb, 1, 1, 1);
250188412SthompsaMODULE_DEPEND(axe, ether, 1, 1, 1);
251188412SthompsaMODULE_DEPEND(axe, miibus, 1, 1, 1);
252212122SthompsaMODULE_VERSION(axe, 1);
253184610Salfred
254192984Sthompsastatic const struct usb_ether_methods axe_ue_methods = {
255188412Sthompsa	.ue_attach_post = axe_attach_post,
256188412Sthompsa	.ue_start = axe_start,
257188412Sthompsa	.ue_init = axe_init,
258188412Sthompsa	.ue_stop = axe_stop,
259188412Sthompsa	.ue_tick = axe_tick,
260188412Sthompsa	.ue_setmulti = axe_setmulti,
261188412Sthompsa	.ue_setpromisc = axe_setpromisc,
262188412Sthompsa	.ue_mii_upd = axe_ifmedia_upd,
263188412Sthompsa	.ue_mii_sts = axe_ifmedia_sts,
264188412Sthompsa};
265188412Sthompsa
266188412Sthompsastatic int
267188412Sthompsaaxe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
268184610Salfred{
269192984Sthompsa	struct usb_device_request req;
270193045Sthompsa	usb_error_t err;
271184610Salfred
272188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
273188412Sthompsa
274184610Salfred	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
275184610Salfred	    UT_WRITE_VENDOR_DEVICE :
276184610Salfred	    UT_READ_VENDOR_DEVICE);
277184610Salfred	req.bRequest = AXE_CMD_CMD(cmd);
278184610Salfred	USETW(req.wValue, val);
279184610Salfred	USETW(req.wIndex, index);
280188412Sthompsa	USETW(req.wLength, AXE_CMD_LEN(cmd));
281184610Salfred
282194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, buf, 1000);
283184610Salfred
284188412Sthompsa	return (err);
285184610Salfred}
286184610Salfred
287184610Salfredstatic int
288188412Sthompsaaxe_miibus_readreg(device_t dev, int phy, int reg)
289184610Salfred{
290184610Salfred	struct axe_softc *sc = device_get_softc(dev);
291184610Salfred	uint16_t val;
292188412Sthompsa	int locked;
293184610Salfred
294188412Sthompsa	if (sc->sc_phyno != phy)
295188412Sthompsa		return (0);
296184610Salfred
297188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
298188412Sthompsa	if (!locked)
299188412Sthompsa		AXE_LOCK(sc);
300186730Salfred
301188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
302188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
303188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
304184610Salfred
305184610Salfred	val = le16toh(val);
306215968Syongari	if (AXE_IS_772(sc) && reg == MII_BMSR) {
307186730Salfred		/*
308186730Salfred		 * BMSR of AX88772 indicates that it supports extended
309186730Salfred		 * capability but the extended status register is
310186730Salfred		 * revered for embedded ethernet PHY. So clear the
311186730Salfred		 * extended capability bit of BMSR.
312186730Salfred		 */
313186730Salfred		val &= ~BMSR_EXTCAP;
314186730Salfred	}
315184610Salfred
316188412Sthompsa	if (!locked)
317188412Sthompsa		AXE_UNLOCK(sc);
318184610Salfred	return (val);
319184610Salfred}
320184610Salfred
321184610Salfredstatic int
322188412Sthompsaaxe_miibus_writereg(device_t dev, int phy, int reg, int val)
323184610Salfred{
324184610Salfred	struct axe_softc *sc = device_get_softc(dev);
325188412Sthompsa	int locked;
326184610Salfred
327189522Sthompsa	val = htole32(val);
328184610Salfred
329186730Salfred	if (sc->sc_phyno != phy)
330188412Sthompsa		return (0);
331186730Salfred
332188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
333188412Sthompsa	if (!locked)
334188412Sthompsa		AXE_LOCK(sc);
335184610Salfred
336188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
337188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
338188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
339188412Sthompsa
340188412Sthompsa	if (!locked)
341188412Sthompsa		AXE_UNLOCK(sc);
342184610Salfred	return (0);
343184610Salfred}
344184610Salfred
345184610Salfredstatic void
346188412Sthompsaaxe_miibus_statchg(device_t dev)
347184610Salfred{
348184610Salfred	struct axe_softc *sc = device_get_softc(dev);
349184610Salfred	struct mii_data *mii = GET_MII(sc);
350188553Sthompsa	struct ifnet *ifp;
351184610Salfred	uint16_t val;
352188412Sthompsa	int err, locked;
353184610Salfred
354188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
355188412Sthompsa	if (!locked)
356188412Sthompsa		AXE_LOCK(sc);
357184610Salfred
358194228Sthompsa	ifp = uether_getifp(&sc->sc_ue);
359188553Sthompsa	if (mii == NULL || ifp == NULL ||
360188553Sthompsa	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
361188553Sthompsa		goto done;
362188553Sthompsa
363188553Sthompsa	sc->sc_flags &= ~AXE_FLAG_LINK;
364188553Sthompsa	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
365188553Sthompsa	    (IFM_ACTIVE | IFM_AVALID)) {
366188553Sthompsa		switch (IFM_SUBTYPE(mii->mii_media_active)) {
367188553Sthompsa		case IFM_10_T:
368188553Sthompsa		case IFM_100_TX:
369188553Sthompsa			sc->sc_flags |= AXE_FLAG_LINK;
370188553Sthompsa			break;
371188553Sthompsa		case IFM_1000_T:
372188553Sthompsa			if ((sc->sc_flags & AXE_FLAG_178) == 0)
373188553Sthompsa				break;
374188553Sthompsa			sc->sc_flags |= AXE_FLAG_LINK;
375188553Sthompsa			break;
376188553Sthompsa		default:
377188553Sthompsa			break;
378188553Sthompsa		}
379188553Sthompsa	}
380188553Sthompsa
381188553Sthompsa	/* Lost link, do nothing. */
382188553Sthompsa	if ((sc->sc_flags & AXE_FLAG_LINK) == 0)
383188553Sthompsa		goto done;
384188553Sthompsa
385188553Sthompsa	val = 0;
386188553Sthompsa	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
387188553Sthompsa		val |= AXE_MEDIA_FULL_DUPLEX;
388215968Syongari	if (AXE_IS_178_FAMILY(sc)) {
389186730Salfred		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
390188553Sthompsa		if ((sc->sc_flags & AXE_FLAG_178) != 0)
391188553Sthompsa			val |= AXE_178_MEDIA_ENCK;
392184610Salfred		switch (IFM_SUBTYPE(mii->mii_media_active)) {
393184610Salfred		case IFM_1000_T:
394184610Salfred			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
395184610Salfred			break;
396184610Salfred		case IFM_100_TX:
397184610Salfred			val |= AXE_178_MEDIA_100TX;
398184610Salfred			break;
399184610Salfred		case IFM_10_T:
400184610Salfred			/* doesn't need to be handled */
401184610Salfred			break;
402184610Salfred		}
403184610Salfred	}
404188412Sthompsa	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
405188412Sthompsa	if (err)
406188412Sthompsa		device_printf(dev, "media change failed, error %d\n", err);
407188553Sthompsadone:
408188412Sthompsa	if (!locked)
409188412Sthompsa		AXE_UNLOCK(sc);
410184610Salfred}
411184610Salfred
412184610Salfred/*
413184610Salfred * Set media options.
414184610Salfred */
415184610Salfredstatic int
416188412Sthompsaaxe_ifmedia_upd(struct ifnet *ifp)
417184610Salfred{
418184610Salfred	struct axe_softc *sc = ifp->if_softc;
419184610Salfred	struct mii_data *mii = GET_MII(sc);
420221407Smarius	struct mii_softc *miisc;
421188553Sthompsa	int error;
422184610Salfred
423188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
424184610Salfred
425221407Smarius	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
426221407Smarius		PHY_RESET(miisc);
427188553Sthompsa	error = mii_mediachg(mii);
428188553Sthompsa	return (error);
429184610Salfred}
430184610Salfred
431184610Salfred/*
432184610Salfred * Report current media status.
433184610Salfred */
434184610Salfredstatic void
435188412Sthompsaaxe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
436184610Salfred{
437184610Salfred	struct axe_softc *sc = ifp->if_softc;
438188412Sthompsa	struct mii_data *mii = GET_MII(sc);
439184610Salfred
440188412Sthompsa	AXE_LOCK(sc);
441188412Sthompsa	mii_pollstat(mii);
442188412Sthompsa	AXE_UNLOCK(sc);
443188412Sthompsa	ifmr->ifm_active = mii->mii_media_active;
444188412Sthompsa	ifmr->ifm_status = mii->mii_media_status;
445184610Salfred}
446184610Salfred
447184610Salfredstatic void
448192984Sthompsaaxe_setmulti(struct usb_ether *ue)
449184610Salfred{
450194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
451194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
452188412Sthompsa	struct ifmultiaddr *ifma;
453188412Sthompsa	uint32_t h = 0;
454184610Salfred	uint16_t rxmode;
455188412Sthompsa	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
456184610Salfred
457188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
458184610Salfred
459188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
460184610Salfred	rxmode = le16toh(rxmode);
461184610Salfred
462188412Sthompsa	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
463184610Salfred		rxmode |= AXE_RXCMD_ALLMULTI;
464188412Sthompsa		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
465184610Salfred		return;
466184610Salfred	}
467184610Salfred	rxmode &= ~AXE_RXCMD_ALLMULTI;
468184610Salfred
469195049Srwatson	if_maddr_rlock(ifp);
470188412Sthompsa	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
471188412Sthompsa	{
472188412Sthompsa		if (ifma->ifma_addr->sa_family != AF_LINK)
473188412Sthompsa			continue;
474188412Sthompsa		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
475188412Sthompsa		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
476188412Sthompsa		hashtbl[h / 8] |= 1 << (h % 8);
477188412Sthompsa	}
478195049Srwatson	if_maddr_runlock(ifp);
479184610Salfred
480188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
481188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
482184610Salfred}
483184610Salfred
484186730Salfredstatic int
485186730Salfredaxe_get_phyno(struct axe_softc *sc, int sel)
486186730Salfred{
487188412Sthompsa	int phyno;
488186730Salfred
489186730Salfred	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
490186730Salfred	case PHY_TYPE_100_HOME:
491186730Salfred	case PHY_TYPE_GIG:
492188412Sthompsa		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
493186730Salfred		break;
494186730Salfred	case PHY_TYPE_SPECIAL:
495186730Salfred		/* FALLTHROUGH */
496186730Salfred	case PHY_TYPE_RSVD:
497186730Salfred		/* FALLTHROUGH */
498186730Salfred	case PHY_TYPE_NON_SUP:
499186730Salfred		/* FALLTHROUGH */
500186730Salfred	default:
501186730Salfred		phyno = -1;
502186730Salfred		break;
503186730Salfred	}
504186730Salfred
505186730Salfred	return (phyno);
506186730Salfred}
507186730Salfred
508212130Sthompsa#define	AXE_GPIO_WRITE(x, y)	do {				\
509212130Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL);		\
510212130Sthompsa	uether_pause(ue, (y));					\
511212130Sthompsa} while (0)
512212130Sthompsa
513184610Salfredstatic void
514188412Sthompsaaxe_ax88178_init(struct axe_softc *sc)
515184610Salfred{
516212130Sthompsa	struct usb_ether *ue;
517222581Syongari	int gpio0, ledmode, phymode;
518212130Sthompsa	uint16_t eeprom, val;
519184610Salfred
520212130Sthompsa	ue = &sc->sc_ue;
521188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
522184610Salfred	/* XXX magic */
523188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
524184610Salfred	eeprom = le16toh(eeprom);
525188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
526184610Salfred
527184610Salfred	/* if EEPROM is invalid we have to use to GPIO0 */
528184610Salfred	if (eeprom == 0xffff) {
529212130Sthompsa		phymode = AXE_PHY_MODE_MARVELL;
530184610Salfred		gpio0 = 1;
531222581Syongari		ledmode = 0;
532184610Salfred	} else {
533212130Sthompsa		phymode = eeprom & 0x7f;
534184610Salfred		gpio0 = (eeprom & 0x80) ? 0 : 1;
535222581Syongari		ledmode = eeprom >> 8;
536184610Salfred	}
537184610Salfred
538212130Sthompsa	if (bootverbose)
539215960Syongari		device_printf(sc->sc_ue.ue_dev,
540215960Syongari		    "EEPROM data : 0x%04x, phymode : 0x%02x\n", eeprom,
541215960Syongari		    phymode);
542212130Sthompsa	/* Program GPIOs depending on PHY hardware. */
543212130Sthompsa	switch (phymode) {
544212130Sthompsa	case AXE_PHY_MODE_MARVELL:
545212130Sthompsa		if (gpio0 == 1) {
546212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
547212130Sthompsa			    hz / 32);
548212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
549212130Sthompsa			    hz / 32);
550212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
551212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
552212130Sthompsa			    hz / 32);
553222581Syongari		} else {
554212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
555222581Syongari			    AXE_GPIO1_EN, hz / 3);
556222581Syongari			if (ledmode == 1) {
557222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
558222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
559222581Syongari				    hz / 3);
560222581Syongari			} else {
561222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
562222581Syongari				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
563222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
564222581Syongari				    AXE_GPIO2_EN, hz / 4);
565222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
566222581Syongari				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
567222581Syongari			}
568222581Syongari		}
569212130Sthompsa		break;
570212130Sthompsa	case AXE_PHY_MODE_CICADA:
571215960Syongari	case AXE_PHY_MODE_CICADA_V2:
572215960Syongari	case AXE_PHY_MODE_CICADA_V2_ASIX:
573212130Sthompsa		if (gpio0 == 1)
574212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
575212130Sthompsa			    AXE_GPIO0_EN, hz / 32);
576212130Sthompsa		else
577212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
578212130Sthompsa			    AXE_GPIO1_EN, hz / 32);
579212130Sthompsa		break;
580212130Sthompsa	case AXE_PHY_MODE_AGERE:
581212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
582212130Sthompsa		    AXE_GPIO1_EN, hz / 32);
583212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
584212130Sthompsa		    AXE_GPIO2_EN, hz / 32);
585212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
586212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
587212130Sthompsa		    AXE_GPIO2_EN, hz / 32);
588212130Sthompsa		break;
589212130Sthompsa	case AXE_PHY_MODE_REALTEK_8211CL:
590212130Sthompsa	case AXE_PHY_MODE_REALTEK_8211BN:
591212130Sthompsa	case AXE_PHY_MODE_REALTEK_8251CL:
592212130Sthompsa		val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
593212130Sthompsa		    AXE_GPIO1 | AXE_GPIO1_EN;
594212130Sthompsa		AXE_GPIO_WRITE(val, hz / 32);
595212130Sthompsa		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
596212130Sthompsa		AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
597212130Sthompsa		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
598212130Sthompsa		if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
599212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
600212130Sthompsa			    0x1F, 0x0005);
601212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
602212130Sthompsa			    0x0C, 0x0000);
603212130Sthompsa			val = axe_miibus_readreg(ue->ue_dev, sc->sc_phyno,
604212130Sthompsa			    0x0001);
605212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
606212130Sthompsa			    0x01, val | 0x0080);
607212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
608212130Sthompsa			    0x1F, 0x0000);
609212130Sthompsa		}
610212130Sthompsa		break;
611212130Sthompsa	default:
612212130Sthompsa		/* Unknown PHY model or no need to program GPIOs. */
613212130Sthompsa		break;
614184610Salfred	}
615184610Salfred
616184610Salfred	/* soft reset */
617188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
618212130Sthompsa	uether_pause(ue, hz / 4);
619184610Salfred
620188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
621184610Salfred	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
622212130Sthompsa	uether_pause(ue, hz / 4);
623186730Salfred	/* Enable MII/GMII/RGMII interface to work with external PHY. */
624188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
625212130Sthompsa	uether_pause(ue, hz / 4);
626184610Salfred
627188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
628184610Salfred}
629184610Salfred
630184610Salfredstatic void
631188412Sthompsaaxe_ax88772_init(struct axe_softc *sc)
632184610Salfred{
633188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
634194228Sthompsa	uether_pause(&sc->sc_ue, hz / 16);
635184610Salfred
636186730Salfred	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
637184610Salfred		/* ask for the embedded PHY */
638188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
639194228Sthompsa		uether_pause(&sc->sc_ue, hz / 64);
640184610Salfred
641184610Salfred		/* power down and reset state, pin reset state */
642188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
643184610Salfred		    AXE_SW_RESET_CLEAR, NULL);
644194228Sthompsa		uether_pause(&sc->sc_ue, hz / 16);
645184610Salfred
646184610Salfred		/* power down/reset state, pin operating state */
647188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
648184610Salfred		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
649194228Sthompsa		uether_pause(&sc->sc_ue, hz / 4);
650184610Salfred
651184610Salfred		/* power up, reset */
652188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
653184610Salfred
654184610Salfred		/* power up, operating */
655188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
656184610Salfred		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
657184610Salfred	} else {
658184610Salfred		/* ask for external PHY */
659188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
660194228Sthompsa		uether_pause(&sc->sc_ue, hz / 64);
661184610Salfred
662184610Salfred		/* power down internal PHY */
663188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
664184610Salfred		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
665184610Salfred	}
666184610Salfred
667194228Sthompsa	uether_pause(&sc->sc_ue, hz / 4);
668188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
669184610Salfred}
670184610Salfred
671184610Salfredstatic void
672215969Syongariaxe_ax88772a_init(struct axe_softc *sc)
673215969Syongari{
674215969Syongari	struct usb_ether *ue;
675215969Syongari	uint16_t eeprom;
676215969Syongari
677215969Syongari	ue = &sc->sc_ue;
678215969Syongari	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
679215969Syongari	eeprom = le16toh(eeprom);
680215969Syongari	/* Reload EEPROM. */
681215969Syongari	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
682215969Syongari	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
683215969Syongari		/* Manually select internal(embedded) PHY - MAC mode. */
684215969Syongari		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
685215969Syongari		    AXE_SW_PHY_SELECT_EMBEDDED | AXE_SW_PHY_SELECT_SS_MII,
686215969Syongari		    NULL);
687215969Syongari		uether_pause(&sc->sc_ue, hz / 32);
688215969Syongari	} else {
689215969Syongari		/*
690215969Syongari		 * Manually select external PHY - MAC mode.
691215969Syongari		 * Reverse MII/RMII is for AX88772A PHY mode.
692215969Syongari		 */
693215969Syongari		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
694215969Syongari		    AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
695215969Syongari		uether_pause(&sc->sc_ue, hz / 32);
696215969Syongari	}
697215969Syongari	/* Take PHY out of power down. */
698215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
699215969Syongari	    AXE_SW_RESET_IPRL, NULL);
700215969Syongari	uether_pause(&sc->sc_ue, hz / 4);
701215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
702215969Syongari	uether_pause(&sc->sc_ue, hz);
703215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
704215969Syongari	uether_pause(&sc->sc_ue, hz / 32);
705215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
706215969Syongari	uether_pause(&sc->sc_ue, hz / 32);
707215969Syongari	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
708215969Syongari}
709215969Syongari
710215969Syongari#undef	AXE_GPIO_WRITE
711215969Syongari
712215969Syongaristatic void
713188412Sthompsaaxe_reset(struct axe_softc *sc)
714184610Salfred{
715192984Sthompsa	struct usb_config_descriptor *cd;
716193045Sthompsa	usb_error_t err;
717184610Salfred
718194228Sthompsa	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
719184610Salfred
720194228Sthompsa	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
721188412Sthompsa	    cd->bConfigurationValue);
722188412Sthompsa	if (err)
723188412Sthompsa		DPRINTF("reset failed (ignored)\n");
724188412Sthompsa
725188412Sthompsa	/* Wait a little while for the chip to get its brains in order. */
726194228Sthompsa	uether_pause(&sc->sc_ue, hz / 100);
727215966Syongari
728215966Syongari	/* Reinitialize controller to achieve full reset. */
729215966Syongari	if (sc->sc_flags & AXE_FLAG_178)
730215966Syongari		axe_ax88178_init(sc);
731215966Syongari	else if (sc->sc_flags & AXE_FLAG_772)
732215966Syongari		axe_ax88772_init(sc);
733215969Syongari	else if (sc->sc_flags & AXE_FLAG_772A)
734215969Syongari		axe_ax88772a_init(sc);
735188412Sthompsa}
736188412Sthompsa
737188412Sthompsastatic void
738192984Sthompsaaxe_attach_post(struct usb_ether *ue)
739188412Sthompsa{
740194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
741188412Sthompsa
742184610Salfred	/*
743184610Salfred	 * Load PHY indexes first. Needed by axe_xxx_init().
744184610Salfred	 */
745188412Sthompsa	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
746212130Sthompsa	if (bootverbose)
747212130Sthompsa		device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
748212130Sthompsa		    sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
749186730Salfred	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
750186730Salfred	if (sc->sc_phyno == -1)
751186730Salfred		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
752186730Salfred	if (sc->sc_phyno == -1) {
753188412Sthompsa		device_printf(sc->sc_ue.ue_dev,
754188412Sthompsa		    "no valid PHY address found, assuming PHY address 0\n");
755186730Salfred		sc->sc_phyno = 0;
756186730Salfred	}
757184610Salfred
758215968Syongari	if (sc->sc_flags & AXE_FLAG_178) {
759188412Sthompsa		axe_ax88178_init(sc);
760215968Syongari		sc->sc_tx_bufsz = 16 * 1024;
761215968Syongari	} else if (sc->sc_flags & AXE_FLAG_772) {
762188412Sthompsa		axe_ax88772_init(sc);
763215968Syongari		sc->sc_tx_bufsz = 8 * 1024;
764215969Syongari	} else if (sc->sc_flags & AXE_FLAG_772A) {
765215969Syongari		axe_ax88772a_init(sc);
766215969Syongari		sc->sc_tx_bufsz = 8 * 1024;
767215968Syongari	}
768188412Sthompsa
769184610Salfred	/*
770184610Salfred	 * Get station address.
771184610Salfred	 */
772215968Syongari	if (AXE_IS_178_FAMILY(sc))
773188412Sthompsa		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
774184610Salfred	else
775188412Sthompsa		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
776184610Salfred
777184610Salfred	/*
778184610Salfred	 * Fetch IPG values.
779184610Salfred	 */
780215969Syongari	if (sc->sc_flags & AXE_FLAG_772A) {
781215969Syongari		/* Set IPG values. */
782215969Syongari		sc->sc_ipgs[0] = 0x15;
783215969Syongari		sc->sc_ipgs[1] = 0x16;
784215969Syongari		sc->sc_ipgs[2] = 0x1A;
785215969Syongari	} else
786215969Syongari		axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
787188412Sthompsa}
788184610Salfred
789188412Sthompsa/*
790188412Sthompsa * Probe for a AX88172 chip.
791188412Sthompsa */
792188412Sthompsastatic int
793188412Sthompsaaxe_probe(device_t dev)
794188412Sthompsa{
795192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
796184610Salfred
797192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
798188412Sthompsa		return (ENXIO);
799188412Sthompsa	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
800188412Sthompsa		return (ENXIO);
801188412Sthompsa	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
802188412Sthompsa		return (ENXIO);
803184610Salfred
804194228Sthompsa	return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
805188412Sthompsa}
806184610Salfred
807188412Sthompsa/*
808188412Sthompsa * Attach the interface. Allocate softc structures, do ifmedia
809188412Sthompsa * setup and ethernet/BPF attach.
810188412Sthompsa */
811188412Sthompsastatic int
812188412Sthompsaaxe_attach(device_t dev)
813188412Sthompsa{
814192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
815188412Sthompsa	struct axe_softc *sc = device_get_softc(dev);
816192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
817188412Sthompsa	uint8_t iface_index;
818188412Sthompsa	int error;
819184610Salfred
820188412Sthompsa	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
821184610Salfred
822194228Sthompsa	device_set_usb_desc(dev);
823184610Salfred
824188412Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
825184610Salfred
826188412Sthompsa	iface_index = AXE_IFACE_IDX;
827194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
828188412Sthompsa	    axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
829188412Sthompsa	if (error) {
830199816Sthompsa		device_printf(dev, "allocating USB transfers failed\n");
831188412Sthompsa		goto detach;
832188412Sthompsa	}
833184610Salfred
834188412Sthompsa	ue->ue_sc = sc;
835188412Sthompsa	ue->ue_dev = dev;
836188412Sthompsa	ue->ue_udev = uaa->device;
837188412Sthompsa	ue->ue_mtx = &sc->sc_mtx;
838188412Sthompsa	ue->ue_methods = &axe_ue_methods;
839184610Salfred
840194228Sthompsa	error = uether_ifattach(ue);
841184610Salfred	if (error) {
842188412Sthompsa		device_printf(dev, "could not attach interface\n");
843188412Sthompsa		goto detach;
844184610Salfred	}
845188412Sthompsa	return (0);			/* success */
846184610Salfred
847188412Sthompsadetach:
848188412Sthompsa	axe_detach(dev);
849188412Sthompsa	return (ENXIO);			/* failure */
850184610Salfred}
851184610Salfred
852184610Salfredstatic int
853184610Salfredaxe_detach(device_t dev)
854184610Salfred{
855184610Salfred	struct axe_softc *sc = device_get_softc(dev);
856192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
857184610Salfred
858194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
859194228Sthompsa	uether_ifdetach(ue);
860184610Salfred	mtx_destroy(&sc->sc_mtx);
861184610Salfred
862184610Salfred	return (0);
863184610Salfred}
864184610Salfred
865184610Salfred#if (AXE_BULK_BUF_SIZE >= 0x10000)
866184610Salfred#error "Please update axe_bulk_read_callback()!"
867184610Salfred#endif
868184610Salfred
869184610Salfredstatic void
870194677Sthompsaaxe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
871184610Salfred{
872194677Sthompsa	struct axe_softc *sc = usbd_xfer_softc(xfer);
873192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
874194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
875184610Salfred	struct axe_sframe_hdr hdr;
876194677Sthompsa	struct usb_page_cache *pc;
877197566Sthompsa	int err, pos, len;
878194677Sthompsa	int actlen;
879184610Salfred
880194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
881194677Sthompsa
882184610Salfred	switch (USB_GET_STATE(xfer)) {
883184610Salfred	case USB_ST_TRANSFERRED:
884184610Salfred		pos = 0;
885197566Sthompsa		len = 0;
886197566Sthompsa		err = 0;
887197566Sthompsa
888194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
889215968Syongari		if (AXE_IS_178_FAMILY(sc)) {
890197566Sthompsa			while (pos < actlen) {
891197566Sthompsa				if ((pos + sizeof(hdr)) > actlen) {
892184610Salfred					/* too little data */
893197566Sthompsa					err = EINVAL;
894184610Salfred					break;
895184610Salfred				}
896194677Sthompsa				usbd_copy_out(pc, pos, &hdr, sizeof(hdr));
897184610Salfred
898184610Salfred				if ((hdr.len ^ hdr.ilen) != 0xFFFF) {
899184610Salfred					/* we lost sync */
900197566Sthompsa					err = EINVAL;
901184610Salfred					break;
902184610Salfred				}
903184610Salfred				pos += sizeof(hdr);
904184610Salfred
905184610Salfred				len = le16toh(hdr.len);
906197566Sthompsa				if ((pos + len) > actlen) {
907184610Salfred					/* invalid length */
908197566Sthompsa					err = EINVAL;
909184610Salfred					break;
910184610Salfred				}
911213436Syongari				uether_rxbuf(ue, pc, pos, len);
912184610Salfred
913197566Sthompsa				pos += len + (len % 2);
914184610Salfred			}
915213436Syongari		} else
916213436Syongari			uether_rxbuf(ue, pc, 0, actlen);
917184610Salfred
918197566Sthompsa		if (err != 0)
919197566Sthompsa			ifp->if_ierrors++;
920184610Salfred
921188412Sthompsa		/* FALLTHROUGH */
922184610Salfred	case USB_ST_SETUP:
923184610Salfredtr_setup:
924194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
925194228Sthompsa		usbd_transfer_submit(xfer);
926194228Sthompsa		uether_rxflush(ue);
927184610Salfred		return;
928184610Salfred
929184610Salfred	default:			/* Error */
930194677Sthompsa		DPRINTF("bulk read error, %s\n", usbd_errstr(error));
931188412Sthompsa
932194677Sthompsa		if (error != USB_ERR_CANCELLED) {
933184610Salfred			/* try to clear stall first */
934194677Sthompsa			usbd_xfer_set_stall(xfer);
935188412Sthompsa			goto tr_setup;
936184610Salfred		}
937184610Salfred		return;
938184610Salfred
939184610Salfred	}
940184610Salfred}
941184610Salfred
942184610Salfred#if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
943184610Salfred#error "Please update axe_bulk_write_callback()!"
944184610Salfred#endif
945184610Salfred
946184610Salfredstatic void
947194677Sthompsaaxe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
948184610Salfred{
949194677Sthompsa	struct axe_softc *sc = usbd_xfer_softc(xfer);
950184610Salfred	struct axe_sframe_hdr hdr;
951194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
952194677Sthompsa	struct usb_page_cache *pc;
953184610Salfred	struct mbuf *m;
954216284Syongari	int nframes, pos;
955184610Salfred
956184610Salfred	switch (USB_GET_STATE(xfer)) {
957184610Salfred	case USB_ST_TRANSFERRED:
958184610Salfred		DPRINTFN(11, "transfer complete\n");
959213424Syongari		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
960188412Sthompsa		/* FALLTHROUGH */
961184610Salfred	case USB_ST_SETUP:
962188412Sthompsatr_setup:
963213424Syongari		if ((sc->sc_flags & AXE_FLAG_LINK) == 0 ||
964213424Syongari		    (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
965184610Salfred			/*
966213424Syongari			 * Don't send anything if there is no link or
967213424Syongari			 * controller is busy.
968184610Salfred			 */
969188412Sthompsa			return;
970184610Salfred		}
971184610Salfred
972216284Syongari		for (nframes = 0; nframes < 16 &&
973216284Syongari		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
974184610Salfred			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
975216284Syongari			if (m == NULL)
976216284Syongari				break;
977216284Syongari			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
978216284Syongari			    nframes);
979216284Syongari			pos = 0;
980216284Syongari			pc = usbd_xfer_get_frame(xfer, nframes);
981215968Syongari			if (AXE_IS_178_FAMILY(sc)) {
982184610Salfred				hdr.len = htole16(m->m_pkthdr.len);
983184610Salfred				hdr.ilen = ~hdr.len;
984194677Sthompsa				usbd_copy_in(pc, pos, &hdr, sizeof(hdr));
985184610Salfred				pos += sizeof(hdr);
986216284Syongari				usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
987216284Syongari				pos += m->m_pkthdr.len;
988216284Syongari				if ((pos % 512) == 0) {
989216284Syongari					hdr.len = 0;
990216284Syongari					hdr.ilen = 0xffff;
991216284Syongari					usbd_copy_in(pc, pos, &hdr,
992216284Syongari					    sizeof(hdr));
993216284Syongari					pos += sizeof(hdr);
994216284Syongari				}
995216284Syongari			} else {
996216284Syongari				usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
997216284Syongari				pos += m->m_pkthdr.len;
998184610Salfred			}
999184610Salfred
1000184610Salfred			/*
1001213423Syongari			 * XXX
1002213423Syongari			 * Update TX packet counter here. This is not
1003213423Syongari			 * correct way but it seems that there is no way
1004213423Syongari			 * to know how many packets are sent at the end
1005213423Syongari			 * of transfer because controller combines
1006213423Syongari			 * multiple writes into single one if there is
1007213423Syongari			 * room in TX buffer of controller.
1008213423Syongari			 */
1009213423Syongari			ifp->if_opackets++;
1010213423Syongari
1011213423Syongari			/*
1012188412Sthompsa			 * if there's a BPF listener, bounce a copy
1013188412Sthompsa			 * of this frame to him:
1014188412Sthompsa			 */
1015184610Salfred			BPF_MTAP(ifp, m);
1016184610Salfred
1017184610Salfred			m_freem(m);
1018184610Salfred
1019216284Syongari			/* Set frame length. */
1020216284Syongari			usbd_xfer_set_frame_len(xfer, nframes, pos);
1021184610Salfred		}
1022216284Syongari		if (nframes != 0) {
1023216284Syongari			usbd_xfer_set_frames(xfer, nframes);
1024216284Syongari			usbd_transfer_submit(xfer);
1025216284Syongari			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1026216284Syongari		}
1027184610Salfred		return;
1028216284Syongari		/* NOTREACHED */
1029184610Salfred	default:			/* Error */
1030184610Salfred		DPRINTFN(11, "transfer error, %s\n",
1031194677Sthompsa		    usbd_errstr(error));
1032184610Salfred
1033188412Sthompsa		ifp->if_oerrors++;
1034213424Syongari		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1035188412Sthompsa
1036194677Sthompsa		if (error != USB_ERR_CANCELLED) {
1037184610Salfred			/* try to clear stall first */
1038194677Sthompsa			usbd_xfer_set_stall(xfer);
1039188412Sthompsa			goto tr_setup;
1040184610Salfred		}
1041184610Salfred		return;
1042184610Salfred
1043184610Salfred	}
1044184610Salfred}
1045184610Salfred
1046184610Salfredstatic void
1047192984Sthompsaaxe_tick(struct usb_ether *ue)
1048184610Salfred{
1049194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1050184610Salfred	struct mii_data *mii = GET_MII(sc);
1051184610Salfred
1052188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1053188412Sthompsa
1054184610Salfred	mii_tick(mii);
1055188553Sthompsa	if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
1056188553Sthompsa		axe_miibus_statchg(ue->ue_dev);
1057188553Sthompsa		if ((sc->sc_flags & AXE_FLAG_LINK) != 0)
1058188553Sthompsa			axe_start(ue);
1059186730Salfred	}
1060184610Salfred}
1061184610Salfred
1062184610Salfredstatic void
1063192984Sthompsaaxe_start(struct usb_ether *ue)
1064184610Salfred{
1065194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1066184610Salfred
1067188412Sthompsa	/*
1068188412Sthompsa	 * start the USB transfers, if not already started:
1069188412Sthompsa	 */
1070194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
1071194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
1072184610Salfred}
1073184610Salfred
1074184610Salfredstatic void
1075192984Sthompsaaxe_init(struct usb_ether *ue)
1076184610Salfred{
1077194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1078194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1079184610Salfred	uint16_t rxmode;
1080184610Salfred
1081188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1082184610Salfred
1083215963Syongari	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1084215963Syongari		return;
1085215963Syongari
1086188412Sthompsa	/* Cancel pending I/O */
1087188412Sthompsa	axe_stop(ue);
1088184610Salfred
1089215962Syongari	axe_reset(sc);
1090215962Syongari
1091197567Sthompsa	/* Set MAC address. */
1092215968Syongari	if (AXE_IS_178_FAMILY(sc))
1093197567Sthompsa		axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
1094197567Sthompsa	else
1095197567Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
1096184610Salfred
1097184610Salfred	/* Set transmitter IPG values */
1098215968Syongari	if (AXE_IS_178_FAMILY(sc))
1099188412Sthompsa		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
1100184610Salfred		    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
1101215968Syongari	else {
1102188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
1103188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
1104188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
1105184610Salfred	}
1106184610Salfred
1107184610Salfred	/* Enable receiver, set RX mode */
1108184610Salfred	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1109215968Syongari	if (AXE_IS_178_FAMILY(sc)) {
1110197566Sthompsa#if 0
1111184610Salfred		rxmode |= AXE_178_RXCMD_MFB_2048;	/* chip default */
1112197566Sthompsa#else
1113197566Sthompsa		/*
1114197566Sthompsa		 * Default Rx buffer size is too small to get
1115197566Sthompsa		 * maximum performance.
1116197566Sthompsa		 */
1117197566Sthompsa		rxmode |= AXE_178_RXCMD_MFB_16384;
1118197566Sthompsa#endif
1119184610Salfred	} else {
1120184610Salfred		rxmode |= AXE_172_RXCMD_UNICAST;
1121184610Salfred	}
1122184610Salfred
1123184610Salfred	/* If we want promiscuous mode, set the allframes bit. */
1124188412Sthompsa	if (ifp->if_flags & IFF_PROMISC)
1125184610Salfred		rxmode |= AXE_RXCMD_PROMISC;
1126188412Sthompsa
1127188412Sthompsa	if (ifp->if_flags & IFF_BROADCAST)
1128184610Salfred		rxmode |= AXE_RXCMD_BROADCAST;
1129184610Salfred
1130188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1131188412Sthompsa
1132184610Salfred	/* Load the multicast filter. */
1133188412Sthompsa	axe_setmulti(ue);
1134184610Salfred
1135194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
1136184610Salfred
1137188412Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1138215964Syongari	/* Switch to selected media. */
1139215964Syongari	axe_ifmedia_upd(ifp);
1140188412Sthompsa	axe_start(ue);
1141184610Salfred}
1142184610Salfred
1143184610Salfredstatic void
1144192984Sthompsaaxe_setpromisc(struct usb_ether *ue)
1145184610Salfred{
1146194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1147194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1148184610Salfred	uint16_t rxmode;
1149184610Salfred
1150188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
1151184610Salfred
1152184610Salfred	rxmode = le16toh(rxmode);
1153184610Salfred
1154188412Sthompsa	if (ifp->if_flags & IFF_PROMISC) {
1155184610Salfred		rxmode |= AXE_RXCMD_PROMISC;
1156184610Salfred	} else {
1157184610Salfred		rxmode &= ~AXE_RXCMD_PROMISC;
1158184610Salfred	}
1159184610Salfred
1160188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1161184610Salfred
1162188412Sthompsa	axe_setmulti(ue);
1163184610Salfred}
1164184610Salfred
1165184610Salfredstatic void
1166192984Sthompsaaxe_stop(struct usb_ether *ue)
1167184610Salfred{
1168194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1169194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1170184610Salfred
1171188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1172184610Salfred
1173213424Syongari	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1174186730Salfred	sc->sc_flags &= ~AXE_FLAG_LINK;
1175184610Salfred
1176184610Salfred	/*
1177184610Salfred	 * stop all the transfers, if not already stopped:
1178184610Salfred	 */
1179194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1180194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
1181184610Salfred}
1182