if_ure.c revision 1.5 1 /* $NetBSD: if_ure.c,v 1.5 2019/05/23 10:57:29 msaitoh Exp $ */
2 /* $OpenBSD: if_ure.c,v 1.10 2018/11/02 21:32:30 jcs Exp $ */
3 /*-
4 * Copyright (c) 2015-2016 Kevin Lo <kevlo (at) FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /* RealTek RTL8152/RTL8153 10/100/Gigabit USB Ethernet device */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: if_ure.c,v 1.5 2019/05/23 10:57:29 msaitoh Exp $");
33
34 #ifdef _KERNEL_OPT
35 #include "opt_usb.h"
36 #include "opt_inet.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/device.h>
48
49 #include <sys/rndsource.h>
50
51 #include <net/if.h>
52 #include <net/if_dl.h>
53 #include <net/if_ether.h>
54 #include <net/if_media.h>
55
56 #include <net/bpf.h>
57
58 #include <netinet/in.h>
59
60 #include <netinet/in_offload.h> /* XXX for in_undefer_cksum() */
61 #ifdef INET6
62 #include <netinet6/in6_offload.h> /* XXX for in6_undefer_cksum() */
63 #endif
64
65 #include <dev/mii/mii.h>
66 #include <dev/mii/miivar.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdivar.h>
72 #include <dev/usb/usbdevs.h>
73
74 #include <dev/ic/rtl81x9reg.h> /* XXX for RTK_GMEDIASTAT */
75 #include <dev/usb/if_urereg.h>
76 #include <dev/usb/if_urevar.h>
77
78 #define URE_PRINTF(sc, fmt, args...) \
79 device_printf((sc)->ure_dev, "%s: " fmt, __func__, ##args);
80
81 #define URE_DEBUG
82 #ifdef URE_DEBUG
83 #define DPRINTF(x) do { if (uredebug) printf x; } while (0)
84 #define DPRINTFN(n, x) do { if (uredebug >= (n)) printf x; } while (0)
85 int uredebug = 1;
86 #else
87 #define DPRINTF(x)
88 #define DPRINTFN(n, x)
89 #endif
90
91 static const struct usb_devno ure_devs[] = {
92 { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8152 },
93 { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8153 }
94 };
95
96 static int ure_match(device_t, cfdata_t, void *);
97 static void ure_attach(device_t, device_t, void *);
98 static int ure_detach(device_t, int);
99 static int ure_activate(device_t, enum devact);
100
101 static int ure_ctl(struct ure_softc *, uint8_t, uint16_t, uint16_t,
102 void *, int);
103 static int ure_read_mem(struct ure_softc *, uint16_t, uint16_t, void *,
104 int);
105 static int ure_write_mem(struct ure_softc *, uint16_t, uint16_t, void *,
106 int);
107 static uint8_t ure_read_1(struct ure_softc *, uint16_t, uint16_t);
108 static uint16_t ure_read_2(struct ure_softc *, uint16_t, uint16_t);
109 static uint32_t ure_read_4(struct ure_softc *, uint16_t, uint16_t);
110 static int ure_write_1(struct ure_softc *, uint16_t, uint16_t, uint32_t);
111 static int ure_write_2(struct ure_softc *, uint16_t, uint16_t, uint32_t);
112 static int ure_write_4(struct ure_softc *, uint16_t, uint16_t, uint32_t);
113 static uint16_t ure_ocp_reg_read(struct ure_softc *, uint16_t);
114 static void ure_ocp_reg_write(struct ure_softc *, uint16_t, uint16_t);
115
116 static int ure_init(struct ifnet *);
117 static void ure_stop(struct ifnet *, int);
118 static void ure_start(struct ifnet *);
119 static void ure_reset(struct ure_softc *);
120 static void ure_miibus_statchg(struct ifnet *);
121 static int ure_miibus_readreg(device_t, int, int, uint16_t *);
122 static int ure_miibus_writereg(device_t, int, int, uint16_t);
123 static void ure_lock_mii(struct ure_softc *);
124 static void ure_unlock_mii(struct ure_softc *);
125
126 static int ure_encap(struct ure_softc *, struct mbuf *, int);
127 static uint32_t ure_txcsum(struct mbuf *);
128 static void ure_rxeof(struct usbd_xfer *, void *, usbd_status);
129 static int ure_rxcsum(struct ifnet *, struct ure_rxpkt *);
130 static void ure_txeof(struct usbd_xfer *, void *, usbd_status);
131 static int ure_rx_list_init(struct ure_softc *);
132 static int ure_tx_list_init(struct ure_softc *);
133
134 static void ure_tick_task(void *);
135 static void ure_tick(void *);
136
137 static int ure_ifmedia_upd(struct ifnet *);
138 static void ure_ifmedia_sts(struct ifnet *, struct ifmediareq *);
139 static int ure_ioctl(struct ifnet *, u_long, void *);
140 static void ure_rtl8152_init(struct ure_softc *);
141 static void ure_rtl8153_init(struct ure_softc *);
142 static void ure_disable_teredo(struct ure_softc *);
143 static void ure_init_fifo(struct ure_softc *);
144
145 CFATTACH_DECL_NEW(ure, sizeof(struct ure_softc), ure_match, ure_attach,
146 ure_detach, ure_activate);
147
148 static int
149 ure_ctl(struct ure_softc *sc, uint8_t rw, uint16_t val, uint16_t index,
150 void *buf, int len)
151 {
152 usb_device_request_t req;
153 usbd_status err;
154
155 if (sc->ure_dying)
156 return 0;
157
158 if (rw == URE_CTL_WRITE)
159 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
160 else
161 req.bmRequestType = UT_READ_VENDOR_DEVICE;
162 req.bRequest = UR_SET_ADDRESS;
163 USETW(req.wValue, val);
164 USETW(req.wIndex, index);
165 USETW(req.wLength, len);
166
167 DPRINTFN(5, ("ure_ctl: rw %d, val 0x%04hu, index 0x%04hu, len %d\n",
168 rw, val, index, len));
169 err = usbd_do_request(sc->ure_udev, &req, buf);
170 if (err) {
171 DPRINTF(("ure_ctl: error %d\n", err));
172 return -1;
173 }
174
175 return 0;
176 }
177
178 static int
179 ure_read_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
180 void *buf, int len)
181 {
182
183 return ure_ctl(sc, URE_CTL_READ, addr, index, buf, len);
184 }
185
186 static int
187 ure_write_mem(struct ure_softc *sc, uint16_t addr, uint16_t index,
188 void *buf, int len)
189 {
190
191 return ure_ctl(sc, URE_CTL_WRITE, addr, index, buf, len);
192 }
193
194 static uint8_t
195 ure_read_1(struct ure_softc *sc, uint16_t reg, uint16_t index)
196 {
197 uint32_t val;
198 uint8_t temp[4];
199 uint8_t shift;
200
201 shift = (reg & 3) << 3;
202 reg &= ~3;
203
204 ure_read_mem(sc, reg, index, &temp, 4);
205 val = UGETDW(temp);
206 val >>= shift;
207
208 return val & 0xff;
209 }
210
211 static uint16_t
212 ure_read_2(struct ure_softc *sc, uint16_t reg, uint16_t index)
213 {
214 uint32_t val;
215 uint8_t temp[4];
216 uint8_t shift;
217
218 shift = (reg & 2) << 3;
219 reg &= ~3;
220
221 ure_read_mem(sc, reg, index, &temp, 4);
222 val = UGETDW(temp);
223 val >>= shift;
224
225 return val & 0xffff;
226 }
227
228 static uint32_t
229 ure_read_4(struct ure_softc *sc, uint16_t reg, uint16_t index)
230 {
231 uint8_t temp[4];
232
233 ure_read_mem(sc, reg, index, &temp, 4);
234 return UGETDW(temp);
235 }
236
237 static int
238 ure_write_1(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
239 {
240 uint16_t byen;
241 uint8_t temp[4];
242 uint8_t shift;
243
244 byen = URE_BYTE_EN_BYTE;
245 shift = reg & 3;
246 val &= 0xff;
247
248 if (reg & 3) {
249 byen <<= shift;
250 val <<= (shift << 3);
251 reg &= ~3;
252 }
253
254 USETDW(temp, val);
255 return ure_write_mem(sc, reg, index | byen, &temp, 4);
256 }
257
258 static int
259 ure_write_2(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
260 {
261 uint16_t byen;
262 uint8_t temp[4];
263 uint8_t shift;
264
265 byen = URE_BYTE_EN_WORD;
266 shift = reg & 2;
267 val &= 0xffff;
268
269 if (reg & 2) {
270 byen <<= shift;
271 val <<= (shift << 3);
272 reg &= ~3;
273 }
274
275 USETDW(temp, val);
276 return ure_write_mem(sc, reg, index | byen, &temp, 4);
277 }
278
279 static int
280 ure_write_4(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val)
281 {
282 uint8_t temp[4];
283
284 USETDW(temp, val);
285 return ure_write_mem(sc, reg, index | URE_BYTE_EN_DWORD, &temp, 4);
286 }
287
288 static uint16_t
289 ure_ocp_reg_read(struct ure_softc *sc, uint16_t addr)
290 {
291 uint16_t reg;
292
293 ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
294 reg = (addr & 0x0fff) | 0xb000;
295
296 return ure_read_2(sc, reg, URE_MCU_TYPE_PLA);
297 }
298
299 static void
300 ure_ocp_reg_write(struct ure_softc *sc, uint16_t addr, uint16_t data)
301 {
302 uint16_t reg;
303
304 ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000);
305 reg = (addr & 0x0fff) | 0xb000;
306
307 ure_write_2(sc, reg, URE_MCU_TYPE_PLA, data);
308 }
309
310 static int
311 ure_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
312 {
313 struct ure_softc *sc = device_private(dev);
314
315 if (sc->ure_dying || sc->ure_phyno != phy) /* XXX */
316 return -1;
317
318 /* Let the rgephy driver read the URE_PLA_PHYSTATUS register. */
319 if (reg == RTK_GMEDIASTAT) {
320 *val = ure_read_1(sc, URE_PLA_PHYSTATUS, URE_MCU_TYPE_PLA);
321 return 0;
322 }
323
324 ure_lock_mii(sc);
325 *val = ure_ocp_reg_read(sc, URE_OCP_BASE_MII + reg * 2);
326 ure_unlock_mii(sc);
327
328 return 0;
329 }
330
331 static int
332 ure_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
333 {
334 struct ure_softc *sc = device_private(dev);
335
336 if (sc->ure_dying || sc->ure_phyno != phy) /* XXX */
337 return -1;
338
339 ure_lock_mii(sc);
340 ure_ocp_reg_write(sc, URE_OCP_BASE_MII + reg * 2, val);
341 ure_unlock_mii(sc);
342
343 return 0;
344 }
345
346 static void
347 ure_miibus_statchg(struct ifnet *ifp)
348 {
349 struct ure_softc *sc;
350 struct mii_data *mii;
351
352 if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0)
353 return;
354
355 sc = ifp->if_softc;
356 mii = GET_MII(sc);
357
358 if (mii == NULL)
359 return;
360
361 sc->ure_flags &= ~URE_FLAG_LINK;
362 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
363 (IFM_ACTIVE | IFM_AVALID)) {
364 switch (IFM_SUBTYPE(mii->mii_media_active)) {
365 case IFM_10_T:
366 case IFM_100_TX:
367 sc->ure_flags |= URE_FLAG_LINK;
368 break;
369 case IFM_1000_T:
370 if ((sc->ure_flags & URE_FLAG_8152) != 0)
371 break;
372 sc->ure_flags |= URE_FLAG_LINK;
373 break;
374 default:
375 break;
376 }
377 }
378 }
379
380 static int
381 ure_ifmedia_upd(struct ifnet *ifp)
382 {
383 struct ure_softc *sc = ifp->if_softc;
384 struct mii_data *mii = GET_MII(sc);
385 int err;
386
387 sc->ure_flags &= ~URE_FLAG_LINK;
388 if (mii->mii_instance) {
389 struct mii_softc *miisc;
390 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
391 mii_phy_reset(miisc);
392 }
393
394 err = mii_mediachg(mii);
395 if (err == ENXIO)
396 return 0; /* XXX */
397 else
398 return err;
399 }
400
401 static void
402 ure_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
403 {
404 struct ure_softc *sc = ifp->if_softc;
405 struct mii_data *mii = GET_MII(sc);
406
407 mii_pollstat(mii);
408 ifmr->ifm_active = mii->mii_media_active;
409 ifmr->ifm_status = mii->mii_media_status;
410 }
411
412 static void
413 ure_iff(struct ure_softc *sc)
414 {
415 struct ifnet *ifp = GET_IFP(sc);
416 struct ether_multi *enm;
417 struct ether_multistep step;
418 uint32_t hashes[2] = { 0, 0 };
419 uint32_t hash;
420 uint32_t rxmode;
421
422 if (sc->ure_dying)
423 return;
424
425 rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA);
426 rxmode &= ~URE_RCR_ACPT_ALL;
427 ifp->if_flags &= ~IFF_ALLMULTI;
428
429 /*
430 * Always accept frames destined to our station address.
431 * Always accept broadcast frames.
432 */
433 rxmode |= URE_RCR_APM | URE_RCR_AB;
434
435 if (ifp->if_flags & IFF_PROMISC) {
436 rxmode |= URE_RCR_AAP;
437 allmulti: ifp->if_flags |= IFF_ALLMULTI;
438 rxmode |= URE_RCR_AM;
439 hashes[0] = hashes[1] = 0xffffffff;
440 } else {
441 rxmode |= URE_RCR_AM;
442
443 ETHER_FIRST_MULTI(step, &sc->ure_ec, enm);
444 while (enm != NULL) {
445 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
446 ETHER_ADDR_LEN))
447 goto allmulti;
448
449 hash = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN)
450 >> 26;
451 if (hash < 32)
452 hashes[0] |= (1 << hash);
453 else
454 hashes[1] |= (1 << (hash - 32));
455
456 ETHER_NEXT_MULTI(step, enm);
457 }
458
459 hash = bswap32(hashes[0]);
460 hashes[0] = bswap32(hashes[1]);
461 hashes[1] = hash;
462 }
463
464 ure_write_4(sc, URE_PLA_MAR0, URE_MCU_TYPE_PLA, hashes[0]);
465 ure_write_4(sc, URE_PLA_MAR4, URE_MCU_TYPE_PLA, hashes[1]);
466 ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode);
467 }
468
469 static void
470 ure_reset(struct ure_softc *sc)
471 {
472 int i;
473
474 ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, URE_CR_RST);
475
476 for (i = 0; i < URE_TIMEOUT; i++) {
477 if (!(ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) &
478 URE_CR_RST))
479 break;
480 usbd_delay_ms(sc->ure_udev, 10);
481 }
482 if (i == URE_TIMEOUT)
483 URE_PRINTF(sc, "reset never completed\n");
484 }
485
486 static int
487 ure_init(struct ifnet *ifp)
488 {
489 struct ure_softc *sc = ifp->if_softc;
490 struct ure_chain *c;
491 usbd_status err;
492 int s, i;
493 uint8_t eaddr[8];
494
495 s = splnet();
496
497 /* Cancel pending I/O. */
498 if (ifp->if_flags & IFF_RUNNING)
499 ure_stop(ifp, 1);
500
501 /* Set MAC address. */
502 memset(eaddr, 0, sizeof(eaddr));
503 memcpy(eaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
504 ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_CONFIG);
505 ure_write_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES,
506 eaddr, 8);
507 ure_write_1(sc, URE_PLA_CRWECR, URE_MCU_TYPE_PLA, URE_CRWECR_NORAML);
508
509 /* Reset the packet filter. */
510 ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA,
511 ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) &
512 ~URE_FMC_FCR_MCU_EN);
513 ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA,
514 ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) |
515 URE_FMC_FCR_MCU_EN);
516
517 /* Enable transmit and receive. */
518 ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA,
519 ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) | URE_CR_RE |
520 URE_CR_TE);
521
522 ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA,
523 ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) &
524 ~URE_RXDY_GATED_EN);
525
526 /* Load the multicast filter. */
527 ure_iff(sc);
528
529 /* Open RX and TX pipes. */
530 err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_RX],
531 USBD_EXCLUSIVE_USE, &sc->ure_ep[URE_ENDPT_RX]);
532 if (err) {
533 URE_PRINTF(sc, "open rx pipe failed: %s\n", usbd_errstr(err));
534 splx(s);
535 return EIO;
536 }
537
538 err = usbd_open_pipe(sc->ure_iface, sc->ure_ed[URE_ENDPT_TX],
539 USBD_EXCLUSIVE_USE, &sc->ure_ep[URE_ENDPT_TX]);
540 if (err) {
541 URE_PRINTF(sc, "open tx pipe failed: %s\n", usbd_errstr(err));
542 splx(s);
543 return EIO;
544 }
545
546 if (ure_rx_list_init(sc)) {
547 URE_PRINTF(sc, "rx list init failed\n");
548 splx(s);
549 return ENOBUFS;
550 }
551
552 if (ure_tx_list_init(sc)) {
553 URE_PRINTF(sc, "tx list init failed\n");
554 splx(s);
555 return ENOBUFS;
556 }
557
558 /* Start up the receive pipe. */
559 for (i = 0; i < URE_RX_LIST_CNT; i++) {
560 c = &sc->ure_cdata.rx_chain[i];
561 usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, sc->ure_bufsz,
562 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
563 usbd_transfer(c->uc_xfer);
564 }
565
566 /* Indicate we are up and running. */
567 ifp->if_flags |= IFF_RUNNING;
568 ifp->if_flags &= ~IFF_OACTIVE;
569
570 splx(s);
571
572 callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc);
573
574 return 0;
575 }
576
577 static void
578 ure_start(struct ifnet *ifp)
579 {
580 struct ure_softc *sc = ifp->if_softc;
581 struct mbuf *m;
582 struct ure_cdata *cd = &sc->ure_cdata;
583 int idx;
584
585 if ((sc->ure_flags & URE_FLAG_LINK) == 0 ||
586 (ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) {
587 return;
588 }
589
590 idx = cd->tx_prod;
591 while (cd->tx_cnt < URE_TX_LIST_CNT) {
592 IFQ_POLL(&ifp->if_snd, m);
593 if (m == NULL)
594 break;
595
596 if (ure_encap(sc, m, idx)) {
597 ifp->if_oerrors++;
598 break;
599 }
600 IFQ_DEQUEUE(&ifp->if_snd, m);
601
602 bpf_mtap(ifp, m, BPF_D_OUT);
603 m_freem(m);
604
605 idx = (idx + 1) % URE_TX_LIST_CNT;
606 cd->tx_cnt++;
607 }
608 cd->tx_prod = idx;
609
610 if (cd->tx_cnt >= URE_TX_LIST_CNT)
611 ifp->if_flags |= IFF_OACTIVE;
612 }
613
614 static void
615 ure_tick(void *xsc)
616 {
617 struct ure_softc *sc = xsc;
618
619 if (sc == NULL)
620 return;
621
622 if (sc->ure_dying)
623 return;
624
625 usb_add_task(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER);
626 }
627
628 static void
629 ure_stop(struct ifnet *ifp, int disable __unused)
630 {
631 struct ure_softc *sc = ifp->if_softc;
632 struct ure_chain *c;
633 usbd_status err;
634 int i;
635
636 ure_reset(sc);
637
638 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
639
640 callout_stop(&sc->ure_stat_ch);
641
642 sc->ure_flags &= ~URE_FLAG_LINK; /* XXX */
643
644 if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
645 err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
646 if (err)
647 URE_PRINTF(sc, "abort rx pipe failed: %s\n",
648 usbd_errstr(err));
649 }
650
651 if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
652 err = usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
653 if (err)
654 URE_PRINTF(sc, "abort tx pipe failed: %s\n",
655 usbd_errstr(err));
656 }
657
658 for (i = 0; i < URE_RX_LIST_CNT; i++) {
659 c = &sc->ure_cdata.rx_chain[i];
660 if (c->uc_xfer != NULL) {
661 usbd_destroy_xfer(c->uc_xfer);
662 c->uc_xfer = NULL;
663 }
664 }
665
666 for (i = 0; i < URE_TX_LIST_CNT; i++) {
667 c = &sc->ure_cdata.tx_chain[i];
668 if (c->uc_xfer != NULL) {
669 usbd_destroy_xfer(c->uc_xfer);
670 c->uc_xfer = NULL;
671 }
672 }
673
674 if (sc->ure_ep[URE_ENDPT_RX] != NULL) {
675 err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_RX]);
676 if (err)
677 URE_PRINTF(sc, "close rx pipe failed: %s\n",
678 usbd_errstr(err));
679 sc->ure_ep[URE_ENDPT_RX] = NULL;
680 }
681
682 if (sc->ure_ep[URE_ENDPT_TX] != NULL) {
683 err = usbd_close_pipe(sc->ure_ep[URE_ENDPT_TX]);
684 if (err)
685 URE_PRINTF(sc, "close tx pipe failed: %s\n",
686 usbd_errstr(err));
687 sc->ure_ep[URE_ENDPT_TX] = NULL;
688 }
689 }
690
691 static void
692 ure_rtl8152_init(struct ure_softc *sc)
693 {
694 uint32_t pwrctrl;
695
696 /* Disable ALDPS. */
697 ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
698 URE_DIS_SDSAVE);
699 usbd_delay_ms(sc->ure_udev, 20);
700
701 if (sc->ure_chip & URE_CHIP_VER_4C00) {
702 ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
703 ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) &
704 ~URE_LED_MODE_MASK);
705 }
706
707 ure_write_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB,
708 ure_read_2(sc, URE_USB_UPS_CTRL, URE_MCU_TYPE_USB) &
709 ~URE_POWER_CUT);
710 ure_write_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB,
711 ure_read_2(sc, URE_USB_PM_CTRL_STATUS, URE_MCU_TYPE_USB) &
712 ~URE_RESUME_INDICATE);
713
714 ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
715 ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) |
716 URE_TX_10M_IDLE_EN | URE_PFM_PWM_SWITCH);
717 pwrctrl = ure_read_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA);
718 pwrctrl &= ~URE_MCU_CLK_RATIO_MASK;
719 pwrctrl |= URE_MCU_CLK_RATIO | URE_D3_CLK_GATED_EN;
720 ure_write_4(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA, pwrctrl);
721 ure_write_2(sc, URE_PLA_GPHY_INTR_IMR, URE_MCU_TYPE_PLA,
722 URE_GPHY_STS_MSK | URE_SPEED_DOWN_MSK | URE_SPDWN_RXDV_MSK |
723 URE_SPDWN_LINKCHG_MSK);
724
725 /* Enable Rx aggregation. */
726 ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
727 ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) &
728 ~URE_RX_AGG_DISABLE);
729
730 /* Disable ALDPS. */
731 ure_ocp_reg_write(sc, URE_OCP_ALDPS_CONFIG, URE_ENPDNPS | URE_LINKENA |
732 URE_DIS_SDSAVE);
733 usbd_delay_ms(sc->ure_udev, 20);
734
735 ure_init_fifo(sc);
736
737 ure_write_1(sc, URE_USB_TX_AGG, URE_MCU_TYPE_USB,
738 URE_TX_AGG_MAX_THRESHOLD);
739 ure_write_4(sc, URE_USB_RX_BUF_TH, URE_MCU_TYPE_USB, URE_RX_THR_HIGH);
740 ure_write_4(sc, URE_USB_TX_DMA, URE_MCU_TYPE_USB,
741 URE_TEST_MODE_DISABLE | URE_TX_SIZE_ADJUST1);
742 }
743
744 static void
745 ure_rtl8153_init(struct ure_softc *sc)
746 {
747 uint16_t val;
748 uint8_t u1u2[8];
749 int i;
750
751 /* Disable ALDPS. */
752 ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
753 ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
754 usbd_delay_ms(sc->ure_udev, 20);
755
756 memset(u1u2, 0x00, sizeof(u1u2));
757 ure_write_mem(sc, URE_USB_TOLERANCE,
758 URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
759
760 for (i = 0; i < URE_TIMEOUT; i++) {
761 if (ure_read_2(sc, URE_PLA_BOOT_CTRL, URE_MCU_TYPE_PLA) &
762 URE_AUTOLOAD_DONE)
763 break;
764 usbd_delay_ms(sc->ure_udev, 10);
765 }
766 if (i == URE_TIMEOUT)
767 URE_PRINTF(sc, "timeout waiting for chip autoload\n");
768
769 for (i = 0; i < URE_TIMEOUT; i++) {
770 val = ure_ocp_reg_read(sc, URE_OCP_PHY_STATUS) &
771 URE_PHY_STAT_MASK;
772 if (val == URE_PHY_STAT_LAN_ON || val == URE_PHY_STAT_PWRDN)
773 break;
774 usbd_delay_ms(sc->ure_udev, 10);
775 }
776 if (i == URE_TIMEOUT)
777 URE_PRINTF(sc, "timeout waiting for phy to stabilize\n");
778
779 ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB,
780 ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB) &
781 ~URE_U2P3_ENABLE);
782
783 if (sc->ure_chip & URE_CHIP_VER_5C10) {
784 val = ure_read_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB);
785 val &= ~URE_PWD_DN_SCALE_MASK;
786 val |= URE_PWD_DN_SCALE(96);
787 ure_write_2(sc, URE_USB_SSPHYLINK2, URE_MCU_TYPE_USB, val);
788
789 ure_write_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB,
790 ure_read_1(sc, URE_USB_USB2PHY, URE_MCU_TYPE_USB) |
791 URE_USB2PHY_L1 | URE_USB2PHY_SUSPEND);
792 } else if (sc->ure_chip & URE_CHIP_VER_5C20) {
793 ure_write_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA,
794 ure_read_1(sc, URE_PLA_DMY_REG0, URE_MCU_TYPE_PLA) &
795 ~URE_ECM_ALDPS);
796 }
797 if (sc->ure_chip & (URE_CHIP_VER_5C20 | URE_CHIP_VER_5C30)) {
798 val = ure_read_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB);
799 if (ure_read_2(sc, URE_USB_BURST_SIZE, URE_MCU_TYPE_USB) ==
800 0)
801 val &= ~URE_DYNAMIC_BURST;
802 else
803 val |= URE_DYNAMIC_BURST;
804 ure_write_1(sc, URE_USB_CSR_DUMMY1, URE_MCU_TYPE_USB, val);
805 }
806
807 ure_write_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB,
808 ure_read_1(sc, URE_USB_CSR_DUMMY2, URE_MCU_TYPE_USB) |
809 URE_EP4_FULL_FC);
810
811 ure_write_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB,
812 ure_read_2(sc, URE_USB_WDT11_CTRL, URE_MCU_TYPE_USB) &
813 ~URE_TIMER11_EN);
814
815 ure_write_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA,
816 ure_read_2(sc, URE_PLA_LED_FEATURE, URE_MCU_TYPE_PLA) &
817 ~URE_LED_MODE_MASK);
818
819 if ((sc->ure_chip & URE_CHIP_VER_5C10) &&
820 sc->ure_udev->ud_speed != USB_SPEED_SUPER)
821 val = URE_LPM_TIMER_500MS;
822 else
823 val = URE_LPM_TIMER_500US;
824 ure_write_1(sc, URE_USB_LPM_CTRL, URE_MCU_TYPE_USB,
825 val | URE_FIFO_EMPTY_1FB | URE_ROK_EXIT_LPM);
826
827 val = ure_read_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB);
828 val &= ~URE_SEN_VAL_MASK;
829 val |= URE_SEN_VAL_NORMAL | URE_SEL_RXIDLE;
830 ure_write_2(sc, URE_USB_AFE_CTRL2, URE_MCU_TYPE_USB, val);
831
832 ure_write_2(sc, URE_USB_CONNECT_TIMER, URE_MCU_TYPE_USB, 0x0001);
833
834 ure_write_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB,
835 ure_read_2(sc, URE_USB_POWER_CUT, URE_MCU_TYPE_USB) &
836 ~(URE_PWR_EN | URE_PHASE2_EN));
837 ure_write_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB,
838 ure_read_2(sc, URE_USB_MISC_0, URE_MCU_TYPE_USB) &
839 ~URE_PCUT_STATUS);
840
841 memset(u1u2, 0xff, sizeof(u1u2));
842 ure_write_mem(sc, URE_USB_TOLERANCE,
843 URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
844
845 ure_write_2(sc, URE_PLA_MAC_PWR_CTRL, URE_MCU_TYPE_PLA,
846 URE_ALDPS_SPDWN_RATIO);
847 ure_write_2(sc, URE_PLA_MAC_PWR_CTRL2, URE_MCU_TYPE_PLA,
848 URE_EEE_SPDWN_RATIO);
849 ure_write_2(sc, URE_PLA_MAC_PWR_CTRL3, URE_MCU_TYPE_PLA,
850 URE_PKT_AVAIL_SPDWN_EN | URE_SUSPEND_SPDWN_EN |
851 URE_U1U2_SPDWN_EN | URE_L1_SPDWN_EN);
852 ure_write_2(sc, URE_PLA_MAC_PWR_CTRL4, URE_MCU_TYPE_PLA,
853 URE_PWRSAVE_SPDWN_EN | URE_RXDV_SPDWN_EN | URE_TX10MIDLE_EN |
854 URE_TP100_SPDWN_EN | URE_TP500_SPDWN_EN | URE_TP1000_SPDWN_EN |
855 URE_EEE_SPDWN_EN);
856
857 val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB);
858 if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10)))
859 val |= URE_U2P3_ENABLE;
860 else
861 val &= ~URE_U2P3_ENABLE;
862 ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val);
863
864 memset(u1u2, 0x00, sizeof(u1u2));
865 ure_write_mem(sc, URE_USB_TOLERANCE,
866 URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
867
868 /* Disable ALDPS. */
869 ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
870 ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) & ~URE_EN_ALDPS);
871 usbd_delay_ms(sc->ure_udev, 20);
872
873 ure_init_fifo(sc);
874
875 /* Enable Rx aggregation. */
876 ure_write_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB,
877 ure_read_2(sc, URE_USB_USB_CTRL, URE_MCU_TYPE_USB) &
878 ~URE_RX_AGG_DISABLE);
879
880 val = ure_read_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB);
881 if (!(sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10)))
882 val |= URE_U2P3_ENABLE;
883 else
884 val &= ~URE_U2P3_ENABLE;
885 ure_write_2(sc, URE_USB_U2P3_CTRL, URE_MCU_TYPE_USB, val);
886
887 memset(u1u2, 0xff, sizeof(u1u2));
888 ure_write_mem(sc, URE_USB_TOLERANCE,
889 URE_MCU_TYPE_USB | URE_BYTE_EN_SIX_BYTES, u1u2, sizeof(u1u2));
890 }
891
892 static void
893 ure_disable_teredo(struct ure_softc *sc)
894 {
895
896 ure_write_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA,
897 ure_read_4(sc, URE_PLA_TEREDO_CFG, URE_MCU_TYPE_PLA) &
898 ~(URE_TEREDO_SEL | URE_TEREDO_RS_EVENT_MASK | URE_OOB_TEREDO_EN));
899 ure_write_2(sc, URE_PLA_WDT6_CTRL, URE_MCU_TYPE_PLA,
900 URE_WDT6_SET_MODE);
901 ure_write_2(sc, URE_PLA_REALWOW_TIMER, URE_MCU_TYPE_PLA, 0);
902 ure_write_4(sc, URE_PLA_TEREDO_TIMER, URE_MCU_TYPE_PLA, 0);
903 }
904
905 static void
906 ure_init_fifo(struct ure_softc *sc)
907 {
908 uint32_t rx_fifo1, rx_fifo2;
909 int i;
910
911 ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA,
912 ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) |
913 URE_RXDY_GATED_EN);
914
915 ure_disable_teredo(sc);
916
917 ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA,
918 ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA) &
919 ~URE_RCR_ACPT_ALL);
920
921 if (!(sc->ure_flags & URE_FLAG_8152)) {
922 if (sc->ure_chip & (URE_CHIP_VER_5C00 | URE_CHIP_VER_5C10 |
923 URE_CHIP_VER_5C20))
924 ure_ocp_reg_write(sc, URE_OCP_ADC_CFG,
925 URE_CKADSEL_L | URE_ADC_EN | URE_EN_EMI_L);
926 if (sc->ure_chip & URE_CHIP_VER_5C00)
927 ure_ocp_reg_write(sc, URE_OCP_EEE_CFG,
928 ure_ocp_reg_read(sc, URE_OCP_EEE_CFG) &
929 ~URE_CTAP_SHORT_EN);
930 ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
931 ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) |
932 URE_EEE_CLKDIV_EN);
933 ure_ocp_reg_write(sc, URE_OCP_DOWN_SPEED,
934 ure_ocp_reg_read(sc, URE_OCP_DOWN_SPEED) |
935 URE_EN_10M_BGOFF);
936 ure_ocp_reg_write(sc, URE_OCP_POWER_CFG,
937 ure_ocp_reg_read(sc, URE_OCP_POWER_CFG) |
938 URE_EN_10M_PLLOFF);
939 ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_IMPEDANCE);
940 ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0b13);
941 ure_write_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA,
942 ure_read_2(sc, URE_PLA_PHY_PWR, URE_MCU_TYPE_PLA) |
943 URE_PFM_PWM_SWITCH);
944
945 /* Enable LPF corner auto tune. */
946 ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_LPF_CFG);
947 ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0xf70f);
948
949 /* Adjust 10M amplitude. */
950 ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP1);
951 ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x00af);
952 ure_ocp_reg_write(sc, URE_OCP_SRAM_ADDR, URE_SRAM_10M_AMP2);
953 ure_ocp_reg_write(sc, URE_OCP_SRAM_DATA, 0x0208);
954 }
955
956 ure_reset(sc);
957
958 ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, 0);
959
960 ure_write_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA,
961 ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
962 ~URE_NOW_IS_OOB);
963
964 ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
965 ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) &
966 ~URE_MCU_BORW_EN);
967 for (i = 0; i < URE_TIMEOUT; i++) {
968 if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
969 URE_LINK_LIST_READY)
970 break;
971 usbd_delay_ms(sc->ure_udev, 10);
972 }
973 if (i == URE_TIMEOUT)
974 URE_PRINTF(sc, "timeout waiting for OOB control\n");
975 ure_write_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA,
976 ure_read_2(sc, URE_PLA_SFF_STS_7, URE_MCU_TYPE_PLA) |
977 URE_RE_INIT_LL);
978 for (i = 0; i < URE_TIMEOUT; i++) {
979 if (ure_read_1(sc, URE_PLA_OOB_CTRL, URE_MCU_TYPE_PLA) &
980 URE_LINK_LIST_READY)
981 break;
982 usbd_delay_ms(sc->ure_udev, 10);
983 }
984 if (i == URE_TIMEOUT)
985 URE_PRINTF(sc, "timeout waiting for OOB control\n");
986
987 ure_write_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA,
988 ure_read_2(sc, URE_PLA_CPCR, URE_MCU_TYPE_PLA) &
989 ~URE_CPCR_RX_VLAN);
990 ure_write_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA,
991 ure_read_2(sc, URE_PLA_TCR0, URE_MCU_TYPE_PLA) |
992 URE_TCR0_AUTO_FIFO);
993
994 /* Configure Rx FIFO threshold and coalescing. */
995 ure_write_4(sc, URE_PLA_RXFIFO_CTRL0, URE_MCU_TYPE_PLA,
996 URE_RXFIFO_THR1_NORMAL);
997 if (sc->ure_udev->ud_speed == USB_SPEED_FULL) {
998 rx_fifo1 = URE_RXFIFO_THR2_FULL;
999 rx_fifo2 = URE_RXFIFO_THR3_FULL;
1000 } else {
1001 rx_fifo1 = URE_RXFIFO_THR2_HIGH;
1002 rx_fifo2 = URE_RXFIFO_THR3_HIGH;
1003 }
1004 ure_write_4(sc, URE_PLA_RXFIFO_CTRL1, URE_MCU_TYPE_PLA, rx_fifo1);
1005 ure_write_4(sc, URE_PLA_RXFIFO_CTRL2, URE_MCU_TYPE_PLA, rx_fifo2);
1006
1007 /* Configure Tx FIFO threshold. */
1008 ure_write_4(sc, URE_PLA_TXFIFO_CTRL, URE_MCU_TYPE_PLA,
1009 URE_TXFIFO_THR_NORMAL);
1010 }
1011
1012 int
1013 ure_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1014 {
1015 struct ure_softc *sc = ifp->if_softc;
1016 int s, error = 0, oflags = ifp->if_flags;
1017
1018 s = splnet();
1019
1020 switch (cmd) {
1021 case SIOCSIFFLAGS:
1022 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1023 break;
1024 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
1025 case IFF_RUNNING:
1026 ure_stop(ifp, 1);
1027 break;
1028 case IFF_UP:
1029 ure_init(ifp);
1030 break;
1031 case IFF_UP | IFF_RUNNING:
1032 if ((ifp->if_flags ^ oflags) == IFF_PROMISC)
1033 ure_iff(sc);
1034 else
1035 ure_init(ifp);
1036 }
1037 break;
1038 default:
1039 if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
1040 break;
1041 error = 0;
1042 if ((ifp->if_flags & IFF_RUNNING) == 0)
1043 break;
1044 switch (cmd) {
1045 case SIOCADDMULTI:
1046 case SIOCDELMULTI:
1047 ure_iff(sc);
1048 break;
1049 default:
1050 break;
1051 }
1052 }
1053
1054 splx(s);
1055
1056 return error;
1057 }
1058
1059 static int
1060 ure_match(device_t parent, cfdata_t match, void *aux)
1061 {
1062 struct usb_attach_arg *uaa = aux;
1063
1064 return usb_lookup(ure_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ?
1065 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
1066 }
1067
1068 static void
1069 ure_attach(device_t parent, device_t self, void *aux)
1070 {
1071 struct ure_softc *sc = device_private(self);
1072 struct usb_attach_arg *uaa = aux;
1073 struct usbd_device *dev = uaa->uaa_device;
1074 usb_interface_descriptor_t *id;
1075 usb_endpoint_descriptor_t *ed;
1076 struct ifnet *ifp;
1077 struct mii_data *mii;
1078 int error, i, s;
1079 uint16_t ver;
1080 uint8_t eaddr[8]; /* 2byte padded */
1081 char *devinfop;
1082
1083 aprint_naive("\n");
1084 aprint_normal("\n");
1085
1086 sc->ure_dev = self;
1087 sc->ure_udev = dev;
1088
1089 devinfop = usbd_devinfo_alloc(sc->ure_udev, 0);
1090 aprint_normal_dev(self, "%s\n", devinfop);
1091 usbd_devinfo_free(devinfop);
1092
1093 #define URE_CONFIG_NO 1 /* XXX */
1094 error = usbd_set_config_no(dev, URE_CONFIG_NO, 1);
1095 if (error) {
1096 aprint_error_dev(self, "failed to set configuration: %s\n",
1097 usbd_errstr(error));
1098 return; /* XXX */
1099 }
1100
1101 if (uaa->uaa_product == USB_PRODUCT_REALTEK_RTL8152)
1102 sc->ure_flags |= URE_FLAG_8152;
1103
1104 usb_init_task(&sc->ure_tick_task, ure_tick_task, sc, 0);
1105 mutex_init(&sc->ure_mii_lock, MUTEX_DEFAULT, IPL_NONE);
1106
1107 #define URE_IFACE_IDX 0 /* XXX */
1108 error = usbd_device2interface_handle(dev, URE_IFACE_IDX, &sc->ure_iface);
1109 if (error) {
1110 aprint_error_dev(self, "failed to get interface handle: %s\n",
1111 usbd_errstr(error));
1112 return; /* XXX */
1113 }
1114
1115 sc->ure_bufsz = 16 * 1024;
1116
1117 id = usbd_get_interface_descriptor(sc->ure_iface);
1118 for (i = 0; i < id->bNumEndpoints; i++) {
1119 ed = usbd_interface2endpoint_descriptor(sc->ure_iface, i);
1120 if (ed == NULL) {
1121 aprint_error_dev(self, "couldn't get ep %d\n", i);
1122 return; /* XXX */
1123 }
1124 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1125 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1126 sc->ure_ed[URE_ENDPT_RX] = ed->bEndpointAddress;
1127 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1128 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1129 sc->ure_ed[URE_ENDPT_TX] = ed->bEndpointAddress;
1130 }
1131 }
1132
1133 s = splnet();
1134
1135 sc->ure_phyno = 0;
1136
1137 ver = ure_read_2(sc, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK;
1138 switch (ver) {
1139 case 0x4c00:
1140 sc->ure_chip |= URE_CHIP_VER_4C00;
1141 break;
1142 case 0x4c10:
1143 sc->ure_chip |= URE_CHIP_VER_4C10;
1144 break;
1145 case 0x5c00:
1146 sc->ure_chip |= URE_CHIP_VER_5C00;
1147 break;
1148 case 0x5c10:
1149 sc->ure_chip |= URE_CHIP_VER_5C10;
1150 break;
1151 case 0x5c20:
1152 sc->ure_chip |= URE_CHIP_VER_5C20;
1153 break;
1154 case 0x5c30:
1155 sc->ure_chip |= URE_CHIP_VER_5C30;
1156 break;
1157 default:
1158 /* fake addr? or just fail? */
1159 break;
1160 }
1161 aprint_normal_dev(self, "RTL%d %sver %04x\n",
1162 (sc->ure_flags & URE_FLAG_8152) ? 8152 : 8153,
1163 (sc->ure_chip != 0) ? "" : "unknown ",
1164 ver);
1165
1166 if (sc->ure_flags & URE_FLAG_8152)
1167 ure_rtl8152_init(sc);
1168 else
1169 ure_rtl8153_init(sc);
1170
1171 if (sc->ure_chip & URE_CHIP_VER_4C00)
1172 ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr,
1173 sizeof(eaddr));
1174 else
1175 ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr,
1176 sizeof(eaddr));
1177
1178 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
1179
1180 ifp = GET_IFP(sc);
1181 ifp->if_softc = sc;
1182 strlcpy(ifp->if_xname, device_xname(sc->ure_dev), IFNAMSIZ);
1183 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1184 ifp->if_init = ure_init;
1185 ifp->if_ioctl = ure_ioctl;
1186 ifp->if_start = ure_start;
1187 ifp->if_stop = ure_stop;
1188
1189 /*
1190 * We don't support TSOv4 and v6 for now, that are required to
1191 * be handled in software for some cases.
1192 */
1193 ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx |
1194 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx;
1195 #ifdef INET6
1196 ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
1197 #endif
1198 if (sc->ure_chip & ~URE_CHIP_VER_4C00) {
1199 ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx |
1200 IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
1201 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
1202 }
1203 sc->ure_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1204 #ifdef notyet
1205 sc->ure_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
1206 #endif
1207
1208 IFQ_SET_READY(&ifp->if_snd);
1209
1210 mii = GET_MII(sc);
1211 mii->mii_ifp = ifp;
1212 mii->mii_readreg = ure_miibus_readreg;
1213 mii->mii_writereg = ure_miibus_writereg;
1214 mii->mii_statchg = ure_miibus_statchg;
1215 mii->mii_flags = MIIF_AUTOTSLEEP;
1216
1217 sc->ure_ec.ec_mii = mii;
1218 ifmedia_init(&mii->mii_media, 0, ure_ifmedia_upd, ure_ifmedia_sts);
1219 mii_attach(self, mii, 0xffffffff, sc->ure_phyno, MII_OFFSET_ANY, 0);
1220
1221 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1222 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1223 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1224 } else
1225 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1226
1227 if_attach(ifp);
1228 ether_ifattach(ifp, eaddr);
1229
1230 rnd_attach_source(&sc->ure_rnd_source, device_xname(sc->ure_dev),
1231 RND_TYPE_NET, RND_FLAG_DEFAULT);
1232
1233 callout_init(&sc->ure_stat_ch, 0);
1234
1235 splx(s);
1236
1237 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->ure_udev, sc->ure_dev);
1238
1239 if (!pmf_device_register(self, NULL, NULL))
1240 aprint_error_dev(self, "couldn't establish power handler\n");
1241 }
1242
1243 static int
1244 ure_detach(device_t self, int flags)
1245 {
1246 struct ure_softc *sc = device_private(self);
1247 struct ifnet *ifp = GET_IFP(sc);
1248 int s;
1249
1250 pmf_device_deregister(self);
1251
1252 sc->ure_dying = true;
1253
1254 callout_halt(&sc->ure_stat_ch, NULL);
1255
1256 if (sc->ure_ep[URE_ENDPT_TX] != NULL)
1257 usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
1258 if (sc->ure_ep[URE_ENDPT_RX] != NULL)
1259 usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
1260
1261 usb_rem_task_wait(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER,
1262 NULL);
1263
1264 s = splusb();
1265
1266 if (ifp->if_flags & IFF_RUNNING)
1267 ure_stop(ifp, 1);
1268
1269 callout_destroy(&sc->ure_stat_ch);
1270 rnd_detach_source(&sc->ure_rnd_source);
1271 mii_detach(&sc->ure_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1272 ifmedia_delete_instance(&sc->ure_mii.mii_media, IFM_INST_ANY);
1273 if (ifp->if_softc != NULL) {
1274 ether_ifdetach(ifp);
1275 if_detach(ifp);
1276 }
1277
1278 if (--sc->ure_refcnt >= 0) {
1279 /* Wait for processes to go away. */
1280 usb_detach_waitold(sc->ure_dev);
1281 }
1282 splx(s);
1283
1284 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->ure_udev, sc->ure_dev);
1285
1286 mutex_destroy(&sc->ure_mii_lock);
1287
1288 return 0;
1289 }
1290
1291 static int
1292 ure_activate(device_t self, enum devact act)
1293 {
1294 struct ure_softc *sc = device_private(self);
1295 struct ifnet *ifp = GET_IFP(sc);
1296
1297 switch (act) {
1298 case DVACT_DEACTIVATE:
1299 if_deactivate(ifp);
1300 sc->ure_dying = true;
1301 return 0;
1302 default:
1303 return EOPNOTSUPP;
1304 }
1305 return 0;
1306 }
1307
1308 static void
1309 ure_tick_task(void *xsc)
1310 {
1311 struct ure_softc *sc = xsc;
1312 struct ifnet *ifp = GET_IFP(sc);
1313 struct mii_data *mii;
1314 int s;
1315
1316 if (sc == NULL)
1317 return;
1318
1319 if (sc->ure_dying)
1320 return;
1321
1322 mii = GET_MII(sc);
1323
1324 s = splnet();
1325 mii_tick(mii);
1326 if ((sc->ure_flags & URE_FLAG_LINK) == 0)
1327 ure_miibus_statchg(ifp);
1328 callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc);
1329 splx(s);
1330 }
1331
1332 static void
1333 ure_lock_mii(struct ure_softc *sc)
1334 {
1335
1336 sc->ure_refcnt++;
1337 mutex_enter(&sc->ure_mii_lock);
1338 }
1339
1340 static void
1341 ure_unlock_mii(struct ure_softc *sc)
1342 {
1343
1344 mutex_exit(&sc->ure_mii_lock);
1345 if (--sc->ure_refcnt < 0)
1346 usb_detach_wakeupold(sc->ure_dev);
1347 }
1348
1349 static void
1350 ure_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1351 {
1352 struct ure_chain *c = (struct ure_chain *)priv;
1353 struct ure_softc *sc = c->uc_sc;
1354 struct ifnet *ifp = GET_IFP(sc);
1355 uint8_t *buf = c->uc_buf;
1356 uint32_t total_len;
1357 uint16_t pktlen = 0;
1358 struct mbuf *m;
1359 int s;
1360 struct ure_rxpkt rxhdr;
1361
1362 if (sc->ure_dying)
1363 return;
1364
1365 if (!(ifp->if_flags & IFF_RUNNING))
1366 return;
1367
1368 if (status != USBD_NORMAL_COMPLETION) {
1369 if (status == USBD_INVAL)
1370 return; /* XXX plugged out or down */
1371 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1372 return;
1373 if (usbd_ratecheck(&sc->ure_rx_notice))
1374 URE_PRINTF(sc, "usb errors on rx: %s\n",
1375 usbd_errstr(status));
1376 if (status == USBD_STALLED)
1377 usbd_clear_endpoint_stall_async(
1378 sc->ure_ep[URE_ENDPT_RX]);
1379 goto done;
1380 }
1381
1382 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1383 DPRINTFN(3, ("received %d bytes\n", total_len));
1384
1385 KASSERTMSG(total_len <= sc->ure_bufsz, "%u vs %u",
1386 total_len, sc->ure_bufsz);
1387
1388 do {
1389 if (total_len < sizeof(rxhdr)) {
1390 DPRINTF(("too few bytes left for a packet header\n"));
1391 ifp->if_ierrors++;
1392 goto done;
1393 }
1394
1395 buf += roundup(pktlen, 8);
1396
1397 memcpy(&rxhdr, buf, sizeof(rxhdr));
1398 total_len -= sizeof(rxhdr);
1399
1400 pktlen = le32toh(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK;
1401 DPRINTFN(4, ("next packet is %d bytes\n", pktlen));
1402 if (pktlen > total_len) {
1403 DPRINTF(("not enough bytes left for next packet\n"));
1404 ifp->if_ierrors++;
1405 goto done;
1406 }
1407
1408 total_len -= roundup(pktlen, 8);
1409 buf += sizeof(rxhdr);
1410
1411 m = m_devget(buf, pktlen - ETHER_CRC_LEN, 0, ifp);
1412 if (m == NULL) {
1413 DPRINTF(("unable to allocate mbuf for next packet\n"));
1414 ifp->if_ierrors++;
1415 goto done;
1416 }
1417
1418 m->m_pkthdr.csum_flags = ure_rxcsum(ifp, &rxhdr);
1419
1420 s = splnet();
1421 if_percpuq_enqueue(ifp->if_percpuq, m);
1422 splx(s);
1423 } while (total_len > 0);
1424
1425 done:
1426 usbd_setup_xfer(xfer, c, c->uc_buf, sc->ure_bufsz,
1427 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
1428 usbd_transfer(xfer);
1429 }
1430
1431 static int
1432 ure_rxcsum(struct ifnet *ifp, struct ure_rxpkt *rp)
1433 {
1434 int enabled = ifp->if_csum_flags_rx, flags = 0;
1435 uint32_t csum, misc;
1436
1437 if (enabled == 0)
1438 return 0;
1439
1440 csum = le32toh(rp->ure_csum);
1441 misc = le32toh(rp->ure_misc);
1442
1443 if (csum & URE_RXPKT_IPV4_CS) {
1444 flags |= M_CSUM_IPv4;
1445 if (csum & URE_RXPKT_TCP_CS)
1446 flags |= M_CSUM_TCPv4;
1447 if (csum & URE_RXPKT_UDP_CS)
1448 flags |= M_CSUM_UDPv4;
1449 } else if (csum & URE_RXPKT_IPV6_CS) {
1450 flags = 0;
1451 if (csum & URE_RXPKT_TCP_CS)
1452 flags |= M_CSUM_TCPv6;
1453 if (csum & URE_RXPKT_UDP_CS)
1454 flags |= M_CSUM_UDPv6;
1455 }
1456
1457 flags &= enabled;
1458 if (__predict_false((flags & M_CSUM_IPv4) &&
1459 (misc & URE_RXPKT_IP_F)))
1460 flags |= M_CSUM_IPv4_BAD;
1461 if (__predict_false(
1462 ((flags & (M_CSUM_TCPv4 | M_CSUM_TCPv6)) && (misc & URE_RXPKT_TCP_F))
1463 || ((flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) && (misc & URE_RXPKT_UDP_F))
1464 ))
1465 flags |= M_CSUM_TCP_UDP_BAD;
1466
1467 return flags;
1468 }
1469
1470 static void
1471 ure_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1472 {
1473 struct ure_chain *c = priv;
1474 struct ure_softc *sc = c->uc_sc;
1475 struct ure_cdata *cd = &sc->ure_cdata;
1476 struct ifnet *ifp = GET_IFP(sc);
1477 int s;
1478
1479 if (sc->ure_dying)
1480 return;
1481
1482 DPRINTFN(2, ("tx completion\n"));
1483
1484 s = splnet();
1485
1486 KASSERT(cd->tx_cnt > 0);
1487 cd->tx_cnt--;
1488
1489 if (status != USBD_NORMAL_COMPLETION) {
1490 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1491 splx(s);
1492 return;
1493 }
1494 ifp->if_oerrors++;
1495 if (usbd_ratecheck(&sc->ure_tx_notice))
1496 URE_PRINTF(sc, "usb error on tx: %s\n",
1497 usbd_errstr(status));
1498 if (status == USBD_STALLED)
1499 usbd_clear_endpoint_stall_async(
1500 sc->ure_ep[URE_ENDPT_TX]);
1501 splx(s);
1502 return;
1503 }
1504
1505 ifp->if_flags &= ~IFF_OACTIVE;
1506
1507 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1508 ure_start(ifp);
1509
1510 splx(s);
1511 }
1512
1513 static int
1514 ure_tx_list_init(struct ure_softc *sc)
1515 {
1516 struct ure_cdata *cd;
1517 struct ure_chain *c;
1518 int i, error;
1519
1520 cd = &sc->ure_cdata;
1521 for (i = 0; i < URE_TX_LIST_CNT; i++) {
1522 c = &cd->tx_chain[i];
1523 c->uc_sc = sc;
1524 if (c->uc_xfer == NULL) {
1525 error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_TX],
1526 sc->ure_bufsz, USBD_FORCE_SHORT_XFER, 0,
1527 &c->uc_xfer);
1528 if (error)
1529 return error;
1530 c->uc_buf = usbd_get_buffer(c->uc_xfer);
1531 }
1532 }
1533
1534 cd->tx_prod = cd->tx_cnt = 0;
1535
1536 return 0;
1537 }
1538
1539 static int
1540 ure_rx_list_init(struct ure_softc *sc)
1541 {
1542 struct ure_cdata *cd;
1543 struct ure_chain *c;
1544 int i, error;
1545
1546 cd = &sc->ure_cdata;
1547 for (i = 0; i < URE_RX_LIST_CNT; i++) {
1548 c = &cd->rx_chain[i];
1549 c->uc_sc = sc;
1550 error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_RX],
1551 sc->ure_bufsz, 0, 0, &c->uc_xfer);
1552 if (error)
1553 return error;
1554 c->uc_buf = usbd_get_buffer(c->uc_xfer);
1555 }
1556
1557 return 0;
1558 }
1559
1560 static int
1561 ure_encap(struct ure_softc *sc, struct mbuf *m, int idx)
1562 {
1563 struct ifnet *ifp = GET_IFP(sc);
1564 struct ure_chain *c;
1565 usbd_status err;
1566 struct ure_txpkt txhdr;
1567 uint32_t frm_len = 0;
1568 uint8_t *buf;
1569
1570 c = &sc->ure_cdata.tx_chain[idx];
1571 buf = c->uc_buf;
1572
1573 /* header */
1574 txhdr.ure_pktlen = htole32(m->m_pkthdr.len | URE_TXPKT_TX_FS |
1575 URE_TXPKT_TX_LS);
1576 txhdr.ure_csum = htole32(ure_txcsum(m));
1577 memcpy(buf, &txhdr, sizeof(txhdr));
1578 buf += sizeof(txhdr);
1579 frm_len = sizeof(txhdr);
1580
1581 /* packet */
1582 m_copydata(m, 0, m->m_pkthdr.len, buf);
1583 frm_len += m->m_pkthdr.len;
1584
1585 if (__predict_false(c->uc_xfer == NULL))
1586 return EIO; /* XXX plugged out or down */
1587
1588 DPRINTFN(2, ("tx %d bytes\n", frm_len));
1589 usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, frm_len,
1590 USBD_FORCE_SHORT_XFER, 10000, ure_txeof);
1591
1592 err = usbd_transfer(c->uc_xfer);
1593 if (err != USBD_IN_PROGRESS) {
1594 ure_stop(ifp, 0);
1595 return EIO;
1596 }
1597
1598 return 0;
1599 }
1600
1601 /*
1602 * We need to calculate L4 checksum in software, if the offset of
1603 * L4 header is larger than 0x7ff = 2047.
1604 */
1605 static uint32_t
1606 ure_txcsum(struct mbuf *m)
1607 {
1608 struct ether_header *eh;
1609 int flags = m->m_pkthdr.csum_flags;
1610 uint32_t data = m->m_pkthdr.csum_data;
1611 uint32_t reg = 0;
1612 int l3off, l4off;
1613 uint16_t type;
1614
1615 if (flags == 0)
1616 return 0;
1617
1618 if (__predict_true(m->m_len >= (int)sizeof(*eh))) {
1619 eh = mtod(m, struct ether_header *);
1620 type = eh->ether_type;
1621 } else
1622 m_copydata(m, offsetof(struct ether_header, ether_type),
1623 sizeof(type), &type);
1624 switch (type = htons(type)) {
1625 case ETHERTYPE_IP:
1626 case ETHERTYPE_IPV6:
1627 l3off = ETHER_HDR_LEN;
1628 break;
1629 case ETHERTYPE_VLAN:
1630 l3off = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1631 break;
1632 default:
1633 return 0;
1634 }
1635
1636 if (flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1637 l4off = l3off + M_CSUM_DATA_IPv4_IPHL(data);
1638 if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
1639 in_undefer_cksum(m, l3off, flags);
1640 return 0;
1641 }
1642 reg |= URE_TXPKT_IPV4_CS;
1643 if (flags & M_CSUM_TCPv4)
1644 reg |= URE_TXPKT_TCP_CS;
1645 else
1646 reg |= URE_TXPKT_UDP_CS;
1647 reg |= l4off << URE_L4_OFFSET_SHIFT;
1648 }
1649 #ifdef INET6
1650 else if (flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
1651 l4off = l3off + M_CSUM_DATA_IPv6_IPHL(data);
1652 if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
1653 in6_undefer_cksum(m, l3off, flags);
1654 return 0;
1655 }
1656 reg |= URE_TXPKT_IPV6_CS;
1657 if (flags & M_CSUM_TCPv6)
1658 reg |= URE_TXPKT_TCP_CS;
1659 else
1660 reg |= URE_TXPKT_UDP_CS;
1661 reg |= l4off << URE_L4_OFFSET_SHIFT;
1662 }
1663 #endif
1664 else if (flags & M_CSUM_IPv4)
1665 reg |= URE_TXPKT_IPV4_CS;
1666
1667 return reg;
1668 }
1669