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