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