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