if_an.c revision 315221
1/*-
2 * Copyright (c) 1997, 1998, 1999
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32/*
33 * Aironet 4500/4800 802.11 PCMCIA/ISA/PCI driver for FreeBSD.
34 *
35 * Written by Bill Paul <wpaul@ctr.columbia.edu>
36 * Electrical Engineering Department
37 * Columbia University, New York City
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/sys/dev/an/if_an.c 315221 2017-03-14 02:06:03Z pfg $");
42
43/*
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.
50 *
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:
55 *
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
62 *
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.
72 *
73 * Note that some PCMCIA controller software packages for Windows NT
74 * fail to set the voltages as well.
75 *
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.
82 */
83
84#include "opt_inet.h"
85
86#ifdef INET
87#define ANCACHE			/* enable signal strength cache */
88#endif
89
90#include <sys/param.h>
91#include <sys/ctype.h>
92#include <sys/systm.h>
93#include <sys/sockio.h>
94#include <sys/mbuf.h>
95#include <sys/priv.h>
96#include <sys/proc.h>
97#include <sys/kernel.h>
98#include <sys/socket.h>
99#ifdef ANCACHE
100#include <sys/syslog.h>
101#endif
102#include <sys/sysctl.h>
103
104#include <sys/module.h>
105#include <sys/bus.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>
112
113#include <net/if.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>
120
121#include <net80211/ieee80211_var.h>
122#include <net80211/ieee80211_ioctl.h>
123
124#ifdef INET
125#include <netinet/in.h>
126#include <netinet/in_systm.h>
127#include <netinet/in_var.h>
128#include <netinet/ip.h>
129#endif
130
131#include <net/bpf.h>
132
133#include <machine/md_var.h>
134
135#include <dev/an/if_aironet_ieee.h>
136#include <dev/an/if_anreg.h>
137
138/* These are global because we need them in sys/pci/if_an_p.c. */
139static void an_reset(struct an_softc *);
140static int an_init_mpi350_desc(struct an_softc *);
141static int an_ioctl(struct ifnet *, u_long, caddr_t);
142static void an_init(void *);
143static void an_init_locked(struct an_softc *);
144static int an_init_tx_ring(struct an_softc *);
145static void an_start(struct ifnet *);
146static void an_start_locked(struct ifnet *);
147static void an_watchdog(struct an_softc *);
148static void an_rxeof(struct an_softc *);
149static void an_txeof(struct an_softc *, int);
150
151static void an_promisc(struct an_softc *, int);
152static int an_cmd(struct an_softc *, int, int);
153static int an_cmd_struct(struct an_softc *, struct an_command *,
154    struct an_reply *);
155static int an_read_record(struct an_softc *, struct an_ltv_gen *);
156static int an_write_record(struct an_softc *, struct an_ltv_gen *);
157static int an_read_data(struct an_softc *, int, int, caddr_t, int);
158static int an_write_data(struct an_softc *, int, int, caddr_t, int);
159static int an_seek(struct an_softc *, int, int, int);
160static int an_alloc_nicmem(struct an_softc *, int, int *);
161static int an_dma_malloc(struct an_softc *, bus_size_t, struct an_dma_alloc *,
162    int);
163static void an_dma_free(struct an_softc *, struct an_dma_alloc *);
164static void an_dma_malloc_cb(void *, bus_dma_segment_t *, int, int);
165static void an_stats_update(void *);
166static void an_setdef(struct an_softc *, struct an_req *);
167#ifdef ANCACHE
168static void an_cache_store(struct an_softc *, struct ether_header *,
169    struct mbuf *, u_int8_t, u_int8_t);
170#endif
171
172/* function definitions for use with the Cisco's Linux configuration
173   utilities
174*/
175
176static int readrids(struct ifnet*, struct aironet_ioctl*);
177static int writerids(struct ifnet*, struct aironet_ioctl*);
178static int flashcard(struct ifnet*, struct aironet_ioctl*);
179
180static int cmdreset(struct ifnet *);
181static int setflashmode(struct ifnet *);
182static int flashgchar(struct ifnet *,int,int);
183static int flashpchar(struct ifnet *,int,int);
184static int flashputbuf(struct ifnet *);
185static int flashrestart(struct ifnet *);
186static int WaitBusy(struct ifnet *, int);
187static int unstickbusy(struct ifnet *);
188
189static void an_dump_record	(struct an_softc *,struct an_ltv_gen *,
190				    char *);
191
192static int an_media_change	(struct ifnet *);
193static void an_media_status	(struct ifnet *, struct ifmediareq *);
194
195static int	an_dump = 0;
196static int	an_cache_mode = 0;
197
198#define DBM 0
199#define PERCENT 1
200#define RAW 2
201
202static char an_conf[256];
203static char an_conf_cache[256];
204
205/* sysctl vars */
206
207static SYSCTL_NODE(_hw, OID_AUTO, an, CTLFLAG_RD, 0,
208    "Wireless driver parameters");
209
210/* XXX violate ethernet/netgraph callback hooks */
211extern	void	(*ng_ether_attach_p)(struct ifnet *ifp);
212extern	void	(*ng_ether_detach_p)(struct ifnet *ifp);
213
214static int
215sysctl_an_dump(SYSCTL_HANDLER_ARGS)
216{
217	int	error, r, last;
218	char 	*s = an_conf;
219
220	last = an_dump;
221
222	switch (an_dump) {
223	case 0:
224		strcpy(an_conf, "off");
225		break;
226	case 1:
227		strcpy(an_conf, "type");
228		break;
229	case 2:
230		strcpy(an_conf, "dump");
231		break;
232	default:
233		snprintf(an_conf, 5, "%x", an_dump);
234		break;
235	}
236
237	error = sysctl_handle_string(oidp, an_conf, sizeof(an_conf), req);
238
239	if (strncmp(an_conf,"off", 3) == 0) {
240		an_dump = 0;
241 	}
242	if (strncmp(an_conf,"dump", 4) == 0) {
243		an_dump = 1;
244	}
245	if (strncmp(an_conf,"type", 4) == 0) {
246		an_dump = 2;
247	}
248	if (*s == 'f') {
249		r = 0;
250		for (;;s++) {
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);
255			} else {
256				break;
257			}
258		}
259		an_dump = r;
260	}
261	if (an_dump != last)
262		printf("Sysctl changed for Aironet driver\n");
263
264	return error;
265}
266
267SYSCTL_PROC(_hw_an, OID_AUTO, an_dump, CTLTYPE_STRING | CTLFLAG_RW,
268	    0, sizeof(an_conf), sysctl_an_dump, "A", "");
269
270static int
271sysctl_an_cache_mode(SYSCTL_HANDLER_ARGS)
272{
273	int	error;
274
275	switch (an_cache_mode) {
276	case 1:
277		strcpy(an_conf_cache, "per");
278		break;
279	case 2:
280		strcpy(an_conf_cache, "raw");
281		break;
282	default:
283		strcpy(an_conf_cache, "dbm");
284		break;
285	}
286
287	error = sysctl_handle_string(oidp, an_conf_cache,
288			sizeof(an_conf_cache), req);
289
290	if (strncmp(an_conf_cache,"dbm", 3) == 0) {
291		an_cache_mode = 0;
292	}
293	if (strncmp(an_conf_cache,"per", 3) == 0) {
294		an_cache_mode = 1;
295 	}
296	if (strncmp(an_conf_cache,"raw", 3) == 0) {
297		an_cache_mode = 2;
298	}
299
300	return error;
301}
302
303SYSCTL_PROC(_hw_an, OID_AUTO, an_cache_mode, CTLTYPE_STRING | CTLFLAG_RW,
304	    0, sizeof(an_conf_cache), sysctl_an_cache_mode, "A", "");
305
306/*
307 * We probe for an Aironet 4500/4800 card by attempting to
308 * read the default SSID list. On reset, the first entry in
309 * the SSID list will contain the name "tsunami." If we don't
310 * find this, then there's no card present.
311 */
312int
313an_probe(device_t dev)
314{
315	struct an_softc *sc = device_get_softc(dev);
316	struct an_ltv_ssidlist_new	ssid;
317	int	error;
318
319	bzero((char *)&ssid, sizeof(ssid));
320
321	error = an_alloc_port(dev, 0, AN_IOSIZ);
322	if (error != 0)
323		return (0);
324
325	/* can't do autoprobing */
326	if (rman_get_start(sc->port_res) == -1)
327		return(0);
328
329	/*
330	 * We need to fake up a softc structure long enough
331	 * to be able to issue commands and call some of the
332	 * other routines.
333	 */
334	ssid.an_len = sizeof(ssid);
335	ssid.an_type = AN_RID_SSIDLIST;
336
337	/* Make sure interrupts are disabled. */
338	sc->mpi350 = 0;
339	CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
340	CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), 0xFFFF);
341
342	sc->an_dev = dev;
343	mtx_init(&sc->an_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
344	    MTX_DEF);
345	AN_LOCK(sc);
346	an_reset(sc);
347
348	if (an_cmd(sc, AN_CMD_READCFG, 0)) {
349		AN_UNLOCK(sc);
350		goto fail;
351	}
352
353	if (an_read_record(sc, (struct an_ltv_gen *)&ssid)) {
354		AN_UNLOCK(sc);
355		goto fail;
356	}
357
358	/* See if the ssid matches what we expect ... but doesn't have to */
359	if (strcmp(ssid.an_entry[0].an_ssid, AN_DEF_SSID)) {
360		AN_UNLOCK(sc);
361		goto fail;
362	}
363
364	AN_UNLOCK(sc);
365	return(AN_IOSIZ);
366fail:
367	mtx_destroy(&sc->an_mtx);
368	return(0);
369}
370
371/*
372 * Allocate a port resource with the given resource id.
373 */
374int
375an_alloc_port(device_t dev, int rid, int size)
376{
377	struct an_softc *sc = device_get_softc(dev);
378	struct resource *res;
379
380	res = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
381					  size, RF_ACTIVE);
382	if (res) {
383		sc->port_rid = rid;
384		sc->port_res = res;
385		return (0);
386	} else {
387		return (ENOENT);
388	}
389}
390
391/*
392 * Allocate a memory resource with the given resource id.
393 */
394int an_alloc_memory(device_t dev, int rid, int size)
395{
396	struct an_softc *sc = device_get_softc(dev);
397	struct resource *res;
398
399	res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY, &rid,
400					  size, RF_ACTIVE);
401	if (res) {
402		sc->mem_rid = rid;
403		sc->mem_res = res;
404		sc->mem_used = size;
405		return (0);
406	} else {
407		return (ENOENT);
408	}
409}
410
411/*
412 * Allocate a auxiliary memory resource with the given resource id.
413 */
414int an_alloc_aux_memory(device_t dev, int rid, int size)
415{
416	struct an_softc *sc = device_get_softc(dev);
417	struct resource *res;
418
419	res = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY, &rid,
420					  size, RF_ACTIVE);
421	if (res) {
422		sc->mem_aux_rid = rid;
423		sc->mem_aux_res = res;
424		sc->mem_aux_used = size;
425		return (0);
426	} else {
427		return (ENOENT);
428	}
429}
430
431/*
432 * Allocate an irq resource with the given resource id.
433 */
434int
435an_alloc_irq(device_t dev, int rid, int flags)
436{
437	struct an_softc *sc = device_get_softc(dev);
438	struct resource *res;
439
440	res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
441				     (RF_ACTIVE | flags));
442	if (res) {
443		sc->irq_rid = rid;
444		sc->irq_res = res;
445		return (0);
446	} else {
447		return (ENOENT);
448	}
449}
450
451static void
452an_dma_malloc_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
453{
454	bus_addr_t *paddr = (bus_addr_t*) arg;
455	*paddr = segs->ds_addr;
456}
457
458/*
459 * Alloc DMA memory and set the pointer to it
460 */
461static int
462an_dma_malloc(struct an_softc *sc, bus_size_t size, struct an_dma_alloc *dma,
463    int mapflags)
464{
465	int r;
466
467	r = bus_dmamem_alloc(sc->an_dtag, (void**) &dma->an_dma_vaddr,
468			     BUS_DMA_NOWAIT, &dma->an_dma_map);
469	if (r != 0)
470		goto fail_1;
471
472	r = bus_dmamap_load(sc->an_dtag, dma->an_dma_map, dma->an_dma_vaddr,
473			    size,
474			    an_dma_malloc_cb,
475			    &dma->an_dma_paddr,
476			    mapflags | BUS_DMA_NOWAIT);
477	if (r != 0)
478		goto fail_2;
479
480	dma->an_dma_size = size;
481	return (0);
482
483fail_2:
484	bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
485fail_1:
486	bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
487	return (r);
488}
489
490static void
491an_dma_free(struct an_softc *sc, struct an_dma_alloc *dma)
492{
493	bus_dmamap_unload(sc->an_dtag, dma->an_dma_map);
494	bus_dmamem_free(sc->an_dtag, dma->an_dma_vaddr, dma->an_dma_map);
495	dma->an_dma_vaddr = 0;
496}
497
498/*
499 * Release all resources
500 */
501void
502an_release_resources(device_t dev)
503{
504	struct an_softc *sc = device_get_softc(dev);
505	int i;
506
507	if (sc->port_res) {
508		bus_release_resource(dev, SYS_RES_IOPORT,
509				     sc->port_rid, sc->port_res);
510		sc->port_res = 0;
511	}
512	if (sc->mem_res) {
513		bus_release_resource(dev, SYS_RES_MEMORY,
514				     sc->mem_rid, sc->mem_res);
515		sc->mem_res = 0;
516	}
517	if (sc->mem_aux_res) {
518		bus_release_resource(dev, SYS_RES_MEMORY,
519				     sc->mem_aux_rid, sc->mem_aux_res);
520		sc->mem_aux_res = 0;
521	}
522	if (sc->irq_res) {
523		bus_release_resource(dev, SYS_RES_IRQ,
524				     sc->irq_rid, sc->irq_res);
525		sc->irq_res = 0;
526	}
527	if (sc->an_rid_buffer.an_dma_paddr) {
528		an_dma_free(sc, &sc->an_rid_buffer);
529	}
530	for (i = 0; i < AN_MAX_RX_DESC; i++)
531		if (sc->an_rx_buffer[i].an_dma_paddr) {
532			an_dma_free(sc, &sc->an_rx_buffer[i]);
533		}
534	for (i = 0; i < AN_MAX_TX_DESC; i++)
535		if (sc->an_tx_buffer[i].an_dma_paddr) {
536			an_dma_free(sc, &sc->an_tx_buffer[i]);
537		}
538	if (sc->an_dtag) {
539		bus_dma_tag_destroy(sc->an_dtag);
540	}
541
542}
543
544int
545an_init_mpi350_desc(struct an_softc *sc)
546{
547	struct an_command	cmd_struct;
548	struct an_reply		reply;
549	struct an_card_rid_desc an_rid_desc;
550	struct an_card_rx_desc	an_rx_desc;
551	struct an_card_tx_desc	an_tx_desc;
552	int			i, desc;
553
554	AN_LOCK_ASSERT(sc);
555	if(!sc->an_rid_buffer.an_dma_paddr)
556		an_dma_malloc(sc, AN_RID_BUFFER_SIZE,
557				 &sc->an_rid_buffer, 0);
558	for (i = 0; i < AN_MAX_RX_DESC; i++)
559		if(!sc->an_rx_buffer[i].an_dma_paddr)
560			an_dma_malloc(sc, AN_RX_BUFFER_SIZE,
561				      &sc->an_rx_buffer[i], 0);
562	for (i = 0; i < AN_MAX_TX_DESC; i++)
563		if(!sc->an_tx_buffer[i].an_dma_paddr)
564			an_dma_malloc(sc, AN_TX_BUFFER_SIZE,
565				      &sc->an_tx_buffer[i], 0);
566
567	/*
568	 * Allocate RX descriptor
569	 */
570	bzero(&reply,sizeof(reply));
571	cmd_struct.an_cmd   = AN_CMD_ALLOC_DESC;
572	cmd_struct.an_parm0 = AN_DESCRIPTOR_RX;
573	cmd_struct.an_parm1 = AN_RX_DESC_OFFSET;
574	cmd_struct.an_parm2 = AN_MAX_RX_DESC;
575	if (an_cmd_struct(sc, &cmd_struct, &reply)) {
576		if_printf(sc->an_ifp, "failed to allocate RX descriptor\n");
577		return(EIO);
578	}
579
580	for (desc = 0; desc < AN_MAX_RX_DESC; desc++) {
581		bzero(&an_rx_desc, sizeof(an_rx_desc));
582		an_rx_desc.an_valid = 1;
583		an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
584		an_rx_desc.an_done = 0;
585		an_rx_desc.an_phys = sc->an_rx_buffer[desc].an_dma_paddr;
586
587		for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
588			CSR_MEM_AUX_WRITE_4(sc, AN_RX_DESC_OFFSET
589			    + (desc * sizeof(an_rx_desc))
590			    + (i * 4),
591			    ((u_int32_t *)(void *)&an_rx_desc)[i]);
592	}
593
594	/*
595	 * Allocate TX descriptor
596	 */
597
598	bzero(&reply,sizeof(reply));
599	cmd_struct.an_cmd   = AN_CMD_ALLOC_DESC;
600	cmd_struct.an_parm0 = AN_DESCRIPTOR_TX;
601	cmd_struct.an_parm1 = AN_TX_DESC_OFFSET;
602	cmd_struct.an_parm2 = AN_MAX_TX_DESC;
603	if (an_cmd_struct(sc, &cmd_struct, &reply)) {
604		if_printf(sc->an_ifp, "failed to allocate TX descriptor\n");
605		return(EIO);
606	}
607
608	for (desc = 0; desc < AN_MAX_TX_DESC; desc++) {
609		bzero(&an_tx_desc, sizeof(an_tx_desc));
610		an_tx_desc.an_offset = 0;
611		an_tx_desc.an_eoc = 0;
612		an_tx_desc.an_valid = 0;
613		an_tx_desc.an_len = 0;
614		an_tx_desc.an_phys = sc->an_tx_buffer[desc].an_dma_paddr;
615
616		for (i = 0; i < sizeof(an_tx_desc) / 4; i++)
617			CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
618			    + (desc * sizeof(an_tx_desc))
619			    + (i * 4),
620			    ((u_int32_t *)(void *)&an_tx_desc)[i]);
621	}
622
623	/*
624	 * Allocate RID descriptor
625	 */
626
627	bzero(&reply,sizeof(reply));
628	cmd_struct.an_cmd   = AN_CMD_ALLOC_DESC;
629	cmd_struct.an_parm0 = AN_DESCRIPTOR_HOSTRW;
630	cmd_struct.an_parm1 = AN_HOST_DESC_OFFSET;
631	cmd_struct.an_parm2 = 1;
632	if (an_cmd_struct(sc, &cmd_struct, &reply)) {
633		if_printf(sc->an_ifp, "failed to allocate host descriptor\n");
634		return(EIO);
635	}
636
637	bzero(&an_rid_desc, sizeof(an_rid_desc));
638	an_rid_desc.an_valid = 1;
639	an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
640	an_rid_desc.an_rid = 0;
641	an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
642
643	for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
644		CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
645				    ((u_int32_t *)(void *)&an_rid_desc)[i]);
646
647	return(0);
648}
649
650int
651an_attach(struct an_softc *sc, int flags)
652{
653	struct ifnet		*ifp;
654	int			error = EIO;
655	int			i, nrate, mword;
656	u_int8_t		r;
657
658	ifp = sc->an_ifp = if_alloc(IFT_ETHER);
659	if (ifp == NULL) {
660		device_printf(sc->an_dev, "can not if_alloc()\n");
661		goto fail;
662	}
663	ifp->if_softc = sc;
664	if_initname(ifp, device_get_name(sc->an_dev),
665	    device_get_unit(sc->an_dev));
666
667	sc->an_gone = 0;
668	sc->an_associated = 0;
669	sc->an_monitor = 0;
670	sc->an_was_monitor = 0;
671	sc->an_flash_buffer = NULL;
672
673	/* Reset the NIC. */
674	AN_LOCK(sc);
675	an_reset(sc);
676	if (sc->mpi350) {
677		error = an_init_mpi350_desc(sc);
678		if (error)
679			goto fail;
680	}
681
682	/* Load factory config */
683	if (an_cmd(sc, AN_CMD_READCFG, 0)) {
684		device_printf(sc->an_dev, "failed to load config data\n");
685		goto fail;
686	}
687
688	/* Read the current configuration */
689	sc->an_config.an_type = AN_RID_GENCONFIG;
690	sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
691	if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
692		device_printf(sc->an_dev, "read record failed\n");
693		goto fail;
694	}
695
696	/* Read the card capabilities */
697	sc->an_caps.an_type = AN_RID_CAPABILITIES;
698	sc->an_caps.an_len = sizeof(struct an_ltv_caps);
699	if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_caps)) {
700		device_printf(sc->an_dev, "read record failed\n");
701		goto fail;
702	}
703
704	/* Read ssid list */
705	sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
706	sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
707	if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
708		device_printf(sc->an_dev, "read record failed\n");
709		goto fail;
710	}
711
712	/* Read AP list */
713	sc->an_aplist.an_type = AN_RID_APLIST;
714	sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
715	if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
716		device_printf(sc->an_dev, "read record failed\n");
717		goto fail;
718	}
719
720#ifdef ANCACHE
721	/* Read the RSSI <-> dBm map */
722	sc->an_have_rssimap = 0;
723	if (sc->an_caps.an_softcaps & 8) {
724		sc->an_rssimap.an_type = AN_RID_RSSI_MAP;
725		sc->an_rssimap.an_len = sizeof(struct an_ltv_rssi_map);
726		if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_rssimap)) {
727			device_printf(sc->an_dev,
728			    "unable to get RSSI <-> dBM map\n");
729		} else {
730			device_printf(sc->an_dev, "got RSSI <-> dBM map\n");
731			sc->an_have_rssimap = 1;
732		}
733	} else {
734		device_printf(sc->an_dev, "no RSSI <-> dBM map\n");
735	}
736#endif
737	AN_UNLOCK(sc);
738
739	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
740	ifp->if_ioctl = an_ioctl;
741	ifp->if_start = an_start;
742	ifp->if_init = an_init;
743	ifp->if_baudrate = 10000000;
744	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
745	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
746	IFQ_SET_READY(&ifp->if_snd);
747
748	bzero(sc->an_config.an_nodename, sizeof(sc->an_config.an_nodename));
749	bcopy(AN_DEFAULT_NODENAME, sc->an_config.an_nodename,
750	    sizeof(AN_DEFAULT_NODENAME) - 1);
751
752	bzero(sc->an_ssidlist.an_entry[0].an_ssid,
753	      sizeof(sc->an_ssidlist.an_entry[0].an_ssid));
754	bcopy(AN_DEFAULT_NETNAME, sc->an_ssidlist.an_entry[0].an_ssid,
755	    sizeof(AN_DEFAULT_NETNAME) - 1);
756	sc->an_ssidlist.an_entry[0].an_len = strlen(AN_DEFAULT_NETNAME);
757
758	sc->an_config.an_opmode =
759	    AN_OPMODE_INFRASTRUCTURE_STATION;
760
761	sc->an_tx_rate = 0;
762	bzero((char *)&sc->an_stats, sizeof(sc->an_stats));
763
764	nrate = 8;
765
766	ifmedia_init(&sc->an_ifmedia, 0, an_media_change, an_media_status);
767	if_printf(ifp, "supported rates: ");
768#define	ADD(s, o)	ifmedia_add(&sc->an_ifmedia, \
769	IFM_MAKEWORD(IFM_IEEE80211, (s), (o), 0), 0, NULL)
770	ADD(IFM_AUTO, 0);
771	ADD(IFM_AUTO, IFM_IEEE80211_ADHOC);
772	for (i = 0; i < nrate; i++) {
773		r = sc->an_caps.an_rates[i];
774		mword = ieee80211_rate2media(NULL, r, IEEE80211_MODE_AUTO);
775		if (mword == 0)
776			continue;
777		printf("%s%d%sMbps", (i != 0 ? " " : ""),
778		    (r & IEEE80211_RATE_VAL) / 2, ((r & 0x1) != 0 ? ".5" : ""));
779		ADD(mword, 0);
780		ADD(mword, IFM_IEEE80211_ADHOC);
781	}
782	printf("\n");
783	ifmedia_set(&sc->an_ifmedia, IFM_MAKEWORD(IFM_IEEE80211,
784	    IFM_AUTO, 0, 0));
785#undef ADD
786
787	/*
788	 * Call MI attach routine.
789	 */
790
791	ether_ifattach(ifp, sc->an_caps.an_oemaddr);
792	callout_init_mtx(&sc->an_stat_ch, &sc->an_mtx, 0);
793
794	return(0);
795fail:
796	AN_UNLOCK(sc);
797	mtx_destroy(&sc->an_mtx);
798	if (ifp != NULL)
799		if_free(ifp);
800	return(error);
801}
802
803int
804an_detach(device_t dev)
805{
806	struct an_softc		*sc = device_get_softc(dev);
807	struct ifnet		*ifp = sc->an_ifp;
808
809	if (sc->an_gone) {
810		device_printf(dev,"already unloaded\n");
811		return(0);
812	}
813	AN_LOCK(sc);
814	an_stop(sc);
815	sc->an_gone = 1;
816	ifmedia_removeall(&sc->an_ifmedia);
817	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
818	AN_UNLOCK(sc);
819	ether_ifdetach(ifp);
820	bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
821	callout_drain(&sc->an_stat_ch);
822	if_free(ifp);
823	an_release_resources(dev);
824	mtx_destroy(&sc->an_mtx);
825	return (0);
826}
827
828static void
829an_rxeof(struct an_softc *sc)
830{
831	struct ifnet   *ifp;
832	struct ether_header *eh;
833	struct ieee80211_frame *ih;
834	struct an_rxframe rx_frame;
835	struct an_rxframe_802_3 rx_frame_802_3;
836	struct mbuf    *m;
837	int		len, id, error = 0, i, count = 0;
838	int		ieee80211_header_len;
839	u_char		*bpf_buf;
840	u_short		fc1;
841	struct an_card_rx_desc an_rx_desc;
842	u_int8_t	*buf;
843
844	AN_LOCK_ASSERT(sc);
845
846	ifp = sc->an_ifp;
847
848	if (!sc->mpi350) {
849		id = CSR_READ_2(sc, AN_RX_FID);
850
851		if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
852			/* read raw 802.11 packet */
853			bpf_buf = sc->buf_802_11;
854
855			/* read header */
856			if (an_read_data(sc, id, 0x0, (caddr_t)&rx_frame,
857					 sizeof(rx_frame))) {
858				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
859				return;
860			}
861
862			/*
863			 * skip beacon by default since this increases the
864			 * system load a lot
865			 */
866
867			if (!(sc->an_monitor & AN_MONITOR_INCLUDE_BEACON) &&
868			    (rx_frame.an_frame_ctl &
869			     IEEE80211_FC0_SUBTYPE_BEACON)) {
870				return;
871			}
872
873			if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
874				len = rx_frame.an_rx_payload_len
875					+ sizeof(rx_frame);
876				/* Check for insane frame length */
877				if (len > sizeof(sc->buf_802_11)) {
878					if_printf(ifp, "oversized packet "
879					       "received (%d, %d)\n",
880					       len, MCLBYTES);
881					if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
882					return;
883				}
884
885				bcopy((char *)&rx_frame,
886				      bpf_buf, sizeof(rx_frame));
887
888				error = an_read_data(sc, id, sizeof(rx_frame),
889					    (caddr_t)bpf_buf+sizeof(rx_frame),
890					    rx_frame.an_rx_payload_len);
891			} else {
892				fc1=rx_frame.an_frame_ctl >> 8;
893				ieee80211_header_len =
894					sizeof(struct ieee80211_frame);
895				if ((fc1 & IEEE80211_FC1_DIR_TODS) &&
896				    (fc1 & IEEE80211_FC1_DIR_FROMDS)) {
897					ieee80211_header_len += ETHER_ADDR_LEN;
898				}
899
900				len = rx_frame.an_rx_payload_len
901					+ ieee80211_header_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",
906					       len, MCLBYTES);
907					if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
908					return;
909				}
910
911				ih = (struct ieee80211_frame *)bpf_buf;
912
913				bcopy((char *)&rx_frame.an_frame_ctl,
914				      (char *)ih, ieee80211_header_len);
915
916				error = an_read_data(sc, id, sizeof(rx_frame) +
917					    rx_frame.an_gaplen,
918					    (caddr_t)ih +ieee80211_header_len,
919					    rx_frame.an_rx_payload_len);
920			}
921			/* dump raw 802.11 packet to bpf and skip ip stack */
922			BPF_TAP(ifp, bpf_buf, len);
923		} else {
924			MGETHDR(m, M_NOWAIT, MT_DATA);
925			if (m == NULL) {
926				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
927				return;
928			}
929			if (!(MCLGET(m, M_NOWAIT))) {
930				m_freem(m);
931				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
932				return;
933			}
934			m->m_pkthdr.rcvif = ifp;
935			/* Read Ethernet encapsulated packet */
936
937#ifdef ANCACHE
938			/* Read NIC frame header */
939			if (an_read_data(sc, id, 0, (caddr_t)&rx_frame,
940					 sizeof(rx_frame))) {
941				m_freem(m);
942				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
943				return;
944			}
945#endif
946			/* Read in the 802_3 frame header */
947			if (an_read_data(sc, id, 0x34,
948					 (caddr_t)&rx_frame_802_3,
949					 sizeof(rx_frame_802_3))) {
950				m_freem(m);
951				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
952				return;
953			}
954			if (rx_frame_802_3.an_rx_802_3_status != 0) {
955				m_freem(m);
956				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
957				return;
958			}
959			/* Check for insane frame length */
960			len = rx_frame_802_3.an_rx_802_3_payload_len;
961			if (len > sizeof(sc->buf_802_11)) {
962				m_freem(m);
963				if_printf(ifp, "oversized packet "
964				       "received (%d, %d)\n",
965				       len, MCLBYTES);
966				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
967				return;
968			}
969			m->m_pkthdr.len = m->m_len =
970				rx_frame_802_3.an_rx_802_3_payload_len + 12;
971
972			eh = mtod(m, struct ether_header *);
973
974			bcopy((char *)&rx_frame_802_3.an_rx_dst_addr,
975			      (char *)&eh->ether_dhost, ETHER_ADDR_LEN);
976			bcopy((char *)&rx_frame_802_3.an_rx_src_addr,
977			      (char *)&eh->ether_shost, ETHER_ADDR_LEN);
978
979			/* in mbuf header type is just before payload */
980			error = an_read_data(sc, id, 0x44,
981				    (caddr_t)&(eh->ether_type),
982				    rx_frame_802_3.an_rx_802_3_payload_len);
983
984			if (error) {
985				m_freem(m);
986				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
987				return;
988			}
989			if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
990
991			/* Receive packet. */
992#ifdef ANCACHE
993			an_cache_store(sc, eh, m,
994				rx_frame.an_rx_signal_strength,
995				rx_frame.an_rsvd0);
996#endif
997			AN_UNLOCK(sc);
998			(*ifp->if_input)(ifp, m);
999			AN_LOCK(sc);
1000		}
1001
1002	} else { /* MPI-350 */
1003		for (count = 0; count < AN_MAX_RX_DESC; count++){
1004			for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1005				((u_int32_t *)(void *)&an_rx_desc)[i]
1006					= CSR_MEM_AUX_READ_4(sc,
1007						AN_RX_DESC_OFFSET
1008						+ (count * sizeof(an_rx_desc))
1009						+ (i * 4));
1010
1011			if (an_rx_desc.an_done && !an_rx_desc.an_valid) {
1012				buf = sc->an_rx_buffer[count].an_dma_vaddr;
1013
1014				MGETHDR(m, M_NOWAIT, MT_DATA);
1015				if (m == NULL) {
1016					if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1017					return;
1018				}
1019				if (!(MCLGET(m, M_NOWAIT))) {
1020					m_freem(m);
1021					if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1022					return;
1023				}
1024				m->m_pkthdr.rcvif = ifp;
1025				/* Read Ethernet encapsulated packet */
1026
1027				/*
1028				 * No ANCACHE support since we just get back
1029				 * an Ethernet packet no 802.11 info
1030				 */
1031#if 0
1032#ifdef ANCACHE
1033				/* Read NIC frame header */
1034				bcopy(buf, (caddr_t)&rx_frame,
1035				      sizeof(rx_frame));
1036#endif
1037#endif
1038				/* Check for insane frame length */
1039				len = an_rx_desc.an_len + 12;
1040				if (len > MCLBYTES) {
1041					m_freem(m);
1042					if_printf(ifp, "oversized packet "
1043					       "received (%d, %d)\n",
1044					       len, MCLBYTES);
1045					if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1046					return;
1047				}
1048
1049				m->m_pkthdr.len = m->m_len =
1050					an_rx_desc.an_len + 12;
1051
1052				eh = mtod(m, struct ether_header *);
1053
1054				bcopy(buf, (char *)eh,
1055				      m->m_pkthdr.len);
1056
1057				if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1058
1059				/* Receive packet. */
1060#if 0
1061#ifdef ANCACHE
1062				an_cache_store(sc, eh, m,
1063					rx_frame.an_rx_signal_strength,
1064					rx_frame.an_rsvd0);
1065#endif
1066#endif
1067				AN_UNLOCK(sc);
1068				(*ifp->if_input)(ifp, m);
1069				AN_LOCK(sc);
1070
1071				an_rx_desc.an_valid = 1;
1072				an_rx_desc.an_len = AN_RX_BUFFER_SIZE;
1073				an_rx_desc.an_done = 0;
1074				an_rx_desc.an_phys =
1075					sc->an_rx_buffer[count].an_dma_paddr;
1076
1077				for (i = 0; i < sizeof(an_rx_desc) / 4; i++)
1078					CSR_MEM_AUX_WRITE_4(sc,
1079					    AN_RX_DESC_OFFSET
1080					    + (count * sizeof(an_rx_desc))
1081					    + (i * 4),
1082					    ((u_int32_t *)(void *)&an_rx_desc)[i]);
1083
1084			} else {
1085				if_printf(ifp, "Didn't get valid RX packet "
1086				       "%x %x %d\n",
1087				       an_rx_desc.an_done,
1088				       an_rx_desc.an_valid, an_rx_desc.an_len);
1089			}
1090		}
1091	}
1092}
1093
1094static void
1095an_txeof(struct an_softc *sc, int status)
1096{
1097	struct ifnet		*ifp;
1098	int			id, i;
1099
1100	AN_LOCK_ASSERT(sc);
1101	ifp = sc->an_ifp;
1102
1103	sc->an_timer = 0;
1104	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1105
1106	if (!sc->mpi350) {
1107		id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1108
1109		if (status & AN_EV_TX_EXC) {
1110			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1111		} else
1112			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1113
1114		for (i = 0; i < AN_TX_RING_CNT; i++) {
1115			if (id == sc->an_rdata.an_tx_ring[i]) {
1116				sc->an_rdata.an_tx_ring[i] = 0;
1117				break;
1118			}
1119		}
1120
1121		AN_INC(sc->an_rdata.an_tx_cons, AN_TX_RING_CNT);
1122	} else { /* MPI 350 */
1123		id = CSR_READ_2(sc, AN_TX_CMP_FID(sc->mpi350));
1124		if (!sc->an_rdata.an_tx_empty){
1125			if (status & AN_EV_TX_EXC) {
1126				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1127			} else
1128				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1129			AN_INC(sc->an_rdata.an_tx_cons, AN_MAX_TX_DESC);
1130			if (sc->an_rdata.an_tx_prod ==
1131			    sc->an_rdata.an_tx_cons)
1132				sc->an_rdata.an_tx_empty = 1;
1133		}
1134	}
1135
1136	return;
1137}
1138
1139/*
1140 * We abuse the stats updater to check the current NIC status. This
1141 * is important because we don't want to allow transmissions until
1142 * the NIC has synchronized to the current cell (either as the master
1143 * in an ad-hoc group, or as a station connected to an access point).
1144 *
1145 * Note that this function will be called via callout(9) with a lock held.
1146 */
1147static void
1148an_stats_update(void *xsc)
1149{
1150	struct an_softc		*sc;
1151	struct ifnet		*ifp;
1152
1153	sc = xsc;
1154	AN_LOCK_ASSERT(sc);
1155	ifp = sc->an_ifp;
1156	if (sc->an_timer > 0 && --sc->an_timer == 0)
1157		an_watchdog(sc);
1158
1159	sc->an_status.an_type = AN_RID_STATUS;
1160	sc->an_status.an_len = sizeof(struct an_ltv_status);
1161	if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_status))
1162		return;
1163
1164	if (sc->an_status.an_opmode & AN_STATUS_OPMODE_IN_SYNC)
1165		sc->an_associated = 1;
1166	else
1167		sc->an_associated = 0;
1168
1169	/* Don't do this while we're transmitting */
1170	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1171		callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1172		return;
1173	}
1174
1175	sc->an_stats.an_len = sizeof(struct an_ltv_stats);
1176	sc->an_stats.an_type = AN_RID_32BITS_CUM;
1177	if (an_read_record(sc, (struct an_ltv_gen *)&sc->an_stats.an_len))
1178		return;
1179
1180	callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
1181
1182	return;
1183}
1184
1185void
1186an_intr(void *xsc)
1187{
1188	struct an_softc		*sc;
1189	struct ifnet		*ifp;
1190	u_int16_t		status;
1191
1192	sc = (struct an_softc*)xsc;
1193
1194	AN_LOCK(sc);
1195
1196	if (sc->an_gone) {
1197		AN_UNLOCK(sc);
1198		return;
1199	}
1200
1201	ifp = sc->an_ifp;
1202
1203	/* Disable interrupts. */
1204	CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
1205
1206	status = CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350));
1207	CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), ~AN_INTRS(sc->mpi350));
1208
1209	if (status & AN_EV_MIC) {
1210		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_MIC);
1211	}
1212
1213	if (status & AN_EV_LINKSTAT) {
1214		if (CSR_READ_2(sc, AN_LINKSTAT(sc->mpi350))
1215		    == AN_LINKSTAT_ASSOCIATED)
1216			sc->an_associated = 1;
1217		else
1218			sc->an_associated = 0;
1219		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_LINKSTAT);
1220	}
1221
1222	if (status & AN_EV_RX) {
1223		an_rxeof(sc);
1224		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_RX);
1225	}
1226
1227	if (sc->mpi350 && status & AN_EV_TX_CPY) {
1228		an_txeof(sc, status);
1229		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_CPY);
1230	}
1231
1232	if (status & AN_EV_TX) {
1233		an_txeof(sc, status);
1234		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX);
1235	}
1236
1237	if (status & AN_EV_TX_EXC) {
1238		an_txeof(sc, status);
1239		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_TX_EXC);
1240	}
1241
1242	if (status & AN_EV_ALLOC)
1243		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1244
1245	/* Re-enable interrupts. */
1246	CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
1247
1248	if ((ifp->if_flags & IFF_UP) && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1249		an_start_locked(ifp);
1250
1251	AN_UNLOCK(sc);
1252
1253	return;
1254}
1255
1256
1257static int
1258an_cmd_struct(struct an_softc *sc, struct an_command *cmd,
1259    struct an_reply *reply)
1260{
1261	int			i;
1262
1263	AN_LOCK_ASSERT(sc);
1264	for (i = 0; i != AN_TIMEOUT; i++) {
1265		if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
1266			DELAY(1000);
1267		} else
1268			break;
1269	}
1270
1271	if( i == AN_TIMEOUT) {
1272		printf("BUSY\n");
1273		return(ETIMEDOUT);
1274	}
1275
1276	CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), cmd->an_parm0);
1277	CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), cmd->an_parm1);
1278	CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), cmd->an_parm2);
1279	CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd->an_cmd);
1280
1281	for (i = 0; i < AN_TIMEOUT; i++) {
1282		if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1283			break;
1284		DELAY(1000);
1285	}
1286
1287	reply->an_resp0 = CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1288	reply->an_resp1 = CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1289	reply->an_resp2 = CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1290	reply->an_status = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1291
1292	if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1293		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
1294		    AN_EV_CLR_STUCK_BUSY);
1295
1296	/* Ack the command */
1297	CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1298
1299	if (i == AN_TIMEOUT)
1300		return(ETIMEDOUT);
1301
1302	return(0);
1303}
1304
1305static int
1306an_cmd(struct an_softc *sc, int cmd, int val)
1307{
1308	int			i, s = 0;
1309
1310	AN_LOCK_ASSERT(sc);
1311	CSR_WRITE_2(sc, AN_PARAM0(sc->mpi350), val);
1312	CSR_WRITE_2(sc, AN_PARAM1(sc->mpi350), 0);
1313	CSR_WRITE_2(sc, AN_PARAM2(sc->mpi350), 0);
1314	CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1315
1316	for (i = 0; i < AN_TIMEOUT; i++) {
1317		if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_CMD)
1318			break;
1319		else {
1320			if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) == cmd)
1321				CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), cmd);
1322		}
1323	}
1324
1325	for (i = 0; i < AN_TIMEOUT; i++) {
1326		CSR_READ_2(sc, AN_RESP0(sc->mpi350));
1327		CSR_READ_2(sc, AN_RESP1(sc->mpi350));
1328		CSR_READ_2(sc, AN_RESP2(sc->mpi350));
1329		s = CSR_READ_2(sc, AN_STATUS(sc->mpi350));
1330		if ((s & AN_STAT_CMD_CODE) == (cmd & AN_STAT_CMD_CODE))
1331			break;
1332	}
1333
1334	/* Ack the command */
1335	CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CMD);
1336
1337	if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY)
1338		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_CLR_STUCK_BUSY);
1339
1340	if (i == AN_TIMEOUT)
1341		return(ETIMEDOUT);
1342
1343	return(0);
1344}
1345
1346/*
1347 * This reset sequence may look a little strange, but this is the
1348 * most reliable method I've found to really kick the NIC in the
1349 * head and force it to reboot correctly.
1350 */
1351static void
1352an_reset(struct an_softc *sc)
1353{
1354	if (sc->an_gone)
1355		return;
1356
1357	AN_LOCK_ASSERT(sc);
1358	an_cmd(sc, AN_CMD_ENABLE, 0);
1359	an_cmd(sc, AN_CMD_FW_RESTART, 0);
1360	an_cmd(sc, AN_CMD_NOOP2, 0);
1361
1362	if (an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0) == ETIMEDOUT)
1363		device_printf(sc->an_dev, "reset failed\n");
1364
1365	an_cmd(sc, AN_CMD_DISABLE, 0);
1366
1367	return;
1368}
1369
1370/*
1371 * Read an LTV record from the NIC.
1372 */
1373static int
1374an_read_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1375{
1376	struct an_ltv_gen	*an_ltv;
1377	struct an_card_rid_desc an_rid_desc;
1378	struct an_command	cmd;
1379	struct an_reply		reply;
1380	struct ifnet		*ifp;
1381	u_int16_t		*ptr;
1382	u_int8_t		*ptr2;
1383	int			i, len;
1384
1385	AN_LOCK_ASSERT(sc);
1386	if (ltv->an_len < 4 || ltv->an_type == 0)
1387		return(EINVAL);
1388
1389	ifp = sc->an_ifp;
1390	if (!sc->mpi350){
1391		/* Tell the NIC to enter record read mode. */
1392		if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type)) {
1393			if_printf(ifp, "RID access failed\n");
1394			return(EIO);
1395		}
1396
1397		/* Seek to the record. */
1398		if (an_seek(sc, ltv->an_type, 0, AN_BAP1)) {
1399			if_printf(ifp, "seek to record failed\n");
1400			return(EIO);
1401		}
1402
1403		/*
1404		 * Read the length and record type and make sure they
1405		 * match what we expect (this verifies that we have enough
1406		 * room to hold all of the returned data).
1407		 * Length includes type but not length.
1408		 */
1409		len = CSR_READ_2(sc, AN_DATA1);
1410		if (len > (ltv->an_len - 2)) {
1411			if_printf(ifp, "record length mismatch -- expected %d, "
1412			       "got %d for Rid %x\n",
1413			       ltv->an_len - 2, len, ltv->an_type);
1414			len = ltv->an_len - 2;
1415		} else {
1416			ltv->an_len = len + 2;
1417		}
1418
1419		/* Now read the data. */
1420		len -= 2;	/* skip the type */
1421		ptr = &ltv->an_val;
1422		for (i = len; i > 1; i -= 2)
1423			*ptr++ = CSR_READ_2(sc, AN_DATA1);
1424		if (i) {
1425			ptr2 = (u_int8_t *)ptr;
1426			*ptr2 = CSR_READ_1(sc, AN_DATA1);
1427		}
1428	} else { /* MPI-350 */
1429		if (!sc->an_rid_buffer.an_dma_vaddr)
1430			return(EIO);
1431		an_rid_desc.an_valid = 1;
1432		an_rid_desc.an_len = AN_RID_BUFFER_SIZE;
1433		an_rid_desc.an_rid = 0;
1434		an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1435		bzero(sc->an_rid_buffer.an_dma_vaddr, AN_RID_BUFFER_SIZE);
1436
1437		bzero(&cmd, sizeof(cmd));
1438		bzero(&reply, sizeof(reply));
1439		cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_READ;
1440		cmd.an_parm0 = ltv->an_type;
1441
1442		for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1443			CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1444			    ((u_int32_t *)(void *)&an_rid_desc)[i]);
1445
1446		if (an_cmd_struct(sc, &cmd, &reply)
1447		    || reply.an_status & AN_CMD_QUAL_MASK) {
1448			if_printf(ifp, "failed to read RID %x %x %x %x %x, %d\n",
1449			       ltv->an_type,
1450			       reply.an_status,
1451			       reply.an_resp0,
1452			       reply.an_resp1,
1453			       reply.an_resp2,
1454			       i);
1455			return(EIO);
1456		}
1457
1458		an_ltv = (struct an_ltv_gen *)sc->an_rid_buffer.an_dma_vaddr;
1459		if (an_ltv->an_len + 2 < an_rid_desc.an_len) {
1460			an_rid_desc.an_len = an_ltv->an_len;
1461		}
1462
1463		len = an_rid_desc.an_len;
1464		if (len > (ltv->an_len - 2)) {
1465			if_printf(ifp, "record length mismatch -- expected %d, "
1466			       "got %d for Rid %x\n",
1467			       ltv->an_len - 2, len, ltv->an_type);
1468			len = ltv->an_len - 2;
1469		} else {
1470			ltv->an_len = len + 2;
1471		}
1472		bcopy(&an_ltv->an_type,
1473		    &ltv->an_val,
1474		    len);
1475	}
1476
1477	if (an_dump)
1478		an_dump_record(sc, ltv, "Read");
1479
1480	return(0);
1481}
1482
1483/*
1484 * Same as read, except we inject data instead of reading it.
1485 */
1486static int
1487an_write_record(struct an_softc *sc, struct an_ltv_gen *ltv)
1488{
1489	struct an_card_rid_desc an_rid_desc;
1490	struct an_command	cmd;
1491	struct an_reply		reply;
1492	u_int16_t		*ptr;
1493	u_int8_t		*ptr2;
1494	int			i, len;
1495
1496	AN_LOCK_ASSERT(sc);
1497	if (an_dump)
1498		an_dump_record(sc, ltv, "Write");
1499
1500	if (!sc->mpi350){
1501		if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_READ, ltv->an_type))
1502			return(EIO);
1503
1504		if (an_seek(sc, ltv->an_type, 0, AN_BAP1))
1505			return(EIO);
1506
1507		/*
1508		 * Length includes type but not length.
1509		 */
1510		len = ltv->an_len - 2;
1511		CSR_WRITE_2(sc, AN_DATA1, len);
1512
1513		len -= 2;	/* skip the type */
1514		ptr = &ltv->an_val;
1515		for (i = len; i > 1; i -= 2)
1516			CSR_WRITE_2(sc, AN_DATA1, *ptr++);
1517		if (i) {
1518			ptr2 = (u_int8_t *)ptr;
1519			CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1520		}
1521
1522		if (an_cmd(sc, AN_CMD_ACCESS|AN_ACCESS_WRITE, ltv->an_type))
1523			return(EIO);
1524	} else {
1525		/* MPI-350 */
1526
1527		for (i = 0; i != AN_TIMEOUT; i++) {
1528			if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350))
1529			    & AN_CMD_BUSY) {
1530				DELAY(10);
1531			} else
1532				break;
1533		}
1534		if (i == AN_TIMEOUT) {
1535			printf("BUSY\n");
1536		}
1537
1538		an_rid_desc.an_valid = 1;
1539		an_rid_desc.an_len = ltv->an_len - 2;
1540		an_rid_desc.an_rid = ltv->an_type;
1541		an_rid_desc.an_phys = sc->an_rid_buffer.an_dma_paddr;
1542
1543		bcopy(&ltv->an_type, sc->an_rid_buffer.an_dma_vaddr,
1544		      an_rid_desc.an_len);
1545
1546		bzero(&cmd,sizeof(cmd));
1547		bzero(&reply,sizeof(reply));
1548		cmd.an_cmd = AN_CMD_ACCESS|AN_ACCESS_WRITE;
1549		cmd.an_parm0 = ltv->an_type;
1550
1551		for (i = 0; i < sizeof(an_rid_desc) / 4; i++)
1552			CSR_MEM_AUX_WRITE_4(sc, AN_HOST_DESC_OFFSET + i * 4,
1553			    ((u_int32_t *)(void *)&an_rid_desc)[i]);
1554
1555		DELAY(100000);
1556
1557		if ((i = an_cmd_struct(sc, &cmd, &reply))) {
1558			if_printf(sc->an_ifp,
1559			    "failed to write RID 1 %x %x %x %x %x, %d\n",
1560			    ltv->an_type,
1561			    reply.an_status,
1562			    reply.an_resp0,
1563			    reply.an_resp1,
1564			    reply.an_resp2,
1565			    i);
1566			return(EIO);
1567		}
1568
1569
1570		if (reply.an_status & AN_CMD_QUAL_MASK) {
1571			if_printf(sc->an_ifp,
1572			    "failed to write RID 2 %x %x %x %x %x, %d\n",
1573			    ltv->an_type,
1574			    reply.an_status,
1575			    reply.an_resp0,
1576			    reply.an_resp1,
1577			    reply.an_resp2,
1578			    i);
1579			return(EIO);
1580		}
1581		DELAY(100000);
1582	}
1583
1584	return(0);
1585}
1586
1587static void
1588an_dump_record(struct an_softc *sc, struct an_ltv_gen *ltv, char *string)
1589{
1590	u_int8_t		*ptr2;
1591	int			len;
1592	int			i;
1593	int			count = 0;
1594	char			buf[17], temp;
1595
1596	len = ltv->an_len - 4;
1597	if_printf(sc->an_ifp, "RID %4x, Length %4d, Mode %s\n",
1598		ltv->an_type, ltv->an_len - 4, string);
1599
1600	if (an_dump == 1 || (an_dump == ltv->an_type)) {
1601		if_printf(sc->an_ifp, "\t");
1602		bzero(buf,sizeof(buf));
1603
1604		ptr2 = (u_int8_t *)&ltv->an_val;
1605		for (i = len; i > 0; i--) {
1606			printf("%02x ", *ptr2);
1607
1608			temp = *ptr2++;
1609			if (isprint(temp))
1610				buf[count] = temp;
1611			else
1612				buf[count] = '.';
1613			if (++count == 16) {
1614				count = 0;
1615				printf("%s\n",buf);
1616				if_printf(sc->an_ifp, "\t");
1617				bzero(buf,sizeof(buf));
1618			}
1619		}
1620		for (; count != 16; count++) {
1621			printf("   ");
1622		}
1623		printf(" %s\n",buf);
1624	}
1625}
1626
1627static int
1628an_seek(struct an_softc *sc, int id, int off, int chan)
1629{
1630	int			i;
1631	int			selreg, offreg;
1632
1633	switch (chan) {
1634	case AN_BAP0:
1635		selreg = AN_SEL0;
1636		offreg = AN_OFF0;
1637		break;
1638	case AN_BAP1:
1639		selreg = AN_SEL1;
1640		offreg = AN_OFF1;
1641		break;
1642	default:
1643		if_printf(sc->an_ifp, "invalid data path: %x\n", chan);
1644		return(EIO);
1645	}
1646
1647	CSR_WRITE_2(sc, selreg, id);
1648	CSR_WRITE_2(sc, offreg, off);
1649
1650	for (i = 0; i < AN_TIMEOUT; i++) {
1651		if (!(CSR_READ_2(sc, offreg) & (AN_OFF_BUSY|AN_OFF_ERR)))
1652			break;
1653	}
1654
1655	if (i == AN_TIMEOUT)
1656		return(ETIMEDOUT);
1657
1658	return(0);
1659}
1660
1661static int
1662an_read_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1663{
1664	int			i;
1665	u_int16_t		*ptr;
1666	u_int8_t		*ptr2;
1667
1668	if (off != -1) {
1669		if (an_seek(sc, id, off, AN_BAP1))
1670			return(EIO);
1671	}
1672
1673	ptr = (u_int16_t *)buf;
1674	for (i = len; i > 1; i -= 2)
1675		*ptr++ = CSR_READ_2(sc, AN_DATA1);
1676	if (i) {
1677		ptr2 = (u_int8_t *)ptr;
1678		*ptr2 = CSR_READ_1(sc, AN_DATA1);
1679	}
1680
1681	return(0);
1682}
1683
1684static int
1685an_write_data(struct an_softc *sc, int id, int off, caddr_t buf, int len)
1686{
1687	int			i;
1688	u_int16_t		*ptr;
1689	u_int8_t		*ptr2;
1690
1691	if (off != -1) {
1692		if (an_seek(sc, id, off, AN_BAP0))
1693			return(EIO);
1694	}
1695
1696	ptr = (u_int16_t *)buf;
1697	for (i = len; i > 1; i -= 2)
1698		CSR_WRITE_2(sc, AN_DATA0, *ptr++);
1699	if (i) {
1700		ptr2 = (u_int8_t *)ptr;
1701		CSR_WRITE_1(sc, AN_DATA0, *ptr2);
1702	}
1703
1704	return(0);
1705}
1706
1707/*
1708 * Allocate a region of memory inside the NIC and zero
1709 * it out.
1710 */
1711static int
1712an_alloc_nicmem(struct an_softc *sc, int len, int *id)
1713{
1714	int			i;
1715
1716	if (an_cmd(sc, AN_CMD_ALLOC_MEM, len)) {
1717		if_printf(sc->an_ifp, "failed to allocate %d bytes on NIC\n",
1718		    len);
1719		return(ENOMEM);
1720	}
1721
1722	for (i = 0; i < AN_TIMEOUT; i++) {
1723		if (CSR_READ_2(sc, AN_EVENT_STAT(sc->mpi350)) & AN_EV_ALLOC)
1724			break;
1725	}
1726
1727	if (i == AN_TIMEOUT)
1728		return(ETIMEDOUT);
1729
1730	CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
1731	*id = CSR_READ_2(sc, AN_ALLOC_FID);
1732
1733	if (an_seek(sc, *id, 0, AN_BAP0))
1734		return(EIO);
1735
1736	for (i = 0; i < len / 2; i++)
1737		CSR_WRITE_2(sc, AN_DATA0, 0);
1738
1739	return(0);
1740}
1741
1742static void
1743an_setdef(struct an_softc *sc, struct an_req *areq)
1744{
1745	struct ifnet		*ifp;
1746	struct an_ltv_genconfig	*cfg;
1747	struct an_ltv_ssidlist_new	*ssid;
1748	struct an_ltv_aplist	*ap;
1749	struct an_ltv_gen	*sp;
1750
1751	ifp = sc->an_ifp;
1752
1753	AN_LOCK_ASSERT(sc);
1754	switch (areq->an_type) {
1755	case AN_RID_GENCONFIG:
1756		cfg = (struct an_ltv_genconfig *)areq;
1757
1758		bcopy((char *)&cfg->an_macaddr, IF_LLADDR(sc->an_ifp),
1759		    ETHER_ADDR_LEN);
1760
1761		bcopy((char *)cfg, (char *)&sc->an_config,
1762			sizeof(struct an_ltv_genconfig));
1763		break;
1764	case AN_RID_SSIDLIST:
1765		ssid = (struct an_ltv_ssidlist_new *)areq;
1766		bcopy((char *)ssid, (char *)&sc->an_ssidlist,
1767			sizeof(struct an_ltv_ssidlist_new));
1768		break;
1769	case AN_RID_APLIST:
1770		ap = (struct an_ltv_aplist *)areq;
1771		bcopy((char *)ap, (char *)&sc->an_aplist,
1772			sizeof(struct an_ltv_aplist));
1773		break;
1774	case AN_RID_TX_SPEED:
1775		sp = (struct an_ltv_gen *)areq;
1776		sc->an_tx_rate = sp->an_val;
1777
1778		/* Read the current configuration */
1779		sc->an_config.an_type = AN_RID_GENCONFIG;
1780		sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1781		an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
1782		cfg = &sc->an_config;
1783
1784		/* clear other rates and set the only one we want */
1785		bzero(cfg->an_rates, sizeof(cfg->an_rates));
1786		cfg->an_rates[0] = sc->an_tx_rate;
1787
1788		/* Save the new rate */
1789		sc->an_config.an_type = AN_RID_GENCONFIG;
1790		sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
1791		break;
1792	case AN_RID_WEP_TEMP:
1793		/* Cache the temp keys */
1794		bcopy(areq,
1795		    &sc->an_temp_keys[((struct an_ltv_key *)areq)->kindex],
1796		    sizeof(struct an_ltv_key));
1797	case AN_RID_WEP_PERM:
1798	case AN_RID_LEAPUSERNAME:
1799	case AN_RID_LEAPPASSWORD:
1800		an_init_locked(sc);
1801
1802		/* Disable the MAC. */
1803		an_cmd(sc, AN_CMD_DISABLE, 0);
1804
1805		/* Write the key */
1806		an_write_record(sc, (struct an_ltv_gen *)areq);
1807
1808		/* Turn the MAC back on. */
1809		an_cmd(sc, AN_CMD_ENABLE, 0);
1810
1811		break;
1812	case AN_RID_MONITOR_MODE:
1813		cfg = (struct an_ltv_genconfig *)areq;
1814		bpfdetach(ifp);
1815		if (ng_ether_detach_p != NULL)
1816			(*ng_ether_detach_p) (ifp);
1817		sc->an_monitor = cfg->an_len;
1818
1819		if (sc->an_monitor & AN_MONITOR) {
1820			if (sc->an_monitor & AN_MONITOR_AIRONET_HEADER) {
1821				bpfattach(ifp, DLT_AIRONET_HEADER,
1822					sizeof(struct ether_header));
1823			} else {
1824				bpfattach(ifp, DLT_IEEE802_11,
1825					sizeof(struct ether_header));
1826			}
1827		} else {
1828			bpfattach(ifp, DLT_EN10MB,
1829				  sizeof(struct ether_header));
1830			if (ng_ether_attach_p != NULL)
1831				(*ng_ether_attach_p) (ifp);
1832		}
1833		break;
1834	default:
1835		if_printf(ifp, "unknown RID: %x\n", areq->an_type);
1836		return;
1837	}
1838
1839
1840	/* Reinitialize the card. */
1841	if (ifp->if_flags)
1842		an_init_locked(sc);
1843
1844	return;
1845}
1846
1847/*
1848 * Derived from Linux driver to enable promiscious mode.
1849 */
1850
1851static void
1852an_promisc(struct an_softc *sc, int promisc)
1853{
1854	AN_LOCK_ASSERT(sc);
1855	if (sc->an_was_monitor) {
1856		an_reset(sc);
1857		if (sc->mpi350)
1858			an_init_mpi350_desc(sc);
1859	}
1860	if (sc->an_monitor || sc->an_was_monitor)
1861		an_init_locked(sc);
1862
1863	sc->an_was_monitor = sc->an_monitor;
1864	an_cmd(sc, AN_CMD_SET_MODE, promisc ? 0xffff : 0);
1865
1866	return;
1867}
1868
1869static int
1870an_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1871{
1872	int			error = 0;
1873	int			len;
1874	int			i, max;
1875	struct an_softc		*sc;
1876	struct ifreq		*ifr;
1877	struct thread		*td = curthread;
1878	struct ieee80211req	*ireq;
1879	struct ieee80211_channel	ch;
1880	u_int8_t		tmpstr[IEEE80211_NWID_LEN*2];
1881	u_int8_t		*tmpptr;
1882	struct an_ltv_genconfig	*config;
1883	struct an_ltv_key	*key;
1884	struct an_ltv_status	*status;
1885	struct an_ltv_ssidlist_new	*ssids;
1886	int			mode;
1887	struct aironet_ioctl	l_ioctl;
1888
1889	sc = ifp->if_softc;
1890	ifr = (struct ifreq *)data;
1891	ireq = (struct ieee80211req *)data;
1892
1893	config = (struct an_ltv_genconfig *)&sc->areq;
1894	key = (struct an_ltv_key *)&sc->areq;
1895	status = (struct an_ltv_status *)&sc->areq;
1896	ssids = (struct an_ltv_ssidlist_new *)&sc->areq;
1897
1898	if (sc->an_gone) {
1899		error = ENODEV;
1900		goto out;
1901	}
1902
1903	switch (command) {
1904	case SIOCSIFFLAGS:
1905		AN_LOCK(sc);
1906		if (ifp->if_flags & IFF_UP) {
1907			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1908			    ifp->if_flags & IFF_PROMISC &&
1909			    !(sc->an_if_flags & IFF_PROMISC)) {
1910				an_promisc(sc, 1);
1911			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1912			    !(ifp->if_flags & IFF_PROMISC) &&
1913			    sc->an_if_flags & IFF_PROMISC) {
1914				an_promisc(sc, 0);
1915			} else
1916				an_init_locked(sc);
1917		} else {
1918			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1919				an_stop(sc);
1920		}
1921		sc->an_if_flags = ifp->if_flags;
1922		AN_UNLOCK(sc);
1923		error = 0;
1924		break;
1925	case SIOCSIFMEDIA:
1926	case SIOCGIFMEDIA:
1927		error = ifmedia_ioctl(ifp, ifr, &sc->an_ifmedia, command);
1928		break;
1929	case SIOCADDMULTI:
1930	case SIOCDELMULTI:
1931		/* The Aironet has no multicast filter. */
1932		error = 0;
1933		break;
1934	case SIOCGAIRONET:
1935		error = copyin(ifr->ifr_data, &sc->areq, sizeof(sc->areq));
1936		if (error != 0)
1937			break;
1938		AN_LOCK(sc);
1939#ifdef ANCACHE
1940		if (sc->areq.an_type == AN_RID_ZERO_CACHE) {
1941			error = priv_check(td, PRIV_DRIVER);
1942			if (error)
1943				break;
1944			sc->an_sigitems = sc->an_nextitem = 0;
1945			break;
1946		} else if (sc->areq.an_type == AN_RID_READ_CACHE) {
1947			char *pt = (char *)&sc->areq.an_val;
1948			bcopy((char *)&sc->an_sigitems, (char *)pt,
1949			    sizeof(int));
1950			pt += sizeof(int);
1951			sc->areq.an_len = sizeof(int) / 2;
1952			bcopy((char *)&sc->an_sigcache, (char *)pt,
1953			    sizeof(struct an_sigcache) * sc->an_sigitems);
1954			sc->areq.an_len += ((sizeof(struct an_sigcache) *
1955			    sc->an_sigitems) / 2) + 1;
1956		} else
1957#endif
1958		if (an_read_record(sc, (struct an_ltv_gen *)&sc->areq)) {
1959			AN_UNLOCK(sc);
1960			error = EINVAL;
1961			break;
1962		}
1963		AN_UNLOCK(sc);
1964		error = copyout(&sc->areq, ifr->ifr_data, sizeof(sc->areq));
1965		break;
1966	case SIOCSAIRONET:
1967		if ((error = priv_check(td, PRIV_DRIVER)))
1968			goto out;
1969		AN_LOCK(sc);
1970		error = copyin(ifr->ifr_data, &sc->areq, sizeof(sc->areq));
1971		if (error != 0)
1972			break;
1973		an_setdef(sc, &sc->areq);
1974		AN_UNLOCK(sc);
1975		break;
1976	case SIOCGPRIVATE_0:		/* used by Cisco client utility */
1977		if ((error = priv_check(td, PRIV_DRIVER)))
1978			goto out;
1979		error = copyin(ifr->ifr_data, &l_ioctl, sizeof(l_ioctl));
1980		if (error)
1981			goto out;
1982		mode = l_ioctl.command;
1983
1984		AN_LOCK(sc);
1985		if (mode >= AIROGCAP && mode <= AIROGSTATSD32) {
1986			error = readrids(ifp, &l_ioctl);
1987		} else if (mode >= AIROPCAP && mode <= AIROPLEAPUSR) {
1988			error = writerids(ifp, &l_ioctl);
1989		} else if (mode >= AIROFLSHRST && mode <= AIRORESTART) {
1990			error = flashcard(ifp, &l_ioctl);
1991		} else {
1992			error =-1;
1993		}
1994		AN_UNLOCK(sc);
1995		if (!error) {
1996			/* copy out the updated command info */
1997			error = copyout(&l_ioctl, ifr->ifr_data, sizeof(l_ioctl));
1998		}
1999		break;
2000	case SIOCGPRIVATE_1:		/* used by Cisco client utility */
2001		if ((error = priv_check(td, PRIV_DRIVER)))
2002			goto out;
2003		error = copyin(ifr->ifr_data, &l_ioctl, sizeof(l_ioctl));
2004		if (error)
2005			goto out;
2006		l_ioctl.command = 0;
2007		error = AIROMAGIC;
2008		(void) copyout(&error, l_ioctl.data, sizeof(error));
2009		error = 0;
2010		break;
2011	case SIOCG80211:
2012		sc->areq.an_len = sizeof(sc->areq);
2013		/* was that a good idea DJA we are doing a short-cut */
2014		switch (ireq->i_type) {
2015		case IEEE80211_IOC_SSID:
2016			AN_LOCK(sc);
2017			if (ireq->i_val == -1) {
2018				sc->areq.an_type = AN_RID_STATUS;
2019				if (an_read_record(sc,
2020				    (struct an_ltv_gen *)&sc->areq)) {
2021					error = EINVAL;
2022					AN_UNLOCK(sc);
2023					break;
2024				}
2025				len = status->an_ssidlen;
2026				tmpptr = status->an_ssid;
2027			} else if (ireq->i_val >= 0) {
2028				sc->areq.an_type = AN_RID_SSIDLIST;
2029				if (an_read_record(sc,
2030				    (struct an_ltv_gen *)&sc->areq)) {
2031					error = EINVAL;
2032					AN_UNLOCK(sc);
2033					break;
2034				}
2035				max = (sc->areq.an_len - 4)
2036				    / sizeof(struct an_ltv_ssid_entry);
2037				if ( max > MAX_SSIDS ) {
2038					printf("To many SSIDs only using "
2039					    "%d of %d\n",
2040					    MAX_SSIDS, max);
2041					max = MAX_SSIDS;
2042				}
2043				if (ireq->i_val > max) {
2044					error = EINVAL;
2045					AN_UNLOCK(sc);
2046					break;
2047				} else {
2048					len = ssids->an_entry[ireq->i_val].an_len;
2049					tmpptr = ssids->an_entry[ireq->i_val].an_ssid;
2050				}
2051			} else {
2052				error = EINVAL;
2053				AN_UNLOCK(sc);
2054				break;
2055			}
2056			if (len > IEEE80211_NWID_LEN) {
2057				error = EINVAL;
2058				AN_UNLOCK(sc);
2059				break;
2060			}
2061			AN_UNLOCK(sc);
2062			ireq->i_len = len;
2063			bzero(tmpstr, IEEE80211_NWID_LEN);
2064			bcopy(tmpptr, tmpstr, len);
2065			error = copyout(tmpstr, ireq->i_data,
2066			    IEEE80211_NWID_LEN);
2067			break;
2068		case IEEE80211_IOC_NUMSSIDS:
2069			AN_LOCK(sc);
2070			sc->areq.an_len = sizeof(sc->areq);
2071			sc->areq.an_type = AN_RID_SSIDLIST;
2072			if (an_read_record(sc,
2073			    (struct an_ltv_gen *)&sc->areq)) {
2074				AN_UNLOCK(sc);
2075				error = EINVAL;
2076				break;
2077			}
2078			max = (sc->areq.an_len - 4)
2079			    / sizeof(struct an_ltv_ssid_entry);
2080			AN_UNLOCK(sc);
2081			if ( max > MAX_SSIDS ) {
2082				printf("To many SSIDs only using "
2083				    "%d of %d\n",
2084				    MAX_SSIDS, max);
2085				max = MAX_SSIDS;
2086			}
2087			ireq->i_val = max;
2088			break;
2089		case IEEE80211_IOC_WEP:
2090			AN_LOCK(sc);
2091			sc->areq.an_type = AN_RID_ACTUALCFG;
2092			if (an_read_record(sc,
2093			    (struct an_ltv_gen *)&sc->areq)) {
2094				error = EINVAL;
2095				AN_UNLOCK(sc);
2096				break;
2097			}
2098			AN_UNLOCK(sc);
2099			if (config->an_authtype & AN_AUTHTYPE_PRIVACY_IN_USE) {
2100				if (config->an_authtype &
2101				    AN_AUTHTYPE_ALLOW_UNENCRYPTED)
2102					ireq->i_val = IEEE80211_WEP_MIXED;
2103				else
2104					ireq->i_val = IEEE80211_WEP_ON;
2105			} else {
2106				ireq->i_val = IEEE80211_WEP_OFF;
2107			}
2108			break;
2109		case IEEE80211_IOC_WEPKEY:
2110			/*
2111			 * XXX: I'm not entierly convinced this is
2112			 * correct, but it's what is implemented in
2113			 * ancontrol so it will have to do until we get
2114			 * access to actual Cisco code.
2115			 */
2116			if (ireq->i_val < 0 || ireq->i_val > 8) {
2117				error = EINVAL;
2118				break;
2119			}
2120			len = 0;
2121			if (ireq->i_val < 5) {
2122				AN_LOCK(sc);
2123				sc->areq.an_type = AN_RID_WEP_TEMP;
2124				for (i = 0; i < 5; i++) {
2125					if (an_read_record(sc,
2126					    (struct an_ltv_gen *)&sc->areq)) {
2127						error = EINVAL;
2128						break;
2129					}
2130					if (key->kindex == 0xffff)
2131						break;
2132					if (key->kindex == ireq->i_val)
2133						len = key->klen;
2134					/* Required to get next entry */
2135					sc->areq.an_type = AN_RID_WEP_PERM;
2136				}
2137				AN_UNLOCK(sc);
2138				if (error != 0) {
2139					break;
2140				}
2141			}
2142			/* We aren't allowed to read the value of the
2143			 * key from the card so we just output zeros
2144			 * like we would if we could read the card, but
2145			 * denied the user access.
2146			 */
2147			bzero(tmpstr, len);
2148			ireq->i_len = len;
2149			error = copyout(tmpstr, ireq->i_data, len);
2150			break;
2151		case IEEE80211_IOC_NUMWEPKEYS:
2152			ireq->i_val = 9; /* include home key */
2153			break;
2154		case IEEE80211_IOC_WEPTXKEY:
2155			/*
2156			 * For some strange reason, you have to read all
2157			 * keys before you can read the txkey.
2158			 */
2159			AN_LOCK(sc);
2160			sc->areq.an_type = AN_RID_WEP_TEMP;
2161			for (i = 0; i < 5; i++) {
2162				if (an_read_record(sc,
2163				    (struct an_ltv_gen *) &sc->areq)) {
2164					error = EINVAL;
2165					break;
2166				}
2167				if (key->kindex == 0xffff) {
2168					break;
2169				}
2170				/* Required to get next entry */
2171				sc->areq.an_type = AN_RID_WEP_PERM;
2172			}
2173			if (error != 0) {
2174				AN_UNLOCK(sc);
2175				break;
2176			}
2177
2178			sc->areq.an_type = AN_RID_WEP_PERM;
2179			key->kindex = 0xffff;
2180			if (an_read_record(sc,
2181			    (struct an_ltv_gen *)&sc->areq)) {
2182				error = EINVAL;
2183				AN_UNLOCK(sc);
2184				break;
2185			}
2186			ireq->i_val = key->mac[0];
2187			/*
2188			 * Check for home mode.  Map home mode into
2189			 * 5th key since that is how it is stored on
2190			 * the card
2191			 */
2192			sc->areq.an_len  = sizeof(struct an_ltv_genconfig);
2193			sc->areq.an_type = AN_RID_GENCONFIG;
2194			if (an_read_record(sc,
2195			    (struct an_ltv_gen *)&sc->areq)) {
2196				error = EINVAL;
2197				AN_UNLOCK(sc);
2198				break;
2199			}
2200			if (config->an_home_product & AN_HOME_NETWORK)
2201				ireq->i_val = 4;
2202			AN_UNLOCK(sc);
2203			break;
2204		case IEEE80211_IOC_AUTHMODE:
2205			AN_LOCK(sc);
2206			sc->areq.an_type = AN_RID_ACTUALCFG;
2207			if (an_read_record(sc,
2208			    (struct an_ltv_gen *)&sc->areq)) {
2209				error = EINVAL;
2210				AN_UNLOCK(sc);
2211				break;
2212			}
2213			AN_UNLOCK(sc);
2214			if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2215			    AN_AUTHTYPE_NONE) {
2216			    ireq->i_val = IEEE80211_AUTH_NONE;
2217			} else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2218			    AN_AUTHTYPE_OPEN) {
2219			    ireq->i_val = IEEE80211_AUTH_OPEN;
2220			} else if ((config->an_authtype & AN_AUTHTYPE_MASK) ==
2221			    AN_AUTHTYPE_SHAREDKEY) {
2222			    ireq->i_val = IEEE80211_AUTH_SHARED;
2223			} else
2224				error = EINVAL;
2225			break;
2226		case IEEE80211_IOC_STATIONNAME:
2227			AN_LOCK(sc);
2228			sc->areq.an_type = AN_RID_ACTUALCFG;
2229			if (an_read_record(sc,
2230			    (struct an_ltv_gen *)&sc->areq)) {
2231				error = EINVAL;
2232				AN_UNLOCK(sc);
2233				break;
2234			}
2235			AN_UNLOCK(sc);
2236			ireq->i_len = sizeof(config->an_nodename);
2237			tmpptr = config->an_nodename;
2238			bzero(tmpstr, IEEE80211_NWID_LEN);
2239			bcopy(tmpptr, tmpstr, ireq->i_len);
2240			error = copyout(tmpstr, ireq->i_data,
2241			    IEEE80211_NWID_LEN);
2242			break;
2243		case IEEE80211_IOC_CHANNEL:
2244			AN_LOCK(sc);
2245			sc->areq.an_type = AN_RID_STATUS;
2246			if (an_read_record(sc,
2247			    (struct an_ltv_gen *)&sc->areq)) {
2248				error = EINVAL;
2249				AN_UNLOCK(sc);
2250				break;
2251			}
2252			AN_UNLOCK(sc);
2253			ireq->i_val = status->an_cur_channel;
2254			break;
2255		case IEEE80211_IOC_CURCHAN:
2256			AN_LOCK(sc);
2257			sc->areq.an_type = AN_RID_STATUS;
2258			if (an_read_record(sc,
2259			    (struct an_ltv_gen *)&sc->areq)) {
2260				error = EINVAL;
2261				AN_UNLOCK(sc);
2262				break;
2263			}
2264			AN_UNLOCK(sc);
2265			bzero(&ch, sizeof(ch));
2266			ch.ic_freq = ieee80211_ieee2mhz(status->an_cur_channel,
2267			    IEEE80211_CHAN_B);
2268			ch.ic_flags = IEEE80211_CHAN_B;
2269			ch.ic_ieee = status->an_cur_channel;
2270			error = copyout(&ch, ireq->i_data, sizeof(ch));
2271			break;
2272		case IEEE80211_IOC_POWERSAVE:
2273			AN_LOCK(sc);
2274			sc->areq.an_type = AN_RID_ACTUALCFG;
2275			if (an_read_record(sc,
2276			    (struct an_ltv_gen *)&sc->areq)) {
2277				error = EINVAL;
2278				AN_UNLOCK(sc);
2279				break;
2280			}
2281			AN_UNLOCK(sc);
2282			if (config->an_psave_mode == AN_PSAVE_NONE) {
2283				ireq->i_val = IEEE80211_POWERSAVE_OFF;
2284			} else if (config->an_psave_mode == AN_PSAVE_CAM) {
2285				ireq->i_val = IEEE80211_POWERSAVE_CAM;
2286			} else if (config->an_psave_mode == AN_PSAVE_PSP) {
2287				ireq->i_val = IEEE80211_POWERSAVE_PSP;
2288			} else if (config->an_psave_mode == AN_PSAVE_PSP_CAM) {
2289				ireq->i_val = IEEE80211_POWERSAVE_PSP_CAM;
2290			} else
2291				error = EINVAL;
2292			break;
2293		case IEEE80211_IOC_POWERSAVESLEEP:
2294			AN_LOCK(sc);
2295			sc->areq.an_type = AN_RID_ACTUALCFG;
2296			if (an_read_record(sc,
2297			    (struct an_ltv_gen *)&sc->areq)) {
2298				error = EINVAL;
2299				AN_UNLOCK(sc);
2300				break;
2301			}
2302			AN_UNLOCK(sc);
2303			ireq->i_val = config->an_listen_interval;
2304			break;
2305		}
2306		break;
2307	case SIOCS80211:
2308		if ((error = priv_check(td, PRIV_NET80211_MANAGE)))
2309			goto out;
2310		AN_LOCK(sc);
2311		sc->areq.an_len = sizeof(sc->areq);
2312		/*
2313		 * We need a config structure for everything but the WEP
2314		 * key management and SSIDs so we get it now so avoid
2315		 * duplicating this code every time.
2316		 */
2317		if (ireq->i_type != IEEE80211_IOC_SSID &&
2318		    ireq->i_type != IEEE80211_IOC_WEPKEY &&
2319		    ireq->i_type != IEEE80211_IOC_WEPTXKEY) {
2320			sc->areq.an_type = AN_RID_GENCONFIG;
2321			if (an_read_record(sc,
2322			    (struct an_ltv_gen *)&sc->areq)) {
2323				error = EINVAL;
2324				AN_UNLOCK(sc);
2325				break;
2326			}
2327		}
2328		switch (ireq->i_type) {
2329		case IEEE80211_IOC_SSID:
2330			sc->areq.an_len = sizeof(sc->areq);
2331			sc->areq.an_type = AN_RID_SSIDLIST;
2332			if (an_read_record(sc,
2333			    (struct an_ltv_gen *)&sc->areq)) {
2334				error = EINVAL;
2335				AN_UNLOCK(sc);
2336				break;
2337			}
2338			if (ireq->i_len > IEEE80211_NWID_LEN) {
2339				error = EINVAL;
2340				AN_UNLOCK(sc);
2341				break;
2342			}
2343			max = (sc->areq.an_len - 4)
2344			    / sizeof(struct an_ltv_ssid_entry);
2345			if ( max > MAX_SSIDS ) {
2346				printf("To many SSIDs only using "
2347				    "%d of %d\n",
2348				    MAX_SSIDS, max);
2349				max = MAX_SSIDS;
2350			}
2351			if (ireq->i_val > max) {
2352				error = EINVAL;
2353				AN_UNLOCK(sc);
2354				break;
2355			} else {
2356				error = copyin(ireq->i_data,
2357				    ssids->an_entry[ireq->i_val].an_ssid,
2358				    ireq->i_len);
2359				ssids->an_entry[ireq->i_val].an_len
2360				    = ireq->i_len;
2361				sc->areq.an_len = sizeof(sc->areq);
2362				sc->areq.an_type = AN_RID_SSIDLIST;
2363				an_setdef(sc, &sc->areq);
2364				AN_UNLOCK(sc);
2365				break;
2366			}
2367			break;
2368		case IEEE80211_IOC_WEP:
2369			switch (ireq->i_val) {
2370			case IEEE80211_WEP_OFF:
2371				config->an_authtype &=
2372				    ~(AN_AUTHTYPE_PRIVACY_IN_USE |
2373				    AN_AUTHTYPE_ALLOW_UNENCRYPTED);
2374				break;
2375			case IEEE80211_WEP_ON:
2376				config->an_authtype |=
2377				    AN_AUTHTYPE_PRIVACY_IN_USE;
2378				config->an_authtype &=
2379				    ~AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2380				break;
2381			case IEEE80211_WEP_MIXED:
2382				config->an_authtype |=
2383				    AN_AUTHTYPE_PRIVACY_IN_USE |
2384				    AN_AUTHTYPE_ALLOW_UNENCRYPTED;
2385				break;
2386			default:
2387				error = EINVAL;
2388				break;
2389			}
2390			if (error != EINVAL)
2391				an_setdef(sc, &sc->areq);
2392			AN_UNLOCK(sc);
2393			break;
2394		case IEEE80211_IOC_WEPKEY:
2395			if (ireq->i_val < 0 || ireq->i_val > 8 ||
2396			    ireq->i_len > 13) {
2397				error = EINVAL;
2398				AN_UNLOCK(sc);
2399				break;
2400			}
2401			error = copyin(ireq->i_data, tmpstr, 13);
2402			if (error != 0) {
2403				AN_UNLOCK(sc);
2404				break;
2405			}
2406			/*
2407			 * Map the 9th key into the home mode
2408			 * since that is how it is stored on
2409			 * the card
2410			 */
2411			bzero(&sc->areq, sizeof(struct an_ltv_key));
2412			sc->areq.an_len = sizeof(struct an_ltv_key);
2413			key->mac[0] = 1;	/* The others are 0. */
2414			if (ireq->i_val < 4) {
2415				sc->areq.an_type = AN_RID_WEP_TEMP;
2416				key->kindex = ireq->i_val;
2417			} else {
2418				sc->areq.an_type = AN_RID_WEP_PERM;
2419				key->kindex = ireq->i_val - 4;
2420			}
2421			key->klen = ireq->i_len;
2422			bcopy(tmpstr, key->key, key->klen);
2423			an_setdef(sc, &sc->areq);
2424			AN_UNLOCK(sc);
2425			break;
2426		case IEEE80211_IOC_WEPTXKEY:
2427			if (ireq->i_val < 0 || ireq->i_val > 4) {
2428				error = EINVAL;
2429				AN_UNLOCK(sc);
2430				break;
2431			}
2432
2433			/*
2434			 * Map the 5th key into the home mode
2435			 * since that is how it is stored on
2436			 * the card
2437			 */
2438			sc->areq.an_len  = sizeof(struct an_ltv_genconfig);
2439			sc->areq.an_type = AN_RID_ACTUALCFG;
2440			if (an_read_record(sc,
2441			    (struct an_ltv_gen *)&sc->areq)) {
2442				error = EINVAL;
2443				AN_UNLOCK(sc);
2444				break;
2445			}
2446			if (ireq->i_val ==  4) {
2447				config->an_home_product |= AN_HOME_NETWORK;
2448				ireq->i_val = 0;
2449			} else {
2450				config->an_home_product &= ~AN_HOME_NETWORK;
2451			}
2452
2453			sc->an_config.an_home_product
2454				= config->an_home_product;
2455
2456			/* update configuration */
2457			an_init_locked(sc);
2458
2459			bzero(&sc->areq, sizeof(struct an_ltv_key));
2460			sc->areq.an_len = sizeof(struct an_ltv_key);
2461			sc->areq.an_type = AN_RID_WEP_PERM;
2462			key->kindex = 0xffff;
2463			key->mac[0] = ireq->i_val;
2464			an_setdef(sc, &sc->areq);
2465			AN_UNLOCK(sc);
2466			break;
2467		case IEEE80211_IOC_AUTHMODE:
2468			switch (ireq->i_val) {
2469			case IEEE80211_AUTH_NONE:
2470				config->an_authtype = AN_AUTHTYPE_NONE |
2471				    (config->an_authtype & ~AN_AUTHTYPE_MASK);
2472				break;
2473			case IEEE80211_AUTH_OPEN:
2474				config->an_authtype = AN_AUTHTYPE_OPEN |
2475				    (config->an_authtype & ~AN_AUTHTYPE_MASK);
2476				break;
2477			case IEEE80211_AUTH_SHARED:
2478				config->an_authtype = AN_AUTHTYPE_SHAREDKEY |
2479				    (config->an_authtype & ~AN_AUTHTYPE_MASK);
2480				break;
2481			default:
2482				error = EINVAL;
2483			}
2484			if (error != EINVAL) {
2485				an_setdef(sc, &sc->areq);
2486			}
2487			AN_UNLOCK(sc);
2488			break;
2489		case IEEE80211_IOC_STATIONNAME:
2490			if (ireq->i_len > 16) {
2491				error = EINVAL;
2492				AN_UNLOCK(sc);
2493				break;
2494			}
2495			bzero(config->an_nodename, 16);
2496			error = copyin(ireq->i_data,
2497			    config->an_nodename, ireq->i_len);
2498			an_setdef(sc, &sc->areq);
2499			AN_UNLOCK(sc);
2500			break;
2501		case IEEE80211_IOC_CHANNEL:
2502			/*
2503			 * The actual range is 1-14, but if you set it
2504			 * to 0 you get the default so we let that work
2505			 * too.
2506			 */
2507			if (ireq->i_val < 0 || ireq->i_val >14) {
2508				error = EINVAL;
2509				AN_UNLOCK(sc);
2510				break;
2511			}
2512			config->an_ds_channel = ireq->i_val;
2513			an_setdef(sc, &sc->areq);
2514			AN_UNLOCK(sc);
2515			break;
2516		case IEEE80211_IOC_POWERSAVE:
2517			switch (ireq->i_val) {
2518			case IEEE80211_POWERSAVE_OFF:
2519				config->an_psave_mode = AN_PSAVE_NONE;
2520				break;
2521			case IEEE80211_POWERSAVE_CAM:
2522				config->an_psave_mode = AN_PSAVE_CAM;
2523				break;
2524			case IEEE80211_POWERSAVE_PSP:
2525				config->an_psave_mode = AN_PSAVE_PSP;
2526				break;
2527			case IEEE80211_POWERSAVE_PSP_CAM:
2528				config->an_psave_mode = AN_PSAVE_PSP_CAM;
2529				break;
2530			default:
2531				error = EINVAL;
2532				break;
2533			}
2534			an_setdef(sc, &sc->areq);
2535			AN_UNLOCK(sc);
2536			break;
2537		case IEEE80211_IOC_POWERSAVESLEEP:
2538			config->an_listen_interval = ireq->i_val;
2539			an_setdef(sc, &sc->areq);
2540			AN_UNLOCK(sc);
2541			break;
2542		default:
2543			AN_UNLOCK(sc);
2544			break;
2545		}
2546
2547		/*
2548		if (!error) {
2549			AN_LOCK(sc);
2550			an_setdef(sc, &sc->areq);
2551			AN_UNLOCK(sc);
2552		}
2553		*/
2554		break;
2555	default:
2556		error = ether_ioctl(ifp, command, data);
2557		break;
2558	}
2559out:
2560
2561	return(error != 0);
2562}
2563
2564static int
2565an_init_tx_ring(struct an_softc *sc)
2566{
2567	int			i;
2568	int			id;
2569
2570	if (sc->an_gone)
2571		return (0);
2572
2573	if (!sc->mpi350) {
2574		for (i = 0; i < AN_TX_RING_CNT; i++) {
2575			if (an_alloc_nicmem(sc, 1518 +
2576			    0x44, &id))
2577				return(ENOMEM);
2578			sc->an_rdata.an_tx_fids[i] = id;
2579			sc->an_rdata.an_tx_ring[i] = 0;
2580		}
2581	}
2582
2583	sc->an_rdata.an_tx_prod = 0;
2584	sc->an_rdata.an_tx_cons = 0;
2585	sc->an_rdata.an_tx_empty = 1;
2586
2587	return(0);
2588}
2589
2590static void
2591an_init(void *xsc)
2592{
2593	struct an_softc		*sc = xsc;
2594
2595	AN_LOCK(sc);
2596	an_init_locked(sc);
2597	AN_UNLOCK(sc);
2598}
2599
2600static void
2601an_init_locked(struct an_softc *sc)
2602{
2603	struct ifnet *ifp;
2604
2605	AN_LOCK_ASSERT(sc);
2606	ifp = sc->an_ifp;
2607	if (sc->an_gone)
2608		return;
2609
2610	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2611		an_stop(sc);
2612
2613	sc->an_associated = 0;
2614
2615	/* Allocate the TX buffers */
2616	if (an_init_tx_ring(sc)) {
2617		an_reset(sc);
2618		if (sc->mpi350)
2619			an_init_mpi350_desc(sc);
2620		if (an_init_tx_ring(sc)) {
2621			if_printf(ifp, "tx buffer allocation failed\n");
2622			return;
2623		}
2624	}
2625
2626	/* Set our MAC address. */
2627	bcopy((char *)IF_LLADDR(sc->an_ifp),
2628	    (char *)&sc->an_config.an_macaddr, ETHER_ADDR_LEN);
2629
2630	if (ifp->if_flags & IFF_BROADCAST)
2631		sc->an_config.an_rxmode = AN_RXMODE_BC_ADDR;
2632	else
2633		sc->an_config.an_rxmode = AN_RXMODE_ADDR;
2634
2635	if (ifp->if_flags & IFF_MULTICAST)
2636		sc->an_config.an_rxmode = AN_RXMODE_BC_MC_ADDR;
2637
2638	if (ifp->if_flags & IFF_PROMISC) {
2639		if (sc->an_monitor & AN_MONITOR) {
2640			if (sc->an_monitor & AN_MONITOR_ANY_BSS) {
2641				sc->an_config.an_rxmode |=
2642				    AN_RXMODE_80211_MONITOR_ANYBSS |
2643				    AN_RXMODE_NO_8023_HEADER;
2644			} else {
2645				sc->an_config.an_rxmode |=
2646				    AN_RXMODE_80211_MONITOR_CURBSS |
2647				    AN_RXMODE_NO_8023_HEADER;
2648			}
2649		}
2650	}
2651
2652#ifdef ANCACHE
2653	if (sc->an_have_rssimap)
2654		sc->an_config.an_rxmode |= AN_RXMODE_NORMALIZED_RSSI;
2655#endif
2656
2657	/* Set the ssid list */
2658	sc->an_ssidlist.an_type = AN_RID_SSIDLIST;
2659	sc->an_ssidlist.an_len = sizeof(struct an_ltv_ssidlist_new);
2660	if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_ssidlist)) {
2661		if_printf(ifp, "failed to set ssid list\n");
2662		return;
2663	}
2664
2665	/* Set the AP list */
2666	sc->an_aplist.an_type = AN_RID_APLIST;
2667	sc->an_aplist.an_len = sizeof(struct an_ltv_aplist);
2668	if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_aplist)) {
2669		if_printf(ifp, "failed to set AP list\n");
2670		return;
2671	}
2672
2673	/* Set the configuration in the NIC */
2674	sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
2675	sc->an_config.an_type = AN_RID_GENCONFIG;
2676	if (an_write_record(sc, (struct an_ltv_gen *)&sc->an_config)) {
2677		if_printf(ifp, "failed to set configuration\n");
2678		return;
2679	}
2680
2681	/* Enable the MAC */
2682	if (an_cmd(sc, AN_CMD_ENABLE, 0)) {
2683		if_printf(ifp, "failed to enable MAC\n");
2684		return;
2685	}
2686
2687	if (ifp->if_flags & IFF_PROMISC)
2688		an_cmd(sc, AN_CMD_SET_MODE, 0xffff);
2689
2690	/* enable interrupts */
2691	CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2692
2693	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2694	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2695
2696	callout_reset(&sc->an_stat_ch, hz, an_stats_update, sc);
2697
2698	return;
2699}
2700
2701static void
2702an_start(struct ifnet *ifp)
2703{
2704	struct an_softc		*sc;
2705
2706	sc = ifp->if_softc;
2707	AN_LOCK(sc);
2708	an_start_locked(ifp);
2709	AN_UNLOCK(sc);
2710}
2711
2712static void
2713an_start_locked(struct ifnet *ifp)
2714{
2715	struct an_softc		*sc;
2716	struct mbuf		*m0 = NULL;
2717	struct an_txframe_802_3	tx_frame_802_3;
2718	struct ether_header	*eh;
2719	int			id, idx, i;
2720	unsigned char		txcontrol;
2721	struct an_card_tx_desc an_tx_desc;
2722	u_int8_t		*buf;
2723
2724	sc = ifp->if_softc;
2725
2726	AN_LOCK_ASSERT(sc);
2727	if (sc->an_gone)
2728		return;
2729
2730	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
2731		return;
2732
2733	if (!sc->an_associated)
2734		return;
2735
2736	/* We can't send in monitor mode so toss any attempts. */
2737	if (sc->an_monitor && (ifp->if_flags & IFF_PROMISC)) {
2738		for (;;) {
2739			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2740			if (m0 == NULL)
2741				break;
2742			m_freem(m0);
2743		}
2744		return;
2745	}
2746
2747	idx = sc->an_rdata.an_tx_prod;
2748
2749	if (!sc->mpi350) {
2750		bzero((char *)&tx_frame_802_3, sizeof(tx_frame_802_3));
2751
2752		while (sc->an_rdata.an_tx_ring[idx] == 0) {
2753			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2754			if (m0 == NULL)
2755				break;
2756
2757			id = sc->an_rdata.an_tx_fids[idx];
2758			eh = mtod(m0, struct ether_header *);
2759
2760			bcopy((char *)&eh->ether_dhost,
2761			      (char *)&tx_frame_802_3.an_tx_dst_addr,
2762			      ETHER_ADDR_LEN);
2763			bcopy((char *)&eh->ether_shost,
2764			      (char *)&tx_frame_802_3.an_tx_src_addr,
2765			      ETHER_ADDR_LEN);
2766
2767			/* minus src/dest mac & type */
2768			tx_frame_802_3.an_tx_802_3_payload_len =
2769				m0->m_pkthdr.len - 12;
2770
2771			m_copydata(m0, sizeof(struct ether_header) - 2 ,
2772				   tx_frame_802_3.an_tx_802_3_payload_len,
2773				   (caddr_t)&sc->an_txbuf);
2774
2775			txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2776			/* write the txcontrol only */
2777			an_write_data(sc, id, 0x08, (caddr_t)&txcontrol,
2778				      sizeof(txcontrol));
2779
2780			/* 802_3 header */
2781			an_write_data(sc, id, 0x34, (caddr_t)&tx_frame_802_3,
2782				      sizeof(struct an_txframe_802_3));
2783
2784			/* in mbuf header type is just before payload */
2785			an_write_data(sc, id, 0x44, (caddr_t)&sc->an_txbuf,
2786				      tx_frame_802_3.an_tx_802_3_payload_len);
2787
2788			/*
2789			 * If there's a BPF listner, bounce a copy of
2790			 * this frame to him.
2791			 */
2792			BPF_MTAP(ifp, m0);
2793
2794			m_freem(m0);
2795			m0 = NULL;
2796
2797			sc->an_rdata.an_tx_ring[idx] = id;
2798			if (an_cmd(sc, AN_CMD_TX, id))
2799				if_printf(ifp, "xmit failed\n");
2800
2801			AN_INC(idx, AN_TX_RING_CNT);
2802
2803			/*
2804			 * Set a timeout in case the chip goes out to lunch.
2805			 */
2806			sc->an_timer = 5;
2807		}
2808	} else { /* MPI-350 */
2809		/* Disable interrupts. */
2810		CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2811
2812		while (sc->an_rdata.an_tx_empty ||
2813		    idx != sc->an_rdata.an_tx_cons) {
2814			IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2815			if (m0 == NULL) {
2816				break;
2817			}
2818			buf = sc->an_tx_buffer[idx].an_dma_vaddr;
2819
2820			eh = mtod(m0, struct ether_header *);
2821
2822			/* DJA optimize this to limit bcopy */
2823			bcopy((char *)&eh->ether_dhost,
2824			      (char *)&tx_frame_802_3.an_tx_dst_addr,
2825			      ETHER_ADDR_LEN);
2826			bcopy((char *)&eh->ether_shost,
2827			      (char *)&tx_frame_802_3.an_tx_src_addr,
2828			      ETHER_ADDR_LEN);
2829
2830			/* minus src/dest mac & type */
2831			tx_frame_802_3.an_tx_802_3_payload_len =
2832				m0->m_pkthdr.len - 12;
2833
2834			m_copydata(m0, sizeof(struct ether_header) - 2 ,
2835				   tx_frame_802_3.an_tx_802_3_payload_len,
2836				   (caddr_t)&sc->an_txbuf);
2837
2838			txcontrol = AN_TXCTL_8023 | AN_TXCTL_HW(sc->mpi350);
2839			/* write the txcontrol only */
2840			bcopy((caddr_t)&txcontrol, &buf[0x08],
2841			      sizeof(txcontrol));
2842
2843			/* 802_3 header */
2844			bcopy((caddr_t)&tx_frame_802_3, &buf[0x34],
2845			      sizeof(struct an_txframe_802_3));
2846
2847			/* in mbuf header type is just before payload */
2848			bcopy((caddr_t)&sc->an_txbuf, &buf[0x44],
2849			      tx_frame_802_3.an_tx_802_3_payload_len);
2850
2851
2852			bzero(&an_tx_desc, sizeof(an_tx_desc));
2853			an_tx_desc.an_offset = 0;
2854			an_tx_desc.an_eoc = 1;
2855			an_tx_desc.an_valid = 1;
2856			an_tx_desc.an_len =  0x44 +
2857			    tx_frame_802_3.an_tx_802_3_payload_len;
2858			an_tx_desc.an_phys
2859			    = sc->an_tx_buffer[idx].an_dma_paddr;
2860			for (i = sizeof(an_tx_desc) / 4 - 1; i >= 0; i--) {
2861				CSR_MEM_AUX_WRITE_4(sc, AN_TX_DESC_OFFSET
2862				    /* zero for now */
2863				    + (0 * sizeof(an_tx_desc))
2864				    + (i * 4),
2865				    ((u_int32_t *)(void *)&an_tx_desc)[i]);
2866			}
2867
2868			/*
2869			 * If there's a BPF listner, bounce a copy of
2870			 * this frame to him.
2871			 */
2872			BPF_MTAP(ifp, m0);
2873
2874			m_freem(m0);
2875			m0 = NULL;
2876			AN_INC(idx, AN_MAX_TX_DESC);
2877			sc->an_rdata.an_tx_empty = 0;
2878			CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350), AN_EV_ALLOC);
2879
2880			/*
2881			 * Set a timeout in case the chip goes out to lunch.
2882			 */
2883			sc->an_timer = 5;
2884		}
2885
2886		/* Re-enable interrupts. */
2887		CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), AN_INTRS(sc->mpi350));
2888	}
2889
2890	if (m0 != NULL)
2891		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2892
2893	sc->an_rdata.an_tx_prod = idx;
2894
2895	return;
2896}
2897
2898void
2899an_stop(struct an_softc *sc)
2900{
2901	struct ifnet		*ifp;
2902	int			i;
2903
2904	AN_LOCK_ASSERT(sc);
2905
2906	if (sc->an_gone)
2907		return;
2908
2909	ifp = sc->an_ifp;
2910
2911	an_cmd(sc, AN_CMD_FORCE_SYNCLOSS, 0);
2912	CSR_WRITE_2(sc, AN_INT_EN(sc->mpi350), 0);
2913	an_cmd(sc, AN_CMD_DISABLE, 0);
2914
2915	for (i = 0; i < AN_TX_RING_CNT; i++)
2916		an_cmd(sc, AN_CMD_DEALLOC_MEM, sc->an_rdata.an_tx_fids[i]);
2917
2918	callout_stop(&sc->an_stat_ch);
2919
2920	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
2921
2922	if (sc->an_flash_buffer) {
2923		free(sc->an_flash_buffer, M_DEVBUF);
2924		sc->an_flash_buffer = NULL;
2925	}
2926}
2927
2928static void
2929an_watchdog(struct an_softc *sc)
2930{
2931	struct ifnet *ifp;
2932
2933	AN_LOCK_ASSERT(sc);
2934
2935	if (sc->an_gone)
2936		return;
2937
2938	ifp = sc->an_ifp;
2939	if_printf(ifp, "device timeout\n");
2940
2941	an_reset(sc);
2942	if (sc->mpi350)
2943		an_init_mpi350_desc(sc);
2944	an_init_locked(sc);
2945
2946	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2947}
2948
2949int
2950an_shutdown(device_t dev)
2951{
2952	struct an_softc		*sc;
2953
2954	sc = device_get_softc(dev);
2955	AN_LOCK(sc);
2956	an_stop(sc);
2957	sc->an_gone = 1;
2958	AN_UNLOCK(sc);
2959
2960	return (0);
2961}
2962
2963void
2964an_resume(device_t dev)
2965{
2966	struct an_softc		*sc;
2967	struct ifnet		*ifp;
2968	int			i;
2969
2970	sc = device_get_softc(dev);
2971	AN_LOCK(sc);
2972	ifp = sc->an_ifp;
2973
2974	sc->an_gone = 0;
2975	an_reset(sc);
2976	if (sc->mpi350)
2977		an_init_mpi350_desc(sc);
2978	an_init_locked(sc);
2979
2980	/* Recovery temporary keys */
2981	for (i = 0; i < 4; i++) {
2982		sc->areq.an_type = AN_RID_WEP_TEMP;
2983		sc->areq.an_len = sizeof(struct an_ltv_key);
2984		bcopy(&sc->an_temp_keys[i],
2985		    &sc->areq, sizeof(struct an_ltv_key));
2986		an_setdef(sc, &sc->areq);
2987	}
2988
2989	if (ifp->if_flags & IFF_UP)
2990		an_start_locked(ifp);
2991	AN_UNLOCK(sc);
2992
2993	return;
2994}
2995
2996#ifdef ANCACHE
2997/* Aironet signal strength cache code.
2998 * store signal/noise/quality on per MAC src basis in
2999 * a small fixed cache.  The cache wraps if > MAX slots
3000 * used.  The cache may be zeroed out to start over.
3001 * Two simple filters exist to reduce computation:
3002 * 1. ip only (literally 0x800, ETHERTYPE_IP) which may be used
3003 * to ignore some packets.  It defaults to ip only.
3004 * it could be used to focus on broadcast, non-IP 802.11 beacons.
3005 * 2. multicast/broadcast only.  This may be used to
3006 * ignore unicast packets and only cache signal strength
3007 * for multicast/broadcast packets (beacons); e.g., Mobile-IP
3008 * beacons and not unicast traffic.
3009 *
3010 * The cache stores (MAC src(index), IP src (major clue), signal,
3011 *	quality, noise)
3012 *
3013 * No apologies for storing IP src here.  It's easy and saves much
3014 * trouble elsewhere.  The cache is assumed to be INET dependent,
3015 * although it need not be.
3016 *
3017 * Note: the Aironet only has a single byte of signal strength value
3018 * in the rx frame header, and it's not scaled to anything sensible.
3019 * This is kind of lame, but it's all we've got.
3020 */
3021
3022#ifdef documentation
3023
3024int an_sigitems;				/* number of cached entries */
3025struct an_sigcache an_sigcache[MAXANCACHE];	/* array of cache entries */
3026int an_nextitem;				/* index/# of entries */
3027
3028
3029#endif
3030
3031/* control variables for cache filtering.  Basic idea is
3032 * to reduce cost (e.g., to only Mobile-IP agent beacons
3033 * which are broadcast or multicast).  Still you might
3034 * want to measure signal strength anth unicast ping packets
3035 * on a pt. to pt. ant. setup.
3036 */
3037/* set true if you want to limit cache items to broadcast/mcast
3038 * only packets (not unicast).  Useful for mobile-ip beacons which
3039 * are broadcast/multicast at network layer.  Default is all packets
3040 * so ping/unicast anll work say anth pt. to pt. antennae setup.
3041 */
3042static int an_cache_mcastonly = 0;
3043SYSCTL_INT(_hw_an, OID_AUTO, an_cache_mcastonly, CTLFLAG_RW,
3044	&an_cache_mcastonly, 0, "");
3045
3046/* set true if you want to limit cache items to IP packets only
3047*/
3048static int an_cache_iponly = 1;
3049SYSCTL_INT(_hw_an, OID_AUTO, an_cache_iponly, CTLFLAG_RW,
3050	&an_cache_iponly, 0, "");
3051
3052/*
3053 * an_cache_store, per rx packet store signal
3054 * strength in MAC (src) indexed cache.
3055 */
3056static void
3057an_cache_store(struct an_softc *sc, struct ether_header *eh, struct mbuf *m,
3058    u_int8_t rx_rssi, u_int8_t rx_quality)
3059{
3060	struct ip *ip = NULL;
3061	int i;
3062	static int cache_slot = 0; 	/* use this cache entry */
3063	static int wrapindex = 0;	/* next "free" cache entry */
3064	int type_ipv4 = 0;
3065
3066	/* filters:
3067	 * 1. ip only
3068	 * 2. configurable filter to throw out unicast packets,
3069	 * keep multicast only.
3070	 */
3071
3072	if ((ntohs(eh->ether_type) == ETHERTYPE_IP)) {
3073		type_ipv4 = 1;
3074	}
3075
3076	/* filter for ip packets only
3077	*/
3078	if ( an_cache_iponly && !type_ipv4) {
3079		return;
3080	}
3081
3082	/* filter for broadcast/multicast only
3083	 */
3084	if (an_cache_mcastonly && ((eh->ether_dhost[0] & 1) == 0)) {
3085		return;
3086	}
3087
3088#ifdef SIGDEBUG
3089	if_printf(sc->an_ifp, "q value %x (MSB=0x%x, LSB=0x%x) \n",
3090		rx_rssi & 0xffff, rx_rssi >> 8, rx_rssi & 0xff);
3091#endif
3092
3093	/* find the ip header.  we want to store the ip_src
3094	 * address.
3095	 */
3096	if (type_ipv4) {
3097		ip = mtod(m, struct ip *);
3098	}
3099
3100	/* do a linear search for a matching MAC address
3101	 * in the cache table
3102	 * . MAC address is 6 bytes,
3103	 * . var w_nextitem holds total number of entries already cached
3104	 */
3105	for (i = 0; i < sc->an_nextitem; i++) {
3106		if (! bcmp(eh->ether_shost , sc->an_sigcache[i].macsrc,  6 )) {
3107			/* Match!,
3108			 * so we already have this entry,
3109			 * update the data
3110			 */
3111			break;
3112		}
3113	}
3114
3115	/* did we find a matching mac address?
3116	 * if yes, then overwrite a previously existing cache entry
3117	 */
3118	if (i < sc->an_nextitem )   {
3119		cache_slot = i;
3120	}
3121	/* else, have a new address entry,so
3122	 * add this new entry,
3123	 * if table full, then we need to replace LRU entry
3124	 */
3125	else    {
3126
3127		/* check for space in cache table
3128		 * note: an_nextitem also holds number of entries
3129		 * added in the cache table
3130		 */
3131		if ( sc->an_nextitem < MAXANCACHE ) {
3132			cache_slot = sc->an_nextitem;
3133			sc->an_nextitem++;
3134			sc->an_sigitems = sc->an_nextitem;
3135		}
3136		/* no space found, so simply wrap anth wrap index
3137		 * and "zap" the next entry
3138		 */
3139		else {
3140			if (wrapindex == MAXANCACHE) {
3141				wrapindex = 0;
3142			}
3143			cache_slot = wrapindex++;
3144		}
3145	}
3146
3147	/* invariant: cache_slot now points at some slot
3148	 * in cache.
3149	 */
3150	if (cache_slot < 0 || cache_slot >= MAXANCACHE) {
3151		log(LOG_ERR, "an_cache_store, bad index: %d of "
3152		    "[0..%d], gross cache error\n",
3153		    cache_slot, MAXANCACHE);
3154		return;
3155	}
3156
3157	/*  store items in cache
3158	 *  .ip source address
3159	 *  .mac src
3160	 *  .signal, etc.
3161	 */
3162	if (type_ipv4) {
3163		sc->an_sigcache[cache_slot].ipsrc = ip->ip_src.s_addr;
3164	}
3165	bcopy( eh->ether_shost, sc->an_sigcache[cache_slot].macsrc,  6);
3166
3167
3168	switch (an_cache_mode) {
3169	case DBM:
3170		if (sc->an_have_rssimap) {
3171			sc->an_sigcache[cache_slot].signal =
3172				- sc->an_rssimap.an_entries[rx_rssi].an_rss_dbm;
3173			sc->an_sigcache[cache_slot].quality =
3174				- sc->an_rssimap.an_entries[rx_quality].an_rss_dbm;
3175		} else {
3176			sc->an_sigcache[cache_slot].signal = rx_rssi - 100;
3177			sc->an_sigcache[cache_slot].quality = rx_quality - 100;
3178		}
3179		break;
3180	case PERCENT:
3181		if (sc->an_have_rssimap) {
3182			sc->an_sigcache[cache_slot].signal =
3183				sc->an_rssimap.an_entries[rx_rssi].an_rss_pct;
3184			sc->an_sigcache[cache_slot].quality =
3185				sc->an_rssimap.an_entries[rx_quality].an_rss_pct;
3186		} else {
3187			if (rx_rssi > 100)
3188				rx_rssi = 100;
3189			if (rx_quality > 100)
3190				rx_quality = 100;
3191			sc->an_sigcache[cache_slot].signal = rx_rssi;
3192			sc->an_sigcache[cache_slot].quality = rx_quality;
3193		}
3194		break;
3195	case RAW:
3196		sc->an_sigcache[cache_slot].signal = rx_rssi;
3197		sc->an_sigcache[cache_slot].quality = rx_quality;
3198		break;
3199	}
3200
3201	sc->an_sigcache[cache_slot].noise = 0;
3202
3203	return;
3204}
3205#endif
3206
3207static int
3208an_media_change(struct ifnet *ifp)
3209{
3210	struct an_softc *sc = ifp->if_softc;
3211	struct an_ltv_genconfig	*cfg;
3212	int otype = sc->an_config.an_opmode;
3213	int orate = sc->an_tx_rate;
3214
3215	AN_LOCK(sc);
3216	sc->an_tx_rate = ieee80211_media2rate(
3217		IFM_SUBTYPE(sc->an_ifmedia.ifm_cur->ifm_media));
3218	if (sc->an_tx_rate < 0)
3219		sc->an_tx_rate = 0;
3220
3221	if (orate != sc->an_tx_rate) {
3222		/* Read the current configuration */
3223		sc->an_config.an_type = AN_RID_GENCONFIG;
3224		sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3225		an_read_record(sc, (struct an_ltv_gen *)&sc->an_config);
3226		cfg = &sc->an_config;
3227
3228		/* clear other rates and set the only one we want */
3229		bzero(cfg->an_rates, sizeof(cfg->an_rates));
3230		cfg->an_rates[0] = sc->an_tx_rate;
3231
3232		/* Save the new rate */
3233		sc->an_config.an_type = AN_RID_GENCONFIG;
3234		sc->an_config.an_len = sizeof(struct an_ltv_genconfig);
3235	}
3236
3237	if ((sc->an_ifmedia.ifm_cur->ifm_media & IFM_IEEE80211_ADHOC) != 0)
3238		sc->an_config.an_opmode &= ~AN_OPMODE_INFRASTRUCTURE_STATION;
3239	else
3240		sc->an_config.an_opmode |= AN_OPMODE_INFRASTRUCTURE_STATION;
3241
3242	if (otype != sc->an_config.an_opmode ||
3243	    orate != sc->an_tx_rate)
3244		an_init_locked(sc);
3245	AN_UNLOCK(sc);
3246
3247	return(0);
3248}
3249
3250static void
3251an_media_status(struct ifnet *ifp, struct ifmediareq *imr)
3252{
3253	struct an_ltv_status	status;
3254	struct an_softc		*sc = ifp->if_softc;
3255
3256	imr->ifm_active = IFM_IEEE80211;
3257
3258	AN_LOCK(sc);
3259	status.an_len = sizeof(status);
3260	status.an_type = AN_RID_STATUS;
3261	if (an_read_record(sc, (struct an_ltv_gen *)&status)) {
3262		/* If the status read fails, just lie. */
3263		imr->ifm_active = sc->an_ifmedia.ifm_cur->ifm_media;
3264		imr->ifm_status = IFM_AVALID|IFM_ACTIVE;
3265	}
3266
3267	if (sc->an_tx_rate == 0) {
3268		imr->ifm_active = IFM_IEEE80211|IFM_AUTO;
3269	}
3270
3271	if (sc->an_config.an_opmode == AN_OPMODE_IBSS_ADHOC)
3272		imr->ifm_active |= IFM_IEEE80211_ADHOC;
3273	imr->ifm_active |= ieee80211_rate2media(NULL,
3274		status.an_current_tx_rate, IEEE80211_MODE_AUTO);
3275	imr->ifm_status = IFM_AVALID;
3276	if (status.an_opmode & AN_STATUS_OPMODE_ASSOCIATED)
3277		imr->ifm_status |= IFM_ACTIVE;
3278	AN_UNLOCK(sc);
3279}
3280
3281/********************** Cisco utility support routines *************/
3282
3283/*
3284 * ReadRids & WriteRids derived from Cisco driver additions to Ben Reed's
3285 * Linux driver
3286 */
3287
3288static int
3289readrids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3290{
3291	unsigned short  rid;
3292	struct an_softc *sc;
3293	int error;
3294
3295	switch (l_ioctl->command) {
3296	case AIROGCAP:
3297		rid = AN_RID_CAPABILITIES;
3298		break;
3299	case AIROGCFG:
3300		rid = AN_RID_GENCONFIG;
3301		break;
3302	case AIROGSLIST:
3303		rid = AN_RID_SSIDLIST;
3304		break;
3305	case AIROGVLIST:
3306		rid = AN_RID_APLIST;
3307		break;
3308	case AIROGDRVNAM:
3309		rid = AN_RID_DRVNAME;
3310		break;
3311	case AIROGEHTENC:
3312		rid = AN_RID_ENCAPPROTO;
3313		break;
3314	case AIROGWEPKTMP:
3315		rid = AN_RID_WEP_TEMP;
3316		break;
3317	case AIROGWEPKNV:
3318		rid = AN_RID_WEP_PERM;
3319		break;
3320	case AIROGSTAT:
3321		rid = AN_RID_STATUS;
3322		break;
3323	case AIROGSTATSD32:
3324		rid = AN_RID_32BITS_DELTA;
3325		break;
3326	case AIROGSTATSC32:
3327		rid = AN_RID_32BITS_CUM;
3328		break;
3329	default:
3330		rid = 999;
3331		break;
3332	}
3333
3334	if (rid == 999)	/* Is bad command */
3335		return -EINVAL;
3336
3337	sc = ifp->if_softc;
3338	sc->areq.an_len  = AN_MAX_DATALEN;
3339	sc->areq.an_type = rid;
3340
3341	an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3342
3343	l_ioctl->len = sc->areq.an_len - 4;	/* just data */
3344
3345	AN_UNLOCK(sc);
3346	/* the data contains the length at first */
3347	if (copyout(&(sc->areq.an_len), l_ioctl->data,
3348		    sizeof(sc->areq.an_len))) {
3349		error = -EFAULT;
3350		goto lock_exit;
3351	}
3352	/* Just copy the data back */
3353	if (copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3354		    l_ioctl->len)) {
3355		error = -EFAULT;
3356		goto lock_exit;
3357	}
3358	error = 0;
3359lock_exit:
3360	AN_LOCK(sc);
3361	return (error);
3362}
3363
3364static int
3365writerids(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3366{
3367	struct an_softc *sc;
3368	int		rid, command, error;
3369
3370	sc = ifp->if_softc;
3371	AN_LOCK_ASSERT(sc);
3372	rid = 0;
3373	command = l_ioctl->command;
3374
3375	switch (command) {
3376	case AIROPSIDS:
3377		rid = AN_RID_SSIDLIST;
3378		break;
3379	case AIROPCAP:
3380		rid = AN_RID_CAPABILITIES;
3381		break;
3382	case AIROPAPLIST:
3383		rid = AN_RID_APLIST;
3384		break;
3385	case AIROPCFG:
3386		rid = AN_RID_GENCONFIG;
3387		break;
3388	case AIROPMACON:
3389		an_cmd(sc, AN_CMD_ENABLE, 0);
3390		return 0;
3391		break;
3392	case AIROPMACOFF:
3393		an_cmd(sc, AN_CMD_DISABLE, 0);
3394		return 0;
3395		break;
3396	case AIROPSTCLR:
3397		/*
3398		 * This command merely clears the counts does not actually
3399		 * store any data only reads rid. But as it changes the cards
3400		 * state, I put it in the writerid routines.
3401		 */
3402
3403		rid = AN_RID_32BITS_DELTACLR;
3404		sc = ifp->if_softc;
3405		sc->areq.an_len = AN_MAX_DATALEN;
3406		sc->areq.an_type = rid;
3407
3408		an_read_record(sc, (struct an_ltv_gen *)&sc->areq);
3409		l_ioctl->len = sc->areq.an_len - 4;	/* just data */
3410
3411		AN_UNLOCK(sc);
3412		/* the data contains the length at first */
3413		error = copyout(&(sc->areq.an_len), l_ioctl->data,
3414			    sizeof(sc->areq.an_len));
3415		if (error) {
3416			AN_LOCK(sc);
3417			return -EFAULT;
3418		}
3419		/* Just copy the data */
3420		error = copyout(&(sc->areq.an_val), l_ioctl->data + 2,
3421			    l_ioctl->len);
3422		AN_LOCK(sc);
3423		if (error)
3424			return -EFAULT;
3425		return 0;
3426		break;
3427	case AIROPWEPKEY:
3428		rid = AN_RID_WEP_TEMP;
3429		break;
3430	case AIROPWEPKEYNV:
3431		rid = AN_RID_WEP_PERM;
3432		break;
3433	case AIROPLEAPUSR:
3434		rid = AN_RID_LEAPUSERNAME;
3435		break;
3436	case AIROPLEAPPWD:
3437		rid = AN_RID_LEAPPASSWORD;
3438		break;
3439	default:
3440		return -EOPNOTSUPP;
3441	}
3442
3443	if (rid) {
3444		if (l_ioctl->len > sizeof(sc->areq.an_val) + 4)
3445			return -EINVAL;
3446		sc->areq.an_len = l_ioctl->len + 4;	/* add type & length */
3447		sc->areq.an_type = rid;
3448
3449		/* Just copy the data back */
3450		AN_UNLOCK(sc);
3451		error = copyin((l_ioctl->data) + 2, &sc->areq.an_val,
3452		       l_ioctl->len);
3453		AN_LOCK(sc);
3454		if (error)
3455			return -EFAULT;
3456
3457		an_cmd(sc, AN_CMD_DISABLE, 0);
3458		an_write_record(sc, (struct an_ltv_gen *)&sc->areq);
3459		an_cmd(sc, AN_CMD_ENABLE, 0);
3460		return 0;
3461	}
3462	return -EOPNOTSUPP;
3463}
3464
3465/*
3466 * General Flash utilities derived from Cisco driver additions to Ben Reed's
3467 * Linux driver
3468 */
3469
3470#define FLASH_DELAY(_sc, x)	msleep(ifp, &(_sc)->an_mtx, PZERO, \
3471	"flash", ((x) / hz) + 1);
3472#define FLASH_COMMAND	0x7e7e
3473#define FLASH_SIZE	32 * 1024
3474
3475static int
3476unstickbusy(struct ifnet *ifp)
3477{
3478	struct an_softc *sc = ifp->if_softc;
3479
3480	if (CSR_READ_2(sc, AN_COMMAND(sc->mpi350)) & AN_CMD_BUSY) {
3481		CSR_WRITE_2(sc, AN_EVENT_ACK(sc->mpi350),
3482			    AN_EV_CLR_STUCK_BUSY);
3483		return 1;
3484	}
3485	return 0;
3486}
3487
3488/*
3489 * Wait for busy completion from card wait for delay uSec's Return true for
3490 * success meaning command reg is clear
3491 */
3492
3493static int
3494WaitBusy(struct ifnet *ifp, int uSec)
3495{
3496	int		statword = 0xffff;
3497	int		delay = 0;
3498	struct an_softc	*sc = ifp->if_softc;
3499
3500	while ((statword & AN_CMD_BUSY) && delay <= (1000 * 100)) {
3501		FLASH_DELAY(sc, 10);
3502		delay += 10;
3503		statword = CSR_READ_2(sc, AN_COMMAND(sc->mpi350));
3504
3505		if ((AN_CMD_BUSY & statword) && (delay % 200)) {
3506			unstickbusy(ifp);
3507		}
3508	}
3509
3510	return 0 == (AN_CMD_BUSY & statword);
3511}
3512
3513/*
3514 * STEP 1) Disable MAC and do soft reset on card.
3515 */
3516
3517static int
3518cmdreset(struct ifnet *ifp)
3519{
3520	int		status;
3521	struct an_softc	*sc = ifp->if_softc;
3522
3523	AN_LOCK(sc);
3524	an_stop(sc);
3525
3526	an_cmd(sc, AN_CMD_DISABLE, 0);
3527
3528	if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3529		if_printf(ifp, "Waitbusy hang b4 RESET =%d\n", status);
3530		AN_UNLOCK(sc);
3531		return -EBUSY;
3532	}
3533	CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), AN_CMD_FW_RESTART);
3534
3535	FLASH_DELAY(sc, 1000);	/* WAS 600 12/7/00 */
3536
3537
3538	if (!(status = WaitBusy(ifp, 100))) {
3539		if_printf(ifp, "Waitbusy hang AFTER RESET =%d\n", status);
3540		AN_UNLOCK(sc);
3541		return -EBUSY;
3542	}
3543	AN_UNLOCK(sc);
3544	return 0;
3545}
3546
3547/*
3548 * STEP 2) Put the card in legendary flash mode
3549 */
3550
3551static int
3552setflashmode(struct ifnet *ifp)
3553{
3554	int		status;
3555	struct an_softc	*sc = ifp->if_softc;
3556
3557	CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3558	CSR_WRITE_2(sc, AN_SW1(sc->mpi350), FLASH_COMMAND);
3559	CSR_WRITE_2(sc, AN_SW0(sc->mpi350), FLASH_COMMAND);
3560	CSR_WRITE_2(sc, AN_COMMAND(sc->mpi350), FLASH_COMMAND);
3561
3562	/*
3563	 * mdelay(500); // 500ms delay
3564	 */
3565
3566	FLASH_DELAY(sc, 500);
3567
3568	if (!(status = WaitBusy(ifp, AN_TIMEOUT))) {
3569		printf("Waitbusy hang after setflash mode\n");
3570		return -EIO;
3571	}
3572	return 0;
3573}
3574
3575/*
3576 * Get a character from the card matching matchbyte Step 3)
3577 */
3578
3579static int
3580flashgchar(struct ifnet *ifp, int matchbyte, int dwelltime)
3581{
3582	int		rchar;
3583	unsigned char	rbyte = 0;
3584	int		success = -1;
3585	struct an_softc	*sc = ifp->if_softc;
3586
3587
3588	do {
3589		rchar = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3590
3591		if (dwelltime && !(0x8000 & rchar)) {
3592			dwelltime -= 10;
3593			FLASH_DELAY(sc, 10);
3594			continue;
3595		}
3596		rbyte = 0xff & rchar;
3597
3598		if ((rbyte == matchbyte) && (0x8000 & rchar)) {
3599			CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3600			success = 1;
3601			break;
3602		}
3603		if (rbyte == 0x81 || rbyte == 0x82 || rbyte == 0x83 || rbyte == 0x1a || 0xffff == rchar)
3604			break;
3605		CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3606
3607	} while (dwelltime > 0);
3608	return success;
3609}
3610
3611/*
3612 * Put character to SWS0 wait for dwelltime x 50us for  echo .
3613 */
3614
3615static int
3616flashpchar(struct ifnet *ifp, int byte, int dwelltime)
3617{
3618	int		echo;
3619	int		pollbusy, waittime;
3620	struct an_softc	*sc = ifp->if_softc;
3621
3622	byte |= 0x8000;
3623
3624	if (dwelltime == 0)
3625		dwelltime = 200;
3626
3627	waittime = dwelltime;
3628
3629	/*
3630	 * Wait for busy bit d15 to go false indicating buffer empty
3631	 */
3632	do {
3633		pollbusy = CSR_READ_2(sc, AN_SW0(sc->mpi350));
3634
3635		if (pollbusy & 0x8000) {
3636			FLASH_DELAY(sc, 50);
3637			waittime -= 50;
3638			continue;
3639		} else
3640			break;
3641	}
3642	while (waittime >= 0);
3643
3644	/* timeout for busy clear wait */
3645
3646	if (waittime <= 0) {
3647		if_printf(ifp, "flash putchar busywait timeout!\n");
3648		return -1;
3649	}
3650	/*
3651	 * Port is clear now write byte and wait for it to echo back
3652	 */
3653	do {
3654		CSR_WRITE_2(sc, AN_SW0(sc->mpi350), byte);
3655		FLASH_DELAY(sc, 50);
3656		dwelltime -= 50;
3657		echo = CSR_READ_2(sc, AN_SW1(sc->mpi350));
3658	} while (dwelltime >= 0 && echo != byte);
3659
3660
3661	CSR_WRITE_2(sc, AN_SW1(sc->mpi350), 0);
3662
3663	return echo == byte;
3664}
3665
3666/*
3667 * Transfer 32k of firmware data from user buffer to our buffer and send to
3668 * the card
3669 */
3670
3671static int
3672flashputbuf(struct ifnet *ifp)
3673{
3674	unsigned short *bufp;
3675	int		nwords;
3676	struct an_softc	*sc = ifp->if_softc;
3677
3678	/* Write stuff */
3679
3680	bufp = sc->an_flash_buffer;
3681
3682	if (!sc->mpi350) {
3683		CSR_WRITE_2(sc, AN_AUX_PAGE, 0x100);
3684		CSR_WRITE_2(sc, AN_AUX_OFFSET, 0);
3685
3686		for (nwords = 0; nwords != FLASH_SIZE / 2; nwords++) {
3687			CSR_WRITE_2(sc, AN_AUX_DATA, bufp[nwords] & 0xffff);
3688		}
3689	} else {
3690		for (nwords = 0; nwords != FLASH_SIZE / 4; nwords++) {
3691			CSR_MEM_AUX_WRITE_4(sc, 0x8000,
3692				((u_int32_t *)bufp)[nwords] & 0xffff);
3693		}
3694	}
3695
3696	CSR_WRITE_2(sc, AN_SW0(sc->mpi350), 0x8000);
3697
3698	return 0;
3699}
3700
3701/*
3702 * After flashing restart the card.
3703 */
3704
3705static int
3706flashrestart(struct ifnet *ifp)
3707{
3708	int		status = 0;
3709	struct an_softc	*sc = ifp->if_softc;
3710
3711	FLASH_DELAY(sc, 1024);		/* Added 12/7/00 */
3712
3713	an_init_locked(sc);
3714
3715	FLASH_DELAY(sc, 1024);		/* Added 12/7/00 */
3716	return status;
3717}
3718
3719/*
3720 * Entry point for flash ioclt.
3721 */
3722
3723static int
3724flashcard(struct ifnet *ifp, struct aironet_ioctl *l_ioctl)
3725{
3726	int		z = 0, status;
3727	struct an_softc	*sc;
3728
3729	sc = ifp->if_softc;
3730	if (sc->mpi350) {
3731		if_printf(ifp, "flashing not supported on MPI 350 yet\n");
3732		return(-1);
3733	}
3734	status = l_ioctl->command;
3735
3736	switch (l_ioctl->command) {
3737	case AIROFLSHRST:
3738		return cmdreset(ifp);
3739		break;
3740	case AIROFLSHSTFL:
3741		if (sc->an_flash_buffer) {
3742			free(sc->an_flash_buffer, M_DEVBUF);
3743			sc->an_flash_buffer = NULL;
3744		}
3745		sc->an_flash_buffer = malloc(FLASH_SIZE, M_DEVBUF, M_WAITOK);
3746		if (sc->an_flash_buffer)
3747			return setflashmode(ifp);
3748		else
3749			return ENOBUFS;
3750		break;
3751	case AIROFLSHGCHR:	/* Get char from aux */
3752		if (l_ioctl->len > sizeof(sc->areq)) {
3753			return -EINVAL;
3754		}
3755		AN_UNLOCK(sc);
3756		status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3757		AN_LOCK(sc);
3758		if (status)
3759			return status;
3760		z = *(int *)&sc->areq;
3761		if ((status = flashgchar(ifp, z, 8000)) == 1)
3762			return 0;
3763		else
3764			return -1;
3765	case AIROFLSHPCHR:	/* Send char to card. */
3766		if (l_ioctl->len > sizeof(sc->areq)) {
3767			return -EINVAL;
3768		}
3769		AN_UNLOCK(sc);
3770		status = copyin(l_ioctl->data, &sc->areq, l_ioctl->len);
3771		AN_LOCK(sc);
3772		if (status)
3773			return status;
3774		z = *(int *)&sc->areq;
3775		if ((status = flashpchar(ifp, z, 8000)) == -1)
3776			return -EIO;
3777		else
3778			return 0;
3779		break;
3780	case AIROFLPUTBUF:	/* Send 32k to card */
3781		if (l_ioctl->len > FLASH_SIZE) {
3782			if_printf(ifp, "Buffer to big, %x %x\n",
3783			       l_ioctl->len, FLASH_SIZE);
3784			return -EINVAL;
3785		}
3786		AN_UNLOCK(sc);
3787		status = copyin(l_ioctl->data, sc->an_flash_buffer, l_ioctl->len);
3788		AN_LOCK(sc);
3789		if (status)
3790			return status;
3791
3792		if ((status = flashputbuf(ifp)) != 0)
3793			return -EIO;
3794		else
3795			return 0;
3796		break;
3797	case AIRORESTART:
3798		if ((status = flashrestart(ifp)) != 0) {
3799			if_printf(ifp, "FLASHRESTART returned %d\n", status);
3800			return -EIO;
3801		} else
3802			return 0;
3803
3804		break;
3805	default:
3806		return -EINVAL;
3807	}
3808
3809	return -EINVAL;
3810}
3811