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