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