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