2 * Copyright (c) 2006,2007
3 * Damien Bergamini <damien.bergamini@free.fr>
4 * Benjamin Close <Benjamin.Close@clearchain.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
23 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
25 * The 3945ABG network adapter doesn't use traditional hardware as
26 * many other adaptors do. Instead at run time the eeprom is set into a known
27 * state and told to load boot firmware. The boot firmware loads an init and a
28 * main binary firmware image into SRAM on the card via DMA.
29 * Once the firmware is loaded, the driver/hw then
30 * communicate by way of circular dma rings via the SRAM to the firmware.
32 * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings.
33 * The 4 tx data rings allow for prioritization QoS.
35 * The rx data ring consists of 32 dma buffers. Two registers are used to
36 * indicate where in the ring the driver and the firmware are up to. The
37 * driver sets the initial read index (reg1) and the initial write index (reg2),
38 * the firmware updates the read index (reg1) on rx of a packet and fires an
39 * interrupt. The driver then processes the buffers starting at reg1 indicating
40 * to the firmware which buffers have been accessed by updating reg2. At the
41 * same time allocating new memory for the processed buffer.
43 * A similar thing happens with the tx rings. The difference is the firmware
44 * stop processing buffers once the queue is full and until confirmation
45 * of a successful transmition (tx_done) has occurred.
47 * The command ring operates in the same manner as the tx queues.
49 * All communication direct to the card (ie eeprom) is classed as Stage1
52 * All communication via the firmware to the card is classed as State2.
53 * The firmware consists of 2 parts. A bootstrap firmware and a runtime
54 * firmware. The bootstrap firmware and runtime firmware are loaded
55 * from host memory via dma to the card then told to execute. From this point
56 * on the majority of communications between the driver and the card goes
63 #include <sys/param.h>
64 #include <sys/sysctl.h>
65 #include <sys/sockio.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/queue.h>
72 #include <sys/taskqueue.h>
73 #include <sys/module.h>
75 #include <sys/endian.h>
76 #include <sys/linker.h>
77 #include <sys/firmware.h>
79 #include <machine/bus.h>
80 #include <machine/resource.h>
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcivar.h>
88 #include <net/if_var.h>
89 #include <net/if_arp.h>
90 #include <net/ethernet.h>
91 #include <net/if_dl.h>
92 #include <net/if_media.h>
93 #include <net/if_types.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_var.h>
98 #include <netinet/if_ether.h>
99 #include <netinet/ip.h>
101 #include <net80211/ieee80211_var.h>
102 #include <net80211/ieee80211_radiotap.h>
103 #include <net80211/ieee80211_regdomain.h>
104 #include <net80211/ieee80211_ratectl.h>
106 #include <dev/wpi/if_wpireg.h>
107 #include <dev/wpi/if_wpivar.h>
108 #include <dev/wpi/if_wpi_debug.h>
117 static const struct wpi_ident wpi_ident_table[] = {
118 /* The below entries support ABG regardless of the subid */
119 { 0x8086, 0x4222, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
120 { 0x8086, 0x4227, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
121 /* The below entries only support BG */
122 { 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG" },
123 { 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG" },
124 { 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG" },
125 { 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG" },
129 static int wpi_probe(device_t);
130 static int wpi_attach(device_t);
131 static void wpi_radiotap_attach(struct wpi_softc *);
132 static void wpi_sysctlattach(struct wpi_softc *);
133 static void wpi_init_beacon(struct wpi_vap *);
134 static struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
135 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
136 const uint8_t [IEEE80211_ADDR_LEN],
137 const uint8_t [IEEE80211_ADDR_LEN]);
138 static void wpi_vap_delete(struct ieee80211vap *);
139 static int wpi_detach(device_t);
140 static int wpi_shutdown(device_t);
141 static int wpi_suspend(device_t);
142 static int wpi_resume(device_t);
143 static int wpi_nic_lock(struct wpi_softc *);
144 static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
145 static void wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
146 static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
147 void **, bus_size_t, bus_size_t);
148 static void wpi_dma_contig_free(struct wpi_dma_info *);
149 static int wpi_alloc_shared(struct wpi_softc *);
150 static void wpi_free_shared(struct wpi_softc *);
151 static int wpi_alloc_fwmem(struct wpi_softc *);
152 static void wpi_free_fwmem(struct wpi_softc *);
153 static int wpi_alloc_rx_ring(struct wpi_softc *);
154 static void wpi_update_rx_ring(struct wpi_softc *);
155 static void wpi_update_rx_ring_ps(struct wpi_softc *);
156 static void wpi_reset_rx_ring(struct wpi_softc *);
157 static void wpi_free_rx_ring(struct wpi_softc *);
158 static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
160 static void wpi_update_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
161 static void wpi_update_tx_ring_ps(struct wpi_softc *,
162 struct wpi_tx_ring *);
163 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
164 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
165 static int wpi_read_eeprom(struct wpi_softc *,
166 uint8_t macaddr[IEEE80211_ADDR_LEN]);
167 static uint32_t wpi_eeprom_channel_flags(struct wpi_eeprom_chan *);
168 static void wpi_read_eeprom_band(struct wpi_softc *, int);
169 static int wpi_read_eeprom_channels(struct wpi_softc *, int);
170 static struct wpi_eeprom_chan *wpi_find_eeprom_channel(struct wpi_softc *,
171 struct ieee80211_channel *);
172 static int wpi_setregdomain(struct ieee80211com *,
173 struct ieee80211_regdomain *, int,
174 struct ieee80211_channel[]);
175 static int wpi_read_eeprom_group(struct wpi_softc *, int);
176 static int wpi_add_node_entry_adhoc(struct wpi_softc *);
177 static struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
178 const uint8_t mac[IEEE80211_ADDR_LEN]);
179 static void wpi_node_free(struct ieee80211_node *);
180 static void wpi_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
181 const struct ieee80211_rx_stats *,
183 static void wpi_restore_node(void *, struct ieee80211_node *);
184 static void wpi_restore_node_table(struct wpi_softc *, struct wpi_vap *);
185 static int wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
186 static void wpi_calib_timeout(void *);
187 static void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *,
188 struct wpi_rx_data *);
189 static void wpi_rx_statistics(struct wpi_softc *, struct wpi_rx_desc *,
190 struct wpi_rx_data *);
191 static void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *);
192 static void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *);
193 static void wpi_notif_intr(struct wpi_softc *);
194 static void wpi_wakeup_intr(struct wpi_softc *);
196 static void wpi_debug_registers(struct wpi_softc *);
198 static void wpi_fatal_intr(struct wpi_softc *);
199 static void wpi_intr(void *);
200 static int wpi_cmd2(struct wpi_softc *, struct wpi_buf *);
201 static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
202 struct ieee80211_node *);
203 static int wpi_tx_data_raw(struct wpi_softc *, struct mbuf *,
204 struct ieee80211_node *,
205 const struct ieee80211_bpf_params *);
206 static int wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
207 const struct ieee80211_bpf_params *);
208 static int wpi_transmit(struct ieee80211com *, struct mbuf *);
209 static void wpi_watchdog_rfkill(void *);
210 static void wpi_scan_timeout(void *);
211 static void wpi_tx_timeout(void *);
212 static void wpi_parent(struct ieee80211com *);
213 static int wpi_cmd(struct wpi_softc *, int, const void *, size_t, int);
214 static int wpi_mrr_setup(struct wpi_softc *);
215 static int wpi_add_node(struct wpi_softc *, struct ieee80211_node *);
216 static int wpi_add_broadcast_node(struct wpi_softc *, int);
217 static int wpi_add_ibss_node(struct wpi_softc *, struct ieee80211_node *);
218 static void wpi_del_node(struct wpi_softc *, struct ieee80211_node *);
219 static int wpi_updateedca(struct ieee80211com *);
220 static void wpi_set_promisc(struct wpi_softc *);
221 static void wpi_update_promisc(struct ieee80211com *);
222 static void wpi_update_mcast(struct ieee80211com *);
223 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
224 static int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *);
225 static void wpi_power_calibration(struct wpi_softc *);
226 static int wpi_set_txpower(struct wpi_softc *, int);
227 static int wpi_get_power_index(struct wpi_softc *,
228 struct wpi_power_group *, uint8_t, int, int);
229 static int wpi_set_pslevel(struct wpi_softc *, uint8_t, int, int);
230 static int wpi_send_btcoex(struct wpi_softc *);
231 static int wpi_send_rxon(struct wpi_softc *, int, int);
232 static int wpi_config(struct wpi_softc *);
233 static uint16_t wpi_get_active_dwell_time(struct wpi_softc *,
234 struct ieee80211_channel *, uint8_t);
235 static uint16_t wpi_limit_dwell(struct wpi_softc *, uint16_t);
236 static uint16_t wpi_get_passive_dwell_time(struct wpi_softc *,
237 struct ieee80211_channel *);
238 static uint32_t wpi_get_scan_pause_time(uint32_t, uint16_t);
239 static int wpi_scan(struct wpi_softc *, struct ieee80211_channel *);
240 static int wpi_auth(struct wpi_softc *, struct ieee80211vap *);
241 static int wpi_config_beacon(struct wpi_vap *);
242 static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
243 static void wpi_update_beacon(struct ieee80211vap *, int);
244 static void wpi_newassoc(struct ieee80211_node *, int);
245 static int wpi_run(struct wpi_softc *, struct ieee80211vap *);
246 static int wpi_load_key(struct ieee80211_node *,
247 const struct ieee80211_key *);
248 static void wpi_load_key_cb(void *, struct ieee80211_node *);
249 static int wpi_set_global_keys(struct ieee80211_node *);
250 static int wpi_del_key(struct ieee80211_node *,
251 const struct ieee80211_key *);
252 static void wpi_del_key_cb(void *, struct ieee80211_node *);
253 static int wpi_process_key(struct ieee80211vap *,
254 const struct ieee80211_key *, int);
255 static int wpi_key_set(struct ieee80211vap *,
256 const struct ieee80211_key *);
257 static int wpi_key_delete(struct ieee80211vap *,
258 const struct ieee80211_key *);
259 static int wpi_post_alive(struct wpi_softc *);
260 static int wpi_load_bootcode(struct wpi_softc *, const uint8_t *, int);
261 static int wpi_load_firmware(struct wpi_softc *);
262 static int wpi_read_firmware(struct wpi_softc *);
263 static void wpi_unload_firmware(struct wpi_softc *);
264 static int wpi_clock_wait(struct wpi_softc *);
265 static int wpi_apm_init(struct wpi_softc *);
266 static void wpi_apm_stop_master(struct wpi_softc *);
267 static void wpi_apm_stop(struct wpi_softc *);
268 static void wpi_nic_config(struct wpi_softc *);
269 static int wpi_hw_init(struct wpi_softc *);
270 static void wpi_hw_stop(struct wpi_softc *);
271 static void wpi_radio_on(void *, int);
272 static void wpi_radio_off(void *, int);
273 static int wpi_init(struct wpi_softc *);
274 static void wpi_stop_locked(struct wpi_softc *);
275 static void wpi_stop(struct wpi_softc *);
276 static void wpi_scan_start(struct ieee80211com *);
277 static void wpi_scan_end(struct ieee80211com *);
278 static void wpi_set_channel(struct ieee80211com *);
279 static void wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
280 static void wpi_scan_mindwell(struct ieee80211_scan_state *);
281 static void wpi_hw_reset(void *, int);
283 static device_method_t wpi_methods[] = {
284 /* Device interface */
285 DEVMETHOD(device_probe, wpi_probe),
286 DEVMETHOD(device_attach, wpi_attach),
287 DEVMETHOD(device_detach, wpi_detach),
288 DEVMETHOD(device_shutdown, wpi_shutdown),
289 DEVMETHOD(device_suspend, wpi_suspend),
290 DEVMETHOD(device_resume, wpi_resume),
295 static driver_t wpi_driver = {
298 sizeof (struct wpi_softc)
300 static devclass_t wpi_devclass;
302 DRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, NULL, NULL);
304 MODULE_VERSION(wpi, 1);
306 MODULE_DEPEND(wpi, pci, 1, 1, 1);
307 MODULE_DEPEND(wpi, wlan, 1, 1, 1);
308 MODULE_DEPEND(wpi, firmware, 1, 1, 1);
311 wpi_probe(device_t dev)
313 const struct wpi_ident *ident;
315 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
316 if (pci_get_vendor(dev) == ident->vendor &&
317 pci_get_device(dev) == ident->device) {
318 device_set_desc(dev, ident->name);
319 return (BUS_PROBE_DEFAULT);
326 wpi_attach(device_t dev)
328 struct wpi_softc *sc = (struct wpi_softc *)device_get_softc(dev);
329 struct ieee80211com *ic;
333 const struct wpi_ident *ident;
339 error = resource_int_value(device_get_name(sc->sc_dev),
340 device_get_unit(sc->sc_dev), "debug", &(sc->sc_debug));
347 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
350 * Get the offset of the PCI Express Capability Structure in PCI
351 * Configuration Space.
353 error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off);
355 device_printf(dev, "PCIe capability structure not found!\n");
360 * Some card's only support 802.11b/g not a, check to see if
361 * this is one such card. A 0x0 in the subdevice table indicates
362 * the entire subdevice range is to be ignored.
365 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
366 if (ident->subdevice &&
367 pci_get_subdevice(dev) == ident->subdevice) {
374 /* Clear device-specific "PCI retry timeout" register (41h). */
375 pci_write_config(dev, 0x41, 0, 1);
377 /* Enable bus-mastering. */
378 pci_enable_busmaster(dev);
381 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
383 if (sc->mem == NULL) {
384 device_printf(dev, "can't map mem space\n");
387 sc->sc_st = rman_get_bustag(sc->mem);
388 sc->sc_sh = rman_get_bushandle(sc->mem);
392 if (pci_alloc_msi(dev, &i) == 0)
394 /* Install interrupt handler. */
395 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
396 (rid != 0 ? 0 : RF_SHAREABLE));
397 if (sc->irq == NULL) {
398 device_printf(dev, "can't map interrupt\n");
404 WPI_TX_LOCK_INIT(sc);
405 WPI_RXON_LOCK_INIT(sc);
406 WPI_NT_LOCK_INIT(sc);
407 WPI_TXQ_LOCK_INIT(sc);
408 WPI_TXQ_STATE_LOCK_INIT(sc);
410 /* Allocate DMA memory for firmware transfers. */
411 if ((error = wpi_alloc_fwmem(sc)) != 0) {
413 "could not allocate memory for firmware, error %d\n",
418 /* Allocate shared page. */
419 if ((error = wpi_alloc_shared(sc)) != 0) {
420 device_printf(dev, "could not allocate shared page\n");
424 /* Allocate TX rings - 4 for QoS purposes, 1 for commands. */
425 for (i = 0; i < WPI_NTXQUEUES; i++) {
426 if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) {
428 "could not allocate TX ring %d, error %d\n", i,
434 /* Allocate RX ring. */
435 if ((error = wpi_alloc_rx_ring(sc)) != 0) {
436 device_printf(dev, "could not allocate RX ring, error %d\n",
441 /* Clear pending interrupts. */
442 WPI_WRITE(sc, WPI_INT, 0xffffffff);
446 ic->ic_name = device_get_nameunit(dev);
447 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
448 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
450 /* Set device capabilities. */
452 IEEE80211_C_STA /* station mode supported */
453 | IEEE80211_C_IBSS /* IBSS mode supported */
454 | IEEE80211_C_HOSTAP /* Host access point mode */
455 | IEEE80211_C_MONITOR /* monitor mode supported */
456 | IEEE80211_C_AHDEMO /* adhoc demo mode */
457 | IEEE80211_C_BGSCAN /* capable of bg scanning */
458 | IEEE80211_C_TXPMGT /* tx power management */
459 | IEEE80211_C_SHSLOT /* short slot time supported */
460 | IEEE80211_C_WPA /* 802.11i */
461 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
462 | IEEE80211_C_WME /* 802.11e */
463 | IEEE80211_C_PMGT /* Station-side power mgmt */
467 IEEE80211_CRYPTO_AES_CCM;
470 * Read in the eeprom and also setup the channels for
471 * net80211. We don't set the rates as net80211 does this for us
473 if ((error = wpi_read_eeprom(sc, ic->ic_macaddr)) != 0) {
474 device_printf(dev, "could not read EEPROM, error %d\n",
481 device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n",
483 device_printf(sc->sc_dev, "Hardware Type: %c\n",
484 sc->type > 1 ? 'B': '?');
485 device_printf(sc->sc_dev, "Hardware Revision: %c\n",
486 ((sc->rev & 0xf0) == 0xd0) ? 'D': '?');
487 device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
488 supportsa ? "does" : "does not");
490 /* XXX hw_config uses the PCIDEV for the Hardware rev. Must
491 check what sc->rev really represents - benjsc 20070615 */
495 ieee80211_ifattach(ic);
496 ic->ic_vap_create = wpi_vap_create;
497 ic->ic_vap_delete = wpi_vap_delete;
498 ic->ic_parent = wpi_parent;
499 ic->ic_raw_xmit = wpi_raw_xmit;
500 ic->ic_transmit = wpi_transmit;
501 ic->ic_node_alloc = wpi_node_alloc;
502 sc->sc_node_free = ic->ic_node_free;
503 ic->ic_node_free = wpi_node_free;
504 ic->ic_wme.wme_update = wpi_updateedca;
505 ic->ic_update_promisc = wpi_update_promisc;
506 ic->ic_update_mcast = wpi_update_mcast;
507 ic->ic_newassoc = wpi_newassoc;
508 ic->ic_scan_start = wpi_scan_start;
509 ic->ic_scan_end = wpi_scan_end;
510 ic->ic_set_channel = wpi_set_channel;
511 ic->ic_scan_curchan = wpi_scan_curchan;
512 ic->ic_scan_mindwell = wpi_scan_mindwell;
513 ic->ic_setregdomain = wpi_setregdomain;
515 sc->sc_update_rx_ring = wpi_update_rx_ring;
516 sc->sc_update_tx_ring = wpi_update_tx_ring;
518 wpi_radiotap_attach(sc);
520 callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0);
521 callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0);
522 callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0);
523 callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0);
524 TASK_INIT(&sc->sc_reinittask, 0, wpi_hw_reset, sc);
525 TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc);
526 TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc);
528 sc->sc_tq = taskqueue_create("wpi_taskq", M_WAITOK,
529 taskqueue_thread_enqueue, &sc->sc_tq);
530 error = taskqueue_start_threads(&sc->sc_tq, 1, 0, "wpi_taskq");
532 device_printf(dev, "can't start threads, error %d\n", error);
536 wpi_sysctlattach(sc);
539 * Hook our interrupt after all initialization is complete.
541 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
542 NULL, wpi_intr, sc, &sc->sc_ih);
544 device_printf(dev, "can't establish interrupt, error %d\n",
550 ieee80211_announce(ic);
553 if (sc->sc_debug & WPI_DEBUG_HW)
554 ieee80211_announce_channels(ic);
557 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
560 fail: wpi_detach(dev);
561 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
566 * Attach the interface to 802.11 radiotap.
569 wpi_radiotap_attach(struct wpi_softc *sc)
571 struct wpi_rx_radiotap_header *rxtap = &sc->sc_rxtap;
572 struct wpi_tx_radiotap_header *txtap = &sc->sc_txtap;
574 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
575 ieee80211_radiotap_attach(&sc->sc_ic,
576 &txtap->wt_ihdr, sizeof(*txtap), WPI_TX_RADIOTAP_PRESENT,
577 &rxtap->wr_ihdr, sizeof(*rxtap), WPI_RX_RADIOTAP_PRESENT);
578 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
582 wpi_sysctlattach(struct wpi_softc *sc)
585 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
586 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
588 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
589 "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug,
590 "control debugging printfs");
595 wpi_init_beacon(struct wpi_vap *wvp)
597 struct wpi_buf *bcn = &wvp->wv_bcbuf;
598 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
600 cmd->id = WPI_ID_BROADCAST;
601 cmd->ofdm_mask = 0xff;
602 cmd->cck_mask = 0x0f;
603 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE);
606 * XXX WPI_TX_AUTO_SEQ seems to be ignored - workaround this issue
607 * XXX by using WPI_TX_NEED_ACK instead (with some side effects).
609 cmd->flags = htole32(WPI_TX_NEED_ACK | WPI_TX_INSERT_TSTAMP);
611 bcn->code = WPI_CMD_SET_BEACON;
612 bcn->ac = WPI_CMD_QUEUE_NUM;
613 bcn->size = sizeof(struct wpi_cmd_beacon);
616 static struct ieee80211vap *
617 wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
618 enum ieee80211_opmode opmode, int flags,
619 const uint8_t bssid[IEEE80211_ADDR_LEN],
620 const uint8_t mac[IEEE80211_ADDR_LEN])
623 struct ieee80211vap *vap;
625 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
628 wvp = malloc(sizeof(struct wpi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
630 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
632 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
633 WPI_VAP_LOCK_INIT(wvp);
634 wpi_init_beacon(wvp);
637 /* Override with driver methods. */
638 vap->iv_key_set = wpi_key_set;
639 vap->iv_key_delete = wpi_key_delete;
640 wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
641 vap->iv_recv_mgmt = wpi_recv_mgmt;
642 wvp->wv_newstate = vap->iv_newstate;
643 vap->iv_newstate = wpi_newstate;
644 vap->iv_update_beacon = wpi_update_beacon;
645 vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1;
647 ieee80211_ratectl_init(vap);
648 /* Complete setup. */
649 ieee80211_vap_attach(vap, ieee80211_media_change,
650 ieee80211_media_status, mac);
651 ic->ic_opmode = opmode;
656 wpi_vap_delete(struct ieee80211vap *vap)
658 struct wpi_vap *wvp = WPI_VAP(vap);
659 struct wpi_buf *bcn = &wvp->wv_bcbuf;
660 enum ieee80211_opmode opmode = vap->iv_opmode;
662 ieee80211_ratectl_deinit(vap);
663 ieee80211_vap_detach(vap);
665 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
669 WPI_VAP_LOCK_DESTROY(wvp);
672 free(wvp, M_80211_VAP);
676 wpi_detach(device_t dev)
678 struct wpi_softc *sc = device_get_softc(dev);
679 struct ieee80211com *ic = &sc->sc_ic;
682 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
684 if (ic->ic_vap_create == wpi_vap_create) {
685 ieee80211_draintask(ic, &sc->sc_radioon_task);
689 if (sc->sc_tq != NULL) {
690 taskqueue_drain_all(sc->sc_tq);
691 taskqueue_free(sc->sc_tq);
694 callout_drain(&sc->watchdog_rfkill);
695 callout_drain(&sc->tx_timeout);
696 callout_drain(&sc->scan_timeout);
697 callout_drain(&sc->calib_to);
698 ieee80211_ifdetach(ic);
701 /* Uninstall interrupt handler. */
702 if (sc->irq != NULL) {
703 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
704 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
706 pci_release_msi(dev);
709 if (sc->txq[0].data_dmat) {
710 /* Free DMA resources. */
711 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
712 wpi_free_tx_ring(sc, &sc->txq[qid]);
714 wpi_free_rx_ring(sc);
722 bus_release_resource(dev, SYS_RES_MEMORY,
723 rman_get_rid(sc->mem), sc->mem);
725 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
726 WPI_TXQ_STATE_LOCK_DESTROY(sc);
727 WPI_TXQ_LOCK_DESTROY(sc);
728 WPI_NT_LOCK_DESTROY(sc);
729 WPI_RXON_LOCK_DESTROY(sc);
730 WPI_TX_LOCK_DESTROY(sc);
731 WPI_LOCK_DESTROY(sc);
736 wpi_shutdown(device_t dev)
738 struct wpi_softc *sc = device_get_softc(dev);
745 wpi_suspend(device_t dev)
747 struct wpi_softc *sc = device_get_softc(dev);
748 struct ieee80211com *ic = &sc->sc_ic;
750 ieee80211_suspend_all(ic);
755 wpi_resume(device_t dev)
757 struct wpi_softc *sc = device_get_softc(dev);
758 struct ieee80211com *ic = &sc->sc_ic;
760 /* Clear device-specific "PCI retry timeout" register (41h). */
761 pci_write_config(dev, 0x41, 0, 1);
763 ieee80211_resume_all(ic);
768 * Grab exclusive access to NIC memory.
771 wpi_nic_lock(struct wpi_softc *sc)
775 /* Request exclusive access to NIC. */
776 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
778 /* Spin until we actually get the lock. */
779 for (ntries = 0; ntries < 1000; ntries++) {
780 if ((WPI_READ(sc, WPI_GP_CNTRL) &
781 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
782 WPI_GP_CNTRL_MAC_ACCESS_ENA)
787 device_printf(sc->sc_dev, "could not lock memory\n");
793 * Release lock on NIC memory.
796 wpi_nic_unlock(struct wpi_softc *sc)
798 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
801 static __inline uint32_t
802 wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
804 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
805 WPI_BARRIER_READ_WRITE(sc);
806 return WPI_READ(sc, WPI_PRPH_RDATA);
810 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
812 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
813 WPI_BARRIER_WRITE(sc);
814 WPI_WRITE(sc, WPI_PRPH_WDATA, data);
818 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
820 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
824 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
826 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
830 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
831 const uint32_t *data, int count)
833 for (; count > 0; count--, data++, addr += 4)
834 wpi_prph_write(sc, addr, *data);
837 static __inline uint32_t
838 wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
840 WPI_WRITE(sc, WPI_MEM_RADDR, addr);
841 WPI_BARRIER_READ_WRITE(sc);
842 return WPI_READ(sc, WPI_MEM_RDATA);
846 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
849 for (; count > 0; count--, addr += 4)
850 *data++ = wpi_mem_read(sc, addr);
854 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
860 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
862 if ((error = wpi_nic_lock(sc)) != 0)
865 for (; count > 0; count -= 2, addr++) {
866 WPI_WRITE(sc, WPI_EEPROM, addr << 2);
867 for (ntries = 0; ntries < 10; ntries++) {
868 val = WPI_READ(sc, WPI_EEPROM);
869 if (val & WPI_EEPROM_READ_VALID)
874 device_printf(sc->sc_dev,
875 "timeout reading ROM at 0x%x\n", addr);
885 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
891 wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
895 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
896 *(bus_addr_t *)arg = segs[0].ds_addr;
900 * Allocates a contiguous block of dma memory of the requested size and
904 wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
905 void **kvap, bus_size_t size, bus_size_t alignment)
912 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
913 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
914 1, size, BUS_DMA_NOWAIT, NULL, NULL, &dma->tag);
918 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
919 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
923 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
924 wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
928 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
935 fail: wpi_dma_contig_free(dma);
940 wpi_dma_contig_free(struct wpi_dma_info *dma)
942 if (dma->vaddr != NULL) {
943 bus_dmamap_sync(dma->tag, dma->map,
944 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
945 bus_dmamap_unload(dma->tag, dma->map);
946 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
949 if (dma->tag != NULL) {
950 bus_dma_tag_destroy(dma->tag);
956 * Allocate a shared page between host and NIC.
959 wpi_alloc_shared(struct wpi_softc *sc)
961 /* Shared buffer must be aligned on a 4KB boundary. */
962 return wpi_dma_contig_alloc(sc, &sc->shared_dma,
963 (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
967 wpi_free_shared(struct wpi_softc *sc)
969 wpi_dma_contig_free(&sc->shared_dma);
973 * Allocate DMA-safe memory for firmware transfer.
976 wpi_alloc_fwmem(struct wpi_softc *sc)
978 /* Must be aligned on a 16-byte boundary. */
979 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
980 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
984 wpi_free_fwmem(struct wpi_softc *sc)
986 wpi_dma_contig_free(&sc->fw_dma);
990 wpi_alloc_rx_ring(struct wpi_softc *sc)
992 struct wpi_rx_ring *ring = &sc->rxq;
999 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1001 /* Allocate RX descriptors (16KB aligned.) */
1002 size = WPI_RX_RING_COUNT * sizeof (uint32_t);
1003 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1004 (void **)&ring->desc, size, WPI_RING_DMA_ALIGN);
1006 device_printf(sc->sc_dev,
1007 "%s: could not allocate RX ring DMA memory, error %d\n",
1012 /* Create RX buffer DMA tag. */
1013 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1014 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1015 MJUMPAGESIZE, 1, MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL,
1018 device_printf(sc->sc_dev,
1019 "%s: could not create RX buf DMA tag, error %d\n",
1025 * Allocate and map RX buffers.
1027 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1028 struct wpi_rx_data *data = &ring->data[i];
1031 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1033 device_printf(sc->sc_dev,
1034 "%s: could not create RX buf DMA map, error %d\n",
1039 data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1040 if (data->m == NULL) {
1041 device_printf(sc->sc_dev,
1042 "%s: could not allocate RX mbuf\n", __func__);
1047 error = bus_dmamap_load(ring->data_dmat, data->map,
1048 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1049 &paddr, BUS_DMA_NOWAIT);
1050 if (error != 0 && error != EFBIG) {
1051 device_printf(sc->sc_dev,
1052 "%s: can't map mbuf (error %d)\n", __func__,
1057 /* Set physical address of RX buffer. */
1058 ring->desc[i] = htole32(paddr);
1061 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1062 BUS_DMASYNC_PREWRITE);
1064 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1068 fail: wpi_free_rx_ring(sc);
1070 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1076 wpi_update_rx_ring(struct wpi_softc *sc)
1078 WPI_WRITE(sc, WPI_FH_RX_WPTR, sc->rxq.cur & ~7);
1082 wpi_update_rx_ring_ps(struct wpi_softc *sc)
1084 struct wpi_rx_ring *ring = &sc->rxq;
1086 if (ring->update != 0) {
1087 /* Wait for INT_WAKEUP event. */
1091 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1092 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1093 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n",
1097 wpi_update_rx_ring(sc);
1098 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1103 wpi_reset_rx_ring(struct wpi_softc *sc)
1105 struct wpi_rx_ring *ring = &sc->rxq;
1108 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1110 if (wpi_nic_lock(sc) == 0) {
1111 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
1112 for (ntries = 0; ntries < 1000; ntries++) {
1113 if (WPI_READ(sc, WPI_FH_RX_STATUS) &
1114 WPI_FH_RX_STATUS_IDLE)
1126 wpi_free_rx_ring(struct wpi_softc *sc)
1128 struct wpi_rx_ring *ring = &sc->rxq;
1131 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1133 wpi_dma_contig_free(&ring->desc_dma);
1135 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1136 struct wpi_rx_data *data = &ring->data[i];
1138 if (data->m != NULL) {
1139 bus_dmamap_sync(ring->data_dmat, data->map,
1140 BUS_DMASYNC_POSTREAD);
1141 bus_dmamap_unload(ring->data_dmat, data->map);
1145 if (data->map != NULL)
1146 bus_dmamap_destroy(ring->data_dmat, data->map);
1148 if (ring->data_dmat != NULL) {
1149 bus_dma_tag_destroy(ring->data_dmat);
1150 ring->data_dmat = NULL;
1155 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int qid)
1166 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1168 /* Allocate TX descriptors (16KB aligned.) */
1169 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
1170 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc,
1171 size, WPI_RING_DMA_ALIGN);
1173 device_printf(sc->sc_dev,
1174 "%s: could not allocate TX ring DMA memory, error %d\n",
1179 /* Update shared area with ring physical address. */
1180 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1181 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1182 BUS_DMASYNC_PREWRITE);
1185 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
1186 * to allocate commands space for other rings.
1187 * XXX Do we really need to allocate descriptors for other rings?
1189 if (qid > WPI_CMD_QUEUE_NUM) {
1190 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1194 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
1195 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1198 device_printf(sc->sc_dev,
1199 "%s: could not allocate TX cmd DMA memory, error %d\n",
1204 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1205 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1206 WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1209 device_printf(sc->sc_dev,
1210 "%s: could not create TX buf DMA tag, error %d\n",
1215 paddr = ring->cmd_dma.paddr;
1216 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1217 struct wpi_tx_data *data = &ring->data[i];
1219 data->cmd_paddr = paddr;
1220 paddr += sizeof (struct wpi_tx_cmd);
1222 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1224 device_printf(sc->sc_dev,
1225 "%s: could not create TX buf DMA map, error %d\n",
1231 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1235 fail: wpi_free_tx_ring(sc, ring);
1236 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1241 wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1243 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
1247 wpi_update_tx_ring_ps(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1250 if (ring->update != 0) {
1251 /* Wait for INT_WAKEUP event. */
1255 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1256 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1257 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n",
1258 __func__, ring->qid);
1261 wpi_update_tx_ring(sc, ring);
1262 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1267 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1271 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1273 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1274 struct wpi_tx_data *data = &ring->data[i];
1276 if (data->m != NULL) {
1277 bus_dmamap_sync(ring->data_dmat, data->map,
1278 BUS_DMASYNC_POSTWRITE);
1279 bus_dmamap_unload(ring->data_dmat, data->map);
1283 if (data->ni != NULL) {
1284 ieee80211_free_node(data->ni);
1288 /* Clear TX descriptors. */
1289 memset(ring->desc, 0, ring->desc_dma.size);
1290 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1291 BUS_DMASYNC_PREWRITE);
1298 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1302 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1304 wpi_dma_contig_free(&ring->desc_dma);
1305 wpi_dma_contig_free(&ring->cmd_dma);
1307 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1308 struct wpi_tx_data *data = &ring->data[i];
1310 if (data->m != NULL) {
1311 bus_dmamap_sync(ring->data_dmat, data->map,
1312 BUS_DMASYNC_POSTWRITE);
1313 bus_dmamap_unload(ring->data_dmat, data->map);
1316 if (data->map != NULL)
1317 bus_dmamap_destroy(ring->data_dmat, data->map);
1319 if (ring->data_dmat != NULL) {
1320 bus_dma_tag_destroy(ring->data_dmat);
1321 ring->data_dmat = NULL;
1326 * Extract various information from EEPROM.
1329 wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1331 #define WPI_CHK(res) do { \
1332 if ((error = res) != 0) \
1337 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1339 /* Adapter has to be powered on for EEPROM access to work. */
1340 if ((error = wpi_apm_init(sc)) != 0) {
1341 device_printf(sc->sc_dev,
1342 "%s: could not power ON adapter, error %d\n", __func__,
1347 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
1348 device_printf(sc->sc_dev, "bad EEPROM signature\n");
1352 /* Clear HW ownership of EEPROM. */
1353 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
1355 /* Read the hardware capabilities, revision and SKU type. */
1356 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap,
1358 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,
1360 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type,
1363 sc->rev = le16toh(sc->rev);
1364 DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap,
1367 /* Read the regulatory domain (4 ASCII characters.) */
1368 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain,
1369 sizeof(sc->domain)));
1371 /* Read MAC address. */
1372 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr,
1373 IEEE80211_ADDR_LEN));
1375 /* Read the list of authorized channels. */
1376 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
1377 WPI_CHK(wpi_read_eeprom_channels(sc, i));
1379 /* Read the list of TX power groups. */
1380 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
1381 WPI_CHK(wpi_read_eeprom_group(sc, i));
1383 fail: wpi_apm_stop(sc); /* Power OFF adapter. */
1385 DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
1393 * Translate EEPROM flags to net80211.
1396 wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel)
1401 if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0)
1402 nflags |= IEEE80211_CHAN_PASSIVE;
1403 if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0)
1404 nflags |= IEEE80211_CHAN_NOADHOC;
1405 if (channel->flags & WPI_EEPROM_CHAN_RADAR) {
1406 nflags |= IEEE80211_CHAN_DFS;
1407 /* XXX apparently IBSS may still be marked */
1408 nflags |= IEEE80211_CHAN_NOADHOC;
1411 /* XXX HOSTAP uses WPI_MODE_IBSS */
1412 if (nflags & IEEE80211_CHAN_NOADHOC)
1413 nflags |= IEEE80211_CHAN_NOHOSTAP;
1419 wpi_read_eeprom_band(struct wpi_softc *sc, int n)
1421 struct ieee80211com *ic = &sc->sc_ic;
1422 struct wpi_eeprom_chan *channels = sc->eeprom_channels[n];
1423 const struct wpi_chan_band *band = &wpi_bands[n];
1424 struct ieee80211_channel *c;
1428 for (i = 0; i < band->nchan; i++) {
1429 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
1430 DPRINTF(sc, WPI_DEBUG_EEPROM,
1431 "Channel Not Valid: %d, band %d\n",
1436 chan = band->chan[i];
1437 nflags = wpi_eeprom_channel_flags(&channels[i]);
1439 c = &ic->ic_channels[ic->ic_nchans++];
1441 c->ic_maxregpower = channels[i].maxpwr;
1442 c->ic_maxpower = 2*c->ic_maxregpower;
1444 if (n == 0) { /* 2GHz band */
1445 c->ic_freq = ieee80211_ieee2mhz(chan,
1448 /* G =>'s B is supported */
1449 c->ic_flags = IEEE80211_CHAN_B | nflags;
1450 c = &ic->ic_channels[ic->ic_nchans++];
1452 c->ic_flags = IEEE80211_CHAN_G | nflags;
1453 } else { /* 5GHz band */
1454 c->ic_freq = ieee80211_ieee2mhz(chan,
1457 c->ic_flags = IEEE80211_CHAN_A | nflags;
1460 /* Save maximum allowed TX power for this channel. */
1461 sc->maxpwr[chan] = channels[i].maxpwr;
1463 DPRINTF(sc, WPI_DEBUG_EEPROM,
1464 "adding chan %d (%dMHz) flags=0x%x maxpwr=%d passive=%d,"
1465 " offset %d\n", chan, c->ic_freq,
1466 channels[i].flags, sc->maxpwr[chan],
1467 IEEE80211_IS_CHAN_PASSIVE(c), ic->ic_nchans);
1472 * Read the eeprom to find out what channels are valid for the given
1473 * band and update net80211 with what we find.
1476 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
1478 struct ieee80211com *ic = &sc->sc_ic;
1479 const struct wpi_chan_band *band = &wpi_bands[n];
1482 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1484 error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n],
1485 band->nchan * sizeof (struct wpi_eeprom_chan));
1487 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1491 wpi_read_eeprom_band(sc, n);
1493 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1495 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1500 static struct wpi_eeprom_chan *
1501 wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c)
1505 for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++)
1506 for (i = 0; i < wpi_bands[j].nchan; i++)
1507 if (wpi_bands[j].chan[i] == c->ic_ieee)
1508 return &sc->eeprom_channels[j][i];
1514 * Enforce flags read from EEPROM.
1517 wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
1518 int nchan, struct ieee80211_channel chans[])
1520 struct wpi_softc *sc = ic->ic_softc;
1523 for (i = 0; i < nchan; i++) {
1524 struct ieee80211_channel *c = &chans[i];
1525 struct wpi_eeprom_chan *channel;
1527 channel = wpi_find_eeprom_channel(sc, c);
1528 if (channel == NULL) {
1529 ic_printf(ic, "%s: invalid channel %u freq %u/0x%x\n",
1530 __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
1533 c->ic_flags |= wpi_eeprom_channel_flags(channel);
1540 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
1542 struct wpi_power_group *group = &sc->groups[n];
1543 struct wpi_eeprom_group rgroup;
1546 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1548 if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32,
1549 &rgroup, sizeof rgroup)) != 0) {
1550 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1554 /* Save TX power group information. */
1555 group->chan = rgroup.chan;
1556 group->maxpwr = rgroup.maxpwr;
1557 /* Retrieve temperature at which the samples were taken. */
1558 group->temp = (int16_t)le16toh(rgroup.temp);
1560 DPRINTF(sc, WPI_DEBUG_EEPROM,
1561 "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan,
1562 group->maxpwr, group->temp);
1564 for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
1565 group->samples[i].index = rgroup.samples[i].index;
1566 group->samples[i].power = rgroup.samples[i].power;
1568 DPRINTF(sc, WPI_DEBUG_EEPROM,
1569 "\tsample %d: index=%d power=%d\n", i,
1570 group->samples[i].index, group->samples[i].power);
1573 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1579 wpi_add_node_entry_adhoc(struct wpi_softc *sc)
1581 int newid = WPI_ID_IBSS_MIN;
1583 for (; newid <= WPI_ID_IBSS_MAX; newid++) {
1584 if ((sc->nodesmsk & (1 << newid)) == 0) {
1585 sc->nodesmsk |= 1 << newid;
1590 return WPI_ID_UNDEFINED;
1594 wpi_add_node_entry_sta(struct wpi_softc *sc)
1596 sc->nodesmsk |= 1 << WPI_ID_BSS;
1602 wpi_check_node_entry(struct wpi_softc *sc, uint8_t id)
1604 if (id == WPI_ID_UNDEFINED)
1607 return (sc->nodesmsk >> id) & 1;
1610 static __inline void
1611 wpi_clear_node_table(struct wpi_softc *sc)
1616 static __inline void
1617 wpi_del_node_entry(struct wpi_softc *sc, uint8_t id)
1619 sc->nodesmsk &= ~(1 << id);
1622 static struct ieee80211_node *
1623 wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1625 struct wpi_node *wn;
1627 wn = malloc(sizeof (struct wpi_node), M_80211_NODE,
1633 wn->id = WPI_ID_UNDEFINED;
1639 wpi_node_free(struct ieee80211_node *ni)
1641 struct wpi_softc *sc = ni->ni_ic->ic_softc;
1642 struct wpi_node *wn = WPI_NODE(ni);
1644 if (wn->id != WPI_ID_UNDEFINED) {
1646 if (wpi_check_node_entry(sc, wn->id)) {
1647 wpi_del_node_entry(sc, wn->id);
1648 wpi_del_node(sc, ni);
1653 sc->sc_node_free(ni);
1657 wpi_check_bss_filter(struct wpi_softc *sc)
1659 return (sc->rxon.filter & htole32(WPI_FILTER_BSS)) != 0;
1663 wpi_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
1664 const struct ieee80211_rx_stats *rxs,
1667 struct ieee80211vap *vap = ni->ni_vap;
1668 struct wpi_softc *sc = vap->iv_ic->ic_softc;
1669 struct wpi_vap *wvp = WPI_VAP(vap);
1670 uint64_t ni_tstamp, rx_tstamp;
1672 wvp->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1674 if (vap->iv_opmode == IEEE80211_M_IBSS &&
1675 vap->iv_state == IEEE80211_S_RUN &&
1676 (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
1677 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) {
1678 ni_tstamp = le64toh(ni->ni_tstamp.tsf);
1679 rx_tstamp = le64toh(sc->rx_tstamp);
1681 if (ni_tstamp >= rx_tstamp) {
1682 DPRINTF(sc, WPI_DEBUG_STATE,
1683 "ibss merge, tsf %ju tstamp %ju\n",
1684 (uintmax_t)rx_tstamp, (uintmax_t)ni_tstamp);
1685 (void) ieee80211_ibss_merge(ni);
1691 wpi_restore_node(void *arg, struct ieee80211_node *ni)
1693 struct wpi_softc *sc = arg;
1694 struct wpi_node *wn = WPI_NODE(ni);
1698 if (wn->id != WPI_ID_UNDEFINED) {
1699 wn->id = WPI_ID_UNDEFINED;
1700 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
1701 device_printf(sc->sc_dev,
1702 "%s: could not add IBSS node, error %d\n",
1710 wpi_restore_node_table(struct wpi_softc *sc, struct wpi_vap *wvp)
1712 struct ieee80211com *ic = &sc->sc_ic;
1714 /* Set group keys once. */
1719 ieee80211_iterate_nodes(&ic->ic_sta, wpi_restore_node, sc);
1720 ieee80211_crypto_reload_keys(ic);
1724 * Called by net80211 when ever there is a change to 80211 state machine
1727 wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1729 struct wpi_vap *wvp = WPI_VAP(vap);
1730 struct ieee80211com *ic = vap->iv_ic;
1731 struct wpi_softc *sc = ic->ic_softc;
1734 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1737 if (nstate > IEEE80211_S_INIT && sc->sc_running == 0) {
1738 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1745 DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1746 ieee80211_state_name[vap->iv_state],
1747 ieee80211_state_name[nstate]);
1749 if (vap->iv_state == IEEE80211_S_RUN && nstate < IEEE80211_S_RUN) {
1750 if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) {
1751 device_printf(sc->sc_dev,
1752 "%s: could not set power saving level\n",
1757 wpi_set_led(sc, WPI_LED_LINK, 1, 0);
1761 case IEEE80211_S_SCAN:
1763 if (wpi_check_bss_filter(sc) != 0) {
1764 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1765 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1766 device_printf(sc->sc_dev,
1767 "%s: could not send RXON\n", __func__);
1770 WPI_RXON_UNLOCK(sc);
1773 case IEEE80211_S_ASSOC:
1774 if (vap->iv_state != IEEE80211_S_RUN)
1777 case IEEE80211_S_AUTH:
1779 * NB: do not optimize AUTH -> AUTH state transmission -
1780 * this will break powersave with non-QoS AP!
1784 * The node must be registered in the firmware before auth.
1785 * Also the associd must be cleared on RUN -> ASSOC
1788 if ((error = wpi_auth(sc, vap)) != 0) {
1789 device_printf(sc->sc_dev,
1790 "%s: could not move to AUTH state, error %d\n",
1795 case IEEE80211_S_RUN:
1797 * RUN -> RUN transition:
1798 * STA mode: Just restart the timers.
1799 * IBSS mode: Process IBSS merge.
1801 if (vap->iv_state == IEEE80211_S_RUN) {
1802 if (vap->iv_opmode != IEEE80211_M_IBSS) {
1804 wpi_calib_timeout(sc);
1805 WPI_RXON_UNLOCK(sc);
1809 * Drop the BSS_FILTER bit
1810 * (there is no another way to change bssid).
1813 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1814 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1815 device_printf(sc->sc_dev,
1816 "%s: could not send RXON\n",
1819 WPI_RXON_UNLOCK(sc);
1821 /* Restore all what was lost. */
1822 wpi_restore_node_table(sc, wvp);
1824 /* XXX set conditionally? */
1830 * !RUN -> RUN requires setting the association id
1831 * which is done with a firmware cmd. We also defer
1832 * starting the timers until that work is done.
1834 if ((error = wpi_run(sc, vap)) != 0) {
1835 device_printf(sc->sc_dev,
1836 "%s: could not move to RUN state\n", __func__);
1844 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1848 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1850 return wvp->wv_newstate(vap, nstate, arg);
1854 wpi_calib_timeout(void *arg)
1856 struct wpi_softc *sc = arg;
1858 if (wpi_check_bss_filter(sc) == 0)
1861 wpi_power_calibration(sc);
1863 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
1866 static __inline uint8_t
1867 rate2plcp(const uint8_t rate)
1870 case 12: return 0xd;
1871 case 18: return 0xf;
1872 case 24: return 0x5;
1873 case 36: return 0x7;
1874 case 48: return 0x9;
1875 case 72: return 0xb;
1876 case 96: return 0x1;
1877 case 108: return 0x3;
1881 case 22: return 110;
1886 static __inline uint8_t
1887 plcp2rate(const uint8_t plcp)
1890 case 0xd: return 12;
1891 case 0xf: return 18;
1892 case 0x5: return 24;
1893 case 0x7: return 36;
1894 case 0x9: return 48;
1895 case 0xb: return 72;
1896 case 0x1: return 96;
1897 case 0x3: return 108;
1901 case 110: return 22;
1906 /* Quickly determine if a given rate is CCK or OFDM. */
1907 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1910 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1911 struct wpi_rx_data *data)
1913 struct ieee80211com *ic = &sc->sc_ic;
1914 struct wpi_rx_ring *ring = &sc->rxq;
1915 struct wpi_rx_stat *stat;
1916 struct wpi_rx_head *head;
1917 struct wpi_rx_tail *tail;
1918 struct ieee80211_frame *wh;
1919 struct ieee80211_node *ni;
1920 struct mbuf *m, *m1;
1926 stat = (struct wpi_rx_stat *)(desc + 1);
1928 if (stat->len > WPI_STAT_MAXLEN) {
1929 device_printf(sc->sc_dev, "invalid RX statistic header\n");
1933 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
1934 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1935 len = le16toh(head->len);
1936 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len);
1937 flags = le32toh(tail->flags);
1939 DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d"
1940 " rate %x chan %d tstamp %ju\n", __func__, ring->cur,
1941 le32toh(desc->len), len, (int8_t)stat->rssi,
1942 head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp));
1944 /* Discard frames with a bad FCS early. */
1945 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1946 DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n",
1950 /* Discard frames that are too short. */
1951 if (len < sizeof (struct ieee80211_frame_ack)) {
1952 DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n",
1957 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1959 DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n",
1963 bus_dmamap_unload(ring->data_dmat, data->map);
1965 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *),
1966 MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1967 if (error != 0 && error != EFBIG) {
1968 device_printf(sc->sc_dev,
1969 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1972 /* Try to reload the old mbuf. */
1973 error = bus_dmamap_load(ring->data_dmat, data->map,
1974 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1975 &paddr, BUS_DMA_NOWAIT);
1976 if (error != 0 && error != EFBIG) {
1977 panic("%s: could not load old RX mbuf", __func__);
1979 /* Physical address may have changed. */
1980 ring->desc[ring->cur] = htole32(paddr);
1981 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map,
1982 BUS_DMASYNC_PREWRITE);
1988 /* Update RX descriptor. */
1989 ring->desc[ring->cur] = htole32(paddr);
1990 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1991 BUS_DMASYNC_PREWRITE);
1993 /* Finalize mbuf. */
1994 m->m_data = (caddr_t)(head + 1);
1995 m->m_pkthdr.len = m->m_len = len;
1997 /* Grab a reference to the source node. */
1998 wh = mtod(m, struct ieee80211_frame *);
2000 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
2001 (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) {
2002 /* Check whether decryption was successful or not. */
2003 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
2004 DPRINTF(sc, WPI_DEBUG_RECV,
2005 "CCMP decryption failed 0x%x\n", flags);
2008 m->m_flags |= M_WEP;
2011 if (len >= sizeof(struct ieee80211_frame_min))
2012 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
2016 sc->rx_tstamp = tail->tstamp;
2018 if (ieee80211_radiotap_active(ic)) {
2019 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
2022 if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE))
2023 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2024 tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET);
2025 tap->wr_dbm_antnoise = WPI_RSSI_OFFSET;
2026 tap->wr_tsft = tail->tstamp;
2027 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
2028 tap->wr_rate = plcp2rate(head->plcp);
2033 /* Send the frame to the 802.11 layer. */
2035 (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET);
2036 /* Node is no longer needed. */
2037 ieee80211_free_node(ni);
2039 (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET);
2047 fail1: counter_u64_add(ic->ic_ierrors, 1);
2051 wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc,
2052 struct wpi_rx_data *data)
2058 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2060 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
2061 struct wpi_tx_data *data = &ring->data[desc->idx];
2062 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
2064 struct ieee80211_node *ni;
2065 struct ieee80211vap *vap;
2066 struct ieee80211com *ic;
2067 uint32_t status = le32toh(stat->status);
2068 int ackfailcnt = stat->ackfailcnt / WPI_NTRIES_DEFAULT;
2070 KASSERT(data->ni != NULL, ("no node"));
2071 KASSERT(data->m != NULL, ("no mbuf"));
2073 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2075 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: "
2076 "qid %d idx %d retries %d btkillcnt %d rate %x duration %d "
2077 "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt,
2078 stat->btkillcnt, stat->rate, le32toh(stat->duration), status);
2080 /* Unmap and free mbuf. */
2081 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
2082 bus_dmamap_unload(ring->data_dmat, data->map);
2083 m = data->m, data->m = NULL;
2084 ni = data->ni, data->ni = NULL;
2089 * Update rate control statistics for the node.
2091 if (status & WPI_TX_STATUS_FAIL) {
2092 ieee80211_ratectl_tx_complete(vap, ni,
2093 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL);
2095 ieee80211_ratectl_tx_complete(vap, ni,
2096 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL);
2098 ieee80211_tx_complete(ni, m, (status & WPI_TX_STATUS_FAIL) != 0);
2100 WPI_TXQ_STATE_LOCK(sc);
2101 if (--ring->queued > 0)
2102 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2104 callout_stop(&sc->tx_timeout);
2105 WPI_TXQ_STATE_UNLOCK(sc);
2107 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2111 * Process a "command done" firmware notification. This is where we wakeup
2112 * processes waiting for a synchronous command completion.
2115 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2117 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
2118 struct wpi_tx_data *data;
2119 struct wpi_tx_cmd *cmd;
2121 DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x "
2122 "type %s len %d\n", desc->qid, desc->idx,
2123 desc->flags, wpi_cmd_str(desc->type),
2124 le32toh(desc->len));
2126 if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM)
2127 return; /* Not a command ack. */
2129 KASSERT(ring->queued == 0, ("ring->queued must be 0"));
2131 data = &ring->data[desc->idx];
2132 cmd = &ring->cmd[desc->idx];
2134 /* If the command was mapped in an mbuf, free it. */
2135 if (data->m != NULL) {
2136 bus_dmamap_sync(ring->data_dmat, data->map,
2137 BUS_DMASYNC_POSTWRITE);
2138 bus_dmamap_unload(ring->data_dmat, data->map);
2145 if (desc->type == WPI_CMD_SET_POWER_MODE) {
2146 struct wpi_pmgt_cmd *pcmd = (struct wpi_pmgt_cmd *)cmd->data;
2148 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2149 BUS_DMASYNC_POSTREAD);
2152 if (le16toh(pcmd->flags) & WPI_PS_ALLOW_SLEEP) {
2153 sc->sc_update_rx_ring = wpi_update_rx_ring_ps;
2154 sc->sc_update_tx_ring = wpi_update_tx_ring_ps;
2156 sc->sc_update_rx_ring = wpi_update_rx_ring;
2157 sc->sc_update_tx_ring = wpi_update_tx_ring;
2164 wpi_notif_intr(struct wpi_softc *sc)
2166 struct ieee80211com *ic = &sc->sc_ic;
2167 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2170 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
2171 BUS_DMASYNC_POSTREAD);
2173 hw = le32toh(sc->shared->next) & 0xfff;
2174 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
2176 while (sc->rxq.cur != hw) {
2177 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
2179 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2180 struct wpi_rx_desc *desc;
2182 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2183 BUS_DMASYNC_POSTREAD);
2184 desc = mtod(data->m, struct wpi_rx_desc *);
2186 DPRINTF(sc, WPI_DEBUG_NOTIFY,
2187 "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n",
2188 __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags,
2189 desc->type, wpi_cmd_str(desc->type), le32toh(desc->len));
2191 if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) {
2192 /* Reply to a command. */
2193 wpi_cmd_done(sc, desc);
2196 switch (desc->type) {
2198 /* An 802.11 frame has been received. */
2199 wpi_rx_done(sc, desc, data);
2201 if (sc->sc_running == 0) {
2202 /* wpi_stop() was called. */
2209 /* An 802.11 frame has been transmitted. */
2210 wpi_tx_done(sc, desc);
2213 case WPI_RX_STATISTICS:
2214 case WPI_BEACON_STATISTICS:
2215 wpi_rx_statistics(sc, desc, data);
2218 case WPI_BEACON_MISSED:
2220 struct wpi_beacon_missed *miss =
2221 (struct wpi_beacon_missed *)(desc + 1);
2222 uint32_t expected, misses, received, threshold;
2224 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2225 BUS_DMASYNC_POSTREAD);
2227 misses = le32toh(miss->consecutive);
2228 expected = le32toh(miss->expected);
2229 received = le32toh(miss->received);
2230 threshold = MAX(2, vap->iv_bmissthreshold);
2232 DPRINTF(sc, WPI_DEBUG_BMISS,
2233 "%s: beacons missed %u(%u) (received %u/%u)\n",
2234 __func__, misses, le32toh(miss->total), received,
2237 if (misses >= threshold ||
2238 (received == 0 && expected >= threshold)) {
2240 if (callout_pending(&sc->scan_timeout)) {
2241 wpi_cmd(sc, WPI_CMD_SCAN_ABORT, NULL,
2244 WPI_RXON_UNLOCK(sc);
2245 if (vap->iv_state == IEEE80211_S_RUN &&
2246 (ic->ic_flags & IEEE80211_F_SCAN) == 0)
2247 ieee80211_beacon_miss(ic);
2253 case WPI_BEACON_SENT:
2255 struct wpi_tx_stat *stat =
2256 (struct wpi_tx_stat *)(desc + 1);
2257 uint64_t *tsf = (uint64_t *)(stat + 1);
2258 uint32_t *mode = (uint32_t *)(tsf + 1);
2260 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2261 BUS_DMASYNC_POSTREAD);
2263 DPRINTF(sc, WPI_DEBUG_BEACON,
2264 "beacon sent: rts %u, ack %u, btkill %u, rate %u, "
2265 "duration %u, status %x, tsf %ju, mode %x\n",
2266 stat->rtsfailcnt, stat->ackfailcnt,
2267 stat->btkillcnt, stat->rate, le32toh(stat->duration),
2268 le32toh(stat->status), *tsf, *mode);
2275 struct wpi_ucode_info *uc =
2276 (struct wpi_ucode_info *)(desc + 1);
2278 /* The microcontroller is ready. */
2279 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2280 BUS_DMASYNC_POSTREAD);
2281 DPRINTF(sc, WPI_DEBUG_RESET,
2282 "microcode alive notification version=%d.%d "
2283 "subtype=%x alive=%x\n", uc->major, uc->minor,
2284 uc->subtype, le32toh(uc->valid));
2286 if (le32toh(uc->valid) != 1) {
2287 device_printf(sc->sc_dev,
2288 "microcontroller initialization failed\n");
2289 wpi_stop_locked(sc);
2292 /* Save the address of the error log in SRAM. */
2293 sc->errptr = le32toh(uc->errptr);
2296 case WPI_STATE_CHANGED:
2298 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2299 BUS_DMASYNC_POSTREAD);
2301 uint32_t *status = (uint32_t *)(desc + 1);
2303 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n",
2306 if (le32toh(*status) & 1) {
2308 wpi_clear_node_table(sc);
2310 taskqueue_enqueue(sc->sc_tq,
2311 &sc->sc_radiooff_task);
2317 case WPI_START_SCAN:
2319 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2320 BUS_DMASYNC_POSTREAD);
2322 struct wpi_start_scan *scan =
2323 (struct wpi_start_scan *)(desc + 1);
2324 DPRINTF(sc, WPI_DEBUG_SCAN,
2325 "%s: scanning channel %d status %x\n",
2326 __func__, scan->chan, le32toh(scan->status));
2333 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2334 BUS_DMASYNC_POSTREAD);
2336 struct wpi_stop_scan *scan =
2337 (struct wpi_stop_scan *)(desc + 1);
2339 DPRINTF(sc, WPI_DEBUG_SCAN,
2340 "scan finished nchan=%d status=%d chan=%d\n",
2341 scan->nchan, scan->status, scan->chan);
2344 callout_stop(&sc->scan_timeout);
2345 WPI_RXON_UNLOCK(sc);
2346 if (scan->status == WPI_SCAN_ABORTED)
2347 ieee80211_cancel_scan(vap);
2349 ieee80211_scan_next(vap);
2354 if (sc->rxq.cur % 8 == 0) {
2355 /* Tell the firmware what we have processed. */
2356 sc->sc_update_rx_ring(sc);
2362 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2363 * from power-down sleep mode.
2366 wpi_wakeup_intr(struct wpi_softc *sc)
2370 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
2371 "%s: ucode wakeup from power-down sleep\n", __func__);
2373 /* Wakeup RX and TX rings. */
2374 if (sc->rxq.update) {
2376 wpi_update_rx_ring(sc);
2379 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) {
2380 struct wpi_tx_ring *ring = &sc->txq[qid];
2384 wpi_update_tx_ring(sc, ring);
2387 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
2392 * This function prints firmware registers
2396 wpi_debug_registers(struct wpi_softc *sc)
2399 static const uint32_t csr_tbl[] = {
2416 static const uint32_t prph_tbl[] = {
2423 DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n");
2425 for (i = 0; i < nitems(csr_tbl); i++) {
2426 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2427 wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i]));
2429 if ((i + 1) % 2 == 0)
2430 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2432 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n");
2434 if (wpi_nic_lock(sc) == 0) {
2435 for (i = 0; i < nitems(prph_tbl); i++) {
2436 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2437 wpi_get_prph_string(prph_tbl[i]),
2438 wpi_prph_read(sc, prph_tbl[i]));
2440 if ((i + 1) % 2 == 0)
2441 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2443 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2446 DPRINTF(sc, WPI_DEBUG_REGISTER,
2447 "Cannot access internal registers.\n");
2453 * Dump the error log of the firmware when a firmware panic occurs. Although
2454 * we can't debug the firmware because it is neither open source nor free, it
2455 * can help us to identify certain classes of problems.
2458 wpi_fatal_intr(struct wpi_softc *sc)
2460 struct wpi_fw_dump dump;
2461 uint32_t i, offset, count;
2463 /* Check that the error log address is valid. */
2464 if (sc->errptr < WPI_FW_DATA_BASE ||
2465 sc->errptr + sizeof (dump) >
2466 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
2467 printf("%s: bad firmware error log address 0x%08x\n", __func__,
2471 if (wpi_nic_lock(sc) != 0) {
2472 printf("%s: could not read firmware error log\n", __func__);
2475 /* Read number of entries in the log. */
2476 count = wpi_mem_read(sc, sc->errptr);
2477 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
2478 printf("%s: invalid count field (count = %u)\n", __func__,
2483 /* Skip "count" field. */
2484 offset = sc->errptr + sizeof (uint32_t);
2485 printf("firmware error log (count = %u):\n", count);
2486 for (i = 0; i < count; i++) {
2487 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
2488 sizeof (dump) / sizeof (uint32_t));
2490 printf(" error type = \"%s\" (0x%08X)\n",
2491 (dump.desc < nitems(wpi_fw_errmsg)) ?
2492 wpi_fw_errmsg[dump.desc] : "UNKNOWN",
2494 printf(" error data = 0x%08X\n",
2496 printf(" branch link = 0x%08X%08X\n",
2497 dump.blink[0], dump.blink[1]);
2498 printf(" interrupt link = 0x%08X%08X\n",
2499 dump.ilink[0], dump.ilink[1]);
2500 printf(" time = %u\n", dump.time);
2502 offset += sizeof (dump);
2505 /* Dump driver status (TX and RX rings) while we're here. */
2506 printf("driver status:\n");
2508 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
2509 struct wpi_tx_ring *ring = &sc->txq[i];
2510 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2511 i, ring->qid, ring->cur, ring->queued);
2514 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2520 struct wpi_softc *sc = arg;
2525 /* Disable interrupts. */
2526 WPI_WRITE(sc, WPI_INT_MASK, 0);
2528 r1 = WPI_READ(sc, WPI_INT);
2530 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
2531 goto end; /* Hardware gone! */
2533 r2 = WPI_READ(sc, WPI_FH_INT);
2535 DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__,
2538 if (r1 == 0 && r2 == 0)
2539 goto done; /* Interrupt not for us. */
2541 /* Acknowledge interrupts. */
2542 WPI_WRITE(sc, WPI_INT, r1);
2543 WPI_WRITE(sc, WPI_FH_INT, r2);
2545 if (r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR)) {
2546 device_printf(sc->sc_dev, "fatal firmware error\n");
2548 wpi_debug_registers(sc);
2551 DPRINTF(sc, WPI_DEBUG_HW,
2552 "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" :
2553 "(Hardware Error)");
2554 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2558 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
2559 (r2 & WPI_FH_INT_RX))
2562 if (r1 & WPI_INT_ALIVE)
2563 wakeup(sc); /* Firmware is alive. */
2565 if (r1 & WPI_INT_WAKEUP)
2566 wpi_wakeup_intr(sc);
2569 /* Re-enable interrupts. */
2571 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
2573 end: WPI_UNLOCK(sc);
2577 wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf)
2579 struct ieee80211_frame *wh;
2580 struct wpi_tx_cmd *cmd;
2581 struct wpi_tx_data *data;
2582 struct wpi_tx_desc *desc;
2583 struct wpi_tx_ring *ring;
2585 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER];
2586 int error, i, hdrlen, nsegs, totlen, pad;
2590 KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow"));
2592 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2594 if (sc->sc_running == 0) {
2595 /* wpi_stop() was called */
2600 wh = mtod(buf->m, struct ieee80211_frame *);
2601 hdrlen = ieee80211_anyhdrsize(wh);
2602 totlen = buf->m->m_pkthdr.len;
2605 /* First segment length must be a multiple of 4. */
2606 pad = 4 - (hdrlen & 3);
2610 ring = &sc->txq[buf->ac];
2611 desc = &ring->desc[ring->cur];
2612 data = &ring->data[ring->cur];
2614 /* Prepare TX firmware command. */
2615 cmd = &ring->cmd[ring->cur];
2616 cmd->code = buf->code;
2618 cmd->qid = ring->qid;
2619 cmd->idx = ring->cur;
2621 memcpy(cmd->data, buf->data, buf->size);
2623 /* Save and trim IEEE802.11 header. */
2624 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen);
2625 m_adj(buf->m, hdrlen);
2627 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m,
2628 segs, &nsegs, BUS_DMA_NOWAIT);
2629 if (error != 0 && error != EFBIG) {
2630 device_printf(sc->sc_dev,
2631 "%s: can't map mbuf (error %d)\n", __func__, error);
2635 /* Too many DMA segments, linearize mbuf. */
2636 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1);
2638 device_printf(sc->sc_dev,
2639 "%s: could not defrag mbuf\n", __func__);
2645 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
2646 buf->m, segs, &nsegs, BUS_DMA_NOWAIT);
2648 device_printf(sc->sc_dev,
2649 "%s: can't map mbuf (error %d)\n", __func__,
2655 KASSERT(nsegs < WPI_MAX_SCATTER,
2656 ("too many DMA segments, nsegs (%d) should be less than %d",
2657 nsegs, WPI_MAX_SCATTER));
2662 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
2663 __func__, ring->qid, ring->cur, totlen, nsegs);
2665 /* Fill TX descriptor. */
2666 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs);
2667 /* First DMA segment is used by the TX command. */
2668 desc->segs[0].addr = htole32(data->cmd_paddr);
2669 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad);
2670 /* Other DMA segments are for data payload. */
2672 for (i = 1; i <= nsegs; i++) {
2673 desc->segs[i].addr = htole32(seg->ds_addr);
2674 desc->segs[i].len = htole32(seg->ds_len);
2678 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2679 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2680 BUS_DMASYNC_PREWRITE);
2681 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2682 BUS_DMASYNC_PREWRITE);
2685 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2686 sc->sc_update_tx_ring(sc, ring);
2688 if (ring->qid < WPI_CMD_QUEUE_NUM) {
2689 WPI_TXQ_STATE_LOCK(sc);
2691 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2692 WPI_TXQ_STATE_UNLOCK(sc);
2695 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2701 fail: m_freem(buf->m);
2703 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
2711 * Construct the data packet for a transmit buffer.
2714 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2716 const struct ieee80211_txparam *tp;
2717 struct ieee80211vap *vap = ni->ni_vap;
2718 struct ieee80211com *ic = ni->ni_ic;
2719 struct wpi_node *wn = WPI_NODE(ni);
2720 struct ieee80211_channel *chan;
2721 struct ieee80211_frame *wh;
2722 struct ieee80211_key *k = NULL;
2723 struct wpi_buf tx_data;
2724 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2728 int ac, error, swcrypt, rate, ismcast, totlen;
2730 wh = mtod(m, struct ieee80211_frame *);
2731 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2732 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2734 /* Select EDCA Access Category and TX ring for this frame. */
2735 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2736 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2737 tid = qos & IEEE80211_QOS_TID;
2742 ac = M_WME_GETAC(m);
2744 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
2745 ni->ni_chan : ic->ic_curchan;
2746 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2748 /* Choose a TX rate index. */
2749 if (type == IEEE80211_FC0_TYPE_MGT)
2750 rate = tp->mgmtrate;
2752 rate = tp->mcastrate;
2753 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2754 rate = tp->ucastrate;
2755 else if (m->m_flags & M_EAPOL)
2756 rate = tp->mgmtrate;
2758 /* XXX pass pktlen */
2759 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2760 rate = ni->ni_txrate;
2763 /* Encrypt the frame if need be. */
2764 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2765 /* Retrieve key for TX. */
2766 k = ieee80211_crypto_encap(ni, m);
2771 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2773 /* 802.11 header may have moved. */
2774 wh = mtod(m, struct ieee80211_frame *);
2776 totlen = m->m_pkthdr.len;
2778 if (ieee80211_radiotap_active_vap(vap)) {
2779 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2782 tap->wt_rate = rate;
2784 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2786 ieee80211_radiotap_tx(vap, m);
2791 /* Unicast frame, check if an ACK is expected. */
2792 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2793 IEEE80211_QOS_ACKPOLICY_NOACK)
2794 flags |= WPI_TX_NEED_ACK;
2797 if (!IEEE80211_QOS_HAS_SEQ(wh))
2798 flags |= WPI_TX_AUTO_SEQ;
2799 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2800 flags |= WPI_TX_MORE_FRAG; /* Cannot happen yet. */
2802 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2804 /* NB: Group frames are sent using CCK in 802.11b/g. */
2805 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2806 flags |= WPI_TX_NEED_RTS;
2807 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2808 WPI_RATE_IS_OFDM(rate)) {
2809 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2810 flags |= WPI_TX_NEED_CTS;
2811 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2812 flags |= WPI_TX_NEED_RTS;
2815 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2816 flags |= WPI_TX_FULL_TXOP;
2819 memset(tx, 0, sizeof (struct wpi_cmd_data));
2820 if (type == IEEE80211_FC0_TYPE_MGT) {
2821 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2823 /* Tell HW to set timestamp in probe responses. */
2824 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2825 flags |= WPI_TX_INSERT_TSTAMP;
2826 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2827 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2828 tx->timeout = htole16(3);
2830 tx->timeout = htole16(2);
2833 if (ismcast || type != IEEE80211_FC0_TYPE_DATA)
2834 tx->id = WPI_ID_BROADCAST;
2836 if (wn->id == WPI_ID_UNDEFINED) {
2837 device_printf(sc->sc_dev,
2838 "%s: undefined node id\n", __func__);
2846 if (k != NULL && !swcrypt) {
2847 switch (k->wk_cipher->ic_cipher) {
2848 case IEEE80211_CIPHER_AES_CCM:
2849 tx->security = WPI_CIPHER_CCMP;
2856 memcpy(tx->key, k->wk_key, k->wk_keylen);
2859 tx->len = htole16(totlen);
2860 tx->flags = htole32(flags);
2861 tx->plcp = rate2plcp(rate);
2863 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2864 tx->ofdm_mask = 0xff;
2865 tx->cck_mask = 0x0f;
2867 tx->data_ntries = tp->maxretry;
2871 tx_data.size = sizeof(struct wpi_cmd_data);
2872 tx_data.code = WPI_CMD_TX_DATA;
2875 return wpi_cmd2(sc, &tx_data);
2882 wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m,
2883 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
2885 struct ieee80211vap *vap = ni->ni_vap;
2886 struct ieee80211_key *k = NULL;
2887 struct ieee80211_frame *wh;
2888 struct wpi_buf tx_data;
2889 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2892 int ac, rate, swcrypt, totlen;
2894 wh = mtod(m, struct ieee80211_frame *);
2895 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2897 ac = params->ibp_pri & 3;
2899 /* Choose a TX rate index. */
2900 rate = params->ibp_rate0;
2903 if (!IEEE80211_QOS_HAS_SEQ(wh))
2904 flags |= WPI_TX_AUTO_SEQ;
2905 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
2906 flags |= WPI_TX_NEED_ACK;
2907 if (params->ibp_flags & IEEE80211_BPF_RTS)
2908 flags |= WPI_TX_NEED_RTS;
2909 if (params->ibp_flags & IEEE80211_BPF_CTS)
2910 flags |= WPI_TX_NEED_CTS;
2911 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2912 flags |= WPI_TX_FULL_TXOP;
2914 /* Encrypt the frame if need be. */
2915 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
2916 /* Retrieve key for TX. */
2917 k = ieee80211_crypto_encap(ni, m);
2922 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2924 /* 802.11 header may have moved. */
2925 wh = mtod(m, struct ieee80211_frame *);
2927 totlen = m->m_pkthdr.len;
2929 if (ieee80211_radiotap_active_vap(vap)) {
2930 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2933 tap->wt_rate = rate;
2934 if (params->ibp_flags & IEEE80211_BPF_CRYPTO)
2935 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2937 ieee80211_radiotap_tx(vap, m);
2940 memset(tx, 0, sizeof (struct wpi_cmd_data));
2941 if (type == IEEE80211_FC0_TYPE_MGT) {
2942 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2944 /* Tell HW to set timestamp in probe responses. */
2945 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2946 flags |= WPI_TX_INSERT_TSTAMP;
2947 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2948 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2949 tx->timeout = htole16(3);
2951 tx->timeout = htole16(2);
2954 if (k != NULL && !swcrypt) {
2955 switch (k->wk_cipher->ic_cipher) {
2956 case IEEE80211_CIPHER_AES_CCM:
2957 tx->security = WPI_CIPHER_CCMP;
2964 memcpy(tx->key, k->wk_key, k->wk_keylen);
2967 tx->len = htole16(totlen);
2968 tx->flags = htole32(flags);
2969 tx->plcp = rate2plcp(rate);
2970 tx->id = WPI_ID_BROADCAST;
2971 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2972 tx->rts_ntries = params->ibp_try1;
2973 tx->data_ntries = params->ibp_try0;
2977 tx_data.size = sizeof(struct wpi_cmd_data);
2978 tx_data.code = WPI_CMD_TX_DATA;
2981 return wpi_cmd2(sc, &tx_data);
2985 wpi_tx_ring_is_full(struct wpi_softc *sc, int ac)
2987 struct wpi_tx_ring *ring = &sc->txq[ac];
2990 WPI_TXQ_STATE_LOCK(sc);
2991 retval = (ring->queued > WPI_TX_RING_HIMARK);
2992 WPI_TXQ_STATE_UNLOCK(sc);
2997 static __inline void
2998 wpi_handle_tx_failure(struct ieee80211_node *ni)
3000 /* NB: m is reclaimed on tx failure */
3001 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
3002 ieee80211_free_node(ni);
3006 wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3007 const struct ieee80211_bpf_params *params)
3009 struct ieee80211com *ic = ni->ni_ic;
3010 struct wpi_softc *sc = ic->ic_softc;
3013 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3015 ac = M_WME_GETAC(m);
3019 if (sc->sc_running == 0 || wpi_tx_ring_is_full(sc, ac)) {
3021 error = sc->sc_running ? ENOBUFS : ENETDOWN;
3025 if (params == NULL) {
3027 * Legacy path; interpret frame contents to decide
3028 * precisely how to send the frame.
3030 error = wpi_tx_data(sc, m, ni);
3033 * Caller supplied explicit parameters to use in
3034 * sending the frame.
3036 error = wpi_tx_data_raw(sc, m, ni, params);
3039 unlock: WPI_TX_UNLOCK(sc);
3042 wpi_handle_tx_failure(ni);
3043 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3048 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3054 wpi_transmit(struct ieee80211com *ic, struct mbuf *m)
3056 struct wpi_softc *sc = ic->ic_softc;
3057 struct ieee80211_node *ni;
3061 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
3063 /* Check if interface is up & running. */
3064 if (sc->sc_running == 0) {
3069 /* Check for available space. */
3070 ac = M_WME_GETAC(m);
3071 if (wpi_tx_ring_is_full(sc, ac)) {
3077 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3078 if (wpi_tx_data(sc, m, ni) != 0) {
3079 wpi_handle_tx_failure(ni);
3082 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
3084 unlock: WPI_TX_UNLOCK(sc);
3090 wpi_watchdog_rfkill(void *arg)
3092 struct wpi_softc *sc = arg;
3093 struct ieee80211com *ic = &sc->sc_ic;
3095 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n");
3097 /* No need to lock firmware memory. */
3098 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) {
3099 /* Radio kill switch is still off. */
3100 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
3103 ieee80211_runtask(ic, &sc->sc_radioon_task);
3107 wpi_scan_timeout(void *arg)
3109 struct wpi_softc *sc = arg;
3110 struct ieee80211com *ic = &sc->sc_ic;
3112 ic_printf(ic, "scan timeout\n");
3113 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
3117 wpi_tx_timeout(void *arg)
3119 struct wpi_softc *sc = arg;
3120 struct ieee80211com *ic = &sc->sc_ic;
3122 ic_printf(ic, "device timeout\n");
3123 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
3127 wpi_parent(struct ieee80211com *ic)
3129 struct wpi_softc *sc = ic->ic_softc;
3130 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3132 if (ic->ic_nrunning > 0) {
3133 if (wpi_init(sc) == 0) {
3134 ieee80211_notify_radio(ic, 1);
3135 ieee80211_start_all(ic);
3137 ieee80211_notify_radio(ic, 0);
3138 ieee80211_stop(vap);
3145 * Send a command to the firmware.
3148 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, size_t size,
3151 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
3152 struct wpi_tx_desc *desc;
3153 struct wpi_tx_data *data;
3154 struct wpi_tx_cmd *cmd;
3161 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3163 if (sc->sc_running == 0) {
3164 /* wpi_stop() was called */
3165 if (code == WPI_CMD_SCAN)
3174 WPI_LOCK_ASSERT(sc);
3176 DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %zu async %d\n",
3177 __func__, wpi_cmd_str(code), size, async);
3179 desc = &ring->desc[ring->cur];
3180 data = &ring->data[ring->cur];
3183 if (size > sizeof cmd->data) {
3184 /* Command is too large to fit in a descriptor. */
3185 if (totlen > MCLBYTES) {
3189 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3194 cmd = mtod(m, struct wpi_tx_cmd *);
3195 error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3196 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3203 cmd = &ring->cmd[ring->cur];
3204 paddr = data->cmd_paddr;
3209 cmd->qid = ring->qid;
3210 cmd->idx = ring->cur;
3211 memcpy(cmd->data, buf, size);
3213 desc->nsegs = 1 + (WPI_PAD32(size) << 4);
3214 desc->segs[0].addr = htole32(paddr);
3215 desc->segs[0].len = htole32(totlen);
3217 if (size > sizeof cmd->data) {
3218 bus_dmamap_sync(ring->data_dmat, data->map,
3219 BUS_DMASYNC_PREWRITE);
3221 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3222 BUS_DMASYNC_PREWRITE);
3224 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3225 BUS_DMASYNC_PREWRITE);
3227 /* Kick command ring. */
3228 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
3229 sc->sc_update_tx_ring(sc, ring);
3231 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3235 return async ? 0 : mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
3237 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3245 * Configure HW multi-rate retries.
3248 wpi_mrr_setup(struct wpi_softc *sc)
3250 struct ieee80211com *ic = &sc->sc_ic;
3251 struct wpi_mrr_setup mrr;
3254 /* CCK rates (not used with 802.11a). */
3255 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
3256 mrr.rates[i].flags = 0;
3257 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3258 /* Fallback to the immediate lower CCK rate (if any.) */
3260 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
3261 /* Try twice at this rate before falling back to "next". */
3262 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3264 /* OFDM rates (not used with 802.11b). */
3265 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
3266 mrr.rates[i].flags = 0;
3267 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3268 /* Fallback to the immediate lower rate (if any.) */
3269 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
3270 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
3271 ((ic->ic_curmode == IEEE80211_MODE_11A) ?
3272 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
3274 /* Try twice at this rate before falling back to "next". */
3275 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3277 /* Setup MRR for control frames. */
3278 mrr.which = htole32(WPI_MRR_CTL);
3279 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3281 device_printf(sc->sc_dev,
3282 "could not setup MRR for control frames\n");
3285 /* Setup MRR for data frames. */
3286 mrr.which = htole32(WPI_MRR_DATA);
3287 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3289 device_printf(sc->sc_dev,
3290 "could not setup MRR for data frames\n");
3297 wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3299 struct ieee80211com *ic = ni->ni_ic;
3300 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
3301 struct wpi_node *wn = WPI_NODE(ni);
3302 struct wpi_node_info node;
3305 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3307 if (wn->id == WPI_ID_UNDEFINED)
3310 memset(&node, 0, sizeof node);
3311 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3313 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3314 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3315 node.action = htole32(WPI_ACTION_SET_RATE);
3316 node.antenna = WPI_ANTENNA_BOTH;
3318 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding node %d (%s)\n", __func__,
3319 wn->id, ether_sprintf(ni->ni_macaddr));
3321 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
3323 device_printf(sc->sc_dev,
3324 "%s: wpi_cmd() call failed with error code %d\n", __func__,
3329 if (wvp->wv_gtk != 0) {
3330 error = wpi_set_global_keys(ni);
3332 device_printf(sc->sc_dev,
3333 "%s: error while setting global keys\n", __func__);
3342 * Broadcast node is used to send group-addressed and management frames.
3345 wpi_add_broadcast_node(struct wpi_softc *sc, int async)
3347 struct ieee80211com *ic = &sc->sc_ic;
3348 struct wpi_node_info node;
3350 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3352 memset(&node, 0, sizeof node);
3353 IEEE80211_ADDR_COPY(node.macaddr, ieee80211broadcastaddr);
3354 node.id = WPI_ID_BROADCAST;
3355 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3356 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3357 node.action = htole32(WPI_ACTION_SET_RATE);
3358 node.antenna = WPI_ANTENNA_BOTH;
3360 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__);
3362 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async);
3366 wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3368 struct wpi_node *wn = WPI_NODE(ni);
3371 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3373 wn->id = wpi_add_node_entry_sta(sc);
3375 if ((error = wpi_add_node(sc, ni)) != 0) {
3376 wpi_del_node_entry(sc, wn->id);
3377 wn->id = WPI_ID_UNDEFINED;
3385 wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3387 struct wpi_node *wn = WPI_NODE(ni);
3390 KASSERT(wn->id == WPI_ID_UNDEFINED,
3391 ("the node %d was added before", wn->id));
3393 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3395 if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) {
3396 device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__);
3400 if ((error = wpi_add_node(sc, ni)) != 0) {
3401 wpi_del_node_entry(sc, wn->id);
3402 wn->id = WPI_ID_UNDEFINED;
3410 wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3412 struct wpi_node *wn = WPI_NODE(ni);
3413 struct wpi_cmd_del_node node;
3416 KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed"));
3418 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3420 memset(&node, 0, sizeof node);
3421 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3424 DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__,
3425 wn->id, ether_sprintf(ni->ni_macaddr));
3427 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1);
3429 device_printf(sc->sc_dev,
3430 "%s: could not delete node %u, error %d\n", __func__,
3436 wpi_updateedca(struct ieee80211com *ic)
3438 #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3439 struct wpi_softc *sc = ic->ic_softc;
3440 struct wpi_edca_params cmd;
3443 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3445 memset(&cmd, 0, sizeof cmd);
3446 cmd.flags = htole32(WPI_EDCA_UPDATE);
3447 for (aci = 0; aci < WME_NUM_AC; aci++) {
3448 const struct wmeParams *ac =
3449 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci];
3450 cmd.ac[aci].aifsn = ac->wmep_aifsn;
3451 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin));
3452 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax));
3453 cmd.ac[aci].txoplimit =
3454 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit));
3456 DPRINTF(sc, WPI_DEBUG_EDCA,
3457 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
3458 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn,
3459 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax,
3460 cmd.ac[aci].txoplimit);
3462 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3464 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3471 wpi_set_promisc(struct wpi_softc *sc)
3473 struct ieee80211com *ic = &sc->sc_ic;
3474 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3475 uint32_t promisc_filter;
3477 promisc_filter = WPI_FILTER_CTL;
3478 if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP)
3479 promisc_filter |= WPI_FILTER_PROMISC;
3481 if (ic->ic_promisc > 0)
3482 sc->rxon.filter |= htole32(promisc_filter);
3484 sc->rxon.filter &= ~htole32(promisc_filter);
3488 wpi_update_promisc(struct ieee80211com *ic)
3490 struct wpi_softc *sc = ic->ic_softc;
3493 wpi_set_promisc(sc);
3495 if (wpi_send_rxon(sc, 1, 1) != 0) {
3496 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3499 WPI_RXON_UNLOCK(sc);
3503 wpi_update_mcast(struct ieee80211com *ic)
3509 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3511 struct wpi_cmd_led led;
3513 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3516 led.unit = htole32(100000); /* on/off in unit of 100ms */
3519 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
3523 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
3525 struct wpi_cmd_timing cmd;
3528 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3530 memset(&cmd, 0, sizeof cmd);
3531 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3532 cmd.bintval = htole16(ni->ni_intval);
3533 cmd.lintval = htole16(10);
3535 /* Compute remaining time until next beacon. */
3536 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU;
3537 mod = le64toh(cmd.tstamp) % val;
3538 cmd.binitval = htole32((uint32_t)(val - mod));
3540 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3541 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
3543 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
3547 * This function is called periodically (every 60 seconds) to adjust output
3548 * power to temperature changes.
3551 wpi_power_calibration(struct wpi_softc *sc)
3555 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3557 /* Update sensor data. */
3558 temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
3559 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp);
3561 /* Sanity-check read value. */
3562 if (temp < -260 || temp > 25) {
3563 /* This can't be correct, ignore. */
3564 DPRINTF(sc, WPI_DEBUG_TEMP,
3565 "out-of-range temperature reported: %d\n", temp);
3569 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp);
3571 /* Adjust Tx power if need be. */
3572 if (abs(temp - sc->temp) <= 6)
3577 if (wpi_set_txpower(sc, 1) != 0) {
3578 /* just warn, too bad for the automatic calibration... */
3579 device_printf(sc->sc_dev,"could not adjust Tx power\n");
3584 * Set TX power for current channel.
3587 wpi_set_txpower(struct wpi_softc *sc, int async)
3589 struct wpi_power_group *group;
3590 struct wpi_cmd_txpower cmd;
3592 int idx, is_chan_5ghz, i;
3594 /* Retrieve current channel from last RXON. */
3595 chan = sc->rxon.chan;
3596 is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0;
3598 /* Find the TX power group to which this channel belongs. */
3600 for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3601 if (chan <= group->chan)
3604 group = &sc->groups[0];
3606 memset(&cmd, 0, sizeof cmd);
3607 cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ;
3608 cmd.chan = htole16(chan);
3610 /* Set TX power for all OFDM and CCK rates. */
3611 for (i = 0; i <= WPI_RIDX_MAX ; i++) {
3612 /* Retrieve TX power for this channel/rate. */
3613 idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i);
3615 cmd.rates[i].plcp = wpi_ridx_to_plcp[i];
3618 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
3619 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
3621 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
3622 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
3624 DPRINTF(sc, WPI_DEBUG_TEMP,
3625 "chan %d/ridx %d: power index %d\n", chan, i, idx);
3628 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
3632 * Determine Tx power index for a given channel/rate combination.
3633 * This takes into account the regulatory information from EEPROM and the
3634 * current temperature.
3637 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3638 uint8_t chan, int is_chan_5ghz, int ridx)
3640 /* Fixed-point arithmetic division using a n-bit fractional part. */
3641 #define fdivround(a, b, n) \
3642 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3644 /* Linear interpolation. */
3645 #define interpolate(x, x1, y1, x2, y2, n) \
3646 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3648 struct wpi_power_sample *sample;
3651 /* Default TX power is group maximum TX power minus 3dB. */
3652 pwr = group->maxpwr / 2;
3654 /* Decrease TX power for highest OFDM rates to reduce distortion. */
3656 case WPI_RIDX_OFDM36:
3657 pwr -= is_chan_5ghz ? 5 : 0;
3659 case WPI_RIDX_OFDM48:
3660 pwr -= is_chan_5ghz ? 10 : 7;
3662 case WPI_RIDX_OFDM54:
3663 pwr -= is_chan_5ghz ? 12 : 9;
3667 /* Never exceed the channel maximum allowed TX power. */
3668 pwr = min(pwr, sc->maxpwr[chan]);
3670 /* Retrieve TX power index into gain tables from samples. */
3671 for (sample = group->samples; sample < &group->samples[3]; sample++)
3672 if (pwr > sample[1].power)
3674 /* Fixed-point linear interpolation using a 19-bit fractional part. */
3675 idx = interpolate(pwr, sample[0].power, sample[0].index,
3676 sample[1].power, sample[1].index, 19);
3679 * Adjust power index based on current temperature:
3680 * - if cooler than factory-calibrated: decrease output power
3681 * - if warmer than factory-calibrated: increase output power
3683 idx -= (sc->temp - group->temp) * 11 / 100;
3685 /* Decrease TX power for CCK rates (-5dB). */
3686 if (ridx >= WPI_RIDX_CCK1)
3689 /* Make sure idx stays in a valid range. */
3692 if (idx > WPI_MAX_PWR_INDEX)
3693 return WPI_MAX_PWR_INDEX;
3701 * Set STA mode power saving level (between 0 and 5).
3702 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
3705 wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async)
3707 struct wpi_pmgt_cmd cmd;
3708 const struct wpi_pmgt *pmgt;
3709 uint32_t max, skip_dtim;
3713 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
3714 "%s: dtim=%d, level=%d, async=%d\n",
3715 __func__, dtim, level, async);
3717 /* Select which PS parameters to use. */
3719 pmgt = &wpi_pmgt[0][level];
3721 pmgt = &wpi_pmgt[1][level];
3723 memset(&cmd, 0, sizeof cmd);
3724 if (level != 0) /* not CAM */
3725 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
3726 /* Retrieve PCIe Active State Power Management (ASPM). */
3727 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
3728 if (!(reg & 0x1)) /* L0s Entry disabled. */
3729 cmd.flags |= htole16(WPI_PS_PCI_PMGT);
3731 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU);
3732 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU);
3738 skip_dtim = pmgt->skip_dtim;
3740 if (skip_dtim != 0) {
3741 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
3742 max = pmgt->intval[4];
3743 if (max == (uint32_t)-1)
3744 max = dtim * (skip_dtim + 1);
3745 else if (max > dtim)
3746 max = (max / dtim) * dtim;
3750 for (i = 0; i < 5; i++)
3751 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
3753 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
3757 wpi_send_btcoex(struct wpi_softc *sc)
3759 struct wpi_bluetooth cmd;
3761 memset(&cmd, 0, sizeof cmd);
3762 cmd.flags = WPI_BT_COEX_MODE_4WIRE;
3763 cmd.lead_time = WPI_BT_LEAD_TIME_DEF;
3764 cmd.max_kill = WPI_BT_MAX_KILL_DEF;
3765 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n",
3767 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0);
3771 wpi_send_rxon(struct wpi_softc *sc, int assoc, int async)
3776 WPI_RXON_LOCK_ASSERT(sc);
3778 if (assoc && wpi_check_bss_filter(sc) != 0) {
3779 struct wpi_assoc rxon_assoc;
3781 rxon_assoc.flags = sc->rxon.flags;
3782 rxon_assoc.filter = sc->rxon.filter;
3783 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask;
3784 rxon_assoc.cck_mask = sc->rxon.cck_mask;
3785 rxon_assoc.reserved = 0;
3787 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc,
3788 sizeof (struct wpi_assoc), async);
3790 device_printf(sc->sc_dev,
3791 "RXON_ASSOC command failed, error %d\n", error);
3797 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3798 sizeof (struct wpi_rxon), async);
3800 wpi_clear_node_table(sc);
3803 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3804 sizeof (struct wpi_rxon), async);
3806 wpi_clear_node_table(sc);
3810 device_printf(sc->sc_dev,
3811 "RXON command failed, error %d\n", error);
3815 /* Add broadcast node. */
3816 error = wpi_add_broadcast_node(sc, async);
3818 device_printf(sc->sc_dev,
3819 "could not add broadcast node, error %d\n", error);
3824 /* Configuration has changed, set Tx power accordingly. */
3825 if ((error = wpi_set_txpower(sc, async)) != 0) {
3826 device_printf(sc->sc_dev,
3827 "%s: could not set TX power, error %d\n", __func__, error);
3835 * Configure the card to listen to a particular channel, this transisions the
3836 * card in to being able to receive frames from remote devices.
3839 wpi_config(struct wpi_softc *sc)
3841 struct ieee80211com *ic = &sc->sc_ic;
3842 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3843 struct ieee80211_channel *c = ic->ic_curchan;
3846 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3848 /* Set power saving level to CAM during initialization. */
3849 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
3850 device_printf(sc->sc_dev,
3851 "%s: could not set power saving level\n", __func__);
3855 /* Configure bluetooth coexistence. */
3856 if ((error = wpi_send_btcoex(sc)) != 0) {
3857 device_printf(sc->sc_dev,
3858 "could not configure bluetooth coexistence\n");
3862 /* Configure adapter. */
3863 memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
3864 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr);
3866 /* Set default channel. */
3867 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
3868 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
3869 if (IEEE80211_IS_CHAN_2GHZ(c))
3870 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
3872 sc->rxon.filter = WPI_FILTER_MULTICAST;
3873 switch (ic->ic_opmode) {
3874 case IEEE80211_M_STA:
3875 sc->rxon.mode = WPI_MODE_STA;
3877 case IEEE80211_M_IBSS:
3878 sc->rxon.mode = WPI_MODE_IBSS;
3879 sc->rxon.filter |= WPI_FILTER_BEACON;
3881 case IEEE80211_M_HOSTAP:
3882 /* XXX workaround for beaconing */
3883 sc->rxon.mode = WPI_MODE_IBSS;
3884 sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC;
3886 case IEEE80211_M_AHDEMO:
3887 sc->rxon.mode = WPI_MODE_HOSTAP;
3889 case IEEE80211_M_MONITOR:
3890 sc->rxon.mode = WPI_MODE_MONITOR;
3893 device_printf(sc->sc_dev, "unknown opmode %d\n",
3897 sc->rxon.filter = htole32(sc->rxon.filter);
3898 wpi_set_promisc(sc);
3899 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
3900 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
3902 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) {
3903 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3908 /* Setup rate scalling. */
3909 if ((error = wpi_mrr_setup(sc)) != 0) {
3910 device_printf(sc->sc_dev, "could not setup MRR, error %d\n",
3915 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3921 wpi_get_active_dwell_time(struct wpi_softc *sc,
3922 struct ieee80211_channel *c, uint8_t n_probes)
3924 /* No channel? Default to 2GHz settings. */
3925 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) {
3926 return (WPI_ACTIVE_DWELL_TIME_2GHZ +
3927 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1));
3930 /* 5GHz dwell time. */
3931 return (WPI_ACTIVE_DWELL_TIME_5GHZ +
3932 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1));
3936 * Limit the total dwell time.
3938 * Returns the dwell time in milliseconds.
3941 wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time)
3943 struct ieee80211com *ic = &sc->sc_ic;
3944 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3947 /* bintval is in TU (1.024mS) */
3949 bintval = vap->iv_bss->ni_intval;
3952 * If it's non-zero, we should calculate the minimum of
3953 * it and the DWELL_BASE.
3955 * XXX Yes, the math should take into account that bintval
3956 * is 1.024mS, not 1mS..
3959 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__,
3961 return (MIN(dwell_time, bintval - WPI_CHANNEL_TUNE_TIME * 2));
3964 /* No association context? Default. */
3969 wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c)
3973 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c))
3974 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ;
3976 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ;
3978 /* Clamp to the beacon interval if we're associated. */
3979 return (wpi_limit_dwell(sc, passive));
3983 wpi_get_scan_pause_time(uint32_t time, uint16_t bintval)
3985 uint32_t mod = (time % bintval) * IEEE80211_DUR_TU;
3986 uint32_t nbeacons = time / bintval;
3988 if (mod > WPI_PAUSE_MAX_TIME)
3989 mod = WPI_PAUSE_MAX_TIME;
3991 return WPI_PAUSE_SCAN(nbeacons, mod);
3995 * Send a scan request to the firmware.
3998 wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c)
4000 struct ieee80211com *ic = &sc->sc_ic;
4001 struct ieee80211_scan_state *ss = ic->ic_scan;
4002 struct ieee80211vap *vap = ss->ss_vap;
4003 struct wpi_scan_hdr *hdr;
4004 struct wpi_cmd_data *tx;
4005 struct wpi_scan_essid *essids;
4006 struct wpi_scan_chan *chan;
4007 struct ieee80211_frame *wh;
4008 struct ieee80211_rateset *rs;
4009 uint16_t dwell_active, dwell_passive;
4011 int bgscan, bintval, buflen, error, i, nssid;
4013 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4016 * We are absolutely not allowed to send a scan command when another
4017 * scan command is pending.
4019 if (callout_pending(&sc->scan_timeout)) {
4020 device_printf(sc->sc_dev, "%s: called whilst scanning!\n",
4026 bgscan = wpi_check_bss_filter(sc);
4027 bintval = vap->iv_bss->ni_intval;
4029 bintval < WPI_QUIET_TIME_DEFAULT + WPI_CHANNEL_TUNE_TIME * 2) {
4034 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
4036 device_printf(sc->sc_dev,
4037 "%s: could not allocate buffer for scan command\n",
4042 hdr = (struct wpi_scan_hdr *)buf;
4045 * Move to the next channel if no packets are received within 10 msecs
4046 * after sending the probe request.
4048 hdr->quiet_time = htole16(WPI_QUIET_TIME_DEFAULT);
4049 hdr->quiet_threshold = htole16(1);
4053 * Max needs to be greater than active and passive and quiet!
4054 * It's also in microseconds!
4056 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU);
4057 hdr->pause_svc = htole32(wpi_get_scan_pause_time(100,
4061 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON);
4063 tx = (struct wpi_cmd_data *)(hdr + 1);
4064 tx->flags = htole32(WPI_TX_AUTO_SEQ);
4065 tx->id = WPI_ID_BROADCAST;
4066 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
4068 if (IEEE80211_IS_CHAN_5GHZ(c)) {
4069 /* Send probe requests at 6Mbps. */
4070 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6];
4071 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4073 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
4074 /* Send probe requests at 1Mbps. */
4075 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4076 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4079 essids = (struct wpi_scan_essid *)(tx + 1);
4080 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
4081 for (i = 0; i < nssid; i++) {
4082 essids[i].id = IEEE80211_ELEMID_SSID;
4083 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN);
4084 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len);
4086 if (sc->sc_debug & WPI_DEBUG_SCAN) {
4087 printf("Scanning Essid: ");
4088 ieee80211_print_essid(essids[i].data, essids[i].len);
4095 * Build a probe request frame. Most of the following code is a
4096 * copy & paste of what is done in net80211.
4098 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS);
4099 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4100 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4101 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
4102 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4103 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
4104 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4106 frm = (uint8_t *)(wh + 1);
4107 frm = ieee80211_add_ssid(frm, NULL, 0);
4108 frm = ieee80211_add_rates(frm, rs);
4109 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4110 frm = ieee80211_add_xrates(frm, rs);
4112 /* Set length of probe request. */
4113 tx->len = htole16(frm - (uint8_t *)wh);
4116 * Construct information about the channel that we
4117 * want to scan. The firmware expects this to be directly
4118 * after the scan probe request
4120 chan = (struct wpi_scan_chan *)frm;
4121 chan->chan = htole16(ieee80211_chan2ieee(ic, c));
4124 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT;
4125 chan->flags |= WPI_CHAN_NPBREQS(nssid);
4127 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER;
4129 if (!IEEE80211_IS_CHAN_PASSIVE(c))
4130 chan->flags |= WPI_CHAN_ACTIVE;
4133 * Calculate the active/passive dwell times.
4135 dwell_active = wpi_get_active_dwell_time(sc, c, nssid);
4136 dwell_passive = wpi_get_passive_dwell_time(sc, c);
4138 /* Make sure they're valid. */
4139 if (dwell_active > dwell_passive)
4140 dwell_active = dwell_passive;
4142 chan->active = htole16(dwell_active);
4143 chan->passive = htole16(dwell_passive);
4145 chan->dsp_gain = 0x6e; /* Default level */
4147 if (IEEE80211_IS_CHAN_5GHZ(c))
4148 chan->rf_gain = 0x3b;
4150 chan->rf_gain = 0x28;
4152 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n",
4153 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c));
4157 if (hdr->nchan == 1 && sc->rxon.chan == chan->chan) {
4158 /* XXX Force probe request transmission. */
4159 memcpy(chan + 1, chan, sizeof (struct wpi_scan_chan));
4163 /* Reduce unnecessary delay. */
4165 chan->passive = chan->active = hdr->quiet_time;
4172 buflen = (uint8_t *)chan - buf;
4173 hdr->len = htole16(buflen);
4175 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n",
4177 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
4178 free(buf, M_DEVBUF);
4183 callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc);
4185 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4189 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
4195 wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
4197 struct ieee80211com *ic = vap->iv_ic;
4198 struct ieee80211_node *ni = vap->iv_bss;
4199 struct ieee80211_channel *c = ni->ni_chan;
4204 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4206 /* Update adapter configuration. */
4207 sc->rxon.associd = 0;
4208 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
4209 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4210 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4211 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4212 if (IEEE80211_IS_CHAN_2GHZ(c))
4213 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4214 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4215 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4216 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4217 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4218 if (IEEE80211_IS_CHAN_A(c)) {
4219 sc->rxon.cck_mask = 0;
4220 sc->rxon.ofdm_mask = 0x15;
4221 } else if (IEEE80211_IS_CHAN_B(c)) {
4222 sc->rxon.cck_mask = 0x03;
4223 sc->rxon.ofdm_mask = 0;
4225 /* Assume 802.11b/g. */
4226 sc->rxon.cck_mask = 0x0f;
4227 sc->rxon.ofdm_mask = 0x15;
4230 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n",
4231 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask,
4232 sc->rxon.ofdm_mask);
4234 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4235 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4239 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4241 WPI_RXON_UNLOCK(sc);
4247 wpi_config_beacon(struct wpi_vap *wvp)
4249 struct ieee80211vap *vap = &wvp->wv_vap;
4250 struct ieee80211com *ic = vap->iv_ic;
4251 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
4252 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4253 struct wpi_softc *sc = ic->ic_softc;
4254 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
4255 struct ieee80211_tim_ie *tie;
4260 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4262 WPI_VAP_LOCK_ASSERT(wvp);
4264 cmd->len = htole16(bcn->m->m_pkthdr.len);
4265 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4266 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4268 /* XXX seems to be unused */
4269 if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) {
4270 tie = (struct ieee80211_tim_ie *) bo->bo_tim;
4271 ptr = mtod(bcn->m, uint8_t *);
4273 cmd->tim = htole16(bo->bo_tim - ptr);
4274 cmd->timsz = tie->tim_len;
4277 /* Necessary for recursion in ieee80211_beacon_update(). */
4279 bcn->m = m_dup(m, M_NOWAIT);
4280 if (bcn->m == NULL) {
4281 device_printf(sc->sc_dev,
4282 "%s: could not copy beacon frame\n", __func__);
4287 if ((error = wpi_cmd2(sc, bcn)) != 0) {
4288 device_printf(sc->sc_dev,
4289 "%s: could not update beacon frame, error %d", __func__,
4300 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
4302 struct ieee80211vap *vap = ni->ni_vap;
4303 struct wpi_vap *wvp = WPI_VAP(vap);
4304 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4308 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4310 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
4313 m = ieee80211_beacon_alloc(ni);
4315 device_printf(sc->sc_dev,
4316 "%s: could not allocate beacon frame\n", __func__);
4326 error = wpi_config_beacon(wvp);
4327 WPI_VAP_UNLOCK(wvp);
4333 wpi_update_beacon(struct ieee80211vap *vap, int item)
4335 struct wpi_softc *sc = vap->iv_ic->ic_softc;
4336 struct wpi_vap *wvp = WPI_VAP(vap);
4337 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4338 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
4339 struct ieee80211_node *ni = vap->iv_bss;
4342 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4345 if (bcn->m == NULL) {
4346 bcn->m = ieee80211_beacon_alloc(ni);
4347 if (bcn->m == NULL) {
4348 device_printf(sc->sc_dev,
4349 "%s: could not allocate beacon frame\n", __func__);
4351 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR,
4354 WPI_VAP_UNLOCK(wvp);
4358 WPI_VAP_UNLOCK(wvp);
4360 if (item == IEEE80211_BEACON_TIM)
4361 mcast = 1; /* TODO */
4363 setbit(bo->bo_flags, item);
4364 ieee80211_beacon_update(ni, bcn->m, mcast);
4367 wpi_config_beacon(wvp);
4368 WPI_VAP_UNLOCK(wvp);
4370 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4374 wpi_newassoc(struct ieee80211_node *ni, int isnew)
4376 struct ieee80211vap *vap = ni->ni_vap;
4377 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4378 struct wpi_node *wn = WPI_NODE(ni);
4383 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4385 if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) {
4386 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
4387 device_printf(sc->sc_dev,
4388 "%s: could not add IBSS node, error %d\n",
4396 wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
4398 struct ieee80211com *ic = vap->iv_ic;
4399 struct ieee80211_node *ni = vap->iv_bss;
4400 struct ieee80211_channel *c = ni->ni_chan;
4403 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4405 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
4406 /* Link LED blinks while monitoring. */
4407 wpi_set_led(sc, WPI_LED_LINK, 5, 5);
4411 /* XXX kernel panic workaround */
4412 if (c == IEEE80211_CHAN_ANYC) {
4413 device_printf(sc->sc_dev, "%s: incomplete configuration\n",
4418 if ((error = wpi_set_timing(sc, ni)) != 0) {
4419 device_printf(sc->sc_dev,
4420 "%s: could not set timing, error %d\n", __func__, error);
4424 /* Update adapter configuration. */
4426 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4427 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni));
4428 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4429 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4430 if (IEEE80211_IS_CHAN_2GHZ(c))
4431 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4432 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4433 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4434 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4435 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4436 if (IEEE80211_IS_CHAN_A(c)) {
4437 sc->rxon.cck_mask = 0;
4438 sc->rxon.ofdm_mask = 0x15;
4439 } else if (IEEE80211_IS_CHAN_B(c)) {
4440 sc->rxon.cck_mask = 0x03;
4441 sc->rxon.ofdm_mask = 0;
4443 /* Assume 802.11b/g. */
4444 sc->rxon.cck_mask = 0x0f;
4445 sc->rxon.ofdm_mask = 0x15;
4447 sc->rxon.filter |= htole32(WPI_FILTER_BSS);
4449 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n",
4450 sc->rxon.chan, sc->rxon.flags);
4452 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4453 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4458 /* Start periodic calibration timer. */
4459 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
4461 WPI_RXON_UNLOCK(sc);
4463 if (vap->iv_opmode == IEEE80211_M_IBSS ||
4464 vap->iv_opmode == IEEE80211_M_HOSTAP) {
4465 if ((error = wpi_setup_beacon(sc, ni)) != 0) {
4466 device_printf(sc->sc_dev,
4467 "%s: could not setup beacon, error %d\n", __func__,
4473 if (vap->iv_opmode == IEEE80211_M_STA) {
4476 error = wpi_add_sta_node(sc, ni);
4479 device_printf(sc->sc_dev,
4480 "%s: could not add BSS node, error %d\n", __func__,
4486 /* Link LED always on while associated. */
4487 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
4489 /* Enable power-saving mode if requested by user. */
4490 if ((vap->iv_flags & IEEE80211_F_PMGTON) &&
4491 vap->iv_opmode != IEEE80211_M_IBSS)
4492 (void)wpi_set_pslevel(sc, 0, 3, 1);
4494 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4500 wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4502 const struct ieee80211_cipher *cip = k->wk_cipher;
4503 struct ieee80211vap *vap = ni->ni_vap;
4504 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4505 struct wpi_node *wn = WPI_NODE(ni);
4506 struct wpi_node_info node;
4510 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4512 if (wpi_check_node_entry(sc, wn->id) == 0) {
4513 device_printf(sc->sc_dev, "%s: node does not exist\n",
4518 switch (cip->ic_cipher) {
4519 case IEEE80211_CIPHER_AES_CCM:
4520 kflags = WPI_KFLAG_CCMP;
4524 device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__,
4529 kflags |= WPI_KFLAG_KID(k->wk_keyix);
4530 if (k->wk_flags & IEEE80211_KEY_GROUP)
4531 kflags |= WPI_KFLAG_MULTICAST;
4533 memset(&node, 0, sizeof node);
4535 node.control = WPI_NODE_UPDATE;
4536 node.flags = WPI_FLAG_KEY_SET;
4537 node.kflags = htole16(kflags);
4538 memcpy(node.key, k->wk_key, k->wk_keylen);
4540 DPRINTF(sc, WPI_DEBUG_KEY,
4541 "%s: setting %s key id %d for node %d (%s)\n", __func__,
4542 (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix,
4543 node.id, ether_sprintf(ni->ni_macaddr));
4545 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4547 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4552 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4553 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4554 kflags |= WPI_KFLAG_MULTICAST;
4555 node.kflags = htole16(kflags);
4564 wpi_load_key_cb(void *arg, struct ieee80211_node *ni)
4566 const struct ieee80211_key *k = arg;
4567 struct ieee80211vap *vap = ni->ni_vap;
4568 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4569 struct wpi_node *wn = WPI_NODE(ni);
4572 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4576 error = wpi_load_key(ni, k);
4580 device_printf(sc->sc_dev, "%s: error while setting key\n",
4586 wpi_set_global_keys(struct ieee80211_node *ni)
4588 struct ieee80211vap *vap = ni->ni_vap;
4589 struct ieee80211_key *wk = &vap->iv_nw_keys[0];
4592 for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++)
4593 if (wk->wk_keyix != IEEE80211_KEYIX_NONE)
4594 error = wpi_load_key(ni, wk);
4600 wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4602 struct ieee80211vap *vap = ni->ni_vap;
4603 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4604 struct wpi_node *wn = WPI_NODE(ni);
4605 struct wpi_node_info node;
4609 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4611 if (wpi_check_node_entry(sc, wn->id) == 0) {
4612 DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__);
4613 return 1; /* Nothing to do. */
4616 kflags = WPI_KFLAG_KID(k->wk_keyix);
4617 if (k->wk_flags & IEEE80211_KEY_GROUP)
4618 kflags |= WPI_KFLAG_MULTICAST;
4620 memset(&node, 0, sizeof node);
4622 node.control = WPI_NODE_UPDATE;
4623 node.flags = WPI_FLAG_KEY_SET;
4624 node.kflags = htole16(kflags);
4626 DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n",
4627 __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast",
4628 k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr));
4630 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4632 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4637 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4638 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4639 kflags |= WPI_KFLAG_MULTICAST;
4640 node.kflags = htole16(kflags);
4649 wpi_del_key_cb(void *arg, struct ieee80211_node *ni)
4651 const struct ieee80211_key *k = arg;
4652 struct ieee80211vap *vap = ni->ni_vap;
4653 struct wpi_softc *sc = ni->ni_ic->ic_softc;
4654 struct wpi_node *wn = WPI_NODE(ni);
4657 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4661 error = wpi_del_key(ni, k);
4665 device_printf(sc->sc_dev, "%s: error while deleting key\n",
4671 wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
4674 struct ieee80211com *ic = vap->iv_ic;
4675 struct wpi_softc *sc = ic->ic_softc;
4676 struct wpi_vap *wvp = WPI_VAP(vap);
4677 struct ieee80211_node *ni;
4678 int error, ni_ref = 0;
4680 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4682 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
4687 if (!(k->wk_flags & IEEE80211_KEY_RECV)) {
4688 /* XMIT keys are handled in wpi_tx_data(). */
4692 /* Handle group keys. */
4693 if (&vap->iv_nw_keys[0] <= k &&
4694 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4697 wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix);
4699 wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix);
4702 if (vap->iv_state == IEEE80211_S_RUN) {
4703 ieee80211_iterate_nodes(&ic->ic_sta,
4704 set ? wpi_load_key_cb : wpi_del_key_cb,
4705 __DECONST(void *, k));
4711 switch (vap->iv_opmode) {
4712 case IEEE80211_M_STA:
4716 case IEEE80211_M_IBSS:
4717 case IEEE80211_M_AHDEMO:
4718 case IEEE80211_M_HOSTAP:
4719 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
4721 return 0; /* should not happen */
4727 device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__,
4734 error = wpi_load_key(ni, k);
4736 error = wpi_del_key(ni, k);
4740 ieee80211_node_decref(ni);
4746 wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
4748 return wpi_process_key(vap, k, 1);
4752 wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4754 return wpi_process_key(vap, k, 0);
4758 * This function is called after the runtime firmware notifies us of its
4759 * readiness (called in a process context).
4762 wpi_post_alive(struct wpi_softc *sc)
4766 /* Check (again) that the radio is not disabled. */
4767 if ((error = wpi_nic_lock(sc)) != 0)
4770 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4772 /* NB: Runtime firmware must be up and running. */
4773 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
4774 device_printf(sc->sc_dev,
4775 "RF switch: radio disabled (%s)\n", __func__);
4777 return EPERM; /* :-) */
4781 /* Wait for thermal sensor to calibrate. */
4782 for (ntries = 0; ntries < 1000; ntries++) {
4783 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
4788 if (ntries == 1000) {
4789 device_printf(sc->sc_dev,
4790 "timeout waiting for thermal sensor calibration\n");
4794 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp);
4799 * The firmware boot code is small and is intended to be copied directly into
4800 * the NIC internal memory (no DMA transfer).
4803 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
4807 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size);
4809 size /= sizeof (uint32_t);
4811 if ((error = wpi_nic_lock(sc)) != 0)
4814 /* Copy microcode image into NIC memory. */
4815 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
4816 (const uint32_t *)ucode, size);
4818 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
4819 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
4820 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
4822 /* Start boot load now. */
4823 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
4825 /* Wait for transfer to complete. */
4826 for (ntries = 0; ntries < 1000; ntries++) {
4827 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS);
4828 DPRINTF(sc, WPI_DEBUG_HW,
4829 "firmware status=0x%x, val=0x%x, result=0x%x\n", status,
4830 WPI_FH_TX_STATUS_IDLE(6),
4831 status & WPI_FH_TX_STATUS_IDLE(6));
4832 if (status & WPI_FH_TX_STATUS_IDLE(6)) {
4833 DPRINTF(sc, WPI_DEBUG_HW,
4834 "Status Match! - ntries = %d\n", ntries);
4839 if (ntries == 1000) {
4840 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4846 /* Enable boot after power up. */
4847 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
4854 wpi_load_firmware(struct wpi_softc *sc)
4856 struct wpi_fw_info *fw = &sc->fw;
4857 struct wpi_dma_info *dma = &sc->fw_dma;
4860 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4862 /* Copy initialization sections into pre-allocated DMA-safe memory. */
4863 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
4864 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4865 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz);
4866 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4868 /* Tell adapter where to find initialization sections. */
4869 if ((error = wpi_nic_lock(sc)) != 0)
4871 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4872 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
4873 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4874 dma->paddr + WPI_FW_DATA_MAXSZ);
4875 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
4878 /* Load firmware boot code. */
4879 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
4881 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4886 /* Now press "execute". */
4887 WPI_WRITE(sc, WPI_RESET, 0);
4889 /* Wait at most one second for first alive notification. */
4890 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
4891 device_printf(sc->sc_dev,
4892 "%s: timeout waiting for adapter to initialize, error %d\n",
4897 /* Copy runtime sections into pre-allocated DMA-safe memory. */
4898 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
4899 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4900 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz);
4901 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4903 /* Tell adapter where to find runtime sections. */
4904 if ((error = wpi_nic_lock(sc)) != 0)
4906 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4907 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
4908 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4909 dma->paddr + WPI_FW_DATA_MAXSZ);
4910 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
4911 WPI_FW_UPDATED | fw->main.textsz);
4918 wpi_read_firmware(struct wpi_softc *sc)
4920 const struct firmware *fp;
4921 struct wpi_fw_info *fw = &sc->fw;
4922 const struct wpi_firmware_hdr *hdr;
4925 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4927 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4928 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME);
4931 fp = firmware_get(WPI_FW_NAME);
4935 device_printf(sc->sc_dev,
4936 "could not load firmware image '%s'\n", WPI_FW_NAME);
4942 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
4943 device_printf(sc->sc_dev,
4944 "firmware file too short: %zu bytes\n", fp->datasize);
4949 fw->size = fp->datasize;
4950 fw->data = (const uint8_t *)fp->data;
4952 /* Extract firmware header information. */
4953 hdr = (const struct wpi_firmware_hdr *)fw->data;
4955 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW |
4956 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
4958 fw->main.textsz = le32toh(hdr->rtextsz);
4959 fw->main.datasz = le32toh(hdr->rdatasz);
4960 fw->init.textsz = le32toh(hdr->itextsz);
4961 fw->init.datasz = le32toh(hdr->idatasz);
4962 fw->boot.textsz = le32toh(hdr->btextsz);
4963 fw->boot.datasz = 0;
4965 /* Sanity-check firmware header. */
4966 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
4967 fw->main.datasz > WPI_FW_DATA_MAXSZ ||
4968 fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
4969 fw->init.datasz > WPI_FW_DATA_MAXSZ ||
4970 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
4971 (fw->boot.textsz & 3) != 0) {
4972 device_printf(sc->sc_dev, "invalid firmware header\n");
4977 /* Check that all firmware sections fit. */
4978 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
4979 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
4980 device_printf(sc->sc_dev,
4981 "firmware file too short: %zu bytes\n", fw->size);
4986 /* Get pointers to firmware sections. */
4987 fw->main.text = (const uint8_t *)(hdr + 1);
4988 fw->main.data = fw->main.text + fw->main.textsz;
4989 fw->init.text = fw->main.data + fw->main.datasz;
4990 fw->init.data = fw->init.text + fw->init.textsz;
4991 fw->boot.text = fw->init.data + fw->init.datasz;
4993 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4994 "Firmware Version: Major %d, Minor %d, Driver %d, \n"
4995 "runtime (text: %u, data: %u) init (text: %u, data %u) "
4996 "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver),
4997 fw->main.textsz, fw->main.datasz,
4998 fw->init.textsz, fw->init.datasz, fw->boot.textsz);
5000 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text);
5001 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data);
5002 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text);
5003 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data);
5004 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text);
5008 fail: wpi_unload_firmware(sc);
5013 * Free the referenced firmware image
5016 wpi_unload_firmware(struct wpi_softc *sc)
5018 if (sc->fw_fp != NULL) {
5019 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
5025 wpi_clock_wait(struct wpi_softc *sc)
5029 /* Set "initialization complete" bit. */
5030 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5032 /* Wait for clock stabilization. */
5033 for (ntries = 0; ntries < 2500; ntries++) {
5034 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
5038 device_printf(sc->sc_dev,
5039 "%s: timeout waiting for clock stabilization\n", __func__);
5045 wpi_apm_init(struct wpi_softc *sc)
5050 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5052 /* Disable L0s exit timer (NMI bug workaround). */
5053 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER);
5054 /* Don't wait for ICH L0s (ICH bug workaround). */
5055 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
5057 /* Set FH wait threshold to max (HW bug under stress workaround). */
5058 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000);
5060 /* Retrieve PCIe Active State Power Management (ASPM). */
5061 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
5062 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
5063 if (reg & 0x02) /* L1 Entry enabled. */
5064 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5066 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5068 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
5070 /* Wait for clock stabilization before accessing prph. */
5071 if ((error = wpi_clock_wait(sc)) != 0)
5074 if ((error = wpi_nic_lock(sc)) != 0)
5077 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400);
5078 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200);
5080 /* Enable DMA and BSM (Bootstrap State Machine). */
5081 wpi_prph_write(sc, WPI_APMG_CLK_EN,
5082 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT);
5084 /* Disable L1-Active. */
5085 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
5092 wpi_apm_stop_master(struct wpi_softc *sc)
5096 /* Stop busmaster DMA activity. */
5097 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
5099 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
5100 WPI_GP_CNTRL_MAC_PS)
5101 return; /* Already asleep. */
5103 for (ntries = 0; ntries < 100; ntries++) {
5104 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
5108 device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
5113 wpi_apm_stop(struct wpi_softc *sc)
5115 wpi_apm_stop_master(sc);
5117 /* Reset the entire device. */
5118 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
5120 /* Clear "initialization complete" bit. */
5121 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5125 wpi_nic_config(struct wpi_softc *sc)
5129 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5131 /* voodoo from the Linux "driver".. */
5132 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
5133 if ((rev & 0xc0) == 0x40)
5134 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
5135 else if (!(rev & 0x80))
5136 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
5138 if (sc->cap == 0x80)
5139 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
5141 if ((sc->rev & 0xf0) == 0xd0)
5142 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5144 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5147 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
5151 wpi_hw_init(struct wpi_softc *sc)
5153 int chnl, ntries, error;
5155 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5157 /* Clear pending interrupts. */
5158 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5160 if ((error = wpi_apm_init(sc)) != 0) {
5161 device_printf(sc->sc_dev,
5162 "%s: could not power ON adapter, error %d\n", __func__,
5167 /* Select VMAIN power source. */
5168 if ((error = wpi_nic_lock(sc)) != 0)
5170 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
5172 /* Spin until VMAIN gets selected. */
5173 for (ntries = 0; ntries < 5000; ntries++) {
5174 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
5178 if (ntries == 5000) {
5179 device_printf(sc->sc_dev, "timeout selecting power source\n");
5183 /* Perform adapter initialization. */
5186 /* Initialize RX ring. */
5187 if ((error = wpi_nic_lock(sc)) != 0)
5189 /* Set physical address of RX ring. */
5190 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
5191 /* Set physical address of RX read pointer. */
5192 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
5193 offsetof(struct wpi_shared, next));
5194 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
5196 WPI_WRITE(sc, WPI_FH_RX_CONFIG,
5197 WPI_FH_RX_CONFIG_DMA_ENA |
5198 WPI_FH_RX_CONFIG_RDRBD_ENA |
5199 WPI_FH_RX_CONFIG_WRSTATUS_ENA |
5200 WPI_FH_RX_CONFIG_MAXFRAG |
5201 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
5202 WPI_FH_RX_CONFIG_IRQ_DST_HOST |
5203 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1));
5204 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */
5206 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
5208 /* Initialize TX rings. */
5209 if ((error = wpi_nic_lock(sc)) != 0)
5211 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */
5212 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */
5213 /* Enable all 6 TX rings. */
5214 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
5215 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
5216 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
5217 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
5218 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
5219 /* Set physical address of TX rings. */
5220 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
5221 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
5223 /* Enable all DMA channels. */
5224 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5225 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
5226 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
5227 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
5230 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */
5232 /* Clear "radio off" and "commands blocked" bits. */
5233 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5234 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
5236 /* Clear pending interrupts. */
5237 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5238 /* Enable interrupts. */
5239 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
5241 /* _Really_ make sure "radio off" bit is cleared! */
5242 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5243 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5245 if ((error = wpi_load_firmware(sc)) != 0) {
5246 device_printf(sc->sc_dev,
5247 "%s: could not load firmware, error %d\n", __func__,
5251 /* Wait at most one second for firmware alive notification. */
5252 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
5253 device_printf(sc->sc_dev,
5254 "%s: timeout waiting for adapter to initialize, error %d\n",
5259 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5261 /* Do post-firmware initialization. */
5262 return wpi_post_alive(sc);
5266 wpi_hw_stop(struct wpi_softc *sc)
5268 int chnl, qid, ntries;
5270 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5272 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP)
5275 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
5277 /* Disable interrupts. */
5278 WPI_WRITE(sc, WPI_INT_MASK, 0);
5279 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5280 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
5282 /* Make sure we no longer hold the NIC lock. */
5285 if (wpi_nic_lock(sc) == 0) {
5286 /* Stop TX scheduler. */
5287 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
5288 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
5290 /* Stop all DMA channels. */
5291 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5292 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
5293 for (ntries = 0; ntries < 200; ntries++) {
5294 if (WPI_READ(sc, WPI_FH_TX_STATUS) &
5295 WPI_FH_TX_STATUS_IDLE(chnl))
5304 wpi_reset_rx_ring(sc);
5306 /* Reset all TX rings. */
5307 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
5308 wpi_reset_tx_ring(sc, &sc->txq[qid]);
5310 if (wpi_nic_lock(sc) == 0) {
5311 wpi_prph_write(sc, WPI_APMG_CLK_DIS,
5312 WPI_APMG_CLK_CTRL_DMA_CLK_RQT);
5316 /* Power OFF adapter. */
5321 wpi_radio_on(void *arg0, int pending)
5323 struct wpi_softc *sc = arg0;
5324 struct ieee80211com *ic = &sc->sc_ic;
5325 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5327 device_printf(sc->sc_dev, "RF switch: radio enabled\n");
5330 callout_stop(&sc->watchdog_rfkill);
5334 ieee80211_init(vap);
5338 wpi_radio_off(void *arg0, int pending)
5340 struct wpi_softc *sc = arg0;
5341 struct ieee80211com *ic = &sc->sc_ic;
5342 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5344 device_printf(sc->sc_dev, "RF switch: radio disabled\n");
5346 ieee80211_notify_radio(ic, 0);
5349 ieee80211_stop(vap);
5352 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc);
5357 wpi_init(struct wpi_softc *sc)
5363 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5365 if (sc->sc_running != 0)
5368 /* Check that the radio is not disabled by hardware switch. */
5369 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
5370 device_printf(sc->sc_dev,
5371 "RF switch: radio disabled (%s)\n", __func__);
5372 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
5374 error = EINPROGRESS;
5378 /* Read firmware images from the filesystem. */
5379 if ((error = wpi_read_firmware(sc)) != 0) {
5380 device_printf(sc->sc_dev,
5381 "%s: could not read firmware, error %d\n", __func__,
5388 /* Initialize hardware and upload firmware. */
5389 error = wpi_hw_init(sc);
5390 wpi_unload_firmware(sc);
5392 device_printf(sc->sc_dev,
5393 "%s: could not initialize hardware, error %d\n", __func__,
5398 /* Configure adapter now that it is ready. */
5399 if ((error = wpi_config(sc)) != 0) {
5400 device_printf(sc->sc_dev,
5401 "%s: could not configure device, error %d\n", __func__,
5406 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5412 fail: wpi_stop_locked(sc);
5414 end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
5421 wpi_stop_locked(struct wpi_softc *sc)
5424 WPI_LOCK_ASSERT(sc);
5426 if (sc->sc_running == 0)
5435 WPI_TXQ_STATE_LOCK(sc);
5436 callout_stop(&sc->tx_timeout);
5437 WPI_TXQ_STATE_UNLOCK(sc);
5440 callout_stop(&sc->scan_timeout);
5441 callout_stop(&sc->calib_to);
5442 WPI_RXON_UNLOCK(sc);
5444 /* Power OFF hardware. */
5449 wpi_stop(struct wpi_softc *sc)
5452 wpi_stop_locked(sc);
5457 * Callback from net80211 to start a scan.
5460 wpi_scan_start(struct ieee80211com *ic)
5462 struct wpi_softc *sc = ic->ic_softc;
5464 wpi_set_led(sc, WPI_LED_LINK, 20, 2);
5468 * Callback from net80211 to terminate a scan.
5471 wpi_scan_end(struct ieee80211com *ic)
5473 struct wpi_softc *sc = ic->ic_softc;
5474 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5476 if (vap->iv_state == IEEE80211_S_RUN)
5477 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
5481 * Called by the net80211 framework to indicate to the driver
5482 * that the channel should be changed
5485 wpi_set_channel(struct ieee80211com *ic)
5487 const struct ieee80211_channel *c = ic->ic_curchan;
5488 struct wpi_softc *sc = ic->ic_softc;
5491 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5494 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
5495 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
5498 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
5499 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
5503 * Only need to set the channel in Monitor mode. AP scanning and auth
5504 * are already taken care of by their respective firmware commands.
5506 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
5508 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
5509 if (IEEE80211_IS_CHAN_2GHZ(c)) {
5510 sc->rxon.flags |= htole32(WPI_RXON_AUTO |
5513 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO |
5516 if ((error = wpi_send_rxon(sc, 0, 1)) != 0)
5517 device_printf(sc->sc_dev,
5518 "%s: error %d setting channel\n", __func__,
5520 WPI_RXON_UNLOCK(sc);
5525 * Called by net80211 to indicate that we need to scan the current
5526 * channel. The channel is previously be set via the wpi_set_channel
5530 wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
5532 struct ieee80211vap *vap = ss->ss_vap;
5533 struct ieee80211com *ic = vap->iv_ic;
5534 struct wpi_softc *sc = ic->ic_softc;
5538 error = wpi_scan(sc, ic->ic_curchan);
5539 WPI_RXON_UNLOCK(sc);
5541 ieee80211_cancel_scan(vap);
5545 * Called by the net80211 framework to indicate
5546 * the minimum dwell time has been met, terminate the scan.
5547 * We don't actually terminate the scan as the firmware will notify
5548 * us when it's finished and we have no way to interrupt it.
5551 wpi_scan_mindwell(struct ieee80211_scan_state *ss)
5553 /* NB: don't try to abort scan; wait for firmware to finish */
5557 wpi_hw_reset(void *arg, int pending)
5559 struct wpi_softc *sc = arg;
5560 struct ieee80211com *ic = &sc->sc_ic;
5561 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5563 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5565 ieee80211_notify_radio(ic, 0);
5566 if (vap != NULL && (ic->ic_flags & IEEE80211_F_SCAN))
5567 ieee80211_cancel_scan(vap);
5571 ieee80211_stop(vap);
5572 ieee80211_init(vap);