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