1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 Andrew Thompson ([email protected]) 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_setsoftc(ifp, ue); 225 if_initname(ifp, "ue", ue->ue_unit); 226 if (ue->ue_methods->ue_attach_post_sub != NULL) { 227 ue->ue_ifp = ifp; 228 error = ue->ue_methods->ue_attach_post_sub(ue); 229 } else { 230 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 231 if (ue->ue_methods->ue_ioctl != NULL) 232 if_setioctlfn(ifp, ue->ue_methods->ue_ioctl); 233 else 234 if_setioctlfn(ifp, uether_ioctl); 235 if_setstartfn(ifp, ue_start); 236 if_setinitfn(ifp, ue_init); 237 if_setsendqlen(ifp, ifqmaxlen); 238 if_setsendqready(ifp); 239 ue->ue_ifp = ifp; 240 241 if (ue->ue_methods->ue_mii_upd != NULL && 242 ue->ue_methods->ue_mii_sts != NULL) { 243 bus_topo_lock(); 244 error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, 245 ue_ifmedia_upd, ue->ue_methods->ue_mii_sts, 246 BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 247 bus_topo_unlock(); 248 } 249 } 250 251 if (error) { 252 device_printf(ue->ue_dev, "attaching PHYs failed\n"); 253 goto fail; 254 } 255 256 if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev)); 257 ether_ifattach(ifp, ue->ue_eaddr); 258 /* Tell upper layer we support VLAN oversized frames. */ 259 if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU) 260 if_setifheaderlen(ifp, sizeof(struct ether_vlan_header)); 261 262 CURVNET_RESTORE(); 263 264 snprintf(num, sizeof(num), "%u", ue->ue_unit); 265 ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx, 266 &SYSCTL_NODE_CHILDREN(_net, ue), 267 OID_AUTO, num, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, ""); 268 SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx, 269 SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO, "%parent", 270 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, ue, 0, 271 ue_sysctl_parent, "A", "parent device"); 272 273 UE_LOCK(ue); 274 return; 275 276 fail: 277 CURVNET_RESTORE(); 278 279 /* drain mbuf queue */ 280 mbufq_drain(&ue->ue_rxq); 281 282 /* free unit */ 283 free_unr(ueunit, ue->ue_unit); 284 if (ue->ue_ifp != NULL) { 285 if_free(ue->ue_ifp); 286 ue->ue_ifp = NULL; 287 } 288 UE_LOCK(ue); 289 return; 290 } 291 292 void 293 uether_ifdetach(struct usb_ether *ue) 294 { 295 if_t ifp; 296 297 /* wait for any post attach or other command to complete */ 298 usb_proc_drain(&ue->ue_tq); 299 300 /* read "ifnet" pointer after taskqueue drain */ 301 ifp = ue->ue_ifp; 302 303 if (ifp != NULL) { 304 /* we are not running any more */ 305 UE_LOCK(ue); 306 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 307 UE_UNLOCK(ue); 308 309 /* drain any callouts */ 310 usb_callout_drain(&ue->ue_watchdog); 311 312 /* 313 * Detach ethernet first to stop miibus calls from 314 * user-space: 315 */ 316 ether_ifdetach(ifp); 317 318 /* detach miibus */ 319 if (ue->ue_miibus != NULL) { 320 bus_topo_lock(); 321 device_delete_child(ue->ue_dev, ue->ue_miibus); 322 bus_topo_unlock(); 323 } 324 325 /* free interface instance */ 326 if_free(ifp); 327 328 /* free sysctl */ 329 sysctl_ctx_free(&ue->ue_sysctl_ctx); 330 331 /* drain mbuf queue */ 332 mbufq_drain(&ue->ue_rxq); 333 334 /* free unit */ 335 free_unr(ueunit, ue->ue_unit); 336 } 337 338 /* free taskqueue, if any */ 339 usb_proc_free(&ue->ue_tq); 340 } 341 342 uint8_t 343 uether_is_gone(struct usb_ether *ue) 344 { 345 return (usb_proc_is_gone(&ue->ue_tq)); 346 } 347 348 void 349 uether_init(void *arg) 350 { 351 352 ue_init(arg); 353 } 354 355 static void 356 ue_init(void *arg) 357 { 358 struct usb_ether *ue = arg; 359 360 UE_LOCK(ue); 361 ue_queue_command(ue, ue_start_task, 362 &ue->ue_sync_task[0].hdr, 363 &ue->ue_sync_task[1].hdr); 364 UE_UNLOCK(ue); 365 } 366 367 static void 368 ue_start_task(struct usb_proc_msg *_task) 369 { 370 struct usb_ether_cfg_task *task = 371 (struct usb_ether_cfg_task *)_task; 372 struct usb_ether *ue = task->ue; 373 if_t ifp = ue->ue_ifp; 374 375 UE_LOCK_ASSERT(ue, MA_OWNED); 376 377 ue->ue_methods->ue_init(ue); 378 379 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 380 return; 381 382 if (ue->ue_methods->ue_tick != NULL) 383 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 384 } 385 386 static void 387 ue_stop_task(struct usb_proc_msg *_task) 388 { 389 struct usb_ether_cfg_task *task = 390 (struct usb_ether_cfg_task *)_task; 391 struct usb_ether *ue = task->ue; 392 393 UE_LOCK_ASSERT(ue, MA_OWNED); 394 395 usb_callout_stop(&ue->ue_watchdog); 396 397 ue->ue_methods->ue_stop(ue); 398 } 399 400 void 401 uether_start(if_t ifp) 402 { 403 404 ue_start(ifp); 405 } 406 407 static void 408 ue_start(if_t ifp) 409 { 410 struct usb_ether *ue = if_getsoftc(ifp); 411 412 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 413 return; 414 415 UE_LOCK(ue); 416 ue->ue_methods->ue_start(ue); 417 UE_UNLOCK(ue); 418 } 419 420 static void 421 ue_promisc_task(struct usb_proc_msg *_task) 422 { 423 struct usb_ether_cfg_task *task = 424 (struct usb_ether_cfg_task *)_task; 425 struct usb_ether *ue = task->ue; 426 427 ue->ue_methods->ue_setpromisc(ue); 428 } 429 430 static void 431 ue_setmulti_task(struct usb_proc_msg *_task) 432 { 433 struct usb_ether_cfg_task *task = 434 (struct usb_ether_cfg_task *)_task; 435 struct usb_ether *ue = task->ue; 436 437 ue->ue_methods->ue_setmulti(ue); 438 } 439 440 int 441 uether_ifmedia_upd(if_t ifp) 442 { 443 444 return (ue_ifmedia_upd(ifp)); 445 } 446 447 static int 448 ue_ifmedia_upd(if_t ifp) 449 { 450 struct usb_ether *ue = if_getsoftc(ifp); 451 452 /* Defer to process context */ 453 UE_LOCK(ue); 454 ue_queue_command(ue, ue_ifmedia_task, 455 &ue->ue_media_task[0].hdr, 456 &ue->ue_media_task[1].hdr); 457 UE_UNLOCK(ue); 458 459 return (0); 460 } 461 462 static void 463 ue_ifmedia_task(struct usb_proc_msg *_task) 464 { 465 struct usb_ether_cfg_task *task = 466 (struct usb_ether_cfg_task *)_task; 467 struct usb_ether *ue = task->ue; 468 if_t ifp = ue->ue_ifp; 469 470 ue->ue_methods->ue_mii_upd(ifp); 471 } 472 473 static void 474 ue_watchdog(void *arg) 475 { 476 struct usb_ether *ue = arg; 477 if_t ifp = ue->ue_ifp; 478 479 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 480 return; 481 482 ue_queue_command(ue, ue_tick_task, 483 &ue->ue_tick_task[0].hdr, 484 &ue->ue_tick_task[1].hdr); 485 486 usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue); 487 } 488 489 static void 490 ue_tick_task(struct usb_proc_msg *_task) 491 { 492 struct usb_ether_cfg_task *task = 493 (struct usb_ether_cfg_task *)_task; 494 struct usb_ether *ue = task->ue; 495 if_t ifp = ue->ue_ifp; 496 497 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) 498 return; 499 500 ue->ue_methods->ue_tick(ue); 501 } 502 503 int 504 uether_ioctl(if_t ifp, u_long command, caddr_t data) 505 { 506 struct usb_ether *ue = if_getsoftc(ifp); 507 struct ifreq *ifr = (struct ifreq *)data; 508 struct mii_data *mii; 509 int error = 0; 510 511 switch (command) { 512 case SIOCSIFFLAGS: 513 UE_LOCK(ue); 514 if (if_getflags(ifp) & IFF_UP) { 515 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) 516 ue_queue_command(ue, ue_promisc_task, 517 &ue->ue_promisc_task[0].hdr, 518 &ue->ue_promisc_task[1].hdr); 519 else 520 ue_queue_command(ue, ue_start_task, 521 &ue->ue_sync_task[0].hdr, 522 &ue->ue_sync_task[1].hdr); 523 } else { 524 ue_queue_command(ue, ue_stop_task, 525 &ue->ue_sync_task[0].hdr, 526 &ue->ue_sync_task[1].hdr); 527 } 528 UE_UNLOCK(ue); 529 break; 530 case SIOCADDMULTI: 531 case SIOCDELMULTI: 532 UE_LOCK(ue); 533 ue_queue_command(ue, ue_setmulti_task, 534 &ue->ue_multi_task[0].hdr, 535 &ue->ue_multi_task[1].hdr); 536 UE_UNLOCK(ue); 537 break; 538 case SIOCGIFMEDIA: 539 case SIOCSIFMEDIA: 540 if (ue->ue_miibus != NULL) { 541 mii = device_get_softc(ue->ue_miibus); 542 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 543 } else 544 error = ether_ioctl(ifp, command, data); 545 break; 546 default: 547 error = ether_ioctl(ifp, command, data); 548 break; 549 } 550 return (error); 551 } 552 553 static int 554 uether_modevent(module_t mod, int type, void *data) 555 { 556 557 switch (type) { 558 case MOD_LOAD: 559 ueunit = new_unrhdr(0, INT_MAX, NULL); 560 break; 561 case MOD_UNLOAD: 562 break; 563 default: 564 return (EOPNOTSUPP); 565 } 566 return (0); 567 } 568 static moduledata_t uether_mod = { 569 "uether", 570 uether_modevent, 571 0 572 }; 573 574 struct mbuf * 575 uether_newbuf(void) 576 { 577 struct mbuf *m_new; 578 579 m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 580 if (m_new == NULL) 581 return (NULL); 582 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 583 584 m_adj(m_new, ETHER_ALIGN); 585 return (m_new); 586 } 587 588 int 589 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m, 590 unsigned len) 591 { 592 if_t ifp = ue->ue_ifp; 593 594 UE_LOCK_ASSERT(ue, MA_OWNED); 595 596 /* finalize mbuf */ 597 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 598 m->m_pkthdr.rcvif = ifp; 599 m->m_pkthdr.len = m->m_len = len; 600 601 /* enqueue for later when the lock can be released */ 602 (void)mbufq_enqueue(&ue->ue_rxq, m); 603 return (0); 604 } 605 606 int 607 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc, 608 unsigned offset, unsigned len) 609 { 610 if_t ifp = ue->ue_ifp; 611 struct mbuf *m; 612 613 UE_LOCK_ASSERT(ue, MA_OWNED); 614 615 if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN) 616 return (1); 617 618 m = uether_newbuf(); 619 if (m == NULL) { 620 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 621 return (ENOMEM); 622 } 623 624 usbd_copy_out(pc, offset, mtod(m, uint8_t *), len); 625 626 /* finalize mbuf */ 627 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 628 m->m_pkthdr.rcvif = ifp; 629 m->m_pkthdr.len = m->m_len = len; 630 631 /* enqueue for later when the lock can be released */ 632 (void)mbufq_enqueue(&ue->ue_rxq, m); 633 return (0); 634 } 635 636 void 637 uether_rxflush(struct usb_ether *ue) 638 { 639 if_t ifp = ue->ue_ifp; 640 struct epoch_tracker et; 641 struct mbuf *m, *n; 642 643 UE_LOCK_ASSERT(ue, MA_OWNED); 644 645 n = mbufq_flush(&ue->ue_rxq); 646 UE_UNLOCK(ue); 647 NET_EPOCH_ENTER(et); 648 while ((m = n) != NULL) { 649 n = STAILQ_NEXT(m, m_stailqpkt); 650 m->m_nextpkt = NULL; 651 if_input(ifp, m); 652 } 653 NET_EPOCH_EXIT(et); 654 UE_LOCK(ue); 655 } 656 657 /* 658 * USB net drivers are run by DRIVER_MODULE() thus SI_SUB_DRIVERS, 659 * SI_ORDER_MIDDLE. Run uether after that. 660 */ 661 DECLARE_MODULE(uether, uether_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); 662 MODULE_VERSION(uether, 1); 663