1 /* $OpenBSD: if_kue.c,v 1.93 2024/05/23 03:21:08 jsg Exp $ */
2 /* $NetBSD: if_kue.c,v 1.50 2002/07/16 22:00:31 augustss Exp $ */
3 /*
4 * Copyright (c) 1997, 1998, 1999, 2000
5 * Bill Paul <wpaul@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@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 "bpfilter.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sockio.h>
78 #include <sys/mbuf.h>
79 #include <sys/malloc.h>
80 #include <sys/device.h>
81
82 #include <net/if.h>
83
84 #if NBPFILTER > 0
85 #include <net/bpf.h>
86 #endif
87
88 #include <netinet/in.h>
89 #include <netinet/if_ether.h>
90
91 #include <dev/usb/usb.h>
92 #include <dev/usb/usbdi.h>
93 #include <dev/usb/usbdi_util.h>
94 #include <dev/usb/usbdevs.h>
95
96 #include <dev/usb/if_kuereg.h>
97 #include <dev/usb/if_kuevar.h>
98
99 #ifdef KUE_DEBUG
100 #define DPRINTF(x) do { if (kuedebug) printf x; } while (0)
101 #define DPRINTFN(n,x) do { if (kuedebug >= (n)) printf x; } while (0)
102 int kuedebug = 0;
103 #else
104 #define DPRINTF(x)
105 #define DPRINTFN(n,x)
106 #endif
107
108 /*
109 * Various supported device vendors/products.
110 */
111 const struct usb_devno kue_devs[] = {
112 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
113 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
114 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
115 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
116 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
117 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
118 { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
119 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
120 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
121 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
122 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
123 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
124 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
125 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
126 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
127 { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
128 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
129 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
130 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
131 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
132 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
133 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
134 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
135 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
136 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
137 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
138 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
139 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
140 { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
141 { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
142 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
143 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE },
144 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
145 };
146
147 int kue_match(struct device *, void *, void *);
148 void kue_attach(struct device *, struct device *, void *);
149 int kue_detach(struct device *, int);
150
151 struct cfdriver kue_cd = {
152 NULL, "kue", DV_IFNET
153 };
154
155 const struct cfattach kue_ca = {
156 sizeof(struct kue_softc), kue_match, kue_attach, kue_detach
157 };
158
159 int kue_tx_list_init(struct kue_softc *);
160 int kue_rx_list_init(struct kue_softc *);
161 int kue_newbuf(struct kue_softc *, struct kue_chain *,struct mbuf *);
162 int kue_send(struct kue_softc *, struct mbuf *, int);
163 int kue_open_pipes(struct kue_softc *);
164 void kue_rxeof(struct usbd_xfer *, void *, usbd_status);
165 void kue_txeof(struct usbd_xfer *, void *, usbd_status);
166 void kue_start(struct ifnet *);
167 int kue_ioctl(struct ifnet *, u_long, caddr_t);
168 void kue_init(void *);
169 void kue_stop(struct kue_softc *);
170 void kue_watchdog(struct ifnet *);
171
172 void kue_setmulti(struct kue_softc *);
173 void kue_reset(struct kue_softc *);
174
175 usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
176 u_int16_t, void *, u_int32_t);
177 usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
178 int kue_load_fw(struct kue_softc *);
179 void kue_attachhook(struct device *);
180
181 usbd_status
kue_setword(struct kue_softc * sc,u_int8_t breq,u_int16_t word)182 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
183 {
184 usb_device_request_t req;
185
186 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
187
188 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
189 req.bRequest = breq;
190 USETW(req.wValue, word);
191 USETW(req.wIndex, 0);
192 USETW(req.wLength, 0);
193
194 return (usbd_do_request(sc->kue_udev, &req, NULL));
195 }
196
197 usbd_status
kue_ctl(struct kue_softc * sc,int rw,u_int8_t breq,u_int16_t val,void * data,u_int32_t len)198 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
199 void *data, u_int32_t len)
200 {
201 usb_device_request_t req;
202
203 DPRINTFN(10,("%s: %s: enter, len=%d\n", sc->kue_dev.dv_xname,
204 __func__, len));
205
206 if (rw == KUE_CTL_WRITE)
207 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
208 else
209 req.bmRequestType = UT_READ_VENDOR_DEVICE;
210
211 req.bRequest = breq;
212 USETW(req.wValue, val);
213 USETW(req.wIndex, 0);
214 USETW(req.wLength, len);
215
216 return (usbd_do_request(sc->kue_udev, &req, data));
217 }
218
219 int
kue_load_fw(struct kue_softc * sc)220 kue_load_fw(struct kue_softc *sc)
221 {
222 usb_device_descriptor_t *dd;
223 usbd_status err;
224 struct kue_firmware *fw;
225 u_char *buf;
226 size_t buflen;
227
228 DPRINTFN(1,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
229
230 /*
231 * First, check if we even need to load the firmware.
232 * If the device was still attached when the system was
233 * rebooted, it may already have firmware loaded in it.
234 * If this is the case, we don't need to do it again.
235 * And in fact, if we try to load it again, we'll hang,
236 * so we have to avoid this condition if we don't want
237 * to look stupid.
238 *
239 * We can test this quickly by checking the bcdRevision
240 * code. The NIC will return a different revision code if
241 * it's probed while the firmware is still loaded and
242 * running.
243 */
244 if ((dd = usbd_get_device_descriptor(sc->kue_udev)) == NULL)
245 return (EIO);
246 if (UGETW(dd->bcdDevice) >= KUE_WARM_REV) {
247 printf("%s: warm boot, no firmware download\n",
248 sc->kue_dev.dv_xname);
249 return (0);
250 }
251
252 err = loadfirmware("kue", &buf, &buflen);
253 if (err) {
254 printf("%s: failed loadfirmware of file %s: errno %d\n",
255 sc->kue_dev.dv_xname, "kue", err);
256 return (err);
257 }
258 fw = (struct kue_firmware *)buf;
259
260 printf("%s: cold boot, downloading firmware\n",
261 sc->kue_dev.dv_xname);
262
263 /* Load code segment */
264 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
265 sc->kue_dev.dv_xname));
266 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
267 0, (void *)&fw->data[0], ntohl(fw->codeseglen));
268 if (err) {
269 printf("%s: failed to load code segment: %s\n",
270 sc->kue_dev.dv_xname, usbd_errstr(err));
271 free(buf, M_DEVBUF, buflen);
272 return (EIO);
273 }
274
275 /* Load fixup segment */
276 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
277 sc->kue_dev.dv_xname));
278 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
279 0, (void *)&fw->data[ntohl(fw->codeseglen)], ntohl(fw->fixseglen));
280 if (err) {
281 printf("%s: failed to load fixup segment: %s\n",
282 sc->kue_dev.dv_xname, usbd_errstr(err));
283 free(buf, M_DEVBUF, buflen);
284 return (EIO);
285 }
286
287 /* Send trigger command. */
288 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
289 sc->kue_dev.dv_xname));
290 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
291 0, (void *)&fw->data[ntohl(fw->codeseglen) + ntohl(fw->fixseglen)],
292 ntohl(fw->trigseglen));
293 if (err) {
294 printf("%s: failed to load trigger segment: %s\n",
295 sc->kue_dev.dv_xname, usbd_errstr(err));
296 free(buf, M_DEVBUF, buflen);
297 return (EIO);
298 }
299 free(buf, M_DEVBUF, buflen);
300
301 usbd_delay_ms(sc->kue_udev, 10);
302
303 /*
304 * Reload device descriptor.
305 * Why? The chip without the firmware loaded returns
306 * one revision code. The chip with the firmware
307 * loaded and running returns a *different* revision
308 * code. This confuses the quirk mechanism, which is
309 * dependent on the revision data.
310 */
311 (void)usbd_reload_device_desc(sc->kue_udev);
312
313 DPRINTFN(1,("%s: %s: done\n", sc->kue_dev.dv_xname, __func__));
314
315 /* Reset the adapter. */
316 kue_reset(sc);
317
318 return (0);
319 }
320
321 void
kue_setmulti(struct kue_softc * sc)322 kue_setmulti(struct kue_softc *sc)
323 {
324 struct arpcom *ac = &sc->arpcom;
325 struct ifnet *ifp = GET_IFP(sc);
326 struct ether_multi *enm;
327 struct ether_multistep step;
328 int i;
329
330 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
331
332 if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
333 allmulti:
334 ifp->if_flags |= IFF_ALLMULTI;
335 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
336 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
337 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
338 return;
339 }
340
341 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
342
343 i = 0;
344 ETHER_FIRST_MULTI(step, ac, enm);
345 while (enm != NULL) {
346 if (i == KUE_MCFILTCNT(sc))
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 void
kue_reset(struct kue_softc * sc)369 kue_reset(struct kue_softc *sc)
370 {
371 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __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", sc->kue_dev.dv_xname);
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
kue_match(struct device * parent,void * match,void * aux)386 kue_match(struct device *parent, void *match, void *aux)
387 {
388 struct usb_attach_arg *uaa = aux;
389
390 DPRINTFN(25,("kue_match: enter\n"));
391
392 if (uaa->iface != NULL)
393 return (UMATCH_NONE);
394
395 return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ?
396 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
397 }
398
399 void
kue_attachhook(struct device * self)400 kue_attachhook(struct device *self)
401 {
402 struct kue_softc *sc = (struct kue_softc *)self;
403 int s;
404 struct ifnet *ifp;
405 struct usbd_device *dev = sc->kue_udev;
406 struct usbd_interface *iface;
407 usbd_status err;
408 usb_interface_descriptor_t *id;
409 usb_endpoint_descriptor_t *ed;
410 int i;
411
412 /* Load the firmware into the NIC. */
413 if (kue_load_fw(sc)) {
414 printf("%s: loading firmware failed\n",
415 sc->kue_dev.dv_xname);
416 return;
417 }
418
419 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
420 if (err) {
421 printf("%s: getting interface handle failed\n",
422 sc->kue_dev.dv_xname);
423 return;
424 }
425
426 sc->kue_iface = iface;
427 id = usbd_get_interface_descriptor(iface);
428
429 /* Find endpoints. */
430 for (i = 0; i < id->bNumEndpoints; i++) {
431 ed = usbd_interface2endpoint_descriptor(iface, i);
432 if (ed == NULL) {
433 printf("%s: couldn't get ep %d\n",
434 sc->kue_dev.dv_xname, i);
435 return;
436 }
437 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
438 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
439 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
440 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
441 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
442 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
443 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
444 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
445 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
446 }
447 }
448
449 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
450 printf("%s: missing endpoint\n", sc->kue_dev.dv_xname);
451 return;
452 }
453
454 /* Read ethernet descriptor */
455 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
456 0, &sc->kue_desc, sizeof(sc->kue_desc));
457 if (err) {
458 printf("%s: could not read Ethernet descriptor\n",
459 sc->kue_dev.dv_xname);
460 return;
461 }
462
463 sc->kue_mcfilters = mallocarray(KUE_MCFILTCNT(sc), ETHER_ADDR_LEN,
464 M_USBDEV, M_NOWAIT);
465 if (sc->kue_mcfilters == NULL) {
466 printf("%s: no memory for multicast filter buffer\n",
467 sc->kue_dev.dv_xname);
468 return;
469 }
470 sc->kue_mcfilterslen = KUE_MCFILTCNT(sc);
471
472 s = splnet();
473
474 /*
475 * A KLSI chip was detected. Inform the world.
476 */
477 printf("%s: address %s\n", sc->kue_dev.dv_xname,
478 ether_sprintf(sc->kue_desc.kue_macaddr));
479
480 bcopy(sc->kue_desc.kue_macaddr,
481 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
482
483 /* Initialize interface info.*/
484 ifp = GET_IFP(sc);
485 ifp->if_softc = sc;
486 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
487 ifp->if_ioctl = kue_ioctl;
488 ifp->if_start = kue_start;
489 ifp->if_watchdog = kue_watchdog;
490 strlcpy(ifp->if_xname, sc->kue_dev.dv_xname, IFNAMSIZ);
491
492 /* Attach the interface. */
493 if_attach(ifp);
494 ether_ifattach(ifp);
495
496 sc->kue_attached = 1;
497 splx(s);
498
499 }
500
501 /*
502 * Attach the interface. Allocate softc structures, do
503 * setup and ethernet/BPF attach.
504 */
505 void
kue_attach(struct device * parent,struct device * self,void * aux)506 kue_attach(struct device *parent, struct device *self, void *aux)
507 {
508 struct kue_softc *sc = (struct kue_softc *)self;
509 struct usb_attach_arg *uaa = aux;
510 struct usbd_device *dev = uaa->device;
511 usbd_status err;
512
513 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
514
515 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
516 if (err) {
517 printf("%s: setting config no failed\n",
518 sc->kue_dev.dv_xname);
519 return;
520 }
521
522 sc->kue_udev = dev;
523 sc->kue_product = uaa->product;
524 sc->kue_vendor = uaa->vendor;
525
526 config_mountroot(self, kue_attachhook);
527 }
528
529 int
kue_detach(struct device * self,int flags)530 kue_detach(struct device *self, int flags)
531 {
532 struct kue_softc *sc = (struct kue_softc *)self;
533 struct ifnet *ifp = GET_IFP(sc);
534 int s;
535
536 /* Detached before attached finished, so just bail out. */
537 if (!sc->kue_attached)
538 return (0);
539
540 s = splusb(); /* XXX why? */
541
542 if (sc->kue_mcfilters != NULL) {
543 free(sc->kue_mcfilters, M_USBDEV, sc->kue_mcfilterslen);
544 sc->kue_mcfilters = NULL;
545 }
546
547 if (ifp->if_flags & IFF_RUNNING)
548 kue_stop(sc);
549
550 if (ifp->if_softc != NULL) {
551 ether_ifdetach(ifp);
552 if_detach(ifp);
553 }
554
555 #ifdef DIAGNOSTIC
556 if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
557 sc->kue_ep[KUE_ENDPT_RX] != NULL ||
558 sc->kue_ep[KUE_ENDPT_INTR] != NULL)
559 printf("%s: detach has active endpoints\n",
560 sc->kue_dev.dv_xname);
561 #endif
562
563 sc->kue_attached = 0;
564 splx(s);
565
566 return (0);
567 }
568
569 /*
570 * Initialize an RX descriptor and attach an MBUF cluster.
571 */
572 int
kue_newbuf(struct kue_softc * sc,struct kue_chain * c,struct mbuf * m)573 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
574 {
575 struct mbuf *m_new = NULL;
576
577 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
578
579 if (m == NULL) {
580 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
581 if (m_new == NULL) {
582 printf("%s: no memory for rx list "
583 "-- packet dropped!\n", sc->kue_dev.dv_xname);
584 return (ENOBUFS);
585 }
586
587 MCLGET(m_new, M_DONTWAIT);
588 if (!(m_new->m_flags & M_EXT)) {
589 printf("%s: no memory for rx list "
590 "-- packet dropped!\n", sc->kue_dev.dv_xname);
591 m_freem(m_new);
592 return (ENOBUFS);
593 }
594 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
595 } else {
596 m_new = m;
597 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
598 m_new->m_data = m_new->m_ext.ext_buf;
599 }
600
601 c->kue_mbuf = m_new;
602
603 return (0);
604 }
605
606 int
kue_rx_list_init(struct kue_softc * sc)607 kue_rx_list_init(struct kue_softc *sc)
608 {
609 struct kue_cdata *cd;
610 struct kue_chain *c;
611 int i;
612
613 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
614
615 cd = &sc->kue_cdata;
616 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
617 c = &cd->kue_rx_chain[i];
618 c->kue_sc = sc;
619 c->kue_idx = i;
620 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
621 return (ENOBUFS);
622 if (c->kue_xfer == NULL) {
623 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
624 if (c->kue_xfer == NULL)
625 return (ENOBUFS);
626 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
627 if (c->kue_buf == NULL)
628 return (ENOBUFS); /* XXX free xfer */
629 }
630 }
631
632 return (0);
633 }
634
635 int
kue_tx_list_init(struct kue_softc * sc)636 kue_tx_list_init(struct kue_softc *sc)
637 {
638 struct kue_cdata *cd;
639 struct kue_chain *c;
640 int i;
641
642 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
643
644 cd = &sc->kue_cdata;
645 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
646 c = &cd->kue_tx_chain[i];
647 c->kue_sc = sc;
648 c->kue_idx = i;
649 c->kue_mbuf = NULL;
650 if (c->kue_xfer == NULL) {
651 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
652 if (c->kue_xfer == NULL)
653 return (ENOBUFS);
654 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
655 if (c->kue_buf == NULL)
656 return (ENOBUFS);
657 }
658 }
659
660 return (0);
661 }
662
663 /*
664 * A frame has been uploaded: pass the resulting mbuf chain up to
665 * the higher level protocols.
666 */
667 void
kue_rxeof(struct usbd_xfer * xfer,void * priv,usbd_status status)668 kue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
669 {
670 struct kue_chain *c = priv;
671 struct kue_softc *sc = c->kue_sc;
672 struct ifnet *ifp = GET_IFP(sc);
673 struct mbuf_list ml = MBUF_LIST_INITIALIZER();
674 struct mbuf *m;
675 int total_len = 0;
676 int s;
677
678 DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname,
679 __func__, status));
680
681 if (usbd_is_dying(sc->kue_udev))
682 return;
683
684 if (!(ifp->if_flags & IFF_RUNNING))
685 return;
686
687 if (status != USBD_NORMAL_COMPLETION) {
688 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
689 return;
690 sc->kue_rx_errs++;
691 if (usbd_ratecheck(&sc->kue_rx_notice)) {
692 printf("%s: %u usb errors on rx: %s\n",
693 sc->kue_dev.dv_xname, sc->kue_rx_errs,
694 usbd_errstr(status));
695 sc->kue_rx_errs = 0;
696 }
697 if (status == USBD_STALLED)
698 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
699 goto done;
700 }
701
702 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
703
704 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", sc->kue_dev.dv_xname,
705 __func__, total_len,
706 UGETW(mtod(c->kue_mbuf, u_int8_t *))));
707
708 if (total_len <= 1)
709 goto done;
710
711 m = c->kue_mbuf;
712 /* copy data to mbuf */
713 memcpy(mtod(m, char *), c->kue_buf, total_len);
714
715 /* No errors; receive the packet. */
716 total_len = UGETW(mtod(m, u_int8_t *));
717 m_adj(m, sizeof(u_int16_t));
718
719 if (total_len < sizeof(struct ether_header)) {
720 ifp->if_ierrors++;
721 goto done;
722 }
723
724 m->m_pkthdr.len = m->m_len = total_len;
725 ml_enqueue(&ml, m);
726
727 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
728 ifp->if_ierrors++;
729 goto done;
730 }
731
732 s = splnet();
733 if_input(ifp, &ml);
734 splx(s);
735
736 done:
737
738 /* Setup new transfer. */
739 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
740 c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
741 USBD_NO_TIMEOUT, kue_rxeof);
742 usbd_transfer(c->kue_xfer);
743
744 DPRINTFN(10,("%s: %s: start rx\n", sc->kue_dev.dv_xname,
745 __func__));
746 }
747
748 /*
749 * A frame was downloaded to the chip. It's safe for us to clean up
750 * the list buffers.
751 */
752
753 void
kue_txeof(struct usbd_xfer * xfer,void * priv,usbd_status status)754 kue_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
755 {
756 struct kue_chain *c = priv;
757 struct kue_softc *sc = c->kue_sc;
758 struct ifnet *ifp = GET_IFP(sc);
759 int s;
760
761 if (usbd_is_dying(sc->kue_udev))
762 return;
763
764 s = splnet();
765
766 DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname,
767 __func__, status));
768
769 ifp->if_timer = 0;
770 ifq_clr_oactive(&ifp->if_snd);
771
772 if (status != USBD_NORMAL_COMPLETION) {
773 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
774 splx(s);
775 return;
776 }
777 ifp->if_oerrors++;
778 printf("%s: usb error on tx: %s\n", sc->kue_dev.dv_xname,
779 usbd_errstr(status));
780 if (status == USBD_STALLED)
781 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
782 splx(s);
783 return;
784 }
785
786 m_freem(c->kue_mbuf);
787 c->kue_mbuf = NULL;
788
789 if (ifq_empty(&ifp->if_snd) == 0)
790 kue_start(ifp);
791
792 splx(s);
793 }
794
795 int
kue_send(struct kue_softc * sc,struct mbuf * m,int idx)796 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
797 {
798 int total_len;
799 struct kue_chain *c;
800 usbd_status err;
801
802 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
803
804 c = &sc->kue_cdata.kue_tx_chain[idx];
805
806 /*
807 * Copy the mbuf data into a contiguous buffer, leaving two
808 * bytes at the beginning to hold the frame length.
809 */
810 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
811 c->kue_mbuf = m;
812
813 total_len = m->m_pkthdr.len + 2;
814 /* XXX what's this? */
815 total_len += 64 - (total_len % 64);
816
817 /* Frame length is specified in the first 2 bytes of the buffer. */
818 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
819 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
820
821 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
822 c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
823 kue_txeof);
824
825 /* Transmit */
826 err = usbd_transfer(c->kue_xfer);
827 if (err != USBD_IN_PROGRESS) {
828 printf("%s: kue_send error=%s\n", sc->kue_dev.dv_xname,
829 usbd_errstr(err));
830 c->kue_mbuf = NULL;
831 kue_stop(sc);
832 return (EIO);
833 }
834
835 sc->kue_cdata.kue_tx_cnt++;
836
837 return (0);
838 }
839
840 void
kue_start(struct ifnet * ifp)841 kue_start(struct ifnet *ifp)
842 {
843 struct kue_softc *sc = ifp->if_softc;
844 struct mbuf *m_head = NULL;
845
846 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
847
848 if (usbd_is_dying(sc->kue_udev))
849 return;
850
851 if (ifq_is_oactive(&ifp->if_snd))
852 return;
853
854 m_head = ifq_dequeue(&ifp->if_snd);
855 if (m_head == NULL)
856 return;
857
858 if (kue_send(sc, m_head, 0)) {
859 m_freem(m_head);
860 ifq_set_oactive(&ifp->if_snd);
861 return;
862 }
863
864 #if NBPFILTER > 0
865 /*
866 * If there's a BPF listener, bounce a copy of this frame
867 * to him.
868 */
869 if (ifp->if_bpf)
870 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
871 #endif
872
873 ifq_set_oactive(&ifp->if_snd);
874
875 /*
876 * Set a timeout in case the chip goes out to lunch.
877 */
878 ifp->if_timer = 6;
879 }
880
881 void
kue_init(void * xsc)882 kue_init(void *xsc)
883 {
884 struct kue_softc *sc = xsc;
885 struct ifnet *ifp = GET_IFP(sc);
886 int s;
887 u_char *eaddr;
888
889 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
890
891 if (ifp->if_flags & IFF_RUNNING)
892 return;
893
894 s = splnet();
895
896 eaddr = sc->arpcom.ac_enaddr;
897 /* Set MAC address */
898 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
899
900 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
901
902 /* If we want promiscuous mode, set the allframes bit. */
903 if (ifp->if_flags & IFF_PROMISC)
904 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
905
906 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
907
908 /* I'm not sure how to tune these. */
909 #if 0
910 /*
911 * Leave this one alone for now; setting it
912 * wrong causes lockups on some machines/controllers.
913 */
914 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
915 #endif
916 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
917
918 /* Init TX ring. */
919 if (kue_tx_list_init(sc) == ENOBUFS) {
920 printf("%s: tx list init failed\n", sc->kue_dev.dv_xname);
921 splx(s);
922 return;
923 }
924
925 /* Init RX ring. */
926 if (kue_rx_list_init(sc) == ENOBUFS) {
927 printf("%s: rx list init failed\n", sc->kue_dev.dv_xname);
928 splx(s);
929 return;
930 }
931
932 /* Load the multicast filter. */
933 kue_setmulti(sc);
934
935 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
936 if (kue_open_pipes(sc)) {
937 splx(s);
938 return;
939 }
940 }
941
942 ifp->if_flags |= IFF_RUNNING;
943 ifq_clr_oactive(&ifp->if_snd);
944
945 splx(s);
946 }
947
948 int
kue_open_pipes(struct kue_softc * sc)949 kue_open_pipes(struct kue_softc *sc)
950 {
951 usbd_status err;
952 struct kue_chain *c;
953 int i;
954
955 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
956
957 /* Open RX and TX pipes. */
958 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
959 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
960 if (err) {
961 printf("%s: open rx pipe failed: %s\n",
962 sc->kue_dev.dv_xname, usbd_errstr(err));
963 return (EIO);
964 }
965
966 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
967 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
968 if (err) {
969 printf("%s: open tx pipe failed: %s\n",
970 sc->kue_dev.dv_xname, usbd_errstr(err));
971 return (EIO);
972 }
973
974 /* Start up the receive pipe. */
975 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
976 c = &sc->kue_cdata.kue_rx_chain[i];
977 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
978 c, c->kue_buf, KUE_BUFSZ,
979 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
980 kue_rxeof);
981 DPRINTFN(5,("%s: %s: start read\n", sc->kue_dev.dv_xname,
982 __func__));
983 usbd_transfer(c->kue_xfer);
984 }
985
986 return (0);
987 }
988
989 int
kue_ioctl(struct ifnet * ifp,u_long command,caddr_t data)990 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
991 {
992 struct kue_softc *sc = ifp->if_softc;
993 int s, error = 0;
994
995 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
996
997 if (usbd_is_dying(sc->kue_udev))
998 return ENXIO;
999
1000 #ifdef DIAGNOSTIC
1001 if (!curproc) {
1002 printf("%s: no proc!!\n", sc->kue_dev.dv_xname);
1003 return EIO;
1004 }
1005 #endif
1006
1007 s = splnet();
1008
1009 switch(command) {
1010 case SIOCSIFADDR:
1011 ifp->if_flags |= IFF_UP;
1012 kue_init(sc);
1013 break;
1014
1015 case SIOCSIFFLAGS:
1016 if (ifp->if_flags & IFF_UP) {
1017 if (ifp->if_flags & IFF_RUNNING &&
1018 ifp->if_flags & IFF_PROMISC &&
1019 !(sc->kue_if_flags & IFF_PROMISC)) {
1020 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1021 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1022 sc->kue_rxfilt);
1023 } else if (ifp->if_flags & IFF_RUNNING &&
1024 !(ifp->if_flags & IFF_PROMISC) &&
1025 sc->kue_if_flags & IFF_PROMISC) {
1026 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1027 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1028 sc->kue_rxfilt);
1029 } else if (!(ifp->if_flags & IFF_RUNNING))
1030 kue_init(sc);
1031 } else {
1032 if (ifp->if_flags & IFF_RUNNING)
1033 kue_stop(sc);
1034 }
1035 sc->kue_if_flags = ifp->if_flags;
1036 error = 0;
1037 break;
1038
1039 default:
1040 error = ether_ioctl(ifp, &sc->arpcom, command, data);
1041 }
1042
1043 if (error == ENETRESET) {
1044 if (ifp->if_flags & IFF_RUNNING)
1045 kue_setmulti(sc);
1046 error = 0;
1047 }
1048
1049 splx(s);
1050 return (error);
1051 }
1052
1053 void
kue_watchdog(struct ifnet * ifp)1054 kue_watchdog(struct ifnet *ifp)
1055 {
1056 struct kue_softc *sc = ifp->if_softc;
1057 struct kue_chain *c;
1058 usbd_status stat;
1059 int s;
1060
1061 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1062
1063 if (usbd_is_dying(sc->kue_udev))
1064 return;
1065
1066 ifp->if_oerrors++;
1067 printf("%s: watchdog timeout\n", sc->kue_dev.dv_xname);
1068
1069 s = splusb();
1070 c = &sc->kue_cdata.kue_tx_chain[0];
1071 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1072 kue_txeof(c->kue_xfer, c, stat);
1073
1074 if (ifq_empty(&ifp->if_snd) == 0)
1075 kue_start(ifp);
1076 splx(s);
1077 }
1078
1079 /*
1080 * Stop the adapter and free any mbufs allocated to the
1081 * RX and TX lists.
1082 */
1083 void
kue_stop(struct kue_softc * sc)1084 kue_stop(struct kue_softc *sc)
1085 {
1086 usbd_status err;
1087 struct ifnet *ifp;
1088 int i;
1089
1090 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1091
1092 ifp = GET_IFP(sc);
1093 ifp->if_timer = 0;
1094 ifp->if_flags &= ~IFF_RUNNING;
1095 ifq_clr_oactive(&ifp->if_snd);
1096
1097 /* Stop transfers. */
1098 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1099 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1100 if (err) {
1101 printf("%s: close rx pipe failed: %s\n",
1102 sc->kue_dev.dv_xname, usbd_errstr(err));
1103 }
1104 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1105 }
1106
1107 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1108 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1109 if (err) {
1110 printf("%s: close tx pipe failed: %s\n",
1111 sc->kue_dev.dv_xname, usbd_errstr(err));
1112 }
1113 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1114 }
1115
1116 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1117 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1118 if (err) {
1119 printf("%s: close intr pipe failed: %s\n",
1120 sc->kue_dev.dv_xname, usbd_errstr(err));
1121 }
1122 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1123 }
1124
1125 /* Free RX resources. */
1126 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1127 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1128 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1129 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1130 }
1131 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1132 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1133 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1134 }
1135 }
1136
1137 /* Free TX resources. */
1138 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1139 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1140 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1141 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1142 }
1143 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1144 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1145 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1146 }
1147 }
1148 }
1149