kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / dev / netif / aue / if_aue.c
CommitLineData
1550dfd9 1/*-
984263bc
MD
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_aue.c,v 1.78 2003/12/17 14:23:07 sanpei Exp $
984263bc
MD
33 */
34
35/*
1550dfd9
MD
36 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
37 * Datasheet is available from http://www.admtek.com.tw.
984263bc
MD
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 Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
46 * support: the control endpoint for reading/writing registers, burst
47 * read endpoint for packet reception, burst write for packet transmission
48 * and one for "interrupts." The chip uses the same RX filter scheme
49 * as the other ADMtek ethernet parts: one perfect filter entry for the
50 * the station address and a 64-bit multicast hash table. The chip supports
51 * both MII and HomePNA attachments.
52 *
53 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
54 * you're never really going to get 100Mbps speeds from this device. I
55 * think the idea is to allow the device to connect to 10 or 100Mbps
56 * networks, not necessarily to provide 100Mbps performance. Also, since
57 * the controller uses an external PHY chip, it's possible that board
58 * designers might simply choose a 10Mbps PHY.
59 *
60 * Registers are accessed using usbd_do_request(). Packet transfers are
61 * done using usbd_transfer() and friends.
62 */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/sockio.h>
67#include <sys/mbuf.h>
68#include <sys/malloc.h>
69#include <sys/kernel.h>
70#include <sys/socket.h>
1f7ab7c9 71#include <sys/bus.h>
984263bc
MD
72
73#include <net/if.h>
c85826be 74#include <net/ifq_var.h>
984263bc
MD
75#include <net/if_arp.h>
76#include <net/ethernet.h>
77#include <net/if_dl.h>
78#include <net/if_media.h>
984263bc
MD
79#include <net/bpf.h>
80
1f2de5d4
MD
81#include <bus/usb/usb.h>
82#include <bus/usb/usbdi.h>
83#include <bus/usb/usbdi_util.h>
84#include <bus/usb/usbdivar.h>
1f2de5d4 85#include <bus/usb/usb_ethersubr.h>
984263bc 86
1f2de5d4
MD
87#include "../mii_layer/mii.h"
88#include "../mii_layer/miivar.h"
984263bc 89
1f2de5d4 90#include "if_auereg.h"
984263bc 91
1550dfd9 92MODULE_DEPEND(aue, usb, 1, 1, 1);
1550dfd9
MD
93MODULE_DEPEND(aue, miibus, 1, 1, 1);
94
984263bc
MD
95/* "controller miibus0" required. See GENERIC if you get errors here. */
96#include "miibus_if.h"
97
1550dfd9
MD
98struct aue_type {
99 struct usb_devno aue_dev;
100 u_int16_t aue_flags;
101#define LSYS 0x0001 /* use Linksys reset */
102#define PNA 0x0002 /* has Home PNA */
103#define PII 0x0004 /* Pegasus II chip */
104};
105
6ed427ca 106static const struct aue_type aue_devs[] = {
3f56796e
HT
107 {{ USB_DEVICE(0x03f0, 0x811c) }, PII }, /* HP HN210E */
108 {{ USB_DEVICE(0x0411, 0x0001) }, 0 }, /* Melco LUA-TX */
109 {{ USB_DEVICE(0x0411, 0x0005) }, 0 }, /* Melco LUA-TX */
110 {{ USB_DEVICE(0x0411, 0x0009) }, PII }, /* Melco LUA2-TX */
111 {{ USB_DEVICE(0x045e, 0x007a) }, PII }, /* Microsoft MN110 */
112 {{ USB_DEVICE(0x04bb, 0x0904) }, 0 }, /* I-O DATA USB ETTX */
113 {{ USB_DEVICE(0x04bb, 0x0913) }, PII }, /* I-O DATA USB ETTX */
114 {{ USB_DEVICE(0x0506, 0x4601) }, PII }, /* 3com HomeConnect 3C460B */
115 {{ USB_DEVICE(0x050d, 0x0121) }, PII }, /* Belkin USB to LAN Converter */
116 {{ USB_DEVICE(0x056e, 0x200c) }, 0 }, /* Elecom LD-USB/TX */
117 {{ USB_DEVICE(0x056e, 0x4002) }, LSYS }, /* Elecom LD-USB/TX */
118 {{ USB_DEVICE(0x056e, 0x4005) }, PII }, /* Elecom LD-USBL/TX */
119 {{ USB_DEVICE(0x056e, 0x400b) }, 0 }, /* Elecom LD-USB/TX */
120 {{ USB_DEVICE(0x056e, 0xabc1) }, LSYS }, /* Elecom LD-USB/TX */
121 {{ USB_DEVICE(0x05cc, 0x3000) }, 0 }, /* Elsa Microlink USB2Ethernet */
122 {{ USB_DEVICE(0x066b, 0x200c) }, LSYS|PII }, /* Linksys USB10TX */
123 {{ USB_DEVICE(0x066b, 0x2202) }, LSYS }, /* Linksys USB10T */
124 {{ USB_DEVICE(0x066b, 0x2203) }, LSYS }, /* Linksys USB100TX */
125 {{ USB_DEVICE(0x066b, 0x2204) }, LSYS|PNA }, /* Linksys USB100H1 */
126 {{ USB_DEVICE(0x066b, 0x2206) }, LSYS }, /* Linksys USB10TA */
127 {{ USB_DEVICE(0x066b, 0x400b) }, LSYS|PII }, /* Linksys USB10TX */
128 {{ USB_DEVICE(0x067c, 0x1001) }, PII }, /* Siemens SpeedStream USB */
129 {{ USB_DEVICE(0x0707, 0x0200) }, 0 }, /* SMC 2202USB */
130 {{ USB_DEVICE(0x0707, 0x0201) }, PII }, /* SMC 2206USB */
131 {{ USB_DEVICE(0x07a6, 0x0986) }, PNA }, /* ADMtek AN986 */
132 {{ USB_DEVICE(0x07a6, 0x8511) }, PII }, /* ADMtek AN8511 */
133 {{ USB_DEVICE(0x07a6, 0x8513) }, PII }, /* ADMtek AN8513 */
134 {{ USB_DEVICE(0x07aa, 0x0004) }, 0 }, /* Corega FEther USB-TX */
135 {{ USB_DEVICE(0x07aa, 0x000d) }, PII }, /* Corega FEther USB-TXS */
136 {{ USB_DEVICE(0x07b8, 0x110c) }, PNA|PII }, /* AboCom XX1 */
137 {{ USB_DEVICE(0x07b8, 0x200c) }, PII }, /* AboCom XX2 */
138 {{ USB_DEVICE(0x07b8, 0x4002) }, LSYS }, /* AboCom UFE1000 */
139 {{ USB_DEVICE(0x07b8, 0x4003) }, 0 }, /* AboCom DSB650TX_PNA */
140 {{ USB_DEVICE(0x07b8, 0x4004) }, PNA }, /* AboCom XX4 */
141 {{ USB_DEVICE(0x07b8, 0x4007) }, PNA }, /* AboCom XX5 */
142 {{ USB_DEVICE(0x07b8, 0x400b) }, PII }, /* AboCom XX6 */
143 {{ USB_DEVICE(0x07b8, 0x400c) }, PII }, /* AboCom XX7 */
144 {{ USB_DEVICE(0x07b8, 0x4102) }, PII }, /* AboCom XX8 */
145 {{ USB_DEVICE(0x07b8, 0x4104) }, PNA }, /* AboCom XX9 */
146 {{ USB_DEVICE(0x07b8, 0xabc1) }, 0 }, /* AboCom XX10 */
147 {{ USB_DEVICE(0x083a, 0x1046) }, 0 }, /* Accton USB320-EC */
148 {{ USB_DEVICE(0x083a, 0x5046) }, PII }, /* Accton SpeedStream 1001 */
149 {{ USB_DEVICE(0x08d1, 0x0003) }, PII }, /* SmartBridges smartNIC 2 PnP */
150 {{ USB_DEVICE(0x08dd, 0x0986) }, 0 }, /* Billionton USB100N */
151 {{ USB_DEVICE(0x08dd, 0x0987) }, PNA }, /* Billionton USB100LP */
152 {{ USB_DEVICE(0x08dd, 0x0988) }, 0 }, /* Billionton USB100EL */
153 {{ USB_DEVICE(0x08dd, 0x8511) }, PII }, /* Billionton USBE100 */
154 {{ USB_DEVICE(0x0951, 0x000a) }, 0 }, /* Kingston KNU101TX */
155 {{ USB_DEVICE(0x0e66, 0x400c) }, PII }, /* Hawking UF100 */
156 {{ USB_DEVICE(0x15e8, 0x9100) }, 0 }, /* SOHOware NUB100 */
157 {{ USB_DEVICE(0x2001, 0x200c) }, LSYS|PII },/* D-Link DSB650TX4 */
158 {{ USB_DEVICE(0x2001, 0x4001) }, LSYS }, /* D-Link DSB650TX1 */
159 {{ USB_DEVICE(0x2001, 0x4002) }, LSYS }, /* D-Link DSB650TX */
160 {{ USB_DEVICE(0x2001, 0x4003) }, PNA }, /* D-Link DSB650TX_PNA */
161 {{ USB_DEVICE(0x2001, 0x400b) }, LSYS|PII }, /* D-Link DSB650TX3 */
162 {{ USB_DEVICE(0x2001, 0x4102) }, LSYS|PII }, /* D-Link DSB650TX2 */
163 {{ USB_DEVICE(0x2001, 0xabc1) }, LSYS }, /* D-Link DSB650 */
984263bc 164};
1550dfd9 165#define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
984263bc 166
6ed427ca
HT
167static int aue_match(device_t);
168static int aue_attach(device_t);
169static int aue_detach(device_t);
984263bc 170
6ed427ca
HT
171static void aue_reset_pegasus_II(struct aue_softc *sc);
172static int aue_tx_list_init(struct aue_softc *);
173static int aue_rx_list_init(struct aue_softc *);
174static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
175static int aue_encap(struct aue_softc *, struct mbuf *, int);
984263bc 176#ifdef AUE_INTR_PIPE
6ed427ca 177static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
984263bc 178#endif
6ed427ca
HT
179static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
180static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
181static void aue_tick(void *);
182static void aue_rxstart(struct ifnet *);
183static void aue_start(struct ifnet *);
184static int aue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
185static void aue_init(void *);
186static void aue_stop(struct aue_softc *);
187static void aue_watchdog(struct ifnet *);
188static void aue_shutdown(device_t);
189static int aue_ifmedia_upd(struct ifnet *);
190static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
191
192static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
193static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
194static int aue_miibus_readreg(device_t, int, int);
195static int aue_miibus_writereg(device_t, int, int, int);
196static void aue_miibus_statchg(device_t);
197
198static void aue_setmulti(struct aue_softc *);
199static void aue_reset(struct aue_softc *);
200
201static int aue_csr_read_1(struct aue_softc *, int);
202static int aue_csr_write_1(struct aue_softc *, int, int);
203static int aue_csr_read_2(struct aue_softc *, int);
204static int aue_csr_write_2(struct aue_softc *, int, int);
205
206static device_method_t aue_methods[] = {
984263bc
MD
207 /* Device interface */
208 DEVMETHOD(device_probe, aue_match),
209 DEVMETHOD(device_attach, aue_attach),
210 DEVMETHOD(device_detach, aue_detach),
211 DEVMETHOD(device_shutdown, aue_shutdown),
212
213 /* bus interface */
214 DEVMETHOD(bus_print_child, bus_generic_print_child),
215 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
216
217 /* MII interface */
218 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
219 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
220 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
221
222 { 0, 0 }
223};
224
6ed427ca 225static driver_t aue_driver = {
984263bc
MD
226 "aue",
227 aue_methods,
228 sizeof(struct aue_softc)
229};
230
6ed427ca 231static devclass_t aue_devclass;
984263bc 232
32832096 233DECLARE_DUMMY_MODULE(if_aue);
aa2b9d05
SW
234DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, NULL);
235DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, NULL, NULL);
984263bc
MD
236
237#define AUE_SETBIT(sc, reg, x) \
1550dfd9 238 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
984263bc
MD
239
240#define AUE_CLRBIT(sc, reg, x) \
1550dfd9 241 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
984263bc 242
6ed427ca 243static int
1550dfd9 244aue_csr_read_1(struct aue_softc *sc, int reg)
984263bc
MD
245{
246 usb_device_request_t req;
247 usbd_status err;
248 u_int8_t val = 0;
984263bc 249
1550dfd9 250 if (sc->aue_dying)
984263bc
MD
251 return(0);
252
1550dfd9 253 AUE_LOCK(sc);
984263bc
MD
254
255 req.bmRequestType = UT_READ_VENDOR_DEVICE;
256 req.bRequest = AUE_UR_READREG;
257 USETW(req.wValue, 0);
258 USETW(req.wIndex, reg);
259 USETW(req.wLength, 1);
260
1550dfd9 261 err = usbd_do_request(sc->aue_udev, &req, &val);
984263bc 262
1550dfd9 263 AUE_UNLOCK(sc);
984263bc 264
1550dfd9
MD
265 if (err) {
266 return (0);
267 }
984263bc 268
1550dfd9 269 return (val);
984263bc
MD
270}
271
6ed427ca 272static int
1550dfd9 273aue_csr_read_2(struct aue_softc *sc, int reg)
984263bc
MD
274{
275 usb_device_request_t req;
276 usbd_status err;
277 u_int16_t val = 0;
984263bc 278
1550dfd9
MD
279 if (sc->aue_dying)
280 return (0);
984263bc 281
1550dfd9 282 AUE_LOCK(sc);
984263bc
MD
283
284 req.bmRequestType = UT_READ_VENDOR_DEVICE;
285 req.bRequest = AUE_UR_READREG;
286 USETW(req.wValue, 0);
287 USETW(req.wIndex, reg);
288 USETW(req.wLength, 2);
289
1550dfd9 290 err = usbd_do_request(sc->aue_udev, &req, &val);
984263bc 291
1550dfd9 292 AUE_UNLOCK(sc);
984263bc 293
1550dfd9
MD
294 if (err) {
295 return (0);
296 }
984263bc 297
1550dfd9 298 return (val);
984263bc
MD
299}
300
6ed427ca 301static int
1550dfd9 302aue_csr_write_1(struct aue_softc *sc, int reg, int val)
984263bc
MD
303{
304 usb_device_request_t req;
305 usbd_status err;
984263bc 306
1550dfd9
MD
307 if (sc->aue_dying)
308 return (0);
984263bc 309
1550dfd9 310 AUE_LOCK(sc);
984263bc
MD
311
312 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
313 req.bRequest = AUE_UR_WRITEREG;
314 USETW(req.wValue, val);
315 USETW(req.wIndex, reg);
316 USETW(req.wLength, 1);
317
1550dfd9 318 err = usbd_do_request(sc->aue_udev, &req, &val);
984263bc 319
1550dfd9 320 AUE_UNLOCK(sc);
984263bc 321
1550dfd9
MD
322 if (err) {
323 return (-1);
324 }
984263bc 325
1550dfd9 326 return (0);
984263bc
MD
327}
328
6ed427ca 329static int
1550dfd9 330aue_csr_write_2(struct aue_softc *sc, int reg, int val)
984263bc
MD
331{
332 usb_device_request_t req;
333 usbd_status err;
984263bc 334
1550dfd9
MD
335 if (sc->aue_dying)
336 return (0);
984263bc 337
1550dfd9 338 AUE_LOCK(sc);
984263bc
MD
339
340 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
341 req.bRequest = AUE_UR_WRITEREG;
342 USETW(req.wValue, val);
343 USETW(req.wIndex, reg);
344 USETW(req.wLength, 2);
345
1550dfd9 346 err = usbd_do_request(sc->aue_udev, &req, &val);
984263bc 347
1550dfd9 348 AUE_UNLOCK(sc);
984263bc 349
1550dfd9
MD
350 if (err) {
351 return (-1);
352 }
984263bc 353
1550dfd9 354 return (0);
984263bc
MD
355}
356
357/*
358 * Read a word of data stored in the EEPROM at address 'addr.'
359 */
6ed427ca 360static void
984263bc
MD
361aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
362{
f96d6c88 363 int i;
1550dfd9 364 u_int16_t word = 0;
984263bc 365
1550dfd9
MD
366 aue_csr_write_1(sc, AUE_EE_REG, addr);
367 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
984263bc
MD
368
369 for (i = 0; i < AUE_TIMEOUT; i++) {
1550dfd9 370 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
984263bc
MD
371 break;
372 }
373
e849b424
JS
374 if (i == AUE_TIMEOUT)
375 if_printf(&sc->arpcom.ac_if, "EEPROM read timed out\n");
984263bc 376
1550dfd9 377 word = aue_csr_read_2(sc, AUE_EE_DATA);
984263bc
MD
378 *dest = word;
379
380 return;
381}
382
383/*
384 * Read a sequence of words from the EEPROM.
385 */
6ed427ca 386static void
984263bc
MD
387aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
388{
389 int i;
390 u_int16_t word = 0, *ptr;
391
392 for (i = 0; i < cnt; i++) {
393 aue_eeprom_getword(sc, off + i, &word);
394 ptr = (u_int16_t *)(dest + (i * 2));
395 if (swap)
396 *ptr = ntohs(word);
397 else
398 *ptr = word;
399 }
400
401 return;
402}
403
6ed427ca
HT
404static int
405aue_miibus_readreg(device_t dev, int phy, int reg)
984263bc 406{
6ed427ca 407 struct aue_softc *sc = device_get_softc(dev);
984263bc
MD
408 int i;
409 u_int16_t val = 0;
410
984263bc
MD
411 /*
412 * The Am79C901 HomePNA PHY actually contains
413 * two transceivers: a 1Mbps HomePNA PHY and a
414 * 10Mbps full/half duplex ethernet PHY with
415 * NWAY autoneg. However in the ADMtek adapter,
416 * only the 1Mbps PHY is actually connected to
417 * anything, so we ignore the 10Mbps one. It
418 * happens to be configured for MII address 3,
419 * so we filter that out.
420 */
3f56796e 421 if (sc->aue_vendor == 0x07a6 && sc->aue_product == 0x0986) {
984263bc 422 if (phy == 3)
1550dfd9 423 return (0);
984263bc
MD
424#ifdef notdef
425 if (phy != 1)
1550dfd9 426 return (0);
984263bc
MD
427#endif
428 }
429
1550dfd9
MD
430 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
431 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
984263bc
MD
432
433 for (i = 0; i < AUE_TIMEOUT; i++) {
1550dfd9 434 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
984263bc
MD
435 break;
436 }
437
e849b424
JS
438 if (i == AUE_TIMEOUT)
439 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
984263bc 440
1550dfd9 441 val = aue_csr_read_2(sc, AUE_PHY_DATA);
984263bc 442
1550dfd9 443 return (val);
984263bc
MD
444}
445
6ed427ca
HT
446static int
447aue_miibus_writereg(device_t dev, int phy, int reg, int data)
984263bc 448{
6ed427ca 449 struct aue_softc *sc = device_get_softc(dev);
984263bc
MD
450 int i;
451
452 if (phy == 3)
1550dfd9 453 return (0);
984263bc 454
1550dfd9
MD
455 aue_csr_write_2(sc, AUE_PHY_DATA, data);
456 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
457 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
984263bc
MD
458
459 for (i = 0; i < AUE_TIMEOUT; i++) {
1550dfd9 460 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
984263bc
MD
461 break;
462 }
463
e849b424
JS
464 if (i == AUE_TIMEOUT)
465 if_printf(&sc->arpcom.ac_if, "MII read timed out\n");
984263bc
MD
466
467 return(0);
468}
469
6ed427ca
HT
470static void
471aue_miibus_statchg(device_t dev)
984263bc 472{
6ed427ca 473 struct aue_softc *sc = device_get_softc(dev);
1550dfd9 474 struct mii_data *mii = GET_MII(sc);
984263bc 475
1550dfd9 476 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
984263bc
MD
477 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
478 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
479 } else {
480 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
481 }
482
1550dfd9 483 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
984263bc 484 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
1550dfd9 485 else
984263bc 486 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
1550dfd9
MD
487
488 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
984263bc
MD
489
490 /*
491 * Set the LED modes on the LinkSys adapter.
492 * This turns on the 'dual link LED' bin in the auxmode
493 * register of the Broadcom PHY.
494 */
1550dfd9
MD
495 if (sc->aue_flags & LSYS) {
496 u_int16_t auxmode;
984263bc
MD
497 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
498 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
499 }
500
501 return;
502}
503
7405ab47 504#define AUE_BITS 6
984263bc 505
6ed427ca 506static void
984263bc
MD
507aue_setmulti(struct aue_softc *sc)
508{
509 struct ifnet *ifp;
510 struct ifmultiaddr *ifma;
511 u_int32_t h = 0, i;
512
513 ifp = &sc->arpcom.ac_if;
514
515 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
516 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
517 return;
518 }
519
520 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
521
522 /* first, zot all the existing hash bits */
523 for (i = 0; i < 8; i++)
1550dfd9 524 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
984263bc
MD
525
526 /* now program new ones */
441d34b2 527 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1550dfd9 528 {
984263bc
MD
529 if (ifma->ifma_addr->sa_family != AF_LINK)
530 continue;
7405ab47
JS
531 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
532 ifma->ifma_addr), ETHER_ADDR_LEN) & ((1 << AUE_BITS) - 1);
984263bc
MD
533 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
534 }
535
536 return;
537}
538
6ed427ca 539static void
984263bc
MD
540aue_reset_pegasus_II(struct aue_softc *sc)
541{
542 /* Magic constants taken from Linux driver. */
1550dfd9
MD
543 aue_csr_write_1(sc, AUE_REG_1D, 0);
544 aue_csr_write_1(sc, AUE_REG_7B, 2);
984263bc
MD
545#if 0
546 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
1550dfd9 547 aue_csr_write_1(sc, AUE_REG_81, 6);
984263bc
MD
548 else
549#endif
1550dfd9 550 aue_csr_write_1(sc, AUE_REG_81, 2);
984263bc
MD
551}
552
6ed427ca 553static void
984263bc
MD
554aue_reset(struct aue_softc *sc)
555{
f96d6c88 556 int i;
984263bc
MD
557
558 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
559
560 for (i = 0; i < AUE_TIMEOUT; i++) {
1550dfd9 561 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
984263bc
MD
562 break;
563 }
564
565 if (i == AUE_TIMEOUT)
e849b424 566 if_printf(&sc->arpcom.ac_if, "reset failed\n");
984263bc
MD
567
568 /*
569 * The PHY(s) attached to the Pegasus chip may be held
570 * in reset until we flip on the GPIO outputs. Make sure
571 * to set the GPIO pins high so that the PHY(s) will
572 * be enabled.
573 *
574 * Note: We force all of the GPIO pins low first, *then*
575 * enable the ones we want.
576 */
1550dfd9
MD
577 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
578 aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
579
580 if (sc->aue_flags & LSYS) {
581 /* Grrr. LinkSys has to be different from everyone else. */
582 aue_csr_write_1(sc, AUE_GPIO0,
583 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
584 aue_csr_write_1(sc, AUE_GPIO0,
585 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
984263bc
MD
586 }
587
1550dfd9 588 if (sc->aue_flags & PII)
984263bc
MD
589 aue_reset_pegasus_II(sc);
590
591 /* Wait a little while for the chip to get its brains in order. */
592 DELAY(10000);
593
1550dfd9 594 return;
984263bc
MD
595}
596
597/*
598 * Probe for a Pegasus chip.
599 */
e785a5d9
HT
600static int
601aue_match(device_t self)
984263bc 602{
e785a5d9 603 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 604
1550dfd9
MD
605 if (uaa->iface != NULL)
606 return (UMATCH_NONE);
984263bc 607
1550dfd9
MD
608 return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
609 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
984263bc
MD
610}
611
612/*
613 * Attach the interface. Allocate softc structures, do ifmedia
614 * setup and ethernet/BPF attach.
615 */
e785a5d9
HT
616static int
617aue_attach(device_t self)
984263bc 618{
e785a5d9
HT
619 struct aue_softc *sc = device_get_softc(self);
620 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
621 u_char eaddr[ETHER_ADDR_LEN];
622 struct ifnet *ifp;
1550dfd9
MD
623 usbd_interface_handle iface;
624 usbd_status err;
984263bc
MD
625 usb_interface_descriptor_t *id;
626 usb_endpoint_descriptor_t *ed;
627 int i;
984263bc 628
984263bc 629 sc->aue_udev = uaa->device;
7824277e 630 callout_init(&sc->aue_stat_timer);
984263bc
MD
631
632 if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
e849b424
JS
633 device_printf(self, "setting config no %d failed\n",
634 AUE_CONFIG_NO);
e785a5d9 635 return ENXIO;
984263bc
MD
636 }
637
1550dfd9
MD
638 err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
639 if (err) {
e849b424 640 device_printf(self, "getting interface handle failed\n");
e785a5d9 641 return ENXIO;
984263bc
MD
642 }
643
1550dfd9
MD
644 sc->aue_iface = iface;
645 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
646
647 sc->aue_product = uaa->product;
648 sc->aue_vendor = uaa->vendor;
649
650 id = usbd_get_interface_descriptor(sc->aue_iface);
984263bc 651
984263bc
MD
652 /* Find endpoints. */
653 for (i = 0; i < id->bNumEndpoints; i++) {
1550dfd9
MD
654 ed = usbd_interface2endpoint_descriptor(iface, i);
655 if (ed == NULL) {
e849b424 656 device_printf(self, "couldn't get ep %d\n", i);
e785a5d9 657 return ENXIO;
984263bc
MD
658 }
659 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 660 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
661 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
662 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1550dfd9 663 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
984263bc
MD
664 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
665 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9 666 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
984263bc
MD
667 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
668 }
669 }
670
1550dfd9
MD
671 AUE_LOCK(sc);
672
e849b424
JS
673 ifp = &sc->arpcom.ac_if;
674 if_initname(ifp, device_get_name(self), device_get_unit(self));
675
984263bc
MD
676 /* Reset the adapter. */
677 aue_reset(sc);
678
679 /*
680 * Get station address from the EEPROM.
681 */
682 aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
683
984263bc 684 ifp->if_softc = sc;
984263bc
MD
685 ifp->if_mtu = ETHERMTU;
686 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
687 ifp->if_ioctl = aue_ioctl;
984263bc
MD
688 ifp->if_start = aue_start;
689 ifp->if_watchdog = aue_watchdog;
690 ifp->if_init = aue_init;
691 ifp->if_baudrate = 10000000;
c85826be
JS
692 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
693 ifq_set_ready(&ifp->if_snd);
984263bc
MD
694
695 /*
696 * Do MII setup.
697 * NOTE: Doing this causes child devices to be attached to us,
698 * which we would normally disconnect at in the detach routine
699 * using device_delete_child(). However the USB code is set up
700 * such that when this driver is removed, all children devices
701 * are removed as well. In effect, the USB code ends up detaching
702 * all of our children for us, so we don't have to do is ourselves
703 * in aue_detach(). It's important to point this out since if
704 * we *do* try to detach the child devices ourselves, we will
705 * end up getting the children deleted twice, which will crash
706 * the system.
707 */
708 if (mii_phy_probe(self, &sc->aue_miibus,
709 aue_ifmedia_upd, aue_ifmedia_sts)) {
e849b424 710 device_printf(self, "MII without any PHY!\n");
1550dfd9 711 AUE_UNLOCK(sc);
e785a5d9 712 return ENXIO;
984263bc
MD
713 }
714
984263bc
MD
715 /*
716 * Call MI attach routine.
717 */
78195a76 718 ether_ifattach(ifp, eaddr, NULL);
984263bc 719 usb_register_netisr();
1550dfd9 720 sc->aue_dying = 0;
984263bc 721
1550dfd9 722 AUE_UNLOCK(sc);
e785a5d9 723 return 0;
984263bc
MD
724}
725
6ed427ca
HT
726static int
727aue_detach(device_t dev)
984263bc
MD
728{
729 struct aue_softc *sc;
730 struct ifnet *ifp;
984263bc
MD
731
732 sc = device_get_softc(dev);
1550dfd9 733 AUE_LOCK(sc);
984263bc
MD
734 ifp = &sc->arpcom.ac_if;
735
1550dfd9 736 sc->aue_dying = 1;
7824277e 737 callout_stop(&sc->aue_stat_timer);
1550dfd9 738 ether_ifdetach(ifp);
984263bc
MD
739
740 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
741 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
742 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
743 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
744#ifdef AUE_INTR_PIPE
745 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
746 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
747#endif
984263bc 748
1550dfd9 749 AUE_UNLOCK(sc);
1550dfd9
MD
750
751 return (0);
984263bc
MD
752}
753
754/*
755 * Initialize an RX descriptor and attach an MBUF cluster.
756 */
6ed427ca 757static int
984263bc
MD
758aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
759{
760 struct mbuf *m_new = NULL;
761
762 if (m == NULL) {
17b71a59 763 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
984263bc 764 if (m_new == NULL) {
17b71a59
JH
765 if_printf(&sc->arpcom.ac_if,
766 "no memory for rx list -- packet dropped!\n");
1550dfd9 767 return (ENOBUFS);
984263bc
MD
768 }
769 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
770 } else {
771 m_new = m;
772 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
773 m_new->m_data = m_new->m_ext.ext_buf;
774 }
775
776 m_adj(m_new, ETHER_ALIGN);
777 c->aue_mbuf = m_new;
778
1550dfd9 779 return (0);
984263bc
MD
780}
781
6ed427ca 782static int
984263bc
MD
783aue_rx_list_init(struct aue_softc *sc)
784{
785 struct aue_cdata *cd;
786 struct aue_chain *c;
787 int i;
788
789 cd = &sc->aue_cdata;
790 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
791 c = &cd->aue_rx_chain[i];
792 c->aue_sc = sc;
793 c->aue_idx = i;
794 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1550dfd9 795 return (ENOBUFS);
984263bc
MD
796 if (c->aue_xfer == NULL) {
797 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
798 if (c->aue_xfer == NULL)
1550dfd9 799 return (ENOBUFS);
984263bc
MD
800 }
801 }
802
1550dfd9 803 return (0);
984263bc
MD
804}
805
6ed427ca 806static int
984263bc
MD
807aue_tx_list_init(struct aue_softc *sc)
808{
809 struct aue_cdata *cd;
810 struct aue_chain *c;
811 int i;
812
813 cd = &sc->aue_cdata;
814 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
815 c = &cd->aue_tx_chain[i];
816 c->aue_sc = sc;
817 c->aue_idx = i;
818 c->aue_mbuf = NULL;
819 if (c->aue_xfer == NULL) {
820 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
821 if (c->aue_xfer == NULL)
1550dfd9 822 return (ENOBUFS);
984263bc 823 }
efda3bd0 824 c->aue_buf = kmalloc(AUE_BUFSZ, M_USBDEV, M_WAITOK);
984263bc
MD
825 }
826
1550dfd9 827 return (0);
984263bc
MD
828}
829
830#ifdef AUE_INTR_PIPE
6ed427ca 831static void
984263bc
MD
832aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
833{
1550dfd9 834 struct aue_softc *sc = priv;
984263bc
MD
835 struct ifnet *ifp;
836 struct aue_intrpkt *p;
984263bc 837
1550dfd9 838 AUE_LOCK(sc);
984263bc
MD
839 ifp = &sc->arpcom.ac_if;
840
841 if (!(ifp->if_flags & IFF_RUNNING)) {
1550dfd9 842 AUE_UNLOCK(sc);
984263bc
MD
843 return;
844 }
845
846 if (status != USBD_NORMAL_COMPLETION) {
847 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1550dfd9 848 AUE_UNLOCK(sc);
984263bc
MD
849 return;
850 }
e849b424 851 if_printf(ifp, "usb error on intr: %s\n", usbd_errstr(status));
984263bc
MD
852 if (status == USBD_STALLED)
853 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1550dfd9 854 AUE_UNLOCK(sc);
984263bc
MD
855 return;
856 }
857
858 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
859
860 if (p->aue_txstat0)
861 ifp->if_oerrors++;
862
863 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
864 ifp->if_collisions++;
865
1550dfd9 866 AUE_UNLOCK(sc);
984263bc
MD
867 return;
868}
869#endif
870
6ed427ca 871static void
984263bc
MD
872aue_rxstart(struct ifnet *ifp)
873{
874 struct aue_softc *sc;
875 struct aue_chain *c;
876
877 sc = ifp->if_softc;
1550dfd9 878 AUE_LOCK(sc);
984263bc
MD
879 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
880
881 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
882 ifp->if_ierrors++;
1550dfd9 883 AUE_UNLOCK(sc);
984263bc
MD
884 return;
885 }
886
887 /* Setup new transfer. */
888 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
889 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
890 USBD_NO_TIMEOUT, aue_rxeof);
891 usbd_transfer(c->aue_xfer);
892
1550dfd9 893 AUE_UNLOCK(sc);
984263bc
MD
894 return;
895}
896
897/*
898 * A frame has been uploaded: pass the resulting mbuf chain up to
899 * the higher level protocols.
900 */
6ed427ca 901static void
984263bc
MD
902aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
903{
904 struct aue_chain *c = priv;
905 struct aue_softc *sc = c->aue_sc;
906 struct mbuf *m;
907 struct ifnet *ifp;
908 int total_len = 0;
909 struct aue_rxpkt r;
910
1550dfd9
MD
911 if (sc->aue_dying)
912 return;
b464d724 913
984263bc
MD
914 ifp = &sc->arpcom.ac_if;
915
b464d724 916 if (!(ifp->if_flags & IFF_RUNNING))
984263bc
MD
917 return;
918
919 if (status != USBD_NORMAL_COMPLETION) {
b464d724 920 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
984263bc 921 return;
e849b424
JS
922 if (usbd_ratecheck(&sc->aue_rx_notice)) {
923 if_printf(ifp, "usb error on rx: %s\n",
1550dfd9 924 usbd_errstr(status));
e849b424 925 }
984263bc
MD
926 if (status == USBD_STALLED)
927 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
928 goto done;
929 }
930
931 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
932
933 if (total_len <= 4 + ETHER_CRC_LEN) {
934 ifp->if_ierrors++;
935 goto done;
936 }
937
938 m = c->aue_mbuf;
939 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
940
941 /* Turn off all the non-error bits in the rx status word. */
942 r.aue_rxstat &= AUE_RXSTAT_MASK;
943
944 if (r.aue_rxstat) {
945 ifp->if_ierrors++;
946 goto done;
947 }
948
949 /* No errors; receive the packet. */
950 total_len -= (4 + ETHER_CRC_LEN);
951
952 ifp->if_ipackets++;
ca3f7aba 953 m->m_pkthdr.rcvif = ifp;
984263bc
MD
954 m->m_pkthdr.len = m->m_len = total_len;
955
956 /* Put the packet on the special USB input queue. */
957 usb_ether_input(m);
fcd8dff9 958 aue_rxstart(ifp);
984263bc
MD
959 return;
960done:
961
962 /* Setup new transfer. */
963 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
964 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
965 USBD_NO_TIMEOUT, aue_rxeof);
966 usbd_transfer(xfer);
b464d724 967}
984263bc 968
b464d724
SZ
969static void
970aue_start_ipifunc(void *arg)
971{
972 struct ifnet *ifp = arg;
002c1265 973 struct lwkt_msg *lmsg = &ifp->if_start_nmsg[mycpuid].lmsg;
b464d724
SZ
974
975 crit_enter();
976 if (lmsg->ms_flags & MSGF_DONE)
006f2e1f 977 lwkt_sendmsg(netisr_portfn(mycpuid), lmsg);
b464d724
SZ
978 crit_exit();
979}
980
981static void
982aue_start_schedule(struct ifnet *ifp)
983{
b464d724
SZ
984 int cpu;
985
986 cpu = ifp->if_start_cpuid(ifp);
987 if (cpu != mycpuid)
988 lwkt_send_ipiq(globaldata_find(cpu), aue_start_ipifunc, ifp);
989 else
1918fc5c 990 aue_start_ipifunc(ifp);
984263bc
MD
991}
992
993/*
994 * A frame was downloaded to the chip. It's safe for us to clean up
995 * the list buffers.
996 */
6ed427ca 997static void
984263bc
MD
998aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
999{
1550dfd9
MD
1000 struct aue_chain *c = priv;
1001 struct aue_softc *sc = c->aue_sc;
984263bc
MD
1002 struct ifnet *ifp;
1003 usbd_status err;
984263bc 1004
984263bc
MD
1005 ifp = &sc->arpcom.ac_if;
1006
1007 if (status != USBD_NORMAL_COMPLETION) {
b464d724 1008 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
984263bc 1009 return;
e849b424 1010 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
984263bc
MD
1011 if (status == USBD_STALLED)
1012 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
984263bc
MD
1013 return;
1014 }
1015
984263bc 1016 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err);
984263bc
MD
1017 if (err)
1018 ifp->if_oerrors++;
1019 else
1020 ifp->if_opackets++;
1021
b464d724
SZ
1022 /* XXX should hold serializer */
1023 ifp->if_timer = 0;
1024 ifp->if_flags &= ~IFF_OACTIVE;
984263bc 1025
b464d724
SZ
1026 if (!ifq_is_empty(&ifp->if_snd))
1027 aue_start_schedule(ifp);
984263bc
MD
1028}
1029
6ed427ca 1030static void
984263bc
MD
1031aue_tick(void *xsc)
1032{
1550dfd9 1033 struct aue_softc *sc = xsc;
984263bc
MD
1034 struct ifnet *ifp;
1035 struct mii_data *mii;
984263bc 1036
1550dfd9 1037 if (sc == NULL)
984263bc 1038 return;
1550dfd9 1039
984263bc 1040 ifp = &sc->arpcom.ac_if;
b464d724
SZ
1041
1042 lwkt_serialize_enter(ifp->if_serializer);
1043
1550dfd9 1044 mii = GET_MII(sc);
984263bc 1045 if (mii == NULL) {
b464d724 1046 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1047 return;
1048 }
1049
1050 mii_tick(mii);
1550dfd9
MD
1051 if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1052 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1053 sc->aue_link++;
c85826be 1054 if (!ifq_is_empty(&ifp->if_snd))
b464d724 1055 aue_start_schedule(ifp);
984263bc
MD
1056 }
1057
7824277e 1058 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
984263bc 1059
b464d724 1060 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
1061}
1062
6ed427ca 1063static int
984263bc
MD
1064aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1065{
1066 int total_len;
1067 struct aue_chain *c;
1068 usbd_status err;
1069
1070 c = &sc->aue_cdata.aue_tx_chain[idx];
1071
1072 /*
1073 * Copy the mbuf data into a contiguous buffer, leaving two
1074 * bytes at the beginning to hold the frame length.
1075 */
1076 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1077 c->aue_mbuf = m;
1078
1079 total_len = m->m_pkthdr.len + 2;
1080
1081 /*
1082 * The ADMtek documentation says that the packet length is
1083 * supposed to be specified in the first two bytes of the
1084 * transfer, however it actually seems to ignore this info
1085 * and base the frame size on the bulk transfer length.
1086 */
1087 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1088 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1089
b464d724
SZ
1090 m_freem(c->aue_mbuf);
1091 c->aue_mbuf = NULL;
1092 m = NULL;
1093
984263bc
MD
1094 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1095 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER,
1096 10000, aue_txeof);
1097
1098 /* Transmit */
1099 err = usbd_transfer(c->aue_xfer);
1100 if (err != USBD_IN_PROGRESS) {
1101 aue_stop(sc);
1550dfd9 1102 return (EIO);
984263bc
MD
1103 }
1104
1105 sc->aue_cdata.aue_tx_cnt++;
1106
1550dfd9 1107 return (0);
984263bc
MD
1108}
1109
6ed427ca 1110static void
984263bc
MD
1111aue_start(struct ifnet *ifp)
1112{
1550dfd9 1113 struct aue_softc *sc = ifp->if_softc;
984263bc
MD
1114 struct mbuf *m_head = NULL;
1115
1550dfd9 1116 AUE_LOCK(sc);
984263bc 1117
1550dfd9 1118 if (!sc->aue_link) {
9db4b353 1119 ifq_purge(&ifp->if_snd);
1550dfd9 1120 AUE_UNLOCK(sc);
984263bc 1121 return;
1550dfd9 1122 }
984263bc 1123
b464d724 1124 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING) {
1550dfd9 1125 AUE_UNLOCK(sc);
984263bc 1126 return;
1550dfd9 1127 }
984263bc 1128
9db4b353 1129 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1550dfd9
MD
1130 if (m_head == NULL) {
1131 AUE_UNLOCK(sc);
984263bc 1132 return;
1550dfd9 1133 }
984263bc
MD
1134
1135 if (aue_encap(sc, m_head, 0)) {
9db4b353 1136 /* aue_encap() will free m_head, if we reach here */
984263bc 1137 ifp->if_flags |= IFF_OACTIVE;
1550dfd9 1138 AUE_UNLOCK(sc);
984263bc
MD
1139 return;
1140 }
1141
1142 /*
1143 * If there's a BPF listener, bounce a copy of this frame
1144 * to him.
1145 */
1550dfd9 1146 BPF_MTAP(ifp, m_head);
984263bc
MD
1147
1148 ifp->if_flags |= IFF_OACTIVE;
1149
1150 /*
1151 * Set a timeout in case the chip goes out to lunch.
1152 */
1153 ifp->if_timer = 5;
1550dfd9 1154 AUE_UNLOCK(sc);
984263bc
MD
1155
1156 return;
1157}
1158
6ed427ca 1159static void
984263bc
MD
1160aue_init(void *xsc)
1161{
1162 struct aue_softc *sc = xsc;
1163 struct ifnet *ifp = &sc->arpcom.ac_if;
1550dfd9 1164 struct mii_data *mii = GET_MII(sc);
984263bc
MD
1165 struct aue_chain *c;
1166 usbd_status err;
1550dfd9 1167 int i;
984263bc 1168
1550dfd9 1169 AUE_LOCK(sc);
984263bc 1170
1550dfd9
MD
1171 if (ifp->if_flags & IFF_RUNNING) {
1172 AUE_UNLOCK(sc);
1173 return;
1174 }
984263bc
MD
1175
1176 /*
1177 * Cancel pending I/O and free all RX/TX buffers.
1178 */
1179 aue_reset(sc);
1180
984263bc
MD
1181 /* Set MAC address */
1182 for (i = 0; i < ETHER_ADDR_LEN; i++)
1550dfd9 1183 aue_csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
984263bc
MD
1184
1185 /* If we want promiscuous mode, set the allframes bit. */
1550dfd9 1186 if (ifp->if_flags & IFF_PROMISC)
984263bc 1187 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1550dfd9 1188 else
984263bc 1189 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
984263bc
MD
1190
1191 /* Init TX ring. */
1192 if (aue_tx_list_init(sc) == ENOBUFS) {
e849b424 1193 if_printf(&sc->arpcom.ac_if, "tx list init failed\n");
1550dfd9 1194 AUE_UNLOCK(sc);
984263bc
MD
1195 return;
1196 }
1197
1198 /* Init RX ring. */
1199 if (aue_rx_list_init(sc) == ENOBUFS) {
e849b424 1200 if_printf(&sc->arpcom.ac_if, "rx list init failed\n");
1550dfd9 1201 AUE_UNLOCK(sc);
984263bc
MD
1202 return;
1203 }
1204
1205#ifdef AUE_INTR_PIPE
efda3bd0 1206 sc->aue_cdata.aue_ibuf = kmalloc(AUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
984263bc
MD
1207#endif
1208
1209 /* Load the multicast filter. */
1210 aue_setmulti(sc);
1211
1212 /* Enable RX and TX */
1550dfd9 1213 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
984263bc
MD
1214 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1215 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1550dfd9 1216
984263bc
MD
1217 mii_mediachg(mii);
1218
1219 /* Open RX and TX pipes. */
1220 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1221 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1222 if (err) {
e849b424
JS
1223 if_printf(&sc->arpcom.ac_if, "open rx pipe failed: %s\n",
1224 usbd_errstr(err));
1550dfd9 1225 AUE_UNLOCK(sc);
984263bc
MD
1226 return;
1227 }
1228 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1229 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1230 if (err) {
e849b424
JS
1231 if_printf(&sc->arpcom.ac_if, "open tx pipe failed: %s\n",
1232 usbd_errstr(err));
1550dfd9 1233 AUE_UNLOCK(sc);
984263bc
MD
1234 return;
1235 }
1236
1237#ifdef AUE_INTR_PIPE
1238 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1239 USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1240 sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1241 AUE_INTR_INTERVAL);
1242 if (err) {
e849b424
JS
1243 if_printf(&sc->arpcom.ac_if, "open intr pipe failed: %s\n",
1244 usbd_errstr(err));
1550dfd9 1245 AUE_UNLOCK(sc);
984263bc
MD
1246 return;
1247 }
1248#endif
1249
1250 /* Start up the receive pipe. */
1251 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1252 c = &sc->aue_cdata.aue_rx_chain[i];
1253 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1254 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ,
1255 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1256 usbd_transfer(c->aue_xfer);
1257 }
1258
1259 ifp->if_flags |= IFF_RUNNING;
1260 ifp->if_flags &= ~IFF_OACTIVE;
1261
7824277e 1262 callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
984263bc 1263
1550dfd9
MD
1264 AUE_UNLOCK(sc);
1265
984263bc
MD
1266 return;
1267}
1268
1269/*
1270 * Set media options.
1271 */
6ed427ca 1272static int
984263bc
MD
1273aue_ifmedia_upd(struct ifnet *ifp)
1274{
1550dfd9
MD
1275 struct aue_softc *sc = ifp->if_softc;
1276 struct mii_data *mii = GET_MII(sc);
984263bc 1277
984263bc
MD
1278 sc->aue_link = 0;
1279 if (mii->mii_instance) {
1280 struct mii_softc *miisc;
1550dfd9 1281 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
984263bc
MD
1282 mii_phy_reset(miisc);
1283 }
1284 mii_mediachg(mii);
1285
1550dfd9 1286 return (0);
984263bc
MD
1287}
1288
1289/*
1290 * Report current media status.
1291 */
6ed427ca 1292static void
984263bc
MD
1293aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1294{
1550dfd9
MD
1295 struct aue_softc *sc = ifp->if_softc;
1296 struct mii_data *mii = GET_MII(sc);
984263bc 1297
984263bc
MD
1298 mii_pollstat(mii);
1299 ifmr->ifm_active = mii->mii_media_active;
1300 ifmr->ifm_status = mii->mii_media_status;
1301
1302 return;
1303}
1304
6ed427ca 1305static int
bd4539cc 1306aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
984263bc
MD
1307{
1308 struct aue_softc *sc = ifp->if_softc;
1550dfd9 1309 struct ifreq *ifr = (struct ifreq *)data;
984263bc 1310 struct mii_data *mii;
1550dfd9 1311 int error = 0;
984263bc 1312
1550dfd9 1313 AUE_LOCK(sc);
984263bc
MD
1314
1315 switch(command) {
984263bc
MD
1316 case SIOCSIFFLAGS:
1317 if (ifp->if_flags & IFF_UP) {
1318 if (ifp->if_flags & IFF_RUNNING &&
1319 ifp->if_flags & IFF_PROMISC &&
1320 !(sc->aue_if_flags & IFF_PROMISC)) {
1321 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1322 } else if (ifp->if_flags & IFF_RUNNING &&
1323 !(ifp->if_flags & IFF_PROMISC) &&
1324 sc->aue_if_flags & IFF_PROMISC) {
1325 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1326 } else if (!(ifp->if_flags & IFF_RUNNING))
1327 aue_init(sc);
1328 } else {
1329 if (ifp->if_flags & IFF_RUNNING)
1330 aue_stop(sc);
1331 }
1332 sc->aue_if_flags = ifp->if_flags;
1333 error = 0;
1334 break;
1335 case SIOCADDMULTI:
1336 case SIOCDELMULTI:
1337 aue_setmulti(sc);
1338 error = 0;
1339 break;
1340 case SIOCGIFMEDIA:
1341 case SIOCSIFMEDIA:
1550dfd9 1342 mii = GET_MII(sc);
984263bc
MD
1343 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1344 break;
1345 default:
1550dfd9 1346 error = ether_ioctl(ifp, command, data);
984263bc
MD
1347 break;
1348 }
1349
1550dfd9 1350 AUE_UNLOCK(sc);
984263bc 1351
1550dfd9 1352 return (error);
984263bc
MD
1353}
1354
6ed427ca 1355static void
984263bc
MD
1356aue_watchdog(struct ifnet *ifp)
1357{
1550dfd9 1358 struct aue_softc *sc = ifp->if_softc;
984263bc
MD
1359 struct aue_chain *c;
1360 usbd_status stat;
1361
b464d724 1362 ASSERT_SERIALIZED(ifp->if_serializer);
984263bc
MD
1363
1364 ifp->if_oerrors++;
e849b424 1365 if_printf(ifp, "watchdog timeout\n");
984263bc
MD
1366
1367 c = &sc->aue_cdata.aue_tx_chain[0];
1368 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1369 aue_txeof(c->aue_xfer, c, stat);
984263bc
MD
1370}
1371
1372/*
1373 * Stop the adapter and free any mbufs allocated to the
1374 * RX and TX lists.
1375 */
6ed427ca 1376static void
984263bc
MD
1377aue_stop(struct aue_softc *sc)
1378{
1379 usbd_status err;
1380 struct ifnet *ifp;
1381 int i;
1382
1550dfd9 1383 AUE_LOCK(sc);
984263bc
MD
1384 ifp = &sc->arpcom.ac_if;
1385 ifp->if_timer = 0;
1386
1550dfd9
MD
1387 aue_csr_write_1(sc, AUE_CTL0, 0);
1388 aue_csr_write_1(sc, AUE_CTL1, 0);
984263bc 1389 aue_reset(sc);
7824277e 1390 callout_stop(&sc->aue_stat_timer);
984263bc
MD
1391
1392 /* Stop transfers. */
1393 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1394 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1395 if (err) {
e849b424
JS
1396 if_printf(ifp, "abort rx pipe failed: %s\n",
1397 usbd_errstr(err));
984263bc
MD
1398 }
1399 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1400 if (err) {
e849b424
JS
1401 if_printf(ifp, "close rx pipe failed: %s\n",
1402 usbd_errstr(err));
984263bc
MD
1403 }
1404 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1405 }
1406
1407 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1408 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1409 if (err) {
e849b424
JS
1410 if_printf(ifp, "abort tx pipe failed: %s\n",
1411 usbd_errstr(err));
984263bc
MD
1412 }
1413 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1414 if (err) {
e849b424
JS
1415 if_printf(ifp, "close tx pipe failed: %s\n",
1416 usbd_errstr(err));
984263bc
MD
1417 }
1418 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1419 }
1420
1421#ifdef AUE_INTR_PIPE
1422 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1423 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1424 if (err) {
e849b424
JS
1425 if_printf(ifp, "abort intr pipe failed: %s\n",
1426 usbd_errstr(err));
984263bc
MD
1427 }
1428 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1429 if (err) {
e849b424
JS
1430 if_printf(ifp, "close intr pipe failed: %s\n",
1431 usbd_errstr(err));
984263bc
MD
1432 }
1433 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1434 }
1435#endif
1436
1437 /* Free RX resources. */
1438 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1439 if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) {
efda3bd0 1440 kfree(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV);
984263bc
MD
1441 sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL;
1442 }
1443 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1444 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1445 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1446 }
1447 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1448 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1449 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1450 }
1451 }
1452
1453 /* Free TX resources. */
1454 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1455 if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) {
efda3bd0 1456 kfree(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV);
984263bc
MD
1457 sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL;
1458 }
1459 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1460 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1461 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1462 }
1463 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1464 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1465 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1466 }
1467 }
1468
1469#ifdef AUE_INTR_PIPE
775fcd6c 1470 if (sc->aue_cdata.aue_ibuf != NULL) {
efda3bd0 1471 kfree(sc->aue_cdata.aue_ibuf, M_USBDEV);
775fcd6c
SZ
1472 sc->aue_cdata.aue_ibuf = NULL;
1473 }
984263bc
MD
1474#endif
1475
1476 sc->aue_link = 0;
1477
1478 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1550dfd9 1479 AUE_UNLOCK(sc);
984263bc
MD
1480
1481 return;
1482}
1483
1484/*
1485 * Stop all chip I/O so that the kernel's probe routines don't
1486 * get confused by errant DMAs when rebooting.
1487 */
6ed427ca
HT
1488static void
1489aue_shutdown(device_t dev)
984263bc
MD
1490{
1491 struct aue_softc *sc;
b464d724 1492 struct ifnet *ifp;
984263bc
MD
1493
1494 sc = device_get_softc(dev);
1550dfd9 1495 sc->aue_dying++;
b464d724
SZ
1496
1497 ifp = &sc->arpcom.ac_if;
1498
1499 lwkt_serialize_enter(ifp->if_serializer);
984263bc
MD
1500 aue_reset(sc);
1501 aue_stop(sc);
b464d724 1502 lwkt_serialize_exit(ifp->if_serializer);
984263bc 1503}