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