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

Cache object: 217f8af1705d91c81770c7c6d6145a2b


[ 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.