Thu Mar 3 05:47:06 2022 UTC ()
usbnet: Take IFNET_LOCK around access to if_flags in usbnet_detach.

This is not stable without IFNET_LOCK.  Extraneous calls to
usbnet_stop arising from this race might be harmless, but let's
render it unnecessary to even think about that.


(riastradh)
diff -r1.46 -r1.47 src/sys/dev/usb/usbnet.c

cvs diff -r1.46 -r1.47 src/sys/dev/usb/usbnet.c (switch to unified diff)

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