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