1 /* $NetBSD: usbnet.c,v 1.120 2024/05/04 12:41:03 mlelstv Exp $ */ 2 3 /* 4 * Copyright (c) 2019 Matthew R. Green 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 ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * 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 /* 30 * Common code shared between USB network drivers. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.120 2024/05/04 12:41:03 mlelstv Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/kernel.h> 38 #include <sys/kmem.h> 39 #include <sys/module.h> 40 #include <sys/atomic.h> 41 42 #include <dev/usb/usbnet.h> 43 #include <dev/usb/usbhist.h> 44 45 struct usbnet_cdata { 46 struct usbnet_chain *uncd_tx_chain; 47 struct usbnet_chain *uncd_rx_chain; 48 49 int uncd_tx_prod; 50 int uncd_tx_cnt; 51 }; 52 53 struct usbnet_private { 54 /* 55 * - unp_miilock protects the MII / media data and tick scheduling. 56 * - unp_rxlock protects the rx path and its data 57 * - unp_txlock protects the tx path and its data 58 * 59 * the lock ordering is: 60 * ifnet lock -> unp_miilock 61 * -> unp_rxlock 62 * -> unp_txlock 63 * -> unp_mcastlock 64 */ 65 kmutex_t unp_miilock; 66 kmutex_t unp_rxlock; 67 kmutex_t unp_txlock; 68 69 kmutex_t unp_mcastlock; 70 bool unp_mcastactive; 71 72 struct usbnet_cdata unp_cdata; 73 74 struct ethercom unp_ec; 75 struct mii_data unp_mii; 76 struct usb_task unp_ticktask; 77 struct callout unp_stat_ch; 78 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; 79 80 volatile bool unp_dying; 81 bool unp_stopped; 82 bool unp_rxstopped; 83 bool unp_txstopped; 84 bool unp_attached; 85 bool unp_ifp_attached; 86 bool unp_link; 87 88 int unp_timer; 89 unsigned short unp_if_flags; 90 unsigned unp_number; 91 92 krndsource_t unp_rndsrc; 93 94 struct timeval unp_rx_notice; 95 struct timeval unp_tx_notice; 96 struct timeval unp_intr_notice; 97 }; 98 99 #define un_cdata(un) (&(un)->un_pri->unp_cdata) 100 101 volatile unsigned usbnet_number; 102 103 static void usbnet_isowned_rx(struct usbnet *); 104 static void usbnet_isowned_tx(struct usbnet *); 105 106 static inline void 107 usbnet_isowned_mii(struct usbnet *un) 108 { 109 KASSERT(mutex_owned(&un->un_pri->unp_miilock)); 110 } 111 112 static int usbnet_modcmd(modcmd_t, void *); 113 114 #ifdef USB_DEBUG 115 #ifndef USBNET_DEBUG 116 #define usbnetdebug 0 117 #else 118 static int usbnetdebug = 0; 119 120 SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") 121 { 122 int err; 123 const struct sysctlnode *rnode; 124 const struct sysctlnode *cnode; 125 126 err = sysctl_createv(clog, 0, NULL, &rnode, 127 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", 128 SYSCTL_DESCR("usbnet global controls"), 129 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 130 131 if (err) 132 goto fail; 133 134 /* control debugging printfs */ 135 err = sysctl_createv(clog, 0, &rnode, &cnode, 136 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 137 "debug", SYSCTL_DESCR("Enable debugging output"), 138 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); 139 if (err) 140 goto fail; 141 142 return; 143 fail: 144 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 145 } 146 147 #endif /* USBNET_DEBUG */ 148 #endif /* USB_DEBUG */ 149 150 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) 151 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) 152 #define USBNETHIST_FUNC() USBHIST_FUNC() 153 #define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) 154 #define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ 155 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) 156 #define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ 157 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) 158 159 /* Callback vectors. */ 160 161 static void 162 uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) 163 { 164 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 165 if (un->un_ops->uno_stop) 166 (*un->un_ops->uno_stop)(ifp, disable); 167 } 168 169 static int 170 uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 171 { 172 173 KASSERTMSG(cmd != SIOCADDMULTI, "%s", ifp->if_xname); 174 KASSERTMSG(cmd != SIOCDELMULTI, "%s", ifp->if_xname); 175 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 176 177 if (un->un_ops->uno_ioctl) 178 return (*un->un_ops->uno_ioctl)(ifp, cmd, data); 179 return 0; 180 } 181 182 static int 183 uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 184 { 185 186 switch (cmd) { 187 case SIOCADDMULTI: 188 case SIOCDELMULTI: 189 break; 190 default: 191 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 192 } 193 194 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); 195 } 196 197 static int 198 uno_init(struct usbnet *un, struct ifnet *ifp) 199 { 200 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 201 return un->un_ops->uno_init ? (*un->un_ops->uno_init)(ifp) : 0; 202 } 203 204 static int 205 uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) 206 { 207 usbnet_isowned_mii(un); 208 return (*un->un_ops->uno_read_reg)(un, phy, reg, val); 209 } 210 211 static int 212 uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) 213 { 214 usbnet_isowned_mii(un); 215 return (*un->un_ops->uno_write_reg)(un, phy, reg, val); 216 } 217 218 static void 219 uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) 220 { 221 usbnet_isowned_mii(un); 222 (*un->un_ops->uno_statchg)(ifp); 223 } 224 225 static unsigned 226 uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 227 { 228 usbnet_isowned_tx(un); 229 return (*un->un_ops->uno_tx_prepare)(un, m, c); 230 } 231 232 static void 233 uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 234 { 235 usbnet_isowned_rx(un); 236 (*un->un_ops->uno_rx_loop)(un, c, total_len); 237 } 238 239 static void 240 uno_tick(struct usbnet *un) 241 { 242 if (un->un_ops->uno_tick) 243 (*un->un_ops->uno_tick)(un); 244 } 245 246 static void 247 uno_intr(struct usbnet *un, usbd_status status) 248 { 249 if (un->un_ops->uno_intr) 250 (*un->un_ops->uno_intr)(un, status); 251 } 252 253 /* Interrupt handling. */ 254 255 static struct mbuf * 256 usbnet_newbuf(size_t buflen) 257 { 258 struct mbuf *m; 259 260 if (buflen > MCLBYTES - ETHER_ALIGN) 261 return NULL; 262 263 MGETHDR(m, M_DONTWAIT, MT_DATA); 264 if (m == NULL) 265 return NULL; 266 267 if (buflen > MHLEN - ETHER_ALIGN) { 268 MCLGET(m, M_DONTWAIT); 269 if (!(m->m_flags & M_EXT)) { 270 m_freem(m); 271 return NULL; 272 } 273 } 274 275 m->m_len = m->m_pkthdr.len = ETHER_ALIGN + buflen; 276 m_adj(m, ETHER_ALIGN); 277 278 return m; 279 } 280 281 /* 282 * usbnet_rxeof() is designed to be the done callback for rx completion. 283 * it provides generic setup and finalisation, calls a different usbnet 284 * rx_loop callback in the middle, which can use usbnet_enqueue() to 285 * enqueue a packet for higher levels (or usbnet_input() if previously 286 * using if_input() path.) 287 */ 288 void 289 usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, 290 int csum_flags, uint32_t csum_data, int mbuf_flags) 291 { 292 USBNETHIST_FUNC(); 293 struct ifnet * const ifp = usbnet_ifp(un); 294 struct usbnet_private * const unp __unused = un->un_pri; 295 struct mbuf *m; 296 297 USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx", 298 unp->unp_number, buflen, csum_flags, mbuf_flags); 299 300 usbnet_isowned_rx(un); 301 302 m = usbnet_newbuf(buflen); 303 if (m == NULL) { 304 DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0); 305 if_statinc(ifp, if_ierrors); 306 return; 307 } 308 309 m_set_rcvif(m, ifp); 310 m->m_pkthdr.csum_flags = csum_flags; 311 m->m_pkthdr.csum_data = csum_data; 312 m->m_flags |= mbuf_flags; 313 memcpy(mtod(m, uint8_t *), buf, buflen); 314 315 /* push the packet up */ 316 if_percpuq_enqueue(ifp->if_percpuq, m); 317 } 318 319 void 320 usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) 321 { 322 USBNETHIST_FUNC(); 323 struct ifnet * const ifp = usbnet_ifp(un); 324 struct usbnet_private * const unp __unused = un->un_pri; 325 struct mbuf *m; 326 327 USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju", 328 unp->unp_number, (uintptr_t)buf, buflen, 0); 329 330 usbnet_isowned_rx(un); 331 332 m = usbnet_newbuf(buflen); 333 if (m == NULL) { 334 if_statinc(ifp, if_ierrors); 335 return; 336 } 337 338 m_set_rcvif(m, ifp); 339 memcpy(mtod(m, char *), buf, buflen); 340 341 /* push the packet up */ 342 if_input(ifp, m); 343 } 344 345 /* 346 * A frame has been uploaded: pass the resulting mbuf chain up to 347 * the higher level protocols. 348 */ 349 static void 350 usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 351 { 352 USBNETHIST_FUNC(); 353 struct usbnet_chain * const c = priv; 354 struct usbnet * const un = c->unc_un; 355 struct usbnet_private * const unp = un->un_pri; 356 uint32_t total_len; 357 358 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", 359 unp->unp_number, status, (uintptr_t)xfer, 0); 360 361 mutex_enter(&unp->unp_rxlock); 362 363 if (usbnet_isdying(un) || unp->unp_rxstopped || 364 status == USBD_INVAL || status == USBD_NOT_STARTED || 365 status == USBD_CANCELLED) 366 goto out; 367 368 if (status != USBD_NORMAL_COMPLETION) { 369 if (usbd_ratecheck(&unp->unp_rx_notice)) 370 device_printf(un->un_dev, "usb errors on rx: %s\n", 371 usbd_errstr(status)); 372 if (status == USBD_STALLED) 373 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); 374 goto done; 375 } 376 377 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 378 379 if (total_len > un->un_rx_bufsz) { 380 device_printf(un->un_dev, 381 "rxeof: too large transfer (%u > %u)\n", 382 total_len, un->un_rx_bufsz); 383 goto done; 384 } 385 386 uno_rx_loop(un, c, total_len); 387 usbnet_isowned_rx(un); 388 389 done: 390 if (usbnet_isdying(un) || unp->unp_rxstopped) 391 goto out; 392 393 mutex_exit(&unp->unp_rxlock); 394 395 /* Setup new transfer. */ 396 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, 397 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 398 usbd_transfer(xfer); 399 return; 400 401 out: 402 mutex_exit(&unp->unp_rxlock); 403 } 404 405 static void 406 usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 407 { 408 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 409 struct usbnet_chain * const c = priv; 410 struct usbnet * const un = c->unc_un; 411 struct usbnet_cdata * const cd = un_cdata(un); 412 struct usbnet_private * const unp = un->un_pri; 413 struct ifnet * const ifp = usbnet_ifp(un); 414 415 USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", 416 unp->unp_number, status, (uintptr_t)xfer, 0); 417 418 mutex_enter(&unp->unp_txlock); 419 if (unp->unp_txstopped || usbnet_isdying(un)) { 420 mutex_exit(&unp->unp_txlock); 421 return; 422 } 423 424 KASSERT(cd->uncd_tx_cnt > 0); 425 cd->uncd_tx_cnt--; 426 427 unp->unp_timer = 0; 428 429 switch (status) { 430 case USBD_NOT_STARTED: 431 case USBD_CANCELLED: 432 break; 433 434 case USBD_NORMAL_COMPLETION: 435 if_statinc(ifp, if_opackets); 436 break; 437 438 default: 439 440 if_statinc(ifp, if_oerrors); 441 if (usbd_ratecheck(&unp->unp_tx_notice)) 442 device_printf(un->un_dev, "usb error on tx: %s\n", 443 usbd_errstr(status)); 444 if (status == USBD_STALLED) 445 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); 446 break; 447 } 448 449 mutex_exit(&unp->unp_txlock); 450 451 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) 452 (*ifp->if_start)(ifp); 453 } 454 455 static void 456 usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 457 { 458 USBNETHIST_FUNC(); 459 struct usbnet * const un = priv; 460 struct usbnet_private * const unp = un->un_pri; 461 struct usbnet_intr * const uni __unused = un->un_intr; 462 463 if (usbnet_isdying(un) || 464 status == USBD_INVAL || status == USBD_NOT_STARTED || 465 status == USBD_CANCELLED) { 466 USBNETHIST_CALLARGS("%jd: uni %#jx dying %#jx status %#jx", 467 unp->unp_number, (uintptr_t)uni, 468 usbnet_isdying(un), status); 469 return; 470 } 471 472 if (status != USBD_NORMAL_COMPLETION) { 473 if (usbd_ratecheck(&unp->unp_intr_notice)) { 474 device_printf(un->un_dev, "usb error on intr: %s\n", 475 usbd_errstr(status)); 476 } 477 if (status == USBD_STALLED) 478 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); 479 USBNETHIST_CALLARGS("%jd: not normal status %#jx", 480 unp->unp_number, status, 0, 0); 481 return; 482 } 483 484 uno_intr(un, status); 485 } 486 487 static void 488 usbnet_start_locked(struct ifnet *ifp) 489 { 490 USBNETHIST_FUNC(); 491 struct usbnet * const un = ifp->if_softc; 492 struct usbnet_cdata * const cd = un_cdata(un); 493 struct usbnet_private * const unp = un->un_pri; 494 struct mbuf *m; 495 unsigned length; 496 bool done_transmit = false; 497 int idx, count; 498 499 USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd", 500 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt, 501 unp->unp_link); 502 503 usbnet_isowned_tx(un); 504 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); 505 KASSERT(!unp->unp_txstopped); 506 507 if (!unp->unp_link) { 508 DPRINTF("start called no link (%jx)", 509 unp->unp_link, 0, 0, 0); 510 return; 511 } 512 513 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { 514 DPRINTF("start called, tx busy (%#jx == %#jx)", 515 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); 516 return; 517 } 518 519 idx = cd->uncd_tx_prod; 520 count = 0; 521 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { 522 IFQ_POLL(&ifp->if_snd, m); 523 if (m == NULL) { 524 DPRINTF("start called, queue empty", 0, 0, 0, 0); 525 break; 526 } 527 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); 528 529 struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; 530 531 length = uno_tx_prepare(un, m, c); 532 if (length == 0) { 533 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); 534 if_statinc(ifp, if_oerrors); 535 break; 536 } 537 538 if (__predict_false(c->unc_xfer == NULL)) { 539 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); 540 if_statinc(ifp, if_oerrors); 541 break; 542 } 543 544 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, 545 un->un_tx_xfer_flags, 10000, usbnet_txeof); 546 547 /* Transmit */ 548 usbd_status err = usbd_transfer(c->unc_xfer); 549 if (err != USBD_IN_PROGRESS) { 550 DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd", 551 (uintptr_t)c->unc_buf, length, err, 0); 552 if_statinc(ifp, if_oerrors); 553 break; 554 } 555 done_transmit = true; 556 557 IFQ_DEQUEUE(&ifp->if_snd, m); 558 559 /* 560 * If there's a BPF listener, bounce a copy of this frame 561 * to him. 562 */ 563 bpf_mtap(ifp, m, BPF_D_OUT); 564 m_freem(m); 565 566 idx = (idx + 1) % un->un_tx_list_cnt; 567 cd->uncd_tx_cnt++; 568 count++; 569 } 570 cd->uncd_tx_prod = idx; 571 572 DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd", 573 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0); 574 575 /* 576 * Set a timeout in case the chip goes out to lunch. 577 */ 578 if (done_transmit) 579 unp->unp_timer = 5; 580 581 if (count != 0) 582 rnd_add_uint32(&unp->unp_rndsrc, count); 583 } 584 585 static void 586 usbnet_if_start(struct ifnet *ifp) 587 { 588 struct usbnet * const un = ifp->if_softc; 589 struct usbnet_private * const unp = un->un_pri; 590 591 USBNETHIST_FUNC(); 592 USBNETHIST_CALLARGS("%jd: txstopped %jd", 593 unp->unp_number, unp->unp_txstopped, 0, 0); 594 595 mutex_enter(&unp->unp_txlock); 596 if (!unp->unp_txstopped) 597 usbnet_start_locked(ifp); 598 mutex_exit(&unp->unp_txlock); 599 } 600 601 /* 602 * Chain management. 603 * 604 * RX and TX are identical. Keep them that way. 605 */ 606 607 /* Start of common RX functions */ 608 609 static size_t 610 usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 611 { 612 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; 613 } 614 615 static void 616 usbnet_rx_list_alloc(struct usbnet * const un) 617 { 618 struct usbnet_cdata * const cd = un_cdata(un); 619 620 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); 621 } 622 623 static void 624 usbnet_rx_list_free(struct usbnet * const un) 625 { 626 struct usbnet_cdata * const cd = un_cdata(un); 627 628 if (cd->uncd_rx_chain) { 629 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); 630 cd->uncd_rx_chain = NULL; 631 } 632 } 633 634 static int 635 usbnet_rx_list_init(struct usbnet * const un) 636 { 637 struct usbnet_cdata * const cd = un_cdata(un); 638 struct usbnet_private * const unp = un->un_pri; 639 640 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 641 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 642 643 c->unc_un = un; 644 if (c->unc_xfer == NULL) { 645 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], 646 un->un_rx_bufsz, un->un_rx_xfer_flags, 0, 647 &c->unc_xfer); 648 if (err) 649 return err; 650 c->unc_buf = usbd_get_buffer(c->unc_xfer); 651 } 652 } 653 654 return 0; 655 } 656 657 static void 658 usbnet_rx_list_fini(struct usbnet * const un) 659 { 660 struct usbnet_cdata * const cd = un_cdata(un); 661 662 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 663 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 664 665 if (c->unc_xfer != NULL) { 666 usbd_destroy_xfer(c->unc_xfer); 667 c->unc_xfer = NULL; 668 c->unc_buf = NULL; 669 } 670 } 671 } 672 673 /* End of common RX functions */ 674 675 static void 676 usbnet_rx_start_pipes(struct usbnet * const un) 677 { 678 struct usbnet_cdata * const cd = un_cdata(un); 679 struct usbnet_private * const unp = un->un_pri; 680 681 mutex_enter(&unp->unp_rxlock); 682 KASSERT(unp->unp_rxstopped); 683 unp->unp_rxstopped = false; 684 685 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 686 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 687 688 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, 689 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 690 usbd_transfer(c->unc_xfer); 691 } 692 693 mutex_exit(&unp->unp_rxlock); 694 } 695 696 /* Start of common TX functions */ 697 698 static size_t 699 usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 700 { 701 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; 702 } 703 704 static void 705 usbnet_tx_list_alloc(struct usbnet * const un) 706 { 707 struct usbnet_cdata * const cd = un_cdata(un); 708 709 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); 710 } 711 712 static void 713 usbnet_tx_list_free(struct usbnet * const un) 714 { 715 struct usbnet_cdata * const cd = un_cdata(un); 716 717 if (cd->uncd_tx_chain) { 718 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); 719 cd->uncd_tx_chain = NULL; 720 } 721 } 722 723 static int 724 usbnet_tx_list_init(struct usbnet * const un) 725 { 726 struct usbnet_cdata * const cd = un_cdata(un); 727 struct usbnet_private * const unp = un->un_pri; 728 729 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 730 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 731 732 c->unc_un = un; 733 if (c->unc_xfer == NULL) { 734 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], 735 un->un_tx_bufsz, un->un_tx_xfer_flags, 0, 736 &c->unc_xfer); 737 if (err) 738 return err; 739 c->unc_buf = usbd_get_buffer(c->unc_xfer); 740 } 741 } 742 743 return 0; 744 } 745 746 static void 747 usbnet_tx_list_fini(struct usbnet * const un) 748 { 749 struct usbnet_cdata * const cd = un_cdata(un); 750 751 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 752 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 753 754 if (c->unc_xfer != NULL) { 755 usbd_destroy_xfer(c->unc_xfer); 756 c->unc_xfer = NULL; 757 c->unc_buf = NULL; 758 } 759 } 760 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; 761 } 762 763 /* End of common TX functions */ 764 765 /* Endpoint pipe management. */ 766 767 static void 768 usbnet_ep_close_pipes(struct usbnet * const un) 769 { 770 struct usbnet_private * const unp = un->un_pri; 771 772 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 773 if (unp->unp_ep[i] == NULL) 774 continue; 775 usbd_close_pipe(unp->unp_ep[i]); 776 unp->unp_ep[i] = NULL; 777 } 778 } 779 780 static usbd_status 781 usbnet_ep_open_pipes(struct usbnet * const un) 782 { 783 struct usbnet_intr * const uni = un->un_intr; 784 struct usbnet_private * const unp = un->un_pri; 785 786 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 787 usbd_status err; 788 789 if (un->un_ed[i] == 0) 790 continue; 791 792 if (i == USBNET_ENDPT_INTR && uni) { 793 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], 794 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, 795 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, 796 uni->uni_interval); 797 } else { 798 err = usbd_open_pipe(un->un_iface, un->un_ed[i], 799 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); 800 } 801 if (err) { 802 usbnet_ep_close_pipes(un); 803 return err; 804 } 805 } 806 807 return USBD_NORMAL_COMPLETION; 808 } 809 810 static void 811 usbnet_ep_stop_pipes(struct usbnet * const un) 812 { 813 struct usbnet_private * const unp = un->un_pri; 814 815 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 816 if (unp->unp_ep[i] == NULL) 817 continue; 818 usbd_abort_pipe(unp->unp_ep[i]); 819 } 820 } 821 822 static int 823 usbnet_init_rx_tx(struct usbnet * const un) 824 { 825 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 826 struct usbnet_private * const unp = un->un_pri; 827 struct ifnet * const ifp = usbnet_ifp(un); 828 usbd_status err; 829 int error = 0; 830 831 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 832 833 if (usbnet_isdying(un)) { 834 return EIO; 835 } 836 837 /* Open RX and TX pipes. */ 838 err = usbnet_ep_open_pipes(un); 839 if (err) { 840 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", 841 usbd_errstr(err)); 842 error = EIO; 843 goto out; 844 } 845 846 /* Init RX ring. */ 847 if (usbnet_rx_list_init(un)) { 848 aprint_error_dev(un->un_dev, "rx list init failed\n"); 849 error = ENOBUFS; 850 goto out; 851 } 852 853 /* Init TX ring. */ 854 if (usbnet_tx_list_init(un)) { 855 aprint_error_dev(un->un_dev, "tx list init failed\n"); 856 error = ENOBUFS; 857 goto out; 858 } 859 860 /* Indicate we are up and running. */ 861 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 862 ifp->if_flags |= IFF_RUNNING; 863 864 /* 865 * If the hardware has a multicast filter, program it and then 866 * allow updates to it while we're running. 867 */ 868 if (un->un_ops->uno_mcast) { 869 mutex_enter(&unp->unp_mcastlock); 870 KASSERTMSG(!unp->unp_mcastactive, "%s", ifp->if_xname); 871 unp->unp_if_flags = ifp->if_flags; 872 (*un->un_ops->uno_mcast)(ifp); 873 unp->unp_mcastactive = true; 874 mutex_exit(&unp->unp_mcastlock); 875 } 876 877 /* Allow transmit. */ 878 mutex_enter(&unp->unp_txlock); 879 KASSERT(unp->unp_txstopped); 880 unp->unp_txstopped = false; 881 mutex_exit(&unp->unp_txlock); 882 883 /* Start up the receive pipe(s). */ 884 usbnet_rx_start_pipes(un); 885 886 /* Kick off the watchdog/stats/mii tick. */ 887 mutex_enter(&unp->unp_miilock); 888 unp->unp_stopped = false; 889 callout_schedule(&unp->unp_stat_ch, hz); 890 mutex_exit(&unp->unp_miilock); 891 892 out: 893 if (error) { 894 usbnet_rx_list_fini(un); 895 usbnet_tx_list_fini(un); 896 usbnet_ep_close_pipes(un); 897 } 898 899 /* 900 * For devices without any media autodetection, treat success 901 * here as an active link. 902 */ 903 if (un->un_ops->uno_statchg == NULL) { 904 mutex_enter(&unp->unp_miilock); 905 usbnet_set_link(un, error == 0); 906 mutex_exit(&unp->unp_miilock); 907 } 908 909 return error; 910 } 911 912 /* MII management. */ 913 914 static int 915 usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) 916 { 917 USBNETHIST_FUNC(); 918 struct usbnet * const un = device_private(dev); 919 int err; 920 921 /* MII layer ensures miilock is held. */ 922 usbnet_isowned_mii(un); 923 924 if (usbnet_isdying(un)) { 925 return EIO; 926 } 927 928 err = uno_read_reg(un, phy, reg, val); 929 if (err) { 930 USBNETHIST_CALLARGS("%jd: read PHY failed: %jd", 931 un->un_pri->unp_number, err, 0, 0); 932 return err; 933 } 934 935 return 0; 936 } 937 938 static int 939 usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) 940 { 941 USBNETHIST_FUNC(); 942 struct usbnet * const un = device_private(dev); 943 int err; 944 945 /* MII layer ensures miilock is held. */ 946 usbnet_isowned_mii(un); 947 948 if (usbnet_isdying(un)) { 949 return EIO; 950 } 951 952 err = uno_write_reg(un, phy, reg, val); 953 if (err) { 954 USBNETHIST_CALLARGS("%jd: write PHY failed: %jd", 955 un->un_pri->unp_number, err, 0, 0); 956 return err; 957 } 958 959 return 0; 960 } 961 962 static void 963 usbnet_mii_statchg(struct ifnet *ifp) 964 { 965 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 966 struct usbnet * const un = ifp->if_softc; 967 968 /* MII layer ensures miilock is held. */ 969 usbnet_isowned_mii(un); 970 971 uno_mii_statchg(un, ifp); 972 } 973 974 static int 975 usbnet_media_upd(struct ifnet *ifp) 976 { 977 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 978 struct usbnet * const un = ifp->if_softc; 979 struct usbnet_private * const unp = un->un_pri; 980 struct mii_data * const mii = usbnet_mii(un); 981 982 /* ifmedia layer ensures miilock is held. */ 983 usbnet_isowned_mii(un); 984 985 /* ifmedia changes only with IFNET_LOCK held. */ 986 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 987 988 if (usbnet_isdying(un)) 989 return EIO; 990 991 unp->unp_link = false; 992 993 if (mii->mii_instance) { 994 struct mii_softc *miisc; 995 996 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 997 mii_phy_reset(miisc); 998 } 999 1000 return ether_mediachange(ifp); 1001 } 1002 1003 /* ioctl */ 1004 1005 /* 1006 * usbnet_ifflags_cb(ec) 1007 * 1008 * Called by if_ethersubr when interface flags change 1009 * (SIOCSIFFLAGS), or ethernet capabilities change 1010 * (SIOCSETHERCAP), on a running interface. 1011 */ 1012 static int 1013 usbnet_ifflags_cb(struct ethercom *ec) 1014 { 1015 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1016 struct ifnet *ifp = &ec->ec_if; 1017 struct usbnet *un = ifp->if_softc; 1018 struct usbnet_private * const unp = un->un_pri; 1019 1020 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1021 1022 const u_short changed = ifp->if_flags ^ unp->unp_if_flags; 1023 1024 /* 1025 * If any user-settable flags have changed other than 1026 * IFF_DEBUG, just reset the interface. 1027 */ 1028 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) 1029 return ENETRESET; 1030 1031 /* 1032 * Otherwise, cache the flags change so we can read the flags 1033 * under unp_mcastlock for multicast updates in SIOCADDMULTI or 1034 * SIOCDELMULTI without IFNET_LOCK. 1035 */ 1036 mutex_enter(&unp->unp_mcastlock); 1037 unp->unp_if_flags = ifp->if_flags; 1038 mutex_exit(&unp->unp_mcastlock); 1039 1040 /* 1041 * If we're switching on or off promiscuous mode, reprogram the 1042 * hardware multicast filter now. 1043 * 1044 * XXX Actually, reset the interface, because some usbnet 1045 * drivers (e.g., aue(4)) initialize the hardware differently 1046 * in uno_init depending on IFF_PROMISC. But some (again, 1047 * aue(4)) _also_ need to know whether IFF_PROMISC is set in 1048 * uno_mcast and do something different with it there. Maybe 1049 * the logic can be unified, but it will require an audit and 1050 * testing of all the usbnet drivers. 1051 */ 1052 if (changed & IFF_PROMISC) 1053 return ENETRESET; 1054 1055 return 0; 1056 } 1057 1058 bool 1059 usbnet_ispromisc(struct usbnet *un) 1060 { 1061 struct ifnet * const ifp = usbnet_ifp(un); 1062 struct usbnet_private * const unp = un->un_pri; 1063 1064 KASSERTMSG(mutex_owned(&unp->unp_mcastlock) || IFNET_LOCKED(ifp), 1065 "%s", ifp->if_xname); 1066 1067 return unp->unp_if_flags & IFF_PROMISC; 1068 } 1069 1070 static int 1071 usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1072 { 1073 USBNETHIST_FUNC(); 1074 struct usbnet * const un = ifp->if_softc; 1075 struct usbnet_private * const unp __unused = un->un_pri; 1076 int error; 1077 1078 USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx", 1079 unp->unp_number, cmd, (uintptr_t)data, 0); 1080 1081 if (un->un_ops->uno_override_ioctl) 1082 return uno_override_ioctl(un, ifp, cmd, data); 1083 1084 error = ether_ioctl(ifp, cmd, data); 1085 if (error == ENETRESET) { 1086 switch (cmd) { 1087 case SIOCADDMULTI: 1088 case SIOCDELMULTI: 1089 /* 1090 * If there's a hardware multicast filter, and 1091 * it has been programmed by usbnet_init_rx_tx 1092 * and is active, update it now. Otherwise, 1093 * drop the update on the floor -- it will be 1094 * observed by usbnet_init_rx_tx next time we 1095 * bring the interface up. 1096 */ 1097 if (un->un_ops->uno_mcast) { 1098 mutex_enter(&unp->unp_mcastlock); 1099 if (unp->unp_mcastactive) 1100 (*un->un_ops->uno_mcast)(ifp); 1101 mutex_exit(&unp->unp_mcastlock); 1102 } 1103 error = 0; 1104 break; 1105 default: 1106 error = uno_ioctl(un, ifp, cmd, data); 1107 } 1108 } 1109 1110 return error; 1111 } 1112 1113 /* 1114 * Generic stop network function: 1115 * - mark as stopping 1116 * - call DD routine to stop the device 1117 * - turn off running, timer, statchg callout, link 1118 * - stop transfers 1119 * - free RX and TX resources 1120 * - close pipes 1121 * 1122 * usbnet_if_stop() is for the if_stop handler. 1123 */ 1124 static void 1125 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) 1126 { 1127 struct usbnet_private * const unp = un->un_pri; 1128 struct mii_data * const mii = usbnet_mii(un); 1129 1130 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1131 1132 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1133 KASSERTMSG(ifp->if_flags & IFF_RUNNING, "%s", ifp->if_xname); 1134 1135 /* 1136 * For drivers with hardware multicast filter update callbacks: 1137 * Prevent concurrent access to the hardware registers by 1138 * multicast filter updates, which happens without IFNET_LOCK. 1139 */ 1140 if (un->un_ops->uno_mcast) { 1141 mutex_enter(&unp->unp_mcastlock); 1142 KASSERTMSG(unp->unp_mcastactive, "%p", ifp->if_xname); 1143 unp->unp_mcastactive = false; 1144 unp->unp_if_flags = 0; 1145 mutex_exit(&unp->unp_mcastlock); 1146 } 1147 1148 /* 1149 * Prevent new activity (rescheduling ticks, xfers, &c.) and 1150 * clear the watchdog timer. 1151 */ 1152 mutex_enter(&unp->unp_miilock); 1153 unp->unp_stopped = true; 1154 mutex_exit(&unp->unp_miilock); 1155 1156 mutex_enter(&unp->unp_rxlock); 1157 unp->unp_rxstopped = true; 1158 mutex_exit(&unp->unp_rxlock); 1159 1160 mutex_enter(&unp->unp_txlock); 1161 unp->unp_txstopped = true; 1162 unp->unp_timer = 0; 1163 mutex_exit(&unp->unp_txlock); 1164 1165 /* 1166 * Stop the timer first, then the task -- if the timer was 1167 * already firing, we stop the task or wait for it complete 1168 * only after it last fired. Setting unp_stopped prevents the 1169 * timer task from being scheduled again. 1170 */ 1171 callout_halt(&unp->unp_stat_ch, NULL); 1172 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1173 NULL); 1174 1175 /* 1176 * Now that we have stopped calling mii_tick, bring the MII 1177 * state machine down. 1178 */ 1179 if (mii) { 1180 mutex_enter(&unp->unp_miilock); 1181 mii_down(mii); 1182 mutex_exit(&unp->unp_miilock); 1183 } 1184 1185 /* Stop transfers. */ 1186 usbnet_ep_stop_pipes(un); 1187 1188 /* 1189 * Now that the software is quiescent, ask the driver to stop 1190 * the hardware. The driver's uno_stop routine now has 1191 * exclusive access to any registers that might previously have 1192 * been used by to ifmedia, mii, or ioctl callbacks. 1193 * 1194 * Don't bother if the device is being detached, though -- if 1195 * it's been unplugged then there's no point in trying to touch 1196 * the registers. 1197 */ 1198 if (!usbnet_isdying(un)) 1199 uno_stop(un, ifp, disable); 1200 1201 /* Free RX/TX resources. */ 1202 usbnet_rx_list_fini(un); 1203 usbnet_tx_list_fini(un); 1204 1205 /* Close pipes. */ 1206 usbnet_ep_close_pipes(un); 1207 1208 /* Everything is quesced now. */ 1209 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1210 ifp->if_flags &= ~IFF_RUNNING; 1211 } 1212 1213 static void 1214 usbnet_if_stop(struct ifnet *ifp, int disable) 1215 { 1216 struct usbnet * const un = ifp->if_softc; 1217 1218 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1219 1220 /* 1221 * If we're already stopped, nothing to do. 1222 * 1223 * XXX This should be an assertion, but it may require some 1224 * analysis -- and possibly some tweaking -- of sys/net to 1225 * ensure. 1226 */ 1227 if ((ifp->if_flags & IFF_RUNNING) == 0) 1228 return; 1229 1230 usbnet_stop(un, ifp, disable); 1231 } 1232 1233 /* 1234 * Generic tick task function. 1235 * 1236 * usbnet_tick() is triggered from a callout, and triggers a call to 1237 * usbnet_tick_task() from the usb_task subsystem. 1238 */ 1239 static void 1240 usbnet_tick(void *arg) 1241 { 1242 USBNETHIST_FUNC(); 1243 struct usbnet * const un = arg; 1244 struct usbnet_private * const unp = un->un_pri; 1245 1246 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); 1247 1248 /* Perform periodic stuff in process context */ 1249 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); 1250 } 1251 1252 static void 1253 usbnet_watchdog(struct ifnet *ifp) 1254 { 1255 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1256 struct usbnet * const un = ifp->if_softc; 1257 struct usbnet_private * const unp = un->un_pri; 1258 struct usbnet_cdata * const cd = un_cdata(un); 1259 1260 if_statinc(ifp, if_oerrors); 1261 device_printf(un->un_dev, "watchdog timeout\n"); 1262 1263 if (cd->uncd_tx_cnt > 0) { 1264 DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0); 1265 usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); 1266 if (cd->uncd_tx_cnt != 0) 1267 DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0); 1268 } 1269 1270 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1271 (*ifp->if_start)(ifp); 1272 } 1273 1274 static void 1275 usbnet_tick_task(void *arg) 1276 { 1277 USBNETHIST_FUNC(); 1278 struct usbnet * const un = arg; 1279 struct usbnet_private * const unp = un->un_pri; 1280 struct ifnet * const ifp = usbnet_ifp(un); 1281 struct mii_data * const mii = usbnet_mii(un); 1282 1283 USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0); 1284 1285 mutex_enter(&unp->unp_txlock); 1286 const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0; 1287 mutex_exit(&unp->unp_txlock); 1288 if (timeout) 1289 usbnet_watchdog(ifp); 1290 1291 /* Call driver if requested. */ 1292 uno_tick(un); 1293 1294 mutex_enter(&unp->unp_miilock); 1295 DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); 1296 if (mii) { 1297 mii_tick(mii); 1298 if (!unp->unp_link) 1299 (*mii->mii_statchg)(ifp); 1300 } 1301 1302 if (!unp->unp_stopped && !usbnet_isdying(un)) 1303 callout_schedule(&unp->unp_stat_ch, hz); 1304 mutex_exit(&unp->unp_miilock); 1305 } 1306 1307 static int 1308 usbnet_if_init(struct ifnet *ifp) 1309 { 1310 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1311 struct usbnet * const un = ifp->if_softc; 1312 int error; 1313 1314 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1315 1316 /* 1317 * Prevent anyone from bringing the interface back up once 1318 * we're detaching. 1319 */ 1320 if (usbnet_isdying(un)) 1321 return EIO; 1322 1323 /* 1324 * If we're already running, stop the interface first -- we're 1325 * reinitializing it. 1326 * 1327 * XXX Grody for sys/net to call if_init to reinitialize. This 1328 * should be an assertion, not a branch, but it will require 1329 * some tweaking of sys/net to avoid. See also the comment in 1330 * usbnet_ifflags_cb about if_init vs uno_mcast on reinitialize. 1331 */ 1332 if (ifp->if_flags & IFF_RUNNING) 1333 usbnet_stop(un, ifp, /*disable*/1/*XXX???*/); 1334 KASSERTMSG((ifp->if_flags & IFF_RUNNING) == 0, "%s", ifp->if_xname); 1335 1336 error = uno_init(un, ifp); 1337 if (error) 1338 return error; 1339 error = usbnet_init_rx_tx(un); 1340 if (error) 1341 return error; 1342 1343 return 0; 1344 } 1345 1346 1347 /* Various accessors. */ 1348 1349 void 1350 usbnet_set_link(struct usbnet *un, bool link) 1351 { 1352 usbnet_isowned_mii(un); 1353 un->un_pri->unp_link = link; 1354 } 1355 1356 struct ifnet * 1357 usbnet_ifp(struct usbnet *un) 1358 { 1359 return &un->un_pri->unp_ec.ec_if; 1360 } 1361 1362 struct ethercom * 1363 usbnet_ec(struct usbnet *un) 1364 { 1365 return &un->un_pri->unp_ec; 1366 } 1367 1368 struct mii_data * 1369 usbnet_mii(struct usbnet *un) 1370 { 1371 return un->un_pri->unp_ec.ec_mii; 1372 } 1373 1374 krndsource_t * 1375 usbnet_rndsrc(struct usbnet *un) 1376 { 1377 return &un->un_pri->unp_rndsrc; 1378 } 1379 1380 void * 1381 usbnet_softc(struct usbnet *un) 1382 { 1383 return un->un_sc; 1384 } 1385 1386 bool 1387 usbnet_havelink(struct usbnet *un) 1388 { 1389 return un->un_pri->unp_link; 1390 } 1391 1392 bool 1393 usbnet_isdying(struct usbnet *un) 1394 { 1395 return atomic_load_relaxed(&un->un_pri->unp_dying); 1396 } 1397 1398 1399 /* Locking. */ 1400 1401 static void 1402 usbnet_isowned_rx(struct usbnet *un) 1403 { 1404 KASSERT(mutex_owned(&un->un_pri->unp_rxlock)); 1405 } 1406 1407 static void 1408 usbnet_isowned_tx(struct usbnet *un) 1409 { 1410 KASSERT(mutex_owned(&un->un_pri->unp_txlock)); 1411 } 1412 1413 /* Autoconf management. */ 1414 1415 static bool 1416 usbnet_empty_eaddr(struct usbnet * const un) 1417 { 1418 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && 1419 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && 1420 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); 1421 } 1422 1423 /* 1424 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant 1425 * 'usbnet'. The first is enough to enable device access (eg, endpoints 1426 * are connected and commands can be sent), and the second connects the 1427 * device to the system networking. 1428 * 1429 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skipped. 1430 * Also usable as driver detach directly. 1431 * 1432 * To skip ethernet configuration (eg, point-to-point), make sure that 1433 * the un_eaddr[] is fully zero. 1434 */ 1435 1436 void 1437 usbnet_attach(struct usbnet *un) 1438 { 1439 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1440 1441 /* Required inputs. */ 1442 KASSERT(un->un_ops->uno_tx_prepare); 1443 KASSERT(un->un_ops->uno_rx_loop); 1444 KASSERT(un->un_rx_bufsz); 1445 KASSERT(un->un_tx_bufsz); 1446 KASSERT(un->un_rx_list_cnt); 1447 KASSERT(un->un_tx_list_cnt); 1448 1449 /* Unfortunate fact. */ 1450 KASSERT(un == device_private(un->un_dev)); 1451 1452 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); 1453 struct usbnet_private * const unp = un->un_pri; 1454 1455 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, 1456 USB_TASKQ_MPSAFE); 1457 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); 1458 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); 1459 1460 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1461 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1462 mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE); 1463 mutex_init(&unp->unp_mcastlock, MUTEX_DEFAULT, IPL_SOFTCLOCK); 1464 1465 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), 1466 RND_TYPE_NET, RND_FLAG_DEFAULT); 1467 1468 usbnet_rx_list_alloc(un); 1469 usbnet_tx_list_alloc(un); 1470 1471 unp->unp_number = atomic_inc_uint_nv(&usbnet_number); 1472 1473 unp->unp_stopped = true; 1474 unp->unp_rxstopped = true; 1475 unp->unp_txstopped = true; 1476 unp->unp_attached = true; 1477 } 1478 1479 static void 1480 usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) 1481 { 1482 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1483 struct usbnet_private * const unp = un->un_pri; 1484 struct mii_data * const mii = &unp->unp_mii; 1485 struct ifnet * const ifp = usbnet_ifp(un); 1486 1487 KASSERT(un->un_ops->uno_read_reg); 1488 KASSERT(un->un_ops->uno_write_reg); 1489 KASSERT(un->un_ops->uno_statchg); 1490 1491 mii->mii_ifp = ifp; 1492 mii->mii_readreg = usbnet_mii_readreg; 1493 mii->mii_writereg = usbnet_mii_writereg; 1494 mii->mii_statchg = usbnet_mii_statchg; 1495 mii->mii_flags = MIIF_AUTOTSLEEP; 1496 1497 usbnet_ec(un)->ec_mii = mii; 1498 ifmedia_init_with_lock(&mii->mii_media, 0, 1499 usbnet_media_upd, ether_mediastatus, &unp->unp_miilock); 1500 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, 1501 unm->un_mii_offset, unm->un_mii_flags); 1502 1503 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1504 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1505 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1506 } else 1507 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1508 } 1509 1510 void 1511 usbnet_attach_ifp(struct usbnet *un, 1512 unsigned if_flags, /* additional if_flags */ 1513 unsigned if_extflags, /* additional if_extflags */ 1514 const struct usbnet_mii *unm) /* additional mii_attach flags */ 1515 { 1516 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1517 struct usbnet_private * const unp = un->un_pri; 1518 struct ifnet * const ifp = usbnet_ifp(un); 1519 1520 KASSERT(unp->unp_attached); 1521 KASSERT(!unp->unp_ifp_attached); 1522 1523 ifp->if_softc = un; 1524 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); 1525 ifp->if_flags = if_flags; 1526 ifp->if_extflags = IFEF_MPSAFE | if_extflags; 1527 ifp->if_ioctl = usbnet_if_ioctl; 1528 ifp->if_start = usbnet_if_start; 1529 ifp->if_init = usbnet_if_init; 1530 ifp->if_stop = usbnet_if_stop; 1531 1532 if (unm) 1533 usbnet_attach_mii(un, unm); 1534 else 1535 unp->unp_link = true; 1536 1537 /* Attach the interface. */ 1538 if_initialize(ifp); 1539 if (ifp->_if_input == NULL) 1540 ifp->if_percpuq = if_percpuq_create(ifp); 1541 if_register(ifp); 1542 unp->unp_ifp_attached = true; 1543 1544 /* 1545 * If ethernet address is all zero, skip ether_ifattach() and 1546 * instead attach bpf here.. 1547 */ 1548 if (!usbnet_empty_eaddr(un)) { 1549 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); 1550 aprint_normal_dev(un->un_dev, "Ethernet address %s\n", 1551 ether_sprintf(un->un_eaddr)); 1552 ether_ifattach(ifp, un->un_eaddr); 1553 } else { 1554 if_alloc_sadl(ifp); 1555 bpf_attach(ifp, DLT_RAW, 0); 1556 } 1557 1558 /* Now ready, and attached. */ 1559 IFQ_SET_READY(&ifp->if_snd); 1560 1561 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); 1562 1563 if (!pmf_device_register(un->un_dev, NULL, NULL)) 1564 aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); 1565 } 1566 1567 int 1568 usbnet_detach(device_t self, int flags) 1569 { 1570 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1571 struct usbnet * const un = device_private(self); 1572 struct usbnet_private * const unp = un->un_pri; 1573 1574 /* Detached before attached finished, so just bail out. */ 1575 if (unp == NULL || !unp->unp_attached) 1576 return 0; 1577 1578 struct ifnet * const ifp = usbnet_ifp(un); 1579 struct mii_data * const mii = usbnet_mii(un); 1580 1581 /* 1582 * Prevent new activity. After we stop the interface, it 1583 * cannot be brought back up. 1584 */ 1585 atomic_store_relaxed(&unp->unp_dying, true); 1586 1587 /* 1588 * If we're still running on the network, stop and wait for all 1589 * asynchronous activity to finish. 1590 * 1591 * If usbnet_attach_ifp never ran, IFNET_LOCK won't work, but 1592 * no activity is possible, so just skip this part. 1593 */ 1594 if (unp->unp_ifp_attached) { 1595 IFNET_LOCK(ifp); 1596 if (ifp->if_flags & IFF_RUNNING) { 1597 usbnet_if_stop(ifp, 1); 1598 } 1599 IFNET_UNLOCK(ifp); 1600 } 1601 1602 /* 1603 * The callout and tick task can't be scheduled anew at this 1604 * point, and usbnet_if_stop has waited for them to complete. 1605 */ 1606 KASSERT(!callout_pending(&unp->unp_stat_ch)); 1607 KASSERT(!usb_task_pending(un->un_udev, &unp->unp_ticktask)); 1608 1609 if (mii) { 1610 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); 1611 ifmedia_fini(&mii->mii_media); 1612 } 1613 if (unp->unp_ifp_attached) { 1614 if (!usbnet_empty_eaddr(un)) 1615 ether_ifdetach(ifp); 1616 else 1617 bpf_detach(ifp); 1618 if_detach(ifp); 1619 } 1620 usbnet_ec(un)->ec_mii = NULL; 1621 1622 usbnet_rx_list_free(un); 1623 usbnet_tx_list_free(un); 1624 1625 rnd_detach_source(&unp->unp_rndsrc); 1626 1627 mutex_destroy(&unp->unp_mcastlock); 1628 mutex_destroy(&unp->unp_miilock); 1629 mutex_destroy(&unp->unp_rxlock); 1630 mutex_destroy(&unp->unp_txlock); 1631 1632 callout_destroy(&unp->unp_stat_ch); 1633 1634 pmf_device_deregister(un->un_dev); 1635 1636 /* 1637 * Notify userland that we're going away, if we arrived in the 1638 * first place. 1639 */ 1640 if (unp->unp_ifp_attached) { 1641 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, 1642 un->un_dev); 1643 } 1644 1645 kmem_free(unp, sizeof(*unp)); 1646 un->un_pri = NULL; 1647 1648 return 0; 1649 } 1650 1651 int 1652 usbnet_activate(device_t self, devact_t act) 1653 { 1654 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1655 struct usbnet * const un = device_private(self); 1656 struct usbnet_private * const unp = un->un_pri; 1657 struct ifnet * const ifp = usbnet_ifp(un); 1658 1659 switch (act) { 1660 case DVACT_DEACTIVATE: 1661 if_deactivate(ifp); 1662 1663 atomic_store_relaxed(&unp->unp_dying, true); 1664 1665 mutex_enter(&unp->unp_miilock); 1666 unp->unp_stopped = true; 1667 mutex_exit(&unp->unp_miilock); 1668 1669 mutex_enter(&unp->unp_rxlock); 1670 unp->unp_rxstopped = true; 1671 mutex_exit(&unp->unp_rxlock); 1672 1673 mutex_enter(&unp->unp_txlock); 1674 unp->unp_txstopped = true; 1675 mutex_exit(&unp->unp_txlock); 1676 1677 return 0; 1678 default: 1679 return EOPNOTSUPP; 1680 } 1681 } 1682 1683 MODULE(MODULE_CLASS_MISC, usbnet, NULL); 1684 1685 static int 1686 usbnet_modcmd(modcmd_t cmd, void *arg) 1687 { 1688 switch (cmd) { 1689 case MODULE_CMD_INIT: 1690 return 0; 1691 case MODULE_CMD_FINI: 1692 return 0; 1693 case MODULE_CMD_STAT: 1694 case MODULE_CMD_AUTOUNLOAD: 1695 default: 1696 return ENOTTY; 1697 } 1698 } 1699