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/ipw/if_ipw.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 /*      $FreeBSD: releng/8.3/sys/dev/ipw/if_ipw.c 215342 2010-11-15 17:48:13Z sobomax $ */
    2 
    3 /*-
    4  * Copyright (c) 2004-2006
    5  *      Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
    6  * Copyright (c) 2006 Sam Leffler, Errno Consulting
    7  * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice unmodified, this list of conditions, and the following
   14  *    disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/8.3/sys/dev/ipw/if_ipw.c 215342 2010-11-15 17:48:13Z sobomax $");
   34 
   35 /*-
   36  * Intel(R) PRO/Wireless 2100 MiniPCI driver
   37  * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
   38  */
   39 
   40 #include <sys/param.h>
   41 #include <sys/sysctl.h>
   42 #include <sys/sockio.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/kernel.h>
   45 #include <sys/socket.h>
   46 #include <sys/systm.h>
   47 #include <sys/malloc.h>
   48 #include <sys/queue.h>
   49 #include <sys/taskqueue.h>
   50 #include <sys/module.h>
   51 #include <sys/bus.h>
   52 #include <sys/endian.h>
   53 #include <sys/linker.h>
   54 #include <sys/firmware.h>
   55 
   56 #include <machine/bus.h>
   57 #include <machine/resource.h>
   58 #include <sys/rman.h>
   59 
   60 #include <dev/pci/pcireg.h>
   61 #include <dev/pci/pcivar.h>
   62 
   63 #include <net/bpf.h>
   64 #include <net/if.h>
   65 #include <net/if_arp.h>
   66 #include <net/ethernet.h>
   67 #include <net/if_dl.h>
   68 #include <net/if_media.h>
   69 #include <net/if_types.h>
   70 
   71 #include <net80211/ieee80211_var.h>
   72 #include <net80211/ieee80211_radiotap.h>
   73 
   74 #include <netinet/in.h>
   75 #include <netinet/in_systm.h>
   76 #include <netinet/in_var.h>
   77 #include <netinet/ip.h>
   78 #include <netinet/if_ether.h>
   79 
   80 #include <dev/ipw/if_ipwreg.h>
   81 #include <dev/ipw/if_ipwvar.h>
   82 
   83 #define IPW_DEBUG
   84 #ifdef IPW_DEBUG
   85 #define DPRINTF(x)      do { if (ipw_debug > 0) printf x; } while (0)
   86 #define DPRINTFN(n, x)  do { if (ipw_debug >= (n)) printf x; } while (0)
   87 int ipw_debug = 0;
   88 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
   89 #else
   90 #define DPRINTF(x)
   91 #define DPRINTFN(n, x)
   92 #endif
   93 
   94 MODULE_DEPEND(ipw, pci,  1, 1, 1);
   95 MODULE_DEPEND(ipw, wlan, 1, 1, 1);
   96 MODULE_DEPEND(ipw, firmware, 1, 1, 1);
   97 
   98 struct ipw_ident {
   99         uint16_t        vendor;
  100         uint16_t        device;
  101         const char      *name;
  102 };
  103 
  104 static const struct ipw_ident ipw_ident_table[] = {
  105         { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
  106 
  107         { 0, 0, NULL }
  108 };
  109 
  110 static struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
  111                     const char name[IFNAMSIZ], int unit, int opmode, int flags,
  112                     const uint8_t bssid[IEEE80211_ADDR_LEN],
  113                     const uint8_t mac[IEEE80211_ADDR_LEN]);
  114 static void     ipw_vap_delete(struct ieee80211vap *);
  115 static int      ipw_dma_alloc(struct ipw_softc *);
  116 static void     ipw_release(struct ipw_softc *);
  117 static void     ipw_media_status(struct ifnet *, struct ifmediareq *);
  118 static int      ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
  119 static uint16_t ipw_read_prom_word(struct ipw_softc *, uint8_t);
  120 static void     ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
  121 static void     ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
  122 static void     ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
  123                     struct ipw_soft_bd *, struct ipw_soft_buf *);
  124 static void     ipw_rx_intr(struct ipw_softc *);
  125 static void     ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
  126 static void     ipw_tx_intr(struct ipw_softc *);
  127 static void     ipw_intr(void *);
  128 static void     ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
  129 static const char * ipw_cmdname(int);
  130 static int      ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
  131 static int      ipw_tx_start(struct ifnet *, struct mbuf *,
  132                     struct ieee80211_node *);
  133 static int      ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
  134                     const struct ieee80211_bpf_params *);
  135 static void     ipw_start(struct ifnet *);
  136 static void     ipw_start_locked(struct ifnet *);
  137 static void     ipw_watchdog(void *);
  138 static int      ipw_ioctl(struct ifnet *, u_long, caddr_t);
  139 static void     ipw_stop_master(struct ipw_softc *);
  140 static int      ipw_enable(struct ipw_softc *);
  141 static int      ipw_disable(struct ipw_softc *);
  142 static int      ipw_reset(struct ipw_softc *);
  143 static int      ipw_load_ucode(struct ipw_softc *, const char *, int);
  144 static int      ipw_load_firmware(struct ipw_softc *, const char *, int);
  145 static int      ipw_config(struct ipw_softc *);
  146 static void     ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
  147 static void     ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
  148 static void     ipw_init_task(void *, int);
  149 static void     ipw_init(void *);
  150 static void     ipw_init_locked(struct ipw_softc *);
  151 static void     ipw_stop(void *);
  152 static void     ipw_stop_locked(struct ipw_softc *);
  153 static int      ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
  154 static int      ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
  155 static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t);
  156 static void     ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
  157 #if 0
  158 static int      ipw_read_table2(struct ipw_softc *, uint32_t, void *,
  159                     uint32_t *);
  160 static void     ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
  161                     bus_size_t);
  162 #endif
  163 static void     ipw_write_mem_1(struct ipw_softc *, bus_size_t,
  164                     const uint8_t *, bus_size_t);
  165 static int      ipw_scan(struct ipw_softc *);
  166 static void     ipw_scan_start(struct ieee80211com *);
  167 static void     ipw_scan_end(struct ieee80211com *);
  168 static void     ipw_set_channel(struct ieee80211com *);
  169 static void     ipw_scan_curchan(struct ieee80211_scan_state *,
  170                     unsigned long maxdwell);
  171 static void     ipw_scan_mindwell(struct ieee80211_scan_state *);
  172 
  173 static int ipw_probe(device_t);
  174 static int ipw_attach(device_t);
  175 static int ipw_detach(device_t);
  176 static int ipw_shutdown(device_t);
  177 static int ipw_suspend(device_t);
  178 static int ipw_resume(device_t);
  179 
  180 static device_method_t ipw_methods[] = {
  181         /* Device interface */
  182         DEVMETHOD(device_probe,         ipw_probe),
  183         DEVMETHOD(device_attach,        ipw_attach),
  184         DEVMETHOD(device_detach,        ipw_detach),
  185         DEVMETHOD(device_shutdown,      ipw_shutdown),
  186         DEVMETHOD(device_suspend,       ipw_suspend),
  187         DEVMETHOD(device_resume,        ipw_resume),
  188 
  189         { 0, 0 }
  190 };
  191 
  192 static driver_t ipw_driver = {
  193         "ipw",
  194         ipw_methods,
  195         sizeof (struct ipw_softc)
  196 };
  197 
  198 static devclass_t ipw_devclass;
  199 
  200 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
  201 
  202 static int
  203 ipw_probe(device_t dev)
  204 {
  205         const struct ipw_ident *ident;
  206 
  207         for (ident = ipw_ident_table; ident->name != NULL; ident++) {
  208                 if (pci_get_vendor(dev) == ident->vendor &&
  209                     pci_get_device(dev) == ident->device) {
  210                         device_set_desc(dev, ident->name);
  211                         return 0;
  212                 }
  213         }
  214         return ENXIO;
  215 }
  216 
  217 /* Base Address Register */
  218 #define IPW_PCI_BAR0    0x10
  219 
  220 static int
  221 ipw_attach(device_t dev)
  222 {
  223         struct ipw_softc *sc = device_get_softc(dev);
  224         struct ifnet *ifp;
  225         struct ieee80211com *ic;
  226         struct ieee80211_channel *c;
  227         uint16_t val;
  228         int error, i;
  229         uint8_t macaddr[IEEE80211_ADDR_LEN];
  230 
  231         sc->sc_dev = dev;
  232 
  233         mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
  234             MTX_DEF | MTX_RECURSE);
  235 
  236         TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
  237         callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
  238 
  239         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
  240                 device_printf(dev, "chip is in D%d power mode "
  241                     "-- setting to D0\n", pci_get_powerstate(dev));
  242                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
  243         }
  244 
  245         pci_write_config(dev, 0x41, 0, 1);
  246 
  247         /* enable bus-mastering */
  248         pci_enable_busmaster(dev);
  249 
  250         sc->mem_rid = IPW_PCI_BAR0;
  251         sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
  252             RF_ACTIVE);
  253         if (sc->mem == NULL) {
  254                 device_printf(dev, "could not allocate memory resource\n");
  255                 goto fail;
  256         }
  257 
  258         sc->sc_st = rman_get_bustag(sc->mem);
  259         sc->sc_sh = rman_get_bushandle(sc->mem);
  260 
  261         sc->irq_rid = 0;
  262         sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
  263             RF_ACTIVE | RF_SHAREABLE);
  264         if (sc->irq == NULL) {
  265                 device_printf(dev, "could not allocate interrupt resource\n");
  266                 goto fail1;
  267         }
  268 
  269         if (ipw_reset(sc) != 0) {
  270                 device_printf(dev, "could not reset adapter\n");
  271                 goto fail2;
  272         }
  273 
  274         if (ipw_dma_alloc(sc) != 0) {
  275                 device_printf(dev, "could not allocate DMA resources\n");
  276                 goto fail2;
  277         }
  278 
  279         ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
  280         if (ifp == NULL) {
  281                 device_printf(dev, "can not if_alloc()\n");
  282                 goto fail3;
  283         }
  284         ic = ifp->if_l2com;
  285 
  286         ifp->if_softc = sc;
  287         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
  288         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  289         ifp->if_init = ipw_init;
  290         ifp->if_ioctl = ipw_ioctl;
  291         ifp->if_start = ipw_start;
  292         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
  293         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
  294         IFQ_SET_READY(&ifp->if_snd);
  295 
  296         ic->ic_ifp = ifp;
  297         ic->ic_opmode = IEEE80211_M_STA;
  298         ic->ic_phytype = IEEE80211_T_DS;
  299 
  300         /* set device capabilities */
  301         ic->ic_caps =
  302                   IEEE80211_C_STA               /* station mode supported */
  303                 | IEEE80211_C_IBSS              /* IBSS mode supported */
  304                 | IEEE80211_C_MONITOR           /* monitor mode supported */
  305                 | IEEE80211_C_PMGT              /* power save supported */
  306                 | IEEE80211_C_SHPREAMBLE        /* short preamble supported */
  307                 | IEEE80211_C_WPA               /* 802.11i supported */
  308                 ;
  309 
  310         /* read MAC address from EEPROM */
  311         val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
  312         macaddr[0] = val >> 8;
  313         macaddr[1] = val & 0xff;
  314         val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
  315         macaddr[2] = val >> 8;
  316         macaddr[3] = val & 0xff;
  317         val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
  318         macaddr[4] = val >> 8;
  319         macaddr[5] = val & 0xff;
  320 
  321         /* set supported .11b channels (read from EEPROM) */
  322         if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
  323                 val = 0x7ff; /* default to channels 1-11 */
  324         val <<= 1;
  325         for (i = 1; i < 16; i++) {
  326                 if (val & (1 << i)) {
  327                         c = &ic->ic_channels[ic->ic_nchans++];
  328                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
  329                         c->ic_flags = IEEE80211_CHAN_B;
  330                         c->ic_ieee = i;
  331                 }
  332         }
  333 
  334         /* check support for radio transmitter switch in EEPROM */
  335         if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
  336                 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
  337 
  338         ieee80211_ifattach(ic, macaddr);
  339         ic->ic_scan_start = ipw_scan_start;
  340         ic->ic_scan_end = ipw_scan_end;
  341         ic->ic_set_channel = ipw_set_channel;
  342         ic->ic_scan_curchan = ipw_scan_curchan;
  343         ic->ic_scan_mindwell = ipw_scan_mindwell;
  344         ic->ic_raw_xmit = ipw_raw_xmit;
  345 
  346         ic->ic_vap_create = ipw_vap_create;
  347         ic->ic_vap_delete = ipw_vap_delete;
  348 
  349         ieee80211_radiotap_attach(ic,
  350             &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
  351                 IPW_TX_RADIOTAP_PRESENT,
  352             &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
  353                 IPW_RX_RADIOTAP_PRESENT);
  354 
  355         /*
  356          * Add a few sysctl knobs.
  357          */
  358         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
  359             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
  360             CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
  361             "radio transmitter switch state (0=off, 1=on)");
  362 
  363         SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
  364             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
  365             CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
  366             "statistics");
  367 
  368         /*
  369          * Hook our interrupt after all initialization is complete.
  370          */
  371         error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
  372             NULL, ipw_intr, sc, &sc->sc_ih);
  373         if (error != 0) {
  374                 device_printf(dev, "could not set up interrupt\n");
  375                 goto fail4;
  376         }
  377 
  378         if (bootverbose)
  379                 ieee80211_announce(ic);
  380 
  381         return 0;
  382 fail4:
  383         if_free(ifp);
  384 fail3:
  385         ipw_release(sc);
  386 fail2:
  387         bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
  388 fail1:
  389         bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
  390 fail:
  391         mtx_destroy(&sc->sc_mtx);
  392         return ENXIO;
  393 }
  394 
  395 static int
  396 ipw_detach(device_t dev)
  397 {
  398         struct ipw_softc *sc = device_get_softc(dev);
  399         struct ifnet *ifp = sc->sc_ifp;
  400         struct ieee80211com *ic = ifp->if_l2com;
  401 
  402         ieee80211_draintask(ic, &sc->sc_init_task);
  403         ipw_stop(sc);
  404 
  405         ieee80211_ifdetach(ic);
  406 
  407         callout_drain(&sc->sc_wdtimer);
  408 
  409         ipw_release(sc);
  410 
  411         bus_teardown_intr(dev, sc->irq, sc->sc_ih);
  412         bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
  413 
  414         bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
  415 
  416         if_free(ifp);
  417 
  418         if (sc->sc_firmware != NULL) {
  419                 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
  420                 sc->sc_firmware = NULL;
  421         }
  422 
  423         mtx_destroy(&sc->sc_mtx);
  424 
  425         return 0;
  426 }
  427 
  428 static struct ieee80211vap *
  429 ipw_vap_create(struct ieee80211com *ic,
  430         const char name[IFNAMSIZ], int unit, int opmode, int flags,
  431         const uint8_t bssid[IEEE80211_ADDR_LEN],
  432         const uint8_t mac[IEEE80211_ADDR_LEN])
  433 {
  434         struct ifnet *ifp = ic->ic_ifp;
  435         struct ipw_softc *sc = ifp->if_softc;
  436         struct ipw_vap *ivp;
  437         struct ieee80211vap *vap;
  438         const struct firmware *fp;
  439         const struct ipw_firmware_hdr *hdr;
  440         const char *imagename;
  441 
  442         if (!TAILQ_EMPTY(&ic->ic_vaps))         /* only one at a time */
  443                 return NULL;
  444 
  445         switch (opmode) {
  446         case IEEE80211_M_STA:
  447                 imagename = "ipw_bss";
  448                 break;
  449         case IEEE80211_M_IBSS:
  450                 imagename = "ipw_ibss";
  451                 break;
  452         case IEEE80211_M_MONITOR:
  453                 imagename = "ipw_monitor";
  454                 break;
  455         default:
  456                 return NULL;
  457         }
  458 
  459         /*
  460          * Load firmware image using the firmware(9) subsystem.  Doing
  461          * this unlocked is ok since we're single-threaded by the
  462          * 802.11 layer.
  463          */
  464         if (sc->sc_firmware == NULL ||
  465             strcmp(sc->sc_firmware->name, imagename) != 0) {
  466                 if (sc->sc_firmware != NULL)
  467                         firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
  468                 sc->sc_firmware = firmware_get(imagename);
  469         }
  470         if (sc->sc_firmware == NULL) {
  471                 device_printf(sc->sc_dev,
  472                     "could not load firmware image '%s'\n", imagename);
  473                 return NULL;
  474         }
  475         fp = sc->sc_firmware;
  476         if (fp->datasize < sizeof *hdr) {
  477                 device_printf(sc->sc_dev,
  478                     "firmware image too short %zu\n", fp->datasize);
  479                 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
  480                 sc->sc_firmware = NULL;
  481                 return NULL;
  482         }
  483         hdr = (const struct ipw_firmware_hdr *)fp->data;
  484         if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
  485             le32toh(hdr->ucodesz)) {
  486                 device_printf(sc->sc_dev,
  487                     "firmware image too short %zu\n", fp->datasize);
  488                 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
  489                 sc->sc_firmware = NULL;
  490                 return NULL;
  491         }
  492 
  493         ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
  494             M_80211_VAP, M_NOWAIT | M_ZERO);
  495         if (ivp == NULL)
  496                 return NULL;
  497         vap = &ivp->vap;
  498 
  499         ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
  500         /* override with driver methods */
  501         ivp->newstate = vap->iv_newstate;
  502         vap->iv_newstate = ipw_newstate;
  503 
  504         /* complete setup */
  505         ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
  506         ic->ic_opmode = opmode;
  507         return vap;
  508 }
  509 
  510 static void
  511 ipw_vap_delete(struct ieee80211vap *vap)
  512 {
  513         struct ipw_vap *ivp = IPW_VAP(vap);
  514 
  515         ieee80211_vap_detach(vap);
  516         free(ivp, M_80211_VAP);
  517 }
  518 
  519 static int
  520 ipw_dma_alloc(struct ipw_softc *sc)
  521 {
  522         struct ipw_soft_bd *sbd;
  523         struct ipw_soft_hdr *shdr;
  524         struct ipw_soft_buf *sbuf;
  525         bus_addr_t physaddr;
  526         int error, i;
  527 
  528         /*
  529          * Allocate and map tx ring.
  530          */
  531         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
  532             BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
  533             NULL, &sc->tbd_dmat);
  534         if (error != 0) {
  535                 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
  536                 goto fail;
  537         }
  538 
  539         error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
  540             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
  541         if (error != 0) {
  542                 device_printf(sc->sc_dev,
  543                     "could not allocate tx ring DMA memory\n");
  544                 goto fail;
  545         }
  546 
  547         error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
  548             IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
  549         if (error != 0) {
  550                 device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
  551                 goto fail;
  552         }
  553 
  554         /*
  555          * Allocate and map rx ring.
  556          */
  557         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
  558             BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
  559             NULL, &sc->rbd_dmat);
  560         if (error != 0) {
  561                 device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
  562                 goto fail;
  563         }
  564 
  565         error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
  566             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
  567         if (error != 0) {
  568                 device_printf(sc->sc_dev,
  569                     "could not allocate rx ring DMA memory\n");
  570                 goto fail;
  571         }
  572 
  573         error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
  574             IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
  575         if (error != 0) {
  576                 device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
  577                 goto fail;
  578         }
  579 
  580         /*
  581          * Allocate and map status ring.
  582          */
  583         error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
  584             BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
  585             NULL, NULL, &sc->status_dmat);
  586         if (error != 0) {
  587                 device_printf(sc->sc_dev,
  588                     "could not create status ring DMA tag\n");
  589                 goto fail;
  590         }
  591 
  592         error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
  593             BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
  594         if (error != 0) {
  595                 device_printf(sc->sc_dev,
  596                     "could not allocate status ring DMA memory\n");
  597                 goto fail;
  598         }
  599 
  600         error = bus_dmamap_load(sc->status_dmat, sc->status_map,
  601             sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
  602             0);
  603         if (error != 0) {
  604                 device_printf(sc->sc_dev,
  605                     "could not map status ring DMA memory\n");
  606                 goto fail;
  607         }
  608 
  609         /*
  610          * Allocate command DMA map.
  611          */
  612         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
  613             BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
  614             sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
  615         if (error != 0) {
  616                 device_printf(sc->sc_dev, "could not create command DMA tag\n");
  617                 goto fail;
  618         }
  619 
  620         error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
  621         if (error != 0) {
  622                 device_printf(sc->sc_dev,
  623                     "could not create command DMA map\n");
  624                 goto fail;
  625         }
  626 
  627         /*
  628          * Allocate headers DMA maps.
  629          */
  630         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
  631             BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
  632             sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
  633         if (error != 0) {
  634                 device_printf(sc->sc_dev, "could not create header DMA tag\n");
  635                 goto fail;
  636         }
  637 
  638         SLIST_INIT(&sc->free_shdr);
  639         for (i = 0; i < IPW_NDATA; i++) {
  640                 shdr = &sc->shdr_list[i];
  641                 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
  642                 if (error != 0) {
  643                         device_printf(sc->sc_dev,
  644                             "could not create header DMA map\n");
  645                         goto fail;
  646                 }
  647                 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
  648         }
  649 
  650         /*
  651          * Allocate tx buffers DMA maps.
  652          */
  653         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
  654             BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
  655             NULL, NULL, &sc->txbuf_dmat);
  656         if (error != 0) {
  657                 device_printf(sc->sc_dev, "could not create tx DMA tag\n");
  658                 goto fail;
  659         }
  660 
  661         SLIST_INIT(&sc->free_sbuf);
  662         for (i = 0; i < IPW_NDATA; i++) {
  663                 sbuf = &sc->tx_sbuf_list[i];
  664                 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
  665                 if (error != 0) {
  666                         device_printf(sc->sc_dev,
  667                             "could not create tx DMA map\n");
  668                         goto fail;
  669                 }
  670                 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
  671         }
  672 
  673         /*
  674          * Initialize tx ring.
  675          */
  676         for (i = 0; i < IPW_NTBD; i++) {
  677                 sbd = &sc->stbd_list[i];
  678                 sbd->bd = &sc->tbd_list[i];
  679                 sbd->type = IPW_SBD_TYPE_NOASSOC;
  680         }
  681 
  682         /*
  683          * Pre-allocate rx buffers and DMA maps.
  684          */
  685         error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
  686             BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
  687             NULL, &sc->rxbuf_dmat);
  688         if (error != 0) {
  689                 device_printf(sc->sc_dev, "could not create rx DMA tag\n");
  690                 goto fail;
  691         }
  692 
  693         for (i = 0; i < IPW_NRBD; i++) {
  694                 sbd = &sc->srbd_list[i];
  695                 sbuf = &sc->rx_sbuf_list[i];
  696                 sbd->bd = &sc->rbd_list[i];
  697 
  698                 sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
  699                 if (sbuf->m == NULL) {
  700                         device_printf(sc->sc_dev,
  701                             "could not allocate rx mbuf\n");
  702                         error = ENOMEM;
  703                         goto fail;
  704                 }
  705 
  706                 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
  707                 if (error != 0) {
  708                         device_printf(sc->sc_dev,
  709                             "could not create rx DMA map\n");
  710                         goto fail;
  711                 }
  712 
  713                 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
  714                     mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
  715                     &physaddr, 0);
  716                 if (error != 0) {
  717                         device_printf(sc->sc_dev,
  718                             "could not map rx DMA memory\n");
  719                         goto fail;
  720                 }
  721 
  722                 sbd->type = IPW_SBD_TYPE_DATA;
  723                 sbd->priv = sbuf;
  724                 sbd->bd->physaddr = htole32(physaddr);
  725                 sbd->bd->len = htole32(MCLBYTES);
  726         }
  727 
  728         bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
  729 
  730         return 0;
  731 
  732 fail:   ipw_release(sc);
  733         return error;
  734 }
  735 
  736 static void
  737 ipw_release(struct ipw_softc *sc)
  738 {
  739         struct ipw_soft_buf *sbuf;
  740         int i;
  741 
  742         if (sc->tbd_dmat != NULL) {
  743                 if (sc->stbd_list != NULL) {
  744                         bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
  745                         bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
  746                             sc->tbd_map);
  747                 }
  748                 bus_dma_tag_destroy(sc->tbd_dmat);
  749         }
  750 
  751         if (sc->rbd_dmat != NULL) {
  752                 if (sc->rbd_list != NULL) {
  753                         bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
  754                         bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
  755                             sc->rbd_map);
  756                 }
  757                 bus_dma_tag_destroy(sc->rbd_dmat);
  758         }
  759 
  760         if (sc->status_dmat != NULL) {
  761                 if (sc->status_list != NULL) {
  762                         bus_dmamap_unload(sc->status_dmat, sc->status_map);
  763                         bus_dmamem_free(sc->status_dmat, sc->status_list,
  764                             sc->status_map);
  765                 }
  766                 bus_dma_tag_destroy(sc->status_dmat);
  767         }
  768 
  769         for (i = 0; i < IPW_NTBD; i++)
  770                 ipw_release_sbd(sc, &sc->stbd_list[i]);
  771 
  772         if (sc->cmd_dmat != NULL) {
  773                 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
  774                 bus_dma_tag_destroy(sc->cmd_dmat);
  775         }
  776 
  777         if (sc->hdr_dmat != NULL) {
  778                 for (i = 0; i < IPW_NDATA; i++)
  779                         bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
  780                 bus_dma_tag_destroy(sc->hdr_dmat);
  781         }
  782 
  783         if (sc->txbuf_dmat != NULL) {
  784                 for (i = 0; i < IPW_NDATA; i++) {
  785                         bus_dmamap_destroy(sc->txbuf_dmat,
  786                             sc->tx_sbuf_list[i].map);
  787                 }
  788                 bus_dma_tag_destroy(sc->txbuf_dmat);
  789         }
  790 
  791         if (sc->rxbuf_dmat != NULL) {
  792                 for (i = 0; i < IPW_NRBD; i++) {
  793                         sbuf = &sc->rx_sbuf_list[i];
  794                         if (sbuf->m != NULL) {
  795                                 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
  796                                     BUS_DMASYNC_POSTREAD);
  797                                 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
  798                                 m_freem(sbuf->m);
  799                         }
  800                         bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
  801                 }
  802                 bus_dma_tag_destroy(sc->rxbuf_dmat);
  803         }
  804 }
  805 
  806 static int
  807 ipw_shutdown(device_t dev)
  808 {
  809         struct ipw_softc *sc = device_get_softc(dev);
  810 
  811         ipw_stop(sc);
  812 
  813         return 0;
  814 }
  815 
  816 static int
  817 ipw_suspend(device_t dev)
  818 {
  819         struct ipw_softc *sc = device_get_softc(dev);
  820 
  821         ipw_stop(sc);
  822 
  823         return 0;
  824 }
  825 
  826 static int
  827 ipw_resume(device_t dev)
  828 {
  829         struct ipw_softc *sc = device_get_softc(dev);
  830         struct ifnet *ifp = sc->sc_ifp;
  831 
  832         pci_write_config(dev, 0x41, 0, 1);
  833 
  834         if (ifp->if_flags & IFF_UP)
  835                 ipw_init(sc);
  836 
  837         return 0;
  838 }
  839 
  840 static int
  841 ipw_cvtrate(int ipwrate)
  842 {
  843         switch (ipwrate) {
  844         case IPW_RATE_DS1:      return 2;
  845         case IPW_RATE_DS2:      return 4;
  846         case IPW_RATE_DS5:      return 11;
  847         case IPW_RATE_DS11:     return 22;
  848         }
  849         return 0;
  850 }
  851 
  852 /*
  853  * The firmware automatically adapts the transmit speed. We report its current
  854  * value here.
  855  */
  856 static void
  857 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
  858 {
  859         struct ieee80211vap *vap = ifp->if_softc;
  860         struct ieee80211com *ic = vap->iv_ic;
  861         struct ipw_softc *sc = ic->ic_ifp->if_softc;
  862 
  863         /* read current transmission rate from adapter */
  864         vap->iv_bss->ni_txrate = ipw_cvtrate(
  865             ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
  866         ieee80211_media_status(ifp, imr);
  867 }
  868 
  869 static int
  870 ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
  871 {
  872         struct ipw_vap *ivp = IPW_VAP(vap);
  873         struct ieee80211com *ic = vap->iv_ic;
  874         struct ifnet *ifp = ic->ic_ifp;
  875         struct ipw_softc *sc = ifp->if_softc;
  876         enum ieee80211_state ostate;
  877 
  878         DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
  879                 ieee80211_state_name[vap->iv_state],
  880                 ieee80211_state_name[nstate], sc->flags));
  881 
  882         ostate = vap->iv_state;
  883         IEEE80211_UNLOCK(ic);
  884 
  885         switch (nstate) {
  886         case IEEE80211_S_RUN:
  887                 if (ic->ic_opmode == IEEE80211_M_IBSS) {
  888                         /*
  889                          * XXX when joining an ibss network we are called
  890                          * with a SCAN -> RUN transition on scan complete.
  891                          * Use that to call ipw_assoc.  On completing the
  892                          * join we are then called again with an AUTH -> RUN
  893                          * transition and we want to do nothing.  This is
  894                          * all totally bogus and needs to be redone.
  895                          */
  896                         if (ostate == IEEE80211_S_SCAN)
  897                                 ipw_assoc(ic, vap);
  898                 }
  899                 break;
  900 
  901         case IEEE80211_S_INIT:
  902                 if (sc->flags & IPW_FLAG_ASSOCIATED)
  903                         ipw_disassoc(ic, vap);
  904                 break;
  905 
  906         case IEEE80211_S_AUTH:
  907                 /*
  908                  * Move to ASSOC state after the ipw_assoc() call.  Firmware
  909                  * takes care of authentication, after the call we'll receive
  910                  * only an assoc response which would otherwise be discared
  911                  * if we are still in AUTH state.
  912                  */
  913                 nstate = IEEE80211_S_ASSOC;
  914                 ipw_assoc(ic, vap);
  915                 break;
  916 
  917         case IEEE80211_S_ASSOC:
  918                 /*
  919                  * If we are not transitioning from AUTH then resend the
  920                  * association request.
  921                  */
  922                 if (ostate != IEEE80211_S_AUTH)
  923                         ipw_assoc(ic, vap);
  924                 break;
  925 
  926         default:
  927                 break;
  928         }
  929         IEEE80211_LOCK(ic);
  930         return ivp->newstate(vap, nstate, arg);
  931 }
  932 
  933 /*
  934  * Read 16 bits at address 'addr' from the serial EEPROM.
  935  */
  936 static uint16_t
  937 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
  938 {
  939         uint32_t tmp;
  940         uint16_t val;
  941         int n;
  942 
  943         /* clock C once before the first command */
  944         IPW_EEPROM_CTL(sc, 0);
  945         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
  946         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
  947         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
  948 
  949         /* write start bit (1) */
  950         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
  951         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
  952 
  953         /* write READ opcode (10) */
  954         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
  955         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
  956         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
  957         IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
  958 
  959         /* write address A7-A0 */
  960         for (n = 7; n >= 0; n--) {
  961                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
  962                     (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
  963                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
  964                     (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
  965         }
  966 
  967         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
  968 
  969         /* read data Q15-Q0 */
  970         val = 0;
  971         for (n = 15; n >= 0; n--) {
  972                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
  973                 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
  974                 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
  975                 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
  976         }
  977 
  978         IPW_EEPROM_CTL(sc, 0);
  979 
  980         /* clear Chip Select and clock C */
  981         IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
  982         IPW_EEPROM_CTL(sc, 0);
  983         IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
  984 
  985         return le16toh(val);
  986 }
  987 
  988 static void
  989 ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
  990 {
  991         struct ipw_cmd *cmd;
  992 
  993         bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
  994 
  995         cmd = mtod(sbuf->m, struct ipw_cmd *);
  996 
  997         DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
  998             ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
  999             le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
 1000             le32toh(cmd->status)));
 1001 
 1002         sc->flags &= ~IPW_FLAG_BUSY;
 1003         wakeup(sc);
 1004 }
 1005 
 1006 static void
 1007 ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
 1008 {
 1009 #define IEEESTATE(vap)  ieee80211_state_name[vap->iv_state]
 1010         struct ifnet *ifp = sc->sc_ifp;
 1011         struct ieee80211com *ic = ifp->if_l2com;
 1012         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 1013         uint32_t state;
 1014 
 1015         bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
 1016 
 1017         state = le32toh(*mtod(sbuf->m, uint32_t *));
 1018 
 1019         switch (state) {
 1020         case IPW_STATE_ASSOCIATED:
 1021                 DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
 1022                         IEEESTATE(vap), sc->flags));
 1023                 /* XXX suppress state change in case the fw auto-associates */
 1024                 if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
 1025                         DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
 1026                                 IEEESTATE(vap), sc->flags));
 1027                         break;
 1028                 }
 1029                 sc->flags &= ~IPW_FLAG_ASSOCIATING;
 1030                 sc->flags |= IPW_FLAG_ASSOCIATED;
 1031                 break;
 1032 
 1033         case IPW_STATE_SCANNING:
 1034                 DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
 1035                         IEEESTATE(vap), sc->flags));
 1036                 /*
 1037                  * NB: Check driver state for association on assoc
 1038                  * loss as the firmware will immediately start to
 1039                  * scan and we would treat it as a beacon miss if
 1040                  * we checked the 802.11 layer state.
 1041                  */
 1042                 if (sc->flags & IPW_FLAG_ASSOCIATED) {
 1043                         IPW_UNLOCK(sc);
 1044                         /* XXX probably need to issue disassoc to fw */
 1045                         ieee80211_beacon_miss(ic);
 1046                         IPW_LOCK(sc);
 1047                 }
 1048                 break;
 1049 
 1050         case IPW_STATE_SCAN_COMPLETE:
 1051                 /*
 1052                  * XXX For some reason scan requests generate scan
 1053                  * started + scan done events before any traffic is
 1054                  * received (e.g. probe response frames).  We work
 1055                  * around this by marking the HACK flag and skipping
 1056                  * the first scan complete event.
 1057                 */
 1058                 DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
 1059                             IEEESTATE(vap), sc->flags));
 1060                 if (sc->flags & IPW_FLAG_HACK) {
 1061                         sc->flags &= ~IPW_FLAG_HACK;
 1062                         break;
 1063                 }
 1064                 if (sc->flags & IPW_FLAG_SCANNING) {
 1065                         IPW_UNLOCK(sc);
 1066                         ieee80211_scan_done(vap);
 1067                         IPW_LOCK(sc);
 1068                         sc->flags &= ~IPW_FLAG_SCANNING;
 1069                         sc->sc_scan_timer = 0;
 1070                 }
 1071                 break;
 1072 
 1073         case IPW_STATE_ASSOCIATION_LOST:
 1074                 DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
 1075                         IEEESTATE(vap), sc->flags));
 1076                 sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
 1077                 if (vap->iv_state == IEEE80211_S_RUN) {
 1078                         IPW_UNLOCK(sc);
 1079                         ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
 1080                         IPW_LOCK(sc);
 1081                 }
 1082                 break;
 1083 
 1084         case IPW_STATE_DISABLED:
 1085                 /* XXX? is this right? */
 1086                 sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
 1087                     IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
 1088                 DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
 1089                         IEEESTATE(vap), sc->flags));
 1090                 break;
 1091 
 1092         case IPW_STATE_RADIO_DISABLED:
 1093                 device_printf(sc->sc_dev, "radio turned off\n");
 1094                 ieee80211_notify_radio(ic, 0);
 1095                 ipw_stop_locked(sc);
 1096                 /* XXX start polling thread to detect radio on */
 1097                 break;
 1098 
 1099         default:
 1100                 DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
 1101                         __func__, state, IEEESTATE(vap), sc->flags));
 1102                 break;
 1103         }
 1104 #undef IEEESTATE
 1105 }
 1106 
 1107 /*
 1108  * Set driver state for current channel.
 1109  */
 1110 static void
 1111 ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
 1112 {
 1113         struct ifnet *ifp = sc->sc_ifp;
 1114         struct ieee80211com *ic = ifp->if_l2com;
 1115 
 1116         ic->ic_curchan = chan;
 1117         ieee80211_radiotap_chan_change(ic);
 1118 }
 1119 
 1120 /*
 1121  * XXX: Hack to set the current channel to the value advertised in beacons or
 1122  * probe responses. Only used during AP detection.
 1123  */
 1124 static void
 1125 ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
 1126 {
 1127         struct ifnet *ifp = sc->sc_ifp;
 1128         struct ieee80211com *ic = ifp->if_l2com;
 1129         struct ieee80211_channel *c;
 1130         struct ieee80211_frame *wh;
 1131         uint8_t subtype;
 1132         uint8_t *frm, *efrm;
 1133 
 1134         wh = mtod(m, struct ieee80211_frame *);
 1135 
 1136         if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
 1137                 return;
 1138 
 1139         subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
 1140 
 1141         if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
 1142             subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
 1143                 return;
 1144 
 1145         /* XXX use ieee80211_parse_beacon */
 1146         frm = (uint8_t *)(wh + 1);
 1147         efrm = mtod(m, uint8_t *) + m->m_len;
 1148 
 1149         frm += 12;      /* skip tstamp, bintval and capinfo fields */
 1150         while (frm < efrm) {
 1151                 if (*frm == IEEE80211_ELEMID_DSPARMS)
 1152 #if IEEE80211_CHAN_MAX < 255
 1153                 if (frm[2] <= IEEE80211_CHAN_MAX)
 1154 #endif
 1155                 {
 1156                         DPRINTF(("Fixing channel to %d\n", frm[2]));
 1157                         c = ieee80211_find_channel(ic,
 1158                                 ieee80211_ieee2mhz(frm[2], 0),
 1159                                 IEEE80211_CHAN_B);
 1160                         if (c == NULL)
 1161                                 c = &ic->ic_channels[0];
 1162                         ipw_setcurchan(sc, c);
 1163                 }
 1164 
 1165                 frm += frm[1] + 2;
 1166         }
 1167 }
 1168 
 1169 static void
 1170 ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
 1171     struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
 1172 {
 1173         struct ifnet *ifp = sc->sc_ifp;
 1174         struct ieee80211com *ic = ifp->if_l2com;
 1175         struct mbuf *mnew, *m;
 1176         struct ieee80211_node *ni;
 1177         bus_addr_t physaddr;
 1178         int error;
 1179         int8_t rssi, nf;
 1180 
 1181         DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
 1182             status->rssi));
 1183 
 1184         if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
 1185             le32toh(status->len) > MCLBYTES)
 1186                 return;
 1187 
 1188         /*
 1189          * Try to allocate a new mbuf for this ring element and load it before
 1190          * processing the current mbuf. If the ring element cannot be loaded,
 1191          * drop the received packet and reuse the old mbuf. In the unlikely
 1192          * case that the old mbuf can't be reloaded either, explicitly panic.
 1193          */
 1194         mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
 1195         if (mnew == NULL) {
 1196                 ifp->if_ierrors++;
 1197                 return;
 1198         }
 1199 
 1200         bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
 1201         bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
 1202 
 1203         error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
 1204             MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
 1205         if (error != 0) {
 1206                 m_freem(mnew);
 1207 
 1208                 /* try to reload the old mbuf */
 1209                 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
 1210                     mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
 1211                     &physaddr, 0);
 1212                 if (error != 0) {
 1213                         /* very unlikely that it will fail... */
 1214                         panic("%s: could not load old rx mbuf",
 1215                             device_get_name(sc->sc_dev));
 1216                 }
 1217                 ifp->if_ierrors++;
 1218                 return;
 1219         }
 1220 
 1221         /*
 1222          * New mbuf successfully loaded, update Rx ring and continue
 1223          * processing.
 1224          */
 1225         m = sbuf->m;
 1226         sbuf->m = mnew;
 1227         sbd->bd->physaddr = htole32(physaddr);
 1228 
 1229         /* finalize mbuf */
 1230         m->m_pkthdr.rcvif = ifp;
 1231         m->m_pkthdr.len = m->m_len = le32toh(status->len);
 1232 
 1233         rssi = status->rssi + IPW_RSSI_TO_DBM;
 1234         nf = -95;
 1235         if (ieee80211_radiotap_active(ic)) {
 1236                 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
 1237 
 1238                 tap->wr_flags = 0;
 1239                 tap->wr_antsignal = rssi;
 1240                 tap->wr_antnoise = nf;
 1241         }
 1242 
 1243         if (sc->flags & IPW_FLAG_SCANNING)
 1244                 ipw_fix_channel(sc, m);
 1245 
 1246         IPW_UNLOCK(sc);
 1247         ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
 1248         if (ni != NULL) {
 1249                 (void) ieee80211_input(ni, m, rssi - nf, nf);
 1250                 ieee80211_free_node(ni);
 1251         } else
 1252                 (void) ieee80211_input_all(ic, m, rssi - nf, nf);
 1253         IPW_LOCK(sc);
 1254 
 1255         bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
 1256 }
 1257 
 1258 static void
 1259 ipw_rx_intr(struct ipw_softc *sc)
 1260 {
 1261         struct ipw_status *status;
 1262         struct ipw_soft_bd *sbd;
 1263         struct ipw_soft_buf *sbuf;
 1264         uint32_t r, i;
 1265 
 1266         if (!(sc->flags & IPW_FLAG_FW_INITED))
 1267                 return;
 1268 
 1269         r = CSR_READ_4(sc, IPW_CSR_RX_READ);
 1270 
 1271         bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
 1272 
 1273         for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
 1274                 status = &sc->status_list[i];
 1275                 sbd = &sc->srbd_list[i];
 1276                 sbuf = sbd->priv;
 1277 
 1278                 switch (le16toh(status->code) & 0xf) {
 1279                 case IPW_STATUS_CODE_COMMAND:
 1280                         ipw_rx_cmd_intr(sc, sbuf);
 1281                         break;
 1282 
 1283                 case IPW_STATUS_CODE_NEWSTATE:
 1284                         ipw_rx_newstate_intr(sc, sbuf);
 1285                         break;
 1286 
 1287                 case IPW_STATUS_CODE_DATA_802_3:
 1288                 case IPW_STATUS_CODE_DATA_802_11:
 1289                         ipw_rx_data_intr(sc, status, sbd, sbuf);
 1290                         break;
 1291 
 1292                 case IPW_STATUS_CODE_NOTIFICATION:
 1293                         DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
 1294                             le32toh(status->len), status->flags));
 1295                         /* XXX maybe drive state machine AUTH->ASSOC? */
 1296                         break;
 1297 
 1298                 default:
 1299                         device_printf(sc->sc_dev, "unexpected status code %u\n",
 1300                             le16toh(status->code));
 1301                 }
 1302 
 1303                 /* firmware was killed, stop processing received frames */
 1304                 if (!(sc->flags & IPW_FLAG_FW_INITED))
 1305                         return;
 1306 
 1307                 sbd->bd->flags = 0;
 1308         }
 1309 
 1310         bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
 1311 
 1312         /* kick the firmware */
 1313         sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
 1314         CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
 1315 }
 1316 
 1317 static void
 1318 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
 1319 {
 1320         struct ipw_soft_hdr *shdr;
 1321         struct ipw_soft_buf *sbuf;
 1322 
 1323         switch (sbd->type) {
 1324         case IPW_SBD_TYPE_COMMAND:
 1325                 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
 1326                     BUS_DMASYNC_POSTWRITE);
 1327                 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
 1328                 break;
 1329 
 1330         case IPW_SBD_TYPE_HEADER:
 1331                 shdr = sbd->priv;
 1332                 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
 1333                 bus_dmamap_unload(sc->hdr_dmat, shdr->map);
 1334                 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
 1335                 break;
 1336 
 1337         case IPW_SBD_TYPE_DATA:
 1338                 sbuf = sbd->priv;
 1339                 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
 1340                     BUS_DMASYNC_POSTWRITE);
 1341                 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
 1342                 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
 1343 
 1344                 if (sbuf->m->m_flags & M_TXCB)
 1345                         ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
 1346                 m_freem(sbuf->m);
 1347                 ieee80211_free_node(sbuf->ni);
 1348 
 1349                 sc->sc_tx_timer = 0;
 1350                 break;
 1351         }
 1352 
 1353         sbd->type = IPW_SBD_TYPE_NOASSOC;
 1354 }
 1355 
 1356 static void
 1357 ipw_tx_intr(struct ipw_softc *sc)
 1358 {
 1359         struct ifnet *ifp = sc->sc_ifp;
 1360         struct ipw_soft_bd *sbd;
 1361         uint32_t r, i;
 1362 
 1363         if (!(sc->flags & IPW_FLAG_FW_INITED))
 1364                 return;
 1365 
 1366         r = CSR_READ_4(sc, IPW_CSR_TX_READ);
 1367 
 1368         for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
 1369                 sbd = &sc->stbd_list[i];
 1370 
 1371                 if (sbd->type == IPW_SBD_TYPE_DATA)
 1372                         ifp->if_opackets++;
 1373 
 1374                 ipw_release_sbd(sc, sbd);
 1375                 sc->txfree++;
 1376         }
 1377 
 1378         /* remember what the firmware has processed */
 1379         sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
 1380 
 1381         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 1382         ipw_start_locked(ifp);
 1383 }
 1384 
 1385 static void
 1386 ipw_fatal_error_intr(struct ipw_softc *sc)
 1387 {
 1388         struct ifnet *ifp = sc->sc_ifp;
 1389         struct ieee80211com *ic = ifp->if_l2com;
 1390         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 1391 
 1392         device_printf(sc->sc_dev, "firmware error\n");
 1393         if (vap != NULL) {
 1394                 IPW_UNLOCK(sc);
 1395                 ieee80211_cancel_scan(vap);
 1396                 IPW_LOCK(sc);
 1397         }
 1398         ieee80211_runtask(ic, &sc->sc_init_task);
 1399 }
 1400 
 1401 static void
 1402 ipw_intr(void *arg)
 1403 {
 1404         struct ipw_softc *sc = arg;
 1405         uint32_t r;
 1406 
 1407         IPW_LOCK(sc);
 1408 
 1409         r = CSR_READ_4(sc, IPW_CSR_INTR);
 1410         if (r == 0 || r == 0xffffffff)
 1411                 goto done;
 1412 
 1413         /* disable interrupts */
 1414         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
 1415 
 1416         /* acknowledge all interrupts */
 1417         CSR_WRITE_4(sc, IPW_CSR_INTR, r);
 1418 
 1419         if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
 1420                 ipw_fatal_error_intr(sc);
 1421                 goto done;
 1422         }
 1423 
 1424         if (r & IPW_INTR_FW_INIT_DONE)
 1425                 wakeup(sc);
 1426 
 1427         if (r & IPW_INTR_RX_TRANSFER)
 1428                 ipw_rx_intr(sc);
 1429 
 1430         if (r & IPW_INTR_TX_TRANSFER)
 1431                 ipw_tx_intr(sc);
 1432 
 1433         /* re-enable interrupts */
 1434         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
 1435 done:
 1436         IPW_UNLOCK(sc);
 1437 }
 1438 
 1439 static void
 1440 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
 1441 {
 1442         if (error != 0)
 1443                 return;
 1444 
 1445         KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
 1446 
 1447         *(bus_addr_t *)arg = segs[0].ds_addr;
 1448 }
 1449 
 1450 static const char *
 1451 ipw_cmdname(int cmd)
 1452 {
 1453 #define N(a)    (sizeof(a) / sizeof(a[0]))
 1454         static const struct {
 1455                 int     cmd;
 1456                 const char *name;
 1457         } cmds[] = {
 1458                 { IPW_CMD_ADD_MULTICAST,        "ADD_MULTICAST" },
 1459                 { IPW_CMD_BROADCAST_SCAN,       "BROADCAST_SCAN" },
 1460                 { IPW_CMD_DISABLE,              "DISABLE" },
 1461                 { IPW_CMD_DISABLE_PHY,          "DISABLE_PHY" },
 1462                 { IPW_CMD_ENABLE,               "ENABLE" },
 1463                 { IPW_CMD_PREPARE_POWER_DOWN,   "PREPARE_POWER_DOWN" },
 1464                 { IPW_CMD_SET_BASIC_TX_RATES,   "SET_BASIC_TX_RATES" },
 1465                 { IPW_CMD_SET_BEACON_INTERVAL,  "SET_BEACON_INTERVAL" },
 1466                 { IPW_CMD_SET_CHANNEL,          "SET_CHANNEL" },
 1467                 { IPW_CMD_SET_CONFIGURATION,    "SET_CONFIGURATION" },
 1468                 { IPW_CMD_SET_DESIRED_BSSID,    "SET_DESIRED_BSSID" },
 1469                 { IPW_CMD_SET_ESSID,            "SET_ESSID" },
 1470                 { IPW_CMD_SET_FRAG_THRESHOLD,   "SET_FRAG_THRESHOLD" },
 1471                 { IPW_CMD_SET_MAC_ADDRESS,      "SET_MAC_ADDRESS" },
 1472                 { IPW_CMD_SET_MANDATORY_BSSID,  "SET_MANDATORY_BSSID" },
 1473                 { IPW_CMD_SET_MODE,             "SET_MODE" },
 1474                 { IPW_CMD_SET_MSDU_TX_RATES,    "SET_MSDU_TX_RATES" },
 1475                 { IPW_CMD_SET_POWER_MODE,       "SET_POWER_MODE" },
 1476                 { IPW_CMD_SET_RTS_THRESHOLD,    "SET_RTS_THRESHOLD" },
 1477                 { IPW_CMD_SET_SCAN_OPTIONS,     "SET_SCAN_OPTIONS" },
 1478                 { IPW_CMD_SET_SECURITY_INFO,    "SET_SECURITY_INFO" },
 1479                 { IPW_CMD_SET_TX_POWER_INDEX,   "SET_TX_POWER_INDEX" },
 1480                 { IPW_CMD_SET_TX_RATES,         "SET_TX_RATES" },
 1481                 { IPW_CMD_SET_WEP_FLAGS,        "SET_WEP_FLAGS" },
 1482                 { IPW_CMD_SET_WEP_KEY,          "SET_WEP_KEY" },
 1483                 { IPW_CMD_SET_WEP_KEY_INDEX,    "SET_WEP_KEY_INDEX" },
 1484                 { IPW_CMD_SET_WPA_IE,           "SET_WPA_IE" },
 1485 
 1486         };
 1487         static char buf[12];
 1488         int i;
 1489 
 1490         for (i = 0; i < N(cmds); i++)
 1491                 if (cmds[i].cmd == cmd)
 1492                         return cmds[i].name;
 1493         snprintf(buf, sizeof(buf), "%u", cmd);
 1494         return buf;
 1495 #undef N
 1496 }
 1497 
 1498 /*
 1499  * Send a command to the firmware and wait for the acknowledgement.
 1500  */
 1501 static int
 1502 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
 1503 {
 1504         struct ipw_soft_bd *sbd;
 1505         bus_addr_t physaddr;
 1506         int error;
 1507 
 1508         IPW_LOCK_ASSERT(sc);
 1509 
 1510         if (sc->flags & IPW_FLAG_BUSY) {
 1511                 device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
 1512                         __func__, ipw_cmdname(type));
 1513                 return EAGAIN;
 1514         }
 1515         sc->flags |= IPW_FLAG_BUSY;
 1516 
 1517         sbd = &sc->stbd_list[sc->txcur];
 1518 
 1519         error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
 1520             sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
 1521         if (error != 0) {
 1522                 device_printf(sc->sc_dev, "could not map command DMA memory\n");
 1523                 sc->flags &= ~IPW_FLAG_BUSY;
 1524                 return error;
 1525         }
 1526 
 1527         sc->cmd.type = htole32(type);
 1528         sc->cmd.subtype = 0;
 1529         sc->cmd.len = htole32(len);
 1530         sc->cmd.seq = 0;
 1531         memcpy(sc->cmd.data, data, len);
 1532 
 1533         sbd->type = IPW_SBD_TYPE_COMMAND;
 1534         sbd->bd->physaddr = htole32(physaddr);
 1535         sbd->bd->len = htole32(sizeof (struct ipw_cmd));
 1536         sbd->bd->nfrag = 1;
 1537         sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
 1538             IPW_BD_FLAG_TX_LAST_FRAGMENT;
 1539 
 1540         bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
 1541         bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
 1542 
 1543 #ifdef IPW_DEBUG
 1544         if (ipw_debug >= 4) {
 1545                 printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
 1546                     0, 0, len);
 1547                 /* Print the data buffer in the higher debug level */
 1548                 if (ipw_debug >= 9 && len > 0) {
 1549                         printf(" data: 0x");
 1550                         for (int i = 1; i <= len; i++)
 1551                                 printf("%1D", (u_char *)data + len - i, "");
 1552                 }
 1553                 printf("\n");
 1554         }
 1555 #endif
 1556 
 1557         /* kick firmware */
 1558         sc->txfree--;
 1559         sc->txcur = (sc->txcur + 1) % IPW_NTBD;
 1560         CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
 1561 
 1562         /* wait at most one second for command to complete */
 1563         error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
 1564         if (error != 0) {
 1565                 device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
 1566                     __func__, ipw_cmdname(type), error);
 1567                 sc->flags &= ~IPW_FLAG_BUSY;
 1568                 return (error);
 1569         }
 1570         return (0);
 1571 }
 1572 
 1573 static int
 1574 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
 1575 {
 1576         struct ipw_softc *sc = ifp->if_softc;
 1577         struct ieee80211com *ic = ifp->if_l2com;
 1578         struct ieee80211vap *vap = ni->ni_vap;
 1579         struct ieee80211_frame *wh;
 1580         struct ipw_soft_bd *sbd;
 1581         struct ipw_soft_hdr *shdr;
 1582         struct ipw_soft_buf *sbuf;
 1583         struct ieee80211_key *k;
 1584         struct mbuf *mnew;
 1585         bus_dma_segment_t segs[IPW_MAX_NSEG];
 1586         bus_addr_t physaddr;
 1587         int nsegs, error, i;
 1588 
 1589         wh = mtod(m0, struct ieee80211_frame *);
 1590 
 1591         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
 1592                 k = ieee80211_crypto_encap(ni, m0);
 1593                 if (k == NULL) {
 1594                         m_freem(m0);
 1595                         return ENOBUFS;
 1596                 }
 1597                 /* packet header may have moved, reset our local pointer */
 1598                 wh = mtod(m0, struct ieee80211_frame *);
 1599         }
 1600 
 1601         if (ieee80211_radiotap_active_vap(vap)) {
 1602                 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
 1603 
 1604                 tap->wt_flags = 0;
 1605 
 1606                 ieee80211_radiotap_tx(vap, m0);
 1607         }
 1608 
 1609         shdr = SLIST_FIRST(&sc->free_shdr);
 1610         sbuf = SLIST_FIRST(&sc->free_sbuf);
 1611         KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
 1612 
 1613         shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
 1614         shdr->hdr.subtype = 0;
 1615         shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
 1616         shdr->hdr.encrypt = 0;
 1617         shdr->hdr.keyidx = 0;
 1618         shdr->hdr.keysz = 0;
 1619         shdr->hdr.fragmentsz = 0;
 1620         IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
 1621         if (ic->ic_opmode == IEEE80211_M_STA)
 1622                 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
 1623         else
 1624                 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
 1625 
 1626         /* trim IEEE802.11 header */
 1627         m_adj(m0, sizeof (struct ieee80211_frame));
 1628 
 1629         error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
 1630             &nsegs, 0);
 1631         if (error != 0 && error != EFBIG) {
 1632                 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
 1633                     error);
 1634                 m_freem(m0);
 1635                 return error;
 1636         }
 1637         if (error != 0) {
 1638                 mnew = m_defrag(m0, M_DONTWAIT);
 1639                 if (mnew == NULL) {
 1640                         device_printf(sc->sc_dev,
 1641                             "could not defragment mbuf\n");
 1642                         m_freem(m0);
 1643                         return ENOBUFS;
 1644                 }
 1645                 m0 = mnew;
 1646 
 1647                 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
 1648                     segs, &nsegs, 0);
 1649                 if (error != 0) {
 1650                         device_printf(sc->sc_dev,
 1651                             "could not map mbuf (error %d)\n", error);
 1652                         m_freem(m0);
 1653                         return error;
 1654                 }
 1655         }
 1656 
 1657         error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
 1658             sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
 1659         if (error != 0) {
 1660                 device_printf(sc->sc_dev, "could not map header DMA memory\n");
 1661                 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
 1662                 m_freem(m0);
 1663                 return error;
 1664         }
 1665 
 1666         SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
 1667         SLIST_REMOVE_HEAD(&sc->free_shdr, next);
 1668 
 1669         sbd = &sc->stbd_list[sc->txcur];
 1670         sbd->type = IPW_SBD_TYPE_HEADER;
 1671         sbd->priv = shdr;
 1672         sbd->bd->physaddr = htole32(physaddr);
 1673         sbd->bd->len = htole32(sizeof (struct ipw_hdr));
 1674         sbd->bd->nfrag = 1 + nsegs;
 1675         sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
 1676             IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
 1677 
 1678         DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
 1679             shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
 1680             shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
 1681             ":"));
 1682 
 1683         sc->txfree--;
 1684         sc->txcur = (sc->txcur + 1) % IPW_NTBD;
 1685 
 1686         sbuf->m = m0;
 1687         sbuf->ni = ni;
 1688 
 1689         for (i = 0; i < nsegs; i++) {
 1690                 sbd = &sc->stbd_list[sc->txcur];
 1691 
 1692                 sbd->bd->physaddr = htole32(segs[i].ds_addr);
 1693                 sbd->bd->len = htole32(segs[i].ds_len);
 1694                 sbd->bd->nfrag = 0;
 1695                 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
 1696                 if (i == nsegs - 1) {
 1697                         sbd->type = IPW_SBD_TYPE_DATA;
 1698                         sbd->priv = sbuf;
 1699                         sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
 1700                 } else {
 1701                         sbd->type = IPW_SBD_TYPE_NOASSOC;
 1702                         sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
 1703                 }
 1704 
 1705                 DPRINTFN(5, ("sending fragment (%d)\n", i));
 1706 
 1707                 sc->txfree--;
 1708                 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
 1709         }
 1710 
 1711         bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
 1712         bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
 1713         bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
 1714 
 1715         /* kick firmware */
 1716         CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
 1717 
 1718         return 0;
 1719 }
 1720 
 1721 static int
 1722 ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
 1723         const struct ieee80211_bpf_params *params)
 1724 {
 1725         /* no support; just discard */
 1726         m_freem(m);
 1727         ieee80211_free_node(ni);
 1728         return 0;
 1729 }
 1730 
 1731 static void
 1732 ipw_start(struct ifnet *ifp)
 1733 {
 1734         struct ipw_softc *sc = ifp->if_softc;
 1735 
 1736         IPW_LOCK(sc);
 1737         ipw_start_locked(ifp);
 1738         IPW_UNLOCK(sc);
 1739 }
 1740 
 1741 static void
 1742 ipw_start_locked(struct ifnet *ifp)
 1743 {
 1744         struct ipw_softc *sc = ifp->if_softc;
 1745         struct ieee80211_node *ni;
 1746         struct mbuf *m;
 1747 
 1748         IPW_LOCK_ASSERT(sc);
 1749 
 1750         for (;;) {
 1751                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
 1752                 if (m == NULL)
 1753                         break;
 1754                 if (sc->txfree < 1 + IPW_MAX_NSEG) {
 1755                         IFQ_DRV_PREPEND(&ifp->if_snd, m);
 1756                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1757                         break;
 1758                 }
 1759                 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
 1760                 if (ipw_tx_start(ifp, m, ni) != 0) {
 1761                         ieee80211_free_node(ni);
 1762                         ifp->if_oerrors++;
 1763                         break;
 1764                 }
 1765                 /* start watchdog timer */
 1766                 sc->sc_tx_timer = 5;
 1767         }
 1768 }
 1769 
 1770 static void
 1771 ipw_watchdog(void *arg)
 1772 {
 1773         struct ipw_softc *sc = arg;
 1774         struct ifnet *ifp = sc->sc_ifp;
 1775         struct ieee80211com *ic = ifp->if_l2com;
 1776 
 1777         IPW_LOCK_ASSERT(sc);
 1778 
 1779         if (sc->sc_tx_timer > 0) {
 1780                 if (--sc->sc_tx_timer == 0) {
 1781                         if_printf(ifp, "device timeout\n");
 1782                         ifp->if_oerrors++;
 1783                         taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
 1784                 }
 1785         }
 1786         if (sc->sc_scan_timer > 0) {
 1787                 if (--sc->sc_scan_timer == 0) {
 1788                         DPRINTFN(3, ("Scan timeout\n"));
 1789                         /* End the scan */
 1790                         if (sc->flags & IPW_FLAG_SCANNING) {
 1791                                 IPW_UNLOCK(sc);
 1792                                 ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
 1793                                 IPW_LOCK(sc);
 1794                                 sc->flags &= ~IPW_FLAG_SCANNING;
 1795                         }
 1796                 }
 1797         }
 1798         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1799                 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
 1800 }
 1801 
 1802 static int
 1803 ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
 1804 {
 1805         struct ipw_softc *sc = ifp->if_softc;
 1806         struct ieee80211com *ic = ifp->if_l2com;
 1807         struct ifreq *ifr = (struct ifreq *) data;
 1808         int error = 0, startall = 0;
 1809 
 1810         switch (cmd) {
 1811         case SIOCSIFFLAGS:
 1812                 IPW_LOCK(sc);
 1813                 if (ifp->if_flags & IFF_UP) {
 1814                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
 1815                                 ipw_init_locked(sc);
 1816                                 startall = 1;
 1817                         }
 1818                 } else {
 1819                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 1820                                 ipw_stop_locked(sc);
 1821                 }
 1822                 IPW_UNLOCK(sc);
 1823                 if (startall)
 1824                         ieee80211_start_all(ic);
 1825                 break;
 1826         case SIOCGIFMEDIA:
 1827                 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
 1828                 break;
 1829         case SIOCGIFADDR:
 1830                 error = ether_ioctl(ifp, cmd, data);
 1831                 break;
 1832         default:
 1833                 error = EINVAL;
 1834                 break;
 1835         }
 1836         return error;
 1837 }
 1838 
 1839 static void
 1840 ipw_stop_master(struct ipw_softc *sc)
 1841 {
 1842         uint32_t tmp;
 1843         int ntries;
 1844 
 1845         /* disable interrupts */
 1846         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
 1847 
 1848         CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
 1849         for (ntries = 0; ntries < 50; ntries++) {
 1850                 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
 1851                         break;
 1852                 DELAY(10);
 1853         }
 1854         if (ntries == 50)
 1855                 device_printf(sc->sc_dev, "timeout waiting for master\n");
 1856 
 1857         tmp = CSR_READ_4(sc, IPW_CSR_RST);
 1858         CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
 1859 
 1860         /* Clear all flags except the following */
 1861         sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
 1862 }
 1863 
 1864 static int
 1865 ipw_reset(struct ipw_softc *sc)
 1866 {
 1867         uint32_t tmp;
 1868         int ntries;
 1869 
 1870         ipw_stop_master(sc);
 1871 
 1872         /* move adapter to D0 state */
 1873         tmp = CSR_READ_4(sc, IPW_CSR_CTL);
 1874         CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
 1875 
 1876         /* wait for clock stabilization */
 1877         for (ntries = 0; ntries < 1000; ntries++) {
 1878                 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
 1879                         break;
 1880                 DELAY(200);
 1881         }
 1882         if (ntries == 1000)
 1883                 return EIO;
 1884 
 1885         tmp =  CSR_READ_4(sc, IPW_CSR_RST);
 1886         CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
 1887 
 1888         DELAY(10);
 1889 
 1890         tmp = CSR_READ_4(sc, IPW_CSR_CTL);
 1891         CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
 1892 
 1893         return 0;
 1894 }
 1895 
 1896 static int
 1897 ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
 1898 {
 1899         int ms = hz < 1000 ? 1 : hz/10;
 1900         int i, error;
 1901 
 1902         for (i = 0; i < 100; i++) {
 1903                 if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
 1904                         return 0;
 1905                 error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
 1906                 if (error == 0 || error != EWOULDBLOCK)
 1907                         return 0;
 1908         }
 1909         DPRINTF(("%s: timeout waiting for %s\n",
 1910                 __func__, waitfor ? "disable" : "enable"));
 1911         return ETIMEDOUT;
 1912 }
 1913 
 1914 static int
 1915 ipw_enable(struct ipw_softc *sc)
 1916 {
 1917         int error;
 1918 
 1919         if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
 1920                 DPRINTF(("Enable adapter\n"));
 1921                 error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
 1922                 if (error != 0)
 1923                         return error;
 1924                 error = ipw_waitfordisable(sc, 0);
 1925                 if (error != 0)
 1926                         return error;
 1927                 sc->flags |= IPW_FLAG_ENABLED;
 1928         }
 1929         return 0;
 1930 }
 1931 
 1932 static int
 1933 ipw_disable(struct ipw_softc *sc)
 1934 {
 1935         int error;
 1936 
 1937         if (sc->flags & IPW_FLAG_ENABLED) {
 1938                 DPRINTF(("Disable adapter\n"));
 1939                 error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
 1940                 if (error != 0)
 1941                         return error;
 1942                 error = ipw_waitfordisable(sc, 1);
 1943                 if (error != 0)
 1944                         return error;
 1945                 sc->flags &= ~IPW_FLAG_ENABLED;
 1946         }
 1947         return 0;
 1948 }
 1949 
 1950 /*
 1951  * Upload the microcode to the device.
 1952  */
 1953 static int
 1954 ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
 1955 {
 1956         int ntries;
 1957 
 1958         MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
 1959         CSR_WRITE_4(sc, IPW_CSR_RST, 0);
 1960 
 1961         MEM_WRITE_2(sc, 0x220000, 0x0703);
 1962         MEM_WRITE_2(sc, 0x220000, 0x0707);
 1963 
 1964         MEM_WRITE_1(sc, 0x210014, 0x72);
 1965         MEM_WRITE_1(sc, 0x210014, 0x72);
 1966 
 1967         MEM_WRITE_1(sc, 0x210000, 0x40);
 1968         MEM_WRITE_1(sc, 0x210000, 0x00);
 1969         MEM_WRITE_1(sc, 0x210000, 0x40);
 1970 
 1971         MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
 1972 
 1973         MEM_WRITE_1(sc, 0x210000, 0x00);
 1974         MEM_WRITE_1(sc, 0x210000, 0x00);
 1975         MEM_WRITE_1(sc, 0x210000, 0x80);
 1976 
 1977         MEM_WRITE_2(sc, 0x220000, 0x0703);
 1978         MEM_WRITE_2(sc, 0x220000, 0x0707);
 1979 
 1980         MEM_WRITE_1(sc, 0x210014, 0x72);
 1981         MEM_WRITE_1(sc, 0x210014, 0x72);
 1982 
 1983         MEM_WRITE_1(sc, 0x210000, 0x00);
 1984         MEM_WRITE_1(sc, 0x210000, 0x80);
 1985 
 1986         for (ntries = 0; ntries < 10; ntries++) {
 1987                 if (MEM_READ_1(sc, 0x210000) & 1)
 1988                         break;
 1989                 DELAY(10);
 1990         }
 1991         if (ntries == 10) {
 1992                 device_printf(sc->sc_dev,
 1993                     "timeout waiting for ucode to initialize\n");
 1994                 return EIO;
 1995         }
 1996 
 1997         MEM_WRITE_4(sc, 0x3000e0, 0);
 1998 
 1999         return 0;
 2000 }
 2001 
 2002 /* set of macros to handle unaligned little endian data in firmware image */
 2003 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
 2004 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
 2005 static int
 2006 ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
 2007 {
 2008         const uint8_t *p, *end;
 2009         uint32_t tmp, dst;
 2010         uint16_t len;
 2011         int error;
 2012 
 2013         p = fw;
 2014         end = fw + size;
 2015         while (p < end) {
 2016                 dst = GETLE32(p); p += 4;
 2017                 len = GETLE16(p); p += 2;
 2018 
 2019                 ipw_write_mem_1(sc, dst, p, len);
 2020                 p += len;
 2021         }
 2022 
 2023         CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
 2024             IPW_IO_LED_OFF);
 2025 
 2026         /* enable interrupts */
 2027         CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
 2028 
 2029         /* kick the firmware */
 2030         CSR_WRITE_4(sc, IPW_CSR_RST, 0);
 2031 
 2032         tmp = CSR_READ_4(sc, IPW_CSR_CTL);
 2033         CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
 2034 
 2035         /* wait at most one second for firmware initialization to complete */
 2036         if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
 2037                 device_printf(sc->sc_dev, "timeout waiting for firmware "
 2038                     "initialization to complete\n");
 2039                 return error;
 2040         }
 2041 
 2042         tmp = CSR_READ_4(sc, IPW_CSR_IO);
 2043         CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
 2044             IPW_IO_GPIO3_MASK);
 2045 
 2046         return 0;
 2047 }
 2048 
 2049 static int
 2050 ipw_setwepkeys(struct ipw_softc *sc)
 2051 {
 2052         struct ifnet *ifp = sc->sc_ifp;
 2053         struct ieee80211com *ic = ifp->if_l2com;
 2054         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 2055         struct ipw_wep_key wepkey;
 2056         struct ieee80211_key *wk;
 2057         int error, i;
 2058 
 2059         for (i = 0; i < IEEE80211_WEP_NKID; i++) {
 2060                 wk = &vap->iv_nw_keys[i];
 2061 
 2062                 if (wk->wk_cipher == NULL ||
 2063                     wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
 2064                         continue;
 2065 
 2066                 wepkey.idx = i;
 2067                 wepkey.len = wk->wk_keylen;
 2068                 memset(wepkey.key, 0, sizeof wepkey.key);
 2069                 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
 2070                 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
 2071                     wepkey.len));
 2072                 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
 2073                     sizeof wepkey);
 2074                 if (error != 0)
 2075                         return error;
 2076         }
 2077         return 0;
 2078 }
 2079 
 2080 static int
 2081 ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
 2082 {
 2083         struct ipw_wpa_ie wpaie;
 2084 
 2085         memset(&wpaie, 0, sizeof(wpaie));
 2086         wpaie.len = htole32(ielen);
 2087         /* XXX verify length */
 2088         memcpy(&wpaie.ie, ie, ielen);
 2089         DPRINTF(("Setting WPA IE\n"));
 2090         return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
 2091 }
 2092 
 2093 static int
 2094 ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
 2095 {
 2096         static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
 2097 
 2098         if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
 2099                 DPRINTF(("Setting mandatory BSSID to null\n"));
 2100                 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
 2101         } else {
 2102                 DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
 2103                 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
 2104                         bssid, IEEE80211_ADDR_LEN);
 2105         }
 2106 }
 2107 
 2108 static int
 2109 ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
 2110 {
 2111         if (ssidlen == 0) {
 2112                 /*
 2113                  * A bug in the firmware breaks the ``don't associate''
 2114                  * bit in the scan options command.  To compensate for
 2115                  * this install a bogus ssid when no ssid is specified
 2116                  * so the firmware won't try to associate.
 2117                  */
 2118                 DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
 2119                 return ipw_cmd(sc, IPW_CMD_SET_ESSID,
 2120                         "\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
 2121                         "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
 2122                         "\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
 2123                         "\x3c\x3d", IEEE80211_NWID_LEN);
 2124         } else {
 2125 #ifdef IPW_DEBUG
 2126                 if (ipw_debug > 0) {
 2127                         printf("Setting ESSID to ");
 2128                         ieee80211_print_essid(ssid, ssidlen);
 2129                         printf("\n");
 2130                 }
 2131 #endif
 2132                 return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
 2133         }
 2134 }
 2135 
 2136 static int
 2137 ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
 2138 {
 2139         struct ipw_scan_options opts;
 2140 
 2141         DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
 2142         opts.channels = htole32(chanmask);
 2143         opts.flags = htole32(flags);
 2144         return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
 2145 }
 2146 
 2147 static int
 2148 ipw_scan(struct ipw_softc *sc)
 2149 {
 2150         uint32_t params;
 2151         int error;
 2152 
 2153         DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
 2154 
 2155         if (sc->flags & IPW_FLAG_SCANNING)
 2156                 return (EBUSY);
 2157         sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
 2158 
 2159         /* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
 2160         error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
 2161         if (error != 0)
 2162                 goto done;
 2163 
 2164         /*
 2165          * Setup null/bogus ssid so firmware doesn't use any previous
 2166          * ssid to try and associate.  This is because the ``don't
 2167          * associate'' option bit is broken (sigh).
 2168          */
 2169         error = ipw_setssid(sc, NULL, 0);
 2170         if (error != 0)
 2171                 goto done;
 2172 
 2173         /*
 2174          * NB: the adapter may be disabled on association lost;
 2175          *     if so just re-enable it to kick off scanning.
 2176          */
 2177         DPRINTF(("Starting scan\n"));
 2178         sc->sc_scan_timer = 3;
 2179         if (sc->flags & IPW_FLAG_ENABLED) {
 2180                 params = 0;                             /* XXX? */
 2181                 error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
 2182                                 &params, sizeof(params));
 2183         } else
 2184                 error = ipw_enable(sc);
 2185 done:
 2186         if (error != 0) {
 2187                 DPRINTF(("Scan failed\n"));
 2188                 sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
 2189         }
 2190         return (error);
 2191 }
 2192 
 2193 static int
 2194 ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
 2195 {
 2196         struct ifnet *ifp = sc->sc_ifp;
 2197         struct ieee80211com *ic = ifp->if_l2com;
 2198         uint32_t data;
 2199         int error;
 2200 
 2201         data = htole32(ieee80211_chan2ieee(ic, chan));
 2202         DPRINTF(("Setting channel to %u\n", le32toh(data)));
 2203         error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
 2204         if (error == 0)
 2205                 ipw_setcurchan(sc, chan);
 2206         return error;
 2207 }
 2208 
 2209 static void
 2210 ipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
 2211 {
 2212         struct ifnet *ifp = vap->iv_ic->ic_ifp;
 2213         struct ipw_softc *sc = ifp->if_softc;
 2214         struct ieee80211_node *ni = vap->iv_bss;
 2215         struct ipw_security security;
 2216         uint32_t data;
 2217         int error;
 2218 
 2219         IPW_LOCK(sc);
 2220         error = ipw_disable(sc);
 2221         if (error != 0)
 2222                 goto done;
 2223 
 2224         memset(&security, 0, sizeof security);
 2225         security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
 2226             IPW_AUTH_SHARED : IPW_AUTH_OPEN;
 2227         security.ciphers = htole32(IPW_CIPHER_NONE);
 2228         DPRINTF(("Setting authmode to %u\n", security.authmode));
 2229         error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
 2230             sizeof security);
 2231         if (error != 0)
 2232                 goto done;
 2233 
 2234         data = htole32(vap->iv_rtsthreshold);
 2235         DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
 2236         error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
 2237         if (error != 0)
 2238                 goto done;
 2239 
 2240         data = htole32(vap->iv_fragthreshold);
 2241         DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
 2242         error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
 2243         if (error != 0)
 2244                 goto done;
 2245 
 2246         if (vap->iv_flags & IEEE80211_F_PRIVACY) {
 2247                 error = ipw_setwepkeys(sc);
 2248                 if (error != 0)
 2249                         goto done;
 2250 
 2251                 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
 2252                         data = htole32(vap->iv_def_txkey);
 2253                         DPRINTF(("Setting wep tx key index to %u\n",
 2254                                 le32toh(data)));
 2255                         error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
 2256                             sizeof data);
 2257                         if (error != 0)
 2258                                 goto done;
 2259                 }
 2260         }
 2261 
 2262         data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
 2263         DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
 2264         error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
 2265         if (error != 0)
 2266                 goto done;
 2267 
 2268         error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
 2269         if (error != 0)
 2270                 goto done;
 2271 
 2272         error = ipw_setbssid(sc, ni->ni_bssid);
 2273         if (error != 0)
 2274                 goto done;
 2275 
 2276         if (vap->iv_appie_wpa != NULL) {
 2277                 struct ieee80211_appie *ie = vap->iv_appie_wpa;
 2278                 error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
 2279                 if (error != 0)
 2280                         goto done;
 2281         }
 2282         if (ic->ic_opmode == IEEE80211_M_IBSS) {
 2283                 error = ipw_setchannel(sc, ni->ni_chan);
 2284                 if (error != 0)
 2285                         goto done;
 2286         }
 2287 
 2288         /* lock scan to ap's channel and enable associate */
 2289         error = ipw_setscanopts(sc,
 2290             1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
 2291         if (error != 0)
 2292                 goto done;
 2293 
 2294         error = ipw_enable(sc);         /* finally, enable adapter */
 2295         if (error == 0)
 2296                 sc->flags |= IPW_FLAG_ASSOCIATING;
 2297 done:
 2298         IPW_UNLOCK(sc);
 2299 }
 2300 
 2301 static void
 2302 ipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
 2303 {
 2304         struct ifnet *ifp = vap->iv_ic->ic_ifp;
 2305         struct ieee80211_node *ni = vap->iv_bss;
 2306         struct ipw_softc *sc = ifp->if_softc;
 2307 
 2308         IPW_LOCK(sc);
 2309         DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
 2310         /*
 2311          * NB: don't try to do this if ipw_stop_master has
 2312          *     shutdown the firmware and disabled interrupts.
 2313          */
 2314         if (sc->flags & IPW_FLAG_FW_INITED) {
 2315                 sc->flags &= ~IPW_FLAG_ASSOCIATED;
 2316                 /*
 2317                  * NB: firmware currently ignores bssid parameter, but
 2318                  *     supply it in case this changes (follow linux driver).
 2319                  */
 2320                 (void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
 2321                         ni->ni_bssid, IEEE80211_ADDR_LEN);
 2322         }
 2323         IPW_UNLOCK(sc);
 2324 }
 2325 
 2326 /*
 2327  * Handler for sc_init_task.  This is a simple wrapper around ipw_init().
 2328  * It is called on firmware panics or on watchdog timeouts.
 2329  */
 2330 static void
 2331 ipw_init_task(void *context, int pending)
 2332 {
 2333         ipw_init(context);
 2334 }
 2335 
 2336 static void
 2337 ipw_init(void *priv)
 2338 {
 2339         struct ipw_softc *sc = priv;
 2340         struct ifnet *ifp = sc->sc_ifp;
 2341         struct ieee80211com *ic = ifp->if_l2com;
 2342 
 2343         IPW_LOCK(sc);
 2344         ipw_init_locked(sc);
 2345         IPW_UNLOCK(sc);
 2346 
 2347         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
 2348                 ieee80211_start_all(ic);                /* start all vap's */
 2349 }
 2350 
 2351 static void
 2352 ipw_init_locked(struct ipw_softc *sc)
 2353 {
 2354         struct ifnet *ifp = sc->sc_ifp;
 2355         struct ieee80211com *ic = ifp->if_l2com;
 2356         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 2357         const struct firmware *fp;
 2358         const struct ipw_firmware_hdr *hdr;
 2359         const char *fw;
 2360 
 2361         IPW_LOCK_ASSERT(sc);
 2362 
 2363         DPRINTF(("%s: state %s flags 0x%x\n", __func__,
 2364                 ieee80211_state_name[vap->iv_state], sc->flags));
 2365 
 2366         /*
 2367          * Avoid re-entrant calls.  We need to release the mutex in ipw_init()
 2368          * when loading the firmware and we don't want to be called during this
 2369          * operation.
 2370          */
 2371         if (sc->flags & IPW_FLAG_INIT_LOCKED)
 2372                 return;
 2373         sc->flags |= IPW_FLAG_INIT_LOCKED;
 2374 
 2375         ipw_stop_locked(sc);
 2376 
 2377         if (ipw_reset(sc) != 0) {
 2378                 device_printf(sc->sc_dev, "could not reset adapter\n");
 2379                 goto fail;
 2380         }
 2381 
 2382         if (sc->sc_firmware == NULL) {
 2383                 device_printf(sc->sc_dev, "no firmware\n");
 2384                 goto fail;
 2385         }
 2386         /* NB: consistency already checked on load */
 2387         fp = sc->sc_firmware;
 2388         hdr = (const struct ipw_firmware_hdr *)fp->data;
 2389 
 2390         DPRINTF(("Loading firmware image '%s'\n", fp->name));
 2391         fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
 2392         if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
 2393                 device_printf(sc->sc_dev, "could not load microcode\n");
 2394                 goto fail;
 2395         }
 2396 
 2397         ipw_stop_master(sc);
 2398 
 2399         /*
 2400          * Setup tx, rx and status rings.
 2401          */
 2402         sc->txold = IPW_NTBD - 1;
 2403         sc->txcur = 0;
 2404         sc->txfree = IPW_NTBD - 2;
 2405         sc->rxcur = IPW_NRBD - 1;
 2406 
 2407         CSR_WRITE_4(sc, IPW_CSR_TX_BASE,  sc->tbd_phys);
 2408         CSR_WRITE_4(sc, IPW_CSR_TX_SIZE,  IPW_NTBD);
 2409         CSR_WRITE_4(sc, IPW_CSR_TX_READ,  0);
 2410         CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
 2411 
 2412         CSR_WRITE_4(sc, IPW_CSR_RX_BASE,  sc->rbd_phys);
 2413         CSR_WRITE_4(sc, IPW_CSR_RX_SIZE,  IPW_NRBD);
 2414         CSR_WRITE_4(sc, IPW_CSR_RX_READ,  0);
 2415         CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
 2416 
 2417         CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
 2418 
 2419         fw = (const char *)fp->data + sizeof *hdr;
 2420         if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
 2421                 device_printf(sc->sc_dev, "could not load firmware\n");
 2422                 goto fail;
 2423         }
 2424 
 2425         sc->flags |= IPW_FLAG_FW_INITED;
 2426 
 2427         /* retrieve information tables base addresses */
 2428         sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
 2429         sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
 2430 
 2431         ipw_write_table1(sc, IPW_INFO_LOCK, 0);
 2432 
 2433         if (ipw_config(sc) != 0) {
 2434                 device_printf(sc->sc_dev, "device configuration failed\n");
 2435                 goto fail;
 2436         }
 2437 
 2438         callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
 2439         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 2440         ifp->if_drv_flags |= IFF_DRV_RUNNING;
 2441 
 2442         sc->flags &=~ IPW_FLAG_INIT_LOCKED;
 2443         return;
 2444 
 2445 fail:
 2446         ipw_stop_locked(sc);
 2447         sc->flags &=~ IPW_FLAG_INIT_LOCKED;
 2448 }
 2449 
 2450 static int
 2451 ipw_config(struct ipw_softc *sc)
 2452 {
 2453         struct ifnet *ifp = sc->sc_ifp;
 2454         struct ieee80211com *ic = ifp->if_l2com;
 2455         struct ipw_configuration config;
 2456         uint32_t data;
 2457         int error;
 2458 
 2459         error = ipw_disable(sc);
 2460         if (error != 0)
 2461                 return error;
 2462 
 2463         switch (ic->ic_opmode) {
 2464         case IEEE80211_M_STA:
 2465         case IEEE80211_M_HOSTAP:
 2466         case IEEE80211_M_WDS:           /* XXX */
 2467                 data = htole32(IPW_MODE_BSS);
 2468                 break;
 2469         case IEEE80211_M_IBSS:
 2470         case IEEE80211_M_AHDEMO:
 2471                 data = htole32(IPW_MODE_IBSS);
 2472                 break;
 2473         case IEEE80211_M_MONITOR:
 2474                 data = htole32(IPW_MODE_MONITOR);
 2475                 break;
 2476         default:
 2477                 device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
 2478                 return EINVAL;
 2479         }
 2480         DPRINTF(("Setting mode to %u\n", le32toh(data)));
 2481         error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
 2482         if (error != 0)
 2483                 return error;
 2484 
 2485         if (ic->ic_opmode == IEEE80211_M_IBSS ||
 2486             ic->ic_opmode == IEEE80211_M_MONITOR) {
 2487                 error = ipw_setchannel(sc, ic->ic_curchan);
 2488                 if (error != 0)
 2489                         return error;
 2490         }
 2491 
 2492         if (ic->ic_opmode == IEEE80211_M_MONITOR)
 2493                 return ipw_enable(sc);
 2494 
 2495         config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
 2496             IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
 2497         if (ic->ic_opmode == IEEE80211_M_IBSS)
 2498                 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
 2499         if (ifp->if_flags & IFF_PROMISC)
 2500                 config.flags |= htole32(IPW_CFG_PROMISCUOUS);
 2501         config.bss_chan = htole32(0x3fff); /* channels 1-14 */
 2502         config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
 2503         DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
 2504         error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
 2505         if (error != 0)
 2506                 return error;
 2507 
 2508         data = htole32(0xf); /* 1, 2, 5.5, 11 */
 2509         DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
 2510         error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
 2511         if (error != 0)
 2512                 return error;
 2513 
 2514         /* Use the same rate set */
 2515         DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
 2516         error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
 2517         if (error != 0)
 2518                 return error;
 2519 
 2520         /* Use the same rate set */
 2521         DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
 2522         error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
 2523         if (error != 0)
 2524                 return error;
 2525 
 2526         data = htole32(IPW_POWER_MODE_CAM);
 2527         DPRINTF(("Setting power mode to %u\n", le32toh(data)));
 2528         error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
 2529         if (error != 0)
 2530                 return error;
 2531 
 2532         if (ic->ic_opmode == IEEE80211_M_IBSS) {
 2533                 data = htole32(32); /* default value */
 2534                 DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
 2535                 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
 2536                     sizeof data);
 2537                 if (error != 0)
 2538                         return error;
 2539         }
 2540 
 2541         return 0;
 2542 }
 2543 
 2544 static void
 2545 ipw_stop(void *priv)
 2546 {
 2547         struct ipw_softc *sc = priv;
 2548 
 2549         IPW_LOCK(sc);
 2550         ipw_stop_locked(sc);
 2551         IPW_UNLOCK(sc);
 2552 }
 2553 
 2554 static void
 2555 ipw_stop_locked(struct ipw_softc *sc)
 2556 {
 2557         struct ifnet *ifp = sc->sc_ifp;
 2558         int i;
 2559 
 2560         IPW_LOCK_ASSERT(sc);
 2561 
 2562         callout_stop(&sc->sc_wdtimer);
 2563         ipw_stop_master(sc);
 2564 
 2565         CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
 2566 
 2567         /*
 2568          * Release tx buffers.
 2569          */
 2570         for (i = 0; i < IPW_NTBD; i++)
 2571                 ipw_release_sbd(sc, &sc->stbd_list[i]);
 2572 
 2573         sc->sc_tx_timer = 0;
 2574         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
 2575 }
 2576 
 2577 static int
 2578 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
 2579 {
 2580         struct ipw_softc *sc = arg1;
 2581         uint32_t i, size, buf[256];
 2582 
 2583         memset(buf, 0, sizeof buf);
 2584 
 2585         if (!(sc->flags & IPW_FLAG_FW_INITED))
 2586                 return SYSCTL_OUT(req, buf, sizeof buf);
 2587 
 2588         CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
 2589 
 2590         size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
 2591         for (i = 1; i < size; i++)
 2592                 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
 2593 
 2594         return SYSCTL_OUT(req, buf, size);
 2595 }
 2596 
 2597 static int
 2598 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
 2599 {
 2600         struct ipw_softc *sc = arg1;
 2601         int val;
 2602 
 2603         val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
 2604                 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
 2605 
 2606         return SYSCTL_OUT(req, &val, sizeof val);
 2607 }
 2608 
 2609 static uint32_t
 2610 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
 2611 {
 2612         return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
 2613 }
 2614 
 2615 static void
 2616 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
 2617 {
 2618         MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
 2619 }
 2620 
 2621 #if 0
 2622 static int
 2623 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
 2624 {
 2625         uint32_t addr, info;
 2626         uint16_t count, size;
 2627         uint32_t total;
 2628 
 2629         /* addr[4] + count[2] + size[2] */
 2630         addr = MEM_READ_4(sc, sc->table2_base + off);
 2631         info = MEM_READ_4(sc, sc->table2_base + off + 4);
 2632 
 2633         count = info >> 16;
 2634         size = info & 0xffff;
 2635         total = count * size;
 2636 
 2637         if (total > *len) {
 2638                 *len = total;
 2639                 return EINVAL;
 2640         }
 2641 
 2642         *len = total;
 2643         ipw_read_mem_1(sc, addr, buf, total);
 2644 
 2645         return 0;
 2646 }
 2647 
 2648 static void
 2649 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
 2650     bus_size_t count)
 2651 {
 2652         for (; count > 0; offset++, datap++, count--) {
 2653                 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
 2654                 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
 2655         }
 2656 }
 2657 #endif
 2658 
 2659 static void
 2660 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
 2661     bus_size_t count)
 2662 {
 2663         for (; count > 0; offset++, datap++, count--) {
 2664                 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
 2665                 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
 2666         }
 2667 }
 2668 
 2669 static void
 2670 ipw_scan_start(struct ieee80211com *ic)
 2671 {
 2672         struct ifnet *ifp = ic->ic_ifp;
 2673         struct ipw_softc *sc = ifp->if_softc;
 2674 
 2675         IPW_LOCK(sc);
 2676         ipw_scan(sc);
 2677         IPW_UNLOCK(sc);
 2678 }
 2679 
 2680 static void
 2681 ipw_set_channel(struct ieee80211com *ic)
 2682 {
 2683         struct ifnet *ifp = ic->ic_ifp;
 2684         struct ipw_softc *sc = ifp->if_softc;
 2685 
 2686         IPW_LOCK(sc);
 2687         if (ic->ic_opmode == IEEE80211_M_MONITOR) {
 2688                 ipw_disable(sc);
 2689                 ipw_setchannel(sc, ic->ic_curchan);
 2690                 ipw_enable(sc);
 2691         }
 2692         IPW_UNLOCK(sc);
 2693 }
 2694 
 2695 static void
 2696 ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
 2697 {
 2698         /* NB: all channels are scanned at once */
 2699 }
 2700 
 2701 static void
 2702 ipw_scan_mindwell(struct ieee80211_scan_state *ss)
 2703 {
 2704         /* NB: don't try to abort scan; wait for firmware to finish */
 2705 }
 2706 
 2707 static void
 2708 ipw_scan_end(struct ieee80211com *ic)
 2709 {
 2710         struct ifnet *ifp = ic->ic_ifp;
 2711         struct ipw_softc *sc = ifp->if_softc;
 2712 
 2713         IPW_LOCK(sc);
 2714         sc->flags &= ~IPW_FLAG_SCANNING;
 2715         IPW_UNLOCK(sc);
 2716 }

Cache object: 36f68f2847800ed3f2acd1768efe1d10


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