2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
37 * Written by Bill Paul <wpaul@ctr.columbia.edu>
38 * Electrical Engineering Department
39 * Columbia University, New York City
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 * The Aironet 4500/4800 series cards come in PCMCIA, ISA and PCI form.
47 * This driver supports all three device types (PCI devices are supported
48 * through an extra PCI shim: /sys/dev/an/if_an_pci.c). ISA devices can be
49 * supported either using hard-coded IO port/IRQ settings or via Plug
50 * and Play. The 4500 series devices support 1Mbps and 2Mbps data rates.
51 * The 4800 devices support 1, 2, 5.5 and 11Mbps rates.
53 * Like the WaveLAN/IEEE cards, the Aironet NICs are all essentially
54 * PCMCIA devices. The ISA and PCI cards are a combination of a PCMCIA
55 * device and a PCMCIA to ISA or PCMCIA to PCI adapter card. There are
56 * a couple of important differences though:
58 * - Lucent ISA card looks to the host like a PCMCIA controller with
59 * a PCMCIA WaveLAN card inserted. This means that even desktop
60 * machines need to be configured with PCMCIA support in order to
61 * use WaveLAN/IEEE ISA cards. The Aironet cards on the other hand
62 * actually look like normal ISA and PCI devices to the host, so
63 * no PCMCIA controller support is needed
65 * The latter point results in a small gotcha. The Aironet PCMCIA
66 * cards can be configured for one of two operating modes depending
67 * on how the Vpp1 and Vpp2 programming voltages are set when the
68 * card is activated. In order to put the card in proper PCMCIA
69 * operation (where the CIS table is visible and the interface is
70 * programmed for PCMCIA operation), both Vpp1 and Vpp2 have to be
71 * set to 5 volts. FreeBSD by default doesn't set the Vpp voltages,
72 * which leaves the card in ISA/PCI mode, which prevents it from
73 * being activated as an PCMCIA device.
75 * Note that some PCMCIA controller software packages for Windows NT
76 * fail to set the voltages as well.
78 * The Aironet devices can operate in both station mode and access point
79 * mode. Typically, when programmed for station mode, the card can be set
80 * to automatically perform encapsulation/decapsulation of Ethernet II
81 * and 802.3 frames within 802.11 frames so that the host doesn't have
82 * to do it itself. This driver doesn't program the card that way: the
83 * driver handles all of the encapsulation/decapsulation itself.
89 #define ANCACHE /* enable signal strength cache */
92 #include <sys/param.h>
93 #include <sys/ctype.h>
94 #include <sys/systm.h>
95 #include <sys/sockio.h>
99 #include <sys/kernel.h>
100 #include <sys/socket.h>
102 #include <sys/syslog.h>
104 #include <sys/sysctl.h>
106 #include <sys/module.h>
108 #include <machine/bus.h>
109 #include <sys/rman.h>
110 #include <sys/lock.h>
111 #include <sys/mutex.h>
112 #include <machine/resource.h>
113 #include <sys/malloc.h>
116 #include <net/if_var.h>
117 #include <net/if_arp.h>
118 #include <net/if_dl.h>
119 #include <net/ethernet.h>
120 #include <net/if_types.h>
121 #include <net/if_media.h>
123 #include <net80211/ieee80211_var.h>
124 #include <net80211/ieee80211_ioctl.h>
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/in_var.h>
130 #include <netinet/ip.h>
135 #include <machine/md_var.h>
137 #include <dev/an/if_aironet_ieee.h>
138 #include <dev/an/if_anreg.h>
140 /* These are global because we need them in sys/pci/if_an_p.c. */
141 static void an_reset(struct an_softc *);
142 static int an_init_mpi350_desc(struct an_softc *);
143 static int an_ioctl(struct ifnet *, u_long, caddr_t);
144 static void an_init(void *);
145 static void an_init_locked(struct an_softc *);
146 static int an_init_tx_ring(struct an_softc *);
147 static void an_start(struct ifnet *);
148 static void an_start_locked(struct ifnet *);
149 static void an_watchdog(struct an_softc *);
150 static void an_rxeof(struct an_softc *);
151 static void an_txeof(struct an_softc *, int);
153 static void an_promisc(struct an_softc *, int);
154 static int an_cmd(struct an_softc *, int, int);
155 static int an_cmd_struct(struct an_softc *, struct an_command *,
157 static int an_read_record(struct an_softc *, struct an_ltv_gen *);
158 static int an_write_record(struct an_softc *, struct an_ltv_gen *);
159 static int an_read_data(struct an_softc *, int, int, caddr_t, int);
160 static int an_write_data(struct an_softc *, int, int, caddr_t, int);
161 static int an_seek(struct an_softc *, int, int, int);
162 static int an_alloc_nicmem(struct an_softc *, int, int *);
163 static int an_dma_malloc(struct an_softc *, bus_size_t, struct an_dma_alloc *,
165 static void an_dma_free(struct an_softc *, struct an_dma_alloc *);
166 static void an_dma_malloc_cb(void *, bus_dma_segment_t *, int, int);
167 static void an_stats_update(void *);
168 static void an_setdef(struct an_softc *, struct an_req *);
170 static void an_cache_store(struct an_softc *, struct ether_header *,
171 struct mbuf *, u_int8_t, u_int8_t);
174 /* function definitions for use with the Cisco's Linux configuration
178 static int readrids(struct ifnet*, struct aironet_ioctl*);
179 static int writerids(struct ifnet*, struct aironet_ioctl*);
180 static int flashcard(struct ifnet*, struct aironet_ioctl*);
182 static int cmdreset(struct ifnet *);
183 static int setflashmode(struct ifnet *);
184 static int flashgchar(struct ifnet *,int,int);
185 static int flashpchar(struct ifnet *,int,int);
186 static int flashputbuf(struct ifnet *);
187 static int flashrestart(struct ifnet *);
188 static int WaitBusy(struct ifnet *, int);
189 static int unstickbusy(struct ifnet *);
191 static void an_dump_record (struct an_softc *,struct an_ltv_gen *,
194 static int an_media_change (struct ifnet *);
195 static void an_media_status (struct ifnet *, struct ifmediareq *);
197 static int an_dump = 0;
198 static int an_cache_mode = 0;
204 static char an_conf[256];
205 static char an_conf_cache[256];
209 static SYSCTL_NODE(_hw, OID_AUTO, an, CTLFLAG_RD, 0,
210 "Wireless driver parameters");
212 /* XXX violate ethernet/netgraph callback hooks */
213 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
214 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
217 sysctl_an_dump(SYSCTL_HANDLER_ARGS)
226 strcpy(an_conf, "off");
229 strcpy(an_conf, "type");
232 strcpy(an_conf, "dump");
235 snprintf(an_conf, 5, "%x", an_dump);
239 error = sysctl_handle_string(oidp, an_conf, sizeof(an_conf), req);
241 if (strncmp(an_conf,"off", 3) == 0) {
244 if (strncmp(an_conf,"dump", 4) == 0) {
247 if (strncmp(an_conf,"type", 4) == 0) {
253 if ((*s >= '0') && (*s <= '9')) {
254 r = r * 16 + (*s - '0');
255 } else if ((*s >= 'a') && (*s <= 'f')) {
256 r = r * 16 + (*s - 'a' + 10);
264 printf("Sysctl changed for Aironet driver\n");
269 SYSCTL_PROC(_hw_an, OID_AUTO, an_dump, CTLTYPE_STRING | CTLFLAG_RW,
270 0, sizeof(an_conf), sysctl_an_dump, "A", "");
273 sysctl_an_cache_mode(SYSCTL_HANDLER_ARGS)
277 switch (an_cache_mode) {
279 strcpy(an_conf_cache, "per");
282 strcpy(an_conf_cache, "raw");
285 strcpy(an_conf_cache, "dbm");
289 error = sysctl_handle_string(oidp, an_conf_cache,
290 sizeof(an_conf_cache), req);
292 if (strncmp(an_conf_cache,"dbm", 3) == 0) {
295 if (strncmp(an_conf_cache,"per", 3) == 0) {
298 if (strncmp(an_conf_cache,"raw", 3) == 0) {
305 SYSCTL_PROC(_hw_an, OID_AUTO, an_cache_mode, CTLTYPE_STRING | CTLFLAG_RW,
306 0, sizeof(an_conf_cache), sysctl_an_cache_mode, "A", "");
309 * We probe for an Aironet 4500/4800 card by attempting to
310 * read the default SSID list. On reset, the first entry in
311 * the SSID list will contain the name "tsunami." If we don't
312 * find this, then there's no card present.
315 an_probe(device_t dev)
317 struct an_softc *sc = device_get_softc(dev);
318 struct an_ltv_ssidlist_new ssid;
321 bzero((char *)&ssid, sizeof(ssid));
323 error = an_alloc_port(dev, 0, AN_IOSIZ);
327 /* can't do autoprobing */
328 if (rman_get_start(sc->port_res) == -1)
332 * We need to fake up a softc structure long enough
333 * to be able to issue commands and call some of the
336 ssid.an_len = sizeof(ssid);
337 ssid.an_type = AN_RID_SSIDLIST;
339 /* Make sure interrupts are disabled. */
341 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
342 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), 0xFFFF);
345 mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
350 if (an_cmd(sc, AN_CMD_READCFG, 0)) {
355 if (an_read_record(sc, (struct an_ltv_gen *)&ssid)) {
360 /* See if the ssid matches what we expect ... but doesn't have to */
361 if (strcmp(ssid.an_entry[0].an_ssid, AN_DEF_SSID)) {
369 mtx_destroy(&sc->an_mtx);
374 * Allocate a port resource with the given resource id.
377 an_alloc_port(device_t dev, int rid, int size)
379 struct an_softc *sc = device_get_softc(dev);
380 struct resource *res;
382 res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
394 * Allocate a memory resource with the given resource id.
396 int an_alloc_memory(device_t dev, int rid, int size)
398 struct an_softc *sc = device_get_softc(dev);
399 struct resource *res;
401 res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY, &rid,
414 * Allocate a auxiliary memory resource with the given resource id.
416 int an_alloc_aux_memory(device_t dev, int rid, int size)
418 struct an_softc *sc = device_get_softc(dev);
419 struct resource *res;
421 res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY, &rid,
424 sc->mem_aux_rid = rid;
425 sc->mem_aux_res = res;
426 sc->mem_aux_used = size;
434 * Allocate an irq resource with the given resource id.
437 an_alloc_irq(device_t dev, int rid, int flags)
439 struct an_softc *sc = device_get_softc(dev);
440 struct resource *res;
442 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
443 (RF_ACTIVE | flags));
454 an_dma_malloc_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
456 bus_addr_t *paddr = (bus_addr_t*) arg;
457 *paddr = segs->ds_addr;
461 * Alloc DMA memory and set the pointer to it
464 an_dma_malloc(struct an_softc *sc, bus_size_t size, struct an_dma_alloc *dma,
469 r = bus_dmamem_alloc(sc->an_dtag, (void**) &dma->an_dma_vaddr,
470 BUS_DMA_NOWAIT, &dma->an_dma_map);
474 r = bus_dmamap_load(sc->an_dtag, dma->an_dma_map, dma->an_dma_vaddr,
478 mapflags | BUS_DMA_NOWAIT);
482 dma->an_dma_size = size;
486 bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
488 bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
493 an_dma_free(struct an_softc *sc, struct an_dma_alloc *dma)
495 bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
496 bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
497 dma->an_dma_vaddr = 0;
501 * Release all resources
504 an_release_resources(device_t dev)
506 struct an_softc *sc = device_get_softc(dev);
510 bus_release_resource(dev, SYS_RES_IOPORT,
511 sc->port_rid, sc->port_res);
515 bus_release_resource(dev, SYS_RES_MEMORY,
516 sc->mem_rid, sc->mem_res);
519 if (sc->mem_aux_res) {
520 bus_release_resource(dev, SYS_RES_MEMORY,
521 sc->mem_aux_rid, sc->mem_aux_res);
525 bus_release_resource(dev, SYS_RES_IRQ,
526 sc->irq_rid, sc->irq_res);
529 if (sc->an_rid_buffer.an_dma_paddr) {
530 an_dma_free(sc, &sc->an_rid_buffer);
532 for (i = 0; i < AN_MAX_RX_DESC; i++)
533 if (sc->an_rx_buffer[i].an_dma_paddr) {
534 an_dma_free(sc, &sc->an_rx_buffer[i]);
536 for (i = 0; i < AN_MAX_TX_DESC; i++)
537 if (sc->an_tx_buffer[i].an_dma_paddr) {
538 an_dma_free(sc, &sc->an_tx_buffer[i]);
541 bus_dma_tag_destroy(sc->an_dtag);
547 an_init_mpi350_desc(struct an_softc *sc)
549 struct an_command cmd_struct;
550 struct an_reply reply;
551 struct an_card_rid_desc an_rid_desc;
552 struct an_card_rx_desc an_rx_desc;
553 struct an_card_tx_desc an_tx_desc;
557 if(!sc->an_rid_buffer.an_dma_paddr)
558 an_dma_malloc(sc, AN_RID_BUFFER_SIZE,
559 &sc->an_rid_buffer, 0);
560 for (i = 0; i < AN_MAX_RX_DESC; i++)
561 if(!sc->an_rx_buffer[i].an_dma_paddr)
562 an_dma_malloc(sc, AN_RX_BUFFER_SIZE,
563 &sc->an_rx_buffer[i], 0);
564 for (i = 0; i < AN_MAX_TX_DESC; i++)
565 if(!sc->an_tx_buffer[i].an_dma_paddr)
566 an_dma_malloc(sc, AN_TX_BUFFER_SIZE,
567 &sc->an_tx_buffer[i], 0);
570 * Allocate RX descriptor
572 bzero(&reply,sizeof(reply));
573 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
574 cmd_struct.an_parm0 = AN_DESCRIPTOR_RX;
575 cmd_struct.an_parm1 = AN_RX_DESC_OFFSET;
576 cmd_struct.an_parm2 = AN_MAX_RX_DESC;
577 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
578 if_printf(sc->an_ifp, "failed to allocate RX descriptor\n");
582 for (desc = 0; desc < AN_MAX_RX_DESC; desc++) {
583 bzero(&an_rx_desc, sizeof(an_rx_desc));
584 an_rx_desc.an_valid = 1;
585 an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
586 an_rx_desc.an_done = 0;
587 an_rx_desc.an_phys = sc->an_rx_buffer[desc].an_dma_paddr;
589 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
590 CSR_MEM_AUX_WRITE_4(sc, AN_RX_DESC_OFFSET
591 + (desc * sizeof(an_rx_desc))
593 ((u_int32_t *)(void *)&an_rx_desc)[i]);
597 * Allocate TX descriptor
600 bzero(&reply,sizeof(reply));
601 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
602 cmd_struct.an_parm0 = AN_DESCRIPTOR_TX;
603 cmd_struct.an_parm1 = AN_TX_DESC_OFFSET;
604 cmd_struct.an_parm2 = AN_MAX_TX_DESC;
605 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
606 if_printf(sc->an_ifp, "failed to allocate TX descriptor\n");
610 for (desc = 0; desc < AN_MAX_TX_DESC; desc++) {
611 bzero(&an_tx_desc, sizeof(an_tx_desc));
612 an_tx_desc.an_offset = 0;
613 an_tx_desc.an_eoc = 0;
614 an_tx_desc.an_valid = 0;
615 an_tx_desc.an_len = 0;
616 an_tx_desc.an_phys = sc->an_tx_buffer[desc].an_dma_paddr;
618 for (i = 0; i < sizeof(an_tx_desc) / 4; i++)
619 CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
620 + (desc * sizeof(an_tx_desc))
622 ((u_int32_t *)(void *)&an_tx_desc)[i]);
626 * Allocate RID descriptor
629 bzero(&reply,sizeof(reply));
630 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
631 cmd_struct.an_parm0 = AN_DESCRIPTOR_HOSTRW;
632 cmd_struct.an_parm1 = AN_HOST_DESC_OFFSET;
633 cmd_struct.an_parm2 = 1;
634 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
635 if_printf(sc->an_ifp, "failed to allocate host descriptor\n");
639 bzero(&an_rid_desc, sizeof(an_rid_desc));
640 an_rid_desc.an_valid = 1;
641 an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
642 an_rid_desc.an_rid = 0;
643 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
645 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
646 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
647 ((u_int32_t *)(void *)&an_rid_desc)[i]);
653 an_attach(struct an_softc *sc, int flags)
660 ifp = sc->an_ifp = if_alloc(IFT_ETHER);
662 device_printf(sc->an_dev, "can not if_alloc()\n");
666 if_initname(ifp, device_get_name(sc->an_dev),
667 device_get_unit(sc->an_dev));
670 sc->an_associated = 0;
672 sc->an_was_monitor = 0;
673 sc->an_flash_buffer = NULL;
679 error = an_init_mpi350_desc(sc);
684 /* Load factory config */
685 if (an_cmd(sc, AN_CMD_READCFG, 0)) {
686 device_printf(sc->an_dev, "failed to load config data\n");
690 /* Read the current configuration */
691 sc->an_config.an_type = AN_RID_GENCONFIG;
692 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
693 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
694 device_printf(sc->an_dev, "read record failed\n");
698 /* Read the card capabilities */
699 sc->an_caps.an_type = AN_RID_CAPABILITIES;
700 sc->an_caps.an_len = sizeof(struct an_ltv_caps);
701 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_caps)) {
702 device_printf(sc->an_dev, "read record failed\n");
707 sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
708 sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
709 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
710 device_printf(sc->an_dev, "read record failed\n");
715 sc->an_aplist.an_type = AN_RID_APLIST;
716 sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
717 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
718 device_printf(sc->an_dev, "read record failed\n");
723 /* Read the RSSI <-> dBm map */
724 sc->an_have_rssimap = 0;
725 if (sc->an_caps.an_softcaps & 8) {
726 sc->an_rssimap.an_type = AN_RID_RSSI_MAP;
727 sc->an_rssimap.an_len = sizeof(struct an_ltv_rssi_map);
728 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_rssimap)) {
729 device_printf(sc->an_dev,
730 "unable to get RSSI <-> dBM map\n");
732 device_printf(sc->an_dev, "got RSSI <-> dBM map\n");
733 sc->an_have_rssimap = 1;
736 device_printf(sc->an_dev, "no RSSI <-> dBM map\n");
741 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
742 ifp->if_ioctl = an_ioctl;
743 ifp->if_start = an_start;
744 ifp->if_init = an_init;
745 ifp->if_baudrate = 10000000;
746 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
747 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
748 IFQ_SET_READY(&ifp->if_snd);
750 bzero(sc->an_config.an_nodename, sizeof(sc->an_config.an_nodename));
751 bcopy(AN_DEFAULT_NODENAME, sc->an_config.an_nodename,
752 sizeof(AN_DEFAULT_NODENAME) - 1);
754 bzero(sc->an_ssidlist.an_entry[0].an_ssid,
755 sizeof(sc->an_ssidlist.an_entry[0].an_ssid));
756 bcopy(AN_DEFAULT_NETNAME, sc->an_ssidlist.an_entry[0].an_ssid,
757 sizeof(AN_DEFAULT_NETNAME) - 1);
758 sc->an_ssidlist.an_entry[0].an_len = strlen(AN_DEFAULT_NETNAME);
760 sc->an_config.an_opmode =
761 AN_OPMODE_INFRASTRUCTURE_STATION;
764 bzero((char *)&sc->an_stats, sizeof(sc->an_stats));
768 ifmedia_init(&sc->an_ifmedia, 0, an_media_change, an_media_status);
769 if_printf(ifp, "supported rates: ");
770 #define ADD(s, o) ifmedia_add(&sc->an_ifmedia, \
771 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
773 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
774 for (i = 0; i < nrate; i++) {
775 r = sc->an_caps.an_rates[i];
776 mword = ieee80211_rate2media(NULL, r, IEEE80211_MODE_AUTO);
779 printf("%s%d%sMbps", (i != 0 ? " " : ""),
780 (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
782 ADD(mword, IFM_IEEE80211_ADHOC);
785 ifmedia_set(&sc->an_ifmedia, IFM_MAKEWORD(IFM_IEEE80211,
790 * Call MI attach routine.
793 ether_ifattach(ifp, sc->an_caps.an_oemaddr);
794 callout_init_mtx(&sc->an_stat_ch, &sc->an_mtx, 0);
799 mtx_destroy(&sc->an_mtx);
806 an_detach(device_t dev)
808 struct an_softc *sc = device_get_softc(dev);
809 struct ifnet *ifp = sc->an_ifp;
812 device_printf(dev,"already unloaded\n");
818 ifmedia_removeall(&sc->an_ifmedia);
819 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
822 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
823 callout_drain(&sc->an_stat_ch);
825 an_release_resources(dev);
826 mtx_destroy(&sc->an_mtx);
831 an_rxeof(struct an_softc *sc)
834 struct ether_header *eh;
835 struct ieee80211_frame *ih;
836 struct an_rxframe rx_frame;
837 struct an_rxframe_802_3 rx_frame_802_3;
839 int len, id, error = 0, i, count = 0;
840 int ieee80211_header_len;
843 struct an_card_rx_desc an_rx_desc;
851 id = CSR_READ_2(sc, AN_RX_FID);
853 if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
854 /* read raw 802.11 packet */
855 bpf_buf = sc->buf_802_11;
858 if (an_read_data(sc, id, 0x0, (caddr_t)&rx_frame,
860 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
865 * skip beacon by default since this increases the
869 if (!(sc->an_monitor & AN_MONITOR_INCLUDE_BEACON) &&
870 (rx_frame.an_frame_ctl &
871 IEEE80211_FC0_SUBTYPE_BEACON)) {
875 if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
876 len = rx_frame.an_rx_payload_len
878 /* Check for insane frame length */
879 if (len > sizeof(sc->buf_802_11)) {
880 if_printf(ifp, "oversized packet "
881 "received (%d, %d)\n",
883 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
887 bcopy((char *)&rx_frame,
888 bpf_buf, sizeof(rx_frame));
890 error = an_read_data(sc, id, sizeof(rx_frame),
891 (caddr_t)bpf_buf+sizeof(rx_frame),
892 rx_frame.an_rx_payload_len);
894 fc1=rx_frame.an_frame_ctl >> 8;
895 ieee80211_header_len =
896 sizeof(struct ieee80211_frame);
897 if ((fc1 & IEEE80211_FC1_DIR_TODS) &&
898 (fc1 & IEEE80211_FC1_DIR_FROMDS)) {
899 ieee80211_header_len += ETHER_ADDR_LEN;
902 len = rx_frame.an_rx_payload_len
903 + ieee80211_header_len;
904 /* Check for insane frame length */
905 if (len > sizeof(sc->buf_802_11)) {
906 if_printf(ifp, "oversized packet "
907 "received (%d, %d)\n",
909 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
913 ih = (struct ieee80211_frame *)bpf_buf;
915 bcopy((char *)&rx_frame.an_frame_ctl,
916 (char *)ih, ieee80211_header_len);
918 error = an_read_data(sc, id, sizeof(rx_frame) +
920 (caddr_t)ih +ieee80211_header_len,
921 rx_frame.an_rx_payload_len);
923 /* dump raw 802.11 packet to bpf and skip ip stack */
924 BPF_TAP(ifp, bpf_buf, len);
926 MGETHDR(m, M_NOWAIT, MT_DATA);
928 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
931 if (!(MCLGET(m, M_NOWAIT))) {
933 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
936 m->m_pkthdr.rcvif = ifp;
937 /* Read Ethernet encapsulated packet */
940 /* Read NIC frame header */
941 if (an_read_data(sc, id, 0, (caddr_t)&rx_frame,
944 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
948 /* Read in the 802_3 frame header */
949 if (an_read_data(sc, id, 0x34,
950 (caddr_t)&rx_frame_802_3,
951 sizeof(rx_frame_802_3))) {
953 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
956 if (rx_frame_802_3.an_rx_802_3_status != 0) {
958 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
961 /* Check for insane frame length */
962 len = rx_frame_802_3.an_rx_802_3_payload_len;
963 if (len > sizeof(sc->buf_802_11)) {
965 if_printf(ifp, "oversized packet "
966 "received (%d, %d)\n",
968 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
971 m->m_pkthdr.len = m->m_len =
972 rx_frame_802_3.an_rx_802_3_payload_len + 12;
974 eh = mtod(m, struct ether_header *);
976 bcopy((char *)&rx_frame_802_3.an_rx_dst_addr,
977 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
978 bcopy((char *)&rx_frame_802_3.an_rx_src_addr,
979 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
981 /* in mbuf header type is just before payload */
982 error = an_read_data(sc, id, 0x44,
983 (caddr_t)&(eh->ether_type),
984 rx_frame_802_3.an_rx_802_3_payload_len);
988 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
991 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
993 /* Receive packet. */
995 an_cache_store(sc, eh, m,
996 rx_frame.an_rx_signal_strength,
1000 (*ifp->if_input)(ifp, m);
1004 } else { /* MPI-350 */
1005 for (count = 0; count < AN_MAX_RX_DESC; count++){
1006 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1007 ((u_int32_t *)(void *)&an_rx_desc)[i]
1008 = CSR_MEM_AUX_READ_4(sc,
1010 + (count * sizeof(an_rx_desc))
1013 if (an_rx_desc.an_done && !an_rx_desc.an_valid) {
1014 buf = sc->an_rx_buffer[count].an_dma_vaddr;
1016 MGETHDR(m, M_NOWAIT, MT_DATA);
1018 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1021 if (!(MCLGET(m, M_NOWAIT))) {
1023 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1026 m->m_pkthdr.rcvif = ifp;
1027 /* Read Ethernet encapsulated packet */
1030 * No ANCACHE support since we just get back
1031 * an Ethernet packet no 802.11 info
1035 /* Read NIC frame header */
1036 bcopy(buf, (caddr_t)&rx_frame,
1040 /* Check for insane frame length */
1041 len = an_rx_desc.an_len + 12;
1042 if (len > MCLBYTES) {
1044 if_printf(ifp, "oversized packet "
1045 "received (%d, %d)\n",
1047 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1051 m->m_pkthdr.len = m->m_len =
1052 an_rx_desc.an_len + 12;
1054 eh = mtod(m, struct ether_header *);
1056 bcopy(buf, (char *)eh,
1059 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1061 /* Receive packet. */
1064 an_cache_store(sc, eh, m,
1065 rx_frame.an_rx_signal_strength,
1070 (*ifp->if_input)(ifp, m);
1073 an_rx_desc.an_valid = 1;
1074 an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
1075 an_rx_desc.an_done = 0;
1076 an_rx_desc.an_phys =
1077 sc->an_rx_buffer[count].an_dma_paddr;
1079 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1080 CSR_MEM_AUX_WRITE_4(sc,
1082 + (count * sizeof(an_rx_desc))
1084 ((u_int32_t *)(void *)&an_rx_desc)[i]);
1087 if_printf(ifp, "Didn't get valid RX packet "
1090 an_rx_desc.an_valid, an_rx_desc.an_len);
1097 an_txeof(struct an_softc *sc, int status)
1106 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1109 id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1111 if (status & AN_EV_TX_EXC) {
1112 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1114 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1116 for (i = 0; i < AN_TX_RING_CNT; i++) {
1117 if (id == sc->an_rdata.an_tx_ring[i]) {
1118 sc->an_rdata.an_tx_ring[i] = 0;
1123 AN_INC(sc->an_rdata.an_tx_cons, AN_TX_RING_CNT);
1124 } else { /* MPI 350 */
1125 id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1126 if (!sc->an_rdata.an_tx_empty){
1127 if (status & AN_EV_TX_EXC) {
1128 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1130 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1131 AN_INC(sc->an_rdata.an_tx_cons, AN_MAX_TX_DESC);
1132 if (sc->an_rdata.an_tx_prod ==
1133 sc->an_rdata.an_tx_cons)
1134 sc->an_rdata.an_tx_empty = 1;
1142 * We abuse the stats updater to check the current NIC status. This
1143 * is important because we don't want to allow transmissions until
1144 * the NIC has synchronized to the current cell (either as the master
1145 * in an ad-hoc group, or as a station connected to an access point).
1147 * Note that this function will be called via callout(9) with a lock held.
1150 an_stats_update(void *xsc)
1152 struct an_softc *sc;
1158 if (sc->an_timer > 0 && --sc->an_timer == 0)
1161 sc->an_status.an_type = AN_RID_STATUS;
1162 sc->an_status.an_len = sizeof(struct an_ltv_status);
1163 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_status))
1166 if (sc->an_status.an_opmode & AN_STATUS_OPMODE_IN_SYNC)
1167 sc->an_associated = 1;
1169 sc->an_associated = 0;
1171 /* Don't do this while we're transmitting */
1172 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1173 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1177 sc->an_stats.an_len = sizeof(struct an_ltv_stats);
1178 sc->an_stats.an_type = AN_RID_32BITS_CUM;
1179 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_stats.an_len))
1182 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1190 struct an_softc *sc;
1194 sc = (struct an_softc*)xsc;
1205 /* Disable interrupts. */
1206 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
1208 status = CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350));
1209 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), ~AN_INTRS(sc->mpi350));
1211 if (status & AN_EV_MIC) {
1212 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_MIC);
1215 if (status & AN_EV_LINKSTAT) {
1216 if (CSR_READ_2(sc, AN_LINKSTAT(sc->mpi350))
1217 == AN_LINKSTAT_ASSOCIATED)
1218 sc->an_associated = 1;
1220 sc->an_associated = 0;
1221 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_LINKSTAT);
1224 if (status & AN_EV_RX) {
1226 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_RX);
1229 if (sc->mpi350 && status & AN_EV_TX_CPY) {
1230 an_txeof(sc, status);
1231 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_CPY);
1234 if (status & AN_EV_TX) {
1235 an_txeof(sc, status);
1236 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX);
1239 if (status & AN_EV_TX_EXC) {
1240 an_txeof(sc, status);
1241 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_EXC);
1244 if (status & AN_EV_ALLOC)
1245 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1247 /* Re-enable interrupts. */
1248 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
1250 if ((ifp->if_flags & IFF_UP) && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1251 an_start_locked(ifp);
1260 an_cmd_struct(struct an_softc *sc, struct an_command *cmd,
1261 struct an_reply *reply)
1266 for (i = 0; i != AN_TIMEOUT; i++) {
1267 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
1273 if( i == AN_TIMEOUT) {
1278 CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), cmd->an_parm0);
1279 CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), cmd->an_parm1);
1280 CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), cmd->an_parm2);
1281 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd->an_cmd);
1283 for (i = 0; i < AN_TIMEOUT; i++) {
1284 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1289 reply->an_resp0 = CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1290 reply->an_resp1 = CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1291 reply->an_resp2 = CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1292 reply->an_status = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1294 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1295 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
1296 AN_EV_CLR_STUCK_BUSY);
1298 /* Ack the command */
1299 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1301 if (i == AN_TIMEOUT)
1308 an_cmd(struct an_softc *sc, int cmd, int val)
1313 CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), val);
1314 CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), 0);
1315 CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), 0);
1316 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1318 for (i = 0; i < AN_TIMEOUT; i++) {
1319 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1322 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) == cmd)
1323 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1327 for (i = 0; i < AN_TIMEOUT; i++) {
1328 CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1329 CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1330 CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1331 s = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1332 if ((s & AN_STAT_CMD_CODE) == (cmd & AN_STAT_CMD_CODE))
1336 /* Ack the command */
1337 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1339 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1340 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CLR_STUCK_BUSY);
1342 if (i == AN_TIMEOUT)
1349 * This reset sequence may look a little strange, but this is the
1350 * most reliable method I've found to really kick the NIC in the
1351 * head and force it to reboot correctly.
1354 an_reset(struct an_softc *sc)
1360 an_cmd(sc, AN_CMD_ENABLE, 0);
1361 an_cmd(sc, AN_CMD_FW_RESTART, 0);
1362 an_cmd(sc, AN_CMD_NOOP2, 0);
1364 if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT)
1365 device_printf(sc->an_dev, "reset failed\n");
1367 an_cmd(sc, AN_CMD_DISABLE, 0);
1373 * Read an LTV record from the NIC.
1376 an_read_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1378 struct an_ltv_gen *an_ltv;
1379 struct an_card_rid_desc an_rid_desc;
1380 struct an_command cmd;
1381 struct an_reply reply;
1388 if (ltv->an_len < 4 || ltv->an_type == 0)
1393 /* Tell the NIC to enter record read mode. */
1394 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type)) {
1395 if_printf(ifp, "RID access failed\n");
1399 /* Seek to the record. */
1400 if (an_seek(sc, ltv->an_type, 0, AN_BAP1)) {
1401 if_printf(ifp, "seek to record failed\n");
1406 * Read the length and record type and make sure they
1407 * match what we expect (this verifies that we have enough
1408 * room to hold all of the returned data).
1409 * Length includes type but not length.
1411 len = CSR_READ_2(sc, AN_DATA1);
1412 if (len > (ltv->an_len - 2)) {
1413 if_printf(ifp, "record length mismatch -- expected %d, "
1414 "got %d for Rid %x\n",
1415 ltv->an_len - 2, len, ltv->an_type);
1416 len = ltv->an_len - 2;
1418 ltv->an_len = len + 2;
1421 /* Now read the data. */
1422 len -= 2; /* skip the type */
1424 for (i = len; i > 1; i -= 2)
1425 *ptr++ = CSR_READ_2(sc, AN_DATA1);
1427 ptr2 = (u_int8_t *)ptr;
1428 *ptr2 = CSR_READ_1(sc, AN_DATA1);
1430 } else { /* MPI-350 */
1431 if (!sc->an_rid_buffer.an_dma_vaddr)
1433 an_rid_desc.an_valid = 1;
1434 an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
1435 an_rid_desc.an_rid = 0;
1436 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1437 bzero(sc->an_rid_buffer.an_dma_vaddr, AN_RID_BUFFER_SIZE);
1439 bzero(&cmd, sizeof(cmd));
1440 bzero(&reply, sizeof(reply));
1441 cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_READ;
1442 cmd.an_parm0 = ltv->an_type;
1444 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1445 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1446 ((u_int32_t *)(void *)&an_rid_desc)[i]);
1448 if (an_cmd_struct(sc, &cmd, &reply)
1449 || reply.an_status & AN_CMD_QUAL_MASK) {
1450 if_printf(ifp, "failed to read RID %x %x %x %x %x, %d\n",
1460 an_ltv = (struct an_ltv_gen *)sc->an_rid_buffer.an_dma_vaddr;
1461 if (an_ltv->an_len + 2 < an_rid_desc.an_len) {
1462 an_rid_desc.an_len = an_ltv->an_len;
1465 len = an_rid_desc.an_len;
1466 if (len > (ltv->an_len - 2)) {
1467 if_printf(ifp, "record length mismatch -- expected %d, "
1468 "got %d for Rid %x\n",
1469 ltv->an_len - 2, len, ltv->an_type);
1470 len = ltv->an_len - 2;
1472 ltv->an_len = len + 2;
1474 bcopy(&an_ltv->an_type,
1480 an_dump_record(sc, ltv, "Read");
1486 * Same as read, except we inject data instead of reading it.
1489 an_write_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1491 struct an_card_rid_desc an_rid_desc;
1492 struct an_command cmd;
1493 struct an_reply reply;
1500 an_dump_record(sc, ltv, "Write");
1503 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type))
1506 if (an_seek(sc, ltv->an_type, 0, AN_BAP1))
1510 * Length includes type but not length.
1512 len = ltv->an_len - 2;
1513 CSR_WRITE_2(sc, AN_DATA1, len);
1515 len -= 2; /* skip the type */
1517 for (i = len; i > 1; i -= 2)
1518 CSR_WRITE_2(sc, AN_DATA1, *ptr++);
1520 ptr2 = (u_int8_t *)ptr;
1521 CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1524 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_WRITE, ltv->an_type))
1529 for (i = 0; i != AN_TIMEOUT; i++) {
1530 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350))
1536 if (i == AN_TIMEOUT) {
1540 an_rid_desc.an_valid = 1;
1541 an_rid_desc.an_len = ltv->an_len - 2;
1542 an_rid_desc.an_rid = ltv->an_type;
1543 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1545 bcopy(<v->an_type, sc->an_rid_buffer.an_dma_vaddr,
1546 an_rid_desc.an_len);
1548 bzero(&cmd,sizeof(cmd));
1549 bzero(&reply,sizeof(reply));
1550 cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_WRITE;
1551 cmd.an_parm0 = ltv->an_type;
1553 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1554 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1555 ((u_int32_t *)(void *)&an_rid_desc)[i]);
1559 if ((i = an_cmd_struct(sc, &cmd, &reply))) {
1560 if_printf(sc->an_ifp,
1561 "failed to write RID 1 %x %x %x %x %x, %d\n",
1572 if (reply.an_status & AN_CMD_QUAL_MASK) {
1573 if_printf(sc->an_ifp,
1574 "failed to write RID 2 %x %x %x %x %x, %d\n",
1590 an_dump_record(struct an_softc *sc, struct an_ltv_gen *ltv, char *string)
1598 len = ltv->an_len - 4;
1599 if_printf(sc->an_ifp, "RID %4x, Length %4d, Mode %s\n",
1600 ltv->an_type, ltv->an_len - 4, string);
1602 if (an_dump == 1 || (an_dump == ltv->an_type)) {
1603 if_printf(sc->an_ifp, "\t");
1604 bzero(buf,sizeof(buf));
1606 ptr2 = (u_int8_t *)<v->an_val;
1607 for (i = len; i > 0; i--) {
1608 printf("%02x ", *ptr2);
1615 if (++count == 16) {
1618 if_printf(sc->an_ifp, "\t");
1619 bzero(buf,sizeof(buf));
1622 for (; count != 16; count++) {
1625 printf(" %s\n",buf);
1630 an_seek(struct an_softc *sc, int id, int off, int chan)
1645 if_printf(sc->an_ifp, "invalid data path: %x\n", chan);
1649 CSR_WRITE_2(sc, selreg, id);
1650 CSR_WRITE_2(sc, offreg, off);
1652 for (i = 0; i < AN_TIMEOUT; i++) {
1653 if (!(CSR_READ_2(sc, offreg) & (AN_OFF_BUSY|AN_OFF_ERR)))
1657 if (i == AN_TIMEOUT)
1664 an_read_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1671 if (an_seek(sc, id, off, AN_BAP1))
1675 ptr = (u_int16_t *)buf;
1676 for (i = len; i > 1; i -= 2)
1677 *ptr++ = CSR_READ_2(sc, AN_DATA1);
1679 ptr2 = (u_int8_t *)ptr;
1680 *ptr2 = CSR_READ_1(sc, AN_DATA1);
1687 an_write_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1694 if (an_seek(sc, id, off, AN_BAP0))
1698 ptr = (u_int16_t *)buf;
1699 for (i = len; i > 1; i -= 2)
1700 CSR_WRITE_2(sc, AN_DATA0, *ptr++);
1702 ptr2 = (u_int8_t *)ptr;
1703 CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1710 * Allocate a region of memory inside the NIC and zero
1714 an_alloc_nicmem(struct an_softc *sc, int len, int *id)
1718 if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1719 if_printf(sc->an_ifp, "failed to allocate %d bytes on NIC\n",
1724 for (i = 0; i < AN_TIMEOUT; i++) {
1725 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_ALLOC)
1729 if (i == AN_TIMEOUT)
1732 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1733 *id = CSR_READ_2(sc, AN_ALLOC_FID);
1735 if (an_seek(sc, *id, 0, AN_BAP0))
1738 for (i = 0; i < len / 2; i++)
1739 CSR_WRITE_2(sc, AN_DATA0, 0);
1745 an_setdef(struct an_softc *sc, struct an_req *areq)
1748 struct an_ltv_genconfig *cfg;
1749 struct an_ltv_ssidlist_new *ssid;
1750 struct an_ltv_aplist *ap;
1751 struct an_ltv_gen *sp;
1756 switch (areq->an_type) {
1757 case AN_RID_GENCONFIG:
1758 cfg = (struct an_ltv_genconfig *)areq;
1760 bcopy((char *)&cfg->an_macaddr, IF_LLADDR(sc->an_ifp),
1763 bcopy((char *)cfg, (char *)&sc->an_config,
1764 sizeof(struct an_ltv_genconfig));
1766 case AN_RID_SSIDLIST:
1767 ssid = (struct an_ltv_ssidlist_new *)areq;
1768 bcopy((char *)ssid, (char *)&sc->an_ssidlist,
1769 sizeof(struct an_ltv_ssidlist_new));
1772 ap = (struct an_ltv_aplist *)areq;
1773 bcopy((char *)ap, (char *)&sc->an_aplist,
1774 sizeof(struct an_ltv_aplist));
1776 case AN_RID_TX_SPEED:
1777 sp = (struct an_ltv_gen *)areq;
1778 sc->an_tx_rate = sp->an_val;
1780 /* Read the current configuration */
1781 sc->an_config.an_type = AN_RID_GENCONFIG;
1782 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1783 an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
1784 cfg = &sc->an_config;
1786 /* clear other rates and set the only one we want */
1787 bzero(cfg->an_rates, sizeof(cfg->an_rates));
1788 cfg->an_rates[0] = sc->an_tx_rate;
1790 /* Save the new rate */
1791 sc->an_config.an_type = AN_RID_GENCONFIG;
1792 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1794 case AN_RID_WEP_TEMP:
1795 /* Cache the temp keys */
1797 &sc->an_temp_keys[((struct an_ltv_key *)areq)->kindex],
1798 sizeof(struct an_ltv_key));
1799 case AN_RID_WEP_PERM:
1800 case AN_RID_LEAPUSERNAME:
1801 case AN_RID_LEAPPASSWORD:
1804 /* Disable the MAC. */
1805 an_cmd(sc, AN_CMD_DISABLE, 0);
1808 an_write_record(sc, (struct an_ltv_gen *)areq);
1810 /* Turn the MAC back on. */
1811 an_cmd(sc, AN_CMD_ENABLE, 0);
1814 case AN_RID_MONITOR_MODE:
1815 cfg = (struct an_ltv_genconfig *)areq;
1817 if (ng_ether_detach_p != NULL)
1818 (*ng_ether_detach_p) (ifp);
1819 sc->an_monitor = cfg->an_len;
1821 if (sc->an_monitor & AN_MONITOR) {
1822 if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
1823 bpfattach(ifp, DLT_AIRONET_HEADER,
1824 sizeof(struct ether_header));
1826 bpfattach(ifp, DLT_IEEE802_11,
1827 sizeof(struct ether_header));
1830 bpfattach(ifp, DLT_EN10MB,
1831 sizeof(struct ether_header));
1832 if (ng_ether_attach_p != NULL)
1833 (*ng_ether_attach_p) (ifp);
1837 if_printf(ifp, "unknown RID: %x\n", areq->an_type);
1842 /* Reinitialize the card. */
1850 * Derived from Linux driver to enable promiscious mode.
1854 an_promisc(struct an_softc *sc, int promisc)
1857 if (sc->an_was_monitor) {
1860 an_init_mpi350_desc(sc);
1862 if (sc->an_monitor || sc->an_was_monitor)
1865 sc->an_was_monitor = sc->an_monitor;
1866 an_cmd(sc, AN_CMD_SET_MODE, promisc ? 0xffff : 0);
1872 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1877 struct an_softc *sc;
1878 struct an_req *areq;
1880 struct thread *td = curthread;
1881 struct ieee80211req *ireq;
1882 struct ieee80211_channel ch;
1883 u_int8_t tmpstr[IEEE80211_NWID_LEN*2];
1885 struct an_ltv_genconfig *config;
1886 struct an_ltv_key *key;
1887 struct an_ltv_status *status;
1888 struct an_ltv_ssidlist_new *ssids;
1890 struct aironet_ioctl l_ioctl;
1893 ifr = (struct ifreq *)data;
1894 ireq = (struct ieee80211req *)data;
1896 config = (struct an_ltv_genconfig *)&sc->areq;
1897 key = (struct an_ltv_key *)&sc->areq;
1898 status = (struct an_ltv_status *)&sc->areq;
1899 ssids = (struct an_ltv_ssidlist_new *)&sc->areq;
1909 if (ifp->if_flags & IFF_UP) {
1910 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1911 ifp->if_flags & IFF_PROMISC &&
1912 !(sc->an_if_flags & IFF_PROMISC)) {
1914 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1915 !(ifp->if_flags & IFF_PROMISC) &&
1916 sc->an_if_flags & IFF_PROMISC) {
1921 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1924 sc->an_if_flags = ifp->if_flags;
1930 error = ifmedia_ioctl(ifp, ifr, &sc->an_ifmedia, command);
1934 /* The Aironet has no multicast filter. */
1938 error = priv_check(td, PRIV_DRIVER);
1941 areq = malloc(sizeof(*areq), M_TEMP, M_WAITOK);
1942 error = copyin(ifr_data_get_ptr(ifr), areq, sizeof(*areq));
1948 memcpy(&sc->areq, areq, sizeof(sc->areq));
1950 if (sc->areq.an_type == AN_RID_ZERO_CACHE) {
1951 sc->an_sigitems = sc->an_nextitem = 0;
1954 } else if (sc->areq.an_type == AN_RID_READ_CACHE) {
1955 char *pt = (char *)&sc->areq.an_val;
1956 bcopy((char *)&sc->an_sigitems, (char *)pt,
1959 sc->areq.an_len = sizeof(int) / 2;
1960 bcopy((char *)&sc->an_sigcache, (char *)pt,
1961 sizeof(struct an_sigcache) * sc->an_sigitems);
1962 sc->areq.an_len += ((sizeof(struct an_sigcache) *
1963 sc->an_sigitems) / 2) + 1;
1966 if (an_read_record(sc, (struct an_ltv_gen *)&sc->areq)) {
1972 memcpy(areq, &sc->areq, sizeof(*areq));
1974 error = copyout(areq, ifr_data_get_ptr(ifr), sizeof(*areq));
1978 if ((error = priv_check(td, PRIV_DRIVER)))
1981 error = copyin(ifr_data_get_ptr(ifr), &sc->areq,
1985 an_setdef(sc, &sc->areq);
1988 case SIOCGPRIVATE_0: /* used by Cisco client utility */
1989 if ((error = priv_check(td, PRIV_DRIVER)))
1991 error = copyin(ifr_data_get_ptr(ifr), &l_ioctl,
1995 mode = l_ioctl.command;
1998 if (mode >= AIROGCAP && mode <= AIROGSTATSD32) {
1999 error = readrids(ifp, &l_ioctl);
2000 } else if (mode >= AIROPCAP && mode <= AIROPLEAPUSR) {
2001 error = writerids(ifp, &l_ioctl);
2002 } else if (mode >= AIROFLSHRST && mode <= AIRORESTART) {
2003 error = flashcard(ifp, &l_ioctl);
2009 /* copy out the updated command info */
2010 error = copyout(&l_ioctl, ifr_data_get_ptr(ifr),
2014 case SIOCGPRIVATE_1: /* used by Cisco client utility */
2015 if ((error = priv_check(td, PRIV_DRIVER)))
2017 error = copyin(ifr_data_get_ptr(ifr), &l_ioctl,
2021 l_ioctl.command = 0;
2023 (void) copyout(&error, l_ioctl.data, sizeof(error));
2027 sc->areq.an_len = sizeof(sc->areq);
2028 /* was that a good idea DJA we are doing a short-cut */
2029 switch (ireq->i_type) {
2030 case IEEE80211_IOC_SSID:
2032 if (ireq->i_val == -1) {
2033 sc->areq.an_type = AN_RID_STATUS;
2034 if (an_read_record(sc,
2035 (struct an_ltv_gen *)&sc->areq)) {
2040 len = status->an_ssidlen;
2041 tmpptr = status->an_ssid;
2042 } else if (ireq->i_val >= 0) {
2043 sc->areq.an_type = AN_RID_SSIDLIST;
2044 if (an_read_record(sc,
2045 (struct an_ltv_gen *)&sc->areq)) {
2050 max = (sc->areq.an_len - 4)
2051 / sizeof(struct an_ltv_ssid_entry);
2052 if ( max > MAX_SSIDS ) {
2053 printf("To many SSIDs only using "
2058 if (ireq->i_val > max) {
2063 len = ssids->an_entry[ireq->i_val].an_len;
2064 tmpptr = ssids->an_entry[ireq->i_val].an_ssid;
2071 if (len > IEEE80211_NWID_LEN) {
2078 bzero(tmpstr, IEEE80211_NWID_LEN);
2079 bcopy(tmpptr, tmpstr, len);
2080 error = copyout(tmpstr, ireq->i_data,
2081 IEEE80211_NWID_LEN);
2083 case IEEE80211_IOC_NUMSSIDS:
2085 sc->areq.an_len = sizeof(sc->areq);
2086 sc->areq.an_type = AN_RID_SSIDLIST;
2087 if (an_read_record(sc,
2088 (struct an_ltv_gen *)&sc->areq)) {
2093 max = (sc->areq.an_len - 4)
2094 / sizeof(struct an_ltv_ssid_entry);
2096 if ( max > MAX_SSIDS ) {
2097 printf("To many SSIDs only using "
2104 case IEEE80211_IOC_WEP:
2106 sc->areq.an_type = AN_RID_ACTUALCFG;
2107 if (an_read_record(sc,
2108 (struct an_ltv_gen *)&sc->areq)) {
2114 if (config->an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE) {
2115 if (config->an_authtype &
2116 AN_AUTHTYPE_ALLOW_UNENCRYPTED)
2117 ireq->i_val = IEEE80211_WEP_MIXED;
2119 ireq->i_val = IEEE80211_WEP_ON;
2121 ireq->i_val = IEEE80211_WEP_OFF;
2124 case IEEE80211_IOC_WEPKEY:
2126 * XXX: I'm not entierly convinced this is
2127 * correct, but it's what is implemented in
2128 * ancontrol so it will have to do until we get
2129 * access to actual Cisco code.
2131 if (ireq->i_val < 0 || ireq->i_val > 8) {
2136 if (ireq->i_val < 5) {
2138 sc->areq.an_type = AN_RID_WEP_TEMP;
2139 for (i = 0; i < 5; i++) {
2140 if (an_read_record(sc,
2141 (struct an_ltv_gen *)&sc->areq)) {
2145 if (key->kindex == 0xffff)
2147 if (key->kindex == ireq->i_val)
2149 /* Required to get next entry */
2150 sc->areq.an_type = AN_RID_WEP_PERM;
2157 /* We aren't allowed to read the value of the
2158 * key from the card so we just output zeros
2159 * like we would if we could read the card, but
2160 * denied the user access.
2164 error = copyout(tmpstr, ireq->i_data, len);
2166 case IEEE80211_IOC_NUMWEPKEYS:
2167 ireq->i_val = 9; /* include home key */
2169 case IEEE80211_IOC_WEPTXKEY:
2171 * For some strange reason, you have to read all
2172 * keys before you can read the txkey.
2175 sc->areq.an_type = AN_RID_WEP_TEMP;
2176 for (i = 0; i < 5; i++) {
2177 if (an_read_record(sc,
2178 (struct an_ltv_gen *) &sc->areq)) {
2182 if (key->kindex == 0xffff) {
2185 /* Required to get next entry */
2186 sc->areq.an_type = AN_RID_WEP_PERM;
2193 sc->areq.an_type = AN_RID_WEP_PERM;
2194 key->kindex = 0xffff;
2195 if (an_read_record(sc,
2196 (struct an_ltv_gen *)&sc->areq)) {
2201 ireq->i_val = key->mac[0];
2203 * Check for home mode. Map home mode into
2204 * 5th key since that is how it is stored on
2207 sc->areq.an_len = sizeof(struct an_ltv_genconfig);
2208 sc->areq.an_type = AN_RID_GENCONFIG;
2209 if (an_read_record(sc,
2210 (struct an_ltv_gen *)&sc->areq)) {
2215 if (config->an_home_product & AN_HOME_NETWORK)
2219 case IEEE80211_IOC_AUTHMODE:
2221 sc->areq.an_type = AN_RID_ACTUALCFG;
2222 if (an_read_record(sc,
2223 (struct an_ltv_gen *)&sc->areq)) {
2229 if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2231 ireq->i_val = IEEE80211_AUTH_NONE;
2232 } else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2234 ireq->i_val = IEEE80211_AUTH_OPEN;
2235 } else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2236 AN_AUTHTYPE_SHAREDKEY) {
2237 ireq->i_val = IEEE80211_AUTH_SHARED;
2241 case IEEE80211_IOC_STATIONNAME:
2243 sc->areq.an_type = AN_RID_ACTUALCFG;
2244 if (an_read_record(sc,
2245 (struct an_ltv_gen *)&sc->areq)) {
2251 ireq->i_len = sizeof(config->an_nodename);
2252 tmpptr = config->an_nodename;
2253 bzero(tmpstr, IEEE80211_NWID_LEN);
2254 bcopy(tmpptr, tmpstr, ireq->i_len);
2255 error = copyout(tmpstr, ireq->i_data,
2256 IEEE80211_NWID_LEN);
2258 case IEEE80211_IOC_CHANNEL:
2260 sc->areq.an_type = AN_RID_STATUS;
2261 if (an_read_record(sc,
2262 (struct an_ltv_gen *)&sc->areq)) {
2268 ireq->i_val = status->an_cur_channel;
2270 case IEEE80211_IOC_CURCHAN:
2272 sc->areq.an_type = AN_RID_STATUS;
2273 if (an_read_record(sc,
2274 (struct an_ltv_gen *)&sc->areq)) {
2280 bzero(&ch, sizeof(ch));
2281 ch.ic_freq = ieee80211_ieee2mhz(status->an_cur_channel,
2283 ch.ic_flags = IEEE80211_CHAN_B;
2284 ch.ic_ieee = status->an_cur_channel;
2285 error = copyout(&ch, ireq->i_data, sizeof(ch));
2287 case IEEE80211_IOC_POWERSAVE:
2289 sc->areq.an_type = AN_RID_ACTUALCFG;
2290 if (an_read_record(sc,
2291 (struct an_ltv_gen *)&sc->areq)) {
2297 if (config->an_psave_mode == AN_PSAVE_NONE) {
2298 ireq->i_val = IEEE80211_POWERSAVE_OFF;
2299 } else if (config->an_psave_mode == AN_PSAVE_CAM) {
2300 ireq->i_val = IEEE80211_POWERSAVE_CAM;
2301 } else if (config->an_psave_mode == AN_PSAVE_PSP) {
2302 ireq->i_val = IEEE80211_POWERSAVE_PSP;
2303 } else if (config->an_psave_mode == AN_PSAVE_PSP_CAM) {
2304 ireq->i_val = IEEE80211_POWERSAVE_PSP_CAM;
2308 case IEEE80211_IOC_POWERSAVESLEEP:
2310 sc->areq.an_type = AN_RID_ACTUALCFG;
2311 if (an_read_record(sc,
2312 (struct an_ltv_gen *)&sc->areq)) {
2318 ireq->i_val = config->an_listen_interval;
2323 if ((error = priv_check(td, PRIV_NET80211_VAP_MANAGE)))
2326 sc->areq.an_len = sizeof(sc->areq);
2328 * We need a config structure for everything but the WEP
2329 * key management and SSIDs so we get it now so avoid
2330 * duplicating this code every time.
2332 if (ireq->i_type != IEEE80211_IOC_SSID &&
2333 ireq->i_type != IEEE80211_IOC_WEPKEY &&
2334 ireq->i_type != IEEE80211_IOC_WEPTXKEY) {
2335 sc->areq.an_type = AN_RID_GENCONFIG;
2336 if (an_read_record(sc,
2337 (struct an_ltv_gen *)&sc->areq)) {
2343 switch (ireq->i_type) {
2344 case IEEE80211_IOC_SSID:
2345 sc->areq.an_len = sizeof(sc->areq);
2346 sc->areq.an_type = AN_RID_SSIDLIST;
2347 if (an_read_record(sc,
2348 (struct an_ltv_gen *)&sc->areq)) {
2353 if (ireq->i_len > IEEE80211_NWID_LEN) {
2358 max = (sc->areq.an_len - 4)
2359 / sizeof(struct an_ltv_ssid_entry);
2360 if ( max > MAX_SSIDS ) {
2361 printf("To many SSIDs only using "
2366 if (ireq->i_val > max) {
2371 error = copyin(ireq->i_data,
2372 ssids->an_entry[ireq->i_val].an_ssid,
2374 ssids->an_entry[ireq->i_val].an_len
2376 sc->areq.an_len = sizeof(sc->areq);
2377 sc->areq.an_type = AN_RID_SSIDLIST;
2378 an_setdef(sc, &sc->areq);
2383 case IEEE80211_IOC_WEP:
2384 switch (ireq->i_val) {
2385 case IEEE80211_WEP_OFF:
2386 config->an_authtype &=
2387 ~(AN_AUTHTYPE_PRIVACY_IN_USE |
2388 AN_AUTHTYPE_ALLOW_UNENCRYPTED);
2390 case IEEE80211_WEP_ON:
2391 config->an_authtype |=
2392 AN_AUTHTYPE_PRIVACY_IN_USE;
2393 config->an_authtype &=
2394 ~AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2396 case IEEE80211_WEP_MIXED:
2397 config->an_authtype |=
2398 AN_AUTHTYPE_PRIVACY_IN_USE |
2399 AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2405 if (error != EINVAL)
2406 an_setdef(sc, &sc->areq);
2409 case IEEE80211_IOC_WEPKEY:
2410 if (ireq->i_val < 0 || ireq->i_val > 8 ||
2416 error = copyin(ireq->i_data, tmpstr, 13);
2422 * Map the 9th key into the home mode
2423 * since that is how it is stored on
2426 bzero(&sc->areq, sizeof(struct an_ltv_key));
2427 sc->areq.an_len = sizeof(struct an_ltv_key);
2428 key->mac[0] = 1; /* The others are 0. */
2429 if (ireq->i_val < 4) {
2430 sc->areq.an_type = AN_RID_WEP_TEMP;
2431 key->kindex = ireq->i_val;
2433 sc->areq.an_type = AN_RID_WEP_PERM;
2434 key->kindex = ireq->i_val - 4;
2436 key->klen = ireq->i_len;
2437 bcopy(tmpstr, key->key, key->klen);
2438 an_setdef(sc, &sc->areq);
2441 case IEEE80211_IOC_WEPTXKEY:
2442 if (ireq->i_val < 0 || ireq->i_val > 4) {
2449 * Map the 5th key into the home mode
2450 * since that is how it is stored on
2453 sc->areq.an_len = sizeof(struct an_ltv_genconfig);
2454 sc->areq.an_type = AN_RID_ACTUALCFG;
2455 if (an_read_record(sc,
2456 (struct an_ltv_gen *)&sc->areq)) {
2461 if (ireq->i_val == 4) {
2462 config->an_home_product |= AN_HOME_NETWORK;
2465 config->an_home_product &= ~AN_HOME_NETWORK;
2468 sc->an_config.an_home_product
2469 = config->an_home_product;
2471 /* update configuration */
2474 bzero(&sc->areq, sizeof(struct an_ltv_key));
2475 sc->areq.an_len = sizeof(struct an_ltv_key);
2476 sc->areq.an_type = AN_RID_WEP_PERM;
2477 key->kindex = 0xffff;
2478 key->mac[0] = ireq->i_val;
2479 an_setdef(sc, &sc->areq);
2482 case IEEE80211_IOC_AUTHMODE:
2483 switch (ireq->i_val) {
2484 case IEEE80211_AUTH_NONE:
2485 config->an_authtype = AN_AUTHTYPE_NONE |
2486 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2488 case IEEE80211_AUTH_OPEN:
2489 config->an_authtype = AN_AUTHTYPE_OPEN |
2490 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2492 case IEEE80211_AUTH_SHARED:
2493 config->an_authtype = AN_AUTHTYPE_SHAREDKEY |
2494 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2499 if (error != EINVAL) {
2500 an_setdef(sc, &sc->areq);
2504 case IEEE80211_IOC_STATIONNAME:
2505 if (ireq->i_len > 16) {
2510 bzero(config->an_nodename, 16);
2511 error = copyin(ireq->i_data,
2512 config->an_nodename, ireq->i_len);
2513 an_setdef(sc, &sc->areq);
2516 case IEEE80211_IOC_CHANNEL:
2518 * The actual range is 1-14, but if you set it
2519 * to 0 you get the default so we let that work
2522 if (ireq->i_val < 0 || ireq->i_val >14) {
2527 config->an_ds_channel = ireq->i_val;
2528 an_setdef(sc, &sc->areq);
2531 case IEEE80211_IOC_POWERSAVE:
2532 switch (ireq->i_val) {
2533 case IEEE80211_POWERSAVE_OFF:
2534 config->an_psave_mode = AN_PSAVE_NONE;
2536 case IEEE80211_POWERSAVE_CAM:
2537 config->an_psave_mode = AN_PSAVE_CAM;
2539 case IEEE80211_POWERSAVE_PSP:
2540 config->an_psave_mode = AN_PSAVE_PSP;
2542 case IEEE80211_POWERSAVE_PSP_CAM:
2543 config->an_psave_mode = AN_PSAVE_PSP_CAM;
2549 an_setdef(sc, &sc->areq);
2552 case IEEE80211_IOC_POWERSAVESLEEP:
2553 config->an_listen_interval = ireq->i_val;
2554 an_setdef(sc, &sc->areq);
2565 an_setdef(sc, &sc->areq);
2571 error = ether_ioctl(ifp, command, data);
2580 an_init_tx_ring(struct an_softc *sc)
2589 for (i = 0; i < AN_TX_RING_CNT; i++) {
2590 if (an_alloc_nicmem(sc, 1518 +
2593 sc->an_rdata.an_tx_fids[i] = id;
2594 sc->an_rdata.an_tx_ring[i] = 0;
2598 sc->an_rdata.an_tx_prod = 0;
2599 sc->an_rdata.an_tx_cons = 0;
2600 sc->an_rdata.an_tx_empty = 1;
2608 struct an_softc *sc = xsc;
2616 an_init_locked(struct an_softc *sc)
2625 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2628 sc->an_associated = 0;
2630 /* Allocate the TX buffers */
2631 if (an_init_tx_ring(sc)) {
2634 an_init_mpi350_desc(sc);
2635 if (an_init_tx_ring(sc)) {
2636 if_printf(ifp, "tx buffer allocation failed\n");
2641 /* Set our MAC address. */
2642 bcopy((char *)IF_LLADDR(sc->an_ifp),
2643 (char *)&sc->an_config.an_macaddr, ETHER_ADDR_LEN);
2645 if (ifp->if_flags & IFF_BROADCAST)
2646 sc->an_config.an_rxmode = AN_RXMODE_BC_ADDR;
2648 sc->an_config.an_rxmode = AN_RXMODE_ADDR;
2650 if (ifp->if_flags & IFF_MULTICAST)
2651 sc->an_config.an_rxmode = AN_RXMODE_BC_MC_ADDR;
2653 if (ifp->if_flags & IFF_PROMISC) {
2654 if (sc->an_monitor & AN_MONITOR) {
2655 if (sc->an_monitor & AN_MONITOR_ANY_BSS) {
2656 sc->an_config.an_rxmode |=
2657 AN_RXMODE_80211_MONITOR_ANYBSS |
2658 AN_RXMODE_NO_8023_HEADER;
2660 sc->an_config.an_rxmode |=
2661 AN_RXMODE_80211_MONITOR_CURBSS |
2662 AN_RXMODE_NO_8023_HEADER;
2668 if (sc->an_have_rssimap)
2669 sc->an_config.an_rxmode |= AN_RXMODE_NORMALIZED_RSSI;
2672 /* Set the ssid list */
2673 sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
2674 sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
2675 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
2676 if_printf(ifp, "failed to set ssid list\n");
2680 /* Set the AP list */
2681 sc->an_aplist.an_type = AN_RID_APLIST;
2682 sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
2683 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
2684 if_printf(ifp, "failed to set AP list\n");
2688 /* Set the configuration in the NIC */
2689 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
2690 sc->an_config.an_type = AN_RID_GENCONFIG;
2691 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
2692 if_printf(ifp, "failed to set configuration\n");
2696 /* Enable the MAC */
2697 if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
2698 if_printf(ifp, "failed to enable MAC\n");
2702 if (ifp->if_flags & IFF_PROMISC)
2703 an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
2705 /* enable interrupts */
2706 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2708 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2709 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2711 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
2717 an_start(struct ifnet *ifp)
2719 struct an_softc *sc;
2723 an_start_locked(ifp);
2728 an_start_locked(struct ifnet *ifp)
2730 struct an_softc *sc;
2731 struct mbuf *m0 = NULL;
2732 struct an_txframe_802_3 tx_frame_802_3;
2733 struct ether_header *eh;
2735 unsigned char txcontrol;
2736 struct an_card_tx_desc an_tx_desc;
2745 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
2748 if (!sc->an_associated)
2751 /* We can't send in monitor mode so toss any attempts. */
2752 if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
2754 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2762 idx = sc->an_rdata.an_tx_prod;
2765 bzero((char *)&tx_frame_802_3, sizeof(tx_frame_802_3));
2767 while (sc->an_rdata.an_tx_ring[idx] == 0) {
2768 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2772 id = sc->an_rdata.an_tx_fids[idx];
2773 eh = mtod(m0, struct ether_header *);
2775 bcopy((char *)&eh->ether_dhost,
2776 (char *)&tx_frame_802_3.an_tx_dst_addr,
2778 bcopy((char *)&eh->ether_shost,
2779 (char *)&tx_frame_802_3.an_tx_src_addr,
2782 /* minus src/dest mac & type */
2783 tx_frame_802_3.an_tx_802_3_payload_len =
2784 m0->m_pkthdr.len - 12;
2786 m_copydata(m0, sizeof(struct ether_header) - 2 ,
2787 tx_frame_802_3.an_tx_802_3_payload_len,
2788 (caddr_t)&sc->an_txbuf);
2790 txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2791 /* write the txcontrol only */
2792 an_write_data(sc, id, 0x08, (caddr_t)&txcontrol,
2796 an_write_data(sc, id, 0x34, (caddr_t)&tx_frame_802_3,
2797 sizeof(struct an_txframe_802_3));
2799 /* in mbuf header type is just before payload */
2800 an_write_data(sc, id, 0x44, (caddr_t)&sc->an_txbuf,
2801 tx_frame_802_3.an_tx_802_3_payload_len);
2804 * If there's a BPF listner, bounce a copy of
2805 * this frame to him.
2812 sc->an_rdata.an_tx_ring[idx] = id;
2813 if (an_cmd(sc, AN_CMD_TX, id))
2814 if_printf(ifp, "xmit failed\n");
2816 AN_INC(idx, AN_TX_RING_CNT);
2819 * Set a timeout in case the chip goes out to lunch.
2823 } else { /* MPI-350 */
2824 /* Disable interrupts. */
2825 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2827 while (sc->an_rdata.an_tx_empty ||
2828 idx != sc->an_rdata.an_tx_cons) {
2829 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2833 buf = sc->an_tx_buffer[idx].an_dma_vaddr;
2835 eh = mtod(m0, struct ether_header *);
2837 /* DJA optimize this to limit bcopy */
2838 bcopy((char *)&eh->ether_dhost,
2839 (char *)&tx_frame_802_3.an_tx_dst_addr,
2841 bcopy((char *)&eh->ether_shost,
2842 (char *)&tx_frame_802_3.an_tx_src_addr,
2845 /* minus src/dest mac & type */
2846 tx_frame_802_3.an_tx_802_3_payload_len =
2847 m0->m_pkthdr.len - 12;
2849 m_copydata(m0, sizeof(struct ether_header) - 2 ,
2850 tx_frame_802_3.an_tx_802_3_payload_len,
2851 (caddr_t)&sc->an_txbuf);
2853 txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2854 /* write the txcontrol only */
2855 bcopy((caddr_t)&txcontrol, &buf[0x08],
2859 bcopy((caddr_t)&tx_frame_802_3, &buf[0x34],
2860 sizeof(struct an_txframe_802_3));
2862 /* in mbuf header type is just before payload */
2863 bcopy((caddr_t)&sc->an_txbuf, &buf[0x44],
2864 tx_frame_802_3.an_tx_802_3_payload_len);
2867 bzero(&an_tx_desc, sizeof(an_tx_desc));
2868 an_tx_desc.an_offset = 0;
2869 an_tx_desc.an_eoc = 1;
2870 an_tx_desc.an_valid = 1;
2871 an_tx_desc.an_len = 0x44 +
2872 tx_frame_802_3.an_tx_802_3_payload_len;
2874 = sc->an_tx_buffer[idx].an_dma_paddr;
2875 for (i = sizeof(an_tx_desc) / 4 - 1; i >= 0; i--) {
2876 CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
2878 + (0 * sizeof(an_tx_desc))
2880 ((u_int32_t *)(void *)&an_tx_desc)[i]);
2884 * If there's a BPF listner, bounce a copy of
2885 * this frame to him.
2891 AN_INC(idx, AN_MAX_TX_DESC);
2892 sc->an_rdata.an_tx_empty = 0;
2893 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
2896 * Set a timeout in case the chip goes out to lunch.
2901 /* Re-enable interrupts. */
2902 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2906 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2908 sc->an_rdata.an_tx_prod = idx;
2914 an_stop(struct an_softc *sc)
2926 an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
2927 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2928 an_cmd(sc, AN_CMD_DISABLE, 0);
2930 for (i = 0; i < AN_TX_RING_CNT; i++)
2931 an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->an_rdata.an_tx_fids[i]);
2933 callout_stop(&sc->an_stat_ch);
2935 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
2937 if (sc->an_flash_buffer) {
2938 free(sc->an_flash_buffer, M_DEVBUF);
2939 sc->an_flash_buffer = NULL;
2944 an_watchdog(struct an_softc *sc)
2954 if_printf(ifp, "device timeout\n");
2958 an_init_mpi350_desc(sc);
2961 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2965 an_shutdown(device_t dev)
2967 struct an_softc *sc;
2969 sc = device_get_softc(dev);
2979 an_resume(device_t dev)
2981 struct an_softc *sc;
2985 sc = device_get_softc(dev);
2992 an_init_mpi350_desc(sc);
2995 /* Recovery temporary keys */
2996 for (i = 0; i < 4; i++) {
2997 sc->areq.an_type = AN_RID_WEP_TEMP;
2998 sc->areq.an_len = sizeof(struct an_ltv_key);
2999 bcopy(&sc->an_temp_keys[i],
3000 &sc->areq, sizeof(struct an_ltv_key));
3001 an_setdef(sc, &sc->areq);
3004 if (ifp->if_flags & IFF_UP)
3005 an_start_locked(ifp);
3012 /* Aironet signal strength cache code.
3013 * store signal/noise/quality on per MAC src basis in
3014 * a small fixed cache. The cache wraps if > MAX slots
3015 * used. The cache may be zeroed out to start over.
3016 * Two simple filters exist to reduce computation:
3017 * 1. ip only (literally 0x800, ETHERTYPE_IP) which may be used
3018 * to ignore some packets. It defaults to ip only.
3019 * it could be used to focus on broadcast, non-IP 802.11 beacons.
3020 * 2. multicast/broadcast only. This may be used to
3021 * ignore unicast packets and only cache signal strength
3022 * for multicast/broadcast packets (beacons); e.g., Mobile-IP
3023 * beacons and not unicast traffic.
3025 * The cache stores (MAC src(index), IP src (major clue), signal,
3028 * No apologies for storing IP src here. It's easy and saves much
3029 * trouble elsewhere. The cache is assumed to be INET dependent,
3030 * although it need not be.
3032 * Note: the Aironet only has a single byte of signal strength value
3033 * in the rx frame header, and it's not scaled to anything sensible.
3034 * This is kind of lame, but it's all we've got.
3037 #ifdef documentation
3039 int an_sigitems; /* number of cached entries */
3040 struct an_sigcache an_sigcache[MAXANCACHE]; /* array of cache entries */
3041 int an_nextitem; /* index/# of entries */
3046 /* control variables for cache filtering. Basic idea is
3047 * to reduce cost (e.g., to only Mobile-IP agent beacons
3048 * which are broadcast or multicast). Still you might
3049 * want to measure signal strength anth unicast ping packets
3050 * on a pt. to pt. ant. setup.
3052 /* set true if you want to limit cache items to broadcast/mcast
3053 * only packets (not unicast). Useful for mobile-ip beacons which
3054 * are broadcast/multicast at network layer. Default is all packets
3055 * so ping/unicast anll work say anth pt. to pt. antennae setup.
3057 static int an_cache_mcastonly = 0;
3058 SYSCTL_INT(_hw_an, OID_AUTO, an_cache_mcastonly, CTLFLAG_RW,
3059 &an_cache_mcastonly, 0, "");
3061 /* set true if you want to limit cache items to IP packets only
3063 static int an_cache_iponly = 1;
3064 SYSCTL_INT(_hw_an, OID_AUTO, an_cache_iponly, CTLFLAG_RW,
3065 &an_cache_iponly, 0, "");
3068 * an_cache_store, per rx packet store signal
3069 * strength in MAC (src) indexed cache.
3072 an_cache_store(struct an_softc *sc, struct ether_header *eh, struct mbuf *m,
3073 u_int8_t rx_rssi, u_int8_t rx_quality)
3075 struct ip *ip = NULL;
3077 static int cache_slot = 0; /* use this cache entry */
3078 static int wrapindex = 0; /* next "free" cache entry */
3083 * 2. configurable filter to throw out unicast packets,
3084 * keep multicast only.
3087 if ((ntohs(eh->ether_type) == ETHERTYPE_IP)) {
3091 /* filter for ip packets only
3093 if ( an_cache_iponly && !type_ipv4) {
3097 /* filter for broadcast/multicast only
3099 if (an_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) {
3104 if_printf(sc->an_ifp, "q value %x (MSB=0x%x, LSB=0x%x) \n",
3105 rx_rssi & 0xffff, rx_rssi >> 8, rx_rssi & 0xff);
3108 /* find the ip header. we want to store the ip_src
3112 ip = mtod(m, struct ip *);
3115 /* do a linear search for a matching MAC address
3116 * in the cache table
3117 * . MAC address is 6 bytes,
3118 * . var w_nextitem holds total number of entries already cached
3120 for (i = 0; i < sc->an_nextitem; i++) {
3121 if (! bcmp(eh->ether_shost , sc->an_sigcache[i].macsrc, 6 )) {
3123 * so we already have this entry,
3130 /* did we find a matching mac address?
3131 * if yes, then overwrite a previously existing cache entry
3133 if (i < sc->an_nextitem ) {
3136 /* else, have a new address entry,so
3137 * add this new entry,
3138 * if table full, then we need to replace LRU entry
3142 /* check for space in cache table
3143 * note: an_nextitem also holds number of entries
3144 * added in the cache table
3146 if ( sc->an_nextitem < MAXANCACHE ) {
3147 cache_slot = sc->an_nextitem;
3149 sc->an_sigitems = sc->an_nextitem;
3151 /* no space found, so simply wrap anth wrap index
3152 * and "zap" the next entry
3155 if (wrapindex == MAXANCACHE) {
3158 cache_slot = wrapindex++;
3162 /* invariant: cache_slot now points at some slot
3165 if (cache_slot < 0 || cache_slot >= MAXANCACHE) {
3166 log(LOG_ERR, "an_cache_store, bad index: %d of "
3167 "[0..%d], gross cache error\n",
3168 cache_slot, MAXANCACHE);
3172 /* store items in cache
3173 * .ip source address
3178 sc->an_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr;
3180 bcopy( eh->ether_shost, sc->an_sigcache[cache_slot].macsrc, 6);
3183 switch (an_cache_mode) {
3185 if (sc->an_have_rssimap) {
3186 sc->an_sigcache[cache_slot].signal =
3187 - sc->an_rssimap.an_entries[rx_rssi].an_rss_dbm;
3188 sc->an_sigcache[cache_slot].quality =
3189 - sc->an_rssimap.an_entries[rx_quality].an_rss_dbm;
3191 sc->an_sigcache[cache_slot].signal = rx_rssi - 100;
3192 sc->an_sigcache[cache_slot].quality = rx_quality - 100;
3196 if (sc->an_have_rssimap) {
3197 sc->an_sigcache[cache_slot].signal =
3198 sc->an_rssimap.an_entries[rx_rssi].an_rss_pct;
3199 sc->an_sigcache[cache_slot].quality =
3200 sc->an_rssimap.an_entries[rx_quality].an_rss_pct;
3204 if (rx_quality > 100)
3206 sc->an_sigcache[cache_slot].signal = rx_rssi;
3207 sc->an_sigcache[cache_slot].quality = rx_quality;
3211 sc->an_sigcache[cache_slot].signal = rx_rssi;
3212 sc->an_sigcache[cache_slot].quality = rx_quality;
3216 sc->an_sigcache[cache_slot].noise = 0;
3223 an_media_change(struct ifnet *ifp)
3225 struct an_softc *sc = ifp->if_softc;
3226 struct an_ltv_genconfig *cfg;
3227 int otype = sc->an_config.an_opmode;
3228 int orate = sc->an_tx_rate;
3231 sc->an_tx_rate = ieee80211_media2rate(
3232 IFM_SUBTYPE(sc->an_ifmedia.ifm_cur->ifm_media));
3233 if (sc->an_tx_rate < 0)
3236 if (orate != sc->an_tx_rate) {
3237 /* Read the current configuration */
3238 sc->an_config.an_type = AN_RID_GENCONFIG;
3239 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3240 an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
3241 cfg = &sc->an_config;
3243 /* clear other rates and set the only one we want */
3244 bzero(cfg->an_rates, sizeof(cfg->an_rates));
3245 cfg->an_rates[0] = sc->an_tx_rate;
3247 /* Save the new rate */
3248 sc->an_config.an_type = AN_RID_GENCONFIG;
3249 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3252 if ((sc->an_ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
3253 sc->an_config.an_opmode &= ~AN_OPMODE_INFRASTRUCTURE_STATION;
3255 sc->an_config.an_opmode |= AN_OPMODE_INFRASTRUCTURE_STATION;
3257 if (otype != sc->an_config.an_opmode ||
3258 orate != sc->an_tx_rate)
3266 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
3268 struct an_ltv_status status;
3269 struct an_softc *sc = ifp->if_softc;
3271 imr->ifm_active = IFM_IEEE80211;
3274 status.an_len = sizeof(status);
3275 status.an_type = AN_RID_STATUS;
3276 if (an_read_record(sc, (struct an_ltv_gen *)&status)) {
3277 /* If the status read fails, just lie. */
3278 imr->ifm_active = sc->an_ifmedia.ifm_cur->ifm_media;
3279 imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
3282 if (sc->an_tx_rate == 0) {
3283 imr->ifm_active = IFM_IEEE80211|IFM_AUTO;
3286 if (sc->an_config.an_opmode == AN_OPMODE_IBSS_ADHOC)
3287 imr->ifm_active |= IFM_IEEE80211_ADHOC;
3288 imr->ifm_active |= ieee80211_rate2media(NULL,
3289 status.an_current_tx_rate, IEEE80211_MODE_AUTO);
3290 imr->ifm_status = IFM_AVALID;
3291 if (status.an_opmode & AN_STATUS_OPMODE_ASSOCIATED)
3292 imr->ifm_status |= IFM_ACTIVE;
3296 /********************** Cisco utility support routines *************/
3299 * ReadRids & WriteRids derived from Cisco driver additions to Ben Reed's
3304 readrids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3307 struct an_softc *sc;
3310 switch (l_ioctl->command) {
3312 rid = AN_RID_CAPABILITIES;
3315 rid = AN_RID_GENCONFIG;
3318 rid = AN_RID_SSIDLIST;
3321 rid = AN_RID_APLIST;
3324 rid = AN_RID_DRVNAME;
3327 rid = AN_RID_ENCAPPROTO;
3330 rid = AN_RID_WEP_TEMP;
3333 rid = AN_RID_WEP_PERM;
3336 rid = AN_RID_STATUS;
3339 rid = AN_RID_32BITS_DELTA;
3342 rid = AN_RID_32BITS_CUM;
3349 if (rid == 999) /* Is bad command */
3353 sc->areq.an_len = AN_MAX_DATALEN;
3354 sc->areq.an_type = rid;
3356 an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3358 l_ioctl->len = sc->areq.an_len - 4; /* just data */
3361 /* the data contains the length at first */
3362 if (copyout(&(sc->areq.an_len), l_ioctl->data,
3363 sizeof(sc->areq.an_len))) {
3367 /* Just copy the data back */
3368 if (copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3380 writerids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3382 struct an_softc *sc;
3383 int rid, command, error;
3388 command = l_ioctl->command;
3392 rid = AN_RID_SSIDLIST;
3395 rid = AN_RID_CAPABILITIES;
3398 rid = AN_RID_APLIST;
3401 rid = AN_RID_GENCONFIG;
3404 an_cmd(sc, AN_CMD_ENABLE, 0);
3408 an_cmd(sc, AN_CMD_DISABLE, 0);
3413 * This command merely clears the counts does not actually
3414 * store any data only reads rid. But as it changes the cards
3415 * state, I put it in the writerid routines.
3418 rid = AN_RID_32BITS_DELTACLR;
3420 sc->areq.an_len = AN_MAX_DATALEN;
3421 sc->areq.an_type = rid;
3423 an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3424 l_ioctl->len = sc->areq.an_len - 4; /* just data */
3427 /* the data contains the length at first */
3428 error = copyout(&(sc->areq.an_len), l_ioctl->data,
3429 sizeof(sc->areq.an_len));
3434 /* Just copy the data */
3435 error = copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3443 rid = AN_RID_WEP_TEMP;
3446 rid = AN_RID_WEP_PERM;
3449 rid = AN_RID_LEAPUSERNAME;
3452 rid = AN_RID_LEAPPASSWORD;
3459 if (l_ioctl->len > sizeof(sc->areq.an_val) + 4)
3461 sc->areq.an_len = l_ioctl->len + 4; /* add type & length */
3462 sc->areq.an_type = rid;
3464 /* Just copy the data back */
3466 error = copyin((l_ioctl->data) + 2, &sc->areq.an_val,
3472 an_cmd(sc, AN_CMD_DISABLE, 0);
3473 an_write_record(sc, (struct an_ltv_gen *)&sc->areq);
3474 an_cmd(sc, AN_CMD_ENABLE, 0);
3481 * General Flash utilities derived from Cisco driver additions to Ben Reed's
3485 #define FLASH_DELAY(_sc, x) msleep(ifp, &(_sc)->an_mtx, PZERO, \
3486 "flash", ((x) / hz) + 1);
3487 #define FLASH_COMMAND 0x7e7e
3488 #define FLASH_SIZE 32 * 1024
3491 unstickbusy(struct ifnet *ifp)
3493 struct an_softc *sc = ifp->if_softc;
3495 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
3496 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
3497 AN_EV_CLR_STUCK_BUSY);
3504 * Wait for busy completion from card wait for delay uSec's Return true for
3505 * success meaning command reg is clear
3509 WaitBusy(struct ifnet *ifp, int uSec)
3511 int statword = 0xffff;
3513 struct an_softc *sc = ifp->if_softc;
3515 while ((statword & AN_CMD_BUSY) && delay <= (1000 * 100)) {
3516 FLASH_DELAY(sc, 10);
3518 statword = CSR_READ_2(sc, AN_COMMAND(sc->mpi350));
3520 if ((AN_CMD_BUSY & statword) && (delay % 200)) {
3525 return 0 == (AN_CMD_BUSY & statword);
3529 * STEP 1) Disable MAC and do soft reset on card.
3533 cmdreset(struct ifnet *ifp)
3536 struct an_softc *sc = ifp->if_softc;
3541 an_cmd(sc, AN_CMD_DISABLE, 0);
3543 if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3544 if_printf(ifp, "Waitbusy hang b4 RESET =%d\n", status);
3548 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), AN_CMD_FW_RESTART);
3550 FLASH_DELAY(sc, 1000); /* WAS 600 12/7/00 */
3553 if (!(status = WaitBusy(ifp, 100))) {
3554 if_printf(ifp, "Waitbusy hang AFTER RESET =%d\n", status);
3563 * STEP 2) Put the card in legendary flash mode
3567 setflashmode(struct ifnet *ifp)
3570 struct an_softc *sc = ifp->if_softc;
3572 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3573 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), FLASH_COMMAND);
3574 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3575 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), FLASH_COMMAND);
3578 * mdelay(500); // 500ms delay
3581 FLASH_DELAY(sc, 500);
3583 if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3584 printf("Waitbusy hang after setflash mode\n");
3591 * Get a character from the card matching matchbyte Step 3)
3595 flashgchar(struct ifnet *ifp, int matchbyte, int dwelltime)
3598 unsigned char rbyte = 0;
3600 struct an_softc *sc = ifp->if_softc;
3604 rchar = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3606 if (dwelltime && !(0x8000 & rchar)) {
3608 FLASH_DELAY(sc, 10);
3611 rbyte = 0xff & rchar;
3613 if ((rbyte == matchbyte) && (0x8000 & rchar)) {
3614 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3618 if (rbyte == 0x81 || rbyte == 0x82 || rbyte == 0x83 || rbyte == 0x1a || 0xffff == rchar)
3620 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3622 } while (dwelltime > 0);
3627 * Put character to SWS0 wait for dwelltime x 50us for echo .
3631 flashpchar(struct ifnet *ifp, int byte, int dwelltime)
3634 int pollbusy, waittime;
3635 struct an_softc *sc = ifp->if_softc;
3642 waittime = dwelltime;
3645 * Wait for busy bit d15 to go false indicating buffer empty
3648 pollbusy = CSR_READ_2(sc, AN_SW0(sc->mpi350));
3650 if (pollbusy & 0x8000) {
3651 FLASH_DELAY(sc, 50);
3657 while (waittime >= 0);
3659 /* timeout for busy clear wait */
3661 if (waittime <= 0) {
3662 if_printf(ifp, "flash putchar busywait timeout!\n");
3666 * Port is clear now write byte and wait for it to echo back
3669 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), byte);
3670 FLASH_DELAY(sc, 50);
3672 echo = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3673 } while (dwelltime >= 0 && echo != byte);
3676 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3678 return echo == byte;
3682 * Transfer 32k of firmware data from user buffer to our buffer and send to
3687 flashputbuf(struct ifnet *ifp)
3689 unsigned short *bufp;
3691 struct an_softc *sc = ifp->if_softc;
3695 bufp = sc->an_flash_buffer;
3698 CSR_WRITE_2(sc, AN_AUX_PAGE, 0x100);
3699 CSR_WRITE_2(sc, AN_AUX_OFFSET, 0);
3701 for (nwords = 0; nwords != FLASH_SIZE / 2; nwords++) {
3702 CSR_WRITE_2(sc, AN_AUX_DATA, bufp[nwords] & 0xffff);
3705 for (nwords = 0; nwords != FLASH_SIZE / 4; nwords++) {
3706 CSR_MEM_AUX_WRITE_4(sc, 0x8000,
3707 ((u_int32_t *)bufp)[nwords] & 0xffff);
3711 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), 0x8000);
3717 * After flashing restart the card.
3721 flashrestart(struct ifnet *ifp)
3724 struct an_softc *sc = ifp->if_softc;
3726 FLASH_DELAY(sc, 1024); /* Added 12/7/00 */
3730 FLASH_DELAY(sc, 1024); /* Added 12/7/00 */
3735 * Entry point for flash ioclt.
3739 flashcard(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3742 struct an_softc *sc;
3746 if_printf(ifp, "flashing not supported on MPI 350 yet\n");
3749 status = l_ioctl->command;
3751 switch (l_ioctl->command) {
3753 return cmdreset(ifp);
3756 if (sc->an_flash_buffer) {
3757 free(sc->an_flash_buffer, M_DEVBUF);
3758 sc->an_flash_buffer = NULL;
3760 sc->an_flash_buffer = malloc(FLASH_SIZE, M_DEVBUF, M_WAITOK);
3761 if (sc->an_flash_buffer)
3762 return setflashmode(ifp);
3766 case AIROFLSHGCHR: /* Get char from aux */
3767 if (l_ioctl->len > sizeof(sc->areq)) {
3771 status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3775 z = *(int *)&sc->areq;
3776 if ((status = flashgchar(ifp, z, 8000)) == 1)
3780 case AIROFLSHPCHR: /* Send char to card. */
3781 if (l_ioctl->len > sizeof(sc->areq)) {
3785 status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3789 z = *(int *)&sc->areq;
3790 if ((status = flashpchar(ifp, z, 8000)) == -1)
3795 case AIROFLPUTBUF: /* Send 32k to card */
3796 if (l_ioctl->len > FLASH_SIZE) {
3797 if_printf(ifp, "Buffer to big, %x %x\n",
3798 l_ioctl->len, FLASH_SIZE);
3802 status = copyin(l_ioctl->data, sc->an_flash_buffer, l_ioctl->len);
3807 if ((status = flashputbuf(ifp)) != 0)
3813 if ((status = flashrestart(ifp)) != 0) {
3814 if_printf(ifp, "FLASHRESTART returned %d\n", status);