if_kue.c revision 1.81.4.15 1 /* $NetBSD: if_kue.c,v 1.81.4.15 2017/09/05 07:04:17 skrll Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2000
5 * Bill Paul <wpaul (at) ee.columbia.edu>. 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
35 */
36
37 /*
38 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
39 *
40 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45 /*
46 * The KLSI USB to ethernet adapter chip contains an USB serial interface,
47 * ethernet MAC and embedded microcontroller (called the QT Engine).
48 * The chip must have firmware loaded into it before it will operate.
49 * Packets are passed between the chip and host via bulk transfers.
50 * There is an interrupt endpoint mentioned in the software spec, however
51 * it's currently unused. This device is 10Mbps half-duplex only, hence
52 * there is no media selection logic. The MAC supports a 128 entry
53 * multicast filter, though the exact size of the filter can depend
54 * on the firmware. Curiously, while the software spec describes various
55 * ethernet statistics counters, my sample adapter and firmware combination
56 * claims not to support any statistics counters at all.
57 *
58 * Note that once we load the firmware in the device, we have to be
59 * careful not to load it again: if you restart your computer but
60 * leave the adapter attached to the USB controller, it may remain
61 * powered on and retain its firmware. In this case, we don't need
62 * to load the firmware a second time.
63 *
64 * Special thanks to Rob Furr for providing an ADS Technologies
65 * adapter for development and testing. No monkeys were harmed during
66 * the development of this driver.
67 */
68
69 /*
70 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.81.4.15 2017/09/05 07:04:17 skrll Exp $");
75
76 #ifdef _KERNEL_OPT
77 #include "opt_inet.h"
78 #include "opt_usb.h"
79 #endif
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/sockio.h>
84 #include <sys/mbuf.h>
85 #include <sys/kmem.h>
86 #include <sys/kernel.h>
87 #include <sys/socket.h>
88 #include <sys/device.h>
89 #include <sys/proc.h>
90 #include <sys/rndsource.h>
91
92 #include <net/if.h>
93 #include <net/if_arp.h>
94 #include <net/if_dl.h>
95 #include <net/bpf.h>
96 #include <net/if_ether.h>
97
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/if_inarp.h>
101 #endif
102
103 #include <dev/usb/usb.h>
104 #include <dev/usb/usbdi.h>
105 #include <dev/usb/usbdi_util.h>
106 #include <dev/usb/usbdivar.h>
107 #include <dev/usb/usbdevs.h>
108
109 #include <dev/usb/if_kuereg.h>
110 #include <dev/usb/kue_fw.h>
111
112 #ifdef KUE_DEBUG
113 #define DPRINTF(x) if (kuedebug) printf x
114 #define DPRINTFN(n,x) if (kuedebug >= (n)) printf x
115 int kuedebug = 0;
116 #else
117 #define DPRINTF(x)
118 #define DPRINTFN(n,x)
119 #endif
120
121 /*
122 * Various supported device vendors/products.
123 */
124 static const struct usb_devno kue_devs[] = {
125 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
126 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
127 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
128 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
129 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
130 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
131 { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
132 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
133 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
134 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
135 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
136 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
137 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
138 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
139 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
140 { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
141 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
142 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
143 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
144 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
145 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
146 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
147 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
148 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
149 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
150 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
151 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
152 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
153 { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
154 { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
155 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
156 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
157 };
158 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p))
159
160 int kue_match(device_t, cfdata_t, void *);
161 void kue_attach(device_t, device_t, void *);
162 int kue_detach(device_t, int);
163 int kue_activate(device_t, enum devact);
164 extern struct cfdriver kue_cd;
165 CFATTACH_DECL_NEW(kue, sizeof(struct kue_softc), kue_match, kue_attach,
166 kue_detach, kue_activate);
167
168 static int kue_tx_list_init(struct kue_softc *);
169 static void kue_tx_list_free(struct kue_softc *);
170 static int kue_rx_list_init(struct kue_softc *);
171 static void kue_rx_list_free(struct kue_softc *);
172 static int kue_send(struct kue_softc *, struct mbuf *, int);
173 static int kue_open_pipes(struct kue_softc *);
174 static void kue_rxeof(struct usbd_xfer *, void *, usbd_status);
175 static void kue_txeof(struct usbd_xfer *, void *, usbd_status);
176 static void kue_start(struct ifnet *);
177 static void kue_start_locked(struct ifnet *);
178 static int kue_ioctl(struct ifnet *, u_long, void *);
179 static void kue_init(void *);
180 static void kue_init_locked(void *);
181 static void kue_stop(struct kue_softc *);
182 static void kue_stop_locked(struct kue_softc *);
183 static void kue_watchdog(struct ifnet *);
184
185 static void kue_setmulti(struct kue_softc *);
186 static void kue_reset(struct kue_softc *);
187
188 static usbd_status kue_ctl(struct kue_softc *, int, uint8_t,
189 uint16_t, void *, uint32_t);
190 static usbd_status kue_setword(struct kue_softc *, uint8_t, uint16_t);
191 static int kue_load_fw(struct kue_softc *);
192
193 static usbd_status
194 kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
195 {
196 usb_device_request_t req;
197
198 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
199
200 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
201 req.bRequest = breq;
202 USETW(req.wValue, word);
203 USETW(req.wIndex, 0);
204 USETW(req.wLength, 0);
205
206 return usbd_do_request(sc->kue_udev, &req, NULL);
207 }
208
209 static usbd_status
210 kue_ctl(struct kue_softc *sc, int rw, uint8_t breq, uint16_t val,
211 void *data, uint32_t len)
212 {
213 usb_device_request_t req;
214
215 DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(sc->kue_dev),
216 __func__, len));
217
218 if (rw == KUE_CTL_WRITE)
219 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
220 else
221 req.bmRequestType = UT_READ_VENDOR_DEVICE;
222
223 req.bRequest = breq;
224 USETW(req.wValue, val);
225 USETW(req.wIndex, 0);
226 USETW(req.wLength, len);
227
228 return usbd_do_request(sc->kue_udev, &req, data);
229 }
230
231 static int
232 kue_load_fw(struct kue_softc *sc)
233 {
234 usb_device_descriptor_t dd;
235 usbd_status err;
236
237 DPRINTFN(1,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
238
239 /*
240 * First, check if we even need to load the firmware.
241 * If the device was still attached when the system was
242 * rebooted, it may already have firmware loaded in it.
243 * If this is the case, we don't need to do it again.
244 * And in fact, if we try to load it again, we'll hang,
245 * so we have to avoid this condition if we don't want
246 * to look stupid.
247 *
248 * We can test this quickly by checking the bcdRevision
249 * code. The NIC will return a different revision code if
250 * it's probed while the firmware is still loaded and
251 * running.
252 */
253 if (usbd_get_device_desc(sc->kue_udev, &dd))
254 return EIO;
255 if (UGETW(dd.bcdDevice) == KUE_WARM_REV) {
256 printf("%s: warm boot, no firmware download\n",
257 device_xname(sc->kue_dev));
258 return 0;
259 }
260
261 printf("%s: cold boot, downloading firmware\n",
262 device_xname(sc->kue_dev));
263
264 /* Load code segment */
265 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
266 device_xname(sc->kue_dev)));
267 /*XXXUNCONST*/
268 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
269 0, __UNCONST(kue_code_seg), sizeof(kue_code_seg));
270 if (err) {
271 printf("%s: failed to load code segment: %s\n",
272 device_xname(sc->kue_dev), usbd_errstr(err));
273 return EIO;
274 }
275
276 /* Load fixup segment */
277 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
278 device_xname(sc->kue_dev)));
279 /*XXXUNCONST*/
280 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
281 0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg));
282 if (err) {
283 printf("%s: failed to load fixup segment: %s\n",
284 device_xname(sc->kue_dev), usbd_errstr(err));
285 return EIO;
286 }
287
288 /* Send trigger command. */
289 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
290 device_xname(sc->kue_dev)));
291 /*XXXUNCONST*/
292 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
293 0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg));
294 if (err) {
295 printf("%s: failed to load trigger segment: %s\n",
296 device_xname(sc->kue_dev), usbd_errstr(err));
297 return EIO;
298 }
299
300 usbd_delay_ms(sc->kue_udev, 10);
301
302 /*
303 * Reload device descriptor.
304 * Why? The chip without the firmware loaded returns
305 * one revision code. The chip with the firmware
306 * loaded and running returns a *different* revision
307 * code. This confuses the quirk mechanism, which is
308 * dependent on the revision data.
309 */
310 (void)usbd_reload_device_desc(sc->kue_udev);
311
312 DPRINTFN(1,("%s: %s: done\n", device_xname(sc->kue_dev), __func__));
313
314 /* Reset the adapter. */
315 kue_reset(sc);
316
317 return 0;
318 }
319
320 static void
321 kue_setmulti(struct kue_softc *sc)
322 {
323 struct ifnet *ifp = GET_IFP(sc);
324 struct ether_multi *enm;
325 struct ether_multistep step;
326 int i;
327
328 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
329
330 if (ifp->if_flags & IFF_PROMISC) {
331 allmulti:
332 ifp->if_flags |= IFF_ALLMULTI;
333 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
334 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
335 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
336 return;
337 }
338
339 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
340
341 i = 0;
342 ETHER_FIRST_MULTI(step, &sc->kue_ec, enm);
343 while (enm != NULL) {
344 if (i == KUE_MCFILTCNT(sc) ||
345 memcmp(enm->enm_addrlo, enm->enm_addrhi,
346 ETHER_ADDR_LEN) != 0)
347 goto allmulti;
348
349 memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
350 ETHER_NEXT_MULTI(step, enm);
351 i++;
352 }
353
354 ifp->if_flags &= ~IFF_ALLMULTI;
355
356 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
357 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
358 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
359
360 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
361 }
362
363 /*
364 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
365 * done after the firmware is loaded into the adapter in order to
366 * bring it into proper operation.
367 */
368 static void
369 kue_reset(struct kue_softc *sc)
370 {
371 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
372
373 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) ||
374 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
375 &sc->kue_iface))
376 printf("%s: reset failed\n", device_xname(sc->kue_dev));
377
378 /* Wait a little while for the chip to get its brains in order. */
379 usbd_delay_ms(sc->kue_udev, 10);
380 }
381
382 /*
383 * Probe for a KLSI chip.
384 */
385 int
386 kue_match(device_t parent, cfdata_t match, void *aux)
387 {
388 struct usb_attach_arg *uaa = aux;
389
390 DPRINTFN(25,("kue_match: enter\n"));
391
392 return kue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
393 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
394 }
395
396 /*
397 * Attach the interface. Allocate softc structures, do
398 * setup and ethernet/BPF attach.
399 */
400 void
401 kue_attach(device_t parent, device_t self, void *aux)
402 {
403 struct kue_softc *sc = device_private(self);
404 struct usb_attach_arg *uaa = aux;
405 char *devinfop;
406 struct ifnet *ifp;
407 struct usbd_device * dev = uaa->uaa_device;
408 struct usbd_interface * iface;
409 usbd_status err;
410 usb_interface_descriptor_t *id;
411 usb_endpoint_descriptor_t *ed;
412 int i;
413
414 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
415
416 sc->kue_dev = self;
417
418 aprint_naive("\n");
419 aprint_normal("\n");
420
421 devinfop = usbd_devinfo_alloc(dev, 0);
422 aprint_normal_dev(self, "%s\n", devinfop);
423 usbd_devinfo_free(devinfop);
424
425 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
426 if (err) {
427 aprint_error_dev(self, "failed to set configuration"
428 ", err=%s\n", usbd_errstr(err));
429 return;
430 }
431
432 sc->kue_udev = dev;
433 sc->kue_product = uaa->uaa_product;
434 sc->kue_vendor = uaa->uaa_vendor;
435
436 /* Load the firmware into the NIC. */
437 if (kue_load_fw(sc)) {
438 aprint_error_dev(self, "loading firmware failed\n");
439 return;
440 }
441
442 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
443 if (err) {
444 aprint_error_dev(self, "getting interface handle failed\n");
445 return;
446 }
447
448 sc->kue_iface = iface;
449 id = usbd_get_interface_descriptor(iface);
450
451 /* Find endpoints. */
452 for (i = 0; i < id->bNumEndpoints; i++) {
453 ed = usbd_interface2endpoint_descriptor(iface, i);
454 if (ed == NULL) {
455 aprint_error_dev(self, "couldn't get ep %d\n", i);
456 return;
457 }
458 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
459 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
460 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
461 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
462 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
463 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
464 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
465 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
466 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
467 }
468 }
469
470 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
471 aprint_error_dev(self, "missing endpoint\n");
472 return;
473 }
474
475 /* Read ethernet descriptor */
476 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
477 0, &sc->kue_desc, sizeof(sc->kue_desc));
478 if (err) {
479 aprint_error_dev(self, "could not read Ethernet descriptor\n");
480 return;
481 }
482
483 sc->kue_mcfilters = kmem_alloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
484 KM_SLEEP);
485
486 /*
487 * A KLSI chip was detected. Inform the world.
488 */
489 aprint_normal_dev(self, "Ethernet address %s\n",
490 ether_sprintf(sc->kue_desc.kue_macaddr));
491
492 /* Initialize interface info.*/
493 ifp = GET_IFP(sc);
494 ifp->if_softc = sc;
495 ifp->if_mtu = ETHERMTU;
496 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
497 ifp->if_ioctl = kue_ioctl;
498 ifp->if_start = kue_start;
499 ifp->if_watchdog = kue_watchdog;
500 strlcpy(ifp->if_xname, device_xname(sc->kue_dev), IFNAMSIZ);
501
502 IFQ_SET_READY(&ifp->if_snd);
503
504 /* Attach the interface. */
505 if_attach(ifp);
506 ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
507 rnd_attach_source(&sc->rnd_source, device_xname(sc->kue_dev),
508 RND_TYPE_NET, RND_FLAG_DEFAULT);
509
510 sc->kue_attached = true;
511
512 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev, sc->kue_dev);
513
514 return;
515 }
516
517 int
518 kue_detach(device_t self, int flags)
519 {
520 struct kue_softc *sc = device_private(self);
521 struct ifnet *ifp = GET_IFP(sc);
522 int s;
523
524 s = splusb(); /* XXX why? */
525
526 if (sc->kue_mcfilters != NULL) {
527 kmem_free(sc->kue_mcfilters,
528 KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN);
529 sc->kue_mcfilters = NULL;
530 }
531
532 if (!sc->kue_attached) {
533 /* Detached before attached finished, so just bail out. */
534 splx(s);
535 return 0;
536 }
537
538 if (ifp->if_flags & IFF_RUNNING)
539 kue_stop(sc);
540
541 rnd_detach_source(&sc->rnd_source);
542 ether_ifdetach(ifp);
543
544 if_detach(ifp);
545
546 #ifdef DIAGNOSTIC
547 if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
548 sc->kue_ep[KUE_ENDPT_RX] != NULL ||
549 sc->kue_ep[KUE_ENDPT_INTR] != NULL)
550 aprint_debug_dev(self, "detach has active endpoints\n");
551 #endif
552
553 sc->kue_attached = false;
554 splx(s);
555
556 return 0;
557 }
558
559 int
560 kue_activate(device_t self, enum devact act)
561 {
562 struct kue_softc *sc = device_private(self);
563
564 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
565
566 switch (act) {
567 case DVACT_DEACTIVATE:
568 /* Deactivate the interface. */
569 if_deactivate(&sc->kue_ec.ec_if);
570 sc->kue_dying = true;
571 return 0;
572 default:
573 return EOPNOTSUPP;
574 }
575 }
576
577 static int
578 kue_rx_list_init(struct kue_softc *sc)
579 {
580 struct kue_cdata *cd;
581 struct kue_chain *c;
582 int i;
583
584 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
585
586 cd = &sc->kue_cdata;
587 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
588 c = &cd->kue_rx_chain[i];
589 c->kue_sc = sc;
590 c->kue_idx = i;
591 if (c->kue_xfer == NULL) {
592 int error = usbd_create_xfer(sc->kue_ep[KUE_ENDPT_RX],
593 KUE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->kue_xfer);
594 if (error)
595 return error;
596 c->kue_buf = usbd_get_buffer(c->kue_xfer);
597 }
598 }
599
600 return 0;
601 }
602
603 static void
604 kue_rx_list_free(struct kue_softc *sc)
605 {
606 /* Free RX resources. */
607 for (int i = 0; i < KUE_RX_LIST_CNT; i++) {
608 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
609 usbd_destroy_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
610 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
611 }
612 }
613 }
614
615 static int
616 kue_tx_list_init(struct kue_softc *sc)
617 {
618 struct kue_cdata *cd;
619 struct kue_chain *c;
620 int i;
621
622 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
623
624 cd = &sc->kue_cdata;
625 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
626 c = &cd->kue_tx_chain[i];
627 c->kue_sc = sc;
628 c->kue_idx = i;
629 if (c->kue_xfer == NULL) {
630 int error = usbd_create_xfer(sc->kue_ep[KUE_ENDPT_TX],
631 KUE_BUFSZ, 0, 0, &c->kue_xfer);
632 if (error)
633 return error;
634 c->kue_buf = usbd_get_buffer(c->kue_xfer);
635 }
636 }
637
638 return 0;
639 }
640
641 static void
642 kue_tx_list_free(struct kue_softc *sc)
643 {
644 /* Free TX resources. */
645 for (int i = 0; i < KUE_TX_LIST_CNT; i++) {
646 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
647 usbd_destroy_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
648 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
649 }
650 }
651 }
652
653 /*
654 * A frame has been uploaded: pass the resulting mbuf chain up to
655 * the higher level protocols.
656 */
657 static void
658 kue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
659 {
660 struct kue_chain *c = priv;
661 struct kue_softc *sc = c->kue_sc;
662 struct ifnet *ifp = GET_IFP(sc);
663 struct mbuf *m;
664 int total_len, pktlen;
665 int s;
666
667 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev),
668 __func__, status));
669
670 if (sc->kue_dying)
671 return;
672
673 if (!(ifp->if_flags & IFF_RUNNING))
674 return;
675
676 if (status != USBD_NORMAL_COMPLETION) {
677 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
678 return;
679 sc->kue_rx_errs++;
680 if (usbd_ratecheck(&sc->kue_rx_notice)) {
681 printf("%s: %u usb errors on rx: %s\n",
682 device_xname(sc->kue_dev), sc->kue_rx_errs,
683 usbd_errstr(status));
684 sc->kue_rx_errs = 0;
685 }
686 if (status == USBD_STALLED)
687 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
688 goto done;
689 }
690
691 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
692
693 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", device_xname(sc->kue_dev),
694 __func__, total_len,
695 le16dec(c->kue_buf)));
696
697 if (total_len <= 1)
698 goto done;
699
700 pktlen = le16dec(c->kue_buf);
701 if (pktlen > total_len - 2)
702 pktlen = total_len - 2;
703
704 if (pktlen < ETHER_MIN_LEN - ETHER_CRC_LEN ||
705 pktlen > MCLBYTES - ETHER_ALIGN) {
706 ifp->if_ierrors++;
707 goto done;
708 }
709
710 /* No errors; receive the packet. */
711 MGETHDR(m, M_DONTWAIT, MT_DATA);
712 if (m == NULL) {
713 ifp->if_ierrors++;
714 goto done;
715 }
716 if (pktlen > MHLEN - ETHER_ALIGN) {
717 MCLGET(m, M_DONTWAIT);
718 if ((m->m_flags & M_EXT) == 0) {
719 m_freem(m);
720 ifp->if_ierrors++;
721 goto done;
722 }
723 }
724 m->m_data += ETHER_ALIGN;
725
726 /* copy data to mbuf */
727 memcpy(mtod(m, uint8_t *), c->kue_buf + 2, pktlen);
728
729 m->m_pkthdr.len = m->m_len = pktlen;
730 m_set_rcvif(m, ifp);
731
732 s = splnet();
733
734 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->kue_dev),
735 __func__, m->m_len));
736 if_percpuq_enqueue(ifp->if_percpuq, m);
737
738 splx(s);
739
740 done:
741
742 /* Setup new transfer. */
743 usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, KUE_BUFSZ,
744 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
745 usbd_transfer(c->kue_xfer);
746
747 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->kue_dev),
748 __func__));
749 }
750
751 /*
752 * A frame was downloaded to the chip. It's safe for us to clean up
753 * the list buffers.
754 */
755
756 static void
757 kue_txeof(struct usbd_xfer *xfer, void *priv,
758 usbd_status status)
759 {
760 struct kue_chain *c = priv;
761 struct kue_softc *sc = c->kue_sc;
762 struct ifnet *ifp = GET_IFP(sc);
763 int s;
764
765 if (sc->kue_dying)
766 return;
767
768 s = splnet();
769
770 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->kue_dev),
771 __func__, status));
772
773 ifp->if_timer = 0;
774 ifp->if_flags &= ~IFF_OACTIVE;
775
776 if (status != USBD_NORMAL_COMPLETION) {
777 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
778 splx(s);
779 return;
780 }
781 ifp->if_oerrors++;
782 printf("%s: usb error on tx: %s\n", device_xname(sc->kue_dev),
783 usbd_errstr(status));
784 if (status == USBD_STALLED)
785 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
786 splx(s);
787 return;
788 }
789
790 ifp->if_opackets++;
791
792 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
793 kue_start(ifp);
794
795 splx(s);
796 }
797
798 static int
799 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
800 {
801 int total_len;
802 struct kue_chain *c;
803 usbd_status err;
804
805 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
806
807 c = &sc->kue_cdata.kue_tx_chain[idx];
808
809 /* Frame length is specified in the first 2 bytes of the buffer. */
810 le16enc(c->kue_buf, (uint16_t)m->m_pkthdr.len);
811
812 /*
813 * Copy the mbuf data into a contiguous buffer, leaving two
814 * bytes at the beginning to hold the frame length.
815 */
816 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
817
818 total_len = 2 + m->m_pkthdr.len;
819 total_len = roundup2(total_len, 64);
820
821 usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, total_len, 0,
822 USBD_DEFAULT_TIMEOUT, kue_txeof);
823
824 /* Transmit */
825 err = usbd_transfer(c->kue_xfer);
826 if (err != USBD_IN_PROGRESS) {
827 printf("%s: kue_send error=%s\n", device_xname(sc->kue_dev),
828 usbd_errstr(err));
829 kue_stop(sc);
830 return EIO;
831 }
832
833 sc->kue_cdata.kue_tx_cnt++;
834
835 return 0;
836 }
837
838 static void
839 kue_start(struct ifnet *ifp)
840 {
841 struct kue_softc *sc = ifp->if_softc;
842 KASSERT(ifp->if_extflags & IFEF_START_MPSAFE);
843
844 mutex_enter(&sc->kue_txlock);
845 kue_start_locked(ifp);
846 mutex_exit(&sc->kue_txlock);
847 }
848
849 static void
850 kue_start_locked(struct ifnet *ifp)
851 {
852 struct kue_softc *sc = ifp->if_softc;
853 struct mbuf *m;
854
855 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
856
857 if (sc->kue_dying)
858 return;
859
860 if (ifp->if_flags & IFF_OACTIVE)
861 return;
862
863 IFQ_POLL(&ifp->if_snd, m);
864 if (m == NULL)
865 return;
866
867 if (kue_send(sc, m, 0)) {
868 ifp->if_flags |= IFF_OACTIVE;
869 return;
870 }
871
872 IFQ_DEQUEUE(&ifp->if_snd, m);
873
874 /*
875 * If there's a BPF listener, bounce a copy of this frame
876 * to him.
877 */
878 bpf_mtap(ifp, m);
879 m_freem(m);
880
881 ifp->if_flags |= IFF_OACTIVE;
882
883 /*
884 * Set a timeout in case the chip goes out to lunch.
885 */
886 ifp->if_timer = 6;
887 }
888
889 static void
890 kue_init(void *xsc)
891 {
892 struct kue_softc *sc = xsc;
893
894 mutex_enter(&sc->kue_lock);
895 kue_init_locked(xsc);
896 mutex_exit(&sc->kue_lock);
897 }
898
899 static void
900 kue_init_locked(void *xsc)
901 {
902 struct kue_softc *sc = xsc;
903 struct ifnet *ifp = GET_IFP(sc);
904 uint8_t eaddr[ETHER_ADDR_LEN];
905
906 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
907
908 if (ifp->if_flags & IFF_RUNNING)
909 return;
910
911 memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
912 /* Set MAC address */
913 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
914
915 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
916
917 /* If we want promiscuous mode, set the allframes bit. */
918 if (ifp->if_flags & IFF_PROMISC)
919 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
920
921 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
922
923 /* I'm not sure how to tune these. */
924 #if 0
925 /*
926 * Leave this one alone for now; setting it
927 * wrong causes lockups on some machines/controllers.
928 */
929 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
930 #endif
931 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
932
933 /* Load the multicast filter. */
934 kue_setmulti(sc);
935
936 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
937 if (kue_open_pipes(sc)) {
938 return;
939 }
940 }
941 /* Init TX ring. */
942 if (kue_tx_list_init(sc)) {
943 printf("%s: tx list init failed\n", device_xname(sc->kue_dev));
944 return;
945 }
946
947 /* Init RX ring. */
948 if (kue_rx_list_init(sc)) {
949 printf("%s: rx list init failed\n", device_xname(sc->kue_dev));
950 return;
951 }
952
953 /* Start up the receive pipe. */
954 for (size_t i = 0; i < KUE_RX_LIST_CNT; i++) {
955 struct kue_chain *c = &sc->kue_cdata.kue_rx_chain[i];
956 usbd_setup_xfer(c->kue_xfer, c, c->kue_buf, KUE_BUFSZ,
957 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
958 DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->kue_dev),
959 __func__));
960 usbd_transfer(c->kue_xfer);
961 }
962
963 ifp->if_flags |= IFF_RUNNING;
964 ifp->if_flags &= ~IFF_OACTIVE;
965 }
966
967 static int
968 kue_open_pipes(struct kue_softc *sc)
969 {
970 usbd_status err;
971
972 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
973
974 /* Open RX and TX pipes. */
975 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
976 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
977 if (err) {
978 printf("%s: open rx pipe failed: %s\n",
979 device_xname(sc->kue_dev), usbd_errstr(err));
980 return EIO;
981 }
982
983 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
984 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
985 if (err) {
986 printf("%s: open tx pipe failed: %s\n",
987 device_xname(sc->kue_dev), usbd_errstr(err));
988 return EIO;
989 }
990
991 return 0;
992 }
993
994 static int
995 kue_ioctl(struct ifnet *ifp, u_long command, void *data)
996 {
997 struct kue_softc *sc = ifp->if_softc;
998 int s, error = 0;
999
1000 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
1001
1002 if (sc->kue_dying)
1003 return EIO;
1004
1005 s = splnet();
1006
1007 switch(command) {
1008 case SIOCSIFFLAGS:
1009 if (ifp->if_flags & IFF_UP) {
1010 if (ifp->if_flags & IFF_RUNNING &&
1011 ifp->if_flags & IFF_PROMISC &&
1012 !(sc->kue_if_flags & IFF_PROMISC)) {
1013 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1014 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1015 sc->kue_rxfilt);
1016 } else if (ifp->if_flags & IFF_RUNNING &&
1017 !(ifp->if_flags & IFF_PROMISC) &&
1018 sc->kue_if_flags & IFF_PROMISC) {
1019 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1020 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1021 sc->kue_rxfilt);
1022 } else if (!(ifp->if_flags & IFF_RUNNING))
1023 kue_init(sc);
1024 } else {
1025 if (ifp->if_flags & IFF_RUNNING)
1026 kue_stop(sc);
1027 }
1028 sc->kue_if_flags = ifp->if_flags;
1029 error = 0;
1030 break;
1031 case SIOCADDMULTI:
1032 case SIOCDELMULTI:
1033 error = ether_ioctl(ifp, command, data);
1034 if (error == ENETRESET) {
1035 if (ifp->if_flags & IFF_RUNNING)
1036 kue_setmulti(sc);
1037 error = 0;
1038 }
1039 break;
1040 default:
1041 error = ether_ioctl(ifp, command, data);
1042 break;
1043 }
1044
1045 splx(s);
1046
1047 return error;
1048 }
1049
1050 static void
1051 kue_watchdog(struct ifnet *ifp)
1052 {
1053 struct kue_softc *sc = ifp->if_softc;
1054 struct kue_chain *c;
1055 usbd_status stat;
1056 int s;
1057
1058 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
1059
1060 if (sc->kue_dying)
1061 return;
1062
1063 ifp->if_oerrors++;
1064 printf("%s: watchdog timeout\n", device_xname(sc->kue_dev));
1065
1066 s = splusb();
1067 c = &sc->kue_cdata.kue_tx_chain[0];
1068 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1069 kue_txeof(c->kue_xfer, c, stat);
1070
1071 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1072 kue_start(ifp);
1073 splx(s);
1074 }
1075
1076 static void
1077 kue_stop(struct kue_softc *sc)
1078 {
1079 mutex_enter(&sc->kue_lock);
1080 kue_stop_locked(sc);
1081 mutex_exit(&sc->kue_lock);
1082 }
1083
1084 /*
1085 * Stop the adapter and free any mbufs allocated to the
1086 * RX and TX lists.
1087 */
1088 static void
1089 kue_stop_locked(struct kue_softc *sc)
1090 {
1091 usbd_status err;
1092 struct ifnet *ifp;
1093
1094 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
1095
1096 ifp = GET_IFP(sc);
1097 ifp->if_timer = 0;
1098
1099 /* Stop transfers. */
1100 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1101 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1102 if (err) {
1103 printf("%s: abort rx pipe failed: %s\n",
1104 device_xname(sc->kue_dev), usbd_errstr(err));
1105 }
1106 }
1107
1108 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1109 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1110 if (err) {
1111 printf("%s: abort tx pipe failed: %s\n",
1112 device_xname(sc->kue_dev), usbd_errstr(err));
1113 }
1114 }
1115
1116 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1117 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1118 if (err) {
1119 printf("%s: abort intr pipe failed: %s\n",
1120 device_xname(sc->kue_dev), usbd_errstr(err));
1121 }
1122 }
1123
1124 kue_rx_list_free(sc);
1125 kue_tx_list_free(sc);
1126
1127 /* Close pipes. */
1128 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1129 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1130 if (err) {
1131 printf("%s: close rx pipe failed: %s\n",
1132 device_xname(sc->kue_dev), usbd_errstr(err));
1133 }
1134 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1135 }
1136
1137 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1138 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1139 if (err) {
1140 printf("%s: close tx pipe failed: %s\n",
1141 device_xname(sc->kue_dev), usbd_errstr(err));
1142 }
1143 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1144 }
1145
1146 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1147 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1148 if (err) {
1149 printf("%s: close intr pipe failed: %s\n",
1150 device_xname(sc->kue_dev), usbd_errstr(err));
1151 }
1152 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1153 }
1154
1155 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1156 }
1157