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