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