Change mbug allocation flags from M_ to MB_ to avoid confusion with malloc
[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 *
1550dfd9 32 * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.17.2.9 2003/04/13 02:39:25 murray Exp $
74f1caca 33 * $DragonFly: src/sys/dev/netif/kue/if_kue.c,v 1.10 2004/06/02 14:42:52 eirikn 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>
75
76#include <net/if.h>
77#include <net/if_arp.h>
78#include <net/ethernet.h>
79#include <net/if_dl.h>
80#include <net/if_media.h>
81
82#include <net/bpf.h>
83
984263bc 84#include <sys/bus.h>
1550dfd9 85#include <machine/bus.h>
929783d0 86#if defined(__DragonFly__) || __FreeBSD_version < 500000
1550dfd9
MD
87#include <machine/clock.h>
88#endif
984263bc 89
1f2de5d4
MD
90#include <bus/usb/usb.h>
91#include <bus/usb/usbdi.h>
92#include <bus/usb/usbdi_util.h>
93#include <bus/usb/usbdivar.h>
94#include <bus/usb/usbdevs.h>
95#include <bus/usb/usb_ethersubr.h>
984263bc 96
1f2de5d4
MD
97#include "if_kuereg.h"
98#include <bus/usb/kue_fw.h>
984263bc 99
1550dfd9
MD
100MODULE_DEPEND(kue, usb, 1, 1, 1);
101MODULE_DEPEND(kue, ether, 1, 1, 1);
102
984263bc
MD
103/*
104 * Various supported device vendors/products.
105 */
106Static struct kue_type kue_devs[] = {
107 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
108 { USB_VENDOR_KLSI, USB_PRODUCT_AOX_USB101 },
109 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
110 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
111 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
112 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
113 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
114 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
115 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
116 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
117 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
118 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
119 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
120 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
121 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
122 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
1550dfd9
MD
123 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
124 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
984263bc
MD
125 { 0, 0 }
126};
127
128Static struct usb_qdat kue_qdat;
129
1550dfd9
MD
130Static int kue_match(device_ptr_t);
131Static int kue_attach(device_ptr_t);
132Static int kue_detach(device_ptr_t);
133Static void kue_shutdown(device_ptr_t);
984263bc
MD
134Static int kue_tx_list_init(struct kue_softc *);
135Static int kue_rx_list_init(struct kue_softc *);
136Static int kue_newbuf(struct kue_softc *, struct kue_chain *, struct mbuf *);
137Static int kue_encap(struct kue_softc *, struct mbuf *, int);
138Static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
139Static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
140Static void kue_start(struct ifnet *);
141Static void kue_rxstart(struct ifnet *);
bd4539cc 142Static int kue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
984263bc
MD
143Static void kue_init(void *);
144Static void kue_stop(struct kue_softc *);
145Static void kue_watchdog(struct ifnet *);
146
147Static void kue_setmulti(struct kue_softc *);
148Static void kue_reset(struct kue_softc *);
149
150Static usbd_status kue_do_request(usbd_device_handle,
151 usb_device_request_t *, void *);
152Static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
153 u_int16_t, char *, int);
154Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
155Static int kue_load_fw(struct kue_softc *);
156
157Static device_method_t kue_methods[] = {
158 /* Device interface */
159 DEVMETHOD(device_probe, kue_match),
160 DEVMETHOD(device_attach, kue_attach),
161 DEVMETHOD(device_detach, kue_detach),
162 DEVMETHOD(device_shutdown, kue_shutdown),
163
164 { 0, 0 }
165};
166
167Static driver_t kue_driver = {
168 "kue",
169 kue_methods,
170 sizeof(struct kue_softc)
171};
172
173Static devclass_t kue_devclass;
174
32832096 175DECLARE_DUMMY_MODULE(if_kue);
1550dfd9 176DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, usbd_driver_load, 0);
984263bc
MD
177
178/*
179 * We have a custom do_request function which is almost like the
180 * regular do_request function, except it has a much longer timeout.
181 * Why? Because we need to make requests over the control endpoint
182 * to download the firmware to the device, which can take longer
183 * than the default timeout.
184 */
185Static usbd_status
186kue_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data)
187{
188 usbd_xfer_handle xfer;
189 usbd_status err;
190
191 xfer = usbd_alloc_xfer(dev);
192 usbd_setup_default_xfer(xfer, dev, 0, 500000, req,
1550dfd9 193 data, UGETW(req->wLength), USBD_SHORT_XFER_OK, 0);
984263bc
MD
194 err = usbd_sync_transfer(xfer);
195 usbd_free_xfer(xfer);
196 return(err);
197}
198
199Static usbd_status
200kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
201{
202 usbd_device_handle dev;
203 usb_device_request_t req;
204 usbd_status err;
984263bc 205
1550dfd9 206 if (sc->kue_dying)
984263bc
MD
207 return(USBD_NORMAL_COMPLETION);
208
209 dev = sc->kue_udev;
210
1550dfd9 211 KUE_LOCK(sc);
984263bc
MD
212
213 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
214
215 req.bRequest = breq;
216 USETW(req.wValue, word);
217 USETW(req.wIndex, 0);
218 USETW(req.wLength, 0);
219
220 err = kue_do_request(dev, &req, NULL);
221
1550dfd9 222 KUE_UNLOCK(sc);
984263bc
MD
223
224 return(err);
225}
226
227Static usbd_status
228kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
229 char *data, int len)
230{
231 usbd_device_handle dev;
232 usb_device_request_t req;
233 usbd_status err;
984263bc
MD
234
235 dev = sc->kue_udev;
236
1550dfd9 237 if (sc->kue_dying)
984263bc
MD
238 return(USBD_NORMAL_COMPLETION);
239
1550dfd9 240 KUE_LOCK(sc);
984263bc
MD
241
242 if (rw == KUE_CTL_WRITE)
243 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
244 else
245 req.bmRequestType = UT_READ_VENDOR_DEVICE;
246
247 req.bRequest = breq;
248 USETW(req.wValue, val);
249 USETW(req.wIndex, 0);
250 USETW(req.wLength, len);
251
252 err = kue_do_request(dev, &req, data);
253
1550dfd9 254 KUE_UNLOCK(sc);
984263bc
MD
255
256 return(err);
257}
258
259Static int
260kue_load_fw(struct kue_softc *sc)
261{
262 usbd_status err;
263 usb_device_descriptor_t *dd;
264 int hwrev;
265
266 dd = &sc->kue_udev->ddesc;
267 hwrev = UGETW(dd->bcdDevice);
268
269 /*
270 * First, check if we even need to load the firmware.
271 * If the device was still attached when the system was
272 * rebooted, it may already have firmware loaded in it.
273 * If this is the case, we don't need to do it again.
274 * And in fact, if we try to load it again, we'll hang,
275 * so we have to avoid this condition if we don't want
276 * to look stupid.
277 *
278 * We can test this quickly by checking the bcdRevision
279 * code. The NIC will return a different revision code if
280 * it's probed while the firmware is still loaded and
281 * running.
282 */
283 if (hwrev == 0x0202)
284 return(0);
285
286 /* Load code segment */
287 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
288 0, kue_code_seg, sizeof(kue_code_seg));
289 if (err) {
290 printf("kue%d: failed to load code segment: %s\n",
291 sc->kue_unit, usbd_errstr(err));
292 return(ENXIO);
293 }
294
295 /* Load fixup segment */
296 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
297 0, kue_fix_seg, sizeof(kue_fix_seg));
298 if (err) {
299 printf("kue%d: failed to load fixup segment: %s\n",
300 sc->kue_unit, usbd_errstr(err));
301 return(ENXIO);
302 }
303
304 /* Send trigger command. */
305 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
306 0, kue_trig_seg, sizeof(kue_trig_seg));
307 if (err) {
308 printf("kue%d: failed to load trigger segment: %s\n",
309 sc->kue_unit, usbd_errstr(err));
310 return(ENXIO);
311 }
312
313 return(0);
314}
315
316Static void
317kue_setmulti(struct kue_softc *sc)
318{
319 struct ifnet *ifp;
320 struct ifmultiaddr *ifma;
321 int i = 0;
322
323 ifp = &sc->arpcom.ac_if;
324
325 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
326 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
327 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
328 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
329 return;
330 }
331
332 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
333
929783d0 334#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
335 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
336#else
337 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
338#endif
339 {
984263bc
MD
340 if (ifma->ifma_addr->sa_family != AF_LINK)
341 continue;
342 /*
343 * If there are too many addresses for the
344 * internal filter, switch over to allmulti mode.
345 */
346 if (i == KUE_MCFILTCNT(sc))
347 break;
348 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
349 KUE_MCFILT(sc, i), ETHER_ADDR_LEN);
350 i++;
351 }
352
353 if (i == KUE_MCFILTCNT(sc))
354 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
355 else {
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
361 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
362
363 return;
364}
365
366/*
367 * Issue a SET_CONFIGURATION command to reset the MAC. This should be
368 * done after the firmware is loaded into the adapter in order to
369 * bring it into proper operation.
370 */
371Static void
372kue_reset(struct kue_softc *sc)
373{
1550dfd9
MD
374 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 0) ||
375 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
376 &sc->kue_iface)) {
984263bc
MD
377 printf("kue%d: getting interface handle failed\n",
378 sc->kue_unit);
379 }
380
381 /* Wait a little while for the chip to get its brains in order. */
382 DELAY(1000);
383 return;
384}
385
386/*
387 * Probe for a KLSI chip.
388 */
389USB_MATCH(kue)
390{
391 USB_MATCH_START(kue, uaa);
392 struct kue_type *t;
393
394 if (!uaa->iface)
395 return(UMATCH_NONE);
396
397 t = kue_devs;
398 while(t->kue_vid) {
399 if (uaa->vendor == t->kue_vid &&
400 uaa->product == t->kue_did) {
401 return(UMATCH_VENDOR_PRODUCT);
402 }
403 t++;
404 }
405
406 return(UMATCH_NONE);
407}
408
409/*
410 * Attach the interface. Allocate softc structures, do
411 * setup and ethernet/BPF attach.
412 */
413USB_ATTACH(kue)
414{
415 USB_ATTACH_START(kue, sc, uaa);
416 char devinfo[1024];
984263bc
MD
417 struct ifnet *ifp;
418 usbd_status err;
419 usb_interface_descriptor_t *id;
420 usb_endpoint_descriptor_t *ed;
421 int i;
422
984263bc
MD
423 bzero(sc, sizeof(struct kue_softc));
424 sc->kue_iface = uaa->iface;
425 sc->kue_udev = uaa->device;
426 sc->kue_unit = device_get_unit(self);
427
428 id = usbd_get_interface_descriptor(uaa->iface);
429
430 usbd_devinfo(uaa->device, 0, devinfo);
431 device_set_desc_copy(self, devinfo);
432 printf("%s: %s\n", USBDEVNAME(self), devinfo);
433
434 /* Find endpoints. */
435 for (i = 0; i < id->bNumEndpoints; i++) {
436 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
437 if (!ed) {
438 printf("kue%d: couldn't get ep %d\n",
439 sc->kue_unit, i);
984263bc
MD
440 USB_ATTACH_ERROR_RETURN;
441 }
442 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 443 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
444 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
445 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1550dfd9 446 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
447 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
448 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 449 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
984263bc
MD
450 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
451 }
452 }
453
929783d0 454#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
455 mtx_init(&sc->kue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
456 MTX_DEF | MTX_RECURSE);
457#endif
458 KUE_LOCK(sc);
459
984263bc
MD
460 /* Load the firmware into the NIC. */
461 if (kue_load_fw(sc)) {
1550dfd9 462 KUE_UNLOCK(sc);
929783d0 463#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
464 mtx_destroy(&sc->kue_mtx);
465#endif
984263bc
MD
466 USB_ATTACH_ERROR_RETURN;
467 }
468
469 /* Reset the adapter. */
470 kue_reset(sc);
471
472 /* Read ethernet descriptor */
473 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
474 0, (char *)&sc->kue_desc, sizeof(sc->kue_desc));
475
476 sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
c5541aee 477 M_USBDEV, M_WAITOK);
984263bc
MD
478
479 /*
480 * A KLSI chip was detected. Inform the world.
481 */
482 printf("kue%d: Ethernet address: %6D\n", sc->kue_unit,
483 sc->kue_desc.kue_macaddr, ":");
484
485 bcopy(sc->kue_desc.kue_macaddr,
486 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
487
488 ifp = &sc->arpcom.ac_if;
489 ifp->if_softc = sc;
1550dfd9 490 if_initname(ifp, "kue", sc->kue_unit);
984263bc
MD
491 ifp->if_mtu = ETHERMTU;
492 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
493 ifp->if_ioctl = kue_ioctl;
494 ifp->if_output = ether_output;
495 ifp->if_start = kue_start;
496 ifp->if_watchdog = kue_watchdog;
497 ifp->if_init = kue_init;
498 ifp->if_baudrate = 10000000;
499 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
500
501 kue_qdat.ifp = ifp;
502 kue_qdat.if_rxstart = kue_rxstart;
503
504 /*
505 * Call MI attach routine.
506 */
1550dfd9 507 ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
984263bc 508 usb_register_netisr();
1550dfd9
MD
509 sc->kue_dying = 0;
510
511 KUE_UNLOCK(sc);
984263bc 512
984263bc
MD
513 USB_ATTACH_SUCCESS_RETURN;
514}
515
516Static int
517kue_detach(device_ptr_t dev)
518{
519 struct kue_softc *sc;
520 struct ifnet *ifp;
984263bc
MD
521
522 sc = device_get_softc(dev);
1550dfd9 523 KUE_LOCK(sc);
984263bc
MD
524 ifp = &sc->arpcom.ac_if;
525
1550dfd9 526 sc->kue_dying = 1;
984263bc
MD
527
528 if (ifp != NULL)
1550dfd9 529 ether_ifdetach(ifp);
984263bc
MD
530
531 if (sc->kue_ep[KUE_ENDPT_TX] != NULL)
532 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
533 if (sc->kue_ep[KUE_ENDPT_RX] != NULL)
534 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
535 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL)
536 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
537
538 if (sc->kue_mcfilters != NULL)
539 free(sc->kue_mcfilters, M_USBDEV);
540
1550dfd9 541 KUE_UNLOCK(sc);
929783d0 542#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1550dfd9
MD
543 mtx_destroy(&sc->kue_mtx);
544#endif
984263bc
MD
545
546 return(0);
547}
548
549/*
550 * Initialize an RX descriptor and attach an MBUF cluster.
551 */
552Static int
553kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
554{
555 struct mbuf *m_new = NULL;
556
557 if (m == NULL) {
74f1caca 558 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
984263bc
MD
559 if (m_new == NULL) {
560 printf("kue%d: no memory for rx list "
561 "-- packet dropped!\n", sc->kue_unit);
562 return(ENOBUFS);
563 }
564
74f1caca 565 MCLGET(m_new, MB_DONTWAIT);
984263bc
MD
566 if (!(m_new->m_flags & M_EXT)) {
567 printf("kue%d: no memory for rx list "
568 "-- packet dropped!\n", sc->kue_unit);
569 m_freem(m_new);
570 return(ENOBUFS);
571 }
572 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
573 } else {
574 m_new = m;
575 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
576 m_new->m_data = m_new->m_ext.ext_buf;
577 }
578
579 c->kue_mbuf = m_new;
580
581 return(0);
582}
583
584Static int
585kue_rx_list_init(struct kue_softc *sc)
586{
587 struct kue_cdata *cd;
588 struct kue_chain *c;
589 int i;
590
591 cd = &sc->kue_cdata;
592 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
593 c = &cd->kue_rx_chain[i];
594 c->kue_sc = sc;
595 c->kue_idx = i;
596 if (kue_newbuf(sc, c, NULL) == ENOBUFS)
597 return(ENOBUFS);
598 if (c->kue_xfer == NULL) {
599 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
600 if (c->kue_xfer == NULL)
601 return(ENOBUFS);
602 }
603 }
604
605 return(0);
606}
607
608Static int
609kue_tx_list_init(struct kue_softc *sc)
610{
611 struct kue_cdata *cd;
612 struct kue_chain *c;
613 int i;
614
615 cd = &sc->kue_cdata;
616 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
617 c = &cd->kue_tx_chain[i];
618 c->kue_sc = sc;
619 c->kue_idx = i;
620 c->kue_mbuf = NULL;
621 if (c->kue_xfer == NULL) {
622 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
623 if (c->kue_xfer == NULL)
624 return(ENOBUFS);
625 }
c5541aee 626 c->kue_buf = malloc(KUE_BUFSZ, M_USBDEV, M_WAITOK);
984263bc
MD
627 if (c->kue_buf == NULL)
628 return(ENOBUFS);
629 }
630
631 return(0);
632}
633
634Static void
635kue_rxstart(struct ifnet *ifp)
636{
637 struct kue_softc *sc;
638 struct kue_chain *c;
639
640 sc = ifp->if_softc;
1550dfd9 641 KUE_LOCK(sc);
984263bc
MD
642 c = &sc->kue_cdata.kue_rx_chain[sc->kue_cdata.kue_rx_prod];
643
644 if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
645 ifp->if_ierrors++;
646 return;
647 }
648
649 /* Setup new transfer. */
650 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
651 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
652 USBD_NO_TIMEOUT, kue_rxeof);
653 usbd_transfer(c->kue_xfer);
654
1550dfd9
MD
655 KUE_UNLOCK(sc);
656
984263bc
MD
657 return;
658}
659
660/*
661 * A frame has been uploaded: pass the resulting mbuf chain up to
662 * the higher level protocols.
663 */
664Static void kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
665 usbd_status status)
666{
667 struct kue_softc *sc;
668 struct kue_chain *c;
669 struct mbuf *m;
670 struct ifnet *ifp;
671 int total_len = 0;
672 u_int16_t len;
673
674 c = priv;
675 sc = c->kue_sc;
1550dfd9 676 KUE_LOCK(sc);
984263bc
MD
677 ifp = &sc->arpcom.ac_if;
678
1550dfd9
MD
679 if (!(ifp->if_flags & IFF_RUNNING)) {
680 KUE_UNLOCK(sc);
984263bc 681 return;
1550dfd9 682 }
984263bc
MD
683
684 if (status != USBD_NORMAL_COMPLETION) {
1550dfd9
MD
685 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
686 KUE_UNLOCK(sc);
984263bc 687 return;
1550dfd9
MD
688 }
689 if (usbd_ratecheck(&sc->kue_rx_notice))
690 printf("kue%d: usb error on rx: %s\n", sc->kue_unit,
691 usbd_errstr(status));
984263bc
MD
692 if (status == USBD_STALLED)
693 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]);
694 goto done;
695 }
696
697 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
698 m = c->kue_mbuf;
699 if (total_len <= 1)
700 goto done;
701
702 len = *mtod(m, u_int16_t *);
703 m_adj(m, sizeof(u_int16_t));
704
705 /* No errors; receive the packet. */
706 total_len = len;
707
708 if (len < sizeof(struct ether_header)) {
709 ifp->if_ierrors++;
710 goto done;
711 }
712
713 ifp->if_ipackets++;
714 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
715 m->m_pkthdr.len = m->m_len = total_len;
716
717 /* Put the packet on the special USB input queue. */
718 usb_ether_input(m);
1550dfd9 719 KUE_UNLOCK(sc);
984263bc
MD
720
721 return;
722done:
723
724 /* Setup new transfer. */
725 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
726 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ, USBD_SHORT_XFER_OK,
727 USBD_NO_TIMEOUT, kue_rxeof);
728 usbd_transfer(c->kue_xfer);
1550dfd9 729 KUE_UNLOCK(sc);
984263bc
MD
730
731 return;
732}
733
734/*
735 * A frame was downloaded to the chip. It's safe for us to clean up
736 * the list buffers.
737 */
738
739Static void
740kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
741{
742 struct kue_softc *sc;
743 struct kue_chain *c;
744 struct ifnet *ifp;
745 usbd_status err;
984263bc
MD
746
747 c = priv;
748 sc = c->kue_sc;
1550dfd9
MD
749 KUE_LOCK(sc);
750
984263bc
MD
751 ifp = &sc->arpcom.ac_if;
752 ifp->if_timer = 0;
753 ifp->if_flags &= ~IFF_OACTIVE;
754
755 if (status != USBD_NORMAL_COMPLETION) {
756 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1550dfd9 757 KUE_UNLOCK(sc);
984263bc
MD
758 return;
759 }
760 printf("kue%d: usb error on tx: %s\n", sc->kue_unit,
761 usbd_errstr(status));
762 if (status == USBD_STALLED)
763 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]);
1550dfd9 764 KUE_UNLOCK(sc);
984263bc
MD
765 return;
766 }
767
768 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &err);
769
770 if (c->kue_mbuf != NULL) {
771 c->kue_mbuf->m_pkthdr.rcvif = ifp;
772 usb_tx_done(c->kue_mbuf);
773 c->kue_mbuf = NULL;
774 }
775
776 if (err)
777 ifp->if_oerrors++;
778 else
779 ifp->if_opackets++;
780
1550dfd9 781 KUE_UNLOCK(sc);
984263bc
MD
782
783 return;
784}
785
786Static int
787kue_encap(struct kue_softc *sc, struct mbuf *m, int idx)
788{
789 int total_len;
790 struct kue_chain *c;
791 usbd_status err;
792
793 c = &sc->kue_cdata.kue_tx_chain[idx];
794
795 /*
796 * Copy the mbuf data into a contiguous buffer, leaving two
797 * bytes at the beginning to hold the frame length.
798 */
799 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
800 c->kue_mbuf = m;
801
802 total_len = m->m_pkthdr.len + 2;
803 total_len += 64 - (total_len % 64);
804
805 /* Frame length is specified in the first 2 bytes of the buffer. */
806 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
807 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
808
809 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
810 c, c->kue_buf, total_len, 0, 10000, kue_txeof);
811
812 /* Transmit */
813 err = usbd_transfer(c->kue_xfer);
814 if (err != USBD_IN_PROGRESS) {
815 kue_stop(sc);
816 return(EIO);
817 }
818
819 sc->kue_cdata.kue_tx_cnt++;
820
821 return(0);
822}
823
824Static void
825kue_start(struct ifnet *ifp)
826{
827 struct kue_softc *sc;
828 struct mbuf *m_head = NULL;
829
830 sc = ifp->if_softc;
1550dfd9 831 KUE_LOCK(sc);
984263bc 832
1550dfd9
MD
833 if (ifp->if_flags & IFF_OACTIVE) {
834 KUE_UNLOCK(sc);
984263bc 835 return;
1550dfd9 836 }
984263bc
MD
837
838 IF_DEQUEUE(&ifp->if_snd, m_head);
1550dfd9
MD
839 if (m_head == NULL) {
840 KUE_UNLOCK(sc);
984263bc 841 return;
1550dfd9 842 }
984263bc
MD
843
844 if (kue_encap(sc, m_head, 0)) {
845 IF_PREPEND(&ifp->if_snd, m_head);
846 ifp->if_flags |= IFF_OACTIVE;
1550dfd9 847 KUE_UNLOCK(sc);
984263bc
MD
848 return;
849 }
850
851 /*
852 * If there's a BPF listener, bounce a copy of this frame
853 * to him.
854 */
1550dfd9 855 BPF_MTAP(ifp, m_head);
984263bc
MD
856
857 ifp->if_flags |= IFF_OACTIVE;
858
859 /*
860 * Set a timeout in case the chip goes out to lunch.
861 */
862 ifp->if_timer = 5;
1550dfd9 863 KUE_UNLOCK(sc);
984263bc
MD
864
865 return;
866}
867
868Static void
869kue_init(void *xsc)
870{
871 struct kue_softc *sc = xsc;
872 struct ifnet *ifp = &sc->arpcom.ac_if;
873 struct kue_chain *c;
874 usbd_status err;
1550dfd9 875 int i;
984263bc 876
1550dfd9 877 KUE_LOCK(sc);
984263bc 878
1550dfd9
MD
879 if (ifp->if_flags & IFF_RUNNING) {
880 KUE_UNLOCK(sc);
881 return;
882 }
984263bc
MD
883
884 /* Set MAC address */
885 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
886 0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
887
888 sc->kue_rxfilt = KUE_RXFILT_UNICAST|KUE_RXFILT_BROADCAST;
889
890 /* If we want promiscuous mode, set the allframes bit. */
891 if (ifp->if_flags & IFF_PROMISC)
892 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
893
894 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
895
896 /* I'm not sure how to tune these. */
897#ifdef notdef
898 /*
899 * Leave this one alone for now; setting it
900 * wrong causes lockups on some machines/controllers.
901 */
902 kue_setword(sc, KUE_CMD_SET_SOFS, 1);
903#endif
904 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
905
906 /* Init TX ring. */
907 if (kue_tx_list_init(sc) == ENOBUFS) {
908 printf("kue%d: tx list init failed\n", sc->kue_unit);
1550dfd9 909 KUE_UNLOCK(sc);
984263bc
MD
910 return;
911 }
912
913 /* Init RX ring. */
914 if (kue_rx_list_init(sc) == ENOBUFS) {
915 printf("kue%d: rx list init failed\n", sc->kue_unit);
1550dfd9 916 KUE_UNLOCK(sc);
984263bc
MD
917 return;
918 }
919
920 /* Load the multicast filter. */
921 kue_setmulti(sc);
922
923 /* Open RX and TX pipes. */
924 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
925 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
926 if (err) {
927 printf("kue%d: open rx pipe failed: %s\n",
928 sc->kue_unit, usbd_errstr(err));
1550dfd9 929 KUE_UNLOCK(sc);
984263bc
MD
930 return;
931 }
932
933 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
934 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
935 if (err) {
936 printf("kue%d: open tx pipe failed: %s\n",
937 sc->kue_unit, usbd_errstr(err));
1550dfd9 938 KUE_UNLOCK(sc);
984263bc
MD
939 return;
940 }
941
942 /* Start up the receive pipe. */
943 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
944 c = &sc->kue_cdata.kue_rx_chain[i];
945 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
946 c, mtod(c->kue_mbuf, char *), KUE_BUFSZ,
947 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, kue_rxeof);
948 usbd_transfer(c->kue_xfer);
949 }
950
951 ifp->if_flags |= IFF_RUNNING;
952 ifp->if_flags &= ~IFF_OACTIVE;
953
1550dfd9 954 KUE_UNLOCK(sc);
984263bc
MD
955
956 return;
957}
958
959Static int
bd4539cc 960kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
984263bc
MD
961{
962 struct kue_softc *sc = ifp->if_softc;
1550dfd9 963 int error = 0;
984263bc 964
1550dfd9 965 KUE_LOCK(sc);
984263bc
MD
966
967 switch(command) {
984263bc
MD
968 case SIOCSIFFLAGS:
969 if (ifp->if_flags & IFF_UP) {
970 if (ifp->if_flags & IFF_RUNNING &&
971 ifp->if_flags & IFF_PROMISC &&
972 !(sc->kue_if_flags & IFF_PROMISC)) {
973 sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
974 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
975 sc->kue_rxfilt);
976 } else if (ifp->if_flags & IFF_RUNNING &&
977 !(ifp->if_flags & IFF_PROMISC) &&
978 sc->kue_if_flags & IFF_PROMISC) {
979 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
980 kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
981 sc->kue_rxfilt);
982 } else if (!(ifp->if_flags & IFF_RUNNING))
983 kue_init(sc);
984 } else {
985 if (ifp->if_flags & IFF_RUNNING)
986 kue_stop(sc);
987 }
988 sc->kue_if_flags = ifp->if_flags;
989 error = 0;
990 break;
991 case SIOCADDMULTI:
992 case SIOCDELMULTI:
993 kue_setmulti(sc);
994 error = 0;
995 break;
996 default:
1550dfd9 997 error = ether_ioctl(ifp, command, data);
984263bc
MD
998 break;
999 }
1000
1550dfd9 1001 KUE_UNLOCK(sc);
984263bc
MD
1002
1003 return(error);
1004}
1005
1006Static void
1007kue_watchdog(struct ifnet *ifp)
1008{
1009 struct kue_softc *sc;
1010 struct kue_chain *c;
1011 usbd_status stat;
1012
1013 sc = ifp->if_softc;
1550dfd9 1014 KUE_LOCK(sc);
984263bc
MD
1015 ifp->if_oerrors++;
1016 printf("kue%d: watchdog timeout\n", sc->kue_unit);
1017
1018 c = &sc->kue_cdata.kue_tx_chain[0];
1019 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1020 kue_txeof(c->kue_xfer, c, stat);
1021
1022 if (ifp->if_snd.ifq_head != NULL)
1023 kue_start(ifp);
1550dfd9 1024 KUE_UNLOCK(sc);
984263bc
MD
1025
1026 return;
1027}
1028
1029/*
1030 * Stop the adapter and free any mbufs allocated to the
1031 * RX and TX lists.
1032 */
1033Static void
1034kue_stop(struct kue_softc *sc)
1035{
1036 usbd_status err;
1037 struct ifnet *ifp;
1038 int i;
1039
1550dfd9 1040 KUE_LOCK(sc);
984263bc
MD
1041 ifp = &sc->arpcom.ac_if;
1042 ifp->if_timer = 0;
1043
1044 /* Stop transfers. */
1045 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1046 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1047 if (err) {
1048 printf("kue%d: abort rx pipe failed: %s\n",
1049 sc->kue_unit, usbd_errstr(err));
1050 }
1051 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1052 if (err) {
1053 printf("kue%d: close rx pipe failed: %s\n",
1054 sc->kue_unit, usbd_errstr(err));
1055 }
1056 sc->kue_ep[KUE_ENDPT_RX] = NULL;
1057 }
1058
1059 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1060 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1061 if (err) {
1062 printf("kue%d: abort tx pipe failed: %s\n",
1063 sc->kue_unit, usbd_errstr(err));
1064 }
1065 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1066 if (err) {
1067 printf("kue%d: close tx pipe failed: %s\n",
1068 sc->kue_unit, usbd_errstr(err));
1069 }
1070 sc->kue_ep[KUE_ENDPT_TX] = NULL;
1071 }
1072
1073 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1074 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1075 if (err) {
1076 printf("kue%d: abort intr pipe failed: %s\n",
1077 sc->kue_unit, usbd_errstr(err));
1078 }
1079 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1080 if (err) {
1081 printf("kue%d: close intr pipe failed: %s\n",
1082 sc->kue_unit, usbd_errstr(err));
1083 }
1084 sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1085 }
1086
1087 /* Free RX resources. */
1088 for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1089 if (sc->kue_cdata.kue_rx_chain[i].kue_buf != NULL) {
1090 free(sc->kue_cdata.kue_rx_chain[i].kue_buf, M_USBDEV);
1091 sc->kue_cdata.kue_rx_chain[i].kue_buf = NULL;
1092 }
1093 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1094 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1095 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1096 }
1097 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1098 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1099 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1100 }
1101 }
1102
1103 /* Free TX resources. */
1104 for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1105 if (sc->kue_cdata.kue_tx_chain[i].kue_buf != NULL) {
1106 free(sc->kue_cdata.kue_tx_chain[i].kue_buf, M_USBDEV);
1107 sc->kue_cdata.kue_tx_chain[i].kue_buf = NULL;
1108 }
1109 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1110 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1111 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1112 }
1113 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1114 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1115 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1116 }
1117 }
1118
1119 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1550dfd9 1120 KUE_UNLOCK(sc);
984263bc
MD
1121
1122 return;
1123}
1124
1125/*
1126 * Stop all chip I/O so that the kernel's probe routines don't
1127 * get confused by errant DMAs when rebooting.
1128 */
1129Static void
1130kue_shutdown(device_ptr_t dev)
1131{
1132 struct kue_softc *sc;
1133
1134 sc = device_get_softc(dev);
1135
1136 kue_stop(sc);
1137
1138 return;
1139}