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