1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 Andrew Thompson (thompsa@FreeBSD.org) 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/systm.h> 30 #include <sys/bus.h> 31 #include <sys/condvar.h> 32 #include <sys/kernel.h> 33 #include <sys/lock.h> 34 #include <sys/malloc.h> 35 #include <sys/mbuf.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/socket.h> 39 #include <sys/sockio.h> 40 #include <sys/sysctl.h> 41 #include <sys/sx.h> 42 43 #include <net/if.h> 44 #include <net/if_var.h> 45 #include <net/ethernet.h> 46 #include <net/if_types.h> 47 #include <net/if_media.h> 48 #include <net/if_vlan_var.h> 49 50 #include <dev/mii/mii.h> 51 #include <dev/mii/miivar.h> 52 53 #include <dev/usb/usb.h> 54 #include <dev/usb/usbdi.h> 55 56 #include <dev/usb/usb_process.h> 57 #include <dev/usb/net/usb_ethernet.h> 58 59 static SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 60 "USB Ethernet parameters"); 61 62 #define UE_LOCK(_ue) mtx_lock((_ue)->ue_mtx) 63 #define UE_UNLOCK(_ue) mtx_unlock((_ue)->ue_mtx) 64 #define UE_LOCK_ASSERT(_ue, t) mtx_assert((_ue)->ue_mtx, t) 65 66 MODULE_DEPEND(uether, usb, 1, 1, 1); 67 MODULE_DEPEND(uether, miibus, 1, 1, 1); 68 69 static struct unrhdr *ueunit; 70 71 static usb_proc_callback_t ue_attach_post_task; 72 static usb_proc_callback_t ue_promisc_task; 73 static usb_proc_callback_t ue_setmulti_task; 74 static usb_proc_callback_t ue_ifmedia_task; 75 static usb_proc_callback_t ue_tick_task; 76 static usb_proc_callback_t ue_start_task; 77 static usb_proc_callback_t ue_stop_task; 78 79 static void ue_init(void *); 80 static void ue_start(if_t); 81 static int ue_ifmedia_upd(if_t); 82 static void ue_watchdog(void *); 83 84 /* 85 * Return values: 86 * 0: success 87 * Else: device has been detached 88 */ 89 uint8_t 90 uether_pause(struct usb_ether *ue, unsigned _ticks) 91 { 92 if (usb_proc_is_gone(&ue->ue_tq)) { 93 /* nothing to do */ 94 return (1); 95 } 96 usb_pause_mtx(ue->ue_mtx, _ticks); 97 return (0); 98 } 99 100 static void 101 ue_queue_command(struct usb_ether *ue, 102 usb_proc_callback_t *fn, 103 struct usb_proc_msg *t0, struct usb_proc_msg *t1) 104 { 105 struct usb_ether_cfg_task *task; 106 107 UE_LOCK_ASSERT(ue, MA_OWNED); 108 109 if (usb_proc_is_gone(&ue->ue_tq)) { 110 return; /* nothing to do */ 111 } 112 /* 113 * NOTE: The task cannot get executed before we drop the 114 * "sc_mtx" mutex. It is safe to update fields in the message 115 * structure after that the message got queued. 116 */ 117 task = (struct usb_ether_cfg_task *) 118 usb_proc_msignal(&ue->ue_tq, t0, t1); 119 120 /* Setup callback and self pointers */ 121 task->hdr.pm_callback = fn; 122 task->ue = ue; 123 124 /* 125 * Start and stop must be synchronous! 126 */ 127 if ((fn == ue_start_task) || (fn == ue_stop_task)) 128 usb_proc_mwait(&ue->ue_tq, t0, t1); 129 } 130 131 if_t 132 uether_getifp(struct usb_ether *ue) 133 { 134 return (ue->ue_ifp); 135 } 136 137 struct mii_data * 138 uether_getmii(struct usb_ether *ue) 139 { 140 return (device_get_softc(ue->ue_miibus)); 141 } 142 143 void * 144 uether_getsc(struct usb_ether *ue) 145 { 146 return (ue->ue_sc); 147 } 148 149 static int 150 ue_sysctl_parent(SYSCTL_HANDLER_ARGS) 151 { 152 struct usb_ether *ue = arg1; 153 const char *name; 154 155 name = device_get_nameunit(ue->ue_dev); 156 return SYSCTL_OUT_STR(req, name); 157 } 158 159 int 160 uether_ifattach(struct usb_ether *ue) 161 { 162 int error; 163 164 /* check some critical parameters */ 165 if ((ue->ue_dev == NULL) || 166 (ue->ue_udev == NULL) || 167 (ue->ue_mtx == NULL) || 168 (ue->ue_methods == NULL)) 169 return (EINVAL); 170 171 error = usb_proc_create(&ue->ue_tq, ue->ue_mtx, 172 device_get_nameunit(ue->ue_dev), USB_PRI_MED); 173 if (error) { 174 device_printf(ue->ue_dev, "could not setup taskqueue\n"); 175 goto error; 176 } 177 178 /* fork rest of the attach code */ 179 UE_LOCK(ue); 180 ue_queue_command(ue, ue_attach_post_task, 181 &ue->ue_sync_task[0].hdr, 182 &ue->ue_sync_task[1].hdr); 183 UE_UNLOCK(ue); 184 185 error: 186 return (error); 187 } 188 189 void 190 uether_ifattach_wait(struct usb_ether *ue) 191 { 192 193 UE_LOCK(ue); 194 usb_proc_mwait(&ue->ue_tq, 195 &ue->ue_sync_task[0].hdr, 196 &ue->ue_sync_task[1].hdr); 197 UE_UNLOCK(ue); 198 } 199 200 static void 201 ue_attach_post_task(struct usb_proc_msg *_task) 202 { 203 struct usb_ether_cfg_task *task = 204 (struct usb_ether_cfg_task *)_task; 205 struct usb_ether *ue = task->ue; 206 if_t ifp; 207 int error; 208 char num[14]; /* sufficient for 32 bits */ 209 210 /* first call driver's post attach routine */ 211 ue->ue_methods->ue_attach_post(ue); 212 213 UE_UNLOCK(ue); 214 215 ue->ue_unit = alloc_unr(ueunit); 216 usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_mtx, 0); 217 sysctl_ctx_init(&ue->ue_sysctl_ctx); 218 mbufq_init(&ue->ue_rxq, 0 /* unlimited length */); 219 220 error = 0; 221 CURVNET_SET_QUIET(vnet0); 222 ifp = if_alloc(IFT_ETHER); 223 if (ifp == NULL) { 224 device_printf(ue->ue_dev, "could not allocate ifnet\n"); 225 goto fail; 226 } 227 228 if_setsoftc(ifp, ue); 229 if_initname(ifp, "ue", ue->ue_unit); 230 if (ue->ue_methods->ue_attach_post_sub != NULL) { 231 ue->ue_ifp = ifp; 232 error = ue->ue_methods->ue_attach_post_sub(ue); 233 } else { 234 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 235 if (ue->ue_methods->ue_ioctl != NULL) 236 if_setioctlfn(ifp, ue->ue_methods->ue_ioctl); 237 else 238 if_setioctlfn(ifp, uether_ioctl); 239 if_setstartfn(ifp, ue_start); 240 if_setinitfn(ifp, ue_init); 241 if_setsendqlen(ifp, ifqmaxlen); 242 if_setsendqready(ifp); 243 ue->ue_ifp = ifp; 244 245 if (ue->ue_methods->ue_mii_upd != NULL && 246 ue->ue_methods->ue_mii_sts != NULL) { 247 bus_topo_lock(); 248 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, 249 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts, 250 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 251 bus_topo_unlock(); 252 } 253 } 254 255 if (error) { 256 device_printf(ue->ue_dev, "attaching PHYs failed\n"); 257 goto fail; 258 } 259 260 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev)); 261 ether_ifattach(ifp, ue->ue_eaddr); 262 /* Tell upper layer we support VLAN oversized frames. */ 263 if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 264 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 265 266 CURVNET_RESTORE(); 267 268 snprintf(num, sizeof(num), "%u", ue->ue_unit); 269 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx, 270 &SYSCTL_NODE_CHILDREN(_net, ue), 271 OID_AUTO, num, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 272 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx, 273 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO, "%parent", 274 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ue, 0, 275 ue_sysctl_parent, "A", "parent device"); 276 277 UE_LOCK(ue); 278 return; 279 280 fail: 281 CURVNET_RESTORE(); 282 283 /* drain mbuf queue */ 284 mbufq_drain(&ue->ue_rxq); 285 286 /* free unit */ 287 free_unr(ueunit, ue->ue_unit); 288 if (ue->ue_ifp != NULL) { 289 if_free(ue->ue_ifp); 290 ue->ue_ifp = NULL; 291 } 292 UE_LOCK(ue); 293 return; 294 } 295 296 void 297 uether_ifdetach(struct usb_ether *ue) 298 { 299 if_t ifp; 300 301 /* wait for any post attach or other command to complete */ 302 usb_proc_drain(&ue->ue_tq); 303 304 /* read "ifnet" pointer after taskqueue drain */ 305 ifp = ue->ue_ifp; 306 307 if (ifp != NULL) { 308 /* we are not running any more */ 309 UE_LOCK(ue); 310 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 311 UE_UNLOCK(ue); 312 313 /* drain any callouts */ 314 usb_callout_drain(&ue->ue_watchdog); 315 316 /* 317 * Detach ethernet first to stop miibus calls from 318 * user-space: 319 */ 320 ether_ifdetach(ifp); 321 322 /* detach miibus */ 323 if (ue->ue_miibus != NULL) { 324 bus_topo_lock(); 325 device_delete_child(ue->ue_dev, ue->ue_miibus); 326 bus_topo_unlock(); 327 } 328 329 /* free interface instance */ 330 if_free(ifp); 331 332 /* free sysctl */ 333 sysctl_ctx_free(&ue->ue_sysctl_ctx); 334 335 /* drain mbuf queue */ 336 mbufq_drain(&ue->ue_rxq); 337 338 /* free unit */ 339 free_unr(ueunit, ue->ue_unit); 340 } 341 342 /* free taskqueue, if any */ 343 usb_proc_free(&ue->ue_tq); 344 } 345 346 uint8_t 347 uether_is_gone(struct usb_ether *ue) 348 { 349 return (usb_proc_is_gone(&ue->ue_tq)); 350 } 351 352 void 353 uether_init(void *arg) 354 { 355 356 ue_init(arg); 357 } 358 359 static void 360 ue_init(void *arg) 361 { 362 struct usb_ether *ue = arg; 363 364 UE_LOCK(ue); 365 ue_queue_command(ue, ue_start_task, 366 &ue->ue_sync_task[0].hdr, 367 &ue->ue_sync_task[1].hdr); 368 UE_UNLOCK(ue); 369 } 370 371 static void 372 ue_start_task(struct usb_proc_msg *_task) 373 { 374 struct usb_ether_cfg_task *task = 375 (struct usb_ether_cfg_task *)_task; 376 struct usb_ether *ue = task->ue; 377 if_t ifp = ue->ue_ifp; 378 379 UE_LOCK_ASSERT(ue, MA_OWNED); 380 381 ue->ue_methods->ue_init(ue); 382 383 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 384 return; 385 386 if (ue->ue_methods->ue_tick != NULL) 387 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 388 } 389 390 static void 391 ue_stop_task(struct usb_proc_msg *_task) 392 { 393 struct usb_ether_cfg_task *task = 394 (struct usb_ether_cfg_task *)_task; 395 struct usb_ether *ue = task->ue; 396 397 UE_LOCK_ASSERT(ue, MA_OWNED); 398 399 usb_callout_stop(&ue->ue_watchdog); 400 401 ue->ue_methods->ue_stop(ue); 402 } 403 404 void 405 uether_start(if_t ifp) 406 { 407 408 ue_start(ifp); 409 } 410 411 static void 412 ue_start(if_t ifp) 413 { 414 struct usb_ether *ue = if_getsoftc(ifp); 415 416 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 417 return; 418 419 UE_LOCK(ue); 420 ue->ue_methods->ue_start(ue); 421 UE_UNLOCK(ue); 422 } 423 424 static void 425 ue_promisc_task(struct usb_proc_msg *_task) 426 { 427 struct usb_ether_cfg_task *task = 428 (struct usb_ether_cfg_task *)_task; 429 struct usb_ether *ue = task->ue; 430 431 ue->ue_methods->ue_setpromisc(ue); 432 } 433 434 static void 435 ue_setmulti_task(struct usb_proc_msg *_task) 436 { 437 struct usb_ether_cfg_task *task = 438 (struct usb_ether_cfg_task *)_task; 439 struct usb_ether *ue = task->ue; 440 441 ue->ue_methods->ue_setmulti(ue); 442 } 443 444 int 445 uether_ifmedia_upd(if_t ifp) 446 { 447 448 return (ue_ifmedia_upd(ifp)); 449 } 450 451 static int 452 ue_ifmedia_upd(if_t ifp) 453 { 454 struct usb_ether *ue = if_getsoftc(ifp); 455 456 /* Defer to process context */ 457 UE_LOCK(ue); 458 ue_queue_command(ue, ue_ifmedia_task, 459 &ue->ue_media_task[0].hdr, 460 &ue->ue_media_task[1].hdr); 461 UE_UNLOCK(ue); 462 463 return (0); 464 } 465 466 static void 467 ue_ifmedia_task(struct usb_proc_msg *_task) 468 { 469 struct usb_ether_cfg_task *task = 470 (struct usb_ether_cfg_task *)_task; 471 struct usb_ether *ue = task->ue; 472 if_t ifp = ue->ue_ifp; 473 474 ue->ue_methods->ue_mii_upd(ifp); 475 } 476 477 static void 478 ue_watchdog(void *arg) 479 { 480 struct usb_ether *ue = arg; 481 if_t ifp = ue->ue_ifp; 482 483 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 484 return; 485 486 ue_queue_command(ue, ue_tick_task, 487 &ue->ue_tick_task[0].hdr, 488 &ue->ue_tick_task[1].hdr); 489 490 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 491 } 492 493 static void 494 ue_tick_task(struct usb_proc_msg *_task) 495 { 496 struct usb_ether_cfg_task *task = 497 (struct usb_ether_cfg_task *)_task; 498 struct usb_ether *ue = task->ue; 499 if_t ifp = ue->ue_ifp; 500 501 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 502 return; 503 504 ue->ue_methods->ue_tick(ue); 505 } 506 507 int 508 uether_ioctl(if_t ifp, u_long command, caddr_t data) 509 { 510 struct usb_ether *ue = if_getsoftc(ifp); 511 struct ifreq *ifr = (struct ifreq *)data; 512 struct mii_data *mii; 513 int error = 0; 514 515 switch (command) { 516 case SIOCSIFFLAGS: 517 UE_LOCK(ue); 518 if (if_getflags(ifp) & IFF_UP) { 519 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) 520 ue_queue_command(ue, ue_promisc_task, 521 &ue->ue_promisc_task[0].hdr, 522 &ue->ue_promisc_task[1].hdr); 523 else 524 ue_queue_command(ue, ue_start_task, 525 &ue->ue_sync_task[0].hdr, 526 &ue->ue_sync_task[1].hdr); 527 } else { 528 ue_queue_command(ue, ue_stop_task, 529 &ue->ue_sync_task[0].hdr, 530 &ue->ue_sync_task[1].hdr); 531 } 532 UE_UNLOCK(ue); 533 break; 534 case SIOCADDMULTI: 535 case SIOCDELMULTI: 536 UE_LOCK(ue); 537 ue_queue_command(ue, ue_setmulti_task, 538 &ue->ue_multi_task[0].hdr, 539 &ue->ue_multi_task[1].hdr); 540 UE_UNLOCK(ue); 541 break; 542 case SIOCGIFMEDIA: 543 case SIOCSIFMEDIA: 544 if (ue->ue_miibus != NULL) { 545 mii = device_get_softc(ue->ue_miibus); 546 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 547 } else 548 error = ether_ioctl(ifp, command, data); 549 break; 550 default: 551 error = ether_ioctl(ifp, command, data); 552 break; 553 } 554 return (error); 555 } 556 557 static int 558 uether_modevent(module_t mod, int type, void *data) 559 { 560 561 switch (type) { 562 case MOD_LOAD: 563 ueunit = new_unrhdr(0, INT_MAX, NULL); 564 break; 565 case MOD_UNLOAD: 566 break; 567 default: 568 return (EOPNOTSUPP); 569 } 570 return (0); 571 } 572 static moduledata_t uether_mod = { 573 "uether", 574 uether_modevent, 575 0 576 }; 577 578 struct mbuf * 579 uether_newbuf(void) 580 { 581 struct mbuf *m_new; 582 583 m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 584 if (m_new == NULL) 585 return (NULL); 586 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 587 588 m_adj(m_new, ETHER_ALIGN); 589 return (m_new); 590 } 591 592 int 593 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m, 594 unsigned len) 595 { 596 if_t ifp = ue->ue_ifp; 597 598 UE_LOCK_ASSERT(ue, MA_OWNED); 599 600 /* finalize mbuf */ 601 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 602 m->m_pkthdr.rcvif = ifp; 603 m->m_pkthdr.len = m->m_len = len; 604 605 /* enqueue for later when the lock can be released */ 606 (void)mbufq_enqueue(&ue->ue_rxq, m); 607 return (0); 608 } 609 610 int 611 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc, 612 unsigned offset, unsigned len) 613 { 614 if_t ifp = ue->ue_ifp; 615 struct mbuf *m; 616 617 UE_LOCK_ASSERT(ue, MA_OWNED); 618 619 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) 620 return (1); 621 622 m = uether_newbuf(); 623 if (m == NULL) { 624 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 625 return (ENOMEM); 626 } 627 628 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len); 629 630 /* finalize mbuf */ 631 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 632 m->m_pkthdr.rcvif = ifp; 633 m->m_pkthdr.len = m->m_len = len; 634 635 /* enqueue for later when the lock can be released */ 636 (void)mbufq_enqueue(&ue->ue_rxq, m); 637 return (0); 638 } 639 640 void 641 uether_rxflush(struct usb_ether *ue) 642 { 643 if_t ifp = ue->ue_ifp; 644 struct epoch_tracker et; 645 struct mbuf *m, *n; 646 647 UE_LOCK_ASSERT(ue, MA_OWNED); 648 649 n = mbufq_flush(&ue->ue_rxq); 650 UE_UNLOCK(ue); 651 NET_EPOCH_ENTER(et); 652 while ((m = n) != NULL) { 653 n = STAILQ_NEXT(m, m_stailqpkt); 654 m->m_nextpkt = NULL; 655 if_input(ifp, m); 656 } 657 NET_EPOCH_EXIT(et); 658 UE_LOCK(ue); 659 } 660 661 /* 662 * USB net drivers are run by DRIVER_MODULE() thus SI_SUB_DRIVERS, 663 * SI_ORDER_MIDDLE. Run uether after that. 664 */ 665 DECLARE_MODULE(uether, uether_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 666 MODULE_VERSION(uether, 1); 667