if_ure.c revision 1.1 1 /* $NetBSD: if_ure.c,v 1.1 2019/02/06 11:55:06 rin 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.1 2019/02/06 11:55:06 rin 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, "%sver %04x\n",
1162 sc->ure_chip != 0 ? "" : "unknown", ver);
1163
1164 if (sc->ure_flags & URE_FLAG_8152)
1165 ure_rtl8152_init(sc);
1166 else
1167 ure_rtl8153_init(sc);
1168
1169 if (sc->ure_chip & URE_CHIP_VER_4C00)
1170 ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, eaddr,
1171 sizeof(eaddr));
1172 else
1173 ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, eaddr,
1174 sizeof(eaddr));
1175
1176 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
1177
1178 ifp = GET_IFP(sc);
1179 ifp->if_softc = sc;
1180 strlcpy(ifp->if_xname, device_xname(sc->ure_dev), IFNAMSIZ);
1181 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1182 ifp->if_init = ure_init;
1183 ifp->if_ioctl = ure_ioctl;
1184 ifp->if_start = ure_start;
1185 ifp->if_stop = ure_stop;
1186
1187 /*
1188 * We don't support TSOv4 and v6 for now, that are required to
1189 * be handled in software for some cases.
1190 */
1191 ifp->if_capabilities = IFCAP_CSUM_IPv4_Tx |
1192 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx;
1193 #ifdef INET6
1194 ifp->if_capabilities |= IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx;
1195 #endif
1196 if (sc->ure_chip & ~URE_CHIP_VER_4C00) {
1197 ifp->if_capabilities |= IFCAP_CSUM_IPv4_Rx |
1198 IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
1199 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
1200 }
1201 sc->ure_ec.ec_capabilities = ETHERCAP_VLAN_MTU;
1202 #ifdef notyet
1203 sc->ure_ec.ec_capabilities |= ETHERCAP_JUMBO_MTU;
1204 #endif
1205
1206 IFQ_SET_READY(&ifp->if_snd);
1207
1208 mii = GET_MII(sc);
1209 mii->mii_ifp = ifp;
1210 mii->mii_readreg = ure_miibus_readreg;
1211 mii->mii_writereg = ure_miibus_writereg;
1212 mii->mii_statchg = ure_miibus_statchg;
1213 mii->mii_flags = MIIF_AUTOTSLEEP;
1214
1215 sc->ure_ec.ec_mii = mii;
1216 ifmedia_init(&mii->mii_media, 0, ure_ifmedia_upd, ure_ifmedia_sts);
1217 mii_attach(self, mii, 0xffffffff, sc->ure_phyno, MII_OFFSET_ANY, 0);
1218
1219 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1220 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1221 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1222 } else
1223 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1224
1225 if_attach(ifp);
1226 ether_ifattach(ifp, eaddr);
1227
1228 rnd_attach_source(&sc->ure_rnd_source, device_xname(sc->ure_dev),
1229 RND_TYPE_NET, RND_FLAG_DEFAULT);
1230
1231 callout_init(&sc->ure_stat_ch, 0);
1232
1233 splx(s);
1234
1235 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->ure_udev, sc->ure_dev);
1236 }
1237
1238 static int
1239 ure_detach(device_t self, int flags)
1240 {
1241 struct ure_softc *sc = device_private(self);
1242 struct ifnet *ifp = GET_IFP(sc);
1243 int s;
1244
1245 sc->ure_dying = true;
1246
1247 callout_halt(&sc->ure_stat_ch, NULL);
1248
1249 if (sc->ure_ep[URE_ENDPT_TX] != NULL)
1250 usbd_abort_pipe(sc->ure_ep[URE_ENDPT_TX]);
1251 if (sc->ure_ep[URE_ENDPT_RX] != NULL)
1252 usbd_abort_pipe(sc->ure_ep[URE_ENDPT_RX]);
1253
1254 usb_rem_task_wait(sc->ure_udev, &sc->ure_tick_task, USB_TASKQ_DRIVER,
1255 NULL);
1256
1257 s = splusb();
1258
1259 if (ifp->if_flags & IFF_RUNNING)
1260 ure_stop(ifp, 1);
1261
1262 callout_destroy(&sc->ure_stat_ch);
1263 rnd_detach_source(&sc->ure_rnd_source);
1264 mii_detach(&sc->ure_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1265 ifmedia_delete_instance(&sc->ure_mii.mii_media, IFM_INST_ANY);
1266 if (ifp->if_softc != NULL) {
1267 ether_ifdetach(ifp);
1268 if_detach(ifp);
1269 }
1270
1271 if (--sc->ure_refcnt >= 0) {
1272 /* Wait for processes to go away. */
1273 usb_detach_waitold(sc->ure_dev);
1274 }
1275 splx(s);
1276
1277 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->ure_udev, sc->ure_dev);
1278
1279 mutex_destroy(&sc->ure_mii_lock);
1280
1281 return 0;
1282 }
1283
1284 static int
1285 ure_activate(device_t self, enum devact act)
1286 {
1287 struct ure_softc *sc = device_private(self);
1288 struct ifnet *ifp = GET_IFP(sc);
1289
1290 switch (act) {
1291 case DVACT_DEACTIVATE:
1292 if_deactivate(ifp);
1293 sc->ure_dying = true;
1294 return 0;
1295 default:
1296 return EOPNOTSUPP;
1297 }
1298 return 0;
1299 }
1300
1301 static void
1302 ure_tick_task(void *xsc)
1303 {
1304 struct ure_softc *sc = xsc;
1305 struct ifnet *ifp = GET_IFP(sc);
1306 struct mii_data *mii;
1307 int s;
1308
1309 if (sc == NULL)
1310 return;
1311
1312 if (sc->ure_dying)
1313 return;
1314
1315 mii = GET_MII(sc);
1316
1317 s = splnet();
1318 mii_tick(mii);
1319 if ((sc->ure_flags & URE_FLAG_LINK) == 0)
1320 ure_miibus_statchg(ifp);
1321 callout_reset(&sc->ure_stat_ch, hz, ure_tick, sc);
1322 splx(s);
1323 }
1324
1325 static void
1326 ure_lock_mii(struct ure_softc *sc)
1327 {
1328
1329 sc->ure_refcnt++;
1330 mutex_enter(&sc->ure_mii_lock);
1331 }
1332
1333 static void
1334 ure_unlock_mii(struct ure_softc *sc)
1335 {
1336
1337 mutex_exit(&sc->ure_mii_lock);
1338 if (--sc->ure_refcnt < 0)
1339 usb_detach_wakeupold(sc->ure_dev);
1340 }
1341
1342 static void
1343 ure_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1344 {
1345 struct ure_chain *c = (struct ure_chain *)priv;
1346 struct ure_softc *sc = c->uc_sc;
1347 struct ifnet *ifp = GET_IFP(sc);
1348 uint8_t *buf = c->uc_buf;
1349 uint32_t total_len;
1350 uint16_t pktlen = 0;
1351 struct mbuf *m;
1352 int s;
1353 struct ure_rxpkt rxhdr;
1354
1355 if (sc->ure_dying)
1356 return;
1357
1358 if (!(ifp->if_flags & IFF_RUNNING))
1359 return;
1360
1361 if (status != USBD_NORMAL_COMPLETION) {
1362 if (status == USBD_INVAL)
1363 return; /* XXX plugged out or down */
1364 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1365 return;
1366 if (usbd_ratecheck(&sc->ure_rx_notice))
1367 URE_PRINTF(sc, "usb errors on rx: %s\n",
1368 usbd_errstr(status));
1369 if (status == USBD_STALLED)
1370 usbd_clear_endpoint_stall_async(
1371 sc->ure_ep[URE_ENDPT_RX]);
1372 goto done;
1373 }
1374
1375 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1376 DPRINTFN(3, ("received %d bytes\n", total_len));
1377
1378 KASSERTMSG(total_len <= sc->ure_bufsz, "%u vs %u",
1379 total_len, sc->ure_bufsz);
1380
1381 do {
1382 if (total_len < sizeof(rxhdr)) {
1383 DPRINTF(("too few bytes left for a packet header\n"));
1384 ifp->if_ierrors++;
1385 goto done;
1386 }
1387
1388 buf += roundup(pktlen, 8);
1389
1390 memcpy(&rxhdr, buf, sizeof(rxhdr));
1391 total_len -= sizeof(rxhdr);
1392
1393 pktlen = le32toh(rxhdr.ure_pktlen) & URE_RXPKT_LEN_MASK;
1394 DPRINTFN(4, ("next packet is %d bytes\n", pktlen));
1395 if (pktlen > total_len) {
1396 DPRINTF(("not enough bytes left for next packet\n"));
1397 ifp->if_ierrors++;
1398 goto done;
1399 }
1400
1401 total_len -= roundup(pktlen, 8);
1402 buf += sizeof(rxhdr);
1403
1404 m = m_devget(buf, pktlen - ETHER_CRC_LEN, 0, ifp);
1405 if (m == NULL) {
1406 DPRINTF(("unable to allocate mbuf for next packet\n"));
1407 ifp->if_ierrors++;
1408 goto done;
1409 }
1410
1411 m->m_pkthdr.csum_flags = ure_rxcsum(ifp, &rxhdr);
1412
1413 s = splnet();
1414 if_percpuq_enqueue(ifp->if_percpuq, m);
1415 splx(s);
1416 } while (total_len > 0);
1417
1418 done:
1419 usbd_setup_xfer(xfer, c, c->uc_buf, sc->ure_bufsz,
1420 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ure_rxeof);
1421 usbd_transfer(xfer);
1422 }
1423
1424 static int
1425 ure_rxcsum(struct ifnet *ifp, struct ure_rxpkt *rp)
1426 {
1427 int enabled = ifp->if_csum_flags_rx, flags = 0;
1428 uint32_t csum, misc;
1429
1430 if (enabled == 0)
1431 return 0;
1432
1433 csum = le32toh(rp->ure_csum);
1434 misc = le32toh(rp->ure_misc);
1435
1436 if (csum & URE_RXPKT_IPV4_CS) {
1437 flags |= M_CSUM_IPv4;
1438 if (csum & URE_RXPKT_TCP_CS)
1439 flags |= M_CSUM_TCPv4;
1440 if (csum & URE_RXPKT_UDP_CS)
1441 flags |= M_CSUM_UDPv4;
1442 } else if (csum & URE_RXPKT_IPV6_CS) {
1443 flags = 0;
1444 if (csum & URE_RXPKT_TCP_CS)
1445 flags |= M_CSUM_TCPv6;
1446 if (csum & URE_RXPKT_UDP_CS)
1447 flags |= M_CSUM_UDPv6;
1448 }
1449
1450 flags &= enabled;
1451 if (__predict_false((flags & M_CSUM_IPv4) &&
1452 (misc & URE_RXPKT_IP_F)))
1453 flags |= M_CSUM_IPv4_BAD;
1454 if (__predict_false(
1455 ((flags & (M_CSUM_TCPv4 | M_CSUM_TCPv6)) && (misc & URE_RXPKT_TCP_F))
1456 || ((flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) && (misc & URE_RXPKT_UDP_F))
1457 ))
1458 flags |= M_CSUM_TCP_UDP_BAD;
1459
1460 return flags;
1461 }
1462
1463 static void
1464 ure_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1465 {
1466 struct ure_chain *c = priv;
1467 struct ure_softc *sc = c->uc_sc;
1468 struct ure_cdata *cd = &sc->ure_cdata;
1469 struct ifnet *ifp = GET_IFP(sc);
1470 int s;
1471
1472 if (sc->ure_dying)
1473 return;
1474
1475 DPRINTFN(2, ("tx completion\n"));
1476
1477 s = splnet();
1478
1479 KASSERT(cd->tx_cnt > 0);
1480 cd->tx_cnt--;
1481
1482 if (status != USBD_NORMAL_COMPLETION) {
1483 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1484 splx(s);
1485 return;
1486 }
1487 ifp->if_oerrors++;
1488 if (usbd_ratecheck(&sc->ure_tx_notice))
1489 URE_PRINTF(sc, "usb error on tx: %s\n",
1490 usbd_errstr(status));
1491 if (status == USBD_STALLED)
1492 usbd_clear_endpoint_stall_async(
1493 sc->ure_ep[URE_ENDPT_TX]);
1494 splx(s);
1495 return;
1496 }
1497
1498 ifp->if_flags &= ~IFF_OACTIVE;
1499
1500 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1501 ure_start(ifp);
1502
1503 splx(s);
1504 }
1505
1506 static int
1507 ure_tx_list_init(struct ure_softc *sc)
1508 {
1509 struct ure_cdata *cd;
1510 struct ure_chain *c;
1511 int i, error;
1512
1513 cd = &sc->ure_cdata;
1514 for (i = 0; i < URE_TX_LIST_CNT; i++) {
1515 c = &cd->tx_chain[i];
1516 c->uc_sc = sc;
1517 if (c->uc_xfer == NULL) {
1518 error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_TX],
1519 sc->ure_bufsz, USBD_FORCE_SHORT_XFER, 0,
1520 &c->uc_xfer);
1521 if (error)
1522 return error;
1523 c->uc_buf = usbd_get_buffer(c->uc_xfer);
1524 }
1525 }
1526
1527 cd->tx_prod = cd->tx_cnt = 0;
1528
1529 return 0;
1530 }
1531
1532 static int
1533 ure_rx_list_init(struct ure_softc *sc)
1534 {
1535 struct ure_cdata *cd;
1536 struct ure_chain *c;
1537 int i, error;
1538
1539 cd = &sc->ure_cdata;
1540 for (i = 0; i < URE_RX_LIST_CNT; i++) {
1541 c = &cd->rx_chain[i];
1542 c->uc_sc = sc;
1543 error = usbd_create_xfer(sc->ure_ep[URE_ENDPT_RX],
1544 sc->ure_bufsz, 0, 0, &c->uc_xfer);
1545 if (error)
1546 return error;
1547 c->uc_buf = usbd_get_buffer(c->uc_xfer);
1548 }
1549
1550 return 0;
1551 }
1552
1553 static int
1554 ure_encap(struct ure_softc *sc, struct mbuf *m, int idx)
1555 {
1556 struct ifnet *ifp = GET_IFP(sc);
1557 struct ure_chain *c;
1558 usbd_status err;
1559 struct ure_txpkt txhdr;
1560 uint32_t frm_len = 0;
1561 uint8_t *buf;
1562
1563 c = &sc->ure_cdata.tx_chain[idx];
1564 buf = c->uc_buf;
1565
1566 /* header */
1567 txhdr.ure_pktlen = htole32(m->m_pkthdr.len | URE_TXPKT_TX_FS |
1568 URE_TXPKT_TX_LS);
1569 txhdr.ure_csum = htole32(ure_txcsum(m));
1570 memcpy(buf, &txhdr, sizeof(txhdr));
1571 buf += sizeof(txhdr);
1572 frm_len = sizeof(txhdr);
1573
1574 /* packet */
1575 m_copydata(m, 0, m->m_pkthdr.len, buf);
1576 frm_len += m->m_pkthdr.len;
1577
1578 if (__predict_false(c->uc_xfer == NULL))
1579 return EIO; /* XXX plugged out or down */
1580
1581 DPRINTFN(2, ("tx %d bytes\n", frm_len));
1582 usbd_setup_xfer(c->uc_xfer, c, c->uc_buf, frm_len,
1583 USBD_FORCE_SHORT_XFER, 10000, ure_txeof);
1584
1585 err = usbd_transfer(c->uc_xfer);
1586 if (err != USBD_IN_PROGRESS) {
1587 ure_stop(ifp, 0);
1588 return EIO;
1589 }
1590
1591 return 0;
1592 }
1593
1594 /*
1595 * We need to calculate L4 checksum in software, if the offset of
1596 * L4 header is larger than 0x7ff = 2047.
1597 */
1598 static uint32_t
1599 ure_txcsum(struct mbuf *m)
1600 {
1601 struct ether_header *eh;
1602 int flags = m->m_pkthdr.csum_flags;
1603 uint32_t data = m->m_pkthdr.csum_data;
1604 uint32_t reg = 0;
1605 int l3off, l4off;
1606 uint16_t type;
1607
1608 if (flags == 0)
1609 return 0;
1610
1611 if (__predict_true((unsigned)m->m_len >= sizeof(*eh))) {
1612 eh = mtod(m, struct ether_header *);
1613 type = eh->ether_type;
1614 } else
1615 m_copydata(m, offsetof(struct ether_header, ether_type),
1616 sizeof(type), &type);
1617 switch (type = htons(type)) {
1618 case ETHERTYPE_IP:
1619 case ETHERTYPE_IPV6:
1620 l3off = ETHER_HDR_LEN;
1621 break;
1622 case ETHERTYPE_VLAN:
1623 l3off = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
1624 break;
1625 default:
1626 return 0;
1627 }
1628
1629 if (flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
1630 l4off = l3off + M_CSUM_DATA_IPv4_IPHL(data);
1631 if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
1632 in_undefer_cksum(m, l3off, flags);
1633 return 0;
1634 }
1635 reg |= URE_TXPKT_IPV4_CS;
1636 if (flags & M_CSUM_TCPv4)
1637 reg |= URE_TXPKT_TCP_CS;
1638 else
1639 reg |= URE_TXPKT_UDP_CS;
1640 reg |= l4off << URE_L4_OFFSET_SHIFT;
1641 }
1642 #ifdef INET6
1643 else if (flags & (M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
1644 l4off = l3off + M_CSUM_DATA_IPv6_IPHL(data);
1645 if (__predict_false(l4off > URE_L4_OFFSET_MAX)) {
1646 in6_undefer_cksum(m, l3off, flags);
1647 return 0;
1648 }
1649 reg |= URE_TXPKT_IPV6_CS;
1650 if (flags & M_CSUM_TCPv6)
1651 reg |= URE_TXPKT_TCP_CS;
1652 else
1653 reg |= URE_TXPKT_UDP_CS;
1654 reg |= l4off << URE_L4_OFFSET_SHIFT;
1655 }
1656 #endif
1657 else if (flags & M_CSUM_IPv4)
1658 reg |= URE_TXPKT_IPV4_CS;
1659
1660 return reg;
1661 }
1662