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