Thu Mar 3 05:51:56 2022 UTC ()
usbnet: No need for usbnet_busy in usbnet_init_rx_tx or usbnet_stop.

These run with IFNET_LOCK held, and the interface cannot be detached
until the IFNET_LOCK is released, so there is no need to hang onto a
reference count here.


(riastradh)
diff -r1.75 -r1.76 src/sys/dev/usb/usbnet.c

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

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