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