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