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