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