1/* $FreeBSD: stable/10/sys/dev/usb/net/usb_ethernet.c 334758 2018-06-07 07:33:46Z hselasky $ */
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/cdefs.h>
28__FBSDID("$FreeBSD: stable/10/sys/dev/usb/net/usb_ethernet.c 334758 2018-06-07 07:33:46Z hselasky $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/condvar.h>
34#include <sys/kernel.h>
35#include <sys/lock.h>
36#include <sys/malloc.h>
37#include <sys/mbuf.h>
38#include <sys/module.h>
39#include <sys/mutex.h>
40#include <sys/socket.h>
41#include <sys/sockio.h>
42#include <sys/sysctl.h>
43#include <sys/sx.h>
44
45#include <net/if.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
60static SYSCTL_NODE(_net, OID_AUTO, ue, CTLFLAG_RD, 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
67MODULE_DEPEND(uether, usb, 1, 1, 1);
68MODULE_DEPEND(uether, miibus, 1, 1, 1);
69
70static struct unrhdr *ueunit;
71
72static usb_proc_callback_t ue_attach_post_task;
73static usb_proc_callback_t ue_promisc_task;
74static usb_proc_callback_t ue_setmulti_task;
75static usb_proc_callback_t ue_ifmedia_task;
76static usb_proc_callback_t ue_tick_task;
77static usb_proc_callback_t ue_start_task;
78static usb_proc_callback_t ue_stop_task;
79
80static void	ue_init(void *);
81static void	ue_start(struct ifnet *);
82static int	ue_ifmedia_upd(struct ifnet *);
83static void	ue_watchdog(void *);
84
85/*
86 * Return values:
87 *    0: success
88 * Else: device has been detached
89 */
90uint8_t
91uether_pause(struct usb_ether *ue, unsigned int _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
101static void
102ue_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
132struct ifnet *
133uether_getifp(struct usb_ether *ue)
134{
135	return (ue->ue_ifp);
136}
137
138struct mii_data *
139uether_getmii(struct usb_ether *ue)
140{
141	return (device_get_softc(ue->ue_miibus));
142}
143
144void *
145uether_getsc(struct usb_ether *ue)
146{
147	return (ue->ue_sc);
148}
149
150static int
151ue_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(req, name, strlen(name));
158}
159
160int
161uether_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
186error:
187	return (error);
188}
189
190void
191uether_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
201static void
202ue_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	struct ifnet *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
220	error = 0;
221	CURVNET_SET_QUIET(vnet0);
222	ifp = if_alloc(IFT_ETHER);
223	if (ifp == NULL) {
224		device_printf(ue->ue_dev, "could not allocate ifnet\n");
225		goto fail;
226	}
227
228	ifp->if_softc = ue;
229	if_initname(ifp, "ue", ue->ue_unit);
230	if (ue->ue_methods->ue_attach_post_sub != NULL) {
231		ue->ue_ifp = ifp;
232		error = ue->ue_methods->ue_attach_post_sub(ue);
233	} else {
234		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
235		if (ue->ue_methods->ue_ioctl != NULL)
236			ifp->if_ioctl = ue->ue_methods->ue_ioctl;
237		else
238			ifp->if_ioctl = uether_ioctl;
239		ifp->if_start = ue_start;
240		ifp->if_init = ue_init;
241		IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
242		ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
243		IFQ_SET_READY(&ifp->if_snd);
244		ue->ue_ifp = ifp;
245
246		if (ue->ue_methods->ue_mii_upd != NULL &&
247		    ue->ue_methods->ue_mii_sts != NULL) {
248			/* device_xxx() depends on this */
249			mtx_lock(&Giant);
250			error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp,
251			    ue_ifmedia_upd, ue->ue_methods->ue_mii_sts,
252			    BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
253			mtx_unlock(&Giant);
254		}
255	}
256
257	if (error) {
258		device_printf(ue->ue_dev, "attaching PHYs failed\n");
259		goto fail;
260	}
261
262	if_printf(ifp, "<USB Ethernet> on %s\n", device_get_nameunit(ue->ue_dev));
263	ether_ifattach(ifp, ue->ue_eaddr);
264	/* Tell upper layer we support VLAN oversized frames. */
265	if (ifp->if_capabilities & IFCAP_VLAN_MTU)
266		ifp->if_hdrlen = sizeof(struct ether_vlan_header);
267
268	CURVNET_RESTORE();
269
270	snprintf(num, sizeof(num), "%u", ue->ue_unit);
271	ue->ue_sysctl_oid = SYSCTL_ADD_NODE(&ue->ue_sysctl_ctx,
272	    &SYSCTL_NODE_CHILDREN(_net, ue),
273	    OID_AUTO, num, CTLFLAG_RD, NULL, "");
274	SYSCTL_ADD_PROC(&ue->ue_sysctl_ctx,
275	    SYSCTL_CHILDREN(ue->ue_sysctl_oid), OID_AUTO,
276	    "%parent", CTLTYPE_STRING | CTLFLAG_RD, ue, 0,
277	    ue_sysctl_parent, "A", "parent device");
278
279	UE_LOCK(ue);
280	return;
281
282fail:
283	CURVNET_RESTORE();
284	free_unr(ueunit, ue->ue_unit);
285	if (ue->ue_ifp != NULL) {
286		if_free(ue->ue_ifp);
287		ue->ue_ifp = NULL;
288	}
289	UE_LOCK(ue);
290	return;
291}
292
293void
294uether_ifdetach(struct usb_ether *ue)
295{
296	struct ifnet *ifp;
297
298	/* wait for any post attach or other command to complete */
299	usb_proc_drain(&ue->ue_tq);
300
301	/* read "ifnet" pointer after taskqueue drain */
302	ifp = ue->ue_ifp;
303
304	if (ifp != NULL) {
305
306		/* we are not running any more */
307		UE_LOCK(ue);
308		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
309		UE_UNLOCK(ue);
310
311		/* drain any callouts */
312		usb_callout_drain(&ue->ue_watchdog);
313
314		/* detach miibus */
315		if (ue->ue_miibus != NULL) {
316			mtx_lock(&Giant);	/* device_xxx() depends on this */
317			device_delete_child(ue->ue_dev, ue->ue_miibus);
318			mtx_unlock(&Giant);
319		}
320
321		/* detach ethernet */
322		ether_ifdetach(ifp);
323
324		/* free interface instance */
325		if_free(ifp);
326
327		/* free sysctl */
328		sysctl_ctx_free(&ue->ue_sysctl_ctx);
329
330		/* free unit */
331		free_unr(ueunit, ue->ue_unit);
332	}
333
334	/* free taskqueue, if any */
335	usb_proc_free(&ue->ue_tq);
336}
337
338uint8_t
339uether_is_gone(struct usb_ether *ue)
340{
341	return (usb_proc_is_gone(&ue->ue_tq));
342}
343
344void
345uether_init(void *arg)
346{
347
348	ue_init(arg);
349}
350
351static void
352ue_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
363static void
364ue_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, MA_OWNED);
372
373	ue->ue_methods->ue_init(ue);
374
375	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
376		return;
377
378	if (ue->ue_methods->ue_tick != NULL)
379		usb_callout_reset(&ue->ue_watchdog, hz, ue_watchdog, ue);
380}
381
382static void
383ue_stop_task(struct usb_proc_msg *_task)
384{
385	struct usb_ether_cfg_task *task =
386	    (struct usb_ether_cfg_task *)_task;
387	struct usb_ether *ue = task->ue;
388
389	UE_LOCK_ASSERT(ue, MA_OWNED);
390
391	usb_callout_stop(&ue->ue_watchdog);
392
393	ue->ue_methods->ue_stop(ue);
394}
395
396void
397uether_start(struct ifnet *ifp)
398{
399
400	ue_start(ifp);
401}
402
403static void
404ue_start(struct ifnet *ifp)
405{
406	struct usb_ether *ue = ifp->if_softc;
407
408	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
409		return;
410
411	UE_LOCK(ue);
412	ue->ue_methods->ue_start(ue);
413	UE_UNLOCK(ue);
414}
415
416static void
417ue_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
426static void
427ue_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
436int
437uether_ifmedia_upd(struct ifnet *ifp)
438{
439
440	return (ue_ifmedia_upd(ifp));
441}
442
443static int
444ue_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
458static void
459ue_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
469static void
470ue_watchdog(void *arg)
471{
472	struct usb_ether *ue = arg;
473	struct ifnet *ifp = ue->ue_ifp;
474
475	if ((ifp->if_drv_flags & IFF_DRV_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
485static void
486ue_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_drv_flags & IFF_DRV_RUNNING) == 0)
494		return;
495
496	ue->ue_methods->ue_tick(ue);
497}
498
499int
500uether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
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_drv_flags & IFF_DRV_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
549static int
550uether_modevent(module_t mod, int type, void *data)
551{
552
553	switch (type) {
554	case MOD_LOAD:
555		ueunit = new_unrhdr(0, INT_MAX, NULL);
556		break;
557	case MOD_UNLOAD:
558		break;
559	default:
560		return (EOPNOTSUPP);
561	}
562	return (0);
563}
564static moduledata_t uether_mod = {
565	"uether",
566	uether_modevent,
567	0
568};
569
570struct mbuf *
571uether_newbuf(void)
572{
573	struct mbuf *m_new;
574
575	m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
576	if (m_new == NULL)
577		return (NULL);
578	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
579
580	m_adj(m_new, ETHER_ALIGN);
581	return (m_new);
582}
583
584int
585uether_rxmbuf(struct usb_ether *ue, struct mbuf *m,
586    unsigned int len)
587{
588	struct ifnet *ifp = ue->ue_ifp;
589
590	UE_LOCK_ASSERT(ue, MA_OWNED);
591
592	/* finalize mbuf */
593	ifp->if_ipackets++;
594	m->m_pkthdr.rcvif = ifp;
595	m->m_pkthdr.len = m->m_len = len;
596
597	/* enqueue for later when the lock can be released */
598	_IF_ENQUEUE(&ue->ue_rxq, m);
599	return (0);
600}
601
602int
603uether_rxbuf(struct usb_ether *ue, struct usb_page_cache *pc,
604    unsigned int offset, unsigned int len)
605{
606	struct ifnet *ifp = ue->ue_ifp;
607	struct mbuf *m;
608
609	UE_LOCK_ASSERT(ue, MA_OWNED);
610
611	if (len < ETHER_HDR_LEN || len > MCLBYTES - ETHER_ALIGN)
612		return (1);
613
614	m = uether_newbuf();
615	if (m == NULL) {
616		ifp->if_iqdrops++;
617		return (ENOMEM);
618	}
619
620	usbd_copy_out(pc, offset, mtod(m, uint8_t *), len);
621
622	/* finalize mbuf */
623	ifp->if_ipackets++;
624	m->m_pkthdr.rcvif = ifp;
625	m->m_pkthdr.len = m->m_len = len;
626
627	/* enqueue for later when the lock can be released */
628	_IF_ENQUEUE(&ue->ue_rxq, m);
629	return (0);
630}
631
632void
633uether_rxflush(struct usb_ether *ue)
634{
635	struct ifnet *ifp = ue->ue_ifp;
636	struct mbuf *m;
637
638	UE_LOCK_ASSERT(ue, MA_OWNED);
639
640	for (;;) {
641		_IF_DEQUEUE(&ue->ue_rxq, m);
642		if (m == NULL)
643			break;
644
645		/*
646		 * The USB xfer has been resubmitted so its safe to unlock now.
647		 */
648		UE_UNLOCK(ue);
649		ifp->if_input(ifp, m);
650		UE_LOCK(ue);
651	}
652}
653
654DECLARE_MODULE(uether, uether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
655MODULE_VERSION(uether, 1);
656