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

Cache object: cfc7f419e6a581a0a361dfbd4f61325a


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