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