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