| @@ -1,1754 +1,1742 @@ | | | @@ -1,1754 +1,1742 @@ |
1 | /* $NetBSD: usbnet.c,v 1.73 2022/03/03 05:50:39 riastradh Exp $ */ | | 1 | /* $NetBSD: usbnet.c,v 1.74 2022/03/03 05:50:47 riastradh Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2019 Matthew R. Green | | 4 | * Copyright (c) 2019 Matthew R. Green |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
15 | * | | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
26 | * SUCH DAMAGE. | | 26 | * SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | /* | | 29 | /* |
30 | * Common code shared between USB network drivers. | | 30 | * Common code shared between USB network drivers. |
31 | */ | | 31 | */ |
32 | | | 32 | |
33 | #include <sys/cdefs.h> | | 33 | #include <sys/cdefs.h> |
34 | __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.73 2022/03/03 05:50:39 riastradh Exp $"); | | 34 | __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.74 2022/03/03 05:50:47 riastradh Exp $"); |
35 | | | 35 | |
36 | #include <sys/param.h> | | 36 | #include <sys/param.h> |
37 | #include <sys/kernel.h> | | 37 | #include <sys/kernel.h> |
38 | #include <sys/kmem.h> | | 38 | #include <sys/kmem.h> |
39 | #include <sys/module.h> | | 39 | #include <sys/module.h> |
40 | #include <sys/atomic.h> | | 40 | #include <sys/atomic.h> |
41 | | | 41 | |
42 | #include <dev/usb/usbnet.h> | | 42 | #include <dev/usb/usbnet.h> |
43 | #include <dev/usb/usbhist.h> | | 43 | #include <dev/usb/usbhist.h> |
44 | | | 44 | |
45 | struct usbnet_cdata { | | 45 | struct usbnet_cdata { |
46 | struct usbnet_chain *uncd_tx_chain; | | 46 | struct usbnet_chain *uncd_tx_chain; |
47 | struct usbnet_chain *uncd_rx_chain; | | 47 | struct usbnet_chain *uncd_rx_chain; |
48 | | | 48 | |
49 | int uncd_tx_prod; | | 49 | int uncd_tx_prod; |
50 | int uncd_tx_cnt; | | 50 | int uncd_tx_cnt; |
51 | }; | | 51 | }; |
52 | | | 52 | |
53 | struct usbnet_private { | | 53 | struct usbnet_private { |
54 | /* | | 54 | /* |
55 | * - unp_core_lock protects most of this structure, the public one, | | 55 | * - unp_core_lock protects most of this structure, the public one, |
56 | * and the MII / media data. | | 56 | * and the MII / media data. |
57 | * - unp_rxlock protects the rx path and its data | | 57 | * - unp_rxlock protects the rx path and its data |
58 | * - unp_txlock protects the tx path and its data | | 58 | * - unp_txlock protects the tx path and its data |
59 | * - unp_detachcv handles detach vs open references | | 59 | * - unp_detachcv handles detach vs open references |
60 | * | | 60 | * |
61 | * the lock ordering is: | | 61 | * the lock ordering is: |
62 | * ifnet lock -> unp_core_lock -> unp_rxlock -> unp_txlock | | 62 | * ifnet lock -> unp_core_lock -> unp_rxlock -> unp_txlock |
63 | * - ifnet lock is not needed for unp_core_lock, but if ifnet lock is | | 63 | * - ifnet lock is not needed for unp_core_lock, but if ifnet lock is |
64 | * involved, it must be taken first | | 64 | * involved, it must be taken first |
65 | */ | | 65 | */ |
66 | kmutex_t unp_core_lock; | | 66 | kmutex_t unp_core_lock; |
67 | kmutex_t unp_rxlock; | | 67 | kmutex_t unp_rxlock; |
68 | kmutex_t unp_txlock; | | 68 | kmutex_t unp_txlock; |
69 | kcondvar_t unp_detachcv; | | 69 | kcondvar_t unp_detachcv; |
70 | | | 70 | |
71 | struct usbnet_cdata unp_cdata; | | 71 | struct usbnet_cdata unp_cdata; |
72 | | | 72 | |
73 | struct ethercom unp_ec; | | 73 | struct ethercom unp_ec; |
74 | struct mii_data unp_mii; | | 74 | struct mii_data unp_mii; |
75 | struct usb_task unp_mcasttask; | | 75 | struct usb_task unp_mcasttask; |
76 | struct usb_task unp_ticktask; | | 76 | struct usb_task unp_ticktask; |
77 | struct callout unp_stat_ch; | | 77 | struct callout unp_stat_ch; |
78 | struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; | | 78 | struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; |
79 | | | 79 | |
80 | volatile bool unp_dying; | | 80 | volatile bool unp_dying; |
81 | bool unp_stopping; | | 81 | bool unp_stopping; |
82 | bool unp_attached; | | 82 | bool unp_attached; |
83 | bool unp_ifp_attached; | | 83 | bool unp_ifp_attached; |
84 | bool unp_link; | | 84 | bool unp_link; |
85 | | | 85 | |
86 | int unp_refcnt; | | 86 | int unp_refcnt; |
87 | int unp_timer; | | 87 | int unp_timer; |
88 | unsigned short unp_if_flags; | | 88 | unsigned short unp_if_flags; |
89 | unsigned unp_number; | | 89 | unsigned unp_number; |
90 | | | 90 | |
91 | krndsource_t unp_rndsrc; | | 91 | krndsource_t unp_rndsrc; |
92 | | | 92 | |
93 | struct timeval unp_rx_notice; | | 93 | struct timeval unp_rx_notice; |
94 | struct timeval unp_tx_notice; | | 94 | struct timeval unp_tx_notice; |
95 | struct timeval unp_intr_notice; | | 95 | struct timeval unp_intr_notice; |
96 | }; | | 96 | }; |
97 | | | 97 | |
98 | #define un_cdata(un) (&(un)->un_pri->unp_cdata) | | 98 | #define un_cdata(un) (&(un)->un_pri->unp_cdata) |
99 | | | 99 | |
100 | volatile unsigned usbnet_number; | | 100 | volatile unsigned usbnet_number; |
101 | | | 101 | |
102 | static int usbnet_modcmd(modcmd_t, void *); | | 102 | static int usbnet_modcmd(modcmd_t, void *); |
103 | | | 103 | |
104 | #ifdef USB_DEBUG | | 104 | #ifdef USB_DEBUG |
105 | #ifndef USBNET_DEBUG | | 105 | #ifndef USBNET_DEBUG |
106 | #define usbnetdebug 0 | | 106 | #define usbnetdebug 0 |
107 | #else | | 107 | #else |
108 | static int usbnetdebug = 0; | | 108 | static int usbnetdebug = 0; |
109 | | | 109 | |
110 | SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") | | 110 | SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") |
111 | { | | 111 | { |
112 | int err; | | 112 | int err; |
113 | const struct sysctlnode *rnode; | | 113 | const struct sysctlnode *rnode; |
114 | const struct sysctlnode *cnode; | | 114 | const struct sysctlnode *cnode; |
115 | | | 115 | |
116 | err = sysctl_createv(clog, 0, NULL, &rnode, | | 116 | err = sysctl_createv(clog, 0, NULL, &rnode, |
117 | CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", | | 117 | CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", |
118 | SYSCTL_DESCR("usbnet global controls"), | | 118 | SYSCTL_DESCR("usbnet global controls"), |
119 | NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); | | 119 | NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); |
120 | | | 120 | |
121 | if (err) | | 121 | if (err) |
122 | goto fail; | | 122 | goto fail; |
123 | | | 123 | |
124 | /* control debugging printfs */ | | 124 | /* control debugging printfs */ |
125 | err = sysctl_createv(clog, 0, &rnode, &cnode, | | 125 | err = sysctl_createv(clog, 0, &rnode, &cnode, |
126 | CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, | | 126 | CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, |
127 | "debug", SYSCTL_DESCR("Enable debugging output"), | | 127 | "debug", SYSCTL_DESCR("Enable debugging output"), |
128 | NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); | | 128 | NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); |
129 | if (err) | | 129 | if (err) |
130 | goto fail; | | 130 | goto fail; |
131 | | | 131 | |
132 | return; | | 132 | return; |
133 | fail: | | 133 | fail: |
134 | aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); | | 134 | aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); |
135 | } | | 135 | } |
136 | | | 136 | |
137 | #endif /* USBNET_DEBUG */ | | 137 | #endif /* USBNET_DEBUG */ |
138 | #endif /* USB_DEBUG */ | | 138 | #endif /* USB_DEBUG */ |
139 | | | 139 | |
140 | #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) | | 140 | #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) |
141 | #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) | | 141 | #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) |
142 | #define USBNETHIST_FUNC() USBHIST_FUNC() | | 142 | #define USBNETHIST_FUNC() USBHIST_FUNC() |
143 | #define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) | | 143 | #define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) |
144 | #define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ | | 144 | #define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ |
145 | USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) | | 145 | USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) |
146 | #define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ | | 146 | #define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ |
147 | USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) | | 147 | USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) |
148 | | | 148 | |
149 | /* Callback vectors. */ | | 149 | /* Callback vectors. */ |
150 | | | 150 | |
151 | static void | | 151 | static void |
152 | uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) | | 152 | uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) |
153 | { | | 153 | { |
154 | KASSERTMSG(!un->un_pri->unp_ifp_attached || IFNET_LOCKED(ifp), | | 154 | KASSERTMSG(!un->un_pri->unp_ifp_attached || IFNET_LOCKED(ifp), |
155 | "%s", ifp->if_xname); | | 155 | "%s", ifp->if_xname); |
156 | usbnet_isowned_core(un); | | 156 | usbnet_isowned_core(un); |
157 | if (un->un_ops->uno_stop) | | 157 | if (un->un_ops->uno_stop) |
158 | (*un->un_ops->uno_stop)(ifp, disable); | | 158 | (*un->un_ops->uno_stop)(ifp, disable); |
159 | } | | 159 | } |
160 | | | 160 | |
161 | static int | | 161 | static int |
162 | uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) | | 162 | uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) |
163 | { | | 163 | { |
164 | | | 164 | |
165 | switch (cmd) { | | 165 | switch (cmd) { |
166 | case SIOCADDMULTI: | | 166 | case SIOCADDMULTI: |
167 | case SIOCDELMULTI: | | 167 | case SIOCDELMULTI: |
168 | break; | | 168 | break; |
169 | default: | | 169 | default: |
170 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 170 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
171 | } | | 171 | } |
172 | | | 172 | |
173 | if (un->un_ops->uno_ioctl) | | 173 | if (un->un_ops->uno_ioctl) |
174 | return (*un->un_ops->uno_ioctl)(ifp, cmd, data); | | 174 | return (*un->un_ops->uno_ioctl)(ifp, cmd, data); |
175 | return 0; | | 175 | return 0; |
176 | } | | 176 | } |
177 | | | 177 | |
178 | static int | | 178 | static int |
179 | uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) | | 179 | uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) |
180 | { | | 180 | { |
181 | | | 181 | |
182 | switch (cmd) { | | 182 | switch (cmd) { |
183 | case SIOCADDMULTI: | | 183 | case SIOCADDMULTI: |
184 | case SIOCDELMULTI: | | 184 | case SIOCDELMULTI: |
185 | break; | | 185 | break; |
186 | default: | | 186 | default: |
187 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 187 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
188 | } | | 188 | } |
189 | | | 189 | |
190 | return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); | | 190 | return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); |
191 | } | | 191 | } |
192 | | | 192 | |
193 | static int | | 193 | static int |
194 | uno_init(struct usbnet *un, struct ifnet *ifp) | | 194 | uno_init(struct usbnet *un, struct ifnet *ifp) |
195 | { | | 195 | { |
196 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 196 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
197 | return (*un->un_ops->uno_init)(ifp); | | 197 | return (*un->un_ops->uno_init)(ifp); |
198 | } | | 198 | } |
199 | | | 199 | |
200 | static int | | 200 | static int |
201 | uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) | | 201 | uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) |
202 | { | | 202 | { |
203 | usbnet_isowned_core(un); | | 203 | usbnet_isowned_core(un); |
204 | return (*un->un_ops->uno_read_reg)(un, phy, reg, val); | | 204 | return (*un->un_ops->uno_read_reg)(un, phy, reg, val); |
205 | } | | 205 | } |
206 | | | 206 | |
207 | static int | | 207 | static int |
208 | uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) | | 208 | uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) |
209 | { | | 209 | { |
210 | usbnet_isowned_core(un); | | 210 | usbnet_isowned_core(un); |
211 | return (*un->un_ops->uno_write_reg)(un, phy, reg, val); | | 211 | return (*un->un_ops->uno_write_reg)(un, phy, reg, val); |
212 | } | | 212 | } |
213 | | | 213 | |
214 | static void | | 214 | static void |
215 | uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) | | 215 | uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) |
216 | { | | 216 | { |
217 | usbnet_isowned_core(un); | | 217 | usbnet_isowned_core(un); |
218 | (*un->un_ops->uno_statchg)(ifp); | | 218 | (*un->un_ops->uno_statchg)(ifp); |
219 | } | | 219 | } |
220 | | | 220 | |
221 | static unsigned | | 221 | static unsigned |
222 | uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) | | 222 | uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) |
223 | { | | 223 | { |
224 | usbnet_isowned_tx(un); | | 224 | usbnet_isowned_tx(un); |
225 | return (*un->un_ops->uno_tx_prepare)(un, m, c); | | 225 | return (*un->un_ops->uno_tx_prepare)(un, m, c); |
226 | } | | 226 | } |
227 | | | 227 | |
228 | static void | | 228 | static void |
229 | uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) | | 229 | uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) |
230 | { | | 230 | { |
231 | usbnet_isowned_rx(un); | | 231 | usbnet_isowned_rx(un); |
232 | (*un->un_ops->uno_rx_loop)(un, c, total_len); | | 232 | (*un->un_ops->uno_rx_loop)(un, c, total_len); |
233 | } | | 233 | } |
234 | | | 234 | |
235 | static void | | 235 | static void |
236 | uno_tick(struct usbnet *un) | | 236 | uno_tick(struct usbnet *un) |
237 | { | | 237 | { |
238 | if (un->un_ops->uno_tick) | | 238 | if (un->un_ops->uno_tick) |
239 | (*un->un_ops->uno_tick)(un); | | 239 | (*un->un_ops->uno_tick)(un); |
240 | } | | 240 | } |
241 | | | 241 | |
242 | static void | | 242 | static void |
243 | uno_intr(struct usbnet *un, usbd_status status) | | 243 | uno_intr(struct usbnet *un, usbd_status status) |
244 | { | | 244 | { |
245 | if (un->un_ops->uno_intr) | | 245 | if (un->un_ops->uno_intr) |
246 | (*un->un_ops->uno_intr)(un, status); | | 246 | (*un->un_ops->uno_intr)(un, status); |
247 | } | | 247 | } |
248 | | | 248 | |
249 | /* Interrupt handling. */ | | 249 | /* Interrupt handling. */ |
250 | | | 250 | |
251 | static struct mbuf * | | 251 | static struct mbuf * |
252 | usbnet_newbuf(size_t buflen) | | 252 | usbnet_newbuf(size_t buflen) |
253 | { | | 253 | { |
254 | struct mbuf *m; | | 254 | struct mbuf *m; |
255 | | | 255 | |
256 | if (buflen > MCLBYTES) | | 256 | if (buflen > MCLBYTES) |
257 | return NULL; | | 257 | return NULL; |
258 | | | 258 | |
259 | MGETHDR(m, M_DONTWAIT, MT_DATA); | | 259 | MGETHDR(m, M_DONTWAIT, MT_DATA); |
260 | if (m == NULL) | | 260 | if (m == NULL) |
261 | return NULL; | | 261 | return NULL; |
262 | | | 262 | |
263 | if (buflen > MHLEN - ETHER_ALIGN) { | | 263 | if (buflen > MHLEN - ETHER_ALIGN) { |
264 | MCLGET(m, M_DONTWAIT); | | 264 | MCLGET(m, M_DONTWAIT); |
265 | if (!(m->m_flags & M_EXT)) { | | 265 | if (!(m->m_flags & M_EXT)) { |
266 | m_freem(m); | | 266 | m_freem(m); |
267 | return NULL; | | 267 | return NULL; |
268 | } | | 268 | } |
269 | } | | 269 | } |
270 | | | 270 | |
271 | m_adj(m, ETHER_ALIGN); | | 271 | m_adj(m, ETHER_ALIGN); |
272 | m->m_len = m->m_pkthdr.len = buflen; | | 272 | m->m_len = m->m_pkthdr.len = buflen; |
273 | | | 273 | |
274 | return m; | | 274 | return m; |
275 | } | | 275 | } |
276 | | | 276 | |
277 | /* | | 277 | /* |
278 | * usbnet_rxeof() is designed to be the done callback for rx completion. | | 278 | * usbnet_rxeof() is designed to be the done callback for rx completion. |
279 | * it provides generic setup and finalisation, calls a different usbnet | | 279 | * it provides generic setup and finalisation, calls a different usbnet |
280 | * rx_loop callback in the middle, which can use usbnet_enqueue() to | | 280 | * rx_loop callback in the middle, which can use usbnet_enqueue() to |
281 | * enqueue a packet for higher levels (or usbnet_input() if previously | | 281 | * enqueue a packet for higher levels (or usbnet_input() if previously |
282 | * using if_input() path.) | | 282 | * using if_input() path.) |
283 | */ | | 283 | */ |
284 | void | | 284 | void |
285 | usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, | | 285 | usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, |
286 | int csum_flags, uint32_t csum_data, int mbuf_flags) | | 286 | int csum_flags, uint32_t csum_data, int mbuf_flags) |
287 | { | | 287 | { |
288 | USBNETHIST_FUNC(); | | 288 | USBNETHIST_FUNC(); |
289 | struct ifnet * const ifp = usbnet_ifp(un); | | 289 | struct ifnet * const ifp = usbnet_ifp(un); |
290 | struct usbnet_private * const unp __unused = un->un_pri; | | 290 | struct usbnet_private * const unp __unused = un->un_pri; |
291 | struct mbuf *m; | | 291 | struct mbuf *m; |
292 | | | 292 | |
293 | USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx", | | 293 | USBNETHIST_CALLARGSN(5, "%jd: enter: len=%ju csf %#jx mbf %#jx", |
294 | unp->unp_number, buflen, csum_flags, mbuf_flags); | | 294 | unp->unp_number, buflen, csum_flags, mbuf_flags); |
295 | | | 295 | |
296 | usbnet_isowned_rx(un); | | 296 | usbnet_isowned_rx(un); |
297 | | | 297 | |
298 | m = usbnet_newbuf(buflen); | | 298 | m = usbnet_newbuf(buflen); |
299 | if (m == NULL) { | | 299 | if (m == NULL) { |
300 | DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0); | | 300 | DPRINTF("%jd: no memory", unp->unp_number, 0, 0, 0); |
301 | if_statinc(ifp, if_ierrors); | | 301 | if_statinc(ifp, if_ierrors); |
302 | return; | | 302 | return; |
303 | } | | 303 | } |
304 | | | 304 | |
305 | m_set_rcvif(m, ifp); | | 305 | m_set_rcvif(m, ifp); |
306 | m->m_pkthdr.csum_flags = csum_flags; | | 306 | m->m_pkthdr.csum_flags = csum_flags; |
307 | m->m_pkthdr.csum_data = csum_data; | | 307 | m->m_pkthdr.csum_data = csum_data; |
308 | m->m_flags |= mbuf_flags; | | 308 | m->m_flags |= mbuf_flags; |
309 | memcpy(mtod(m, uint8_t *), buf, buflen); | | 309 | memcpy(mtod(m, uint8_t *), buf, buflen); |
310 | | | 310 | |
311 | /* push the packet up */ | | 311 | /* push the packet up */ |
312 | if_percpuq_enqueue(ifp->if_percpuq, m); | | 312 | if_percpuq_enqueue(ifp->if_percpuq, m); |
313 | } | | 313 | } |
314 | | | 314 | |
315 | void | | 315 | void |
316 | usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) | | 316 | usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) |
317 | { | | 317 | { |
318 | USBNETHIST_FUNC(); | | 318 | USBNETHIST_FUNC(); |
319 | struct ifnet * const ifp = usbnet_ifp(un); | | 319 | struct ifnet * const ifp = usbnet_ifp(un); |
320 | struct usbnet_private * const unp __unused = un->un_pri; | | 320 | struct usbnet_private * const unp __unused = un->un_pri; |
321 | struct mbuf *m; | | 321 | struct mbuf *m; |
322 | | | 322 | |
323 | USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju", | | 323 | USBNETHIST_CALLARGSN(5, "%jd: enter: buf %#jx len %ju", |
324 | unp->unp_number, (uintptr_t)buf, buflen, 0); | | 324 | unp->unp_number, (uintptr_t)buf, buflen, 0); |
325 | | | 325 | |
326 | usbnet_isowned_rx(un); | | 326 | usbnet_isowned_rx(un); |
327 | | | 327 | |
328 | m = usbnet_newbuf(buflen); | | 328 | m = usbnet_newbuf(buflen); |
329 | if (m == NULL) { | | 329 | if (m == NULL) { |
330 | if_statinc(ifp, if_ierrors); | | 330 | if_statinc(ifp, if_ierrors); |
331 | return; | | 331 | return; |
332 | } | | 332 | } |
333 | | | 333 | |
334 | m_set_rcvif(m, ifp); | | 334 | m_set_rcvif(m, ifp); |
335 | memcpy(mtod(m, char *), buf, buflen); | | 335 | memcpy(mtod(m, char *), buf, buflen); |
336 | | | 336 | |
337 | /* push the packet up */ | | 337 | /* push the packet up */ |
338 | if_input(ifp, m); | | 338 | if_input(ifp, m); |
339 | } | | 339 | } |
340 | | | 340 | |
341 | /* | | 341 | /* |
342 | * A frame has been uploaded: pass the resulting mbuf chain up to | | 342 | * A frame has been uploaded: pass the resulting mbuf chain up to |
343 | * the higher level protocols. | | 343 | * the higher level protocols. |
344 | */ | | 344 | */ |
345 | static void | | 345 | static void |
346 | usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) | | 346 | usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) |
347 | { | | 347 | { |
348 | USBNETHIST_FUNC(); | | 348 | USBNETHIST_FUNC(); |
349 | struct usbnet_chain * const c = priv; | | 349 | struct usbnet_chain * const c = priv; |
350 | struct usbnet * const un = c->unc_un; | | 350 | struct usbnet * const un = c->unc_un; |
351 | struct usbnet_private * const unp = un->un_pri; | | 351 | struct usbnet_private * const unp = un->un_pri; |
352 | uint32_t total_len; | | 352 | uint32_t total_len; |
353 | | | 353 | |
354 | USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", | | 354 | USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", |
355 | unp->unp_number, status, (uintptr_t)xfer, 0); | | 355 | unp->unp_number, status, (uintptr_t)xfer, 0); |
356 | | | 356 | |
357 | mutex_enter(&unp->unp_rxlock); | | 357 | mutex_enter(&unp->unp_rxlock); |
358 | | | 358 | |
359 | if (usbnet_isdying(un) || unp->unp_stopping || | | 359 | if (usbnet_isdying(un) || unp->unp_stopping || |
360 | status == USBD_INVAL || status == USBD_NOT_STARTED || | | 360 | status == USBD_INVAL || status == USBD_NOT_STARTED || |
361 | status == USBD_CANCELLED) | | 361 | status == USBD_CANCELLED) |
362 | goto out; | | 362 | goto out; |
363 | | | 363 | |
364 | if (status != USBD_NORMAL_COMPLETION) { | | 364 | if (status != USBD_NORMAL_COMPLETION) { |
365 | if (usbd_ratecheck(&unp->unp_rx_notice)) | | 365 | if (usbd_ratecheck(&unp->unp_rx_notice)) |
366 | device_printf(un->un_dev, "usb errors on rx: %s\n", | | 366 | device_printf(un->un_dev, "usb errors on rx: %s\n", |
367 | usbd_errstr(status)); | | 367 | usbd_errstr(status)); |
368 | if (status == USBD_STALLED) | | 368 | if (status == USBD_STALLED) |
369 | usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); | | 369 | usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); |
370 | goto done; | | 370 | goto done; |
371 | } | | 371 | } |
372 | | | 372 | |
373 | usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); | | 373 | usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); |
374 | | | 374 | |
375 | if (total_len > un->un_rx_bufsz) { | | 375 | if (total_len > un->un_rx_bufsz) { |
376 | aprint_error_dev(un->un_dev, | | 376 | aprint_error_dev(un->un_dev, |
377 | "rxeof: too large transfer (%u > %u)\n", | | 377 | "rxeof: too large transfer (%u > %u)\n", |
378 | total_len, un->un_rx_bufsz); | | 378 | total_len, un->un_rx_bufsz); |
379 | goto done; | | 379 | goto done; |
380 | } | | 380 | } |
381 | | | 381 | |
382 | uno_rx_loop(un, c, total_len); | | 382 | uno_rx_loop(un, c, total_len); |
383 | usbnet_isowned_rx(un); | | 383 | usbnet_isowned_rx(un); |
384 | | | 384 | |
385 | done: | | 385 | done: |
386 | if (usbnet_isdying(un) || unp->unp_stopping) | | 386 | if (usbnet_isdying(un) || unp->unp_stopping) |
387 | goto out; | | 387 | goto out; |
388 | | | 388 | |
389 | mutex_exit(&unp->unp_rxlock); | | 389 | mutex_exit(&unp->unp_rxlock); |
390 | | | 390 | |
391 | /* Setup new transfer. */ | | 391 | /* Setup new transfer. */ |
392 | usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, | | 392 | usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, |
393 | un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); | | 393 | un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); |
394 | usbd_transfer(xfer); | | 394 | usbd_transfer(xfer); |
395 | return; | | 395 | return; |
396 | | | 396 | |
397 | out: | | 397 | out: |
398 | mutex_exit(&unp->unp_rxlock); | | 398 | mutex_exit(&unp->unp_rxlock); |
399 | } | | 399 | } |
400 | | | 400 | |
401 | static void | | 401 | static void |
402 | usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) | | 402 | usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) |
403 | { | | 403 | { |
404 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 404 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
405 | struct usbnet_chain * const c = priv; | | 405 | struct usbnet_chain * const c = priv; |
406 | struct usbnet * const un = c->unc_un; | | 406 | struct usbnet * const un = c->unc_un; |
407 | struct usbnet_cdata * const cd = un_cdata(un); | | 407 | struct usbnet_cdata * const cd = un_cdata(un); |
408 | struct usbnet_private * const unp = un->un_pri; | | 408 | struct usbnet_private * const unp = un->un_pri; |
409 | struct ifnet * const ifp = usbnet_ifp(un); | | 409 | struct ifnet * const ifp = usbnet_ifp(un); |
410 | | | 410 | |
411 | USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", | | 411 | USBNETHIST_CALLARGSN(5, "%jd: enter: status %#jx xfer %#jx", |
412 | unp->unp_number, status, (uintptr_t)xfer, 0); | | 412 | unp->unp_number, status, (uintptr_t)xfer, 0); |
413 | | | 413 | |
414 | mutex_enter(&unp->unp_txlock); | | 414 | mutex_enter(&unp->unp_txlock); |
415 | if (unp->unp_stopping || usbnet_isdying(un)) { | | 415 | if (unp->unp_stopping || usbnet_isdying(un)) { |
416 | mutex_exit(&unp->unp_txlock); | | 416 | mutex_exit(&unp->unp_txlock); |
417 | return; | | 417 | return; |
418 | } | | 418 | } |
419 | | | 419 | |
420 | KASSERT(cd->uncd_tx_cnt > 0); | | 420 | KASSERT(cd->uncd_tx_cnt > 0); |
421 | cd->uncd_tx_cnt--; | | 421 | cd->uncd_tx_cnt--; |
422 | | | 422 | |
423 | unp->unp_timer = 0; | | 423 | unp->unp_timer = 0; |
424 | | | 424 | |
425 | switch (status) { | | 425 | switch (status) { |
426 | case USBD_NOT_STARTED: | | 426 | case USBD_NOT_STARTED: |
427 | case USBD_CANCELLED: | | 427 | case USBD_CANCELLED: |
428 | break; | | 428 | break; |
429 | | | 429 | |
430 | case USBD_NORMAL_COMPLETION: | | 430 | case USBD_NORMAL_COMPLETION: |
431 | if_statinc(ifp, if_opackets); | | 431 | if_statinc(ifp, if_opackets); |
432 | break; | | 432 | break; |
433 | | | 433 | |
434 | default: | | 434 | default: |
435 | | | 435 | |
436 | if_statinc(ifp, if_oerrors); | | 436 | if_statinc(ifp, if_oerrors); |
437 | if (usbd_ratecheck(&unp->unp_tx_notice)) | | 437 | if (usbd_ratecheck(&unp->unp_tx_notice)) |
438 | device_printf(un->un_dev, "usb error on tx: %s\n", | | 438 | device_printf(un->un_dev, "usb error on tx: %s\n", |
439 | usbd_errstr(status)); | | 439 | usbd_errstr(status)); |
440 | if (status == USBD_STALLED) | | 440 | if (status == USBD_STALLED) |
441 | usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); | | 441 | usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); |
442 | break; | | 442 | break; |
443 | } | | 443 | } |
444 | | | 444 | |
445 | mutex_exit(&unp->unp_txlock); | | 445 | mutex_exit(&unp->unp_txlock); |
446 | | | 446 | |
447 | if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) | | 447 | if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) |
448 | (*ifp->if_start)(ifp); | | 448 | (*ifp->if_start)(ifp); |
449 | } | | 449 | } |
450 | | | 450 | |
451 | static void | | 451 | static void |
452 | usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) | | 452 | usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) |
453 | { | | 453 | { |
454 | USBNETHIST_FUNC(); | | 454 | USBNETHIST_FUNC(); |
455 | struct usbnet * const un = priv; | | 455 | struct usbnet * const un = priv; |
456 | struct usbnet_private * const unp = un->un_pri; | | 456 | struct usbnet_private * const unp = un->un_pri; |
457 | struct usbnet_intr * const uni = un->un_intr; | | 457 | struct usbnet_intr * const uni = un->un_intr; |
458 | | | 458 | |
459 | if (uni == NULL || usbnet_isdying(un) || unp->unp_stopping || | | 459 | if (uni == NULL || usbnet_isdying(un) || unp->unp_stopping || |
460 | status == USBD_INVAL || status == USBD_NOT_STARTED || | | 460 | status == USBD_INVAL || status == USBD_NOT_STARTED || |
461 | status == USBD_CANCELLED) { | | 461 | status == USBD_CANCELLED) { |
462 | USBNETHIST_CALLARGS("%jd: uni %#jx d/s %#jx status %#jx", | | 462 | USBNETHIST_CALLARGS("%jd: uni %#jx d/s %#jx status %#jx", |
463 | unp->unp_number, (uintptr_t)uni, | | 463 | unp->unp_number, (uintptr_t)uni, |
464 | (usbnet_isdying(un) << 8) | unp->unp_stopping, status); | | 464 | (usbnet_isdying(un) << 8) | unp->unp_stopping, status); |
465 | return; | | 465 | return; |
466 | } | | 466 | } |
467 | | | 467 | |
468 | if (status != USBD_NORMAL_COMPLETION) { | | 468 | if (status != USBD_NORMAL_COMPLETION) { |
469 | if (usbd_ratecheck(&unp->unp_intr_notice)) { | | 469 | if (usbd_ratecheck(&unp->unp_intr_notice)) { |
470 | aprint_error_dev(un->un_dev, "usb error on intr: %s\n", | | 470 | aprint_error_dev(un->un_dev, "usb error on intr: %s\n", |
471 | usbd_errstr(status)); | | 471 | usbd_errstr(status)); |
472 | } | | 472 | } |
473 | if (status == USBD_STALLED) | | 473 | if (status == USBD_STALLED) |
474 | usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); | | 474 | usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); |
475 | USBNETHIST_CALLARGS("%jd: not normal status %#jx", | | 475 | USBNETHIST_CALLARGS("%jd: not normal status %#jx", |
476 | unp->unp_number, status, 0, 0); | | 476 | unp->unp_number, status, 0, 0); |
477 | return; | | 477 | return; |
478 | } | | 478 | } |
479 | | | 479 | |
480 | uno_intr(un, status); | | 480 | uno_intr(un, status); |
481 | } | | 481 | } |
482 | | | 482 | |
483 | static void | | 483 | static void |
484 | usbnet_start_locked(struct ifnet *ifp) | | 484 | usbnet_start_locked(struct ifnet *ifp) |
485 | { | | 485 | { |
486 | USBNETHIST_FUNC(); | | 486 | USBNETHIST_FUNC(); |
487 | struct usbnet * const un = ifp->if_softc; | | 487 | struct usbnet * const un = ifp->if_softc; |
488 | struct usbnet_cdata * const cd = un_cdata(un); | | 488 | struct usbnet_cdata * const cd = un_cdata(un); |
489 | struct usbnet_private * const unp = un->un_pri; | | 489 | struct usbnet_private * const unp = un->un_pri; |
490 | struct mbuf *m; | | 490 | struct mbuf *m; |
491 | unsigned length; | | 491 | unsigned length; |
492 | bool done_transmit = false; | | 492 | bool done_transmit = false; |
493 | int idx, count; | | 493 | int idx, count; |
494 | | | 494 | |
495 | USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd", | | 495 | USBNETHIST_CALLARGS("%jd: tx_cnt %jd list_cnt %jd link %jd", |
496 | unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt, | | 496 | unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt, |
497 | unp->unp_link); | | 497 | unp->unp_link); |
498 | | | 498 | |
499 | usbnet_isowned_tx(un); | | 499 | usbnet_isowned_tx(un); |
500 | KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); | | 500 | KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); |
501 | | | 501 | |
502 | if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) { | | 502 | if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) { |
503 | DPRINTF("start called no link (%jx) or running (flags %jx)", | | 503 | DPRINTF("start called no link (%jx) or running (flags %jx)", |
504 | unp->unp_link, ifp->if_flags, 0, 0); | | 504 | unp->unp_link, ifp->if_flags, 0, 0); |
505 | return; | | 505 | return; |
506 | } | | 506 | } |
507 | | | 507 | |
508 | if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { | | 508 | if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { |
509 | DPRINTF("start called, tx busy (%#jx == %#jx)", | | 509 | DPRINTF("start called, tx busy (%#jx == %#jx)", |
510 | cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); | | 510 | cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); |
511 | return; | | 511 | return; |
512 | } | | 512 | } |
513 | | | 513 | |
514 | idx = cd->uncd_tx_prod; | | 514 | idx = cd->uncd_tx_prod; |
515 | count = 0; | | 515 | count = 0; |
516 | while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { | | 516 | while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { |
517 | IFQ_POLL(&ifp->if_snd, m); | | 517 | IFQ_POLL(&ifp->if_snd, m); |
518 | if (m == NULL) { | | 518 | if (m == NULL) { |
519 | DPRINTF("start called, queue empty", 0, 0, 0, 0); | | 519 | DPRINTF("start called, queue empty", 0, 0, 0, 0); |
520 | break; | | 520 | break; |
521 | } | | 521 | } |
522 | KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); | | 522 | KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); |
523 | | | 523 | |
524 | struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; | | 524 | struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; |
525 | | | 525 | |
526 | length = uno_tx_prepare(un, m, c); | | 526 | length = uno_tx_prepare(un, m, c); |
527 | if (length == 0) { | | 527 | if (length == 0) { |
528 | DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); | | 528 | DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); |
529 | if_statinc(ifp, if_oerrors); | | 529 | if_statinc(ifp, if_oerrors); |
530 | break; | | 530 | break; |
531 | } | | 531 | } |
532 | | | 532 | |
533 | if (__predict_false(c->unc_xfer == NULL)) { | | 533 | if (__predict_false(c->unc_xfer == NULL)) { |
534 | DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); | | 534 | DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); |
535 | if_statinc(ifp, if_oerrors); | | 535 | if_statinc(ifp, if_oerrors); |
536 | break; | | 536 | break; |
537 | } | | 537 | } |
538 | | | 538 | |
539 | usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, | | 539 | usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, |
540 | un->un_tx_xfer_flags, 10000, usbnet_txeof); | | 540 | un->un_tx_xfer_flags, 10000, usbnet_txeof); |
541 | | | 541 | |
542 | /* Transmit */ | | 542 | /* Transmit */ |
543 | usbd_status err = usbd_transfer(c->unc_xfer); | | 543 | usbd_status err = usbd_transfer(c->unc_xfer); |
544 | if (err != USBD_IN_PROGRESS) { | | 544 | if (err != USBD_IN_PROGRESS) { |
545 | DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd", | | 545 | DPRINTF("usbd_transfer on %#jx for %ju bytes: %jd", |
546 | (uintptr_t)c->unc_buf, length, err, 0); | | 546 | (uintptr_t)c->unc_buf, length, err, 0); |
547 | if_statinc(ifp, if_oerrors); | | 547 | if_statinc(ifp, if_oerrors); |
548 | break; | | 548 | break; |
549 | } | | 549 | } |
550 | done_transmit = true; | | 550 | done_transmit = true; |
551 | | | 551 | |
552 | IFQ_DEQUEUE(&ifp->if_snd, m); | | 552 | IFQ_DEQUEUE(&ifp->if_snd, m); |
553 | | | 553 | |
554 | /* | | 554 | /* |
555 | * If there's a BPF listener, bounce a copy of this frame | | 555 | * If there's a BPF listener, bounce a copy of this frame |
556 | * to him. | | 556 | * to him. |
557 | */ | | 557 | */ |
558 | bpf_mtap(ifp, m, BPF_D_OUT); | | 558 | bpf_mtap(ifp, m, BPF_D_OUT); |
559 | m_freem(m); | | 559 | m_freem(m); |
560 | | | 560 | |
561 | idx = (idx + 1) % un->un_tx_list_cnt; | | 561 | idx = (idx + 1) % un->un_tx_list_cnt; |
562 | cd->uncd_tx_cnt++; | | 562 | cd->uncd_tx_cnt++; |
563 | count++; | | 563 | count++; |
564 | } | | 564 | } |
565 | cd->uncd_tx_prod = idx; | | 565 | cd->uncd_tx_prod = idx; |
566 | | | 566 | |
567 | DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd", | | 567 | DPRINTF("finished with start; tx_cnt %jd list_cnt %jd link %jd", |
568 | cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0); | | 568 | cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0); |
569 | | | 569 | |
570 | /* | | 570 | /* |
571 | * Set a timeout in case the chip goes out to lunch. | | 571 | * Set a timeout in case the chip goes out to lunch. |
572 | */ | | 572 | */ |
573 | if (done_transmit) | | 573 | if (done_transmit) |
574 | unp->unp_timer = 5; | | 574 | unp->unp_timer = 5; |
575 | | | 575 | |
576 | if (count != 0) | | 576 | if (count != 0) |
577 | rnd_add_uint32(&unp->unp_rndsrc, count); | | 577 | rnd_add_uint32(&unp->unp_rndsrc, count); |
578 | } | | 578 | } |
579 | | | 579 | |
580 | static void | | 580 | static void |
581 | usbnet_if_start(struct ifnet *ifp) | | 581 | usbnet_if_start(struct ifnet *ifp) |
582 | { | | 582 | { |
583 | struct usbnet * const un = ifp->if_softc; | | 583 | struct usbnet * const un = ifp->if_softc; |
584 | struct usbnet_private * const unp = un->un_pri; | | 584 | struct usbnet_private * const unp = un->un_pri; |
585 | | | 585 | |
586 | USBNETHIST_FUNC(); | | 586 | USBNETHIST_FUNC(); |
587 | USBNETHIST_CALLARGS("%jd: stopping %jd", | | 587 | USBNETHIST_CALLARGS("%jd: stopping %jd", |
588 | unp->unp_number, unp->unp_stopping, 0, 0); | | 588 | unp->unp_number, unp->unp_stopping, 0, 0); |
589 | | | 589 | |
590 | mutex_enter(&unp->unp_txlock); | | 590 | mutex_enter(&unp->unp_txlock); |
591 | if (!unp->unp_stopping) | | 591 | if (!unp->unp_stopping) |
592 | usbnet_start_locked(ifp); | | 592 | usbnet_start_locked(ifp); |
593 | mutex_exit(&unp->unp_txlock); | | 593 | mutex_exit(&unp->unp_txlock); |
594 | } | | 594 | } |
595 | | | 595 | |
596 | /* | | 596 | /* |
597 | * Chain management. | | 597 | * Chain management. |
598 | * | | 598 | * |
599 | * RX and TX are identical. Keep them that way. | | 599 | * RX and TX are identical. Keep them that way. |
600 | */ | | 600 | */ |
601 | | | 601 | |
602 | /* Start of common RX functions */ | | 602 | /* Start of common RX functions */ |
603 | | | 603 | |
604 | static size_t | | 604 | static size_t |
605 | usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) | | 605 | usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) |
606 | { | | 606 | { |
607 | return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; | | 607 | return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; |
608 | } | | 608 | } |
609 | | | 609 | |
610 | static void | | 610 | static void |
611 | usbnet_rx_list_alloc(struct usbnet * const un) | | 611 | usbnet_rx_list_alloc(struct usbnet * const un) |
612 | { | | 612 | { |
613 | struct usbnet_cdata * const cd = un_cdata(un); | | 613 | struct usbnet_cdata * const cd = un_cdata(un); |
614 | | | 614 | |
615 | cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); | | 615 | cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); |
616 | } | | 616 | } |
617 | | | 617 | |
618 | static void | | 618 | static void |
619 | usbnet_rx_list_free(struct usbnet * const un) | | 619 | usbnet_rx_list_free(struct usbnet * const un) |
620 | { | | 620 | { |
621 | struct usbnet_cdata * const cd = un_cdata(un); | | 621 | struct usbnet_cdata * const cd = un_cdata(un); |
622 | | | 622 | |
623 | if (cd->uncd_rx_chain) { | | 623 | if (cd->uncd_rx_chain) { |
624 | kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); | | 624 | kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); |
625 | cd->uncd_rx_chain = NULL; | | 625 | cd->uncd_rx_chain = NULL; |
626 | } | | 626 | } |
627 | } | | 627 | } |
628 | | | 628 | |
629 | static int | | 629 | static int |
630 | usbnet_rx_list_init(struct usbnet * const un) | | 630 | usbnet_rx_list_init(struct usbnet * const un) |
631 | { | | 631 | { |
632 | struct usbnet_cdata * const cd = un_cdata(un); | | 632 | struct usbnet_cdata * const cd = un_cdata(un); |
633 | struct usbnet_private * const unp = un->un_pri; | | 633 | struct usbnet_private * const unp = un->un_pri; |
634 | | | 634 | |
635 | for (size_t i = 0; i < un->un_rx_list_cnt; i++) { | | 635 | for (size_t i = 0; i < un->un_rx_list_cnt; i++) { |
636 | struct usbnet_chain *c = &cd->uncd_rx_chain[i]; | | 636 | struct usbnet_chain *c = &cd->uncd_rx_chain[i]; |
637 | | | 637 | |
638 | c->unc_un = un; | | 638 | c->unc_un = un; |
639 | if (c->unc_xfer == NULL) { | | 639 | if (c->unc_xfer == NULL) { |
640 | int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], | | 640 | int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], |
641 | un->un_rx_bufsz, un->un_rx_xfer_flags, 0, | | 641 | un->un_rx_bufsz, un->un_rx_xfer_flags, 0, |
642 | &c->unc_xfer); | | 642 | &c->unc_xfer); |
643 | if (err) | | 643 | if (err) |
644 | return err; | | 644 | return err; |
645 | c->unc_buf = usbd_get_buffer(c->unc_xfer); | | 645 | c->unc_buf = usbd_get_buffer(c->unc_xfer); |
646 | } | | 646 | } |
647 | } | | 647 | } |
648 | | | 648 | |
649 | return 0; | | 649 | return 0; |
650 | } | | 650 | } |
651 | | | 651 | |
652 | static void | | 652 | static void |
653 | usbnet_rx_list_fini(struct usbnet * const un) | | 653 | usbnet_rx_list_fini(struct usbnet * const un) |
654 | { | | 654 | { |
655 | struct usbnet_cdata * const cd = un_cdata(un); | | 655 | struct usbnet_cdata * const cd = un_cdata(un); |
656 | | | 656 | |
657 | for (size_t i = 0; i < un->un_rx_list_cnt; i++) { | | 657 | for (size_t i = 0; i < un->un_rx_list_cnt; i++) { |
658 | struct usbnet_chain *c = &cd->uncd_rx_chain[i]; | | 658 | struct usbnet_chain *c = &cd->uncd_rx_chain[i]; |
659 | | | 659 | |
660 | if (c->unc_xfer != NULL) { | | 660 | if (c->unc_xfer != NULL) { |
661 | usbd_destroy_xfer(c->unc_xfer); | | 661 | usbd_destroy_xfer(c->unc_xfer); |
662 | c->unc_xfer = NULL; | | 662 | c->unc_xfer = NULL; |
663 | c->unc_buf = NULL; | | 663 | c->unc_buf = NULL; |
664 | } | | 664 | } |
665 | } | | 665 | } |
666 | } | | 666 | } |
667 | | | 667 | |
668 | /* End of common RX functions */ | | 668 | /* End of common RX functions */ |
669 | | | 669 | |
670 | static void | | 670 | static void |
671 | usbnet_rx_start_pipes(struct usbnet * const un) | | 671 | usbnet_rx_start_pipes(struct usbnet * const un) |
672 | { | | 672 | { |
673 | struct usbnet_cdata * const cd = un_cdata(un); | | 673 | struct usbnet_cdata * const cd = un_cdata(un); |
674 | struct usbnet_private * const unp = un->un_pri; | | 674 | struct usbnet_private * const unp = un->un_pri; |
675 | | | 675 | |
676 | mutex_enter(&unp->unp_rxlock); | | 676 | mutex_enter(&unp->unp_rxlock); |
677 | mutex_enter(&unp->unp_txlock); | | 677 | mutex_enter(&unp->unp_txlock); |
678 | unp->unp_stopping = false; | | 678 | unp->unp_stopping = false; |
679 | | | 679 | |
680 | for (size_t i = 0; i < un->un_rx_list_cnt; i++) { | | 680 | for (size_t i = 0; i < un->un_rx_list_cnt; i++) { |
681 | struct usbnet_chain *c = &cd->uncd_rx_chain[i]; | | 681 | struct usbnet_chain *c = &cd->uncd_rx_chain[i]; |
682 | | | 682 | |
683 | usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, | | 683 | usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, |
684 | un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); | | 684 | un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); |
685 | usbd_transfer(c->unc_xfer); | | 685 | usbd_transfer(c->unc_xfer); |
686 | } | | 686 | } |
687 | | | 687 | |
688 | mutex_exit(&unp->unp_txlock); | | 688 | mutex_exit(&unp->unp_txlock); |
689 | mutex_exit(&unp->unp_rxlock); | | 689 | mutex_exit(&unp->unp_rxlock); |
690 | } | | 690 | } |
691 | | | 691 | |
692 | /* Start of common TX functions */ | | 692 | /* Start of common TX functions */ |
693 | | | 693 | |
694 | static size_t | | 694 | static size_t |
695 | usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) | | 695 | usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) |
696 | { | | 696 | { |
697 | return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; | | 697 | return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; |
698 | } | | 698 | } |
699 | | | 699 | |
700 | static void | | 700 | static void |
701 | usbnet_tx_list_alloc(struct usbnet * const un) | | 701 | usbnet_tx_list_alloc(struct usbnet * const un) |
702 | { | | 702 | { |
703 | struct usbnet_cdata * const cd = un_cdata(un); | | 703 | struct usbnet_cdata * const cd = un_cdata(un); |
704 | | | 704 | |
705 | cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); | | 705 | cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); |
706 | } | | 706 | } |
707 | | | 707 | |
708 | static void | | 708 | static void |
709 | usbnet_tx_list_free(struct usbnet * const un) | | 709 | usbnet_tx_list_free(struct usbnet * const un) |
710 | { | | 710 | { |
711 | struct usbnet_cdata * const cd = un_cdata(un); | | 711 | struct usbnet_cdata * const cd = un_cdata(un); |
712 | | | 712 | |
713 | if (cd->uncd_tx_chain) { | | 713 | if (cd->uncd_tx_chain) { |
714 | kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); | | 714 | kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); |
715 | cd->uncd_tx_chain = NULL; | | 715 | cd->uncd_tx_chain = NULL; |
716 | } | | 716 | } |
717 | } | | 717 | } |
718 | | | 718 | |
719 | static int | | 719 | static int |
720 | usbnet_tx_list_init(struct usbnet * const un) | | 720 | usbnet_tx_list_init(struct usbnet * const un) |
721 | { | | 721 | { |
722 | struct usbnet_cdata * const cd = un_cdata(un); | | 722 | struct usbnet_cdata * const cd = un_cdata(un); |
723 | struct usbnet_private * const unp = un->un_pri; | | 723 | struct usbnet_private * const unp = un->un_pri; |
724 | | | 724 | |
725 | for (size_t i = 0; i < un->un_tx_list_cnt; i++) { | | 725 | for (size_t i = 0; i < un->un_tx_list_cnt; i++) { |
726 | struct usbnet_chain *c = &cd->uncd_tx_chain[i]; | | 726 | struct usbnet_chain *c = &cd->uncd_tx_chain[i]; |
727 | | | 727 | |
728 | c->unc_un = un; | | 728 | c->unc_un = un; |
729 | if (c->unc_xfer == NULL) { | | 729 | if (c->unc_xfer == NULL) { |
730 | int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], | | 730 | int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], |
731 | un->un_tx_bufsz, un->un_tx_xfer_flags, 0, | | 731 | un->un_tx_bufsz, un->un_tx_xfer_flags, 0, |
732 | &c->unc_xfer); | | 732 | &c->unc_xfer); |
733 | if (err) | | 733 | if (err) |
734 | return err; | | 734 | return err; |
735 | c->unc_buf = usbd_get_buffer(c->unc_xfer); | | 735 | c->unc_buf = usbd_get_buffer(c->unc_xfer); |
736 | } | | 736 | } |
737 | } | | 737 | } |
738 | | | 738 | |
739 | return 0; | | 739 | return 0; |
740 | } | | 740 | } |
741 | | | 741 | |
742 | static void | | 742 | static void |
743 | usbnet_tx_list_fini(struct usbnet * const un) | | 743 | usbnet_tx_list_fini(struct usbnet * const un) |
744 | { | | 744 | { |
745 | struct usbnet_cdata * const cd = un_cdata(un); | | 745 | struct usbnet_cdata * const cd = un_cdata(un); |
746 | | | 746 | |
747 | for (size_t i = 0; i < un->un_tx_list_cnt; i++) { | | 747 | for (size_t i = 0; i < un->un_tx_list_cnt; i++) { |
748 | struct usbnet_chain *c = &cd->uncd_tx_chain[i]; | | 748 | struct usbnet_chain *c = &cd->uncd_tx_chain[i]; |
749 | | | 749 | |
750 | if (c->unc_xfer != NULL) { | | 750 | if (c->unc_xfer != NULL) { |
751 | usbd_destroy_xfer(c->unc_xfer); | | 751 | usbd_destroy_xfer(c->unc_xfer); |
752 | c->unc_xfer = NULL; | | 752 | c->unc_xfer = NULL; |
753 | c->unc_buf = NULL; | | 753 | c->unc_buf = NULL; |
754 | } | | 754 | } |
755 | } | | 755 | } |
756 | cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; | | 756 | cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; |
757 | } | | 757 | } |
758 | | | 758 | |
759 | /* End of common TX functions */ | | 759 | /* End of common TX functions */ |
760 | | | 760 | |
761 | /* Endpoint pipe management. */ | | 761 | /* Endpoint pipe management. */ |
762 | | | 762 | |
763 | static void | | 763 | static void |
764 | usbnet_ep_close_pipes(struct usbnet * const un) | | 764 | usbnet_ep_close_pipes(struct usbnet * const un) |
765 | { | | 765 | { |
766 | struct usbnet_private * const unp = un->un_pri; | | 766 | struct usbnet_private * const unp = un->un_pri; |
767 | | | 767 | |
768 | for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { | | 768 | for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { |
769 | if (unp->unp_ep[i] == NULL) | | 769 | if (unp->unp_ep[i] == NULL) |
770 | continue; | | 770 | continue; |
771 | usbd_status err = usbd_close_pipe(unp->unp_ep[i]); | | 771 | usbd_status err = usbd_close_pipe(unp->unp_ep[i]); |
772 | if (err) | | 772 | if (err) |
773 | aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i, | | 773 | aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i, |
774 | usbd_errstr(err)); | | 774 | usbd_errstr(err)); |
775 | unp->unp_ep[i] = NULL; | | 775 | unp->unp_ep[i] = NULL; |
776 | } | | 776 | } |
777 | } | | 777 | } |
778 | | | 778 | |
779 | static usbd_status | | 779 | static usbd_status |
780 | usbnet_ep_open_pipes(struct usbnet * const un) | | 780 | usbnet_ep_open_pipes(struct usbnet * const un) |
781 | { | | 781 | { |
782 | struct usbnet_intr * const uni = un->un_intr; | | 782 | struct usbnet_intr * const uni = un->un_intr; |
783 | struct usbnet_private * const unp = un->un_pri; | | 783 | struct usbnet_private * const unp = un->un_pri; |
784 | | | 784 | |
785 | for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { | | 785 | for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { |
786 | usbd_status err; | | 786 | usbd_status err; |
787 | | | 787 | |
788 | if (un->un_ed[i] == 0) | | 788 | if (un->un_ed[i] == 0) |
789 | continue; | | 789 | continue; |
790 | | | 790 | |
791 | if (i == USBNET_ENDPT_INTR && uni) { | | 791 | if (i == USBNET_ENDPT_INTR && uni) { |
792 | err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], | | 792 | err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], |
793 | USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, | | 793 | USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, |
794 | uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, | | 794 | uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, |
795 | uni->uni_interval); | | 795 | uni->uni_interval); |
796 | } else { | | 796 | } else { |
797 | err = usbd_open_pipe(un->un_iface, un->un_ed[i], | | 797 | err = usbd_open_pipe(un->un_iface, un->un_ed[i], |
798 | USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); | | 798 | USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); |
799 | } | | 799 | } |
800 | if (err) { | | 800 | if (err) { |
801 | usbnet_ep_close_pipes(un); | | 801 | usbnet_ep_close_pipes(un); |
802 | return err; | | 802 | return err; |
803 | } | | 803 | } |
804 | } | | 804 | } |
805 | | | 805 | |
806 | return USBD_NORMAL_COMPLETION; | | 806 | return USBD_NORMAL_COMPLETION; |
807 | } | | 807 | } |
808 | | | 808 | |
809 | static usbd_status | | 809 | static usbd_status |
810 | usbnet_ep_stop_pipes(struct usbnet * const un) | | 810 | usbnet_ep_stop_pipes(struct usbnet * const un) |
811 | { | | 811 | { |
812 | struct usbnet_private * const unp = un->un_pri; | | 812 | struct usbnet_private * const unp = un->un_pri; |
813 | usbd_status err = USBD_NORMAL_COMPLETION; | | 813 | usbd_status err = USBD_NORMAL_COMPLETION; |
814 | | | 814 | |
815 | for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { | | 815 | for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { |
816 | if (unp->unp_ep[i] == NULL) | | 816 | if (unp->unp_ep[i] == NULL) |
817 | continue; | | 817 | continue; |
818 | usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]); | | 818 | usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]); |
819 | if (err == USBD_NORMAL_COMPLETION && err2) | | 819 | if (err == USBD_NORMAL_COMPLETION && err2) |
820 | err = err2; | | 820 | err = err2; |
821 | } | | 821 | } |
822 | | | 822 | |
823 | return err; | | 823 | return err; |
824 | } | | 824 | } |
825 | | | 825 | |
826 | int | | 826 | int |
827 | usbnet_init_rx_tx(struct usbnet * const un) | | 827 | usbnet_init_rx_tx(struct usbnet * const un) |
828 | { | | 828 | { |
829 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 829 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
830 | struct usbnet_private * const unp = un->un_pri; | | 830 | struct usbnet_private * const unp = un->un_pri; |
831 | struct ifnet * const ifp = usbnet_ifp(un); | | 831 | struct ifnet * const ifp = usbnet_ifp(un); |
832 | usbd_status err; | | 832 | usbd_status err; |
833 | int error = 0; | | 833 | int error = 0; |
834 | | | 834 | |
835 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), | | 835 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), |
836 | "%s", ifp->if_xname); | | 836 | "%s", ifp->if_xname); |
837 | | | 837 | |
838 | usbnet_isowned_core(un); | | 838 | usbnet_isowned_core(un); |
839 | | | 839 | |
840 | if (usbnet_isdying(un)) { | | 840 | if (usbnet_isdying(un)) { |
841 | return EIO; | | 841 | return EIO; |
842 | } | | 842 | } |
843 | | | 843 | |
844 | usbnet_busy(un); | | 844 | usbnet_busy(un); |
845 | | | 845 | |
846 | /* Open RX and TX pipes. */ | | 846 | /* Open RX and TX pipes. */ |
847 | err = usbnet_ep_open_pipes(un); | | 847 | err = usbnet_ep_open_pipes(un); |
848 | if (err) { | | 848 | if (err) { |
849 | aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", | | 849 | aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", |
850 | usbd_errstr(err)); | | 850 | usbd_errstr(err)); |
851 | error = EIO; | | 851 | error = EIO; |
852 | goto out; | | 852 | goto out; |
853 | } | | 853 | } |
854 | | | 854 | |
855 | /* Init RX ring. */ | | 855 | /* Init RX ring. */ |
856 | if (usbnet_rx_list_init(un)) { | | 856 | if (usbnet_rx_list_init(un)) { |
857 | aprint_error_dev(un->un_dev, "rx list init failed\n"); | | 857 | aprint_error_dev(un->un_dev, "rx list init failed\n"); |
858 | error = ENOBUFS; | | 858 | error = ENOBUFS; |
859 | goto out; | | 859 | goto out; |
860 | } | | 860 | } |
861 | | | 861 | |
862 | /* Init TX ring. */ | | 862 | /* Init TX ring. */ |
863 | if (usbnet_tx_list_init(un)) { | | 863 | if (usbnet_tx_list_init(un)) { |
864 | aprint_error_dev(un->un_dev, "tx list init failed\n"); | | 864 | aprint_error_dev(un->un_dev, "tx list init failed\n"); |
865 | error = ENOBUFS; | | 865 | error = ENOBUFS; |
866 | goto out; | | 866 | goto out; |
867 | } | | 867 | } |
868 | | | 868 | |
869 | /* Indicate we are up and running. */ | | 869 | /* Indicate we are up and running. */ |
870 | /* XXX urndis calls usbnet_init_rx_tx before usbnet_attach_ifp. */ | | 870 | /* XXX urndis calls usbnet_init_rx_tx before usbnet_attach_ifp. */ |
871 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), | | 871 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), |
872 | "%s", ifp->if_xname); | | 872 | "%s", ifp->if_xname); |
873 | ifp->if_flags |= IFF_RUNNING; | | 873 | ifp->if_flags |= IFF_RUNNING; |
874 | | | 874 | |
875 | /* Start up the receive pipe(s). */ | | 875 | /* Start up the receive pipe(s). */ |
876 | usbnet_rx_start_pipes(un); | | 876 | usbnet_rx_start_pipes(un); |
877 | | | 877 | |
878 | callout_schedule(&unp->unp_stat_ch, hz); | | 878 | callout_schedule(&unp->unp_stat_ch, hz); |
879 | | | 879 | |
880 | out: | | 880 | out: |
881 | if (error) { | | 881 | if (error) { |
882 | usbnet_rx_list_fini(un); | | 882 | usbnet_rx_list_fini(un); |
883 | usbnet_tx_list_fini(un); | | 883 | usbnet_tx_list_fini(un); |
884 | usbnet_ep_close_pipes(un); | | 884 | usbnet_ep_close_pipes(un); |
885 | } | | 885 | } |
886 | usbnet_unbusy(un); | | 886 | usbnet_unbusy(un); |
887 | | | 887 | |
888 | usbnet_isowned_core(un); | | 888 | usbnet_isowned_core(un); |
889 | | | 889 | |
890 | return error; | | 890 | return error; |
891 | } | | 891 | } |
892 | | | 892 | |
893 | void | | 893 | void |
894 | usbnet_busy(struct usbnet *un) | | 894 | usbnet_busy(struct usbnet *un) |
895 | { | | 895 | { |
896 | struct usbnet_private * const unp = un->un_pri; | | 896 | struct usbnet_private * const unp = un->un_pri; |
897 | | | 897 | |
898 | usbnet_isowned_core(un); | | 898 | usbnet_isowned_core(un); |
899 | | | 899 | |
900 | unp->unp_refcnt++; | | 900 | unp->unp_refcnt++; |
901 | } | | 901 | } |
902 | | | 902 | |
903 | void | | 903 | void |
904 | usbnet_unbusy(struct usbnet *un) | | 904 | usbnet_unbusy(struct usbnet *un) |
905 | { | | 905 | { |
906 | struct usbnet_private * const unp = un->un_pri; | | 906 | struct usbnet_private * const unp = un->un_pri; |
907 | | | 907 | |
908 | usbnet_isowned_core(un); | | 908 | usbnet_isowned_core(un); |
909 | | | 909 | |
910 | if (--unp->unp_refcnt < 0) | | 910 | if (--unp->unp_refcnt < 0) |
911 | cv_broadcast(&unp->unp_detachcv); | | 911 | cv_broadcast(&unp->unp_detachcv); |
912 | } | | 912 | } |
913 | | | 913 | |
914 | /* MII management. */ | | 914 | /* MII management. */ |
915 | | | 915 | |
916 | int | | 916 | int |
917 | usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) | | 917 | usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) |
918 | { | | 918 | { |
919 | USBNETHIST_FUNC(); | | 919 | USBNETHIST_FUNC(); |
920 | struct usbnet * const un = device_private(dev); | | 920 | struct usbnet * const un = device_private(dev); |
921 | int err; | | 921 | int err; |
922 | | | 922 | |
923 | /* MII layer ensures core_lock is held. */ | | 923 | /* MII layer ensures core_lock is held. */ |
924 | usbnet_isowned_core(un); | | 924 | usbnet_isowned_core(un); |
925 | | | 925 | |
926 | if (usbnet_isdying(un)) { | | 926 | if (usbnet_isdying(un)) { |
927 | return EIO; | | 927 | return EIO; |
928 | } | | 928 | } |
929 | | | 929 | |
930 | usbnet_busy(un); | | 930 | usbnet_busy(un); |
931 | err = uno_read_reg(un, phy, reg, val); | | 931 | err = uno_read_reg(un, phy, reg, val); |
932 | usbnet_unbusy(un); | | 932 | usbnet_unbusy(un); |
933 | | | 933 | |
934 | if (err) { | | 934 | if (err) { |
935 | USBNETHIST_CALLARGS("%jd: read PHY failed: %jd", | | 935 | USBNETHIST_CALLARGS("%jd: read PHY failed: %jd", |
936 | un->un_pri->unp_number, err, 0, 0); | | 936 | un->un_pri->unp_number, err, 0, 0); |
937 | return err; | | 937 | return err; |
938 | } | | 938 | } |
939 | | | 939 | |
940 | return 0; | | 940 | return 0; |
941 | } | | 941 | } |
942 | | | 942 | |
943 | int | | 943 | int |
944 | usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) | | 944 | usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) |
945 | { | | 945 | { |
946 | USBNETHIST_FUNC(); | | 946 | USBNETHIST_FUNC(); |
947 | struct usbnet * const un = device_private(dev); | | 947 | struct usbnet * const un = device_private(dev); |
948 | int err; | | 948 | int err; |
949 | | | 949 | |
950 | /* MII layer ensures core_lock is held. */ | | 950 | /* MII layer ensures core_lock is held. */ |
951 | usbnet_isowned_core(un); | | 951 | usbnet_isowned_core(un); |
952 | | | 952 | |
953 | if (usbnet_isdying(un)) { | | 953 | if (usbnet_isdying(un)) { |
954 | return EIO; | | 954 | return EIO; |
955 | } | | 955 | } |
956 | | | 956 | |
957 | usbnet_busy(un); | | 957 | usbnet_busy(un); |
958 | err = uno_write_reg(un, phy, reg, val); | | 958 | err = uno_write_reg(un, phy, reg, val); |
959 | usbnet_unbusy(un); | | 959 | usbnet_unbusy(un); |
960 | | | 960 | |
961 | if (err) { | | 961 | if (err) { |
962 | USBNETHIST_CALLARGS("%jd: write PHY failed: %jd", | | 962 | USBNETHIST_CALLARGS("%jd: write PHY failed: %jd", |
963 | un->un_pri->unp_number, err, 0, 0); | | 963 | un->un_pri->unp_number, err, 0, 0); |
964 | return err; | | 964 | return err; |
965 | } | | 965 | } |
966 | | | 966 | |
967 | return 0; | | 967 | return 0; |
968 | } | | 968 | } |
969 | | | 969 | |
970 | void | | 970 | void |
971 | usbnet_mii_statchg(struct ifnet *ifp) | | 971 | usbnet_mii_statchg(struct ifnet *ifp) |
972 | { | | 972 | { |
973 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 973 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
974 | struct usbnet * const un = ifp->if_softc; | | 974 | struct usbnet * const un = ifp->if_softc; |
975 | | | 975 | |
976 | /* MII layer ensures core_lock is held. */ | | 976 | /* MII layer ensures core_lock is held. */ |
977 | usbnet_isowned_core(un); | | 977 | usbnet_isowned_core(un); |
978 | | | 978 | |
979 | usbnet_busy(un); | | 979 | usbnet_busy(un); |
980 | uno_mii_statchg(un, ifp); | | 980 | uno_mii_statchg(un, ifp); |
981 | usbnet_unbusy(un); | | 981 | usbnet_unbusy(un); |
982 | } | | 982 | } |
983 | | | 983 | |
984 | static int | | 984 | static int |
985 | usbnet_media_upd(struct ifnet *ifp) | | 985 | usbnet_media_upd(struct ifnet *ifp) |
986 | { | | 986 | { |
987 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 987 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
988 | struct usbnet * const un = ifp->if_softc; | | 988 | struct usbnet * const un = ifp->if_softc; |
989 | struct usbnet_private * const unp = un->un_pri; | | 989 | struct usbnet_private * const unp = un->un_pri; |
990 | struct mii_data * const mii = usbnet_mii(un); | | 990 | struct mii_data * const mii = usbnet_mii(un); |
991 | | | 991 | |
992 | /* ifmedia layer ensures core_lock is held. */ | | 992 | /* ifmedia layer ensures core_lock is held. */ |
993 | usbnet_isowned_core(un); | | 993 | usbnet_isowned_core(un); |
994 | | | 994 | |
995 | /* ifmedia changes only with IFNET_LOCK held. */ | | 995 | /* ifmedia changes only with IFNET_LOCK held. */ |
996 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 996 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
997 | | | 997 | |
998 | if (usbnet_isdying(un)) | | 998 | if (usbnet_isdying(un)) |
999 | return EIO; | | 999 | return EIO; |
1000 | | | 1000 | |
1001 | unp->unp_link = false; | | 1001 | unp->unp_link = false; |
1002 | | | 1002 | |
1003 | if (mii->mii_instance) { | | 1003 | if (mii->mii_instance) { |
1004 | struct mii_softc *miisc; | | 1004 | struct mii_softc *miisc; |
1005 | | | 1005 | |
1006 | LIST_FOREACH(miisc, &mii->mii_phys, mii_list) | | 1006 | LIST_FOREACH(miisc, &mii->mii_phys, mii_list) |
1007 | mii_phy_reset(miisc); | | 1007 | mii_phy_reset(miisc); |
1008 | } | | 1008 | } |
1009 | | | 1009 | |
1010 | return ether_mediachange(ifp); | | 1010 | return ether_mediachange(ifp); |
1011 | } | | 1011 | } |
1012 | | | 1012 | |
1013 | /* ioctl */ | | 1013 | /* ioctl */ |
1014 | | | 1014 | |
1015 | static int | | 1015 | static int |
1016 | usbnet_ifflags_cb(struct ethercom *ec) | | 1016 | usbnet_ifflags_cb(struct ethercom *ec) |
1017 | { | | 1017 | { |
1018 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1018 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1019 | struct ifnet *ifp = &ec->ec_if; | | 1019 | struct ifnet *ifp = &ec->ec_if; |
1020 | struct usbnet *un = ifp->if_softc; | | 1020 | struct usbnet *un = ifp->if_softc; |
1021 | struct usbnet_private * const unp = un->un_pri; | | 1021 | struct usbnet_private * const unp = un->un_pri; |
1022 | int rv = 0; | | 1022 | int rv = 0; |
1023 | | | 1023 | |
1024 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 1024 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
1025 | | | 1025 | |
1026 | mutex_enter(&unp->unp_core_lock); | | 1026 | mutex_enter(&unp->unp_core_lock); |
1027 | | | 1027 | |
1028 | const u_short changed = ifp->if_flags ^ unp->unp_if_flags; | | 1028 | const u_short changed = ifp->if_flags ^ unp->unp_if_flags; |
1029 | if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { | | 1029 | if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { |
1030 | unp->unp_if_flags = ifp->if_flags; | | 1030 | unp->unp_if_flags = ifp->if_flags; |
1031 | if ((changed & IFF_PROMISC) != 0) | | 1031 | if ((changed & IFF_PROMISC) != 0) |
1032 | rv = ENETRESET; | | 1032 | rv = ENETRESET; |
1033 | } else { | | 1033 | } else { |
1034 | rv = ENETRESET; | | 1034 | rv = ENETRESET; |
1035 | } | | 1035 | } |
1036 | | | 1036 | |
1037 | mutex_exit(&unp->unp_core_lock); | | 1037 | mutex_exit(&unp->unp_core_lock); |
1038 | | | 1038 | |
1039 | return rv; | | 1039 | return rv; |
1040 | } | | 1040 | } |
1041 | | | 1041 | |
1042 | static int | | 1042 | static int |
1043 | usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data) | | 1043 | usbnet_if_ioctl(struct ifnet *ifp, u_long cmd, void *data) |
1044 | { | | 1044 | { |
1045 | USBNETHIST_FUNC(); | | 1045 | USBNETHIST_FUNC(); |
1046 | struct usbnet * const un = ifp->if_softc; | | 1046 | struct usbnet * const un = ifp->if_softc; |
1047 | struct usbnet_private * const unp __unused = un->un_pri; | | 1047 | struct usbnet_private * const unp __unused = un->un_pri; |
1048 | int error; | | 1048 | int error; |
1049 | | | 1049 | |
1050 | USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx", | | 1050 | USBNETHIST_CALLARGSN(11, "%jd: enter %#jx data %#jx", |
1051 | unp->unp_number, cmd, (uintptr_t)data, 0); | | 1051 | unp->unp_number, cmd, (uintptr_t)data, 0); |
1052 | | | 1052 | |
1053 | if (un->un_ops->uno_override_ioctl) | | 1053 | if (un->un_ops->uno_override_ioctl) |
1054 | return uno_override_ioctl(un, ifp, cmd, data); | | 1054 | return uno_override_ioctl(un, ifp, cmd, data); |
1055 | | | 1055 | |
1056 | error = ether_ioctl(ifp, cmd, data); | | 1056 | error = ether_ioctl(ifp, cmd, data); |
1057 | if (error == ENETRESET) { | | 1057 | if (error == ENETRESET) { |
1058 | switch (cmd) { | | 1058 | switch (cmd) { |
1059 | case SIOCADDMULTI: | | 1059 | case SIOCADDMULTI: |
1060 | case SIOCDELMULTI: | | 1060 | case SIOCDELMULTI: |
1061 | usb_add_task(un->un_udev, &unp->unp_mcasttask, | | 1061 | usb_add_task(un->un_udev, &unp->unp_mcasttask, |
1062 | USB_TASKQ_DRIVER); | | 1062 | USB_TASKQ_DRIVER); |
1063 | error = 0; | | 1063 | error = 0; |
1064 | break; | | 1064 | break; |
1065 | default: | | 1065 | default: |
1066 | error = uno_ioctl(un, ifp, cmd, data); | | 1066 | error = uno_ioctl(un, ifp, cmd, data); |
1067 | } | | 1067 | } |
1068 | } | | 1068 | } |
1069 | | | 1069 | |
1070 | return error; | | 1070 | return error; |
1071 | } | | 1071 | } |
1072 | | | 1072 | |
1073 | static void | | 1073 | static void |
1074 | usbnet_mcast_task(void *arg) | | 1074 | usbnet_mcast_task(void *arg) |
1075 | { | | 1075 | { |
1076 | USBNETHIST_FUNC(); | | 1076 | USBNETHIST_FUNC(); |
1077 | struct usbnet * const un = arg; | | 1077 | struct usbnet * const un = arg; |
1078 | struct usbnet_private * const unp = un->un_pri; | | | |
1079 | struct ifnet * const ifp = usbnet_ifp(un); | | 1078 | struct ifnet * const ifp = usbnet_ifp(un); |
1080 | bool dying; | | | |
1081 | struct ifreq ifr; | | 1079 | struct ifreq ifr; |
1082 | | | 1080 | |
1083 | USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); | | 1081 | USBNETHIST_CALLARGSN(10, "%jd: enter", |
| | | 1082 | un->un_pri->unp_number, 0, 0, 0); |
1084 | | | 1083 | |
1085 | /* | | 1084 | /* |
1086 | * If we're detaching, we must check usbnet_isdying _before_ | | 1085 | * If we're detaching, we must check usbnet_isdying _before_ |
1087 | * touching IFNET_LOCK -- the ifnet may have been detached by | | 1086 | * touching IFNET_LOCK -- the ifnet may have been detached by |
1088 | * the time this task runs. This is racy -- unp_dying may be | | 1087 | * the time this task runs. This is racy -- unp_dying may be |
1089 | * set immediately after we test it -- but nevertheless safe, | | 1088 | * set immediately after we test it -- but nevertheless safe, |
1090 | * because usbnet_detach waits for the task to complete before | | 1089 | * because usbnet_detach waits for the task to complete before |
1091 | * issuing if_detach, and necessary, so that we don't touch | | 1090 | * issuing if_detach, and necessary, so that we don't touch |
1092 | * IFNET_LOCK after if_detach. See usbnet_detach for details. | | 1091 | * IFNET_LOCK after if_detach. See usbnet_detach for details. |
1093 | */ | | 1092 | */ |
1094 | mutex_enter(&unp->unp_core_lock); | | 1093 | if (usbnet_isdying(un)) |
1095 | dying = usbnet_isdying(un); | | | |
1096 | mutex_exit(&unp->unp_core_lock); | | | |
1097 | if (dying) | | | |
1098 | return; | | 1094 | return; |
1099 | | | 1095 | |
1100 | /* | | 1096 | /* |
1101 | * Pass a bogus ifr with SIOCDELMULTI -- the goal is to just | | 1097 | * Pass a bogus ifr with SIOCDELMULTI -- the goal is to just |
1102 | * notify the driver to reprogram any hardware multicast | | 1098 | * notify the driver to reprogram any hardware multicast |
1103 | * filter, according to what's already stored in the ethercom. | | 1099 | * filter, according to what's already stored in the ethercom. |
1104 | * None of the drivers actually examine this argument, so it | | 1100 | * None of the drivers actually examine this argument, so it |
1105 | * doesn't change the ABI as far as they can tell. | | 1101 | * doesn't change the ABI as far as they can tell. |
1106 | */ | | 1102 | */ |
1107 | IFNET_LOCK(ifp); | | 1103 | IFNET_LOCK(ifp); |
1108 | if (ifp->if_flags & IFF_RUNNING) { | | 1104 | if (ifp->if_flags & IFF_RUNNING) { |
1109 | memset(&ifr, 0, sizeof(ifr)); | | 1105 | memset(&ifr, 0, sizeof(ifr)); |
1110 | (void)uno_ioctl(un, ifp, SIOCDELMULTI, &ifr); | | 1106 | (void)uno_ioctl(un, ifp, SIOCDELMULTI, &ifr); |
1111 | } | | 1107 | } |
1112 | IFNET_UNLOCK(ifp); | | 1108 | IFNET_UNLOCK(ifp); |
1113 | } | | 1109 | } |
1114 | | | 1110 | |
1115 | /* | | 1111 | /* |
1116 | * Generic stop network function: | | 1112 | * Generic stop network function: |
1117 | * - mark as stopping | | 1113 | * - mark as stopping |
1118 | * - call DD routine to stop the device | | 1114 | * - call DD routine to stop the device |
1119 | * - turn off running, timer, statchg callout, link | | 1115 | * - turn off running, timer, statchg callout, link |
1120 | * - stop transfers | | 1116 | * - stop transfers |
1121 | * - free RX and TX resources | | 1117 | * - free RX and TX resources |
1122 | * - close pipes | | 1118 | * - close pipes |
1123 | * | | 1119 | * |
1124 | * usbnet_stop() is exported for drivers to use, expects lock held. | | 1120 | * usbnet_stop() is exported for drivers to use, expects lock held. |
1125 | * | | 1121 | * |
1126 | * usbnet_if_stop() is for the if_stop handler. | | 1122 | * usbnet_if_stop() is for the if_stop handler. |
1127 | */ | | 1123 | */ |
1128 | void | | 1124 | void |
1129 | usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) | | 1125 | usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) |
1130 | { | | 1126 | { |
1131 | struct usbnet_private * const unp = un->un_pri; | | 1127 | struct usbnet_private * const unp = un->un_pri; |
1132 | | | 1128 | |
1133 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1129 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1134 | | | 1130 | |
1135 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), | | 1131 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), |
1136 | "%s", ifp->if_xname); | | 1132 | "%s", ifp->if_xname); |
1137 | usbnet_isowned_core(un); | | 1133 | usbnet_isowned_core(un); |
1138 | | | 1134 | |
1139 | usbnet_busy(un); | | 1135 | usbnet_busy(un); |
1140 | | | 1136 | |
1141 | /* | | 1137 | /* |
1142 | * Prevent new activity (rescheduling ticks, xfers, &c.) and | | 1138 | * Prevent new activity (rescheduling ticks, xfers, &c.) and |
1143 | * clear the watchdog timer. | | 1139 | * clear the watchdog timer. |
1144 | */ | | 1140 | */ |
1145 | mutex_enter(&unp->unp_rxlock); | | 1141 | mutex_enter(&unp->unp_rxlock); |
1146 | mutex_enter(&unp->unp_txlock); | | 1142 | mutex_enter(&unp->unp_txlock); |
1147 | unp->unp_stopping = true; | | 1143 | unp->unp_stopping = true; |
1148 | unp->unp_timer = 0; | | 1144 | unp->unp_timer = 0; |
1149 | mutex_exit(&unp->unp_txlock); | | 1145 | mutex_exit(&unp->unp_txlock); |
1150 | mutex_exit(&unp->unp_rxlock); | | 1146 | mutex_exit(&unp->unp_rxlock); |
1151 | | | 1147 | |
1152 | /* | | 1148 | /* |
1153 | * Stop the timer first, then the task -- if the timer was | | 1149 | * Stop the timer first, then the task -- if the timer was |
1154 | * already firing, we stop the task or wait for it complete | | 1150 | * already firing, we stop the task or wait for it complete |
1155 | * only after if last fired. Setting unp_stopping prevents the | | 1151 | * only after if last fired. Setting unp_stopping prevents the |
1156 | * timer task from being scheduled again. | | 1152 | * timer task from being scheduled again. |
1157 | */ | | 1153 | */ |
1158 | callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock); | | 1154 | callout_halt(&unp->unp_stat_ch, &unp->unp_core_lock); |
1159 | usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, | | 1155 | usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, |
1160 | &unp->unp_core_lock); | | 1156 | &unp->unp_core_lock); |
1161 | | | 1157 | |
1162 | /* | | 1158 | /* |
1163 | * Now that the software is quiescent, ask the driver to stop | | 1159 | * Now that the software is quiescent, ask the driver to stop |
1164 | * the hardware. The driver's uno_stop routine now has | | 1160 | * the hardware. The driver's uno_stop routine now has |
1165 | * exclusive access to any registers that might previously have | | 1161 | * exclusive access to any registers that might previously have |
1166 | * been used by to ifmedia, mii, or ioctl callbacks. | | 1162 | * been used by to ifmedia, mii, or ioctl callbacks. |
1167 | * | | 1163 | * |
1168 | * Don't bother if the device is being detached, though -- if | | 1164 | * Don't bother if the device is being detached, though -- if |
1169 | * it's been unplugged then there's no point in trying to touch | | 1165 | * it's been unplugged then there's no point in trying to touch |
1170 | * the registers. | | 1166 | * the registers. |
1171 | */ | | 1167 | */ |
1172 | if (!usbnet_isdying(un)) | | 1168 | if (!usbnet_isdying(un)) |
1173 | uno_stop(un, ifp, disable); | | 1169 | uno_stop(un, ifp, disable); |
1174 | | | 1170 | |
1175 | /* Stop transfers. */ | | 1171 | /* Stop transfers. */ |
1176 | usbnet_ep_stop_pipes(un); | | 1172 | usbnet_ep_stop_pipes(un); |
1177 | | | 1173 | |
1178 | /* Free RX/TX resources. */ | | 1174 | /* Free RX/TX resources. */ |
1179 | usbnet_rx_list_fini(un); | | 1175 | usbnet_rx_list_fini(un); |
1180 | usbnet_tx_list_fini(un); | | 1176 | usbnet_tx_list_fini(un); |
1181 | | | 1177 | |
1182 | /* Close pipes. */ | | 1178 | /* Close pipes. */ |
1183 | usbnet_ep_close_pipes(un); | | 1179 | usbnet_ep_close_pipes(un); |
1184 | | | 1180 | |
1185 | /* Everything is quesced now. */ | | 1181 | /* Everything is quesced now. */ |
1186 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), | | 1182 | KASSERTMSG(!unp->unp_ifp_attached || IFNET_LOCKED(ifp), |
1187 | "%s", ifp->if_xname); | | 1183 | "%s", ifp->if_xname); |
1188 | ifp->if_flags &= ~IFF_RUNNING; | | 1184 | ifp->if_flags &= ~IFF_RUNNING; |
1189 | | | 1185 | |
1190 | usbnet_unbusy(un); | | 1186 | usbnet_unbusy(un); |
1191 | } | | 1187 | } |
1192 | | | 1188 | |
1193 | static void | | 1189 | static void |
1194 | usbnet_if_stop(struct ifnet *ifp, int disable) | | 1190 | usbnet_if_stop(struct ifnet *ifp, int disable) |
1195 | { | | 1191 | { |
1196 | struct usbnet * const un = ifp->if_softc; | | 1192 | struct usbnet * const un = ifp->if_softc; |
1197 | struct usbnet_private * const unp = un->un_pri; | | 1193 | struct usbnet_private * const unp = un->un_pri; |
1198 | | | 1194 | |
1199 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 1195 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
1200 | | | 1196 | |
1201 | mutex_enter(&unp->unp_core_lock); | | 1197 | mutex_enter(&unp->unp_core_lock); |
1202 | usbnet_stop(un, ifp, disable); | | 1198 | usbnet_stop(un, ifp, disable); |
1203 | mutex_exit(&unp->unp_core_lock); | | 1199 | mutex_exit(&unp->unp_core_lock); |
1204 | } | | 1200 | } |
1205 | | | 1201 | |
1206 | /* | | 1202 | /* |
1207 | * Generic tick task function. | | 1203 | * Generic tick task function. |
1208 | * | | 1204 | * |
1209 | * usbnet_tick() is triggered from a callout, and triggers a call to | | 1205 | * usbnet_tick() is triggered from a callout, and triggers a call to |
1210 | * usbnet_tick_task() from the usb_task subsystem. | | 1206 | * usbnet_tick_task() from the usb_task subsystem. |
1211 | */ | | 1207 | */ |
1212 | static void | | 1208 | static void |
1213 | usbnet_tick(void *arg) | | 1209 | usbnet_tick(void *arg) |
1214 | { | | 1210 | { |
1215 | USBNETHIST_FUNC(); | | 1211 | USBNETHIST_FUNC(); |
1216 | struct usbnet * const un = arg; | | 1212 | struct usbnet * const un = arg; |
1217 | struct usbnet_private * const unp = un->un_pri; | | 1213 | struct usbnet_private * const unp = un->un_pri; |
1218 | | | 1214 | |
1219 | USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); | | 1215 | USBNETHIST_CALLARGSN(10, "%jd: enter", unp->unp_number, 0, 0, 0); |
1220 | | | 1216 | |
1221 | /* Perform periodic stuff in process context */ | | 1217 | /* Perform periodic stuff in process context */ |
1222 | usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); | | 1218 | usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); |
1223 | } | | 1219 | } |
1224 | | | 1220 | |
1225 | static void | | 1221 | static void |
1226 | usbnet_watchdog(struct ifnet *ifp) | | 1222 | usbnet_watchdog(struct ifnet *ifp) |
1227 | { | | 1223 | { |
1228 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1224 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1229 | struct usbnet * const un = ifp->if_softc; | | 1225 | struct usbnet * const un = ifp->if_softc; |
1230 | struct usbnet_private * const unp = un->un_pri; | | 1226 | struct usbnet_private * const unp = un->un_pri; |
1231 | struct usbnet_cdata * const cd = un_cdata(un); | | 1227 | struct usbnet_cdata * const cd = un_cdata(un); |
1232 | usbd_status err; | | 1228 | usbd_status err; |
1233 | | | 1229 | |
1234 | if_statinc(ifp, if_oerrors); | | 1230 | if_statinc(ifp, if_oerrors); |
1235 | device_printf(un->un_dev, "watchdog timeout\n"); | | 1231 | device_printf(un->un_dev, "watchdog timeout\n"); |
1236 | | | 1232 | |
1237 | if (cd->uncd_tx_cnt > 0) { | | 1233 | if (cd->uncd_tx_cnt > 0) { |
1238 | DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0); | | 1234 | DPRINTF("uncd_tx_cnt=%ju non zero, aborting pipe", 0, 0, 0, 0); |
1239 | err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); | | 1235 | err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); |
1240 | if (err) | | 1236 | if (err) |
1241 | device_printf(un->un_dev, "pipe abort failed: %s\n", | | 1237 | device_printf(un->un_dev, "pipe abort failed: %s\n", |
1242 | usbd_errstr(err)); | | 1238 | usbd_errstr(err)); |
1243 | if (cd->uncd_tx_cnt != 0) | | 1239 | if (cd->uncd_tx_cnt != 0) |
1244 | DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0); | | 1240 | DPRINTF("uncd_tx_cnt now %ju", cd->uncd_tx_cnt, 0, 0, 0); |
1245 | } | | 1241 | } |
1246 | | | 1242 | |
1247 | if (!IFQ_IS_EMPTY(&ifp->if_snd)) | | 1243 | if (!IFQ_IS_EMPTY(&ifp->if_snd)) |
1248 | (*ifp->if_start)(ifp); | | 1244 | (*ifp->if_start)(ifp); |
1249 | } | | 1245 | } |
1250 | | | 1246 | |
1251 | static void | | 1247 | static void |
1252 | usbnet_tick_task(void *arg) | | 1248 | usbnet_tick_task(void *arg) |
1253 | { | | 1249 | { |
1254 | USBNETHIST_FUNC(); | | 1250 | USBNETHIST_FUNC(); |
1255 | struct usbnet * const un = arg; | | 1251 | struct usbnet * const un = arg; |
1256 | struct usbnet_private * const unp = un->un_pri; | | 1252 | struct usbnet_private * const unp = un->un_pri; |
1257 | struct ifnet * const ifp = usbnet_ifp(un); | | 1253 | struct ifnet * const ifp = usbnet_ifp(un); |
1258 | struct mii_data * const mii = usbnet_mii(un); | | 1254 | struct mii_data * const mii = usbnet_mii(un); |
1259 | | | 1255 | |
1260 | USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0); | | 1256 | USBNETHIST_CALLARGSN(8, "%jd: enter", unp->unp_number, 0, 0, 0); |
1261 | | | 1257 | |
1262 | mutex_enter(&unp->unp_txlock); | | 1258 | mutex_enter(&unp->unp_txlock); |
1263 | const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0; | | 1259 | const bool timeout = unp->unp_timer != 0 && --unp->unp_timer == 0; |
1264 | mutex_exit(&unp->unp_txlock); | | 1260 | mutex_exit(&unp->unp_txlock); |
1265 | if (timeout) | | 1261 | if (timeout) |
1266 | usbnet_watchdog(ifp); | | 1262 | usbnet_watchdog(ifp); |
1267 | | | 1263 | |
1268 | DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); | | 1264 | DPRINTFN(8, "mii %#jx ifp %#jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); |
1269 | if (mii) { | | 1265 | if (mii) { |
1270 | mutex_enter(&unp->unp_core_lock); | | 1266 | mutex_enter(&unp->unp_core_lock); |
1271 | mii_tick(mii); | | 1267 | mii_tick(mii); |
1272 | if (!unp->unp_link) | | 1268 | if (!unp->unp_link) |
1273 | (*mii->mii_statchg)(ifp); | | 1269 | (*mii->mii_statchg)(ifp); |
1274 | mutex_exit(&unp->unp_core_lock); | | 1270 | mutex_exit(&unp->unp_core_lock); |
1275 | } | | 1271 | } |
1276 | | | 1272 | |
1277 | /* Call driver if requested. */ | | 1273 | /* Call driver if requested. */ |
1278 | uno_tick(un); | | 1274 | uno_tick(un); |
1279 | | | 1275 | |
1280 | mutex_enter(&unp->unp_core_lock); | | 1276 | mutex_enter(&unp->unp_core_lock); |
1281 | if (!unp->unp_stopping && !usbnet_isdying(un)) | | 1277 | if (!unp->unp_stopping && !usbnet_isdying(un)) |
1282 | callout_schedule(&unp->unp_stat_ch, hz); | | 1278 | callout_schedule(&unp->unp_stat_ch, hz); |
1283 | mutex_exit(&unp->unp_core_lock); | | 1279 | mutex_exit(&unp->unp_core_lock); |
1284 | } | | 1280 | } |
1285 | | | 1281 | |
1286 | static int | | 1282 | static int |
1287 | usbnet_if_init(struct ifnet *ifp) | | 1283 | usbnet_if_init(struct ifnet *ifp) |
1288 | { | | 1284 | { |
1289 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1285 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1290 | struct usbnet * const un = ifp->if_softc; | | 1286 | struct usbnet * const un = ifp->if_softc; |
1291 | bool dying; | | | |
1292 | int error; | | 1287 | int error; |
1293 | | | 1288 | |
1294 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); | | 1289 | KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname); |
1295 | | | 1290 | |
1296 | /* | | 1291 | /* |
1297 | * Prevent anyone from bringing the interface back up once | | 1292 | * Prevent anyone from bringing the interface back up once |
1298 | * we're detaching. | | 1293 | * we're detaching. |
1299 | */ | | 1294 | */ |
1300 | mutex_enter(&un->un_pri->unp_core_lock); | | 1295 | if (usbnet_isdying(un)) |
1301 | dying = usbnet_isdying(un); | | | |
1302 | mutex_exit(&un->un_pri->unp_core_lock); | | | |
1303 | if (dying) | | | |
1304 | return EIO; | | 1296 | return EIO; |
1305 | | | 1297 | |
1306 | mutex_enter(&un->un_pri->unp_core_lock); | | 1298 | mutex_enter(&un->un_pri->unp_core_lock); |
1307 | error = uno_init(un, ifp); | | 1299 | error = uno_init(un, ifp); |
1308 | mutex_exit(&un->un_pri->unp_core_lock); | | 1300 | mutex_exit(&un->un_pri->unp_core_lock); |
1309 | | | 1301 | |
1310 | return error; | | 1302 | return error; |
1311 | } | | 1303 | } |
1312 | | | 1304 | |
1313 | | | 1305 | |
1314 | /* Various accessors. */ | | 1306 | /* Various accessors. */ |
1315 | | | 1307 | |
1316 | void | | 1308 | void |
1317 | usbnet_set_link(struct usbnet *un, bool link) | | 1309 | usbnet_set_link(struct usbnet *un, bool link) |
1318 | { | | 1310 | { |
1319 | un->un_pri->unp_link = link; | | 1311 | un->un_pri->unp_link = link; |
1320 | } | | 1312 | } |
1321 | | | 1313 | |
1322 | struct ifnet * | | 1314 | struct ifnet * |
1323 | usbnet_ifp(struct usbnet *un) | | 1315 | usbnet_ifp(struct usbnet *un) |
1324 | { | | 1316 | { |
1325 | return &un->un_pri->unp_ec.ec_if; | | 1317 | return &un->un_pri->unp_ec.ec_if; |
1326 | } | | 1318 | } |
1327 | | | 1319 | |
1328 | struct ethercom * | | 1320 | struct ethercom * |
1329 | usbnet_ec(struct usbnet *un) | | 1321 | usbnet_ec(struct usbnet *un) |
1330 | { | | 1322 | { |
1331 | return &un->un_pri->unp_ec; | | 1323 | return &un->un_pri->unp_ec; |
1332 | } | | 1324 | } |
1333 | | | 1325 | |
1334 | struct mii_data * | | 1326 | struct mii_data * |
1335 | usbnet_mii(struct usbnet *un) | | 1327 | usbnet_mii(struct usbnet *un) |
1336 | { | | 1328 | { |
1337 | return un->un_pri->unp_ec.ec_mii; | | 1329 | return un->un_pri->unp_ec.ec_mii; |
1338 | } | | 1330 | } |
1339 | | | 1331 | |
1340 | krndsource_t * | | 1332 | krndsource_t * |
1341 | usbnet_rndsrc(struct usbnet *un) | | 1333 | usbnet_rndsrc(struct usbnet *un) |
1342 | { | | 1334 | { |
1343 | return &un->un_pri->unp_rndsrc; | | 1335 | return &un->un_pri->unp_rndsrc; |
1344 | } | | 1336 | } |
1345 | | | 1337 | |
1346 | void * | | 1338 | void * |
1347 | usbnet_softc(struct usbnet *un) | | 1339 | usbnet_softc(struct usbnet *un) |
1348 | { | | 1340 | { |
1349 | return un->un_sc; | | 1341 | return un->un_sc; |
1350 | } | | 1342 | } |
1351 | | | 1343 | |
1352 | bool | | 1344 | bool |
1353 | usbnet_havelink(struct usbnet *un) | | 1345 | usbnet_havelink(struct usbnet *un) |
1354 | { | | 1346 | { |
1355 | return un->un_pri->unp_link; | | 1347 | return un->un_pri->unp_link; |
1356 | } | | 1348 | } |
1357 | | | 1349 | |
1358 | bool | | 1350 | bool |
1359 | usbnet_isdying(struct usbnet *un) | | 1351 | usbnet_isdying(struct usbnet *un) |
1360 | { | | 1352 | { |
1361 | return atomic_load_relaxed(&un->un_pri->unp_dying); | | 1353 | return atomic_load_relaxed(&un->un_pri->unp_dying); |
1362 | } | | 1354 | } |
1363 | | | 1355 | |
1364 | | | 1356 | |
1365 | /* Locking. */ | | 1357 | /* Locking. */ |
1366 | | | 1358 | |
1367 | void | | 1359 | void |
1368 | usbnet_lock_core(struct usbnet *un) | | 1360 | usbnet_lock_core(struct usbnet *un) |
1369 | { | | 1361 | { |
1370 | mutex_enter(&un->un_pri->unp_core_lock); | | 1362 | mutex_enter(&un->un_pri->unp_core_lock); |
1371 | } | | 1363 | } |
1372 | | | 1364 | |
1373 | void | | 1365 | void |
1374 | usbnet_unlock_core(struct usbnet *un) | | 1366 | usbnet_unlock_core(struct usbnet *un) |
1375 | { | | 1367 | { |
1376 | mutex_exit(&un->un_pri->unp_core_lock); | | 1368 | mutex_exit(&un->un_pri->unp_core_lock); |
1377 | } | | 1369 | } |
1378 | | | 1370 | |
1379 | kmutex_t * | | 1371 | kmutex_t * |
1380 | usbnet_mutex_core(struct usbnet *un) | | 1372 | usbnet_mutex_core(struct usbnet *un) |
1381 | { | | 1373 | { |
1382 | return &un->un_pri->unp_core_lock; | | 1374 | return &un->un_pri->unp_core_lock; |
1383 | } | | 1375 | } |
1384 | | | 1376 | |
1385 | void | | 1377 | void |
1386 | usbnet_lock_rx(struct usbnet *un) | | 1378 | usbnet_lock_rx(struct usbnet *un) |
1387 | { | | 1379 | { |
1388 | mutex_enter(&un->un_pri->unp_rxlock); | | 1380 | mutex_enter(&un->un_pri->unp_rxlock); |
1389 | } | | 1381 | } |
1390 | | | 1382 | |
1391 | void | | 1383 | void |
1392 | usbnet_unlock_rx(struct usbnet *un) | | 1384 | usbnet_unlock_rx(struct usbnet *un) |
1393 | { | | 1385 | { |
1394 | mutex_exit(&un->un_pri->unp_rxlock); | | 1386 | mutex_exit(&un->un_pri->unp_rxlock); |
1395 | } | | 1387 | } |
1396 | | | 1388 | |
1397 | kmutex_t * | | 1389 | kmutex_t * |
1398 | usbnet_mutex_rx(struct usbnet *un) | | 1390 | usbnet_mutex_rx(struct usbnet *un) |
1399 | { | | 1391 | { |
1400 | return &un->un_pri->unp_rxlock; | | 1392 | return &un->un_pri->unp_rxlock; |
1401 | } | | 1393 | } |
1402 | | | 1394 | |
1403 | void | | 1395 | void |
1404 | usbnet_lock_tx(struct usbnet *un) | | 1396 | usbnet_lock_tx(struct usbnet *un) |
1405 | { | | 1397 | { |
1406 | mutex_enter(&un->un_pri->unp_txlock); | | 1398 | mutex_enter(&un->un_pri->unp_txlock); |
1407 | } | | 1399 | } |
1408 | | | 1400 | |
1409 | void | | 1401 | void |
1410 | usbnet_unlock_tx(struct usbnet *un) | | 1402 | usbnet_unlock_tx(struct usbnet *un) |
1411 | { | | 1403 | { |
1412 | mutex_exit(&un->un_pri->unp_txlock); | | 1404 | mutex_exit(&un->un_pri->unp_txlock); |
1413 | } | | 1405 | } |
1414 | | | 1406 | |
1415 | kmutex_t * | | 1407 | kmutex_t * |
1416 | usbnet_mutex_tx(struct usbnet *un) | | 1408 | usbnet_mutex_tx(struct usbnet *un) |
1417 | { | | 1409 | { |
1418 | return &un->un_pri->unp_txlock; | | 1410 | return &un->un_pri->unp_txlock; |
1419 | } | | 1411 | } |
1420 | | | 1412 | |
1421 | /* Autoconf management. */ | | 1413 | /* Autoconf management. */ |
1422 | | | 1414 | |
1423 | static bool | | 1415 | static bool |
1424 | usbnet_empty_eaddr(struct usbnet * const un) | | 1416 | usbnet_empty_eaddr(struct usbnet * const un) |
1425 | { | | 1417 | { |
1426 | return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && | | 1418 | return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 && |
1427 | un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && | | 1419 | un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 && |
1428 | un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); | | 1420 | un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0); |
1429 | } | | 1421 | } |
1430 | | | 1422 | |
1431 | /* | | 1423 | /* |
1432 | * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant | | 1424 | * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant |
1433 | * 'usbnet'. The first is enough to enable device access (eg, endpoints | | 1425 | * 'usbnet'. The first is enough to enable device access (eg, endpoints |
1434 | * are connected and commands can be sent), and the second connects the | | 1426 | * are connected and commands can be sent), and the second connects the |
1435 | * device to the system networking. | | 1427 | * device to the system networking. |
1436 | * | | 1428 | * |
1437 | * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped. | | 1429 | * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped. |
1438 | * Also usable as driver detach directly. | | 1430 | * Also usable as driver detach directly. |
1439 | * | | 1431 | * |
1440 | * To skip ethernet configuration (eg, point-to-point), make sure that | | 1432 | * To skip ethernet configuration (eg, point-to-point), make sure that |
1441 | * the un_eaddr[] is fully zero. | | 1433 | * the un_eaddr[] is fully zero. |
1442 | */ | | 1434 | */ |
1443 | | | 1435 | |
1444 | void | | 1436 | void |
1445 | usbnet_attach(struct usbnet *un, | | 1437 | usbnet_attach(struct usbnet *un, |
1446 | const char *detname) /* detach cv name */ | | 1438 | const char *detname) /* detach cv name */ |
1447 | { | | 1439 | { |
1448 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1440 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1449 | | | 1441 | |
1450 | /* Required inputs. */ | | 1442 | /* Required inputs. */ |
1451 | KASSERT(un->un_ops->uno_tx_prepare); | | 1443 | KASSERT(un->un_ops->uno_tx_prepare); |
1452 | KASSERT(un->un_ops->uno_rx_loop); | | 1444 | KASSERT(un->un_ops->uno_rx_loop); |
1453 | KASSERT(un->un_ops->uno_init); | | 1445 | KASSERT(un->un_ops->uno_init); |
1454 | KASSERT(un->un_rx_bufsz); | | 1446 | KASSERT(un->un_rx_bufsz); |
1455 | KASSERT(un->un_tx_bufsz); | | 1447 | KASSERT(un->un_tx_bufsz); |
1456 | KASSERT(un->un_rx_list_cnt); | | 1448 | KASSERT(un->un_rx_list_cnt); |
1457 | KASSERT(un->un_tx_list_cnt); | | 1449 | KASSERT(un->un_tx_list_cnt); |
1458 | | | 1450 | |
1459 | /* Unfortunate fact. */ | | 1451 | /* Unfortunate fact. */ |
1460 | KASSERT(un == device_private(un->un_dev)); | | 1452 | KASSERT(un == device_private(un->un_dev)); |
1461 | | | 1453 | |
1462 | un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); | | 1454 | un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP); |
1463 | struct usbnet_private * const unp = un->un_pri; | | 1455 | struct usbnet_private * const unp = un->un_pri; |
1464 | | | 1456 | |
1465 | usb_init_task(&unp->unp_mcasttask, usbnet_mcast_task, un, | | 1457 | usb_init_task(&unp->unp_mcasttask, usbnet_mcast_task, un, |
1466 | USB_TASKQ_MPSAFE); | | 1458 | USB_TASKQ_MPSAFE); |
1467 | usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, | | 1459 | usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, |
1468 | USB_TASKQ_MPSAFE); | | 1460 | USB_TASKQ_MPSAFE); |
1469 | callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); | | 1461 | callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE); |
1470 | callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); | | 1462 | callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un); |
1471 | | | 1463 | |
1472 | mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); | | 1464 | mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB); |
1473 | mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); | | 1465 | mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB); |
1474 | mutex_init(&unp->unp_core_lock, MUTEX_DEFAULT, IPL_NONE); | | 1466 | mutex_init(&unp->unp_core_lock, MUTEX_DEFAULT, IPL_NONE); |
1475 | cv_init(&unp->unp_detachcv, detname); | | 1467 | cv_init(&unp->unp_detachcv, detname); |
1476 | | | 1468 | |
1477 | rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), | | 1469 | rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev), |
1478 | RND_TYPE_NET, RND_FLAG_DEFAULT); | | 1470 | RND_TYPE_NET, RND_FLAG_DEFAULT); |
1479 | | | 1471 | |
1480 | usbnet_rx_list_alloc(un); | | 1472 | usbnet_rx_list_alloc(un); |
1481 | usbnet_tx_list_alloc(un); | | 1473 | usbnet_tx_list_alloc(un); |
1482 | | | 1474 | |
1483 | unp->unp_number = atomic_inc_uint_nv(&usbnet_number); | | 1475 | unp->unp_number = atomic_inc_uint_nv(&usbnet_number); |
1484 | | | 1476 | |
1485 | unp->unp_attached = true; | | 1477 | unp->unp_attached = true; |
1486 | } | | 1478 | } |
1487 | | | 1479 | |
1488 | static void | | 1480 | static void |
1489 | usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) | | 1481 | usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm) |
1490 | { | | 1482 | { |
1491 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1483 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1492 | struct usbnet_private * const unp = un->un_pri; | | 1484 | struct usbnet_private * const unp = un->un_pri; |
1493 | struct mii_data * const mii = &unp->unp_mii; | | 1485 | struct mii_data * const mii = &unp->unp_mii; |
1494 | struct ifnet * const ifp = usbnet_ifp(un); | | 1486 | struct ifnet * const ifp = usbnet_ifp(un); |
1495 | | | 1487 | |
1496 | KASSERT(un->un_ops->uno_read_reg); | | 1488 | KASSERT(un->un_ops->uno_read_reg); |
1497 | KASSERT(un->un_ops->uno_write_reg); | | 1489 | KASSERT(un->un_ops->uno_write_reg); |
1498 | KASSERT(un->un_ops->uno_statchg); | | 1490 | KASSERT(un->un_ops->uno_statchg); |
1499 | | | 1491 | |
1500 | mii->mii_ifp = ifp; | | 1492 | mii->mii_ifp = ifp; |
1501 | mii->mii_readreg = usbnet_mii_readreg; | | 1493 | mii->mii_readreg = usbnet_mii_readreg; |
1502 | mii->mii_writereg = usbnet_mii_writereg; | | 1494 | mii->mii_writereg = usbnet_mii_writereg; |
1503 | mii->mii_statchg = usbnet_mii_statchg; | | 1495 | mii->mii_statchg = usbnet_mii_statchg; |
1504 | mii->mii_flags = MIIF_AUTOTSLEEP; | | 1496 | mii->mii_flags = MIIF_AUTOTSLEEP; |
1505 | | | 1497 | |
1506 | usbnet_ec(un)->ec_mii = mii; | | 1498 | usbnet_ec(un)->ec_mii = mii; |
1507 | ifmedia_init_with_lock(&mii->mii_media, 0, | | 1499 | ifmedia_init_with_lock(&mii->mii_media, 0, |
1508 | usbnet_media_upd, ether_mediastatus, usbnet_mutex_core(un)); | | 1500 | usbnet_media_upd, ether_mediastatus, usbnet_mutex_core(un)); |
1509 | mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, | | 1501 | mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc, |
1510 | unm->un_mii_offset, unm->un_mii_flags); | | 1502 | unm->un_mii_offset, unm->un_mii_flags); |
1511 | | | 1503 | |
1512 | if (LIST_FIRST(&mii->mii_phys) == NULL) { | | 1504 | if (LIST_FIRST(&mii->mii_phys) == NULL) { |
1513 | ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); | | 1505 | ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); |
1514 | ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); | | 1506 | ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); |
1515 | } else | | 1507 | } else |
1516 | ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); | | 1508 | ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); |
1517 | } | | 1509 | } |
1518 | | | 1510 | |
1519 | void | | 1511 | void |
1520 | usbnet_attach_ifp(struct usbnet *un, | | 1512 | usbnet_attach_ifp(struct usbnet *un, |
1521 | unsigned if_flags, /* additional if_flags */ | | 1513 | unsigned if_flags, /* additional if_flags */ |
1522 | unsigned if_extflags, /* additional if_extflags */ | | 1514 | unsigned if_extflags, /* additional if_extflags */ |
1523 | const struct usbnet_mii *unm) /* additional mii_attach flags */ | | 1515 | const struct usbnet_mii *unm) /* additional mii_attach flags */ |
1524 | { | | 1516 | { |
1525 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1517 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1526 | struct usbnet_private * const unp = un->un_pri; | | 1518 | struct usbnet_private * const unp = un->un_pri; |
1527 | struct ifnet * const ifp = usbnet_ifp(un); | | 1519 | struct ifnet * const ifp = usbnet_ifp(un); |
1528 | | | 1520 | |
1529 | KASSERT(unp->unp_attached); | | 1521 | KASSERT(unp->unp_attached); |
1530 | KASSERT(!unp->unp_ifp_attached); | | 1522 | KASSERT(!unp->unp_ifp_attached); |
1531 | | | 1523 | |
1532 | ifp->if_softc = un; | | 1524 | ifp->if_softc = un; |
1533 | strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); | | 1525 | strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ); |
1534 | ifp->if_flags = if_flags; | | 1526 | ifp->if_flags = if_flags; |
1535 | ifp->if_extflags = IFEF_MPSAFE | if_extflags; | | 1527 | ifp->if_extflags = IFEF_MPSAFE | if_extflags; |
1536 | ifp->if_ioctl = usbnet_if_ioctl; | | 1528 | ifp->if_ioctl = usbnet_if_ioctl; |
1537 | ifp->if_start = usbnet_if_start; | | 1529 | ifp->if_start = usbnet_if_start; |
1538 | ifp->if_init = usbnet_if_init; | | 1530 | ifp->if_init = usbnet_if_init; |
1539 | ifp->if_stop = usbnet_if_stop; | | 1531 | ifp->if_stop = usbnet_if_stop; |
1540 | | | 1532 | |
1541 | if (unm) | | 1533 | if (unm) |
1542 | usbnet_attach_mii(un, unm); | | 1534 | usbnet_attach_mii(un, unm); |
1543 | else | | 1535 | else |
1544 | unp->unp_link = true; | | 1536 | unp->unp_link = true; |
1545 | | | 1537 | |
1546 | /* Attach the interface. */ | | 1538 | /* Attach the interface. */ |
1547 | if_initialize(ifp); | | 1539 | if_initialize(ifp); |
1548 | if (ifp->_if_input == NULL) | | 1540 | if (ifp->_if_input == NULL) |
1549 | ifp->if_percpuq = if_percpuq_create(ifp); | | 1541 | ifp->if_percpuq = if_percpuq_create(ifp); |
1550 | if_register(ifp); | | 1542 | if_register(ifp); |
1551 | unp->unp_ifp_attached = true; | | 1543 | unp->unp_ifp_attached = true; |
1552 | | | 1544 | |
1553 | /* | | 1545 | /* |
1554 | * If ethernet address is all zero, skip ether_ifattach() and | | 1546 | * If ethernet address is all zero, skip ether_ifattach() and |
1555 | * instead attach bpf here.. | | 1547 | * instead attach bpf here.. |
1556 | */ | | 1548 | */ |
1557 | if (!usbnet_empty_eaddr(un)) { | | 1549 | if (!usbnet_empty_eaddr(un)) { |
1558 | ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); | | 1550 | ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb); |
1559 | aprint_normal_dev(un->un_dev, "Ethernet address %s\n", | | 1551 | aprint_normal_dev(un->un_dev, "Ethernet address %s\n", |
1560 | ether_sprintf(un->un_eaddr)); | | 1552 | ether_sprintf(un->un_eaddr)); |
1561 | ether_ifattach(ifp, un->un_eaddr); | | 1553 | ether_ifattach(ifp, un->un_eaddr); |
1562 | } else { | | 1554 | } else { |
1563 | if_alloc_sadl(ifp); | | 1555 | if_alloc_sadl(ifp); |
1564 | bpf_attach(ifp, DLT_RAW, 0); | | 1556 | bpf_attach(ifp, DLT_RAW, 0); |
1565 | } | | 1557 | } |
1566 | | | 1558 | |
1567 | /* Now ready, and attached. */ | | 1559 | /* Now ready, and attached. */ |
1568 | IFQ_SET_READY(&ifp->if_snd); | | 1560 | IFQ_SET_READY(&ifp->if_snd); |
1569 | | | 1561 | |
1570 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); | | 1562 | usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev); |
1571 | | | 1563 | |
1572 | if (!pmf_device_register(un->un_dev, NULL, NULL)) | | 1564 | if (!pmf_device_register(un->un_dev, NULL, NULL)) |
1573 | aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); | | 1565 | aprint_error_dev(un->un_dev, "couldn't establish power handler\n"); |
1574 | } | | 1566 | } |
1575 | | | 1567 | |
1576 | int | | 1568 | int |
1577 | usbnet_detach(device_t self, int flags) | | 1569 | usbnet_detach(device_t self, int flags) |
1578 | { | | 1570 | { |
1579 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1571 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1580 | struct usbnet * const un = device_private(self); | | 1572 | struct usbnet * const un = device_private(self); |
1581 | struct usbnet_private * const unp = un->un_pri; | | 1573 | struct usbnet_private * const unp = un->un_pri; |
1582 | | | 1574 | |
1583 | /* Detached before attached finished, so just bail out. */ | | 1575 | /* Detached before attached finished, so just bail out. */ |
1584 | if (unp == NULL || !unp->unp_attached) | | 1576 | if (unp == NULL || !unp->unp_attached) |
1585 | return 0; | | 1577 | return 0; |
1586 | | | 1578 | |
1587 | struct ifnet * const ifp = usbnet_ifp(un); | | 1579 | struct ifnet * const ifp = usbnet_ifp(un); |
1588 | struct mii_data * const mii = usbnet_mii(un); | | 1580 | struct mii_data * const mii = usbnet_mii(un); |
1589 | | | 1581 | |
1590 | /* | | 1582 | /* |
1591 | * Prevent new activity. After we stop the interface, it | | 1583 | * Prevent new activity. After we stop the interface, it |
1592 | * cannot be brought back up. | | 1584 | * cannot be brought back up. |
1593 | */ | | 1585 | */ |
1594 | mutex_enter(&unp->unp_core_lock); | | | |
1595 | atomic_store_relaxed(&unp->unp_dying, true); | | 1586 | atomic_store_relaxed(&unp->unp_dying, true); |
1596 | mutex_exit(&unp->unp_core_lock); | | | |
1597 | | | 1587 | |
1598 | /* | | 1588 | /* |
1599 | * If we're still running on the network, stop and wait for all | | 1589 | * If we're still running on the network, stop and wait for all |
1600 | * asynchronous activity to finish. | | 1590 | * asynchronous activity to finish. |
1601 | * | | 1591 | * |
1602 | * If usbnet_attach_ifp never ran, IFNET_LOCK won't work, but | | 1592 | * If usbnet_attach_ifp never ran, IFNET_LOCK won't work, but |
1603 | * no activity is possible, so just skip this part. | | 1593 | * no activity is possible, so just skip this part. |
1604 | */ | | 1594 | */ |
1605 | if (unp->unp_ifp_attached) { | | 1595 | if (unp->unp_ifp_attached) { |
1606 | IFNET_LOCK(ifp); | | 1596 | IFNET_LOCK(ifp); |
1607 | if (ifp->if_flags & IFF_RUNNING) { | | 1597 | if (ifp->if_flags & IFF_RUNNING) { |
1608 | usbnet_if_stop(ifp, 1); | | 1598 | usbnet_if_stop(ifp, 1); |
1609 | } | | 1599 | } |
1610 | IFNET_UNLOCK(ifp); | | 1600 | IFNET_UNLOCK(ifp); |
1611 | } | | 1601 | } |
1612 | | | 1602 | |
1613 | /* | | 1603 | /* |
1614 | * The callout and tick task can't be scheduled anew at this | | 1604 | * The callout and tick task can't be scheduled anew at this |
1615 | * point, and usbnet_if_stop has waited for them to complete. | | 1605 | * point, and usbnet_if_stop has waited for them to complete. |
1616 | */ | | 1606 | */ |
1617 | KASSERT(!callout_pending(&unp->unp_stat_ch)); | | 1607 | KASSERT(!callout_pending(&unp->unp_stat_ch)); |
1618 | KASSERT(!usb_task_pending(un->un_udev, &unp->unp_ticktask)); | | 1608 | KASSERT(!usb_task_pending(un->un_udev, &unp->unp_ticktask)); |
1619 | | | 1609 | |
1620 | usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, | | 1610 | usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, |
1621 | NULL); | | 1611 | NULL); |
1622 | | | 1612 | |
1623 | if (mii) { | | 1613 | if (mii) { |
1624 | mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); | | 1614 | mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY); |
1625 | ifmedia_fini(&mii->mii_media); | | 1615 | ifmedia_fini(&mii->mii_media); |
1626 | } | | 1616 | } |
1627 | if (unp->unp_ifp_attached) { | | 1617 | if (unp->unp_ifp_attached) { |
1628 | if (!usbnet_empty_eaddr(un)) | | 1618 | if (!usbnet_empty_eaddr(un)) |
1629 | ether_ifdetach(ifp); | | 1619 | ether_ifdetach(ifp); |
1630 | else | | 1620 | else |
1631 | bpf_detach(ifp); | | 1621 | bpf_detach(ifp); |
1632 | if_detach(ifp); | | 1622 | if_detach(ifp); |
1633 | } | | 1623 | } |
1634 | usbnet_ec(un)->ec_mii = NULL; | | 1624 | usbnet_ec(un)->ec_mii = NULL; |
1635 | | | 1625 | |
1636 | /* | | 1626 | /* |
1637 | * We have already waited for the multicast task to complete. | | 1627 | * We have already waited for the multicast task to complete. |
1638 | * Unfortunately, until if_detach, nothing has prevented it | | 1628 | * Unfortunately, until if_detach, nothing has prevented it |
1639 | * from running again -- another thread might issue if_mcast_op | | 1629 | * from running again -- another thread might issue if_mcast_op |
1640 | * between the time of our first usb_rem_task_wait and the time | | 1630 | * between the time of our first usb_rem_task_wait and the time |
1641 | * we actually get around to if_detach. | | 1631 | * we actually get around to if_detach. |
1642 | * | | 1632 | * |
1643 | * Fortunately, the first usb_rem_task_wait ensures that if the | | 1633 | * Fortunately, the first usb_rem_task_wait ensures that if the |
1644 | * task is scheduled again, it will witness our setting of | | 1634 | * task is scheduled again, it will witness our setting of |
1645 | * unp_dying to true[*]. So after that point, if the task is | | 1635 | * unp_dying to true[*]. So after that point, if the task is |
1646 | * scheduled again, it will decline to touch IFNET_LOCK and do | | 1636 | * scheduled again, it will decline to touch IFNET_LOCK and do |
1647 | * nothing. But we still need to wait for it to complete. | | 1637 | * nothing. But we still need to wait for it to complete. |
1648 | * | | 1638 | * |
1649 | * It would be nice if we could write | | 1639 | * It would be nice if we could write |
1650 | * | | 1640 | * |
1651 | * if_pleasestopissuingmcastopsthanks(ifp); | | 1641 | * if_pleasestopissuingmcastopsthanks(ifp); |
1652 | * usb_rem_task_wait(..., &unp->unp_mcasttask, ...); | | 1642 | * usb_rem_task_wait(..., &unp->unp_mcasttask, ...); |
1653 | * if_detach(ifp); | | 1643 | * if_detach(ifp); |
1654 | * | | 1644 | * |
1655 | * and then we would need only one usb_rem_task_wait. | | 1645 | * and then we would need only one usb_rem_task_wait. |
1656 | * | | 1646 | * |
1657 | * Unfortunately, there is no such operation available in | | 1647 | * Unfortunately, there is no such operation available in |
1658 | * sys/net at the moment, and it would require a bit of | | 1648 | * sys/net at the moment, and it would require a bit of |
1659 | * coordination with if_mcast_op and doifioctl probably under a | | 1649 | * coordination with if_mcast_op and doifioctl probably under a |
1660 | * new lock. So we'll use this kludge until that mechanism is | | 1650 | * new lock. So we'll use this kludge until that mechanism is |
1661 | * invented. | | 1651 | * invented. |
1662 | * | | 1652 | * |
1663 | * [*] This is not exactly a documented property of the API, | | 1653 | * [*] This is not exactly a documented property of the API, |
1664 | * but it is implied by the single lock in the task queue | | 1654 | * but it is implied by the single lock in the task queue |
1665 | * serializing changes to the task state. | | 1655 | * serializing changes to the task state. |
1666 | */ | | 1656 | */ |
1667 | usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, | | 1657 | usb_rem_task_wait(un->un_udev, &unp->unp_mcasttask, USB_TASKQ_DRIVER, |
1668 | NULL); | | 1658 | NULL); |
1669 | | | 1659 | |
1670 | mutex_enter(&unp->unp_core_lock); | | 1660 | mutex_enter(&unp->unp_core_lock); |
1671 | unp->unp_refcnt--; | | 1661 | unp->unp_refcnt--; |
1672 | if (unp->unp_refcnt >= 0) { | | 1662 | if (unp->unp_refcnt >= 0) { |
1673 | aprint_error_dev(un->un_dev, "%d stragglers\n", | | 1663 | aprint_error_dev(un->un_dev, "%d stragglers\n", |
1674 | unp->unp_refcnt + 1); | | 1664 | unp->unp_refcnt + 1); |
1675 | } | | 1665 | } |
1676 | while (unp->unp_refcnt >= 0) { | | 1666 | while (unp->unp_refcnt >= 0) { |
1677 | /* Wait for processes to go away */ | | 1667 | /* Wait for processes to go away */ |
1678 | cv_wait(&unp->unp_detachcv, &unp->unp_core_lock); | | 1668 | cv_wait(&unp->unp_detachcv, &unp->unp_core_lock); |
1679 | } | | 1669 | } |
1680 | mutex_exit(&unp->unp_core_lock); | | 1670 | mutex_exit(&unp->unp_core_lock); |
1681 | | | 1671 | |
1682 | usbnet_rx_list_free(un); | | 1672 | usbnet_rx_list_free(un); |
1683 | usbnet_tx_list_free(un); | | 1673 | usbnet_tx_list_free(un); |
1684 | | | 1674 | |
1685 | rnd_detach_source(&unp->unp_rndsrc); | | 1675 | rnd_detach_source(&unp->unp_rndsrc); |
1686 | | | 1676 | |
1687 | cv_destroy(&unp->unp_detachcv); | | 1677 | cv_destroy(&unp->unp_detachcv); |
1688 | mutex_destroy(&unp->unp_core_lock); | | 1678 | mutex_destroy(&unp->unp_core_lock); |
1689 | mutex_destroy(&unp->unp_rxlock); | | 1679 | mutex_destroy(&unp->unp_rxlock); |
1690 | mutex_destroy(&unp->unp_txlock); | | 1680 | mutex_destroy(&unp->unp_txlock); |
1691 | | | 1681 | |
1692 | callout_destroy(&unp->unp_stat_ch); | | 1682 | callout_destroy(&unp->unp_stat_ch); |
1693 | | | 1683 | |
1694 | pmf_device_deregister(un->un_dev); | | 1684 | pmf_device_deregister(un->un_dev); |
1695 | | | 1685 | |
1696 | /* | | 1686 | /* |
1697 | * Notify userland that we're going away, if we arrived in the | | 1687 | * Notify userland that we're going away, if we arrived in the |
1698 | * first place. | | 1688 | * first place. |
1699 | */ | | 1689 | */ |
1700 | if (unp->unp_ifp_attached) { | | 1690 | if (unp->unp_ifp_attached) { |
1701 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, | | 1691 | usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, |
1702 | un->un_dev); | | 1692 | un->un_dev); |
1703 | } | | 1693 | } |
1704 | | | 1694 | |
1705 | kmem_free(unp, sizeof(*unp)); | | 1695 | kmem_free(unp, sizeof(*unp)); |
1706 | un->un_pri = NULL; | | 1696 | un->un_pri = NULL; |
1707 | | | 1697 | |
1708 | return 0; | | 1698 | return 0; |
1709 | } | | 1699 | } |
1710 | | | 1700 | |
1711 | int | | 1701 | int |
1712 | usbnet_activate(device_t self, devact_t act) | | 1702 | usbnet_activate(device_t self, devact_t act) |
1713 | { | | 1703 | { |
1714 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); | | 1704 | USBNETHIST_FUNC(); USBNETHIST_CALLED(); |
1715 | struct usbnet * const un = device_private(self); | | 1705 | struct usbnet * const un = device_private(self); |
1716 | struct usbnet_private * const unp = un->un_pri; | | 1706 | struct usbnet_private * const unp = un->un_pri; |
1717 | struct ifnet * const ifp = usbnet_ifp(un); | | 1707 | struct ifnet * const ifp = usbnet_ifp(un); |
1718 | | | 1708 | |
1719 | switch (act) { | | 1709 | switch (act) { |
1720 | case DVACT_DEACTIVATE: | | 1710 | case DVACT_DEACTIVATE: |
1721 | if_deactivate(ifp); | | 1711 | if_deactivate(ifp); |
1722 | | | 1712 | |
1723 | mutex_enter(&unp->unp_core_lock); | | | |
1724 | atomic_store_relaxed(&unp->unp_dying, true); | | 1713 | atomic_store_relaxed(&unp->unp_dying, true); |
1725 | mutex_exit(&unp->unp_core_lock); | | | |
1726 | | | 1714 | |
1727 | mutex_enter(&unp->unp_rxlock); | | 1715 | mutex_enter(&unp->unp_rxlock); |
1728 | mutex_enter(&unp->unp_txlock); | | 1716 | mutex_enter(&unp->unp_txlock); |
1729 | unp->unp_stopping = true; | | 1717 | unp->unp_stopping = true; |
1730 | mutex_exit(&unp->unp_txlock); | | 1718 | mutex_exit(&unp->unp_txlock); |
1731 | mutex_exit(&unp->unp_rxlock); | | 1719 | mutex_exit(&unp->unp_rxlock); |
1732 | | | 1720 | |
1733 | return 0; | | 1721 | return 0; |
1734 | default: | | 1722 | default: |
1735 | return EOPNOTSUPP; | | 1723 | return EOPNOTSUPP; |
1736 | } | | 1724 | } |
1737 | } | | 1725 | } |
1738 | | | 1726 | |
1739 | MODULE(MODULE_CLASS_MISC, usbnet, NULL); | | 1727 | MODULE(MODULE_CLASS_MISC, usbnet, NULL); |
1740 | | | 1728 | |
1741 | static int | | 1729 | static int |
1742 | usbnet_modcmd(modcmd_t cmd, void *arg) | | 1730 | usbnet_modcmd(modcmd_t cmd, void *arg) |
1743 | { | | 1731 | { |
1744 | switch (cmd) { | | 1732 | switch (cmd) { |
1745 | case MODULE_CMD_INIT: | | 1733 | case MODULE_CMD_INIT: |
1746 | return 0; | | 1734 | return 0; |
1747 | case MODULE_CMD_FINI: | | 1735 | case MODULE_CMD_FINI: |
1748 | return 0; | | 1736 | return 0; |
1749 | case MODULE_CMD_STAT: | | 1737 | case MODULE_CMD_STAT: |
1750 | case MODULE_CMD_AUTOUNLOAD: | | 1738 | case MODULE_CMD_AUTOUNLOAD: |
1751 | default: | | 1739 | default: |
1752 | return ENOTTY; | | 1740 | return ENOTTY; |
1753 | } | | 1741 | } |
1754 | } | | 1742 | } |