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