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