2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
35 * Written by Bill Paul <wpaul@ctr.columbia.edu>
36 * Electrical Engineering Department
37 * Columbia University, New York City
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * The Aironet 4500/4800 series cards come in PCMCIA, ISA and PCI form.
45 * This driver supports all three device types (PCI devices are supported
46 * through an extra PCI shim: /sys/dev/an/if_an_pci.c). ISA devices can be
47 * supported either using hard-coded IO port/IRQ settings or via Plug
48 * and Play. The 4500 series devices support 1Mbps and 2Mbps data rates.
49 * The 4800 devices support 1, 2, 5.5 and 11Mbps rates.
51 * Like the WaveLAN/IEEE cards, the Aironet NICs are all essentially
52 * PCMCIA devices. The ISA and PCI cards are a combination of a PCMCIA
53 * device and a PCMCIA to ISA or PCMCIA to PCI adapter card. There are
54 * a couple of important differences though:
56 * - Lucent ISA card looks to the host like a PCMCIA controller with
57 * a PCMCIA WaveLAN card inserted. This means that even desktop
58 * machines need to be configured with PCMCIA support in order to
59 * use WaveLAN/IEEE ISA cards. The Aironet cards on the other hand
60 * actually look like normal ISA and PCI devices to the host, so
61 * no PCMCIA controller support is needed
63 * The latter point results in a small gotcha. The Aironet PCMCIA
64 * cards can be configured for one of two operating modes depending
65 * on how the Vpp1 and Vpp2 programming voltages are set when the
66 * card is activated. In order to put the card in proper PCMCIA
67 * operation (where the CIS table is visible and the interface is
68 * programmed for PCMCIA operation), both Vpp1 and Vpp2 have to be
69 * set to 5 volts. FreeBSD by default doesn't set the Vpp voltages,
70 * which leaves the card in ISA/PCI mode, which prevents it from
71 * being activated as an PCMCIA device.
73 * Note that some PCMCIA controller software packages for Windows NT
74 * fail to set the voltages as well.
76 * The Aironet devices can operate in both station mode and access point
77 * mode. Typically, when programmed for station mode, the card can be set
78 * to automatically perform encapsulation/decapsulation of Ethernet II
79 * and 802.3 frames within 802.11 frames so that the host doesn't have
80 * to do it itself. This driver doesn't program the card that way: the
81 * driver handles all of the encapsulation/decapsulation itself.
87 #define ANCACHE /* enable signal strength cache */
90 #include <sys/param.h>
91 #include <sys/ctype.h>
92 #include <sys/systm.h>
93 #include <sys/sockio.h>
97 #include <sys/kernel.h>
98 #include <sys/socket.h>
100 #include <sys/syslog.h>
102 #include <sys/sysctl.h>
104 #include <sys/module.h>
106 #include <machine/bus.h>
107 #include <sys/rman.h>
108 #include <sys/lock.h>
109 #include <sys/mutex.h>
110 #include <machine/resource.h>
111 #include <sys/malloc.h>
114 #include <net/if_arp.h>
115 #include <net/if_dl.h>
116 #include <net/ethernet.h>
117 #include <net/if_types.h>
118 #include <net/if_media.h>
120 #include <net80211/ieee80211_var.h>
121 #include <net80211/ieee80211_ioctl.h>
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/in_var.h>
127 #include <netinet/ip.h>
132 #include <machine/md_var.h>
134 #include <dev/an/if_aironet_ieee.h>
135 #include <dev/an/if_anreg.h>
137 /* These are global because we need them in sys/pci/if_an_p.c. */
138 static void an_reset(struct an_softc *);
139 static int an_init_mpi350_desc(struct an_softc *);
140 static int an_ioctl(struct ifnet *, u_long, caddr_t);
141 static void an_init(void *);
142 static void an_init_locked(struct an_softc *);
143 static int an_init_tx_ring(struct an_softc *);
144 static void an_start(struct ifnet *);
145 static void an_start_locked(struct ifnet *);
146 static void an_watchdog(struct an_softc *);
147 static void an_rxeof(struct an_softc *);
148 static void an_txeof(struct an_softc *, int);
150 static void an_promisc(struct an_softc *, int);
151 static int an_cmd(struct an_softc *, int, int);
152 static int an_cmd_struct(struct an_softc *, struct an_command *,
154 static int an_read_record(struct an_softc *, struct an_ltv_gen *);
155 static int an_write_record(struct an_softc *, struct an_ltv_gen *);
156 static int an_read_data(struct an_softc *, int, int, caddr_t, int);
157 static int an_write_data(struct an_softc *, int, int, caddr_t, int);
158 static int an_seek(struct an_softc *, int, int, int);
159 static int an_alloc_nicmem(struct an_softc *, int, int *);
160 static int an_dma_malloc(struct an_softc *, bus_size_t, struct an_dma_alloc *,
162 static void an_dma_free(struct an_softc *, struct an_dma_alloc *);
163 static void an_dma_malloc_cb(void *, bus_dma_segment_t *, int, int);
164 static void an_stats_update(void *);
165 static void an_setdef(struct an_softc *, struct an_req *);
167 static void an_cache_store(struct an_softc *, struct ether_header *,
168 struct mbuf *, u_int8_t, u_int8_t);
171 /* function definitions for use with the Cisco's Linux configuration
175 static int readrids(struct ifnet*, struct aironet_ioctl*);
176 static int writerids(struct ifnet*, struct aironet_ioctl*);
177 static int flashcard(struct ifnet*, struct aironet_ioctl*);
179 static int cmdreset(struct ifnet *);
180 static int setflashmode(struct ifnet *);
181 static int flashgchar(struct ifnet *,int,int);
182 static int flashpchar(struct ifnet *,int,int);
183 static int flashputbuf(struct ifnet *);
184 static int flashrestart(struct ifnet *);
185 static int WaitBusy(struct ifnet *, int);
186 static int unstickbusy(struct ifnet *);
188 static void an_dump_record (struct an_softc *,struct an_ltv_gen *,
191 static int an_media_change (struct ifnet *);
192 static void an_media_status (struct ifnet *, struct ifmediareq *);
194 static int an_dump = 0;
195 static int an_cache_mode = 0;
201 static char an_conf[256];
202 static char an_conf_cache[256];
206 static SYSCTL_NODE(_hw, OID_AUTO, an, CTLFLAG_RD, 0,
207 "Wireless driver parameters");
209 /* XXX violate ethernet/netgraph callback hooks */
210 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
211 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
214 sysctl_an_dump(SYSCTL_HANDLER_ARGS)
223 strcpy(an_conf, "off");
226 strcpy(an_conf, "type");
229 strcpy(an_conf, "dump");
232 snprintf(an_conf, 5, "%x", an_dump);
236 error = sysctl_handle_string(oidp, an_conf, sizeof(an_conf), req);
238 if (strncmp(an_conf,"off", 3) == 0) {
241 if (strncmp(an_conf,"dump", 4) == 0) {
244 if (strncmp(an_conf,"type", 4) == 0) {
250 if ((*s >= '0') && (*s <= '9')) {
251 r = r * 16 + (*s - '0');
252 } else if ((*s >= 'a') && (*s <= 'f')) {
253 r = r * 16 + (*s - 'a' + 10);
261 printf("Sysctl changed for Aironet driver\n");
266 SYSCTL_PROC(_hw_an, OID_AUTO, an_dump, CTLTYPE_STRING | CTLFLAG_RW,
267 0, sizeof(an_conf), sysctl_an_dump, "A", "");
270 sysctl_an_cache_mode(SYSCTL_HANDLER_ARGS)
274 last = an_cache_mode;
276 switch (an_cache_mode) {
278 strcpy(an_conf_cache, "per");
281 strcpy(an_conf_cache, "raw");
284 strcpy(an_conf_cache, "dbm");
288 error = sysctl_handle_string(oidp, an_conf_cache,
289 sizeof(an_conf_cache), req);
291 if (strncmp(an_conf_cache,"dbm", 3) == 0) {
294 if (strncmp(an_conf_cache,"per", 3) == 0) {
297 if (strncmp(an_conf_cache,"raw", 3) == 0) {
304 SYSCTL_PROC(_hw_an, OID_AUTO, an_cache_mode, CTLTYPE_STRING | CTLFLAG_RW,
305 0, sizeof(an_conf_cache), sysctl_an_cache_mode, "A", "");
308 * Setup the lock for PCI attachment since it skips the an_probe
309 * function. We need to setup the lock in an_probe since some
310 * operations need the lock. So we might as well create the
314 an_pci_probe(device_t dev)
316 struct an_softc *sc = device_get_softc(dev);
318 mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
325 * We probe for an Aironet 4500/4800 card by attempting to
326 * read the default SSID list. On reset, the first entry in
327 * the SSID list will contain the name "tsunami." If we don't
328 * find this, then there's no card present.
331 an_probe(device_t dev)
333 struct an_softc *sc = device_get_softc(dev);
334 struct an_ltv_ssidlist_new ssid;
337 bzero((char *)&ssid, sizeof(ssid));
339 error = an_alloc_port(dev, 0, AN_IOSIZ);
343 /* can't do autoprobing */
344 if (rman_get_start(sc->port_res) == -1)
348 * We need to fake up a softc structure long enough
349 * to be able to issue commands and call some of the
352 ssid.an_len = sizeof(ssid);
353 ssid.an_type = AN_RID_SSIDLIST;
355 /* Make sure interrupts are disabled. */
357 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
358 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), 0xFFFF);
360 mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
365 if (an_cmd(sc, AN_CMD_READCFG, 0)) {
370 if (an_read_record(sc, (struct an_ltv_gen *)&ssid)) {
375 /* See if the ssid matches what we expect ... but doesn't have to */
376 if (strcmp(ssid.an_entry[0].an_ssid, AN_DEF_SSID)) {
384 mtx_destroy(&sc->an_mtx);
389 * Allocate a port resource with the given resource id.
392 an_alloc_port(device_t dev, int rid, int size)
394 struct an_softc *sc = device_get_softc(dev);
395 struct resource *res;
397 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
398 0ul, ~0ul, size, RF_ACTIVE);
409 * Allocate a memory resource with the given resource id.
411 int an_alloc_memory(device_t dev, int rid, int size)
413 struct an_softc *sc = device_get_softc(dev);
414 struct resource *res;
416 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
417 0ul, ~0ul, size, RF_ACTIVE);
429 * Allocate a auxilary memory resource with the given resource id.
431 int an_alloc_aux_memory(device_t dev, int rid, int size)
433 struct an_softc *sc = device_get_softc(dev);
434 struct resource *res;
436 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
437 0ul, ~0ul, size, RF_ACTIVE);
439 sc->mem_aux_rid = rid;
440 sc->mem_aux_res = res;
441 sc->mem_aux_used = size;
449 * Allocate an irq resource with the given resource id.
452 an_alloc_irq(device_t dev, int rid, int flags)
454 struct an_softc *sc = device_get_softc(dev);
455 struct resource *res;
457 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
458 (RF_ACTIVE | flags));
469 an_dma_malloc_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
471 bus_addr_t *paddr = (bus_addr_t*) arg;
472 *paddr = segs->ds_addr;
476 * Alloc DMA memory and set the pointer to it
479 an_dma_malloc(struct an_softc *sc, bus_size_t size, struct an_dma_alloc *dma,
484 r = bus_dmamap_create(sc->an_dtag, BUS_DMA_NOWAIT, &dma->an_dma_map);
488 r = bus_dmamem_alloc(sc->an_dtag, (void**) &dma->an_dma_vaddr,
489 BUS_DMA_NOWAIT, &dma->an_dma_map);
493 r = bus_dmamap_load(sc->an_dtag, dma->an_dma_map, dma->an_dma_vaddr,
497 mapflags | BUS_DMA_NOWAIT);
501 dma->an_dma_size = size;
505 bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
507 bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
509 bus_dmamap_destroy(sc->an_dtag, dma->an_dma_map);
510 dma->an_dma_map = NULL;
515 an_dma_free(struct an_softc *sc, struct an_dma_alloc *dma)
517 bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
518 bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
519 dma->an_dma_vaddr = 0;
520 bus_dmamap_destroy(sc->an_dtag, dma->an_dma_map);
524 * Release all resources
527 an_release_resources(device_t dev)
529 struct an_softc *sc = device_get_softc(dev);
533 bus_release_resource(dev, SYS_RES_IOPORT,
534 sc->port_rid, sc->port_res);
538 bus_release_resource(dev, SYS_RES_MEMORY,
539 sc->mem_rid, sc->mem_res);
542 if (sc->mem_aux_res) {
543 bus_release_resource(dev, SYS_RES_MEMORY,
544 sc->mem_aux_rid, sc->mem_aux_res);
548 bus_release_resource(dev, SYS_RES_IRQ,
549 sc->irq_rid, sc->irq_res);
552 if (sc->an_rid_buffer.an_dma_paddr) {
553 an_dma_free(sc, &sc->an_rid_buffer);
555 for (i = 0; i < AN_MAX_RX_DESC; i++)
556 if (sc->an_rx_buffer[i].an_dma_paddr) {
557 an_dma_free(sc, &sc->an_rx_buffer[i]);
559 for (i = 0; i < AN_MAX_TX_DESC; i++)
560 if (sc->an_tx_buffer[i].an_dma_paddr) {
561 an_dma_free(sc, &sc->an_tx_buffer[i]);
564 bus_dma_tag_destroy(sc->an_dtag);
570 an_init_mpi350_desc(struct an_softc *sc)
572 struct an_command cmd_struct;
573 struct an_reply reply;
574 struct an_card_rid_desc an_rid_desc;
575 struct an_card_rx_desc an_rx_desc;
576 struct an_card_tx_desc an_tx_desc;
580 if(!sc->an_rid_buffer.an_dma_paddr)
581 an_dma_malloc(sc, AN_RID_BUFFER_SIZE,
582 &sc->an_rid_buffer, 0);
583 for (i = 0; i < AN_MAX_RX_DESC; i++)
584 if(!sc->an_rx_buffer[i].an_dma_paddr)
585 an_dma_malloc(sc, AN_RX_BUFFER_SIZE,
586 &sc->an_rx_buffer[i], 0);
587 for (i = 0; i < AN_MAX_TX_DESC; i++)
588 if(!sc->an_tx_buffer[i].an_dma_paddr)
589 an_dma_malloc(sc, AN_TX_BUFFER_SIZE,
590 &sc->an_tx_buffer[i], 0);
593 * Allocate RX descriptor
595 bzero(&reply,sizeof(reply));
596 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
597 cmd_struct.an_parm0 = AN_DESCRIPTOR_RX;
598 cmd_struct.an_parm1 = AN_RX_DESC_OFFSET;
599 cmd_struct.an_parm2 = AN_MAX_RX_DESC;
600 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
601 if_printf(sc->an_ifp, "failed to allocate RX descriptor\n");
605 for (desc = 0; desc < AN_MAX_RX_DESC; desc++) {
606 bzero(&an_rx_desc, sizeof(an_rx_desc));
607 an_rx_desc.an_valid = 1;
608 an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
609 an_rx_desc.an_done = 0;
610 an_rx_desc.an_phys = sc->an_rx_buffer[desc].an_dma_paddr;
612 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
613 CSR_MEM_AUX_WRITE_4(sc, AN_RX_DESC_OFFSET
614 + (desc * sizeof(an_rx_desc))
616 ((u_int32_t *)(void *)&an_rx_desc)[i]);
620 * Allocate TX descriptor
623 bzero(&reply,sizeof(reply));
624 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
625 cmd_struct.an_parm0 = AN_DESCRIPTOR_TX;
626 cmd_struct.an_parm1 = AN_TX_DESC_OFFSET;
627 cmd_struct.an_parm2 = AN_MAX_TX_DESC;
628 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
629 if_printf(sc->an_ifp, "failed to allocate TX descriptor\n");
633 for (desc = 0; desc < AN_MAX_TX_DESC; desc++) {
634 bzero(&an_tx_desc, sizeof(an_tx_desc));
635 an_tx_desc.an_offset = 0;
636 an_tx_desc.an_eoc = 0;
637 an_tx_desc.an_valid = 0;
638 an_tx_desc.an_len = 0;
639 an_tx_desc.an_phys = sc->an_tx_buffer[desc].an_dma_paddr;
641 for (i = 0; i < sizeof(an_tx_desc) / 4; i++)
642 CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
643 + (desc * sizeof(an_tx_desc))
645 ((u_int32_t *)(void *)&an_tx_desc)[i]);
649 * Allocate RID descriptor
652 bzero(&reply,sizeof(reply));
653 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
654 cmd_struct.an_parm0 = AN_DESCRIPTOR_HOSTRW;
655 cmd_struct.an_parm1 = AN_HOST_DESC_OFFSET;
656 cmd_struct.an_parm2 = 1;
657 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
658 if_printf(sc->an_ifp, "failed to allocate host descriptor\n");
662 bzero(&an_rid_desc, sizeof(an_rid_desc));
663 an_rid_desc.an_valid = 1;
664 an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
665 an_rid_desc.an_rid = 0;
666 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
668 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
669 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
670 ((u_int32_t *)(void *)&an_rid_desc)[i]);
676 an_attach(struct an_softc *sc, int flags)
683 ifp = sc->an_ifp = if_alloc(IFT_ETHER);
685 device_printf(sc->an_dev, "can not if_alloc()\n");
690 sc->an_associated = 0;
692 sc->an_was_monitor = 0;
693 sc->an_flash_buffer = NULL;
699 error = an_init_mpi350_desc(sc);
704 /* Load factory config */
705 if (an_cmd(sc, AN_CMD_READCFG, 0)) {
706 device_printf(sc->an_dev, "failed to load config data\n");
710 /* Read the current configuration */
711 sc->an_config.an_type = AN_RID_GENCONFIG;
712 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
713 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
714 device_printf(sc->an_dev, "read record failed\n");
718 /* Read the card capabilities */
719 sc->an_caps.an_type = AN_RID_CAPABILITIES;
720 sc->an_caps.an_len = sizeof(struct an_ltv_caps);
721 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_caps)) {
722 device_printf(sc->an_dev, "read record failed\n");
727 sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
728 sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
729 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
730 device_printf(sc->an_dev, "read record failed\n");
735 sc->an_aplist.an_type = AN_RID_APLIST;
736 sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
737 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
738 device_printf(sc->an_dev, "read record failed\n");
743 /* Read the RSSI <-> dBm map */
744 sc->an_have_rssimap = 0;
745 if (sc->an_caps.an_softcaps & 8) {
746 sc->an_rssimap.an_type = AN_RID_RSSI_MAP;
747 sc->an_rssimap.an_len = sizeof(struct an_ltv_rssi_map);
748 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_rssimap)) {
749 device_printf(sc->an_dev,
750 "unable to get RSSI <-> dBM map\n");
752 device_printf(sc->an_dev, "got RSSI <-> dBM map\n");
753 sc->an_have_rssimap = 1;
756 device_printf(sc->an_dev, "no RSSI <-> dBM map\n");
762 if_initname(ifp, device_get_name(sc->an_dev),
763 device_get_unit(sc->an_dev));
764 ifp->if_mtu = ETHERMTU;
765 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
766 ifp->if_ioctl = an_ioctl;
767 ifp->if_start = an_start;
768 ifp->if_init = an_init;
769 ifp->if_baudrate = 10000000;
770 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
771 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
772 IFQ_SET_READY(&ifp->if_snd);
774 bzero(sc->an_config.an_nodename, sizeof(sc->an_config.an_nodename));
775 bcopy(AN_DEFAULT_NODENAME, sc->an_config.an_nodename,
776 sizeof(AN_DEFAULT_NODENAME) - 1);
778 bzero(sc->an_ssidlist.an_entry[0].an_ssid,
779 sizeof(sc->an_ssidlist.an_entry[0].an_ssid));
780 bcopy(AN_DEFAULT_NETNAME, sc->an_ssidlist.an_entry[0].an_ssid,
781 sizeof(AN_DEFAULT_NETNAME) - 1);
782 sc->an_ssidlist.an_entry[0].an_len = strlen(AN_DEFAULT_NETNAME);
784 sc->an_config.an_opmode =
785 AN_OPMODE_INFRASTRUCTURE_STATION;
788 bzero((char *)&sc->an_stats, sizeof(sc->an_stats));
792 ifmedia_init(&sc->an_ifmedia, 0, an_media_change, an_media_status);
793 if_printf(ifp, "supported rates: ");
794 #define ADD(s, o) ifmedia_add(&sc->an_ifmedia, \
795 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
797 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
798 for (i = 0; i < nrate; i++) {
799 r = sc->an_caps.an_rates[i];
800 mword = ieee80211_rate2media(NULL, r, IEEE80211_MODE_AUTO);
803 printf("%s%d%sMbps", (i != 0 ? " " : ""),
804 (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
806 ADD(mword, IFM_IEEE80211_ADHOC);
809 ifmedia_set(&sc->an_ifmedia, IFM_MAKEWORD(IFM_IEEE80211,
814 * Call MI attach routine.
817 ether_ifattach(ifp, sc->an_caps.an_oemaddr);
818 callout_init_mtx(&sc->an_stat_ch, &sc->an_mtx, 0);
823 mtx_destroy(&sc->an_mtx);
830 an_detach(device_t dev)
832 struct an_softc *sc = device_get_softc(dev);
833 struct ifnet *ifp = sc->an_ifp;
836 device_printf(dev,"already unloaded\n");
842 ifmedia_removeall(&sc->an_ifmedia);
843 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
846 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
847 callout_drain(&sc->an_stat_ch);
849 an_release_resources(dev);
850 mtx_destroy(&sc->an_mtx);
855 an_rxeof(struct an_softc *sc)
858 struct ether_header *eh;
859 struct ieee80211_frame *ih;
860 struct an_rxframe rx_frame;
861 struct an_rxframe_802_3 rx_frame_802_3;
863 int len, id, error = 0, i, count = 0;
864 int ieee80211_header_len;
867 struct an_card_rx_desc an_rx_desc;
875 id = CSR_READ_2(sc, AN_RX_FID);
877 if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
878 /* read raw 802.11 packet */
879 bpf_buf = sc->buf_802_11;
882 if (an_read_data(sc, id, 0x0, (caddr_t)&rx_frame,
889 * skip beacon by default since this increases the
893 if (!(sc->an_monitor & AN_MONITOR_INCLUDE_BEACON) &&
894 (rx_frame.an_frame_ctl &
895 IEEE80211_FC0_SUBTYPE_BEACON)) {
899 if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
900 len = rx_frame.an_rx_payload_len
902 /* Check for insane frame length */
903 if (len > sizeof(sc->buf_802_11)) {
904 if_printf(ifp, "oversized packet "
905 "received (%d, %d)\n",
911 bcopy((char *)&rx_frame,
912 bpf_buf, sizeof(rx_frame));
914 error = an_read_data(sc, id, sizeof(rx_frame),
915 (caddr_t)bpf_buf+sizeof(rx_frame),
916 rx_frame.an_rx_payload_len);
918 fc1=rx_frame.an_frame_ctl >> 8;
919 ieee80211_header_len =
920 sizeof(struct ieee80211_frame);
921 if ((fc1 & IEEE80211_FC1_DIR_TODS) &&
922 (fc1 & IEEE80211_FC1_DIR_FROMDS)) {
923 ieee80211_header_len += ETHER_ADDR_LEN;
926 len = rx_frame.an_rx_payload_len
927 + ieee80211_header_len;
928 /* Check for insane frame length */
929 if (len > sizeof(sc->buf_802_11)) {
930 if_printf(ifp, "oversized packet "
931 "received (%d, %d)\n",
937 ih = (struct ieee80211_frame *)bpf_buf;
939 bcopy((char *)&rx_frame.an_frame_ctl,
940 (char *)ih, ieee80211_header_len);
942 error = an_read_data(sc, id, sizeof(rx_frame) +
944 (caddr_t)ih +ieee80211_header_len,
945 rx_frame.an_rx_payload_len);
947 /* dump raw 802.11 packet to bpf and skip ip stack */
948 BPF_TAP(ifp, bpf_buf, len);
950 MGETHDR(m, M_NOWAIT, MT_DATA);
956 if (!(m->m_flags & M_EXT)) {
961 m->m_pkthdr.rcvif = ifp;
962 /* Read Ethernet encapsulated packet */
965 /* Read NIC frame header */
966 if (an_read_data(sc, id, 0, (caddr_t)&rx_frame,
973 /* Read in the 802_3 frame header */
974 if (an_read_data(sc, id, 0x34,
975 (caddr_t)&rx_frame_802_3,
976 sizeof(rx_frame_802_3))) {
981 if (rx_frame_802_3.an_rx_802_3_status != 0) {
986 /* Check for insane frame length */
987 len = rx_frame_802_3.an_rx_802_3_payload_len;
988 if (len > sizeof(sc->buf_802_11)) {
990 if_printf(ifp, "oversized packet "
991 "received (%d, %d)\n",
996 m->m_pkthdr.len = m->m_len =
997 rx_frame_802_3.an_rx_802_3_payload_len + 12;
999 eh = mtod(m, struct ether_header *);
1001 bcopy((char *)&rx_frame_802_3.an_rx_dst_addr,
1002 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
1003 bcopy((char *)&rx_frame_802_3.an_rx_src_addr,
1004 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
1006 /* in mbuf header type is just before payload */
1007 error = an_read_data(sc, id, 0x44,
1008 (caddr_t)&(eh->ether_type),
1009 rx_frame_802_3.an_rx_802_3_payload_len);
1018 /* Receive packet. */
1020 an_cache_store(sc, eh, m,
1021 rx_frame.an_rx_signal_strength,
1025 (*ifp->if_input)(ifp, m);
1029 } else { /* MPI-350 */
1030 for (count = 0; count < AN_MAX_RX_DESC; count++){
1031 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1032 ((u_int32_t *)(void *)&an_rx_desc)[i]
1033 = CSR_MEM_AUX_READ_4(sc,
1035 + (count * sizeof(an_rx_desc))
1038 if (an_rx_desc.an_done && !an_rx_desc.an_valid) {
1039 buf = sc->an_rx_buffer[count].an_dma_vaddr;
1041 MGETHDR(m, M_NOWAIT, MT_DATA);
1046 MCLGET(m, M_NOWAIT);
1047 if (!(m->m_flags & M_EXT)) {
1052 m->m_pkthdr.rcvif = ifp;
1053 /* Read Ethernet encapsulated packet */
1056 * No ANCACHE support since we just get back
1057 * an Ethernet packet no 802.11 info
1061 /* Read NIC frame header */
1062 bcopy(buf, (caddr_t)&rx_frame,
1066 /* Check for insane frame length */
1067 len = an_rx_desc.an_len + 12;
1068 if (len > MCLBYTES) {
1070 if_printf(ifp, "oversized packet "
1071 "received (%d, %d)\n",
1077 m->m_pkthdr.len = m->m_len =
1078 an_rx_desc.an_len + 12;
1080 eh = mtod(m, struct ether_header *);
1082 bcopy(buf, (char *)eh,
1087 /* Receive packet. */
1090 an_cache_store(sc, eh, m,
1091 rx_frame.an_rx_signal_strength,
1096 (*ifp->if_input)(ifp, m);
1099 an_rx_desc.an_valid = 1;
1100 an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
1101 an_rx_desc.an_done = 0;
1102 an_rx_desc.an_phys =
1103 sc->an_rx_buffer[count].an_dma_paddr;
1105 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1106 CSR_MEM_AUX_WRITE_4(sc,
1108 + (count * sizeof(an_rx_desc))
1110 ((u_int32_t *)(void *)&an_rx_desc)[i]);
1113 if_printf(ifp, "Didn't get valid RX packet "
1116 an_rx_desc.an_valid, an_rx_desc.an_len);
1123 an_txeof(struct an_softc *sc, int status)
1132 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1135 id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1137 if (status & AN_EV_TX_EXC) {
1142 for (i = 0; i < AN_TX_RING_CNT; i++) {
1143 if (id == sc->an_rdata.an_tx_ring[i]) {
1144 sc->an_rdata.an_tx_ring[i] = 0;
1149 AN_INC(sc->an_rdata.an_tx_cons, AN_TX_RING_CNT);
1150 } else { /* MPI 350 */
1151 id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1152 if (!sc->an_rdata.an_tx_empty){
1153 if (status & AN_EV_TX_EXC) {
1157 AN_INC(sc->an_rdata.an_tx_cons, AN_MAX_TX_DESC);
1158 if (sc->an_rdata.an_tx_prod ==
1159 sc->an_rdata.an_tx_cons)
1160 sc->an_rdata.an_tx_empty = 1;
1168 * We abuse the stats updater to check the current NIC status. This
1169 * is important because we don't want to allow transmissions until
1170 * the NIC has synchronized to the current cell (either as the master
1171 * in an ad-hoc group, or as a station connected to an access point).
1173 * Note that this function will be called via callout(9) with a lock held.
1176 an_stats_update(void *xsc)
1178 struct an_softc *sc;
1184 if (sc->an_timer > 0 && --sc->an_timer == 0)
1187 sc->an_status.an_type = AN_RID_STATUS;
1188 sc->an_status.an_len = sizeof(struct an_ltv_status);
1189 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_status))
1192 if (sc->an_status.an_opmode & AN_STATUS_OPMODE_IN_SYNC)
1193 sc->an_associated = 1;
1195 sc->an_associated = 0;
1197 /* Don't do this while we're transmitting */
1198 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1199 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1203 sc->an_stats.an_len = sizeof(struct an_ltv_stats);
1204 sc->an_stats.an_type = AN_RID_32BITS_CUM;
1205 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_stats.an_len))
1208 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1216 struct an_softc *sc;
1220 sc = (struct an_softc*)xsc;
1231 /* Disable interrupts. */
1232 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
1234 status = CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350));
1235 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), ~AN_INTRS(sc->mpi350));
1237 if (status & AN_EV_MIC) {
1238 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_MIC);
1241 if (status & AN_EV_LINKSTAT) {
1242 if (CSR_READ_2(sc, AN_LINKSTAT(sc->mpi350))
1243 == AN_LINKSTAT_ASSOCIATED)
1244 sc->an_associated = 1;
1246 sc->an_associated = 0;
1247 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_LINKSTAT);
1250 if (status & AN_EV_RX) {
1252 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_RX);
1255 if (sc->mpi350 && status & AN_EV_TX_CPY) {
1256 an_txeof(sc, status);
1257 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_CPY);
1260 if (status & AN_EV_TX) {
1261 an_txeof(sc, status);
1262 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX);
1265 if (status & AN_EV_TX_EXC) {
1266 an_txeof(sc, status);
1267 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_EXC);
1270 if (status & AN_EV_ALLOC)
1271 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1273 /* Re-enable interrupts. */
1274 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
1276 if ((ifp->if_flags & IFF_UP) && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1277 an_start_locked(ifp);
1286 an_cmd_struct(struct an_softc *sc, struct an_command *cmd,
1287 struct an_reply *reply)
1292 for (i = 0; i != AN_TIMEOUT; i++) {
1293 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
1299 if( i == AN_TIMEOUT) {
1304 CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), cmd->an_parm0);
1305 CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), cmd->an_parm1);
1306 CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), cmd->an_parm2);
1307 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd->an_cmd);
1309 for (i = 0; i < AN_TIMEOUT; i++) {
1310 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1315 reply->an_resp0 = CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1316 reply->an_resp1 = CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1317 reply->an_resp2 = CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1318 reply->an_status = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1320 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1321 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
1322 AN_EV_CLR_STUCK_BUSY);
1324 /* Ack the command */
1325 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1327 if (i == AN_TIMEOUT)
1334 an_cmd(struct an_softc *sc, int cmd, int val)
1339 CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), val);
1340 CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), 0);
1341 CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), 0);
1342 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1344 for (i = 0; i < AN_TIMEOUT; i++) {
1345 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1348 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) == cmd)
1349 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1353 for (i = 0; i < AN_TIMEOUT; i++) {
1354 CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1355 CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1356 CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1357 s = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1358 if ((s & AN_STAT_CMD_CODE) == (cmd & AN_STAT_CMD_CODE))
1362 /* Ack the command */
1363 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1365 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1366 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CLR_STUCK_BUSY);
1368 if (i == AN_TIMEOUT)
1375 * This reset sequence may look a little strange, but this is the
1376 * most reliable method I've found to really kick the NIC in the
1377 * head and force it to reboot correctly.
1380 an_reset(struct an_softc *sc)
1386 an_cmd(sc, AN_CMD_ENABLE, 0);
1387 an_cmd(sc, AN_CMD_FW_RESTART, 0);
1388 an_cmd(sc, AN_CMD_NOOP2, 0);
1390 if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT)
1391 if_printf(sc->an_ifp, "reset failed\n");
1393 an_cmd(sc, AN_CMD_DISABLE, 0);
1399 * Read an LTV record from the NIC.
1402 an_read_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1404 struct an_ltv_gen *an_ltv;
1405 struct an_card_rid_desc an_rid_desc;
1406 struct an_command cmd;
1407 struct an_reply reply;
1414 if (ltv->an_len < 4 || ltv->an_type == 0)
1419 /* Tell the NIC to enter record read mode. */
1420 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type)) {
1421 if_printf(ifp, "RID access failed\n");
1425 /* Seek to the record. */
1426 if (an_seek(sc, ltv->an_type, 0, AN_BAP1)) {
1427 if_printf(ifp, "seek to record failed\n");
1432 * Read the length and record type and make sure they
1433 * match what we expect (this verifies that we have enough
1434 * room to hold all of the returned data).
1435 * Length includes type but not length.
1437 len = CSR_READ_2(sc, AN_DATA1);
1438 if (len > (ltv->an_len - 2)) {
1439 if_printf(ifp, "record length mismatch -- expected %d, "
1440 "got %d for Rid %x\n",
1441 ltv->an_len - 2, len, ltv->an_type);
1442 len = ltv->an_len - 2;
1444 ltv->an_len = len + 2;
1447 /* Now read the data. */
1448 len -= 2; /* skip the type */
1450 for (i = len; i > 1; i -= 2)
1451 *ptr++ = CSR_READ_2(sc, AN_DATA1);
1453 ptr2 = (u_int8_t *)ptr;
1454 *ptr2 = CSR_READ_1(sc, AN_DATA1);
1456 } else { /* MPI-350 */
1457 if (!sc->an_rid_buffer.an_dma_vaddr)
1459 an_rid_desc.an_valid = 1;
1460 an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
1461 an_rid_desc.an_rid = 0;
1462 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1463 bzero(sc->an_rid_buffer.an_dma_vaddr, AN_RID_BUFFER_SIZE);
1465 bzero(&cmd, sizeof(cmd));
1466 bzero(&reply, sizeof(reply));
1467 cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_READ;
1468 cmd.an_parm0 = ltv->an_type;
1470 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1471 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1472 ((u_int32_t *)(void *)&an_rid_desc)[i]);
1474 if (an_cmd_struct(sc, &cmd, &reply)
1475 || reply.an_status & AN_CMD_QUAL_MASK) {
1476 if_printf(ifp, "failed to read RID %x %x %x %x %x, %d\n",
1486 an_ltv = (struct an_ltv_gen *)sc->an_rid_buffer.an_dma_vaddr;
1487 if (an_ltv->an_len + 2 < an_rid_desc.an_len) {
1488 an_rid_desc.an_len = an_ltv->an_len;
1491 len = an_rid_desc.an_len;
1492 if (len > (ltv->an_len - 2)) {
1493 if_printf(ifp, "record length mismatch -- expected %d, "
1494 "got %d for Rid %x\n",
1495 ltv->an_len - 2, len, ltv->an_type);
1496 len = ltv->an_len - 2;
1498 ltv->an_len = len + 2;
1500 bcopy(&an_ltv->an_type,
1506 an_dump_record(sc, ltv, "Read");
1512 * Same as read, except we inject data instead of reading it.
1515 an_write_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1517 struct an_card_rid_desc an_rid_desc;
1518 struct an_command cmd;
1519 struct an_reply reply;
1526 an_dump_record(sc, ltv, "Write");
1529 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type))
1532 if (an_seek(sc, ltv->an_type, 0, AN_BAP1))
1536 * Length includes type but not length.
1538 len = ltv->an_len - 2;
1539 CSR_WRITE_2(sc, AN_DATA1, len);
1541 len -= 2; /* skip the type */
1543 for (i = len; i > 1; i -= 2)
1544 CSR_WRITE_2(sc, AN_DATA1, *ptr++);
1546 ptr2 = (u_int8_t *)ptr;
1547 CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1550 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_WRITE, ltv->an_type))
1555 for (i = 0; i != AN_TIMEOUT; i++) {
1556 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350))
1562 if (i == AN_TIMEOUT) {
1566 an_rid_desc.an_valid = 1;
1567 an_rid_desc.an_len = ltv->an_len - 2;
1568 an_rid_desc.an_rid = ltv->an_type;
1569 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1571 bcopy(<v->an_type, sc->an_rid_buffer.an_dma_vaddr,
1572 an_rid_desc.an_len);
1574 bzero(&cmd,sizeof(cmd));
1575 bzero(&reply,sizeof(reply));
1576 cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_WRITE;
1577 cmd.an_parm0 = ltv->an_type;
1579 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1580 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1581 ((u_int32_t *)(void *)&an_rid_desc)[i]);
1585 if ((i = an_cmd_struct(sc, &cmd, &reply))) {
1586 if_printf(sc->an_ifp,
1587 "failed to write RID 1 %x %x %x %x %x, %d\n",
1598 if (reply.an_status & AN_CMD_QUAL_MASK) {
1599 if_printf(sc->an_ifp,
1600 "failed to write RID 2 %x %x %x %x %x, %d\n",
1616 an_dump_record(struct an_softc *sc, struct an_ltv_gen *ltv, char *string)
1624 len = ltv->an_len - 4;
1625 if_printf(sc->an_ifp, "RID %4x, Length %4d, Mode %s\n",
1626 ltv->an_type, ltv->an_len - 4, string);
1628 if (an_dump == 1 || (an_dump == ltv->an_type)) {
1629 if_printf(sc->an_ifp, "\t");
1630 bzero(buf,sizeof(buf));
1632 ptr2 = (u_int8_t *)<v->an_val;
1633 for (i = len; i > 0; i--) {
1634 printf("%02x ", *ptr2);
1641 if (++count == 16) {
1644 if_printf(sc->an_ifp, "\t");
1645 bzero(buf,sizeof(buf));
1648 for (; count != 16; count++) {
1651 printf(" %s\n",buf);
1656 an_seek(struct an_softc *sc, int id, int off, int chan)
1671 if_printf(sc->an_ifp, "invalid data path: %x\n", chan);
1675 CSR_WRITE_2(sc, selreg, id);
1676 CSR_WRITE_2(sc, offreg, off);
1678 for (i = 0; i < AN_TIMEOUT; i++) {
1679 if (!(CSR_READ_2(sc, offreg) & (AN_OFF_BUSY|AN_OFF_ERR)))
1683 if (i == AN_TIMEOUT)
1690 an_read_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1697 if (an_seek(sc, id, off, AN_BAP1))
1701 ptr = (u_int16_t *)buf;
1702 for (i = len; i > 1; i -= 2)
1703 *ptr++ = CSR_READ_2(sc, AN_DATA1);
1705 ptr2 = (u_int8_t *)ptr;
1706 *ptr2 = CSR_READ_1(sc, AN_DATA1);
1713 an_write_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1720 if (an_seek(sc, id, off, AN_BAP0))
1724 ptr = (u_int16_t *)buf;
1725 for (i = len; i > 1; i -= 2)
1726 CSR_WRITE_2(sc, AN_DATA0, *ptr++);
1728 ptr2 = (u_int8_t *)ptr;
1729 CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1736 * Allocate a region of memory inside the NIC and zero
1740 an_alloc_nicmem(struct an_softc *sc, int len, int *id)
1744 if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1745 if_printf(sc->an_ifp, "failed to allocate %d bytes on NIC\n",
1750 for (i = 0; i < AN_TIMEOUT; i++) {
1751 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_ALLOC)
1755 if (i == AN_TIMEOUT)
1758 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1759 *id = CSR_READ_2(sc, AN_ALLOC_FID);
1761 if (an_seek(sc, *id, 0, AN_BAP0))
1764 for (i = 0; i < len / 2; i++)
1765 CSR_WRITE_2(sc, AN_DATA0, 0);
1771 an_setdef(struct an_softc *sc, struct an_req *areq)
1774 struct an_ltv_genconfig *cfg;
1775 struct an_ltv_ssidlist_new *ssid;
1776 struct an_ltv_aplist *ap;
1777 struct an_ltv_gen *sp;
1782 switch (areq->an_type) {
1783 case AN_RID_GENCONFIG:
1784 cfg = (struct an_ltv_genconfig *)areq;
1786 bcopy((char *)&cfg->an_macaddr, IF_LLADDR(sc->an_ifp),
1789 bcopy((char *)cfg, (char *)&sc->an_config,
1790 sizeof(struct an_ltv_genconfig));
1792 case AN_RID_SSIDLIST:
1793 ssid = (struct an_ltv_ssidlist_new *)areq;
1794 bcopy((char *)ssid, (char *)&sc->an_ssidlist,
1795 sizeof(struct an_ltv_ssidlist_new));
1798 ap = (struct an_ltv_aplist *)areq;
1799 bcopy((char *)ap, (char *)&sc->an_aplist,
1800 sizeof(struct an_ltv_aplist));
1802 case AN_RID_TX_SPEED:
1803 sp = (struct an_ltv_gen *)areq;
1804 sc->an_tx_rate = sp->an_val;
1806 /* Read the current configuration */
1807 sc->an_config.an_type = AN_RID_GENCONFIG;
1808 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1809 an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
1810 cfg = &sc->an_config;
1812 /* clear other rates and set the only one we want */
1813 bzero(cfg->an_rates, sizeof(cfg->an_rates));
1814 cfg->an_rates[0] = sc->an_tx_rate;
1816 /* Save the new rate */
1817 sc->an_config.an_type = AN_RID_GENCONFIG;
1818 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1820 case AN_RID_WEP_TEMP:
1821 /* Cache the temp keys */
1823 &sc->an_temp_keys[((struct an_ltv_key *)areq)->kindex],
1824 sizeof(struct an_ltv_key));
1825 case AN_RID_WEP_PERM:
1826 case AN_RID_LEAPUSERNAME:
1827 case AN_RID_LEAPPASSWORD:
1830 /* Disable the MAC. */
1831 an_cmd(sc, AN_CMD_DISABLE, 0);
1834 an_write_record(sc, (struct an_ltv_gen *)areq);
1836 /* Turn the MAC back on. */
1837 an_cmd(sc, AN_CMD_ENABLE, 0);
1840 case AN_RID_MONITOR_MODE:
1841 cfg = (struct an_ltv_genconfig *)areq;
1843 if (ng_ether_detach_p != NULL)
1844 (*ng_ether_detach_p) (ifp);
1845 sc->an_monitor = cfg->an_len;
1847 if (sc->an_monitor & AN_MONITOR) {
1848 if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
1849 bpfattach(ifp, DLT_AIRONET_HEADER,
1850 sizeof(struct ether_header));
1852 bpfattach(ifp, DLT_IEEE802_11,
1853 sizeof(struct ether_header));
1856 bpfattach(ifp, DLT_EN10MB,
1857 sizeof(struct ether_header));
1858 if (ng_ether_attach_p != NULL)
1859 (*ng_ether_attach_p) (ifp);
1863 if_printf(ifp, "unknown RID: %x\n", areq->an_type);
1868 /* Reinitialize the card. */
1876 * Derived from Linux driver to enable promiscious mode.
1880 an_promisc(struct an_softc *sc, int promisc)
1883 if (sc->an_was_monitor) {
1886 an_init_mpi350_desc(sc);
1888 if (sc->an_monitor || sc->an_was_monitor)
1891 sc->an_was_monitor = sc->an_monitor;
1892 an_cmd(sc, AN_CMD_SET_MODE, promisc ? 0xffff : 0);
1898 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1903 struct an_softc *sc;
1905 struct thread *td = curthread;
1906 struct ieee80211req *ireq;
1907 struct ieee80211_channel ch;
1908 u_int8_t tmpstr[IEEE80211_NWID_LEN*2];
1910 struct an_ltv_genconfig *config;
1911 struct an_ltv_key *key;
1912 struct an_ltv_status *status;
1913 struct an_ltv_ssidlist_new *ssids;
1915 struct aironet_ioctl l_ioctl;
1918 ifr = (struct ifreq *)data;
1919 ireq = (struct ieee80211req *)data;
1921 config = (struct an_ltv_genconfig *)&sc->areq;
1922 key = (struct an_ltv_key *)&sc->areq;
1923 status = (struct an_ltv_status *)&sc->areq;
1924 ssids = (struct an_ltv_ssidlist_new *)&sc->areq;
1934 if (ifp->if_flags & IFF_UP) {
1935 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1936 ifp->if_flags & IFF_PROMISC &&
1937 !(sc->an_if_flags & IFF_PROMISC)) {
1939 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1940 !(ifp->if_flags & IFF_PROMISC) &&
1941 sc->an_if_flags & IFF_PROMISC) {
1946 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1949 sc->an_if_flags = ifp->if_flags;
1955 error = ifmedia_ioctl(ifp, ifr, &sc->an_ifmedia, command);
1959 /* The Aironet has no multicast filter. */
1963 error = copyin(ifr->ifr_data, &sc->areq, sizeof(sc->areq));
1968 if (sc->areq.an_type == AN_RID_ZERO_CACHE) {
1969 error = priv_check(td, PRIV_DRIVER);
1972 sc->an_sigitems = sc->an_nextitem = 0;
1974 } else if (sc->areq.an_type == AN_RID_READ_CACHE) {
1975 char *pt = (char *)&sc->areq.an_val;
1976 bcopy((char *)&sc->an_sigitems, (char *)pt,
1979 sc->areq.an_len = sizeof(int) / 2;
1980 bcopy((char *)&sc->an_sigcache, (char *)pt,
1981 sizeof(struct an_sigcache) * sc->an_sigitems);
1982 sc->areq.an_len += ((sizeof(struct an_sigcache) *
1983 sc->an_sigitems) / 2) + 1;
1986 if (an_read_record(sc, (struct an_ltv_gen *)&sc->areq)) {
1992 error = copyout(&sc->areq, ifr->ifr_data, sizeof(sc->areq));
1995 if ((error = priv_check(td, PRIV_DRIVER)))
1998 error = copyin(ifr->ifr_data, &sc->areq, sizeof(sc->areq));
2001 an_setdef(sc, &sc->areq);
2004 case SIOCGPRIVATE_0: /* used by Cisco client utility */
2005 if ((error = priv_check(td, PRIV_DRIVER)))
2007 error = copyin(ifr->ifr_data, &l_ioctl, sizeof(l_ioctl));
2010 mode = l_ioctl.command;
2013 if (mode >= AIROGCAP && mode <= AIROGSTATSD32) {
2014 error = readrids(ifp, &l_ioctl);
2015 } else if (mode >= AIROPCAP && mode <= AIROPLEAPUSR) {
2016 error = writerids(ifp, &l_ioctl);
2017 } else if (mode >= AIROFLSHRST && mode <= AIRORESTART) {
2018 error = flashcard(ifp, &l_ioctl);
2024 /* copy out the updated command info */
2025 error = copyout(&l_ioctl, ifr->ifr_data, sizeof(l_ioctl));
2028 case SIOCGPRIVATE_1: /* used by Cisco client utility */
2029 if ((error = priv_check(td, PRIV_DRIVER)))
2031 error = copyin(ifr->ifr_data, &l_ioctl, sizeof(l_ioctl));
2034 l_ioctl.command = 0;
2036 (void) copyout(&error, l_ioctl.data, sizeof(error));
2040 sc->areq.an_len = sizeof(sc->areq);
2041 /* was that a good idea DJA we are doing a short-cut */
2042 switch (ireq->i_type) {
2043 case IEEE80211_IOC_SSID:
2045 if (ireq->i_val == -1) {
2046 sc->areq.an_type = AN_RID_STATUS;
2047 if (an_read_record(sc,
2048 (struct an_ltv_gen *)&sc->areq)) {
2053 len = status->an_ssidlen;
2054 tmpptr = status->an_ssid;
2055 } else if (ireq->i_val >= 0) {
2056 sc->areq.an_type = AN_RID_SSIDLIST;
2057 if (an_read_record(sc,
2058 (struct an_ltv_gen *)&sc->areq)) {
2063 max = (sc->areq.an_len - 4)
2064 / sizeof(struct an_ltv_ssid_entry);
2065 if ( max > MAX_SSIDS ) {
2066 printf("To many SSIDs only using "
2071 if (ireq->i_val > max) {
2076 len = ssids->an_entry[ireq->i_val].an_len;
2077 tmpptr = ssids->an_entry[ireq->i_val].an_ssid;
2084 if (len > IEEE80211_NWID_LEN) {
2091 bzero(tmpstr, IEEE80211_NWID_LEN);
2092 bcopy(tmpptr, tmpstr, len);
2093 error = copyout(tmpstr, ireq->i_data,
2094 IEEE80211_NWID_LEN);
2096 case IEEE80211_IOC_NUMSSIDS:
2098 sc->areq.an_len = sizeof(sc->areq);
2099 sc->areq.an_type = AN_RID_SSIDLIST;
2100 if (an_read_record(sc,
2101 (struct an_ltv_gen *)&sc->areq)) {
2106 max = (sc->areq.an_len - 4)
2107 / sizeof(struct an_ltv_ssid_entry);
2109 if ( max > MAX_SSIDS ) {
2110 printf("To many SSIDs only using "
2117 case IEEE80211_IOC_WEP:
2119 sc->areq.an_type = AN_RID_ACTUALCFG;
2120 if (an_read_record(sc,
2121 (struct an_ltv_gen *)&sc->areq)) {
2127 if (config->an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE) {
2128 if (config->an_authtype &
2129 AN_AUTHTYPE_ALLOW_UNENCRYPTED)
2130 ireq->i_val = IEEE80211_WEP_MIXED;
2132 ireq->i_val = IEEE80211_WEP_ON;
2134 ireq->i_val = IEEE80211_WEP_OFF;
2137 case IEEE80211_IOC_WEPKEY:
2139 * XXX: I'm not entierly convinced this is
2140 * correct, but it's what is implemented in
2141 * ancontrol so it will have to do until we get
2142 * access to actual Cisco code.
2144 if (ireq->i_val < 0 || ireq->i_val > 8) {
2149 if (ireq->i_val < 5) {
2151 sc->areq.an_type = AN_RID_WEP_TEMP;
2152 for (i = 0; i < 5; i++) {
2153 if (an_read_record(sc,
2154 (struct an_ltv_gen *)&sc->areq)) {
2158 if (key->kindex == 0xffff)
2160 if (key->kindex == ireq->i_val)
2162 /* Required to get next entry */
2163 sc->areq.an_type = AN_RID_WEP_PERM;
2170 /* We aren't allowed to read the value of the
2171 * key from the card so we just output zeros
2172 * like we would if we could read the card, but
2173 * denied the user access.
2177 error = copyout(tmpstr, ireq->i_data, len);
2179 case IEEE80211_IOC_NUMWEPKEYS:
2180 ireq->i_val = 9; /* include home key */
2182 case IEEE80211_IOC_WEPTXKEY:
2184 * For some strange reason, you have to read all
2185 * keys before you can read the txkey.
2188 sc->areq.an_type = AN_RID_WEP_TEMP;
2189 for (i = 0; i < 5; i++) {
2190 if (an_read_record(sc,
2191 (struct an_ltv_gen *) &sc->areq)) {
2195 if (key->kindex == 0xffff) {
2198 /* Required to get next entry */
2199 sc->areq.an_type = AN_RID_WEP_PERM;
2206 sc->areq.an_type = AN_RID_WEP_PERM;
2207 key->kindex = 0xffff;
2208 if (an_read_record(sc,
2209 (struct an_ltv_gen *)&sc->areq)) {
2214 ireq->i_val = key->mac[0];
2216 * Check for home mode. Map home mode into
2217 * 5th key since that is how it is stored on
2220 sc->areq.an_len = sizeof(struct an_ltv_genconfig);
2221 sc->areq.an_type = AN_RID_GENCONFIG;
2222 if (an_read_record(sc,
2223 (struct an_ltv_gen *)&sc->areq)) {
2228 if (config->an_home_product & AN_HOME_NETWORK)
2232 case IEEE80211_IOC_AUTHMODE:
2234 sc->areq.an_type = AN_RID_ACTUALCFG;
2235 if (an_read_record(sc,
2236 (struct an_ltv_gen *)&sc->areq)) {
2242 if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2244 ireq->i_val = IEEE80211_AUTH_NONE;
2245 } else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2247 ireq->i_val = IEEE80211_AUTH_OPEN;
2248 } else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2249 AN_AUTHTYPE_SHAREDKEY) {
2250 ireq->i_val = IEEE80211_AUTH_SHARED;
2254 case IEEE80211_IOC_STATIONNAME:
2256 sc->areq.an_type = AN_RID_ACTUALCFG;
2257 if (an_read_record(sc,
2258 (struct an_ltv_gen *)&sc->areq)) {
2264 ireq->i_len = sizeof(config->an_nodename);
2265 tmpptr = config->an_nodename;
2266 bzero(tmpstr, IEEE80211_NWID_LEN);
2267 bcopy(tmpptr, tmpstr, ireq->i_len);
2268 error = copyout(tmpstr, ireq->i_data,
2269 IEEE80211_NWID_LEN);
2271 case IEEE80211_IOC_CHANNEL:
2273 sc->areq.an_type = AN_RID_STATUS;
2274 if (an_read_record(sc,
2275 (struct an_ltv_gen *)&sc->areq)) {
2281 ireq->i_val = status->an_cur_channel;
2283 case IEEE80211_IOC_CURCHAN:
2285 sc->areq.an_type = AN_RID_STATUS;
2286 if (an_read_record(sc,
2287 (struct an_ltv_gen *)&sc->areq)) {
2293 bzero(&ch, sizeof(ch));
2294 ch.ic_freq = ieee80211_ieee2mhz(status->an_cur_channel,
2296 ch.ic_flags = IEEE80211_CHAN_B;
2297 ch.ic_ieee = status->an_cur_channel;
2298 error = copyout(&ch, ireq->i_data, sizeof(ch));
2300 case IEEE80211_IOC_POWERSAVE:
2302 sc->areq.an_type = AN_RID_ACTUALCFG;
2303 if (an_read_record(sc,
2304 (struct an_ltv_gen *)&sc->areq)) {
2310 if (config->an_psave_mode == AN_PSAVE_NONE) {
2311 ireq->i_val = IEEE80211_POWERSAVE_OFF;
2312 } else if (config->an_psave_mode == AN_PSAVE_CAM) {
2313 ireq->i_val = IEEE80211_POWERSAVE_CAM;
2314 } else if (config->an_psave_mode == AN_PSAVE_PSP) {
2315 ireq->i_val = IEEE80211_POWERSAVE_PSP;
2316 } else if (config->an_psave_mode == AN_PSAVE_PSP_CAM) {
2317 ireq->i_val = IEEE80211_POWERSAVE_PSP_CAM;
2321 case IEEE80211_IOC_POWERSAVESLEEP:
2323 sc->areq.an_type = AN_RID_ACTUALCFG;
2324 if (an_read_record(sc,
2325 (struct an_ltv_gen *)&sc->areq)) {
2331 ireq->i_val = config->an_listen_interval;
2336 if ((error = priv_check(td, PRIV_NET80211_MANAGE)))
2339 sc->areq.an_len = sizeof(sc->areq);
2341 * We need a config structure for everything but the WEP
2342 * key management and SSIDs so we get it now so avoid
2343 * duplicating this code every time.
2345 if (ireq->i_type != IEEE80211_IOC_SSID &&
2346 ireq->i_type != IEEE80211_IOC_WEPKEY &&
2347 ireq->i_type != IEEE80211_IOC_WEPTXKEY) {
2348 sc->areq.an_type = AN_RID_GENCONFIG;
2349 if (an_read_record(sc,
2350 (struct an_ltv_gen *)&sc->areq)) {
2356 switch (ireq->i_type) {
2357 case IEEE80211_IOC_SSID:
2358 sc->areq.an_len = sizeof(sc->areq);
2359 sc->areq.an_type = AN_RID_SSIDLIST;
2360 if (an_read_record(sc,
2361 (struct an_ltv_gen *)&sc->areq)) {
2366 if (ireq->i_len > IEEE80211_NWID_LEN) {
2371 max = (sc->areq.an_len - 4)
2372 / sizeof(struct an_ltv_ssid_entry);
2373 if ( max > MAX_SSIDS ) {
2374 printf("To many SSIDs only using "
2379 if (ireq->i_val > max) {
2384 error = copyin(ireq->i_data,
2385 ssids->an_entry[ireq->i_val].an_ssid,
2387 ssids->an_entry[ireq->i_val].an_len
2389 sc->areq.an_len = sizeof(sc->areq);
2390 sc->areq.an_type = AN_RID_SSIDLIST;
2391 an_setdef(sc, &sc->areq);
2396 case IEEE80211_IOC_WEP:
2397 switch (ireq->i_val) {
2398 case IEEE80211_WEP_OFF:
2399 config->an_authtype &=
2400 ~(AN_AUTHTYPE_PRIVACY_IN_USE |
2401 AN_AUTHTYPE_ALLOW_UNENCRYPTED);
2403 case IEEE80211_WEP_ON:
2404 config->an_authtype |=
2405 AN_AUTHTYPE_PRIVACY_IN_USE;
2406 config->an_authtype &=
2407 ~AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2409 case IEEE80211_WEP_MIXED:
2410 config->an_authtype |=
2411 AN_AUTHTYPE_PRIVACY_IN_USE |
2412 AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2418 if (error != EINVAL)
2419 an_setdef(sc, &sc->areq);
2422 case IEEE80211_IOC_WEPKEY:
2423 if (ireq->i_val < 0 || ireq->i_val > 8 ||
2429 error = copyin(ireq->i_data, tmpstr, 13);
2435 * Map the 9th key into the home mode
2436 * since that is how it is stored on
2439 bzero(&sc->areq, sizeof(struct an_ltv_key));
2440 sc->areq.an_len = sizeof(struct an_ltv_key);
2441 key->mac[0] = 1; /* The others are 0. */
2442 if (ireq->i_val < 4) {
2443 sc->areq.an_type = AN_RID_WEP_TEMP;
2444 key->kindex = ireq->i_val;
2446 sc->areq.an_type = AN_RID_WEP_PERM;
2447 key->kindex = ireq->i_val - 4;
2449 key->klen = ireq->i_len;
2450 bcopy(tmpstr, key->key, key->klen);
2451 an_setdef(sc, &sc->areq);
2454 case IEEE80211_IOC_WEPTXKEY:
2455 if (ireq->i_val < 0 || ireq->i_val > 4) {
2462 * Map the 5th key into the home mode
2463 * since that is how it is stored on
2466 sc->areq.an_len = sizeof(struct an_ltv_genconfig);
2467 sc->areq.an_type = AN_RID_ACTUALCFG;
2468 if (an_read_record(sc,
2469 (struct an_ltv_gen *)&sc->areq)) {
2474 if (ireq->i_val == 4) {
2475 config->an_home_product |= AN_HOME_NETWORK;
2478 config->an_home_product &= ~AN_HOME_NETWORK;
2481 sc->an_config.an_home_product
2482 = config->an_home_product;
2484 /* update configuration */
2487 bzero(&sc->areq, sizeof(struct an_ltv_key));
2488 sc->areq.an_len = sizeof(struct an_ltv_key);
2489 sc->areq.an_type = AN_RID_WEP_PERM;
2490 key->kindex = 0xffff;
2491 key->mac[0] = ireq->i_val;
2492 an_setdef(sc, &sc->areq);
2495 case IEEE80211_IOC_AUTHMODE:
2496 switch (ireq->i_val) {
2497 case IEEE80211_AUTH_NONE:
2498 config->an_authtype = AN_AUTHTYPE_NONE |
2499 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2501 case IEEE80211_AUTH_OPEN:
2502 config->an_authtype = AN_AUTHTYPE_OPEN |
2503 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2505 case IEEE80211_AUTH_SHARED:
2506 config->an_authtype = AN_AUTHTYPE_SHAREDKEY |
2507 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2512 if (error != EINVAL) {
2513 an_setdef(sc, &sc->areq);
2517 case IEEE80211_IOC_STATIONNAME:
2518 if (ireq->i_len > 16) {
2523 bzero(config->an_nodename, 16);
2524 error = copyin(ireq->i_data,
2525 config->an_nodename, ireq->i_len);
2526 an_setdef(sc, &sc->areq);
2529 case IEEE80211_IOC_CHANNEL:
2531 * The actual range is 1-14, but if you set it
2532 * to 0 you get the default so we let that work
2535 if (ireq->i_val < 0 || ireq->i_val >14) {
2540 config->an_ds_channel = ireq->i_val;
2541 an_setdef(sc, &sc->areq);
2544 case IEEE80211_IOC_POWERSAVE:
2545 switch (ireq->i_val) {
2546 case IEEE80211_POWERSAVE_OFF:
2547 config->an_psave_mode = AN_PSAVE_NONE;
2549 case IEEE80211_POWERSAVE_CAM:
2550 config->an_psave_mode = AN_PSAVE_CAM;
2552 case IEEE80211_POWERSAVE_PSP:
2553 config->an_psave_mode = AN_PSAVE_PSP;
2555 case IEEE80211_POWERSAVE_PSP_CAM:
2556 config->an_psave_mode = AN_PSAVE_PSP_CAM;
2562 an_setdef(sc, &sc->areq);
2565 case IEEE80211_IOC_POWERSAVESLEEP:
2566 config->an_listen_interval = ireq->i_val;
2567 an_setdef(sc, &sc->areq);
2578 an_setdef(sc, &sc->areq);
2584 error = ether_ioctl(ifp, command, data);
2593 an_init_tx_ring(struct an_softc *sc)
2602 for (i = 0; i < AN_TX_RING_CNT; i++) {
2603 if (an_alloc_nicmem(sc, 1518 +
2606 sc->an_rdata.an_tx_fids[i] = id;
2607 sc->an_rdata.an_tx_ring[i] = 0;
2611 sc->an_rdata.an_tx_prod = 0;
2612 sc->an_rdata.an_tx_cons = 0;
2613 sc->an_rdata.an_tx_empty = 1;
2621 struct an_softc *sc = xsc;
2629 an_init_locked(struct an_softc *sc)
2638 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2641 sc->an_associated = 0;
2643 /* Allocate the TX buffers */
2644 if (an_init_tx_ring(sc)) {
2647 an_init_mpi350_desc(sc);
2648 if (an_init_tx_ring(sc)) {
2649 if_printf(ifp, "tx buffer allocation failed\n");
2654 /* Set our MAC address. */
2655 bcopy((char *)IF_LLADDR(sc->an_ifp),
2656 (char *)&sc->an_config.an_macaddr, ETHER_ADDR_LEN);
2658 if (ifp->if_flags & IFF_BROADCAST)
2659 sc->an_config.an_rxmode = AN_RXMODE_BC_ADDR;
2661 sc->an_config.an_rxmode = AN_RXMODE_ADDR;
2663 if (ifp->if_flags & IFF_MULTICAST)
2664 sc->an_config.an_rxmode = AN_RXMODE_BC_MC_ADDR;
2666 if (ifp->if_flags & IFF_PROMISC) {
2667 if (sc->an_monitor & AN_MONITOR) {
2668 if (sc->an_monitor & AN_MONITOR_ANY_BSS) {
2669 sc->an_config.an_rxmode |=
2670 AN_RXMODE_80211_MONITOR_ANYBSS |
2671 AN_RXMODE_NO_8023_HEADER;
2673 sc->an_config.an_rxmode |=
2674 AN_RXMODE_80211_MONITOR_CURBSS |
2675 AN_RXMODE_NO_8023_HEADER;
2681 if (sc->an_have_rssimap)
2682 sc->an_config.an_rxmode |= AN_RXMODE_NORMALIZED_RSSI;
2685 /* Set the ssid list */
2686 sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
2687 sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
2688 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
2689 if_printf(ifp, "failed to set ssid list\n");
2693 /* Set the AP list */
2694 sc->an_aplist.an_type = AN_RID_APLIST;
2695 sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
2696 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
2697 if_printf(ifp, "failed to set AP list\n");
2701 /* Set the configuration in the NIC */
2702 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
2703 sc->an_config.an_type = AN_RID_GENCONFIG;
2704 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
2705 if_printf(ifp, "failed to set configuration\n");
2709 /* Enable the MAC */
2710 if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
2711 if_printf(ifp, "failed to enable MAC\n");
2715 if (ifp->if_flags & IFF_PROMISC)
2716 an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
2718 /* enable interrupts */
2719 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2721 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2722 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2724 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
2730 an_start(struct ifnet *ifp)
2732 struct an_softc *sc;
2736 an_start_locked(ifp);
2741 an_start_locked(struct ifnet *ifp)
2743 struct an_softc *sc;
2744 struct mbuf *m0 = NULL;
2745 struct an_txframe_802_3 tx_frame_802_3;
2746 struct ether_header *eh;
2748 unsigned char txcontrol;
2749 struct an_card_tx_desc an_tx_desc;
2758 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
2761 if (!sc->an_associated)
2764 /* We can't send in monitor mode so toss any attempts. */
2765 if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
2767 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2775 idx = sc->an_rdata.an_tx_prod;
2778 bzero((char *)&tx_frame_802_3, sizeof(tx_frame_802_3));
2780 while (sc->an_rdata.an_tx_ring[idx] == 0) {
2781 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2785 id = sc->an_rdata.an_tx_fids[idx];
2786 eh = mtod(m0, struct ether_header *);
2788 bcopy((char *)&eh->ether_dhost,
2789 (char *)&tx_frame_802_3.an_tx_dst_addr,
2791 bcopy((char *)&eh->ether_shost,
2792 (char *)&tx_frame_802_3.an_tx_src_addr,
2795 /* minus src/dest mac & type */
2796 tx_frame_802_3.an_tx_802_3_payload_len =
2797 m0->m_pkthdr.len - 12;
2799 m_copydata(m0, sizeof(struct ether_header) - 2 ,
2800 tx_frame_802_3.an_tx_802_3_payload_len,
2801 (caddr_t)&sc->an_txbuf);
2803 txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2804 /* write the txcontrol only */
2805 an_write_data(sc, id, 0x08, (caddr_t)&txcontrol,
2809 an_write_data(sc, id, 0x34, (caddr_t)&tx_frame_802_3,
2810 sizeof(struct an_txframe_802_3));
2812 /* in mbuf header type is just before payload */
2813 an_write_data(sc, id, 0x44, (caddr_t)&sc->an_txbuf,
2814 tx_frame_802_3.an_tx_802_3_payload_len);
2817 * If there's a BPF listner, bounce a copy of
2818 * this frame to him.
2825 sc->an_rdata.an_tx_ring[idx] = id;
2826 if (an_cmd(sc, AN_CMD_TX, id))
2827 if_printf(ifp, "xmit failed\n");
2829 AN_INC(idx, AN_TX_RING_CNT);
2832 * Set a timeout in case the chip goes out to lunch.
2836 } else { /* MPI-350 */
2837 /* Disable interrupts. */
2838 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2840 while (sc->an_rdata.an_tx_empty ||
2841 idx != sc->an_rdata.an_tx_cons) {
2842 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2846 buf = sc->an_tx_buffer[idx].an_dma_vaddr;
2848 eh = mtod(m0, struct ether_header *);
2850 /* DJA optimize this to limit bcopy */
2851 bcopy((char *)&eh->ether_dhost,
2852 (char *)&tx_frame_802_3.an_tx_dst_addr,
2854 bcopy((char *)&eh->ether_shost,
2855 (char *)&tx_frame_802_3.an_tx_src_addr,
2858 /* minus src/dest mac & type */
2859 tx_frame_802_3.an_tx_802_3_payload_len =
2860 m0->m_pkthdr.len - 12;
2862 m_copydata(m0, sizeof(struct ether_header) - 2 ,
2863 tx_frame_802_3.an_tx_802_3_payload_len,
2864 (caddr_t)&sc->an_txbuf);
2866 txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2867 /* write the txcontrol only */
2868 bcopy((caddr_t)&txcontrol, &buf[0x08],
2872 bcopy((caddr_t)&tx_frame_802_3, &buf[0x34],
2873 sizeof(struct an_txframe_802_3));
2875 /* in mbuf header type is just before payload */
2876 bcopy((caddr_t)&sc->an_txbuf, &buf[0x44],
2877 tx_frame_802_3.an_tx_802_3_payload_len);
2880 bzero(&an_tx_desc, sizeof(an_tx_desc));
2881 an_tx_desc.an_offset = 0;
2882 an_tx_desc.an_eoc = 1;
2883 an_tx_desc.an_valid = 1;
2884 an_tx_desc.an_len = 0x44 +
2885 tx_frame_802_3.an_tx_802_3_payload_len;
2887 = sc->an_tx_buffer[idx].an_dma_paddr;
2888 for (i = sizeof(an_tx_desc) / 4 - 1; i >= 0; i--) {
2889 CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
2891 + (0 * sizeof(an_tx_desc))
2893 ((u_int32_t *)(void *)&an_tx_desc)[i]);
2897 * If there's a BPF listner, bounce a copy of
2898 * this frame to him.
2904 AN_INC(idx, AN_MAX_TX_DESC);
2905 sc->an_rdata.an_tx_empty = 0;
2906 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
2909 * Set a timeout in case the chip goes out to lunch.
2914 /* Re-enable interrupts. */
2915 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2919 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2921 sc->an_rdata.an_tx_prod = idx;
2927 an_stop(struct an_softc *sc)
2939 an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
2940 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2941 an_cmd(sc, AN_CMD_DISABLE, 0);
2943 for (i = 0; i < AN_TX_RING_CNT; i++)
2944 an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->an_rdata.an_tx_fids[i]);
2946 callout_stop(&sc->an_stat_ch);
2948 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
2950 if (sc->an_flash_buffer) {
2951 free(sc->an_flash_buffer, M_DEVBUF);
2952 sc->an_flash_buffer = NULL;
2957 an_watchdog(struct an_softc *sc)
2967 if_printf(ifp, "device timeout\n");
2971 an_init_mpi350_desc(sc);
2978 an_shutdown(device_t dev)
2980 struct an_softc *sc;
2982 sc = device_get_softc(dev);
2992 an_resume(device_t dev)
2994 struct an_softc *sc;
2998 sc = device_get_softc(dev);
3005 an_init_mpi350_desc(sc);
3008 /* Recovery temporary keys */
3009 for (i = 0; i < 4; i++) {
3010 sc->areq.an_type = AN_RID_WEP_TEMP;
3011 sc->areq.an_len = sizeof(struct an_ltv_key);
3012 bcopy(&sc->an_temp_keys[i],
3013 &sc->areq, sizeof(struct an_ltv_key));
3014 an_setdef(sc, &sc->areq);
3017 if (ifp->if_flags & IFF_UP)
3018 an_start_locked(ifp);
3025 /* Aironet signal strength cache code.
3026 * store signal/noise/quality on per MAC src basis in
3027 * a small fixed cache. The cache wraps if > MAX slots
3028 * used. The cache may be zeroed out to start over.
3029 * Two simple filters exist to reduce computation:
3030 * 1. ip only (literally 0x800, ETHERTYPE_IP) which may be used
3031 * to ignore some packets. It defaults to ip only.
3032 * it could be used to focus on broadcast, non-IP 802.11 beacons.
3033 * 2. multicast/broadcast only. This may be used to
3034 * ignore unicast packets and only cache signal strength
3035 * for multicast/broadcast packets (beacons); e.g., Mobile-IP
3036 * beacons and not unicast traffic.
3038 * The cache stores (MAC src(index), IP src (major clue), signal,
3041 * No apologies for storing IP src here. It's easy and saves much
3042 * trouble elsewhere. The cache is assumed to be INET dependent,
3043 * although it need not be.
3045 * Note: the Aironet only has a single byte of signal strength value
3046 * in the rx frame header, and it's not scaled to anything sensible.
3047 * This is kind of lame, but it's all we've got.
3050 #ifdef documentation
3052 int an_sigitems; /* number of cached entries */
3053 struct an_sigcache an_sigcache[MAXANCACHE]; /* array of cache entries */
3054 int an_nextitem; /* index/# of entries */
3059 /* control variables for cache filtering. Basic idea is
3060 * to reduce cost (e.g., to only Mobile-IP agent beacons
3061 * which are broadcast or multicast). Still you might
3062 * want to measure signal strength anth unicast ping packets
3063 * on a pt. to pt. ant. setup.
3065 /* set true if you want to limit cache items to broadcast/mcast
3066 * only packets (not unicast). Useful for mobile-ip beacons which
3067 * are broadcast/multicast at network layer. Default is all packets
3068 * so ping/unicast anll work say anth pt. to pt. antennae setup.
3070 static int an_cache_mcastonly = 0;
3071 SYSCTL_INT(_hw_an, OID_AUTO, an_cache_mcastonly, CTLFLAG_RW,
3072 &an_cache_mcastonly, 0, "");
3074 /* set true if you want to limit cache items to IP packets only
3076 static int an_cache_iponly = 1;
3077 SYSCTL_INT(_hw_an, OID_AUTO, an_cache_iponly, CTLFLAG_RW,
3078 &an_cache_iponly, 0, "");
3081 * an_cache_store, per rx packet store signal
3082 * strength in MAC (src) indexed cache.
3085 an_cache_store(struct an_softc *sc, struct ether_header *eh, struct mbuf *m,
3086 u_int8_t rx_rssi, u_int8_t rx_quality)
3090 static int cache_slot = 0; /* use this cache entry */
3091 static int wrapindex = 0; /* next "free" cache entry */
3096 * 2. configurable filter to throw out unicast packets,
3097 * keep multicast only.
3100 if ((ntohs(eh->ether_type) == ETHERTYPE_IP)) {
3104 /* filter for ip packets only
3106 if ( an_cache_iponly && !type_ipv4) {
3110 /* filter for broadcast/multicast only
3112 if (an_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) {
3117 if_printf(sc->an_ifp, "q value %x (MSB=0x%x, LSB=0x%x) \n",
3118 rx_rssi & 0xffff, rx_rssi >> 8, rx_rssi & 0xff);
3121 /* find the ip header. we want to store the ip_src
3125 ip = mtod(m, struct ip *);
3128 /* do a linear search for a matching MAC address
3129 * in the cache table
3130 * . MAC address is 6 bytes,
3131 * . var w_nextitem holds total number of entries already cached
3133 for (i = 0; i < sc->an_nextitem; i++) {
3134 if (! bcmp(eh->ether_shost , sc->an_sigcache[i].macsrc, 6 )) {
3136 * so we already have this entry,
3143 /* did we find a matching mac address?
3144 * if yes, then overwrite a previously existing cache entry
3146 if (i < sc->an_nextitem ) {
3149 /* else, have a new address entry,so
3150 * add this new entry,
3151 * if table full, then we need to replace LRU entry
3155 /* check for space in cache table
3156 * note: an_nextitem also holds number of entries
3157 * added in the cache table
3159 if ( sc->an_nextitem < MAXANCACHE ) {
3160 cache_slot = sc->an_nextitem;
3162 sc->an_sigitems = sc->an_nextitem;
3164 /* no space found, so simply wrap anth wrap index
3165 * and "zap" the next entry
3168 if (wrapindex == MAXANCACHE) {
3171 cache_slot = wrapindex++;
3175 /* invariant: cache_slot now points at some slot
3178 if (cache_slot < 0 || cache_slot >= MAXANCACHE) {
3179 log(LOG_ERR, "an_cache_store, bad index: %d of "
3180 "[0..%d], gross cache error\n",
3181 cache_slot, MAXANCACHE);
3185 /* store items in cache
3186 * .ip source address
3191 sc->an_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr;
3193 bcopy( eh->ether_shost, sc->an_sigcache[cache_slot].macsrc, 6);
3196 switch (an_cache_mode) {
3198 if (sc->an_have_rssimap) {
3199 sc->an_sigcache[cache_slot].signal =
3200 - sc->an_rssimap.an_entries[rx_rssi].an_rss_dbm;
3201 sc->an_sigcache[cache_slot].quality =
3202 - sc->an_rssimap.an_entries[rx_quality].an_rss_dbm;
3204 sc->an_sigcache[cache_slot].signal = rx_rssi - 100;
3205 sc->an_sigcache[cache_slot].quality = rx_quality - 100;
3209 if (sc->an_have_rssimap) {
3210 sc->an_sigcache[cache_slot].signal =
3211 sc->an_rssimap.an_entries[rx_rssi].an_rss_pct;
3212 sc->an_sigcache[cache_slot].quality =
3213 sc->an_rssimap.an_entries[rx_quality].an_rss_pct;
3217 if (rx_quality > 100)
3219 sc->an_sigcache[cache_slot].signal = rx_rssi;
3220 sc->an_sigcache[cache_slot].quality = rx_quality;
3224 sc->an_sigcache[cache_slot].signal = rx_rssi;
3225 sc->an_sigcache[cache_slot].quality = rx_quality;
3229 sc->an_sigcache[cache_slot].noise = 0;
3236 an_media_change(struct ifnet *ifp)
3238 struct an_softc *sc = ifp->if_softc;
3239 struct an_ltv_genconfig *cfg;
3240 int otype = sc->an_config.an_opmode;
3241 int orate = sc->an_tx_rate;
3244 sc->an_tx_rate = ieee80211_media2rate(
3245 IFM_SUBTYPE(sc->an_ifmedia.ifm_cur->ifm_media));
3246 if (sc->an_tx_rate < 0)
3249 if (orate != sc->an_tx_rate) {
3250 /* Read the current configuration */
3251 sc->an_config.an_type = AN_RID_GENCONFIG;
3252 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3253 an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
3254 cfg = &sc->an_config;
3256 /* clear other rates and set the only one we want */
3257 bzero(cfg->an_rates, sizeof(cfg->an_rates));
3258 cfg->an_rates[0] = sc->an_tx_rate;
3260 /* Save the new rate */
3261 sc->an_config.an_type = AN_RID_GENCONFIG;
3262 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3265 if ((sc->an_ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
3266 sc->an_config.an_opmode &= ~AN_OPMODE_INFRASTRUCTURE_STATION;
3268 sc->an_config.an_opmode |= AN_OPMODE_INFRASTRUCTURE_STATION;
3270 if (otype != sc->an_config.an_opmode ||
3271 orate != sc->an_tx_rate)
3279 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
3281 struct an_ltv_status status;
3282 struct an_softc *sc = ifp->if_softc;
3284 imr->ifm_active = IFM_IEEE80211;
3287 status.an_len = sizeof(status);
3288 status.an_type = AN_RID_STATUS;
3289 if (an_read_record(sc, (struct an_ltv_gen *)&status)) {
3290 /* If the status read fails, just lie. */
3291 imr->ifm_active = sc->an_ifmedia.ifm_cur->ifm_media;
3292 imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
3295 if (sc->an_tx_rate == 0) {
3296 imr->ifm_active = IFM_IEEE80211|IFM_AUTO;
3299 if (sc->an_config.an_opmode == AN_OPMODE_IBSS_ADHOC)
3300 imr->ifm_active |= IFM_IEEE80211_ADHOC;
3301 imr->ifm_active |= ieee80211_rate2media(NULL,
3302 status.an_current_tx_rate, IEEE80211_MODE_AUTO);
3303 imr->ifm_status = IFM_AVALID;
3304 if (status.an_opmode & AN_STATUS_OPMODE_ASSOCIATED)
3305 imr->ifm_status |= IFM_ACTIVE;
3309 /********************** Cisco utility support routines *************/
3312 * ReadRids & WriteRids derived from Cisco driver additions to Ben Reed's
3317 readrids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3320 struct an_softc *sc;
3323 switch (l_ioctl->command) {
3325 rid = AN_RID_CAPABILITIES;
3328 rid = AN_RID_GENCONFIG;
3331 rid = AN_RID_SSIDLIST;
3334 rid = AN_RID_APLIST;
3337 rid = AN_RID_DRVNAME;
3340 rid = AN_RID_ENCAPPROTO;
3343 rid = AN_RID_WEP_TEMP;
3346 rid = AN_RID_WEP_PERM;
3349 rid = AN_RID_STATUS;
3352 rid = AN_RID_32BITS_DELTA;
3355 rid = AN_RID_32BITS_CUM;
3362 if (rid == 999) /* Is bad command */
3366 sc->areq.an_len = AN_MAX_DATALEN;
3367 sc->areq.an_type = rid;
3369 an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3371 l_ioctl->len = sc->areq.an_len - 4; /* just data */
3374 /* the data contains the length at first */
3375 if (copyout(&(sc->areq.an_len), l_ioctl->data,
3376 sizeof(sc->areq.an_len))) {
3380 /* Just copy the data back */
3381 if (copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3393 writerids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3395 struct an_softc *sc;
3396 int rid, command, error;
3401 command = l_ioctl->command;
3405 rid = AN_RID_SSIDLIST;
3408 rid = AN_RID_CAPABILITIES;
3411 rid = AN_RID_APLIST;
3414 rid = AN_RID_GENCONFIG;
3417 an_cmd(sc, AN_CMD_ENABLE, 0);
3421 an_cmd(sc, AN_CMD_DISABLE, 0);
3426 * This command merely clears the counts does not actually
3427 * store any data only reads rid. But as it changes the cards
3428 * state, I put it in the writerid routines.
3431 rid = AN_RID_32BITS_DELTACLR;
3433 sc->areq.an_len = AN_MAX_DATALEN;
3434 sc->areq.an_type = rid;
3436 an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3437 l_ioctl->len = sc->areq.an_len - 4; /* just data */
3440 /* the data contains the length at first */
3441 error = copyout(&(sc->areq.an_len), l_ioctl->data,
3442 sizeof(sc->areq.an_len));
3447 /* Just copy the data */
3448 error = copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3456 rid = AN_RID_WEP_TEMP;
3459 rid = AN_RID_WEP_PERM;
3462 rid = AN_RID_LEAPUSERNAME;
3465 rid = AN_RID_LEAPPASSWORD;
3472 if (l_ioctl->len > sizeof(sc->areq.an_val) + 4)
3474 sc->areq.an_len = l_ioctl->len + 4; /* add type & length */
3475 sc->areq.an_type = rid;
3477 /* Just copy the data back */
3479 error = copyin((l_ioctl->data) + 2, &sc->areq.an_val,
3485 an_cmd(sc, AN_CMD_DISABLE, 0);
3486 an_write_record(sc, (struct an_ltv_gen *)&sc->areq);
3487 an_cmd(sc, AN_CMD_ENABLE, 0);
3494 * General Flash utilities derived from Cisco driver additions to Ben Reed's
3498 #define FLASH_DELAY(_sc, x) msleep(ifp, &(_sc)->an_mtx, PZERO, \
3499 "flash", ((x) / hz) + 1);
3500 #define FLASH_COMMAND 0x7e7e
3501 #define FLASH_SIZE 32 * 1024
3504 unstickbusy(struct ifnet *ifp)
3506 struct an_softc *sc = ifp->if_softc;
3508 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
3509 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
3510 AN_EV_CLR_STUCK_BUSY);
3517 * Wait for busy completion from card wait for delay uSec's Return true for
3518 * success meaning command reg is clear
3522 WaitBusy(struct ifnet *ifp, int uSec)
3524 int statword = 0xffff;
3526 struct an_softc *sc = ifp->if_softc;
3528 while ((statword & AN_CMD_BUSY) && delay <= (1000 * 100)) {
3529 FLASH_DELAY(sc, 10);
3531 statword = CSR_READ_2(sc, AN_COMMAND(sc->mpi350));
3533 if ((AN_CMD_BUSY & statword) && (delay % 200)) {
3538 return 0 == (AN_CMD_BUSY & statword);
3542 * STEP 1) Disable MAC and do soft reset on card.
3546 cmdreset(struct ifnet *ifp)
3549 struct an_softc *sc = ifp->if_softc;
3554 an_cmd(sc, AN_CMD_DISABLE, 0);
3556 if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3557 if_printf(ifp, "Waitbusy hang b4 RESET =%d\n", status);
3561 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), AN_CMD_FW_RESTART);
3563 FLASH_DELAY(sc, 1000); /* WAS 600 12/7/00 */
3566 if (!(status = WaitBusy(ifp, 100))) {
3567 if_printf(ifp, "Waitbusy hang AFTER RESET =%d\n", status);
3576 * STEP 2) Put the card in legendary flash mode
3580 setflashmode(struct ifnet *ifp)
3583 struct an_softc *sc = ifp->if_softc;
3585 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3586 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), FLASH_COMMAND);
3587 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3588 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), FLASH_COMMAND);
3591 * mdelay(500); // 500ms delay
3594 FLASH_DELAY(sc, 500);
3596 if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3597 printf("Waitbusy hang after setflash mode\n");
3604 * Get a character from the card matching matchbyte Step 3)
3608 flashgchar(struct ifnet *ifp, int matchbyte, int dwelltime)
3611 unsigned char rbyte = 0;
3613 struct an_softc *sc = ifp->if_softc;
3617 rchar = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3619 if (dwelltime && !(0x8000 & rchar)) {
3621 FLASH_DELAY(sc, 10);
3624 rbyte = 0xff & rchar;
3626 if ((rbyte == matchbyte) && (0x8000 & rchar)) {
3627 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3631 if (rbyte == 0x81 || rbyte == 0x82 || rbyte == 0x83 || rbyte == 0x1a || 0xffff == rchar)
3633 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3635 } while (dwelltime > 0);
3640 * Put character to SWS0 wait for dwelltime x 50us for echo .
3644 flashpchar(struct ifnet *ifp, int byte, int dwelltime)
3647 int pollbusy, waittime;
3648 struct an_softc *sc = ifp->if_softc;
3655 waittime = dwelltime;
3658 * Wait for busy bit d15 to go false indicating buffer empty
3661 pollbusy = CSR_READ_2(sc, AN_SW0(sc->mpi350));
3663 if (pollbusy & 0x8000) {
3664 FLASH_DELAY(sc, 50);
3670 while (waittime >= 0);
3672 /* timeout for busy clear wait */
3674 if (waittime <= 0) {
3675 if_printf(ifp, "flash putchar busywait timeout!\n");
3679 * Port is clear now write byte and wait for it to echo back
3682 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), byte);
3683 FLASH_DELAY(sc, 50);
3685 echo = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3686 } while (dwelltime >= 0 && echo != byte);
3689 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3691 return echo == byte;
3695 * Transfer 32k of firmware data from user buffer to our buffer and send to
3700 flashputbuf(struct ifnet *ifp)
3702 unsigned short *bufp;
3704 struct an_softc *sc = ifp->if_softc;
3708 bufp = sc->an_flash_buffer;
3711 CSR_WRITE_2(sc, AN_AUX_PAGE, 0x100);
3712 CSR_WRITE_2(sc, AN_AUX_OFFSET, 0);
3714 for (nwords = 0; nwords != FLASH_SIZE / 2; nwords++) {
3715 CSR_WRITE_2(sc, AN_AUX_DATA, bufp[nwords] & 0xffff);
3718 for (nwords = 0; nwords != FLASH_SIZE / 4; nwords++) {
3719 CSR_MEM_AUX_WRITE_4(sc, 0x8000,
3720 ((u_int32_t *)bufp)[nwords] & 0xffff);
3724 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), 0x8000);
3730 * After flashing restart the card.
3734 flashrestart(struct ifnet *ifp)
3737 struct an_softc *sc = ifp->if_softc;
3739 FLASH_DELAY(sc, 1024); /* Added 12/7/00 */
3743 FLASH_DELAY(sc, 1024); /* Added 12/7/00 */
3748 * Entry point for flash ioclt.
3752 flashcard(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3755 struct an_softc *sc;
3759 if_printf(ifp, "flashing not supported on MPI 350 yet\n");
3762 status = l_ioctl->command;
3764 switch (l_ioctl->command) {
3766 return cmdreset(ifp);
3769 if (sc->an_flash_buffer) {
3770 free(sc->an_flash_buffer, M_DEVBUF);
3771 sc->an_flash_buffer = NULL;
3773 sc->an_flash_buffer = malloc(FLASH_SIZE, M_DEVBUF, M_WAITOK);
3774 if (sc->an_flash_buffer)
3775 return setflashmode(ifp);
3779 case AIROFLSHGCHR: /* Get char from aux */
3781 status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3785 z = *(int *)&sc->areq;
3786 if ((status = flashgchar(ifp, z, 8000)) == 1)
3790 case AIROFLSHPCHR: /* Send char to card. */
3792 status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3796 z = *(int *)&sc->areq;
3797 if ((status = flashpchar(ifp, z, 8000)) == -1)
3802 case AIROFLPUTBUF: /* Send 32k to card */
3803 if (l_ioctl->len > FLASH_SIZE) {
3804 if_printf(ifp, "Buffer to big, %x %x\n",
3805 l_ioctl->len, FLASH_SIZE);
3809 status = copyin(l_ioctl->data, sc->an_flash_buffer, l_ioctl->len);
3814 if ((status = flashputbuf(ifp)) != 0)
3820 if ((status = flashrestart(ifp)) != 0) {
3821 if_printf(ifp, "FLASHRESTART returned %d\n", status);