usbnet.c revision 1.25 1 /* $NetBSD: usbnet.c,v 1.25 2019/08/29 09:17:51 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.25 2019/08/29 09:17:51 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 = 1;
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 struct usbnet * const un = priv;
422 struct usbnet_private * const unp = un->un_pri;
423 struct usbnet_intr * const uni = un->un_intr;
424 struct ifnet * const ifp = usbnet_ifp(un);
425
426 if (uni == NULL || unp->unp_dying || unp->unp_stopping ||
427 status == USBD_INVAL || status == USBD_NOT_STARTED ||
428 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
429 return;
430
431 if (status != USBD_NORMAL_COMPLETION) {
432 if (usbd_ratecheck(&unp->unp_intr_notice)) {
433 aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
434 usbd_errstr(status));
435 }
436 if (status == USBD_STALLED)
437 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]);
438 return;
439 }
440
441 uno_intr(un, status);
442 }
443
444 static void
445 usbnet_start_locked(struct ifnet *ifp)
446 {
447 USBNETHIST_FUNC(); USBNETHIST_CALLED();
448 struct usbnet * const un = ifp->if_softc;
449 struct usbnet_cdata * const cd = un_cdata(un);
450 struct usbnet_private * const unp = un->un_pri;
451 struct mbuf *m;
452 unsigned length;
453 int idx;
454
455 usbnet_isowned_tx(un);
456 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt);
457
458 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) {
459 DPRINTF("start called no link (%x) or running (flags %x)",
460 unp->unp_link, ifp->if_flags, 0, 0);
461 return;
462 }
463
464 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) {
465 DPRINTF("start called, tx busy (%jx == %jx)",
466 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0);
467 return;
468 }
469
470 idx = cd->uncd_tx_prod;
471 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) {
472 IFQ_POLL(&ifp->if_snd, m);
473 if (m == NULL) {
474 DPRINTF("start called, queue empty", 0, 0, 0, 0);
475 break;
476 }
477 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz);
478
479 struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
480
481 length = uno_tx_prepare(un, m, c);
482 if (length == 0) {
483 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0);
484 ifp->if_oerrors++;
485 break;
486 }
487
488 if (__predict_false(c->unc_xfer == NULL)) {
489 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0);
490 ifp->if_oerrors++;
491 break;
492 }
493
494 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
495 un->un_tx_xfer_flags, 10000, usbnet_txeof);
496
497 /* Transmit */
498 usbd_status err = usbd_transfer(c->unc_xfer);
499 if (err != USBD_IN_PROGRESS) {
500 DPRINTF("usbd_transfer on %jx for %ju bytes: %d",
501 (uintptr_t)c->unc_buf, length, err, 0);
502 ifp->if_oerrors++;
503 break;
504 }
505
506 IFQ_DEQUEUE(&ifp->if_snd, m);
507
508 /*
509 * If there's a BPF listener, bounce a copy of this frame
510 * to him.
511 */
512 bpf_mtap(ifp, m, BPF_D_OUT);
513 m_freem(m);
514
515 idx = (idx + 1) % un->un_tx_list_cnt;
516 cd->uncd_tx_cnt++;
517 }
518 cd->uncd_tx_prod = idx;
519
520 /*
521 * Set a timeout in case the chip goes out to lunch.
522 */
523 unp->unp_timer = 5;
524 }
525
526 static void
527 usbnet_start(struct ifnet *ifp)
528 {
529 struct usbnet * const un = ifp->if_softc;
530 struct usbnet_private * const unp = un->un_pri;
531
532 mutex_enter(&unp->unp_txlock);
533 if (!unp->unp_stopping)
534 usbnet_start_locked(ifp);
535 mutex_exit(&unp->unp_txlock);
536 }
537
538 /*
539 * Chain management.
540 *
541 * RX and TX are identical. Keep them that way.
542 */
543
544 /* Start of common RX functions */
545
546 static size_t
547 usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
548 {
549 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
550 }
551
552 static void
553 usbnet_rx_list_alloc(struct usbnet * const un)
554 {
555 struct usbnet_cdata * const cd = un_cdata(un);
556
557 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
558 }
559
560 static void
561 usbnet_rx_list_free(struct usbnet * const un)
562 {
563 struct usbnet_cdata * const cd = un_cdata(un);
564
565 if (cd->uncd_rx_chain) {
566 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
567 cd->uncd_rx_chain = NULL;
568 }
569 }
570
571 static int
572 usbnet_rx_list_init(struct usbnet * const un)
573 {
574 struct usbnet_cdata * const cd = un_cdata(un);
575 struct usbnet_private * const unp = un->un_pri;
576
577 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
578 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
579
580 c->unc_un = un;
581 if (c->unc_xfer == NULL) {
582 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
583 un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
584 &c->unc_xfer);
585 if (err)
586 return err;
587 c->unc_buf = usbd_get_buffer(c->unc_xfer);
588 }
589 }
590
591 return 0;
592 }
593
594 static void
595 usbnet_rx_list_fini(struct usbnet * const un)
596 {
597 struct usbnet_cdata * const cd = un_cdata(un);
598
599 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
600 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
601
602 if (c->unc_xfer != NULL) {
603 usbd_destroy_xfer(c->unc_xfer);
604 c->unc_xfer = NULL;
605 c->unc_buf = NULL;
606 }
607 }
608 }
609
610 /* End of common RX functions */
611
612 static void
613 usbnet_rx_start_pipes(struct usbnet * const un)
614 {
615 struct usbnet_cdata * const cd = un_cdata(un);
616 struct usbnet_private * const unp = un->un_pri;
617
618 mutex_enter(&unp->unp_rxlock);
619 mutex_enter(&unp->unp_txlock);
620 unp->unp_stopping = false;
621
622 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
623 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
624
625 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
626 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
627 usbd_transfer(c->unc_xfer);
628 }
629
630 mutex_exit(&unp->unp_txlock);
631 mutex_exit(&unp->unp_rxlock);
632 }
633
634 /* Start of common TX functions */
635
636 static size_t
637 usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
638 {
639 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
640 }
641
642 static void
643 usbnet_tx_list_alloc(struct usbnet * const un)
644 {
645 struct usbnet_cdata * const cd = un_cdata(un);
646
647 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
648 }
649
650 static void
651 usbnet_tx_list_free(struct usbnet * const un)
652 {
653 struct usbnet_cdata * const cd = un_cdata(un);
654
655 if (cd->uncd_tx_chain) {
656 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
657 cd->uncd_tx_chain = NULL;
658 }
659 }
660
661 static int
662 usbnet_tx_list_init(struct usbnet * const un)
663 {
664 struct usbnet_cdata * const cd = un_cdata(un);
665 struct usbnet_private * const unp = un->un_pri;
666
667 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
668 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
669
670 c->unc_un = un;
671 if (c->unc_xfer == NULL) {
672 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
673 un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
674 &c->unc_xfer);
675 if (err)
676 return err;
677 c->unc_buf = usbd_get_buffer(c->unc_xfer);
678 }
679 }
680
681 return 0;
682 }
683
684 static void
685 usbnet_tx_list_fini(struct usbnet * const un)
686 {
687 struct usbnet_cdata * const cd = un_cdata(un);
688
689 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
690 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
691
692 if (c->unc_xfer != NULL) {
693 usbd_destroy_xfer(c->unc_xfer);
694 c->unc_xfer = NULL;
695 c->unc_buf = NULL;
696 }
697 }
698 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0;
699 }
700
701 /* End of common TX functions */
702
703 /* Endpoint pipe management. */
704
705 static void
706 usbnet_ep_close_pipes(struct usbnet * const un)
707 {
708 struct usbnet_private * const unp = un->un_pri;
709
710 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
711 if (unp->unp_ep[i] == NULL)
712 continue;
713 usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
714 if (err)
715 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
716 usbd_errstr(err));
717 unp->unp_ep[i] = NULL;
718 }
719 }
720
721 static usbd_status
722 usbnet_ep_open_pipes(struct usbnet * const un)
723 {
724 struct usbnet_intr * const uni = un->un_intr;
725 struct usbnet_private * const unp = un->un_pri;
726
727 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
728 usbd_status err;
729
730 if (un->un_ed[i] == 0)
731 continue;
732
733 if (i == USBNET_ENDPT_INTR && uni) {
734 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
735 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
736 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
737 uni->uni_interval);
738 } else {
739 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
740 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
741 }
742 if (err) {
743 usbnet_ep_close_pipes(un);
744 return err;
745 }
746 }
747
748 return USBD_NORMAL_COMPLETION;
749 }
750
751 static usbd_status
752 usbnet_ep_stop_pipes(struct usbnet * const un)
753 {
754 struct usbnet_private * const unp = un->un_pri;
755 usbd_status err = USBD_NORMAL_COMPLETION;
756
757 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
758 if (unp->unp_ep[i] == NULL)
759 continue;
760 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]);
761 if (err == USBD_NORMAL_COMPLETION && err2)
762 err = err2;
763 }
764
765 return err;
766 }
767
768 int
769 usbnet_init_rx_tx(struct usbnet * const un)
770 {
771 USBNETHIST_FUNC(); USBNETHIST_CALLED();
772 struct usbnet_private * const unp = un->un_pri;
773 struct ifnet * const ifp = usbnet_ifp(un);
774 usbd_status err;
775 int error = 0;
776
777 usbnet_isowned(un);
778
779 if (unp->unp_dying) {
780 return EIO;
781 }
782 unp->unp_refcnt++;
783
784 /* Open RX and TX pipes. */
785 err = usbnet_ep_open_pipes(un);
786 if (err) {
787 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
788 usbd_errstr(err));
789 error = EIO;
790 goto out;
791 }
792
793 /* Init RX ring. */
794 if (usbnet_rx_list_init(un)) {
795 aprint_error_dev(un->un_dev, "rx list init failed\n");
796 error = ENOBUFS;
797 goto out;
798 }
799
800 /* Init TX ring. */
801 if (usbnet_tx_list_init(un)) {
802 aprint_error_dev(un->un_dev, "tx list init failed\n");
803 error = ENOBUFS;
804 goto out;
805 }
806
807 /* Start up the receive pipe(s). */
808 usbnet_rx_start_pipes(un);
809
810 /* Indicate we are up and running. */
811 #if 0
812 /* XXX if_mcast_op() can call this without ifnet locked */
813 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
814 #endif
815 ifp->if_flags |= IFF_RUNNING;
816
817 callout_schedule(&unp->unp_stat_ch, hz);
818
819 out:
820 if (error) {
821 usbnet_rx_list_fini(un);
822 usbnet_tx_list_fini(un);
823 usbnet_ep_close_pipes(un);
824 }
825 if (--unp->unp_refcnt < 0)
826 cv_broadcast(&unp->unp_detachcv);
827
828 usbnet_isowned(un);
829
830 return error;
831 }
832
833 /* MII management. */
834
835 /*
836 * Access functions for MII. Take the MII lock to call access MII regs.
837 * Two forms: usbnet (softc) lock currently held or not.
838 */
839 void
840 usbnet_lock_mii(struct usbnet *un)
841 {
842 struct usbnet_private * const unp = un->un_pri;
843
844 mutex_enter(&unp->unp_lock);
845 unp->unp_refcnt++;
846 mutex_exit(&unp->unp_lock);
847
848 mutex_enter(&unp->unp_miilock);
849 }
850
851 void
852 usbnet_lock_mii_un_locked(struct usbnet *un)
853 {
854 struct usbnet_private * const unp = un->un_pri;
855
856 usbnet_isowned(un);
857
858 unp->unp_refcnt++;
859 mutex_enter(&unp->unp_miilock);
860 }
861
862 void
863 usbnet_unlock_mii(struct usbnet *un)
864 {
865 struct usbnet_private * const unp = un->un_pri;
866
867 mutex_exit(&unp->unp_miilock);
868 mutex_enter(&unp->unp_lock);
869 if (--unp->unp_refcnt < 0)
870 cv_broadcast(&unp->unp_detachcv);
871 mutex_exit(&unp->unp_lock);
872 }
873
874 void
875 usbnet_unlock_mii_un_locked(struct usbnet *un)
876 {
877 struct usbnet_private * const unp = un->un_pri;
878
879 usbnet_isowned(un);
880
881 mutex_exit(&unp->unp_miilock);
882 if (--unp->unp_refcnt < 0)
883 cv_broadcast(&unp->unp_detachcv);
884 }
885
886 kmutex_t *
887 usbnet_mutex_mii(struct usbnet *un)
888 {
889 struct usbnet_private * const unp = un->un_pri;
890
891 return &unp->unp_miilock;
892 }
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 mutex_enter(&unp->unp_lock);
903 if (unp->unp_dying) {
904 mutex_exit(&unp->unp_lock);
905 return EIO;
906 }
907
908 usbnet_lock_mii_un_locked(un);
909 mutex_exit(&unp->unp_lock);
910 err = uno_read_reg(un, phy, reg, val);
911 usbnet_unlock_mii(un);
912
913 if (err) {
914 USBNETHIST_CALLARGS("read PHY failed: %d", err, 0, 0, 0);
915 return err;
916 }
917
918 return 0;
919 }
920
921 int
922 usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
923 {
924 USBNETHIST_FUNC();
925 struct usbnet * const un = device_private(dev);
926 struct usbnet_private * const unp = un->un_pri;
927 int err;
928
929 mutex_enter(&unp->unp_lock);
930 if (unp->unp_dying) {
931 mutex_exit(&unp->unp_lock);
932 return EIO;
933 }
934
935 usbnet_lock_mii_un_locked(un);
936 mutex_exit(&unp->unp_lock);
937 err = uno_write_reg(un, phy, reg, val);
938 usbnet_unlock_mii(un);
939
940 if (err) {
941 USBNETHIST_CALLARGS("write PHY failed: %d", err, 0, 0, 0);
942 return err;
943 }
944
945 return 0;
946 }
947
948 void
949 usbnet_mii_statchg(struct ifnet *ifp)
950 {
951 USBNETHIST_FUNC(); USBNETHIST_CALLED();
952 struct usbnet * const un = ifp->if_softc;
953
954 uno_mii_statchg(un, ifp);
955 }
956
957 static int
958 usbnet_media_upd(struct ifnet *ifp)
959 {
960 USBNETHIST_FUNC(); USBNETHIST_CALLED();
961 struct usbnet * const un = ifp->if_softc;
962 struct usbnet_private * const unp = un->un_pri;
963 struct mii_data * const mii = usbnet_mii(un);
964
965 if (unp->unp_dying)
966 return EIO;
967
968 unp->unp_link = false;
969
970 if (mii->mii_instance) {
971 struct mii_softc *miisc;
972
973 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
974 mii_phy_reset(miisc);
975 }
976
977 return ether_mediachange(ifp);
978 }
979
980 /* ioctl */
981
982 static int
983 usbnet_ifflags_cb(struct ethercom *ec)
984 {
985 USBNETHIST_FUNC(); USBNETHIST_CALLED();
986 struct ifnet *ifp = &ec->ec_if;
987 struct usbnet *un = ifp->if_softc;
988 struct usbnet_private * const unp = un->un_pri;
989 int rv = 0;
990
991 mutex_enter(&unp->unp_lock);
992
993 const int changed = ifp->if_flags ^ unp->unp_if_flags;
994 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
995 unp->unp_if_flags = ifp->if_flags;
996 if ((changed & IFF_PROMISC) != 0)
997 rv = ENETRESET;
998 } else {
999 rv = ENETRESET;
1000 }
1001
1002 mutex_exit(&unp->unp_lock);
1003
1004 return rv;
1005 }
1006
1007 static int
1008 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1009 {
1010 USBNETHIST_FUNC();
1011 struct usbnet * const un = ifp->if_softc;
1012 struct usbnet_private * const unp __unused = un->un_pri;
1013 int error;
1014
1015 USBNETHIST_CALLARGSN(11, "%d: enter %jx data %x",
1016 unp->unp_number, cmd, (uintptr_t)data, 0);
1017
1018 if (un->un_ops->uno_override_ioctl)
1019 return uno_override_ioctl(un, ifp, cmd, data);
1020
1021 error = ether_ioctl(ifp, cmd, data);
1022 if (error == ENETRESET)
1023 error = uno_ioctl(un, ifp, cmd, data);
1024
1025 return error;
1026 }
1027
1028 /*
1029 * Generic stop network function:
1030 * - mark as stopping
1031 * - call DD routine to stop the device
1032 * - turn off running, timer, statchg callout, link
1033 * - stop transfers
1034 * - free RX and TX resources
1035 * - close pipes
1036 *
1037 * usbnet_stop() is exported for drivers to use, expects lock held.
1038 *
1039 * usbnet_stop_ifp() is for the if_stop handler.
1040 */
1041 void
1042 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
1043 {
1044 struct usbnet_private * const unp = un->un_pri;
1045
1046 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1047
1048 usbnet_isowned(un);
1049
1050 mutex_enter(&unp->unp_rxlock);
1051 mutex_enter(&unp->unp_txlock);
1052 unp->unp_stopping = true;
1053 mutex_exit(&unp->unp_txlock);
1054 mutex_exit(&unp->unp_rxlock);
1055
1056 uno_stop(un, ifp, disable);
1057
1058 /*
1059 * XXXSMP Would like to
1060 * KASSERT(IFNET_LOCKED(ifp))
1061 * here but the locking order is:
1062 * ifnet -> unlock -> rxlock -> txlock
1063 * and unlock is already held.
1064 */
1065 ifp->if_flags &= ~IFF_RUNNING;
1066 unp->unp_timer = 0;
1067
1068 callout_stop(&unp->unp_stat_ch);
1069
1070 /* Stop transfers. */
1071 usbnet_ep_stop_pipes(un);
1072
1073 /* Free RX/TX resources. */
1074 usbnet_rx_list_fini(un);
1075 usbnet_tx_list_fini(un);
1076
1077 /* Close pipes. */
1078 usbnet_ep_close_pipes(un);
1079 }
1080
1081 static void
1082 usbnet_stop_ifp(struct ifnet *ifp, int disable)
1083 {
1084 struct usbnet * const un = ifp->if_softc;
1085 struct usbnet_private * const unp = un->un_pri;
1086
1087 mutex_enter(&unp->unp_lock);
1088 usbnet_stop(un, ifp, disable);
1089 mutex_exit(&unp->unp_lock);
1090 }
1091
1092 /*
1093 * Generic tick task function.
1094 *
1095 * usbnet_tick() is triggered from a callout, and triggers a call to
1096 * usbnet_tick_task() from the usb_task subsystem.
1097 */
1098 static void
1099 usbnet_tick(void *arg)
1100 {
1101 struct usbnet * const un = arg;
1102 struct usbnet_private * const unp = un->un_pri;
1103
1104 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) {
1105 /* Perform periodic stuff in process context */
1106 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
1107 }
1108 }
1109
1110 static void
1111 usbnet_watchdog(struct ifnet *ifp)
1112 {
1113 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1114 struct usbnet * const un = ifp->if_softc;
1115 struct usbnet_private * const unp = un->un_pri;
1116 struct usbnet_cdata * const cd = un_cdata(un);
1117 usbd_status err;
1118
1119 ifp->if_oerrors++;
1120 aprint_error_dev(un->un_dev, "watchdog timeout\n");
1121
1122 if (cd->uncd_tx_cnt > 0) {
1123 DPRINTF("uncd_tx_cnt=%u non zero, aborting pipe", 0, 0, 0, 0);
1124 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]);
1125 if (err)
1126 aprint_error_dev(un->un_dev, "pipe abort failed: %s\n",
1127 usbd_errstr(err));
1128 if (cd->uncd_tx_cnt != 0)
1129 DPRINTF("uncd_tx_cnt now %u", cd->uncd_tx_cnt, 0, 0, 0);
1130 }
1131
1132 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1133 (*ifp->if_start)(ifp);
1134 }
1135
1136 static void
1137 usbnet_tick_task(void *arg)
1138 {
1139 struct usbnet * const un = arg;
1140 struct usbnet_private * const unp = un->un_pri;
1141
1142 mutex_enter(&unp->unp_lock);
1143 if (unp->unp_stopping || unp->unp_dying) {
1144 mutex_exit(&unp->unp_lock);
1145 return;
1146 }
1147
1148 struct ifnet * const ifp = usbnet_ifp(un);
1149 struct mii_data * const mii = usbnet_mii(un);
1150
1151 unp->unp_refcnt++;
1152 mutex_exit(&unp->unp_lock);
1153
1154 if (ifp && unp->unp_timer != 0 && --unp->unp_timer == 0)
1155 usbnet_watchdog(ifp);
1156
1157 if (mii && ifp) {
1158 mii_tick(mii);
1159
1160 if (!unp->unp_link)
1161 (*mii->mii_statchg)(ifp);
1162 }
1163
1164 /* Call driver if requested. */
1165 uno_tick(un);
1166
1167 mutex_enter(&unp->unp_lock);
1168 if (--unp->unp_refcnt < 0)
1169 cv_broadcast(&unp->unp_detachcv);
1170 if (!unp->unp_stopping && !unp->unp_dying)
1171 callout_schedule(&unp->unp_stat_ch, hz);
1172 mutex_exit(&unp->unp_lock);
1173 }
1174
1175 static int
1176 usbnet_init(struct ifnet *ifp)
1177 {
1178 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1179 struct usbnet * const un = ifp->if_softc;
1180
1181 return uno_init(un, ifp);
1182 }
1183
1184
1185 /* Various accessors. */
1186
1187 void
1188 usbnet_set_link(struct usbnet *un, bool link)
1189 {
1190 un->un_pri->unp_link = link;
1191 }
1192
1193 void
1194 usbnet_set_dying(struct usbnet *un, bool link)
1195 {
1196 un->un_pri->unp_dying = link;
1197 }
1198
1199 struct ifnet *
1200 usbnet_ifp(struct usbnet *un)
1201 {
1202 return &un->un_pri->unp_ec.ec_if;
1203 }
1204
1205 struct ethercom *
1206 usbnet_ec(struct usbnet *un)
1207 {
1208 return &un->un_pri->unp_ec;
1209 }
1210
1211 struct mii_data *
1212 usbnet_mii(struct usbnet *un)
1213 {
1214 return un->un_pri->unp_ec.ec_mii;
1215 }
1216
1217 krndsource_t *
1218 usbnet_rndsrc(struct usbnet *un)
1219 {
1220 return &un->un_pri->unp_rndsrc;
1221 }
1222
1223 void *
1224 usbnet_softc(struct usbnet *un)
1225 {
1226 return un->un_sc;
1227 }
1228
1229 bool
1230 usbnet_havelink(struct usbnet *un)
1231 {
1232 return un->un_pri->unp_link;
1233 }
1234
1235 bool
1236 usbnet_isdying(struct usbnet *un)
1237 {
1238 return un->un_pri == NULL || un->un_pri->unp_dying;
1239 }
1240
1241
1242 /* Locking. */
1243
1244 void
1245 usbnet_lock(struct usbnet *un)
1246 {
1247 mutex_enter(&un->un_pri->unp_lock);
1248 }
1249
1250 void
1251 usbnet_unlock(struct usbnet *un)
1252 {
1253 mutex_exit(&un->un_pri->unp_lock);
1254 }
1255
1256 kmutex_t *
1257 usbnet_mutex(struct usbnet *un)
1258 {
1259 return &un->un_pri->unp_lock;
1260 }
1261
1262 void
1263 usbnet_lock_rx(struct usbnet *un)
1264 {
1265 mutex_enter(&un->un_pri->unp_rxlock);
1266 }
1267
1268 void
1269 usbnet_unlock_rx(struct usbnet *un)
1270 {
1271 mutex_exit(&un->un_pri->unp_rxlock);
1272 }
1273
1274 kmutex_t *
1275 usbnet_mutex_rx(struct usbnet *un)
1276 {
1277 return &un->un_pri->unp_rxlock;
1278 }
1279
1280 void
1281 usbnet_lock_tx(struct usbnet *un)
1282 {
1283 mutex_enter(&un->un_pri->unp_txlock);
1284 }
1285
1286 void
1287 usbnet_unlock_tx(struct usbnet *un)
1288 {
1289 mutex_exit(&un->un_pri->unp_txlock);
1290 }
1291
1292 kmutex_t *
1293 usbnet_mutex_tx(struct usbnet *un)
1294 {
1295 return &un->un_pri->unp_txlock;
1296 }
1297
1298 /* Autoconf management. */
1299
1300 static bool
1301 usbnet_empty_eaddr(struct usbnet * const un)
1302 {
1303 return (un->un_eaddr[0] == 0 && un->un_eaddr[1] == 0 &&
1304 un->un_eaddr[2] == 0 && un->un_eaddr[3] == 0 &&
1305 un->un_eaddr[4] == 0 && un->un_eaddr[5] == 0);
1306 }
1307
1308 /*
1309 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
1310 * 'usbnet'. The first is enough to enable device access (eg, endpoints
1311 * are connected and commands can be sent), and the second connects the
1312 * device to the system networking.
1313 *
1314 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
1315 * Also usable as driver detach directly.
1316 *
1317 * To skip ethernet configuration (eg, point-to-point), make sure that
1318 * the un_eaddr[] is fully zero.
1319 */
1320
1321 void
1322 usbnet_attach(struct usbnet *un,
1323 const char *detname) /* detach cv name */
1324 {
1325 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1326
1327 /* Required inputs. */
1328 KASSERT(un->un_ops->uno_tx_prepare);
1329 KASSERT(un->un_ops->uno_rx_loop);
1330 KASSERT(un->un_ops->uno_init);
1331 KASSERT(un->un_rx_bufsz);
1332 KASSERT(un->un_tx_bufsz);
1333 KASSERT(un->un_rx_list_cnt);
1334 KASSERT(un->un_tx_list_cnt);
1335
1336 /* Unfortunate fact. */
1337 KASSERT(un == device_private(un->un_dev));
1338
1339 un->un_pri = kmem_zalloc(sizeof(*un->un_pri), KM_SLEEP);
1340 struct usbnet_private * const unp = un->un_pri;
1341
1342 usb_init_task(&unp->unp_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
1343 callout_init(&unp->unp_stat_ch, CALLOUT_MPSAFE);
1344 callout_setfunc(&unp->unp_stat_ch, usbnet_tick, un);
1345
1346 mutex_init(&unp->unp_miilock, MUTEX_DEFAULT, IPL_NONE);
1347 mutex_init(&unp->unp_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1348 mutex_init(&unp->unp_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
1349 mutex_init(&unp->unp_lock, MUTEX_DEFAULT, IPL_NONE);
1350 cv_init(&unp->unp_detachcv, detname);
1351
1352 rnd_attach_source(&unp->unp_rndsrc, device_xname(un->un_dev),
1353 RND_TYPE_NET, RND_FLAG_DEFAULT);
1354
1355 usbnet_rx_list_alloc(un);
1356 usbnet_tx_list_alloc(un);
1357
1358 unp->unp_number = atomic_inc_uint_nv(&usbnet_number);
1359
1360 unp->unp_attached = true;
1361 }
1362
1363 static void
1364 usbnet_attach_mii(struct usbnet *un, const struct usbnet_mii *unm)
1365 {
1366 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1367 struct usbnet_private * const unp = un->un_pri;
1368 struct mii_data * const mii = &unp->unp_mii;
1369 struct ifnet * const ifp = usbnet_ifp(un);
1370
1371 KASSERT(un->un_ops->uno_read_reg);
1372 KASSERT(un->un_ops->uno_write_reg);
1373 KASSERT(un->un_ops->uno_statchg);
1374
1375 mii->mii_ifp = ifp;
1376 mii->mii_readreg = usbnet_mii_readreg;
1377 mii->mii_writereg = usbnet_mii_writereg;
1378 mii->mii_statchg = usbnet_mii_statchg;
1379 mii->mii_flags = MIIF_AUTOTSLEEP;
1380
1381 usbnet_ec(un)->ec_mii = mii;
1382 ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
1383 mii_attach(un->un_dev, mii, unm->un_mii_capmask, unm->un_mii_phyloc,
1384 unm->un_mii_offset, unm->un_mii_flags);
1385
1386 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1387 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1388 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1389 } else
1390 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1391 }
1392
1393 void
1394 usbnet_attach_ifp(struct usbnet *un,
1395 unsigned if_flags, /* additional if_flags */
1396 unsigned if_extflags, /* additional if_extflags */
1397 const struct usbnet_mii *unm) /* additional mii_attach flags */
1398 {
1399 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1400 struct usbnet_private * const unp = un->un_pri;
1401 struct ifnet * const ifp = usbnet_ifp(un);
1402
1403 KASSERT(unp->unp_attached);
1404
1405 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1406 ifp->if_flags = if_flags;
1407 ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1408 ifp->if_ioctl = usbnet_ioctl;
1409 ifp->if_start = usbnet_start;
1410 ifp->if_init = usbnet_init;
1411 ifp->if_stop = usbnet_stop_ifp;
1412
1413 if (unm)
1414 usbnet_attach_mii(un, unm);
1415 else
1416 unp->unp_link = true;
1417
1418 /* Attach the interface. */
1419 int rv = if_initialize(ifp);
1420 if (rv != 0) {
1421 aprint_error_dev(un->un_dev, "if_initialize failed(%d)\n", rv);
1422 return;
1423 }
1424 if (ifp->_if_input == NULL)
1425 ifp->if_percpuq = if_percpuq_create(ifp);
1426 if_register(ifp);
1427
1428 /*
1429 * If ethernet address is all zero, skip ether_ifattach() and
1430 * instead attach bpf here..
1431 */
1432 if (!usbnet_empty_eaddr(un)) {
1433 ether_set_ifflags_cb(&unp->unp_ec, usbnet_ifflags_cb);
1434 aprint_normal_dev(un->un_dev, "Ethernet address %s\n",
1435 ether_sprintf(un->un_eaddr));
1436 ether_ifattach(ifp, un->un_eaddr);
1437 } else {
1438 if_alloc_sadl(ifp);
1439 bpf_attach(ifp, DLT_RAW, 0);
1440 }
1441
1442 /* Now ready, and attached. */
1443 IFQ_SET_READY(&ifp->if_snd);
1444 ifp->if_softc = un;
1445
1446 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1447
1448 if (!pmf_device_register(un->un_dev, NULL, NULL))
1449 aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1450 }
1451
1452 int
1453 usbnet_detach(device_t self, int flags)
1454 {
1455 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1456 struct usbnet * const un = device_private(self);
1457 struct usbnet_private * const unp = un->un_pri;
1458
1459 /* Detached before attached finished, so just bail out. */
1460 if (unp == NULL || !unp->unp_attached)
1461 return 0;
1462
1463 struct ifnet * const ifp = usbnet_ifp(un);
1464 struct mii_data * const mii = usbnet_mii(un);
1465
1466 mutex_enter(&unp->unp_lock);
1467 unp->unp_dying = true;
1468 mutex_exit(&unp->unp_lock);
1469
1470 callout_halt(&unp->unp_stat_ch, NULL);
1471 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, NULL);
1472
1473 if (ifp->if_flags & IFF_RUNNING) {
1474 IFNET_LOCK(ifp);
1475 usbnet_stop_ifp(ifp, 1);
1476 IFNET_UNLOCK(ifp);
1477 }
1478
1479 mutex_enter(&unp->unp_lock);
1480 unp->unp_refcnt--;
1481 while (unp->unp_refcnt > 0) {
1482 /* Wait for processes to go away */
1483 cv_wait(&unp->unp_detachcv, &unp->unp_lock);
1484 }
1485 mutex_exit(&unp->unp_lock);
1486
1487 usbnet_rx_list_free(un);
1488 usbnet_tx_list_free(un);
1489
1490 callout_destroy(&unp->unp_stat_ch);
1491 rnd_detach_source(&unp->unp_rndsrc);
1492
1493 if (mii) {
1494 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1495 ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
1496 }
1497 if (ifp->if_softc) {
1498 if (!usbnet_empty_eaddr(un))
1499 ether_ifdetach(ifp);
1500 else
1501 bpf_detach(ifp);
1502 if_detach(ifp);
1503 }
1504
1505 cv_destroy(&unp->unp_detachcv);
1506 mutex_destroy(&unp->unp_lock);
1507 mutex_destroy(&unp->unp_rxlock);
1508 mutex_destroy(&unp->unp_txlock);
1509 mutex_destroy(&unp->unp_miilock);
1510
1511 pmf_device_deregister(un->un_dev);
1512
1513 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
1514
1515 kmem_free(unp, sizeof(*unp));
1516
1517 return 0;
1518 }
1519
1520 int
1521 usbnet_activate(device_t self, devact_t act)
1522 {
1523 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1524 struct usbnet * const un = device_private(self);
1525 struct usbnet_private * const unp = un->un_pri;
1526 struct ifnet * const ifp = usbnet_ifp(un);
1527
1528 switch (act) {
1529 case DVACT_DEACTIVATE:
1530 if_deactivate(ifp);
1531
1532 mutex_enter(&unp->unp_lock);
1533 unp->unp_dying = true;
1534 mutex_exit(&unp->unp_lock);
1535
1536 mutex_enter(&unp->unp_rxlock);
1537 mutex_enter(&unp->unp_txlock);
1538 unp->unp_stopping = true;
1539 mutex_exit(&unp->unp_txlock);
1540 mutex_exit(&unp->unp_rxlock);
1541
1542 return 0;
1543 default:
1544 return EOPNOTSUPP;
1545 }
1546 }
1547
1548 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1549
1550 static int
1551 usbnet_modcmd(modcmd_t cmd, void *arg)
1552 {
1553 switch (cmd) {
1554 case MODULE_CMD_INIT:
1555 return 0;
1556 case MODULE_CMD_FINI:
1557 return 0;
1558 case MODULE_CMD_STAT:
1559 case MODULE_CMD_AUTOUNLOAD:
1560 default:
1561 return ENOTTY;
1562 }
1563 }
1564