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