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