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