2 * Copyright (c) 2006,2007
3 * Damien Bergamini <damien.bergamini@free.fr>
4 * Benjamin Close <Benjamin.Close@clearchain.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
23 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
25 * The 3945ABG network adapter doesn't use traditional hardware as
26 * many other adaptors do. Instead at run time the eeprom is set into a known
27 * state and told to load boot firmware. The boot firmware loads an init and a
28 * main binary firmware image into SRAM on the card via DMA.
29 * Once the firmware is loaded, the driver/hw then
30 * communicate by way of circular dma rings via the SRAM to the firmware.
32 * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings.
33 * The 4 tx data rings allow for prioritization QoS.
35 * The rx data ring consists of 32 dma buffers. Two registers are used to
36 * indicate where in the ring the driver and the firmware are up to. The
37 * driver sets the initial read index (reg1) and the initial write index (reg2),
38 * the firmware updates the read index (reg1) on rx of a packet and fires an
39 * interrupt. The driver then processes the buffers starting at reg1 indicating
40 * to the firmware which buffers have been accessed by updating reg2. At the
41 * same time allocating new memory for the processed buffer.
43 * A similar thing happens with the tx rings. The difference is the firmware
44 * stop processing buffers once the queue is full and until confirmation
45 * of a successful transmition (tx_done) has occurred.
47 * The command ring operates in the same manner as the tx queues.
49 * All communication direct to the card (ie eeprom) is classed as Stage1
52 * All communication via the firmware to the card is classed as State2.
53 * The firmware consists of 2 parts. A bootstrap firmware and a runtime
54 * firmware. The bootstrap firmware and runtime firmware are loaded
55 * from host memory via dma to the card then told to execute. From this point
56 * on the majority of communications between the driver and the card goes
63 #include <sys/param.h>
64 #include <sys/sysctl.h>
65 #include <sys/sockio.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/queue.h>
72 #include <sys/taskqueue.h>
73 #include <sys/module.h>
75 #include <sys/endian.h>
76 #include <sys/linker.h>
77 #include <sys/firmware.h>
79 #include <machine/bus.h>
80 #include <machine/resource.h>
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcivar.h>
88 #include <net/if_var.h>
89 #include <net/if_arp.h>
90 #include <net/ethernet.h>
91 #include <net/if_dl.h>
92 #include <net/if_media.h>
93 #include <net/if_types.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
99 #include <netinet/ip.h>
101 #include <net80211/ieee80211_var.h>
102 #include <net80211/ieee80211_radiotap.h>
103 #include <net80211/ieee80211_regdomain.h>
104 #include <net80211/ieee80211_ratectl.h>
106 #include <dev/wpi/if_wpireg.h>
107 #include <dev/wpi/if_wpivar.h>
108 #include <dev/wpi/if_wpi_debug.h>
117 static const struct wpi_ident wpi_ident_table[] = {
118 /* The below entries support ABG regardless of the subid */
119 { 0x8086, 0x4222, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
120 { 0x8086, 0x4227, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
121 /* The below entries only support BG */
122 { 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG" },
123 { 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG" },
124 { 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG" },
125 { 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG" },
129 static int wpi_probe(device_t);
130 static int wpi_attach(device_t);
131 static void wpi_radiotap_attach(struct wpi_softc *);
132 static void wpi_sysctlattach(struct wpi_softc *);
133 static void wpi_init_beacon(struct wpi_vap *);
134 static struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
135 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
136 const uint8_t [IEEE80211_ADDR_LEN],
137 const uint8_t [IEEE80211_ADDR_LEN]);
138 static void wpi_vap_delete(struct ieee80211vap *);
139 static int wpi_detach(device_t);
140 static int wpi_shutdown(device_t);
141 static int wpi_suspend(device_t);
142 static int wpi_resume(device_t);
143 static int wpi_nic_lock(struct wpi_softc *);
144 static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
145 static void wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
146 static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
147 void **, bus_size_t, bus_size_t);
148 static void wpi_dma_contig_free(struct wpi_dma_info *);
149 static int wpi_alloc_shared(struct wpi_softc *);
150 static void wpi_free_shared(struct wpi_softc *);
151 static int wpi_alloc_fwmem(struct wpi_softc *);
152 static void wpi_free_fwmem(struct wpi_softc *);
153 static int wpi_alloc_rx_ring(struct wpi_softc *);
154 static void wpi_update_rx_ring(struct wpi_softc *);
155 static void wpi_update_rx_ring_ps(struct wpi_softc *);
156 static void wpi_reset_rx_ring(struct wpi_softc *);
157 static void wpi_free_rx_ring(struct wpi_softc *);
158 static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
160 static void wpi_update_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
161 static void wpi_update_tx_ring_ps(struct wpi_softc *,
162 struct wpi_tx_ring *);
163 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
164 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
165 static int wpi_read_eeprom(struct wpi_softc *,
166 uint8_t macaddr[IEEE80211_ADDR_LEN]);
167 static uint32_t wpi_eeprom_channel_flags(struct wpi_eeprom_chan *);
168 static void wpi_read_eeprom_band(struct wpi_softc *, int);
169 static int wpi_read_eeprom_channels(struct wpi_softc *, int);
170 static struct wpi_eeprom_chan *wpi_find_eeprom_channel(struct wpi_softc *,
171 struct ieee80211_channel *);
172 static int wpi_setregdomain(struct ieee80211com *,
173 struct ieee80211_regdomain *, int,
174 struct ieee80211_channel[]);
175 static int wpi_read_eeprom_group(struct wpi_softc *, int);
176 static int wpi_add_node_entry_adhoc(struct wpi_softc *);
177 static struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
178 const uint8_t mac[IEEE80211_ADDR_LEN]);
179 static void wpi_node_free(struct ieee80211_node *);
180 static void wpi_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
181 const struct ieee80211_rx_stats *,
183 static void wpi_restore_node(void *, struct ieee80211_node *);
184 static void wpi_restore_node_table(struct wpi_softc *, struct wpi_vap *);
185 static int wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
186 static void wpi_calib_timeout(void *);
187 static void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *,
188 struct wpi_rx_data *);
189 static void wpi_rx_statistics(struct wpi_softc *, struct wpi_rx_desc *,
190 struct wpi_rx_data *);
191 static void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *);
192 static void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *);
193 static void wpi_notif_intr(struct wpi_softc *);
194 static void wpi_wakeup_intr(struct wpi_softc *);
196 static void wpi_debug_registers(struct wpi_softc *);
198 static void wpi_fatal_intr(struct wpi_softc *);
199 static void wpi_intr(void *);
200 static int wpi_cmd2(struct wpi_softc *, struct wpi_buf *);
201 static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
202 struct ieee80211_node *);
203 static int wpi_tx_data_raw(struct wpi_softc *, struct mbuf *,
204 struct ieee80211_node *,
205 const struct ieee80211_bpf_params *);
206 static int wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
207 const struct ieee80211_bpf_params *);
208 static int wpi_transmit(struct ieee80211com *, struct mbuf *);
209 static void wpi_start(void *, int);
210 static void wpi_watchdog_rfkill(void *);
211 static void wpi_scan_timeout(void *);
212 static void wpi_tx_timeout(void *);
213 static void wpi_parent(struct ieee80211com *);
214 static int wpi_cmd(struct wpi_softc *, int, const void *, size_t, int);
215 static int wpi_mrr_setup(struct wpi_softc *);
216 static int wpi_add_node(struct wpi_softc *, struct ieee80211_node *);
217 static int wpi_add_broadcast_node(struct wpi_softc *, int);
218 static int wpi_add_ibss_node(struct wpi_softc *, struct ieee80211_node *);
219 static void wpi_del_node(struct wpi_softc *, struct ieee80211_node *);
220 static int wpi_updateedca(struct ieee80211com *);
221 static void wpi_set_promisc(struct wpi_softc *);
222 static void wpi_update_promisc(struct ieee80211com *);
223 static void wpi_update_mcast(struct ieee80211com *);
224 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
225 static int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *);
226 static void wpi_power_calibration(struct wpi_softc *);
227 static int wpi_set_txpower(struct wpi_softc *, int);
228 static int wpi_get_power_index(struct wpi_softc *,
229 struct wpi_power_group *, uint8_t, int, int);
230 static int wpi_set_pslevel(struct wpi_softc *, uint8_t, int, int);
231 static int wpi_send_btcoex(struct wpi_softc *);
232 static int wpi_send_rxon(struct wpi_softc *, int, int);
233 static int wpi_config(struct wpi_softc *);
234 static uint16_t wpi_get_active_dwell_time(struct wpi_softc *,
235 struct ieee80211_channel *, uint8_t);
236 static uint16_t wpi_limit_dwell(struct wpi_softc *, uint16_t);
237 static uint16_t wpi_get_passive_dwell_time(struct wpi_softc *,
238 struct ieee80211_channel *);
239 static uint32_t wpi_get_scan_pause_time(uint32_t, uint16_t);
240 static int wpi_scan(struct wpi_softc *, struct ieee80211_channel *);
241 static int wpi_auth(struct wpi_softc *, struct ieee80211vap *);
242 static int wpi_config_beacon(struct wpi_vap *);
243 static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
244 static void wpi_update_beacon(struct ieee80211vap *, int);
245 static void wpi_newassoc(struct ieee80211_node *, int);
246 static int wpi_run(struct wpi_softc *, struct ieee80211vap *);
247 static int wpi_load_key(struct ieee80211_node *,
248 const struct ieee80211_key *);
249 static void wpi_load_key_cb(void *, struct ieee80211_node *);
250 static int wpi_set_global_keys(struct ieee80211_node *);
251 static int wpi_del_key(struct ieee80211_node *,
252 const struct ieee80211_key *);
253 static void wpi_del_key_cb(void *, struct ieee80211_node *);
254 static int wpi_process_key(struct ieee80211vap *,
255 const struct ieee80211_key *, int);
256 static int wpi_key_set(struct ieee80211vap *,
257 const struct ieee80211_key *,
258 const uint8_t mac[IEEE80211_ADDR_LEN]);
259 static int wpi_key_delete(struct ieee80211vap *,
260 const struct ieee80211_key *);
261 static int wpi_post_alive(struct wpi_softc *);
262 static int wpi_load_bootcode(struct wpi_softc *, const uint8_t *, int);
263 static int wpi_load_firmware(struct wpi_softc *);
264 static int wpi_read_firmware(struct wpi_softc *);
265 static void wpi_unload_firmware(struct wpi_softc *);
266 static int wpi_clock_wait(struct wpi_softc *);
267 static int wpi_apm_init(struct wpi_softc *);
268 static void wpi_apm_stop_master(struct wpi_softc *);
269 static void wpi_apm_stop(struct wpi_softc *);
270 static void wpi_nic_config(struct wpi_softc *);
271 static int wpi_hw_init(struct wpi_softc *);
272 static void wpi_hw_stop(struct wpi_softc *);
273 static void wpi_radio_on(void *, int);
274 static void wpi_radio_off(void *, int);
275 static int wpi_init(struct wpi_softc *);
276 static void wpi_stop_locked(struct wpi_softc *);
277 static void wpi_stop(struct wpi_softc *);
278 static void wpi_scan_start(struct ieee80211com *);
279 static void wpi_scan_end(struct ieee80211com *);
280 static void wpi_set_channel(struct ieee80211com *);
281 static void wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
282 static void wpi_scan_mindwell(struct ieee80211_scan_state *);
283 static void wpi_hw_reset(void *, int);
285 static device_method_t wpi_methods[] = {
286 /* Device interface */
287 DEVMETHOD(device_probe, wpi_probe),
288 DEVMETHOD(device_attach, wpi_attach),
289 DEVMETHOD(device_detach, wpi_detach),
290 DEVMETHOD(device_shutdown, wpi_shutdown),
291 DEVMETHOD(device_suspend, wpi_suspend),
292 DEVMETHOD(device_resume, wpi_resume),
297 static driver_t wpi_driver = {
300 sizeof (struct wpi_softc)
302 static devclass_t wpi_devclass;
304 DRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, NULL, NULL);
306 MODULE_VERSION(wpi, 1);
308 MODULE_DEPEND(wpi, pci, 1, 1, 1);
309 MODULE_DEPEND(wpi, wlan, 1, 1, 1);
310 MODULE_DEPEND(wpi, firmware, 1, 1, 1);
313 wpi_probe(device_t dev)
315 const struct wpi_ident *ident;
317 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
318 if (pci_get_vendor(dev) == ident->vendor &&
319 pci_get_device(dev) == ident->device) {
320 device_set_desc(dev, ident->name);
321 return (BUS_PROBE_DEFAULT);
328 wpi_attach(device_t dev)
330 struct wpi_softc *sc = (struct wpi_softc *)device_get_softc(dev);
331 struct ieee80211com *ic;
335 const struct wpi_ident *ident;
341 error = resource_int_value(device_get_name(sc->sc_dev),
342 device_get_unit(sc->sc_dev), "debug", &(sc->sc_debug));
349 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
352 * Get the offset of the PCI Express Capability Structure in PCI
353 * Configuration Space.
355 error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off);
357 device_printf(dev, "PCIe capability structure not found!\n");
362 * Some card's only support 802.11b/g not a, check to see if
363 * this is one such card. A 0x0 in the subdevice table indicates
364 * the entire subdevice range is to be ignored.
367 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
368 if (ident->subdevice &&
369 pci_get_subdevice(dev) == ident->subdevice) {
376 /* Clear device-specific "PCI retry timeout" register (41h). */
377 pci_write_config(dev, 0x41, 0, 1);
379 /* Enable bus-mastering. */
380 pci_enable_busmaster(dev);
383 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
385 if (sc->mem == NULL) {
386 device_printf(dev, "can't map mem space\n");
389 sc->sc_st = rman_get_bustag(sc->mem);
390 sc->sc_sh = rman_get_bushandle(sc->mem);
394 if (pci_alloc_msi(dev, &i) == 0)
396 /* Install interrupt handler. */
397 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
398 (rid != 0 ? 0 : RF_SHAREABLE));
399 if (sc->irq == NULL) {
400 device_printf(dev, "can't map interrupt\n");
406 WPI_TX_LOCK_INIT(sc);
407 WPI_RXON_LOCK_INIT(sc);
408 WPI_NT_LOCK_INIT(sc);
409 WPI_TXQ_LOCK_INIT(sc);
410 WPI_TXQ_STATE_LOCK_INIT(sc);
412 /* Allocate DMA memory for firmware transfers. */
413 if ((error = wpi_alloc_fwmem(sc)) != 0) {
415 "could not allocate memory for firmware, error %d\n",
420 /* Allocate shared page. */
421 if ((error = wpi_alloc_shared(sc)) != 0) {
422 device_printf(dev, "could not allocate shared page\n");
426 /* Allocate TX rings - 4 for QoS purposes, 1 for commands. */
427 for (i = 0; i < WPI_NTXQUEUES; i++) {
428 if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) {
430 "could not allocate TX ring %d, error %d\n", i,
436 /* Allocate RX ring. */
437 if ((error = wpi_alloc_rx_ring(sc)) != 0) {
438 device_printf(dev, "could not allocate RX ring, error %d\n",
443 /* Clear pending interrupts. */
444 WPI_WRITE(sc, WPI_INT, 0xffffffff);
448 ic->ic_name = device_get_nameunit(dev);
449 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
450 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
452 /* Set device capabilities. */
454 IEEE80211_C_STA /* station mode supported */
455 | IEEE80211_C_IBSS /* IBSS mode supported */
456 | IEEE80211_C_HOSTAP /* Host access point mode */
457 | IEEE80211_C_MONITOR /* monitor mode supported */
458 | IEEE80211_C_AHDEMO /* adhoc demo mode */
459 | IEEE80211_C_BGSCAN /* capable of bg scanning */
460 | IEEE80211_C_TXPMGT /* tx power management */
461 | IEEE80211_C_SHSLOT /* short slot time supported */
462 | IEEE80211_C_WPA /* 802.11i */
463 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
464 | IEEE80211_C_WME /* 802.11e */
465 | IEEE80211_C_PMGT /* Station-side power mgmt */
469 IEEE80211_CRYPTO_AES_CCM;
472 * Read in the eeprom and also setup the channels for
473 * net80211. We don't set the rates as net80211 does this for us
475 if ((error = wpi_read_eeprom(sc, ic->ic_macaddr)) != 0) {
476 device_printf(dev, "could not read EEPROM, error %d\n",
483 device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n",
485 device_printf(sc->sc_dev, "Hardware Type: %c\n",
486 sc->type > 1 ? 'B': '?');
487 device_printf(sc->sc_dev, "Hardware Revision: %c\n",
488 ((sc->rev & 0xf0) == 0xd0) ? 'D': '?');
489 device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
490 supportsa ? "does" : "does not");
492 /* XXX hw_config uses the PCIDEV for the Hardware rev. Must
493 check what sc->rev really represents - benjsc 20070615 */
497 ieee80211_ifattach(ic);
498 ic->ic_vap_create = wpi_vap_create;
499 ic->ic_vap_delete = wpi_vap_delete;
500 ic->ic_parent = wpi_parent;
501 ic->ic_raw_xmit = wpi_raw_xmit;
502 ic->ic_transmit = wpi_transmit;
503 ic->ic_node_alloc = wpi_node_alloc;
504 sc->sc_node_free = ic->ic_node_free;
505 ic->ic_node_free = wpi_node_free;
506 ic->ic_wme.wme_update = wpi_updateedca;
507 ic->ic_update_promisc = wpi_update_promisc;
508 ic->ic_update_mcast = wpi_update_mcast;
509 ic->ic_newassoc = wpi_newassoc;
510 ic->ic_scan_start = wpi_scan_start;
511 ic->ic_scan_end = wpi_scan_end;
512 ic->ic_set_channel = wpi_set_channel;
513 ic->ic_scan_curchan = wpi_scan_curchan;
514 ic->ic_scan_mindwell = wpi_scan_mindwell;
515 ic->ic_setregdomain = wpi_setregdomain;
517 sc->sc_update_rx_ring = wpi_update_rx_ring;
518 sc->sc_update_tx_ring = wpi_update_tx_ring;
520 wpi_radiotap_attach(sc);
522 callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0);
523 callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0);
524 callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0);
525 callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0);
526 TASK_INIT(&sc->sc_reinittask, 0, wpi_hw_reset, sc);
527 TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc);
528 TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc);
529 TASK_INIT(&sc->sc_start_task, 0, wpi_start, sc);
531 sc->sc_tq = taskqueue_create("wpi_taskq", M_WAITOK,
532 taskqueue_thread_enqueue, &sc->sc_tq);
533 error = taskqueue_start_threads(&sc->sc_tq, 1, 0, "wpi_taskq");
535 device_printf(dev, "can't start threads, error %d\n", error);
539 wpi_sysctlattach(sc);
542 * Hook our interrupt after all initialization is complete.
544 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
545 NULL, wpi_intr, sc, &sc->sc_ih);
547 device_printf(dev, "can't establish interrupt, error %d\n",
553 ieee80211_announce(ic);
556 if (sc->sc_debug & WPI_DEBUG_HW)
557 ieee80211_announce_channels(ic);
560 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
563 fail: wpi_detach(dev);
564 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
569 * Attach the interface to 802.11 radiotap.
572 wpi_radiotap_attach(struct wpi_softc *sc)
574 struct wpi_rx_radiotap_header *rxtap = &sc->sc_rxtap;
575 struct wpi_tx_radiotap_header *txtap = &sc->sc_txtap;
577 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
578 ieee80211_radiotap_attach(&sc->sc_ic,
579 &txtap->wt_ihdr, sizeof(*txtap), WPI_TX_RADIOTAP_PRESENT,
580 &rxtap->wr_ihdr, sizeof(*rxtap), WPI_RX_RADIOTAP_PRESENT);
581 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
585 wpi_sysctlattach(struct wpi_softc *sc)
588 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
589 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
591 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
592 "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug,
593 "control debugging printfs");
598 wpi_init_beacon(struct wpi_vap *wvp)
600 struct wpi_buf *bcn = &wvp->wv_bcbuf;
601 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
603 cmd->id = WPI_ID_BROADCAST;
604 cmd->ofdm_mask = 0xff;
605 cmd->cck_mask = 0x0f;
606 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE);
609 * XXX WPI_TX_AUTO_SEQ seems to be ignored - workaround this issue
610 * XXX by using WPI_TX_NEED_ACK instead (with some side effects).
612 cmd->flags = htole32(WPI_TX_NEED_ACK | WPI_TX_INSERT_TSTAMP);
614 bcn->code = WPI_CMD_SET_BEACON;
615 bcn->ac = WPI_CMD_QUEUE_NUM;
616 bcn->size = sizeof(struct wpi_cmd_beacon);
619 static struct ieee80211vap *
620 wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
621 enum ieee80211_opmode opmode, int flags,
622 const uint8_t bssid[IEEE80211_ADDR_LEN],
623 const uint8_t mac[IEEE80211_ADDR_LEN])
626 struct ieee80211vap *vap;
628 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
631 wvp = malloc(sizeof(struct wpi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
633 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
635 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
636 WPI_VAP_LOCK_INIT(wvp);
637 wpi_init_beacon(wvp);
640 /* Override with driver methods. */
641 vap->iv_key_set = wpi_key_set;
642 vap->iv_key_delete = wpi_key_delete;
643 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
644 vap->iv_recv_mgmt = wpi_recv_mgmt;
645 wvp->wv_newstate = vap->iv_newstate;
646 vap->iv_newstate = wpi_newstate;
647 vap->iv_update_beacon = wpi_update_beacon;
648 vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1;
650 ieee80211_ratectl_init(vap);
651 /* Complete setup. */
652 ieee80211_vap_attach(vap, ieee80211_media_change,
653 ieee80211_media_status, mac);
654 ic->ic_opmode = opmode;
659 wpi_vap_delete(struct ieee80211vap *vap)
661 struct wpi_vap *wvp = WPI_VAP(vap);
662 struct wpi_buf *bcn = &wvp->wv_bcbuf;
663 enum ieee80211_opmode opmode = vap->iv_opmode;
665 ieee80211_ratectl_deinit(vap);
666 ieee80211_vap_detach(vap);
668 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
672 WPI_VAP_LOCK_DESTROY(wvp);
675 free(wvp, M_80211_VAP);
679 wpi_detach(device_t dev)
681 struct wpi_softc *sc = device_get_softc(dev);
682 struct ieee80211com *ic = &sc->sc_ic;
685 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
687 if (ic->ic_vap_create == wpi_vap_create) {
688 ieee80211_draintask(ic, &sc->sc_radioon_task);
689 ieee80211_draintask(ic, &sc->sc_start_task);
693 if (sc->sc_tq != NULL) {
694 taskqueue_drain_all(sc->sc_tq);
695 taskqueue_free(sc->sc_tq);
698 callout_drain(&sc->watchdog_rfkill);
699 callout_drain(&sc->tx_timeout);
700 callout_drain(&sc->scan_timeout);
701 callout_drain(&sc->calib_to);
702 ieee80211_ifdetach(ic);
705 /* Uninstall interrupt handler. */
706 if (sc->irq != NULL) {
707 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
708 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
710 pci_release_msi(dev);
713 if (sc->txq[0].data_dmat) {
714 /* Free DMA resources. */
715 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
716 wpi_free_tx_ring(sc, &sc->txq[qid]);
718 wpi_free_rx_ring(sc);
726 bus_release_resource(dev, SYS_RES_MEMORY,
727 rman_get_rid(sc->mem), sc->mem);
729 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
730 WPI_TXQ_STATE_LOCK_DESTROY(sc);
731 WPI_TXQ_LOCK_DESTROY(sc);
732 WPI_NT_LOCK_DESTROY(sc);
733 WPI_RXON_LOCK_DESTROY(sc);
734 WPI_TX_LOCK_DESTROY(sc);
735 WPI_LOCK_DESTROY(sc);
740 wpi_shutdown(device_t dev)
742 struct wpi_softc *sc = device_get_softc(dev);
749 wpi_suspend(device_t dev)
751 struct wpi_softc *sc = device_get_softc(dev);
752 struct ieee80211com *ic = &sc->sc_ic;
754 ieee80211_suspend_all(ic);
759 wpi_resume(device_t dev)
761 struct wpi_softc *sc = device_get_softc(dev);
762 struct ieee80211com *ic = &sc->sc_ic;
764 /* Clear device-specific "PCI retry timeout" register (41h). */
765 pci_write_config(dev, 0x41, 0, 1);
767 ieee80211_resume_all(ic);
772 * Grab exclusive access to NIC memory.
775 wpi_nic_lock(struct wpi_softc *sc)
779 /* Request exclusive access to NIC. */
780 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
782 /* Spin until we actually get the lock. */
783 for (ntries = 0; ntries < 1000; ntries++) {
784 if ((WPI_READ(sc, WPI_GP_CNTRL) &
785 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
786 WPI_GP_CNTRL_MAC_ACCESS_ENA)
791 device_printf(sc->sc_dev, "could not lock memory\n");
797 * Release lock on NIC memory.
800 wpi_nic_unlock(struct wpi_softc *sc)
802 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
805 static __inline uint32_t
806 wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
808 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
809 WPI_BARRIER_READ_WRITE(sc);
810 return WPI_READ(sc, WPI_PRPH_RDATA);
814 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
816 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
817 WPI_BARRIER_WRITE(sc);
818 WPI_WRITE(sc, WPI_PRPH_WDATA, data);
822 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
824 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
828 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
830 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
834 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
835 const uint32_t *data, int count)
837 for (; count > 0; count--, data++, addr += 4)
838 wpi_prph_write(sc, addr, *data);
841 static __inline uint32_t
842 wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
844 WPI_WRITE(sc, WPI_MEM_RADDR, addr);
845 WPI_BARRIER_READ_WRITE(sc);
846 return WPI_READ(sc, WPI_MEM_RDATA);
850 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
853 for (; count > 0; count--, addr += 4)
854 *data++ = wpi_mem_read(sc, addr);
858 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
864 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
866 if ((error = wpi_nic_lock(sc)) != 0)
869 for (; count > 0; count -= 2, addr++) {
870 WPI_WRITE(sc, WPI_EEPROM, addr << 2);
871 for (ntries = 0; ntries < 10; ntries++) {
872 val = WPI_READ(sc, WPI_EEPROM);
873 if (val & WPI_EEPROM_READ_VALID)
878 device_printf(sc->sc_dev,
879 "timeout reading ROM at 0x%x\n", addr);
889 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
895 wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
899 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
900 *(bus_addr_t *)arg = segs[0].ds_addr;
904 * Allocates a contiguous block of dma memory of the requested size and
908 wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
909 void **kvap, bus_size_t size, bus_size_t alignment)
916 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
917 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
918 1, size, BUS_DMA_NOWAIT, NULL, NULL, &dma->tag);
922 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
923 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
927 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
928 wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
932 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
939 fail: wpi_dma_contig_free(dma);
944 wpi_dma_contig_free(struct wpi_dma_info *dma)
946 if (dma->vaddr != NULL) {
947 bus_dmamap_sync(dma->tag, dma->map,
948 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
949 bus_dmamap_unload(dma->tag, dma->map);
950 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
953 if (dma->tag != NULL) {
954 bus_dma_tag_destroy(dma->tag);
960 * Allocate a shared page between host and NIC.
963 wpi_alloc_shared(struct wpi_softc *sc)
965 /* Shared buffer must be aligned on a 4KB boundary. */
966 return wpi_dma_contig_alloc(sc, &sc->shared_dma,
967 (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
971 wpi_free_shared(struct wpi_softc *sc)
973 wpi_dma_contig_free(&sc->shared_dma);
977 * Allocate DMA-safe memory for firmware transfer.
980 wpi_alloc_fwmem(struct wpi_softc *sc)
982 /* Must be aligned on a 16-byte boundary. */
983 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
984 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
988 wpi_free_fwmem(struct wpi_softc *sc)
990 wpi_dma_contig_free(&sc->fw_dma);
994 wpi_alloc_rx_ring(struct wpi_softc *sc)
996 struct wpi_rx_ring *ring = &sc->rxq;
1003 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1005 /* Allocate RX descriptors (16KB aligned.) */
1006 size = WPI_RX_RING_COUNT * sizeof (uint32_t);
1007 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1008 (void **)&ring->desc, size, WPI_RING_DMA_ALIGN);
1010 device_printf(sc->sc_dev,
1011 "%s: could not allocate RX ring DMA memory, error %d\n",
1016 /* Create RX buffer DMA tag. */
1017 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1018 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1019 MJUMPAGESIZE, 1, MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL,
1022 device_printf(sc->sc_dev,
1023 "%s: could not create RX buf DMA tag, error %d\n",
1029 * Allocate and map RX buffers.
1031 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1032 struct wpi_rx_data *data = &ring->data[i];
1035 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1037 device_printf(sc->sc_dev,
1038 "%s: could not create RX buf DMA map, error %d\n",
1043 data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1044 if (data->m == NULL) {
1045 device_printf(sc->sc_dev,
1046 "%s: could not allocate RX mbuf\n", __func__);
1051 error = bus_dmamap_load(ring->data_dmat, data->map,
1052 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1053 &paddr, BUS_DMA_NOWAIT);
1054 if (error != 0 && error != EFBIG) {
1055 device_printf(sc->sc_dev,
1056 "%s: can't map mbuf (error %d)\n", __func__,
1061 /* Set physical address of RX buffer. */
1062 ring->desc[i] = htole32(paddr);
1065 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1066 BUS_DMASYNC_PREWRITE);
1068 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1072 fail: wpi_free_rx_ring(sc);
1074 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1080 wpi_update_rx_ring(struct wpi_softc *sc)
1082 WPI_WRITE(sc, WPI_FH_RX_WPTR, sc->rxq.cur & ~7);
1086 wpi_update_rx_ring_ps(struct wpi_softc *sc)
1088 struct wpi_rx_ring *ring = &sc->rxq;
1090 if (ring->update != 0) {
1091 /* Wait for INT_WAKEUP event. */
1095 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1096 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1097 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n",
1101 wpi_update_rx_ring(sc);
1102 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1107 wpi_reset_rx_ring(struct wpi_softc *sc)
1109 struct wpi_rx_ring *ring = &sc->rxq;
1112 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1114 if (wpi_nic_lock(sc) == 0) {
1115 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
1116 for (ntries = 0; ntries < 1000; ntries++) {
1117 if (WPI_READ(sc, WPI_FH_RX_STATUS) &
1118 WPI_FH_RX_STATUS_IDLE)
1130 wpi_free_rx_ring(struct wpi_softc *sc)
1132 struct wpi_rx_ring *ring = &sc->rxq;
1135 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1137 wpi_dma_contig_free(&ring->desc_dma);
1139 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1140 struct wpi_rx_data *data = &ring->data[i];
1142 if (data->m != NULL) {
1143 bus_dmamap_sync(ring->data_dmat, data->map,
1144 BUS_DMASYNC_POSTREAD);
1145 bus_dmamap_unload(ring->data_dmat, data->map);
1149 if (data->map != NULL)
1150 bus_dmamap_destroy(ring->data_dmat, data->map);
1152 if (ring->data_dmat != NULL) {
1153 bus_dma_tag_destroy(ring->data_dmat);
1154 ring->data_dmat = NULL;
1159 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int qid)
1169 mbufq_init(&ring->snd, ifqmaxlen);
1171 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1173 /* Allocate TX descriptors (16KB aligned.) */
1174 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
1175 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc,
1176 size, WPI_RING_DMA_ALIGN);
1178 device_printf(sc->sc_dev,
1179 "%s: could not allocate TX ring DMA memory, error %d\n",
1184 /* Update shared area with ring physical address. */
1185 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1186 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1187 BUS_DMASYNC_PREWRITE);
1190 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
1191 * to allocate commands space for other rings.
1192 * XXX Do we really need to allocate descriptors for other rings?
1194 if (qid > WPI_CMD_QUEUE_NUM) {
1195 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1199 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
1200 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1203 device_printf(sc->sc_dev,
1204 "%s: could not allocate TX cmd DMA memory, error %d\n",
1209 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1210 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1211 WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1214 device_printf(sc->sc_dev,
1215 "%s: could not create TX buf DMA tag, error %d\n",
1220 paddr = ring->cmd_dma.paddr;
1221 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1222 struct wpi_tx_data *data = &ring->data[i];
1224 data->cmd_paddr = paddr;
1225 paddr += sizeof (struct wpi_tx_cmd);
1227 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1229 device_printf(sc->sc_dev,
1230 "%s: could not create TX buf DMA map, error %d\n",
1236 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1240 fail: wpi_free_tx_ring(sc, ring);
1241 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1246 wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1248 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
1252 wpi_update_tx_ring_ps(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1255 if (ring->update != 0) {
1256 /* Wait for INT_WAKEUP event. */
1260 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1261 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1262 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n",
1263 __func__, ring->qid);
1266 wpi_update_tx_ring(sc, ring);
1267 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1272 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1276 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1278 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1279 struct wpi_tx_data *data = &ring->data[i];
1281 if (data->m != NULL) {
1282 bus_dmamap_sync(ring->data_dmat, data->map,
1283 BUS_DMASYNC_POSTWRITE);
1284 bus_dmamap_unload(ring->data_dmat, data->map);
1288 if (data->ni != NULL) {
1289 ieee80211_free_node(data->ni);
1293 /* Clear TX descriptors. */
1294 memset(ring->desc, 0, ring->desc_dma.size);
1295 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1296 BUS_DMASYNC_PREWRITE);
1297 mbufq_drain(&ring->snd);
1298 sc->qfullmsk &= ~(1 << ring->qid);
1305 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1309 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1311 wpi_dma_contig_free(&ring->desc_dma);
1312 wpi_dma_contig_free(&ring->cmd_dma);
1314 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1315 struct wpi_tx_data *data = &ring->data[i];
1317 if (data->m != NULL) {
1318 bus_dmamap_sync(ring->data_dmat, data->map,
1319 BUS_DMASYNC_POSTWRITE);
1320 bus_dmamap_unload(ring->data_dmat, data->map);
1323 if (data->map != NULL)
1324 bus_dmamap_destroy(ring->data_dmat, data->map);
1326 if (ring->data_dmat != NULL) {
1327 bus_dma_tag_destroy(ring->data_dmat);
1328 ring->data_dmat = NULL;
1333 * Extract various information from EEPROM.
1336 wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1338 #define WPI_CHK(res) do { \
1339 if ((error = res) != 0) \
1344 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1346 /* Adapter has to be powered on for EEPROM access to work. */
1347 if ((error = wpi_apm_init(sc)) != 0) {
1348 device_printf(sc->sc_dev,
1349 "%s: could not power ON adapter, error %d\n", __func__,
1354 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
1355 device_printf(sc->sc_dev, "bad EEPROM signature\n");
1359 /* Clear HW ownership of EEPROM. */
1360 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
1362 /* Read the hardware capabilities, revision and SKU type. */
1363 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap,
1365 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,
1367 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type,
1370 sc->rev = le16toh(sc->rev);
1371 DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap,
1374 /* Read the regulatory domain (4 ASCII characters.) */
1375 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain,
1376 sizeof(sc->domain)));
1378 /* Read MAC address. */
1379 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr,
1380 IEEE80211_ADDR_LEN));
1382 /* Read the list of authorized channels. */
1383 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
1384 WPI_CHK(wpi_read_eeprom_channels(sc, i));
1386 /* Read the list of TX power groups. */
1387 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
1388 WPI_CHK(wpi_read_eeprom_group(sc, i));
1390 fail: wpi_apm_stop(sc); /* Power OFF adapter. */
1392 DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
1400 * Translate EEPROM flags to net80211.
1403 wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel)
1408 if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0)
1409 nflags |= IEEE80211_CHAN_PASSIVE;
1410 if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0)
1411 nflags |= IEEE80211_CHAN_NOADHOC;
1412 if (channel->flags & WPI_EEPROM_CHAN_RADAR) {
1413 nflags |= IEEE80211_CHAN_DFS;
1414 /* XXX apparently IBSS may still be marked */
1415 nflags |= IEEE80211_CHAN_NOADHOC;
1418 /* XXX HOSTAP uses WPI_MODE_IBSS */
1419 if (nflags & IEEE80211_CHAN_NOADHOC)
1420 nflags |= IEEE80211_CHAN_NOHOSTAP;
1426 wpi_read_eeprom_band(struct wpi_softc *sc, int n)
1428 struct ieee80211com *ic = &sc->sc_ic;
1429 struct wpi_eeprom_chan *channels = sc->eeprom_channels[n];
1430 const struct wpi_chan_band *band = &wpi_bands[n];
1431 struct ieee80211_channel *c;
1435 for (i = 0; i < band->nchan; i++) {
1436 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
1437 DPRINTF(sc, WPI_DEBUG_EEPROM,
1438 "Channel Not Valid: %d, band %d\n",
1443 chan = band->chan[i];
1444 nflags = wpi_eeprom_channel_flags(&channels[i]);
1446 c = &ic->ic_channels[ic->ic_nchans++];
1448 c->ic_maxregpower = channels[i].maxpwr;
1449 c->ic_maxpower = 2*c->ic_maxregpower;
1451 if (n == 0) { /* 2GHz band */
1452 c->ic_freq = ieee80211_ieee2mhz(chan,
1455 /* G =>'s B is supported */
1456 c->ic_flags = IEEE80211_CHAN_B | nflags;
1457 c = &ic->ic_channels[ic->ic_nchans++];
1459 c->ic_flags = IEEE80211_CHAN_G | nflags;
1460 } else { /* 5GHz band */
1461 c->ic_freq = ieee80211_ieee2mhz(chan,
1464 c->ic_flags = IEEE80211_CHAN_A | nflags;
1467 /* Save maximum allowed TX power for this channel. */
1468 sc->maxpwr[chan] = channels[i].maxpwr;
1470 DPRINTF(sc, WPI_DEBUG_EEPROM,
1471 "adding chan %d (%dMHz) flags=0x%x maxpwr=%d passive=%d,"
1472 " offset %d\n", chan, c->ic_freq,
1473 channels[i].flags, sc->maxpwr[chan],
1474 IEEE80211_IS_CHAN_PASSIVE(c), ic->ic_nchans);
1479 * Read the eeprom to find out what channels are valid for the given
1480 * band and update net80211 with what we find.
1483 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
1485 struct ieee80211com *ic = &sc->sc_ic;
1486 const struct wpi_chan_band *band = &wpi_bands[n];
1489 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1491 error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n],
1492 band->nchan * sizeof (struct wpi_eeprom_chan));
1494 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1498 wpi_read_eeprom_band(sc, n);
1500 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1502 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1507 static struct wpi_eeprom_chan *
1508 wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c)
1512 for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++)
1513 for (i = 0; i < wpi_bands[j].nchan; i++)
1514 if (wpi_bands[j].chan[i] == c->ic_ieee)
1515 return &sc->eeprom_channels[j][i];
1521 * Enforce flags read from EEPROM.
1524 wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
1525 int nchan, struct ieee80211_channel chans[])
1527 struct wpi_softc *sc = ic->ic_softc;
1530 for (i = 0; i < nchan; i++) {
1531 struct ieee80211_channel *c = &chans[i];
1532 struct wpi_eeprom_chan *channel;
1534 channel = wpi_find_eeprom_channel(sc, c);
1535 if (channel == NULL) {
1536 ic_printf(ic, "%s: invalid channel %u freq %u/0x%x\n",
1537 __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
1540 c->ic_flags |= wpi_eeprom_channel_flags(channel);
1547 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
1549 struct wpi_power_group *group = &sc->groups[n];
1550 struct wpi_eeprom_group rgroup;
1553 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1555 if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32,
1556 &rgroup, sizeof rgroup)) != 0) {
1557 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1561 /* Save TX power group information. */
1562 group->chan = rgroup.chan;
1563 group->maxpwr = rgroup.maxpwr;
1564 /* Retrieve temperature at which the samples were taken. */
1565 group->temp = (int16_t)le16toh(rgroup.temp);
1567 DPRINTF(sc, WPI_DEBUG_EEPROM,
1568 "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan,
1569 group->maxpwr, group->temp);
1571 for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
1572 group->samples[i].index = rgroup.samples[i].index;
1573 group->samples[i].power = rgroup.samples[i].power;
1575 DPRINTF(sc, WPI_DEBUG_EEPROM,
1576 "\tsample %d: index=%d power=%d\n", i,
1577 group->samples[i].index, group->samples[i].power);
1580 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1586 wpi_add_node_entry_adhoc(struct wpi_softc *sc)
1588 int newid = WPI_ID_IBSS_MIN;
1590 for (; newid <= WPI_ID_IBSS_MAX; newid++) {
1591 if ((sc->nodesmsk & (1 << newid)) == 0) {
1592 sc->nodesmsk |= 1 << newid;
1597 return WPI_ID_UNDEFINED;
1601 wpi_add_node_entry_sta(struct wpi_softc *sc)
1603 sc->nodesmsk |= 1 << WPI_ID_BSS;
1609 wpi_check_node_entry(struct wpi_softc *sc, uint8_t id)
1611 if (id == WPI_ID_UNDEFINED)
1614 return (sc->nodesmsk >> id) & 1;
1617 static __inline void
1618 wpi_clear_node_table(struct wpi_softc *sc)
1623 static __inline void
1624 wpi_del_node_entry(struct wpi_softc *sc, uint8_t id)
1626 sc->nodesmsk &= ~(1 << id);
1629 static struct ieee80211_node *
1630 wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1632 struct wpi_node *wn;
1634 wn = malloc(sizeof (struct wpi_node), M_80211_NODE,
1640 wn->id = WPI_ID_UNDEFINED;
1646 wpi_node_free(struct ieee80211_node *ni)
1648 struct wpi_softc *sc = ni->ni_ic->ic_softc;
1649 struct wpi_node *wn = WPI_NODE(ni);
1651 if (wn->id != WPI_ID_UNDEFINED) {
1653 if (wpi_check_node_entry(sc, wn->id)) {
1654 wpi_del_node_entry(sc, wn->id);
1655 wpi_del_node(sc, ni);
1660 sc->sc_node_free(ni);
1664 wpi_check_bss_filter(struct wpi_softc *sc)
1666 return (sc->rxon.filter & htole32(WPI_FILTER_BSS)) != 0;
1670 wpi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
1671 const struct ieee80211_rx_stats *rxs,
1674 struct ieee80211vap *vap = ni->ni_vap;
1675 struct wpi_softc *sc = vap->iv_ic->ic_softc;
1676 struct wpi_vap *wvp = WPI_VAP(vap);
1677 uint64_t ni_tstamp, rx_tstamp;
1679 wvp->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1681 if (vap->iv_opmode == IEEE80211_M_IBSS &&
1682 vap->iv_state == IEEE80211_S_RUN &&
1683 (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
1684 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) {
1685 ni_tstamp = le64toh(ni->ni_tstamp.tsf);
1686 rx_tstamp = le64toh(sc->rx_tstamp);
1688 if (ni_tstamp >= rx_tstamp) {
1689 DPRINTF(sc, WPI_DEBUG_STATE,
1690 "ibss merge, tsf %ju tstamp %ju\n",
1691 (uintmax_t)rx_tstamp, (uintmax_t)ni_tstamp);
1692 (void) ieee80211_ibss_merge(ni);
1698 wpi_restore_node(void *arg, struct ieee80211_node *ni)
1700 struct wpi_softc *sc = arg;
1701 struct wpi_node *wn = WPI_NODE(ni);
1705 if (wn->id != WPI_ID_UNDEFINED) {
1706 wn->id = WPI_ID_UNDEFINED;
1707 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
1708 device_printf(sc->sc_dev,
1709 "%s: could not add IBSS node, error %d\n",
1717 wpi_restore_node_table(struct wpi_softc *sc, struct wpi_vap *wvp)
1719 struct ieee80211com *ic = &sc->sc_ic;
1721 /* Set group keys once. */
1726 ieee80211_iterate_nodes(&ic->ic_sta, wpi_restore_node, sc);
1727 ieee80211_crypto_reload_keys(ic);
1731 * Called by net80211 when ever there is a change to 80211 state machine
1734 wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1736 struct wpi_vap *wvp = WPI_VAP(vap);
1737 struct ieee80211com *ic = vap->iv_ic;
1738 struct wpi_softc *sc = ic->ic_softc;
1741 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1744 if (nstate > IEEE80211_S_INIT && sc->sc_running == 0) {
1745 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1752 DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1753 ieee80211_state_name[vap->iv_state],
1754 ieee80211_state_name[nstate]);
1756 if (vap->iv_state == IEEE80211_S_RUN && nstate < IEEE80211_S_RUN) {
1757 if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) {
1758 device_printf(sc->sc_dev,
1759 "%s: could not set power saving level\n",
1764 wpi_set_led(sc, WPI_LED_LINK, 1, 0);
1768 case IEEE80211_S_SCAN:
1770 if (wpi_check_bss_filter(sc) != 0) {
1771 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1772 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1773 device_printf(sc->sc_dev,
1774 "%s: could not send RXON\n", __func__);
1777 WPI_RXON_UNLOCK(sc);
1780 case IEEE80211_S_ASSOC:
1781 if (vap->iv_state != IEEE80211_S_RUN)
1784 case IEEE80211_S_AUTH:
1786 * NB: do not optimize AUTH -> AUTH state transmission -
1787 * this will break powersave with non-QoS AP!
1791 * The node must be registered in the firmware before auth.
1792 * Also the associd must be cleared on RUN -> ASSOC
1795 if ((error = wpi_auth(sc, vap)) != 0) {
1796 device_printf(sc->sc_dev,
1797 "%s: could not move to AUTH state, error %d\n",
1802 case IEEE80211_S_RUN:
1804 * RUN -> RUN transition:
1805 * STA mode: Just restart the timers.
1806 * IBSS mode: Process IBSS merge.
1808 if (vap->iv_state == IEEE80211_S_RUN) {
1809 if (vap->iv_opmode != IEEE80211_M_IBSS) {
1811 wpi_calib_timeout(sc);
1812 WPI_RXON_UNLOCK(sc);
1816 * Drop the BSS_FILTER bit
1817 * (there is no another way to change bssid).
1820 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1821 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1822 device_printf(sc->sc_dev,
1823 "%s: could not send RXON\n",
1826 WPI_RXON_UNLOCK(sc);
1828 /* Restore all what was lost. */
1829 wpi_restore_node_table(sc, wvp);
1831 /* XXX set conditionally? */
1837 * !RUN -> RUN requires setting the association id
1838 * which is done with a firmware cmd. We also defer
1839 * starting the timers until that work is done.
1841 if ((error = wpi_run(sc, vap)) != 0) {
1842 device_printf(sc->sc_dev,
1843 "%s: could not move to RUN state\n", __func__);
1851 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1855 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1857 return wvp->wv_newstate(vap, nstate, arg);
1861 wpi_calib_timeout(void *arg)
1863 struct wpi_softc *sc = arg;
1865 if (wpi_check_bss_filter(sc) == 0)
1868 wpi_power_calibration(sc);
1870 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
1873 static __inline uint8_t
1874 rate2plcp(const uint8_t rate)
1877 case 12: return 0xd;
1878 case 18: return 0xf;
1879 case 24: return 0x5;
1880 case 36: return 0x7;
1881 case 48: return 0x9;
1882 case 72: return 0xb;
1883 case 96: return 0x1;
1884 case 108: return 0x3;
1888 case 22: return 110;
1893 static __inline uint8_t
1894 plcp2rate(const uint8_t plcp)
1897 case 0xd: return 12;
1898 case 0xf: return 18;
1899 case 0x5: return 24;
1900 case 0x7: return 36;
1901 case 0x9: return 48;
1902 case 0xb: return 72;
1903 case 0x1: return 96;
1904 case 0x3: return 108;
1908 case 110: return 22;
1913 /* Quickly determine if a given rate is CCK or OFDM. */
1914 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1917 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1918 struct wpi_rx_data *data)
1920 struct ieee80211com *ic = &sc->sc_ic;
1921 struct wpi_rx_ring *ring = &sc->rxq;
1922 struct wpi_rx_stat *stat;
1923 struct wpi_rx_head *head;
1924 struct wpi_rx_tail *tail;
1925 struct ieee80211_frame *wh;
1926 struct ieee80211_node *ni;
1927 struct mbuf *m, *m1;
1933 stat = (struct wpi_rx_stat *)(desc + 1);
1935 if (stat->len > WPI_STAT_MAXLEN) {
1936 device_printf(sc->sc_dev, "invalid RX statistic header\n");
1940 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
1941 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1942 len = le16toh(head->len);
1943 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len);
1944 flags = le32toh(tail->flags);
1946 DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d"
1947 " rate %x chan %d tstamp %ju\n", __func__, ring->cur,
1948 le32toh(desc->len), len, (int8_t)stat->rssi,
1949 head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp));
1951 /* Discard frames with a bad FCS early. */
1952 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1953 DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n",
1957 /* Discard frames that are too short. */
1958 if (len < sizeof (struct ieee80211_frame_ack)) {
1959 DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n",
1964 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1966 DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n",
1970 bus_dmamap_unload(ring->data_dmat, data->map);
1972 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *),
1973 MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1974 if (error != 0 && error != EFBIG) {
1975 device_printf(sc->sc_dev,
1976 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1979 /* Try to reload the old mbuf. */
1980 error = bus_dmamap_load(ring->data_dmat, data->map,
1981 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1982 &paddr, BUS_DMA_NOWAIT);
1983 if (error != 0 && error != EFBIG) {
1984 panic("%s: could not load old RX mbuf", __func__);
1986 /* Physical address may have changed. */
1987 ring->desc[ring->cur] = htole32(paddr);
1988 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map,
1989 BUS_DMASYNC_PREWRITE);
1995 /* Update RX descriptor. */
1996 ring->desc[ring->cur] = htole32(paddr);
1997 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1998 BUS_DMASYNC_PREWRITE);
2000 /* Finalize mbuf. */
2001 m->m_data = (caddr_t)(head + 1);
2002 m->m_pkthdr.len = m->m_len = len;
2004 /* Grab a reference to the source node. */
2005 wh = mtod(m, struct ieee80211_frame *);
2007 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
2008 (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) {
2009 /* Check whether decryption was successful or not. */
2010 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
2011 DPRINTF(sc, WPI_DEBUG_RECV,
2012 "CCMP decryption failed 0x%x\n", flags);
2015 m->m_flags |= M_WEP;
2018 if (len >= sizeof(struct ieee80211_frame_min))
2019 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
2023 sc->rx_tstamp = tail->tstamp;
2025 if (ieee80211_radiotap_active(ic)) {
2026 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
2029 if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE))
2030 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2031 tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET);
2032 tap->wr_dbm_antnoise = WPI_RSSI_OFFSET;
2033 tap->wr_tsft = tail->tstamp;
2034 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
2035 tap->wr_rate = plcp2rate(head->plcp);
2040 /* Send the frame to the 802.11 layer. */
2042 (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET);
2043 /* Node is no longer needed. */
2044 ieee80211_free_node(ni);
2046 (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET);
2054 fail1: counter_u64_add(ic->ic_ierrors, 1);
2058 wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc,
2059 struct wpi_rx_data *data)
2065 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2067 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
2068 struct wpi_tx_data *data = &ring->data[desc->idx];
2069 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
2071 struct ieee80211_node *ni;
2072 struct ieee80211vap *vap;
2073 struct ieee80211com *ic;
2074 uint32_t status = le32toh(stat->status);
2075 int ackfailcnt = stat->ackfailcnt / WPI_NTRIES_DEFAULT;
2077 KASSERT(data->ni != NULL, ("no node"));
2078 KASSERT(data->m != NULL, ("no mbuf"));
2080 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2082 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: "
2083 "qid %d idx %d retries %d btkillcnt %d rate %x duration %d "
2084 "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt,
2085 stat->btkillcnt, stat->rate, le32toh(stat->duration), status);
2087 /* Unmap and free mbuf. */
2088 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
2089 bus_dmamap_unload(ring->data_dmat, data->map);
2090 m = data->m, data->m = NULL;
2091 ni = data->ni, data->ni = NULL;
2096 * Update rate control statistics for the node.
2098 if (status & WPI_TX_STATUS_FAIL) {
2099 ieee80211_ratectl_tx_complete(vap, ni,
2100 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL);
2102 ieee80211_ratectl_tx_complete(vap, ni,
2103 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL);
2105 ieee80211_tx_complete(ni, m, (status & WPI_TX_STATUS_FAIL) != 0);
2107 WPI_TXQ_STATE_LOCK(sc);
2109 if (ring->queued > 0) {
2110 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2112 if ((sc->qfullmsk & (1 << ring->qid)) != 0 &&
2113 ring->queued < WPI_TX_RING_LOMARK) {
2114 sc->qfullmsk &= ~(1 << ring->qid);
2115 ieee80211_runtask(ic, &sc->sc_start_task);
2118 callout_stop(&sc->tx_timeout);
2119 WPI_TXQ_STATE_UNLOCK(sc);
2121 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2125 * Process a "command done" firmware notification. This is where we wakeup
2126 * processes waiting for a synchronous command completion.
2129 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2131 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
2132 struct wpi_tx_data *data;
2134 DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x "
2135 "type %s len %d\n", desc->qid, desc->idx,
2136 desc->flags, wpi_cmd_str(desc->type),
2137 le32toh(desc->len));
2139 if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM)
2140 return; /* Not a command ack. */
2142 KASSERT(ring->queued == 0, ("ring->queued must be 0"));
2144 data = &ring->data[desc->idx];
2146 /* If the command was mapped in an mbuf, free it. */
2147 if (data->m != NULL) {
2148 bus_dmamap_sync(ring->data_dmat, data->map,
2149 BUS_DMASYNC_POSTWRITE);
2150 bus_dmamap_unload(ring->data_dmat, data->map);
2155 wakeup(&ring->cmd[desc->idx]);
2157 if (desc->type == WPI_CMD_SET_POWER_MODE) {
2159 if (sc->sc_flags & WPI_PS_PATH) {
2160 sc->sc_update_rx_ring = wpi_update_rx_ring_ps;
2161 sc->sc_update_tx_ring = wpi_update_tx_ring_ps;
2163 sc->sc_update_rx_ring = wpi_update_rx_ring;
2164 sc->sc_update_tx_ring = wpi_update_tx_ring;
2171 wpi_notif_intr(struct wpi_softc *sc)
2173 struct ieee80211com *ic = &sc->sc_ic;
2174 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2177 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
2178 BUS_DMASYNC_POSTREAD);
2180 hw = le32toh(sc->shared->next) & 0xfff;
2181 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
2183 while (sc->rxq.cur != hw) {
2184 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
2186 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2187 struct wpi_rx_desc *desc;
2189 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2190 BUS_DMASYNC_POSTREAD);
2191 desc = mtod(data->m, struct wpi_rx_desc *);
2193 DPRINTF(sc, WPI_DEBUG_NOTIFY,
2194 "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n",
2195 __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags,
2196 desc->type, wpi_cmd_str(desc->type), le32toh(desc->len));
2198 if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) {
2199 /* Reply to a command. */
2200 wpi_cmd_done(sc, desc);
2203 switch (desc->type) {
2205 /* An 802.11 frame has been received. */
2206 wpi_rx_done(sc, desc, data);
2208 if (sc->sc_running == 0) {
2209 /* wpi_stop() was called. */
2216 /* An 802.11 frame has been transmitted. */
2217 wpi_tx_done(sc, desc);
2220 case WPI_RX_STATISTICS:
2221 case WPI_BEACON_STATISTICS:
2222 wpi_rx_statistics(sc, desc, data);
2225 case WPI_BEACON_MISSED:
2227 struct wpi_beacon_missed *miss =
2228 (struct wpi_beacon_missed *)(desc + 1);
2229 uint32_t expected, misses, received, threshold;
2231 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2232 BUS_DMASYNC_POSTREAD);
2234 misses = le32toh(miss->consecutive);
2235 expected = le32toh(miss->expected);
2236 received = le32toh(miss->received);
2237 threshold = MAX(2, vap->iv_bmissthreshold);
2239 DPRINTF(sc, WPI_DEBUG_BMISS,
2240 "%s: beacons missed %u(%u) (received %u/%u)\n",
2241 __func__, misses, le32toh(miss->total), received,
2244 if (misses >= threshold ||
2245 (received == 0 && expected >= threshold)) {
2247 if (callout_pending(&sc->scan_timeout)) {
2248 wpi_cmd(sc, WPI_CMD_SCAN_ABORT, NULL,
2251 WPI_RXON_UNLOCK(sc);
2252 if (vap->iv_state == IEEE80211_S_RUN &&
2253 (ic->ic_flags & IEEE80211_F_SCAN) == 0)
2254 ieee80211_beacon_miss(ic);
2260 case WPI_BEACON_SENT:
2262 struct wpi_tx_stat *stat =
2263 (struct wpi_tx_stat *)(desc + 1);
2264 uint64_t *tsf = (uint64_t *)(stat + 1);
2265 uint32_t *mode = (uint32_t *)(tsf + 1);
2267 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2268 BUS_DMASYNC_POSTREAD);
2270 DPRINTF(sc, WPI_DEBUG_BEACON,
2271 "beacon sent: rts %u, ack %u, btkill %u, rate %u, "
2272 "duration %u, status %x, tsf %ju, mode %x\n",
2273 stat->rtsfailcnt, stat->ackfailcnt,
2274 stat->btkillcnt, stat->rate, le32toh(stat->duration),
2275 le32toh(stat->status), *tsf, *mode);
2282 struct wpi_ucode_info *uc =
2283 (struct wpi_ucode_info *)(desc + 1);
2285 /* The microcontroller is ready. */
2286 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2287 BUS_DMASYNC_POSTREAD);
2288 DPRINTF(sc, WPI_DEBUG_RESET,
2289 "microcode alive notification version=%d.%d "
2290 "subtype=%x alive=%x\n", uc->major, uc->minor,
2291 uc->subtype, le32toh(uc->valid));
2293 if (le32toh(uc->valid) != 1) {
2294 device_printf(sc->sc_dev,
2295 "microcontroller initialization failed\n");
2296 wpi_stop_locked(sc);
2299 /* Save the address of the error log in SRAM. */
2300 sc->errptr = le32toh(uc->errptr);
2303 case WPI_STATE_CHANGED:
2305 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2306 BUS_DMASYNC_POSTREAD);
2308 uint32_t *status = (uint32_t *)(desc + 1);
2310 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n",
2313 if (le32toh(*status) & 1) {
2315 wpi_clear_node_table(sc);
2317 taskqueue_enqueue(sc->sc_tq,
2318 &sc->sc_radiooff_task);
2324 case WPI_START_SCAN:
2326 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2327 BUS_DMASYNC_POSTREAD);
2329 struct wpi_start_scan *scan =
2330 (struct wpi_start_scan *)(desc + 1);
2331 DPRINTF(sc, WPI_DEBUG_SCAN,
2332 "%s: scanning channel %d status %x\n",
2333 __func__, scan->chan, le32toh(scan->status));
2340 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2341 BUS_DMASYNC_POSTREAD);
2343 struct wpi_stop_scan *scan =
2344 (struct wpi_stop_scan *)(desc + 1);
2346 DPRINTF(sc, WPI_DEBUG_SCAN,
2347 "scan finished nchan=%d status=%d chan=%d\n",
2348 scan->nchan, scan->status, scan->chan);
2351 callout_stop(&sc->scan_timeout);
2352 WPI_RXON_UNLOCK(sc);
2353 if (scan->status == WPI_SCAN_ABORTED)
2354 ieee80211_cancel_scan(vap);
2356 ieee80211_scan_next(vap);
2361 if (sc->rxq.cur % 8 == 0) {
2362 /* Tell the firmware what we have processed. */
2363 sc->sc_update_rx_ring(sc);
2369 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2370 * from power-down sleep mode.
2373 wpi_wakeup_intr(struct wpi_softc *sc)
2377 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
2378 "%s: ucode wakeup from power-down sleep\n", __func__);
2380 /* Wakeup RX and TX rings. */
2381 if (sc->rxq.update) {
2383 wpi_update_rx_ring(sc);
2386 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) {
2387 struct wpi_tx_ring *ring = &sc->txq[qid];
2391 wpi_update_tx_ring(sc, ring);
2394 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
2399 * This function prints firmware registers
2403 wpi_debug_registers(struct wpi_softc *sc)
2406 static const uint32_t csr_tbl[] = {
2423 static const uint32_t prph_tbl[] = {
2430 DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n");
2432 for (i = 0; i < nitems(csr_tbl); i++) {
2433 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2434 wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i]));
2436 if ((i + 1) % 2 == 0)
2437 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2439 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n");
2441 if (wpi_nic_lock(sc) == 0) {
2442 for (i = 0; i < nitems(prph_tbl); i++) {
2443 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2444 wpi_get_prph_string(prph_tbl[i]),
2445 wpi_prph_read(sc, prph_tbl[i]));
2447 if ((i + 1) % 2 == 0)
2448 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2450 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2453 DPRINTF(sc, WPI_DEBUG_REGISTER,
2454 "Cannot access internal registers.\n");
2460 * Dump the error log of the firmware when a firmware panic occurs. Although
2461 * we can't debug the firmware because it is neither open source nor free, it
2462 * can help us to identify certain classes of problems.
2465 wpi_fatal_intr(struct wpi_softc *sc)
2467 struct wpi_fw_dump dump;
2468 uint32_t i, offset, count;
2470 /* Check that the error log address is valid. */
2471 if (sc->errptr < WPI_FW_DATA_BASE ||
2472 sc->errptr + sizeof (dump) >
2473 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
2474 printf("%s: bad firmware error log address 0x%08x\n", __func__,
2478 if (wpi_nic_lock(sc) != 0) {
2479 printf("%s: could not read firmware error log\n", __func__);
2482 /* Read number of entries in the log. */
2483 count = wpi_mem_read(sc, sc->errptr);
2484 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
2485 printf("%s: invalid count field (count = %u)\n", __func__,
2490 /* Skip "count" field. */
2491 offset = sc->errptr + sizeof (uint32_t);
2492 printf("firmware error log (count = %u):\n", count);
2493 for (i = 0; i < count; i++) {
2494 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
2495 sizeof (dump) / sizeof (uint32_t));
2497 printf(" error type = \"%s\" (0x%08X)\n",
2498 (dump.desc < nitems(wpi_fw_errmsg)) ?
2499 wpi_fw_errmsg[dump.desc] : "UNKNOWN",
2501 printf(" error data = 0x%08X\n",
2503 printf(" branch link = 0x%08X%08X\n",
2504 dump.blink[0], dump.blink[1]);
2505 printf(" interrupt link = 0x%08X%08X\n",
2506 dump.ilink[0], dump.ilink[1]);
2507 printf(" time = %u\n", dump.time);
2509 offset += sizeof (dump);
2512 /* Dump driver status (TX and RX rings) while we're here. */
2513 printf("driver status:\n");
2515 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
2516 struct wpi_tx_ring *ring = &sc->txq[i];
2517 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2518 i, ring->qid, ring->cur, ring->queued);
2521 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2527 struct wpi_softc *sc = arg;
2532 /* Disable interrupts. */
2533 WPI_WRITE(sc, WPI_INT_MASK, 0);
2535 r1 = WPI_READ(sc, WPI_INT);
2537 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
2538 goto end; /* Hardware gone! */
2540 r2 = WPI_READ(sc, WPI_FH_INT);
2542 DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__,
2545 if (r1 == 0 && r2 == 0)
2546 goto done; /* Interrupt not for us. */
2548 /* Acknowledge interrupts. */
2549 WPI_WRITE(sc, WPI_INT, r1);
2550 WPI_WRITE(sc, WPI_FH_INT, r2);
2552 if (r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR)) {
2553 device_printf(sc->sc_dev, "fatal firmware error\n");
2555 wpi_debug_registers(sc);
2558 DPRINTF(sc, WPI_DEBUG_HW,
2559 "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" :
2560 "(Hardware Error)");
2561 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2565 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
2566 (r2 & WPI_FH_INT_RX))
2569 if (r1 & WPI_INT_ALIVE)
2570 wakeup(sc); /* Firmware is alive. */
2572 if (r1 & WPI_INT_WAKEUP)
2573 wpi_wakeup_intr(sc);
2576 /* Re-enable interrupts. */
2578 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
2580 end: WPI_UNLOCK(sc);
2584 wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf)
2586 struct ieee80211_frame *wh;
2587 struct wpi_tx_cmd *cmd;
2588 struct wpi_tx_data *data;
2589 struct wpi_tx_desc *desc;
2590 struct wpi_tx_ring *ring;
2592 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER];
2593 int error, i, hdrlen, nsegs, totlen, pad;
2597 KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow"));
2599 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2601 if (sc->sc_running == 0) {
2602 /* wpi_stop() was called */
2607 wh = mtod(buf->m, struct ieee80211_frame *);
2608 hdrlen = ieee80211_anyhdrsize(wh);
2609 totlen = buf->m->m_pkthdr.len;
2612 /* First segment length must be a multiple of 4. */
2613 pad = 4 - (hdrlen & 3);
2617 ring = &sc->txq[buf->ac];
2618 desc = &ring->desc[ring->cur];
2619 data = &ring->data[ring->cur];
2621 /* Prepare TX firmware command. */
2622 cmd = &ring->cmd[ring->cur];
2623 cmd->code = buf->code;
2625 cmd->qid = ring->qid;
2626 cmd->idx = ring->cur;
2628 memcpy(cmd->data, buf->data, buf->size);
2630 /* Save and trim IEEE802.11 header. */
2631 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen);
2632 m_adj(buf->m, hdrlen);
2634 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m,
2635 segs, &nsegs, BUS_DMA_NOWAIT);
2636 if (error != 0 && error != EFBIG) {
2637 device_printf(sc->sc_dev,
2638 "%s: can't map mbuf (error %d)\n", __func__, error);
2642 /* Too many DMA segments, linearize mbuf. */
2643 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1);
2645 device_printf(sc->sc_dev,
2646 "%s: could not defrag mbuf\n", __func__);
2652 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
2653 buf->m, segs, &nsegs, BUS_DMA_NOWAIT);
2655 device_printf(sc->sc_dev,
2656 "%s: can't map mbuf (error %d)\n", __func__,
2662 KASSERT(nsegs < WPI_MAX_SCATTER,
2663 ("too many DMA segments, nsegs (%d) should be less than %d",
2664 nsegs, WPI_MAX_SCATTER));
2669 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
2670 __func__, ring->qid, ring->cur, totlen, nsegs);
2672 /* Fill TX descriptor. */
2673 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs);
2674 /* First DMA segment is used by the TX command. */
2675 desc->segs[0].addr = htole32(data->cmd_paddr);
2676 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad);
2677 /* Other DMA segments are for data payload. */
2679 for (i = 1; i <= nsegs; i++) {
2680 desc->segs[i].addr = htole32(seg->ds_addr);
2681 desc->segs[i].len = htole32(seg->ds_len);
2685 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2686 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2687 BUS_DMASYNC_PREWRITE);
2688 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2689 BUS_DMASYNC_PREWRITE);
2692 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2693 sc->sc_update_tx_ring(sc, ring);
2695 if (ring->qid < WPI_CMD_QUEUE_NUM) {
2696 /* Mark TX ring as full if we reach a certain threshold. */
2697 WPI_TXQ_STATE_LOCK(sc);
2698 if (++ring->queued > WPI_TX_RING_HIMARK)
2699 sc->qfullmsk |= 1 << ring->qid;
2700 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2701 WPI_TXQ_STATE_UNLOCK(sc);
2704 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2710 fail: m_freem(buf->m);
2712 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
2720 * Construct the data packet for a transmit buffer.
2723 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2725 const struct ieee80211_txparam *tp;
2726 struct ieee80211vap *vap = ni->ni_vap;
2727 struct ieee80211com *ic = ni->ni_ic;
2728 struct wpi_node *wn = WPI_NODE(ni);
2729 struct ieee80211_channel *chan;
2730 struct ieee80211_frame *wh;
2731 struct ieee80211_key *k = NULL;
2732 struct wpi_buf tx_data;
2733 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2737 int ac, error, swcrypt, rate, ismcast, totlen;
2739 wh = mtod(m, struct ieee80211_frame *);
2740 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2741 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2743 /* Select EDCA Access Category and TX ring for this frame. */
2744 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2745 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2746 tid = qos & IEEE80211_QOS_TID;
2751 ac = M_WME_GETAC(m);
2753 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
2754 ni->ni_chan : ic->ic_curchan;
2755 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2757 /* Choose a TX rate index. */
2758 if (type == IEEE80211_FC0_TYPE_MGT)
2759 rate = tp->mgmtrate;
2761 rate = tp->mcastrate;
2762 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2763 rate = tp->ucastrate;
2764 else if (m->m_flags & M_EAPOL)
2765 rate = tp->mgmtrate;
2767 /* XXX pass pktlen */
2768 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2769 rate = ni->ni_txrate;
2772 /* Encrypt the frame if need be. */
2773 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2774 /* Retrieve key for TX. */
2775 k = ieee80211_crypto_encap(ni, m);
2780 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2782 /* 802.11 header may have moved. */
2783 wh = mtod(m, struct ieee80211_frame *);
2785 totlen = m->m_pkthdr.len;
2787 if (ieee80211_radiotap_active_vap(vap)) {
2788 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2791 tap->wt_rate = rate;
2793 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2795 ieee80211_radiotap_tx(vap, m);
2800 /* Unicast frame, check if an ACK is expected. */
2801 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2802 IEEE80211_QOS_ACKPOLICY_NOACK)
2803 flags |= WPI_TX_NEED_ACK;
2806 if (!IEEE80211_QOS_HAS_SEQ(wh))
2807 flags |= WPI_TX_AUTO_SEQ;
2808 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2809 flags |= WPI_TX_MORE_FRAG; /* Cannot happen yet. */
2811 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2813 /* NB: Group frames are sent using CCK in 802.11b/g. */
2814 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2815 flags |= WPI_TX_NEED_RTS;
2816 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2817 WPI_RATE_IS_OFDM(rate)) {
2818 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2819 flags |= WPI_TX_NEED_CTS;
2820 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2821 flags |= WPI_TX_NEED_RTS;
2824 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2825 flags |= WPI_TX_FULL_TXOP;
2828 memset(tx, 0, sizeof (struct wpi_cmd_data));
2829 if (type == IEEE80211_FC0_TYPE_MGT) {
2830 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2832 /* Tell HW to set timestamp in probe responses. */
2833 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2834 flags |= WPI_TX_INSERT_TSTAMP;
2835 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2836 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2837 tx->timeout = htole16(3);
2839 tx->timeout = htole16(2);
2842 if (ismcast || type != IEEE80211_FC0_TYPE_DATA)
2843 tx->id = WPI_ID_BROADCAST;
2845 if (wn->id == WPI_ID_UNDEFINED) {
2846 device_printf(sc->sc_dev,
2847 "%s: undefined node id\n", __func__);
2855 if (k != NULL && !swcrypt) {
2856 switch (k->wk_cipher->ic_cipher) {
2857 case IEEE80211_CIPHER_AES_CCM:
2858 tx->security = WPI_CIPHER_CCMP;
2865 memcpy(tx->key, k->wk_key, k->wk_keylen);
2868 tx->len = htole16(totlen);
2869 tx->flags = htole32(flags);
2870 tx->plcp = rate2plcp(rate);
2872 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2873 tx->ofdm_mask = 0xff;
2874 tx->cck_mask = 0x0f;
2876 tx->data_ntries = tp->maxretry;
2880 tx_data.size = sizeof(struct wpi_cmd_data);
2881 tx_data.code = WPI_CMD_TX_DATA;
2884 return wpi_cmd2(sc, &tx_data);
2891 wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m,
2892 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
2894 struct ieee80211vap *vap = ni->ni_vap;
2895 struct ieee80211_key *k = NULL;
2896 struct ieee80211_frame *wh;
2897 struct wpi_buf tx_data;
2898 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2901 int ac, rate, swcrypt, totlen;
2903 wh = mtod(m, struct ieee80211_frame *);
2904 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2906 ac = params->ibp_pri & 3;
2908 /* Choose a TX rate index. */
2909 rate = params->ibp_rate0;
2912 if (!IEEE80211_QOS_HAS_SEQ(wh))
2913 flags |= WPI_TX_AUTO_SEQ;
2914 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
2915 flags |= WPI_TX_NEED_ACK;
2916 if (params->ibp_flags & IEEE80211_BPF_RTS)
2917 flags |= WPI_TX_NEED_RTS;
2918 if (params->ibp_flags & IEEE80211_BPF_CTS)
2919 flags |= WPI_TX_NEED_CTS;
2920 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2921 flags |= WPI_TX_FULL_TXOP;
2923 /* Encrypt the frame if need be. */
2924 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
2925 /* Retrieve key for TX. */
2926 k = ieee80211_crypto_encap(ni, m);
2931 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2933 /* 802.11 header may have moved. */
2934 wh = mtod(m, struct ieee80211_frame *);
2936 totlen = m->m_pkthdr.len;
2938 if (ieee80211_radiotap_active_vap(vap)) {
2939 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2942 tap->wt_rate = rate;
2943 if (params->ibp_flags & IEEE80211_BPF_CRYPTO)
2944 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2946 ieee80211_radiotap_tx(vap, m);
2949 memset(tx, 0, sizeof (struct wpi_cmd_data));
2950 if (type == IEEE80211_FC0_TYPE_MGT) {
2951 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2953 /* Tell HW to set timestamp in probe responses. */
2954 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2955 flags |= WPI_TX_INSERT_TSTAMP;
2956 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2957 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2958 tx->timeout = htole16(3);
2960 tx->timeout = htole16(2);
2963 if (k != NULL && !swcrypt) {
2964 switch (k->wk_cipher->ic_cipher) {
2965 case IEEE80211_CIPHER_AES_CCM:
2966 tx->security = WPI_CIPHER_CCMP;
2973 memcpy(tx->key, k->wk_key, k->wk_keylen);
2976 tx->len = htole16(totlen);
2977 tx->flags = htole32(flags);
2978 tx->plcp = rate2plcp(rate);
2979 tx->id = WPI_ID_BROADCAST;
2980 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2981 tx->rts_ntries = params->ibp_try1;
2982 tx->data_ntries = params->ibp_try0;
2986 tx_data.size = sizeof(struct wpi_cmd_data);
2987 tx_data.code = WPI_CMD_TX_DATA;
2990 return wpi_cmd2(sc, &tx_data);
2994 wpi_tx_ring_is_full(struct wpi_softc *sc, int ac)
2996 struct wpi_tx_ring *ring = &sc->txq[ac];
2999 WPI_TXQ_STATE_LOCK(sc);
3000 retval = (ring->queued > WPI_TX_RING_HIMARK);
3001 WPI_TXQ_STATE_UNLOCK(sc);
3006 static __inline void
3007 wpi_handle_tx_failure(struct ieee80211_node *ni)
3009 /* NB: m is reclaimed on tx failure */
3010 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
3011 ieee80211_free_node(ni);
3015 wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3016 const struct ieee80211_bpf_params *params)
3018 struct ieee80211com *ic = ni->ni_ic;
3019 struct wpi_softc *sc = ic->ic_softc;
3022 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3024 ac = M_WME_GETAC(m);
3028 if (sc->sc_running == 0 || wpi_tx_ring_is_full(sc, ac)) {
3030 error = sc->sc_running ? ENOBUFS : ENETDOWN;
3034 if (params == NULL) {
3036 * Legacy path; interpret frame contents to decide
3037 * precisely how to send the frame.
3039 error = wpi_tx_data(sc, m, ni);
3042 * Caller supplied explicit parameters to use in
3043 * sending the frame.
3045 error = wpi_tx_data_raw(sc, m, ni, params);
3048 unlock: WPI_TX_UNLOCK(sc);
3051 wpi_handle_tx_failure(ni);
3052 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3057 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3063 wpi_transmit(struct ieee80211com *ic, struct mbuf *m)
3065 struct wpi_softc *sc = ic->ic_softc;
3066 struct ieee80211_node *ni;
3071 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
3073 /* Check if interface is up & running. */
3074 if (sc->sc_running == 0) {
3079 /* Check for available space. */
3080 ac = M_WME_GETAC(m);
3081 sndq = &sc->txq[ac].snd;
3082 if (wpi_tx_ring_is_full(sc, ac) || mbufq_len(sndq) != 0) {
3083 /* wpi_tx_done() will dequeue it. */
3084 error = mbufq_enqueue(sndq, m);
3089 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3090 if (wpi_tx_data(sc, m, ni) != 0) {
3091 wpi_handle_tx_failure(ni);
3094 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
3096 unlock: WPI_TX_UNLOCK(sc);
3102 * Process data waiting to be sent on the output queue
3105 wpi_start(void *arg0, int pending)
3107 struct wpi_softc *sc = arg0;
3108 struct ieee80211_node *ni;
3113 if (sc->sc_running == 0)
3116 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
3118 for (i = 0; i < WPI_CMD_QUEUE_NUM; i++) {
3119 struct mbufq *sndq = &sc->txq[i].snd;
3122 if (wpi_tx_ring_is_full(sc, i))
3125 if ((m = mbufq_dequeue(sndq)) == NULL)
3128 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3129 if (wpi_tx_data(sc, m, ni) != 0) {
3130 wpi_handle_tx_failure(ni);
3135 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
3136 unlock: WPI_TX_UNLOCK(sc);
3140 wpi_watchdog_rfkill(void *arg)
3142 struct wpi_softc *sc = arg;
3143 struct ieee80211com *ic = &sc->sc_ic;
3145 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n");
3147 /* No need to lock firmware memory. */
3148 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) {
3149 /* Radio kill switch is still off. */
3150 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
3153 ieee80211_runtask(ic, &sc->sc_radioon_task);
3157 wpi_scan_timeout(void *arg)
3159 struct wpi_softc *sc = arg;
3160 struct ieee80211com *ic = &sc->sc_ic;
3162 ic_printf(ic, "scan timeout\n");
3163 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
3167 wpi_tx_timeout(void *arg)
3169 struct wpi_softc *sc = arg;
3170 struct ieee80211com *ic = &sc->sc_ic;
3172 ic_printf(ic, "device timeout\n");
3173 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
3177 wpi_parent(struct ieee80211com *ic)
3179 struct wpi_softc *sc = ic->ic_softc;
3180 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3182 if (ic->ic_nrunning > 0) {
3183 if (wpi_init(sc) == 0) {
3184 ieee80211_notify_radio(ic, 1);
3185 ieee80211_start_all(ic);
3187 ieee80211_notify_radio(ic, 0);
3188 ieee80211_stop(vap);
3195 * Send a command to the firmware.
3198 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, size_t size,
3201 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
3202 struct wpi_tx_desc *desc;
3203 struct wpi_tx_data *data;
3204 struct wpi_tx_cmd *cmd;
3211 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3213 if (sc->sc_running == 0) {
3214 /* wpi_stop() was called */
3215 if (code == WPI_CMD_SCAN)
3224 WPI_LOCK_ASSERT(sc);
3226 DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %zu async %d\n",
3227 __func__, wpi_cmd_str(code), size, async);
3229 desc = &ring->desc[ring->cur];
3230 data = &ring->data[ring->cur];
3233 if (size > sizeof cmd->data) {
3234 /* Command is too large to fit in a descriptor. */
3235 if (totlen > MCLBYTES) {
3239 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3244 cmd = mtod(m, struct wpi_tx_cmd *);
3245 error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3246 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3253 cmd = &ring->cmd[ring->cur];
3254 paddr = data->cmd_paddr;
3259 cmd->qid = ring->qid;
3260 cmd->idx = ring->cur;
3261 memcpy(cmd->data, buf, size);
3263 desc->nsegs = 1 + (WPI_PAD32(size) << 4);
3264 desc->segs[0].addr = htole32(paddr);
3265 desc->segs[0].len = htole32(totlen);
3267 if (size > sizeof cmd->data) {
3268 bus_dmamap_sync(ring->data_dmat, data->map,
3269 BUS_DMASYNC_PREWRITE);
3271 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3272 BUS_DMASYNC_PREWRITE);
3274 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3275 BUS_DMASYNC_PREWRITE);
3277 /* Kick command ring. */
3278 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
3279 sc->sc_update_tx_ring(sc, ring);
3281 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3285 return async ? 0 : mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
3287 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3295 * Configure HW multi-rate retries.
3298 wpi_mrr_setup(struct wpi_softc *sc)
3300 struct ieee80211com *ic = &sc->sc_ic;
3301 struct wpi_mrr_setup mrr;
3304 /* CCK rates (not used with 802.11a). */
3305 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
3306 mrr.rates[i].flags = 0;
3307 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3308 /* Fallback to the immediate lower CCK rate (if any.) */
3310 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
3311 /* Try twice at this rate before falling back to "next". */
3312 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3314 /* OFDM rates (not used with 802.11b). */
3315 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
3316 mrr.rates[i].flags = 0;
3317 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3318 /* Fallback to the immediate lower rate (if any.) */
3319 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
3320 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
3321 ((ic->ic_curmode == IEEE80211_MODE_11A) ?
3322 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
3324 /* Try twice at this rate before falling back to "next". */
3325 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3327 /* Setup MRR for control frames. */
3328 mrr.which = htole32(WPI_MRR_CTL);
3329 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3331 device_printf(sc->sc_dev,
3332 "could not setup MRR for control frames\n");
3335 /* Setup MRR for data frames. */
3336 mrr.which = htole32(WPI_MRR_DATA);
3337 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3339 device_printf(sc->sc_dev,
3340 "could not setup MRR for data frames\n");
3347 wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3349 struct ieee80211com *ic = ni->ni_ic;
3350 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
3351 struct wpi_node *wn = WPI_NODE(ni);
3352 struct wpi_node_info node;
3355 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3357 if (wn->id == WPI_ID_UNDEFINED)
3360 memset(&node, 0, sizeof node);
3361 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3363 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3364 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3365 node.action = htole32(WPI_ACTION_SET_RATE);
3366 node.antenna = WPI_ANTENNA_BOTH;
3368 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding node %d (%s)\n", __func__,
3369 wn->id, ether_sprintf(ni->ni_macaddr));
3371 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
3373 device_printf(sc->sc_dev,
3374 "%s: wpi_cmd() call failed with error code %d\n", __func__,
3379 if (wvp->wv_gtk != 0) {
3380 error = wpi_set_global_keys(ni);
3382 device_printf(sc->sc_dev,
3383 "%s: error while setting global keys\n", __func__);
3392 * Broadcast node is used to send group-addressed and management frames.
3395 wpi_add_broadcast_node(struct wpi_softc *sc, int async)
3397 struct ieee80211com *ic = &sc->sc_ic;
3398 struct wpi_node_info node;
3400 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3402 memset(&node, 0, sizeof node);
3403 IEEE80211_ADDR_COPY(node.macaddr, ieee80211broadcastaddr);
3404 node.id = WPI_ID_BROADCAST;
3405 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3406 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3407 node.action = htole32(WPI_ACTION_SET_RATE);
3408 node.antenna = WPI_ANTENNA_BOTH;
3410 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__);
3412 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async);
3416 wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3418 struct wpi_node *wn = WPI_NODE(ni);
3421 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3423 wn->id = wpi_add_node_entry_sta(sc);
3425 if ((error = wpi_add_node(sc, ni)) != 0) {
3426 wpi_del_node_entry(sc, wn->id);
3427 wn->id = WPI_ID_UNDEFINED;
3435 wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3437 struct wpi_node *wn = WPI_NODE(ni);
3440 KASSERT(wn->id == WPI_ID_UNDEFINED,
3441 ("the node %d was added before", wn->id));
3443 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3445 if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) {
3446 device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__);
3450 if ((error = wpi_add_node(sc, ni)) != 0) {
3451 wpi_del_node_entry(sc, wn->id);
3452 wn->id = WPI_ID_UNDEFINED;
3460 wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3462 struct wpi_node *wn = WPI_NODE(ni);
3463 struct wpi_cmd_del_node node;
3466 KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed"));
3468 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3470 memset(&node, 0, sizeof node);
3471 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3474 DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__,
3475 wn->id, ether_sprintf(ni->ni_macaddr));
3477 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1);
3479 device_printf(sc->sc_dev,
3480 "%s: could not delete node %u, error %d\n", __func__,
3486 wpi_updateedca(struct ieee80211com *ic)
3488 #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3489 struct wpi_softc *sc = ic->ic_softc;
3490 struct wpi_edca_params cmd;
3493 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3495 memset(&cmd, 0, sizeof cmd);
3496 cmd.flags = htole32(WPI_EDCA_UPDATE);
3497 for (aci = 0; aci < WME_NUM_AC; aci++) {
3498 const struct wmeParams *ac =
3499 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci];
3500 cmd.ac[aci].aifsn = ac->wmep_aifsn;
3501 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin));
3502 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax));
3503 cmd.ac[aci].txoplimit =
3504 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit));
3506 DPRINTF(sc, WPI_DEBUG_EDCA,
3507 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
3508 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn,
3509 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax,
3510 cmd.ac[aci].txoplimit);
3512 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3514 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3521 wpi_set_promisc(struct wpi_softc *sc)
3523 struct ieee80211com *ic = &sc->sc_ic;
3524 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3525 uint32_t promisc_filter;
3527 promisc_filter = WPI_FILTER_CTL;
3528 if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP)
3529 promisc_filter |= WPI_FILTER_PROMISC;
3531 if (ic->ic_promisc > 0)
3532 sc->rxon.filter |= htole32(promisc_filter);
3534 sc->rxon.filter &= ~htole32(promisc_filter);
3538 wpi_update_promisc(struct ieee80211com *ic)
3540 struct wpi_softc *sc = ic->ic_softc;
3543 wpi_set_promisc(sc);
3545 if (wpi_send_rxon(sc, 1, 1) != 0) {
3546 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3549 WPI_RXON_UNLOCK(sc);
3553 wpi_update_mcast(struct ieee80211com *ic)
3559 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3561 struct wpi_cmd_led led;
3563 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3566 led.unit = htole32(100000); /* on/off in unit of 100ms */
3569 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
3573 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
3575 struct wpi_cmd_timing cmd;
3578 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3580 memset(&cmd, 0, sizeof cmd);
3581 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3582 cmd.bintval = htole16(ni->ni_intval);
3583 cmd.lintval = htole16(10);
3585 /* Compute remaining time until next beacon. */
3586 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU;
3587 mod = le64toh(cmd.tstamp) % val;
3588 cmd.binitval = htole32((uint32_t)(val - mod));
3590 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3591 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
3593 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
3597 * This function is called periodically (every 60 seconds) to adjust output
3598 * power to temperature changes.
3601 wpi_power_calibration(struct wpi_softc *sc)
3605 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3607 /* Update sensor data. */
3608 temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
3609 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp);
3611 /* Sanity-check read value. */
3612 if (temp < -260 || temp > 25) {
3613 /* This can't be correct, ignore. */
3614 DPRINTF(sc, WPI_DEBUG_TEMP,
3615 "out-of-range temperature reported: %d\n", temp);
3619 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp);
3621 /* Adjust Tx power if need be. */
3622 if (abs(temp - sc->temp) <= 6)
3627 if (wpi_set_txpower(sc, 1) != 0) {
3628 /* just warn, too bad for the automatic calibration... */
3629 device_printf(sc->sc_dev,"could not adjust Tx power\n");
3634 * Set TX power for current channel.
3637 wpi_set_txpower(struct wpi_softc *sc, int async)
3639 struct wpi_power_group *group;
3640 struct wpi_cmd_txpower cmd;
3642 int idx, is_chan_5ghz, i;
3644 /* Retrieve current channel from last RXON. */
3645 chan = sc->rxon.chan;
3646 is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0;
3648 /* Find the TX power group to which this channel belongs. */
3650 for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3651 if (chan <= group->chan)
3654 group = &sc->groups[0];
3656 memset(&cmd, 0, sizeof cmd);
3657 cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ;
3658 cmd.chan = htole16(chan);
3660 /* Set TX power for all OFDM and CCK rates. */
3661 for (i = 0; i <= WPI_RIDX_MAX ; i++) {
3662 /* Retrieve TX power for this channel/rate. */
3663 idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i);
3665 cmd.rates[i].plcp = wpi_ridx_to_plcp[i];
3668 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
3669 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
3671 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
3672 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
3674 DPRINTF(sc, WPI_DEBUG_TEMP,
3675 "chan %d/ridx %d: power index %d\n", chan, i, idx);
3678 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
3682 * Determine Tx power index for a given channel/rate combination.
3683 * This takes into account the regulatory information from EEPROM and the
3684 * current temperature.
3687 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3688 uint8_t chan, int is_chan_5ghz, int ridx)
3690 /* Fixed-point arithmetic division using a n-bit fractional part. */
3691 #define fdivround(a, b, n) \
3692 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3694 /* Linear interpolation. */
3695 #define interpolate(x, x1, y1, x2, y2, n) \
3696 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3698 struct wpi_power_sample *sample;
3701 /* Default TX power is group maximum TX power minus 3dB. */
3702 pwr = group->maxpwr / 2;
3704 /* Decrease TX power for highest OFDM rates to reduce distortion. */
3706 case WPI_RIDX_OFDM36:
3707 pwr -= is_chan_5ghz ? 5 : 0;
3709 case WPI_RIDX_OFDM48:
3710 pwr -= is_chan_5ghz ? 10 : 7;
3712 case WPI_RIDX_OFDM54:
3713 pwr -= is_chan_5ghz ? 12 : 9;
3717 /* Never exceed the channel maximum allowed TX power. */
3718 pwr = min(pwr, sc->maxpwr[chan]);
3720 /* Retrieve TX power index into gain tables from samples. */
3721 for (sample = group->samples; sample < &group->samples[3]; sample++)
3722 if (pwr > sample[1].power)
3724 /* Fixed-point linear interpolation using a 19-bit fractional part. */
3725 idx = interpolate(pwr, sample[0].power, sample[0].index,
3726 sample[1].power, sample[1].index, 19);
3729 * Adjust power index based on current temperature:
3730 * - if cooler than factory-calibrated: decrease output power
3731 * - if warmer than factory-calibrated: increase output power
3733 idx -= (sc->temp - group->temp) * 11 / 100;
3735 /* Decrease TX power for CCK rates (-5dB). */
3736 if (ridx >= WPI_RIDX_CCK1)
3739 /* Make sure idx stays in a valid range. */
3742 if (idx > WPI_MAX_PWR_INDEX)
3743 return WPI_MAX_PWR_INDEX;
3751 * Set STA mode power saving level (between 0 and 5).
3752 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
3755 wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async)
3757 struct wpi_pmgt_cmd cmd;
3758 const struct wpi_pmgt *pmgt;
3759 uint32_t max, skip_dtim;
3763 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
3764 "%s: dtim=%d, level=%d, async=%d\n",
3765 __func__, dtim, level, async);
3767 /* Select which PS parameters to use. */
3769 pmgt = &wpi_pmgt[0][level];
3771 pmgt = &wpi_pmgt[1][level];
3773 memset(&cmd, 0, sizeof cmd);
3775 if (level != 0) { /* not CAM */
3776 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
3777 sc->sc_flags |= WPI_PS_PATH;
3779 sc->sc_flags &= ~WPI_PS_PATH;
3781 /* Retrieve PCIe Active State Power Management (ASPM). */
3782 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
3783 if (!(reg & 0x1)) /* L0s Entry disabled. */
3784 cmd.flags |= htole16(WPI_PS_PCI_PMGT);
3786 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU);
3787 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU);
3793 skip_dtim = pmgt->skip_dtim;
3795 if (skip_dtim != 0) {
3796 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
3797 max = pmgt->intval[4];
3798 if (max == (uint32_t)-1)
3799 max = dtim * (skip_dtim + 1);
3800 else if (max > dtim)
3801 max = (max / dtim) * dtim;
3805 for (i = 0; i < 5; i++)
3806 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
3808 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
3812 wpi_send_btcoex(struct wpi_softc *sc)
3814 struct wpi_bluetooth cmd;
3816 memset(&cmd, 0, sizeof cmd);
3817 cmd.flags = WPI_BT_COEX_MODE_4WIRE;
3818 cmd.lead_time = WPI_BT_LEAD_TIME_DEF;
3819 cmd.max_kill = WPI_BT_MAX_KILL_DEF;
3820 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n",
3822 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0);
3826 wpi_send_rxon(struct wpi_softc *sc, int assoc, int async)
3831 WPI_RXON_LOCK_ASSERT(sc);
3833 if (assoc && wpi_check_bss_filter(sc) != 0) {
3834 struct wpi_assoc rxon_assoc;
3836 rxon_assoc.flags = sc->rxon.flags;
3837 rxon_assoc.filter = sc->rxon.filter;
3838 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask;
3839 rxon_assoc.cck_mask = sc->rxon.cck_mask;
3840 rxon_assoc.reserved = 0;
3842 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc,
3843 sizeof (struct wpi_assoc), async);
3845 device_printf(sc->sc_dev,
3846 "RXON_ASSOC command failed, error %d\n", error);
3852 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3853 sizeof (struct wpi_rxon), async);
3855 wpi_clear_node_table(sc);
3858 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3859 sizeof (struct wpi_rxon), async);
3861 wpi_clear_node_table(sc);
3865 device_printf(sc->sc_dev,
3866 "RXON command failed, error %d\n", error);
3870 /* Add broadcast node. */
3871 error = wpi_add_broadcast_node(sc, async);
3873 device_printf(sc->sc_dev,
3874 "could not add broadcast node, error %d\n", error);
3879 /* Configuration has changed, set Tx power accordingly. */
3880 if ((error = wpi_set_txpower(sc, async)) != 0) {
3881 device_printf(sc->sc_dev,
3882 "%s: could not set TX power, error %d\n", __func__, error);
3890 * Configure the card to listen to a particular channel, this transisions the
3891 * card in to being able to receive frames from remote devices.
3894 wpi_config(struct wpi_softc *sc)
3896 struct ieee80211com *ic = &sc->sc_ic;
3897 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3898 struct ieee80211_channel *c = ic->ic_curchan;
3901 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3903 /* Set power saving level to CAM during initialization. */
3904 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
3905 device_printf(sc->sc_dev,
3906 "%s: could not set power saving level\n", __func__);
3910 /* Configure bluetooth coexistence. */
3911 if ((error = wpi_send_btcoex(sc)) != 0) {
3912 device_printf(sc->sc_dev,
3913 "could not configure bluetooth coexistence\n");
3917 /* Configure adapter. */
3918 memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
3919 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr);
3921 /* Set default channel. */
3922 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
3923 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
3924 if (IEEE80211_IS_CHAN_2GHZ(c))
3925 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
3927 sc->rxon.filter = WPI_FILTER_MULTICAST;
3928 switch (ic->ic_opmode) {
3929 case IEEE80211_M_STA:
3930 sc->rxon.mode = WPI_MODE_STA;
3932 case IEEE80211_M_IBSS:
3933 sc->rxon.mode = WPI_MODE_IBSS;
3934 sc->rxon.filter |= WPI_FILTER_BEACON;
3936 case IEEE80211_M_HOSTAP:
3937 /* XXX workaround for beaconing */
3938 sc->rxon.mode = WPI_MODE_IBSS;
3939 sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC;
3941 case IEEE80211_M_AHDEMO:
3942 sc->rxon.mode = WPI_MODE_HOSTAP;
3944 case IEEE80211_M_MONITOR:
3945 sc->rxon.mode = WPI_MODE_MONITOR;
3948 device_printf(sc->sc_dev, "unknown opmode %d\n",
3952 sc->rxon.filter = htole32(sc->rxon.filter);
3953 wpi_set_promisc(sc);
3954 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
3955 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
3957 /* XXX Current configuration may be unusable. */
3958 if (IEEE80211_IS_CHAN_NOADHOC(c) && sc->rxon.mode == WPI_MODE_IBSS) {
3959 device_printf(sc->sc_dev,
3960 "%s: invalid channel (%d) selected for IBSS mode\n",
3961 __func__, ieee80211_chan2ieee(ic, c));
3965 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) {
3966 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3971 /* Setup rate scalling. */
3972 if ((error = wpi_mrr_setup(sc)) != 0) {
3973 device_printf(sc->sc_dev, "could not setup MRR, error %d\n",
3978 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3984 wpi_get_active_dwell_time(struct wpi_softc *sc,
3985 struct ieee80211_channel *c, uint8_t n_probes)
3987 /* No channel? Default to 2GHz settings. */
3988 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) {
3989 return (WPI_ACTIVE_DWELL_TIME_2GHZ +
3990 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1));
3993 /* 5GHz dwell time. */
3994 return (WPI_ACTIVE_DWELL_TIME_5GHZ +
3995 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1));
3999 * Limit the total dwell time.
4001 * Returns the dwell time in milliseconds.
4004 wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time)
4006 struct ieee80211com *ic = &sc->sc_ic;
4007 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4010 /* bintval is in TU (1.024mS) */
4012 bintval = vap->iv_bss->ni_intval;
4015 * If it's non-zero, we should calculate the minimum of
4016 * it and the DWELL_BASE.
4018 * XXX Yes, the math should take into account that bintval
4019 * is 1.024mS, not 1mS..
4022 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__,
4024 return (MIN(dwell_time, bintval - WPI_CHANNEL_TUNE_TIME * 2));
4027 /* No association context? Default. */
4032 wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c)
4036 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c))
4037 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ;
4039 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ;
4041 /* Clamp to the beacon interval if we're associated. */
4042 return (wpi_limit_dwell(sc, passive));
4046 wpi_get_scan_pause_time(uint32_t time, uint16_t bintval)
4048 uint32_t mod = (time % bintval) * IEEE80211_DUR_TU;
4049 uint32_t nbeacons = time / bintval;
4051 if (mod > WPI_PAUSE_MAX_TIME)
4052 mod = WPI_PAUSE_MAX_TIME;
4054 return WPI_PAUSE_SCAN(nbeacons, mod);
4058 * Send a scan request to the firmware.
4061 wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c)
4063 struct ieee80211com *ic = &sc->sc_ic;
4064 struct ieee80211_scan_state *ss = ic->ic_scan;
4065 struct ieee80211vap *vap = ss->ss_vap;
4066 struct wpi_scan_hdr *hdr;
4067 struct wpi_cmd_data *tx;
4068 struct wpi_scan_essid *essids;
4069 struct wpi_scan_chan *chan;
4070 struct ieee80211_frame *wh;
4071 struct ieee80211_rateset *rs;
4072 uint16_t dwell_active, dwell_passive;
4074 int bgscan, bintval, buflen, error, i, nssid;
4076 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4079 * We are absolutely not allowed to send a scan command when another
4080 * scan command is pending.
4082 if (callout_pending(&sc->scan_timeout)) {
4083 device_printf(sc->sc_dev, "%s: called whilst scanning!\n",
4089 bgscan = wpi_check_bss_filter(sc);
4090 bintval = vap->iv_bss->ni_intval;
4092 bintval < WPI_QUIET_TIME_DEFAULT + WPI_CHANNEL_TUNE_TIME * 2) {
4097 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
4099 device_printf(sc->sc_dev,
4100 "%s: could not allocate buffer for scan command\n",
4105 hdr = (struct wpi_scan_hdr *)buf;
4108 * Move to the next channel if no packets are received within 10 msecs
4109 * after sending the probe request.
4111 hdr->quiet_time = htole16(WPI_QUIET_TIME_DEFAULT);
4112 hdr->quiet_threshold = htole16(1);
4116 * Max needs to be greater than active and passive and quiet!
4117 * It's also in microseconds!
4119 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU);
4120 hdr->pause_svc = htole32(wpi_get_scan_pause_time(100,
4124 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON);
4126 tx = (struct wpi_cmd_data *)(hdr + 1);
4127 tx->flags = htole32(WPI_TX_AUTO_SEQ);
4128 tx->id = WPI_ID_BROADCAST;
4129 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
4131 if (IEEE80211_IS_CHAN_5GHZ(c)) {
4132 /* Send probe requests at 6Mbps. */
4133 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6];
4134 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4136 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
4137 /* Send probe requests at 1Mbps. */
4138 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4139 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4142 essids = (struct wpi_scan_essid *)(tx + 1);
4143 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
4144 for (i = 0; i < nssid; i++) {
4145 essids[i].id = IEEE80211_ELEMID_SSID;
4146 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN);
4147 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len);
4149 if (sc->sc_debug & WPI_DEBUG_SCAN) {
4150 printf("Scanning Essid: ");
4151 ieee80211_print_essid(essids[i].data, essids[i].len);
4158 * Build a probe request frame. Most of the following code is a
4159 * copy & paste of what is done in net80211.
4161 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS);
4162 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4163 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4164 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
4165 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4166 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
4167 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4169 frm = (uint8_t *)(wh + 1);
4170 frm = ieee80211_add_ssid(frm, NULL, 0);
4171 frm = ieee80211_add_rates(frm, rs);
4172 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4173 frm = ieee80211_add_xrates(frm, rs);
4175 /* Set length of probe request. */
4176 tx->len = htole16(frm - (uint8_t *)wh);
4179 * Construct information about the channel that we
4180 * want to scan. The firmware expects this to be directly
4181 * after the scan probe request
4183 chan = (struct wpi_scan_chan *)frm;
4184 chan->chan = htole16(ieee80211_chan2ieee(ic, c));
4187 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT;
4188 chan->flags |= WPI_CHAN_NPBREQS(nssid);
4190 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER;
4192 if (!IEEE80211_IS_CHAN_PASSIVE(c))
4193 chan->flags |= WPI_CHAN_ACTIVE;
4196 * Calculate the active/passive dwell times.
4198 dwell_active = wpi_get_active_dwell_time(sc, c, nssid);
4199 dwell_passive = wpi_get_passive_dwell_time(sc, c);
4201 /* Make sure they're valid. */
4202 if (dwell_active > dwell_passive)
4203 dwell_active = dwell_passive;
4205 chan->active = htole16(dwell_active);
4206 chan->passive = htole16(dwell_passive);
4208 chan->dsp_gain = 0x6e; /* Default level */
4210 if (IEEE80211_IS_CHAN_5GHZ(c))
4211 chan->rf_gain = 0x3b;
4213 chan->rf_gain = 0x28;
4215 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n",
4216 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c));
4220 if (hdr->nchan == 1 && sc->rxon.chan == chan->chan) {
4221 /* XXX Force probe request transmission. */
4222 memcpy(chan + 1, chan, sizeof (struct wpi_scan_chan));
4226 /* Reduce unnecessary delay. */
4228 chan->passive = chan->active = hdr->quiet_time;
4235 buflen = (uint8_t *)chan - buf;
4236 hdr->len = htole16(buflen);
4238 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n",
4240 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
4241 free(buf, M_DEVBUF);
4246 callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc);
4248 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4252 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
4258 wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
4260 struct ieee80211com *ic = vap->iv_ic;
4261 struct ieee80211_node *ni = vap->iv_bss;
4262 struct ieee80211_channel *c = ni->ni_chan;
4267 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4269 /* Update adapter configuration. */
4270 sc->rxon.associd = 0;
4271 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
4272 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4273 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4274 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4275 if (IEEE80211_IS_CHAN_2GHZ(c))
4276 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4277 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4278 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4279 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4280 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4281 if (IEEE80211_IS_CHAN_A(c)) {
4282 sc->rxon.cck_mask = 0;
4283 sc->rxon.ofdm_mask = 0x15;
4284 } else if (IEEE80211_IS_CHAN_B(c)) {
4285 sc->rxon.cck_mask = 0x03;
4286 sc->rxon.ofdm_mask = 0;
4288 /* Assume 802.11b/g. */
4289 sc->rxon.cck_mask = 0x0f;
4290 sc->rxon.ofdm_mask = 0x15;
4293 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n",
4294 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask,
4295 sc->rxon.ofdm_mask);
4297 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4298 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4302 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4304 WPI_RXON_UNLOCK(sc);
4310 wpi_config_beacon(struct wpi_vap *wvp)
4312 struct ieee80211com *ic = wvp->wv_vap.iv_ic;
4313 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4314 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4315 struct wpi_softc *sc = ic->ic_softc;
4316 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
4317 struct ieee80211_tim_ie *tie;
4322 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4324 WPI_VAP_LOCK_ASSERT(wvp);
4326 cmd->len = htole16(bcn->m->m_pkthdr.len);
4327 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4328 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4330 /* XXX seems to be unused */
4331 if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) {
4332 tie = (struct ieee80211_tim_ie *) bo->bo_tim;
4333 ptr = mtod(bcn->m, uint8_t *);
4335 cmd->tim = htole16(bo->bo_tim - ptr);
4336 cmd->timsz = tie->tim_len;
4339 /* Necessary for recursion in ieee80211_beacon_update(). */
4341 bcn->m = m_dup(m, M_NOWAIT);
4342 if (bcn->m == NULL) {
4343 device_printf(sc->sc_dev,
4344 "%s: could not copy beacon frame\n", __func__);
4349 if ((error = wpi_cmd2(sc, bcn)) != 0) {
4350 device_printf(sc->sc_dev,
4351 "%s: could not update beacon frame, error %d", __func__,
4362 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
4364 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
4365 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4366 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4370 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4372 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
4375 m = ieee80211_beacon_alloc(ni, bo);
4377 device_printf(sc->sc_dev,
4378 "%s: could not allocate beacon frame\n", __func__);
4388 error = wpi_config_beacon(wvp);
4389 WPI_VAP_UNLOCK(wvp);
4395 wpi_update_beacon(struct ieee80211vap *vap, int item)
4397 struct wpi_softc *sc = vap->iv_ic->ic_softc;
4398 struct wpi_vap *wvp = WPI_VAP(vap);
4399 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4400 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4401 struct ieee80211_node *ni = vap->iv_bss;
4404 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4407 if (bcn->m == NULL) {
4408 bcn->m = ieee80211_beacon_alloc(ni, bo);
4409 if (bcn->m == NULL) {
4410 device_printf(sc->sc_dev,
4411 "%s: could not allocate beacon frame\n", __func__);
4413 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR,
4416 WPI_VAP_UNLOCK(wvp);
4420 WPI_VAP_UNLOCK(wvp);
4422 if (item == IEEE80211_BEACON_TIM)
4423 mcast = 1; /* TODO */
4425 setbit(bo->bo_flags, item);
4426 ieee80211_beacon_update(ni, bo, bcn->m, mcast);
4429 wpi_config_beacon(wvp);
4430 WPI_VAP_UNLOCK(wvp);
4432 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4436 wpi_newassoc(struct ieee80211_node *ni, int isnew)
4438 struct ieee80211vap *vap = ni->ni_vap;
4439 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4440 struct wpi_node *wn = WPI_NODE(ni);
4445 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4447 if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) {
4448 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
4449 device_printf(sc->sc_dev,
4450 "%s: could not add IBSS node, error %d\n",
4458 wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
4460 struct ieee80211com *ic = vap->iv_ic;
4461 struct ieee80211_node *ni = vap->iv_bss;
4462 struct ieee80211_channel *c = ni->ni_chan;
4465 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4467 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
4468 /* Link LED blinks while monitoring. */
4469 wpi_set_led(sc, WPI_LED_LINK, 5, 5);
4473 /* XXX kernel panic workaround */
4474 if (c == IEEE80211_CHAN_ANYC) {
4475 device_printf(sc->sc_dev, "%s: incomplete configuration\n",
4480 if ((error = wpi_set_timing(sc, ni)) != 0) {
4481 device_printf(sc->sc_dev,
4482 "%s: could not set timing, error %d\n", __func__, error);
4486 /* Update adapter configuration. */
4488 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4489 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni));
4490 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4491 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4492 if (IEEE80211_IS_CHAN_2GHZ(c))
4493 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4494 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4495 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4496 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4497 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4498 if (IEEE80211_IS_CHAN_A(c)) {
4499 sc->rxon.cck_mask = 0;
4500 sc->rxon.ofdm_mask = 0x15;
4501 } else if (IEEE80211_IS_CHAN_B(c)) {
4502 sc->rxon.cck_mask = 0x03;
4503 sc->rxon.ofdm_mask = 0;
4505 /* Assume 802.11b/g. */
4506 sc->rxon.cck_mask = 0x0f;
4507 sc->rxon.ofdm_mask = 0x15;
4509 sc->rxon.filter |= htole32(WPI_FILTER_BSS);
4511 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n",
4512 sc->rxon.chan, sc->rxon.flags);
4514 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4515 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4520 /* Start periodic calibration timer. */
4521 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
4523 WPI_RXON_UNLOCK(sc);
4525 if (vap->iv_opmode == IEEE80211_M_IBSS ||
4526 vap->iv_opmode == IEEE80211_M_HOSTAP) {
4527 if ((error = wpi_setup_beacon(sc, ni)) != 0) {
4528 device_printf(sc->sc_dev,
4529 "%s: could not setup beacon, error %d\n", __func__,
4535 if (vap->iv_opmode == IEEE80211_M_STA) {
4538 error = wpi_add_sta_node(sc, ni);
4541 device_printf(sc->sc_dev,
4542 "%s: could not add BSS node, error %d\n", __func__,
4548 /* Link LED always on while associated. */
4549 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
4551 /* Enable power-saving mode if requested by user. */
4552 if ((vap->iv_flags & IEEE80211_F_PMGTON) &&
4553 vap->iv_opmode != IEEE80211_M_IBSS)
4554 (void)wpi_set_pslevel(sc, 0, 3, 1);
4556 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4562 wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4564 const struct ieee80211_cipher *cip = k->wk_cipher;
4565 struct ieee80211vap *vap = ni->ni_vap;
4566 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4567 struct wpi_node *wn = WPI_NODE(ni);
4568 struct wpi_node_info node;
4572 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4574 if (wpi_check_node_entry(sc, wn->id) == 0) {
4575 device_printf(sc->sc_dev, "%s: node does not exist\n",
4580 switch (cip->ic_cipher) {
4581 case IEEE80211_CIPHER_AES_CCM:
4582 kflags = WPI_KFLAG_CCMP;
4586 device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__,
4591 kflags |= WPI_KFLAG_KID(k->wk_keyix);
4592 if (k->wk_flags & IEEE80211_KEY_GROUP)
4593 kflags |= WPI_KFLAG_MULTICAST;
4595 memset(&node, 0, sizeof node);
4597 node.control = WPI_NODE_UPDATE;
4598 node.flags = WPI_FLAG_KEY_SET;
4599 node.kflags = htole16(kflags);
4600 memcpy(node.key, k->wk_key, k->wk_keylen);
4602 DPRINTF(sc, WPI_DEBUG_KEY,
4603 "%s: setting %s key id %d for node %d (%s)\n", __func__,
4604 (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix,
4605 node.id, ether_sprintf(ni->ni_macaddr));
4607 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4609 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4614 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4615 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4616 kflags |= WPI_KFLAG_MULTICAST;
4617 node.kflags = htole16(kflags);
4626 wpi_load_key_cb(void *arg, struct ieee80211_node *ni)
4628 const struct ieee80211_key *k = arg;
4629 struct ieee80211vap *vap = ni->ni_vap;
4630 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4631 struct wpi_node *wn = WPI_NODE(ni);
4634 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4638 error = wpi_load_key(ni, k);
4642 device_printf(sc->sc_dev, "%s: error while setting key\n",
4648 wpi_set_global_keys(struct ieee80211_node *ni)
4650 struct ieee80211vap *vap = ni->ni_vap;
4651 struct ieee80211_key *wk = &vap->iv_nw_keys[0];
4654 for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++)
4655 if (wk->wk_keyix != IEEE80211_KEYIX_NONE)
4656 error = wpi_load_key(ni, wk);
4662 wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4664 struct ieee80211vap *vap = ni->ni_vap;
4665 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4666 struct wpi_node *wn = WPI_NODE(ni);
4667 struct wpi_node_info node;
4671 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4673 if (wpi_check_node_entry(sc, wn->id) == 0) {
4674 DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__);
4675 return 1; /* Nothing to do. */
4678 kflags = WPI_KFLAG_KID(k->wk_keyix);
4679 if (k->wk_flags & IEEE80211_KEY_GROUP)
4680 kflags |= WPI_KFLAG_MULTICAST;
4682 memset(&node, 0, sizeof node);
4684 node.control = WPI_NODE_UPDATE;
4685 node.flags = WPI_FLAG_KEY_SET;
4686 node.kflags = htole16(kflags);
4688 DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n",
4689 __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast",
4690 k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr));
4692 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4694 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4699 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4700 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4701 kflags |= WPI_KFLAG_MULTICAST;
4702 node.kflags = htole16(kflags);
4711 wpi_del_key_cb(void *arg, struct ieee80211_node *ni)
4713 const struct ieee80211_key *k = arg;
4714 struct ieee80211vap *vap = ni->ni_vap;
4715 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4716 struct wpi_node *wn = WPI_NODE(ni);
4719 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4723 error = wpi_del_key(ni, k);
4727 device_printf(sc->sc_dev, "%s: error while deleting key\n",
4733 wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
4736 struct ieee80211com *ic = vap->iv_ic;
4737 struct wpi_softc *sc = ic->ic_softc;
4738 struct wpi_vap *wvp = WPI_VAP(vap);
4739 struct ieee80211_node *ni;
4740 int error, ni_ref = 0;
4742 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4744 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
4749 if (!(k->wk_flags & IEEE80211_KEY_RECV)) {
4750 /* XMIT keys are handled in wpi_tx_data(). */
4754 /* Handle group keys. */
4755 if (&vap->iv_nw_keys[0] <= k &&
4756 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4759 wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix);
4761 wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix);
4764 if (vap->iv_state == IEEE80211_S_RUN) {
4765 ieee80211_iterate_nodes(&ic->ic_sta,
4766 set ? wpi_load_key_cb : wpi_del_key_cb,
4767 __DECONST(void *, k));
4773 switch (vap->iv_opmode) {
4774 case IEEE80211_M_STA:
4778 case IEEE80211_M_IBSS:
4779 case IEEE80211_M_AHDEMO:
4780 case IEEE80211_M_HOSTAP:
4781 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
4783 return 0; /* should not happen */
4789 device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__,
4796 error = wpi_load_key(ni, k);
4798 error = wpi_del_key(ni, k);
4802 ieee80211_node_decref(ni);
4808 wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
4809 const uint8_t mac[IEEE80211_ADDR_LEN])
4811 return wpi_process_key(vap, k, 1);
4815 wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4817 return wpi_process_key(vap, k, 0);
4821 * This function is called after the runtime firmware notifies us of its
4822 * readiness (called in a process context).
4825 wpi_post_alive(struct wpi_softc *sc)
4829 /* Check (again) that the radio is not disabled. */
4830 if ((error = wpi_nic_lock(sc)) != 0)
4833 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4835 /* NB: Runtime firmware must be up and running. */
4836 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
4837 device_printf(sc->sc_dev,
4838 "RF switch: radio disabled (%s)\n", __func__);
4840 return EPERM; /* :-) */
4844 /* Wait for thermal sensor to calibrate. */
4845 for (ntries = 0; ntries < 1000; ntries++) {
4846 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
4851 if (ntries == 1000) {
4852 device_printf(sc->sc_dev,
4853 "timeout waiting for thermal sensor calibration\n");
4857 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp);
4862 * The firmware boot code is small and is intended to be copied directly into
4863 * the NIC internal memory (no DMA transfer).
4866 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
4870 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size);
4872 size /= sizeof (uint32_t);
4874 if ((error = wpi_nic_lock(sc)) != 0)
4877 /* Copy microcode image into NIC memory. */
4878 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
4879 (const uint32_t *)ucode, size);
4881 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
4882 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
4883 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
4885 /* Start boot load now. */
4886 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
4888 /* Wait for transfer to complete. */
4889 for (ntries = 0; ntries < 1000; ntries++) {
4890 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS);
4891 DPRINTF(sc, WPI_DEBUG_HW,
4892 "firmware status=0x%x, val=0x%x, result=0x%x\n", status,
4893 WPI_FH_TX_STATUS_IDLE(6),
4894 status & WPI_FH_TX_STATUS_IDLE(6));
4895 if (status & WPI_FH_TX_STATUS_IDLE(6)) {
4896 DPRINTF(sc, WPI_DEBUG_HW,
4897 "Status Match! - ntries = %d\n", ntries);
4902 if (ntries == 1000) {
4903 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4909 /* Enable boot after power up. */
4910 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
4917 wpi_load_firmware(struct wpi_softc *sc)
4919 struct wpi_fw_info *fw = &sc->fw;
4920 struct wpi_dma_info *dma = &sc->fw_dma;
4923 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4925 /* Copy initialization sections into pre-allocated DMA-safe memory. */
4926 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
4927 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4928 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz);
4929 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4931 /* Tell adapter where to find initialization sections. */
4932 if ((error = wpi_nic_lock(sc)) != 0)
4934 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4935 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
4936 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4937 dma->paddr + WPI_FW_DATA_MAXSZ);
4938 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
4941 /* Load firmware boot code. */
4942 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
4944 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4949 /* Now press "execute". */
4950 WPI_WRITE(sc, WPI_RESET, 0);
4952 /* Wait at most one second for first alive notification. */
4953 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
4954 device_printf(sc->sc_dev,
4955 "%s: timeout waiting for adapter to initialize, error %d\n",
4960 /* Copy runtime sections into pre-allocated DMA-safe memory. */
4961 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
4962 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4963 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz);
4964 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4966 /* Tell adapter where to find runtime sections. */
4967 if ((error = wpi_nic_lock(sc)) != 0)
4969 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4970 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
4971 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4972 dma->paddr + WPI_FW_DATA_MAXSZ);
4973 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
4974 WPI_FW_UPDATED | fw->main.textsz);
4981 wpi_read_firmware(struct wpi_softc *sc)
4983 const struct firmware *fp;
4984 struct wpi_fw_info *fw = &sc->fw;
4985 const struct wpi_firmware_hdr *hdr;
4988 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4990 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4991 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME);
4994 fp = firmware_get(WPI_FW_NAME);
4998 device_printf(sc->sc_dev,
4999 "could not load firmware image '%s'\n", WPI_FW_NAME);
5005 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
5006 device_printf(sc->sc_dev,
5007 "firmware file too short: %zu bytes\n", fp->datasize);
5012 fw->size = fp->datasize;
5013 fw->data = (const uint8_t *)fp->data;
5015 /* Extract firmware header information. */
5016 hdr = (const struct wpi_firmware_hdr *)fw->data;
5018 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW |
5019 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
5021 fw->main.textsz = le32toh(hdr->rtextsz);
5022 fw->main.datasz = le32toh(hdr->rdatasz);
5023 fw->init.textsz = le32toh(hdr->itextsz);
5024 fw->init.datasz = le32toh(hdr->idatasz);
5025 fw->boot.textsz = le32toh(hdr->btextsz);
5026 fw->boot.datasz = 0;
5028 /* Sanity-check firmware header. */
5029 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
5030 fw->main.datasz > WPI_FW_DATA_MAXSZ ||
5031 fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
5032 fw->init.datasz > WPI_FW_DATA_MAXSZ ||
5033 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
5034 (fw->boot.textsz & 3) != 0) {
5035 device_printf(sc->sc_dev, "invalid firmware header\n");
5040 /* Check that all firmware sections fit. */
5041 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
5042 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
5043 device_printf(sc->sc_dev,
5044 "firmware file too short: %zu bytes\n", fw->size);
5049 /* Get pointers to firmware sections. */
5050 fw->main.text = (const uint8_t *)(hdr + 1);
5051 fw->main.data = fw->main.text + fw->main.textsz;
5052 fw->init.text = fw->main.data + fw->main.datasz;
5053 fw->init.data = fw->init.text + fw->init.textsz;
5054 fw->boot.text = fw->init.data + fw->init.datasz;
5056 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
5057 "Firmware Version: Major %d, Minor %d, Driver %d, \n"
5058 "runtime (text: %u, data: %u) init (text: %u, data %u) "
5059 "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver),
5060 fw->main.textsz, fw->main.datasz,
5061 fw->init.textsz, fw->init.datasz, fw->boot.textsz);
5063 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text);
5064 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data);
5065 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text);
5066 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data);
5067 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text);
5071 fail: wpi_unload_firmware(sc);
5076 * Free the referenced firmware image
5079 wpi_unload_firmware(struct wpi_softc *sc)
5081 if (sc->fw_fp != NULL) {
5082 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
5088 wpi_clock_wait(struct wpi_softc *sc)
5092 /* Set "initialization complete" bit. */
5093 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5095 /* Wait for clock stabilization. */
5096 for (ntries = 0; ntries < 2500; ntries++) {
5097 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
5101 device_printf(sc->sc_dev,
5102 "%s: timeout waiting for clock stabilization\n", __func__);
5108 wpi_apm_init(struct wpi_softc *sc)
5113 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5115 /* Disable L0s exit timer (NMI bug workaround). */
5116 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER);
5117 /* Don't wait for ICH L0s (ICH bug workaround). */
5118 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
5120 /* Set FH wait threshold to max (HW bug under stress workaround). */
5121 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000);
5123 /* Retrieve PCIe Active State Power Management (ASPM). */
5124 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
5125 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
5126 if (reg & 0x02) /* L1 Entry enabled. */
5127 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5129 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5131 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
5133 /* Wait for clock stabilization before accessing prph. */
5134 if ((error = wpi_clock_wait(sc)) != 0)
5137 if ((error = wpi_nic_lock(sc)) != 0)
5140 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400);
5141 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200);
5143 /* Enable DMA and BSM (Bootstrap State Machine). */
5144 wpi_prph_write(sc, WPI_APMG_CLK_EN,
5145 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT);
5147 /* Disable L1-Active. */
5148 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
5155 wpi_apm_stop_master(struct wpi_softc *sc)
5159 /* Stop busmaster DMA activity. */
5160 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
5162 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
5163 WPI_GP_CNTRL_MAC_PS)
5164 return; /* Already asleep. */
5166 for (ntries = 0; ntries < 100; ntries++) {
5167 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
5171 device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
5176 wpi_apm_stop(struct wpi_softc *sc)
5178 wpi_apm_stop_master(sc);
5180 /* Reset the entire device. */
5181 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
5183 /* Clear "initialization complete" bit. */
5184 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5188 wpi_nic_config(struct wpi_softc *sc)
5192 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5194 /* voodoo from the Linux "driver".. */
5195 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
5196 if ((rev & 0xc0) == 0x40)
5197 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
5198 else if (!(rev & 0x80))
5199 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
5201 if (sc->cap == 0x80)
5202 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
5204 if ((sc->rev & 0xf0) == 0xd0)
5205 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5207 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5210 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
5214 wpi_hw_init(struct wpi_softc *sc)
5216 int chnl, ntries, error;
5218 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5220 /* Clear pending interrupts. */
5221 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5223 if ((error = wpi_apm_init(sc)) != 0) {
5224 device_printf(sc->sc_dev,
5225 "%s: could not power ON adapter, error %d\n", __func__,
5230 /* Select VMAIN power source. */
5231 if ((error = wpi_nic_lock(sc)) != 0)
5233 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
5235 /* Spin until VMAIN gets selected. */
5236 for (ntries = 0; ntries < 5000; ntries++) {
5237 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
5241 if (ntries == 5000) {
5242 device_printf(sc->sc_dev, "timeout selecting power source\n");
5246 /* Perform adapter initialization. */
5249 /* Initialize RX ring. */
5250 if ((error = wpi_nic_lock(sc)) != 0)
5252 /* Set physical address of RX ring. */
5253 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
5254 /* Set physical address of RX read pointer. */
5255 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
5256 offsetof(struct wpi_shared, next));
5257 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
5259 WPI_WRITE(sc, WPI_FH_RX_CONFIG,
5260 WPI_FH_RX_CONFIG_DMA_ENA |
5261 WPI_FH_RX_CONFIG_RDRBD_ENA |
5262 WPI_FH_RX_CONFIG_WRSTATUS_ENA |
5263 WPI_FH_RX_CONFIG_MAXFRAG |
5264 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
5265 WPI_FH_RX_CONFIG_IRQ_DST_HOST |
5266 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1));
5267 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */
5269 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
5271 /* Initialize TX rings. */
5272 if ((error = wpi_nic_lock(sc)) != 0)
5274 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */
5275 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */
5276 /* Enable all 6 TX rings. */
5277 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
5278 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
5279 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
5280 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
5281 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
5282 /* Set physical address of TX rings. */
5283 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
5284 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
5286 /* Enable all DMA channels. */
5287 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5288 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
5289 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
5290 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
5293 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */
5295 /* Clear "radio off" and "commands blocked" bits. */
5296 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5297 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
5299 /* Clear pending interrupts. */
5300 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5301 /* Enable interrupts. */
5302 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
5304 /* _Really_ make sure "radio off" bit is cleared! */
5305 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5306 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5308 if ((error = wpi_load_firmware(sc)) != 0) {
5309 device_printf(sc->sc_dev,
5310 "%s: could not load firmware, error %d\n", __func__,
5314 /* Wait at most one second for firmware alive notification. */
5315 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
5316 device_printf(sc->sc_dev,
5317 "%s: timeout waiting for adapter to initialize, error %d\n",
5322 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5324 /* Do post-firmware initialization. */
5325 return wpi_post_alive(sc);
5329 wpi_hw_stop(struct wpi_softc *sc)
5331 int chnl, qid, ntries;
5333 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5335 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP)
5338 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
5340 /* Disable interrupts. */
5341 WPI_WRITE(sc, WPI_INT_MASK, 0);
5342 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5343 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
5345 /* Make sure we no longer hold the NIC lock. */
5348 if (wpi_nic_lock(sc) == 0) {
5349 /* Stop TX scheduler. */
5350 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
5351 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
5353 /* Stop all DMA channels. */
5354 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5355 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
5356 for (ntries = 0; ntries < 200; ntries++) {
5357 if (WPI_READ(sc, WPI_FH_TX_STATUS) &
5358 WPI_FH_TX_STATUS_IDLE(chnl))
5367 wpi_reset_rx_ring(sc);
5369 /* Reset all TX rings. */
5370 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
5371 wpi_reset_tx_ring(sc, &sc->txq[qid]);
5373 if (wpi_nic_lock(sc) == 0) {
5374 wpi_prph_write(sc, WPI_APMG_CLK_DIS,
5375 WPI_APMG_CLK_CTRL_DMA_CLK_RQT);
5379 /* Power OFF adapter. */
5384 wpi_radio_on(void *arg0, int pending)
5386 struct wpi_softc *sc = arg0;
5387 struct ieee80211com *ic = &sc->sc_ic;
5388 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5390 device_printf(sc->sc_dev, "RF switch: radio enabled\n");
5393 callout_stop(&sc->watchdog_rfkill);
5397 ieee80211_init(vap);
5401 wpi_radio_off(void *arg0, int pending)
5403 struct wpi_softc *sc = arg0;
5404 struct ieee80211com *ic = &sc->sc_ic;
5405 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5407 device_printf(sc->sc_dev, "RF switch: radio disabled\n");
5409 ieee80211_notify_radio(ic, 0);
5412 ieee80211_stop(vap);
5415 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc);
5420 wpi_init(struct wpi_softc *sc)
5426 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5428 if (sc->sc_running != 0)
5431 /* Check that the radio is not disabled by hardware switch. */
5432 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
5433 device_printf(sc->sc_dev,
5434 "RF switch: radio disabled (%s)\n", __func__);
5435 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
5437 error = EINPROGRESS;
5441 /* Read firmware images from the filesystem. */
5442 if ((error = wpi_read_firmware(sc)) != 0) {
5443 device_printf(sc->sc_dev,
5444 "%s: could not read firmware, error %d\n", __func__,
5451 /* Initialize hardware and upload firmware. */
5452 error = wpi_hw_init(sc);
5453 wpi_unload_firmware(sc);
5455 device_printf(sc->sc_dev,
5456 "%s: could not initialize hardware, error %d\n", __func__,
5461 /* Configure adapter now that it is ready. */
5462 if ((error = wpi_config(sc)) != 0) {
5463 device_printf(sc->sc_dev,
5464 "%s: could not configure device, error %d\n", __func__,
5469 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5475 fail: wpi_stop_locked(sc);
5477 end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
5484 wpi_stop_locked(struct wpi_softc *sc)
5487 WPI_LOCK_ASSERT(sc);
5489 if (sc->sc_running == 0)
5498 WPI_TXQ_STATE_LOCK(sc);
5499 callout_stop(&sc->tx_timeout);
5500 WPI_TXQ_STATE_UNLOCK(sc);
5503 callout_stop(&sc->scan_timeout);
5504 callout_stop(&sc->calib_to);
5505 WPI_RXON_UNLOCK(sc);
5507 /* Power OFF hardware. */
5512 wpi_stop(struct wpi_softc *sc)
5515 wpi_stop_locked(sc);
5520 * Callback from net80211 to start a scan.
5523 wpi_scan_start(struct ieee80211com *ic)
5525 struct wpi_softc *sc = ic->ic_softc;
5527 wpi_set_led(sc, WPI_LED_LINK, 20, 2);
5531 * Callback from net80211 to terminate a scan.
5534 wpi_scan_end(struct ieee80211com *ic)
5536 struct wpi_softc *sc = ic->ic_softc;
5537 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5539 if (vap->iv_state == IEEE80211_S_RUN)
5540 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
5544 * Called by the net80211 framework to indicate to the driver
5545 * that the channel should be changed
5548 wpi_set_channel(struct ieee80211com *ic)
5550 const struct ieee80211_channel *c = ic->ic_curchan;
5551 struct wpi_softc *sc = ic->ic_softc;
5554 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5557 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
5558 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
5561 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
5562 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
5566 * Only need to set the channel in Monitor mode. AP scanning and auth
5567 * are already taken care of by their respective firmware commands.
5569 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
5571 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
5572 if (IEEE80211_IS_CHAN_2GHZ(c)) {
5573 sc->rxon.flags |= htole32(WPI_RXON_AUTO |
5576 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO |
5579 if ((error = wpi_send_rxon(sc, 0, 1)) != 0)
5580 device_printf(sc->sc_dev,
5581 "%s: error %d setting channel\n", __func__,
5583 WPI_RXON_UNLOCK(sc);
5588 * Called by net80211 to indicate that we need to scan the current
5589 * channel. The channel is previously be set via the wpi_set_channel
5593 wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
5595 struct ieee80211vap *vap = ss->ss_vap;
5596 struct ieee80211com *ic = vap->iv_ic;
5597 struct wpi_softc *sc = ic->ic_softc;
5601 error = wpi_scan(sc, ic->ic_curchan);
5602 WPI_RXON_UNLOCK(sc);
5604 ieee80211_cancel_scan(vap);
5608 * Called by the net80211 framework to indicate
5609 * the minimum dwell time has been met, terminate the scan.
5610 * We don't actually terminate the scan as the firmware will notify
5611 * us when it's finished and we have no way to interrupt it.
5614 wpi_scan_mindwell(struct ieee80211_scan_state *ss)
5616 /* NB: don't try to abort scan; wait for firmware to finish */
5620 wpi_hw_reset(void *arg, int pending)
5622 struct wpi_softc *sc = arg;
5623 struct ieee80211com *ic = &sc->sc_ic;
5624 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5626 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5628 ieee80211_notify_radio(ic, 0);
5629 if (vap != NULL && (ic->ic_flags & IEEE80211_F_SCAN))
5630 ieee80211_cancel_scan(vap);
5634 ieee80211_stop(vap);
5635 ieee80211_init(vap);