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_var.h>
115 #include <net/if_arp.h>
116 #include <net/if_dl.h>
117 #include <net/ethernet.h>
118 #include <net/if_types.h>
119 #include <net/if_media.h>
121 #include <net80211/ieee80211_var.h>
122 #include <net80211/ieee80211_ioctl.h>
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/in_var.h>
128 #include <netinet/ip.h>
133 #include <machine/md_var.h>
135 #include <dev/an/if_aironet_ieee.h>
136 #include <dev/an/if_anreg.h>
138 /* These are global because we need them in sys/pci/if_an_p.c. */
139 static void an_reset(struct an_softc *);
140 static int an_init_mpi350_desc(struct an_softc *);
141 static int an_ioctl(struct ifnet *, u_long, caddr_t);
142 static void an_init(void *);
143 static void an_init_locked(struct an_softc *);
144 static int an_init_tx_ring(struct an_softc *);
145 static void an_start(struct ifnet *);
146 static void an_start_locked(struct ifnet *);
147 static void an_watchdog(struct an_softc *);
148 static void an_rxeof(struct an_softc *);
149 static void an_txeof(struct an_softc *, int);
151 static void an_promisc(struct an_softc *, int);
152 static int an_cmd(struct an_softc *, int, int);
153 static int an_cmd_struct(struct an_softc *, struct an_command *,
155 static int an_read_record(struct an_softc *, struct an_ltv_gen *);
156 static int an_write_record(struct an_softc *, struct an_ltv_gen *);
157 static int an_read_data(struct an_softc *, int, int, caddr_t, int);
158 static int an_write_data(struct an_softc *, int, int, caddr_t, int);
159 static int an_seek(struct an_softc *, int, int, int);
160 static int an_alloc_nicmem(struct an_softc *, int, int *);
161 static int an_dma_malloc(struct an_softc *, bus_size_t, struct an_dma_alloc *,
163 static void an_dma_free(struct an_softc *, struct an_dma_alloc *);
164 static void an_dma_malloc_cb(void *, bus_dma_segment_t *, int, int);
165 static void an_stats_update(void *);
166 static void an_setdef(struct an_softc *, struct an_req *);
168 static void an_cache_store(struct an_softc *, struct ether_header *,
169 struct mbuf *, u_int8_t, u_int8_t);
172 /* function definitions for use with the Cisco's Linux configuration
176 static int readrids(struct ifnet*, struct aironet_ioctl*);
177 static int writerids(struct ifnet*, struct aironet_ioctl*);
178 static int flashcard(struct ifnet*, struct aironet_ioctl*);
180 static int cmdreset(struct ifnet *);
181 static int setflashmode(struct ifnet *);
182 static int flashgchar(struct ifnet *,int,int);
183 static int flashpchar(struct ifnet *,int,int);
184 static int flashputbuf(struct ifnet *);
185 static int flashrestart(struct ifnet *);
186 static int WaitBusy(struct ifnet *, int);
187 static int unstickbusy(struct ifnet *);
189 static void an_dump_record (struct an_softc *,struct an_ltv_gen *,
192 static int an_media_change (struct ifnet *);
193 static void an_media_status (struct ifnet *, struct ifmediareq *);
195 static int an_dump = 0;
196 static int an_cache_mode = 0;
202 static char an_conf[256];
203 static char an_conf_cache[256];
207 static SYSCTL_NODE(_hw, OID_AUTO, an, CTLFLAG_RD, 0,
208 "Wireless driver parameters");
210 /* XXX violate ethernet/netgraph callback hooks */
211 extern void (*ng_ether_attach_p)(struct ifnet *ifp);
212 extern void (*ng_ether_detach_p)(struct ifnet *ifp);
215 sysctl_an_dump(SYSCTL_HANDLER_ARGS)
224 strcpy(an_conf, "off");
227 strcpy(an_conf, "type");
230 strcpy(an_conf, "dump");
233 snprintf(an_conf, 5, "%x", an_dump);
237 error = sysctl_handle_string(oidp, an_conf, sizeof(an_conf), req);
239 if (strncmp(an_conf,"off", 3) == 0) {
242 if (strncmp(an_conf,"dump", 4) == 0) {
245 if (strncmp(an_conf,"type", 4) == 0) {
251 if ((*s >= '0') && (*s <= '9')) {
252 r = r * 16 + (*s - '0');
253 } else if ((*s >= 'a') && (*s <= 'f')) {
254 r = r * 16 + (*s - 'a' + 10);
262 printf("Sysctl changed for Aironet driver\n");
267 SYSCTL_PROC(_hw_an, OID_AUTO, an_dump, CTLTYPE_STRING | CTLFLAG_RW,
268 0, sizeof(an_conf), sysctl_an_dump, "A", "");
271 sysctl_an_cache_mode(SYSCTL_HANDLER_ARGS)
275 last = an_cache_mode;
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 * Setup the lock for PCI attachment since it skips the an_probe
310 * function. We need to setup the lock in an_probe since some
311 * operations need the lock. So we might as well create the
315 an_pci_probe(device_t dev)
317 struct an_softc *sc = device_get_softc(dev);
319 mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
326 * We probe for an Aironet 4500/4800 card by attempting to
327 * read the default SSID list. On reset, the first entry in
328 * the SSID list will contain the name "tsunami." If we don't
329 * find this, then there's no card present.
332 an_probe(device_t dev)
334 struct an_softc *sc = device_get_softc(dev);
335 struct an_ltv_ssidlist_new ssid;
338 bzero((char *)&ssid, sizeof(ssid));
340 error = an_alloc_port(dev, 0, AN_IOSIZ);
344 /* can't do autoprobing */
345 if (rman_get_start(sc->port_res) == -1)
349 * We need to fake up a softc structure long enough
350 * to be able to issue commands and call some of the
353 ssid.an_len = sizeof(ssid);
354 ssid.an_type = AN_RID_SSIDLIST;
356 /* Make sure interrupts are disabled. */
358 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
359 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), 0xFFFF);
362 mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
367 if (an_cmd(sc, AN_CMD_READCFG, 0)) {
372 if (an_read_record(sc, (struct an_ltv_gen *)&ssid)) {
377 /* See if the ssid matches what we expect ... but doesn't have to */
378 if (strcmp(ssid.an_entry[0].an_ssid, AN_DEF_SSID)) {
386 mtx_destroy(&sc->an_mtx);
391 * Allocate a port resource with the given resource id.
394 an_alloc_port(device_t dev, int rid, int size)
396 struct an_softc *sc = device_get_softc(dev);
397 struct resource *res;
399 res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
400 0ul, ~0ul, size, RF_ACTIVE);
411 * Allocate a memory resource with the given resource id.
413 int an_alloc_memory(device_t dev, int rid, int size)
415 struct an_softc *sc = device_get_softc(dev);
416 struct resource *res;
418 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
419 0ul, ~0ul, size, RF_ACTIVE);
431 * Allocate a auxilary memory resource with the given resource id.
433 int an_alloc_aux_memory(device_t dev, int rid, int size)
435 struct an_softc *sc = device_get_softc(dev);
436 struct resource *res;
438 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
439 0ul, ~0ul, size, RF_ACTIVE);
441 sc->mem_aux_rid = rid;
442 sc->mem_aux_res = res;
443 sc->mem_aux_used = size;
451 * Allocate an irq resource with the given resource id.
454 an_alloc_irq(device_t dev, int rid, int flags)
456 struct an_softc *sc = device_get_softc(dev);
457 struct resource *res;
459 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
460 (RF_ACTIVE | flags));
471 an_dma_malloc_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
473 bus_addr_t *paddr = (bus_addr_t*) arg;
474 *paddr = segs->ds_addr;
478 * Alloc DMA memory and set the pointer to it
481 an_dma_malloc(struct an_softc *sc, bus_size_t size, struct an_dma_alloc *dma,
486 r = bus_dmamem_alloc(sc->an_dtag, (void**) &dma->an_dma_vaddr,
487 BUS_DMA_NOWAIT, &dma->an_dma_map);
491 r = bus_dmamap_load(sc->an_dtag, dma->an_dma_map, dma->an_dma_vaddr,
495 mapflags | BUS_DMA_NOWAIT);
499 dma->an_dma_size = size;
503 bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
505 bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
510 an_dma_free(struct an_softc *sc, struct an_dma_alloc *dma)
512 bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
513 bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
514 dma->an_dma_vaddr = 0;
518 * Release all resources
521 an_release_resources(device_t dev)
523 struct an_softc *sc = device_get_softc(dev);
527 bus_release_resource(dev, SYS_RES_IOPORT,
528 sc->port_rid, sc->port_res);
532 bus_release_resource(dev, SYS_RES_MEMORY,
533 sc->mem_rid, sc->mem_res);
536 if (sc->mem_aux_res) {
537 bus_release_resource(dev, SYS_RES_MEMORY,
538 sc->mem_aux_rid, sc->mem_aux_res);
542 bus_release_resource(dev, SYS_RES_IRQ,
543 sc->irq_rid, sc->irq_res);
546 if (sc->an_rid_buffer.an_dma_paddr) {
547 an_dma_free(sc, &sc->an_rid_buffer);
549 for (i = 0; i < AN_MAX_RX_DESC; i++)
550 if (sc->an_rx_buffer[i].an_dma_paddr) {
551 an_dma_free(sc, &sc->an_rx_buffer[i]);
553 for (i = 0; i < AN_MAX_TX_DESC; i++)
554 if (sc->an_tx_buffer[i].an_dma_paddr) {
555 an_dma_free(sc, &sc->an_tx_buffer[i]);
558 bus_dma_tag_destroy(sc->an_dtag);
564 an_init_mpi350_desc(struct an_softc *sc)
566 struct an_command cmd_struct;
567 struct an_reply reply;
568 struct an_card_rid_desc an_rid_desc;
569 struct an_card_rx_desc an_rx_desc;
570 struct an_card_tx_desc an_tx_desc;
574 if(!sc->an_rid_buffer.an_dma_paddr)
575 an_dma_malloc(sc, AN_RID_BUFFER_SIZE,
576 &sc->an_rid_buffer, 0);
577 for (i = 0; i < AN_MAX_RX_DESC; i++)
578 if(!sc->an_rx_buffer[i].an_dma_paddr)
579 an_dma_malloc(sc, AN_RX_BUFFER_SIZE,
580 &sc->an_rx_buffer[i], 0);
581 for (i = 0; i < AN_MAX_TX_DESC; i++)
582 if(!sc->an_tx_buffer[i].an_dma_paddr)
583 an_dma_malloc(sc, AN_TX_BUFFER_SIZE,
584 &sc->an_tx_buffer[i], 0);
587 * Allocate RX descriptor
589 bzero(&reply,sizeof(reply));
590 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
591 cmd_struct.an_parm0 = AN_DESCRIPTOR_RX;
592 cmd_struct.an_parm1 = AN_RX_DESC_OFFSET;
593 cmd_struct.an_parm2 = AN_MAX_RX_DESC;
594 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
595 if_printf(sc->an_ifp, "failed to allocate RX descriptor\n");
599 for (desc = 0; desc < AN_MAX_RX_DESC; desc++) {
600 bzero(&an_rx_desc, sizeof(an_rx_desc));
601 an_rx_desc.an_valid = 1;
602 an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
603 an_rx_desc.an_done = 0;
604 an_rx_desc.an_phys = sc->an_rx_buffer[desc].an_dma_paddr;
606 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
607 CSR_MEM_AUX_WRITE_4(sc, AN_RX_DESC_OFFSET
608 + (desc * sizeof(an_rx_desc))
610 ((u_int32_t *)(void *)&an_rx_desc)[i]);
614 * Allocate TX descriptor
617 bzero(&reply,sizeof(reply));
618 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
619 cmd_struct.an_parm0 = AN_DESCRIPTOR_TX;
620 cmd_struct.an_parm1 = AN_TX_DESC_OFFSET;
621 cmd_struct.an_parm2 = AN_MAX_TX_DESC;
622 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
623 if_printf(sc->an_ifp, "failed to allocate TX descriptor\n");
627 for (desc = 0; desc < AN_MAX_TX_DESC; desc++) {
628 bzero(&an_tx_desc, sizeof(an_tx_desc));
629 an_tx_desc.an_offset = 0;
630 an_tx_desc.an_eoc = 0;
631 an_tx_desc.an_valid = 0;
632 an_tx_desc.an_len = 0;
633 an_tx_desc.an_phys = sc->an_tx_buffer[desc].an_dma_paddr;
635 for (i = 0; i < sizeof(an_tx_desc) / 4; i++)
636 CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
637 + (desc * sizeof(an_tx_desc))
639 ((u_int32_t *)(void *)&an_tx_desc)[i]);
643 * Allocate RID descriptor
646 bzero(&reply,sizeof(reply));
647 cmd_struct.an_cmd = AN_CMD_ALLOC_DESC;
648 cmd_struct.an_parm0 = AN_DESCRIPTOR_HOSTRW;
649 cmd_struct.an_parm1 = AN_HOST_DESC_OFFSET;
650 cmd_struct.an_parm2 = 1;
651 if (an_cmd_struct(sc, &cmd_struct, &reply)) {
652 if_printf(sc->an_ifp, "failed to allocate host descriptor\n");
656 bzero(&an_rid_desc, sizeof(an_rid_desc));
657 an_rid_desc.an_valid = 1;
658 an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
659 an_rid_desc.an_rid = 0;
660 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
662 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
663 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
664 ((u_int32_t *)(void *)&an_rid_desc)[i]);
670 an_attach(struct an_softc *sc, int flags)
677 ifp = sc->an_ifp = if_alloc(IFT_ETHER);
679 device_printf(sc->an_dev, "can not if_alloc()\n");
683 if_initname(ifp, device_get_name(sc->an_dev),
684 device_get_unit(sc->an_dev));
687 sc->an_associated = 0;
689 sc->an_was_monitor = 0;
690 sc->an_flash_buffer = NULL;
696 error = an_init_mpi350_desc(sc);
701 /* Load factory config */
702 if (an_cmd(sc, AN_CMD_READCFG, 0)) {
703 device_printf(sc->an_dev, "failed to load config data\n");
707 /* Read the current configuration */
708 sc->an_config.an_type = AN_RID_GENCONFIG;
709 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
710 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
711 device_printf(sc->an_dev, "read record failed\n");
715 /* Read the card capabilities */
716 sc->an_caps.an_type = AN_RID_CAPABILITIES;
717 sc->an_caps.an_len = sizeof(struct an_ltv_caps);
718 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_caps)) {
719 device_printf(sc->an_dev, "read record failed\n");
724 sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
725 sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
726 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
727 device_printf(sc->an_dev, "read record failed\n");
732 sc->an_aplist.an_type = AN_RID_APLIST;
733 sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
734 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
735 device_printf(sc->an_dev, "read record failed\n");
740 /* Read the RSSI <-> dBm map */
741 sc->an_have_rssimap = 0;
742 if (sc->an_caps.an_softcaps & 8) {
743 sc->an_rssimap.an_type = AN_RID_RSSI_MAP;
744 sc->an_rssimap.an_len = sizeof(struct an_ltv_rssi_map);
745 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_rssimap)) {
746 device_printf(sc->an_dev,
747 "unable to get RSSI <-> dBM map\n");
749 device_printf(sc->an_dev, "got RSSI <-> dBM map\n");
750 sc->an_have_rssimap = 1;
753 device_printf(sc->an_dev, "no RSSI <-> dBM map\n");
758 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
759 ifp->if_ioctl = an_ioctl;
760 ifp->if_start = an_start;
761 ifp->if_init = an_init;
762 ifp->if_baudrate = 10000000;
763 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
764 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
765 IFQ_SET_READY(&ifp->if_snd);
767 bzero(sc->an_config.an_nodename, sizeof(sc->an_config.an_nodename));
768 bcopy(AN_DEFAULT_NODENAME, sc->an_config.an_nodename,
769 sizeof(AN_DEFAULT_NODENAME) - 1);
771 bzero(sc->an_ssidlist.an_entry[0].an_ssid,
772 sizeof(sc->an_ssidlist.an_entry[0].an_ssid));
773 bcopy(AN_DEFAULT_NETNAME, sc->an_ssidlist.an_entry[0].an_ssid,
774 sizeof(AN_DEFAULT_NETNAME) - 1);
775 sc->an_ssidlist.an_entry[0].an_len = strlen(AN_DEFAULT_NETNAME);
777 sc->an_config.an_opmode =
778 AN_OPMODE_INFRASTRUCTURE_STATION;
781 bzero((char *)&sc->an_stats, sizeof(sc->an_stats));
785 ifmedia_init(&sc->an_ifmedia, 0, an_media_change, an_media_status);
786 if_printf(ifp, "supported rates: ");
787 #define ADD(s, o) ifmedia_add(&sc->an_ifmedia, \
788 IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
790 ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
791 for (i = 0; i < nrate; i++) {
792 r = sc->an_caps.an_rates[i];
793 mword = ieee80211_rate2media(NULL, r, IEEE80211_MODE_AUTO);
796 printf("%s%d%sMbps", (i != 0 ? " " : ""),
797 (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
799 ADD(mword, IFM_IEEE80211_ADHOC);
802 ifmedia_set(&sc->an_ifmedia, IFM_MAKEWORD(IFM_IEEE80211,
807 * Call MI attach routine.
810 ether_ifattach(ifp, sc->an_caps.an_oemaddr);
811 callout_init_mtx(&sc->an_stat_ch, &sc->an_mtx, 0);
816 mtx_destroy(&sc->an_mtx);
823 an_detach(device_t dev)
825 struct an_softc *sc = device_get_softc(dev);
826 struct ifnet *ifp = sc->an_ifp;
829 device_printf(dev,"already unloaded\n");
835 ifmedia_removeall(&sc->an_ifmedia);
836 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
839 bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
840 callout_drain(&sc->an_stat_ch);
842 an_release_resources(dev);
843 mtx_destroy(&sc->an_mtx);
848 an_rxeof(struct an_softc *sc)
851 struct ether_header *eh;
852 struct ieee80211_frame *ih;
853 struct an_rxframe rx_frame;
854 struct an_rxframe_802_3 rx_frame_802_3;
856 int len, id, error = 0, i, count = 0;
857 int ieee80211_header_len;
860 struct an_card_rx_desc an_rx_desc;
868 id = CSR_READ_2(sc, AN_RX_FID);
870 if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
871 /* read raw 802.11 packet */
872 bpf_buf = sc->buf_802_11;
875 if (an_read_data(sc, id, 0x0, (caddr_t)&rx_frame,
882 * skip beacon by default since this increases the
886 if (!(sc->an_monitor & AN_MONITOR_INCLUDE_BEACON) &&
887 (rx_frame.an_frame_ctl &
888 IEEE80211_FC0_SUBTYPE_BEACON)) {
892 if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
893 len = rx_frame.an_rx_payload_len
895 /* Check for insane frame length */
896 if (len > sizeof(sc->buf_802_11)) {
897 if_printf(ifp, "oversized packet "
898 "received (%d, %d)\n",
904 bcopy((char *)&rx_frame,
905 bpf_buf, sizeof(rx_frame));
907 error = an_read_data(sc, id, sizeof(rx_frame),
908 (caddr_t)bpf_buf+sizeof(rx_frame),
909 rx_frame.an_rx_payload_len);
911 fc1=rx_frame.an_frame_ctl >> 8;
912 ieee80211_header_len =
913 sizeof(struct ieee80211_frame);
914 if ((fc1 & IEEE80211_FC1_DIR_TODS) &&
915 (fc1 & IEEE80211_FC1_DIR_FROMDS)) {
916 ieee80211_header_len += ETHER_ADDR_LEN;
919 len = rx_frame.an_rx_payload_len
920 + ieee80211_header_len;
921 /* Check for insane frame length */
922 if (len > sizeof(sc->buf_802_11)) {
923 if_printf(ifp, "oversized packet "
924 "received (%d, %d)\n",
930 ih = (struct ieee80211_frame *)bpf_buf;
932 bcopy((char *)&rx_frame.an_frame_ctl,
933 (char *)ih, ieee80211_header_len);
935 error = an_read_data(sc, id, sizeof(rx_frame) +
937 (caddr_t)ih +ieee80211_header_len,
938 rx_frame.an_rx_payload_len);
940 /* dump raw 802.11 packet to bpf and skip ip stack */
941 BPF_TAP(ifp, bpf_buf, len);
943 MGETHDR(m, M_NOWAIT, MT_DATA);
949 if (!(m->m_flags & M_EXT)) {
954 m->m_pkthdr.rcvif = ifp;
955 /* Read Ethernet encapsulated packet */
958 /* Read NIC frame header */
959 if (an_read_data(sc, id, 0, (caddr_t)&rx_frame,
966 /* Read in the 802_3 frame header */
967 if (an_read_data(sc, id, 0x34,
968 (caddr_t)&rx_frame_802_3,
969 sizeof(rx_frame_802_3))) {
974 if (rx_frame_802_3.an_rx_802_3_status != 0) {
979 /* Check for insane frame length */
980 len = rx_frame_802_3.an_rx_802_3_payload_len;
981 if (len > sizeof(sc->buf_802_11)) {
983 if_printf(ifp, "oversized packet "
984 "received (%d, %d)\n",
989 m->m_pkthdr.len = m->m_len =
990 rx_frame_802_3.an_rx_802_3_payload_len + 12;
992 eh = mtod(m, struct ether_header *);
994 bcopy((char *)&rx_frame_802_3.an_rx_dst_addr,
995 (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
996 bcopy((char *)&rx_frame_802_3.an_rx_src_addr,
997 (char *)&eh->ether_shost, ETHER_ADDR_LEN);
999 /* in mbuf header type is just before payload */
1000 error = an_read_data(sc, id, 0x44,
1001 (caddr_t)&(eh->ether_type),
1002 rx_frame_802_3.an_rx_802_3_payload_len);
1011 /* Receive packet. */
1013 an_cache_store(sc, eh, m,
1014 rx_frame.an_rx_signal_strength,
1018 (*ifp->if_input)(ifp, m);
1022 } else { /* MPI-350 */
1023 for (count = 0; count < AN_MAX_RX_DESC; count++){
1024 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1025 ((u_int32_t *)(void *)&an_rx_desc)[i]
1026 = CSR_MEM_AUX_READ_4(sc,
1028 + (count * sizeof(an_rx_desc))
1031 if (an_rx_desc.an_done && !an_rx_desc.an_valid) {
1032 buf = sc->an_rx_buffer[count].an_dma_vaddr;
1034 MGETHDR(m, M_NOWAIT, MT_DATA);
1039 MCLGET(m, M_NOWAIT);
1040 if (!(m->m_flags & M_EXT)) {
1045 m->m_pkthdr.rcvif = ifp;
1046 /* Read Ethernet encapsulated packet */
1049 * No ANCACHE support since we just get back
1050 * an Ethernet packet no 802.11 info
1054 /* Read NIC frame header */
1055 bcopy(buf, (caddr_t)&rx_frame,
1059 /* Check for insane frame length */
1060 len = an_rx_desc.an_len + 12;
1061 if (len > MCLBYTES) {
1063 if_printf(ifp, "oversized packet "
1064 "received (%d, %d)\n",
1070 m->m_pkthdr.len = m->m_len =
1071 an_rx_desc.an_len + 12;
1073 eh = mtod(m, struct ether_header *);
1075 bcopy(buf, (char *)eh,
1080 /* Receive packet. */
1083 an_cache_store(sc, eh, m,
1084 rx_frame.an_rx_signal_strength,
1089 (*ifp->if_input)(ifp, m);
1092 an_rx_desc.an_valid = 1;
1093 an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
1094 an_rx_desc.an_done = 0;
1095 an_rx_desc.an_phys =
1096 sc->an_rx_buffer[count].an_dma_paddr;
1098 for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1099 CSR_MEM_AUX_WRITE_4(sc,
1101 + (count * sizeof(an_rx_desc))
1103 ((u_int32_t *)(void *)&an_rx_desc)[i]);
1106 if_printf(ifp, "Didn't get valid RX packet "
1109 an_rx_desc.an_valid, an_rx_desc.an_len);
1116 an_txeof(struct an_softc *sc, int status)
1125 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1128 id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1130 if (status & AN_EV_TX_EXC) {
1135 for (i = 0; i < AN_TX_RING_CNT; i++) {
1136 if (id == sc->an_rdata.an_tx_ring[i]) {
1137 sc->an_rdata.an_tx_ring[i] = 0;
1142 AN_INC(sc->an_rdata.an_tx_cons, AN_TX_RING_CNT);
1143 } else { /* MPI 350 */
1144 id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1145 if (!sc->an_rdata.an_tx_empty){
1146 if (status & AN_EV_TX_EXC) {
1150 AN_INC(sc->an_rdata.an_tx_cons, AN_MAX_TX_DESC);
1151 if (sc->an_rdata.an_tx_prod ==
1152 sc->an_rdata.an_tx_cons)
1153 sc->an_rdata.an_tx_empty = 1;
1161 * We abuse the stats updater to check the current NIC status. This
1162 * is important because we don't want to allow transmissions until
1163 * the NIC has synchronized to the current cell (either as the master
1164 * in an ad-hoc group, or as a station connected to an access point).
1166 * Note that this function will be called via callout(9) with a lock held.
1169 an_stats_update(void *xsc)
1171 struct an_softc *sc;
1177 if (sc->an_timer > 0 && --sc->an_timer == 0)
1180 sc->an_status.an_type = AN_RID_STATUS;
1181 sc->an_status.an_len = sizeof(struct an_ltv_status);
1182 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_status))
1185 if (sc->an_status.an_opmode & AN_STATUS_OPMODE_IN_SYNC)
1186 sc->an_associated = 1;
1188 sc->an_associated = 0;
1190 /* Don't do this while we're transmitting */
1191 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1192 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1196 sc->an_stats.an_len = sizeof(struct an_ltv_stats);
1197 sc->an_stats.an_type = AN_RID_32BITS_CUM;
1198 if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_stats.an_len))
1201 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1209 struct an_softc *sc;
1213 sc = (struct an_softc*)xsc;
1224 /* Disable interrupts. */
1225 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
1227 status = CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350));
1228 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), ~AN_INTRS(sc->mpi350));
1230 if (status & AN_EV_MIC) {
1231 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_MIC);
1234 if (status & AN_EV_LINKSTAT) {
1235 if (CSR_READ_2(sc, AN_LINKSTAT(sc->mpi350))
1236 == AN_LINKSTAT_ASSOCIATED)
1237 sc->an_associated = 1;
1239 sc->an_associated = 0;
1240 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_LINKSTAT);
1243 if (status & AN_EV_RX) {
1245 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_RX);
1248 if (sc->mpi350 && status & AN_EV_TX_CPY) {
1249 an_txeof(sc, status);
1250 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_CPY);
1253 if (status & AN_EV_TX) {
1254 an_txeof(sc, status);
1255 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX);
1258 if (status & AN_EV_TX_EXC) {
1259 an_txeof(sc, status);
1260 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_EXC);
1263 if (status & AN_EV_ALLOC)
1264 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1266 /* Re-enable interrupts. */
1267 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
1269 if ((ifp->if_flags & IFF_UP) && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1270 an_start_locked(ifp);
1279 an_cmd_struct(struct an_softc *sc, struct an_command *cmd,
1280 struct an_reply *reply)
1285 for (i = 0; i != AN_TIMEOUT; i++) {
1286 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
1292 if( i == AN_TIMEOUT) {
1297 CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), cmd->an_parm0);
1298 CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), cmd->an_parm1);
1299 CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), cmd->an_parm2);
1300 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd->an_cmd);
1302 for (i = 0; i < AN_TIMEOUT; i++) {
1303 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1308 reply->an_resp0 = CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1309 reply->an_resp1 = CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1310 reply->an_resp2 = CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1311 reply->an_status = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1313 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1314 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
1315 AN_EV_CLR_STUCK_BUSY);
1317 /* Ack the command */
1318 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1320 if (i == AN_TIMEOUT)
1327 an_cmd(struct an_softc *sc, int cmd, int val)
1332 CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), val);
1333 CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), 0);
1334 CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), 0);
1335 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1337 for (i = 0; i < AN_TIMEOUT; i++) {
1338 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1341 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) == cmd)
1342 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1346 for (i = 0; i < AN_TIMEOUT; i++) {
1347 CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1348 CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1349 CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1350 s = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1351 if ((s & AN_STAT_CMD_CODE) == (cmd & AN_STAT_CMD_CODE))
1355 /* Ack the command */
1356 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1358 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1359 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CLR_STUCK_BUSY);
1361 if (i == AN_TIMEOUT)
1368 * This reset sequence may look a little strange, but this is the
1369 * most reliable method I've found to really kick the NIC in the
1370 * head and force it to reboot correctly.
1373 an_reset(struct an_softc *sc)
1379 an_cmd(sc, AN_CMD_ENABLE, 0);
1380 an_cmd(sc, AN_CMD_FW_RESTART, 0);
1381 an_cmd(sc, AN_CMD_NOOP2, 0);
1383 if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT)
1384 device_printf(sc->an_dev, "reset failed\n");
1386 an_cmd(sc, AN_CMD_DISABLE, 0);
1392 * Read an LTV record from the NIC.
1395 an_read_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1397 struct an_ltv_gen *an_ltv;
1398 struct an_card_rid_desc an_rid_desc;
1399 struct an_command cmd;
1400 struct an_reply reply;
1407 if (ltv->an_len < 4 || ltv->an_type == 0)
1412 /* Tell the NIC to enter record read mode. */
1413 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type)) {
1414 if_printf(ifp, "RID access failed\n");
1418 /* Seek to the record. */
1419 if (an_seek(sc, ltv->an_type, 0, AN_BAP1)) {
1420 if_printf(ifp, "seek to record failed\n");
1425 * Read the length and record type and make sure they
1426 * match what we expect (this verifies that we have enough
1427 * room to hold all of the returned data).
1428 * Length includes type but not length.
1430 len = CSR_READ_2(sc, AN_DATA1);
1431 if (len > (ltv->an_len - 2)) {
1432 if_printf(ifp, "record length mismatch -- expected %d, "
1433 "got %d for Rid %x\n",
1434 ltv->an_len - 2, len, ltv->an_type);
1435 len = ltv->an_len - 2;
1437 ltv->an_len = len + 2;
1440 /* Now read the data. */
1441 len -= 2; /* skip the type */
1443 for (i = len; i > 1; i -= 2)
1444 *ptr++ = CSR_READ_2(sc, AN_DATA1);
1446 ptr2 = (u_int8_t *)ptr;
1447 *ptr2 = CSR_READ_1(sc, AN_DATA1);
1449 } else { /* MPI-350 */
1450 if (!sc->an_rid_buffer.an_dma_vaddr)
1452 an_rid_desc.an_valid = 1;
1453 an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
1454 an_rid_desc.an_rid = 0;
1455 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1456 bzero(sc->an_rid_buffer.an_dma_vaddr, AN_RID_BUFFER_SIZE);
1458 bzero(&cmd, sizeof(cmd));
1459 bzero(&reply, sizeof(reply));
1460 cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_READ;
1461 cmd.an_parm0 = ltv->an_type;
1463 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1464 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1465 ((u_int32_t *)(void *)&an_rid_desc)[i]);
1467 if (an_cmd_struct(sc, &cmd, &reply)
1468 || reply.an_status & AN_CMD_QUAL_MASK) {
1469 if_printf(ifp, "failed to read RID %x %x %x %x %x, %d\n",
1479 an_ltv = (struct an_ltv_gen *)sc->an_rid_buffer.an_dma_vaddr;
1480 if (an_ltv->an_len + 2 < an_rid_desc.an_len) {
1481 an_rid_desc.an_len = an_ltv->an_len;
1484 len = an_rid_desc.an_len;
1485 if (len > (ltv->an_len - 2)) {
1486 if_printf(ifp, "record length mismatch -- expected %d, "
1487 "got %d for Rid %x\n",
1488 ltv->an_len - 2, len, ltv->an_type);
1489 len = ltv->an_len - 2;
1491 ltv->an_len = len + 2;
1493 bcopy(&an_ltv->an_type,
1499 an_dump_record(sc, ltv, "Read");
1505 * Same as read, except we inject data instead of reading it.
1508 an_write_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1510 struct an_card_rid_desc an_rid_desc;
1511 struct an_command cmd;
1512 struct an_reply reply;
1519 an_dump_record(sc, ltv, "Write");
1522 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type))
1525 if (an_seek(sc, ltv->an_type, 0, AN_BAP1))
1529 * Length includes type but not length.
1531 len = ltv->an_len - 2;
1532 CSR_WRITE_2(sc, AN_DATA1, len);
1534 len -= 2; /* skip the type */
1536 for (i = len; i > 1; i -= 2)
1537 CSR_WRITE_2(sc, AN_DATA1, *ptr++);
1539 ptr2 = (u_int8_t *)ptr;
1540 CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1543 if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_WRITE, ltv->an_type))
1548 for (i = 0; i != AN_TIMEOUT; i++) {
1549 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350))
1555 if (i == AN_TIMEOUT) {
1559 an_rid_desc.an_valid = 1;
1560 an_rid_desc.an_len = ltv->an_len - 2;
1561 an_rid_desc.an_rid = ltv->an_type;
1562 an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1564 bcopy(<v->an_type, sc->an_rid_buffer.an_dma_vaddr,
1565 an_rid_desc.an_len);
1567 bzero(&cmd,sizeof(cmd));
1568 bzero(&reply,sizeof(reply));
1569 cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_WRITE;
1570 cmd.an_parm0 = ltv->an_type;
1572 for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1573 CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1574 ((u_int32_t *)(void *)&an_rid_desc)[i]);
1578 if ((i = an_cmd_struct(sc, &cmd, &reply))) {
1579 if_printf(sc->an_ifp,
1580 "failed to write RID 1 %x %x %x %x %x, %d\n",
1591 if (reply.an_status & AN_CMD_QUAL_MASK) {
1592 if_printf(sc->an_ifp,
1593 "failed to write RID 2 %x %x %x %x %x, %d\n",
1609 an_dump_record(struct an_softc *sc, struct an_ltv_gen *ltv, char *string)
1617 len = ltv->an_len - 4;
1618 if_printf(sc->an_ifp, "RID %4x, Length %4d, Mode %s\n",
1619 ltv->an_type, ltv->an_len - 4, string);
1621 if (an_dump == 1 || (an_dump == ltv->an_type)) {
1622 if_printf(sc->an_ifp, "\t");
1623 bzero(buf,sizeof(buf));
1625 ptr2 = (u_int8_t *)<v->an_val;
1626 for (i = len; i > 0; i--) {
1627 printf("%02x ", *ptr2);
1634 if (++count == 16) {
1637 if_printf(sc->an_ifp, "\t");
1638 bzero(buf,sizeof(buf));
1641 for (; count != 16; count++) {
1644 printf(" %s\n",buf);
1649 an_seek(struct an_softc *sc, int id, int off, int chan)
1664 if_printf(sc->an_ifp, "invalid data path: %x\n", chan);
1668 CSR_WRITE_2(sc, selreg, id);
1669 CSR_WRITE_2(sc, offreg, off);
1671 for (i = 0; i < AN_TIMEOUT; i++) {
1672 if (!(CSR_READ_2(sc, offreg) & (AN_OFF_BUSY|AN_OFF_ERR)))
1676 if (i == AN_TIMEOUT)
1683 an_read_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1690 if (an_seek(sc, id, off, AN_BAP1))
1694 ptr = (u_int16_t *)buf;
1695 for (i = len; i > 1; i -= 2)
1696 *ptr++ = CSR_READ_2(sc, AN_DATA1);
1698 ptr2 = (u_int8_t *)ptr;
1699 *ptr2 = CSR_READ_1(sc, AN_DATA1);
1706 an_write_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1713 if (an_seek(sc, id, off, AN_BAP0))
1717 ptr = (u_int16_t *)buf;
1718 for (i = len; i > 1; i -= 2)
1719 CSR_WRITE_2(sc, AN_DATA0, *ptr++);
1721 ptr2 = (u_int8_t *)ptr;
1722 CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1729 * Allocate a region of memory inside the NIC and zero
1733 an_alloc_nicmem(struct an_softc *sc, int len, int *id)
1737 if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1738 if_printf(sc->an_ifp, "failed to allocate %d bytes on NIC\n",
1743 for (i = 0; i < AN_TIMEOUT; i++) {
1744 if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_ALLOC)
1748 if (i == AN_TIMEOUT)
1751 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1752 *id = CSR_READ_2(sc, AN_ALLOC_FID);
1754 if (an_seek(sc, *id, 0, AN_BAP0))
1757 for (i = 0; i < len / 2; i++)
1758 CSR_WRITE_2(sc, AN_DATA0, 0);
1764 an_setdef(struct an_softc *sc, struct an_req *areq)
1767 struct an_ltv_genconfig *cfg;
1768 struct an_ltv_ssidlist_new *ssid;
1769 struct an_ltv_aplist *ap;
1770 struct an_ltv_gen *sp;
1775 switch (areq->an_type) {
1776 case AN_RID_GENCONFIG:
1777 cfg = (struct an_ltv_genconfig *)areq;
1779 bcopy((char *)&cfg->an_macaddr, IF_LLADDR(sc->an_ifp),
1782 bcopy((char *)cfg, (char *)&sc->an_config,
1783 sizeof(struct an_ltv_genconfig));
1785 case AN_RID_SSIDLIST:
1786 ssid = (struct an_ltv_ssidlist_new *)areq;
1787 bcopy((char *)ssid, (char *)&sc->an_ssidlist,
1788 sizeof(struct an_ltv_ssidlist_new));
1791 ap = (struct an_ltv_aplist *)areq;
1792 bcopy((char *)ap, (char *)&sc->an_aplist,
1793 sizeof(struct an_ltv_aplist));
1795 case AN_RID_TX_SPEED:
1796 sp = (struct an_ltv_gen *)areq;
1797 sc->an_tx_rate = sp->an_val;
1799 /* Read the current configuration */
1800 sc->an_config.an_type = AN_RID_GENCONFIG;
1801 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1802 an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
1803 cfg = &sc->an_config;
1805 /* clear other rates and set the only one we want */
1806 bzero(cfg->an_rates, sizeof(cfg->an_rates));
1807 cfg->an_rates[0] = sc->an_tx_rate;
1809 /* Save the new rate */
1810 sc->an_config.an_type = AN_RID_GENCONFIG;
1811 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1813 case AN_RID_WEP_TEMP:
1814 /* Cache the temp keys */
1816 &sc->an_temp_keys[((struct an_ltv_key *)areq)->kindex],
1817 sizeof(struct an_ltv_key));
1818 case AN_RID_WEP_PERM:
1819 case AN_RID_LEAPUSERNAME:
1820 case AN_RID_LEAPPASSWORD:
1823 /* Disable the MAC. */
1824 an_cmd(sc, AN_CMD_DISABLE, 0);
1827 an_write_record(sc, (struct an_ltv_gen *)areq);
1829 /* Turn the MAC back on. */
1830 an_cmd(sc, AN_CMD_ENABLE, 0);
1833 case AN_RID_MONITOR_MODE:
1834 cfg = (struct an_ltv_genconfig *)areq;
1836 if (ng_ether_detach_p != NULL)
1837 (*ng_ether_detach_p) (ifp);
1838 sc->an_monitor = cfg->an_len;
1840 if (sc->an_monitor & AN_MONITOR) {
1841 if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
1842 bpfattach(ifp, DLT_AIRONET_HEADER,
1843 sizeof(struct ether_header));
1845 bpfattach(ifp, DLT_IEEE802_11,
1846 sizeof(struct ether_header));
1849 bpfattach(ifp, DLT_EN10MB,
1850 sizeof(struct ether_header));
1851 if (ng_ether_attach_p != NULL)
1852 (*ng_ether_attach_p) (ifp);
1856 if_printf(ifp, "unknown RID: %x\n", areq->an_type);
1861 /* Reinitialize the card. */
1869 * Derived from Linux driver to enable promiscious mode.
1873 an_promisc(struct an_softc *sc, int promisc)
1876 if (sc->an_was_monitor) {
1879 an_init_mpi350_desc(sc);
1881 if (sc->an_monitor || sc->an_was_monitor)
1884 sc->an_was_monitor = sc->an_monitor;
1885 an_cmd(sc, AN_CMD_SET_MODE, promisc ? 0xffff : 0);
1891 an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1896 struct an_softc *sc;
1898 struct thread *td = curthread;
1899 struct ieee80211req *ireq;
1900 struct ieee80211_channel ch;
1901 u_int8_t tmpstr[IEEE80211_NWID_LEN*2];
1903 struct an_ltv_genconfig *config;
1904 struct an_ltv_key *key;
1905 struct an_ltv_status *status;
1906 struct an_ltv_ssidlist_new *ssids;
1908 struct aironet_ioctl l_ioctl;
1911 ifr = (struct ifreq *)data;
1912 ireq = (struct ieee80211req *)data;
1914 config = (struct an_ltv_genconfig *)&sc->areq;
1915 key = (struct an_ltv_key *)&sc->areq;
1916 status = (struct an_ltv_status *)&sc->areq;
1917 ssids = (struct an_ltv_ssidlist_new *)&sc->areq;
1927 if (ifp->if_flags & IFF_UP) {
1928 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1929 ifp->if_flags & IFF_PROMISC &&
1930 !(sc->an_if_flags & IFF_PROMISC)) {
1932 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1933 !(ifp->if_flags & IFF_PROMISC) &&
1934 sc->an_if_flags & IFF_PROMISC) {
1939 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1942 sc->an_if_flags = ifp->if_flags;
1948 error = ifmedia_ioctl(ifp, ifr, &sc->an_ifmedia, command);
1952 /* The Aironet has no multicast filter. */
1956 error = copyin(ifr->ifr_data, &sc->areq, sizeof(sc->areq));
1961 if (sc->areq.an_type == AN_RID_ZERO_CACHE) {
1962 error = priv_check(td, PRIV_DRIVER);
1965 sc->an_sigitems = sc->an_nextitem = 0;
1967 } else if (sc->areq.an_type == AN_RID_READ_CACHE) {
1968 char *pt = (char *)&sc->areq.an_val;
1969 bcopy((char *)&sc->an_sigitems, (char *)pt,
1972 sc->areq.an_len = sizeof(int) / 2;
1973 bcopy((char *)&sc->an_sigcache, (char *)pt,
1974 sizeof(struct an_sigcache) * sc->an_sigitems);
1975 sc->areq.an_len += ((sizeof(struct an_sigcache) *
1976 sc->an_sigitems) / 2) + 1;
1979 if (an_read_record(sc, (struct an_ltv_gen *)&sc->areq)) {
1985 error = copyout(&sc->areq, ifr->ifr_data, sizeof(sc->areq));
1988 if ((error = priv_check(td, PRIV_DRIVER)))
1991 error = copyin(ifr->ifr_data, &sc->areq, sizeof(sc->areq));
1994 an_setdef(sc, &sc->areq);
1997 case SIOCGPRIVATE_0: /* used by Cisco client utility */
1998 if ((error = priv_check(td, PRIV_DRIVER)))
2000 error = copyin(ifr->ifr_data, &l_ioctl, sizeof(l_ioctl));
2003 mode = l_ioctl.command;
2006 if (mode >= AIROGCAP && mode <= AIROGSTATSD32) {
2007 error = readrids(ifp, &l_ioctl);
2008 } else if (mode >= AIROPCAP && mode <= AIROPLEAPUSR) {
2009 error = writerids(ifp, &l_ioctl);
2010 } else if (mode >= AIROFLSHRST && mode <= AIRORESTART) {
2011 error = flashcard(ifp, &l_ioctl);
2017 /* copy out the updated command info */
2018 error = copyout(&l_ioctl, ifr->ifr_data, sizeof(l_ioctl));
2021 case SIOCGPRIVATE_1: /* used by Cisco client utility */
2022 if ((error = priv_check(td, PRIV_DRIVER)))
2024 error = copyin(ifr->ifr_data, &l_ioctl, sizeof(l_ioctl));
2027 l_ioctl.command = 0;
2029 (void) copyout(&error, l_ioctl.data, sizeof(error));
2033 sc->areq.an_len = sizeof(sc->areq);
2034 /* was that a good idea DJA we are doing a short-cut */
2035 switch (ireq->i_type) {
2036 case IEEE80211_IOC_SSID:
2038 if (ireq->i_val == -1) {
2039 sc->areq.an_type = AN_RID_STATUS;
2040 if (an_read_record(sc,
2041 (struct an_ltv_gen *)&sc->areq)) {
2046 len = status->an_ssidlen;
2047 tmpptr = status->an_ssid;
2048 } else if (ireq->i_val >= 0) {
2049 sc->areq.an_type = AN_RID_SSIDLIST;
2050 if (an_read_record(sc,
2051 (struct an_ltv_gen *)&sc->areq)) {
2056 max = (sc->areq.an_len - 4)
2057 / sizeof(struct an_ltv_ssid_entry);
2058 if ( max > MAX_SSIDS ) {
2059 printf("To many SSIDs only using "
2064 if (ireq->i_val > max) {
2069 len = ssids->an_entry[ireq->i_val].an_len;
2070 tmpptr = ssids->an_entry[ireq->i_val].an_ssid;
2077 if (len > IEEE80211_NWID_LEN) {
2084 bzero(tmpstr, IEEE80211_NWID_LEN);
2085 bcopy(tmpptr, tmpstr, len);
2086 error = copyout(tmpstr, ireq->i_data,
2087 IEEE80211_NWID_LEN);
2089 case IEEE80211_IOC_NUMSSIDS:
2091 sc->areq.an_len = sizeof(sc->areq);
2092 sc->areq.an_type = AN_RID_SSIDLIST;
2093 if (an_read_record(sc,
2094 (struct an_ltv_gen *)&sc->areq)) {
2099 max = (sc->areq.an_len - 4)
2100 / sizeof(struct an_ltv_ssid_entry);
2102 if ( max > MAX_SSIDS ) {
2103 printf("To many SSIDs only using "
2110 case IEEE80211_IOC_WEP:
2112 sc->areq.an_type = AN_RID_ACTUALCFG;
2113 if (an_read_record(sc,
2114 (struct an_ltv_gen *)&sc->areq)) {
2120 if (config->an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE) {
2121 if (config->an_authtype &
2122 AN_AUTHTYPE_ALLOW_UNENCRYPTED)
2123 ireq->i_val = IEEE80211_WEP_MIXED;
2125 ireq->i_val = IEEE80211_WEP_ON;
2127 ireq->i_val = IEEE80211_WEP_OFF;
2130 case IEEE80211_IOC_WEPKEY:
2132 * XXX: I'm not entierly convinced this is
2133 * correct, but it's what is implemented in
2134 * ancontrol so it will have to do until we get
2135 * access to actual Cisco code.
2137 if (ireq->i_val < 0 || ireq->i_val > 8) {
2142 if (ireq->i_val < 5) {
2144 sc->areq.an_type = AN_RID_WEP_TEMP;
2145 for (i = 0; i < 5; i++) {
2146 if (an_read_record(sc,
2147 (struct an_ltv_gen *)&sc->areq)) {
2151 if (key->kindex == 0xffff)
2153 if (key->kindex == ireq->i_val)
2155 /* Required to get next entry */
2156 sc->areq.an_type = AN_RID_WEP_PERM;
2163 /* We aren't allowed to read the value of the
2164 * key from the card so we just output zeros
2165 * like we would if we could read the card, but
2166 * denied the user access.
2170 error = copyout(tmpstr, ireq->i_data, len);
2172 case IEEE80211_IOC_NUMWEPKEYS:
2173 ireq->i_val = 9; /* include home key */
2175 case IEEE80211_IOC_WEPTXKEY:
2177 * For some strange reason, you have to read all
2178 * keys before you can read the txkey.
2181 sc->areq.an_type = AN_RID_WEP_TEMP;
2182 for (i = 0; i < 5; i++) {
2183 if (an_read_record(sc,
2184 (struct an_ltv_gen *) &sc->areq)) {
2188 if (key->kindex == 0xffff) {
2191 /* Required to get next entry */
2192 sc->areq.an_type = AN_RID_WEP_PERM;
2199 sc->areq.an_type = AN_RID_WEP_PERM;
2200 key->kindex = 0xffff;
2201 if (an_read_record(sc,
2202 (struct an_ltv_gen *)&sc->areq)) {
2207 ireq->i_val = key->mac[0];
2209 * Check for home mode. Map home mode into
2210 * 5th key since that is how it is stored on
2213 sc->areq.an_len = sizeof(struct an_ltv_genconfig);
2214 sc->areq.an_type = AN_RID_GENCONFIG;
2215 if (an_read_record(sc,
2216 (struct an_ltv_gen *)&sc->areq)) {
2221 if (config->an_home_product & AN_HOME_NETWORK)
2225 case IEEE80211_IOC_AUTHMODE:
2227 sc->areq.an_type = AN_RID_ACTUALCFG;
2228 if (an_read_record(sc,
2229 (struct an_ltv_gen *)&sc->areq)) {
2235 if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2237 ireq->i_val = IEEE80211_AUTH_NONE;
2238 } else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2240 ireq->i_val = IEEE80211_AUTH_OPEN;
2241 } else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2242 AN_AUTHTYPE_SHAREDKEY) {
2243 ireq->i_val = IEEE80211_AUTH_SHARED;
2247 case IEEE80211_IOC_STATIONNAME:
2249 sc->areq.an_type = AN_RID_ACTUALCFG;
2250 if (an_read_record(sc,
2251 (struct an_ltv_gen *)&sc->areq)) {
2257 ireq->i_len = sizeof(config->an_nodename);
2258 tmpptr = config->an_nodename;
2259 bzero(tmpstr, IEEE80211_NWID_LEN);
2260 bcopy(tmpptr, tmpstr, ireq->i_len);
2261 error = copyout(tmpstr, ireq->i_data,
2262 IEEE80211_NWID_LEN);
2264 case IEEE80211_IOC_CHANNEL:
2266 sc->areq.an_type = AN_RID_STATUS;
2267 if (an_read_record(sc,
2268 (struct an_ltv_gen *)&sc->areq)) {
2274 ireq->i_val = status->an_cur_channel;
2276 case IEEE80211_IOC_CURCHAN:
2278 sc->areq.an_type = AN_RID_STATUS;
2279 if (an_read_record(sc,
2280 (struct an_ltv_gen *)&sc->areq)) {
2286 bzero(&ch, sizeof(ch));
2287 ch.ic_freq = ieee80211_ieee2mhz(status->an_cur_channel,
2289 ch.ic_flags = IEEE80211_CHAN_B;
2290 ch.ic_ieee = status->an_cur_channel;
2291 error = copyout(&ch, ireq->i_data, sizeof(ch));
2293 case IEEE80211_IOC_POWERSAVE:
2295 sc->areq.an_type = AN_RID_ACTUALCFG;
2296 if (an_read_record(sc,
2297 (struct an_ltv_gen *)&sc->areq)) {
2303 if (config->an_psave_mode == AN_PSAVE_NONE) {
2304 ireq->i_val = IEEE80211_POWERSAVE_OFF;
2305 } else if (config->an_psave_mode == AN_PSAVE_CAM) {
2306 ireq->i_val = IEEE80211_POWERSAVE_CAM;
2307 } else if (config->an_psave_mode == AN_PSAVE_PSP) {
2308 ireq->i_val = IEEE80211_POWERSAVE_PSP;
2309 } else if (config->an_psave_mode == AN_PSAVE_PSP_CAM) {
2310 ireq->i_val = IEEE80211_POWERSAVE_PSP_CAM;
2314 case IEEE80211_IOC_POWERSAVESLEEP:
2316 sc->areq.an_type = AN_RID_ACTUALCFG;
2317 if (an_read_record(sc,
2318 (struct an_ltv_gen *)&sc->areq)) {
2324 ireq->i_val = config->an_listen_interval;
2329 if ((error = priv_check(td, PRIV_NET80211_MANAGE)))
2332 sc->areq.an_len = sizeof(sc->areq);
2334 * We need a config structure for everything but the WEP
2335 * key management and SSIDs so we get it now so avoid
2336 * duplicating this code every time.
2338 if (ireq->i_type != IEEE80211_IOC_SSID &&
2339 ireq->i_type != IEEE80211_IOC_WEPKEY &&
2340 ireq->i_type != IEEE80211_IOC_WEPTXKEY) {
2341 sc->areq.an_type = AN_RID_GENCONFIG;
2342 if (an_read_record(sc,
2343 (struct an_ltv_gen *)&sc->areq)) {
2349 switch (ireq->i_type) {
2350 case IEEE80211_IOC_SSID:
2351 sc->areq.an_len = sizeof(sc->areq);
2352 sc->areq.an_type = AN_RID_SSIDLIST;
2353 if (an_read_record(sc,
2354 (struct an_ltv_gen *)&sc->areq)) {
2359 if (ireq->i_len > IEEE80211_NWID_LEN) {
2364 max = (sc->areq.an_len - 4)
2365 / sizeof(struct an_ltv_ssid_entry);
2366 if ( max > MAX_SSIDS ) {
2367 printf("To many SSIDs only using "
2372 if (ireq->i_val > max) {
2377 error = copyin(ireq->i_data,
2378 ssids->an_entry[ireq->i_val].an_ssid,
2380 ssids->an_entry[ireq->i_val].an_len
2382 sc->areq.an_len = sizeof(sc->areq);
2383 sc->areq.an_type = AN_RID_SSIDLIST;
2384 an_setdef(sc, &sc->areq);
2389 case IEEE80211_IOC_WEP:
2390 switch (ireq->i_val) {
2391 case IEEE80211_WEP_OFF:
2392 config->an_authtype &=
2393 ~(AN_AUTHTYPE_PRIVACY_IN_USE |
2394 AN_AUTHTYPE_ALLOW_UNENCRYPTED);
2396 case IEEE80211_WEP_ON:
2397 config->an_authtype |=
2398 AN_AUTHTYPE_PRIVACY_IN_USE;
2399 config->an_authtype &=
2400 ~AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2402 case IEEE80211_WEP_MIXED:
2403 config->an_authtype |=
2404 AN_AUTHTYPE_PRIVACY_IN_USE |
2405 AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2411 if (error != EINVAL)
2412 an_setdef(sc, &sc->areq);
2415 case IEEE80211_IOC_WEPKEY:
2416 if (ireq->i_val < 0 || ireq->i_val > 8 ||
2422 error = copyin(ireq->i_data, tmpstr, 13);
2428 * Map the 9th key into the home mode
2429 * since that is how it is stored on
2432 bzero(&sc->areq, sizeof(struct an_ltv_key));
2433 sc->areq.an_len = sizeof(struct an_ltv_key);
2434 key->mac[0] = 1; /* The others are 0. */
2435 if (ireq->i_val < 4) {
2436 sc->areq.an_type = AN_RID_WEP_TEMP;
2437 key->kindex = ireq->i_val;
2439 sc->areq.an_type = AN_RID_WEP_PERM;
2440 key->kindex = ireq->i_val - 4;
2442 key->klen = ireq->i_len;
2443 bcopy(tmpstr, key->key, key->klen);
2444 an_setdef(sc, &sc->areq);
2447 case IEEE80211_IOC_WEPTXKEY:
2448 if (ireq->i_val < 0 || ireq->i_val > 4) {
2455 * Map the 5th key into the home mode
2456 * since that is how it is stored on
2459 sc->areq.an_len = sizeof(struct an_ltv_genconfig);
2460 sc->areq.an_type = AN_RID_ACTUALCFG;
2461 if (an_read_record(sc,
2462 (struct an_ltv_gen *)&sc->areq)) {
2467 if (ireq->i_val == 4) {
2468 config->an_home_product |= AN_HOME_NETWORK;
2471 config->an_home_product &= ~AN_HOME_NETWORK;
2474 sc->an_config.an_home_product
2475 = config->an_home_product;
2477 /* update configuration */
2480 bzero(&sc->areq, sizeof(struct an_ltv_key));
2481 sc->areq.an_len = sizeof(struct an_ltv_key);
2482 sc->areq.an_type = AN_RID_WEP_PERM;
2483 key->kindex = 0xffff;
2484 key->mac[0] = ireq->i_val;
2485 an_setdef(sc, &sc->areq);
2488 case IEEE80211_IOC_AUTHMODE:
2489 switch (ireq->i_val) {
2490 case IEEE80211_AUTH_NONE:
2491 config->an_authtype = AN_AUTHTYPE_NONE |
2492 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2494 case IEEE80211_AUTH_OPEN:
2495 config->an_authtype = AN_AUTHTYPE_OPEN |
2496 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2498 case IEEE80211_AUTH_SHARED:
2499 config->an_authtype = AN_AUTHTYPE_SHAREDKEY |
2500 (config->an_authtype & ~AN_AUTHTYPE_MASK);
2505 if (error != EINVAL) {
2506 an_setdef(sc, &sc->areq);
2510 case IEEE80211_IOC_STATIONNAME:
2511 if (ireq->i_len > 16) {
2516 bzero(config->an_nodename, 16);
2517 error = copyin(ireq->i_data,
2518 config->an_nodename, ireq->i_len);
2519 an_setdef(sc, &sc->areq);
2522 case IEEE80211_IOC_CHANNEL:
2524 * The actual range is 1-14, but if you set it
2525 * to 0 you get the default so we let that work
2528 if (ireq->i_val < 0 || ireq->i_val >14) {
2533 config->an_ds_channel = ireq->i_val;
2534 an_setdef(sc, &sc->areq);
2537 case IEEE80211_IOC_POWERSAVE:
2538 switch (ireq->i_val) {
2539 case IEEE80211_POWERSAVE_OFF:
2540 config->an_psave_mode = AN_PSAVE_NONE;
2542 case IEEE80211_POWERSAVE_CAM:
2543 config->an_psave_mode = AN_PSAVE_CAM;
2545 case IEEE80211_POWERSAVE_PSP:
2546 config->an_psave_mode = AN_PSAVE_PSP;
2548 case IEEE80211_POWERSAVE_PSP_CAM:
2549 config->an_psave_mode = AN_PSAVE_PSP_CAM;
2555 an_setdef(sc, &sc->areq);
2558 case IEEE80211_IOC_POWERSAVESLEEP:
2559 config->an_listen_interval = ireq->i_val;
2560 an_setdef(sc, &sc->areq);
2571 an_setdef(sc, &sc->areq);
2577 error = ether_ioctl(ifp, command, data);
2586 an_init_tx_ring(struct an_softc *sc)
2595 for (i = 0; i < AN_TX_RING_CNT; i++) {
2596 if (an_alloc_nicmem(sc, 1518 +
2599 sc->an_rdata.an_tx_fids[i] = id;
2600 sc->an_rdata.an_tx_ring[i] = 0;
2604 sc->an_rdata.an_tx_prod = 0;
2605 sc->an_rdata.an_tx_cons = 0;
2606 sc->an_rdata.an_tx_empty = 1;
2614 struct an_softc *sc = xsc;
2622 an_init_locked(struct an_softc *sc)
2631 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2634 sc->an_associated = 0;
2636 /* Allocate the TX buffers */
2637 if (an_init_tx_ring(sc)) {
2640 an_init_mpi350_desc(sc);
2641 if (an_init_tx_ring(sc)) {
2642 if_printf(ifp, "tx buffer allocation failed\n");
2647 /* Set our MAC address. */
2648 bcopy((char *)IF_LLADDR(sc->an_ifp),
2649 (char *)&sc->an_config.an_macaddr, ETHER_ADDR_LEN);
2651 if (ifp->if_flags & IFF_BROADCAST)
2652 sc->an_config.an_rxmode = AN_RXMODE_BC_ADDR;
2654 sc->an_config.an_rxmode = AN_RXMODE_ADDR;
2656 if (ifp->if_flags & IFF_MULTICAST)
2657 sc->an_config.an_rxmode = AN_RXMODE_BC_MC_ADDR;
2659 if (ifp->if_flags & IFF_PROMISC) {
2660 if (sc->an_monitor & AN_MONITOR) {
2661 if (sc->an_monitor & AN_MONITOR_ANY_BSS) {
2662 sc->an_config.an_rxmode |=
2663 AN_RXMODE_80211_MONITOR_ANYBSS |
2664 AN_RXMODE_NO_8023_HEADER;
2666 sc->an_config.an_rxmode |=
2667 AN_RXMODE_80211_MONITOR_CURBSS |
2668 AN_RXMODE_NO_8023_HEADER;
2674 if (sc->an_have_rssimap)
2675 sc->an_config.an_rxmode |= AN_RXMODE_NORMALIZED_RSSI;
2678 /* Set the ssid list */
2679 sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
2680 sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
2681 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
2682 if_printf(ifp, "failed to set ssid list\n");
2686 /* Set the AP list */
2687 sc->an_aplist.an_type = AN_RID_APLIST;
2688 sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
2689 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
2690 if_printf(ifp, "failed to set AP list\n");
2694 /* Set the configuration in the NIC */
2695 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
2696 sc->an_config.an_type = AN_RID_GENCONFIG;
2697 if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
2698 if_printf(ifp, "failed to set configuration\n");
2702 /* Enable the MAC */
2703 if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
2704 if_printf(ifp, "failed to enable MAC\n");
2708 if (ifp->if_flags & IFF_PROMISC)
2709 an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
2711 /* enable interrupts */
2712 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2714 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2715 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2717 callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
2723 an_start(struct ifnet *ifp)
2725 struct an_softc *sc;
2729 an_start_locked(ifp);
2734 an_start_locked(struct ifnet *ifp)
2736 struct an_softc *sc;
2737 struct mbuf *m0 = NULL;
2738 struct an_txframe_802_3 tx_frame_802_3;
2739 struct ether_header *eh;
2741 unsigned char txcontrol;
2742 struct an_card_tx_desc an_tx_desc;
2751 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
2754 if (!sc->an_associated)
2757 /* We can't send in monitor mode so toss any attempts. */
2758 if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
2760 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2768 idx = sc->an_rdata.an_tx_prod;
2771 bzero((char *)&tx_frame_802_3, sizeof(tx_frame_802_3));
2773 while (sc->an_rdata.an_tx_ring[idx] == 0) {
2774 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2778 id = sc->an_rdata.an_tx_fids[idx];
2779 eh = mtod(m0, struct ether_header *);
2781 bcopy((char *)&eh->ether_dhost,
2782 (char *)&tx_frame_802_3.an_tx_dst_addr,
2784 bcopy((char *)&eh->ether_shost,
2785 (char *)&tx_frame_802_3.an_tx_src_addr,
2788 /* minus src/dest mac & type */
2789 tx_frame_802_3.an_tx_802_3_payload_len =
2790 m0->m_pkthdr.len - 12;
2792 m_copydata(m0, sizeof(struct ether_header) - 2 ,
2793 tx_frame_802_3.an_tx_802_3_payload_len,
2794 (caddr_t)&sc->an_txbuf);
2796 txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2797 /* write the txcontrol only */
2798 an_write_data(sc, id, 0x08, (caddr_t)&txcontrol,
2802 an_write_data(sc, id, 0x34, (caddr_t)&tx_frame_802_3,
2803 sizeof(struct an_txframe_802_3));
2805 /* in mbuf header type is just before payload */
2806 an_write_data(sc, id, 0x44, (caddr_t)&sc->an_txbuf,
2807 tx_frame_802_3.an_tx_802_3_payload_len);
2810 * If there's a BPF listner, bounce a copy of
2811 * this frame to him.
2818 sc->an_rdata.an_tx_ring[idx] = id;
2819 if (an_cmd(sc, AN_CMD_TX, id))
2820 if_printf(ifp, "xmit failed\n");
2822 AN_INC(idx, AN_TX_RING_CNT);
2825 * Set a timeout in case the chip goes out to lunch.
2829 } else { /* MPI-350 */
2830 /* Disable interrupts. */
2831 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2833 while (sc->an_rdata.an_tx_empty ||
2834 idx != sc->an_rdata.an_tx_cons) {
2835 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2839 buf = sc->an_tx_buffer[idx].an_dma_vaddr;
2841 eh = mtod(m0, struct ether_header *);
2843 /* DJA optimize this to limit bcopy */
2844 bcopy((char *)&eh->ether_dhost,
2845 (char *)&tx_frame_802_3.an_tx_dst_addr,
2847 bcopy((char *)&eh->ether_shost,
2848 (char *)&tx_frame_802_3.an_tx_src_addr,
2851 /* minus src/dest mac & type */
2852 tx_frame_802_3.an_tx_802_3_payload_len =
2853 m0->m_pkthdr.len - 12;
2855 m_copydata(m0, sizeof(struct ether_header) - 2 ,
2856 tx_frame_802_3.an_tx_802_3_payload_len,
2857 (caddr_t)&sc->an_txbuf);
2859 txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2860 /* write the txcontrol only */
2861 bcopy((caddr_t)&txcontrol, &buf[0x08],
2865 bcopy((caddr_t)&tx_frame_802_3, &buf[0x34],
2866 sizeof(struct an_txframe_802_3));
2868 /* in mbuf header type is just before payload */
2869 bcopy((caddr_t)&sc->an_txbuf, &buf[0x44],
2870 tx_frame_802_3.an_tx_802_3_payload_len);
2873 bzero(&an_tx_desc, sizeof(an_tx_desc));
2874 an_tx_desc.an_offset = 0;
2875 an_tx_desc.an_eoc = 1;
2876 an_tx_desc.an_valid = 1;
2877 an_tx_desc.an_len = 0x44 +
2878 tx_frame_802_3.an_tx_802_3_payload_len;
2880 = sc->an_tx_buffer[idx].an_dma_paddr;
2881 for (i = sizeof(an_tx_desc) / 4 - 1; i >= 0; i--) {
2882 CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
2884 + (0 * sizeof(an_tx_desc))
2886 ((u_int32_t *)(void *)&an_tx_desc)[i]);
2890 * If there's a BPF listner, bounce a copy of
2891 * this frame to him.
2897 AN_INC(idx, AN_MAX_TX_DESC);
2898 sc->an_rdata.an_tx_empty = 0;
2899 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
2902 * Set a timeout in case the chip goes out to lunch.
2907 /* Re-enable interrupts. */
2908 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2912 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2914 sc->an_rdata.an_tx_prod = idx;
2920 an_stop(struct an_softc *sc)
2932 an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
2933 CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2934 an_cmd(sc, AN_CMD_DISABLE, 0);
2936 for (i = 0; i < AN_TX_RING_CNT; i++)
2937 an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->an_rdata.an_tx_fids[i]);
2939 callout_stop(&sc->an_stat_ch);
2941 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
2943 if (sc->an_flash_buffer) {
2944 free(sc->an_flash_buffer, M_DEVBUF);
2945 sc->an_flash_buffer = NULL;
2950 an_watchdog(struct an_softc *sc)
2960 if_printf(ifp, "device timeout\n");
2964 an_init_mpi350_desc(sc);
2971 an_shutdown(device_t dev)
2973 struct an_softc *sc;
2975 sc = device_get_softc(dev);
2985 an_resume(device_t dev)
2987 struct an_softc *sc;
2991 sc = device_get_softc(dev);
2998 an_init_mpi350_desc(sc);
3001 /* Recovery temporary keys */
3002 for (i = 0; i < 4; i++) {
3003 sc->areq.an_type = AN_RID_WEP_TEMP;
3004 sc->areq.an_len = sizeof(struct an_ltv_key);
3005 bcopy(&sc->an_temp_keys[i],
3006 &sc->areq, sizeof(struct an_ltv_key));
3007 an_setdef(sc, &sc->areq);
3010 if (ifp->if_flags & IFF_UP)
3011 an_start_locked(ifp);
3018 /* Aironet signal strength cache code.
3019 * store signal/noise/quality on per MAC src basis in
3020 * a small fixed cache. The cache wraps if > MAX slots
3021 * used. The cache may be zeroed out to start over.
3022 * Two simple filters exist to reduce computation:
3023 * 1. ip only (literally 0x800, ETHERTYPE_IP) which may be used
3024 * to ignore some packets. It defaults to ip only.
3025 * it could be used to focus on broadcast, non-IP 802.11 beacons.
3026 * 2. multicast/broadcast only. This may be used to
3027 * ignore unicast packets and only cache signal strength
3028 * for multicast/broadcast packets (beacons); e.g., Mobile-IP
3029 * beacons and not unicast traffic.
3031 * The cache stores (MAC src(index), IP src (major clue), signal,
3034 * No apologies for storing IP src here. It's easy and saves much
3035 * trouble elsewhere. The cache is assumed to be INET dependent,
3036 * although it need not be.
3038 * Note: the Aironet only has a single byte of signal strength value
3039 * in the rx frame header, and it's not scaled to anything sensible.
3040 * This is kind of lame, but it's all we've got.
3043 #ifdef documentation
3045 int an_sigitems; /* number of cached entries */
3046 struct an_sigcache an_sigcache[MAXANCACHE]; /* array of cache entries */
3047 int an_nextitem; /* index/# of entries */
3052 /* control variables for cache filtering. Basic idea is
3053 * to reduce cost (e.g., to only Mobile-IP agent beacons
3054 * which are broadcast or multicast). Still you might
3055 * want to measure signal strength anth unicast ping packets
3056 * on a pt. to pt. ant. setup.
3058 /* set true if you want to limit cache items to broadcast/mcast
3059 * only packets (not unicast). Useful for mobile-ip beacons which
3060 * are broadcast/multicast at network layer. Default is all packets
3061 * so ping/unicast anll work say anth pt. to pt. antennae setup.
3063 static int an_cache_mcastonly = 0;
3064 SYSCTL_INT(_hw_an, OID_AUTO, an_cache_mcastonly, CTLFLAG_RW,
3065 &an_cache_mcastonly, 0, "");
3067 /* set true if you want to limit cache items to IP packets only
3069 static int an_cache_iponly = 1;
3070 SYSCTL_INT(_hw_an, OID_AUTO, an_cache_iponly, CTLFLAG_RW,
3071 &an_cache_iponly, 0, "");
3074 * an_cache_store, per rx packet store signal
3075 * strength in MAC (src) indexed cache.
3078 an_cache_store(struct an_softc *sc, struct ether_header *eh, struct mbuf *m,
3079 u_int8_t rx_rssi, u_int8_t rx_quality)
3083 static int cache_slot = 0; /* use this cache entry */
3084 static int wrapindex = 0; /* next "free" cache entry */
3089 * 2. configurable filter to throw out unicast packets,
3090 * keep multicast only.
3093 if ((ntohs(eh->ether_type) == ETHERTYPE_IP)) {
3097 /* filter for ip packets only
3099 if ( an_cache_iponly && !type_ipv4) {
3103 /* filter for broadcast/multicast only
3105 if (an_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) {
3110 if_printf(sc->an_ifp, "q value %x (MSB=0x%x, LSB=0x%x) \n",
3111 rx_rssi & 0xffff, rx_rssi >> 8, rx_rssi & 0xff);
3114 /* find the ip header. we want to store the ip_src
3118 ip = mtod(m, struct ip *);
3121 /* do a linear search for a matching MAC address
3122 * in the cache table
3123 * . MAC address is 6 bytes,
3124 * . var w_nextitem holds total number of entries already cached
3126 for (i = 0; i < sc->an_nextitem; i++) {
3127 if (! bcmp(eh->ether_shost , sc->an_sigcache[i].macsrc, 6 )) {
3129 * so we already have this entry,
3136 /* did we find a matching mac address?
3137 * if yes, then overwrite a previously existing cache entry
3139 if (i < sc->an_nextitem ) {
3142 /* else, have a new address entry,so
3143 * add this new entry,
3144 * if table full, then we need to replace LRU entry
3148 /* check for space in cache table
3149 * note: an_nextitem also holds number of entries
3150 * added in the cache table
3152 if ( sc->an_nextitem < MAXANCACHE ) {
3153 cache_slot = sc->an_nextitem;
3155 sc->an_sigitems = sc->an_nextitem;
3157 /* no space found, so simply wrap anth wrap index
3158 * and "zap" the next entry
3161 if (wrapindex == MAXANCACHE) {
3164 cache_slot = wrapindex++;
3168 /* invariant: cache_slot now points at some slot
3171 if (cache_slot < 0 || cache_slot >= MAXANCACHE) {
3172 log(LOG_ERR, "an_cache_store, bad index: %d of "
3173 "[0..%d], gross cache error\n",
3174 cache_slot, MAXANCACHE);
3178 /* store items in cache
3179 * .ip source address
3184 sc->an_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr;
3186 bcopy( eh->ether_shost, sc->an_sigcache[cache_slot].macsrc, 6);
3189 switch (an_cache_mode) {
3191 if (sc->an_have_rssimap) {
3192 sc->an_sigcache[cache_slot].signal =
3193 - sc->an_rssimap.an_entries[rx_rssi].an_rss_dbm;
3194 sc->an_sigcache[cache_slot].quality =
3195 - sc->an_rssimap.an_entries[rx_quality].an_rss_dbm;
3197 sc->an_sigcache[cache_slot].signal = rx_rssi - 100;
3198 sc->an_sigcache[cache_slot].quality = rx_quality - 100;
3202 if (sc->an_have_rssimap) {
3203 sc->an_sigcache[cache_slot].signal =
3204 sc->an_rssimap.an_entries[rx_rssi].an_rss_pct;
3205 sc->an_sigcache[cache_slot].quality =
3206 sc->an_rssimap.an_entries[rx_quality].an_rss_pct;
3210 if (rx_quality > 100)
3212 sc->an_sigcache[cache_slot].signal = rx_rssi;
3213 sc->an_sigcache[cache_slot].quality = rx_quality;
3217 sc->an_sigcache[cache_slot].signal = rx_rssi;
3218 sc->an_sigcache[cache_slot].quality = rx_quality;
3222 sc->an_sigcache[cache_slot].noise = 0;
3229 an_media_change(struct ifnet *ifp)
3231 struct an_softc *sc = ifp->if_softc;
3232 struct an_ltv_genconfig *cfg;
3233 int otype = sc->an_config.an_opmode;
3234 int orate = sc->an_tx_rate;
3237 sc->an_tx_rate = ieee80211_media2rate(
3238 IFM_SUBTYPE(sc->an_ifmedia.ifm_cur->ifm_media));
3239 if (sc->an_tx_rate < 0)
3242 if (orate != sc->an_tx_rate) {
3243 /* Read the current configuration */
3244 sc->an_config.an_type = AN_RID_GENCONFIG;
3245 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3246 an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
3247 cfg = &sc->an_config;
3249 /* clear other rates and set the only one we want */
3250 bzero(cfg->an_rates, sizeof(cfg->an_rates));
3251 cfg->an_rates[0] = sc->an_tx_rate;
3253 /* Save the new rate */
3254 sc->an_config.an_type = AN_RID_GENCONFIG;
3255 sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3258 if ((sc->an_ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
3259 sc->an_config.an_opmode &= ~AN_OPMODE_INFRASTRUCTURE_STATION;
3261 sc->an_config.an_opmode |= AN_OPMODE_INFRASTRUCTURE_STATION;
3263 if (otype != sc->an_config.an_opmode ||
3264 orate != sc->an_tx_rate)
3272 an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
3274 struct an_ltv_status status;
3275 struct an_softc *sc = ifp->if_softc;
3277 imr->ifm_active = IFM_IEEE80211;
3280 status.an_len = sizeof(status);
3281 status.an_type = AN_RID_STATUS;
3282 if (an_read_record(sc, (struct an_ltv_gen *)&status)) {
3283 /* If the status read fails, just lie. */
3284 imr->ifm_active = sc->an_ifmedia.ifm_cur->ifm_media;
3285 imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
3288 if (sc->an_tx_rate == 0) {
3289 imr->ifm_active = IFM_IEEE80211|IFM_AUTO;
3292 if (sc->an_config.an_opmode == AN_OPMODE_IBSS_ADHOC)
3293 imr->ifm_active |= IFM_IEEE80211_ADHOC;
3294 imr->ifm_active |= ieee80211_rate2media(NULL,
3295 status.an_current_tx_rate, IEEE80211_MODE_AUTO);
3296 imr->ifm_status = IFM_AVALID;
3297 if (status.an_opmode & AN_STATUS_OPMODE_ASSOCIATED)
3298 imr->ifm_status |= IFM_ACTIVE;
3302 /********************** Cisco utility support routines *************/
3305 * ReadRids & WriteRids derived from Cisco driver additions to Ben Reed's
3310 readrids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3313 struct an_softc *sc;
3316 switch (l_ioctl->command) {
3318 rid = AN_RID_CAPABILITIES;
3321 rid = AN_RID_GENCONFIG;
3324 rid = AN_RID_SSIDLIST;
3327 rid = AN_RID_APLIST;
3330 rid = AN_RID_DRVNAME;
3333 rid = AN_RID_ENCAPPROTO;
3336 rid = AN_RID_WEP_TEMP;
3339 rid = AN_RID_WEP_PERM;
3342 rid = AN_RID_STATUS;
3345 rid = AN_RID_32BITS_DELTA;
3348 rid = AN_RID_32BITS_CUM;
3355 if (rid == 999) /* Is bad command */
3359 sc->areq.an_len = AN_MAX_DATALEN;
3360 sc->areq.an_type = rid;
3362 an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3364 l_ioctl->len = sc->areq.an_len - 4; /* just data */
3367 /* the data contains the length at first */
3368 if (copyout(&(sc->areq.an_len), l_ioctl->data,
3369 sizeof(sc->areq.an_len))) {
3373 /* Just copy the data back */
3374 if (copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3386 writerids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3388 struct an_softc *sc;
3389 int rid, command, error;
3394 command = l_ioctl->command;
3398 rid = AN_RID_SSIDLIST;
3401 rid = AN_RID_CAPABILITIES;
3404 rid = AN_RID_APLIST;
3407 rid = AN_RID_GENCONFIG;
3410 an_cmd(sc, AN_CMD_ENABLE, 0);
3414 an_cmd(sc, AN_CMD_DISABLE, 0);
3419 * This command merely clears the counts does not actually
3420 * store any data only reads rid. But as it changes the cards
3421 * state, I put it in the writerid routines.
3424 rid = AN_RID_32BITS_DELTACLR;
3426 sc->areq.an_len = AN_MAX_DATALEN;
3427 sc->areq.an_type = rid;
3429 an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3430 l_ioctl->len = sc->areq.an_len - 4; /* just data */
3433 /* the data contains the length at first */
3434 error = copyout(&(sc->areq.an_len), l_ioctl->data,
3435 sizeof(sc->areq.an_len));
3440 /* Just copy the data */
3441 error = copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3449 rid = AN_RID_WEP_TEMP;
3452 rid = AN_RID_WEP_PERM;
3455 rid = AN_RID_LEAPUSERNAME;
3458 rid = AN_RID_LEAPPASSWORD;
3465 if (l_ioctl->len > sizeof(sc->areq.an_val) + 4)
3467 sc->areq.an_len = l_ioctl->len + 4; /* add type & length */
3468 sc->areq.an_type = rid;
3470 /* Just copy the data back */
3472 error = copyin((l_ioctl->data) + 2, &sc->areq.an_val,
3478 an_cmd(sc, AN_CMD_DISABLE, 0);
3479 an_write_record(sc, (struct an_ltv_gen *)&sc->areq);
3480 an_cmd(sc, AN_CMD_ENABLE, 0);
3487 * General Flash utilities derived from Cisco driver additions to Ben Reed's
3491 #define FLASH_DELAY(_sc, x) msleep(ifp, &(_sc)->an_mtx, PZERO, \
3492 "flash", ((x) / hz) + 1);
3493 #define FLASH_COMMAND 0x7e7e
3494 #define FLASH_SIZE 32 * 1024
3497 unstickbusy(struct ifnet *ifp)
3499 struct an_softc *sc = ifp->if_softc;
3501 if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
3502 CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
3503 AN_EV_CLR_STUCK_BUSY);
3510 * Wait for busy completion from card wait for delay uSec's Return true for
3511 * success meaning command reg is clear
3515 WaitBusy(struct ifnet *ifp, int uSec)
3517 int statword = 0xffff;
3519 struct an_softc *sc = ifp->if_softc;
3521 while ((statword & AN_CMD_BUSY) && delay <= (1000 * 100)) {
3522 FLASH_DELAY(sc, 10);
3524 statword = CSR_READ_2(sc, AN_COMMAND(sc->mpi350));
3526 if ((AN_CMD_BUSY & statword) && (delay % 200)) {
3531 return 0 == (AN_CMD_BUSY & statword);
3535 * STEP 1) Disable MAC and do soft reset on card.
3539 cmdreset(struct ifnet *ifp)
3542 struct an_softc *sc = ifp->if_softc;
3547 an_cmd(sc, AN_CMD_DISABLE, 0);
3549 if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3550 if_printf(ifp, "Waitbusy hang b4 RESET =%d\n", status);
3554 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), AN_CMD_FW_RESTART);
3556 FLASH_DELAY(sc, 1000); /* WAS 600 12/7/00 */
3559 if (!(status = WaitBusy(ifp, 100))) {
3560 if_printf(ifp, "Waitbusy hang AFTER RESET =%d\n", status);
3569 * STEP 2) Put the card in legendary flash mode
3573 setflashmode(struct ifnet *ifp)
3576 struct an_softc *sc = ifp->if_softc;
3578 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3579 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), FLASH_COMMAND);
3580 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3581 CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), FLASH_COMMAND);
3584 * mdelay(500); // 500ms delay
3587 FLASH_DELAY(sc, 500);
3589 if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3590 printf("Waitbusy hang after setflash mode\n");
3597 * Get a character from the card matching matchbyte Step 3)
3601 flashgchar(struct ifnet *ifp, int matchbyte, int dwelltime)
3604 unsigned char rbyte = 0;
3606 struct an_softc *sc = ifp->if_softc;
3610 rchar = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3612 if (dwelltime && !(0x8000 & rchar)) {
3614 FLASH_DELAY(sc, 10);
3617 rbyte = 0xff & rchar;
3619 if ((rbyte == matchbyte) && (0x8000 & rchar)) {
3620 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3624 if (rbyte == 0x81 || rbyte == 0x82 || rbyte == 0x83 || rbyte == 0x1a || 0xffff == rchar)
3626 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3628 } while (dwelltime > 0);
3633 * Put character to SWS0 wait for dwelltime x 50us for echo .
3637 flashpchar(struct ifnet *ifp, int byte, int dwelltime)
3640 int pollbusy, waittime;
3641 struct an_softc *sc = ifp->if_softc;
3648 waittime = dwelltime;
3651 * Wait for busy bit d15 to go false indicating buffer empty
3654 pollbusy = CSR_READ_2(sc, AN_SW0(sc->mpi350));
3656 if (pollbusy & 0x8000) {
3657 FLASH_DELAY(sc, 50);
3663 while (waittime >= 0);
3665 /* timeout for busy clear wait */
3667 if (waittime <= 0) {
3668 if_printf(ifp, "flash putchar busywait timeout!\n");
3672 * Port is clear now write byte and wait for it to echo back
3675 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), byte);
3676 FLASH_DELAY(sc, 50);
3678 echo = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3679 } while (dwelltime >= 0 && echo != byte);
3682 CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3684 return echo == byte;
3688 * Transfer 32k of firmware data from user buffer to our buffer and send to
3693 flashputbuf(struct ifnet *ifp)
3695 unsigned short *bufp;
3697 struct an_softc *sc = ifp->if_softc;
3701 bufp = sc->an_flash_buffer;
3704 CSR_WRITE_2(sc, AN_AUX_PAGE, 0x100);
3705 CSR_WRITE_2(sc, AN_AUX_OFFSET, 0);
3707 for (nwords = 0; nwords != FLASH_SIZE / 2; nwords++) {
3708 CSR_WRITE_2(sc, AN_AUX_DATA, bufp[nwords] & 0xffff);
3711 for (nwords = 0; nwords != FLASH_SIZE / 4; nwords++) {
3712 CSR_MEM_AUX_WRITE_4(sc, 0x8000,
3713 ((u_int32_t *)bufp)[nwords] & 0xffff);
3717 CSR_WRITE_2(sc, AN_SW0(sc->mpi350), 0x8000);
3723 * After flashing restart the card.
3727 flashrestart(struct ifnet *ifp)
3730 struct an_softc *sc = ifp->if_softc;
3732 FLASH_DELAY(sc, 1024); /* Added 12/7/00 */
3736 FLASH_DELAY(sc, 1024); /* Added 12/7/00 */
3741 * Entry point for flash ioclt.
3745 flashcard(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3748 struct an_softc *sc;
3752 if_printf(ifp, "flashing not supported on MPI 350 yet\n");
3755 status = l_ioctl->command;
3757 switch (l_ioctl->command) {
3759 return cmdreset(ifp);
3762 if (sc->an_flash_buffer) {
3763 free(sc->an_flash_buffer, M_DEVBUF);
3764 sc->an_flash_buffer = NULL;
3766 sc->an_flash_buffer = malloc(FLASH_SIZE, M_DEVBUF, M_WAITOK);
3767 if (sc->an_flash_buffer)
3768 return setflashmode(ifp);
3772 case AIROFLSHGCHR: /* Get char from aux */
3774 status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3778 z = *(int *)&sc->areq;
3779 if ((status = flashgchar(ifp, z, 8000)) == 1)
3783 case AIROFLSHPCHR: /* Send char to card. */
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);