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>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Intel(R) PRO/Wireless 2100 MiniPCI driver
37 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
40 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 #include <sys/sockio.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>
52 #include <sys/endian.h>
53 #include <sys/linker.h>
54 #include <sys/firmware.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.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>
71 #include <net80211/ieee80211_var.h>
72 #include <net80211/ieee80211_radiotap.h>
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>
80 #include <dev/ipw/if_ipwreg.h>
81 #include <dev/ipw/if_ipwvar.h>
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)
88 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
91 #define DPRINTFN(n, x)
94 MODULE_DEPEND(ipw, pci, 1, 1, 1);
95 MODULE_DEPEND(ipw, wlan, 1, 1, 1);
96 MODULE_DEPEND(ipw, firmware, 1, 1, 1);
104 static const struct ipw_ident ipw_ident_table[] = {
105 { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
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_assocsuccess(void *, int);
122 static void ipw_assocfailed(void *, int);
123 static void ipw_scandone(void *, int);
124 static void ipw_bmiss(void *, int);
125 static void ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
126 static void ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
127 struct ipw_soft_bd *, struct ipw_soft_buf *);
128 static void ipw_rx_intr(struct ipw_softc *);
129 static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
130 static void ipw_tx_intr(struct ipw_softc *);
131 static void ipw_intr(void *);
132 static void ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
133 static const char * ipw_cmdname(int);
134 static int ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
135 static int ipw_tx_start(struct ifnet *, struct mbuf *,
136 struct ieee80211_node *);
137 static int ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
138 const struct ieee80211_bpf_params *);
139 static void ipw_start(struct ifnet *);
140 static void ipw_start_locked(struct ifnet *);
141 static void ipw_watchdog(void *);
142 static int ipw_ioctl(struct ifnet *, u_long, caddr_t);
143 static void ipw_stop_master(struct ipw_softc *);
144 static int ipw_enable(struct ipw_softc *);
145 static int ipw_disable(struct ipw_softc *);
146 static int ipw_reset(struct ipw_softc *);
147 static int ipw_load_ucode(struct ipw_softc *, const char *, int);
148 static int ipw_load_firmware(struct ipw_softc *, const char *, int);
149 static int ipw_config(struct ipw_softc *);
150 static void ipw_assoc_task(void *, int);
151 static void ipw_disassoc_task(void *, int);
152 static void ipw_init_task(void *, int);
153 static void ipw_init(void *);
154 static void ipw_init_locked(struct ipw_softc *);
155 static void ipw_stop(void *);
156 static void ipw_stop_locked(struct ipw_softc *);
157 static int ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
158 static int ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
159 static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t);
160 static void ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
162 static int ipw_read_table2(struct ipw_softc *, uint32_t, void *,
164 static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
167 static void ipw_write_mem_1(struct ipw_softc *, bus_size_t,
168 const uint8_t *, bus_size_t);
169 static void ipw_scan_task(void *, int);
170 static int ipw_scan(struct ipw_softc *);
171 static void ipw_scan_start(struct ieee80211com *);
172 static void ipw_scan_end(struct ieee80211com *);
173 static void ipw_set_channel(struct ieee80211com *);
174 static void ipw_scan_curchan(struct ieee80211_scan_state *,
175 unsigned long maxdwell);
176 static void ipw_scan_mindwell(struct ieee80211_scan_state *);
178 static int ipw_probe(device_t);
179 static int ipw_attach(device_t);
180 static int ipw_detach(device_t);
181 static int ipw_shutdown(device_t);
182 static int ipw_suspend(device_t);
183 static int ipw_resume(device_t);
185 static device_method_t ipw_methods[] = {
186 /* Device interface */
187 DEVMETHOD(device_probe, ipw_probe),
188 DEVMETHOD(device_attach, ipw_attach),
189 DEVMETHOD(device_detach, ipw_detach),
190 DEVMETHOD(device_shutdown, ipw_shutdown),
191 DEVMETHOD(device_suspend, ipw_suspend),
192 DEVMETHOD(device_resume, ipw_resume),
197 static driver_t ipw_driver = {
200 sizeof (struct ipw_softc)
203 static devclass_t ipw_devclass;
205 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0);
206 DRIVER_MODULE(ipw, cardbus, ipw_driver, ipw_devclass, 0, 0);
209 ipw_probe(device_t dev)
211 const struct ipw_ident *ident;
213 for (ident = ipw_ident_table; ident->name != NULL; ident++) {
214 if (pci_get_vendor(dev) == ident->vendor &&
215 pci_get_device(dev) == ident->device) {
216 device_set_desc(dev, ident->name);
223 /* Base Address Register */
224 #define IPW_PCI_BAR0 0x10
227 ipw_attach(device_t dev)
229 struct ipw_softc *sc = device_get_softc(dev);
231 struct ieee80211com *ic;
232 struct ieee80211_channel *c;
238 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
239 MTX_DEF | MTX_RECURSE);
241 TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
242 TASK_INIT(&sc->sc_scan_task, 0, ipw_scan_task, sc);
243 TASK_INIT(&sc->sc_bmiss_task, 0, ipw_bmiss, sc);
244 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
246 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
247 device_printf(dev, "chip is in D%d power mode "
248 "-- setting to D0\n", pci_get_powerstate(dev));
249 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
252 pci_write_config(dev, 0x41, 0, 1);
254 /* enable bus-mastering */
255 pci_enable_busmaster(dev);
257 sc->mem_rid = IPW_PCI_BAR0;
258 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
260 if (sc->mem == NULL) {
261 device_printf(dev, "could not allocate memory resource\n");
265 sc->sc_st = rman_get_bustag(sc->mem);
266 sc->sc_sh = rman_get_bushandle(sc->mem);
269 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
270 RF_ACTIVE | RF_SHAREABLE);
271 if (sc->irq == NULL) {
272 device_printf(dev, "could not allocate interrupt resource\n");
276 if (ipw_reset(sc) != 0) {
277 device_printf(dev, "could not reset adapter\n");
281 if (ipw_dma_alloc(sc) != 0) {
282 device_printf(dev, "could not allocate DMA resources\n");
286 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
288 device_printf(dev, "can not if_alloc()\n");
294 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
295 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
296 ifp->if_init = ipw_init;
297 ifp->if_ioctl = ipw_ioctl;
298 ifp->if_start = ipw_start;
299 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
300 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
301 IFQ_SET_READY(&ifp->if_snd);
304 ic->ic_opmode = IEEE80211_M_STA;
305 ic->ic_phytype = IEEE80211_T_DS;
307 /* set device capabilities */
309 IEEE80211_C_STA /* station mode supported */
310 | IEEE80211_C_IBSS /* IBSS mode supported */
311 | IEEE80211_C_MONITOR /* monitor mode supported */
312 | IEEE80211_C_PMGT /* power save supported */
313 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
314 | IEEE80211_C_WPA /* 802.11i supported */
317 /* read MAC address from EEPROM */
318 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
319 ic->ic_myaddr[0] = val >> 8;
320 ic->ic_myaddr[1] = val & 0xff;
321 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
322 ic->ic_myaddr[2] = val >> 8;
323 ic->ic_myaddr[3] = val & 0xff;
324 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
325 ic->ic_myaddr[4] = val >> 8;
326 ic->ic_myaddr[5] = val & 0xff;
328 /* set supported .11b channels (read from EEPROM) */
329 if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
330 val = 0x7ff; /* default to channels 1-11 */
332 for (i = 1; i < 16; i++) {
333 if (val & (1 << i)) {
334 c = &ic->ic_channels[ic->ic_nchans++];
335 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
336 c->ic_flags = IEEE80211_CHAN_B;
341 /* check support for radio transmitter switch in EEPROM */
342 if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
343 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
345 ieee80211_ifattach(ic);
346 ic->ic_scan_start = ipw_scan_start;
347 ic->ic_scan_end = ipw_scan_end;
348 ic->ic_set_channel = ipw_set_channel;
349 ic->ic_scan_curchan = ipw_scan_curchan;
350 ic->ic_scan_mindwell = ipw_scan_mindwell;
351 ic->ic_raw_xmit = ipw_raw_xmit;
353 ic->ic_vap_create = ipw_vap_create;
354 ic->ic_vap_delete = ipw_vap_delete;
356 bpfattach(ifp, DLT_IEEE802_11_RADIO,
357 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap));
359 sc->sc_rxtap_len = sizeof sc->sc_rxtap;
360 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
361 sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT);
363 sc->sc_txtap_len = sizeof sc->sc_txtap;
364 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
365 sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT);
368 * Add a few sysctl knobs.
370 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
371 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
372 CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I",
373 "radio transmitter switch state (0=off, 1=on)");
375 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
376 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
377 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S",
381 * Hook our interrupt after all initialization is complete.
383 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
384 NULL, ipw_intr, sc, &sc->sc_ih);
386 device_printf(dev, "could not set up interrupt\n");
391 ieee80211_announce(ic);
399 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
401 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
403 mtx_destroy(&sc->sc_mtx);
408 ipw_detach(device_t dev)
410 struct ipw_softc *sc = device_get_softc(dev);
411 struct ifnet *ifp = sc->sc_ifp;
412 struct ieee80211com *ic = ifp->if_l2com;
417 ieee80211_ifdetach(ic);
419 callout_drain(&sc->sc_wdtimer);
420 taskqueue_drain(taskqueue_fast, &sc->sc_init_task);
421 taskqueue_drain(taskqueue_fast, &sc->sc_scan_task);
422 taskqueue_drain(taskqueue_fast, &sc->sc_bmiss_task);
426 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
427 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
429 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
433 if (sc->sc_firmware != NULL) {
434 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
435 sc->sc_firmware = NULL;
438 mtx_destroy(&sc->sc_mtx);
443 static struct ieee80211vap *
444 ipw_vap_create(struct ieee80211com *ic,
445 const char name[IFNAMSIZ], int unit, int opmode, int flags,
446 const uint8_t bssid[IEEE80211_ADDR_LEN],
447 const uint8_t mac[IEEE80211_ADDR_LEN])
449 struct ifnet *ifp = ic->ic_ifp;
450 struct ipw_softc *sc = ifp->if_softc;
452 struct ieee80211vap *vap;
453 const struct firmware *fp;
454 const struct ipw_firmware_hdr *hdr;
455 const char *imagename;
457 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
461 case IEEE80211_M_STA:
462 imagename = "ipw_bss";
464 case IEEE80211_M_IBSS:
465 imagename = "ipw_ibss";
467 case IEEE80211_M_MONITOR:
468 imagename = "ipw_monitor";
475 * Load firmware image using the firmware(9) subsystem. Doing
476 * this unlocked is ok since we're single-threaded by the
479 if (sc->sc_firmware == NULL ||
480 strcmp(sc->sc_firmware->name, imagename) != 0) {
481 if (sc->sc_firmware != NULL)
482 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
483 sc->sc_firmware = firmware_get(imagename);
485 if (sc->sc_firmware == NULL) {
486 device_printf(sc->sc_dev,
487 "could not load firmware image '%s'\n", imagename);
490 fp = sc->sc_firmware;
491 if (fp->datasize < sizeof *hdr) {
492 device_printf(sc->sc_dev,
493 "firmware image too short %zu\n", fp->datasize);
494 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
495 sc->sc_firmware = NULL;
498 hdr = (const struct ipw_firmware_hdr *)fp->data;
499 if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
500 le32toh(hdr->ucodesz)) {
501 device_printf(sc->sc_dev,
502 "firmware image too short %zu\n", fp->datasize);
503 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
504 sc->sc_firmware = NULL;
508 ivp = (struct ipw_vap *) malloc(sizeof(struct ipw_vap),
509 M_80211_VAP, M_NOWAIT | M_ZERO);
514 TASK_INIT(&ivp->assoc_task, 0, ipw_assoc_task, vap);
515 TASK_INIT(&ivp->disassoc_task, 0, ipw_disassoc_task, vap);
516 TASK_INIT(&ivp->assoc_success_task, 0, ipw_assocsuccess, vap);
517 TASK_INIT(&ivp->assoc_failed_task, 0, ipw_assocfailed, vap);
518 TASK_INIT(&ivp->scandone_task, 0, ipw_scandone, vap);
520 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
521 /* override with driver methods */
522 ivp->newstate = vap->iv_newstate;
523 vap->iv_newstate = ipw_newstate;
526 ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status);
527 ic->ic_opmode = opmode;
532 ipw_vap_delete(struct ieee80211vap *vap)
534 struct ipw_vap *ivp = IPW_VAP(vap);
536 ieee80211_vap_detach(vap);
537 free(ivp, M_80211_VAP);
541 ipw_dma_alloc(struct ipw_softc *sc)
543 struct ipw_soft_bd *sbd;
544 struct ipw_soft_hdr *shdr;
545 struct ipw_soft_buf *sbuf;
550 * Allocate and map tx ring.
552 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
553 BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
554 NULL, &sc->tbd_dmat);
556 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
560 error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
561 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
563 device_printf(sc->sc_dev,
564 "could not allocate tx ring DMA memory\n");
568 error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
569 IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
571 device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
576 * Allocate and map rx ring.
578 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
579 BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
580 NULL, &sc->rbd_dmat);
582 device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
586 error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
587 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
589 device_printf(sc->sc_dev,
590 "could not allocate rx ring DMA memory\n");
594 error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
595 IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
597 device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
602 * Allocate and map status ring.
604 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
605 BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
606 NULL, NULL, &sc->status_dmat);
608 device_printf(sc->sc_dev,
609 "could not create status ring DMA tag\n");
613 error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
614 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
616 device_printf(sc->sc_dev,
617 "could not allocate status ring DMA memory\n");
621 error = bus_dmamap_load(sc->status_dmat, sc->status_map,
622 sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
625 device_printf(sc->sc_dev,
626 "could not map status ring DMA memory\n");
631 * Allocate command DMA map.
633 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
634 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
635 sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
637 device_printf(sc->sc_dev, "could not create command DMA tag\n");
641 error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
643 device_printf(sc->sc_dev,
644 "could not create command DMA map\n");
649 * Allocate headers DMA maps.
651 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
652 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
653 sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
655 device_printf(sc->sc_dev, "could not create header DMA tag\n");
659 SLIST_INIT(&sc->free_shdr);
660 for (i = 0; i < IPW_NDATA; i++) {
661 shdr = &sc->shdr_list[i];
662 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
664 device_printf(sc->sc_dev,
665 "could not create header DMA map\n");
668 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
672 * Allocate tx buffers DMA maps.
674 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
675 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
676 NULL, NULL, &sc->txbuf_dmat);
678 device_printf(sc->sc_dev, "could not create tx DMA tag\n");
682 SLIST_INIT(&sc->free_sbuf);
683 for (i = 0; i < IPW_NDATA; i++) {
684 sbuf = &sc->tx_sbuf_list[i];
685 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
687 device_printf(sc->sc_dev,
688 "could not create tx DMA map\n");
691 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
695 * Initialize tx ring.
697 for (i = 0; i < IPW_NTBD; i++) {
698 sbd = &sc->stbd_list[i];
699 sbd->bd = &sc->tbd_list[i];
700 sbd->type = IPW_SBD_TYPE_NOASSOC;
704 * Pre-allocate rx buffers and DMA maps.
706 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
707 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
708 NULL, &sc->rxbuf_dmat);
710 device_printf(sc->sc_dev, "could not create rx DMA tag\n");
714 for (i = 0; i < IPW_NRBD; i++) {
715 sbd = &sc->srbd_list[i];
716 sbuf = &sc->rx_sbuf_list[i];
717 sbd->bd = &sc->rbd_list[i];
719 sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
720 if (sbuf->m == NULL) {
721 device_printf(sc->sc_dev,
722 "could not allocate rx mbuf\n");
727 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
729 device_printf(sc->sc_dev,
730 "could not create rx DMA map\n");
734 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
735 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
738 device_printf(sc->sc_dev,
739 "could not map rx DMA memory\n");
743 sbd->type = IPW_SBD_TYPE_DATA;
745 sbd->bd->physaddr = htole32(physaddr);
746 sbd->bd->len = htole32(MCLBYTES);
749 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
753 fail: ipw_release(sc);
758 ipw_release(struct ipw_softc *sc)
760 struct ipw_soft_buf *sbuf;
763 if (sc->tbd_dmat != NULL) {
764 if (sc->stbd_list != NULL) {
765 bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
766 bus_dmamem_free(sc->tbd_dmat, sc->tbd_list,
769 bus_dma_tag_destroy(sc->tbd_dmat);
772 if (sc->rbd_dmat != NULL) {
773 if (sc->rbd_list != NULL) {
774 bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
775 bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
778 bus_dma_tag_destroy(sc->rbd_dmat);
781 if (sc->status_dmat != NULL) {
782 if (sc->status_list != NULL) {
783 bus_dmamap_unload(sc->status_dmat, sc->status_map);
784 bus_dmamem_free(sc->status_dmat, sc->status_list,
787 bus_dma_tag_destroy(sc->status_dmat);
790 for (i = 0; i < IPW_NTBD; i++)
791 ipw_release_sbd(sc, &sc->stbd_list[i]);
793 if (sc->cmd_dmat != NULL) {
794 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
795 bus_dma_tag_destroy(sc->cmd_dmat);
798 if (sc->hdr_dmat != NULL) {
799 for (i = 0; i < IPW_NDATA; i++)
800 bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
801 bus_dma_tag_destroy(sc->hdr_dmat);
804 if (sc->txbuf_dmat != NULL) {
805 for (i = 0; i < IPW_NDATA; i++) {
806 bus_dmamap_destroy(sc->txbuf_dmat,
807 sc->tx_sbuf_list[i].map);
809 bus_dma_tag_destroy(sc->txbuf_dmat);
812 if (sc->rxbuf_dmat != NULL) {
813 for (i = 0; i < IPW_NRBD; i++) {
814 sbuf = &sc->rx_sbuf_list[i];
815 if (sbuf->m != NULL) {
816 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
817 BUS_DMASYNC_POSTREAD);
818 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
821 bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
823 bus_dma_tag_destroy(sc->rxbuf_dmat);
828 ipw_shutdown(device_t dev)
830 struct ipw_softc *sc = device_get_softc(dev);
838 ipw_suspend(device_t dev)
840 struct ipw_softc *sc = device_get_softc(dev);
848 ipw_resume(device_t dev)
850 struct ipw_softc *sc = device_get_softc(dev);
851 struct ifnet *ifp = sc->sc_ifp;
853 pci_write_config(dev, 0x41, 0, 1);
855 if (ifp->if_flags & IFF_UP)
862 ipw_cvtrate(int ipwrate)
865 case IPW_RATE_DS1: return 2;
866 case IPW_RATE_DS2: return 4;
867 case IPW_RATE_DS5: return 11;
868 case IPW_RATE_DS11: return 22;
874 * The firmware automatically adapts the transmit speed. We report its current
878 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
880 struct ieee80211vap *vap = ifp->if_softc;
881 struct ieee80211com *ic = vap->iv_ic;
882 struct ipw_softc *sc = ic->ic_ifp->if_softc;
884 /* read current transmission rate from adapter */
885 vap->iv_bss->ni_txrate = ipw_cvtrate(
886 ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
887 ieee80211_media_status(ifp, imr);
891 ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
893 struct ipw_vap *ivp = IPW_VAP(vap);
894 struct ieee80211com *ic = vap->iv_ic;
895 struct ifnet *ifp = ic->ic_ifp;
896 struct ipw_softc *sc = ifp->if_softc;
898 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
899 ieee80211_state_name[vap->iv_state],
900 ieee80211_state_name[nstate], sc->flags));
903 case IEEE80211_S_RUN:
904 if (ic->ic_opmode == IEEE80211_M_IBSS) {
906 * XXX when joining an ibss network we are called
907 * with a SCAN -> RUN transition on scan complete.
908 * Use that to call ipw_auth_and_assoc. On completing
909 * the join we are then called again with an
910 * AUTH -> RUN transition and we want to do nothing.
911 * This is all totally bogus and needs to be redone.
913 if (vap->iv_state == IEEE80211_S_SCAN) {
914 taskqueue_enqueue(taskqueue_swi,
915 &IPW_VAP(vap)->assoc_task);
921 case IEEE80211_S_INIT:
922 if (sc->flags & IPW_FLAG_ASSOCIATED)
923 taskqueue_enqueue(taskqueue_swi,
924 &IPW_VAP(vap)->disassoc_task);
927 case IEEE80211_S_AUTH:
928 taskqueue_enqueue(taskqueue_swi, &IPW_VAP(vap)->assoc_task);
931 case IEEE80211_S_ASSOC:
933 * If we are not transitioning from AUTH the resend the
934 * association request.
936 if (vap->iv_state != IEEE80211_S_AUTH) {
937 taskqueue_enqueue(taskqueue_swi,
938 &IPW_VAP(vap)->assoc_task);
946 return ivp->newstate(vap, nstate, arg);
950 * Read 16 bits at address 'addr' from the serial EEPROM.
953 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
959 /* clock C once before the first command */
960 IPW_EEPROM_CTL(sc, 0);
961 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
962 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
963 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
965 /* write start bit (1) */
966 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
967 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
969 /* write READ opcode (10) */
970 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
971 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
972 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
973 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
975 /* write address A7-A0 */
976 for (n = 7; n >= 0; n--) {
977 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
978 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
979 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
980 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
983 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
985 /* read data Q15-Q0 */
987 for (n = 15; n >= 0; n--) {
988 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
989 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
990 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
991 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
994 IPW_EEPROM_CTL(sc, 0);
996 /* clear Chip Select and clock C */
997 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
998 IPW_EEPROM_CTL(sc, 0);
999 IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
1001 return le16toh(val);
1005 ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1007 struct ipw_cmd *cmd;
1009 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1011 cmd = mtod(sbuf->m, struct ipw_cmd *);
1013 DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
1014 ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
1015 le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
1016 le32toh(cmd->status)));
1018 sc->flags &= ~IPW_FLAG_BUSY;
1023 ipw_assocsuccess(void *arg, int npending)
1025 struct ieee80211vap *vap = arg;
1027 ieee80211_new_state(vap, IEEE80211_S_RUN, -1);
1031 ipw_assocfailed(void *arg, int npending)
1033 struct ieee80211vap *vap = arg;
1035 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1039 ipw_scandone(void *arg, int npending)
1041 struct ieee80211vap *vap = arg;
1043 ieee80211_scan_done(vap);
1047 ipw_bmiss(void *arg, int npending)
1049 struct ieee80211com *ic = arg;
1051 ieee80211_beacon_miss(ic);
1055 ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1057 #define IEEESTATE(vap) ieee80211_state_name[vap->iv_state]
1058 struct ifnet *ifp = sc->sc_ifp;
1059 struct ieee80211com *ic = ifp->if_l2com;
1060 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1063 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1065 state = le32toh(*mtod(sbuf->m, uint32_t *));
1068 case IPW_STATE_ASSOCIATED:
1069 DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1070 IEEESTATE(vap), sc->flags));
1071 /* XXX suppress state change in case the fw auto-associates */
1072 if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1073 DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1074 IEEESTATE(vap), sc->flags));
1077 sc->flags &= ~IPW_FLAG_ASSOCIATING;
1078 sc->flags |= IPW_FLAG_ASSOCIATED;
1079 taskqueue_enqueue(taskqueue_swi,
1080 &IPW_VAP(vap)->assoc_success_task);
1083 case IPW_STATE_SCANNING:
1084 DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1085 IEEESTATE(vap), sc->flags));
1087 * NB: Check driver state for association on assoc
1088 * loss as the firmware will immediately start to
1089 * scan and we would treat it as a beacon miss if
1090 * we checked the 802.11 layer state.
1092 if (sc->flags & IPW_FLAG_ASSOCIATED) {
1093 /* XXX probably need to issue disassoc to fw */
1094 taskqueue_enqueue(taskqueue_swi, &sc->sc_bmiss_task);
1098 case IPW_STATE_SCAN_COMPLETE:
1100 * XXX For some reason scan requests generate scan
1101 * started + scan done events before any traffic is
1102 * received (e.g. probe response frames). We work
1103 * around this by marking the HACK flag and skipping
1104 * the first scan complete event.
1106 DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1107 IEEESTATE(vap), sc->flags));
1108 if (sc->flags & IPW_FLAG_HACK) {
1109 sc->flags &= ~IPW_FLAG_HACK;
1112 if (sc->flags & IPW_FLAG_SCANNING) {
1113 taskqueue_enqueue(taskqueue_swi,
1114 &IPW_VAP(vap)->scandone_task);
1115 sc->flags &= ~IPW_FLAG_SCANNING;
1116 sc->sc_scan_timer = 0;
1120 case IPW_STATE_ASSOCIATION_LOST:
1121 DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1122 IEEESTATE(vap), sc->flags));
1123 sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1124 if (vap->iv_state == IEEE80211_S_RUN)
1125 taskqueue_enqueue(taskqueue_swi,
1126 &IPW_VAP(vap)->assoc_failed_task);
1129 case IPW_STATE_DISABLED:
1130 /* XXX? is this right? */
1131 sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1132 IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1133 DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1134 IEEESTATE(vap), sc->flags));
1137 case IPW_STATE_RADIO_DISABLED:
1138 device_printf(sc->sc_dev, "radio turned off\n");
1139 ieee80211_notify_radio(ic, 0);
1140 ipw_stop_locked(sc);
1141 /* XXX start polling thread to detect radio on */
1145 DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1146 __func__, state, IEEESTATE(vap), sc->flags));
1153 * Set driver state for current channel.
1156 ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1158 struct ifnet *ifp = sc->sc_ifp;
1159 struct ieee80211com *ic = ifp->if_l2com;
1161 ic->ic_curchan = chan;
1162 sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
1163 htole16(ic->ic_curchan->ic_freq);
1164 sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
1165 htole16(ic->ic_curchan->ic_flags);
1169 * XXX: Hack to set the current channel to the value advertised in beacons or
1170 * probe responses. Only used during AP detection.
1173 ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1175 struct ifnet *ifp = sc->sc_ifp;
1176 struct ieee80211com *ic = ifp->if_l2com;
1177 struct ieee80211_channel *c;
1178 struct ieee80211_frame *wh;
1180 uint8_t *frm, *efrm;
1182 wh = mtod(m, struct ieee80211_frame *);
1184 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1187 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1189 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1190 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1193 /* XXX use ieee80211_parse_beacon */
1194 frm = (uint8_t *)(wh + 1);
1195 efrm = mtod(m, uint8_t *) + m->m_len;
1197 frm += 12; /* skip tstamp, bintval and capinfo fields */
1198 while (frm < efrm) {
1199 if (*frm == IEEE80211_ELEMID_DSPARMS)
1200 #if IEEE80211_CHAN_MAX < 255
1201 if (frm[2] <= IEEE80211_CHAN_MAX)
1204 DPRINTF(("Fixing channel to %d\n", frm[2]));
1205 c = ieee80211_find_channel(ic,
1206 ieee80211_ieee2mhz(frm[2], 0),
1209 c = &ic->ic_channels[0];
1210 ipw_setcurchan(sc, c);
1218 ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1219 struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1221 struct ifnet *ifp = sc->sc_ifp;
1222 struct ieee80211com *ic = ifp->if_l2com;
1223 struct mbuf *mnew, *m;
1224 struct ieee80211_node *ni;
1225 bus_addr_t physaddr;
1229 DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1232 if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1233 le32toh(status->len) > MCLBYTES)
1237 * Try to allocate a new mbuf for this ring element and load it before
1238 * processing the current mbuf. If the ring element cannot be loaded,
1239 * drop the received packet and reuse the old mbuf. In the unlikely
1240 * case that the old mbuf can't be reloaded either, explicitly panic.
1242 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1248 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1249 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1251 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1252 MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1256 /* try to reload the old mbuf */
1257 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1258 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1261 /* very unlikely that it will fail... */
1262 panic("%s: could not load old rx mbuf",
1263 device_get_name(sc->sc_dev));
1270 * New mbuf successfully loaded, update Rx ring and continue
1275 sbd->bd->physaddr = htole32(physaddr);
1278 m->m_pkthdr.rcvif = ifp;
1279 m->m_pkthdr.len = m->m_len = le32toh(status->len);
1281 if (bpf_peers_present(ifp->if_bpf)) {
1282 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1285 tap->wr_antsignal = status->rssi + IPW_RSSI_TO_DBM;
1286 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1287 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1289 bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m);
1292 if (sc->flags & IPW_FLAG_SCANNING)
1293 ipw_fix_channel(sc, m);
1296 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1298 (void) ieee80211_input(ni, m, status->rssi, -95, 0);
1299 ieee80211_free_node(ni);
1301 (void) ieee80211_input_all(ic, m, status->rssi, -95, 0);
1304 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1308 ipw_rx_intr(struct ipw_softc *sc)
1310 struct ipw_status *status;
1311 struct ipw_soft_bd *sbd;
1312 struct ipw_soft_buf *sbuf;
1315 if (!(sc->flags & IPW_FLAG_FW_INITED))
1318 r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1320 bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1322 for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1323 status = &sc->status_list[i];
1324 sbd = &sc->srbd_list[i];
1327 switch (le16toh(status->code) & 0xf) {
1328 case IPW_STATUS_CODE_COMMAND:
1329 ipw_rx_cmd_intr(sc, sbuf);
1332 case IPW_STATUS_CODE_NEWSTATE:
1333 ipw_rx_newstate_intr(sc, sbuf);
1336 case IPW_STATUS_CODE_DATA_802_3:
1337 case IPW_STATUS_CODE_DATA_802_11:
1338 ipw_rx_data_intr(sc, status, sbd, sbuf);
1341 case IPW_STATUS_CODE_NOTIFICATION:
1342 DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1343 le32toh(status->len), status->flags));
1344 /* XXX maybe drive state machine AUTH->ASSOC? */
1348 device_printf(sc->sc_dev, "unexpected status code %u\n",
1349 le16toh(status->code));
1352 /* firmware was killed, stop processing received frames */
1353 if (!(sc->flags & IPW_FLAG_FW_INITED))
1359 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1361 /* kick the firmware */
1362 sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1363 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1367 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1369 struct ipw_soft_hdr *shdr;
1370 struct ipw_soft_buf *sbuf;
1372 switch (sbd->type) {
1373 case IPW_SBD_TYPE_COMMAND:
1374 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1375 BUS_DMASYNC_POSTWRITE);
1376 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1379 case IPW_SBD_TYPE_HEADER:
1381 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1382 bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1383 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1386 case IPW_SBD_TYPE_DATA:
1388 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1389 BUS_DMASYNC_POSTWRITE);
1390 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1391 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1393 if (sbuf->m->m_flags & M_TXCB)
1394 ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/);
1396 ieee80211_free_node(sbuf->ni);
1398 sc->sc_tx_timer = 0;
1402 sbd->type = IPW_SBD_TYPE_NOASSOC;
1406 ipw_tx_intr(struct ipw_softc *sc)
1408 struct ifnet *ifp = sc->sc_ifp;
1409 struct ipw_soft_bd *sbd;
1412 if (!(sc->flags & IPW_FLAG_FW_INITED))
1415 r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1417 for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1418 sbd = &sc->stbd_list[i];
1420 if (sbd->type == IPW_SBD_TYPE_DATA)
1423 ipw_release_sbd(sc, sbd);
1427 /* remember what the firmware has processed */
1428 sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1430 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1431 ipw_start_locked(ifp);
1437 struct ipw_softc *sc = arg;
1443 if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff) {
1448 /* disable interrupts */
1449 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1451 /* acknowledge all interrupts */
1452 CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1454 if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1455 device_printf(sc->sc_dev, "firmware error\n");
1456 taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1457 r = 0; /* don't process more interrupts */
1460 if (r & IPW_INTR_FW_INIT_DONE)
1463 if (r & IPW_INTR_RX_TRANSFER)
1466 if (r & IPW_INTR_TX_TRANSFER)
1469 /* re-enable interrupts */
1470 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1476 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1481 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1483 *(bus_addr_t *)arg = segs[0].ds_addr;
1487 ipw_cmdname(int cmd)
1489 #define N(a) (sizeof(a) / sizeof(a[0]))
1490 static const struct {
1494 { IPW_CMD_ADD_MULTICAST, "ADD_MULTICAST" },
1495 { IPW_CMD_BROADCAST_SCAN, "BROADCAST_SCAN" },
1496 { IPW_CMD_DISABLE, "DISABLE" },
1497 { IPW_CMD_DISABLE_PHY, "DISABLE_PHY" },
1498 { IPW_CMD_ENABLE, "ENABLE" },
1499 { IPW_CMD_PREPARE_POWER_DOWN, "PREPARE_POWER_DOWN" },
1500 { IPW_CMD_SET_BASIC_TX_RATES, "SET_BASIC_TX_RATES" },
1501 { IPW_CMD_SET_BEACON_INTERVAL, "SET_BEACON_INTERVAL" },
1502 { IPW_CMD_SET_CHANNEL, "SET_CHANNEL" },
1503 { IPW_CMD_SET_CONFIGURATION, "SET_CONFIGURATION" },
1504 { IPW_CMD_SET_DESIRED_BSSID, "SET_DESIRED_BSSID" },
1505 { IPW_CMD_SET_ESSID, "SET_ESSID" },
1506 { IPW_CMD_SET_FRAG_THRESHOLD, "SET_FRAG_THRESHOLD" },
1507 { IPW_CMD_SET_MAC_ADDRESS, "SET_MAC_ADDRESS" },
1508 { IPW_CMD_SET_MANDATORY_BSSID, "SET_MANDATORY_BSSID" },
1509 { IPW_CMD_SET_MODE, "SET_MODE" },
1510 { IPW_CMD_SET_MSDU_TX_RATES, "SET_MSDU_TX_RATES" },
1511 { IPW_CMD_SET_POWER_MODE, "SET_POWER_MODE" },
1512 { IPW_CMD_SET_RTS_THRESHOLD, "SET_RTS_THRESHOLD" },
1513 { IPW_CMD_SET_SCAN_OPTIONS, "SET_SCAN_OPTIONS" },
1514 { IPW_CMD_SET_SECURITY_INFO, "SET_SECURITY_INFO" },
1515 { IPW_CMD_SET_TX_POWER_INDEX, "SET_TX_POWER_INDEX" },
1516 { IPW_CMD_SET_TX_RATES, "SET_TX_RATES" },
1517 { IPW_CMD_SET_WEP_FLAGS, "SET_WEP_FLAGS" },
1518 { IPW_CMD_SET_WEP_KEY, "SET_WEP_KEY" },
1519 { IPW_CMD_SET_WEP_KEY_INDEX, "SET_WEP_KEY_INDEX" },
1520 { IPW_CMD_SET_WPA_IE, "SET_WPA_IE" },
1523 static char buf[12];
1526 for (i = 0; i < N(cmds); i++)
1527 if (cmds[i].cmd == cmd)
1528 return cmds[i].name;
1529 snprintf(buf, sizeof(buf), "%u", cmd);
1535 * Send a command to the firmware and wait for the acknowledgement.
1538 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1540 struct ipw_soft_bd *sbd;
1541 bus_addr_t physaddr;
1544 IPW_LOCK_ASSERT(sc);
1546 if (sc->flags & IPW_FLAG_BUSY) {
1547 device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1548 __func__, ipw_cmdname(type));
1551 sc->flags |= IPW_FLAG_BUSY;
1553 sbd = &sc->stbd_list[sc->txcur];
1555 error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1556 sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1558 device_printf(sc->sc_dev, "could not map command DMA memory\n");
1559 sc->flags &= ~IPW_FLAG_BUSY;
1563 sc->cmd.type = htole32(type);
1564 sc->cmd.subtype = 0;
1565 sc->cmd.len = htole32(len);
1567 memcpy(sc->cmd.data, data, len);
1569 sbd->type = IPW_SBD_TYPE_COMMAND;
1570 sbd->bd->physaddr = htole32(physaddr);
1571 sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1573 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1574 IPW_BD_FLAG_TX_LAST_FRAGMENT;
1576 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1577 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1580 if (ipw_debug >= 4) {
1581 printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1583 /* Print the data buffer in the higher debug level */
1584 if (ipw_debug >= 9 && len > 0) {
1585 printf(" data: 0x");
1586 for (int i = 1; i <= len; i++)
1587 printf("%1D", (u_char *)data + len - i, "");
1595 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1596 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1598 /* wait at most one second for command to complete */
1599 error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1601 device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1602 __func__, ipw_cmdname(type), error);
1603 sc->flags &= ~IPW_FLAG_BUSY;
1610 ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1612 struct ipw_softc *sc = ifp->if_softc;
1613 struct ieee80211com *ic = ifp->if_l2com;
1614 struct ieee80211_frame *wh;
1615 struct ipw_soft_bd *sbd;
1616 struct ipw_soft_hdr *shdr;
1617 struct ipw_soft_buf *sbuf;
1618 struct ieee80211_key *k;
1620 bus_dma_segment_t segs[IPW_MAX_NSEG];
1621 bus_addr_t physaddr;
1622 int nsegs, error, i;
1624 wh = mtod(m0, struct ieee80211_frame *);
1626 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1627 k = ieee80211_crypto_encap(ni, m0);
1632 /* packet header may have moved, reset our local pointer */
1633 wh = mtod(m0, struct ieee80211_frame *);
1636 if (bpf_peers_present(ifp->if_bpf)) {
1637 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1640 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1641 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1643 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0);
1646 shdr = SLIST_FIRST(&sc->free_shdr);
1647 sbuf = SLIST_FIRST(&sc->free_sbuf);
1648 KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1650 shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1651 shdr->hdr.subtype = 0;
1652 shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0;
1653 shdr->hdr.encrypt = 0;
1654 shdr->hdr.keyidx = 0;
1655 shdr->hdr.keysz = 0;
1656 shdr->hdr.fragmentsz = 0;
1657 IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1658 if (ic->ic_opmode == IEEE80211_M_STA)
1659 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1661 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1663 /* trim IEEE802.11 header */
1664 m_adj(m0, sizeof (struct ieee80211_frame));
1666 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1668 if (error != 0 && error != EFBIG) {
1669 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1675 mnew = m_defrag(m0, M_DONTWAIT);
1677 device_printf(sc->sc_dev,
1678 "could not defragment mbuf\n");
1684 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1687 device_printf(sc->sc_dev,
1688 "could not map mbuf (error %d)\n", error);
1694 error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1695 sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1697 device_printf(sc->sc_dev, "could not map header DMA memory\n");
1698 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1703 SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1704 SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1706 sbd = &sc->stbd_list[sc->txcur];
1707 sbd->type = IPW_SBD_TYPE_HEADER;
1709 sbd->bd->physaddr = htole32(physaddr);
1710 sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1711 sbd->bd->nfrag = 1 + nsegs;
1712 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1713 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1715 DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1716 shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1717 shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1721 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1726 for (i = 0; i < nsegs; i++) {
1727 sbd = &sc->stbd_list[sc->txcur];
1729 sbd->bd->physaddr = htole32(segs[i].ds_addr);
1730 sbd->bd->len = htole32(segs[i].ds_len);
1732 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1733 if (i == nsegs - 1) {
1734 sbd->type = IPW_SBD_TYPE_DATA;
1736 sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1738 sbd->type = IPW_SBD_TYPE_NOASSOC;
1739 sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1742 DPRINTFN(5, ("sending fragment (%d)\n", i));
1745 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1748 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1749 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1750 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1753 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1759 ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1760 const struct ieee80211_bpf_params *params)
1762 /* no support; just discard */
1764 ieee80211_free_node(ni);
1769 ipw_start(struct ifnet *ifp)
1771 struct ipw_softc *sc = ifp->if_softc;
1775 ipw_start_locked(ifp);
1780 ipw_start_locked(struct ifnet *ifp)
1782 struct ipw_softc *sc = ifp->if_softc;
1783 struct ieee80211_node *ni;
1786 IPW_LOCK_ASSERT(sc);
1789 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1792 if (sc->txfree < 1 + IPW_MAX_NSEG) {
1793 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1794 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1797 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1798 m = ieee80211_encap(ni, m);
1800 ieee80211_free_node(ni);
1803 if (ipw_tx_start(ifp, m, ni) != 0) {
1804 ieee80211_free_node(ni);
1808 /* start watchdog timer */
1809 sc->sc_tx_timer = 5;
1814 ipw_watchdog(void *arg)
1816 struct ipw_softc *sc = arg;
1817 struct ifnet *ifp = sc->sc_ifp;
1818 struct ieee80211com *ic = ifp->if_l2com;
1820 IPW_LOCK_ASSERT(sc);
1822 if (sc->sc_tx_timer > 0) {
1823 if (--sc->sc_tx_timer == 0) {
1824 if_printf(ifp, "device timeout\n");
1826 taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1829 if (sc->sc_scan_timer > 0) {
1830 if (--sc->sc_scan_timer == 0) {
1831 DPRINTFN(3, ("Scan timeout\n"));
1833 if (sc->flags & IPW_FLAG_SCANNING) {
1834 ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1835 sc->flags &= ~IPW_FLAG_SCANNING;
1839 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1840 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1844 ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1846 struct ipw_softc *sc = ifp->if_softc;
1847 struct ieee80211com *ic = ifp->if_l2com;
1848 struct ifreq *ifr = (struct ifreq *) data;
1849 int error = 0, startall = 0;
1855 if (ifp->if_flags & IFF_UP) {
1856 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1857 ipw_init_locked(sc);
1861 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1862 ipw_stop_locked(sc);
1866 ieee80211_start_all(ic);
1869 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1872 error = ether_ioctl(ifp, cmd, data);
1882 ipw_stop_master(struct ipw_softc *sc)
1887 /* disable interrupts */
1888 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1890 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1891 for (ntries = 0; ntries < 50; ntries++) {
1892 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1897 device_printf(sc->sc_dev, "timeout waiting for master\n");
1899 tmp = CSR_READ_4(sc, IPW_CSR_RST);
1900 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1902 /* Clear all flags except the following */
1903 sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1907 ipw_reset(struct ipw_softc *sc)
1912 ipw_stop_master(sc);
1914 /* move adapter to D0 state */
1915 tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1916 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1918 /* wait for clock stabilization */
1919 for (ntries = 0; ntries < 1000; ntries++) {
1920 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1927 tmp = CSR_READ_4(sc, IPW_CSR_RST);
1928 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1932 tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1933 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1939 ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1941 int ms = hz < 1000 ? 1 : hz/10;
1944 for (i = 0; i < 100; i++) {
1945 if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1947 error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1948 if (error == 0 || error != EWOULDBLOCK)
1951 DPRINTF(("%s: timeout waiting for %s\n",
1952 __func__, waitfor ? "disable" : "enable"));
1957 ipw_enable(struct ipw_softc *sc)
1961 if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1962 DPRINTF(("Enable adapter\n"));
1963 error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1966 error = ipw_waitfordisable(sc, 0);
1969 sc->flags |= IPW_FLAG_ENABLED;
1975 ipw_disable(struct ipw_softc *sc)
1979 if (sc->flags & IPW_FLAG_ENABLED) {
1980 DPRINTF(("Disable adapter\n"));
1981 error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1984 error = ipw_waitfordisable(sc, 1);
1987 sc->flags &= ~IPW_FLAG_ENABLED;
1993 * Upload the microcode to the device.
1996 ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
2000 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2001 CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2003 MEM_WRITE_2(sc, 0x220000, 0x0703);
2004 MEM_WRITE_2(sc, 0x220000, 0x0707);
2006 MEM_WRITE_1(sc, 0x210014, 0x72);
2007 MEM_WRITE_1(sc, 0x210014, 0x72);
2009 MEM_WRITE_1(sc, 0x210000, 0x40);
2010 MEM_WRITE_1(sc, 0x210000, 0x00);
2011 MEM_WRITE_1(sc, 0x210000, 0x40);
2013 MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
2015 MEM_WRITE_1(sc, 0x210000, 0x00);
2016 MEM_WRITE_1(sc, 0x210000, 0x00);
2017 MEM_WRITE_1(sc, 0x210000, 0x80);
2019 MEM_WRITE_2(sc, 0x220000, 0x0703);
2020 MEM_WRITE_2(sc, 0x220000, 0x0707);
2022 MEM_WRITE_1(sc, 0x210014, 0x72);
2023 MEM_WRITE_1(sc, 0x210014, 0x72);
2025 MEM_WRITE_1(sc, 0x210000, 0x00);
2026 MEM_WRITE_1(sc, 0x210000, 0x80);
2028 for (ntries = 0; ntries < 10; ntries++) {
2029 if (MEM_READ_1(sc, 0x210000) & 1)
2034 device_printf(sc->sc_dev,
2035 "timeout waiting for ucode to initialize\n");
2039 MEM_WRITE_4(sc, 0x3000e0, 0);
2044 /* set of macros to handle unaligned little endian data in firmware image */
2045 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2046 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
2048 ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
2050 const uint8_t *p, *end;
2058 dst = GETLE32(p); p += 4;
2059 len = GETLE16(p); p += 2;
2061 ipw_write_mem_1(sc, dst, p, len);
2065 CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
2068 /* enable interrupts */
2069 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
2071 /* kick the firmware */
2072 CSR_WRITE_4(sc, IPW_CSR_RST, 0);
2074 tmp = CSR_READ_4(sc, IPW_CSR_CTL);
2075 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
2077 /* wait at most one second for firmware initialization to complete */
2078 if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
2079 device_printf(sc->sc_dev, "timeout waiting for firmware "
2080 "initialization to complete\n");
2084 tmp = CSR_READ_4(sc, IPW_CSR_IO);
2085 CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2092 ipw_setwepkeys(struct ipw_softc *sc)
2094 struct ifnet *ifp = sc->sc_ifp;
2095 struct ieee80211com *ic = ifp->if_l2com;
2096 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2097 struct ipw_wep_key wepkey;
2098 struct ieee80211_key *wk;
2101 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2102 wk = &vap->iv_nw_keys[i];
2104 if (wk->wk_cipher == NULL ||
2105 wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2109 wepkey.len = wk->wk_keylen;
2110 memset(wepkey.key, 0, sizeof wepkey.key);
2111 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2112 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2114 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2123 ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2125 struct ipw_wpa_ie wpaie;
2127 memset(&wpaie, 0, sizeof(wpaie));
2128 wpaie.len = htole32(ielen);
2129 /* XXX verify length */
2130 memcpy(&wpaie.ie, ie, ielen);
2131 DPRINTF(("Setting WPA IE\n"));
2132 return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2136 ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2138 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2140 if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2141 DPRINTF(("Setting mandatory BSSID to null\n"));
2142 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2144 DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2145 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2146 bssid, IEEE80211_ADDR_LEN);
2151 ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2155 * A bug in the firmware breaks the ``don't associate''
2156 * bit in the scan options command. To compensate for
2157 * this install a bogus ssid when no ssid is specified
2158 * so the firmware won't try to associate.
2160 DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2161 return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2162 "\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2163 "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2164 "\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2165 "\x3c\x3d", IEEE80211_NWID_LEN);
2168 if (ipw_debug > 0) {
2169 printf("Setting ESSID to ");
2170 ieee80211_print_essid(ssid, ssidlen);
2174 return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2179 ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2181 struct ipw_scan_options opts;
2183 DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2184 opts.channels = htole32(chanmask);
2185 opts.flags = htole32(flags);
2186 return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2190 * Handler for sc_scan_task. This is a simple wrapper around ipw_scan().
2193 ipw_scan_task(void *context, int pending)
2195 struct ipw_softc *sc = context;
2204 ipw_scan(struct ipw_softc *sc)
2209 DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2211 if (sc->flags & IPW_FLAG_SCANNING)
2213 sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2215 /* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2216 error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2221 * Setup null/bogus ssid so firmware doesn't use any previous
2222 * ssid to try and associate. This is because the ``don't
2223 * associate'' option bit is broken (sigh).
2225 error = ipw_setssid(sc, NULL, 0);
2230 * NB: the adapter may be disabled on association lost;
2231 * if so just re-enable it to kick off scanning.
2233 DPRINTF(("Starting scan\n"));
2234 sc->sc_scan_timer = 3;
2235 if (sc->flags & IPW_FLAG_ENABLED) {
2236 params = 0; /* XXX? */
2237 error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2238 ¶ms, sizeof(params));
2240 error = ipw_enable(sc);
2243 DPRINTF(("Scan failed\n"));
2244 sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2250 ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2252 struct ifnet *ifp = sc->sc_ifp;
2253 struct ieee80211com *ic = ifp->if_l2com;
2257 data = htole32(ieee80211_chan2ieee(ic, chan));
2258 DPRINTF(("Setting channel to %u\n", le32toh(data)));
2259 error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2261 ipw_setcurchan(sc, chan);
2266 ipw_assoc_task(void *context, int pending)
2268 struct ieee80211vap *vap = context;
2269 struct ifnet *ifp = vap->iv_ic->ic_ifp;
2270 struct ieee80211com *ic = ifp->if_l2com;
2271 struct ipw_softc *sc = ifp->if_softc;
2272 struct ieee80211_node *ni = vap->iv_bss;
2273 struct ipw_security security;
2279 error = ipw_disable(sc);
2283 memset(&security, 0, sizeof security);
2284 security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2285 IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2286 security.ciphers = htole32(IPW_CIPHER_NONE);
2287 DPRINTF(("Setting authmode to %u\n", security.authmode));
2288 error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2293 data = htole32(vap->iv_rtsthreshold);
2294 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2295 error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2299 data = htole32(vap->iv_fragthreshold);
2300 DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2301 error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2305 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2306 error = ipw_setwepkeys(sc);
2310 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2311 data = htole32(vap->iv_def_txkey);
2312 DPRINTF(("Setting wep tx key index to %u\n",
2314 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2321 data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2322 DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2323 error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2327 error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2331 error = ipw_setbssid(sc, ni->ni_bssid);
2335 if (vap->iv_appie_assocreq != NULL) {
2336 struct ieee80211_appie *ie = vap->iv_appie_assocreq;
2337 error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2341 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2342 error = ipw_setchannel(sc, ni->ni_chan);
2347 /* lock scan to ap's channel and enable associate */
2348 error = ipw_setscanopts(sc,
2349 1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2353 error = ipw_enable(sc); /* finally, enable adapter */
2355 sc->flags |= IPW_FLAG_ASSOCIATING;
2361 ipw_disassoc_task(void *context, int pending)
2363 struct ieee80211vap *vap = context;
2364 struct ifnet *ifp = vap->iv_ic->ic_ifp;
2365 struct ieee80211_node *ni = vap->iv_bss;
2366 struct ipw_softc *sc = ifp->if_softc;
2370 DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2372 * NB: don't try to do this if ipw_stop_master has
2373 * shutdown the firmware and disabled interrupts.
2375 if (sc->flags & IPW_FLAG_FW_INITED) {
2376 sc->flags &= ~IPW_FLAG_ASSOCIATED;
2378 * NB: firmware currently ignores bssid parameter, but
2379 * supply it in case this changes (follow linux driver).
2381 (void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2382 ni->ni_bssid, IEEE80211_ADDR_LEN);
2388 * Handler for sc_init_task. This is a simple wrapper around ipw_init().
2389 * It is called on firmware panics or on watchdog timeouts.
2392 ipw_init_task(void *context, int pending)
2398 ipw_init(void *priv)
2400 struct ipw_softc *sc = priv;
2401 struct ifnet *ifp = sc->sc_ifp;
2402 struct ieee80211com *ic = ifp->if_l2com;
2406 ipw_init_locked(sc);
2409 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2410 ieee80211_start_all(ic); /* start all vap's */
2414 ipw_init_locked(struct ipw_softc *sc)
2416 struct ifnet *ifp = sc->sc_ifp;
2417 struct ieee80211com *ic = ifp->if_l2com;
2418 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2419 const struct firmware *fp;
2420 const struct ipw_firmware_hdr *hdr;
2423 IPW_LOCK_ASSERT(sc);
2425 DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2426 ieee80211_state_name[vap->iv_state], sc->flags));
2429 * Avoid re-entrant calls. We need to release the mutex in ipw_init()
2430 * when loading the firmware and we don't want to be called during this
2433 if (sc->flags & IPW_FLAG_INIT_LOCKED)
2435 sc->flags |= IPW_FLAG_INIT_LOCKED;
2437 ipw_stop_locked(sc);
2439 if (ipw_reset(sc) != 0) {
2440 device_printf(sc->sc_dev, "could not reset adapter\n");
2444 if (sc->sc_firmware == NULL) {
2445 device_printf(sc->sc_dev, "no firmware\n");
2448 /* NB: consistency already checked on load */
2449 fp = sc->sc_firmware;
2450 hdr = (const struct ipw_firmware_hdr *)fp->data;
2452 DPRINTF(("Loading firmware image '%s'\n", fp->name));
2453 fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2454 if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2455 device_printf(sc->sc_dev, "could not load microcode\n");
2459 ipw_stop_master(sc);
2462 * Setup tx, rx and status rings.
2464 sc->txold = IPW_NTBD - 1;
2466 sc->txfree = IPW_NTBD - 2;
2467 sc->rxcur = IPW_NRBD - 1;
2469 CSR_WRITE_4(sc, IPW_CSR_TX_BASE, sc->tbd_phys);
2470 CSR_WRITE_4(sc, IPW_CSR_TX_SIZE, IPW_NTBD);
2471 CSR_WRITE_4(sc, IPW_CSR_TX_READ, 0);
2472 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2474 CSR_WRITE_4(sc, IPW_CSR_RX_BASE, sc->rbd_phys);
2475 CSR_WRITE_4(sc, IPW_CSR_RX_SIZE, IPW_NRBD);
2476 CSR_WRITE_4(sc, IPW_CSR_RX_READ, 0);
2477 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2479 CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2481 fw = (const char *)fp->data + sizeof *hdr;
2482 if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2483 device_printf(sc->sc_dev, "could not load firmware\n");
2487 sc->flags |= IPW_FLAG_FW_INITED;
2489 /* retrieve information tables base addresses */
2490 sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2491 sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2493 ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2495 if (ipw_config(sc) != 0) {
2496 device_printf(sc->sc_dev, "device configuration failed\n");
2500 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2501 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2502 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2504 sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2508 ipw_stop_locked(sc);
2509 sc->flags &=~ IPW_FLAG_INIT_LOCKED;
2513 ipw_config(struct ipw_softc *sc)
2515 struct ifnet *ifp = sc->sc_ifp;
2516 struct ieee80211com *ic = ifp->if_l2com;
2517 struct ipw_configuration config;
2521 error = ipw_disable(sc);
2525 switch (ic->ic_opmode) {
2526 case IEEE80211_M_STA:
2527 case IEEE80211_M_HOSTAP:
2528 case IEEE80211_M_WDS: /* XXX */
2529 data = htole32(IPW_MODE_BSS);
2531 case IEEE80211_M_IBSS:
2532 case IEEE80211_M_AHDEMO:
2533 data = htole32(IPW_MODE_IBSS);
2535 case IEEE80211_M_MONITOR:
2536 data = htole32(IPW_MODE_MONITOR);
2539 DPRINTF(("Setting mode to %u\n", le32toh(data)));
2540 error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2544 if (ic->ic_opmode == IEEE80211_M_IBSS ||
2545 ic->ic_opmode == IEEE80211_M_MONITOR) {
2546 error = ipw_setchannel(sc, ic->ic_curchan);
2551 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2552 return ipw_enable(sc);
2554 config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2555 IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2556 if (ic->ic_opmode == IEEE80211_M_IBSS)
2557 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2558 if (ifp->if_flags & IFF_PROMISC)
2559 config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2560 config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2561 config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2562 DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2563 error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2567 data = htole32(0x3); /* 1, 2 */
2568 DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2569 error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2573 /* NB: use the same rate set */
2574 DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2575 error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2579 data = htole32(0xf); /* 1, 2, 5.5, 11 */
2580 DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2581 error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2585 data = htole32(IPW_POWER_MODE_CAM);
2586 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2587 error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2591 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2592 data = htole32(32); /* default value */
2593 DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2594 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2604 ipw_stop(void *priv)
2606 struct ipw_softc *sc = priv;
2610 ipw_stop_locked(sc);
2615 ipw_stop_locked(struct ipw_softc *sc)
2617 struct ifnet *ifp = sc->sc_ifp;
2620 IPW_LOCK_ASSERT(sc);
2622 callout_stop(&sc->sc_wdtimer);
2623 ipw_stop_master(sc);
2625 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2628 * Release tx buffers.
2630 for (i = 0; i < IPW_NTBD; i++)
2631 ipw_release_sbd(sc, &sc->stbd_list[i]);
2633 sc->sc_tx_timer = 0;
2634 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2638 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2640 struct ipw_softc *sc = arg1;
2641 uint32_t i, size, buf[256];
2643 memset(buf, 0, sizeof buf);
2645 if (!(sc->flags & IPW_FLAG_FW_INITED))
2646 return SYSCTL_OUT(req, buf, sizeof buf);
2648 CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2650 size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2651 for (i = 1; i < size; i++)
2652 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2654 return SYSCTL_OUT(req, buf, size);
2658 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2660 struct ipw_softc *sc = arg1;
2663 val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2664 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2666 return SYSCTL_OUT(req, &val, sizeof val);
2670 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
2672 return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2676 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2678 MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2683 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2685 uint32_t addr, info;
2686 uint16_t count, size;
2689 /* addr[4] + count[2] + size[2] */
2690 addr = MEM_READ_4(sc, sc->table2_base + off);
2691 info = MEM_READ_4(sc, sc->table2_base + off + 4);
2694 size = info & 0xffff;
2695 total = count * size;
2703 ipw_read_mem_1(sc, addr, buf, total);
2709 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2712 for (; count > 0; offset++, datap++, count--) {
2713 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2714 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2720 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2723 for (; count > 0; offset++, datap++, count--) {
2724 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2725 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2730 ipw_scan_start(struct ieee80211com *ic)
2732 struct ifnet *ifp = ic->ic_ifp;
2733 struct ipw_softc *sc = ifp->if_softc;
2737 if (!(sc->flags & IPW_FLAG_SCANNING))
2738 taskqueue_enqueue(taskqueue_swi, &sc->sc_scan_task);
2743 ipw_set_channel(struct ieee80211com *ic)
2745 struct ifnet *ifp = ic->ic_ifp;
2746 struct ipw_softc *sc = ifp->if_softc;
2750 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2752 ipw_setchannel(sc, ic->ic_curchan);
2759 ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2761 /* NB: all channels are scanned at once */
2765 ipw_scan_mindwell(struct ieee80211_scan_state *ss)
2767 /* NB: don't try to abort scan; wait for firmware to finish */
2771 ipw_scan_end(struct ieee80211com *ic)
2773 struct ifnet *ifp = ic->ic_ifp;
2774 struct ipw_softc *sc = ifp->if_softc;
2778 sc->flags &= ~IPW_FLAG_SCANNING;