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