xref: /dragonfly/sys/bus/u4b/net/usb_ethernet.c (revision 2234273d)
1 /* $FreeBSD: head/sys/dev/usb/net/usb_ethernet.c 271832 2014-09-18 21:09:22Z glebius $ */
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 #if 0
69 static struct unrhdr *ueunit;
70 #endif
71 DEVFS_DEFINE_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_arpcom.ac_if);
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 = uether_getifp(ue);
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 	KKASSERT(!lockowned(ue->ue_lock));
207 	ue->ue_unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ue), 0);
208 	usb_callout_init_mtx(&ue->ue_watchdog, ue->ue_lock, 0);
209 	sysctl_ctx_init(&ue->ue_sysctl_ctx);
210 
211 	KKASSERT(!lockowned(ue->ue_lock));
212 	error = 0;
213 
214 	ifp->if_softc = ue;
215 	if_initname(ifp, "ue", ue->ue_unit);
216 	if (ue->ue_methods->ue_attach_post_sub != NULL) {
217 		error = ue->ue_methods->ue_attach_post_sub(ue);
218 		KKASSERT(!lockowned(ue->ue_lock));
219 	} else {
220 		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
221 		if (ue->ue_methods->ue_ioctl != NULL)
222 			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
223 		else
224 			ifp->if_ioctl = uether_ioctl;
225 		ifp->if_start = ue_start;
226 		ifp->if_init = ue_init;
227 		ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
228 		ifq_set_ready(&ifp->if_snd);
229 
230 		if (ue->ue_methods->ue_mii_upd != NULL &&
231 		    ue->ue_methods->ue_mii_sts != NULL) {
232 			error = mii_phy_probe(ue->ue_dev, &ue->ue_miibus,
233 					      ue_ifmedia_upd, ue->ue_methods->ue_mii_sts);
234 		}
235 	}
236 
237 	if (error) {
238 		device_printf(ue->ue_dev, "attaching PHYs failed\n");
239 		goto fail;
240 	}
241 
242 	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
243 	ether_ifattach(ifp, ue->ue_eaddr, NULL);
244 	/* Tell upper layer we support VLAN oversized frames. */
245 	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
246 		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
247 
248 	ksnprintf(num, sizeof(num), "%u", ue->ue_unit);
249 	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
250 	    &SYSCTL_NODE_CHILDREN(_net, ue),
251 	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
252 	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
253 	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
254 	    "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
255 	    ue_sysctl_parent, "A", "parent device");
256 
257 	KKASSERT(!lockowned(ue->ue_lock));
258 	UE_LOCK(ue);
259 	return;
260 
261 fail:
262 	devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ue), ue->ue_unit);
263 	UE_LOCK(ue);
264 	return;
265 }
266 
267 void
268 uether_ifdetach(struct usb_ether *ue)
269 {
270 	struct ifnet *ifp;
271 
272 	/* wait for any post attach or other command to complete */
273 	usb_proc_drain(&ue->ue_tq);
274 
275 	/* read "ifnet" pointer after taskqueue drain */
276 	ifp = uether_getifp(ue);
277 
278 	if (ifp != NULL) {
279 
280 		/* we are not running any more */
281 		UE_LOCK(ue);
282 		ifp->if_flags &= ~IFF_RUNNING;
283 		UE_UNLOCK(ue);
284 
285 		/* drain any callouts */
286 		usb_callout_drain(&ue->ue_watchdog);
287 
288 		/* detach miibus */
289 		if (ue->ue_miibus != NULL) {
290 			device_delete_child(ue->ue_dev, ue->ue_miibus);
291 		}
292 
293 		/* detach ethernet */
294 		ether_ifdetach(ifp);
295 
296 		/* free sysctl */
297 		sysctl_ctx_free(&ue->ue_sysctl_ctx);
298 
299 		/* free unit */
300 		devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ue), ue->ue_unit);
301 	}
302 
303 	/* free taskqueue, if any */
304 	usb_proc_free(&ue->ue_tq);
305 }
306 
307 uint8_t
308 uether_is_gone(struct usb_ether *ue)
309 {
310 	return (usb_proc_is_gone(&ue->ue_tq));
311 }
312 
313 void
314 uether_init(void *arg)
315 {
316 	ue_init(arg);
317 }
318 
319 static void
320 ue_init(void *arg)
321 {
322 	struct usb_ether *ue = arg;
323 
324 	UE_LOCK(ue);
325 	ue_queue_command(ue, ue_start_task,
326 	    &ue->ue_sync_task[0].hdr,
327 	    &ue->ue_sync_task[1].hdr);
328 	UE_UNLOCK(ue);
329 }
330 
331 static void
332 ue_start_task(struct usb_proc_msg *_task)
333 {
334 	struct usb_ether_cfg_task *task =
335 	    (struct usb_ether_cfg_task *)_task;
336 	struct usb_ether *ue = task->ue;
337 	struct ifnet *ifp = uether_getifp(ue);
338 
339 	UE_LOCK_ASSERT(ue);
340 
341 	ue->ue_methods->ue_init(ue);
342 
343 	if ((ifp->if_flags & IFF_RUNNING) == 0)
344 		return;
345 
346 	if (ue->ue_methods->ue_tick != NULL)
347 		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
348 }
349 
350 static void
351 ue_stop_task(struct usb_proc_msg *_task)
352 {
353 	struct usb_ether_cfg_task *task =
354 	    (struct usb_ether_cfg_task *)_task;
355 	struct usb_ether *ue = task->ue;
356 
357 	UE_LOCK_ASSERT(ue);
358 
359 	usb_callout_stop(&ue->ue_watchdog);
360 
361 	ue->ue_methods->ue_stop(ue);
362 }
363 
364 void
365 uether_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
366 {
367 
368 	ue_start(ifp, ifsq);
369 }
370 
371 static void
372 ue_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
373 {
374 	struct usb_ether *ue = ifp->if_softc;
375 
376 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
377 
378 	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
379 	    ifq_is_oactive(&ifp->if_snd))
380 		return;
381 
382 	UE_LOCK(ue);
383 	ue->ue_methods->ue_start(ue);
384 	UE_UNLOCK(ue);
385 }
386 
387 static void
388 ue_promisc_task(struct usb_proc_msg *_task)
389 {
390 	struct usb_ether_cfg_task *task =
391 	    (struct usb_ether_cfg_task *)_task;
392 	struct usb_ether *ue = task->ue;
393 
394 	ue->ue_methods->ue_setpromisc(ue);
395 }
396 
397 static void
398 ue_setmulti_task(struct usb_proc_msg *_task)
399 {
400 	struct usb_ether_cfg_task *task =
401 	    (struct usb_ether_cfg_task *)_task;
402 	struct usb_ether *ue = task->ue;
403 
404 	ue->ue_methods->ue_setmulti(ue);
405 }
406 
407 int
408 uether_ifmedia_upd(struct ifnet *ifp)
409 {
410 
411 	return (ue_ifmedia_upd(ifp));
412 }
413 
414 static int
415 ue_ifmedia_upd(struct ifnet *ifp)
416 {
417 	struct usb_ether *ue = ifp->if_softc;
418 
419 	/* Defer to process context */
420 	UE_LOCK(ue);
421 	ue_queue_command(ue, ue_ifmedia_task,
422 	    &ue->ue_media_task[0].hdr,
423 	    &ue->ue_media_task[1].hdr);
424 	UE_UNLOCK(ue);
425 
426 	return (0);
427 }
428 
429 static void
430 ue_ifmedia_task(struct usb_proc_msg *_task)
431 {
432 	struct usb_ether_cfg_task *task =
433 	    (struct usb_ether_cfg_task *)_task;
434 	struct usb_ether *ue = task->ue;
435 	struct ifnet *ifp = uether_getifp(ue);
436 
437 	ue->ue_methods->ue_mii_upd(ifp);
438 }
439 
440 static void
441 ue_watchdog(void *arg)
442 {
443 	struct usb_ether *ue = arg;
444 	struct ifnet *ifp = uether_getifp(ue);
445 
446 	if ((ifp->if_flags & IFF_RUNNING) == 0)
447 		return;
448 
449 	ue_queue_command(ue, ue_tick_task,
450 	    &ue->ue_tick_task[0].hdr,
451 	    &ue->ue_tick_task[1].hdr);
452 
453 	usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
454 }
455 
456 static void
457 ue_tick_task(struct usb_proc_msg *_task)
458 {
459 	struct usb_ether_cfg_task *task =
460 	    (struct usb_ether_cfg_task *)_task;
461 	struct usb_ether *ue = task->ue;
462 	struct ifnet *ifp = uether_getifp(ue);
463 
464 	if ((ifp->if_flags & IFF_RUNNING) == 0)
465 		return;
466 
467 	ue->ue_methods->ue_tick(ue);
468 }
469 
470 int
471 uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred* uc)
472 {
473 	struct usb_ether *ue = ifp->if_softc;
474 	struct ifreq *ifr = (struct ifreq *)data;
475 	struct mii_data *mii;
476 	int error = 0;
477 
478 	switch (command) {
479 	case SIOCSIFFLAGS:
480 		UE_LOCK(ue);
481 		if (ifp->if_flags & IFF_UP) {
482 			if (ifp->if_flags & IFF_RUNNING)
483 				ue_queue_command(ue, ue_promisc_task,
484 				    &ue->ue_promisc_task[0].hdr,
485 				    &ue->ue_promisc_task[1].hdr);
486 			else
487 				ue_queue_command(ue, ue_start_task,
488 				    &ue->ue_sync_task[0].hdr,
489 				    &ue->ue_sync_task[1].hdr);
490 		} else {
491 			ue_queue_command(ue, ue_stop_task,
492 			    &ue->ue_sync_task[0].hdr,
493 			    &ue->ue_sync_task[1].hdr);
494 		}
495 		UE_UNLOCK(ue);
496 		break;
497 	case SIOCADDMULTI:
498 	case SIOCDELMULTI:
499 		UE_LOCK(ue);
500 		ue_queue_command(ue, ue_setmulti_task,
501 		    &ue->ue_multi_task[0].hdr,
502 		    &ue->ue_multi_task[1].hdr);
503 		UE_UNLOCK(ue);
504 		break;
505 	case SIOCGIFMEDIA:
506 	case SIOCSIFMEDIA:
507 		if (ue->ue_miibus != NULL) {
508 			mii = device_get_softc(ue->ue_miibus);
509 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
510 		} else
511 			error = ether_ioctl(ifp, command, data);
512 		break;
513 	default:
514 		error = ether_ioctl(ifp, command, data);
515 		break;
516 	}
517 	return (error);
518 }
519 
520 static int
521 uether_modevent(module_t mod, int type, void *data)
522 {
523 	static int attached = 0;
524 
525 	switch (type) {
526 	case MOD_LOAD:
527 		if (attached)
528 			return (EEXIST);
529 
530 		devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ue));
531 
532 		attached = 1;
533 		break;
534 	case MOD_UNLOAD:
535 		devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ue));
536 		break;
537 	default:
538 		return (EOPNOTSUPP);
539 	}
540 	return (0);
541 }
542 static moduledata_t uether_mod = {
543 	"uether",
544 	uether_modevent,
545 	0
546 };
547 
548 struct mbuf *
549 uether_newbuf(void)
550 {
551 	struct mbuf *m_new;
552 
553 	m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
554 	if (m_new == NULL)
555 		return (NULL);
556 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
557 
558 	m_adj(m_new, ETHER_ALIGN);
559 	return (m_new);
560 }
561 
562 int
563 uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
564     unsigned int len)
565 {
566 	struct ifnet *ifp = uether_getifp(ue);
567 
568 	UE_LOCK_ASSERT(ue);
569 
570 	/* finalize mbuf */
571 	IFNET_STAT_INC(ifp, ipackets, 1);
572 	m->m_pkthdr.rcvif = ifp;
573 	m->m_pkthdr.len = m->m_len = len;
574 
575 	/* enqueue for later when the lock can be released */
576 	IF_ENQUEUE(&ue->ue_rxq, m);
577 	return (0);
578 }
579 
580 int
581 uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
582     unsigned int offset, unsigned int len)
583 {
584 	struct ifnet *ifp = uether_getifp(ue);
585 	struct mbuf *m;
586 
587 	UE_LOCK_ASSERT(ue);
588 
589 	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
590 		return (1);
591 
592 	m = uether_newbuf();
593 	if (m == NULL) {
594 		IFNET_STAT_INC(ifp, iqdrops, 1);
595 		return (ENOMEM);
596 	}
597 
598 	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
599 
600 	/* finalize mbuf */
601 	IFNET_STAT_INC(ifp, ipackets, 1);
602 	m->m_pkthdr.rcvif = ifp;
603 	m->m_pkthdr.len = m->m_len = len;
604 
605 	/* enqueue for later when the lock can be released */
606 	IF_ENQUEUE(&ue->ue_rxq, m);
607 	return (0);
608 }
609 
610 void
611 uether_rxflush(struct usb_ether *ue)
612 {
613 	struct ifnet *ifp = uether_getifp(ue);
614 	struct mbuf *m;
615 
616 	UE_LOCK_ASSERT(ue);
617 
618 	for (;;) {
619 		IF_DEQUEUE(&ue->ue_rxq, m);
620 		if (m == NULL)
621 			break;
622 
623 		/*
624 		 * The USB xfer has been resubmitted so its safe to unlock now.
625 		 */
626 		UE_UNLOCK(ue);
627 		ifp->if_input(ifp, m, NULL, -1);
628 		UE_LOCK(ue);
629 	}
630 }
631 
632 DECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
633 MODULE_VERSION(uether, 1);
634