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