2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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_var.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/if_types.h>
72 #include <net80211/ieee80211_var.h>
73 #include <net80211/ieee80211_radiotap.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/if_ether.h>
81 #include <dev/ipw/if_ipwreg.h>
82 #include <dev/ipw/if_ipwvar.h>
86 #define DPRINTF(x) do { if (ipw_debug > 0) printf x; } while (0)
87 #define DPRINTFN(n, x) do { if (ipw_debug >= (n)) printf x; } while (0)
89 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level");
92 #define DPRINTFN(n, x)
95 MODULE_DEPEND(ipw, pci, 1, 1, 1);
96 MODULE_DEPEND(ipw, wlan, 1, 1, 1);
97 MODULE_DEPEND(ipw, firmware, 1, 1, 1);
105 static const struct ipw_ident ipw_ident_table[] = {
106 { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" },
111 static struct ieee80211vap *ipw_vap_create(struct ieee80211com *,
112 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
113 const uint8_t [IEEE80211_ADDR_LEN],
114 const uint8_t [IEEE80211_ADDR_LEN]);
115 static void ipw_vap_delete(struct ieee80211vap *);
116 static int ipw_dma_alloc(struct ipw_softc *);
117 static void ipw_release(struct ipw_softc *);
118 static void ipw_media_status(struct ifnet *, struct ifmediareq *);
119 static int ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int);
120 static uint16_t ipw_read_prom_word(struct ipw_softc *, uint8_t);
121 static uint16_t ipw_read_chanmask(struct ipw_softc *);
122 static void ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *);
123 static void ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *);
124 static void ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *,
125 struct ipw_soft_bd *, struct ipw_soft_buf *);
126 static void ipw_rx_intr(struct ipw_softc *);
127 static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *);
128 static void ipw_tx_intr(struct ipw_softc *);
129 static void ipw_intr(void *);
130 static void ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int);
131 static const char * ipw_cmdname(int);
132 static int ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t);
133 static int ipw_tx_start(struct ipw_softc *, struct mbuf *,
134 struct ieee80211_node *);
135 static int ipw_raw_xmit(struct ieee80211_node *, struct mbuf *,
136 const struct ieee80211_bpf_params *);
137 static int ipw_transmit(struct ieee80211com *, struct mbuf *);
138 static void ipw_start(struct ipw_softc *);
139 static void ipw_watchdog(void *);
140 static void ipw_parent(struct ieee80211com *);
141 static void ipw_stop_master(struct ipw_softc *);
142 static int ipw_enable(struct ipw_softc *);
143 static int ipw_disable(struct ipw_softc *);
144 static int ipw_reset(struct ipw_softc *);
145 static int ipw_load_ucode(struct ipw_softc *, const char *, int);
146 static int ipw_load_firmware(struct ipw_softc *, const char *, int);
147 static int ipw_config(struct ipw_softc *);
148 static void ipw_assoc(struct ieee80211com *, struct ieee80211vap *);
149 static void ipw_disassoc(struct ieee80211com *, struct ieee80211vap *);
150 static void ipw_init_task(void *, int);
151 static void ipw_init(void *);
152 static void ipw_init_locked(struct ipw_softc *);
153 static void ipw_stop(void *);
154 static void ipw_stop_locked(struct ipw_softc *);
155 static int ipw_sysctl_stats(SYSCTL_HANDLER_ARGS);
156 static int ipw_sysctl_radio(SYSCTL_HANDLER_ARGS);
157 static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t);
158 static void ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t);
160 static int ipw_read_table2(struct ipw_softc *, uint32_t, void *,
162 static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *,
165 static void ipw_write_mem_1(struct ipw_softc *, bus_size_t,
166 const uint8_t *, bus_size_t);
167 static int ipw_scan(struct ipw_softc *);
168 static void ipw_scan_start(struct ieee80211com *);
169 static void ipw_scan_end(struct ieee80211com *);
170 static void ipw_getradiocaps(struct ieee80211com *, int, int *,
171 struct ieee80211_channel[]);
172 static void ipw_set_channel(struct ieee80211com *);
173 static void ipw_scan_curchan(struct ieee80211_scan_state *,
174 unsigned long maxdwell);
175 static void ipw_scan_mindwell(struct ieee80211_scan_state *);
177 static int ipw_probe(device_t);
178 static int ipw_attach(device_t);
179 static int ipw_detach(device_t);
180 static int ipw_shutdown(device_t);
181 static int ipw_suspend(device_t);
182 static int ipw_resume(device_t);
184 static device_method_t ipw_methods[] = {
185 /* Device interface */
186 DEVMETHOD(device_probe, ipw_probe),
187 DEVMETHOD(device_attach, ipw_attach),
188 DEVMETHOD(device_detach, ipw_detach),
189 DEVMETHOD(device_shutdown, ipw_shutdown),
190 DEVMETHOD(device_suspend, ipw_suspend),
191 DEVMETHOD(device_resume, ipw_resume),
196 static driver_t ipw_driver = {
199 sizeof (struct ipw_softc)
202 static devclass_t ipw_devclass;
204 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, NULL, NULL);
205 MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, ipw, ipw_ident_table,
206 nitems(ipw_ident_table) - 1);
208 MODULE_VERSION(ipw, 1);
211 ipw_probe(device_t dev)
213 const struct ipw_ident *ident;
215 for (ident = ipw_ident_table; ident->name != NULL; ident++) {
216 if (pci_get_vendor(dev) == ident->vendor &&
217 pci_get_device(dev) == ident->device) {
218 device_set_desc(dev, ident->name);
219 return (BUS_PROBE_DEFAULT);
225 /* Base Address Register */
227 ipw_attach(device_t dev)
229 struct ipw_softc *sc = device_get_softc(dev);
230 struct ieee80211com *ic = &sc->sc_ic;
236 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
237 MTX_DEF | MTX_RECURSE);
238 mbufq_init(&sc->sc_snd, ifqmaxlen);
239 TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc);
240 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0);
242 pci_write_config(dev, 0x41, 0, 1);
244 /* enable bus-mastering */
245 pci_enable_busmaster(dev);
248 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE);
249 if (sc->mem == NULL) {
250 device_printf(dev, "could not allocate memory resource\n");
254 sc->sc_st = rman_get_bustag(sc->mem);
255 sc->sc_sh = rman_get_bushandle(sc->mem);
258 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i,
259 RF_ACTIVE | RF_SHAREABLE);
260 if (sc->irq == NULL) {
261 device_printf(dev, "could not allocate interrupt resource\n");
265 if (ipw_reset(sc) != 0) {
266 device_printf(dev, "could not reset adapter\n");
270 if (ipw_dma_alloc(sc) != 0) {
271 device_printf(dev, "could not allocate DMA resources\n");
276 ic->ic_name = device_get_nameunit(dev);
277 ic->ic_opmode = IEEE80211_M_STA;
278 ic->ic_phytype = IEEE80211_T_DS;
280 /* set device capabilities */
282 IEEE80211_C_STA /* station mode supported */
283 | IEEE80211_C_IBSS /* IBSS mode supported */
284 | IEEE80211_C_MONITOR /* monitor mode supported */
285 | IEEE80211_C_PMGT /* power save supported */
286 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
287 | IEEE80211_C_WPA /* 802.11i supported */
290 /* read MAC address from EEPROM */
291 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0);
292 ic->ic_macaddr[0] = val >> 8;
293 ic->ic_macaddr[1] = val & 0xff;
294 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1);
295 ic->ic_macaddr[2] = val >> 8;
296 ic->ic_macaddr[3] = val & 0xff;
297 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2);
298 ic->ic_macaddr[4] = val >> 8;
299 ic->ic_macaddr[5] = val & 0xff;
301 sc->chanmask = ipw_read_chanmask(sc);
302 ipw_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
305 /* check support for radio transmitter switch in EEPROM */
306 if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8))
307 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH;
309 ieee80211_ifattach(ic);
310 ic->ic_scan_start = ipw_scan_start;
311 ic->ic_scan_end = ipw_scan_end;
312 ic->ic_getradiocaps = ipw_getradiocaps;
313 ic->ic_set_channel = ipw_set_channel;
314 ic->ic_scan_curchan = ipw_scan_curchan;
315 ic->ic_scan_mindwell = ipw_scan_mindwell;
316 ic->ic_raw_xmit = ipw_raw_xmit;
317 ic->ic_vap_create = ipw_vap_create;
318 ic->ic_vap_delete = ipw_vap_delete;
319 ic->ic_transmit = ipw_transmit;
320 ic->ic_parent = ipw_parent;
322 ieee80211_radiotap_attach(ic,
323 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
324 IPW_TX_RADIOTAP_PRESENT,
325 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
326 IPW_RX_RADIOTAP_PRESENT);
329 * Add a few sysctl knobs.
331 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
332 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio",
333 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
334 ipw_sysctl_radio, "I",
335 "radio transmitter switch state (0=off, 1=on)");
337 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
338 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats",
339 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, 0,
340 ipw_sysctl_stats, "S", "statistics");
343 * Hook our interrupt after all initialization is complete.
345 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
346 NULL, ipw_intr, sc, &sc->sc_ih);
348 device_printf(dev, "could not set up interrupt\n");
353 ieee80211_announce(ic);
359 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
361 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
364 mtx_destroy(&sc->sc_mtx);
369 ipw_detach(device_t dev)
371 struct ipw_softc *sc = device_get_softc(dev);
372 struct ieee80211com *ic = &sc->sc_ic;
374 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
376 ieee80211_draintask(ic, &sc->sc_init_task);
379 ieee80211_ifdetach(ic);
381 callout_drain(&sc->sc_wdtimer);
382 mbufq_drain(&sc->sc_snd);
386 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq);
388 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem),
391 if (sc->sc_firmware != NULL) {
392 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
393 sc->sc_firmware = NULL;
396 mtx_destroy(&sc->sc_mtx);
401 static struct ieee80211vap *
402 ipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
403 enum ieee80211_opmode opmode, int flags,
404 const uint8_t bssid[IEEE80211_ADDR_LEN],
405 const uint8_t mac[IEEE80211_ADDR_LEN])
407 struct ipw_softc *sc = ic->ic_softc;
409 struct ieee80211vap *vap;
410 const struct firmware *fp;
411 const struct ipw_firmware_hdr *hdr;
412 const char *imagename;
414 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
418 case IEEE80211_M_STA:
419 imagename = "ipw_bss";
421 case IEEE80211_M_IBSS:
422 imagename = "ipw_ibss";
424 case IEEE80211_M_MONITOR:
425 imagename = "ipw_monitor";
432 * Load firmware image using the firmware(9) subsystem. Doing
433 * this unlocked is ok since we're single-threaded by the
436 if (sc->sc_firmware == NULL ||
437 strcmp(sc->sc_firmware->name, imagename) != 0) {
438 if (sc->sc_firmware != NULL)
439 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
440 sc->sc_firmware = firmware_get(imagename);
442 if (sc->sc_firmware == NULL) {
443 device_printf(sc->sc_dev,
444 "could not load firmware image '%s'\n", imagename);
447 fp = sc->sc_firmware;
448 if (fp->datasize < sizeof *hdr) {
449 device_printf(sc->sc_dev,
450 "firmware image too short %zu\n", fp->datasize);
451 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
452 sc->sc_firmware = NULL;
455 hdr = (const struct ipw_firmware_hdr *)fp->data;
456 if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) +
457 le32toh(hdr->ucodesz)) {
458 device_printf(sc->sc_dev,
459 "firmware image too short %zu\n", fp->datasize);
460 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD);
461 sc->sc_firmware = NULL;
465 ivp = malloc(sizeof(struct ipw_vap), M_80211_VAP, M_WAITOK | M_ZERO);
468 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
469 /* override with driver methods */
470 ivp->newstate = vap->iv_newstate;
471 vap->iv_newstate = ipw_newstate;
474 ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status,
476 ic->ic_opmode = opmode;
481 ipw_vap_delete(struct ieee80211vap *vap)
483 struct ipw_vap *ivp = IPW_VAP(vap);
485 ieee80211_vap_detach(vap);
486 free(ivp, M_80211_VAP);
490 ipw_dma_alloc(struct ipw_softc *sc)
492 struct ipw_soft_bd *sbd;
493 struct ipw_soft_hdr *shdr;
494 struct ipw_soft_buf *sbuf;
499 * Allocate parent DMA tag for subsequent allocations.
501 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
502 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
503 BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED,
504 BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat);
506 device_printf(sc->sc_dev, "could not create parent DMA tag\n");
511 * Allocate and map tx ring.
513 error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
514 BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL,
515 NULL, &sc->tbd_dmat);
517 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
521 error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list,
522 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map);
524 device_printf(sc->sc_dev,
525 "could not allocate tx ring DMA memory\n");
529 error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list,
530 IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0);
532 device_printf(sc->sc_dev, "could not map tx ring DMA memory\n");
537 * Allocate and map rx ring.
539 error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
540 BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL,
541 NULL, &sc->rbd_dmat);
543 device_printf(sc->sc_dev, "could not create rx ring DMA tag\n");
547 error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list,
548 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map);
550 device_printf(sc->sc_dev,
551 "could not allocate rx ring DMA memory\n");
555 error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list,
556 IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0);
558 device_printf(sc->sc_dev, "could not map rx ring DMA memory\n");
563 * Allocate and map status ring.
565 error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT,
566 BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0,
567 NULL, NULL, &sc->status_dmat);
569 device_printf(sc->sc_dev,
570 "could not create status ring DMA tag\n");
574 error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list,
575 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map);
577 device_printf(sc->sc_dev,
578 "could not allocate status ring DMA memory\n");
582 error = bus_dmamap_load(sc->status_dmat, sc->status_map,
583 sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys,
586 device_printf(sc->sc_dev,
587 "could not map status ring DMA memory\n");
592 * Allocate command DMA map.
594 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
595 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1,
596 sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat);
598 device_printf(sc->sc_dev, "could not create command DMA tag\n");
602 error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map);
604 device_printf(sc->sc_dev,
605 "could not create command DMA map\n");
610 * Allocate headers DMA maps.
612 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
613 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1,
614 sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat);
616 device_printf(sc->sc_dev, "could not create header DMA tag\n");
620 SLIST_INIT(&sc->free_shdr);
621 for (i = 0; i < IPW_NDATA; i++) {
622 shdr = &sc->shdr_list[i];
623 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map);
625 device_printf(sc->sc_dev,
626 "could not create header DMA map\n");
629 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
633 * Allocate tx buffers DMA maps.
635 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
636 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0,
637 NULL, NULL, &sc->txbuf_dmat);
639 device_printf(sc->sc_dev, "could not create tx DMA tag\n");
643 SLIST_INIT(&sc->free_sbuf);
644 for (i = 0; i < IPW_NDATA; i++) {
645 sbuf = &sc->tx_sbuf_list[i];
646 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map);
648 device_printf(sc->sc_dev,
649 "could not create tx DMA map\n");
652 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
656 * Initialize tx ring.
658 for (i = 0; i < IPW_NTBD; i++) {
659 sbd = &sc->stbd_list[i];
660 sbd->bd = &sc->tbd_list[i];
661 sbd->type = IPW_SBD_TYPE_NOASSOC;
665 * Pre-allocate rx buffers and DMA maps.
667 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT,
668 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL,
669 NULL, &sc->rxbuf_dmat);
671 device_printf(sc->sc_dev, "could not create rx DMA tag\n");
675 for (i = 0; i < IPW_NRBD; i++) {
676 sbd = &sc->srbd_list[i];
677 sbuf = &sc->rx_sbuf_list[i];
678 sbd->bd = &sc->rbd_list[i];
680 sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
681 if (sbuf->m == NULL) {
682 device_printf(sc->sc_dev,
683 "could not allocate rx mbuf\n");
688 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map);
690 device_printf(sc->sc_dev,
691 "could not create rx DMA map\n");
695 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
696 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
699 device_printf(sc->sc_dev,
700 "could not map rx DMA memory\n");
704 sbd->type = IPW_SBD_TYPE_DATA;
706 sbd->bd->physaddr = htole32(physaddr);
707 sbd->bd->len = htole32(MCLBYTES);
710 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
714 fail: ipw_release(sc);
719 ipw_release(struct ipw_softc *sc)
721 struct ipw_soft_buf *sbuf;
724 if (sc->parent_dmat != NULL) {
725 bus_dma_tag_destroy(sc->parent_dmat);
728 if (sc->tbd_dmat != NULL) {
729 bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map);
730 bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, sc->tbd_map);
731 bus_dma_tag_destroy(sc->tbd_dmat);
734 if (sc->rbd_dmat != NULL) {
735 if (sc->rbd_list != NULL) {
736 bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map);
737 bus_dmamem_free(sc->rbd_dmat, sc->rbd_list,
740 bus_dma_tag_destroy(sc->rbd_dmat);
743 if (sc->status_dmat != NULL) {
744 if (sc->status_list != NULL) {
745 bus_dmamap_unload(sc->status_dmat, sc->status_map);
746 bus_dmamem_free(sc->status_dmat, sc->status_list,
749 bus_dma_tag_destroy(sc->status_dmat);
752 for (i = 0; i < IPW_NTBD; i++)
753 ipw_release_sbd(sc, &sc->stbd_list[i]);
755 if (sc->cmd_dmat != NULL) {
756 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map);
757 bus_dma_tag_destroy(sc->cmd_dmat);
760 if (sc->hdr_dmat != NULL) {
761 for (i = 0; i < IPW_NDATA; i++)
762 bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map);
763 bus_dma_tag_destroy(sc->hdr_dmat);
766 if (sc->txbuf_dmat != NULL) {
767 for (i = 0; i < IPW_NDATA; i++) {
768 bus_dmamap_destroy(sc->txbuf_dmat,
769 sc->tx_sbuf_list[i].map);
771 bus_dma_tag_destroy(sc->txbuf_dmat);
774 if (sc->rxbuf_dmat != NULL) {
775 for (i = 0; i < IPW_NRBD; i++) {
776 sbuf = &sc->rx_sbuf_list[i];
777 if (sbuf->m != NULL) {
778 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map,
779 BUS_DMASYNC_POSTREAD);
780 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
783 bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map);
785 bus_dma_tag_destroy(sc->rxbuf_dmat);
790 ipw_shutdown(device_t dev)
792 struct ipw_softc *sc = device_get_softc(dev);
800 ipw_suspend(device_t dev)
802 struct ipw_softc *sc = device_get_softc(dev);
803 struct ieee80211com *ic = &sc->sc_ic;
805 ieee80211_suspend_all(ic);
810 ipw_resume(device_t dev)
812 struct ipw_softc *sc = device_get_softc(dev);
813 struct ieee80211com *ic = &sc->sc_ic;
815 pci_write_config(dev, 0x41, 0, 1);
817 ieee80211_resume_all(ic);
822 ipw_cvtrate(int ipwrate)
825 case IPW_RATE_DS1: return 2;
826 case IPW_RATE_DS2: return 4;
827 case IPW_RATE_DS5: return 11;
828 case IPW_RATE_DS11: return 22;
834 * The firmware automatically adapts the transmit speed. We report its current
838 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr)
840 struct ieee80211vap *vap = ifp->if_softc;
841 struct ieee80211com *ic = vap->iv_ic;
842 struct ipw_softc *sc = ic->ic_softc;
844 /* read current transmission rate from adapter */
845 vap->iv_bss->ni_txrate = ipw_cvtrate(
846 ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf);
847 ieee80211_media_status(ifp, imr);
851 ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
853 struct ipw_vap *ivp = IPW_VAP(vap);
854 struct ieee80211com *ic = vap->iv_ic;
855 struct ipw_softc *sc = ic->ic_softc;
856 enum ieee80211_state ostate;
858 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
859 ieee80211_state_name[vap->iv_state],
860 ieee80211_state_name[nstate], sc->flags));
862 ostate = vap->iv_state;
863 IEEE80211_UNLOCK(ic);
866 case IEEE80211_S_RUN:
867 if (ic->ic_opmode == IEEE80211_M_IBSS) {
869 * XXX when joining an ibss network we are called
870 * with a SCAN -> RUN transition on scan complete.
871 * Use that to call ipw_assoc. On completing the
872 * join we are then called again with an AUTH -> RUN
873 * transition and we want to do nothing. This is
874 * all totally bogus and needs to be redone.
876 if (ostate == IEEE80211_S_SCAN)
881 case IEEE80211_S_INIT:
882 if (sc->flags & IPW_FLAG_ASSOCIATED)
883 ipw_disassoc(ic, vap);
886 case IEEE80211_S_AUTH:
888 * Move to ASSOC state after the ipw_assoc() call. Firmware
889 * takes care of authentication, after the call we'll receive
890 * only an assoc response which would otherwise be discared
891 * if we are still in AUTH state.
893 nstate = IEEE80211_S_ASSOC;
897 case IEEE80211_S_ASSOC:
899 * If we are not transitioning from AUTH then resend the
900 * association request.
902 if (ostate != IEEE80211_S_AUTH)
910 return ivp->newstate(vap, nstate, arg);
914 * Read 16 bits at address 'addr' from the serial EEPROM.
917 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr)
923 /* clock C once before the first command */
924 IPW_EEPROM_CTL(sc, 0);
925 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
926 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
927 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
929 /* write start bit (1) */
930 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
931 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
933 /* write READ opcode (10) */
934 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D);
935 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C);
936 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
937 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
939 /* write address A7-A0 */
940 for (n = 7; n >= 0; n--) {
941 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
942 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D));
943 IPW_EEPROM_CTL(sc, IPW_EEPROM_S |
944 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C);
947 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
949 /* read data Q15-Q0 */
951 for (n = 15; n >= 0; n--) {
952 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C);
953 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
954 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL);
955 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n;
958 IPW_EEPROM_CTL(sc, 0);
960 /* clear Chip Select and clock C */
961 IPW_EEPROM_CTL(sc, IPW_EEPROM_S);
962 IPW_EEPROM_CTL(sc, 0);
963 IPW_EEPROM_CTL(sc, IPW_EEPROM_C);
969 ipw_read_chanmask(struct ipw_softc *sc)
973 /* set supported .11b channels (read from EEPROM) */
974 if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0)
975 val = 0x7ff; /* default to channels 1-11 */
982 ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
986 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
988 cmd = mtod(sbuf->m, struct ipw_cmd *);
990 DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n",
991 ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type),
992 le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len),
993 le32toh(cmd->status)));
995 sc->flags &= ~IPW_FLAG_BUSY;
1000 ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf)
1002 #define IEEESTATE(vap) ieee80211_state_name[vap->iv_state]
1003 struct ieee80211com *ic = &sc->sc_ic;
1004 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1007 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1009 state = le32toh(*mtod(sbuf->m, uint32_t *));
1012 case IPW_STATE_ASSOCIATED:
1013 DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n",
1014 IEEESTATE(vap), sc->flags));
1015 /* XXX suppress state change in case the fw auto-associates */
1016 if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) {
1017 DPRINTF(("Unexpected association (%s, flags 0x%x)\n",
1018 IEEESTATE(vap), sc->flags));
1021 sc->flags &= ~IPW_FLAG_ASSOCIATING;
1022 sc->flags |= IPW_FLAG_ASSOCIATED;
1025 case IPW_STATE_SCANNING:
1026 DPRINTFN(3, ("Scanning (%s flags 0x%x)\n",
1027 IEEESTATE(vap), sc->flags));
1029 * NB: Check driver state for association on assoc
1030 * loss as the firmware will immediately start to
1031 * scan and we would treat it as a beacon miss if
1032 * we checked the 802.11 layer state.
1034 if (sc->flags & IPW_FLAG_ASSOCIATED) {
1036 /* XXX probably need to issue disassoc to fw */
1037 ieee80211_beacon_miss(ic);
1042 case IPW_STATE_SCAN_COMPLETE:
1044 * XXX For some reason scan requests generate scan
1045 * started + scan done events before any traffic is
1046 * received (e.g. probe response frames). We work
1047 * around this by marking the HACK flag and skipping
1048 * the first scan complete event.
1050 DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n",
1051 IEEESTATE(vap), sc->flags));
1052 if (sc->flags & IPW_FLAG_HACK) {
1053 sc->flags &= ~IPW_FLAG_HACK;
1056 if (sc->flags & IPW_FLAG_SCANNING) {
1058 ieee80211_scan_done(vap);
1060 sc->flags &= ~IPW_FLAG_SCANNING;
1061 sc->sc_scan_timer = 0;
1065 case IPW_STATE_ASSOCIATION_LOST:
1066 DPRINTFN(2, ("Association lost (%s flags 0x%x)\n",
1067 IEEESTATE(vap), sc->flags));
1068 sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1069 if (vap->iv_state == IEEE80211_S_RUN) {
1071 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1076 case IPW_STATE_DISABLED:
1077 /* XXX? is this right? */
1078 sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING |
1079 IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED);
1080 DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n",
1081 IEEESTATE(vap), sc->flags));
1084 case IPW_STATE_RADIO_DISABLED:
1085 device_printf(sc->sc_dev, "radio turned off\n");
1086 ieee80211_notify_radio(ic, 0);
1087 ipw_stop_locked(sc);
1088 /* XXX start polling thread to detect radio on */
1092 DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n",
1093 __func__, state, IEEESTATE(vap), sc->flags));
1100 * Set driver state for current channel.
1103 ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan)
1105 struct ieee80211com *ic = &sc->sc_ic;
1107 ic->ic_curchan = chan;
1108 ieee80211_radiotap_chan_change(ic);
1112 * XXX: Hack to set the current channel to the value advertised in beacons or
1113 * probe responses. Only used during AP detection.
1116 ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m)
1118 struct ieee80211com *ic = &sc->sc_ic;
1119 struct ieee80211_channel *c;
1120 struct ieee80211_frame *wh;
1122 uint8_t *frm, *efrm;
1124 wh = mtod(m, struct ieee80211_frame *);
1126 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1129 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1131 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1132 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1135 /* XXX use ieee80211_parse_beacon */
1136 frm = (uint8_t *)(wh + 1);
1137 efrm = mtod(m, uint8_t *) + m->m_len;
1139 frm += 12; /* skip tstamp, bintval and capinfo fields */
1140 while (frm < efrm) {
1141 if (*frm == IEEE80211_ELEMID_DSPARMS)
1142 #if IEEE80211_CHAN_MAX < 255
1143 if (frm[2] <= IEEE80211_CHAN_MAX)
1146 DPRINTF(("Fixing channel to %d\n", frm[2]));
1147 c = ieee80211_find_channel(ic,
1148 ieee80211_ieee2mhz(frm[2], 0),
1151 c = &ic->ic_channels[0];
1152 ipw_setcurchan(sc, c);
1160 ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status,
1161 struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf)
1163 struct epoch_tracker et;
1164 struct ieee80211com *ic = &sc->sc_ic;
1165 struct mbuf *mnew, *m;
1166 struct ieee80211_node *ni;
1167 bus_addr_t physaddr;
1171 DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len),
1174 if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) ||
1175 le32toh(status->len) > MCLBYTES)
1179 * Try to allocate a new mbuf for this ring element and load it before
1180 * processing the current mbuf. If the ring element cannot be loaded,
1181 * drop the received packet and reuse the old mbuf. In the unlikely
1182 * case that the old mbuf can't be reloaded either, explicitly panic.
1184 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1186 counter_u64_add(ic->ic_ierrors, 1);
1190 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD);
1191 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map);
1193 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *),
1194 MCLBYTES, ipw_dma_map_addr, &physaddr, 0);
1198 /* try to reload the old mbuf */
1199 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map,
1200 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr,
1203 /* very unlikely that it will fail... */
1204 panic("%s: could not load old rx mbuf",
1205 device_get_name(sc->sc_dev));
1207 counter_u64_add(ic->ic_ierrors, 1);
1212 * New mbuf successfully loaded, update Rx ring and continue
1217 sbd->bd->physaddr = htole32(physaddr);
1218 m->m_pkthdr.len = m->m_len = le32toh(status->len);
1220 rssi = status->rssi + IPW_RSSI_TO_DBM;
1222 if (ieee80211_radiotap_active(ic)) {
1223 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap;
1226 tap->wr_antsignal = rssi;
1227 tap->wr_antnoise = nf;
1230 if (sc->flags & IPW_FLAG_SCANNING)
1231 ipw_fix_channel(sc, m);
1234 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *));
1235 NET_EPOCH_ENTER(et);
1237 (void) ieee80211_input(ni, m, rssi - nf, nf);
1238 ieee80211_free_node(ni);
1240 (void) ieee80211_input_all(ic, m, rssi - nf, nf);
1244 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1248 ipw_rx_intr(struct ipw_softc *sc)
1250 struct ipw_status *status;
1251 struct ipw_soft_bd *sbd;
1252 struct ipw_soft_buf *sbuf;
1255 if (!(sc->flags & IPW_FLAG_FW_INITED))
1258 r = CSR_READ_4(sc, IPW_CSR_RX_READ);
1260 bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD);
1262 for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) {
1263 status = &sc->status_list[i];
1264 sbd = &sc->srbd_list[i];
1267 switch (le16toh(status->code) & 0xf) {
1268 case IPW_STATUS_CODE_COMMAND:
1269 ipw_rx_cmd_intr(sc, sbuf);
1272 case IPW_STATUS_CODE_NEWSTATE:
1273 ipw_rx_newstate_intr(sc, sbuf);
1276 case IPW_STATUS_CODE_DATA_802_3:
1277 case IPW_STATUS_CODE_DATA_802_11:
1278 ipw_rx_data_intr(sc, status, sbd, sbuf);
1281 case IPW_STATUS_CODE_NOTIFICATION:
1282 DPRINTFN(2, ("notification status, len %u flags 0x%x\n",
1283 le32toh(status->len), status->flags));
1284 /* XXX maybe drive state machine AUTH->ASSOC? */
1288 device_printf(sc->sc_dev, "unexpected status code %u\n",
1289 le16toh(status->code));
1292 /* firmware was killed, stop processing received frames */
1293 if (!(sc->flags & IPW_FLAG_FW_INITED))
1299 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE);
1301 /* kick the firmware */
1302 sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1;
1303 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
1307 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd)
1309 struct ipw_soft_hdr *shdr;
1310 struct ipw_soft_buf *sbuf;
1312 switch (sbd->type) {
1313 case IPW_SBD_TYPE_COMMAND:
1314 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map,
1315 BUS_DMASYNC_POSTWRITE);
1316 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map);
1319 case IPW_SBD_TYPE_HEADER:
1321 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE);
1322 bus_dmamap_unload(sc->hdr_dmat, shdr->map);
1323 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next);
1326 case IPW_SBD_TYPE_DATA:
1328 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map,
1329 BUS_DMASYNC_POSTWRITE);
1330 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1331 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next);
1333 ieee80211_tx_complete(sbuf->ni, sbuf->m, 0/*XXX*/);
1335 sc->sc_tx_timer = 0;
1339 sbd->type = IPW_SBD_TYPE_NOASSOC;
1343 ipw_tx_intr(struct ipw_softc *sc)
1345 struct ipw_soft_bd *sbd;
1348 if (!(sc->flags & IPW_FLAG_FW_INITED))
1351 r = CSR_READ_4(sc, IPW_CSR_TX_READ);
1353 for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) {
1354 sbd = &sc->stbd_list[i];
1355 ipw_release_sbd(sc, sbd);
1359 /* remember what the firmware has processed */
1360 sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1;
1366 ipw_fatal_error_intr(struct ipw_softc *sc)
1368 struct ieee80211com *ic = &sc->sc_ic;
1369 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1371 device_printf(sc->sc_dev, "firmware error\n");
1374 ieee80211_cancel_scan(vap);
1377 ieee80211_runtask(ic, &sc->sc_init_task);
1383 struct ipw_softc *sc = arg;
1388 r = CSR_READ_4(sc, IPW_CSR_INTR);
1389 if (r == 0 || r == 0xffffffff)
1392 /* disable interrupts */
1393 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1395 /* acknowledge all interrupts */
1396 CSR_WRITE_4(sc, IPW_CSR_INTR, r);
1398 if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) {
1399 ipw_fatal_error_intr(sc);
1403 if (r & IPW_INTR_FW_INIT_DONE)
1406 if (r & IPW_INTR_RX_TRANSFER)
1409 if (r & IPW_INTR_TX_TRANSFER)
1412 /* re-enable interrupts */
1413 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1419 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1424 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1426 *(bus_addr_t *)arg = segs[0].ds_addr;
1430 ipw_cmdname(int cmd)
1432 static const struct {
1436 { IPW_CMD_ADD_MULTICAST, "ADD_MULTICAST" },
1437 { IPW_CMD_BROADCAST_SCAN, "BROADCAST_SCAN" },
1438 { IPW_CMD_DISABLE, "DISABLE" },
1439 { IPW_CMD_DISABLE_PHY, "DISABLE_PHY" },
1440 { IPW_CMD_ENABLE, "ENABLE" },
1441 { IPW_CMD_PREPARE_POWER_DOWN, "PREPARE_POWER_DOWN" },
1442 { IPW_CMD_SET_BASIC_TX_RATES, "SET_BASIC_TX_RATES" },
1443 { IPW_CMD_SET_BEACON_INTERVAL, "SET_BEACON_INTERVAL" },
1444 { IPW_CMD_SET_CHANNEL, "SET_CHANNEL" },
1445 { IPW_CMD_SET_CONFIGURATION, "SET_CONFIGURATION" },
1446 { IPW_CMD_SET_DESIRED_BSSID, "SET_DESIRED_BSSID" },
1447 { IPW_CMD_SET_ESSID, "SET_ESSID" },
1448 { IPW_CMD_SET_FRAG_THRESHOLD, "SET_FRAG_THRESHOLD" },
1449 { IPW_CMD_SET_MAC_ADDRESS, "SET_MAC_ADDRESS" },
1450 { IPW_CMD_SET_MANDATORY_BSSID, "SET_MANDATORY_BSSID" },
1451 { IPW_CMD_SET_MODE, "SET_MODE" },
1452 { IPW_CMD_SET_MSDU_TX_RATES, "SET_MSDU_TX_RATES" },
1453 { IPW_CMD_SET_POWER_MODE, "SET_POWER_MODE" },
1454 { IPW_CMD_SET_RTS_THRESHOLD, "SET_RTS_THRESHOLD" },
1455 { IPW_CMD_SET_SCAN_OPTIONS, "SET_SCAN_OPTIONS" },
1456 { IPW_CMD_SET_SECURITY_INFO, "SET_SECURITY_INFO" },
1457 { IPW_CMD_SET_TX_POWER_INDEX, "SET_TX_POWER_INDEX" },
1458 { IPW_CMD_SET_TX_RATES, "SET_TX_RATES" },
1459 { IPW_CMD_SET_WEP_FLAGS, "SET_WEP_FLAGS" },
1460 { IPW_CMD_SET_WEP_KEY, "SET_WEP_KEY" },
1461 { IPW_CMD_SET_WEP_KEY_INDEX, "SET_WEP_KEY_INDEX" },
1462 { IPW_CMD_SET_WPA_IE, "SET_WPA_IE" },
1465 static char buf[12];
1468 for (i = 0; i < nitems(cmds); i++)
1469 if (cmds[i].cmd == cmd)
1470 return cmds[i].name;
1471 snprintf(buf, sizeof(buf), "%u", cmd);
1476 * Send a command to the firmware and wait for the acknowledgement.
1479 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len)
1481 struct ipw_soft_bd *sbd;
1482 bus_addr_t physaddr;
1485 IPW_LOCK_ASSERT(sc);
1487 if (sc->flags & IPW_FLAG_BUSY) {
1488 device_printf(sc->sc_dev, "%s: %s not sent, busy\n",
1489 __func__, ipw_cmdname(type));
1492 sc->flags |= IPW_FLAG_BUSY;
1494 sbd = &sc->stbd_list[sc->txcur];
1496 error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd,
1497 sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0);
1499 device_printf(sc->sc_dev, "could not map command DMA memory\n");
1500 sc->flags &= ~IPW_FLAG_BUSY;
1504 sc->cmd.type = htole32(type);
1505 sc->cmd.subtype = 0;
1506 sc->cmd.len = htole32(len);
1508 memcpy(sc->cmd.data, data, len);
1510 sbd->type = IPW_SBD_TYPE_COMMAND;
1511 sbd->bd->physaddr = htole32(physaddr);
1512 sbd->bd->len = htole32(sizeof (struct ipw_cmd));
1514 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND |
1515 IPW_BD_FLAG_TX_LAST_FRAGMENT;
1517 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE);
1518 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1521 if (ipw_debug >= 4) {
1522 printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type,
1524 /* Print the data buffer in the higher debug level */
1525 if (ipw_debug >= 9 && len > 0) {
1526 printf(" data: 0x");
1527 for (int i = 1; i <= len; i++)
1528 printf("%1D", (u_char *)data + len - i, "");
1536 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1537 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1539 /* wait at most one second for command to complete */
1540 error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz);
1542 device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n",
1543 __func__, ipw_cmdname(type), error);
1544 sc->flags &= ~IPW_FLAG_BUSY;
1551 ipw_tx_start(struct ipw_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1553 struct ieee80211com *ic = &sc->sc_ic;
1554 struct ieee80211vap *vap = ni->ni_vap;
1555 struct ieee80211_frame *wh;
1556 struct ipw_soft_bd *sbd;
1557 struct ipw_soft_hdr *shdr;
1558 struct ipw_soft_buf *sbuf;
1559 struct ieee80211_key *k;
1561 bus_dma_segment_t segs[IPW_MAX_NSEG];
1562 bus_addr_t physaddr;
1563 int nsegs, error, i;
1565 wh = mtod(m0, struct ieee80211_frame *);
1567 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1568 k = ieee80211_crypto_encap(ni, m0);
1573 /* packet header may have moved, reset our local pointer */
1574 wh = mtod(m0, struct ieee80211_frame *);
1577 if (ieee80211_radiotap_active_vap(vap)) {
1578 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap;
1582 ieee80211_radiotap_tx(vap, m0);
1585 shdr = SLIST_FIRST(&sc->free_shdr);
1586 sbuf = SLIST_FIRST(&sc->free_sbuf);
1587 KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool"));
1589 shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND);
1590 shdr->hdr.subtype = 0;
1591 shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0;
1592 shdr->hdr.encrypt = 0;
1593 shdr->hdr.keyidx = 0;
1594 shdr->hdr.keysz = 0;
1595 shdr->hdr.fragmentsz = 0;
1596 IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2);
1597 if (ic->ic_opmode == IEEE80211_M_STA)
1598 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3);
1600 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1);
1602 /* trim IEEE802.11 header */
1603 m_adj(m0, sizeof (struct ieee80211_frame));
1605 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs,
1607 if (error != 0 && error != EFBIG) {
1608 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1614 mnew = m_defrag(m0, M_NOWAIT);
1616 device_printf(sc->sc_dev,
1617 "could not defragment mbuf\n");
1623 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0,
1626 device_printf(sc->sc_dev,
1627 "could not map mbuf (error %d)\n", error);
1633 error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr,
1634 sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0);
1636 device_printf(sc->sc_dev, "could not map header DMA memory\n");
1637 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map);
1642 SLIST_REMOVE_HEAD(&sc->free_sbuf, next);
1643 SLIST_REMOVE_HEAD(&sc->free_shdr, next);
1645 sbd = &sc->stbd_list[sc->txcur];
1646 sbd->type = IPW_SBD_TYPE_HEADER;
1648 sbd->bd->physaddr = htole32(physaddr);
1649 sbd->bd->len = htole32(sizeof (struct ipw_hdr));
1650 sbd->bd->nfrag = 1 + nsegs;
1651 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 |
1652 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1654 DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n",
1655 shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted,
1656 shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr,
1660 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1665 for (i = 0; i < nsegs; i++) {
1666 sbd = &sc->stbd_list[sc->txcur];
1668 sbd->bd->physaddr = htole32(segs[i].ds_addr);
1669 sbd->bd->len = htole32(segs[i].ds_len);
1671 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3;
1672 if (i == nsegs - 1) {
1673 sbd->type = IPW_SBD_TYPE_DATA;
1675 sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT;
1677 sbd->type = IPW_SBD_TYPE_NOASSOC;
1678 sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT;
1681 DPRINTFN(5, ("sending fragment (%d)\n", i));
1684 sc->txcur = (sc->txcur + 1) % IPW_NTBD;
1687 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE);
1688 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE);
1689 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE);
1692 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
1698 ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1699 const struct ieee80211_bpf_params *params)
1701 /* no support; just discard */
1703 ieee80211_free_node(ni);
1708 ipw_transmit(struct ieee80211com *ic, struct mbuf *m)
1710 struct ipw_softc *sc = ic->ic_softc;
1714 if ((sc->flags & IPW_FLAG_RUNNING) == 0) {
1718 error = mbufq_enqueue(&sc->sc_snd, m);
1729 ipw_start(struct ipw_softc *sc)
1731 struct ieee80211_node *ni;
1734 IPW_LOCK_ASSERT(sc);
1736 while (sc->txfree >= 1 + IPW_MAX_NSEG &&
1737 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1738 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1739 if (ipw_tx_start(sc, m, ni) != 0) {
1740 if_inc_counter(ni->ni_vap->iv_ifp,
1741 IFCOUNTER_OERRORS, 1);
1742 ieee80211_free_node(ni);
1745 /* start watchdog timer */
1746 sc->sc_tx_timer = 5;
1751 ipw_watchdog(void *arg)
1753 struct ipw_softc *sc = arg;
1754 struct ieee80211com *ic = &sc->sc_ic;
1756 IPW_LOCK_ASSERT(sc);
1758 if (sc->sc_tx_timer > 0) {
1759 if (--sc->sc_tx_timer == 0) {
1760 device_printf(sc->sc_dev, "device timeout\n");
1761 counter_u64_add(ic->ic_oerrors, 1);
1762 taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task);
1765 if (sc->sc_scan_timer > 0) {
1766 if (--sc->sc_scan_timer == 0) {
1767 DPRINTFN(3, ("Scan timeout\n"));
1769 if (sc->flags & IPW_FLAG_SCANNING) {
1771 ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps));
1773 sc->flags &= ~IPW_FLAG_SCANNING;
1777 if (sc->flags & IPW_FLAG_RUNNING)
1778 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
1782 ipw_parent(struct ieee80211com *ic)
1784 struct ipw_softc *sc = ic->ic_softc;
1788 if (ic->ic_nrunning > 0) {
1789 if (!(sc->flags & IPW_FLAG_RUNNING)) {
1790 ipw_init_locked(sc);
1793 } else if (sc->flags & IPW_FLAG_RUNNING)
1794 ipw_stop_locked(sc);
1797 ieee80211_start_all(ic);
1801 ipw_stop_master(struct ipw_softc *sc)
1806 /* disable interrupts */
1807 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0);
1809 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER);
1810 for (ntries = 0; ntries < 50; ntries++) {
1811 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED)
1816 device_printf(sc->sc_dev, "timeout waiting for master\n");
1818 tmp = CSR_READ_4(sc, IPW_CSR_RST);
1819 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET);
1821 /* Clear all flags except the following */
1822 sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH;
1826 ipw_reset(struct ipw_softc *sc)
1831 ipw_stop_master(sc);
1833 /* move adapter to D0 state */
1834 tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1835 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1837 /* wait for clock stabilization */
1838 for (ntries = 0; ntries < 1000; ntries++) {
1839 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY)
1846 tmp = CSR_READ_4(sc, IPW_CSR_RST);
1847 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET);
1851 tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1852 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT);
1858 ipw_waitfordisable(struct ipw_softc *sc, int waitfor)
1860 int ms = hz < 1000 ? 1 : hz/10;
1863 for (i = 0; i < 100; i++) {
1864 if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor)
1866 error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms);
1867 if (error == 0 || error != EWOULDBLOCK)
1870 DPRINTF(("%s: timeout waiting for %s\n",
1871 __func__, waitfor ? "disable" : "enable"));
1876 ipw_enable(struct ipw_softc *sc)
1880 if ((sc->flags & IPW_FLAG_ENABLED) == 0) {
1881 DPRINTF(("Enable adapter\n"));
1882 error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0);
1885 error = ipw_waitfordisable(sc, 0);
1888 sc->flags |= IPW_FLAG_ENABLED;
1894 ipw_disable(struct ipw_softc *sc)
1898 if (sc->flags & IPW_FLAG_ENABLED) {
1899 DPRINTF(("Disable adapter\n"));
1900 error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0);
1903 error = ipw_waitfordisable(sc, 1);
1906 sc->flags &= ~IPW_FLAG_ENABLED;
1912 * Upload the microcode to the device.
1915 ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size)
1919 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1920 CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1922 MEM_WRITE_2(sc, 0x220000, 0x0703);
1923 MEM_WRITE_2(sc, 0x220000, 0x0707);
1925 MEM_WRITE_1(sc, 0x210014, 0x72);
1926 MEM_WRITE_1(sc, 0x210014, 0x72);
1928 MEM_WRITE_1(sc, 0x210000, 0x40);
1929 MEM_WRITE_1(sc, 0x210000, 0x00);
1930 MEM_WRITE_1(sc, 0x210000, 0x40);
1932 MEM_WRITE_MULTI_1(sc, 0x210010, uc, size);
1934 MEM_WRITE_1(sc, 0x210000, 0x00);
1935 MEM_WRITE_1(sc, 0x210000, 0x00);
1936 MEM_WRITE_1(sc, 0x210000, 0x80);
1938 MEM_WRITE_2(sc, 0x220000, 0x0703);
1939 MEM_WRITE_2(sc, 0x220000, 0x0707);
1941 MEM_WRITE_1(sc, 0x210014, 0x72);
1942 MEM_WRITE_1(sc, 0x210014, 0x72);
1944 MEM_WRITE_1(sc, 0x210000, 0x00);
1945 MEM_WRITE_1(sc, 0x210000, 0x80);
1947 for (ntries = 0; ntries < 10; ntries++) {
1948 if (MEM_READ_1(sc, 0x210000) & 1)
1953 device_printf(sc->sc_dev,
1954 "timeout waiting for ucode to initialize\n");
1958 MEM_WRITE_4(sc, 0x3000e0, 0);
1963 /* set of macros to handle unaligned little endian data in firmware image */
1964 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
1965 #define GETLE16(p) ((p)[0] | (p)[1] << 8)
1967 ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size)
1969 const uint8_t *p, *end;
1977 dst = GETLE32(p); p += 4;
1978 len = GETLE16(p); p += 2;
1980 ipw_write_mem_1(sc, dst, p, len);
1984 CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK |
1987 /* enable interrupts */
1988 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK);
1990 /* kick the firmware */
1991 CSR_WRITE_4(sc, IPW_CSR_RST, 0);
1993 tmp = CSR_READ_4(sc, IPW_CSR_CTL);
1994 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY);
1996 /* wait at most one second for firmware initialization to complete */
1997 if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) {
1998 device_printf(sc->sc_dev, "timeout waiting for firmware "
1999 "initialization to complete\n");
2003 tmp = CSR_READ_4(sc, IPW_CSR_IO);
2004 CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK |
2011 ipw_setwepkeys(struct ipw_softc *sc)
2013 struct ieee80211com *ic = &sc->sc_ic;
2014 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2015 struct ipw_wep_key wepkey;
2016 struct ieee80211_key *wk;
2019 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2020 wk = &vap->iv_nw_keys[i];
2022 if (wk->wk_cipher == NULL ||
2023 wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP)
2027 wepkey.len = wk->wk_keylen;
2028 memset(wepkey.key, 0, sizeof wepkey.key);
2029 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2030 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx,
2032 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey,
2041 ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen)
2043 struct ipw_wpa_ie wpaie;
2045 memset(&wpaie, 0, sizeof(wpaie));
2046 wpaie.len = htole32(ielen);
2047 /* XXX verify length */
2048 memcpy(&wpaie.ie, ie, ielen);
2049 DPRINTF(("Setting WPA IE\n"));
2050 return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie));
2054 ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid)
2056 static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
2058 if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) {
2059 DPRINTF(("Setting mandatory BSSID to null\n"));
2060 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0);
2062 DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":"));
2063 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID,
2064 bssid, IEEE80211_ADDR_LEN);
2069 ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen)
2073 * A bug in the firmware breaks the ``don't associate''
2074 * bit in the scan options command. To compensate for
2075 * this install a bogus ssid when no ssid is specified
2076 * so the firmware won't try to associate.
2078 DPRINTF(("Setting bogus ESSID to WAR firmware bug\n"));
2079 return ipw_cmd(sc, IPW_CMD_SET_ESSID,
2080 "\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27"
2081 "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31"
2082 "\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b"
2083 "\x3c\x3d", IEEE80211_NWID_LEN);
2086 if (ipw_debug > 0) {
2087 printf("Setting ESSID to ");
2088 ieee80211_print_essid(ssid, ssidlen);
2092 return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen);
2097 ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags)
2099 struct ipw_scan_options opts;
2101 DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags));
2102 opts.channels = htole32(chanmask);
2103 opts.flags = htole32(flags);
2104 return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts));
2108 ipw_scan(struct ipw_softc *sc)
2113 DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags));
2115 if (sc->flags & IPW_FLAG_SCANNING)
2117 sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK;
2119 /* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */
2120 error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE);
2125 * Setup null/bogus ssid so firmware doesn't use any previous
2126 * ssid to try and associate. This is because the ``don't
2127 * associate'' option bit is broken (sigh).
2129 error = ipw_setssid(sc, NULL, 0);
2134 * NB: the adapter may be disabled on association lost;
2135 * if so just re-enable it to kick off scanning.
2137 DPRINTF(("Starting scan\n"));
2138 sc->sc_scan_timer = 3;
2139 if (sc->flags & IPW_FLAG_ENABLED) {
2140 params = 0; /* XXX? */
2141 error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN,
2142 ¶ms, sizeof(params));
2144 error = ipw_enable(sc);
2147 DPRINTF(("Scan failed\n"));
2148 sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK);
2154 ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan)
2156 struct ieee80211com *ic = &sc->sc_ic;
2160 data = htole32(ieee80211_chan2ieee(ic, chan));
2161 DPRINTF(("Setting channel to %u\n", le32toh(data)));
2162 error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data);
2164 ipw_setcurchan(sc, chan);
2169 ipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2171 struct ipw_softc *sc = ic->ic_softc;
2172 struct ieee80211_node *ni = vap->iv_bss;
2173 struct ipw_security security;
2178 error = ipw_disable(sc);
2182 memset(&security, 0, sizeof security);
2183 security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ?
2184 IPW_AUTH_SHARED : IPW_AUTH_OPEN;
2185 security.ciphers = htole32(IPW_CIPHER_NONE);
2186 DPRINTF(("Setting authmode to %u\n", security.authmode));
2187 error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security,
2192 data = htole32(vap->iv_rtsthreshold);
2193 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2194 error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data);
2198 data = htole32(vap->iv_fragthreshold);
2199 DPRINTF(("Setting frag threshold to %u\n", le32toh(data)));
2200 error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data);
2204 if (vap->iv_flags & IEEE80211_F_PRIVACY) {
2205 error = ipw_setwepkeys(sc);
2209 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) {
2210 data = htole32(vap->iv_def_txkey);
2211 DPRINTF(("Setting wep tx key index to %u\n",
2213 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data,
2220 data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0);
2221 DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data)));
2222 error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data);
2226 error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen);
2230 error = ipw_setbssid(sc, ni->ni_bssid);
2234 if (vap->iv_appie_wpa != NULL) {
2235 struct ieee80211_appie *ie = vap->iv_appie_wpa;
2236 error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len);
2240 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2241 error = ipw_setchannel(sc, ni->ni_chan);
2246 /* lock scan to ap's channel and enable associate */
2247 error = ipw_setscanopts(sc,
2248 1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0);
2252 error = ipw_enable(sc); /* finally, enable adapter */
2254 sc->flags |= IPW_FLAG_ASSOCIATING;
2260 ipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap)
2262 struct ieee80211_node *ni = vap->iv_bss;
2263 struct ipw_softc *sc = ic->ic_softc;
2266 DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":"));
2268 * NB: don't try to do this if ipw_stop_master has
2269 * shutdown the firmware and disabled interrupts.
2271 if (sc->flags & IPW_FLAG_FW_INITED) {
2272 sc->flags &= ~IPW_FLAG_ASSOCIATED;
2274 * NB: firmware currently ignores bssid parameter, but
2275 * supply it in case this changes (follow linux driver).
2277 (void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE,
2278 ni->ni_bssid, IEEE80211_ADDR_LEN);
2284 * Handler for sc_init_task. This is a simple wrapper around ipw_init().
2285 * It is called on firmware panics or on watchdog timeouts.
2288 ipw_init_task(void *context, int pending)
2294 ipw_init(void *priv)
2296 struct ipw_softc *sc = priv;
2297 struct ieee80211com *ic = &sc->sc_ic;
2300 ipw_init_locked(sc);
2303 if (sc->flags & IPW_FLAG_RUNNING)
2304 ieee80211_start_all(ic); /* start all vap's */
2308 ipw_init_locked(struct ipw_softc *sc)
2310 struct ieee80211com *ic = &sc->sc_ic;
2311 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2312 const struct firmware *fp;
2313 const struct ipw_firmware_hdr *hdr;
2316 IPW_LOCK_ASSERT(sc);
2318 DPRINTF(("%s: state %s flags 0x%x\n", __func__,
2319 ieee80211_state_name[vap->iv_state], sc->flags));
2322 * Avoid re-entrant calls. We need to release the mutex in ipw_init()
2323 * when loading the firmware and we don't want to be called during this
2326 if (sc->flags & IPW_FLAG_INIT_LOCKED)
2328 sc->flags |= IPW_FLAG_INIT_LOCKED;
2330 ipw_stop_locked(sc);
2332 if (ipw_reset(sc) != 0) {
2333 device_printf(sc->sc_dev, "could not reset adapter\n");
2337 if (sc->sc_firmware == NULL) {
2338 device_printf(sc->sc_dev, "no firmware\n");
2341 /* NB: consistency already checked on load */
2342 fp = sc->sc_firmware;
2343 hdr = (const struct ipw_firmware_hdr *)fp->data;
2345 DPRINTF(("Loading firmware image '%s'\n", fp->name));
2346 fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz);
2347 if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) {
2348 device_printf(sc->sc_dev, "could not load microcode\n");
2352 ipw_stop_master(sc);
2355 * Setup tx, rx and status rings.
2357 sc->txold = IPW_NTBD - 1;
2359 sc->txfree = IPW_NTBD - 2;
2360 sc->rxcur = IPW_NRBD - 1;
2362 CSR_WRITE_4(sc, IPW_CSR_TX_BASE, sc->tbd_phys);
2363 CSR_WRITE_4(sc, IPW_CSR_TX_SIZE, IPW_NTBD);
2364 CSR_WRITE_4(sc, IPW_CSR_TX_READ, 0);
2365 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur);
2367 CSR_WRITE_4(sc, IPW_CSR_RX_BASE, sc->rbd_phys);
2368 CSR_WRITE_4(sc, IPW_CSR_RX_SIZE, IPW_NRBD);
2369 CSR_WRITE_4(sc, IPW_CSR_RX_READ, 0);
2370 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur);
2372 CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys);
2374 fw = (const char *)fp->data + sizeof *hdr;
2375 if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) {
2376 device_printf(sc->sc_dev, "could not load firmware\n");
2380 sc->flags |= IPW_FLAG_FW_INITED;
2382 /* retrieve information tables base addresses */
2383 sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE);
2384 sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE);
2386 ipw_write_table1(sc, IPW_INFO_LOCK, 0);
2388 if (ipw_config(sc) != 0) {
2389 device_printf(sc->sc_dev, "device configuration failed\n");
2393 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc);
2394 sc->flags |= IPW_FLAG_RUNNING;
2395 sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2399 ipw_stop_locked(sc);
2400 sc->flags &= ~IPW_FLAG_INIT_LOCKED;
2404 ipw_config(struct ipw_softc *sc)
2406 struct ieee80211com *ic = &sc->sc_ic;
2407 struct ipw_configuration config;
2411 error = ipw_disable(sc);
2415 switch (ic->ic_opmode) {
2416 case IEEE80211_M_STA:
2417 case IEEE80211_M_HOSTAP:
2418 case IEEE80211_M_WDS: /* XXX */
2419 data = htole32(IPW_MODE_BSS);
2421 case IEEE80211_M_IBSS:
2422 case IEEE80211_M_AHDEMO:
2423 data = htole32(IPW_MODE_IBSS);
2425 case IEEE80211_M_MONITOR:
2426 data = htole32(IPW_MODE_MONITOR);
2429 device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode);
2432 DPRINTF(("Setting mode to %u\n", le32toh(data)));
2433 error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data);
2437 if (ic->ic_opmode == IEEE80211_M_IBSS ||
2438 ic->ic_opmode == IEEE80211_M_MONITOR) {
2439 error = ipw_setchannel(sc, ic->ic_curchan);
2444 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2445 return ipw_enable(sc);
2447 config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK |
2448 IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE);
2449 if (ic->ic_opmode == IEEE80211_M_IBSS)
2450 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START);
2451 if (ic->ic_promisc > 0)
2452 config.flags |= htole32(IPW_CFG_PROMISCUOUS);
2453 config.bss_chan = htole32(0x3fff); /* channels 1-14 */
2454 config.ibss_chan = htole32(0x7ff); /* channels 1-11 */
2455 DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags)));
2456 error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config);
2460 data = htole32(0xf); /* 1, 2, 5.5, 11 */
2461 DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data)));
2462 error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data);
2466 /* Use the same rate set */
2467 DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data)));
2468 error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data);
2472 /* Use the same rate set */
2473 DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data)));
2474 error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data);
2478 data = htole32(IPW_POWER_MODE_CAM);
2479 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2480 error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data);
2484 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2485 data = htole32(32); /* default value */
2486 DPRINTF(("Setting tx power index to %u\n", le32toh(data)));
2487 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data,
2497 ipw_stop(void *priv)
2499 struct ipw_softc *sc = priv;
2502 ipw_stop_locked(sc);
2507 ipw_stop_locked(struct ipw_softc *sc)
2511 IPW_LOCK_ASSERT(sc);
2513 callout_stop(&sc->sc_wdtimer);
2514 ipw_stop_master(sc);
2516 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET);
2519 * Release tx buffers.
2521 for (i = 0; i < IPW_NTBD; i++)
2522 ipw_release_sbd(sc, &sc->stbd_list[i]);
2524 sc->sc_tx_timer = 0;
2525 sc->flags &= ~IPW_FLAG_RUNNING;
2529 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS)
2531 struct ipw_softc *sc = arg1;
2532 uint32_t i, size, buf[256];
2534 memset(buf, 0, sizeof buf);
2536 if (!(sc->flags & IPW_FLAG_FW_INITED))
2537 return SYSCTL_OUT(req, buf, sizeof buf);
2539 CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base);
2541 size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256);
2542 for (i = 1; i < size; i++)
2543 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA));
2545 return SYSCTL_OUT(req, buf, size);
2549 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS)
2551 struct ipw_softc *sc = arg1;
2554 val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) &&
2555 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED));
2557 return SYSCTL_OUT(req, &val, sizeof val);
2561 ipw_read_table1(struct ipw_softc *sc, uint32_t off)
2563 return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off));
2567 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info)
2569 MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info);
2574 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len)
2576 uint32_t addr, info;
2577 uint16_t count, size;
2580 /* addr[4] + count[2] + size[2] */
2581 addr = MEM_READ_4(sc, sc->table2_base + off);
2582 info = MEM_READ_4(sc, sc->table2_base + off + 4);
2585 size = info & 0xffff;
2586 total = count * size;
2594 ipw_read_mem_1(sc, addr, buf, total);
2600 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap,
2603 for (; count > 0; offset++, datap++, count--) {
2604 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2605 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3));
2611 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap,
2614 for (; count > 0; offset++, datap++, count--) {
2615 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3);
2616 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap);
2621 ipw_scan_start(struct ieee80211com *ic)
2623 struct ipw_softc *sc = ic->ic_softc;
2631 ipw_getradiocaps(struct ieee80211com *ic,
2632 int maxchans, int *nchans, struct ieee80211_channel chans[])
2634 struct ipw_softc *sc = ic->ic_softc;
2635 uint8_t bands[IEEE80211_MODE_BYTES];
2638 memset(bands, 0, sizeof(bands));
2639 setbit(bands, IEEE80211_MODE_11B);
2641 for (i = 1; i < 16; i++) {
2642 if (sc->chanmask & (1 << i)) {
2643 ieee80211_add_channel(chans, maxchans, nchans,
2651 ipw_set_channel(struct ieee80211com *ic)
2653 struct ipw_softc *sc = ic->ic_softc;
2656 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2658 ipw_setchannel(sc, ic->ic_curchan);
2665 ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
2667 /* NB: all channels are scanned at once */
2671 ipw_scan_mindwell(struct ieee80211_scan_state *ss)
2673 /* NB: don't try to abort scan; wait for firmware to finish */
2677 ipw_scan_end(struct ieee80211com *ic)
2679 struct ipw_softc *sc = ic->ic_softc;
2682 sc->flags &= ~IPW_FLAG_SCANNING;