Add NETISR_FLAG_NOTMPSAFE, which could be used as the last parameter to
[dragonfly.git] / sys / bus / usb / usb_ethersubr.c
1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000
3  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *
33  * $FreeBSD: src/sys/dev/usb/usb_ethersubr.c,v 1.17 2003/11/14 11:09:45 johan Exp $
34  * $DragonFly: src/sys/bus/usb/usb_ethersubr.c,v 1.21 2008/09/24 14:26:38 sephe Exp $
35  */
36
37 /*
38  * Callbacks in the USB code operate in a critical section.
39  *
40  * It is conceivable that this arrangement could trigger a condition
41  * where the input queues could get trampled in spite of our best effors
42  * to prevent it. To work around this, we implement a special input queue
43  * for USB ethernet adapter drivers. Rather than passing the frames directly
44  * to ether_input(), we pass them here, then schedule a soft interrupt to
45  * hand them to ether_input() later, outside of the USB interrupt context.
46  *
47  * It's questional as to whether this code should be expanded to
48  * handle other kinds of devices, or handle USB transfer callbacks
49  * in general. Right now, I need USB network interfaces to work
50  * properly.
51  */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/sockio.h>
56 #include <sys/mbuf.h>
57 #include <sys/malloc.h>
58 #include <sys/socket.h>
59
60 #include <sys/thread2.h>
61 #include <sys/msgport2.h>
62
63 #include <net/if.h>
64 #include <net/ifq_var.h>
65 #include <net/if_types.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/netisr.h>
69 #include <net/bpf.h>
70
71 #include "usb.h"
72 #include "usb_ethersubr.h"
73
74 static int netisr_inited = 0;
75
76 static void
77 usbintr(struct netmsg *msg)
78 {
79         struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
80         struct ifnet *ifp;
81
82         ifp = m->m_pkthdr.rcvif;
83         lwkt_serialize_enter(ifp->if_serializer);
84         (*ifp->if_input)(ifp, m);
85         lwkt_serialize_exit(ifp->if_serializer);
86         /* the msg is embedded in the mbuf, do not reply it */
87 }
88
89 void
90 usb_register_netisr(void)
91 {
92         if (netisr_inited == 0) {
93                 netisr_inited = 1;
94                 netisr_register(NETISR_USB, cpu0_portfn, usbintr,
95                                 NETISR_FLAG_NOTMPSAFE);
96         }
97 }
98
99 /*
100  * Must be called from a critical section.  This should be the case when
101  * called from a transfer callback routine.  Don't trust it, though.
102  */
103 void
104 usb_ether_input(struct mbuf *m)
105 {
106         crit_enter();
107         netisr_queue(NETISR_USB, m);
108         crit_exit();
109 }