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