xref: /openbsd-src/sys/dev/usb/if_ure.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: if_ure.c,v 1.13 2019/12/07 08:45:28 kevlo Exp $	*/
2 /*-
3  * Copyright (c) 2015, 2016, 2019 Kevin Lo <kevlo@openbsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "bpfilter.h"
29 #include "vlan.h"
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sockio.h>
34 #include <sys/rwlock.h>
35 #include <sys/mbuf.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/device.h>
39 
40 #include <machine/bus.h>
41 
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #if NBPFILTER > 0
46 #include <net/bpf.h>
47 #endif
48 
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
54 
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdivar.h>
59 #include <dev/usb/usbdevs.h>
60 
61 #include <dev/ic/rtl81x9reg.h>
62 #include <dev/usb/if_urereg.h>
63 
64 #ifdef URE_DEBUG
65 #define DPRINTF(x)	do { if (uredebug) printf x; } while (0)
66 #define DPRINTFN(n,x)	do { if (uredebug >= (n)) printf x; } while (0)
67 int	uredebug = 0;
68 #else
69 #define DPRINTF(x)
70 #define DPRINTFN(n,x)
71 #endif
72 
73 const struct usb_devno ure_devs[] = {
74 	{ USB_VENDOR_LENOVO, USB_PRODUCT_LENOVO_DOCK_ETHERNET },
75 	{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8152 },
76 	{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 },
77 	{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8156 }
78 };
79 
80 int	ure_match(struct device *, void *, void *);
81 void	ure_attach(struct device *, struct device *, void *);
82 int	ure_detach(struct device *, int);
83 
84 struct cfdriver ure_cd = {
85 	NULL, "ure", DV_IFNET
86 };
87 
88 const struct cfattach ure_ca = {
89 	sizeof(struct ure_softc), ure_match, ure_attach, ure_detach
90 };
91 
92 int		ure_ctl(struct ure_softc *, uint8_t, uint16_t, uint16_t,
93 		    void *, int);
94 int		ure_read_mem(struct ure_softc *, uint16_t, uint16_t, void *,
95 		    int);
96 int		ure_write_mem(struct ure_softc *, uint16_t, uint16_t, void *,
97 		    int);
98 uint8_t		ure_read_1(struct ure_softc *, uint16_t, uint16_t);
99 uint16_t	ure_read_2(struct ure_softc *, uint16_t, uint16_t);
100 uint32_t	ure_read_4(struct ure_softc *, uint16_t, uint16_t);
101 int		ure_write_1(struct ure_softc *, uint16_t, uint16_t, uint32_t);
102 int		ure_write_2(struct ure_softc *, uint16_t, uint16_t, uint32_t);
103 int		ure_write_4(struct ure_softc *, uint16_t, uint16_t, uint32_t);
104 uint16_t	ure_ocp_reg_read(struct ure_softc *, uint16_t);
105 void		ure_ocp_reg_write(struct ure_softc *, uint16_t, uint16_t);
106 
107 void		ure_init(void *);
108 void		ure_stop(struct ure_softc *);
109 void		ure_start(struct ifnet *);
110 void		ure_reset(struct ure_softc *);
111 void		ure_watchdog(struct ifnet *);
112 
113 void		ure_miibus_statchg(struct device *);
114 int		ure_miibus_readreg(struct device *, int, int);
115 void		ure_miibus_writereg(struct device *, int, int, int);
116 void		ure_lock_mii(struct ure_softc *);
117 void		ure_unlock_mii(struct ure_softc *);
118 
119 int		ure_encap(struct ure_softc *, struct mbuf *);
120 void		ure_rxeof(struct usbd_xfer *, void *, usbd_status);
121 void		ure_txeof(struct usbd_xfer *, void *, usbd_status);
122 int		ure_rx_list_init(struct ure_softc *);
123 int		ure_tx_list_init(struct ure_softc *);
124 
125 void		ure_tick_task(void *);
126 void		ure_tick(void *);
127 
128 int		ure_ifmedia_upd(struct ifnet *);
129 void		ure_ifmedia_sts(struct ifnet *, struct ifmediareq *);
130 void		ure_add_media_types(struct ure_softc *);
131 void		ure_link_state(struct ure_softc *);
132 int		ure_get_link_status(struct ure_softc *);
133 void		ure_iff(struct ure_softc *);
134 void		ure_rxvlan(struct ure_softc *);
135 int		ure_ioctl(struct ifnet *, u_long, caddr_t);
136 void		ure_rtl8152_init(struct ure_softc *);
137 void		ure_rtl8153_init(struct ure_softc *);
138 void		ure_rtl8153b_init(struct ure_softc *);
139 void		ure_rtl8152_nic_reset(struct ure_softc *);
140 void		ure_rtl8153_nic_reset(struct ure_softc *);
141 void		ure_rtl8153_phy_status(struct ure_softc *, int);
142 void		ure_reset_bmu(struct ure_softc *);
143 void		ure_disable_teredo(struct ure_softc *);
144 
145 #define URE_SETBIT_1(sc, reg, index, x) \
146 	ure_write_1(sc, reg, index, ure_read_1(sc, reg, index) | (x))
147 #define URE_SETBIT_2(sc, reg, index, x) \
148 	ure_write_2(sc, reg, index, ure_read_2(sc, reg, index) | (x))
149 #define URE_SETBIT_4(sc, reg, index, x) \
150 	ure_write_4(sc, reg, index, ure_read_4(sc, reg, index) | (x))
151 
152 #define URE_CLRBIT_1(sc, reg, index, x) \
153 	ure_write_1(sc, reg, index, ure_read_1(sc, reg, index) & ~(x))
154 #define URE_CLRBIT_2(sc, reg, index, x) \
155 	ure_write_2(sc, reg, index, ure_read_2(sc, reg, index) & ~(x))
156 #define URE_CLRBIT_4(sc, reg, index, x) \
157 	ure_write_4(sc, reg, index, ure_read_4(sc, reg, index) & ~(x))
158 
159 int
160 ure_ctl(struct ure_softc *sc, uint8_t rw, uint16_t val, uint16_t index,
161     void *buf, int len)
162 {
163 	usb_device_request_t	req;
164 	usbd_status		err;
165 
166 	if (usbd_is_dying(sc->ure_udev))
167 		return 0;
168 
169 	if (rw == URE_CTL_WRITE)
170 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
171 	else
172 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
173 	req.bRequest = UR_SET_ADDRESS;
174 	USETW(req.wValue, val);
175 	USETW(req.wIndex, index);
176 	USETW(req.wLength, len);
177 
178 	DPRINTFN(5, ("ure_ctl: rw %d, val 0x%04hu, index 0x%04hu, len %d\n",
179 	    rw, val, index, len));
180 	err = usbd_do_request(sc->ure_udev, &req, buf);
181 	if (err) {
182 		DPRINTF(("ure_ctl: error %d\n", err));
183 		return -1;
184 	}
185 
186 	return 0;
187 }
188 
189 int
190 ure_read_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
191     void *buf, int len)
192 {
193 	return (ure_ctl(sc, URE_CTL_READ, addr, index, buf, len));
194 }
195 
196 int
197 ure_write_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
198     void *buf, int len)
199 {
200 	return (ure_ctl(sc, URE_CTL_WRITE, addr, index, buf, len));
201 }
202 
203 uint8_t
204 ure_read_1(struct ure_softc *sc, uint16_t reg, uint16_t index)
205 {
206 	uint32_t	val;
207 	uint8_t		temp[4];
208 	uint8_t		shift;
209 
210 	shift = (reg & 3) << 3;
211 	reg &= ~3;
212 
213 	ure_read_mem(sc, reg, index, &temp, 4);
214 	val = UGETDW(temp);
215 	val >>= shift;
216 
217 	return (val & 0xff);
218 }
219 
220 uint16_t
221 ure_read_2(struct ure_softc *sc, uint16_t reg, uint16_t index)
222 {
223 	uint32_t	val;
224 	uint8_t		temp[4];
225 	uint8_t		shift;
226 
227 	shift = (reg & 2) << 3;
228 	reg &= ~3;
229 
230 	ure_read_mem(sc, reg, index, &temp, 4);
231 	val = UGETDW(temp);
232 	val >>= shift;
233 
234 	return (val & 0xffff);
235 }
236 
237 uint32_t
238 ure_read_4(struct ure_softc *sc, uint16_t reg, uint16_t index)
239 {
240 	uint8_t	temp[4];
241 
242 	ure_read_mem(sc, reg, index, &temp, 4);
243 	return (UGETDW(temp));
244 }
245 
246 int
247 ure_write_1(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
248 {
249 	uint16_t	byen;
250 	uint8_t		temp[4];
251 	uint8_t		shift;
252 
253 	byen = URE_BYTE_EN_BYTE;
254 	shift = reg & 3;
255 	val &= 0xff;
256 
257 	if (reg & 3) {
258 		byen <<= shift;
259 		val <<= (shift << 3);
260 		reg &= ~3;
261 	}
262 
263 	USETDW(temp, val);
264 	return (ure_write_mem(sc, reg, index | byen, &temp, 4));
265 }
266 
267 int
268 ure_write_2(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
269 {
270 	uint16_t	byen;
271 	uint8_t		temp[4];
272 	uint8_t		shift;
273 
274 	byen = URE_BYTE_EN_WORD;
275 	shift = reg & 2;
276 	val &= 0xffff;
277 
278 	if (reg & 2) {
279 		byen <<= shift;
280 		val <<= (shift << 3);
281 		reg &= ~3;
282 	}
283 
284 	USETDW(temp, val);
285 	return (ure_write_mem(sc, reg, index | byen, &temp, 4));
286 }
287 
288 int
289 ure_write_4(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
290 {
291 	uint8_t	temp[4];
292 
293 	USETDW(temp, val);
294 	return (ure_write_mem(sc, reg, index | URE_BYTE_EN_DWORD, &temp, 4));
295 }
296 
297 uint16_t
298 ure_ocp_reg_read(struct ure_softc *sc, uint16_t addr)
299 {
300 	uint16_t	reg;
301 
302 	ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
303 	reg = (addr & 0x0fff) | 0xb000;
304 
305 	return (ure_read_2(sc, reg, URE_MCU_TYPE_PLA));
306 }
307 
308 void
309 ure_ocp_reg_write(struct ure_softc *sc, uint16_t addr, uint16_t data)
310 {
311 	uint16_t	reg;
312 
313 	ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
314 	reg = (addr & 0x0fff) | 0xb000;
315 
316 	ure_write_2(sc, reg, URE_MCU_TYPE_PLA, data);
317 }
318 
319 int
320 ure_miibus_readreg(struct device *dev, int phy, int reg)
321 {
322 	struct ure_softc	*sc = (void *)dev;
323 	uint16_t		val;
324 
325 	if (usbd_is_dying(sc->ure_udev))
326 		return 0;
327 
328 	/* Let the rgephy driver read the URE_PLA_PHYSTATUS register. */
329 	if (reg == RL_GMEDIASTAT)
330 		return ure_read_1(sc, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA);
331 
332 	ure_lock_mii(sc);
333 	val = ure_ocp_reg_read(sc, URE_OCP_BASE_MII + reg * 2);
334 	ure_unlock_mii(sc);
335 
336 	return val;	/* letoh16? */
337 }
338 
339 void
340 ure_miibus_writereg(struct device *dev, int phy, int reg, int val)
341 {
342 	struct ure_softc	*sc = (void *)dev;
343 
344 	ure_lock_mii(sc);
345 	ure_ocp_reg_write(sc, URE_OCP_BASE_MII + reg * 2, val);	/* htole16? */
346 	ure_unlock_mii(sc);
347 }
348 
349 void
350 ure_miibus_statchg(struct device *dev)
351 {
352 	struct ure_softc	*sc = (void *)dev;
353 	struct mii_data		*mii = &sc->ure_mii;
354 	struct ifnet		*ifp = &sc->ure_ac.ac_if;
355 
356 	if ((ifp->if_flags & IFF_RUNNING) == 0)
357 		return;
358 
359 	sc->ure_flags &= ~URE_FLAG_LINK;
360 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
361 	    (IFM_ACTIVE | IFM_AVALID)) {
362 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
363 		case IFM_10_T:
364 		case IFM_100_TX:
365 			sc->ure_flags |= URE_FLAG_LINK;
366 			break;
367 		case IFM_1000_T:
368 			if ((sc->ure_flags & URE_FLAG_8152) != 0)
369 				break;
370 			sc->ure_flags |= URE_FLAG_LINK;
371 			break;
372 		default:
373 			break;
374 		}
375 	}
376 
377 	/* Lost link, do nothing. */
378 	if ((sc->ure_flags & URE_FLAG_LINK) == 0)
379 		return;
380 }
381 
382 int
383 ure_ifmedia_upd(struct ifnet *ifp)
384 {
385 	struct ure_softc	*sc = ifp->if_softc;
386 	struct mii_data		*mii = &sc->ure_mii;
387 	struct ifmedia		*ifm = &sc->ure_ifmedia;
388 	int			anar, gig, err, reg;
389 
390 	if (sc->ure_flags & URE_FLAG_8156) {
391 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
392 			return (EINVAL);
393 
394 		reg = ure_ocp_reg_read(sc, 0xa5d4);
395 		reg &= ~URE_ADV_2500TFDX;
396 
397 		anar = gig = 0;
398 		switch (IFM_SUBTYPE(ifm->ifm_media)) {
399 		case IFM_AUTO:
400 			anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
401 			gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
402 			reg |= URE_ADV_2500TFDX;
403 			break;
404 		case IFM_2500_T:
405 			anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
406 			gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
407 			reg |= URE_ADV_2500TFDX;
408 			ifp->if_baudrate = IF_Mbps(2500);
409 			break;
410 		case IFM_1000_T:
411 			anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
412 			gig |= GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
413 			ifp->if_baudrate = IF_Gbps(1);
414 			break;
415 		case IFM_100_TX:
416 			anar |= ANAR_TX | ANAR_TX_FD;
417 			ifp->if_baudrate = IF_Mbps(100);
418 			break;
419 		case IFM_10_T:
420 			anar |= ANAR_10 | ANAR_10_FD;
421 			ifp->if_baudrate = IF_Mbps(10);
422 			break;
423 		default:
424 			printf("%s: unsupported media type\n",
425 			    sc->ure_dev.dv_xname);
426 			return (EINVAL);
427 		}
428 
429 		ure_ocp_reg_write(sc, URE_OCP_BASE_MII + MII_ANAR * 2,
430 		    anar | ANAR_PAUSE_ASYM | ANAR_FC);
431 		ure_ocp_reg_write(sc, URE_OCP_BASE_MII + MII_100T2CR * 2, gig);
432 		ure_ocp_reg_write(sc, 0xa5d4, reg);
433 		ure_ocp_reg_write(sc, URE_OCP_BASE_MII + MII_BMCR,
434 		    BMCR_AUTOEN | BMCR_STARTNEG);
435 
436 		return (0);
437 	}
438 
439 	if (mii->mii_instance) {
440 		struct mii_softc *miisc;
441 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
442 			PHY_RESET(miisc);
443 	}
444 
445 	err = mii_mediachg(mii);
446 	if (err == ENXIO)
447 		return (0);
448 	else
449 		return (err);
450 }
451 
452 void
453 ure_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
454 {
455 	struct ure_softc	*sc = ifp->if_softc;
456 	struct mii_data		*mii = &sc->ure_mii;
457 	uint16_t		status = 0;
458 
459 	if (sc->ure_flags & URE_FLAG_8156) {
460 		ifmr->ifm_status = IFM_AVALID;
461 		if (ure_get_link_status(sc)) {
462 			ifmr->ifm_status |= IFM_ACTIVE;
463 			status = ure_read_2(sc, URE_PLA_PHYSTATUS,
464 			    URE_MCU_TYPE_PLA);
465 			if ((status & URE_PHYSTATUS_FDX) ||
466 			    (status & URE_PHYSTATUS_2500MBPS))
467 				ifmr->ifm_active |= IFM_FDX;
468 			else
469 				ifmr->ifm_active |= IFM_HDX;
470 			if (status & URE_PHYSTATUS_10MBPS)
471 				ifmr->ifm_active |= IFM_10_T;
472 			else if (status & URE_PHYSTATUS_100MBPS)
473 				ifmr->ifm_active |= IFM_100_TX;
474 			else if (status & URE_PHYSTATUS_1000MBPS)
475 				ifmr->ifm_active |= IFM_1000_T;
476 			else if (status & URE_PHYSTATUS_2500MBPS)
477 				ifmr->ifm_active |= IFM_2500_T;
478 		}
479 		return;
480 	}
481 
482 	mii_pollstat(mii);
483 	ifmr->ifm_active = mii->mii_media_active;
484 	ifmr->ifm_status = mii->mii_media_status;
485 }
486 
487 void
488 ure_add_media_types(struct ure_softc *sc)
489 {
490 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_10_T, 0, NULL);
491 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
492 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL);
493 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX, 0,
494 	    NULL);
495 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_1000_T, 0, NULL);
496 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_1000_T | IFM_FDX, 0,
497 	    NULL);
498 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_2500_T, 0, NULL);
499 	ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_2500_T | IFM_FDX, 0,
500 	    NULL);
501 }
502 
503 void
504 ure_link_state(struct ure_softc *sc)
505 {
506 	struct ifnet	*ifp = &sc->ure_ac.ac_if;
507 	int		link = LINK_STATE_DOWN;
508 
509 	if (ure_get_link_status(sc))
510 		link = LINK_STATE_UP;
511 
512 	if (ifp->if_link_state != link) {
513 		ifp->if_link_state = link;
514 		if_link_state_change(ifp);
515 	}
516 }
517 
518 int
519 ure_get_link_status(struct ure_softc *sc)
520 {
521 	if (ure_read_2(sc, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA) &
522 	    URE_PHYSTATUS_LINK) {
523 		sc->ure_flags |= URE_FLAG_LINK;
524 		return (1);
525 	} else {
526 		sc->ure_flags &= ~URE_FLAG_LINK;
527 		return (0);
528 	}
529 }
530 
531 void
532 ure_iff(struct ure_softc *sc)
533 {
534 	struct ifnet		*ifp = &sc->ure_ac.ac_if;
535 	struct ether_multi	*enm;
536 	struct ether_multistep	step;
537 	uint32_t		hashes[2] = { 0, 0 };
538 	uint32_t		hash;
539 	uint32_t		rxmode;
540 
541 	if (usbd_is_dying(sc->ure_udev))
542 		return;
543 
544 	rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA);
545 	rxmode &= ~URE_RCR_ACPT_ALL;
546 	ifp->if_flags &= ~IFF_ALLMULTI;
547 
548 	/*
549 	 * Always accept frames destined to our station address.
550 	 * Always accept broadcast frames.
551 	 */
552 	rxmode |= URE_RCR_APM | URE_RCR_AB;
553 
554 	if (ifp->if_flags & IFF_PROMISC || sc->ure_ac.ac_multirangecnt > 0) {
555 		ifp->if_flags |= IFF_ALLMULTI;
556 		rxmode |= URE_RCR_AM;
557 		if (ifp->if_flags & IFF_PROMISC)
558 			rxmode |= URE_RCR_AAP;
559 		hashes[0] = hashes[1] = 0xffffffff;
560 	} else {
561 		rxmode |= URE_RCR_AM;
562 
563 		ETHER_FIRST_MULTI(step, &sc->ure_ac, enm);
564 		while (enm != NULL) {
565 			hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN)
566 			    >> 26;
567 			if (hash < 32)
568 				hashes[0] |= (1 << hash);
569 			else
570 				hashes[1] |= (1 << (hash - 32));
571 
572 			ETHER_NEXT_MULTI(step, enm);
573 		}
574 
575 		hash = swap32(hashes[0]);
576 		hashes[0] = swap32(hashes[1]);
577 		hashes[1] = hash;
578 	}
579 
580 	ure_write_mem(sc, URE_PLA_MAR, URE_MCU_TYPE_PLA | URE_BYTE_EN_DWORD,
581 	    hashes, sizeof(hashes));
582 	ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode);
583 }
584 
585 void
586 ure_rxvlan(struct ure_softc *sc)
587 {
588 	struct ifnet	*ifp = &sc->ure_ac.ac_if;
589 	uint16_t	reg;
590 
591 	if (sc->ure_flags & URE_FLAG_8156) {
592 		reg = ure_read_2(sc, 0xc012, URE_MCU_TYPE_PLA);
593 		reg &= ~0x00c0;
594 		if (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING)
595 			reg |= 0x00c0;
596 		ure_write_2(sc, 0xc012, URE_MCU_TYPE_PLA, reg);
597 	} else {
598 		reg = ure_read_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA);
599 		reg &= ~URE_CPCR_RX_VLAN;
600 		if (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING)
601 			reg |= URE_CPCR_RX_VLAN;
602 		ure_write_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA, reg);
603 	}
604 }
605 
606 void
607 ure_reset(struct ure_softc *sc)
608 {
609 	int	i;
610 
611 	ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RST);
612 
613 	for (i = 0; i < URE_TIMEOUT; i++) {
614 		if (!(ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) &
615 		    URE_CR_RST))
616 			break;
617 		DELAY(100);
618 	}
619 	if (i == URE_TIMEOUT)
620 		printf("%s: reset never completed\n", sc->ure_dev.dv_xname);
621 }
622 
623 void
624 ure_watchdog(struct ifnet *ifp)
625 {
626 	struct ure_softc	*sc = ifp->if_softc;
627 
628 	if (usbd_is_dying(sc->ure_udev))
629 		return;
630 
631 	ifp->if_oerrors++;
632 	printf("%s: watchdog timeout\n", sc->ure_dev.dv_xname);
633 }
634 
635 void
636 ure_init(void *xsc)
637 {
638 	struct ure_softc	*sc = xsc;
639 	struct ure_chain	*c;
640 	struct ifnet		*ifp = &sc->ure_ac.ac_if;
641 	usbd_status		err;
642 	uint32_t		reg = 0;
643 	int			s, i;
644 
645 	s = splnet();
646 
647 	/* Cancel pending I/O. */
648 	ure_stop(sc);
649 
650 	if (sc->ure_flags & URE_FLAG_8152)
651 		ure_rtl8152_nic_reset(sc);
652 	else
653 		ure_rtl8153_nic_reset(sc);
654 
655 	if (ure_rx_list_init(sc) == ENOBUFS) {
656 		printf("%s: rx list init failed\n", sc->ure_dev.dv_xname);
657 		splx(s);
658 		return;
659 	}
660 
661 	if (ure_tx_list_init(sc) == ENOBUFS) {
662 		printf("%s: tx list init failed\n", sc->ure_dev.dv_xname);
663 		splx(s);
664 		return;
665 	}
666 
667 	/* Set MAC address. */
668 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG);
669 	ure_write_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES,
670 	    sc->ure_ac.ac_enaddr, 8);
671 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML);
672 
673 	if (!(sc->ure_flags & URE_FLAG_8152)) {
674 		reg = sc->ure_rxbufsz - URE_FRAMELEN(ifp->if_mtu) +
675 		    sizeof(struct ure_rxpkt) + URE_RX_BUF_ALIGN;
676 		if (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156)) {
677 			ure_write_2(sc, URE_USB_RX_EARLY_SIZE, URE_MCU_TYPE_USB,
678 			    reg / 8);
679 
680 			ure_write_2(sc, URE_USB_RX_EARLY_AGG, URE_MCU_TYPE_USB,
681 			    (sc->ure_flags & URE_FLAG_8153B) ? 16 : 80);
682 			ure_write_2(sc, URE_USB_PM_CTRL_STATUS,
683 			    URE_MCU_TYPE_USB, 1875);
684 		} else {
685 			ure_write_2(sc, URE_USB_RX_EARLY_SIZE, URE_MCU_TYPE_USB,
686 			    reg / 4);
687 			switch (sc->ure_udev->speed) {
688 			case USB_SPEED_SUPER:
689 				reg = URE_COALESCE_SUPER / 8;
690 				break;
691 			case USB_SPEED_HIGH:
692 				reg = URE_COALESCE_HIGH / 8;
693 				break;
694 			default:
695 				reg = URE_COALESCE_SLOW / 8;
696 				break;
697 			}
698 			ure_write_2(sc, URE_USB_RX_EARLY_AGG, URE_MCU_TYPE_USB,
699 			    reg);
700 		}
701 	}
702 
703 	/* Reset the packet filter. */
704 	URE_CLRBIT_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA, URE_FMC_FCR_MCU_EN);
705 	URE_SETBIT_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA, URE_FMC_FCR_MCU_EN);
706 
707 	/* Enable transmit and receive. */
708 	URE_SETBIT_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RE | URE_CR_TE);
709 
710 	if (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156)) {
711 		ure_write_1(sc, URE_USB_UPT_RXDMA_OWN, URE_MCU_TYPE_USB,
712 		    URE_OWN_UPDATE | URE_OWN_CLEAR);
713 	}
714 
715 	URE_CLRBIT_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, URE_RXDY_GATED_EN);
716 
717 	/* Load the multicast filter. */
718 	ure_iff(sc);
719 
720 	/* Open RX and TX pipes. */
721 	err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_RX],
722 	    USBD_EXCLUSIVE_USE, &sc->ure_ep[URE_ENDPT_RX]);
723 	if (err) {
724 		printf("%s: open rx pipe failed: %s\n",
725 		    sc->ure_dev.dv_xname, usbd_errstr(err));
726 		splx(s);
727 		return;
728 	}
729 
730 	err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_TX],
731 	    USBD_EXCLUSIVE_USE, &sc->ure_ep[URE_ENDPT_TX]);
732 	if (err) {
733 		printf("%s: open tx pipe failed: %s\n",
734 		    sc->ure_dev.dv_xname, usbd_errstr(err));
735 		splx(s);
736 		return;
737 	}
738 
739 	/* Start up the receive pipe. */
740 	for (i = 0; i < URE_RX_LIST_CNT; i++) {
741 		c = &sc->ure_cdata.rx_chain[i];
742 		usbd_setup_xfer(c->uc_xfer, sc->ure_ep[URE_ENDPT_RX],
743 		    c, c->uc_buf, sc->ure_rxbufsz,
744 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
745 		    USBD_NO_TIMEOUT, ure_rxeof);
746 		usbd_transfer(c->uc_xfer);
747 	}
748 
749 	ure_ifmedia_upd(ifp);
750 
751 	/* Indicate we are up and running. */
752 	sc->ure_flags &= ~URE_FLAG_LINK;
753 	ifp->if_flags |= IFF_RUNNING;
754 	ifq_clr_oactive(&ifp->if_snd);
755 
756 	timeout_add_sec(&sc->ure_stat_ch, 1);
757 
758 	splx(s);
759 }
760 
761 void
762 ure_start(struct ifnet *ifp)
763 {
764 	struct ure_softc	*sc = ifp->if_softc;
765 	struct mbuf		*m_head = NULL;
766 
767 	if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd) ||
768 	    !(sc->ure_flags & URE_FLAG_LINK))
769 		return;
770 
771 	for (;;) {
772 		if (sc->ure_cdata.tx_cnt == sc->ure_tx_list_cnt) {
773 			ifq_set_oactive(&ifp->if_snd);
774 			break;
775 		}
776 
777 		m_head = ifq_deq_begin(&ifp->if_snd);
778 		if (m_head == NULL)
779 			break;
780 
781 		if (ure_encap(sc, m_head)) {
782 			ifq_deq_rollback(&ifp->if_snd, m_head);
783 			ifq_set_oactive(&ifp->if_snd);
784 			break;
785 		}
786 		ifq_deq_commit(&ifp->if_snd, m_head);
787 
788 #if NBPFILTER > 0
789 		if (ifp->if_bpf)
790 			bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
791 #endif
792 		ifp->if_timer = 5;
793 	}
794 }
795 
796 void
797 ure_tick(void *xsc)
798 {
799 	struct ure_softc	*sc = xsc;
800 
801 	if (sc == NULL)
802 		return;
803 
804 	if (usbd_is_dying(sc->ure_udev))
805 		return;
806 
807 	usb_add_task(sc->ure_udev, &sc->ure_tick_task);
808 }
809 
810 void
811 ure_stop(struct ure_softc *sc)
812 {
813 	usbd_status		err;
814 	struct ifnet		*ifp;
815 	int			i;
816 
817 	ure_reset(sc);
818 
819 	ifp = &sc->ure_ac.ac_if;
820 	ifp->if_timer = 0;
821 	ifp->if_flags &= ~IFF_RUNNING;
822 	ifq_clr_oactive(&ifp->if_snd);
823 
824 	timeout_del(&sc->ure_stat_ch);
825 	sc->ure_flags &= ~URE_FLAG_LINK;
826 
827 	if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
828 		usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
829 		err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_RX]);
830 		if (err) {
831 			printf("%s: close rx pipe failed: %s\n",
832 			    sc->ure_dev.dv_xname, usbd_errstr(err));
833 		}
834 		sc->ure_ep[URE_ENDPT_RX] = NULL;
835 	}
836 
837 	if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
838 		usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
839 		err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_TX]);
840 		if (err) {
841 			printf("%s: close tx pipe failed: %s\n",
842 			    sc->ure_dev.dv_xname, usbd_errstr(err));
843 		}
844 		sc->ure_ep[URE_ENDPT_TX] = NULL;
845 	}
846 
847 	for (i = 0; i < URE_RX_LIST_CNT; i++) {
848 		if (sc->ure_cdata.rx_chain[i].uc_mbuf != NULL) {
849 			m_freem(sc->ure_cdata.rx_chain[i].uc_mbuf);
850 			sc->ure_cdata.rx_chain[i].uc_mbuf = NULL;
851 		}
852 		if (sc->ure_cdata.rx_chain[i].uc_xfer != NULL) {
853 			usbd_free_xfer(sc->ure_cdata.rx_chain[i].uc_xfer);
854 			sc->ure_cdata.rx_chain[i].uc_xfer = NULL;
855 		}
856 	}
857 
858 	for (i = 0; i < sc->ure_tx_list_cnt; i++) {
859 		if (sc->ure_cdata.tx_chain[i].uc_mbuf != NULL) {
860 			m_freem(sc->ure_cdata.tx_chain[i].uc_mbuf);
861 			sc->ure_cdata.tx_chain[i].uc_mbuf = NULL;
862 		}
863 		if (sc->ure_cdata.tx_chain[i].uc_xfer != NULL) {
864 			usbd_free_xfer(sc->ure_cdata.tx_chain[i].uc_xfer);
865 			sc->ure_cdata.tx_chain[i].uc_xfer = NULL;
866 		}
867 	}
868 }
869 
870 void
871 ure_rtl8152_init(struct ure_softc *sc)
872 {
873 	uint32_t	pwrctrl;
874 
875 	/* Disable ALDPS. */
876 	ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
877 	    URE_DIS_SDSAVE);
878 	usbd_delay_ms(sc->ure_udev, 20);
879 
880 	if (sc->ure_chip & URE_CHIP_VER_4C00)
881 		URE_CLRBIT_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
882 		    URE_LED_MODE_MASK);
883 
884 	URE_CLRBIT_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB, URE_POWER_CUT);
885 	URE_CLRBIT_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB,
886 	    URE_RESUME_INDICATE);
887 
888 	URE_SETBIT_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
889 	    URE_TX_10M_IDLE_EN | URE_PFM_PWM_SWITCH);
890 	pwrctrl = ure_read_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA);
891 	pwrctrl &= ~URE_MCU_CLK_RATIO_MASK;
892 	pwrctrl |= URE_MCU_CLK_RATIO | URE_D3_CLK_GATED_EN;
893 	ure_write_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, pwrctrl);
894 	ure_write_2(sc, URE_PLA_GPHY_INTR_IMR, URE_MCU_TYPE_PLA,
895 	    URE_GPHY_STS_MSK | URE_SPEED_DOWN_MSK | URE_SPDWN_RXDV_MSK |
896 	    URE_SPDWN_LINKCHG_MSK);
897 
898 	URE_SETBIT_2(sc, URE_PLA_RSTTALLY, URE_MCU_TYPE_PLA, URE_TALLY_RESET);
899 
900 	/* Enable Rx aggregation. */
901 	URE_CLRBIT_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
902 	    URE_RX_AGG_DISABLE | URE_RX_ZERO_EN);
903 }
904 
905 void
906 ure_rtl8153_init(struct ure_softc *sc)
907 {
908 	uint16_t	reg;
909 	uint8_t		u1u2[8];
910 	int		i;
911 
912 	memset(u1u2, 0x00, sizeof(u1u2));
913 	ure_write_mem(sc, URE_USB_TOLERANCE, URE_BYTE_EN_SIX_BYTES, u1u2,
914 	    sizeof(u1u2));
915 
916         for (i = 0; i < 500; i++) {
917 		if (ure_read_2(sc, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) &
918 		    URE_AUTOLOAD_DONE)
919 			break;
920 		usbd_delay_ms(sc->ure_udev, 20);
921 	}
922 	if (i == 500)
923 		printf("%s: timeout waiting for chip autoload\n",
924 		    sc->ure_dev.dv_xname);
925 
926 	ure_rtl8153_phy_status(sc, 0);
927 
928 	if (sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10 |
929 	    URE_CHIP_VER_5C20)) {
930 		ure_ocp_reg_write(sc, URE_OCP_ADC_CFG,
931 		    URE_CKADSEL_L | URE_ADC_EN | URE_EN_EMI_L);
932 	}
933 
934 	ure_rtl8153_phy_status(sc, 1);
935 
936 	URE_CLRBIT_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, URE_U2P3_ENABLE);
937 
938 	if (sc->ure_chip & URE_CHIP_VER_5C10) {
939 		reg = ure_read_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB);
940 		reg &= ~URE_PWD_DN_SCALE_MASK;
941 		reg |= URE_PWD_DN_SCALE(96);
942 		ure_write_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB, reg);
943 
944 		URE_SETBIT_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB,
945 		    URE_USB2PHY_L1 | URE_USB2PHY_SUSPEND);
946 	} else if (sc->ure_chip & URE_CHIP_VER_5C20) {
947 		URE_CLRBIT_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA,
948 		    URE_ECM_ALDPS);
949 	}
950 	if (sc->ure_chip & (URE_CHIP_VER_5C20 | URE_CHIP_VER_5C30)) {
951 		if (ure_read_2(sc, URE_USB_BURST_SIZE, URE_MCU_TYPE_USB))
952 			URE_SETBIT_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB,
953 			    URE_DYNAMIC_BURST);
954 		else
955 			URE_CLRBIT_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB,
956 			    URE_DYNAMIC_BURST);
957 	}
958 
959 	URE_SETBIT_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB, URE_EP4_FULL_FC);
960 
961 	URE_CLRBIT_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB, URE_TIMER11_EN);
962 
963 	URE_CLRBIT_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
964 	    URE_LED_MODE_MASK);
965 
966 	if ((sc->ure_chip & URE_CHIP_VER_5C10) &&
967 	    sc->ure_udev->speed != USB_SPEED_SUPER)
968 		reg = URE_LPM_TIMER_500MS;
969 	else
970 		reg = URE_LPM_TIMER_500US;
971 	ure_write_1(sc, URE_USB_LPM_CTRL, URE_MCU_TYPE_USB,
972 	    URE_FIFO_EMPTY_1FB | URE_ROK_EXIT_LPM | reg);
973 
974 	reg = ure_read_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB);
975 	reg &= ~URE_SEN_VAL_MASK;
976 	reg |= URE_SEN_VAL_NORMAL | URE_SEL_RXIDLE;
977 	ure_write_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB, reg);
978 
979 	ure_write_2(sc, URE_USB_CONNECT_TIMER, URE_MCU_TYPE_USB, 0x0001);
980 
981 	URE_CLRBIT_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB,
982 	    URE_PWR_EN | URE_PHASE2_EN);
983 	URE_CLRBIT_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB, URE_PCUT_STATUS);
984 
985 	memset(u1u2, 0xff, sizeof(u1u2));
986 	ure_write_mem(sc, URE_USB_TOLERANCE, URE_BYTE_EN_SIX_BYTES, u1u2,
987 	    sizeof(u1u2));
988 
989 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, 0);
990 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA, 0);
991 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA, 0);
992 	ure_write_2(sc, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA, 0);
993 
994 	/* Enable Rx aggregation. */
995 	URE_CLRBIT_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
996 	    URE_RX_AGG_DISABLE | URE_RX_ZERO_EN);
997 
998 	URE_SETBIT_2(sc, URE_PLA_RSTTALLY, URE_MCU_TYPE_PLA, URE_TALLY_RESET);
999 }
1000 
1001 void
1002 ure_rtl8153b_init(struct ure_softc *sc)
1003 {
1004 	uint16_t	reg;
1005 	int		i;
1006 
1007 	if (sc->ure_flags & URE_FLAG_8156) {
1008 		URE_CLRBIT_1(sc, 0xd26b, URE_MCU_TYPE_USB, 0x01);
1009 		ure_write_2(sc, 0xd32a, URE_MCU_TYPE_USB, 0);
1010 		URE_SETBIT_2(sc, 0xcfee, URE_MCU_TYPE_USB, 0x0020);
1011 	}
1012 
1013 	URE_CLRBIT_2(sc, URE_USB_LPM_CONFIG, URE_MCU_TYPE_USB, LPM_U1U2_EN);
1014 
1015         for (i = 0; i < 500; i++) {
1016 		if (ure_read_2(sc, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) &
1017 		    URE_AUTOLOAD_DONE)
1018 			break;
1019 		usbd_delay_ms(sc->ure_udev, 20);
1020 	}
1021 	if (i == 500)
1022 		printf("%s: timeout waiting for chip autoload\n",
1023 		    sc->ure_dev.dv_xname);
1024 
1025 	ure_rtl8153_phy_status(sc, 0);
1026 	ure_rtl8153_phy_status(sc, 1);
1027 
1028 	URE_CLRBIT_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, URE_U2P3_ENABLE);
1029 
1030 	/* MSC timer, 32760 ms. */
1031 	ure_write_2(sc, URE_USB_MSC_TIMER, URE_MCU_TYPE_USB, 0x0fff);
1032 
1033 	/* U1/U2/L1 idle timer, 500 us. */
1034 	ure_write_2(sc, URE_USB_U1U2_TIMER, URE_MCU_TYPE_USB, 500);
1035 
1036 	URE_CLRBIT_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB, URE_PWR_EN);
1037 	URE_CLRBIT_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB, URE_PCUT_STATUS);
1038 
1039 	URE_CLRBIT_1(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB,
1040 	    URE_UPS_EN | URE_USP_PREWAKE);
1041 	URE_CLRBIT_1(sc, 0xcfff, URE_MCU_TYPE_USB, 0x01);
1042 
1043 	if (!(sc->ure_flags & URE_FLAG_8156)) {
1044 		URE_CLRBIT_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB,
1045 		    URE_PCUT_STATUS);
1046 		ure_rtl8153_phy_status(sc, 0);
1047 	}
1048 
1049 	URE_CLRBIT_1(sc, URE_PLA_INDICATE_FALG, URE_MCU_TYPE_PLA,
1050 	    URE_UPCOMING_RUNTIME_D3);
1051 	URE_CLRBIT_1(sc, URE_PLA_SUSPEND_FLAG, URE_MCU_TYPE_PLA,
1052 	    URE_LINK_CHG_EVENT);
1053 	URE_CLRBIT_2(sc, URE_PLA_EXTRA_STATUS, URE_MCU_TYPE_PLA,
1054 	    URE_LINK_CHANGE_FLAG);
1055 
1056 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG);
1057 	URE_CLRBIT_2(sc, URE_PLA_CONFIG34, URE_MCU_TYPE_PLA,
1058 	    URE_LINK_OFF_WAKE_EN);
1059 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML);
1060 
1061 	URE_SETBIT_2(sc, URE_USB_LPM_CONFIG, URE_MCU_TYPE_USB, LPM_U1U2_EN);
1062 
1063 	/* MAC clock speed down. */
1064 	if (sc->ure_flags & URE_FLAG_8156) {
1065 		ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, 0x0403);
1066 		reg = ure_read_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA) &
1067 		    ~0xff;
1068 		reg |= URE_MAC_CLK_SPDWN_EN | 0x0003;
1069 		ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA, reg);
1070 
1071 		URE_CLRBIT_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA,
1072 		    0x4000);
1073 
1074 		reg = ure_read_2(sc, URE_PLA_EXTRA_STATUS, URE_MCU_TYPE_PLA);
1075 		if (ure_get_link_status(sc))
1076 			reg |= 0x8000;
1077 		else
1078 			reg &= ~0x8000;
1079 		reg |= 0x0001;
1080 		ure_write_2(sc, URE_PLA_EXTRA_STATUS, URE_MCU_TYPE_PLA, reg);
1081 	} else
1082 		URE_SETBIT_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA,
1083 		    URE_MAC_CLK_SPDWN_EN);
1084 
1085 	/* Enable Rx aggregation. */
1086 	URE_CLRBIT_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
1087 	    URE_RX_AGG_DISABLE | URE_RX_ZERO_EN);
1088 
1089 	if (sc->ure_flags & URE_FLAG_8156)
1090 		URE_SETBIT_1(sc, 0xcfd9, URE_MCU_TYPE_USB, 0x04);
1091 
1092 	URE_SETBIT_2(sc, URE_PLA_RSTTALLY, URE_MCU_TYPE_PLA, URE_TALLY_RESET);
1093 }
1094 
1095 void
1096 ure_rtl8152_nic_reset(struct ure_softc *sc)
1097 {
1098 	uint32_t	rx_fifo1, rx_fifo2;
1099 	int		i;
1100 
1101 	/* Disable ALDPS. */
1102 	ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
1103 	    URE_DIS_SDSAVE);
1104 	usbd_delay_ms(sc->ure_udev, 20);
1105 
1106 	URE_CLRBIT_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, URE_RCR_ACPT_ALL);
1107 
1108 	URE_SETBIT_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, URE_RXDY_GATED_EN);
1109 	ure_disable_teredo(sc);
1110 	ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML);
1111 	ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, 0);
1112 
1113 	URE_CLRBIT_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA, URE_NOW_IS_OOB);
1114 	URE_CLRBIT_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, URE_MCU_BORW_EN);
1115 	for (i = 0; i < URE_TIMEOUT; i++) {
1116 		if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
1117 		    URE_LINK_LIST_READY)
1118 			break;
1119 		usbd_delay_ms(sc->ure_udev, 1);
1120 	}
1121 	if (i == URE_TIMEOUT)
1122 		printf("%s: timeout waiting for OOB control\n",
1123 		    sc->ure_dev.dv_xname);
1124 	URE_SETBIT_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, URE_RE_INIT_LL);
1125 	for (i = 0; i < URE_TIMEOUT; i++) {
1126 		if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
1127 		    URE_LINK_LIST_READY)
1128 			break;
1129 		usbd_delay_ms(sc->ure_udev, 1);
1130 	}
1131 	if (i == URE_TIMEOUT)
1132 		printf("%s: timeout waiting for OOB control\n",
1133 		    sc->ure_dev.dv_xname);
1134 
1135 	ure_reset(sc);
1136 
1137 	/* Configure Rx FIFO threshold. */
1138 	ure_write_4(sc, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA,
1139 	    URE_RXFIFO_THR1_NORMAL);
1140 	if (sc->ure_udev->speed == USB_SPEED_FULL) {
1141 		rx_fifo1 = URE_RXFIFO_THR2_FULL;
1142 		rx_fifo2 = URE_RXFIFO_THR3_FULL;
1143 	} else {
1144 		rx_fifo1 = URE_RXFIFO_THR2_HIGH;
1145 		rx_fifo2 = URE_RXFIFO_THR3_HIGH;
1146 	}
1147 	ure_write_4(sc, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA, rx_fifo1);
1148 	ure_write_4(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, rx_fifo2);
1149 
1150 	/* Configure Tx FIFO threshold. */
1151 	ure_write_4(sc, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA,
1152 	    URE_TXFIFO_THR_NORMAL);
1153 
1154 	ure_write_1(sc, URE_USB_TX_AGG, URE_MCU_TYPE_USB,
1155 	    URE_TX_AGG_MAX_THRESHOLD);
1156 	ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB, URE_RX_THR_HIGH);
1157 	ure_write_4(sc, URE_USB_TX_DMA, URE_MCU_TYPE_USB,
1158 	    URE_TEST_MODE_DISABLE | URE_TX_SIZE_ADJUST1);
1159 
1160 	ure_rxvlan(sc);
1161         ure_write_2(sc, URE_PLA_RMS, URE_MCU_TYPE_PLA,
1162 	    ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN);
1163 	URE_SETBIT_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA, URE_TCR0_AUTO_FIFO);
1164 
1165 	/* Enable ALDPS. */
1166 	ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG,
1167 	    URE_ENPWRSAVE | URE_ENPDNPS | URE_LINKENA | URE_DIS_SDSAVE);
1168 }
1169 
1170 void
1171 ure_rtl8153_nic_reset(struct ure_softc *sc)
1172 {
1173 	struct ifnet	*ifp = &sc->ure_ac.ac_if;
1174 	uint32_t	reg = 0;
1175 	uint8_t		u1u2[8] = { 0 };
1176 	int		i;
1177 
1178 	if (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156)) {
1179 		URE_CLRBIT_2(sc, URE_USB_LPM_CONFIG, URE_MCU_TYPE_USB,
1180 		    LPM_U1U2_EN);
1181 	} else {
1182 		memset(u1u2, 0x00, sizeof(u1u2));
1183 		ure_write_mem(sc, URE_USB_TOLERANCE, URE_BYTE_EN_SIX_BYTES,
1184 		    u1u2, sizeof(u1u2));
1185 	}
1186 	URE_CLRBIT_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, URE_U2P3_ENABLE);
1187 
1188 	/* Disable ALDPS. */
1189 	ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
1190 	    ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
1191 	for (i = 0; i < 20; i++) {
1192 		usbd_delay_ms(sc->ure_udev, 1);
1193 		if (ure_read_2(sc, 0xe000, URE_MCU_TYPE_PLA) & 0x0100)
1194 			break;
1195 	}
1196 	if (sc->ure_flags & URE_FLAG_8153B) {
1197 		URE_CLRBIT_4(sc, URE_USB_UPS_FLAGS, URE_MCU_TYPE_USB,
1198 		    URE_UPS_FLAGS_EN_ALDPS);
1199 	}
1200 
1201 	if (!(sc->ure_flags & URE_FLAG_8156)) {
1202 		ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, 0);
1203 		ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA, 0);
1204 		ure_write_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA, 0);
1205 		ure_write_2(sc, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA, 0);
1206 	}
1207 	URE_SETBIT_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, URE_RXDY_GATED_EN);
1208 	ure_disable_teredo(sc);
1209 
1210 	URE_CLRBIT_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, URE_RCR_ACPT_ALL);
1211 
1212 	if (sc->ure_flags & URE_FLAG_8156)
1213 		ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, 0);
1214 	else
1215 		ure_reset(sc);
1216 
1217 	ure_reset_bmu(sc);
1218 
1219 	URE_CLRBIT_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA, URE_NOW_IS_OOB);
1220 	URE_CLRBIT_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA, URE_MCU_BORW_EN);
1221 
1222 	if (!(sc->ure_flags & URE_FLAG_8156)) {
1223 		for (i = 0; i < URE_TIMEOUT; i++) {
1224 			if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
1225 			    URE_LINK_LIST_READY)
1226 				break;
1227 			usbd_delay_ms(sc->ure_udev, 1);
1228 		}
1229 		if (i == URE_TIMEOUT)
1230 			printf("%s: timeout waiting for OOB control\n",
1231 			    sc->ure_dev.dv_xname);
1232 		URE_SETBIT_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
1233 		    URE_RE_INIT_LL);
1234 		for (i = 0; i < URE_TIMEOUT; i++) {
1235 			if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
1236 			    URE_LINK_LIST_READY)
1237 				break;
1238 			usbd_delay_ms(sc->ure_udev, 1);
1239 		}
1240 		if (i == URE_TIMEOUT)
1241 			printf("%s: timeout waiting for OOB control\n",
1242 			    sc->ure_dev.dv_xname);
1243 	}
1244 	ure_rxvlan(sc);
1245 	ure_write_2(sc, URE_PLA_RMS, URE_MCU_TYPE_PLA,
1246 	    URE_FRAMELEN(ifp->if_mtu));
1247 	ure_write_1(sc, URE_PLA_MTPS, URE_MCU_TYPE_PLA, MTPS_JUMBO);
1248 
1249 	if (!(sc->ure_flags & URE_FLAG_8156)) {
1250 		URE_SETBIT_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA,
1251 		    URE_TCR0_AUTO_FIFO);
1252 		ure_reset(sc);
1253 	}
1254 
1255 	/* Configure Rx FIFO threshold. */
1256 	if (sc->ure_flags & URE_FLAG_8156) {
1257 		ure_write_2(sc, URE_PLA_RXFIFO_CTRL0 + 2, URE_MCU_TYPE_PLA,
1258 		    0x0008);
1259 		ure_write_2(sc, URE_PLA_RXFIFO_CTRL1 + 2, URE_MCU_TYPE_PLA,
1260 		    0x0100);
1261 		ure_write_2(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, 0);
1262 	} else {
1263 		ure_write_4(sc, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA,
1264 		    URE_RXFIFO_THR1_NORMAL);
1265 		ure_write_2(sc, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA,
1266 		    URE_RXFIFO_THR2_NORMAL);
1267 		ure_write_2(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA,
1268 		    URE_RXFIFO_THR3_NORMAL);
1269 	}
1270 
1271 	/* Configure Tx FIFO threshold. */
1272 	ure_write_4(sc, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA,
1273 	    URE_TXFIFO_THR_NORMAL2);
1274 
1275 	if (sc->ure_flags & URE_FLAG_8156) {
1276 		URE_CLRBIT_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA,
1277 		    0x4000);
1278 		ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB,
1279 		    0x00600400);
1280 	} else if (sc->ure_flags & URE_FLAG_8153B) {
1281 		ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB,
1282 		    URE_RX_THR_B);
1283 	}
1284 
1285 	/* Enable ALDPS. */
1286 	ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
1287 	    ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) | URE_EN_ALDPS);
1288 	if (sc->ure_flags & URE_FLAG_8153B) {
1289 		reg = ure_read_4(sc, URE_USB_UPS_FLAGS, URE_MCU_TYPE_USB) &
1290 		    URE_UPS_FLAGS_MASK;
1291 		ure_write_4(sc, URE_USB_UPS_FLAGS, URE_MCU_TYPE_USB,
1292 		    reg | URE_UPS_FLAGS_EN_ALDPS);
1293 	}
1294 
1295 	if ((sc->ure_chip & (URE_CHIP_VER_5C20 | URE_CHIP_VER_5C30)) ||
1296 	    (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156)))
1297 		URE_SETBIT_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB,
1298 		    URE_U2P3_ENABLE);
1299 
1300 	if (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156)) {
1301 		URE_SETBIT_2(sc, URE_USB_LPM_CONFIG, URE_MCU_TYPE_USB,
1302 		    LPM_U1U2_EN);
1303 	} else {
1304 		memset(u1u2, 0xff, sizeof(u1u2));
1305 		ure_write_mem(sc, URE_USB_TOLERANCE, URE_BYTE_EN_SIX_BYTES,
1306 		    u1u2, sizeof(u1u2));
1307 	}
1308 }
1309 
1310 void
1311 ure_rtl8153_phy_status(struct ure_softc *sc, int enable)
1312 {
1313 	uint16_t	reg;
1314 	int		i;
1315 
1316 	for (i = 0; i < 500; i++) {
1317 		reg = ure_ocp_reg_read(sc, URE_OCP_PHY_STATUS) &
1318 		    URE_PHY_STAT_MASK;
1319 		if (enable) {
1320 			if (reg == URE_PHY_STAT_LAN_ON)
1321 				break;
1322 		} else {
1323 			if (reg == URE_PHY_STAT_LAN_ON ||
1324 			    reg == URE_PHY_STAT_PWRDN ||
1325 			    reg == URE_PHY_STAT_EXT_INIT)
1326 				break;
1327 		}
1328 		usbd_delay_ms(sc->ure_udev, 20);
1329 	}
1330 	if (i == 500)
1331 		printf("%s: timeout waiting for phy to stabilize\n",
1332 		    sc->ure_dev.dv_xname);
1333 }
1334 
1335 void
1336 ure_reset_bmu(struct ure_softc *sc)
1337 {
1338 	URE_CLRBIT_1(sc, URE_USB_BMU_RESET, URE_MCU_TYPE_USB,
1339 	    BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
1340 	URE_SETBIT_1(sc, URE_USB_BMU_RESET, URE_MCU_TYPE_USB,
1341 	    BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
1342 }
1343 
1344 void
1345 ure_disable_teredo(struct ure_softc *sc)
1346 {
1347 	if (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156))
1348 		ure_write_1(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA, 0xff);
1349 	else {
1350 		URE_CLRBIT_2(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA,
1351 		    URE_TEREDO_SEL | URE_TEREDO_RS_EVENT_MASK |
1352 		    URE_OOB_TEREDO_EN);
1353 	}
1354 	ure_write_2(sc, URE_PLA_WDT6_CTRL, URE_MCU_TYPE_PLA, URE_WDT6_SET_MODE);
1355 	ure_write_2(sc, URE_PLA_REALWOW_TIMER, URE_MCU_TYPE_PLA, 0);
1356 	ure_write_4(sc, URE_PLA_TEREDO_TIMER, URE_MCU_TYPE_PLA, 0);
1357 }
1358 
1359 int
1360 ure_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1361 {
1362 	struct ure_softc	*sc = ifp->if_softc;
1363 	struct ifreq		*ifr = (struct ifreq *)data;
1364 	int			s, error = 0;
1365 
1366 	s = splnet();
1367 
1368 	switch (cmd) {
1369 	case SIOCSIFADDR:
1370 		ifp->if_flags |= IFF_UP;
1371 		if (!(ifp->if_flags & IFF_RUNNING))
1372 			ure_init(sc);
1373 		break;
1374 
1375 	case SIOCSIFFLAGS:
1376 		if (ifp->if_flags & IFF_UP) {
1377 			if (ifp->if_flags & IFF_RUNNING)
1378 				error = ENETRESET;
1379 			else
1380 				ure_init(sc);
1381 		} else {
1382 			if (ifp->if_flags & IFF_RUNNING)
1383 				ure_stop(sc);
1384 		}
1385 		break;
1386 
1387 	case SIOCGIFMEDIA:
1388 	case SIOCSIFMEDIA:
1389 		if (sc->ure_flags & URE_FLAG_8156)
1390 			error = ifmedia_ioctl(ifp, ifr, &sc->ure_ifmedia, cmd);
1391 		else
1392 			error = ifmedia_ioctl(ifp, ifr, &sc->ure_mii.mii_media,
1393 			    cmd);
1394 		break;
1395 
1396 	default:
1397 		error = ether_ioctl(ifp, &sc->ure_ac, cmd, data);
1398 	}
1399 
1400 	if (error == ENETRESET) {
1401 		if (ifp->if_flags & IFF_RUNNING)
1402 			ure_iff(sc);
1403 		error = 0;
1404 	}
1405 
1406 	splx(s);
1407 
1408 	return (error);
1409 }
1410 
1411 int
1412 ure_match(struct device *parent, void *match, void *aux)
1413 {
1414 	struct usb_attach_arg	*uaa = aux;
1415 
1416 	if (uaa->iface == NULL || uaa->configno != 1)
1417 		return (UMATCH_NONE);
1418 
1419 	return (usb_lookup(ure_devs, uaa->vendor, uaa->product) != NULL ?
1420 	    UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE);
1421 }
1422 
1423 void
1424 ure_attach(struct device *parent, struct device *self, void *aux)
1425 {
1426 	struct ure_softc		*sc = (struct ure_softc *)self;
1427 	struct usb_attach_arg		*uaa = aux;
1428 	usb_interface_descriptor_t	*id;
1429 	usb_endpoint_descriptor_t	*ed;
1430 	u_char				eaddr[8]; /* 4byte padded */
1431 	struct ifnet			*ifp;
1432 	int				i, mii_flags = 0, s;
1433 	uint16_t			ver;
1434 
1435 	sc->ure_udev = uaa->device;
1436 	sc->ure_iface = uaa->iface;
1437 
1438 	usb_init_task(&sc->ure_tick_task, ure_tick_task, sc,
1439 	    USB_TASK_TYPE_GENERIC);
1440 	usb_init_task(&sc->ure_stop_task, (void (*)(void *))ure_stop, sc,
1441 	    USB_TASK_TYPE_GENERIC);
1442 
1443 	id = usbd_get_interface_descriptor(sc->ure_iface);
1444 
1445 	for (i = 0; i < id->bNumEndpoints; i++) {
1446 		ed = usbd_interface2endpoint_descriptor(sc->ure_iface, i);
1447 		if (!ed) {
1448 			printf("%s: couldn't get ep %d\n",
1449 			    sc->ure_dev.dv_xname, i);
1450 			return;
1451 		}
1452 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1453 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1454 			sc->ure_ed[URE_ENDPT_RX] = ed->bEndpointAddress;
1455 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1456 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1457 			sc->ure_ed[URE_ENDPT_TX] = ed->bEndpointAddress;
1458 		}
1459 	}
1460 
1461 	switch (uaa->product) {
1462 	case USB_PRODUCT_REALTEK_RTL8152:
1463 		sc->ure_flags |= URE_FLAG_8152;
1464 		sc->ure_rxbufsz = URE_8152_RXBUFSZ;
1465 		sc->ure_tx_list_cnt = 1;
1466 		break;
1467 	case USB_PRODUCT_REALTEK_RTL8156:
1468 		sc->ure_flags |= URE_FLAG_8156;
1469 		sc->ure_rxbufsz = URE_8153_RXBUFSZ;
1470 		sc->ure_tx_list_cnt = URE_TX_LIST_CNT;
1471 		break;
1472 	default:
1473 		sc->ure_rxbufsz = URE_8153_RXBUFSZ;
1474 		sc->ure_tx_list_cnt = 1;
1475 	}
1476 
1477 	s = splnet();
1478 
1479 	sc->ure_phyno = 0;
1480 	printf("%s: ", sc->ure_dev.dv_xname);
1481 
1482 	ver = ure_read_2(sc, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK;
1483 	switch (ver) {
1484 	case 0x4c00:
1485 		sc->ure_chip |= URE_CHIP_VER_4C00;
1486 		printf("RTL8152 (0x4c00)");
1487 		break;
1488 	case 0x4c10:
1489 		sc->ure_chip |= URE_CHIP_VER_4C10;
1490 		printf("RTL8152 (0x4c10)");
1491 		break;
1492 	case 0x5c00:
1493 		sc->ure_chip |= URE_CHIP_VER_5C00;
1494 		printf("RTL8153 (0x5c00)");
1495 		break;
1496 	case 0x5c10:
1497 		sc->ure_chip |= URE_CHIP_VER_5C10;
1498 		printf("RTL8153 (0x5c10)");
1499 		break;
1500 	case 0x5c20:
1501 		sc->ure_chip |= URE_CHIP_VER_5C20;
1502 		printf("RTL8153 (0x5c20)");
1503 		break;
1504 	case 0x5c30:
1505 		sc->ure_chip |= URE_CHIP_VER_5C30;
1506 		printf("RTL8153 (0x5c30)");
1507 		break;
1508 	case 0x6000:
1509 		sc->ure_flags = URE_FLAG_8153B;
1510 		sc->ure_tx_list_cnt = URE_TX_LIST_CNT;
1511 		printf("RTL8153B (0x6000)");
1512 		break;
1513 	case 0x6010:
1514 		sc->ure_flags = URE_FLAG_8153B;
1515 		sc->ure_tx_list_cnt = URE_TX_LIST_CNT;
1516 		printf("RTL8153B (0x6010)");
1517 		break;
1518 	case 0x7020:
1519 		printf("RTL8156 (0x7020)");
1520 		break;
1521 	case 0x7030:
1522 		printf("RTL8156 (0x7030)");
1523 		break;
1524 	default:
1525 		printf(", unknown ver %02x", ver);
1526 		break;
1527 	}
1528 
1529 	if (sc->ure_flags & URE_FLAG_8152)
1530 		ure_rtl8152_init(sc);
1531 	else if (sc->ure_flags & (URE_FLAG_8153B | URE_FLAG_8156))
1532 		ure_rtl8153b_init(sc);
1533 	else
1534 		ure_rtl8153_init(sc);
1535 
1536 	if (sc->ure_chip & URE_CHIP_VER_4C00)
1537 		ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr,
1538 		    sizeof(eaddr));
1539 	else
1540 		ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr,
1541 		    sizeof(eaddr));
1542 
1543 	printf(", address %s\n", ether_sprintf(eaddr));
1544 
1545 	bcopy(eaddr, (char *)&sc->ure_ac.ac_enaddr, ETHER_ADDR_LEN);
1546 
1547 	ifp = &sc->ure_ac.ac_if;
1548 	ifp->if_softc = sc;
1549 	strlcpy(ifp->if_xname, sc->ure_dev.dv_xname, IFNAMSIZ);
1550 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1551 	ifp->if_ioctl = ure_ioctl;
1552 	ifp->if_start = ure_start;
1553 	ifp->if_watchdog = ure_watchdog;
1554 
1555 	ifp->if_capabilities = IFCAP_VLAN_MTU | IFCAP_CSUM_IPv4 |
1556 	    IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
1557 
1558 #if NVLAN > 0
1559 	ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
1560 #endif
1561 
1562 	if (sc->ure_flags & URE_FLAG_8156) {
1563 		ifmedia_init(&sc->ure_ifmedia, IFM_IMASK, ure_ifmedia_upd,
1564 		    ure_ifmedia_sts);
1565 		ure_add_media_types(sc);
1566 		ifmedia_add(&sc->ure_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
1567 		ifmedia_set(&sc->ure_ifmedia, IFM_ETHER | IFM_AUTO);
1568 		sc->ure_ifmedia.ifm_media = sc->ure_ifmedia.ifm_cur->ifm_media;
1569 	} else {
1570 		rw_init(&sc->ure_mii_lock, "uremii");
1571 
1572 		sc->ure_mii.mii_ifp = ifp;
1573 		sc->ure_mii.mii_readreg = ure_miibus_readreg;
1574 		sc->ure_mii.mii_writereg = ure_miibus_writereg;
1575 		sc->ure_mii.mii_statchg = ure_miibus_statchg;
1576 		sc->ure_mii.mii_flags = MIIF_AUTOTSLEEP;
1577 
1578 		ifmedia_init(&sc->ure_mii.mii_media, 0, ure_ifmedia_upd,
1579 		    ure_ifmedia_sts);
1580 		if (!(sc->ure_flags & URE_FLAG_8152))
1581 			mii_flags |= MIIF_DOPAUSE;
1582 		mii_attach(self, &sc->ure_mii, 0xffffffff, sc->ure_phyno,
1583 		    MII_OFFSET_ANY, mii_flags);
1584 		if (LIST_FIRST(&sc->ure_mii.mii_phys) == NULL) {
1585 			ifmedia_add(&sc->ure_mii.mii_media,
1586 			    IFM_ETHER | IFM_NONE, 0, NULL);
1587 			ifmedia_set(&sc->ure_mii.mii_media,
1588 			    IFM_ETHER | IFM_NONE);
1589 		} else
1590 			ifmedia_set(&sc->ure_mii.mii_media,
1591 			    IFM_ETHER | IFM_AUTO);
1592 	}
1593 
1594 	if_attach(ifp);
1595 	ether_ifattach(ifp);
1596 
1597 	timeout_set(&sc->ure_stat_ch, ure_tick, sc);
1598 
1599 	splx(s);
1600 }
1601 
1602 int
1603 ure_detach(struct device *self, int flags)
1604 {
1605 	struct ure_softc	*sc = (struct ure_softc *)self;
1606 	struct ifnet		*ifp = &sc->ure_ac.ac_if;
1607 	int			s;
1608 
1609 	if (timeout_initialized(&sc->ure_stat_ch))
1610 		timeout_del(&sc->ure_stat_ch);
1611 
1612 	if (sc->ure_ep[URE_ENDPT_TX] != NULL)
1613 		usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
1614 	if (sc->ure_ep[URE_ENDPT_RX] != NULL)
1615 		usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
1616 
1617 	usb_rem_task(sc->ure_udev, &sc->ure_tick_task);
1618 	usb_rem_task(sc->ure_udev, &sc->ure_stop_task);
1619 
1620 	s = splusb();
1621 
1622 	if (--sc->ure_refcnt >= 0) {
1623 		usb_detach_wait(&sc->ure_dev);
1624 	}
1625 
1626 	if (ifp->if_flags & IFF_RUNNING)
1627 		ure_stop(sc);
1628 
1629 	mii_detach(&sc->ure_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1630 	ifmedia_delete_instance(&sc->ure_mii.mii_media, IFM_INST_ANY);
1631 	if (ifp->if_softc != NULL) {
1632 		ether_ifdetach(ifp);
1633 		if_detach(ifp);
1634 	}
1635 
1636 	splx(s);
1637 
1638 	return 0;
1639 }
1640 
1641 void
1642 ure_tick_task(void *xsc)
1643 {
1644 	int			s;
1645 	struct ure_softc	*sc = xsc;
1646 	struct mii_data		*mii;
1647 
1648 	if (sc == NULL)
1649 		return;
1650 
1651 	if (usbd_is_dying(sc->ure_udev))
1652 		return;
1653 	mii = &sc->ure_mii;
1654 
1655 	s = splnet();
1656 	if (sc->ure_flags & URE_FLAG_8156)
1657 		ure_link_state(sc);
1658 	else {
1659 		mii_tick(mii);
1660 		if ((sc->ure_flags & URE_FLAG_LINK) == 0)
1661 			ure_miibus_statchg(&sc->ure_dev);
1662 	}
1663 	timeout_add_sec(&sc->ure_stat_ch, 1);
1664 	splx(s);
1665 }
1666 
1667 void
1668 ure_lock_mii(struct ure_softc *sc)
1669 {
1670 	sc->ure_refcnt++;
1671 	rw_enter_write(&sc->ure_mii_lock);
1672 }
1673 
1674 void
1675 ure_unlock_mii(struct ure_softc *sc)
1676 {
1677 	rw_exit_write(&sc->ure_mii_lock);
1678 	if (--sc->ure_refcnt < 0)
1679 		usb_detach_wakeup(&sc->ure_dev);
1680 }
1681 
1682 void
1683 ure_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1684 {
1685 	struct ure_chain	*c = (struct ure_chain *)priv;
1686 	struct ure_softc	*sc = c->uc_sc;
1687 	struct ifnet		*ifp = &sc->ure_ac.ac_if;
1688 	u_char			*buf = c->uc_buf;
1689 	uint32_t		cflags, rxvlan, total_len;
1690 	struct mbuf_list	ml = MBUF_LIST_INITIALIZER();
1691 	struct mbuf		*m;
1692 	int			pktlen = 0, s;
1693 	struct ure_rxpkt	rxhdr;
1694 
1695 	if (usbd_is_dying(sc->ure_udev))
1696 		return;
1697 
1698 	if (!(ifp->if_flags & IFF_RUNNING))
1699 		return;
1700 
1701 	if (status != USBD_NORMAL_COMPLETION) {
1702 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1703 			return;
1704 		if (usbd_ratecheck(&sc->ure_rx_notice)) {
1705 			printf("%s: usb errors on rx: %s\n",
1706 				sc->ure_dev.dv_xname, usbd_errstr(status));
1707 		}
1708 		if (status == USBD_STALLED)
1709 			usbd_clear_endpoint_stall_async(
1710 			    sc->ure_ep[URE_ENDPT_RX]);
1711 		goto done;
1712 	}
1713 
1714 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1715 	DPRINTFN(3, ("received %d bytes\n", total_len));
1716 
1717 	do {
1718 		if (total_len < sizeof(rxhdr)) {
1719 			DPRINTF(("too few bytes left for a packet header\n"));
1720 			ifp->if_ierrors++;
1721 			goto done;
1722 		}
1723 
1724 		buf += roundup(pktlen, 8);
1725 
1726 		memcpy(&rxhdr, buf, sizeof(rxhdr));
1727 		total_len -= sizeof(rxhdr);
1728 
1729 		pktlen = letoh32(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK;
1730 		DPRINTFN(4, ("next packet is %d bytes\n", pktlen));
1731 		if (pktlen > total_len) {
1732 			DPRINTF(("not enough bytes left for next packet\n"));
1733 			ifp->if_ierrors++;
1734 			goto done;
1735 		}
1736 
1737 		total_len -= roundup(pktlen, 8);
1738 		buf += sizeof(rxhdr);
1739 
1740 		m = m_devget(buf, pktlen, ETHER_ALIGN);
1741 		if (m == NULL) {
1742 			DPRINTF(("unable to allocate mbuf for next packet\n"));
1743 			ifp->if_ierrors++;
1744 			goto done;
1745 		}
1746 
1747 		cflags = letoh32(rxhdr.ure_csum);
1748 		rxvlan = letoh32(rxhdr.ure_vlan);
1749 
1750 		/* Check IP header checksum. */
1751 		if ((rxvlan & URE_RXPKT_IPV4) &&
1752 		    !(cflags & URE_RXPKT_IPSUMBAD))
1753 			m->m_pkthdr.csum_flags |= M_IPV4_CSUM_IN_OK;
1754 
1755 		/* Check TCP/UDP checksum. */
1756 		if ((rxvlan & (URE_RXPKT_IPV4 | URE_RXPKT_IPV6)) &&
1757 		    (((rxvlan & URE_RXPKT_TCP) &&
1758 		    !(cflags & URE_RXPKT_TCPSUMBAD)) ||
1759 		    ((rxvlan & URE_RXPKT_UDP) &&
1760 		    !(cflags & URE_RXPKT_UDPSUMBAD))))
1761 			 m->m_pkthdr.csum_flags |= M_TCP_CSUM_IN_OK |
1762 			     M_UDP_CSUM_IN_OK;
1763 #if NVLAN > 0
1764 		if (rxvlan & URE_RXPKT_VLAN_TAG) {
1765 			m->m_pkthdr.ether_vtag =
1766 			    swap16(rxvlan & URE_RXPKT_VLAN_DATA);
1767 			 m->m_flags |= M_VLANTAG;
1768 		}
1769 #endif
1770 
1771 		ml_enqueue(&ml, m);
1772 	} while (total_len > 0);
1773 
1774 done:
1775 	s = splnet();
1776 	if_input(ifp, &ml);
1777 	splx(s);
1778 	memset(c->uc_buf, 0, sc->ure_rxbufsz);
1779 
1780 	usbd_setup_xfer(xfer, sc->ure_ep[URE_ENDPT_RX], c, c->uc_buf,
1781 	    sc->ure_rxbufsz, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1782 	    USBD_NO_TIMEOUT, ure_rxeof);
1783 	usbd_transfer(xfer);
1784 }
1785 
1786 
1787 void
1788 ure_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1789 {
1790 	struct ure_softc	*sc;
1791 	struct ure_chain	*c;
1792 	struct ifnet		*ifp;
1793 	int			s;
1794 
1795 	c = priv;
1796 	sc = c->uc_sc;
1797 	ifp = &sc->ure_ac.ac_if;
1798 
1799 	if (usbd_is_dying(sc->ure_udev))
1800 		return;
1801 
1802 	DPRINTFN(2, ("tx completion\n"));
1803 
1804 	s = splnet();
1805 	sc->ure_cdata.tx_cnt--;
1806 	if (status != USBD_NORMAL_COMPLETION) {
1807 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1808 			splx(s);
1809 			return;
1810 		}
1811 		ifp->if_oerrors++;
1812 		printf("%s: usb error on tx: %s\n", sc->ure_dev.dv_xname,
1813 		    usbd_errstr(status));
1814 		if (status == USBD_STALLED)
1815 			usbd_clear_endpoint_stall_async(
1816 			    sc->ure_ep[URE_ENDPT_TX]);
1817 		splx(s);
1818 		return;
1819 	}
1820 
1821 	ifp->if_timer = 0;
1822 	ifq_clr_oactive(&ifp->if_snd);
1823 
1824 	m_freem(c->uc_mbuf);
1825 	c->uc_mbuf = NULL;
1826 
1827 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
1828 		ure_start(ifp);
1829 
1830 	splx(s);
1831 }
1832 
1833 int
1834 ure_tx_list_init(struct ure_softc *sc)
1835 {
1836 	struct ure_cdata *cd;
1837 	struct ure_chain *c;
1838 	int i;
1839 
1840 	cd = &sc->ure_cdata;
1841 	for (i = 0; i < sc->ure_tx_list_cnt; i++) {
1842 		c = &cd->tx_chain[i];
1843 		c->uc_sc = sc;
1844 		c->uc_idx = i;
1845 		c->uc_mbuf = NULL;
1846 		if (c->uc_xfer == NULL) {
1847 			c->uc_xfer = usbd_alloc_xfer(sc->ure_udev);
1848 			if (c->uc_xfer == NULL)
1849 				return ENOBUFS;
1850 			c->uc_buf = usbd_alloc_buffer(c->uc_xfer, URE_TXBUFSZ);
1851 			if (c->uc_buf == NULL) {
1852 				usbd_free_xfer(c->uc_xfer);
1853 				return ENOBUFS;
1854 			}
1855 		}
1856 	}
1857 
1858 	cd->tx_prod = cd->tx_cnt = 0;
1859 
1860 	return (0);
1861 }
1862 
1863 int
1864 ure_rx_list_init(struct ure_softc *sc)
1865 {
1866 	struct ure_cdata *cd;
1867 	struct ure_chain *c;
1868 	int i;
1869 
1870 	cd = &sc->ure_cdata;
1871 	for (i = 0; i < URE_RX_LIST_CNT; i++) {
1872 		c = &cd->rx_chain[i];
1873 		c->uc_sc = sc;
1874 		c->uc_idx = i;
1875 		c->uc_mbuf = NULL;
1876 		if (c->uc_xfer == NULL) {
1877 			c->uc_xfer = usbd_alloc_xfer(sc->ure_udev);
1878 			if (c->uc_xfer == NULL)
1879 				return ENOBUFS;
1880 			c->uc_buf = usbd_alloc_buffer(c->uc_xfer,
1881 			    sc->ure_rxbufsz);
1882 			if (c->uc_buf == NULL) {
1883 				usbd_free_xfer(c->uc_xfer);
1884 				return ENOBUFS;
1885 			}
1886 		}
1887 	}
1888 
1889 	return (0);
1890 }
1891 
1892 int
1893 ure_encap(struct ure_softc *sc, struct mbuf *m)
1894 {
1895 	struct ure_chain	*c;
1896 	usbd_status		err;
1897 	struct ure_txpkt	txhdr;
1898 	uint32_t		frm_len = 0, cflags = 0;
1899 
1900 	if ((m->m_pkthdr.csum_flags &
1901 	    (M_IPV4_CSUM_OUT | M_TCP_CSUM_OUT | M_UDP_CSUM_OUT)) != 0) {
1902 		cflags |= URE_TXPKT_IPV4;
1903 		if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT)
1904 			cflags |= URE_TXPKT_TCP;
1905 		if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT)
1906 			cflags |= URE_TXPKT_UDP;
1907 	}
1908 
1909 #if NVLAN > 0
1910 	if (m->m_flags & M_VLANTAG)
1911 		cflags |= swap16(m->m_pkthdr.ether_vtag | URE_TXPKT_VLAN_TAG);
1912 #endif
1913 
1914 	c = &sc->ure_cdata.tx_chain[sc->ure_cdata.tx_prod];
1915 
1916 	/* header */
1917 	txhdr.ure_pktlen = htole32(m->m_pkthdr.len | URE_TXPKT_TX_FS |
1918 	    URE_TXPKT_TX_LS);
1919 	txhdr.ure_vlan = htole32(cflags);
1920 	memcpy(c->uc_buf, &txhdr, sizeof(txhdr));
1921 	frm_len = sizeof(txhdr);
1922 
1923 	/* packet */
1924 	m_copydata(m, 0, m->m_pkthdr.len, c->uc_buf + frm_len);
1925 	frm_len += m->m_pkthdr.len;
1926 
1927 	c->uc_mbuf = m;
1928 
1929 	DPRINTFN(2, ("tx %d bytes\n", frm_len));
1930 	usbd_setup_xfer(c->uc_xfer, sc->ure_ep[URE_ENDPT_TX], c, c->uc_buf,
1931 	    frm_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 10000, ure_txeof);
1932 
1933 	err = usbd_transfer(c->uc_xfer);
1934 	if (err != 0 && err != USBD_IN_PROGRESS) {
1935 		ure_stop(sc);
1936 		return (EIO);
1937 	}
1938 
1939 	sc->ure_cdata.tx_cnt++;
1940 	sc->ure_cdata.tx_prod = (sc->ure_cdata.tx_prod + 1) %
1941 	    sc->ure_tx_list_cnt;
1942 
1943 	return (0);
1944 }
1945