network code: Convert if_multiaddrs from LIST to TAILQ.
[dragonfly.git] / sys / dev / netif / kue / if_kue.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1997, 1998, 1999, 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 *
198d1293 32 * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.17.2.9 2003/04/13 02:39:25 murray Exp $
9db4b353 33 * $DragonFly: src/sys/dev/netif/kue/if_kue.c,v 1.30 2008/05/14 11:59:20 sephe Exp $
984263bc
MD
34 */
35
36/*
37 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
38 *
39 * Written by Bill Paul <wpaul@ee.columbia.edu>
40 * Electrical Engineering Department
41 * Columbia University, New York City
42 */
43
44/*
45 * The KLSI USB to ethernet adapter chip contains an USB serial interface,
46 * ethernet MAC and embedded microcontroller (called the QT Engine).
47 * The chip must have firmware loaded into it before it will operate.
48 * Packets are passed between the chip and host via bulk transfers.
49 * There is an interrupt endpoint mentioned in the software spec, however
50 * it's currently unused. This device is 10Mbps half-duplex only, hence
51 * there is no media selection logic. The MAC supports a 128 entry
52 * multicast filter, though the exact size of the filter can depend
53 * on the firmware. Curiously, while the software spec describes various
54 * ethernet statistics counters, my sample adapter and firmware combination
55 * claims not to support any statistics counters at all.
56 *
57 * Note that once we load the firmware in the device, we have to be
58 * careful not to load it again: if you restart your computer but
59 * leave the adapter attached to the USB controller, it may remain
60 * powered on and retain its firmware. In this case, we don't need
61 * to load the firmware a second time.
62 *
63 * Special thanks to Rob Furr for providing an ADS Technologies
64 * adapter for development and testing. No monkeys were harmed during
65 * the development of this driver.
66 */
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/sockio.h>
71#include <sys/mbuf.h>
72#include <sys/malloc.h>
73#include <sys/kernel.h>
74#include <sys/socket.h>
1f7ab7c9 75#include <sys/bus.h>
984263bc
MD
76
77#include <net/if.h>
ca3f7aba 78#include <net/ifq_var.h>
984263bc
MD
79#include <net/if_arp.h>
80#include <net/ethernet.h>
81#include <net/if_dl.h>
82#include <net/if_media.h>
984263bc
MD
83#include <net/bpf.h>
84
1550dfd9 85#include <machine/clock.h>
984263bc 86
1f2de5d4
MD
87#include <bus/usb/usb.h>
88#include <bus/usb/usbdi.h>
89#include <bus/usb/usbdi_util.h>
90#include <bus/usb/usbdivar.h>
1f2de5d4 91#include <bus/usb/usb_ethersubr.h>
984263bc 92
1f2de5d4
MD
93#include "if_kuereg.h"
94#include <bus/usb/kue_fw.h>
984263bc 95
1550dfd9 96MODULE_DEPEND(kue, usb, 1, 1, 1);
1550dfd9 97
984263bc
MD
98/*
99 * Various supported device vendors/products.
100 */
3f56796e
HT
101static const struct usb_devno kue_devs[] = {
102 { USB_DEVICE(0x03e8, 0x0008) }, /* AOX USB101 */
103 { USB_DEVICE(0x04bb, 0x0901) }, /* I-O DATA USBETT */
104 { USB_DEVICE(0x0506, 0x03e8) }, /* 3Com 3C19250 */
105 { USB_DEVICE(0x0557, 0x2002) }, /* ATen UC10T */
106 { USB_DEVICE(0x0565, 0x0002) }, /* Peracom Ethernet adapter */
107 { USB_DEVICE(0x0565, 0x0003) }, /* Peracom Ethernet adapter*/
108 { USB_DEVICE(0x0565, 0x0005) }, /* Peracom Ethernet adapter */
109 { USB_DEVICE(0x05e9, 0x0008) }, /* AOX (rebranded Kawasaki?) USB101*/
110 { USB_DEVICE(0x05e9, 0x0008) }, /* Kawasaki LSI DUH3E10BT */
111 { USB_DEVICE(0x05e9, 0x0009) }, /* Kawasaki LSI DUH3E10BTN */
112 { USB_DEVICE(0x066b, 0x2202) }, /* Linksys USB10T */
113 { USB_DEVICE(0x06e1, 0x0008) }, /* ADS UBS-10BT */
114 { USB_DEVICE(0x0707, 0x0100) }, /* SMC 2102USB */
115 { USB_DEVICE(0x07aa, 0x0001) }, /* Corega USB-T*/
116 { USB_DEVICE(0x07b8, 0x4000) }, /* AboCom URE450 */
117 { USB_DEVICE(0x0846, 0x1001) }, /* Netgear EA101 */
118 { USB_DEVICE(0x1645, 0x0005) }, /* Entrega E45*/
119 { USB_DEVICE(0x2001, 0x4000) }, /* D-Link DSB650C */
984263bc
MD
120};
121
6ed427ca
HT
122static int kue_match(device_t);
123static int kue_attach(device_t);
124static int kue_detach(device_t);
125static void kue_shutdown(device_t);
126static int kue_tx_list_init(struct kue_softc *);
127static int kue_rx_list_init(struct kue_softc *);
128static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
129static int kue_encap(struct kue_softc *, struct mbuf *, int);
130static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
131static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
132static void kue_start(struct ifnet *);
133static void kue_rxstart(struct ifnet *);
134static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
135static void kue_init(void *);
136static void kue_stop(struct kue_softc *);
137static void kue_watchdog(struct ifnet *);
138
139static void kue_setmulti(struct kue_softc *);
140static void kue_reset(struct kue_softc *);
141
142static usbd_status kue_do_request(usbd_device_handle,
984263bc 143 usb_device_request_t *, void *);
6ed427ca 144static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
984263bc 145 u_int16_t, char *, int);
6ed427ca
HT
146static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
147static int kue_load_fw(struct kue_softc *);
984263bc 148
6ed427ca 149static device_method_t kue_methods[] = {
984263bc
MD
150 /* Device interface */
151 DEVMETHOD(device_probe, kue_match),
152 DEVMETHOD(device_attach, kue_attach),
153 DEVMETHOD(device_detach, kue_detach),
154 DEVMETHOD(device_shutdown, kue_shutdown),
155
156 { 0, 0 }
157};
158
6ed427ca 159static driver_t kue_driver = {
984263bc
MD
160 "kue",
161 kue_methods,
162 sizeof(struct kue_softc)
163};
164
6ed427ca 165static devclass_t kue_devclass;
984263bc 166
32832096 167DECLARE_DUMMY_MODULE(if_kue);
1550dfd9 168DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
984263bc
MD
169
170/*
171 * We have a custom do_request function which is almost like the
172 * regular do_request function, except it has a much longer timeout.
173 * Why? Because we need to make requests over the control endpoint
174 * to download the firmware to the device, which can take longer
175 * than the default timeout.
176 */
6ed427ca 177static usbd_status
984263bc
MD
178kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
179{
180 usbd_xfer_handle xfer;
181 usbd_status err;
182
183 xfer = usbd_alloc_xfer(dev);
184 usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
1550dfd9 185 data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
984263bc
MD
186 err = usbd_sync_transfer(xfer);
187 usbd_free_xfer(xfer);
188 return(err);
189}
190
6ed427ca 191static usbd_status
984263bc
MD
192kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
193{
194 usbd_device_handle dev;
195 usb_device_request_t req;
196 usbd_status err;
984263bc 197
1550dfd9 198 if (sc->kue_dying)
984263bc
MD
199 return(USBD_NORMAL_COMPLETION);
200
201 dev = sc->kue_udev;
202
1550dfd9 203 KUE_LOCK(sc);
984263bc
MD
204
205 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
206
207 req.bRequest = breq;
208 USETW(req.wValue, word);
209 USETW(req.wIndex, 0);
210 USETW(req.wLength, 0);
211
212 err = kue_do_request(dev, &req, NULL);
213
1550dfd9 214 KUE_UNLOCK(sc);
984263bc
MD
215
216 return(err);
217}
218
6ed427ca 219static usbd_status
984263bc
MD
220kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
221 char *data, int len)
222{
223 usbd_device_handle dev;
224 usb_device_request_t req;
225 usbd_status err;
984263bc
MD
226
227 dev = sc->kue_udev;
228
1550dfd9 229 if (sc->kue_dying)
984263bc
MD
230 return(USBD_NORMAL_COMPLETION);
231
1550dfd9 232 KUE_LOCK(sc);
984263bc
MD
233
234 if (rw == KUE_CTL_WRITE)
235 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
236 else
237 req.bmRequestType = UT_READ_VENDOR_DEVICE;
238
239 req.bRequest = breq;
240 USETW(req.wValue, val);
241 USETW(req.wIndex, 0);
242 USETW(req.wLength, len);
243
244 err = kue_do_request(dev, &req, data);
245
1550dfd9 246 KUE_UNLOCK(sc);
984263bc
MD
247
248 return(err);
249}
250
6ed427ca 251static int
984263bc
MD
252kue_load_fw(struct kue_softc *sc)
253{
254 usbd_status err;
255 usb_device_descriptor_t *dd;
256 int hwrev;
257
258 dd = &sc->kue_udev->ddesc;
259 hwrev = UGETW(dd->bcdDevice);
260
261 /*
262 * First, check if we even need to load the firmware.
263 * If the device was still attached when the system was
264 * rebooted, it may already have firmware loaded in it.
265 * If this is the case, we don't need to do it again.
266 * And in fact, if we try to load it again, we'll hang,
267 * so we have to avoid this condition if we don't want
268 * to look stupid.
269 *
270 * We can test this quickly by checking the bcdRevision
271 * code. The NIC will return a different revision code if
272 * it's probed while the firmware is still loaded and
273 * running.
274 */
275 if (hwrev == 0x0202)
276 return(0);
277
278 /* Load code segment */
279 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
280 0, kue_code_seg, sizeof(kue_code_seg));
281 if (err) {
e3869ec7 282 kprintf("kue%d: failed to load code segment: %s\n",
984263bc
MD
283 sc->kue_unit, usbd_errstr(err));
284 return(ENXIO);
285 }
286
287 /* Load fixup segment */
288 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
289 0, kue_fix_seg, sizeof(kue_fix_seg));
290 if (err) {
e3869ec7 291 kprintf("kue%d: failed to load fixup segment: %s\n",
984263bc
MD
292 sc->kue_unit, usbd_errstr(err));
293 return(ENXIO);
294 }
295
296 /* Send trigger command. */
297 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
298 0, kue_trig_seg, sizeof(kue_trig_seg));
299 if (err) {
e3869ec7 300 kprintf("kue%d: failed to load trigger segment: %s\n",
984263bc
MD
301 sc->kue_unit, usbd_errstr(err));
302 return(ENXIO);
303 }
304
305 return(0);
306}
307
6ed427ca 308static void
984263bc
MD
309kue_setmulti(struct kue_softc *sc)
310{
311 struct ifnet *ifp;
312 struct ifmultiaddr *ifma;
313 int i = 0;
314
315 ifp = &sc->arpcom.ac_if;
316
317 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
318 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
319 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
320 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
321 return;
322 }
323
324 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
325
441d34b2 326 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
984263bc
MD
327 if (ifma->ifma_addr->sa_family != AF_LINK)
328 continue;
329 /*
330 * If there are too many addresses for the
331 * internal filter, switch over to allmulti mode.
332 */
333 if (i == KUE_MCFILTCNT(sc))
334 break;
335 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
336 KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
337 i++;
338 }
339
340 if (i == KUE_MCFILTCNT(sc))
341 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
342 else {
343 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
344 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
345 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
346 }
347
348 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
349
350 return;
351}
352
353/*
354 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
355 * done after the firmware is loaded into the adapter in order to
356 * bring it into proper operation.
357 */
6ed427ca 358static void
984263bc
MD
359kue_reset(struct kue_softc *sc)
360{
1550dfd9
MD
361 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
362 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
363 &sc->kue_iface)) {
e3869ec7 364 kprintf("kue%d: getting interface handle failed\n",
984263bc
MD
365 sc->kue_unit);
366 }
367
368 /* Wait a little while for the chip to get its brains in order. */
369 DELAY(1000);
370 return;
371}
372
373/*
374 * Probe for a KLSI chip.
375 */
e785a5d9
HT
376static int
377kue_match(device_t self)
984263bc 378{
e785a5d9 379 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 380
3f56796e 381 if (uaa->iface == NULL)
984263bc
MD
382 return(UMATCH_NONE);
383
3f56796e
HT
384 return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ?
385 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
984263bc
MD
386}
387
388/*
389 * Attach the interface. Allocate softc structures, do
390 * setup and ethernet/BPF attach.
391 */
e785a5d9
HT
392static int
393kue_attach(device_t self)
984263bc 394{
e785a5d9
HT
395 struct kue_softc *sc = device_get_softc(self);
396 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
397 struct ifnet *ifp;
398 usbd_status err;
399 usb_interface_descriptor_t *id;
400 usb_endpoint_descriptor_t *ed;
401 int i;
402
984263bc
MD
403 sc->kue_iface = uaa->iface;
404 sc->kue_udev = uaa->device;
405 sc->kue_unit = device_get_unit(self);
406
407 id = usbd_get_interface_descriptor(uaa->iface);
408
984263bc
MD
409 /* Find endpoints. */
410 for (i = 0; i < id->bNumEndpoints; i++) {
411 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
412 if (!ed) {
e3869ec7 413 kprintf("kue%d: couldn't get ep %d\n",
984263bc 414 sc->kue_unit, i);
e785a5d9 415 return ENXIO;
984263bc
MD
416 }
417 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 418 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
419 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
420 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1550dfd9 421 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
422 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
423 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 424 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
984263bc
MD
425 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
426 }
427 }
428
929783d0 429#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
430 mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
431 MTX_DEF | MTX_RECURSE);
432#endif
433 KUE_LOCK(sc);
434
984263bc
MD
435 /* Load the firmware into the NIC. */
436 if (kue_load_fw(sc)) {
1550dfd9 437 KUE_UNLOCK(sc);
929783d0 438#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
439 mtx_destroy(&sc->kue_mtx);
440#endif
e785a5d9 441 return ENXIO;
984263bc
MD
442 }
443
444 /* Reset the adapter. */
445 kue_reset(sc);
446
447 /* Read ethernet descriptor */
448 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
449 0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
450
77652cad 451 sc->kue_mcfilters = kmalloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
c5541aee 452 M_USBDEV, M_WAITOK);
984263bc 453
984263bc
MD
454 ifp = &sc->arpcom.ac_if;
455 ifp->if_softc = sc;
1550dfd9 456 if_initname(ifp, "kue", sc->kue_unit);
984263bc
MD
457 ifp->if_mtu = ETHERMTU;
458 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
459 ifp->if_ioctl = kue_ioctl;
984263bc
MD
460 ifp->if_start = kue_start;
461 ifp->if_watchdog = kue_watchdog;
462 ifp->if_init = kue_init;
463 ifp->if_baudrate = 10000000;
ca3f7aba
JS
464 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
465 ifq_set_ready(&ifp->if_snd);
984263bc
MD
466
467 /*
468 * Call MI attach routine.
469 */
78195a76 470 ether_ifattach(ifp, sc->kue_desc.kue_macaddr, NULL);
984263bc 471 usb_register_netisr();
1550dfd9
MD
472 sc->kue_dying = 0;
473
474 KUE_UNLOCK(sc);
984263bc 475
e785a5d9 476 return 0;
984263bc
MD
477}
478
6ed427ca
HT
479static int
480kue_detach(device_t dev)
984263bc
MD
481{
482 struct kue_softc *sc;
483 struct ifnet *ifp;
984263bc
MD
484
485 sc = device_get_softc(dev);
1550dfd9 486 KUE_LOCK(sc);
984263bc
MD
487 ifp = &sc->arpcom.ac_if;
488
1550dfd9 489 sc->kue_dying = 1;
984263bc
MD
490
491 if (ifp != NULL)
1550dfd9 492 ether_ifdetach(ifp);
984263bc
MD
493
494 if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
495 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
496 if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
497 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
498 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
499 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
500
501 if (sc->kue_mcfilters != NULL)
efda3bd0 502 kfree(sc->kue_mcfilters, M_USBDEV);
984263bc 503
1550dfd9 504 KUE_UNLOCK(sc);
929783d0 505#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
506 mtx_destroy(&sc->kue_mtx);
507#endif
984263bc
MD
508
509 return(0);
510}
511
512/*
513 * Initialize an RX descriptor and attach an MBUF cluster.
514 */
6ed427ca 515static int
984263bc
MD
516kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
517{
518 struct mbuf *m_new = NULL;
519
520 if (m == NULL) {
74f1caca 521 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
984263bc 522 if (m_new == NULL) {
e3869ec7 523 kprintf("kue%d: no memory for rx list "
984263bc
MD
524 "-- packet dropped!\n", sc->kue_unit);
525 return(ENOBUFS);
526 }
527
74f1caca 528 MCLGET(m_new, MB_DONTWAIT);
984263bc 529 if (!(m_new->m_flags & M_EXT)) {
e3869ec7 530 kprintf("kue%d: no memory for rx list "
984263bc
MD
531 "-- packet dropped!\n", sc->kue_unit);
532 m_freem(m_new);
533 return(ENOBUFS);
534 }
535 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
536 } else {
537 m_new = m;
538 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
539 m_new->m_data = m_new->m_ext.ext_buf;
540 }
541
542 c->kue_mbuf = m_new;
543
544 return(0);
545}
546
6ed427ca 547static int
984263bc
MD
548kue_rx_list_init(struct kue_softc *sc)
549{
550 struct kue_cdata *cd;
551 struct kue_chain *c;
552 int i;
553
554 cd = &sc->kue_cdata;
555 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
556 c = &cd->kue_rx_chain[i];
557 c->kue_sc = sc;
558 c->kue_idx = i;
559 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
560 return(ENOBUFS);
561 if (c->kue_xfer == NULL) {
562 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
563 if (c->kue_xfer == NULL)
564 return(ENOBUFS);
565 }
566 }
567
568 return(0);
569}
570
6ed427ca 571static int
984263bc
MD
572kue_tx_list_init(struct kue_softc *sc)
573{
574 struct kue_cdata *cd;
575 struct kue_chain *c;
576 int i;
577
578 cd = &sc->kue_cdata;
579 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
580 c = &cd->kue_tx_chain[i];
581 c->kue_sc = sc;
582 c->kue_idx = i;
583 c->kue_mbuf = NULL;
584 if (c->kue_xfer == NULL) {
585 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
586 if (c->kue_xfer == NULL)
587 return(ENOBUFS);
588 }
efda3bd0 589 c->kue_buf = kmalloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
984263bc
MD
590 }
591
592 return(0);
593}
594
6ed427ca 595static void
984263bc
MD
596kue_rxstart(struct ifnet *ifp)
597{
598 struct kue_softc *sc;
599 struct kue_chain *c;
600
601 sc = ifp->if_softc;
1550dfd9 602 KUE_LOCK(sc);
984263bc
MD
603 c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
604
605 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
606 ifp->if_ierrors++;
607 return;
608 }
609
610 /* Setup new transfer. */
611 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
612 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
613 USBD_NO_TIMEOUT, kue_rxeof);
614 usbd_transfer(c->kue_xfer);
615
1550dfd9
MD
616 KUE_UNLOCK(sc);
617
984263bc
MD
618 return;
619}
620
621/*
622 * A frame has been uploaded: pass the resulting mbuf chain up to
623 * the higher level protocols.
624 */
6ed427ca 625static void
7b9f668c 626kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
984263bc
MD
627{
628 struct kue_softc *sc;
629 struct kue_chain *c;
630 struct mbuf *m;
631 struct ifnet *ifp;
632 int total_len = 0;
633 u_int16_t len;
634
635 c = priv;
636 sc = c->kue_sc;
1550dfd9 637 KUE_LOCK(sc);
984263bc
MD
638 ifp = &sc->arpcom.ac_if;
639
1550dfd9
MD
640 if (!(ifp->if_flags & IFF_RUNNING)) {
641 KUE_UNLOCK(sc);
984263bc 642 return;
1550dfd9 643 }
984263bc
MD
644
645 if (status != USBD_NORMAL_COMPLETION) {
1550dfd9
MD
646 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
647 KUE_UNLOCK(sc);
984263bc 648 return;
1550dfd9
MD
649 }
650 if (usbd_ratecheck(&sc->kue_rx_notice))
e3869ec7 651 kprintf("kue%d: usb error on rx: %s\n", sc->kue_unit,
1550dfd9 652 usbd_errstr(status));
984263bc
MD
653 if (status == USBD_STALLED)
654 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
655 goto done;
656 }
657
658 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
659 m = c->kue_mbuf;
660 if (total_len <= 1)
661 goto done;
662
663 len = *mtod(m, u_int16_t *);
664 m_adj(m, sizeof(u_int16_t));
665
666 /* No errors; receive the packet. */
667 total_len = len;
668
669 if (len < sizeof(struct ether_header)) {
670 ifp->if_ierrors++;
671 goto done;
672 }
673
674 ifp->if_ipackets++;
ca3f7aba 675 m->m_pkthdr.rcvif = ifp;
984263bc
MD
676 m->m_pkthdr.len = m->m_len = total_len;
677
678 /* Put the packet on the special USB input queue. */
679 usb_ether_input(m);
ca3f7aba
JS
680 kue_rxstart(ifp);
681
1550dfd9 682 KUE_UNLOCK(sc);
984263bc
MD
683
684 return;
685done:
686
687 /* Setup new transfer. */
688 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
689 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
690 USBD_NO_TIMEOUT, kue_rxeof);
691 usbd_transfer(c->kue_xfer);
1550dfd9 692 KUE_UNLOCK(sc);
984263bc
MD
693
694 return;
695}
696
697/*
698 * A frame was downloaded to the chip. It's safe for us to clean up
699 * the list buffers.
700 */
701
6ed427ca 702static void
984263bc
MD
703kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
704{
705 struct kue_softc *sc;
706 struct kue_chain *c;
707 struct ifnet *ifp;
708 usbd_status err;
984263bc
MD
709
710 c = priv;
711 sc = c->kue_sc;
1550dfd9
MD
712 KUE_LOCK(sc);
713
984263bc
MD
714 ifp = &sc->arpcom.ac_if;
715 ifp->if_timer = 0;
716 ifp->if_flags &= ~IFF_OACTIVE;
717
718 if (status != USBD_NORMAL_COMPLETION) {
719 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1550dfd9 720 KUE_UNLOCK(sc);
984263bc
MD
721 return;
722 }
e3869ec7 723 kprintf("kue%d: usb error on tx: %s\n", sc->kue_unit,
984263bc
MD
724 usbd_errstr(status));
725 if (status == USBD_STALLED)
726 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
1550dfd9 727 KUE_UNLOCK(sc);
984263bc
MD
728 return;
729 }
730
731 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
732
733 if (c->kue_mbuf != NULL) {
ca3f7aba 734 m_freem(c->kue_mbuf);
984263bc
MD
735 c->kue_mbuf = NULL;
736 }
737
738 if (err)
739 ifp->if_oerrors++;
740 else
741 ifp->if_opackets++;
742
ca3f7aba 743 if (!ifq_is_empty(&ifp->if_snd))
9db4b353 744 if_devstart(ifp);
ca3f7aba 745
1550dfd9 746 KUE_UNLOCK(sc);
984263bc
MD
747
748 return;
749}
750
6ed427ca 751static int
984263bc
MD
752kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
753{
754 int total_len;
755 struct kue_chain *c;
756 usbd_status err;
757
758 c = &sc->kue_cdata.kue_tx_chain[idx];
759
760 /*
761 * Copy the mbuf data into a contiguous buffer, leaving two
762 * bytes at the beginning to hold the frame length.
763 */
764 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
765 c->kue_mbuf = m;
766
767 total_len = m->m_pkthdr.len + 2;
768 total_len += 64 - (total_len % 64);
769
770 /* Frame length is specified in the first 2 bytes of the buffer. */
771 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
772 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
773
774 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
775 c, c->kue_buf, total_len, 0, 10000, kue_txeof);
776
777 /* Transmit */
778 err = usbd_transfer(c->kue_xfer);
779 if (err != USBD_IN_PROGRESS) {
780 kue_stop(sc);
781 return(EIO);
782 }
783
784 sc->kue_cdata.kue_tx_cnt++;
785
786 return(0);
787}
788
6ed427ca 789static void
984263bc
MD
790kue_start(struct ifnet *ifp)
791{
792 struct kue_softc *sc;
793 struct mbuf *m_head = NULL;
794
795 sc = ifp->if_softc;
1550dfd9 796 KUE_LOCK(sc);
984263bc 797
1550dfd9
MD
798 if (ifp->if_flags & IFF_OACTIVE) {
799 KUE_UNLOCK(sc);
984263bc 800 return;
1550dfd9 801 }
984263bc 802
9db4b353 803 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1550dfd9
MD
804 if (m_head == NULL) {
805 KUE_UNLOCK(sc);
984263bc 806 return;
1550dfd9 807 }
984263bc
MD
808
809 if (kue_encap(sc, m_head, 0)) {
9db4b353 810 /* kue_encap() will free m_head, if we reach here */
984263bc 811 ifp->if_flags |= IFF_OACTIVE;
1550dfd9 812 KUE_UNLOCK(sc);
984263bc
MD
813 return;
814 }
815
816 /*
817 * If there's a BPF listener, bounce a copy of this frame
818 * to him.
819 */
1550dfd9 820 BPF_MTAP(ifp, m_head);
984263bc
MD
821
822 ifp->if_flags |= IFF_OACTIVE;
823
824 /*
825 * Set a timeout in case the chip goes out to lunch.
826 */
827 ifp->if_timer = 5;
1550dfd9 828 KUE_UNLOCK(sc);
984263bc
MD
829
830 return;
831}
832
6ed427ca 833static void
984263bc
MD
834kue_init(void *xsc)
835{
836 struct kue_softc *sc = xsc;
837 struct ifnet *ifp = &sc->arpcom.ac_if;
838 struct kue_chain *c;
839 usbd_status err;
1550dfd9 840 int i;
984263bc 841
1550dfd9 842 KUE_LOCK(sc);
984263bc 843
1550dfd9
MD
844 if (ifp->if_flags & IFF_RUNNING) {
845 KUE_UNLOCK(sc);
846 return;
847 }
984263bc
MD
848
849 /* Set MAC address */
850 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
851 0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
852
853 sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
854
855 /* If we want promiscuous mode, set the allframes bit. */
856 if (ifp->if_flags & IFF_PROMISC)
857 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
858
859 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
860
861 /* I'm not sure how to tune these. */
862#ifdef notdef
863 /*
864 * Leave this one alone for now; setting it
865 * wrong causes lockups on some machines/controllers.
866 */
867 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
868#endif
869 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
870
871 /* Init TX ring. */
872 if (kue_tx_list_init(sc) == ENOBUFS) {
e3869ec7 873 kprintf("kue%d: tx list init failed\n", sc->kue_unit);
1550dfd9 874 KUE_UNLOCK(sc);
984263bc
MD
875 return;
876 }
877
878 /* Init RX ring. */
879 if (kue_rx_list_init(sc) == ENOBUFS) {
e3869ec7 880 kprintf("kue%d: rx list init failed\n", sc->kue_unit);
1550dfd9 881 KUE_UNLOCK(sc);
984263bc
MD
882 return;
883 }
884
885 /* Load the multicast filter. */
886 kue_setmulti(sc);
887
888 /* Open RX and TX pipes. */
889 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
890 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
891 if (err) {
e3869ec7 892 kprintf("kue%d: open rx pipe failed: %s\n",
984263bc 893 sc->kue_unit, usbd_errstr(err));
1550dfd9 894 KUE_UNLOCK(sc);
984263bc
MD
895 return;
896 }
897
898 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
899 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
900 if (err) {
e3869ec7 901 kprintf("kue%d: open tx pipe failed: %s\n",
984263bc 902 sc->kue_unit, usbd_errstr(err));
1550dfd9 903 KUE_UNLOCK(sc);
984263bc
MD
904 return;
905 }
906
907 /* Start up the receive pipe. */
908 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
909 c = &sc->kue_cdata.kue_rx_chain[i];
910 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
911 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
912 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
913 usbd_transfer(c->kue_xfer);
914 }
915
916 ifp->if_flags |= IFF_RUNNING;
917 ifp->if_flags &= ~IFF_OACTIVE;
918
1550dfd9 919 KUE_UNLOCK(sc);
984263bc
MD
920
921 return;
922}
923
6ed427ca 924static int
bd4539cc 925kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
984263bc
MD
926{
927 struct kue_softc *sc = ifp->if_softc;
1550dfd9 928 int error = 0;
984263bc 929
1550dfd9 930 KUE_LOCK(sc);
984263bc
MD
931
932 switch(command) {
984263bc
MD
933 case SIOCSIFFLAGS:
934 if (ifp->if_flags & IFF_UP) {
935 if (ifp->if_flags & IFF_RUNNING &&
936 ifp->if_flags & IFF_PROMISC &&
937 !(sc->kue_if_flags & IFF_PROMISC)) {
938 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
939 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
940 sc->kue_rxfilt);
941 } else if (ifp->if_flags & IFF_RUNNING &&
942 !(ifp->if_flags & IFF_PROMISC) &&
943 sc->kue_if_flags & IFF_PROMISC) {
944 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
945 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
946 sc->kue_rxfilt);
947 } else if (!(ifp->if_flags & IFF_RUNNING))
948 kue_init(sc);
949 } else {
950 if (ifp->if_flags & IFF_RUNNING)
951 kue_stop(sc);
952 }
953 sc->kue_if_flags = ifp->if_flags;
954 error = 0;
955 break;
956 case SIOCADDMULTI:
957 case SIOCDELMULTI:
958 kue_setmulti(sc);
959 error = 0;
960 break;
961 default:
1550dfd9 962 error = ether_ioctl(ifp, command, data);
984263bc
MD
963 break;
964 }
965
1550dfd9 966 KUE_UNLOCK(sc);
984263bc
MD
967
968 return(error);
969}
970
6ed427ca 971static void
984263bc
MD
972kue_watchdog(struct ifnet *ifp)
973{
974 struct kue_softc *sc;
975 struct kue_chain *c;
976 usbd_status stat;
977
978 sc = ifp->if_softc;
1550dfd9 979 KUE_LOCK(sc);
984263bc 980 ifp->if_oerrors++;
e3869ec7 981 kprintf("kue%d: watchdog timeout\n", sc->kue_unit);
984263bc
MD
982
983 c = &sc->kue_cdata.kue_tx_chain[0];
984 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
985 kue_txeof(c->kue_xfer, c, stat);
986
9db4b353
SZ
987 if (!ifq_is_empty(&ifp->if_snd))
988 if_devstart(ifp);
1550dfd9 989 KUE_UNLOCK(sc);
984263bc
MD
990
991 return;
992}
993
994/*
995 * Stop the adapter and free any mbufs allocated to the
996 * RX and TX lists.
997 */
6ed427ca 998static void
984263bc
MD
999kue_stop(struct kue_softc *sc)
1000{
1001 usbd_status err;
1002 struct ifnet *ifp;
1003 int i;
1004
1550dfd9 1005 KUE_LOCK(sc);
984263bc
MD
1006 ifp = &sc->arpcom.ac_if;
1007 ifp->if_timer = 0;
1008
1009 /* Stop transfers. */
1010 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1011 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1012 if (err) {
e3869ec7 1013 kprintf("kue%d: abort rx pipe failed: %s\n",
984263bc
MD
1014 sc->kue_unit, usbd_errstr(err));
1015 }
1016 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1017 if (err) {
e3869ec7 1018 kprintf("kue%d: close rx pipe failed: %s\n",
984263bc
MD
1019 sc->kue_unit, usbd_errstr(err));
1020 }
1021 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1022 }
1023
1024 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1025 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1026 if (err) {
e3869ec7 1027 kprintf("kue%d: abort tx pipe failed: %s\n",
984263bc
MD
1028 sc->kue_unit, usbd_errstr(err));
1029 }
1030 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1031 if (err) {
e3869ec7 1032 kprintf("kue%d: close tx pipe failed: %s\n",
984263bc
MD
1033 sc->kue_unit, usbd_errstr(err));
1034 }
1035 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1036 }
1037
1038 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1039 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1040 if (err) {
e3869ec7 1041 kprintf("kue%d: abort intr pipe failed: %s\n",
984263bc
MD
1042 sc->kue_unit, usbd_errstr(err));
1043 }
1044 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1045 if (err) {
e3869ec7 1046 kprintf("kue%d: close intr pipe failed: %s\n",
984263bc
MD
1047 sc->kue_unit, usbd_errstr(err));
1048 }
1049 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1050 }
1051
1052 /* Free RX resources. */
1053 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1054 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
efda3bd0 1055 kfree(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
984263bc
MD
1056 sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
1057 }
1058 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1059 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1060 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1061 }
1062 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1063 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1064 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1065 }
1066 }
1067
1068 /* Free TX resources. */
1069 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1070 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
efda3bd0 1071 kfree(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
984263bc
MD
1072 sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
1073 }
1074 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1075 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1076 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1077 }
1078 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1079 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1080 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1081 }
1082 }
1083
1084 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1550dfd9 1085 KUE_UNLOCK(sc);
984263bc
MD
1086
1087 return;
1088}
1089
1090/*
1091 * Stop all chip I/O so that the kernel's probe routines don't
1092 * get confused by errant DMAs when rebooting.
1093 */
6ed427ca
HT
1094static void
1095kue_shutdown(device_t dev)
984263bc
MD
1096{
1097 struct kue_softc *sc;
1098
1099 sc = device_get_softc(dev);
1100
1101 kue_stop(sc);
1102
1103 return;
1104}