The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/an/if_an.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 1bd23ebf91181ab05f78b0e2e44e7781


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.