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