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