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