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