usbnet.c revision 1.4 1 /* $NetBSD: usbnet.c,v 1.4 2019/08/04 08:59:13 mrg Exp $ */
2
3 /*
4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * Common code shared between USB ethernet drivers.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.4 2019/08/04 08:59:13 mrg Exp $");
37
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/kmem.h>
41 #include <sys/module.h>
42
43 #include <dev/usb/usbnet.h>
44 #include <dev/usb/usbhist.h>
45
46 static int usbnet_modcmd(modcmd_t, void *);
47
48 #ifdef USB_DEBUG
49 #ifndef USBNET_DEBUG
50 #define usbnetdebug 0
51 #else
52 static int usbnetdebug = 1;
53
54 int sysctl_hw_usbnet_setup(SYSCTLFN_PROTO);
55
56 SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
57 {
58 int err;
59 const struct sysctlnode *rnode;
60 const struct sysctlnode *cnode;
61
62 err = sysctl_createv(clog, 0, NULL, &rnode,
63 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
64 SYSCTL_DESCR("usbnet global controls"),
65 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
66
67 if (err)
68 goto fail;
69
70 /* control debugging printfs */
71 err = sysctl_createv(clog, 0, &rnode, &cnode,
72 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
73 "debug", SYSCTL_DESCR("Enable debugging output"),
74 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
75 if (err)
76 goto fail;
77
78 return;
79 fail:
80 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
81 }
82
83 #endif /* USBNET_DEBUG */
84 #endif /* USB_DEBUG */
85
86 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
87 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
88 #define USBNETHIST_FUNC() USBHIST_FUNC()
89 #define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug)
90
91 /* Interrupt handling. */
92
93 static struct mbuf *
94 usbnet_newbuf(void)
95 {
96 struct mbuf *m;
97
98 MGETHDR(m, M_DONTWAIT, MT_DATA);
99 if (m == NULL)
100 return NULL;
101
102 MCLGET(m, M_DONTWAIT);
103 if (!(m->m_flags & M_EXT)) {
104 m_freem(m);
105 return NULL;
106 }
107
108 m->m_len = m->m_pkthdr.len = MCLBYTES;
109 m_adj(m, ETHER_ALIGN);
110
111 return m;
112 }
113
114 /*
115 * usbnet_rxeof() is designed to be the done callback for rx completion.
116 * it provides generic setup and finalisation, calls a different usbnet
117 * rx_loop callback in the middle, which can use usbnet_enqueue() to
118 * enqueue a packet for higher levels.
119 */
120 void
121 usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
122 int flags)
123 {
124 USBNETHIST_FUNC(); USBNETHIST_CALLED();
125 struct ifnet *ifp = &un->un_ec.ec_if;
126 struct mbuf *m;
127
128 KASSERT(mutex_owned(&un->un_rxlock));
129
130 m = usbnet_newbuf();
131 if (m == NULL) {
132 ifp->if_ierrors++;
133 return;
134 }
135
136 m_set_rcvif(m, ifp);
137 m->m_pkthdr.len = m->m_len = buflen;
138 m->m_pkthdr.csum_flags = flags;
139 memcpy(mtod(m, char *), buf, buflen);
140
141 /* push the packet up */
142 if_percpuq_enqueue(ifp->if_percpuq, m);
143 }
144
145 /*
146 * A frame has been uploaded: pass the resulting mbuf chain up to
147 * the higher level protocols.
148 */
149 static void
150 usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
151 {
152 USBNETHIST_FUNC(); USBNETHIST_CALLED();
153 struct usbnet_chain *c = priv;
154 struct usbnet * const un = c->unc_un;
155 struct ifnet *ifp = &un->un_ec.ec_if;
156 uint32_t total_len;
157
158 mutex_enter(&un->un_rxlock);
159
160 if (un->un_dying || un->un_stopping ||
161 status == USBD_INVAL || status == USBD_NOT_STARTED ||
162 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
163 goto out;
164
165 if (status != USBD_NORMAL_COMPLETION) {
166 if (usbd_ratecheck(&un->un_rx_notice))
167 aprint_error_dev(un->un_dev, "usb errors on rx: %s\n",
168 usbd_errstr(status));
169 if (status == USBD_STALLED)
170 usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_RX]);
171 goto done;
172 }
173
174 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
175
176 if (total_len > un->un_cdata.uncd_rx_bufsz) {
177 aprint_error_dev(un->un_dev,
178 "rxeof: too large transfer (%u > %u)\n",
179 total_len, un->un_cdata.uncd_rx_bufsz);
180 goto done;
181 }
182
183 (*un->un_rx_loop_cb)(un, xfer, c, total_len);
184 KASSERT(mutex_owned(&un->un_rxlock));
185
186 done:
187 if (un->un_dying || un->un_stopping)
188 goto out;
189
190 mutex_exit(&un->un_rxlock);
191
192 /* Setup new transfer. */
193 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_cdata.uncd_rx_bufsz,
194 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
195 usbd_transfer(xfer);
196 return;
197
198 out:
199 mutex_exit(&un->un_rxlock);
200 }
201
202 static void
203 usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
204 {
205 USBNETHIST_FUNC(); USBNETHIST_CALLED();
206 struct usbnet_chain *c = priv;
207 struct usbnet * const un = c->unc_un;
208 struct usbnet_cdata *cd = &un->un_cdata;
209 struct ifnet * const ifp = usbnet_ifp(un);
210
211 mutex_enter(&un->un_txlock);
212 if (un->un_stopping || un->un_dying) {
213 mutex_exit(&un->un_txlock);
214 return;
215 }
216
217 KASSERT(cd->uncd_tx_cnt > 0);
218 cd->uncd_tx_cnt--;
219
220 un->un_timer = 0;
221
222 switch (status) {
223 case USBD_NOT_STARTED:
224 case USBD_CANCELLED:
225 break;
226
227 case USBD_NORMAL_COMPLETION:
228 ifp->if_opackets++;
229 break;
230
231 default:
232
233 ifp->if_oerrors++;
234 if (usbd_ratecheck(&un->un_tx_notice))
235 aprint_error_dev(un->un_dev, "usb error on tx: %s\n",
236 usbd_errstr(status));
237 if (status == USBD_STALLED)
238 usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_TX]);
239 break;
240 }
241
242 mutex_exit(&un->un_txlock);
243
244 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
245 (*ifp->if_start)(ifp);
246 }
247
248 static void
249 usbnet_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
250 {
251 USBNETHIST_FUNC(); USBNETHIST_CALLED();
252 struct usbnet *un = priv;
253 struct ifnet *ifp = usbnet_ifp(un);
254
255 if (un->un_dying || un->un_stopping ||
256 status == USBD_INVAL || status == USBD_NOT_STARTED ||
257 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
258 return;
259
260 if (status != USBD_NORMAL_COMPLETION) {
261 if (usbd_ratecheck(&un->un_intr_notice)) {
262 aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
263 usbd_errstr(status));
264 }
265 if (status == USBD_STALLED)
266 usbd_clear_endpoint_stall_async(un->un_ep[USBNET_ENDPT_INTR]);
267 return;
268 }
269
270 if (un->un_intr_cb)
271 (*un->un_intr_cb)(un, status);
272 }
273
274 static void
275 usbnet_start_locked(struct ifnet *ifp)
276 {
277 USBNETHIST_FUNC(); USBNETHIST_CALLED();
278 struct usbnet * const un = ifp->if_softc;
279 struct usbnet_cdata *cd = &un->un_cdata;
280 struct mbuf *m;
281 unsigned length;
282 int idx;
283
284 KASSERT(mutex_owned(&un->un_txlock));
285 KASSERT(cd->uncd_tx_cnt <= cd->uncd_tx_list_cnt);
286
287 if (!un->un_link || (ifp->if_flags & IFF_RUNNING) == 0)
288 return;
289
290 idx = cd->uncd_tx_prod;
291 while (cd->uncd_tx_cnt < cd->uncd_tx_list_cnt) {
292 IFQ_POLL(&ifp->if_snd, m);
293 if (m == NULL)
294 break;
295
296 struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[idx];
297
298 length = (*un->un_tx_prepare_cb)(un, m, c);
299 if (length == 0) {
300 ifp->if_oerrors++;
301 break;
302 }
303
304 if (__predict_false(c->unc_xfer == NULL)) {
305 ifp->if_oerrors++;
306 break;
307 }
308
309 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
310 un->un_tx_xfer_flags, 10000, usbnet_txeof);
311
312 /* Transmit */
313 usbd_status err = usbd_transfer(c->unc_xfer);
314 if (err != USBD_IN_PROGRESS) {
315 ifp->if_oerrors++;
316 break;
317 }
318
319 IFQ_DEQUEUE(&ifp->if_snd, m);
320
321 /*
322 * If there's a BPF listener, bounce a copy of this frame
323 * to him.
324 */
325 bpf_mtap(ifp, m, BPF_D_OUT);
326 m_freem(m);
327
328 idx = (idx + 1) % cd->uncd_tx_list_cnt;
329 cd->uncd_tx_cnt++;
330 }
331 cd->uncd_tx_prod = idx;
332
333 /*
334 * Set a timeout in case the chip goes out to lunch.
335 */
336 un->un_timer = 5;
337 }
338
339 static void
340 usbnet_start(struct ifnet *ifp)
341 {
342 struct usbnet * const un = ifp->if_softc;
343
344 mutex_enter(&un->un_txlock);
345 if (!un->un_stopping)
346 usbnet_start_locked(ifp);
347 mutex_exit(&un->un_txlock);
348 }
349
350 /*
351 * Chain management.
352 *
353 * RX and TX are identical. Keep them that way.
354 */
355
356 /* Start of common RX functions */
357
358 static size_t
359 usbnet_rx_list_size(struct usbnet_cdata *cd)
360 {
361 return sizeof(*cd->uncd_rx_chain) * cd->uncd_rx_list_cnt;
362 }
363
364 static void
365 usbnet_rx_list_alloc(struct usbnet *un, unsigned cnt)
366 {
367 struct usbnet_cdata *cd = &un->un_cdata;
368
369 cd->uncd_rx_list_cnt = cnt;
370 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd), KM_SLEEP);
371 }
372
373 static void
374 usbnet_rx_list_free(struct usbnet *un)
375 {
376 struct usbnet_cdata *cd = &un->un_cdata;
377
378 if (cd->uncd_rx_chain) {
379 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd));
380 cd->uncd_rx_chain = NULL;
381 }
382 }
383
384 static int
385 usbnet_rx_list_init(struct usbnet *un, unsigned xfer_flags)
386 {
387 struct usbnet_cdata *cd = &un->un_cdata;
388
389 for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
390 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
391
392 c->unc_un = un;
393 if (c->unc_xfer == NULL) {
394 int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_RX],
395 cd->uncd_rx_bufsz, xfer_flags, 0, &c->unc_xfer);
396 if (err)
397 return err;
398 c->unc_buf = usbd_get_buffer(c->unc_xfer);
399 }
400 }
401
402 return 0;
403 }
404
405 static void
406 usbnet_rx_list_fini(struct usbnet *un)
407 {
408 struct usbnet_cdata *cd = &un->un_cdata;
409
410 for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
411 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
412
413 if (c->unc_xfer != NULL) {
414 usbd_destroy_xfer(c->unc_xfer);
415 c->unc_xfer = NULL;
416 c->unc_buf = NULL;
417 }
418 }
419 }
420
421 /* End of common RX functions */
422
423 static void
424 usbnet_rx_start_pipes(struct usbnet *un, usbd_callback cb)
425 {
426 struct usbnet_cdata *cd = &un->un_cdata;
427
428 mutex_enter(&un->un_rxlock);
429 mutex_enter(&un->un_txlock);
430 un->un_stopping = false;
431
432 for (size_t i = 0; i < cd->uncd_rx_list_cnt; i++) {
433 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
434
435 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, cd->uncd_rx_bufsz,
436 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, cb);
437 usbd_transfer(c->unc_xfer);
438 }
439
440 mutex_exit(&un->un_txlock);
441 mutex_exit(&un->un_rxlock);
442 }
443
444 /* Start of common TX functions */
445
446 static size_t
447 usbnet_tx_list_size(struct usbnet_cdata *cd)
448 {
449 return sizeof(*cd->uncd_tx_chain) * cd->uncd_tx_list_cnt;
450 }
451
452 static void
453 usbnet_tx_list_alloc(struct usbnet *un, unsigned cnt)
454 {
455 struct usbnet_cdata *cd = &un->un_cdata;
456
457 cd->uncd_tx_list_cnt = cnt;
458 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd), KM_SLEEP);
459 }
460
461 static void
462 usbnet_tx_list_free(struct usbnet *un)
463 {
464 struct usbnet_cdata *cd = &un->un_cdata;
465
466 if (cd->uncd_tx_chain) {
467 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd));
468 cd->uncd_tx_chain = NULL;
469 }
470 }
471
472 static int
473 usbnet_tx_list_init(struct usbnet *un, unsigned xfer_flags)
474 {
475 struct usbnet_cdata *cd = &un->un_cdata;
476
477 for (size_t i = 0; i < cd->uncd_tx_list_cnt; i++) {
478 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
479
480 c->unc_un = un;
481 if (c->unc_xfer == NULL) {
482 int err = usbd_create_xfer(un->un_ep[USBNET_ENDPT_TX],
483 cd->uncd_tx_bufsz, xfer_flags, 0, &c->unc_xfer);
484 if (err)
485 return err;
486 c->unc_buf = usbd_get_buffer(c->unc_xfer);
487 }
488 }
489
490 return 0;
491 }
492
493 static void
494 usbnet_tx_list_fini(struct usbnet *un)
495 {
496 struct usbnet_cdata *cd = &un->un_cdata;
497
498 for (size_t i = 0; i < cd->uncd_tx_list_cnt; i++) {
499 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
500
501 if (c->unc_xfer != NULL) {
502 usbd_destroy_xfer(c->unc_xfer);
503 c->unc_xfer = NULL;
504 c->unc_buf = NULL;
505 }
506 }
507 }
508
509 /* End of common TX functions */
510
511 /* Endpoint pipe management. */
512
513 static void
514 usbnet_ep_close_pipes(struct usbnet *un)
515 {
516 for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
517 if (un->un_ep[i] == NULL)
518 continue;
519 usbd_status err = usbd_close_pipe(un->un_ep[i]);
520 if (err)
521 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
522 usbd_errstr(err));
523 un->un_ep[i] = NULL;
524 }
525 }
526
527 static usbd_status
528 usbnet_ep_open_pipes(struct usbnet *un)
529 {
530 for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
531 usbd_status err;
532
533 if (un->un_ed[i] == 0)
534 continue;
535
536 if (i == USBNET_ENDPT_INTR && un->un_intr_buf) {
537 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
538 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &un->un_ep[i], un,
539 un->un_intr_buf, un->un_intr_bufsz, usbnet_intr,
540 un->un_intr_interval);
541 } else {
542 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
543 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &un->un_ep[i]);
544 }
545 if (err) {
546 usbnet_ep_close_pipes(un);
547 return err;
548 }
549 }
550
551 return USBD_NORMAL_COMPLETION;
552 }
553
554 static usbd_status
555 usbnet_ep_stop_pipes(struct usbnet *un)
556 {
557 for (size_t i = 0; i < __arraycount(un->un_ep); i++) {
558 if (un->un_ep[i] == NULL)
559 continue;
560 usbd_status err = usbd_abort_pipe(un->un_ep[i]);
561 if (err)
562 return err;
563 }
564
565 return USBD_NORMAL_COMPLETION;
566 }
567
568 int
569 usbnet_init_rx_tx(struct usbnet * const un, unsigned rxflags, unsigned txflags)
570 {
571 USBNETHIST_FUNC(); USBNETHIST_CALLED();
572 struct ifnet * const ifp = usbnet_ifp(un);
573 usbd_status err;
574 int error = 0;
575
576 usbnet_isowned(un);
577
578 if (un->un_dying) {
579 return EIO;
580 }
581 un->un_refcnt++;
582
583 /* Open RX and TX pipes. */
584 err = usbnet_ep_open_pipes(un);
585 if (err) {
586 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
587 usbd_errstr(err));
588 error = EIO;
589 goto out;
590 }
591
592 /* Init RX ring. */
593 if (usbnet_rx_list_init(un, rxflags)) {
594 aprint_error_dev(un->un_dev, "rx list init failed\n");
595 error = ENOBUFS;
596 goto out;
597 }
598
599 /* Init TX ring. */
600 if (usbnet_tx_list_init(un, txflags)) {
601 aprint_error_dev(un->un_dev, "tx list init failed\n");
602 error = ENOBUFS;
603 goto out;
604 }
605
606 /* Start up the receive pipe(s). */
607 usbnet_rx_start_pipes(un, usbnet_rxeof);
608
609 /* Indicate we are up and running. */
610 KASSERT(IFNET_LOCKED(ifp));
611 ifp->if_flags |= IFF_RUNNING;
612
613 callout_schedule(&un->un_stat_ch, hz);
614
615 out:
616 if (error) {
617 usbnet_rx_list_fini(un);
618 usbnet_tx_list_fini(un);
619 usbnet_ep_close_pipes(un);
620 }
621 if (--un->un_refcnt < 0)
622 cv_broadcast(&un->un_detachcv);
623
624 usbnet_isowned(un);
625
626 return error;
627 }
628
629 /* MII management. */
630
631 /*
632 * Access functions for MII. Take the MII lock to call access MII regs.
633 * Two forms: usbnet (softc) lock currently held or not.
634 */
635 void
636 usbnet_lock_mii(struct usbnet *un)
637 {
638
639 mutex_enter(&un->un_lock);
640 un->un_refcnt++;
641 mutex_exit(&un->un_lock);
642
643 mutex_enter(&un->un_miilock);
644 }
645
646 void
647 usbnet_lock_mii_un_locked(struct usbnet *un)
648 {
649 KASSERT(mutex_owned(&un->un_lock));
650
651 un->un_refcnt++;
652 mutex_enter(&un->un_miilock);
653 }
654
655 void
656 usbnet_unlock_mii(struct usbnet *un)
657 {
658
659 mutex_exit(&un->un_miilock);
660 mutex_enter(&un->un_lock);
661 if (--un->un_refcnt < 0)
662 cv_broadcast(&un->un_detachcv);
663 mutex_exit(&un->un_lock);
664 }
665
666 void
667 usbnet_unlock_mii_un_locked(struct usbnet *un)
668 {
669 KASSERT(mutex_owned(&un->un_lock));
670
671 mutex_exit(&un->un_miilock);
672 if (--un->un_refcnt < 0)
673 cv_broadcast(&un->un_detachcv);
674 }
675
676 int
677 usbnet_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
678 {
679 struct usbnet * const un = device_private(dev);
680 usbd_status err;
681
682 mutex_enter(&un->un_lock);
683 if (un->un_dying || un->un_phyno != phy) {
684 mutex_exit(&un->un_lock);
685 return EIO;
686 }
687 mutex_exit(&un->un_lock);
688
689 usbnet_lock_mii(un);
690 err = (*un->un_read_reg_cb)(un, phy, reg, val);
691 usbnet_unlock_mii(un);
692
693 if (err) {
694 aprint_error_dev(un->un_dev, "read PHY failed: %d\n", err);
695 return EIO;
696 }
697
698 return 0;
699 }
700
701 int
702 usbnet_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
703 {
704 struct usbnet * const un = device_private(dev);
705 usbd_status err;
706
707 mutex_enter(&un->un_lock);
708 if (un->un_dying || un->un_phyno != phy) {
709 mutex_exit(&un->un_lock);
710 return EIO;
711 }
712 mutex_exit(&un->un_lock);
713
714 usbnet_lock_mii(un);
715 err = (*un->un_write_reg_cb)(un, phy, reg, val);
716 usbnet_unlock_mii(un);
717
718 if (err) {
719 aprint_error_dev(un->un_dev, "write PHY failed: %d\n", err);
720 return EIO;
721 }
722
723 return 0;
724 }
725
726 void
727 usbnet_miibus_statchg(struct ifnet *ifp)
728 {
729 USBNETHIST_FUNC(); USBNETHIST_CALLED();
730 struct usbnet * const un = ifp->if_softc;
731
732 (*un->un_statchg_cb)(ifp);
733 }
734
735 static int
736 usbnet_media_upd(struct ifnet *ifp)
737 {
738 USBNETHIST_FUNC(); USBNETHIST_CALLED();
739 struct usbnet * const un = ifp->if_softc;
740 struct mii_data * const mii = usbnet_mii(un);
741
742 if (un->un_dying)
743 return EIO;
744
745 un->un_link = false;
746
747 if (mii->mii_instance) {
748 struct mii_softc *miisc;
749
750 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
751 mii_phy_reset(miisc);
752 }
753
754 return ether_mediachange(ifp);
755 }
756
757 /* ioctl */
758
759 static int
760 usbnet_ifflags_cb(struct ethercom *ec)
761 {
762 USBNETHIST_FUNC(); USBNETHIST_CALLED();
763 struct ifnet *ifp = &ec->ec_if;
764 struct usbnet *un = ifp->if_softc;
765 int rv = 0;
766
767 mutex_enter(&un->un_lock);
768
769 const int changed = ifp->if_flags ^ un->un_if_flags;
770 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
771 un->un_if_flags = ifp->if_flags;
772 if ((changed & IFF_PROMISC) != 0)
773 rv = ENETRESET;
774 } else {
775 rv = ENETRESET;
776 }
777
778 mutex_exit(&un->un_lock);
779
780 return rv;
781 }
782
783 static int
784 usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
785 {
786 USBNETHIST_FUNC(); USBNETHIST_CALLED();
787 struct usbnet * const un = ifp->if_softc;
788 int error;
789
790 error = ether_ioctl(ifp, cmd, data);
791 if (error == ENETRESET && un->un_ioctl_cb)
792 error = (*un->un_ioctl_cb)(ifp, cmd, data);
793
794 return error;
795 }
796
797 /*
798 * Generic stop network function:
799 * - mark as stopping
800 * - call DD routine to stop the device
801 * - turn off running, timer, statchg callout, link
802 * - stop transfers
803 * - free RX and TX resources
804 * - close pipes
805 *
806 * usbnet_stop() is exported for drivers to use, expects lock held.
807 *
808 * usbnet_stop_ifp() is for the if_stop handler.
809 */
810 void
811 usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
812 {
813 USBNETHIST_FUNC(); USBNETHIST_CALLED();
814
815 KASSERT(mutex_owned(&un->un_lock));
816
817 mutex_enter(&un->un_rxlock);
818 mutex_enter(&un->un_txlock);
819 un->un_stopping = true;
820 mutex_exit(&un->un_txlock);
821 mutex_exit(&un->un_rxlock);
822
823 if (un->un_stop_cb)
824 (*un->un_stop_cb)(ifp, disable);
825
826 /*
827 * XXXSMP Would like to
828 * KASSERT(IFNET_LOCKED(ifp))
829 * here but the locking order is:
830 * ifnet -> unlock -> rxlock -> txlock
831 * and unlock is already held.
832 */
833 ifp->if_flags &= ~IFF_RUNNING;
834 un->un_timer = 0;
835
836 callout_stop(&un->un_stat_ch);
837 un->un_link = false;
838
839 /* Stop transfers. */
840 usbnet_ep_stop_pipes(un);
841
842 /* Free RX/TX resources. */
843 usbnet_rx_list_fini(un);
844 usbnet_tx_list_fini(un);
845
846 /* Close pipes. */
847 usbnet_ep_close_pipes(un);
848 }
849
850 static void
851 usbnet_stop_ifp(struct ifnet *ifp, int disable)
852 {
853 struct usbnet * const un = ifp->if_softc;
854
855 mutex_enter(&un->un_lock);
856 usbnet_stop(un, ifp, disable);
857 mutex_exit(&un->un_lock);
858 }
859
860 /*
861 * Generic tick task function.
862 *
863 * usbnet_tick() is triggered from a callout, and triggers a call to
864 * usbnet_tick_task() from the usb_task subsystem.
865 */
866 static void
867 usbnet_tick(void *arg)
868 {
869 struct usbnet * const un = arg;
870
871 mutex_enter(&un->un_lock);
872 if (!un->un_stopping && !un->un_dying) {
873 /* Perform periodic stuff in process context */
874 usb_add_task(un->un_udev, &un->un_ticktask, USB_TASKQ_DRIVER);
875 }
876 mutex_exit(&un->un_lock);
877 }
878
879 static void
880 usbnet_watchdog(struct ifnet *ifp)
881 {
882 struct usbnet * const un = ifp->if_softc;
883 struct usbnet_cdata *cd = &un->un_cdata;
884 usbd_status stat;
885
886 ifp->if_oerrors++;
887 aprint_error_dev(un->un_dev, "watchdog timeout\n");
888
889 if (cd->uncd_tx_cnt > 0) {
890 /*
891 * XXX index 0
892 */
893 struct usbnet_chain *c = &un->un_cdata.uncd_tx_chain[0];
894 usbd_get_xfer_status(c->unc_xfer, NULL, NULL, NULL, &stat);
895 usbnet_txeof(c->unc_xfer, c, stat);
896 }
897
898 if (!IFQ_IS_EMPTY(&ifp->if_snd))
899 (*ifp->if_start)(ifp);
900 }
901
902 static void
903 usbnet_tick_task(void *arg)
904 {
905 USBNETHIST_FUNC(); USBNETHIST_CALLED();
906 struct usbnet * const un = arg;
907
908 mutex_enter(&un->un_lock);
909 if (un->un_stopping || un->un_dying) {
910 mutex_exit(&un->un_lock);
911 return;
912 }
913
914 struct ifnet * const ifp = usbnet_ifp(un);
915 struct mii_data * const mii = usbnet_mii(un);
916
917 un->un_refcnt++;
918 mutex_exit(&un->un_lock);
919
920 if (ifp && un->un_timer != 0 && --un->un_timer == 0)
921 usbnet_watchdog(ifp);
922
923 if (mii && ifp) {
924 mii_tick(mii);
925
926 if (!un->un_link)
927 (*mii->mii_statchg)(ifp);
928 }
929
930 mutex_enter(&un->un_lock);
931 if (--un->un_refcnt < 0)
932 cv_broadcast(&un->un_detachcv);
933 if (!un->un_stopping && !un->un_dying)
934 callout_schedule(&un->un_stat_ch, hz);
935 mutex_exit(&un->un_lock);
936 }
937
938 static int
939 usbnet_init(struct ifnet *ifp)
940 {
941 USBNETHIST_FUNC(); USBNETHIST_CALLED();
942 struct usbnet * const un = ifp->if_softc;
943
944 return (*un->un_init_cb)(ifp);
945 }
946
947 /* Autoconf management. */
948
949 /*
950 * usbnet_attach() and usbnet_attach_ifp() perform setup of the relevant
951 * 'usbnet'. The first is enough to enable device access (eg, endpoints
952 * are connected and commands can be sent), and the second connects the
953 * device to the system networking.
954 *
955 * Always call usbnet_detach(), even if usbnet_attach_ifp() is skippped.
956 * Also usable as driver detach directly.
957 */
958 void
959 usbnet_attach(struct usbnet *un,
960 const char *detname, /* detach cv name */
961 unsigned rx_list_cnt, /* size of rx chain list */
962 unsigned tx_list_cnt) /* size of tx chain list */
963 {
964 USBNETHIST_FUNC(); USBNETHIST_CALLED();
965
966 KASSERT(un->un_tx_prepare_cb);
967 KASSERT(un->un_rx_loop_cb);
968 KASSERT(un->un_init_cb);
969 KASSERT(un->un_cdata.uncd_rx_bufsz);
970 KASSERT(un->un_cdata.uncd_tx_bufsz);
971 KASSERT(rx_list_cnt);
972 KASSERT(tx_list_cnt);
973
974 ether_set_ifflags_cb(&un->un_ec, usbnet_ifflags_cb);
975
976 usb_init_task(&un->un_ticktask, usbnet_tick_task, un, USB_TASKQ_MPSAFE);
977 callout_init(&un->un_stat_ch, CALLOUT_MPSAFE);
978 callout_setfunc(&un->un_stat_ch, usbnet_tick, un);
979
980 mutex_init(&un->un_miilock, MUTEX_DEFAULT, IPL_NONE);
981 mutex_init(&un->un_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
982 mutex_init(&un->un_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
983 mutex_init(&un->un_lock, MUTEX_DEFAULT, IPL_NONE);
984 cv_init(&un->un_detachcv, detname);
985
986 rnd_attach_source(&un->un_rndsrc, device_xname(un->un_dev),
987 RND_TYPE_NET, RND_FLAG_DEFAULT);
988
989 usbnet_rx_list_alloc(un, rx_list_cnt);
990 usbnet_tx_list_alloc(un, tx_list_cnt);
991
992 un->un_attached = true;
993 }
994
995 static void
996 usbnet_attach_mii(struct usbnet *un, int mii_flags)
997 {
998 USBNETHIST_FUNC(); USBNETHIST_CALLED();
999 struct mii_data * const mii = &un->un_mii;
1000 struct ifnet *ifp = usbnet_ifp(un);
1001
1002 mii->mii_ifp = ifp;
1003 mii->mii_readreg = usbnet_miibus_readreg;
1004 mii->mii_writereg = usbnet_miibus_writereg;
1005 mii->mii_statchg = usbnet_miibus_statchg;
1006 mii->mii_flags = MIIF_AUTOTSLEEP;
1007
1008 un->un_ec.ec_mii = mii;
1009 ifmedia_init(&mii->mii_media, 0, usbnet_media_upd, ether_mediastatus);
1010 mii_attach(un->un_dev, mii, 0xffffffff, MII_PHY_ANY,
1011 MII_OFFSET_ANY, mii_flags);
1012
1013 if (LIST_FIRST(&mii->mii_phys) == NULL) {
1014 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
1015 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
1016 } else
1017 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
1018
1019 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, un->un_udev, un->un_dev);
1020
1021 if (!pmf_device_register(un->un_dev, NULL, NULL))
1022 aprint_error_dev(un->un_dev, "couldn't establish power handler\n");
1023 }
1024
1025 void
1026 usbnet_attach_ifp(struct usbnet *un,
1027 bool have_mii, /* setup MII */
1028 unsigned if_flags, /* additional if_flags */
1029 unsigned if_extflags, /* additional if_extflags */
1030 int mii_flags) /* additional mii_attach flags */
1031 {
1032 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1033 struct ifnet *ifp = usbnet_ifp(un);
1034
1035 KASSERT(un->un_attached);
1036
1037 IFQ_SET_READY(&ifp->if_snd);
1038
1039 ifp->if_softc = un;
1040 strlcpy(ifp->if_xname, device_xname(un->un_dev), IFNAMSIZ);
1041 ifp->if_flags = if_flags;
1042 ifp->if_extflags = IFEF_MPSAFE | if_extflags;
1043 ifp->if_ioctl = usbnet_ioctl;
1044 ifp->if_start = usbnet_start;
1045 ifp->if_init = usbnet_init;
1046 ifp->if_stop = usbnet_stop_ifp;
1047
1048 IFQ_SET_READY(&ifp->if_snd);
1049
1050 if (have_mii)
1051 usbnet_attach_mii(un, mii_flags);
1052 else
1053 un->un_link = true;
1054
1055 /* Attach the interface. */
1056 if_attach(ifp);
1057 ether_ifattach(ifp, un->un_eaddr);
1058 }
1059
1060 int
1061 usbnet_detach(device_t self, int flags)
1062 {
1063 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1064 struct usbnet * const un = device_private(self);
1065 struct ifnet *ifp = usbnet_ifp(un);
1066 struct mii_data *mii = usbnet_mii(un);
1067
1068 mutex_enter(&un->un_lock);
1069 un->un_dying = true;
1070 mutex_exit(&un->un_lock);
1071
1072 /* Detached before attached finished, so just bail out. */
1073 if (!un->un_attached)
1074 return 0;
1075
1076 callout_halt(&un->un_stat_ch, NULL);
1077 usb_rem_task_wait(un->un_udev, &un->un_ticktask, USB_TASKQ_DRIVER, NULL);
1078
1079 if (ifp->if_flags & IFF_RUNNING) {
1080 IFNET_LOCK(ifp);
1081 usbnet_stop_ifp(ifp, 1);
1082 IFNET_UNLOCK(ifp);
1083 }
1084
1085 mutex_enter(&un->un_lock);
1086 un->un_refcnt--;
1087 while (un->un_refcnt > 0) {
1088 /* Wait for processes to go away */
1089 cv_wait(&un->un_detachcv, &un->un_lock);
1090 }
1091 mutex_exit(&un->un_lock);
1092
1093 usbnet_rx_list_free(un);
1094 usbnet_tx_list_free(un);
1095
1096 callout_destroy(&un->un_stat_ch);
1097 rnd_detach_source(&un->un_rndsrc);
1098
1099 if (mii) {
1100 mii_detach(mii, MII_PHY_ANY, MII_OFFSET_ANY);
1101 ifmedia_delete_instance(&mii->mii_media, IFM_INST_ANY);
1102 }
1103 if (ifp->if_softc) {
1104 ether_ifdetach(ifp);
1105 if_detach(ifp);
1106 }
1107
1108 cv_destroy(&un->un_detachcv);
1109 mutex_destroy(&un->un_lock);
1110 mutex_destroy(&un->un_rxlock);
1111 mutex_destroy(&un->un_txlock);
1112 mutex_destroy(&un->un_miilock);
1113
1114 pmf_device_deregister(un->un_dev);
1115
1116 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, un->un_udev, un->un_dev);
1117
1118 return 0;
1119 }
1120
1121 int
1122 usbnet_activate(device_t self, devact_t act)
1123 {
1124 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1125 struct usbnet * const un = device_private(self);
1126 struct ifnet * const ifp = usbnet_ifp(un);
1127
1128 switch (act) {
1129 case DVACT_DEACTIVATE:
1130 if_deactivate(ifp);
1131
1132 mutex_enter(&un->un_lock);
1133 un->un_dying = true;
1134 mutex_exit(&un->un_lock);
1135
1136 mutex_enter(&un->un_rxlock);
1137 mutex_enter(&un->un_txlock);
1138 un->un_stopping = true;
1139 mutex_exit(&un->un_txlock);
1140 mutex_exit(&un->un_rxlock);
1141
1142 return 0;
1143 default:
1144 return EOPNOTSUPP;
1145 }
1146 }
1147
1148 MODULE(MODULE_CLASS_MISC, usbnet, NULL);
1149
1150 static int
1151 usbnet_modcmd(modcmd_t cmd, void *arg)
1152 {
1153 switch (cmd) {
1154 case MODULE_CMD_INIT:
1155 #ifdef _MODULE
1156 # if defined(USB_DEBUG) && defined(USBNET_DEBUG)
1157 sysctl_hw_usbnet_setup(&usbnet_clog);
1158 # endif
1159 #endif
1160 return 0;
1161 case MODULE_CMD_FINI:
1162 #ifdef _MODULE
1163 # if defined(USB_DEBUG) && defined(USBNET_DEBUG)
1164 sysctl_teardown(&usbnet_clog);
1165 # endif
1166 #endif
1167 return 0;
1168 case MODULE_CMD_STAT:
1169 case MODULE_CMD_AUTOUNLOAD:
1170 default:
1171 return ENOTTY;
1172 }
1173 }
1174