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