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$");
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/param.h>
80194677Sthompsa#include <sys/systm.h>
81229106Shselasky#include <sys/bus.h>
82229106Shselasky#include <sys/condvar.h>
83229106Shselasky#include <sys/endian.h>
84194677Sthompsa#include <sys/kernel.h>
85229106Shselasky#include <sys/lock.h>
86229106Shselasky#include <sys/malloc.h>
87229106Shselasky#include <sys/mbuf.h>
88194677Sthompsa#include <sys/module.h>
89194677Sthompsa#include <sys/mutex.h>
90229106Shselasky#include <sys/socket.h>
91229106Shselasky#include <sys/sockio.h>
92194677Sthompsa#include <sys/sysctl.h>
93194677Sthompsa#include <sys/sx.h>
94194677Sthompsa
95229106Shselasky#include <net/if.h>
96229106Shselasky#include <net/ethernet.h>
97229106Shselasky#include <net/if_types.h>
98229106Shselasky#include <net/if_media.h>
99229106Shselasky#include <net/if_vlan_var.h>
100229106Shselasky
101229106Shselasky#include <dev/mii/mii.h>
102229106Shselasky#include <dev/mii/miivar.h>
103229106Shselasky
104194677Sthompsa#include <dev/usb/usb.h>
105194677Sthompsa#include <dev/usb/usbdi.h>
106194677Sthompsa#include <dev/usb/usbdi_util.h>
107188746Sthompsa#include "usbdevs.h"
108184610Salfred
109184610Salfred#define	USB_DEBUG_VAR axe_debug
110194677Sthompsa#include <dev/usb/usb_debug.h>
111188942Sthompsa#include <dev/usb/usb_process.h>
112184610Salfred
113188942Sthompsa#include <dev/usb/net/usb_ethernet.h>
114188942Sthompsa#include <dev/usb/net/if_axereg.h>
115184610Salfred
116188412Sthompsa/*
117188412Sthompsa * AXE_178_MAX_FRAME_BURST
118188412Sthompsa * max frame burst size for Ax88178 and Ax88772
119188412Sthompsa *	0	2048 bytes
120188412Sthompsa *	1	4096 bytes
121188412Sthompsa *	2	8192 bytes
122188412Sthompsa *	3	16384 bytes
123188412Sthompsa * use the largest your system can handle without USB stalling.
124188412Sthompsa *
125188412Sthompsa * NB: 88772 parts appear to generate lots of input errors with
126188412Sthompsa * a 2K rx buffer and 8K is only slightly faster than 4K on an
127188412Sthompsa * EHCI port on a T42 so change at your own risk.
128188412Sthompsa */
129188412Sthompsa#define AXE_178_MAX_FRAME_BURST	1
130184610Salfred
131229106Shselasky#define	AXE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
132229106Shselasky
133207077Sthompsa#ifdef USB_DEBUG
134184610Salfredstatic int axe_debug = 0;
135184610Salfred
136248085Smariusstatic SYSCTL_NODE(_hw_usb, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
137192502SthompsaSYSCTL_INT(_hw_usb_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
138184610Salfred    "Debug level");
139184610Salfred#endif
140184610Salfred
141184610Salfred/*
142184610Salfred * Various supported device vendors/products.
143184610Salfred */
144223486Shselaskystatic const STRUCT_USB_HOST_ID axe_devs[] = {
145201028Sthompsa#define	AXE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
146201028Sthompsa	AXE_DEV(ABOCOM, UF200, 0),
147201028Sthompsa	AXE_DEV(ACERCM, EP1427X2, 0),
148201028Sthompsa	AXE_DEV(APPLE, ETHERNET, AXE_FLAG_772),
149201028Sthompsa	AXE_DEV(ASIX, AX88172, 0),
150201028Sthompsa	AXE_DEV(ASIX, AX88178, AXE_FLAG_178),
151201028Sthompsa	AXE_DEV(ASIX, AX88772, AXE_FLAG_772),
152215969Syongari	AXE_DEV(ASIX, AX88772A, AXE_FLAG_772A),
153224020Syongari	AXE_DEV(ASIX, AX88772B, AXE_FLAG_772B),
154229116Shselasky	AXE_DEV(ASIX, AX88772B_1, AXE_FLAG_772B),
155201028Sthompsa	AXE_DEV(ATEN, UC210T, 0),
156201028Sthompsa	AXE_DEV(BELKIN, F5D5055, AXE_FLAG_178),
157201028Sthompsa	AXE_DEV(BILLIONTON, USB2AR, 0),
158215969Syongari	AXE_DEV(CISCOLINKSYS, USB200MV2, AXE_FLAG_772A),
159201028Sthompsa	AXE_DEV(COREGA, FETHER_USB2_TX, 0),
160201028Sthompsa	AXE_DEV(DLINK, DUBE100, 0),
161201028Sthompsa	AXE_DEV(DLINK, DUBE100B1, AXE_FLAG_772),
162263166Shselasky	AXE_DEV(DLINK, DUBE100C1, AXE_FLAG_772B),
163201028Sthompsa	AXE_DEV(GOODWAY, GWUSB2E, 0),
164201028Sthompsa	AXE_DEV(IODATA, ETGUS2, AXE_FLAG_178),
165201028Sthompsa	AXE_DEV(JVC, MP_PRX1, 0),
166252444Syongari	AXE_DEV(LENOVO, ETHERNET, AXE_FLAG_772B),
167201028Sthompsa	AXE_DEV(LINKSYS2, USB200M, 0),
168201028Sthompsa	AXE_DEV(LINKSYS4, USB1000, AXE_FLAG_178),
169212980Ssanpei	AXE_DEV(LOGITEC, LAN_GTJU2A, AXE_FLAG_178),
170201028Sthompsa	AXE_DEV(MELCO, LUAU2KTX, 0),
171212980Ssanpei	AXE_DEV(MELCO, LUA3U2AGT, AXE_FLAG_178),
172201028Sthompsa	AXE_DEV(NETGEAR, FA120, 0),
173201028Sthompsa	AXE_DEV(OQO, ETHER01PLUS, AXE_FLAG_772),
174201028Sthompsa	AXE_DEV(PLANEX3, GU1000T, AXE_FLAG_178),
175201028Sthompsa	AXE_DEV(SITECOM, LN029, 0),
176201028Sthompsa	AXE_DEV(SITECOMEU, LN028, AXE_FLAG_178),
177201028Sthompsa	AXE_DEV(SYSTEMTALKS, SGCX2UL, 0),
178201028Sthompsa#undef AXE_DEV
179184610Salfred};
180184610Salfred
181184610Salfredstatic device_probe_t axe_probe;
182184610Salfredstatic device_attach_t axe_attach;
183184610Salfredstatic device_detach_t axe_detach;
184184610Salfred
185193045Sthompsastatic usb_callback_t axe_bulk_read_callback;
186193045Sthompsastatic usb_callback_t axe_bulk_write_callback;
187184610Salfred
188188412Sthompsastatic miibus_readreg_t axe_miibus_readreg;
189188412Sthompsastatic miibus_writereg_t axe_miibus_writereg;
190188412Sthompsastatic miibus_statchg_t axe_miibus_statchg;
191184610Salfred
192193045Sthompsastatic uether_fn_t axe_attach_post;
193193045Sthompsastatic uether_fn_t axe_init;
194193045Sthompsastatic uether_fn_t axe_stop;
195193045Sthompsastatic uether_fn_t axe_start;
196193045Sthompsastatic uether_fn_t axe_tick;
197193045Sthompsastatic uether_fn_t axe_setmulti;
198193045Sthompsastatic uether_fn_t axe_setpromisc;
199184610Salfred
200229106Shselaskystatic int	axe_attach_post_sub(struct usb_ether *);
201188412Sthompsastatic int	axe_ifmedia_upd(struct ifnet *);
202188412Sthompsastatic void	axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
203188412Sthompsastatic int	axe_cmd(struct axe_softc *, int, int, int, void *);
204188412Sthompsastatic void	axe_ax88178_init(struct axe_softc *);
205188412Sthompsastatic void	axe_ax88772_init(struct axe_softc *);
206224020Syongaristatic void	axe_ax88772_phywake(struct axe_softc *);
207215969Syongaristatic void	axe_ax88772a_init(struct axe_softc *);
208224020Syongaristatic void	axe_ax88772b_init(struct axe_softc *);
209186730Salfredstatic int	axe_get_phyno(struct axe_softc *, int);
210229106Shselaskystatic int	axe_ioctl(struct ifnet *, u_long, caddr_t);
211229106Shselaskystatic int	axe_rx_frame(struct usb_ether *, struct usb_page_cache *, int);
212229106Shselaskystatic int	axe_rxeof(struct usb_ether *, struct usb_page_cache *,
213229106Shselasky		    unsigned int offset, unsigned int, struct axe_csum_hdr *);
214229106Shselaskystatic void	axe_csum_cfg(struct usb_ether *);
215184610Salfred
216192984Sthompsastatic const struct usb_config axe_config[AXE_N_TRANSFER] = {
217184610Salfred
218187259Sthompsa	[AXE_BULK_DT_WR] = {
219184610Salfred		.type = UE_BULK,
220184610Salfred		.endpoint = UE_ADDR_ANY,
221184610Salfred		.direction = UE_DIR_OUT,
222216284Syongari		.frames = 16,
223216284Syongari		.bufsize = 16 * MCLBYTES,
224190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
225190734Sthompsa		.callback = axe_bulk_write_callback,
226190734Sthompsa		.timeout = 10000,	/* 10 seconds */
227184610Salfred	},
228184610Salfred
229187259Sthompsa	[AXE_BULK_DT_RD] = {
230184610Salfred		.type = UE_BULK,
231184610Salfred		.endpoint = UE_ADDR_ANY,
232184610Salfred		.direction = UE_DIR_IN,
233197566Sthompsa		.bufsize = 16384,	/* bytes */
234190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
235190734Sthompsa		.callback = axe_bulk_read_callback,
236190734Sthompsa		.timeout = 0,	/* no timeout */
237184610Salfred	},
238184610Salfred};
239184610Salfred
240224020Syongaristatic const struct ax88772b_mfb ax88772b_mfb_table[] = {
241224020Syongari	{ 0x8000, 0x8001, 2048 },
242224020Syongari	{ 0x8100, 0x8147, 4096},
243224020Syongari	{ 0x8200, 0x81EB, 6144},
244224020Syongari	{ 0x8300, 0x83D7, 8192},
245224020Syongari	{ 0x8400, 0x851E, 16384},
246224020Syongari	{ 0x8500, 0x8666, 20480},
247224020Syongari	{ 0x8600, 0x87AE, 24576},
248224020Syongari	{ 0x8700, 0x8A3D, 32768}
249224020Syongari};
250224020Syongari
251184610Salfredstatic device_method_t axe_methods[] = {
252184610Salfred	/* Device interface */
253184610Salfred	DEVMETHOD(device_probe, axe_probe),
254184610Salfred	DEVMETHOD(device_attach, axe_attach),
255184610Salfred	DEVMETHOD(device_detach, axe_detach),
256184610Salfred
257184610Salfred	/* MII interface */
258188412Sthompsa	DEVMETHOD(miibus_readreg, axe_miibus_readreg),
259188412Sthompsa	DEVMETHOD(miibus_writereg, axe_miibus_writereg),
260188412Sthompsa	DEVMETHOD(miibus_statchg, axe_miibus_statchg),
261184610Salfred
262229093Shselasky	DEVMETHOD_END
263184610Salfred};
264184610Salfred
265184610Salfredstatic driver_t axe_driver = {
266184610Salfred	.name = "axe",
267184610Salfred	.methods = axe_methods,
268184610Salfred	.size = sizeof(struct axe_softc),
269184610Salfred};
270184610Salfred
271184610Salfredstatic devclass_t axe_devclass;
272184610Salfred
273189275SthompsaDRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, 0);
274184610SalfredDRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
275188942SthompsaMODULE_DEPEND(axe, uether, 1, 1, 1);
276188942SthompsaMODULE_DEPEND(axe, usb, 1, 1, 1);
277188412SthompsaMODULE_DEPEND(axe, ether, 1, 1, 1);
278188412SthompsaMODULE_DEPEND(axe, miibus, 1, 1, 1);
279212122SthompsaMODULE_VERSION(axe, 1);
280184610Salfred
281192984Sthompsastatic const struct usb_ether_methods axe_ue_methods = {
282188412Sthompsa	.ue_attach_post = axe_attach_post,
283229106Shselasky	.ue_attach_post_sub = axe_attach_post_sub,
284188412Sthompsa	.ue_start = axe_start,
285188412Sthompsa	.ue_init = axe_init,
286188412Sthompsa	.ue_stop = axe_stop,
287188412Sthompsa	.ue_tick = axe_tick,
288188412Sthompsa	.ue_setmulti = axe_setmulti,
289188412Sthompsa	.ue_setpromisc = axe_setpromisc,
290188412Sthompsa	.ue_mii_upd = axe_ifmedia_upd,
291188412Sthompsa	.ue_mii_sts = axe_ifmedia_sts,
292188412Sthompsa};
293188412Sthompsa
294188412Sthompsastatic int
295188412Sthompsaaxe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
296184610Salfred{
297192984Sthompsa	struct usb_device_request req;
298193045Sthompsa	usb_error_t err;
299184610Salfred
300188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
301188412Sthompsa
302184610Salfred	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
303184610Salfred	    UT_WRITE_VENDOR_DEVICE :
304184610Salfred	    UT_READ_VENDOR_DEVICE);
305184610Salfred	req.bRequest = AXE_CMD_CMD(cmd);
306184610Salfred	USETW(req.wValue, val);
307184610Salfred	USETW(req.wIndex, index);
308188412Sthompsa	USETW(req.wLength, AXE_CMD_LEN(cmd));
309184610Salfred
310194228Sthompsa	err = uether_do_request(&sc->sc_ue, &req, buf, 1000);
311184610Salfred
312188412Sthompsa	return (err);
313184610Salfred}
314184610Salfred
315184610Salfredstatic int
316188412Sthompsaaxe_miibus_readreg(device_t dev, int phy, int reg)
317184610Salfred{
318184610Salfred	struct axe_softc *sc = device_get_softc(dev);
319184610Salfred	uint16_t val;
320188412Sthompsa	int locked;
321184610Salfred
322188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
323188412Sthompsa	if (!locked)
324188412Sthompsa		AXE_LOCK(sc);
325186730Salfred
326188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
327188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
328188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
329184610Salfred
330184610Salfred	val = le16toh(val);
331215968Syongari	if (AXE_IS_772(sc) && reg == MII_BMSR) {
332186730Salfred		/*
333186730Salfred		 * BMSR of AX88772 indicates that it supports extended
334186730Salfred		 * capability but the extended status register is
335186730Salfred		 * revered for embedded ethernet PHY. So clear the
336186730Salfred		 * extended capability bit of BMSR.
337186730Salfred		 */
338186730Salfred		val &= ~BMSR_EXTCAP;
339186730Salfred	}
340184610Salfred
341188412Sthompsa	if (!locked)
342188412Sthompsa		AXE_UNLOCK(sc);
343184610Salfred	return (val);
344184610Salfred}
345184610Salfred
346184610Salfredstatic int
347188412Sthompsaaxe_miibus_writereg(device_t dev, int phy, int reg, int val)
348184610Salfred{
349184610Salfred	struct axe_softc *sc = device_get_softc(dev);
350188412Sthompsa	int locked;
351184610Salfred
352189522Sthompsa	val = htole32(val);
353188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
354188412Sthompsa	if (!locked)
355188412Sthompsa		AXE_LOCK(sc);
356184610Salfred
357188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
358188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
359188412Sthompsa	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
360188412Sthompsa
361188412Sthompsa	if (!locked)
362188412Sthompsa		AXE_UNLOCK(sc);
363184610Salfred	return (0);
364184610Salfred}
365184610Salfred
366184610Salfredstatic void
367188412Sthompsaaxe_miibus_statchg(device_t dev)
368184610Salfred{
369184610Salfred	struct axe_softc *sc = device_get_softc(dev);
370184610Salfred	struct mii_data *mii = GET_MII(sc);
371188553Sthompsa	struct ifnet *ifp;
372184610Salfred	uint16_t val;
373188412Sthompsa	int err, locked;
374184610Salfred
375188412Sthompsa	locked = mtx_owned(&sc->sc_mtx);
376188412Sthompsa	if (!locked)
377188412Sthompsa		AXE_LOCK(sc);
378184610Salfred
379194228Sthompsa	ifp = uether_getifp(&sc->sc_ue);
380188553Sthompsa	if (mii == NULL || ifp == NULL ||
381188553Sthompsa	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
382188553Sthompsa		goto done;
383229106Shselasky
384188553Sthompsa	sc->sc_flags &= ~AXE_FLAG_LINK;
385188553Sthompsa	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
386188553Sthompsa	    (IFM_ACTIVE | IFM_AVALID)) {
387188553Sthompsa		switch (IFM_SUBTYPE(mii->mii_media_active)) {
388188553Sthompsa		case IFM_10_T:
389188553Sthompsa		case IFM_100_TX:
390188553Sthompsa			sc->sc_flags |= AXE_FLAG_LINK;
391188553Sthompsa			break;
392188553Sthompsa		case IFM_1000_T:
393188553Sthompsa			if ((sc->sc_flags & AXE_FLAG_178) == 0)
394188553Sthompsa				break;
395188553Sthompsa			sc->sc_flags |= AXE_FLAG_LINK;
396188553Sthompsa			break;
397188553Sthompsa		default:
398188553Sthompsa			break;
399188553Sthompsa		}
400188553Sthompsa	}
401229106Shselasky
402188553Sthompsa	/* Lost link, do nothing. */
403188553Sthompsa	if ((sc->sc_flags & AXE_FLAG_LINK) == 0)
404188553Sthompsa		goto done;
405229106Shselasky
406188553Sthompsa	val = 0;
407229106Shselasky	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
408188553Sthompsa		val |= AXE_MEDIA_FULL_DUPLEX;
409229106Shselasky		if (AXE_IS_178_FAMILY(sc)) {
410229106Shselasky			if ((IFM_OPTIONS(mii->mii_media_active) &
411229106Shselasky			    IFM_ETH_TXPAUSE) != 0)
412229106Shselasky				val |= AXE_178_MEDIA_TXFLOW_CONTROL_EN;
413229106Shselasky			if ((IFM_OPTIONS(mii->mii_media_active) &
414229106Shselasky			    IFM_ETH_RXPAUSE) != 0)
415229106Shselasky				val |= AXE_178_MEDIA_RXFLOW_CONTROL_EN;
416229106Shselasky		}
417229106Shselasky	}
418215968Syongari	if (AXE_IS_178_FAMILY(sc)) {
419186730Salfred		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
420188553Sthompsa		if ((sc->sc_flags & AXE_FLAG_178) != 0)
421188553Sthompsa			val |= AXE_178_MEDIA_ENCK;
422184610Salfred		switch (IFM_SUBTYPE(mii->mii_media_active)) {
423184610Salfred		case IFM_1000_T:
424184610Salfred			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
425184610Salfred			break;
426184610Salfred		case IFM_100_TX:
427184610Salfred			val |= AXE_178_MEDIA_100TX;
428184610Salfred			break;
429184610Salfred		case IFM_10_T:
430184610Salfred			/* doesn't need to be handled */
431184610Salfred			break;
432184610Salfred		}
433184610Salfred	}
434188412Sthompsa	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
435188412Sthompsa	if (err)
436188412Sthompsa		device_printf(dev, "media change failed, error %d\n", err);
437188553Sthompsadone:
438188412Sthompsa	if (!locked)
439188412Sthompsa		AXE_UNLOCK(sc);
440184610Salfred}
441184610Salfred
442184610Salfred/*
443184610Salfred * Set media options.
444184610Salfred */
445184610Salfredstatic int
446188412Sthompsaaxe_ifmedia_upd(struct ifnet *ifp)
447184610Salfred{
448184610Salfred	struct axe_softc *sc = ifp->if_softc;
449184610Salfred	struct mii_data *mii = GET_MII(sc);
450221407Smarius	struct mii_softc *miisc;
451188553Sthompsa	int error;
452184610Salfred
453188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
454184610Salfred
455221407Smarius	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
456221407Smarius		PHY_RESET(miisc);
457188553Sthompsa	error = mii_mediachg(mii);
458188553Sthompsa	return (error);
459184610Salfred}
460184610Salfred
461184610Salfred/*
462184610Salfred * Report current media status.
463184610Salfred */
464184610Salfredstatic void
465188412Sthompsaaxe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
466184610Salfred{
467184610Salfred	struct axe_softc *sc = ifp->if_softc;
468188412Sthompsa	struct mii_data *mii = GET_MII(sc);
469184610Salfred
470188412Sthompsa	AXE_LOCK(sc);
471188412Sthompsa	mii_pollstat(mii);
472188412Sthompsa	ifmr->ifm_active = mii->mii_media_active;
473188412Sthompsa	ifmr->ifm_status = mii->mii_media_status;
474229060Syongari	AXE_UNLOCK(sc);
475184610Salfred}
476184610Salfred
477184610Salfredstatic void
478192984Sthompsaaxe_setmulti(struct usb_ether *ue)
479184610Salfred{
480194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
481194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
482188412Sthompsa	struct ifmultiaddr *ifma;
483188412Sthompsa	uint32_t h = 0;
484184610Salfred	uint16_t rxmode;
485188412Sthompsa	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
486184610Salfred
487188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
488184610Salfred
489188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
490184610Salfred	rxmode = le16toh(rxmode);
491184610Salfred
492188412Sthompsa	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
493184610Salfred		rxmode |= AXE_RXCMD_ALLMULTI;
494188412Sthompsa		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
495184610Salfred		return;
496184610Salfred	}
497184610Salfred	rxmode &= ~AXE_RXCMD_ALLMULTI;
498184610Salfred
499195049Srwatson	if_maddr_rlock(ifp);
500188412Sthompsa	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
501188412Sthompsa	{
502188412Sthompsa		if (ifma->ifma_addr->sa_family != AF_LINK)
503188412Sthompsa			continue;
504188412Sthompsa		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
505188412Sthompsa		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
506188412Sthompsa		hashtbl[h / 8] |= 1 << (h % 8);
507188412Sthompsa	}
508195049Srwatson	if_maddr_runlock(ifp);
509184610Salfred
510188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
511188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
512184610Salfred}
513184610Salfred
514186730Salfredstatic int
515186730Salfredaxe_get_phyno(struct axe_softc *sc, int sel)
516186730Salfred{
517188412Sthompsa	int phyno;
518186730Salfred
519186730Salfred	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
520186730Salfred	case PHY_TYPE_100_HOME:
521186730Salfred	case PHY_TYPE_GIG:
522188412Sthompsa		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
523186730Salfred		break;
524186730Salfred	case PHY_TYPE_SPECIAL:
525186730Salfred		/* FALLTHROUGH */
526186730Salfred	case PHY_TYPE_RSVD:
527186730Salfred		/* FALLTHROUGH */
528186730Salfred	case PHY_TYPE_NON_SUP:
529186730Salfred		/* FALLTHROUGH */
530186730Salfred	default:
531186730Salfred		phyno = -1;
532186730Salfred		break;
533186730Salfred	}
534186730Salfred
535186730Salfred	return (phyno);
536186730Salfred}
537186730Salfred
538212130Sthompsa#define	AXE_GPIO_WRITE(x, y)	do {				\
539212130Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL);		\
540212130Sthompsa	uether_pause(ue, (y));					\
541212130Sthompsa} while (0)
542212130Sthompsa
543184610Salfredstatic void
544188412Sthompsaaxe_ax88178_init(struct axe_softc *sc)
545184610Salfred{
546212130Sthompsa	struct usb_ether *ue;
547222581Syongari	int gpio0, ledmode, phymode;
548212130Sthompsa	uint16_t eeprom, val;
549184610Salfred
550212130Sthompsa	ue = &sc->sc_ue;
551188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
552184610Salfred	/* XXX magic */
553188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
554184610Salfred	eeprom = le16toh(eeprom);
555188412Sthompsa	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
556184610Salfred
557184610Salfred	/* if EEPROM is invalid we have to use to GPIO0 */
558184610Salfred	if (eeprom == 0xffff) {
559212130Sthompsa		phymode = AXE_PHY_MODE_MARVELL;
560184610Salfred		gpio0 = 1;
561222581Syongari		ledmode = 0;
562184610Salfred	} else {
563212130Sthompsa		phymode = eeprom & 0x7f;
564184610Salfred		gpio0 = (eeprom & 0x80) ? 0 : 1;
565222581Syongari		ledmode = eeprom >> 8;
566184610Salfred	}
567184610Salfred
568212130Sthompsa	if (bootverbose)
569215960Syongari		device_printf(sc->sc_ue.ue_dev,
570215960Syongari		    "EEPROM data : 0x%04x, phymode : 0x%02x\n", eeprom,
571215960Syongari		    phymode);
572212130Sthompsa	/* Program GPIOs depending on PHY hardware. */
573212130Sthompsa	switch (phymode) {
574212130Sthompsa	case AXE_PHY_MODE_MARVELL:
575212130Sthompsa		if (gpio0 == 1) {
576212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
577212130Sthompsa			    hz / 32);
578212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
579212130Sthompsa			    hz / 32);
580212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
581212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
582212130Sthompsa			    hz / 32);
583222581Syongari		} else {
584212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
585222581Syongari			    AXE_GPIO1_EN, hz / 3);
586222581Syongari			if (ledmode == 1) {
587222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
588222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
589222581Syongari				    hz / 3);
590222581Syongari			} else {
591222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
592222581Syongari				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
593222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
594222581Syongari				    AXE_GPIO2_EN, hz / 4);
595222581Syongari				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
596222581Syongari				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
597222581Syongari			}
598222581Syongari		}
599212130Sthompsa		break;
600212130Sthompsa	case AXE_PHY_MODE_CICADA:
601215960Syongari	case AXE_PHY_MODE_CICADA_V2:
602215960Syongari	case AXE_PHY_MODE_CICADA_V2_ASIX:
603212130Sthompsa		if (gpio0 == 1)
604212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
605212130Sthompsa			    AXE_GPIO0_EN, hz / 32);
606212130Sthompsa		else
607212130Sthompsa			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
608212130Sthompsa			    AXE_GPIO1_EN, hz / 32);
609212130Sthompsa		break;
610212130Sthompsa	case AXE_PHY_MODE_AGERE:
611212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
612212130Sthompsa		    AXE_GPIO1_EN, hz / 32);
613212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
614212130Sthompsa		    AXE_GPIO2_EN, hz / 32);
615212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
616212130Sthompsa		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
617212130Sthompsa		    AXE_GPIO2_EN, hz / 32);
618212130Sthompsa		break;
619212130Sthompsa	case AXE_PHY_MODE_REALTEK_8211CL:
620212130Sthompsa	case AXE_PHY_MODE_REALTEK_8211BN:
621212130Sthompsa	case AXE_PHY_MODE_REALTEK_8251CL:
622212130Sthompsa		val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
623212130Sthompsa		    AXE_GPIO1 | AXE_GPIO1_EN;
624212130Sthompsa		AXE_GPIO_WRITE(val, hz / 32);
625212130Sthompsa		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
626212130Sthompsa		AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
627212130Sthompsa		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
628212130Sthompsa		if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
629212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
630212130Sthompsa			    0x1F, 0x0005);
631212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
632212130Sthompsa			    0x0C, 0x0000);
633212130Sthompsa			val = axe_miibus_readreg(ue->ue_dev, sc->sc_phyno,
634212130Sthompsa			    0x0001);
635212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
636212130Sthompsa			    0x01, val | 0x0080);
637212130Sthompsa			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
638212130Sthompsa			    0x1F, 0x0000);
639212130Sthompsa		}
640212130Sthompsa		break;
641212130Sthompsa	default:
642212130Sthompsa		/* Unknown PHY model or no need to program GPIOs. */
643212130Sthompsa		break;
644184610Salfred	}
645184610Salfred
646184610Salfred	/* soft reset */
647188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
648212130Sthompsa	uether_pause(ue, hz / 4);
649184610Salfred
650188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
651184610Salfred	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
652212130Sthompsa	uether_pause(ue, hz / 4);
653186730Salfred	/* Enable MII/GMII/RGMII interface to work with external PHY. */
654188412Sthompsa	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
655212130Sthompsa	uether_pause(ue, hz / 4);
656184610Salfred
657188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
658184610Salfred}
659184610Salfred
660184610Salfredstatic void
661188412Sthompsaaxe_ax88772_init(struct axe_softc *sc)
662184610Salfred{
663188412Sthompsa	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
664194228Sthompsa	uether_pause(&sc->sc_ue, hz / 16);
665184610Salfred
666186730Salfred	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
667184610Salfred		/* ask for the embedded PHY */
668188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
669194228Sthompsa		uether_pause(&sc->sc_ue, hz / 64);
670184610Salfred
671184610Salfred		/* power down and reset state, pin reset state */
672188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
673184610Salfred		    AXE_SW_RESET_CLEAR, NULL);
674194228Sthompsa		uether_pause(&sc->sc_ue, hz / 16);
675184610Salfred
676184610Salfred		/* power down/reset state, pin operating state */
677188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
678184610Salfred		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
679194228Sthompsa		uether_pause(&sc->sc_ue, hz / 4);
680184610Salfred
681184610Salfred		/* power up, reset */
682188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
683184610Salfred
684184610Salfred		/* power up, operating */
685188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
686184610Salfred		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
687184610Salfred	} else {
688184610Salfred		/* ask for external PHY */
689188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
690194228Sthompsa		uether_pause(&sc->sc_ue, hz / 64);
691184610Salfred
692184610Salfred		/* power down internal PHY */
693188412Sthompsa		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
694184610Salfred		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
695184610Salfred	}
696184610Salfred
697194228Sthompsa	uether_pause(&sc->sc_ue, hz / 4);
698188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
699184610Salfred}
700184610Salfred
701184610Salfredstatic void
702224020Syongariaxe_ax88772_phywake(struct axe_softc *sc)
703215969Syongari{
704215969Syongari	struct usb_ether *ue;
705215969Syongari
706215969Syongari	ue = &sc->sc_ue;
707215969Syongari	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
708215969Syongari		/* Manually select internal(embedded) PHY - MAC mode. */
709215969Syongari		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
710215969Syongari		    AXE_SW_PHY_SELECT_EMBEDDED | AXE_SW_PHY_SELECT_SS_MII,
711215969Syongari		    NULL);
712215969Syongari		uether_pause(&sc->sc_ue, hz / 32);
713215969Syongari	} else {
714215969Syongari		/*
715215969Syongari		 * Manually select external PHY - MAC mode.
716215969Syongari		 * Reverse MII/RMII is for AX88772A PHY mode.
717215969Syongari		 */
718215969Syongari		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
719215969Syongari		    AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
720215969Syongari		uether_pause(&sc->sc_ue, hz / 32);
721215969Syongari	}
722215969Syongari	/* Take PHY out of power down. */
723215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
724215969Syongari	    AXE_SW_RESET_IPRL, NULL);
725215969Syongari	uether_pause(&sc->sc_ue, hz / 4);
726215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
727215969Syongari	uether_pause(&sc->sc_ue, hz);
728215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
729215969Syongari	uether_pause(&sc->sc_ue, hz / 32);
730215969Syongari	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
731215969Syongari	uether_pause(&sc->sc_ue, hz / 32);
732224020Syongari}
733224020Syongari
734224020Syongaristatic void
735224020Syongariaxe_ax88772a_init(struct axe_softc *sc)
736224020Syongari{
737224020Syongari	struct usb_ether *ue;
738224020Syongari
739224020Syongari	ue = &sc->sc_ue;
740224020Syongari	/* Reload EEPROM. */
741224020Syongari	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
742224020Syongari	axe_ax88772_phywake(sc);
743224020Syongari	/* Stop MAC. */
744215969Syongari	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
745215969Syongari}
746215969Syongari
747224020Syongaristatic void
748224020Syongariaxe_ax88772b_init(struct axe_softc *sc)
749224020Syongari{
750224020Syongari	struct usb_ether *ue;
751224020Syongari	uint16_t eeprom;
752224020Syongari	uint8_t *eaddr;
753224020Syongari	int i;
754224020Syongari
755224020Syongari	ue = &sc->sc_ue;
756224020Syongari	/* Reload EEPROM. */
757224020Syongari	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
758224020Syongari	/*
759224020Syongari	 * Save PHY power saving configuration(high byte) and
760224020Syongari	 * clear EEPROM checksum value(low byte).
761224020Syongari	 */
762224020Syongari	axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom);
763224020Syongari	sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
764224020Syongari
765224020Syongari	/*
766224020Syongari	 * Auto-loaded default station address from internal ROM is
767224020Syongari	 * 00:00:00:00:00:00 such that an explicit access to EEPROM
768224020Syongari	 * is required to get real station address.
769224020Syongari	 */
770224020Syongari	eaddr = ue->ue_eaddr;
771224020Syongari	for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
772224020Syongari		axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i,
773224020Syongari		    &eeprom);
774224020Syongari		eeprom = le16toh(eeprom);
775224020Syongari		*eaddr++ = (uint8_t)(eeprom & 0xFF);
776224020Syongari		*eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
777224020Syongari	}
778224020Syongari	/* Wakeup PHY. */
779224020Syongari	axe_ax88772_phywake(sc);
780224020Syongari	/* Stop MAC. */
781224020Syongari	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
782224020Syongari}
783224020Syongari
784215969Syongari#undef	AXE_GPIO_WRITE
785215969Syongari
786215969Syongaristatic void
787188412Sthompsaaxe_reset(struct axe_softc *sc)
788184610Salfred{
789192984Sthompsa	struct usb_config_descriptor *cd;
790193045Sthompsa	usb_error_t err;
791184610Salfred
792194228Sthompsa	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
793184610Salfred
794194228Sthompsa	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
795188412Sthompsa	    cd->bConfigurationValue);
796188412Sthompsa	if (err)
797188412Sthompsa		DPRINTF("reset failed (ignored)\n");
798188412Sthompsa
799188412Sthompsa	/* Wait a little while for the chip to get its brains in order. */
800194228Sthompsa	uether_pause(&sc->sc_ue, hz / 100);
801215966Syongari
802215966Syongari	/* Reinitialize controller to achieve full reset. */
803215966Syongari	if (sc->sc_flags & AXE_FLAG_178)
804215966Syongari		axe_ax88178_init(sc);
805215966Syongari	else if (sc->sc_flags & AXE_FLAG_772)
806215966Syongari		axe_ax88772_init(sc);
807215969Syongari	else if (sc->sc_flags & AXE_FLAG_772A)
808215969Syongari		axe_ax88772a_init(sc);
809224020Syongari	else if (sc->sc_flags & AXE_FLAG_772B)
810224020Syongari		axe_ax88772b_init(sc);
811188412Sthompsa}
812188412Sthompsa
813188412Sthompsastatic void
814192984Sthompsaaxe_attach_post(struct usb_ether *ue)
815188412Sthompsa{
816194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
817188412Sthompsa
818184610Salfred	/*
819184610Salfred	 * Load PHY indexes first. Needed by axe_xxx_init().
820184610Salfred	 */
821188412Sthompsa	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
822212130Sthompsa	if (bootverbose)
823212130Sthompsa		device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
824212130Sthompsa		    sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
825186730Salfred	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
826186730Salfred	if (sc->sc_phyno == -1)
827186730Salfred		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
828186730Salfred	if (sc->sc_phyno == -1) {
829188412Sthompsa		device_printf(sc->sc_ue.ue_dev,
830188412Sthompsa		    "no valid PHY address found, assuming PHY address 0\n");
831186730Salfred		sc->sc_phyno = 0;
832186730Salfred	}
833184610Salfred
834224020Syongari	/* Initialize controller and get station address. */
835215968Syongari	if (sc->sc_flags & AXE_FLAG_178) {
836188412Sthompsa		axe_ax88178_init(sc);
837215968Syongari		sc->sc_tx_bufsz = 16 * 1024;
838224020Syongari		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
839215968Syongari	} else if (sc->sc_flags & AXE_FLAG_772) {
840188412Sthompsa		axe_ax88772_init(sc);
841215968Syongari		sc->sc_tx_bufsz = 8 * 1024;
842224020Syongari		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
843215969Syongari	} else if (sc->sc_flags & AXE_FLAG_772A) {
844215969Syongari		axe_ax88772a_init(sc);
845215969Syongari		sc->sc_tx_bufsz = 8 * 1024;
846188412Sthompsa		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
847224020Syongari	} else if (sc->sc_flags & AXE_FLAG_772B) {
848224020Syongari		axe_ax88772b_init(sc);
849224020Syongari		sc->sc_tx_bufsz = 8 * 1024;
850224020Syongari	} else
851188412Sthompsa		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
852184610Salfred
853184610Salfred	/*
854184610Salfred	 * Fetch IPG values.
855184610Salfred	 */
856224020Syongari	if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B)) {
857215969Syongari		/* Set IPG values. */
858215969Syongari		sc->sc_ipgs[0] = 0x15;
859215969Syongari		sc->sc_ipgs[1] = 0x16;
860215969Syongari		sc->sc_ipgs[2] = 0x1A;
861215969Syongari	} else
862215969Syongari		axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
863188412Sthompsa}
864184610Salfred
865229106Shselaskystatic int
866229106Shselaskyaxe_attach_post_sub(struct usb_ether *ue)
867229106Shselasky{
868229106Shselasky	struct axe_softc *sc;
869229106Shselasky	struct ifnet *ifp;
870229106Shselasky	u_int adv_pause;
871229106Shselasky	int error;
872229106Shselasky
873229106Shselasky	sc = uether_getsc(ue);
874229106Shselasky	ifp = ue->ue_ifp;
875229106Shselasky	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
876229106Shselasky	ifp->if_start = uether_start;
877229106Shselasky	ifp->if_ioctl = axe_ioctl;
878229106Shselasky	ifp->if_init = uether_init;
879229106Shselasky	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
880229106Shselasky	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
881229106Shselasky	IFQ_SET_READY(&ifp->if_snd);
882229106Shselasky
883229106Shselasky	if (AXE_IS_178_FAMILY(sc))
884229106Shselasky		ifp->if_capabilities |= IFCAP_VLAN_MTU;
885229106Shselasky	if (sc->sc_flags & AXE_FLAG_772B) {
886229106Shselasky		ifp->if_capabilities |= IFCAP_TXCSUM | IFCAP_RXCSUM;
887229106Shselasky		ifp->if_hwassist = AXE_CSUM_FEATURES;
888229106Shselasky		/*
889229106Shselasky		 * Checksum offloading of AX88772B also works with VLAN
890229106Shselasky		 * tagged frames but there is no way to take advantage
891229106Shselasky		 * of the feature because vlan(4) assumes
892229106Shselasky		 * IFCAP_VLAN_HWTAGGING is prerequisite condition to
893229106Shselasky		 * support checksum offloading with VLAN. VLAN hardware
894229106Shselasky		 * tagging support of AX88772B is very limited so it's
895229106Shselasky		 * not possible to announce IFCAP_VLAN_HWTAGGING.
896229106Shselasky		 */
897229106Shselasky	}
898229106Shselasky	ifp->if_capenable = ifp->if_capabilities;
899229106Shselasky	if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B | AXE_FLAG_178))
900229106Shselasky		adv_pause = MIIF_DOPAUSE;
901229106Shselasky	else
902229106Shselasky		adv_pause = 0;
903229106Shselasky	mtx_lock(&Giant);
904229106Shselasky	error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
905229106Shselasky	    uether_ifmedia_upd, ue->ue_methods->ue_mii_sts,
906229106Shselasky	    BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, adv_pause);
907229106Shselasky	mtx_unlock(&Giant);
908229106Shselasky
909229106Shselasky	return (error);
910229106Shselasky}
911229106Shselasky
912188412Sthompsa/*
913188412Sthompsa * Probe for a AX88172 chip.
914188412Sthompsa */
915188412Sthompsastatic int
916188412Sthompsaaxe_probe(device_t dev)
917188412Sthompsa{
918192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
919184610Salfred
920192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
921188412Sthompsa		return (ENXIO);
922188412Sthompsa	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
923188412Sthompsa		return (ENXIO);
924188412Sthompsa	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
925188412Sthompsa		return (ENXIO);
926184610Salfred
927194228Sthompsa	return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
928188412Sthompsa}
929184610Salfred
930188412Sthompsa/*
931188412Sthompsa * Attach the interface. Allocate softc structures, do ifmedia
932188412Sthompsa * setup and ethernet/BPF attach.
933188412Sthompsa */
934188412Sthompsastatic int
935188412Sthompsaaxe_attach(device_t dev)
936188412Sthompsa{
937192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(dev);
938188412Sthompsa	struct axe_softc *sc = device_get_softc(dev);
939192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
940188412Sthompsa	uint8_t iface_index;
941188412Sthompsa	int error;
942184610Salfred
943188412Sthompsa	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
944184610Salfred
945194228Sthompsa	device_set_usb_desc(dev);
946184610Salfred
947188412Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
948184610Salfred
949188412Sthompsa	iface_index = AXE_IFACE_IDX;
950194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
951188412Sthompsa	    axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
952188412Sthompsa	if (error) {
953199816Sthompsa		device_printf(dev, "allocating USB transfers failed\n");
954188412Sthompsa		goto detach;
955188412Sthompsa	}
956184610Salfred
957188412Sthompsa	ue->ue_sc = sc;
958188412Sthompsa	ue->ue_dev = dev;
959188412Sthompsa	ue->ue_udev = uaa->device;
960188412Sthompsa	ue->ue_mtx = &sc->sc_mtx;
961188412Sthompsa	ue->ue_methods = &axe_ue_methods;
962184610Salfred
963194228Sthompsa	error = uether_ifattach(ue);
964184610Salfred	if (error) {
965188412Sthompsa		device_printf(dev, "could not attach interface\n");
966188412Sthompsa		goto detach;
967184610Salfred	}
968188412Sthompsa	return (0);			/* success */
969184610Salfred
970188412Sthompsadetach:
971188412Sthompsa	axe_detach(dev);
972188412Sthompsa	return (ENXIO);			/* failure */
973184610Salfred}
974184610Salfred
975184610Salfredstatic int
976184610Salfredaxe_detach(device_t dev)
977184610Salfred{
978184610Salfred	struct axe_softc *sc = device_get_softc(dev);
979192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
980184610Salfred
981194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
982194228Sthompsa	uether_ifdetach(ue);
983184610Salfred	mtx_destroy(&sc->sc_mtx);
984184610Salfred
985184610Salfred	return (0);
986184610Salfred}
987184610Salfred
988184610Salfred#if (AXE_BULK_BUF_SIZE >= 0x10000)
989184610Salfred#error "Please update axe_bulk_read_callback()!"
990184610Salfred#endif
991184610Salfred
992184610Salfredstatic void
993194677Sthompsaaxe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
994184610Salfred{
995194677Sthompsa	struct axe_softc *sc = usbd_xfer_softc(xfer);
996192984Sthompsa	struct usb_ether *ue = &sc->sc_ue;
997194677Sthompsa	struct usb_page_cache *pc;
998194677Sthompsa	int actlen;
999184610Salfred
1000194677Sthompsa	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1001194677Sthompsa
1002184610Salfred	switch (USB_GET_STATE(xfer)) {
1003184610Salfred	case USB_ST_TRANSFERRED:
1004194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
1005229106Shselasky		axe_rx_frame(ue, pc, actlen);
1006184610Salfred
1007188412Sthompsa		/* FALLTHROUGH */
1008184610Salfred	case USB_ST_SETUP:
1009184610Salfredtr_setup:
1010194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1011194228Sthompsa		usbd_transfer_submit(xfer);
1012194228Sthompsa		uether_rxflush(ue);
1013184610Salfred		return;
1014184610Salfred
1015184610Salfred	default:			/* Error */
1016194677Sthompsa		DPRINTF("bulk read error, %s\n", usbd_errstr(error));
1017188412Sthompsa
1018194677Sthompsa		if (error != USB_ERR_CANCELLED) {
1019184610Salfred			/* try to clear stall first */
1020194677Sthompsa			usbd_xfer_set_stall(xfer);
1021188412Sthompsa			goto tr_setup;
1022184610Salfred		}
1023184610Salfred		return;
1024184610Salfred
1025184610Salfred	}
1026184610Salfred}
1027184610Salfred
1028229106Shselaskystatic int
1029229106Shselaskyaxe_rx_frame(struct usb_ether *ue, struct usb_page_cache *pc, int actlen)
1030229106Shselasky{
1031229106Shselasky	struct axe_softc *sc;
1032229106Shselasky	struct axe_sframe_hdr hdr;
1033229106Shselasky	struct axe_csum_hdr csum_hdr;
1034229106Shselasky	int error, len, pos;
1035229106Shselasky
1036229106Shselasky	sc = uether_getsc(ue);
1037229106Shselasky	pos = 0;
1038229106Shselasky	len = 0;
1039229106Shselasky	error = 0;
1040229106Shselasky	if ((sc->sc_flags & AXE_FLAG_STD_FRAME) != 0) {
1041229106Shselasky		while (pos < actlen) {
1042235000Shselasky			if ((int)(pos + sizeof(hdr)) > actlen) {
1043229106Shselasky				/* too little data */
1044229106Shselasky				error = EINVAL;
1045229106Shselasky				break;
1046229106Shselasky			}
1047229106Shselasky			usbd_copy_out(pc, pos, &hdr, sizeof(hdr));
1048229106Shselasky
1049229106Shselasky			if ((hdr.len ^ hdr.ilen) != sc->sc_lenmask) {
1050229106Shselasky				/* we lost sync */
1051229106Shselasky				error = EINVAL;
1052229106Shselasky				break;
1053229106Shselasky			}
1054229106Shselasky			pos += sizeof(hdr);
1055229106Shselasky			len = le16toh(hdr.len);
1056229106Shselasky			if (pos + len > actlen) {
1057229106Shselasky				/* invalid length */
1058229106Shselasky				error = EINVAL;
1059229106Shselasky				break;
1060229106Shselasky			}
1061229106Shselasky			axe_rxeof(ue, pc, pos, len, NULL);
1062229106Shselasky			pos += len + (len % 2);
1063229106Shselasky		}
1064229106Shselasky	} else if ((sc->sc_flags & AXE_FLAG_CSUM_FRAME) != 0) {
1065229106Shselasky		while (pos < actlen) {
1066235000Shselasky			if ((int)(pos + sizeof(csum_hdr)) > actlen) {
1067229106Shselasky				/* too little data */
1068229106Shselasky				error = EINVAL;
1069229106Shselasky				break;
1070229106Shselasky			}
1071229106Shselasky			usbd_copy_out(pc, pos, &csum_hdr, sizeof(csum_hdr));
1072229106Shselasky
1073229106Shselasky			csum_hdr.len = le16toh(csum_hdr.len);
1074229106Shselasky			csum_hdr.ilen = le16toh(csum_hdr.ilen);
1075229106Shselasky			csum_hdr.cstatus = le16toh(csum_hdr.cstatus);
1076229106Shselasky			if ((AXE_CSUM_RXBYTES(csum_hdr.len) ^
1077229106Shselasky			    AXE_CSUM_RXBYTES(csum_hdr.ilen)) !=
1078229106Shselasky			    sc->sc_lenmask) {
1079229106Shselasky				/* we lost sync */
1080229106Shselasky				error = EINVAL;
1081229106Shselasky				break;
1082229106Shselasky			}
1083229106Shselasky			/*
1084229106Shselasky			 * Get total transferred frame length including
1085229106Shselasky			 * checksum header.  The length should be multiple
1086229106Shselasky			 * of 4.
1087229106Shselasky			 */
1088229106Shselasky			len = sizeof(csum_hdr) + AXE_CSUM_RXBYTES(csum_hdr.len);
1089229106Shselasky			len = (len + 3) & ~3;
1090229106Shselasky			if (pos + len > actlen) {
1091229106Shselasky				/* invalid length */
1092229106Shselasky				error = EINVAL;
1093229106Shselasky				break;
1094229106Shselasky			}
1095229106Shselasky			axe_rxeof(ue, pc, pos + sizeof(csum_hdr),
1096229106Shselasky			    AXE_CSUM_RXBYTES(csum_hdr.len), &csum_hdr);
1097229106Shselasky			pos += len;
1098229106Shselasky		}
1099229106Shselasky	} else
1100229106Shselasky		axe_rxeof(ue, pc, 0, actlen, NULL);
1101229106Shselasky
1102229106Shselasky	if (error != 0)
1103229106Shselasky		ue->ue_ifp->if_ierrors++;
1104229106Shselasky	return (error);
1105229106Shselasky}
1106229106Shselasky
1107229106Shselaskystatic int
1108229106Shselaskyaxe_rxeof(struct usb_ether *ue, struct usb_page_cache *pc, unsigned int offset,
1109229106Shselasky    unsigned int len, struct axe_csum_hdr *csum_hdr)
1110229106Shselasky{
1111229106Shselasky	struct ifnet *ifp = ue->ue_ifp;
1112229106Shselasky	struct mbuf *m;
1113229106Shselasky
1114229106Shselasky	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) {
1115229106Shselasky		ifp->if_ierrors++;
1116229106Shselasky		return (EINVAL);
1117229106Shselasky	}
1118229106Shselasky
1119248078Smarius	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1120229106Shselasky	if (m == NULL) {
1121229106Shselasky		ifp->if_iqdrops++;
1122229106Shselasky		return (ENOMEM);
1123229106Shselasky	}
1124229106Shselasky	m->m_len = m->m_pkthdr.len = MCLBYTES;
1125229106Shselasky	m_adj(m, ETHER_ALIGN);
1126229106Shselasky
1127229106Shselasky	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
1128229106Shselasky
1129229106Shselasky	ifp->if_ipackets++;
1130229106Shselasky	m->m_pkthdr.rcvif = ifp;
1131229106Shselasky	m->m_pkthdr.len = m->m_len = len;
1132229106Shselasky
1133229106Shselasky	if (csum_hdr != NULL && csum_hdr->cstatus & AXE_CSUM_HDR_L3_TYPE_IPV4) {
1134229106Shselasky		if ((csum_hdr->cstatus & (AXE_CSUM_HDR_L4_CSUM_ERR |
1135229106Shselasky		    AXE_CSUM_HDR_L3_CSUM_ERR)) == 0) {
1136229106Shselasky			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
1137229106Shselasky			    CSUM_IP_VALID;
1138229106Shselasky			if ((csum_hdr->cstatus & AXE_CSUM_HDR_L4_TYPE_MASK) ==
1139229106Shselasky			    AXE_CSUM_HDR_L4_TYPE_TCP ||
1140229106Shselasky			    (csum_hdr->cstatus & AXE_CSUM_HDR_L4_TYPE_MASK) ==
1141229106Shselasky			    AXE_CSUM_HDR_L4_TYPE_UDP) {
1142229106Shselasky				m->m_pkthdr.csum_flags |=
1143229106Shselasky				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1144229106Shselasky				m->m_pkthdr.csum_data = 0xffff;
1145229106Shselasky			}
1146229106Shselasky		}
1147229106Shselasky	}
1148229106Shselasky
1149229106Shselasky	_IF_ENQUEUE(&ue->ue_rxq, m);
1150229106Shselasky	return (0);
1151229106Shselasky}
1152229106Shselasky
1153184610Salfred#if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
1154184610Salfred#error "Please update axe_bulk_write_callback()!"
1155184610Salfred#endif
1156184610Salfred
1157184610Salfredstatic void
1158194677Sthompsaaxe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1159184610Salfred{
1160194677Sthompsa	struct axe_softc *sc = usbd_xfer_softc(xfer);
1161184610Salfred	struct axe_sframe_hdr hdr;
1162194228Sthompsa	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1163194677Sthompsa	struct usb_page_cache *pc;
1164184610Salfred	struct mbuf *m;
1165216284Syongari	int nframes, pos;
1166184610Salfred
1167184610Salfred	switch (USB_GET_STATE(xfer)) {
1168184610Salfred	case USB_ST_TRANSFERRED:
1169184610Salfred		DPRINTFN(11, "transfer complete\n");
1170213424Syongari		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1171188412Sthompsa		/* FALLTHROUGH */
1172184610Salfred	case USB_ST_SETUP:
1173188412Sthompsatr_setup:
1174213424Syongari		if ((sc->sc_flags & AXE_FLAG_LINK) == 0 ||
1175213424Syongari		    (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
1176184610Salfred			/*
1177213424Syongari			 * Don't send anything if there is no link or
1178213424Syongari			 * controller is busy.
1179184610Salfred			 */
1180188412Sthompsa			return;
1181184610Salfred		}
1182184610Salfred
1183216284Syongari		for (nframes = 0; nframes < 16 &&
1184216284Syongari		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
1185184610Salfred			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1186216284Syongari			if (m == NULL)
1187216284Syongari				break;
1188216284Syongari			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
1189216284Syongari			    nframes);
1190216284Syongari			pos = 0;
1191216284Syongari			pc = usbd_xfer_get_frame(xfer, nframes);
1192215968Syongari			if (AXE_IS_178_FAMILY(sc)) {
1193184610Salfred				hdr.len = htole16(m->m_pkthdr.len);
1194184610Salfred				hdr.ilen = ~hdr.len;
1195229106Shselasky				/*
1196229106Shselasky				 * If upper stack computed checksum, driver
1197229106Shselasky				 * should tell controller not to insert
1198229106Shselasky				 * computed checksum for checksum offloading
1199229106Shselasky				 * enabled controller.
1200229106Shselasky				 */
1201229106Shselasky				if (ifp->if_capabilities & IFCAP_TXCSUM) {
1202229106Shselasky					if ((m->m_pkthdr.csum_flags &
1203229106Shselasky					    AXE_CSUM_FEATURES) != 0)
1204229106Shselasky						hdr.len |= htole16(
1205229106Shselasky						    AXE_TX_CSUM_PSEUDO_HDR);
1206229106Shselasky					else
1207229106Shselasky						hdr.len |= htole16(
1208229106Shselasky						    AXE_TX_CSUM_DIS);
1209229106Shselasky				}
1210194677Sthompsa				usbd_copy_in(pc, pos, &hdr, sizeof(hdr));
1211184610Salfred				pos += sizeof(hdr);
1212216284Syongari				usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
1213216284Syongari				pos += m->m_pkthdr.len;
1214216284Syongari				if ((pos % 512) == 0) {
1215216284Syongari					hdr.len = 0;
1216216284Syongari					hdr.ilen = 0xffff;
1217216284Syongari					usbd_copy_in(pc, pos, &hdr,
1218216284Syongari					    sizeof(hdr));
1219216284Syongari					pos += sizeof(hdr);
1220216284Syongari				}
1221216284Syongari			} else {
1222216284Syongari				usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
1223216284Syongari				pos += m->m_pkthdr.len;
1224184610Salfred			}
1225184610Salfred
1226184610Salfred			/*
1227213423Syongari			 * XXX
1228213423Syongari			 * Update TX packet counter here. This is not
1229213423Syongari			 * correct way but it seems that there is no way
1230213423Syongari			 * to know how many packets are sent at the end
1231213423Syongari			 * of transfer because controller combines
1232213423Syongari			 * multiple writes into single one if there is
1233213423Syongari			 * room in TX buffer of controller.
1234213423Syongari			 */
1235213423Syongari			ifp->if_opackets++;
1236213423Syongari
1237213423Syongari			/*
1238188412Sthompsa			 * if there's a BPF listener, bounce a copy
1239188412Sthompsa			 * of this frame to him:
1240188412Sthompsa			 */
1241184610Salfred			BPF_MTAP(ifp, m);
1242184610Salfred
1243184610Salfred			m_freem(m);
1244184610Salfred
1245216284Syongari			/* Set frame length. */
1246216284Syongari			usbd_xfer_set_frame_len(xfer, nframes, pos);
1247184610Salfred		}
1248216284Syongari		if (nframes != 0) {
1249216284Syongari			usbd_xfer_set_frames(xfer, nframes);
1250216284Syongari			usbd_transfer_submit(xfer);
1251216284Syongari			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1252216284Syongari		}
1253184610Salfred		return;
1254216284Syongari		/* NOTREACHED */
1255184610Salfred	default:			/* Error */
1256184610Salfred		DPRINTFN(11, "transfer error, %s\n",
1257194677Sthompsa		    usbd_errstr(error));
1258184610Salfred
1259188412Sthompsa		ifp->if_oerrors++;
1260213424Syongari		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1261188412Sthompsa
1262194677Sthompsa		if (error != USB_ERR_CANCELLED) {
1263184610Salfred			/* try to clear stall first */
1264194677Sthompsa			usbd_xfer_set_stall(xfer);
1265188412Sthompsa			goto tr_setup;
1266184610Salfred		}
1267184610Salfred		return;
1268184610Salfred
1269184610Salfred	}
1270184610Salfred}
1271184610Salfred
1272184610Salfredstatic void
1273192984Sthompsaaxe_tick(struct usb_ether *ue)
1274184610Salfred{
1275194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1276184610Salfred	struct mii_data *mii = GET_MII(sc);
1277184610Salfred
1278188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1279188412Sthompsa
1280184610Salfred	mii_tick(mii);
1281188553Sthompsa	if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
1282188553Sthompsa		axe_miibus_statchg(ue->ue_dev);
1283188553Sthompsa		if ((sc->sc_flags & AXE_FLAG_LINK) != 0)
1284188553Sthompsa			axe_start(ue);
1285186730Salfred	}
1286184610Salfred}
1287184610Salfred
1288184610Salfredstatic void
1289192984Sthompsaaxe_start(struct usb_ether *ue)
1290184610Salfred{
1291194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1292184610Salfred
1293188412Sthompsa	/*
1294188412Sthompsa	 * start the USB transfers, if not already started:
1295188412Sthompsa	 */
1296194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
1297194228Sthompsa	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
1298184610Salfred}
1299184610Salfred
1300184610Salfredstatic void
1301229106Shselaskyaxe_csum_cfg(struct usb_ether *ue)
1302229106Shselasky{
1303229106Shselasky	struct axe_softc *sc;
1304229106Shselasky	struct ifnet *ifp;
1305229106Shselasky	uint16_t csum1, csum2;
1306229106Shselasky
1307229106Shselasky	sc = uether_getsc(ue);
1308229106Shselasky	AXE_LOCK_ASSERT(sc, MA_OWNED);
1309229106Shselasky
1310229106Shselasky	if ((sc->sc_flags & AXE_FLAG_772B) != 0) {
1311229106Shselasky		ifp = uether_getifp(ue);
1312229106Shselasky		csum1 = 0;
1313229106Shselasky		csum2 = 0;
1314229106Shselasky		if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
1315229106Shselasky			csum1 |= AXE_TXCSUM_IP | AXE_TXCSUM_TCP |
1316229106Shselasky			    AXE_TXCSUM_UDP;
1317229106Shselasky		axe_cmd(sc, AXE_772B_CMD_WRITE_TXCSUM, csum2, csum1, NULL);
1318229106Shselasky		csum1 = 0;
1319229106Shselasky		csum2 = 0;
1320229106Shselasky		if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1321229106Shselasky			csum1 |= AXE_RXCSUM_IP | AXE_RXCSUM_IPVE |
1322229106Shselasky			    AXE_RXCSUM_TCP | AXE_RXCSUM_UDP | AXE_RXCSUM_ICMP |
1323229106Shselasky			    AXE_RXCSUM_IGMP;
1324229106Shselasky		axe_cmd(sc, AXE_772B_CMD_WRITE_RXCSUM, csum2, csum1, NULL);
1325229106Shselasky	}
1326229106Shselasky}
1327229106Shselasky
1328229106Shselaskystatic void
1329192984Sthompsaaxe_init(struct usb_ether *ue)
1330184610Salfred{
1331194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1332194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1333184610Salfred	uint16_t rxmode;
1334184610Salfred
1335188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1336184610Salfred
1337215963Syongari	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1338215963Syongari		return;
1339215963Syongari
1340188412Sthompsa	/* Cancel pending I/O */
1341188412Sthompsa	axe_stop(ue);
1342184610Salfred
1343215962Syongari	axe_reset(sc);
1344215962Syongari
1345229106Shselasky	/* Set MAC address and transmitter IPG values. */
1346229106Shselasky	if (AXE_IS_178_FAMILY(sc)) {
1347197567Sthompsa		axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
1348188412Sthompsa		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
1349184610Salfred		    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
1350229106Shselasky	} else {
1351229106Shselasky		axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
1352188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
1353188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
1354188412Sthompsa		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
1355184610Salfred	}
1356184610Salfred
1357229106Shselasky	if (AXE_IS_178_FAMILY(sc)) {
1358229106Shselasky		sc->sc_flags &= ~(AXE_FLAG_STD_FRAME | AXE_FLAG_CSUM_FRAME);
1359252442Syongari		if ((sc->sc_flags & AXE_FLAG_772B) != 0 &&
1360252442Syongari		    (ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1361229106Shselasky			sc->sc_lenmask = AXE_CSUM_HDR_LEN_MASK;
1362252442Syongari			sc->sc_flags |= AXE_FLAG_CSUM_FRAME;
1363252442Syongari		} else {
1364229106Shselasky			sc->sc_lenmask = AXE_HDR_LEN_MASK;
1365229106Shselasky			sc->sc_flags |= AXE_FLAG_STD_FRAME;
1366252442Syongari		}
1367229106Shselasky	}
1368229106Shselasky
1369229106Shselasky	/* Configure TX/RX checksum offloading. */
1370229106Shselasky	axe_csum_cfg(ue);
1371229106Shselasky
1372229106Shselasky	if (sc->sc_flags & AXE_FLAG_772B) {
1373229106Shselasky		/* AX88772B uses different maximum frame burst configuration. */
1374224020Syongari		axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
1375224020Syongari		    ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
1376224020Syongari		    ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
1377229106Shselasky	}
1378224020Syongari
1379224020Syongari	/* Enable receiver, set RX mode. */
1380184610Salfred	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
1381215968Syongari	if (AXE_IS_178_FAMILY(sc)) {
1382224020Syongari		if (sc->sc_flags & AXE_FLAG_772B) {
1383224020Syongari			/*
1384224020Syongari			 * Select RX header format type 1.  Aligning IP
1385229106Shselasky			 * header on 4 byte boundary is not needed when
1386229106Shselasky			 * checksum offloading feature is not used
1387224020Syongari			 * because we always copy the received frame in
1388229106Shselasky			 * RX handler.  When RX checksum offloading is
1389229106Shselasky			 * active, aligning IP header is required to
1390229106Shselasky			 * reflect actual frame length including RX
1391229106Shselasky			 * header size.
1392224020Syongari			 */
1393224020Syongari			rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
1394229106Shselasky			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1395229106Shselasky				rxmode |= AXE_772B_RXCMD_IPHDR_ALIGN;
1396224020Syongari		} else {
1397224020Syongari			/*
1398224020Syongari			 * Default Rx buffer size is too small to get
1399224020Syongari			 * maximum performance.
1400224020Syongari			 */
1401224020Syongari			rxmode |= AXE_178_RXCMD_MFB_16384;
1402224020Syongari		}
1403184610Salfred	} else {
1404184610Salfred		rxmode |= AXE_172_RXCMD_UNICAST;
1405184610Salfred	}
1406184610Salfred
1407184610Salfred	/* If we want promiscuous mode, set the allframes bit. */
1408188412Sthompsa	if (ifp->if_flags & IFF_PROMISC)
1409184610Salfred		rxmode |= AXE_RXCMD_PROMISC;
1410188412Sthompsa
1411188412Sthompsa	if (ifp->if_flags & IFF_BROADCAST)
1412184610Salfred		rxmode |= AXE_RXCMD_BROADCAST;
1413184610Salfred
1414188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1415188412Sthompsa
1416184610Salfred	/* Load the multicast filter. */
1417188412Sthompsa	axe_setmulti(ue);
1418184610Salfred
1419194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
1420184610Salfred
1421188412Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1422215964Syongari	/* Switch to selected media. */
1423215964Syongari	axe_ifmedia_upd(ifp);
1424184610Salfred}
1425184610Salfred
1426184610Salfredstatic void
1427192984Sthompsaaxe_setpromisc(struct usb_ether *ue)
1428184610Salfred{
1429194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1430194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1431184610Salfred	uint16_t rxmode;
1432184610Salfred
1433188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
1434184610Salfred
1435184610Salfred	rxmode = le16toh(rxmode);
1436184610Salfred
1437188412Sthompsa	if (ifp->if_flags & IFF_PROMISC) {
1438184610Salfred		rxmode |= AXE_RXCMD_PROMISC;
1439184610Salfred	} else {
1440184610Salfred		rxmode &= ~AXE_RXCMD_PROMISC;
1441184610Salfred	}
1442184610Salfred
1443188412Sthompsa	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1444184610Salfred
1445188412Sthompsa	axe_setmulti(ue);
1446184610Salfred}
1447184610Salfred
1448184610Salfredstatic void
1449192984Sthompsaaxe_stop(struct usb_ether *ue)
1450184610Salfred{
1451194228Sthompsa	struct axe_softc *sc = uether_getsc(ue);
1452194228Sthompsa	struct ifnet *ifp = uether_getifp(ue);
1453184610Salfred
1454188412Sthompsa	AXE_LOCK_ASSERT(sc, MA_OWNED);
1455184610Salfred
1456213424Syongari	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1457186730Salfred	sc->sc_flags &= ~AXE_FLAG_LINK;
1458184610Salfred
1459184610Salfred	/*
1460184610Salfred	 * stop all the transfers, if not already stopped:
1461184610Salfred	 */
1462194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1463194228Sthompsa	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
1464184610Salfred}
1465229106Shselasky
1466229106Shselaskystatic int
1467229106Shselaskyaxe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1468229106Shselasky{
1469229106Shselasky	struct usb_ether *ue = ifp->if_softc;
1470229106Shselasky	struct axe_softc *sc;
1471229106Shselasky	struct ifreq *ifr;
1472229106Shselasky	int error, mask, reinit;
1473229106Shselasky
1474229106Shselasky	sc = uether_getsc(ue);
1475229106Shselasky	ifr = (struct ifreq *)data;
1476229106Shselasky	error = 0;
1477229106Shselasky	reinit = 0;
1478229106Shselasky	if (cmd == SIOCSIFCAP) {
1479229106Shselasky		AXE_LOCK(sc);
1480229106Shselasky		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1481229106Shselasky		if ((mask & IFCAP_TXCSUM) != 0 &&
1482229106Shselasky		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
1483229106Shselasky			ifp->if_capenable ^= IFCAP_TXCSUM;
1484229106Shselasky			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
1485229106Shselasky				ifp->if_hwassist |= AXE_CSUM_FEATURES;
1486229106Shselasky			else
1487229106Shselasky				ifp->if_hwassist &= ~AXE_CSUM_FEATURES;
1488229106Shselasky			reinit++;
1489229106Shselasky		}
1490229106Shselasky		if ((mask & IFCAP_RXCSUM) != 0 &&
1491229106Shselasky		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1492229106Shselasky			ifp->if_capenable ^= IFCAP_RXCSUM;
1493229106Shselasky			reinit++;
1494229106Shselasky		}
1495229106Shselasky		if (reinit > 0 && ifp->if_drv_flags & IFF_DRV_RUNNING)
1496229106Shselasky			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1497229106Shselasky		else
1498229106Shselasky			reinit = 0;
1499229106Shselasky		AXE_UNLOCK(sc);
1500229106Shselasky		if (reinit > 0)
1501229106Shselasky			uether_init(ue);
1502229106Shselasky	} else
1503229106Shselasky		error = uether_ioctl(ifp, cmd, data);
1504229106Shselasky
1505229106Shselasky	return (error);
1506229106Shselasky}
1507