1 /* $NetBSD: usbnet.c,v 1.113 2022/09/22 07:02:21 riastradh 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.113 2022/09/22 07:02:21 riastradh 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 aprint_error_dev(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 aprint_error_dev(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 (*un->un_ops->uno_mcast)(ifp); 871 unp->unp_mcastactive = true; 872 mutex_exit(&unp->unp_mcastlock); 873 } 874 875 /* Allow transmit. */ 876 mutex_enter(&unp->unp_txlock); 877 KASSERT(unp->unp_txstopped); 878 unp->unp_txstopped = false; 879 mutex_exit(&unp->unp_txlock); 880 881 /* Start up the receive pipe(s). */ 882 usbnet_rx_start_pipes(un); 883 884 /* Kick off the watchdog/stats/mii tick. */ 885 mutex_enter(&unp->unp_miilock); 886 unp->unp_stopped = false; 887 callout_schedule(&unp->unp_stat_ch, hz); 888 mutex_exit(&unp->unp_miilock); 889 890 out: 891 if (error) { 892 usbnet_rx_list_fini(un); 893 usbnet_tx_list_fini(un); 894 usbnet_ep_close_pipes(un); 895 } 896 897 /* 898 * For devices without any media autodetection, treat success 899 * here as an active link. 900 */ 901 if (un->un_ops->uno_statchg == NULL) { 902 mutex_enter(&unp->unp_miilock); 903 usbnet_set_link(un, error == 0); 904 mutex_exit(&unp->unp_miilock); 905 } 906 907 return error; 908 } 909 910 /* MII management. */ 911 912 static int 913 usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) 914 { 915 USBNETHIST_FUNC(); 916 struct usbnet * const un = device_private(dev); 917 int err; 918 919 /* MII layer ensures miilock is held. */ 920 usbnet_isowned_mii(un); 921 922 if (usbnet_isdying(un)) { 923 return EIO; 924 } 925 926 err = uno_read_reg(un, phy, reg, val); 927 if (err) { 928 USBNETHIST_CALLARGS("%jd: read PHY failed: %jd", 929 un->un_pri->unp_number, err, 0, 0); 930 return err; 931 } 932 933 return 0; 934 } 935 936 static int 937 usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) 938 { 939 USBNETHIST_FUNC(); 940 struct usbnet * const un = device_private(dev); 941 int err; 942 943 /* MII layer ensures miilock is held. */ 944 usbnet_isowned_mii(un); 945 946 if (usbnet_isdying(un)) { 947 return EIO; 948 } 949 950 err = uno_write_reg(un, phy, reg, val); 951 if (err) { 952 USBNETHIST_CALLARGS("%jd: write PHY failed: %jd", 953 un->un_pri->unp_number, err, 0, 0); 954 return err; 955 } 956 957 return 0; 958 } 959 960 static void 961 usbnet_mii_statchg(struct ifnet *ifp) 962 { 963 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 964 struct usbnet * const un = ifp->if_softc; 965 966 /* MII layer ensures miilock is held. */ 967 usbnet_isowned_mii(un); 968 969 uno_mii_statchg(un, ifp); 970 } 971 972 static int 973 usbnet_media_upd(struct ifnet *ifp) 974 { 975 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 976 struct usbnet * const un = ifp->if_softc; 977 struct usbnet_private * const unp = un->un_pri; 978 struct mii_data * const mii = usbnet_mii(un); 979 980 /* ifmedia layer ensures miilock is held. */ 981 usbnet_isowned_mii(un); 982 983 /* ifmedia changes only with IFNET_LOCK held. */ 984 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 985 986 if (usbnet_isdying(un)) 987 return EIO; 988 989 unp->unp_link = false; 990 991 if (mii->mii_instance) { 992 struct mii_softc *miisc; 993 994 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 995 mii_phy_reset(miisc); 996 } 997 998 return ether_mediachange(ifp); 999 } 1000 1001 /* ioctl */ 1002 1003 static int 1004 usbnet_ifflags_cb(struct ethercom *ec) 1005 { 1006 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1007 struct ifnet *ifp = &ec->ec_if; 1008 struct usbnet *un = ifp->if_softc; 1009 struct usbnet_private * const unp = un->un_pri; 1010 int rv = 0; 1011 1012 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1013 1014 const u_short changed = ifp->if_flags ^ unp->unp_if_flags; 1015 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { 1016 mutex_enter(&unp->unp_mcastlock); 1017 unp->unp_if_flags = ifp->if_flags; 1018 mutex_exit(&unp->unp_mcastlock); 1019 /* 1020 * XXX Can we just do uno_mcast synchronously here 1021 * instead of resetting the whole interface? 1022 * 1023 * Not yet, because some usbnet drivers (e.g., aue(4)) 1024 * initialize the hardware differently in uno_init 1025 * depending on IFF_PROMISC. But some (again, aue(4)) 1026 * _also_ need to know whether IFF_PROMISC is set in 1027 * uno_mcast and do something different with it there. 1028 * Maybe the logic can be unified, but it will require 1029 * an audit and testing of all the usbnet drivers. 1030 */ 1031 if (changed & IFF_PROMISC) 1032 rv = ENETRESET; 1033 } else { 1034 rv = ENETRESET; 1035 } 1036 1037 return rv; 1038 } 1039 1040 bool 1041 usbnet_ispromisc(struct usbnet *un) 1042 { 1043 struct ifnet * const ifp = usbnet_ifp(un); 1044 struct usbnet_private * const unp = un->un_pri; 1045 1046 KASSERTMSG(mutex_owned(&unp->unp_mcastlock) || IFNET_LOCKED(ifp), 1047 "%s", ifp->if_xname); 1048 1049 return unp->unp_if_flags & IFF_PROMISC; 1050 } 1051 1052 static int 1053 usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1054 { 1055 USBNETHIST_FUNC(); 1056 struct usbnet * const un = ifp->if_softc; 1057 struct usbnet_private * const unp __unused = un->un_pri; 1058 int error; 1059 1060 USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx", 1061 unp->unp_number, cmd, (uintptr_t)data, 0); 1062 1063 if (un->un_ops->uno_override_ioctl) 1064 return uno_override_ioctl(un, ifp, cmd, data); 1065 1066 error = ether_ioctl(ifp, cmd, data); 1067 if (error == ENETRESET) { 1068 switch (cmd) { 1069 case SIOCADDMULTI: 1070 case SIOCDELMULTI: 1071 /* 1072 * If there's a hardware multicast filter, and 1073 * it has been programmed by usbnet_init_rx_tx 1074 * and is active, update it now. Otherwise, 1075 * drop the update on the floor -- it will be 1076 * observed by usbnet_init_rx_tx next time we 1077 * bring the interface up. 1078 */ 1079 if (un->un_ops->uno_mcast) { 1080 mutex_enter(&unp->unp_mcastlock); 1081 if (unp->unp_mcastactive) 1082 (*un->un_ops->uno_mcast)(ifp); 1083 mutex_exit(&unp->unp_mcastlock); 1084 } 1085 error = 0; 1086 break; 1087 default: 1088 error = uno_ioctl(un, ifp, cmd, data); 1089 } 1090 } 1091 1092 return error; 1093 } 1094 1095 /* 1096 * Generic stop network function: 1097 * - mark as stopping 1098 * - call DD routine to stop the device 1099 * - turn off running, timer, statchg callout, link 1100 * - stop transfers 1101 * - free RX and TX resources 1102 * - close pipes 1103 * 1104 * usbnet_if_stop() is for the if_stop handler. 1105 */ 1106 static void 1107 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) 1108 { 1109 struct usbnet_private * const unp = un->un_pri; 1110 struct mii_data * const mii = usbnet_mii(un); 1111 1112 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1113 1114 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1115 1116 /* 1117 * For drivers with hardware multicast filter update callbacks: 1118 * Prevent concurrent access to the hardware registers by 1119 * multicast filter updates, which happens without IFNET_LOCK. 1120 */ 1121 if (un->un_ops->uno_mcast) { 1122 mutex_enter(&unp->unp_mcastlock); 1123 unp->unp_mcastactive = false; 1124 mutex_exit(&unp->unp_mcastlock); 1125 } 1126 1127 /* 1128 * Prevent new activity (rescheduling ticks, xfers, &c.) and 1129 * clear the watchdog timer. 1130 */ 1131 mutex_enter(&unp->unp_miilock); 1132 unp->unp_stopped = true; 1133 mutex_exit(&unp->unp_miilock); 1134 1135 mutex_enter(&unp->unp_rxlock); 1136 unp->unp_rxstopped = true; 1137 mutex_exit(&unp->unp_rxlock); 1138 1139 mutex_enter(&unp->unp_txlock); 1140 unp->unp_txstopped = true; 1141 unp->unp_timer = 0; 1142 mutex_exit(&unp->unp_txlock); 1143 1144 /* 1145 * Stop the timer first, then the task -- if the timer was 1146 * already firing, we stop the task or wait for it complete 1147 * only after it last fired. Setting unp_stopped prevents the 1148 * timer task from being scheduled again. 1149 */ 1150 callout_halt(&unp->unp_stat_ch, NULL); 1151 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1152 NULL); 1153 1154 /* 1155 * Now that we have stopped calling mii_tick, bring the MII 1156 * state machine down. 1157 */ 1158 if (mii) { 1159 mutex_enter(&unp->unp_miilock); 1160 mii_down(mii); 1161 mutex_exit(&unp->unp_miilock); 1162 } 1163 1164 /* Stop transfers. */ 1165 usbnet_ep_stop_pipes(un); 1166 1167 /* 1168 * Now that the software is quiescent, ask the driver to stop 1169 * the hardware. The driver's uno_stop routine now has 1170 * exclusive access to any registers that might previously have 1171 * been used by to ifmedia, mii, or ioctl callbacks. 1172 * 1173 * Don't bother if the device is being detached, though -- if 1174 * it's been unplugged then there's no point in trying to touch 1175 * the registers. 1176 */ 1177 if (!usbnet_isdying(un)) 1178 uno_stop(un, ifp, disable); 1179 1180 /* Free RX/TX resources. */ 1181 usbnet_rx_list_fini(un); 1182 usbnet_tx_list_fini(un); 1183 1184 /* Close pipes. */ 1185 usbnet_ep_close_pipes(un); 1186 1187 /* Everything is quesced now. */ 1188 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1189 ifp->if_flags &= ~IFF_RUNNING; 1190 } 1191 1192 static void 1193 usbnet_if_stop(struct ifnet *ifp, int disable) 1194 { 1195 struct usbnet * const un = ifp->if_softc; 1196 1197 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1198 1199 /* 1200 * If we're already stopped, nothing to do. 1201 * 1202 * XXX This should be an assertion, but it may require some 1203 * analysis -- and possibly some tweaking -- of sys/net to 1204 * ensure. 1205 */ 1206 if ((ifp->if_flags & IFF_RUNNING) == 0) 1207 return; 1208 1209 usbnet_stop(un, ifp, disable); 1210 } 1211 1212 /* 1213 * Generic tick task function. 1214 * 1215 * usbnet_tick() is triggered from a callout, and triggers a call to 1216 * usbnet_tick_task() from the usb_task subsystem. 1217 */ 1218 static void 1219 usbnet_tick(void *arg) 1220 { 1221 USBNETHIST_FUNC(); 1222 struct usbnet * const un = arg; 1223 struct usbnet_private * const unp = un->un_pri; 1224 1225 USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); 1226 1227 /* Perform periodic stuff in process context */ 1228 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); 1229 } 1230 1231 static void 1232 usbnet_watchdog(struct ifnet *ifp) 1233 { 1234 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1235 struct usbnet * const un = ifp->if_softc; 1236 struct usbnet_private * const unp = un->un_pri; 1237 struct usbnet_cdata * const cd = un_cdata(un); 1238 1239 if_statinc(ifp, if_oerrors); 1240 device_printf(un->un_dev, "watchdog timeout\n"); 1241 1242 if (cd->uncd_tx_cnt > 0) { 1243 DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0); 1244 usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); 1245 if (cd->uncd_tx_cnt != 0) 1246 DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0); 1247 } 1248 1249 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1250 (*ifp->if_start)(ifp); 1251 } 1252 1253 static void 1254 usbnet_tick_task(void *arg) 1255 { 1256 USBNETHIST_FUNC(); 1257 struct usbnet * const un = arg; 1258 struct usbnet_private * const unp = un->un_pri; 1259 struct ifnet * const ifp = usbnet_ifp(un); 1260 struct mii_data * const mii = usbnet_mii(un); 1261 1262 USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0); 1263 1264 mutex_enter(&unp->unp_txlock); 1265 const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0; 1266 mutex_exit(&unp->unp_txlock); 1267 if (timeout) 1268 usbnet_watchdog(ifp); 1269 1270 /* Call driver if requested. */ 1271 uno_tick(un); 1272 1273 mutex_enter(&unp->unp_miilock); 1274 DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); 1275 if (mii) { 1276 mii_tick(mii); 1277 if (!unp->unp_link) 1278 (*mii->mii_statchg)(ifp); 1279 } 1280 1281 if (!unp->unp_stopped && !usbnet_isdying(un)) 1282 callout_schedule(&unp->unp_stat_ch, hz); 1283 mutex_exit(&unp->unp_miilock); 1284 } 1285 1286 static int 1287 usbnet_if_init(struct ifnet *ifp) 1288 { 1289 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1290 struct usbnet * const un = ifp->if_softc; 1291 int error; 1292 1293 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); 1294 1295 /* 1296 * Prevent anyone from bringing the interface back up once 1297 * we're detaching. 1298 */ 1299 if (usbnet_isdying(un)) 1300 return EIO; 1301 1302 /* 1303 * If we're already running, nothing to do. 1304 * 1305 * XXX This should be an assertion, but it may require some 1306 * analysis -- and possibly some tweaking -- of sys/net to 1307 * ensure. 1308 */ 1309 if (ifp->if_flags & IFF_RUNNING) 1310 return 0; 1311 1312 error = uno_init(un, ifp); 1313 if (error) 1314 return error; 1315 error = usbnet_init_rx_tx(un); 1316 if (error) 1317 return error; 1318 1319 return 0; 1320 } 1321 1322 1323 /* Various accessors. */ 1324 1325 void 1326 usbnet_set_link(struct usbnet *un, bool link) 1327 { 1328 usbnet_isowned_mii(un); 1329 un->un_pri->unp_link = link; 1330 } 1331 1332 struct ifnet * 1333 usbnet_ifp(struct usbnet *un) 1334 { 1335 return &un->un_pri->unp_ec.ec_if; 1336 } 1337 1338 struct ethercom * 1339 usbnet_ec(struct usbnet *un) 1340 { 1341 return &un->un_pri->unp_ec; 1342 } 1343 1344 struct mii_data * 1345 usbnet_mii(struct usbnet *un) 1346 { 1347 return un->un_pri->unp_ec.ec_mii; 1348 } 1349 1350 krndsource_t * 1351 usbnet_rndsrc(struct usbnet *un) 1352 { 1353 return &un->un_pri->unp_rndsrc; 1354 } 1355 1356 void * 1357 usbnet_softc(struct usbnet *un) 1358 { 1359 return un->un_sc; 1360 } 1361 1362 bool 1363 usbnet_havelink(struct usbnet *un) 1364 { 1365 return un->un_pri->unp_link; 1366 } 1367 1368 bool 1369 usbnet_isdying(struct usbnet *un) 1370 { 1371 return atomic_load_relaxed(&un->un_pri->unp_dying); 1372 } 1373 1374 1375 /* Locking. */ 1376 1377 static void 1378 usbnet_isowned_rx(struct usbnet *un) 1379 { 1380 KASSERT(mutex_owned(&un->un_pri->unp_rxlock)); 1381 } 1382 1383 static void 1384 usbnet_isowned_tx(struct usbnet *un) 1385 { 1386 KASSERT(mutex_owned(&un->un_pri->unp_txlock)); 1387 } 1388 1389 /* Autoconf management. */ 1390 1391 static bool 1392 usbnet_empty_eaddr(struct usbnet * const un) 1393 { 1394 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && 1395 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && 1396 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); 1397 } 1398 1399 /* 1400 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant 1401 * 'usbnet'. The first is enough to enable device access (eg, endpoints 1402 * are connected and commands can be sent), and the second connects the 1403 * device to the system networking. 1404 * 1405 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped. 1406 * Also usable as driver detach directly. 1407 * 1408 * To skip ethernet configuration (eg, point-to-point), make sure that 1409 * the un_eaddr[] is fully zero. 1410 */ 1411 1412 void 1413 usbnet_attach(struct usbnet *un) 1414 { 1415 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1416 1417 /* Required inputs. */ 1418 KASSERT(un->un_ops->uno_tx_prepare); 1419 KASSERT(un->un_ops->uno_rx_loop); 1420 KASSERT(un->un_rx_bufsz); 1421 KASSERT(un->un_tx_bufsz); 1422 KASSERT(un->un_rx_list_cnt); 1423 KASSERT(un->un_tx_list_cnt); 1424 1425 /* Unfortunate fact. */ 1426 KASSERT(un == device_private(un->un_dev)); 1427 1428 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); 1429 struct usbnet_private * const unp = un->un_pri; 1430 1431 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, 1432 USB_TASKQ_MPSAFE); 1433 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); 1434 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); 1435 1436 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1437 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); 1438 mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE); 1439 mutex_init(&unp->unp_mcastlock, MUTEX_DEFAULT, IPL_SOFTCLOCK); 1440 1441 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), 1442 RND_TYPE_NET, RND_FLAG_DEFAULT); 1443 1444 usbnet_rx_list_alloc(un); 1445 usbnet_tx_list_alloc(un); 1446 1447 unp->unp_number = atomic_inc_uint_nv(&usbnet_number); 1448 1449 unp->unp_stopped = true; 1450 unp->unp_rxstopped = true; 1451 unp->unp_txstopped = true; 1452 unp->unp_attached = true; 1453 } 1454 1455 static void 1456 usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) 1457 { 1458 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1459 struct usbnet_private * const unp = un->un_pri; 1460 struct mii_data * const mii = &unp->unp_mii; 1461 struct ifnet * const ifp = usbnet_ifp(un); 1462 1463 KASSERT(un->un_ops->uno_read_reg); 1464 KASSERT(un->un_ops->uno_write_reg); 1465 KASSERT(un->un_ops->uno_statchg); 1466 1467 mii->mii_ifp = ifp; 1468 mii->mii_readreg = usbnet_mii_readreg; 1469 mii->mii_writereg = usbnet_mii_writereg; 1470 mii->mii_statchg = usbnet_mii_statchg; 1471 mii->mii_flags = MIIF_AUTOTSLEEP; 1472 1473 usbnet_ec(un)->ec_mii = mii; 1474 ifmedia_init_with_lock(&mii->mii_media, 0, 1475 usbnet_media_upd, ether_mediastatus, &unp->unp_miilock); 1476 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, 1477 unm->un_mii_offset, unm->un_mii_flags); 1478 1479 if (LIST_FIRST(&mii->mii_phys) == NULL) { 1480 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 1481 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 1482 } else 1483 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 1484 } 1485 1486 void 1487 usbnet_attach_ifp(struct usbnet *un, 1488 unsigned if_flags, /* additional if_flags */ 1489 unsigned if_extflags, /* additional if_extflags */ 1490 const struct usbnet_mii *unm) /* additional mii_attach flags */ 1491 { 1492 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1493 struct usbnet_private * const unp = un->un_pri; 1494 struct ifnet * const ifp = usbnet_ifp(un); 1495 1496 KASSERT(unp->unp_attached); 1497 KASSERT(!unp->unp_ifp_attached); 1498 1499 ifp->if_softc = un; 1500 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); 1501 ifp->if_flags = if_flags; 1502 ifp->if_extflags = IFEF_MPSAFE | if_extflags; 1503 ifp->if_ioctl = usbnet_if_ioctl; 1504 ifp->if_start = usbnet_if_start; 1505 ifp->if_init = usbnet_if_init; 1506 ifp->if_stop = usbnet_if_stop; 1507 1508 if (unm) 1509 usbnet_attach_mii(un, unm); 1510 else 1511 unp->unp_link = true; 1512 1513 /* Attach the interface. */ 1514 if_initialize(ifp); 1515 if (ifp->_if_input == NULL) 1516 ifp->if_percpuq = if_percpuq_create(ifp); 1517 if_register(ifp); 1518 unp->unp_ifp_attached = true; 1519 1520 /* 1521 * If ethernet address is all zero, skip ether_ifattach() and 1522 * instead attach bpf here.. 1523 */ 1524 if (!usbnet_empty_eaddr(un)) { 1525 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); 1526 aprint_normal_dev(un->un_dev, "Ethernet address %s\n", 1527 ether_sprintf(un->un_eaddr)); 1528 ether_ifattach(ifp, un->un_eaddr); 1529 } else { 1530 if_alloc_sadl(ifp); 1531 bpf_attach(ifp, DLT_RAW, 0); 1532 } 1533 1534 /* Now ready, and attached. */ 1535 IFQ_SET_READY(&ifp->if_snd); 1536 1537 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); 1538 1539 if (!pmf_device_register(un->un_dev, NULL, NULL)) 1540 aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); 1541 } 1542 1543 int 1544 usbnet_detach(device_t self, int flags) 1545 { 1546 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1547 struct usbnet * const un = device_private(self); 1548 struct usbnet_private * const unp = un->un_pri; 1549 1550 /* Detached before attached finished, so just bail out. */ 1551 if (unp == NULL || !unp->unp_attached) 1552 return 0; 1553 1554 struct ifnet * const ifp = usbnet_ifp(un); 1555 struct mii_data * const mii = usbnet_mii(un); 1556 1557 /* 1558 * Prevent new activity. After we stop the interface, it 1559 * cannot be brought back up. 1560 */ 1561 atomic_store_relaxed(&unp->unp_dying, true); 1562 1563 /* 1564 * If we're still running on the network, stop and wait for all 1565 * asynchronous activity to finish. 1566 * 1567 * If usbnet_attach_ifp never ran, IFNET_LOCK won't work, but 1568 * no activity is possible, so just skip this part. 1569 */ 1570 if (unp->unp_ifp_attached) { 1571 IFNET_LOCK(ifp); 1572 if (ifp->if_flags & IFF_RUNNING) { 1573 usbnet_if_stop(ifp, 1); 1574 } 1575 IFNET_UNLOCK(ifp); 1576 } 1577 1578 /* 1579 * The callout and tick task can't be scheduled anew at this 1580 * point, and usbnet_if_stop has waited for them to complete. 1581 */ 1582 KASSERT(!callout_pending(&unp->unp_stat_ch)); 1583 KASSERT(!usb_task_pending(un->un_udev, &unp->unp_ticktask)); 1584 1585 if (mii) { 1586 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); 1587 ifmedia_fini(&mii->mii_media); 1588 } 1589 if (unp->unp_ifp_attached) { 1590 if (!usbnet_empty_eaddr(un)) 1591 ether_ifdetach(ifp); 1592 else 1593 bpf_detach(ifp); 1594 if_detach(ifp); 1595 } 1596 usbnet_ec(un)->ec_mii = NULL; 1597 1598 usbnet_rx_list_free(un); 1599 usbnet_tx_list_free(un); 1600 1601 rnd_detach_source(&unp->unp_rndsrc); 1602 1603 mutex_destroy(&unp->unp_mcastlock); 1604 mutex_destroy(&unp->unp_miilock); 1605 mutex_destroy(&unp->unp_rxlock); 1606 mutex_destroy(&unp->unp_txlock); 1607 1608 callout_destroy(&unp->unp_stat_ch); 1609 1610 pmf_device_deregister(un->un_dev); 1611 1612 /* 1613 * Notify userland that we're going away, if we arrived in the 1614 * first place. 1615 */ 1616 if (unp->unp_ifp_attached) { 1617 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, 1618 un->un_dev); 1619 } 1620 1621 kmem_free(unp, sizeof(*unp)); 1622 un->un_pri = NULL; 1623 1624 return 0; 1625 } 1626 1627 int 1628 usbnet_activate(device_t self, devact_t act) 1629 { 1630 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1631 struct usbnet * const un = device_private(self); 1632 struct usbnet_private * const unp = un->un_pri; 1633 struct ifnet * const ifp = usbnet_ifp(un); 1634 1635 switch (act) { 1636 case DVACT_DEACTIVATE: 1637 if_deactivate(ifp); 1638 1639 atomic_store_relaxed(&unp->unp_dying, true); 1640 1641 mutex_enter(&unp->unp_miilock); 1642 unp->unp_stopped = true; 1643 mutex_exit(&unp->unp_miilock); 1644 1645 mutex_enter(&unp->unp_rxlock); 1646 unp->unp_rxstopped = true; 1647 mutex_exit(&unp->unp_rxlock); 1648 1649 mutex_enter(&unp->unp_txlock); 1650 unp->unp_txstopped = true; 1651 mutex_exit(&unp->unp_txlock); 1652 1653 return 0; 1654 default: 1655 return EOPNOTSUPP; 1656 } 1657 } 1658 1659 MODULE(MODULE_CLASS_MISC, usbnet, NULL); 1660 1661 static int 1662 usbnet_modcmd(modcmd_t cmd, void *arg) 1663 { 1664 switch (cmd) { 1665 case MODULE_CMD_INIT: 1666 return 0; 1667 case MODULE_CMD_FINI: 1668 return 0; 1669 case MODULE_CMD_STAT: 1670 case MODULE_CMD_AUTOUNLOAD: 1671 default: 1672 return ENOTTY; 1673 } 1674 } 1675