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