2 * Copyright (c) 2006,2007
3 * Damien Bergamini <damien.bergamini@free.fr>
4 * Benjamin Close <Benjamin.Close@clearchain.com>
5 * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
26 * The 3945ABG network adapter doesn't use traditional hardware as
27 * many other adaptors do. Instead at run time the eeprom is set into a known
28 * state and told to load boot firmware. The boot firmware loads an init and a
29 * main binary firmware image into SRAM on the card via DMA.
30 * Once the firmware is loaded, the driver/hw then
31 * communicate by way of circular dma rings via the SRAM to the firmware.
33 * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings.
34 * The 4 tx data rings allow for prioritization QoS.
36 * The rx data ring consists of 32 dma buffers. Two registers are used to
37 * indicate where in the ring the driver and the firmware are up to. The
38 * driver sets the initial read index (reg1) and the initial write index (reg2),
39 * the firmware updates the read index (reg1) on rx of a packet and fires an
40 * interrupt. The driver then processes the buffers starting at reg1 indicating
41 * to the firmware which buffers have been accessed by updating reg2. At the
42 * same time allocating new memory for the processed buffer.
44 * A similar thing happens with the tx rings. The difference is the firmware
45 * stop processing buffers once the queue is full and until confirmation
46 * of a successful transmition (tx_done) has occurred.
48 * The command ring operates in the same manner as the tx queues.
50 * All communication direct to the card (ie eeprom) is classed as Stage1
53 * All communication via the firmware to the card is classed as State2.
54 * The firmware consists of 2 parts. A bootstrap firmware and a runtime
55 * firmware. The bootstrap firmware and runtime firmware are loaded
56 * from host memory via dma to the card then told to execute. From this point
57 * on the majority of communications between the driver and the card goes
64 #include <sys/param.h>
65 #include <sys/sysctl.h>
66 #include <sys/sockio.h>
68 #include <sys/kernel.h>
69 #include <sys/socket.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/queue.h>
73 #include <sys/taskqueue.h>
74 #include <sys/module.h>
76 #include <sys/endian.h>
77 #include <sys/linker.h>
78 #include <sys/firmware.h>
80 #include <machine/bus.h>
81 #include <machine/resource.h>
84 #include <dev/pci/pcireg.h>
85 #include <dev/pci/pcivar.h>
89 #include <net/if_var.h>
90 #include <net/if_arp.h>
91 #include <net/ethernet.h>
92 #include <net/if_dl.h>
93 #include <net/if_media.h>
94 #include <net/if_types.h>
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/in_var.h>
99 #include <netinet/if_ether.h>
100 #include <netinet/ip.h>
102 #include <net80211/ieee80211_var.h>
103 #include <net80211/ieee80211_radiotap.h>
104 #include <net80211/ieee80211_regdomain.h>
105 #include <net80211/ieee80211_ratectl.h>
107 #include <dev/wpi/if_wpireg.h>
108 #include <dev/wpi/if_wpivar.h>
109 #include <dev/wpi/if_wpi_debug.h>
118 static const struct wpi_ident wpi_ident_table[] = {
119 /* The below entries support ABG regardless of the subid */
120 { 0x8086, 0x4222, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
121 { 0x8086, 0x4227, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
122 /* The below entries only support BG */
123 { 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG" },
124 { 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG" },
125 { 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG" },
126 { 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG" },
130 static int wpi_probe(device_t);
131 static int wpi_attach(device_t);
132 static void wpi_radiotap_attach(struct wpi_softc *);
133 static void wpi_sysctlattach(struct wpi_softc *);
134 static void wpi_init_beacon(struct wpi_vap *);
135 static struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
136 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
137 const uint8_t [IEEE80211_ADDR_LEN],
138 const uint8_t [IEEE80211_ADDR_LEN]);
139 static void wpi_vap_delete(struct ieee80211vap *);
140 static int wpi_detach(device_t);
141 static int wpi_shutdown(device_t);
142 static int wpi_suspend(device_t);
143 static int wpi_resume(device_t);
144 static int wpi_nic_lock(struct wpi_softc *);
145 static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
146 static void wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
147 static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
148 void **, bus_size_t, bus_size_t);
149 static void wpi_dma_contig_free(struct wpi_dma_info *);
150 static int wpi_alloc_shared(struct wpi_softc *);
151 static void wpi_free_shared(struct wpi_softc *);
152 static int wpi_alloc_fwmem(struct wpi_softc *);
153 static void wpi_free_fwmem(struct wpi_softc *);
154 static int wpi_alloc_rx_ring(struct wpi_softc *);
155 static void wpi_update_rx_ring(struct wpi_softc *);
156 static void wpi_update_rx_ring_ps(struct wpi_softc *);
157 static void wpi_reset_rx_ring(struct wpi_softc *);
158 static void wpi_free_rx_ring(struct wpi_softc *);
159 static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
161 static void wpi_update_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
162 static void wpi_update_tx_ring_ps(struct wpi_softc *,
163 struct wpi_tx_ring *);
164 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
165 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
166 static int wpi_read_eeprom(struct wpi_softc *,
167 uint8_t macaddr[IEEE80211_ADDR_LEN]);
168 static uint32_t wpi_eeprom_channel_flags(struct wpi_eeprom_chan *);
169 static void wpi_read_eeprom_band(struct wpi_softc *, uint8_t, int, int *,
170 struct ieee80211_channel[]);
171 static int wpi_read_eeprom_channels(struct wpi_softc *, uint8_t);
172 static struct wpi_eeprom_chan *wpi_find_eeprom_channel(struct wpi_softc *,
173 struct ieee80211_channel *);
174 static void wpi_getradiocaps(struct ieee80211com *, int, int *,
175 struct ieee80211_channel[]);
176 static int wpi_setregdomain(struct ieee80211com *,
177 struct ieee80211_regdomain *, int,
178 struct ieee80211_channel[]);
179 static int wpi_read_eeprom_group(struct wpi_softc *, uint8_t);
180 static struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
181 const uint8_t mac[IEEE80211_ADDR_LEN]);
182 static void wpi_node_free(struct ieee80211_node *);
183 static void wpi_ibss_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
184 const struct ieee80211_rx_stats *,
186 static void wpi_restore_node(void *, struct ieee80211_node *);
187 static void wpi_restore_node_table(struct wpi_softc *, struct wpi_vap *);
188 static int wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
189 static void wpi_calib_timeout(void *);
190 static void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *,
191 struct wpi_rx_data *);
192 static void wpi_rx_statistics(struct wpi_softc *, struct wpi_rx_desc *,
193 struct wpi_rx_data *);
194 static void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *);
195 static void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *);
196 static void wpi_notif_intr(struct wpi_softc *);
197 static void wpi_wakeup_intr(struct wpi_softc *);
199 static void wpi_debug_registers(struct wpi_softc *);
201 static void wpi_fatal_intr(struct wpi_softc *);
202 static void wpi_intr(void *);
203 static void wpi_free_txfrags(struct wpi_softc *, uint16_t);
204 static int wpi_cmd2(struct wpi_softc *, struct wpi_buf *);
205 static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
206 struct ieee80211_node *);
207 static int wpi_tx_data_raw(struct wpi_softc *, struct mbuf *,
208 struct ieee80211_node *,
209 const struct ieee80211_bpf_params *);
210 static int wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
211 const struct ieee80211_bpf_params *);
212 static int wpi_transmit(struct ieee80211com *, struct mbuf *);
213 static void wpi_watchdog_rfkill(void *);
214 static void wpi_scan_timeout(void *);
215 static void wpi_tx_timeout(void *);
216 static void wpi_parent(struct ieee80211com *);
217 static int wpi_cmd(struct wpi_softc *, uint8_t, const void *, uint16_t,
219 static int wpi_mrr_setup(struct wpi_softc *);
220 static int wpi_add_node(struct wpi_softc *, struct ieee80211_node *);
221 static int wpi_add_broadcast_node(struct wpi_softc *, int);
222 static int wpi_add_ibss_node(struct wpi_softc *, struct ieee80211_node *);
223 static void wpi_del_node(struct wpi_softc *, struct ieee80211_node *);
224 static int wpi_updateedca(struct ieee80211com *);
225 static void wpi_set_promisc(struct wpi_softc *);
226 static void wpi_update_promisc(struct ieee80211com *);
227 static void wpi_update_mcast(struct ieee80211com *);
228 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
229 static int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *);
230 static void wpi_power_calibration(struct wpi_softc *);
231 static int wpi_set_txpower(struct wpi_softc *, int);
232 static int wpi_get_power_index(struct wpi_softc *,
233 struct wpi_power_group *, uint8_t, int, int);
234 static int wpi_set_pslevel(struct wpi_softc *, uint8_t, int, int);
235 static int wpi_send_btcoex(struct wpi_softc *);
236 static int wpi_send_rxon(struct wpi_softc *, int, int);
237 static int wpi_config(struct wpi_softc *);
238 static uint16_t wpi_get_active_dwell_time(struct wpi_softc *,
239 struct ieee80211_channel *, uint8_t);
240 static uint16_t wpi_limit_dwell(struct wpi_softc *, uint16_t);
241 static uint16_t wpi_get_passive_dwell_time(struct wpi_softc *,
242 struct ieee80211_channel *);
243 static uint32_t wpi_get_scan_pause_time(uint32_t, uint16_t);
244 static int wpi_scan(struct wpi_softc *, struct ieee80211_channel *);
245 static int wpi_auth(struct wpi_softc *, struct ieee80211vap *);
246 static int wpi_config_beacon(struct wpi_vap *);
247 static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
248 static void wpi_update_beacon(struct ieee80211vap *, int);
249 static void wpi_newassoc(struct ieee80211_node *, int);
250 static int wpi_run(struct wpi_softc *, struct ieee80211vap *);
251 static int wpi_load_key(struct ieee80211_node *,
252 const struct ieee80211_key *);
253 static void wpi_load_key_cb(void *, struct ieee80211_node *);
254 static int wpi_set_global_keys(struct ieee80211_node *);
255 static int wpi_del_key(struct ieee80211_node *,
256 const struct ieee80211_key *);
257 static void wpi_del_key_cb(void *, struct ieee80211_node *);
258 static int wpi_process_key(struct ieee80211vap *,
259 const struct ieee80211_key *, int);
260 static int wpi_key_set(struct ieee80211vap *,
261 const struct ieee80211_key *);
262 static int wpi_key_delete(struct ieee80211vap *,
263 const struct ieee80211_key *);
264 static int wpi_post_alive(struct wpi_softc *);
265 static int wpi_load_bootcode(struct wpi_softc *, const uint8_t *,
267 static int wpi_load_firmware(struct wpi_softc *);
268 static int wpi_read_firmware(struct wpi_softc *);
269 static void wpi_unload_firmware(struct wpi_softc *);
270 static int wpi_clock_wait(struct wpi_softc *);
271 static int wpi_apm_init(struct wpi_softc *);
272 static void wpi_apm_stop_master(struct wpi_softc *);
273 static void wpi_apm_stop(struct wpi_softc *);
274 static void wpi_nic_config(struct wpi_softc *);
275 static int wpi_hw_init(struct wpi_softc *);
276 static void wpi_hw_stop(struct wpi_softc *);
277 static void wpi_radio_on(void *, int);
278 static void wpi_radio_off(void *, int);
279 static int wpi_init(struct wpi_softc *);
280 static void wpi_stop_locked(struct wpi_softc *);
281 static void wpi_stop(struct wpi_softc *);
282 static void wpi_scan_start(struct ieee80211com *);
283 static void wpi_scan_end(struct ieee80211com *);
284 static void wpi_set_channel(struct ieee80211com *);
285 static void wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
286 static void wpi_scan_mindwell(struct ieee80211_scan_state *);
287 static void wpi_hw_reset(void *, int);
289 static device_method_t wpi_methods[] = {
290 /* Device interface */
291 DEVMETHOD(device_probe, wpi_probe),
292 DEVMETHOD(device_attach, wpi_attach),
293 DEVMETHOD(device_detach, wpi_detach),
294 DEVMETHOD(device_shutdown, wpi_shutdown),
295 DEVMETHOD(device_suspend, wpi_suspend),
296 DEVMETHOD(device_resume, wpi_resume),
301 static driver_t wpi_driver = {
304 sizeof (struct wpi_softc)
306 static devclass_t wpi_devclass;
308 DRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, NULL, NULL);
310 MODULE_VERSION(wpi, 1);
312 MODULE_DEPEND(wpi, pci, 1, 1, 1);
313 MODULE_DEPEND(wpi, wlan, 1, 1, 1);
314 MODULE_DEPEND(wpi, firmware, 1, 1, 1);
317 wpi_probe(device_t dev)
319 const struct wpi_ident *ident;
321 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
322 if (pci_get_vendor(dev) == ident->vendor &&
323 pci_get_device(dev) == ident->device) {
324 device_set_desc(dev, ident->name);
325 return (BUS_PROBE_DEFAULT);
332 wpi_attach(device_t dev)
334 struct wpi_softc *sc = (struct wpi_softc *)device_get_softc(dev);
335 struct ieee80211com *ic;
340 const struct wpi_ident *ident;
346 error = resource_int_value(device_get_name(sc->sc_dev),
347 device_get_unit(sc->sc_dev), "debug", &(sc->sc_debug));
354 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
357 * Get the offset of the PCI Express Capability Structure in PCI
358 * Configuration Space.
360 error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off);
362 device_printf(dev, "PCIe capability structure not found!\n");
367 * Some card's only support 802.11b/g not a, check to see if
368 * this is one such card. A 0x0 in the subdevice table indicates
369 * the entire subdevice range is to be ignored.
372 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
373 if (ident->subdevice &&
374 pci_get_subdevice(dev) == ident->subdevice) {
381 /* Clear device-specific "PCI retry timeout" register (41h). */
382 pci_write_config(dev, 0x41, 0, 1);
384 /* Enable bus-mastering. */
385 pci_enable_busmaster(dev);
388 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
390 if (sc->mem == NULL) {
391 device_printf(dev, "can't map mem space\n");
394 sc->sc_st = rman_get_bustag(sc->mem);
395 sc->sc_sh = rman_get_bushandle(sc->mem);
398 if (pci_alloc_msi(dev, &rid) == 0)
402 /* Install interrupt handler. */
403 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
404 (rid != 0 ? 0 : RF_SHAREABLE));
405 if (sc->irq == NULL) {
406 device_printf(dev, "can't map interrupt\n");
412 WPI_TX_LOCK_INIT(sc);
413 WPI_RXON_LOCK_INIT(sc);
414 WPI_NT_LOCK_INIT(sc);
415 WPI_TXQ_LOCK_INIT(sc);
416 WPI_TXQ_STATE_LOCK_INIT(sc);
418 /* Allocate DMA memory for firmware transfers. */
419 if ((error = wpi_alloc_fwmem(sc)) != 0) {
421 "could not allocate memory for firmware, error %d\n",
426 /* Allocate shared page. */
427 if ((error = wpi_alloc_shared(sc)) != 0) {
428 device_printf(dev, "could not allocate shared page\n");
432 /* Allocate TX rings - 4 for QoS purposes, 1 for commands. */
433 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
434 if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) {
436 "could not allocate TX ring %d, error %d\n", i,
442 /* Allocate RX ring. */
443 if ((error = wpi_alloc_rx_ring(sc)) != 0) {
444 device_printf(dev, "could not allocate RX ring, error %d\n",
449 /* Clear pending interrupts. */
450 WPI_WRITE(sc, WPI_INT, 0xffffffff);
454 ic->ic_name = device_get_nameunit(dev);
455 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
456 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
458 /* Set device capabilities. */
460 IEEE80211_C_STA /* station mode supported */
461 | IEEE80211_C_IBSS /* IBSS mode supported */
462 | IEEE80211_C_HOSTAP /* Host access point mode */
463 | IEEE80211_C_MONITOR /* monitor mode supported */
464 | IEEE80211_C_AHDEMO /* adhoc demo mode */
465 | IEEE80211_C_BGSCAN /* capable of bg scanning */
466 | IEEE80211_C_TXFRAG /* handle tx frags */
467 | IEEE80211_C_TXPMGT /* tx power management */
468 | IEEE80211_C_SHSLOT /* short slot time supported */
469 | IEEE80211_C_WPA /* 802.11i */
470 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
471 | IEEE80211_C_WME /* 802.11e */
472 | IEEE80211_C_PMGT /* Station-side power mgmt */
476 IEEE80211_CRYPTO_AES_CCM;
479 * Read in the eeprom and also setup the channels for
480 * net80211. We don't set the rates as net80211 does this for us
482 if ((error = wpi_read_eeprom(sc, ic->ic_macaddr)) != 0) {
483 device_printf(dev, "could not read EEPROM, error %d\n",
490 device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n",
492 device_printf(sc->sc_dev, "Hardware Type: %c\n",
493 sc->type > 1 ? 'B': '?');
494 device_printf(sc->sc_dev, "Hardware Revision: %c\n",
495 ((sc->rev & 0xf0) == 0xd0) ? 'D': '?');
496 device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
497 supportsa ? "does" : "does not");
499 /* XXX hw_config uses the PCIDEV for the Hardware rev. Must
500 check what sc->rev really represents - benjsc 20070615 */
504 ieee80211_ifattach(ic);
505 ic->ic_vap_create = wpi_vap_create;
506 ic->ic_vap_delete = wpi_vap_delete;
507 ic->ic_parent = wpi_parent;
508 ic->ic_raw_xmit = wpi_raw_xmit;
509 ic->ic_transmit = wpi_transmit;
510 ic->ic_node_alloc = wpi_node_alloc;
511 sc->sc_node_free = ic->ic_node_free;
512 ic->ic_node_free = wpi_node_free;
513 ic->ic_wme.wme_update = wpi_updateedca;
514 ic->ic_update_promisc = wpi_update_promisc;
515 ic->ic_update_mcast = wpi_update_mcast;
516 ic->ic_newassoc = wpi_newassoc;
517 ic->ic_scan_start = wpi_scan_start;
518 ic->ic_scan_end = wpi_scan_end;
519 ic->ic_set_channel = wpi_set_channel;
520 ic->ic_scan_curchan = wpi_scan_curchan;
521 ic->ic_scan_mindwell = wpi_scan_mindwell;
522 ic->ic_getradiocaps = wpi_getradiocaps;
523 ic->ic_setregdomain = wpi_setregdomain;
525 sc->sc_update_rx_ring = wpi_update_rx_ring;
526 sc->sc_update_tx_ring = wpi_update_tx_ring;
528 wpi_radiotap_attach(sc);
530 callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0);
531 callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0);
532 callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0);
533 callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0);
534 TASK_INIT(&sc->sc_reinittask, 0, wpi_hw_reset, sc);
535 TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc);
536 TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc);
538 sc->sc_tq = taskqueue_create("wpi_taskq", M_WAITOK,
539 taskqueue_thread_enqueue, &sc->sc_tq);
540 error = taskqueue_start_threads(&sc->sc_tq, 1, 0, "wpi_taskq");
542 device_printf(dev, "can't start threads, error %d\n", error);
546 wpi_sysctlattach(sc);
549 * Hook our interrupt after all initialization is complete.
551 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
552 NULL, wpi_intr, sc, &sc->sc_ih);
554 device_printf(dev, "can't establish interrupt, error %d\n",
560 ieee80211_announce(ic);
563 if (sc->sc_debug & WPI_DEBUG_HW)
564 ieee80211_announce_channels(ic);
567 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
570 fail: wpi_detach(dev);
571 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
576 * Attach the interface to 802.11 radiotap.
579 wpi_radiotap_attach(struct wpi_softc *sc)
581 struct wpi_rx_radiotap_header *rxtap = &sc->sc_rxtap;
582 struct wpi_tx_radiotap_header *txtap = &sc->sc_txtap;
584 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
585 ieee80211_radiotap_attach(&sc->sc_ic,
586 &txtap->wt_ihdr, sizeof(*txtap), WPI_TX_RADIOTAP_PRESENT,
587 &rxtap->wr_ihdr, sizeof(*rxtap), WPI_RX_RADIOTAP_PRESENT);
588 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
592 wpi_sysctlattach(struct wpi_softc *sc)
595 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
596 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
598 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
599 "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug,
600 "control debugging printfs");
605 wpi_init_beacon(struct wpi_vap *wvp)
607 struct wpi_buf *bcn = &wvp->wv_bcbuf;
608 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
610 cmd->id = WPI_ID_BROADCAST;
611 cmd->ofdm_mask = 0xff;
612 cmd->cck_mask = 0x0f;
613 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE);
616 * XXX WPI_TX_AUTO_SEQ seems to be ignored - workaround this issue
617 * XXX by using WPI_TX_NEED_ACK instead (with some side effects).
619 cmd->flags = htole32(WPI_TX_NEED_ACK | WPI_TX_INSERT_TSTAMP);
621 bcn->code = WPI_CMD_SET_BEACON;
622 bcn->ac = WPI_CMD_QUEUE_NUM;
623 bcn->size = sizeof(struct wpi_cmd_beacon);
626 static struct ieee80211vap *
627 wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
628 enum ieee80211_opmode opmode, int flags,
629 const uint8_t bssid[IEEE80211_ADDR_LEN],
630 const uint8_t mac[IEEE80211_ADDR_LEN])
633 struct ieee80211vap *vap;
635 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
638 wvp = malloc(sizeof(struct wpi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
640 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
642 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
643 WPI_VAP_LOCK_INIT(wvp);
644 wpi_init_beacon(wvp);
647 /* Override with driver methods. */
648 vap->iv_key_set = wpi_key_set;
649 vap->iv_key_delete = wpi_key_delete;
650 if (opmode == IEEE80211_M_IBSS) {
651 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
652 vap->iv_recv_mgmt = wpi_ibss_recv_mgmt;
654 wvp->wv_newstate = vap->iv_newstate;
655 vap->iv_newstate = wpi_newstate;
656 vap->iv_update_beacon = wpi_update_beacon;
657 vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1;
659 ieee80211_ratectl_init(vap);
660 /* Complete setup. */
661 ieee80211_vap_attach(vap, ieee80211_media_change,
662 ieee80211_media_status, mac);
663 ic->ic_opmode = opmode;
668 wpi_vap_delete(struct ieee80211vap *vap)
670 struct wpi_vap *wvp = WPI_VAP(vap);
671 struct wpi_buf *bcn = &wvp->wv_bcbuf;
672 enum ieee80211_opmode opmode = vap->iv_opmode;
674 ieee80211_ratectl_deinit(vap);
675 ieee80211_vap_detach(vap);
677 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
681 WPI_VAP_LOCK_DESTROY(wvp);
684 free(wvp, M_80211_VAP);
688 wpi_detach(device_t dev)
690 struct wpi_softc *sc = device_get_softc(dev);
691 struct ieee80211com *ic = &sc->sc_ic;
694 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
696 if (ic->ic_vap_create == wpi_vap_create) {
697 ieee80211_draintask(ic, &sc->sc_radioon_task);
701 if (sc->sc_tq != NULL) {
702 taskqueue_drain_all(sc->sc_tq);
703 taskqueue_free(sc->sc_tq);
706 callout_drain(&sc->watchdog_rfkill);
707 callout_drain(&sc->tx_timeout);
708 callout_drain(&sc->scan_timeout);
709 callout_drain(&sc->calib_to);
710 ieee80211_ifdetach(ic);
713 /* Uninstall interrupt handler. */
714 if (sc->irq != NULL) {
715 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
716 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
718 pci_release_msi(dev);
721 if (sc->txq[0].data_dmat) {
722 /* Free DMA resources. */
723 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++)
724 wpi_free_tx_ring(sc, &sc->txq[qid]);
726 wpi_free_rx_ring(sc);
734 bus_release_resource(dev, SYS_RES_MEMORY,
735 rman_get_rid(sc->mem), sc->mem);
737 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
738 WPI_TXQ_STATE_LOCK_DESTROY(sc);
739 WPI_TXQ_LOCK_DESTROY(sc);
740 WPI_NT_LOCK_DESTROY(sc);
741 WPI_RXON_LOCK_DESTROY(sc);
742 WPI_TX_LOCK_DESTROY(sc);
743 WPI_LOCK_DESTROY(sc);
748 wpi_shutdown(device_t dev)
750 struct wpi_softc *sc = device_get_softc(dev);
757 wpi_suspend(device_t dev)
759 struct wpi_softc *sc = device_get_softc(dev);
760 struct ieee80211com *ic = &sc->sc_ic;
762 ieee80211_suspend_all(ic);
767 wpi_resume(device_t dev)
769 struct wpi_softc *sc = device_get_softc(dev);
770 struct ieee80211com *ic = &sc->sc_ic;
772 /* Clear device-specific "PCI retry timeout" register (41h). */
773 pci_write_config(dev, 0x41, 0, 1);
775 ieee80211_resume_all(ic);
780 * Grab exclusive access to NIC memory.
783 wpi_nic_lock(struct wpi_softc *sc)
787 /* Request exclusive access to NIC. */
788 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
790 /* Spin until we actually get the lock. */
791 for (ntries = 0; ntries < 1000; ntries++) {
792 if ((WPI_READ(sc, WPI_GP_CNTRL) &
793 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
794 WPI_GP_CNTRL_MAC_ACCESS_ENA)
799 device_printf(sc->sc_dev, "could not lock memory\n");
805 * Release lock on NIC memory.
808 wpi_nic_unlock(struct wpi_softc *sc)
810 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
813 static __inline uint32_t
814 wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
816 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
817 WPI_BARRIER_READ_WRITE(sc);
818 return WPI_READ(sc, WPI_PRPH_RDATA);
822 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
824 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
825 WPI_BARRIER_WRITE(sc);
826 WPI_WRITE(sc, WPI_PRPH_WDATA, data);
830 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
832 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
836 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
838 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
842 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
843 const uint32_t *data, uint32_t count)
845 for (; count != 0; count--, data++, addr += 4)
846 wpi_prph_write(sc, addr, *data);
849 static __inline uint32_t
850 wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
852 WPI_WRITE(sc, WPI_MEM_RADDR, addr);
853 WPI_BARRIER_READ_WRITE(sc);
854 return WPI_READ(sc, WPI_MEM_RDATA);
858 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
861 for (; count > 0; count--, addr += 4)
862 *data++ = wpi_mem_read(sc, addr);
866 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
872 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
874 if ((error = wpi_nic_lock(sc)) != 0)
877 for (; count > 0; count -= 2, addr++) {
878 WPI_WRITE(sc, WPI_EEPROM, addr << 2);
879 for (ntries = 0; ntries < 10; ntries++) {
880 val = WPI_READ(sc, WPI_EEPROM);
881 if (val & WPI_EEPROM_READ_VALID)
886 device_printf(sc->sc_dev,
887 "timeout reading ROM at 0x%x\n", addr);
897 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
903 wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
907 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
908 *(bus_addr_t *)arg = segs[0].ds_addr;
912 * Allocates a contiguous block of dma memory of the requested size and
916 wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
917 void **kvap, bus_size_t size, bus_size_t alignment)
924 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
925 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
926 1, size, 0, NULL, NULL, &dma->tag);
930 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
931 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
935 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
936 wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
940 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
947 fail: wpi_dma_contig_free(dma);
952 wpi_dma_contig_free(struct wpi_dma_info *dma)
954 if (dma->vaddr != NULL) {
955 bus_dmamap_sync(dma->tag, dma->map,
956 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
957 bus_dmamap_unload(dma->tag, dma->map);
958 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
961 if (dma->tag != NULL) {
962 bus_dma_tag_destroy(dma->tag);
968 * Allocate a shared page between host and NIC.
971 wpi_alloc_shared(struct wpi_softc *sc)
973 /* Shared buffer must be aligned on a 4KB boundary. */
974 return wpi_dma_contig_alloc(sc, &sc->shared_dma,
975 (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
979 wpi_free_shared(struct wpi_softc *sc)
981 wpi_dma_contig_free(&sc->shared_dma);
985 * Allocate DMA-safe memory for firmware transfer.
988 wpi_alloc_fwmem(struct wpi_softc *sc)
990 /* Must be aligned on a 16-byte boundary. */
991 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
992 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
996 wpi_free_fwmem(struct wpi_softc *sc)
998 wpi_dma_contig_free(&sc->fw_dma);
1002 wpi_alloc_rx_ring(struct wpi_softc *sc)
1004 struct wpi_rx_ring *ring = &sc->rxq;
1011 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1013 /* Allocate RX descriptors (16KB aligned.) */
1014 size = WPI_RX_RING_COUNT * sizeof (uint32_t);
1015 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1016 (void **)&ring->desc, size, WPI_RING_DMA_ALIGN);
1018 device_printf(sc->sc_dev,
1019 "%s: could not allocate RX ring DMA memory, error %d\n",
1024 /* Create RX buffer DMA tag. */
1025 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1026 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1027 MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL, &ring->data_dmat);
1029 device_printf(sc->sc_dev,
1030 "%s: could not create RX buf DMA tag, error %d\n",
1036 * Allocate and map RX buffers.
1038 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1039 struct wpi_rx_data *data = &ring->data[i];
1042 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1044 device_printf(sc->sc_dev,
1045 "%s: could not create RX buf DMA map, error %d\n",
1050 data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1051 if (data->m == NULL) {
1052 device_printf(sc->sc_dev,
1053 "%s: could not allocate RX mbuf\n", __func__);
1058 error = bus_dmamap_load(ring->data_dmat, data->map,
1059 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1060 &paddr, BUS_DMA_NOWAIT);
1061 if (error != 0 && error != EFBIG) {
1062 device_printf(sc->sc_dev,
1063 "%s: can't map mbuf (error %d)\n", __func__,
1068 /* Set physical address of RX buffer. */
1069 ring->desc[i] = htole32(paddr);
1072 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1073 BUS_DMASYNC_PREWRITE);
1075 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1079 fail: wpi_free_rx_ring(sc);
1081 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1087 wpi_update_rx_ring(struct wpi_softc *sc)
1089 WPI_WRITE(sc, WPI_FH_RX_WPTR, sc->rxq.cur & ~7);
1093 wpi_update_rx_ring_ps(struct wpi_softc *sc)
1095 struct wpi_rx_ring *ring = &sc->rxq;
1097 if (ring->update != 0) {
1098 /* Wait for INT_WAKEUP event. */
1103 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1104 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1105 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n",
1109 wpi_update_rx_ring(sc);
1110 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1116 wpi_reset_rx_ring(struct wpi_softc *sc)
1118 struct wpi_rx_ring *ring = &sc->rxq;
1121 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1123 if (wpi_nic_lock(sc) == 0) {
1124 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
1125 for (ntries = 0; ntries < 1000; ntries++) {
1126 if (WPI_READ(sc, WPI_FH_RX_STATUS) &
1127 WPI_FH_RX_STATUS_IDLE)
1139 wpi_free_rx_ring(struct wpi_softc *sc)
1141 struct wpi_rx_ring *ring = &sc->rxq;
1144 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1146 wpi_dma_contig_free(&ring->desc_dma);
1148 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1149 struct wpi_rx_data *data = &ring->data[i];
1151 if (data->m != NULL) {
1152 bus_dmamap_sync(ring->data_dmat, data->map,
1153 BUS_DMASYNC_POSTREAD);
1154 bus_dmamap_unload(ring->data_dmat, data->map);
1158 if (data->map != NULL)
1159 bus_dmamap_destroy(ring->data_dmat, data->map);
1161 if (ring->data_dmat != NULL) {
1162 bus_dma_tag_destroy(ring->data_dmat);
1163 ring->data_dmat = NULL;
1168 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, uint8_t qid)
1180 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1182 /* Allocate TX descriptors (16KB aligned.) */
1183 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
1184 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc,
1185 size, WPI_RING_DMA_ALIGN);
1187 device_printf(sc->sc_dev,
1188 "%s: could not allocate TX ring DMA memory, error %d\n",
1193 /* Update shared area with ring physical address. */
1194 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1195 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1196 BUS_DMASYNC_PREWRITE);
1198 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
1199 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1202 device_printf(sc->sc_dev,
1203 "%s: could not allocate TX cmd DMA memory, error %d\n",
1208 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1209 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1210 WPI_MAX_SCATTER - 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
1212 device_printf(sc->sc_dev,
1213 "%s: could not create TX buf DMA tag, error %d\n",
1218 paddr = ring->cmd_dma.paddr;
1219 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1220 struct wpi_tx_data *data = &ring->data[i];
1222 data->cmd_paddr = paddr;
1223 paddr += sizeof (struct wpi_tx_cmd);
1225 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1227 device_printf(sc->sc_dev,
1228 "%s: could not create TX buf DMA map, error %d\n",
1234 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1238 fail: wpi_free_tx_ring(sc, ring);
1239 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1244 wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1246 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
1250 wpi_update_tx_ring_ps(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1253 if (ring->update != 0) {
1254 /* Wait for INT_WAKEUP event. */
1258 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1259 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1260 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n",
1261 __func__, ring->qid);
1264 wpi_update_tx_ring(sc, ring);
1265 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1270 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1274 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1276 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1277 struct wpi_tx_data *data = &ring->data[i];
1279 if (data->m != NULL) {
1280 bus_dmamap_sync(ring->data_dmat, data->map,
1281 BUS_DMASYNC_POSTWRITE);
1282 bus_dmamap_unload(ring->data_dmat, data->map);
1286 if (data->ni != NULL) {
1287 ieee80211_free_node(data->ni);
1291 /* Clear TX descriptors. */
1292 memset(ring->desc, 0, ring->desc_dma.size);
1293 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1294 BUS_DMASYNC_PREWRITE);
1302 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1306 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1308 wpi_dma_contig_free(&ring->desc_dma);
1309 wpi_dma_contig_free(&ring->cmd_dma);
1311 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1312 struct wpi_tx_data *data = &ring->data[i];
1314 if (data->m != NULL) {
1315 bus_dmamap_sync(ring->data_dmat, data->map,
1316 BUS_DMASYNC_POSTWRITE);
1317 bus_dmamap_unload(ring->data_dmat, data->map);
1320 if (data->map != NULL)
1321 bus_dmamap_destroy(ring->data_dmat, data->map);
1323 if (ring->data_dmat != NULL) {
1324 bus_dma_tag_destroy(ring->data_dmat);
1325 ring->data_dmat = NULL;
1330 * Extract various information from EEPROM.
1333 wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1335 #define WPI_CHK(res) do { \
1336 if ((error = res) != 0) \
1342 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1344 /* Adapter has to be powered on for EEPROM access to work. */
1345 if ((error = wpi_apm_init(sc)) != 0) {
1346 device_printf(sc->sc_dev,
1347 "%s: could not power ON adapter, error %d\n", __func__,
1352 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
1353 device_printf(sc->sc_dev, "bad EEPROM signature\n");
1357 /* Clear HW ownership of EEPROM. */
1358 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
1360 /* Read the hardware capabilities, revision and SKU type. */
1361 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap,
1363 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,
1365 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type,
1368 sc->rev = le16toh(sc->rev);
1369 DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap,
1372 /* Read the regulatory domain (4 ASCII characters.) */
1373 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain,
1374 sizeof(sc->domain)));
1376 /* Read MAC address. */
1377 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr,
1378 IEEE80211_ADDR_LEN));
1380 /* Read the list of authorized channels. */
1381 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
1382 WPI_CHK(wpi_read_eeprom_channels(sc, i));
1384 /* Read the list of TX power groups. */
1385 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
1386 WPI_CHK(wpi_read_eeprom_group(sc, i));
1388 fail: wpi_apm_stop(sc); /* Power OFF adapter. */
1390 DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
1398 * Translate EEPROM flags to net80211.
1401 wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel)
1406 if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0)
1407 nflags |= IEEE80211_CHAN_PASSIVE;
1408 if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0)
1409 nflags |= IEEE80211_CHAN_NOADHOC;
1410 if (channel->flags & WPI_EEPROM_CHAN_RADAR) {
1411 nflags |= IEEE80211_CHAN_DFS;
1412 /* XXX apparently IBSS may still be marked */
1413 nflags |= IEEE80211_CHAN_NOADHOC;
1416 /* XXX HOSTAP uses WPI_MODE_IBSS */
1417 if (nflags & IEEE80211_CHAN_NOADHOC)
1418 nflags |= IEEE80211_CHAN_NOHOSTAP;
1424 wpi_read_eeprom_band(struct wpi_softc *sc, uint8_t n, int maxchans,
1425 int *nchans, struct ieee80211_channel chans[])
1427 struct wpi_eeprom_chan *channels = sc->eeprom_channels[n];
1428 const struct wpi_chan_band *band = &wpi_bands[n];
1429 struct ieee80211_channel *c;
1433 for (i = 0; i < band->nchan; i++) {
1434 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
1435 DPRINTF(sc, WPI_DEBUG_EEPROM,
1436 "Channel Not Valid: %d, band %d\n",
1441 if (*nchans >= maxchans)
1444 chan = band->chan[i];
1445 nflags = wpi_eeprom_channel_flags(&channels[i]);
1447 c = &chans[(*nchans)++];
1449 c->ic_maxregpower = channels[i].maxpwr;
1450 c->ic_maxpower = 2*c->ic_maxregpower;
1452 if (n == 0) { /* 2GHz band */
1453 c->ic_freq = ieee80211_ieee2mhz(chan,
1456 /* G =>'s B is supported */
1457 c->ic_flags = IEEE80211_CHAN_B | nflags;
1459 if (*nchans >= maxchans)
1462 c = &chans[(*nchans)++];
1464 c->ic_flags = IEEE80211_CHAN_G | nflags;
1465 } else { /* 5GHz band */
1466 c->ic_freq = ieee80211_ieee2mhz(chan,
1469 c->ic_flags = IEEE80211_CHAN_A | nflags;
1472 /* Save maximum allowed TX power for this channel. */
1473 sc->maxpwr[chan] = channels[i].maxpwr;
1475 DPRINTF(sc, WPI_DEBUG_EEPROM,
1476 "adding chan %d (%dMHz) flags=0x%x maxpwr=%d passive=%d,"
1477 " offset %d\n", chan, c->ic_freq,
1478 channels[i].flags, sc->maxpwr[chan],
1479 IEEE80211_IS_CHAN_PASSIVE(c), *nchans);
1484 * Read the eeprom to find out what channels are valid for the given
1485 * band and update net80211 with what we find.
1488 wpi_read_eeprom_channels(struct wpi_softc *sc, uint8_t n)
1490 struct ieee80211com *ic = &sc->sc_ic;
1491 const struct wpi_chan_band *band = &wpi_bands[n];
1494 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1496 error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n],
1497 band->nchan * sizeof (struct wpi_eeprom_chan));
1499 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1503 wpi_read_eeprom_band(sc, n, IEEE80211_CHAN_MAX, &ic->ic_nchans,
1506 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1508 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1513 static struct wpi_eeprom_chan *
1514 wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c)
1518 for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++)
1519 for (i = 0; i < wpi_bands[j].nchan; i++)
1520 if (wpi_bands[j].chan[i] == c->ic_ieee)
1521 return &sc->eeprom_channels[j][i];
1527 wpi_getradiocaps(struct ieee80211com *ic,
1528 int maxchans, int *nchans, struct ieee80211_channel chans[])
1530 struct wpi_softc *sc = ic->ic_softc;
1533 /* Parse the list of authorized channels. */
1534 for (i = 0; i < WPI_CHAN_BANDS_COUNT && *nchans < maxchans; i++)
1535 wpi_read_eeprom_band(sc, i, maxchans, nchans, chans);
1539 * Enforce flags read from EEPROM.
1542 wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
1543 int nchan, struct ieee80211_channel chans[])
1545 struct wpi_softc *sc = ic->ic_softc;
1548 for (i = 0; i < nchan; i++) {
1549 struct ieee80211_channel *c = &chans[i];
1550 struct wpi_eeprom_chan *channel;
1552 channel = wpi_find_eeprom_channel(sc, c);
1553 if (channel == NULL) {
1554 ic_printf(ic, "%s: invalid channel %u freq %u/0x%x\n",
1555 __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
1558 c->ic_flags |= wpi_eeprom_channel_flags(channel);
1565 wpi_read_eeprom_group(struct wpi_softc *sc, uint8_t n)
1567 struct wpi_power_group *group = &sc->groups[n];
1568 struct wpi_eeprom_group rgroup;
1571 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1573 if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32,
1574 &rgroup, sizeof rgroup)) != 0) {
1575 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1579 /* Save TX power group information. */
1580 group->chan = rgroup.chan;
1581 group->maxpwr = rgroup.maxpwr;
1582 /* Retrieve temperature at which the samples were taken. */
1583 group->temp = (int16_t)le16toh(rgroup.temp);
1585 DPRINTF(sc, WPI_DEBUG_EEPROM,
1586 "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan,
1587 group->maxpwr, group->temp);
1589 for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
1590 group->samples[i].index = rgroup.samples[i].index;
1591 group->samples[i].power = rgroup.samples[i].power;
1593 DPRINTF(sc, WPI_DEBUG_EEPROM,
1594 "\tsample %d: index=%d power=%d\n", i,
1595 group->samples[i].index, group->samples[i].power);
1598 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1603 static __inline uint8_t
1604 wpi_add_node_entry_adhoc(struct wpi_softc *sc)
1606 uint8_t newid = WPI_ID_IBSS_MIN;
1608 for (; newid <= WPI_ID_IBSS_MAX; newid++) {
1609 if ((sc->nodesmsk & (1 << newid)) == 0) {
1610 sc->nodesmsk |= 1 << newid;
1615 return WPI_ID_UNDEFINED;
1618 static __inline uint8_t
1619 wpi_add_node_entry_sta(struct wpi_softc *sc)
1621 sc->nodesmsk |= 1 << WPI_ID_BSS;
1627 wpi_check_node_entry(struct wpi_softc *sc, uint8_t id)
1629 if (id == WPI_ID_UNDEFINED)
1632 return (sc->nodesmsk >> id) & 1;
1635 static __inline void
1636 wpi_clear_node_table(struct wpi_softc *sc)
1641 static __inline void
1642 wpi_del_node_entry(struct wpi_softc *sc, uint8_t id)
1644 sc->nodesmsk &= ~(1 << id);
1647 static struct ieee80211_node *
1648 wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1650 struct wpi_node *wn;
1652 wn = malloc(sizeof (struct wpi_node), M_80211_NODE,
1658 wn->id = WPI_ID_UNDEFINED;
1664 wpi_node_free(struct ieee80211_node *ni)
1666 struct wpi_softc *sc = ni->ni_ic->ic_softc;
1667 struct wpi_node *wn = WPI_NODE(ni);
1669 if (wn->id != WPI_ID_UNDEFINED) {
1671 if (wpi_check_node_entry(sc, wn->id)) {
1672 wpi_del_node_entry(sc, wn->id);
1673 wpi_del_node(sc, ni);
1678 sc->sc_node_free(ni);
1682 wpi_check_bss_filter(struct wpi_softc *sc)
1684 return (sc->rxon.filter & htole32(WPI_FILTER_BSS)) != 0;
1688 wpi_ibss_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
1689 const struct ieee80211_rx_stats *rxs,
1692 struct ieee80211vap *vap = ni->ni_vap;
1693 struct wpi_softc *sc = vap->iv_ic->ic_softc;
1694 struct wpi_vap *wvp = WPI_VAP(vap);
1695 uint64_t ni_tstamp, rx_tstamp;
1697 wvp->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1699 if (vap->iv_state == IEEE80211_S_RUN &&
1700 (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
1701 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) {
1702 ni_tstamp = le64toh(ni->ni_tstamp.tsf);
1703 rx_tstamp = le64toh(sc->rx_tstamp);
1705 if (ni_tstamp >= rx_tstamp) {
1706 DPRINTF(sc, WPI_DEBUG_STATE,
1707 "ibss merge, tsf %ju tstamp %ju\n",
1708 (uintmax_t)rx_tstamp, (uintmax_t)ni_tstamp);
1709 (void) ieee80211_ibss_merge(ni);
1715 wpi_restore_node(void *arg, struct ieee80211_node *ni)
1717 struct wpi_softc *sc = arg;
1718 struct wpi_node *wn = WPI_NODE(ni);
1722 if (wn->id != WPI_ID_UNDEFINED) {
1723 wn->id = WPI_ID_UNDEFINED;
1724 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
1725 device_printf(sc->sc_dev,
1726 "%s: could not add IBSS node, error %d\n",
1734 wpi_restore_node_table(struct wpi_softc *sc, struct wpi_vap *wvp)
1736 struct ieee80211com *ic = &sc->sc_ic;
1738 /* Set group keys once. */
1743 ieee80211_iterate_nodes(&ic->ic_sta, wpi_restore_node, sc);
1744 ieee80211_crypto_reload_keys(ic);
1748 * Called by net80211 when ever there is a change to 80211 state machine
1751 wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1753 struct wpi_vap *wvp = WPI_VAP(vap);
1754 struct ieee80211com *ic = vap->iv_ic;
1755 struct wpi_softc *sc = ic->ic_softc;
1758 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1761 if (nstate > IEEE80211_S_INIT && sc->sc_running == 0) {
1762 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1769 DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1770 ieee80211_state_name[vap->iv_state],
1771 ieee80211_state_name[nstate]);
1773 if (vap->iv_state == IEEE80211_S_RUN && nstate < IEEE80211_S_RUN) {
1774 if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) {
1775 device_printf(sc->sc_dev,
1776 "%s: could not set power saving level\n",
1781 wpi_set_led(sc, WPI_LED_LINK, 1, 0);
1785 case IEEE80211_S_SCAN:
1787 if (wpi_check_bss_filter(sc) != 0) {
1788 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1789 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1790 device_printf(sc->sc_dev,
1791 "%s: could not send RXON\n", __func__);
1794 WPI_RXON_UNLOCK(sc);
1797 case IEEE80211_S_ASSOC:
1798 if (vap->iv_state != IEEE80211_S_RUN)
1801 case IEEE80211_S_AUTH:
1803 * NB: do not optimize AUTH -> AUTH state transmission -
1804 * this will break powersave with non-QoS AP!
1808 * The node must be registered in the firmware before auth.
1809 * Also the associd must be cleared on RUN -> ASSOC
1812 if ((error = wpi_auth(sc, vap)) != 0) {
1813 device_printf(sc->sc_dev,
1814 "%s: could not move to AUTH state, error %d\n",
1819 case IEEE80211_S_RUN:
1821 * RUN -> RUN transition:
1822 * STA mode: Just restart the timers.
1823 * IBSS mode: Process IBSS merge.
1825 if (vap->iv_state == IEEE80211_S_RUN) {
1826 if (vap->iv_opmode != IEEE80211_M_IBSS) {
1828 wpi_calib_timeout(sc);
1829 WPI_RXON_UNLOCK(sc);
1833 * Drop the BSS_FILTER bit
1834 * (there is no another way to change bssid).
1837 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1838 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1839 device_printf(sc->sc_dev,
1840 "%s: could not send RXON\n",
1843 WPI_RXON_UNLOCK(sc);
1845 /* Restore all what was lost. */
1846 wpi_restore_node_table(sc, wvp);
1848 /* XXX set conditionally? */
1854 * !RUN -> RUN requires setting the association id
1855 * which is done with a firmware cmd. We also defer
1856 * starting the timers until that work is done.
1858 if ((error = wpi_run(sc, vap)) != 0) {
1859 device_printf(sc->sc_dev,
1860 "%s: could not move to RUN state\n", __func__);
1868 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1872 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1874 return wvp->wv_newstate(vap, nstate, arg);
1878 wpi_calib_timeout(void *arg)
1880 struct wpi_softc *sc = arg;
1882 if (wpi_check_bss_filter(sc) == 0)
1885 wpi_power_calibration(sc);
1887 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
1890 static __inline uint8_t
1891 rate2plcp(const uint8_t rate)
1894 case 12: return 0xd;
1895 case 18: return 0xf;
1896 case 24: return 0x5;
1897 case 36: return 0x7;
1898 case 48: return 0x9;
1899 case 72: return 0xb;
1900 case 96: return 0x1;
1901 case 108: return 0x3;
1905 case 22: return 110;
1910 static __inline uint8_t
1911 plcp2rate(const uint8_t plcp)
1914 case 0xd: return 12;
1915 case 0xf: return 18;
1916 case 0x5: return 24;
1917 case 0x7: return 36;
1918 case 0x9: return 48;
1919 case 0xb: return 72;
1920 case 0x1: return 96;
1921 case 0x3: return 108;
1925 case 110: return 22;
1930 /* Quickly determine if a given rate is CCK or OFDM. */
1931 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1934 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1935 struct wpi_rx_data *data)
1937 struct ieee80211com *ic = &sc->sc_ic;
1938 struct wpi_rx_ring *ring = &sc->rxq;
1939 struct wpi_rx_stat *stat;
1940 struct wpi_rx_head *head;
1941 struct wpi_rx_tail *tail;
1942 struct ieee80211_frame *wh;
1943 struct ieee80211_node *ni;
1944 struct mbuf *m, *m1;
1950 stat = (struct wpi_rx_stat *)(desc + 1);
1952 if (__predict_false(stat->len > WPI_STAT_MAXLEN)) {
1953 device_printf(sc->sc_dev, "invalid RX statistic header\n");
1957 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
1958 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1959 len = le16toh(head->len);
1960 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len);
1961 flags = le32toh(tail->flags);
1963 DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d"
1964 " rate %x chan %d tstamp %ju\n", __func__, ring->cur,
1965 le32toh(desc->len), len, (int8_t)stat->rssi,
1966 head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp));
1968 /* Discard frames with a bad FCS early. */
1969 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1970 DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n",
1974 /* Discard frames that are too short. */
1975 if (len < sizeof (struct ieee80211_frame_ack)) {
1976 DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n",
1981 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1982 if (__predict_false(m1 == NULL)) {
1983 DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n",
1987 bus_dmamap_unload(ring->data_dmat, data->map);
1989 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *),
1990 MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1991 if (__predict_false(error != 0 && error != EFBIG)) {
1992 device_printf(sc->sc_dev,
1993 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1996 /* Try to reload the old mbuf. */
1997 error = bus_dmamap_load(ring->data_dmat, data->map,
1998 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1999 &paddr, BUS_DMA_NOWAIT);
2000 if (error != 0 && error != EFBIG) {
2001 panic("%s: could not load old RX mbuf", __func__);
2003 /* Physical address may have changed. */
2004 ring->desc[ring->cur] = htole32(paddr);
2005 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map,
2006 BUS_DMASYNC_PREWRITE);
2012 /* Update RX descriptor. */
2013 ring->desc[ring->cur] = htole32(paddr);
2014 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2015 BUS_DMASYNC_PREWRITE);
2017 /* Finalize mbuf. */
2018 m->m_data = (caddr_t)(head + 1);
2019 m->m_pkthdr.len = m->m_len = len;
2021 /* Grab a reference to the source node. */
2022 wh = mtod(m, struct ieee80211_frame *);
2024 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
2025 (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) {
2026 /* Check whether decryption was successful or not. */
2027 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
2028 DPRINTF(sc, WPI_DEBUG_RECV,
2029 "CCMP decryption failed 0x%x\n", flags);
2032 m->m_flags |= M_WEP;
2035 if (len >= sizeof(struct ieee80211_frame_min))
2036 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
2040 sc->rx_tstamp = tail->tstamp;
2042 if (ieee80211_radiotap_active(ic)) {
2043 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
2046 if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE))
2047 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2048 tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET);
2049 tap->wr_dbm_antnoise = WPI_RSSI_OFFSET;
2050 tap->wr_tsft = tail->tstamp;
2051 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
2052 tap->wr_rate = plcp2rate(head->plcp);
2057 /* Send the frame to the 802.11 layer. */
2059 (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET);
2060 /* Node is no longer needed. */
2061 ieee80211_free_node(ni);
2063 (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET);
2071 fail1: counter_u64_add(ic->ic_ierrors, 1);
2075 wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc,
2076 struct wpi_rx_data *data)
2082 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2084 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
2085 struct wpi_tx_data *data = &ring->data[desc->idx];
2086 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
2088 struct ieee80211_node *ni;
2089 struct ieee80211vap *vap;
2090 struct ieee80211com *ic;
2091 uint32_t status = le32toh(stat->status);
2092 int ackfailcnt = stat->ackfailcnt / WPI_NTRIES_DEFAULT;
2094 KASSERT(data->ni != NULL, ("no node"));
2095 KASSERT(data->m != NULL, ("no mbuf"));
2097 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2099 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: "
2100 "qid %d idx %d retries %d btkillcnt %d rate %x duration %d "
2101 "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt,
2102 stat->btkillcnt, stat->rate, le32toh(stat->duration), status);
2104 /* Unmap and free mbuf. */
2105 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
2106 bus_dmamap_unload(ring->data_dmat, data->map);
2107 m = data->m, data->m = NULL;
2108 ni = data->ni, data->ni = NULL;
2113 * Update rate control statistics for the node.
2115 if (status & WPI_TX_STATUS_FAIL) {
2116 ieee80211_ratectl_tx_complete(vap, ni,
2117 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL);
2119 ieee80211_ratectl_tx_complete(vap, ni,
2120 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL);
2122 ieee80211_tx_complete(ni, m, (status & WPI_TX_STATUS_FAIL) != 0);
2124 WPI_TXQ_STATE_LOCK(sc);
2125 if (--ring->queued > 0)
2126 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2128 callout_stop(&sc->tx_timeout);
2129 WPI_TXQ_STATE_UNLOCK(sc);
2131 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2135 * Process a "command done" firmware notification. This is where we wakeup
2136 * processes waiting for a synchronous command completion.
2139 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2141 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
2142 struct wpi_tx_data *data;
2143 struct wpi_tx_cmd *cmd;
2145 DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x "
2146 "type %s len %d\n", desc->qid, desc->idx,
2147 desc->flags, wpi_cmd_str(desc->type),
2148 le32toh(desc->len));
2150 if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM)
2151 return; /* Not a command ack. */
2153 KASSERT(ring->queued == 0, ("ring->queued must be 0"));
2155 data = &ring->data[desc->idx];
2156 cmd = &ring->cmd[desc->idx];
2158 /* If the command was mapped in an mbuf, free it. */
2159 if (data->m != NULL) {
2160 bus_dmamap_sync(ring->data_dmat, data->map,
2161 BUS_DMASYNC_POSTWRITE);
2162 bus_dmamap_unload(ring->data_dmat, data->map);
2169 if (desc->type == WPI_CMD_SET_POWER_MODE) {
2170 struct wpi_pmgt_cmd *pcmd = (struct wpi_pmgt_cmd *)cmd->data;
2172 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2173 BUS_DMASYNC_POSTREAD);
2176 if (le16toh(pcmd->flags) & WPI_PS_ALLOW_SLEEP) {
2177 sc->sc_update_rx_ring = wpi_update_rx_ring_ps;
2178 sc->sc_update_tx_ring = wpi_update_tx_ring_ps;
2180 sc->sc_update_rx_ring = wpi_update_rx_ring;
2181 sc->sc_update_tx_ring = wpi_update_tx_ring;
2188 wpi_notif_intr(struct wpi_softc *sc)
2190 struct ieee80211com *ic = &sc->sc_ic;
2191 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2194 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
2195 BUS_DMASYNC_POSTREAD);
2197 hw = le32toh(sc->shared->next) & 0xfff;
2198 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
2200 while (sc->rxq.cur != hw) {
2201 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
2203 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2204 struct wpi_rx_desc *desc;
2206 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2207 BUS_DMASYNC_POSTREAD);
2208 desc = mtod(data->m, struct wpi_rx_desc *);
2210 DPRINTF(sc, WPI_DEBUG_NOTIFY,
2211 "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n",
2212 __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags,
2213 desc->type, wpi_cmd_str(desc->type), le32toh(desc->len));
2215 if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) {
2216 /* Reply to a command. */
2217 wpi_cmd_done(sc, desc);
2220 switch (desc->type) {
2222 /* An 802.11 frame has been received. */
2223 wpi_rx_done(sc, desc, data);
2225 if (__predict_false(sc->sc_running == 0)) {
2226 /* wpi_stop() was called. */
2233 /* An 802.11 frame has been transmitted. */
2234 wpi_tx_done(sc, desc);
2237 case WPI_RX_STATISTICS:
2238 case WPI_BEACON_STATISTICS:
2239 wpi_rx_statistics(sc, desc, data);
2242 case WPI_BEACON_MISSED:
2244 struct wpi_beacon_missed *miss =
2245 (struct wpi_beacon_missed *)(desc + 1);
2246 uint32_t expected, misses, received, threshold;
2248 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2249 BUS_DMASYNC_POSTREAD);
2251 misses = le32toh(miss->consecutive);
2252 expected = le32toh(miss->expected);
2253 received = le32toh(miss->received);
2254 threshold = MAX(2, vap->iv_bmissthreshold);
2256 DPRINTF(sc, WPI_DEBUG_BMISS,
2257 "%s: beacons missed %u(%u) (received %u/%u)\n",
2258 __func__, misses, le32toh(miss->total), received,
2261 if (misses >= threshold ||
2262 (received == 0 && expected >= threshold)) {
2264 if (callout_pending(&sc->scan_timeout)) {
2265 wpi_cmd(sc, WPI_CMD_SCAN_ABORT, NULL,
2268 WPI_RXON_UNLOCK(sc);
2269 if (vap->iv_state == IEEE80211_S_RUN &&
2270 (ic->ic_flags & IEEE80211_F_SCAN) == 0)
2271 ieee80211_beacon_miss(ic);
2277 case WPI_BEACON_SENT:
2279 struct wpi_tx_stat *stat =
2280 (struct wpi_tx_stat *)(desc + 1);
2281 uint64_t *tsf = (uint64_t *)(stat + 1);
2282 uint32_t *mode = (uint32_t *)(tsf + 1);
2284 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2285 BUS_DMASYNC_POSTREAD);
2287 DPRINTF(sc, WPI_DEBUG_BEACON,
2288 "beacon sent: rts %u, ack %u, btkill %u, rate %u, "
2289 "duration %u, status %x, tsf %ju, mode %x\n",
2290 stat->rtsfailcnt, stat->ackfailcnt,
2291 stat->btkillcnt, stat->rate, le32toh(stat->duration),
2292 le32toh(stat->status), le64toh(*tsf),
2300 struct wpi_ucode_info *uc =
2301 (struct wpi_ucode_info *)(desc + 1);
2303 /* The microcontroller is ready. */
2304 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2305 BUS_DMASYNC_POSTREAD);
2306 DPRINTF(sc, WPI_DEBUG_RESET,
2307 "microcode alive notification version=%d.%d "
2308 "subtype=%x alive=%x\n", uc->major, uc->minor,
2309 uc->subtype, le32toh(uc->valid));
2311 if (le32toh(uc->valid) != 1) {
2312 device_printf(sc->sc_dev,
2313 "microcontroller initialization failed\n");
2314 wpi_stop_locked(sc);
2317 /* Save the address of the error log in SRAM. */
2318 sc->errptr = le32toh(uc->errptr);
2321 case WPI_STATE_CHANGED:
2323 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2324 BUS_DMASYNC_POSTREAD);
2326 uint32_t *status = (uint32_t *)(desc + 1);
2328 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n",
2331 if (le32toh(*status) & 1) {
2333 wpi_clear_node_table(sc);
2335 taskqueue_enqueue(sc->sc_tq,
2336 &sc->sc_radiooff_task);
2342 case WPI_START_SCAN:
2344 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2345 BUS_DMASYNC_POSTREAD);
2347 struct wpi_start_scan *scan =
2348 (struct wpi_start_scan *)(desc + 1);
2349 DPRINTF(sc, WPI_DEBUG_SCAN,
2350 "%s: scanning channel %d status %x\n",
2351 __func__, scan->chan, le32toh(scan->status));
2358 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2359 BUS_DMASYNC_POSTREAD);
2361 struct wpi_stop_scan *scan =
2362 (struct wpi_stop_scan *)(desc + 1);
2364 DPRINTF(sc, WPI_DEBUG_SCAN,
2365 "scan finished nchan=%d status=%d chan=%d\n",
2366 scan->nchan, scan->status, scan->chan);
2369 callout_stop(&sc->scan_timeout);
2370 WPI_RXON_UNLOCK(sc);
2371 if (scan->status == WPI_SCAN_ABORTED)
2372 ieee80211_cancel_scan(vap);
2374 ieee80211_scan_next(vap);
2379 if (sc->rxq.cur % 8 == 0) {
2380 /* Tell the firmware what we have processed. */
2381 sc->sc_update_rx_ring(sc);
2387 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2388 * from power-down sleep mode.
2391 wpi_wakeup_intr(struct wpi_softc *sc)
2395 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
2396 "%s: ucode wakeup from power-down sleep\n", __func__);
2398 /* Wakeup RX and TX rings. */
2399 if (sc->rxq.update) {
2401 wpi_update_rx_ring(sc);
2404 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) {
2405 struct wpi_tx_ring *ring = &sc->txq[qid];
2409 wpi_update_tx_ring(sc, ring);
2412 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
2417 * This function prints firmware registers
2421 wpi_debug_registers(struct wpi_softc *sc)
2424 static const uint32_t csr_tbl[] = {
2441 static const uint32_t prph_tbl[] = {
2448 DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n");
2450 for (i = 0; i < nitems(csr_tbl); i++) {
2451 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2452 wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i]));
2454 if ((i + 1) % 2 == 0)
2455 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2457 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n");
2459 if (wpi_nic_lock(sc) == 0) {
2460 for (i = 0; i < nitems(prph_tbl); i++) {
2461 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2462 wpi_get_prph_string(prph_tbl[i]),
2463 wpi_prph_read(sc, prph_tbl[i]));
2465 if ((i + 1) % 2 == 0)
2466 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2468 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2471 DPRINTF(sc, WPI_DEBUG_REGISTER,
2472 "Cannot access internal registers.\n");
2478 * Dump the error log of the firmware when a firmware panic occurs. Although
2479 * we can't debug the firmware because it is neither open source nor free, it
2480 * can help us to identify certain classes of problems.
2483 wpi_fatal_intr(struct wpi_softc *sc)
2485 struct wpi_fw_dump dump;
2486 uint32_t i, offset, count;
2488 /* Check that the error log address is valid. */
2489 if (sc->errptr < WPI_FW_DATA_BASE ||
2490 sc->errptr + sizeof (dump) >
2491 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
2492 printf("%s: bad firmware error log address 0x%08x\n", __func__,
2496 if (wpi_nic_lock(sc) != 0) {
2497 printf("%s: could not read firmware error log\n", __func__);
2500 /* Read number of entries in the log. */
2501 count = wpi_mem_read(sc, sc->errptr);
2502 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
2503 printf("%s: invalid count field (count = %u)\n", __func__,
2508 /* Skip "count" field. */
2509 offset = sc->errptr + sizeof (uint32_t);
2510 printf("firmware error log (count = %u):\n", count);
2511 for (i = 0; i < count; i++) {
2512 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
2513 sizeof (dump) / sizeof (uint32_t));
2515 printf(" error type = \"%s\" (0x%08X)\n",
2516 (dump.desc < nitems(wpi_fw_errmsg)) ?
2517 wpi_fw_errmsg[dump.desc] : "UNKNOWN",
2519 printf(" error data = 0x%08X\n",
2521 printf(" branch link = 0x%08X%08X\n",
2522 dump.blink[0], dump.blink[1]);
2523 printf(" interrupt link = 0x%08X%08X\n",
2524 dump.ilink[0], dump.ilink[1]);
2525 printf(" time = %u\n", dump.time);
2527 offset += sizeof (dump);
2530 /* Dump driver status (TX and RX rings) while we're here. */
2531 printf("driver status:\n");
2533 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
2534 struct wpi_tx_ring *ring = &sc->txq[i];
2535 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2536 i, ring->qid, ring->cur, ring->queued);
2539 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2545 struct wpi_softc *sc = arg;
2550 /* Disable interrupts. */
2551 WPI_WRITE(sc, WPI_INT_MASK, 0);
2553 r1 = WPI_READ(sc, WPI_INT);
2555 if (__predict_false(r1 == 0xffffffff ||
2556 (r1 & 0xfffffff0) == 0xa5a5a5a0))
2557 goto end; /* Hardware gone! */
2559 r2 = WPI_READ(sc, WPI_FH_INT);
2561 DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__,
2564 if (r1 == 0 && r2 == 0)
2565 goto done; /* Interrupt not for us. */
2567 /* Acknowledge interrupts. */
2568 WPI_WRITE(sc, WPI_INT, r1);
2569 WPI_WRITE(sc, WPI_FH_INT, r2);
2571 if (__predict_false(r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR))) {
2572 device_printf(sc->sc_dev, "fatal firmware error\n");
2574 wpi_debug_registers(sc);
2577 DPRINTF(sc, WPI_DEBUG_HW,
2578 "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" :
2579 "(Hardware Error)");
2580 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2584 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
2585 (r2 & WPI_FH_INT_RX))
2588 if (r1 & WPI_INT_ALIVE)
2589 wakeup(sc); /* Firmware is alive. */
2591 if (r1 & WPI_INT_WAKEUP)
2592 wpi_wakeup_intr(sc);
2595 /* Re-enable interrupts. */
2596 if (__predict_true(sc->sc_running))
2597 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
2599 end: WPI_UNLOCK(sc);
2603 wpi_free_txfrags(struct wpi_softc *sc, uint16_t ac)
2605 struct wpi_tx_ring *ring;
2606 struct wpi_tx_data *data;
2610 ring = &sc->txq[ac];
2612 while (ring->pending != 0) {
2614 cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT;
2615 data = &ring->data[cur];
2617 bus_dmamap_sync(ring->data_dmat, data->map,
2618 BUS_DMASYNC_POSTWRITE);
2619 bus_dmamap_unload(ring->data_dmat, data->map);
2623 ieee80211_node_decref(data->ni);
2631 wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf)
2633 struct ieee80211_frame *wh;
2634 struct wpi_tx_cmd *cmd;
2635 struct wpi_tx_data *data;
2636 struct wpi_tx_desc *desc;
2637 struct wpi_tx_ring *ring;
2639 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER];
2642 int error, i, nsegs, totlen, frag;
2646 KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow"));
2648 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2650 if (__predict_false(sc->sc_running == 0)) {
2651 /* wpi_stop() was called */
2656 wh = mtod(buf->m, struct ieee80211_frame *);
2657 hdrlen = ieee80211_anyhdrsize(wh);
2658 totlen = buf->m->m_pkthdr.len;
2659 frag = ((buf->m->m_flags & (M_FRAG | M_LASTFRAG)) == M_FRAG);
2661 if (__predict_false(totlen < sizeof(struct ieee80211_frame_min))) {
2667 /* First segment length must be a multiple of 4. */
2668 pad = 4 - (hdrlen & 3);
2672 ring = &sc->txq[buf->ac];
2673 cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT;
2674 desc = &ring->desc[cur];
2675 data = &ring->data[cur];
2677 /* Prepare TX firmware command. */
2678 cmd = &ring->cmd[cur];
2679 cmd->code = buf->code;
2681 cmd->qid = ring->qid;
2684 memcpy(cmd->data, buf->data, buf->size);
2686 /* Save and trim IEEE802.11 header. */
2687 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen);
2688 m_adj(buf->m, hdrlen);
2690 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m,
2691 segs, &nsegs, BUS_DMA_NOWAIT);
2692 if (error != 0 && error != EFBIG) {
2693 device_printf(sc->sc_dev,
2694 "%s: can't map mbuf (error %d)\n", __func__, error);
2698 /* Too many DMA segments, linearize mbuf. */
2699 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1);
2701 device_printf(sc->sc_dev,
2702 "%s: could not defrag mbuf\n", __func__);
2708 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
2709 buf->m, segs, &nsegs, BUS_DMA_NOWAIT);
2710 if (__predict_false(error != 0)) {
2711 /* XXX fix this (applicable to the iwn(4) too) */
2713 * NB: Do not return error;
2714 * original mbuf does not exist anymore.
2716 device_printf(sc->sc_dev,
2717 "%s: can't map mbuf (error %d)\n", __func__,
2719 if (ring->qid < WPI_CMD_QUEUE_NUM) {
2720 if_inc_counter(buf->ni->ni_vap->iv_ifp,
2721 IFCOUNTER_OERRORS, 1);
2723 ieee80211_free_node(buf->ni);
2731 KASSERT(nsegs < WPI_MAX_SCATTER,
2732 ("too many DMA segments, nsegs (%d) should be less than %d",
2733 nsegs, WPI_MAX_SCATTER));
2738 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
2739 __func__, ring->qid, cur, totlen, nsegs);
2741 /* Fill TX descriptor. */
2742 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs);
2743 /* First DMA segment is used by the TX command. */
2744 desc->segs[0].addr = htole32(data->cmd_paddr);
2745 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad);
2746 /* Other DMA segments are for data payload. */
2748 for (i = 1; i <= nsegs; i++) {
2749 desc->segs[i].addr = htole32(seg->ds_addr);
2750 desc->segs[i].len = htole32(seg->ds_len);
2754 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2755 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2756 BUS_DMASYNC_PREWRITE);
2757 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2758 BUS_DMASYNC_PREWRITE);
2763 if (ring->qid < WPI_CMD_QUEUE_NUM) {
2764 WPI_TXQ_STATE_LOCK(sc);
2765 ring->queued += ring->pending;
2766 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout,
2768 WPI_TXQ_STATE_UNLOCK(sc);
2772 ring->cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT;
2774 sc->sc_update_tx_ring(sc, ring);
2776 ieee80211_node_incref(data->ni);
2778 end: DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
2787 * Construct the data packet for a transmit buffer.
2790 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2792 const struct ieee80211_txparam *tp;
2793 struct ieee80211vap *vap = ni->ni_vap;
2794 struct ieee80211com *ic = ni->ni_ic;
2795 struct wpi_node *wn = WPI_NODE(ni);
2796 struct ieee80211_channel *chan;
2797 struct ieee80211_frame *wh;
2798 struct ieee80211_key *k = NULL;
2799 struct wpi_buf tx_data;
2800 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2803 uint8_t tid, type, rate;
2804 int swcrypt, ismcast, totlen;
2806 wh = mtod(m, struct ieee80211_frame *);
2807 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2808 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2811 /* Select EDCA Access Category and TX ring for this frame. */
2812 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2813 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2814 tid = qos & IEEE80211_QOS_TID;
2819 ac = M_WME_GETAC(m);
2821 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
2822 ni->ni_chan : ic->ic_curchan;
2823 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2825 /* Choose a TX rate index. */
2826 if (type == IEEE80211_FC0_TYPE_MGT)
2827 rate = tp->mgmtrate;
2829 rate = tp->mcastrate;
2830 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2831 rate = tp->ucastrate;
2832 else if (m->m_flags & M_EAPOL)
2833 rate = tp->mgmtrate;
2835 /* XXX pass pktlen */
2836 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2837 rate = ni->ni_txrate;
2840 /* Encrypt the frame if need be. */
2841 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2842 /* Retrieve key for TX. */
2843 k = ieee80211_crypto_encap(ni, m);
2847 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2849 /* 802.11 header may have moved. */
2850 wh = mtod(m, struct ieee80211_frame *);
2852 totlen = m->m_pkthdr.len;
2854 if (ieee80211_radiotap_active_vap(vap)) {
2855 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2858 tap->wt_rate = rate;
2860 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2861 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2862 tap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2864 ieee80211_radiotap_tx(vap, m);
2869 /* Unicast frame, check if an ACK is expected. */
2870 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2871 IEEE80211_QOS_ACKPOLICY_NOACK)
2872 flags |= WPI_TX_NEED_ACK;
2875 if (!IEEE80211_QOS_HAS_SEQ(wh))
2876 flags |= WPI_TX_AUTO_SEQ;
2877 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2878 flags |= WPI_TX_MORE_FRAG;
2880 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2882 /* NB: Group frames are sent using CCK in 802.11b/g. */
2883 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2884 flags |= WPI_TX_NEED_RTS;
2885 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2886 WPI_RATE_IS_OFDM(rate)) {
2887 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2888 flags |= WPI_TX_NEED_CTS;
2889 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2890 flags |= WPI_TX_NEED_RTS;
2893 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2894 flags |= WPI_TX_FULL_TXOP;
2897 memset(tx, 0, sizeof (struct wpi_cmd_data));
2898 if (type == IEEE80211_FC0_TYPE_MGT) {
2899 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2901 /* Tell HW to set timestamp in probe responses. */
2902 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2903 flags |= WPI_TX_INSERT_TSTAMP;
2904 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2905 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2906 tx->timeout = htole16(3);
2908 tx->timeout = htole16(2);
2911 if (ismcast || type != IEEE80211_FC0_TYPE_DATA)
2912 tx->id = WPI_ID_BROADCAST;
2914 if (wn->id == WPI_ID_UNDEFINED) {
2915 device_printf(sc->sc_dev,
2916 "%s: undefined node id\n", __func__);
2924 switch (k->wk_cipher->ic_cipher) {
2925 case IEEE80211_CIPHER_AES_CCM:
2926 tx->security = WPI_CIPHER_CCMP;
2933 memcpy(tx->key, k->wk_key, k->wk_keylen);
2936 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
2937 struct mbuf *next = m->m_nextpkt;
2939 tx->lnext = htole16(next->m_pkthdr.len);
2940 tx->fnext = htole32(tx->security |
2941 (flags & WPI_TX_NEED_ACK) |
2942 WPI_NEXT_STA_ID(tx->id));
2945 tx->len = htole16(totlen);
2946 tx->flags = htole32(flags);
2947 tx->plcp = rate2plcp(rate);
2949 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2950 tx->ofdm_mask = 0xff;
2951 tx->cck_mask = 0x0f;
2953 tx->data_ntries = tp->maxretry;
2957 tx_data.size = sizeof(struct wpi_cmd_data);
2958 tx_data.code = WPI_CMD_TX_DATA;
2961 return wpi_cmd2(sc, &tx_data);
2965 wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m,
2966 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
2968 struct ieee80211vap *vap = ni->ni_vap;
2969 struct ieee80211_key *k = NULL;
2970 struct ieee80211_frame *wh;
2971 struct wpi_buf tx_data;
2972 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2974 uint8_t ac, type, rate;
2975 int swcrypt, totlen;
2977 wh = mtod(m, struct ieee80211_frame *);
2978 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2981 ac = params->ibp_pri & 3;
2983 /* Choose a TX rate index. */
2984 rate = params->ibp_rate0;
2987 if (!IEEE80211_QOS_HAS_SEQ(wh))
2988 flags |= WPI_TX_AUTO_SEQ;
2989 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
2990 flags |= WPI_TX_NEED_ACK;
2991 if (params->ibp_flags & IEEE80211_BPF_RTS)
2992 flags |= WPI_TX_NEED_RTS;
2993 if (params->ibp_flags & IEEE80211_BPF_CTS)
2994 flags |= WPI_TX_NEED_CTS;
2995 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2996 flags |= WPI_TX_FULL_TXOP;
2998 /* Encrypt the frame if need be. */
2999 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
3000 /* Retrieve key for TX. */
3001 k = ieee80211_crypto_encap(ni, m);
3005 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
3007 /* 802.11 header may have moved. */
3008 wh = mtod(m, struct ieee80211_frame *);
3010 totlen = m->m_pkthdr.len;
3012 if (ieee80211_radiotap_active_vap(vap)) {
3013 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
3016 tap->wt_rate = rate;
3017 if (params->ibp_flags & IEEE80211_BPF_CRYPTO)
3018 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3020 ieee80211_radiotap_tx(vap, m);
3023 memset(tx, 0, sizeof (struct wpi_cmd_data));
3024 if (type == IEEE80211_FC0_TYPE_MGT) {
3025 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
3027 /* Tell HW to set timestamp in probe responses. */
3028 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
3029 flags |= WPI_TX_INSERT_TSTAMP;
3030 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
3031 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
3032 tx->timeout = htole16(3);
3034 tx->timeout = htole16(2);
3038 switch (k->wk_cipher->ic_cipher) {
3039 case IEEE80211_CIPHER_AES_CCM:
3040 tx->security = WPI_CIPHER_CCMP;
3047 memcpy(tx->key, k->wk_key, k->wk_keylen);
3050 tx->len = htole16(totlen);
3051 tx->flags = htole32(flags);
3052 tx->plcp = rate2plcp(rate);
3053 tx->id = WPI_ID_BROADCAST;
3054 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
3055 tx->rts_ntries = params->ibp_try1;
3056 tx->data_ntries = params->ibp_try0;
3060 tx_data.size = sizeof(struct wpi_cmd_data);
3061 tx_data.code = WPI_CMD_TX_DATA;
3064 return wpi_cmd2(sc, &tx_data);
3068 wpi_tx_ring_free_space(struct wpi_softc *sc, uint16_t ac)
3070 struct wpi_tx_ring *ring = &sc->txq[ac];
3073 WPI_TXQ_STATE_LOCK(sc);
3074 retval = WPI_TX_RING_HIMARK - ring->queued;
3075 WPI_TXQ_STATE_UNLOCK(sc);
3081 wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3082 const struct ieee80211_bpf_params *params)
3084 struct ieee80211com *ic = ni->ni_ic;
3085 struct wpi_softc *sc = ic->ic_softc;
3089 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3091 ac = M_WME_GETAC(m);
3095 /* NB: no fragments here */
3096 if (sc->sc_running == 0 || wpi_tx_ring_free_space(sc, ac) < 1) {
3097 error = sc->sc_running ? ENOBUFS : ENETDOWN;
3101 if (params == NULL) {
3103 * Legacy path; interpret frame contents to decide
3104 * precisely how to send the frame.
3106 error = wpi_tx_data(sc, m, ni);
3109 * Caller supplied explicit parameters to use in
3110 * sending the frame.
3112 error = wpi_tx_data_raw(sc, m, ni, params);
3115 unlock: WPI_TX_UNLOCK(sc);
3119 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3124 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3130 wpi_transmit(struct ieee80211com *ic, struct mbuf *m)
3132 struct wpi_softc *sc = ic->ic_softc;
3133 struct ieee80211_node *ni;
3139 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
3141 /* Check if interface is up & running. */
3142 if (__predict_false(sc->sc_running == 0)) {
3148 for (mnext = m->m_nextpkt; mnext != NULL; mnext = mnext->m_nextpkt)
3151 /* Check for available space. */
3152 ac = M_WME_GETAC(m);
3153 if (wpi_tx_ring_free_space(sc, ac) < nmbufs) {
3159 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3161 mnext = m->m_nextpkt;
3162 if (wpi_tx_data(sc, m, ni) != 0) {
3163 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS,
3165 wpi_free_txfrags(sc, ac);
3166 ieee80211_free_mbuf(m);
3167 ieee80211_free_node(ni);
3170 } while((m = mnext) != NULL);
3172 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
3174 unlock: WPI_TX_UNLOCK(sc);
3180 wpi_watchdog_rfkill(void *arg)
3182 struct wpi_softc *sc = arg;
3183 struct ieee80211com *ic = &sc->sc_ic;
3185 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n");
3187 /* No need to lock firmware memory. */
3188 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) {
3189 /* Radio kill switch is still off. */
3190 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
3193 ieee80211_runtask(ic, &sc->sc_radioon_task);
3197 wpi_scan_timeout(void *arg)
3199 struct wpi_softc *sc = arg;
3200 struct ieee80211com *ic = &sc->sc_ic;
3202 ic_printf(ic, "scan timeout\n");
3203 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
3207 wpi_tx_timeout(void *arg)
3209 struct wpi_softc *sc = arg;
3210 struct ieee80211com *ic = &sc->sc_ic;
3212 ic_printf(ic, "device timeout\n");
3213 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
3217 wpi_parent(struct ieee80211com *ic)
3219 struct wpi_softc *sc = ic->ic_softc;
3220 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3222 if (ic->ic_nrunning > 0) {
3223 if (wpi_init(sc) == 0) {
3224 ieee80211_notify_radio(ic, 1);
3225 ieee80211_start_all(ic);
3227 ieee80211_notify_radio(ic, 0);
3228 ieee80211_stop(vap);
3235 * Send a command to the firmware.
3238 wpi_cmd(struct wpi_softc *sc, uint8_t code, const void *buf, uint16_t size,
3241 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
3242 struct wpi_tx_desc *desc;
3243 struct wpi_tx_data *data;
3244 struct wpi_tx_cmd *cmd;
3252 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3254 if (__predict_false(sc->sc_running == 0)) {
3255 /* wpi_stop() was called */
3256 if (code == WPI_CMD_SCAN)
3265 WPI_LOCK_ASSERT(sc);
3267 DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %u async %d\n",
3268 __func__, wpi_cmd_str(code), size, async);
3270 desc = &ring->desc[ring->cur];
3271 data = &ring->data[ring->cur];
3274 if (size > sizeof cmd->data) {
3275 /* Command is too large to fit in a descriptor. */
3276 if (totlen > MCLBYTES) {
3280 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3285 cmd = mtod(m, struct wpi_tx_cmd *);
3286 error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3287 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3294 cmd = &ring->cmd[ring->cur];
3295 paddr = data->cmd_paddr;
3300 cmd->qid = ring->qid;
3301 cmd->idx = ring->cur;
3302 memcpy(cmd->data, buf, size);
3304 desc->nsegs = 1 + (WPI_PAD32(size) << 4);
3305 desc->segs[0].addr = htole32(paddr);
3306 desc->segs[0].len = htole32(totlen);
3308 if (size > sizeof cmd->data) {
3309 bus_dmamap_sync(ring->data_dmat, data->map,
3310 BUS_DMASYNC_PREWRITE);
3312 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3313 BUS_DMASYNC_PREWRITE);
3315 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3316 BUS_DMASYNC_PREWRITE);
3318 /* Kick command ring. */
3319 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
3320 sc->sc_update_tx_ring(sc, ring);
3322 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3326 return async ? 0 : mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
3328 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3336 * Configure HW multi-rate retries.
3339 wpi_mrr_setup(struct wpi_softc *sc)
3341 struct ieee80211com *ic = &sc->sc_ic;
3342 struct wpi_mrr_setup mrr;
3346 /* CCK rates (not used with 802.11a). */
3347 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
3348 mrr.rates[i].flags = 0;
3349 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3350 /* Fallback to the immediate lower CCK rate (if any.) */
3352 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
3353 /* Try twice at this rate before falling back to "next". */
3354 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3356 /* OFDM rates (not used with 802.11b). */
3357 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
3358 mrr.rates[i].flags = 0;
3359 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3360 /* Fallback to the immediate lower rate (if any.) */
3361 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
3362 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
3363 ((ic->ic_curmode == IEEE80211_MODE_11A) ?
3364 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
3366 /* Try twice at this rate before falling back to "next". */
3367 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3369 /* Setup MRR for control frames. */
3370 mrr.which = htole32(WPI_MRR_CTL);
3371 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3373 device_printf(sc->sc_dev,
3374 "could not setup MRR for control frames\n");
3377 /* Setup MRR for data frames. */
3378 mrr.which = htole32(WPI_MRR_DATA);
3379 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3381 device_printf(sc->sc_dev,
3382 "could not setup MRR for data frames\n");
3389 wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3391 struct ieee80211com *ic = ni->ni_ic;
3392 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
3393 struct wpi_node *wn = WPI_NODE(ni);
3394 struct wpi_node_info node;
3397 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3399 if (wn->id == WPI_ID_UNDEFINED)
3402 memset(&node, 0, sizeof node);
3403 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
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 node %d (%s)\n", __func__,
3411 wn->id, ether_sprintf(ni->ni_macaddr));
3413 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
3415 device_printf(sc->sc_dev,
3416 "%s: wpi_cmd() call failed with error code %d\n", __func__,
3421 if (wvp->wv_gtk != 0) {
3422 error = wpi_set_global_keys(ni);
3424 device_printf(sc->sc_dev,
3425 "%s: error while setting global keys\n", __func__);
3434 * Broadcast node is used to send group-addressed and management frames.
3437 wpi_add_broadcast_node(struct wpi_softc *sc, int async)
3439 struct ieee80211com *ic = &sc->sc_ic;
3440 struct wpi_node_info node;
3442 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3444 memset(&node, 0, sizeof node);
3445 IEEE80211_ADDR_COPY(node.macaddr, ieee80211broadcastaddr);
3446 node.id = WPI_ID_BROADCAST;
3447 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3448 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3449 node.action = htole32(WPI_ACTION_SET_RATE);
3450 node.antenna = WPI_ANTENNA_BOTH;
3452 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__);
3454 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async);
3458 wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3460 struct wpi_node *wn = WPI_NODE(ni);
3463 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3465 wn->id = wpi_add_node_entry_sta(sc);
3467 if ((error = wpi_add_node(sc, ni)) != 0) {
3468 wpi_del_node_entry(sc, wn->id);
3469 wn->id = WPI_ID_UNDEFINED;
3477 wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3479 struct wpi_node *wn = WPI_NODE(ni);
3482 KASSERT(wn->id == WPI_ID_UNDEFINED,
3483 ("the node %d was added before", wn->id));
3485 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3487 if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) {
3488 device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__);
3492 if ((error = wpi_add_node(sc, ni)) != 0) {
3493 wpi_del_node_entry(sc, wn->id);
3494 wn->id = WPI_ID_UNDEFINED;
3502 wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3504 struct wpi_node *wn = WPI_NODE(ni);
3505 struct wpi_cmd_del_node node;
3508 KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed"));
3510 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3512 memset(&node, 0, sizeof node);
3513 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3516 DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__,
3517 wn->id, ether_sprintf(ni->ni_macaddr));
3519 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1);
3521 device_printf(sc->sc_dev,
3522 "%s: could not delete node %u, error %d\n", __func__,
3528 wpi_updateedca(struct ieee80211com *ic)
3530 #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3531 struct wpi_softc *sc = ic->ic_softc;
3532 struct wpi_edca_params cmd;
3535 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3537 memset(&cmd, 0, sizeof cmd);
3538 cmd.flags = htole32(WPI_EDCA_UPDATE);
3539 for (aci = 0; aci < WME_NUM_AC; aci++) {
3540 const struct wmeParams *ac =
3541 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci];
3542 cmd.ac[aci].aifsn = ac->wmep_aifsn;
3543 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin));
3544 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax));
3545 cmd.ac[aci].txoplimit =
3546 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit));
3548 DPRINTF(sc, WPI_DEBUG_EDCA,
3549 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
3550 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn,
3551 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax,
3552 cmd.ac[aci].txoplimit);
3554 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3556 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3563 wpi_set_promisc(struct wpi_softc *sc)
3565 struct ieee80211com *ic = &sc->sc_ic;
3566 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3567 uint32_t promisc_filter;
3569 promisc_filter = WPI_FILTER_CTL;
3570 if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP)
3571 promisc_filter |= WPI_FILTER_PROMISC;
3573 if (ic->ic_promisc > 0)
3574 sc->rxon.filter |= htole32(promisc_filter);
3576 sc->rxon.filter &= ~htole32(promisc_filter);
3580 wpi_update_promisc(struct ieee80211com *ic)
3582 struct wpi_softc *sc = ic->ic_softc;
3585 if (sc->sc_running == 0) {
3592 wpi_set_promisc(sc);
3594 if (wpi_send_rxon(sc, 1, 1) != 0) {
3595 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3598 WPI_RXON_UNLOCK(sc);
3602 wpi_update_mcast(struct ieee80211com *ic)
3608 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3610 struct wpi_cmd_led led;
3612 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3615 led.unit = htole32(100000); /* on/off in unit of 100ms */
3618 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
3622 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
3624 struct wpi_cmd_timing cmd;
3627 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3629 memset(&cmd, 0, sizeof cmd);
3630 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3631 cmd.bintval = htole16(ni->ni_intval);
3632 cmd.lintval = htole16(10);
3634 /* Compute remaining time until next beacon. */
3635 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU;
3636 mod = le64toh(cmd.tstamp) % val;
3637 cmd.binitval = htole32((uint32_t)(val - mod));
3639 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3640 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
3642 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
3646 * This function is called periodically (every 60 seconds) to adjust output
3647 * power to temperature changes.
3650 wpi_power_calibration(struct wpi_softc *sc)
3654 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3656 /* Update sensor data. */
3657 temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
3658 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp);
3660 /* Sanity-check read value. */
3661 if (temp < -260 || temp > 25) {
3662 /* This can't be correct, ignore. */
3663 DPRINTF(sc, WPI_DEBUG_TEMP,
3664 "out-of-range temperature reported: %d\n", temp);
3668 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp);
3670 /* Adjust Tx power if need be. */
3671 if (abs(temp - sc->temp) <= 6)
3676 if (wpi_set_txpower(sc, 1) != 0) {
3677 /* just warn, too bad for the automatic calibration... */
3678 device_printf(sc->sc_dev,"could not adjust Tx power\n");
3683 * Set TX power for current channel.
3686 wpi_set_txpower(struct wpi_softc *sc, int async)
3688 struct wpi_power_group *group;
3689 struct wpi_cmd_txpower cmd;
3691 int idx, is_chan_5ghz, i;
3693 /* Retrieve current channel from last RXON. */
3694 chan = sc->rxon.chan;
3695 is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0;
3697 /* Find the TX power group to which this channel belongs. */
3699 for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3700 if (chan <= group->chan)
3703 group = &sc->groups[0];
3705 memset(&cmd, 0, sizeof cmd);
3706 cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ;
3707 cmd.chan = htole16(chan);
3709 /* Set TX power for all OFDM and CCK rates. */
3710 for (i = 0; i <= WPI_RIDX_MAX ; i++) {
3711 /* Retrieve TX power for this channel/rate. */
3712 idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i);
3714 cmd.rates[i].plcp = wpi_ridx_to_plcp[i];
3717 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
3718 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
3720 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
3721 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
3723 DPRINTF(sc, WPI_DEBUG_TEMP,
3724 "chan %d/ridx %d: power index %d\n", chan, i, idx);
3727 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
3731 * Determine Tx power index for a given channel/rate combination.
3732 * This takes into account the regulatory information from EEPROM and the
3733 * current temperature.
3736 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3737 uint8_t chan, int is_chan_5ghz, int ridx)
3739 /* Fixed-point arithmetic division using a n-bit fractional part. */
3740 #define fdivround(a, b, n) \
3741 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3743 /* Linear interpolation. */
3744 #define interpolate(x, x1, y1, x2, y2, n) \
3745 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3747 struct wpi_power_sample *sample;
3750 /* Default TX power is group maximum TX power minus 3dB. */
3751 pwr = group->maxpwr / 2;
3753 /* Decrease TX power for highest OFDM rates to reduce distortion. */
3755 case WPI_RIDX_OFDM36:
3756 pwr -= is_chan_5ghz ? 5 : 0;
3758 case WPI_RIDX_OFDM48:
3759 pwr -= is_chan_5ghz ? 10 : 7;
3761 case WPI_RIDX_OFDM54:
3762 pwr -= is_chan_5ghz ? 12 : 9;
3766 /* Never exceed the channel maximum allowed TX power. */
3767 pwr = min(pwr, sc->maxpwr[chan]);
3769 /* Retrieve TX power index into gain tables from samples. */
3770 for (sample = group->samples; sample < &group->samples[3]; sample++)
3771 if (pwr > sample[1].power)
3773 /* Fixed-point linear interpolation using a 19-bit fractional part. */
3774 idx = interpolate(pwr, sample[0].power, sample[0].index,
3775 sample[1].power, sample[1].index, 19);
3778 * Adjust power index based on current temperature:
3779 * - if cooler than factory-calibrated: decrease output power
3780 * - if warmer than factory-calibrated: increase output power
3782 idx -= (sc->temp - group->temp) * 11 / 100;
3784 /* Decrease TX power for CCK rates (-5dB). */
3785 if (ridx >= WPI_RIDX_CCK1)
3788 /* Make sure idx stays in a valid range. */
3791 if (idx > WPI_MAX_PWR_INDEX)
3792 return WPI_MAX_PWR_INDEX;
3800 * Set STA mode power saving level (between 0 and 5).
3801 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
3804 wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async)
3806 struct wpi_pmgt_cmd cmd;
3807 const struct wpi_pmgt *pmgt;
3812 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
3813 "%s: dtim=%d, level=%d, async=%d\n",
3814 __func__, dtim, level, async);
3816 /* Select which PS parameters to use. */
3818 pmgt = &wpi_pmgt[0][level];
3820 pmgt = &wpi_pmgt[1][level];
3822 memset(&cmd, 0, sizeof cmd);
3823 if (level != 0) /* not CAM */
3824 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
3825 /* Retrieve PCIe Active State Power Management (ASPM). */
3826 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + PCIER_LINK_CTL, 1);
3827 if (!(reg & PCIEM_LINK_CTL_ASPMC_L0S)) /* L0s Entry disabled. */
3828 cmd.flags |= htole16(WPI_PS_PCI_PMGT);
3830 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU);
3831 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU);
3837 skip_dtim = pmgt->skip_dtim;
3839 if (skip_dtim != 0) {
3840 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
3841 max = pmgt->intval[4];
3842 if (max == (uint32_t)-1)
3843 max = dtim * (skip_dtim + 1);
3844 else if (max > dtim)
3845 max = (max / dtim) * dtim;
3849 for (i = 0; i < 5; i++)
3850 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
3852 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
3856 wpi_send_btcoex(struct wpi_softc *sc)
3858 struct wpi_bluetooth cmd;
3860 memset(&cmd, 0, sizeof cmd);
3861 cmd.flags = WPI_BT_COEX_MODE_4WIRE;
3862 cmd.lead_time = WPI_BT_LEAD_TIME_DEF;
3863 cmd.max_kill = WPI_BT_MAX_KILL_DEF;
3864 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n",
3866 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0);
3870 wpi_send_rxon(struct wpi_softc *sc, int assoc, int async)
3875 WPI_RXON_LOCK_ASSERT(sc);
3877 if (assoc && wpi_check_bss_filter(sc) != 0) {
3878 struct wpi_assoc rxon_assoc;
3880 rxon_assoc.flags = sc->rxon.flags;
3881 rxon_assoc.filter = sc->rxon.filter;
3882 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask;
3883 rxon_assoc.cck_mask = sc->rxon.cck_mask;
3884 rxon_assoc.reserved = 0;
3886 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc,
3887 sizeof (struct wpi_assoc), async);
3889 device_printf(sc->sc_dev,
3890 "RXON_ASSOC command failed, error %d\n", error);
3896 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3897 sizeof (struct wpi_rxon), async);
3899 wpi_clear_node_table(sc);
3902 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3903 sizeof (struct wpi_rxon), async);
3905 wpi_clear_node_table(sc);
3909 device_printf(sc->sc_dev,
3910 "RXON command failed, error %d\n", error);
3914 /* Add broadcast node. */
3915 error = wpi_add_broadcast_node(sc, async);
3917 device_printf(sc->sc_dev,
3918 "could not add broadcast node, error %d\n", error);
3923 /* Configuration has changed, set Tx power accordingly. */
3924 if ((error = wpi_set_txpower(sc, async)) != 0) {
3925 device_printf(sc->sc_dev,
3926 "%s: could not set TX power, error %d\n", __func__, error);
3934 * Configure the card to listen to a particular channel, this transisions the
3935 * card in to being able to receive frames from remote devices.
3938 wpi_config(struct wpi_softc *sc)
3940 struct ieee80211com *ic = &sc->sc_ic;
3941 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3942 struct ieee80211_channel *c = ic->ic_curchan;
3945 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3947 /* Set power saving level to CAM during initialization. */
3948 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
3949 device_printf(sc->sc_dev,
3950 "%s: could not set power saving level\n", __func__);
3954 /* Configure bluetooth coexistence. */
3955 if ((error = wpi_send_btcoex(sc)) != 0) {
3956 device_printf(sc->sc_dev,
3957 "could not configure bluetooth coexistence\n");
3961 /* Configure adapter. */
3962 memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
3963 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr);
3965 /* Set default channel. */
3966 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
3967 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
3968 if (IEEE80211_IS_CHAN_2GHZ(c))
3969 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
3971 sc->rxon.filter = WPI_FILTER_MULTICAST;
3972 switch (ic->ic_opmode) {
3973 case IEEE80211_M_STA:
3974 sc->rxon.mode = WPI_MODE_STA;
3976 case IEEE80211_M_IBSS:
3977 sc->rxon.mode = WPI_MODE_IBSS;
3978 sc->rxon.filter |= WPI_FILTER_BEACON;
3980 case IEEE80211_M_HOSTAP:
3981 /* XXX workaround for beaconing */
3982 sc->rxon.mode = WPI_MODE_IBSS;
3983 sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC;
3985 case IEEE80211_M_AHDEMO:
3986 sc->rxon.mode = WPI_MODE_HOSTAP;
3988 case IEEE80211_M_MONITOR:
3989 sc->rxon.mode = WPI_MODE_MONITOR;
3992 device_printf(sc->sc_dev, "unknown opmode %d\n",
3996 sc->rxon.filter = htole32(sc->rxon.filter);
3997 wpi_set_promisc(sc);
3998 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
3999 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
4001 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) {
4002 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4007 /* Setup rate scalling. */
4008 if ((error = wpi_mrr_setup(sc)) != 0) {
4009 device_printf(sc->sc_dev, "could not setup MRR, error %d\n",
4014 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4020 wpi_get_active_dwell_time(struct wpi_softc *sc,
4021 struct ieee80211_channel *c, uint8_t n_probes)
4023 /* No channel? Default to 2GHz settings. */
4024 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) {
4025 return (WPI_ACTIVE_DWELL_TIME_2GHZ +
4026 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1));
4029 /* 5GHz dwell time. */
4030 return (WPI_ACTIVE_DWELL_TIME_5GHZ +
4031 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1));
4035 * Limit the total dwell time.
4037 * Returns the dwell time in milliseconds.
4040 wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time)
4042 struct ieee80211com *ic = &sc->sc_ic;
4043 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4044 uint16_t bintval = 0;
4046 /* bintval is in TU (1.024mS) */
4048 bintval = vap->iv_bss->ni_intval;
4051 * If it's non-zero, we should calculate the minimum of
4052 * it and the DWELL_BASE.
4054 * XXX Yes, the math should take into account that bintval
4055 * is 1.024mS, not 1mS..
4058 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__,
4060 return (MIN(dwell_time, bintval - WPI_CHANNEL_TUNE_TIME * 2));
4063 /* No association context? Default. */
4068 wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c)
4072 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c))
4073 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ;
4075 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ;
4077 /* Clamp to the beacon interval if we're associated. */
4078 return (wpi_limit_dwell(sc, passive));
4082 wpi_get_scan_pause_time(uint32_t time, uint16_t bintval)
4084 uint32_t mod = (time % bintval) * IEEE80211_DUR_TU;
4085 uint32_t nbeacons = time / bintval;
4087 if (mod > WPI_PAUSE_MAX_TIME)
4088 mod = WPI_PAUSE_MAX_TIME;
4090 return WPI_PAUSE_SCAN(nbeacons, mod);
4094 * Send a scan request to the firmware.
4097 wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c)
4099 struct ieee80211com *ic = &sc->sc_ic;
4100 struct ieee80211_scan_state *ss = ic->ic_scan;
4101 struct ieee80211vap *vap = ss->ss_vap;
4102 struct wpi_scan_hdr *hdr;
4103 struct wpi_cmd_data *tx;
4104 struct wpi_scan_essid *essids;
4105 struct wpi_scan_chan *chan;
4106 struct ieee80211_frame *wh;
4107 struct ieee80211_rateset *rs;
4108 uint16_t bintval, buflen, dwell_active, dwell_passive;
4109 uint8_t *buf, *frm, i, nssid;
4112 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4115 * We are absolutely not allowed to send a scan command when another
4116 * scan command is pending.
4118 if (callout_pending(&sc->scan_timeout)) {
4119 device_printf(sc->sc_dev, "%s: called whilst scanning!\n",
4125 bgscan = wpi_check_bss_filter(sc);
4126 bintval = vap->iv_bss->ni_intval;
4128 bintval < WPI_QUIET_TIME_DEFAULT + WPI_CHANNEL_TUNE_TIME * 2) {
4133 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
4135 device_printf(sc->sc_dev,
4136 "%s: could not allocate buffer for scan command\n",
4141 hdr = (struct wpi_scan_hdr *)buf;
4144 * Move to the next channel if no packets are received within 10 msecs
4145 * after sending the probe request.
4147 hdr->quiet_time = htole16(WPI_QUIET_TIME_DEFAULT);
4148 hdr->quiet_threshold = htole16(1);
4152 * Max needs to be greater than active and passive and quiet!
4153 * It's also in microseconds!
4155 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU);
4156 hdr->pause_svc = htole32(wpi_get_scan_pause_time(100,
4160 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON);
4162 tx = (struct wpi_cmd_data *)(hdr + 1);
4163 tx->flags = htole32(WPI_TX_AUTO_SEQ);
4164 tx->id = WPI_ID_BROADCAST;
4165 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
4167 if (IEEE80211_IS_CHAN_5GHZ(c)) {
4168 /* Send probe requests at 6Mbps. */
4169 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6];
4170 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4172 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
4173 /* Send probe requests at 1Mbps. */
4174 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4175 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4178 essids = (struct wpi_scan_essid *)(tx + 1);
4179 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
4180 for (i = 0; i < nssid; i++) {
4181 essids[i].id = IEEE80211_ELEMID_SSID;
4182 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN);
4183 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len);
4185 if (sc->sc_debug & WPI_DEBUG_SCAN) {
4186 printf("Scanning Essid: ");
4187 ieee80211_print_essid(essids[i].data, essids[i].len);
4194 * Build a probe request frame. Most of the following code is a
4195 * copy & paste of what is done in net80211.
4197 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS);
4198 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4199 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4200 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
4201 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4202 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
4203 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4205 frm = (uint8_t *)(wh + 1);
4206 frm = ieee80211_add_ssid(frm, NULL, 0);
4207 frm = ieee80211_add_rates(frm, rs);
4208 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4209 frm = ieee80211_add_xrates(frm, rs);
4211 /* Set length of probe request. */
4212 tx->len = htole16(frm - (uint8_t *)wh);
4215 * Construct information about the channel that we
4216 * want to scan. The firmware expects this to be directly
4217 * after the scan probe request
4219 chan = (struct wpi_scan_chan *)frm;
4220 chan->chan = ieee80211_chan2ieee(ic, c);
4223 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT;
4224 chan->flags |= WPI_CHAN_NPBREQS(nssid);
4226 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER;
4228 if (!IEEE80211_IS_CHAN_PASSIVE(c))
4229 chan->flags |= WPI_CHAN_ACTIVE;
4232 * Calculate the active/passive dwell times.
4234 dwell_active = wpi_get_active_dwell_time(sc, c, nssid);
4235 dwell_passive = wpi_get_passive_dwell_time(sc, c);
4237 /* Make sure they're valid. */
4238 if (dwell_active > dwell_passive)
4239 dwell_active = dwell_passive;
4241 chan->active = htole16(dwell_active);
4242 chan->passive = htole16(dwell_passive);
4244 chan->dsp_gain = 0x6e; /* Default level */
4246 if (IEEE80211_IS_CHAN_5GHZ(c))
4247 chan->rf_gain = 0x3b;
4249 chan->rf_gain = 0x28;
4251 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n",
4252 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c));
4256 if (hdr->nchan == 1 && sc->rxon.chan == chan->chan) {
4257 /* XXX Force probe request transmission. */
4258 memcpy(chan + 1, chan, sizeof (struct wpi_scan_chan));
4262 /* Reduce unnecessary delay. */
4264 chan->passive = chan->active = hdr->quiet_time;
4271 buflen = (uint8_t *)chan - buf;
4272 hdr->len = htole16(buflen);
4274 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n",
4276 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
4277 free(buf, M_DEVBUF);
4282 callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc);
4284 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4288 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
4294 wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
4296 struct ieee80211com *ic = vap->iv_ic;
4297 struct ieee80211_node *ni = vap->iv_bss;
4298 struct ieee80211_channel *c = ni->ni_chan;
4303 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4305 /* Update adapter configuration. */
4306 sc->rxon.associd = 0;
4307 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
4308 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4309 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4310 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4311 if (IEEE80211_IS_CHAN_2GHZ(c))
4312 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4313 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4314 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4315 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4316 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4317 if (IEEE80211_IS_CHAN_A(c)) {
4318 sc->rxon.cck_mask = 0;
4319 sc->rxon.ofdm_mask = 0x15;
4320 } else if (IEEE80211_IS_CHAN_B(c)) {
4321 sc->rxon.cck_mask = 0x03;
4322 sc->rxon.ofdm_mask = 0;
4324 /* Assume 802.11b/g. */
4325 sc->rxon.cck_mask = 0x0f;
4326 sc->rxon.ofdm_mask = 0x15;
4329 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n",
4330 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask,
4331 sc->rxon.ofdm_mask);
4333 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4334 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4338 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4340 WPI_RXON_UNLOCK(sc);
4346 wpi_config_beacon(struct wpi_vap *wvp)
4348 struct ieee80211vap *vap = &wvp->wv_vap;
4349 struct ieee80211com *ic = vap->iv_ic;
4350 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
4351 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4352 struct wpi_softc *sc = ic->ic_softc;
4353 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
4354 struct ieee80211_tim_ie *tie;
4359 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4361 WPI_VAP_LOCK_ASSERT(wvp);
4363 cmd->len = htole16(bcn->m->m_pkthdr.len);
4364 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4365 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4367 /* XXX seems to be unused */
4368 if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) {
4369 tie = (struct ieee80211_tim_ie *) bo->bo_tim;
4370 ptr = mtod(bcn->m, uint8_t *);
4372 cmd->tim = htole16(bo->bo_tim - ptr);
4373 cmd->timsz = tie->tim_len;
4376 /* Necessary for recursion in ieee80211_beacon_update(). */
4378 bcn->m = m_dup(m, M_NOWAIT);
4379 if (bcn->m == NULL) {
4380 device_printf(sc->sc_dev,
4381 "%s: could not copy beacon frame\n", __func__);
4386 if ((error = wpi_cmd2(sc, bcn)) != 0) {
4387 device_printf(sc->sc_dev,
4388 "%s: could not update beacon frame, error %d", __func__,
4400 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
4402 struct ieee80211vap *vap = ni->ni_vap;
4403 struct wpi_vap *wvp = WPI_VAP(vap);
4404 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4408 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4410 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
4413 m = ieee80211_beacon_alloc(ni);
4415 device_printf(sc->sc_dev,
4416 "%s: could not allocate beacon frame\n", __func__);
4426 error = wpi_config_beacon(wvp);
4427 WPI_VAP_UNLOCK(wvp);
4433 wpi_update_beacon(struct ieee80211vap *vap, int item)
4435 struct wpi_softc *sc = vap->iv_ic->ic_softc;
4436 struct wpi_vap *wvp = WPI_VAP(vap);
4437 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4438 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
4439 struct ieee80211_node *ni = vap->iv_bss;
4442 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4445 if (bcn->m == NULL) {
4446 bcn->m = ieee80211_beacon_alloc(ni);
4447 if (bcn->m == NULL) {
4448 device_printf(sc->sc_dev,
4449 "%s: could not allocate beacon frame\n", __func__);
4451 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR,
4454 WPI_VAP_UNLOCK(wvp);
4458 WPI_VAP_UNLOCK(wvp);
4460 if (item == IEEE80211_BEACON_TIM)
4461 mcast = 1; /* TODO */
4463 setbit(bo->bo_flags, item);
4464 ieee80211_beacon_update(ni, bcn->m, mcast);
4467 wpi_config_beacon(wvp);
4468 WPI_VAP_UNLOCK(wvp);
4470 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4474 wpi_newassoc(struct ieee80211_node *ni, int isnew)
4476 struct ieee80211vap *vap = ni->ni_vap;
4477 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4478 struct wpi_node *wn = WPI_NODE(ni);
4483 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4485 if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) {
4486 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
4487 device_printf(sc->sc_dev,
4488 "%s: could not add IBSS node, error %d\n",
4496 wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
4498 struct ieee80211com *ic = vap->iv_ic;
4499 struct ieee80211_node *ni = vap->iv_bss;
4500 struct ieee80211_channel *c = ni->ni_chan;
4503 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4505 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
4506 /* Link LED blinks while monitoring. */
4507 wpi_set_led(sc, WPI_LED_LINK, 5, 5);
4511 /* XXX kernel panic workaround */
4512 if (c == IEEE80211_CHAN_ANYC) {
4513 device_printf(sc->sc_dev, "%s: incomplete configuration\n",
4518 if ((error = wpi_set_timing(sc, ni)) != 0) {
4519 device_printf(sc->sc_dev,
4520 "%s: could not set timing, error %d\n", __func__, error);
4524 /* Update adapter configuration. */
4526 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4527 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni));
4528 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4529 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4530 if (IEEE80211_IS_CHAN_2GHZ(c))
4531 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4532 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4533 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4534 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4535 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4536 if (IEEE80211_IS_CHAN_A(c)) {
4537 sc->rxon.cck_mask = 0;
4538 sc->rxon.ofdm_mask = 0x15;
4539 } else if (IEEE80211_IS_CHAN_B(c)) {
4540 sc->rxon.cck_mask = 0x03;
4541 sc->rxon.ofdm_mask = 0;
4543 /* Assume 802.11b/g. */
4544 sc->rxon.cck_mask = 0x0f;
4545 sc->rxon.ofdm_mask = 0x15;
4547 sc->rxon.filter |= htole32(WPI_FILTER_BSS);
4549 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n",
4550 sc->rxon.chan, sc->rxon.flags);
4552 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4553 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4558 /* Start periodic calibration timer. */
4559 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
4561 WPI_RXON_UNLOCK(sc);
4563 if (vap->iv_opmode == IEEE80211_M_IBSS ||
4564 vap->iv_opmode == IEEE80211_M_HOSTAP) {
4565 if ((error = wpi_setup_beacon(sc, ni)) != 0) {
4566 device_printf(sc->sc_dev,
4567 "%s: could not setup beacon, error %d\n", __func__,
4573 if (vap->iv_opmode == IEEE80211_M_STA) {
4576 error = wpi_add_sta_node(sc, ni);
4579 device_printf(sc->sc_dev,
4580 "%s: could not add BSS node, error %d\n", __func__,
4586 /* Link LED always on while associated. */
4587 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
4589 /* Enable power-saving mode if requested by user. */
4590 if ((vap->iv_flags & IEEE80211_F_PMGTON) &&
4591 vap->iv_opmode != IEEE80211_M_IBSS)
4592 (void)wpi_set_pslevel(sc, 0, 3, 1);
4594 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4600 wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4602 const struct ieee80211_cipher *cip = k->wk_cipher;
4603 struct ieee80211vap *vap = ni->ni_vap;
4604 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4605 struct wpi_node *wn = WPI_NODE(ni);
4606 struct wpi_node_info node;
4610 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4612 if (wpi_check_node_entry(sc, wn->id) == 0) {
4613 device_printf(sc->sc_dev, "%s: node does not exist\n",
4618 switch (cip->ic_cipher) {
4619 case IEEE80211_CIPHER_AES_CCM:
4620 kflags = WPI_KFLAG_CCMP;
4624 device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__,
4629 kflags |= WPI_KFLAG_KID(k->wk_keyix);
4630 if (k->wk_flags & IEEE80211_KEY_GROUP)
4631 kflags |= WPI_KFLAG_MULTICAST;
4633 memset(&node, 0, sizeof node);
4635 node.control = WPI_NODE_UPDATE;
4636 node.flags = WPI_FLAG_KEY_SET;
4637 node.kflags = htole16(kflags);
4638 memcpy(node.key, k->wk_key, k->wk_keylen);
4640 DPRINTF(sc, WPI_DEBUG_KEY,
4641 "%s: setting %s key id %d for node %d (%s)\n", __func__,
4642 (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix,
4643 node.id, ether_sprintf(ni->ni_macaddr));
4645 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4647 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4652 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4653 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4654 kflags |= WPI_KFLAG_MULTICAST;
4655 node.kflags = htole16(kflags);
4664 wpi_load_key_cb(void *arg, struct ieee80211_node *ni)
4666 const struct ieee80211_key *k = arg;
4667 struct ieee80211vap *vap = ni->ni_vap;
4668 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4669 struct wpi_node *wn = WPI_NODE(ni);
4672 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4676 error = wpi_load_key(ni, k);
4680 device_printf(sc->sc_dev, "%s: error while setting key\n",
4686 wpi_set_global_keys(struct ieee80211_node *ni)
4688 struct ieee80211vap *vap = ni->ni_vap;
4689 struct ieee80211_key *wk = &vap->iv_nw_keys[0];
4692 for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++)
4693 if (wk->wk_keyix != IEEE80211_KEYIX_NONE)
4694 error = wpi_load_key(ni, wk);
4700 wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4702 struct ieee80211vap *vap = ni->ni_vap;
4703 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4704 struct wpi_node *wn = WPI_NODE(ni);
4705 struct wpi_node_info node;
4709 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4711 if (wpi_check_node_entry(sc, wn->id) == 0) {
4712 DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__);
4713 return 1; /* Nothing to do. */
4716 kflags = WPI_KFLAG_KID(k->wk_keyix);
4717 if (k->wk_flags & IEEE80211_KEY_GROUP)
4718 kflags |= WPI_KFLAG_MULTICAST;
4720 memset(&node, 0, sizeof node);
4722 node.control = WPI_NODE_UPDATE;
4723 node.flags = WPI_FLAG_KEY_SET;
4724 node.kflags = htole16(kflags);
4726 DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n",
4727 __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast",
4728 k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr));
4730 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4732 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4737 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4738 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4739 kflags |= WPI_KFLAG_MULTICAST;
4740 node.kflags = htole16(kflags);
4749 wpi_del_key_cb(void *arg, struct ieee80211_node *ni)
4751 const struct ieee80211_key *k = arg;
4752 struct ieee80211vap *vap = ni->ni_vap;
4753 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4754 struct wpi_node *wn = WPI_NODE(ni);
4757 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4761 error = wpi_del_key(ni, k);
4765 device_printf(sc->sc_dev, "%s: error while deleting key\n",
4771 wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
4774 struct ieee80211com *ic = vap->iv_ic;
4775 struct wpi_softc *sc = ic->ic_softc;
4776 struct wpi_vap *wvp = WPI_VAP(vap);
4777 struct ieee80211_node *ni;
4778 int error, ni_ref = 0;
4780 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4782 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
4787 if (!(k->wk_flags & IEEE80211_KEY_RECV)) {
4788 /* XMIT keys are handled in wpi_tx_data(). */
4792 /* Handle group keys. */
4793 if (&vap->iv_nw_keys[0] <= k &&
4794 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4797 wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix);
4799 wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix);
4802 if (vap->iv_state == IEEE80211_S_RUN) {
4803 ieee80211_iterate_nodes(&ic->ic_sta,
4804 set ? wpi_load_key_cb : wpi_del_key_cb,
4805 __DECONST(void *, k));
4811 switch (vap->iv_opmode) {
4812 case IEEE80211_M_STA:
4816 case IEEE80211_M_IBSS:
4817 case IEEE80211_M_AHDEMO:
4818 case IEEE80211_M_HOSTAP:
4819 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
4821 return 0; /* should not happen */
4827 device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__,
4834 error = wpi_load_key(ni, k);
4836 error = wpi_del_key(ni, k);
4840 ieee80211_node_decref(ni);
4846 wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
4848 return wpi_process_key(vap, k, 1);
4852 wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4854 return wpi_process_key(vap, k, 0);
4858 * This function is called after the runtime firmware notifies us of its
4859 * readiness (called in a process context).
4862 wpi_post_alive(struct wpi_softc *sc)
4866 /* Check (again) that the radio is not disabled. */
4867 if ((error = wpi_nic_lock(sc)) != 0)
4870 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4872 /* NB: Runtime firmware must be up and running. */
4873 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
4874 device_printf(sc->sc_dev,
4875 "RF switch: radio disabled (%s)\n", __func__);
4877 return EPERM; /* :-) */
4881 /* Wait for thermal sensor to calibrate. */
4882 for (ntries = 0; ntries < 1000; ntries++) {
4883 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
4888 if (ntries == 1000) {
4889 device_printf(sc->sc_dev,
4890 "timeout waiting for thermal sensor calibration\n");
4894 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp);
4899 * The firmware boot code is small and is intended to be copied directly into
4900 * the NIC internal memory (no DMA transfer).
4903 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, uint32_t size)
4907 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size);
4909 size /= sizeof (uint32_t);
4911 if ((error = wpi_nic_lock(sc)) != 0)
4914 /* Copy microcode image into NIC memory. */
4915 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
4916 (const uint32_t *)ucode, size);
4918 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
4919 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
4920 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
4922 /* Start boot load now. */
4923 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
4925 /* Wait for transfer to complete. */
4926 for (ntries = 0; ntries < 1000; ntries++) {
4927 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS);
4928 DPRINTF(sc, WPI_DEBUG_HW,
4929 "firmware status=0x%x, val=0x%x, result=0x%x\n", status,
4930 WPI_FH_TX_STATUS_IDLE(6),
4931 status & WPI_FH_TX_STATUS_IDLE(6));
4932 if (status & WPI_FH_TX_STATUS_IDLE(6)) {
4933 DPRINTF(sc, WPI_DEBUG_HW,
4934 "Status Match! - ntries = %d\n", ntries);
4939 if (ntries == 1000) {
4940 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4946 /* Enable boot after power up. */
4947 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
4954 wpi_load_firmware(struct wpi_softc *sc)
4956 struct wpi_fw_info *fw = &sc->fw;
4957 struct wpi_dma_info *dma = &sc->fw_dma;
4960 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4962 /* Copy initialization sections into pre-allocated DMA-safe memory. */
4963 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
4964 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4965 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz);
4966 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4968 /* Tell adapter where to find initialization sections. */
4969 if ((error = wpi_nic_lock(sc)) != 0)
4971 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4972 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
4973 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4974 dma->paddr + WPI_FW_DATA_MAXSZ);
4975 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
4978 /* Load firmware boot code. */
4979 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
4981 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4986 /* Now press "execute". */
4987 WPI_WRITE(sc, WPI_RESET, 0);
4989 /* Wait at most one second for first alive notification. */
4990 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
4991 device_printf(sc->sc_dev,
4992 "%s: timeout waiting for adapter to initialize, error %d\n",
4997 /* Copy runtime sections into pre-allocated DMA-safe memory. */
4998 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
4999 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
5000 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz);
5001 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
5003 /* Tell adapter where to find runtime sections. */
5004 if ((error = wpi_nic_lock(sc)) != 0)
5006 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
5007 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
5008 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
5009 dma->paddr + WPI_FW_DATA_MAXSZ);
5010 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
5011 WPI_FW_UPDATED | fw->main.textsz);
5018 wpi_read_firmware(struct wpi_softc *sc)
5020 const struct firmware *fp;
5021 struct wpi_fw_info *fw = &sc->fw;
5022 const struct wpi_firmware_hdr *hdr;
5025 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5027 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
5028 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME);
5031 fp = firmware_get(WPI_FW_NAME);
5035 device_printf(sc->sc_dev,
5036 "could not load firmware image '%s'\n", WPI_FW_NAME);
5042 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
5043 device_printf(sc->sc_dev,
5044 "firmware file too short: %zu bytes\n", fp->datasize);
5049 fw->size = fp->datasize;
5050 fw->data = (const uint8_t *)fp->data;
5052 /* Extract firmware header information. */
5053 hdr = (const struct wpi_firmware_hdr *)fw->data;
5055 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW |
5056 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
5058 fw->main.textsz = le32toh(hdr->rtextsz);
5059 fw->main.datasz = le32toh(hdr->rdatasz);
5060 fw->init.textsz = le32toh(hdr->itextsz);
5061 fw->init.datasz = le32toh(hdr->idatasz);
5062 fw->boot.textsz = le32toh(hdr->btextsz);
5063 fw->boot.datasz = 0;
5065 /* Sanity-check firmware header. */
5066 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
5067 fw->main.datasz > WPI_FW_DATA_MAXSZ ||
5068 fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
5069 fw->init.datasz > WPI_FW_DATA_MAXSZ ||
5070 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
5071 (fw->boot.textsz & 3) != 0) {
5072 device_printf(sc->sc_dev, "invalid firmware header\n");
5077 /* Check that all firmware sections fit. */
5078 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
5079 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
5080 device_printf(sc->sc_dev,
5081 "firmware file too short: %zu bytes\n", fw->size);
5086 /* Get pointers to firmware sections. */
5087 fw->main.text = (const uint8_t *)(hdr + 1);
5088 fw->main.data = fw->main.text + fw->main.textsz;
5089 fw->init.text = fw->main.data + fw->main.datasz;
5090 fw->init.data = fw->init.text + fw->init.textsz;
5091 fw->boot.text = fw->init.data + fw->init.datasz;
5093 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
5094 "Firmware Version: Major %d, Minor %d, Driver %d, \n"
5095 "runtime (text: %u, data: %u) init (text: %u, data %u) "
5096 "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver),
5097 fw->main.textsz, fw->main.datasz,
5098 fw->init.textsz, fw->init.datasz, fw->boot.textsz);
5100 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text);
5101 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data);
5102 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text);
5103 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data);
5104 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text);
5108 fail: wpi_unload_firmware(sc);
5113 * Free the referenced firmware image
5116 wpi_unload_firmware(struct wpi_softc *sc)
5118 if (sc->fw_fp != NULL) {
5119 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
5125 wpi_clock_wait(struct wpi_softc *sc)
5129 /* Set "initialization complete" bit. */
5130 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5132 /* Wait for clock stabilization. */
5133 for (ntries = 0; ntries < 2500; ntries++) {
5134 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
5138 device_printf(sc->sc_dev,
5139 "%s: timeout waiting for clock stabilization\n", __func__);
5145 wpi_apm_init(struct wpi_softc *sc)
5150 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5152 /* Disable L0s exit timer (NMI bug workaround). */
5153 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER);
5154 /* Don't wait for ICH L0s (ICH bug workaround). */
5155 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
5157 /* Set FH wait threshold to max (HW bug under stress workaround). */
5158 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000);
5160 /* Retrieve PCIe Active State Power Management (ASPM). */
5161 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + PCIER_LINK_CTL, 1);
5162 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
5163 if (reg & PCIEM_LINK_CTL_ASPMC_L1) /* L1 Entry enabled. */
5164 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5166 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5168 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
5170 /* Wait for clock stabilization before accessing prph. */
5171 if ((error = wpi_clock_wait(sc)) != 0)
5174 if ((error = wpi_nic_lock(sc)) != 0)
5177 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400);
5178 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200);
5180 /* Enable DMA and BSM (Bootstrap State Machine). */
5181 wpi_prph_write(sc, WPI_APMG_CLK_EN,
5182 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT);
5184 /* Disable L1-Active. */
5185 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
5192 wpi_apm_stop_master(struct wpi_softc *sc)
5196 /* Stop busmaster DMA activity. */
5197 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
5199 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
5200 WPI_GP_CNTRL_MAC_PS)
5201 return; /* Already asleep. */
5203 for (ntries = 0; ntries < 100; ntries++) {
5204 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
5208 device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
5213 wpi_apm_stop(struct wpi_softc *sc)
5215 wpi_apm_stop_master(sc);
5217 /* Reset the entire device. */
5218 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
5220 /* Clear "initialization complete" bit. */
5221 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5225 wpi_nic_config(struct wpi_softc *sc)
5229 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5231 /* voodoo from the Linux "driver".. */
5232 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
5233 if ((rev & 0xc0) == 0x40)
5234 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
5235 else if (!(rev & 0x80))
5236 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
5238 if (sc->cap == 0x80)
5239 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
5241 if ((sc->rev & 0xf0) == 0xd0)
5242 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5244 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5247 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
5251 wpi_hw_init(struct wpi_softc *sc)
5256 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5258 /* Clear pending interrupts. */
5259 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5261 if ((error = wpi_apm_init(sc)) != 0) {
5262 device_printf(sc->sc_dev,
5263 "%s: could not power ON adapter, error %d\n", __func__,
5268 /* Select VMAIN power source. */
5269 if ((error = wpi_nic_lock(sc)) != 0)
5271 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
5273 /* Spin until VMAIN gets selected. */
5274 for (ntries = 0; ntries < 5000; ntries++) {
5275 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
5279 if (ntries == 5000) {
5280 device_printf(sc->sc_dev, "timeout selecting power source\n");
5284 /* Perform adapter initialization. */
5287 /* Initialize RX ring. */
5288 if ((error = wpi_nic_lock(sc)) != 0)
5290 /* Set physical address of RX ring. */
5291 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
5292 /* Set physical address of RX read pointer. */
5293 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
5294 offsetof(struct wpi_shared, next));
5295 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
5297 WPI_WRITE(sc, WPI_FH_RX_CONFIG,
5298 WPI_FH_RX_CONFIG_DMA_ENA |
5299 WPI_FH_RX_CONFIG_RDRBD_ENA |
5300 WPI_FH_RX_CONFIG_WRSTATUS_ENA |
5301 WPI_FH_RX_CONFIG_MAXFRAG |
5302 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
5303 WPI_FH_RX_CONFIG_IRQ_DST_HOST |
5304 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1));
5305 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */
5307 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
5309 /* Initialize TX rings. */
5310 if ((error = wpi_nic_lock(sc)) != 0)
5312 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */
5313 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */
5314 /* Enable all 6 TX rings. */
5315 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
5316 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
5317 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
5318 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
5319 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
5320 /* Set physical address of TX rings. */
5321 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
5322 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
5324 /* Enable all DMA channels. */
5325 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5326 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
5327 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
5328 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
5331 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */
5333 /* Clear "radio off" and "commands blocked" bits. */
5334 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5335 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
5337 /* Clear pending interrupts. */
5338 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5339 /* Enable interrupts. */
5340 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
5342 /* _Really_ make sure "radio off" bit is cleared! */
5343 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5344 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5346 if ((error = wpi_load_firmware(sc)) != 0) {
5347 device_printf(sc->sc_dev,
5348 "%s: could not load firmware, error %d\n", __func__,
5352 /* Wait at most one second for firmware alive notification. */
5353 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
5354 device_printf(sc->sc_dev,
5355 "%s: timeout waiting for adapter to initialize, error %d\n",
5360 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5362 /* Do post-firmware initialization. */
5363 return wpi_post_alive(sc);
5367 wpi_hw_stop(struct wpi_softc *sc)
5372 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5374 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP)
5377 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
5379 /* Disable interrupts. */
5380 WPI_WRITE(sc, WPI_INT_MASK, 0);
5381 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5382 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
5384 /* Make sure we no longer hold the NIC lock. */
5387 if (wpi_nic_lock(sc) == 0) {
5388 /* Stop TX scheduler. */
5389 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
5390 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
5392 /* Stop all DMA channels. */
5393 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5394 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
5395 for (ntries = 0; ntries < 200; ntries++) {
5396 if (WPI_READ(sc, WPI_FH_TX_STATUS) &
5397 WPI_FH_TX_STATUS_IDLE(chnl))
5406 wpi_reset_rx_ring(sc);
5408 /* Reset all TX rings. */
5409 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++)
5410 wpi_reset_tx_ring(sc, &sc->txq[qid]);
5412 if (wpi_nic_lock(sc) == 0) {
5413 wpi_prph_write(sc, WPI_APMG_CLK_DIS,
5414 WPI_APMG_CLK_CTRL_DMA_CLK_RQT);
5418 /* Power OFF adapter. */
5423 wpi_radio_on(void *arg0, int pending)
5425 struct wpi_softc *sc = arg0;
5426 struct ieee80211com *ic = &sc->sc_ic;
5427 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5429 device_printf(sc->sc_dev, "RF switch: radio enabled\n");
5432 callout_stop(&sc->watchdog_rfkill);
5436 ieee80211_init(vap);
5440 wpi_radio_off(void *arg0, int pending)
5442 struct wpi_softc *sc = arg0;
5443 struct ieee80211com *ic = &sc->sc_ic;
5444 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5446 device_printf(sc->sc_dev, "RF switch: radio disabled\n");
5448 ieee80211_notify_radio(ic, 0);
5451 ieee80211_stop(vap);
5454 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc);
5459 wpi_init(struct wpi_softc *sc)
5465 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5467 if (sc->sc_running != 0)
5470 /* Check that the radio is not disabled by hardware switch. */
5471 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
5472 device_printf(sc->sc_dev,
5473 "RF switch: radio disabled (%s)\n", __func__);
5474 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
5476 error = EINPROGRESS;
5480 /* Read firmware images from the filesystem. */
5481 if ((error = wpi_read_firmware(sc)) != 0) {
5482 device_printf(sc->sc_dev,
5483 "%s: could not read firmware, error %d\n", __func__,
5490 /* Initialize hardware and upload firmware. */
5491 error = wpi_hw_init(sc);
5492 wpi_unload_firmware(sc);
5494 device_printf(sc->sc_dev,
5495 "%s: could not initialize hardware, error %d\n", __func__,
5500 /* Configure adapter now that it is ready. */
5501 if ((error = wpi_config(sc)) != 0) {
5502 device_printf(sc->sc_dev,
5503 "%s: could not configure device, error %d\n", __func__,
5508 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5514 fail: wpi_stop_locked(sc);
5516 end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
5523 wpi_stop_locked(struct wpi_softc *sc)
5526 WPI_LOCK_ASSERT(sc);
5528 if (sc->sc_running == 0)
5537 WPI_TXQ_STATE_LOCK(sc);
5538 callout_stop(&sc->tx_timeout);
5539 WPI_TXQ_STATE_UNLOCK(sc);
5542 callout_stop(&sc->scan_timeout);
5543 callout_stop(&sc->calib_to);
5544 WPI_RXON_UNLOCK(sc);
5546 /* Power OFF hardware. */
5551 wpi_stop(struct wpi_softc *sc)
5554 wpi_stop_locked(sc);
5559 * Callback from net80211 to start a scan.
5562 wpi_scan_start(struct ieee80211com *ic)
5564 struct wpi_softc *sc = ic->ic_softc;
5566 wpi_set_led(sc, WPI_LED_LINK, 20, 2);
5570 * Callback from net80211 to terminate a scan.
5573 wpi_scan_end(struct ieee80211com *ic)
5575 struct wpi_softc *sc = ic->ic_softc;
5576 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5578 if (vap->iv_state == IEEE80211_S_RUN)
5579 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
5583 * Called by the net80211 framework to indicate to the driver
5584 * that the channel should be changed
5587 wpi_set_channel(struct ieee80211com *ic)
5589 const struct ieee80211_channel *c = ic->ic_curchan;
5590 struct wpi_softc *sc = ic->ic_softc;
5593 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5596 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
5597 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
5600 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
5601 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
5605 * Only need to set the channel in Monitor mode. AP scanning and auth
5606 * are already taken care of by their respective firmware commands.
5608 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
5610 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
5611 if (IEEE80211_IS_CHAN_2GHZ(c)) {
5612 sc->rxon.flags |= htole32(WPI_RXON_AUTO |
5615 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO |
5618 if ((error = wpi_send_rxon(sc, 0, 1)) != 0)
5619 device_printf(sc->sc_dev,
5620 "%s: error %d setting channel\n", __func__,
5622 WPI_RXON_UNLOCK(sc);
5627 * Called by net80211 to indicate that we need to scan the current
5628 * channel. The channel is previously be set via the wpi_set_channel
5632 wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
5634 struct ieee80211vap *vap = ss->ss_vap;
5635 struct ieee80211com *ic = vap->iv_ic;
5636 struct wpi_softc *sc = ic->ic_softc;
5640 error = wpi_scan(sc, ic->ic_curchan);
5641 WPI_RXON_UNLOCK(sc);
5643 ieee80211_cancel_scan(vap);
5647 * Called by the net80211 framework to indicate
5648 * the minimum dwell time has been met, terminate the scan.
5649 * We don't actually terminate the scan as the firmware will notify
5650 * us when it's finished and we have no way to interrupt it.
5653 wpi_scan_mindwell(struct ieee80211_scan_state *ss)
5655 /* NB: don't try to abort scan; wait for firmware to finish */
5659 wpi_hw_reset(void *arg, int pending)
5661 struct wpi_softc *sc = arg;
5662 struct ieee80211com *ic = &sc->sc_ic;
5663 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5665 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5667 ieee80211_notify_radio(ic, 0);
5668 if (vap != NULL && (ic->ic_flags & IEEE80211_F_SCAN))
5669 ieee80211_cancel_scan(vap);
5673 ieee80211_stop(vap);
5674 ieee80211_init(vap);