network code: Convert if_multiaddrs from LIST to TAILQ.
[dragonfly.git] / sys / dev / netif / cue / if_cue.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_cue.c,v 1.45 2003/12/08 07:54:14 obrien Exp $
9db4b353 33 * $DragonFly: src/sys/dev/netif/cue/if_cue.c,v 1.32 2008/05/14 11:59:19 sephe Exp $
984263bc
MD
34 */
35
36/*
37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38 * adapters and others.
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 CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47 * RX filter uses a 512-bit multicast hash table, single perfect entry
48 * for the station address, and promiscuous mode. Unlike the ADMtek
49 * and KLSI chips, the CATC ASIC supports read and write combining
50 * mode where multiple packets can be transfered using a single bulk
51 * transaction, which helps performance a great deal.
52 */
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/sockio.h>
57#include <sys/mbuf.h>
58#include <sys/malloc.h>
59#include <sys/kernel.h>
60#include <sys/socket.h>
1f7ab7c9 61#include <sys/bus.h>
984263bc
MD
62
63#include <net/if.h>
f33fa91c 64#include <net/ifq_var.h>
984263bc
MD
65#include <net/if_arp.h>
66#include <net/ethernet.h>
67#include <net/if_dl.h>
984263bc
MD
68#include <net/bpf.h>
69
1f2de5d4
MD
70#include <bus/usb/usb.h>
71#include <bus/usb/usbdi.h>
72#include <bus/usb/usbdi_util.h>
73#include <bus/usb/usbdivar.h>
1f2de5d4 74#include <bus/usb/usb_ethersubr.h>
984263bc 75
1f2de5d4 76#include "if_cuereg.h"
984263bc 77
984263bc
MD
78/*
79 * Various supported device vendors/products.
80 */
3f56796e
HT
81static struct usb_devno cue_devs[] = {
82 { USB_DEVICE(0x0423, 0x000a) }, /* CATC Netmate */
83 { USB_DEVICE(0x0423, 0x000c) }, /* CATC Netmate2 */
84 { USB_DEVICE(0x08d1, 0x0001) }, /* SmartBridges SmartLink */
984263bc
MD
85};
86
6ed427ca
HT
87static int cue_match(device_t);
88static int cue_attach(device_t);
89static int cue_detach(device_t);
90
91static int cue_tx_list_init(struct cue_softc *);
92static int cue_rx_list_init(struct cue_softc *);
93static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
94static int cue_encap(struct cue_softc *, struct mbuf *, int);
95static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
96static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
97static void cue_tick(void *);
98static void cue_rxstart(struct ifnet *);
99static void cue_start(struct ifnet *);
100static int cue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
101static void cue_init(void *);
102static void cue_stop(struct cue_softc *);
103static void cue_watchdog(struct ifnet *);
104static void cue_shutdown(device_t);
105
106static void cue_setmulti(struct cue_softc *);
107static void cue_reset(struct cue_softc *);
108
109static int cue_csr_read_1(struct cue_softc *, int);
110static int cue_csr_write_1(struct cue_softc *, int, int);
111static int cue_csr_read_2(struct cue_softc *, int);
984263bc 112#ifdef notdef
6ed427ca 113static int cue_csr_write_2(struct cue_softc *, int, int);
984263bc 114#endif
6ed427ca
HT
115static int cue_mem(struct cue_softc *, int, int, void *, int);
116static int cue_getmac(struct cue_softc *, void *);
984263bc 117
6ed427ca 118static device_method_t cue_methods[] = {
984263bc
MD
119 /* Device interface */
120 DEVMETHOD(device_probe, cue_match),
121 DEVMETHOD(device_attach, cue_attach),
122 DEVMETHOD(device_detach, cue_detach),
123 DEVMETHOD(device_shutdown, cue_shutdown),
124
125 { 0, 0 }
126};
127
6ed427ca 128static driver_t cue_driver = {
984263bc
MD
129 "cue",
130 cue_methods,
131 sizeof(struct cue_softc)
132};
133
6ed427ca 134static devclass_t cue_devclass;
984263bc 135
32832096 136DECLARE_DUMMY_MODULE(if_cue);
1550dfd9
MD
137DRIVER_MODULE(cue, uhub, cue_driver, cue_devclass, usbd_driver_load, 0);
138MODULE_DEPEND(cue, usb, 1, 1, 1);
984263bc
MD
139
140#define CUE_SETBIT(sc, reg, x) \
1550dfd9 141 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
984263bc
MD
142
143#define CUE_CLRBIT(sc, reg, x) \
1550dfd9 144 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
984263bc 145
6ed427ca 146static int
1550dfd9 147cue_csr_read_1(struct cue_softc *sc, int reg)
984263bc
MD
148{
149 usb_device_request_t req;
150 usbd_status err;
151 u_int8_t val = 0;
984263bc 152
1550dfd9 153 if (sc->cue_dying)
984263bc
MD
154 return(0);
155
1550dfd9 156 CUE_LOCK(sc);
984263bc
MD
157
158 req.bmRequestType = UT_READ_VENDOR_DEVICE;
159 req.bRequest = CUE_CMD_READREG;
160 USETW(req.wValue, 0);
161 USETW(req.wIndex, reg);
162 USETW(req.wLength, 1);
163
1550dfd9 164 err = usbd_do_request(sc->cue_udev, &req, &val);
984263bc 165
1550dfd9 166 CUE_UNLOCK(sc);
984263bc
MD
167
168 if (err)
169 return(0);
170
171 return(val);
172}
173
6ed427ca 174static int
1550dfd9 175cue_csr_read_2(struct cue_softc *sc, int reg)
984263bc
MD
176{
177 usb_device_request_t req;
178 usbd_status err;
179 u_int16_t val = 0;
984263bc 180
1550dfd9 181 if (sc->cue_dying)
984263bc
MD
182 return(0);
183
1550dfd9 184 CUE_LOCK(sc);
984263bc
MD
185
186 req.bmRequestType = UT_READ_VENDOR_DEVICE;
187 req.bRequest = CUE_CMD_READREG;
188 USETW(req.wValue, 0);
189 USETW(req.wIndex, reg);
190 USETW(req.wLength, 2);
191
1550dfd9 192 err = usbd_do_request(sc->cue_udev, &req, &val);
984263bc 193
1550dfd9 194 CUE_UNLOCK(sc);
984263bc
MD
195
196 if (err)
197 return(0);
198
199 return(val);
200}
201
6ed427ca 202static int
1550dfd9 203cue_csr_write_1(struct cue_softc *sc, int reg, int val)
984263bc
MD
204{
205 usb_device_request_t req;
206 usbd_status err;
984263bc 207
1550dfd9 208 if (sc->cue_dying)
984263bc
MD
209 return(0);
210
1550dfd9 211 CUE_LOCK(sc);
984263bc
MD
212
213 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
214 req.bRequest = CUE_CMD_WRITEREG;
215 USETW(req.wValue, val);
216 USETW(req.wIndex, reg);
217 USETW(req.wLength, 0);
218
1550dfd9 219 err = usbd_do_request(sc->cue_udev, &req, NULL);
984263bc 220
1550dfd9 221 CUE_UNLOCK(sc);
984263bc
MD
222
223 if (err)
224 return(-1);
225
226 return(0);
227}
228
229#ifdef notdef
6ed427ca 230static int
1550dfd9 231cue_csr_write_2(struct cue_softc *sc, int reg, int val)
984263bc
MD
232{
233 usb_device_request_t req;
234 usbd_status err;
984263bc 235
1550dfd9 236 if (sc->cue_dying)
984263bc
MD
237 return(0);
238
1550dfd9 239 CUE_LOCK(sc);
984263bc
MD
240
241 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
242 req.bRequest = CUE_CMD_WRITEREG;
243 USETW(req.wValue, val);
244 USETW(req.wIndex, reg);
245 USETW(req.wLength, 0);
246
1550dfd9 247 err = usbd_do_request(sc->cue_udev, &req, NULL);
984263bc 248
1550dfd9 249 CUE_UNLOCK(sc);
984263bc
MD
250
251 if (err)
252 return(-1);
253
254 return(0);
255}
256#endif
257
6ed427ca 258static int
984263bc
MD
259cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
260{
261 usb_device_request_t req;
262 usbd_status err;
984263bc 263
1550dfd9 264 if (sc->cue_dying)
984263bc
MD
265 return(0);
266
1550dfd9 267 CUE_LOCK(sc);
984263bc
MD
268
269 if (cmd == CUE_CMD_READSRAM)
270 req.bmRequestType = UT_READ_VENDOR_DEVICE;
271 else
272 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
273 req.bRequest = cmd;
274 USETW(req.wValue, 0);
275 USETW(req.wIndex, addr);
276 USETW(req.wLength, len);
277
1550dfd9 278 err = usbd_do_request(sc->cue_udev, &req, buf);
984263bc 279
1550dfd9 280 CUE_UNLOCK(sc);
984263bc
MD
281
282 if (err)
283 return(-1);
284
285 return(0);
286}
287
6ed427ca 288static int
984263bc
MD
289cue_getmac(struct cue_softc *sc, void *buf)
290{
291 usb_device_request_t req;
292 usbd_status err;
984263bc 293
1550dfd9 294 if (sc->cue_dying)
984263bc
MD
295 return(0);
296
1550dfd9 297 CUE_LOCK(sc);
984263bc
MD
298
299 req.bmRequestType = UT_READ_VENDOR_DEVICE;
300 req.bRequest = CUE_CMD_GET_MACADDR;
301 USETW(req.wValue, 0);
302 USETW(req.wIndex, 0);
303 USETW(req.wLength, ETHER_ADDR_LEN);
304
1550dfd9 305 err = usbd_do_request(sc->cue_udev, &req, buf);
984263bc 306
1550dfd9 307 CUE_UNLOCK(sc);
984263bc
MD
308
309 if (err) {
abf66171 310 if_printf(&sc->arpcom.ac_if, "read MAC address failed\n");
984263bc
MD
311 return(-1);
312 }
313
314 return(0);
315}
316
93d4a46b 317#define CUE_BITS 9
984263bc 318
6ed427ca 319static void
984263bc
MD
320cue_setmulti(struct cue_softc *sc)
321{
322 struct ifnet *ifp;
323 struct ifmultiaddr *ifma;
324 u_int32_t h = 0, i;
325
326 ifp = &sc->arpcom.ac_if;
327
328 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
329 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
330 sc->cue_mctab[i] = 0xFF;
1550dfd9
MD
331 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
332 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
984263bc
MD
333 return;
334 }
335
336 /* first, zot all the existing hash bits */
337 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
338 sc->cue_mctab[i] = 0;
339
340 /* now program new ones */
441d34b2 341 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1550dfd9 342 {
984263bc
MD
343 if (ifma->ifma_addr->sa_family != AF_LINK)
344 continue;
93d4a46b
JS
345 h = ether_crc32_le(
346 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
347 ETHER_ADDR_LEN) & ((1 << CUE_BITS) - 1);
1550dfd9 348 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
984263bc
MD
349 }
350
351 /*
352 * Also include the broadcast address in the filter
353 * so we can receive broadcast frames.
354 */
355 if (ifp->if_flags & IFF_BROADCAST) {
93d4a46b
JS
356 h = ether_crc32_le(ifp->if_broadcastaddr, ETHER_ADDR_LEN) &
357 ((1 << CUE_BITS) - 1);
1550dfd9 358 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
984263bc
MD
359 }
360
361 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
362 &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
363
364 return;
365}
366
6ed427ca 367static void
984263bc
MD
368cue_reset(struct cue_softc *sc)
369{
370 usb_device_request_t req;
371 usbd_status err;
984263bc 372
1550dfd9 373 if (sc->cue_dying)
984263bc
MD
374 return;
375
984263bc
MD
376 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
377 req.bRequest = CUE_CMD_RESET;
378 USETW(req.wValue, 0);
379 USETW(req.wIndex, 0);
380 USETW(req.wLength, 0);
1550dfd9 381 err = usbd_do_request(sc->cue_udev, &req, NULL);
984263bc 382 if (err)
abf66171 383 if_printf(&sc->arpcom.ac_if, "reset failed\n");
984263bc
MD
384
385 /* Wait a little while for the chip to get its brains in order. */
386 DELAY(1000);
387 return;
388}
389
390/*
391 * Probe for a Pegasus chip.
392 */
e785a5d9
HT
393static int
394cue_match(device_t self)
984263bc 395{
e785a5d9 396 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 397
3f56796e 398 if (uaa->iface == NULL)
984263bc
MD
399 return(UMATCH_NONE);
400
3f56796e
HT
401 return (usb_lookup(cue_devs, uaa->vendor, uaa->product) != NULL) ?
402 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
984263bc
MD
403}
404
405/*
406 * Attach the interface. Allocate softc structures, do ifmedia
407 * setup and ethernet/BPF attach.
408 */
e785a5d9
HT
409static int
410cue_attach(device_t self)
984263bc 411{
e785a5d9
HT
412 struct cue_softc *sc = device_get_softc(self);
413 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
414 u_char eaddr[ETHER_ADDR_LEN];
415 struct ifnet *ifp;
416 usb_interface_descriptor_t *id;
417 usb_endpoint_descriptor_t *ed;
418 int i;
419
984263bc
MD
420 sc->cue_iface = uaa->iface;
421 sc->cue_udev = uaa->device;
3211e37c 422 callout_init(&sc->cue_stat_timer);
984263bc
MD
423
424 if (usbd_set_config_no(sc->cue_udev, CUE_CONFIG_NO, 0)) {
abf66171
JS
425 device_printf(self, "setting config no %d failed\n",
426 CUE_CONFIG_NO);
e785a5d9 427 return ENXIO;
984263bc
MD
428 }
429
430 id = usbd_get_interface_descriptor(uaa->iface);
431
984263bc
MD
432 /* Find endpoints. */
433 for (i = 0; i < id->bNumEndpoints; i++) {
434 ed = usbd_interface2endpoint_descriptor(uaa->iface, i);
435 if (!ed) {
abf66171 436 device_printf(self, "couldn't get ep %d\n", i);
e785a5d9 437 return ENXIO;
984263bc
MD
438 }
439 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 440 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
441 sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
442 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1550dfd9 443 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
444 sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
445 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 446 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
984263bc
MD
447 sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
448 }
449 }
450
1550dfd9
MD
451 CUE_LOCK(sc);
452
abf66171
JS
453 ifp = &sc->arpcom.ac_if;
454 if_initname(ifp, device_get_name(self), device_get_unit(self));
455
984263bc
MD
456#ifdef notdef
457 /* Reset the adapter. */
458 cue_reset(sc);
459#endif
460 /*
461 * Get station address.
462 */
463 cue_getmac(sc, &eaddr);
464
984263bc 465 ifp->if_softc = sc;
984263bc
MD
466 ifp->if_mtu = ETHERMTU;
467 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
468 ifp->if_ioctl = cue_ioctl;
984263bc
MD
469 ifp->if_start = cue_start;
470 ifp->if_watchdog = cue_watchdog;
471 ifp->if_init = cue_init;
472 ifp->if_baudrate = 10000000;
f33fa91c
JS
473 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
474 ifq_set_ready(&ifp->if_snd);
984263bc 475
984263bc
MD
476 /*
477 * Call MI attach routine.
478 */
78195a76 479 ether_ifattach(ifp, eaddr, NULL);
984263bc 480 usb_register_netisr();
1550dfd9 481 sc->cue_dying = 0;
984263bc 482
1550dfd9 483 CUE_UNLOCK(sc);
e785a5d9 484 return 0;
984263bc
MD
485}
486
6ed427ca
HT
487static int
488cue_detach(device_t dev)
984263bc
MD
489{
490 struct cue_softc *sc;
491 struct ifnet *ifp;
984263bc
MD
492
493 sc = device_get_softc(dev);
1550dfd9 494 CUE_LOCK(sc);
984263bc
MD
495 ifp = &sc->arpcom.ac_if;
496
1550dfd9 497 sc->cue_dying = 1;
3211e37c 498 callout_stop(&sc->cue_stat_timer);
1550dfd9 499 ether_ifdetach(ifp);
984263bc
MD
500
501 if (sc->cue_ep[CUE_ENDPT_TX] != NULL)
502 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
503 if (sc->cue_ep[CUE_ENDPT_RX] != NULL)
504 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
505 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL)
506 usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
507
1550dfd9 508 CUE_UNLOCK(sc);
984263bc
MD
509
510 return(0);
511}
512
513/*
514 * Initialize an RX descriptor and attach an MBUF cluster.
515 */
6ed427ca 516static int
984263bc
MD
517cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
518{
519 struct mbuf *m_new = NULL;
520
521 if (m == NULL) {
74f1caca 522 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
984263bc 523 if (m_new == NULL) {
abf66171
JS
524 if_printf(&sc->arpcom.ac_if, "no memory for rx list "
525 "-- packet dropped!\n");
984263bc
MD
526 return(ENOBUFS);
527 }
528
74f1caca 529 MCLGET(m_new, MB_DONTWAIT);
984263bc 530 if (!(m_new->m_flags & M_EXT)) {
abf66171
JS
531 if_printf(&sc->arpcom.ac_if, "no memory for rx list "
532 "-- packet dropped!\n");
984263bc
MD
533 m_freem(m_new);
534 return(ENOBUFS);
535 }
536 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
537 } else {
538 m_new = m;
539 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
540 m_new->m_data = m_new->m_ext.ext_buf;
541 }
542
543 m_adj(m_new, ETHER_ALIGN);
544 c->cue_mbuf = m_new;
545
546 return(0);
547}
548
6ed427ca 549static int
984263bc
MD
550cue_rx_list_init(struct cue_softc *sc)
551{
552 struct cue_cdata *cd;
553 struct cue_chain *c;
554 int i;
555
556 cd = &sc->cue_cdata;
557 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
558 c = &cd->cue_rx_chain[i];
559 c->cue_sc = sc;
560 c->cue_idx = i;
561 if (cue_newbuf(sc, c, NULL) == ENOBUFS)
562 return(ENOBUFS);
563 if (c->cue_xfer == NULL) {
564 c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
565 if (c->cue_xfer == NULL)
566 return(ENOBUFS);
567 }
568 }
569
570 return(0);
571}
572
6ed427ca 573static int
984263bc
MD
574cue_tx_list_init(struct cue_softc *sc)
575{
576 struct cue_cdata *cd;
577 struct cue_chain *c;
578 int i;
579
580 cd = &sc->cue_cdata;
581 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
582 c = &cd->cue_tx_chain[i];
583 c->cue_sc = sc;
584 c->cue_idx = i;
585 c->cue_mbuf = NULL;
586 if (c->cue_xfer == NULL) {
587 c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
588 if (c->cue_xfer == NULL)
589 return(ENOBUFS);
590 }
efda3bd0 591 c->cue_buf = kmalloc(CUE_BUFSZ, M_USBDEV, M_WAITOK);
984263bc
MD
592 }
593
594 return(0);
595}
596
6ed427ca 597static void
984263bc
MD
598cue_rxstart(struct ifnet *ifp)
599{
600 struct cue_softc *sc;
601 struct cue_chain *c;
602
603 sc = ifp->if_softc;
1550dfd9 604 CUE_LOCK(sc);
984263bc
MD
605 c = &sc->cue_cdata.cue_rx_chain[sc->cue_cdata.cue_rx_prod];
606
607 if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
608 ifp->if_ierrors++;
1550dfd9 609 CUE_UNLOCK(sc);
984263bc
MD
610 return;
611 }
612
613 /* Setup new transfer. */
614 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
615 c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
616 USBD_NO_TIMEOUT, cue_rxeof);
617 usbd_transfer(c->cue_xfer);
1550dfd9 618 CUE_UNLOCK(sc);
984263bc
MD
619
620 return;
621}
622
623/*
624 * A frame has been uploaded: pass the resulting mbuf chain up to
625 * the higher level protocols.
626 */
6ed427ca 627static void
984263bc
MD
628cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
629{
630 struct cue_softc *sc;
631 struct cue_chain *c;
632 struct mbuf *m;
633 struct ifnet *ifp;
634 int total_len = 0;
635 u_int16_t len;
636
637 c = priv;
638 sc = c->cue_sc;
1550dfd9 639 CUE_LOCK(sc);
984263bc
MD
640 ifp = &sc->arpcom.ac_if;
641
1550dfd9
MD
642 if (!(ifp->if_flags & IFF_RUNNING)) {
643 CUE_UNLOCK(sc);
984263bc 644 return;
1550dfd9 645 }
984263bc
MD
646
647 if (status != USBD_NORMAL_COMPLETION) {
1550dfd9
MD
648 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
649 CUE_UNLOCK(sc);
984263bc 650 return;
1550dfd9 651 }
abf66171
JS
652 if (usbd_ratecheck(&sc->cue_rx_notice)) {
653 if_printf(ifp, "usb error on rx: %s\n",
654 usbd_errstr(status));
655 }
984263bc
MD
656 if (status == USBD_STALLED)
657 usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]);
658 goto done;
659 }
660
661 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
662
663 m = c->cue_mbuf;
664 len = *mtod(m, 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++;
675 m_adj(m, sizeof(u_int16_t));
ca3f7aba 676 m->m_pkthdr.rcvif = ifp;
984263bc
MD
677 m->m_pkthdr.len = m->m_len = total_len;
678
679 /* Put the packet on the special USB input queue. */
680 usb_ether_input(m);
ca3f7aba
JS
681 cue_rxstart(ifp);
682
1550dfd9 683 CUE_UNLOCK(sc);
984263bc
MD
684
685 return;
686done:
687 /* Setup new transfer. */
688 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
689 c, mtod(c->cue_mbuf, char *), CUE_BUFSZ, USBD_SHORT_XFER_OK,
690 USBD_NO_TIMEOUT, cue_rxeof);
691 usbd_transfer(c->cue_xfer);
1550dfd9 692 CUE_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
703cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
704{
705 struct cue_softc *sc;
706 struct cue_chain *c;
707 struct ifnet *ifp;
708 usbd_status err;
984263bc
MD
709
710 c = priv;
711 sc = c->cue_sc;
1550dfd9 712 CUE_LOCK(sc);
984263bc
MD
713 ifp = &sc->arpcom.ac_if;
714
715 if (status != USBD_NORMAL_COMPLETION) {
716 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1550dfd9 717 CUE_UNLOCK(sc);
984263bc
MD
718 return;
719 }
abf66171 720 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
984263bc
MD
721 if (status == USBD_STALLED)
722 usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]);
1550dfd9 723 CUE_UNLOCK(sc);
984263bc
MD
724 return;
725 }
726
727 ifp->if_timer = 0;
728 ifp->if_flags &= ~IFF_OACTIVE;
729 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &err);
730
731 if (c->cue_mbuf != NULL) {
ca3f7aba 732 m_freem(c->cue_mbuf);
984263bc
MD
733 c->cue_mbuf = NULL;
734 }
735
736 if (err)
737 ifp->if_oerrors++;
738 else
739 ifp->if_opackets++;
740
ca3f7aba 741 if (!ifq_is_empty(&ifp->if_snd))
9db4b353 742 if_devstart(ifp);
ca3f7aba 743
1550dfd9 744 CUE_UNLOCK(sc);
984263bc
MD
745
746 return;
747}
748
6ed427ca 749static void
984263bc
MD
750cue_tick(void *xsc)
751{
752 struct cue_softc *sc;
753 struct ifnet *ifp;
984263bc
MD
754
755 sc = xsc;
756
1550dfd9 757 if (sc == NULL)
984263bc 758 return;
1550dfd9
MD
759
760 CUE_LOCK(sc);
984263bc
MD
761
762 ifp = &sc->arpcom.ac_if;
763
1550dfd9
MD
764 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
765 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
766 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
984263bc 767
1550dfd9 768 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
984263bc
MD
769 ifp->if_ierrors++;
770
3211e37c 771 callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
984263bc 772
1550dfd9 773 CUE_UNLOCK(sc);
984263bc
MD
774
775 return;
776}
777
6ed427ca 778static int
984263bc
MD
779cue_encap(struct cue_softc *sc, struct mbuf *m, int idx)
780{
781 int total_len;
782 struct cue_chain *c;
783 usbd_status err;
784
785 c = &sc->cue_cdata.cue_tx_chain[idx];
786
787 /*
788 * Copy the mbuf data into a contiguous buffer, leaving two
789 * bytes at the beginning to hold the frame length.
790 */
791 m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
792 c->cue_mbuf = m;
793
794 total_len = m->m_pkthdr.len + 2;
795
796 /* The first two bytes are the frame length */
797 c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
798 c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
799
800 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
801 c, c->cue_buf, total_len, 0, 10000, cue_txeof);
802
803 /* Transmit */
804 err = usbd_transfer(c->cue_xfer);
805 if (err != USBD_IN_PROGRESS) {
806 cue_stop(sc);
807 return(EIO);
808 }
809
810 sc->cue_cdata.cue_tx_cnt++;
811
812 return(0);
813}
814
6ed427ca 815static void
984263bc
MD
816cue_start(struct ifnet *ifp)
817{
818 struct cue_softc *sc;
819 struct mbuf *m_head = NULL;
820
821 sc = ifp->if_softc;
1550dfd9 822 CUE_LOCK(sc);
984263bc 823
1550dfd9
MD
824 if (ifp->if_flags & IFF_OACTIVE) {
825 CUE_UNLOCK(sc);
984263bc 826 return;
1550dfd9 827 }
984263bc 828
9db4b353 829 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1550dfd9
MD
830 if (m_head == NULL) {
831 CUE_UNLOCK(sc);
984263bc 832 return;
1550dfd9 833 }
984263bc
MD
834
835 if (cue_encap(sc, m_head, 0)) {
9db4b353 836 /* cue_encap() will free m_head, if we reach here */
984263bc 837 ifp->if_flags |= IFF_OACTIVE;
1550dfd9 838 CUE_UNLOCK(sc);
984263bc
MD
839 return;
840 }
841
842 /*
843 * If there's a BPF listener, bounce a copy of this frame
844 * to him.
845 */
1550dfd9 846 BPF_MTAP(ifp, m_head);
984263bc
MD
847
848 ifp->if_flags |= IFF_OACTIVE;
849
850 /*
851 * Set a timeout in case the chip goes out to lunch.
852 */
853 ifp->if_timer = 5;
1550dfd9 854 CUE_UNLOCK(sc);
984263bc
MD
855
856 return;
857}
858
6ed427ca 859static void
984263bc
MD
860cue_init(void *xsc)
861{
862 struct cue_softc *sc = xsc;
863 struct ifnet *ifp = &sc->arpcom.ac_if;
864 struct cue_chain *c;
865 usbd_status err;
1550dfd9 866 int i;
984263bc
MD
867
868 if (ifp->if_flags & IFF_RUNNING)
869 return;
870
1550dfd9 871 CUE_LOCK(sc);
984263bc
MD
872
873 /*
874 * Cancel pending I/O and free all RX/TX buffers.
875 */
876#ifdef foo
877 cue_reset(sc);
878#endif
879
880 /* Set MAC address */
881 for (i = 0; i < ETHER_ADDR_LEN; i++)
1550dfd9 882 cue_csr_write_1(sc, CUE_PAR0 - i, sc->arpcom.ac_enaddr[i]);
984263bc
MD
883
884 /* Enable RX logic. */
1550dfd9 885 cue_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON|CUE_ETHCTL_MCAST_ON);
984263bc
MD
886
887 /* If we want promiscuous mode, set the allframes bit. */
888 if (ifp->if_flags & IFF_PROMISC) {
889 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
890 } else {
891 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
892 }
893
894 /* Init TX ring. */
895 if (cue_tx_list_init(sc) == ENOBUFS) {
abf66171 896 if_printf(ifp, "tx list init failed\n");
1550dfd9 897 CUE_UNLOCK(sc);
984263bc
MD
898 return;
899 }
900
901 /* Init RX ring. */
902 if (cue_rx_list_init(sc) == ENOBUFS) {
abf66171 903 if_printf(ifp, "rx list init failed\n");
1550dfd9 904 CUE_UNLOCK(sc);
984263bc
MD
905 return;
906 }
907
908 /* Load the multicast filter. */
909 cue_setmulti(sc);
910
911 /*
912 * Set the number of RX and TX buffers that we want
913 * to reserve inside the ASIC.
914 */
1550dfd9
MD
915 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
916 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
984263bc
MD
917
918 /* Set advanced operation modes. */
1550dfd9 919 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
984263bc
MD
920 CUE_AOP_EMBED_RXLEN|0x01); /* 1 wait state */
921
922 /* Program the LED operation. */
1550dfd9 923 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
984263bc
MD
924
925 /* Open RX and TX pipes. */
926 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
927 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
928 if (err) {
abf66171 929 if_printf(ifp, "open rx pipe failed: %s\n", usbd_errstr(err));
1550dfd9 930 CUE_UNLOCK(sc);
984263bc
MD
931 return;
932 }
933 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
934 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
935 if (err) {
abf66171 936 if_printf(ifp, "open tx pipe failed: %s\n", usbd_errstr(err));
1550dfd9 937 CUE_UNLOCK(sc);
984263bc
MD
938 return;
939 }
940
941 /* Start up the receive pipe. */
942 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
943 c = &sc->cue_cdata.cue_rx_chain[i];
944 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
945 c, mtod(c->cue_mbuf, char *), CUE_BUFSZ,
946 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, cue_rxeof);
947 usbd_transfer(c->cue_xfer);
948 }
949
950 ifp->if_flags |= IFF_RUNNING;
951 ifp->if_flags &= ~IFF_OACTIVE;
952
1550dfd9 953 CUE_UNLOCK(sc);
984263bc 954
3211e37c 955 callout_reset(&sc->cue_stat_timer, hz, cue_tick, sc);
984263bc
MD
956}
957
6ed427ca 958static int
bd4539cc 959cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
984263bc
MD
960{
961 struct cue_softc *sc = ifp->if_softc;
1550dfd9 962 int error = 0;
984263bc 963
1550dfd9 964 CUE_LOCK(sc);
984263bc
MD
965
966 switch(command) {
984263bc
MD
967 case SIOCSIFFLAGS:
968 if (ifp->if_flags & IFF_UP) {
969 if (ifp->if_flags & IFF_RUNNING &&
970 ifp->if_flags & IFF_PROMISC &&
971 !(sc->cue_if_flags & IFF_PROMISC)) {
972 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
973 cue_setmulti(sc);
974 } else if (ifp->if_flags & IFF_RUNNING &&
975 !(ifp->if_flags & IFF_PROMISC) &&
976 sc->cue_if_flags & IFF_PROMISC) {
977 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
978 cue_setmulti(sc);
979 } else if (!(ifp->if_flags & IFF_RUNNING))
980 cue_init(sc);
981 } else {
982 if (ifp->if_flags & IFF_RUNNING)
983 cue_stop(sc);
984 }
985 sc->cue_if_flags = ifp->if_flags;
986 error = 0;
987 break;
988 case SIOCADDMULTI:
989 case SIOCDELMULTI:
990 cue_setmulti(sc);
991 error = 0;
992 break;
993 default:
1550dfd9 994 error = ether_ioctl(ifp, command, data);
984263bc
MD
995 break;
996 }
997
1550dfd9 998 CUE_UNLOCK(sc);
984263bc
MD
999
1000 return(error);
1001}
1002
6ed427ca 1003static void
984263bc
MD
1004cue_watchdog(struct ifnet *ifp)
1005{
1006 struct cue_softc *sc;
1007 struct cue_chain *c;
984263bc 1008 usbd_status stat;
1550dfd9 1009
984263bc 1010 sc = ifp->if_softc;
1550dfd9 1011 CUE_LOCK(sc);
984263bc
MD
1012
1013 ifp->if_oerrors++;
abf66171 1014 if_printf(ifp, "watchdog timeout\n");
984263bc
MD
1015
1016 c = &sc->cue_cdata.cue_tx_chain[0];
1017 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1018 cue_txeof(c->cue_xfer, c, stat);
1019
f33fa91c 1020 if (!ifq_is_empty(&ifp->if_snd))
9db4b353 1021 if_devstart(ifp);
1550dfd9 1022 CUE_UNLOCK(sc);
984263bc
MD
1023
1024 return;
1025}
1026
1027/*
1028 * Stop the adapter and free any mbufs allocated to the
1029 * RX and TX lists.
1030 */
6ed427ca 1031static void
984263bc
MD
1032cue_stop(struct cue_softc *sc)
1033{
1034 usbd_status err;
1035 struct ifnet *ifp;
1036 int i;
1037
1550dfd9
MD
1038 CUE_LOCK(sc);
1039
984263bc
MD
1040 ifp = &sc->arpcom.ac_if;
1041 ifp->if_timer = 0;
1042
1550dfd9 1043 cue_csr_write_1(sc, CUE_ETHCTL, 0);
984263bc 1044 cue_reset(sc);
3211e37c 1045 callout_stop(&sc->cue_stat_timer);
984263bc
MD
1046
1047 /* Stop transfers. */
1048 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1049 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1050 if (err) {
abf66171
JS
1051 if_printf(ifp, "abort rx pipe failed: %s\n",
1052 usbd_errstr(err));
984263bc
MD
1053 }
1054 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1055 if (err) {
abf66171
JS
1056 if_printf(ifp, "close rx pipe failed: %s\n",
1057 usbd_errstr(err));
984263bc
MD
1058 }
1059 sc->cue_ep[CUE_ENDPT_RX] = NULL;
1060 }
1061
1062 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1063 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1064 if (err) {
abf66171
JS
1065 if_printf(ifp, "abort tx pipe failed: %s\n",
1066 usbd_errstr(err));
984263bc
MD
1067 }
1068 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1069 if (err) {
abf66171
JS
1070 if_printf(ifp, "close tx pipe failed: %s\n",
1071 usbd_errstr(err));
984263bc
MD
1072 }
1073 sc->cue_ep[CUE_ENDPT_TX] = NULL;
1074 }
1075
1076 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1077 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1078 if (err) {
abf66171
JS
1079 if_printf(ifp, "abort intr pipe failed: %s\n",
1080 usbd_errstr(err));
984263bc
MD
1081 }
1082 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1083 if (err) {
abf66171
JS
1084 if_printf(ifp, "close intr pipe failed: %s\n",
1085 usbd_errstr(err));
984263bc
MD
1086 }
1087 sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1088 }
1089
1090 /* Free RX resources. */
1091 for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1092 if (sc->cue_cdata.cue_rx_chain[i].cue_buf != NULL) {
efda3bd0 1093 kfree(sc->cue_cdata.cue_rx_chain[i].cue_buf, M_USBDEV);
984263bc
MD
1094 sc->cue_cdata.cue_rx_chain[i].cue_buf = NULL;
1095 }
1096 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1097 m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1098 sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1099 }
1100 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1101 usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1102 sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1103 }
1104 }
1105
1106 /* Free TX resources. */
1107 for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1108 if (sc->cue_cdata.cue_tx_chain[i].cue_buf != NULL) {
efda3bd0 1109 kfree(sc->cue_cdata.cue_tx_chain[i].cue_buf, M_USBDEV);
984263bc
MD
1110 sc->cue_cdata.cue_tx_chain[i].cue_buf = NULL;
1111 }
1112 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1113 m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1114 sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1115 }
1116 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1117 usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1118 sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1119 }
1120 }
1121
1122 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1550dfd9 1123 CUE_UNLOCK(sc);
984263bc
MD
1124
1125 return;
1126}
1127
1128/*
1129 * Stop all chip I/O so that the kernel's probe routines don't
1130 * get confused by errant DMAs when rebooting.
1131 */
6ed427ca
HT
1132static void
1133cue_shutdown(device_t dev)
984263bc
MD
1134{
1135 struct cue_softc *sc;
1136
1137 sc = device_get_softc(dev);
1138
1550dfd9 1139 CUE_LOCK(sc);
984263bc
MD
1140 cue_reset(sc);
1141 cue_stop(sc);
1550dfd9 1142 CUE_UNLOCK(sc);
984263bc
MD
1143
1144 return;
1145}