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