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