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_reset_rx_ring(struct wpi_softc *);
156 static void wpi_free_rx_ring(struct wpi_softc *);
157 static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
159 static void wpi_update_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
160 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
161 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
162 static int wpi_read_eeprom(struct wpi_softc *,
163 uint8_t macaddr[IEEE80211_ADDR_LEN]);
164 static uint32_t wpi_eeprom_channel_flags(struct wpi_eeprom_chan *);
165 static void wpi_read_eeprom_band(struct wpi_softc *, int);
166 static int wpi_read_eeprom_channels(struct wpi_softc *, int);
167 static struct wpi_eeprom_chan *wpi_find_eeprom_channel(struct wpi_softc *,
168 struct ieee80211_channel *);
169 static int wpi_setregdomain(struct ieee80211com *,
170 struct ieee80211_regdomain *, int,
171 struct ieee80211_channel[]);
172 static int wpi_read_eeprom_group(struct wpi_softc *, int);
173 static int wpi_add_node_entry_adhoc(struct wpi_softc *);
174 static void wpi_node_free(struct ieee80211_node *);
175 static struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
176 const uint8_t mac[IEEE80211_ADDR_LEN]);
177 static int wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
178 static void wpi_calib_timeout(void *);
179 static void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *,
180 struct wpi_rx_data *);
181 static void wpi_rx_statistics(struct wpi_softc *, struct wpi_rx_desc *,
182 struct wpi_rx_data *);
183 static void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *);
184 static void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *);
185 static void wpi_notif_intr(struct wpi_softc *);
186 static void wpi_wakeup_intr(struct wpi_softc *);
188 static void wpi_debug_registers(struct wpi_softc *);
190 static void wpi_fatal_intr(struct wpi_softc *);
191 static void wpi_intr(void *);
192 static int wpi_cmd2(struct wpi_softc *, struct wpi_buf *);
193 static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
194 struct ieee80211_node *);
195 static int wpi_tx_data_raw(struct wpi_softc *, struct mbuf *,
196 struct ieee80211_node *,
197 const struct ieee80211_bpf_params *);
198 static int wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
199 const struct ieee80211_bpf_params *);
200 static void wpi_start(struct ifnet *);
201 static void wpi_start_task(void *, int);
202 static void wpi_watchdog_rfkill(void *);
203 static void wpi_scan_timeout(void *);
204 static void wpi_tx_timeout(void *);
205 static int wpi_ioctl(struct ifnet *, u_long, caddr_t);
206 static int wpi_cmd(struct wpi_softc *, int, const void *, size_t, int);
207 static int wpi_mrr_setup(struct wpi_softc *);
208 static int wpi_add_node(struct wpi_softc *, struct ieee80211_node *);
209 static int wpi_add_broadcast_node(struct wpi_softc *, int);
210 static int wpi_add_ibss_node(struct wpi_softc *, struct ieee80211_node *);
211 static void wpi_del_node(struct wpi_softc *, struct ieee80211_node *);
212 static int wpi_updateedca(struct ieee80211com *);
213 static void wpi_set_promisc(struct wpi_softc *);
214 static void wpi_update_promisc(struct ifnet *);
215 static void wpi_update_mcast(struct ifnet *);
216 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
217 static int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *);
218 static void wpi_power_calibration(struct wpi_softc *);
219 static int wpi_set_txpower(struct wpi_softc *, int);
220 static int wpi_get_power_index(struct wpi_softc *,
221 struct wpi_power_group *, uint8_t, int, int);
222 static int wpi_set_pslevel(struct wpi_softc *, uint8_t, int, int);
223 static int wpi_send_btcoex(struct wpi_softc *);
224 static int wpi_send_rxon(struct wpi_softc *, int, int);
225 static int wpi_config(struct wpi_softc *);
226 static uint16_t wpi_get_active_dwell_time(struct wpi_softc *,
227 struct ieee80211_channel *, uint8_t);
228 static uint16_t wpi_limit_dwell(struct wpi_softc *, uint16_t);
229 static uint16_t wpi_get_passive_dwell_time(struct wpi_softc *,
230 struct ieee80211_channel *);
231 static int wpi_scan(struct wpi_softc *, struct ieee80211_channel *);
232 static int wpi_auth(struct wpi_softc *, struct ieee80211vap *);
233 static int wpi_config_beacon(struct wpi_vap *);
234 static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
235 static void wpi_update_beacon(struct ieee80211vap *, int);
236 static void wpi_newassoc(struct ieee80211_node *, int);
237 static int wpi_run(struct wpi_softc *, struct ieee80211vap *);
238 static int wpi_load_key(struct ieee80211_node *,
239 const struct ieee80211_key *);
240 static void wpi_load_key_cb(void *, struct ieee80211_node *);
241 static int wpi_set_global_keys(struct ieee80211_node *);
242 static int wpi_del_key(struct ieee80211_node *,
243 const struct ieee80211_key *);
244 static void wpi_del_key_cb(void *, struct ieee80211_node *);
245 static int wpi_process_key(struct ieee80211vap *,
246 const struct ieee80211_key *, int);
247 static int wpi_key_set(struct ieee80211vap *,
248 const struct ieee80211_key *,
249 const uint8_t mac[IEEE80211_ADDR_LEN]);
250 static int wpi_key_delete(struct ieee80211vap *,
251 const struct ieee80211_key *);
252 static int wpi_post_alive(struct wpi_softc *);
253 static int wpi_load_bootcode(struct wpi_softc *, const uint8_t *, int);
254 static int wpi_load_firmware(struct wpi_softc *);
255 static int wpi_read_firmware(struct wpi_softc *);
256 static void wpi_unload_firmware(struct wpi_softc *);
257 static int wpi_clock_wait(struct wpi_softc *);
258 static int wpi_apm_init(struct wpi_softc *);
259 static void wpi_apm_stop_master(struct wpi_softc *);
260 static void wpi_apm_stop(struct wpi_softc *);
261 static void wpi_nic_config(struct wpi_softc *);
262 static int wpi_hw_init(struct wpi_softc *);
263 static void wpi_hw_stop(struct wpi_softc *);
264 static void wpi_radio_on(void *, int);
265 static void wpi_radio_off(void *, int);
266 static void wpi_init(void *);
267 static void wpi_stop_locked(struct wpi_softc *);
268 static void wpi_stop(struct wpi_softc *);
269 static void wpi_scan_start(struct ieee80211com *);
270 static void wpi_scan_end(struct ieee80211com *);
271 static void wpi_set_channel(struct ieee80211com *);
272 static void wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
273 static void wpi_scan_mindwell(struct ieee80211_scan_state *);
274 static void wpi_hw_reset(void *, int);
276 static device_method_t wpi_methods[] = {
277 /* Device interface */
278 DEVMETHOD(device_probe, wpi_probe),
279 DEVMETHOD(device_attach, wpi_attach),
280 DEVMETHOD(device_detach, wpi_detach),
281 DEVMETHOD(device_shutdown, wpi_shutdown),
282 DEVMETHOD(device_suspend, wpi_suspend),
283 DEVMETHOD(device_resume, wpi_resume),
288 static driver_t wpi_driver = {
291 sizeof (struct wpi_softc)
293 static devclass_t wpi_devclass;
295 DRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, NULL, NULL);
297 MODULE_VERSION(wpi, 1);
299 MODULE_DEPEND(wpi, pci, 1, 1, 1);
300 MODULE_DEPEND(wpi, wlan, 1, 1, 1);
301 MODULE_DEPEND(wpi, firmware, 1, 1, 1);
304 wpi_probe(device_t dev)
306 const struct wpi_ident *ident;
308 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
309 if (pci_get_vendor(dev) == ident->vendor &&
310 pci_get_device(dev) == ident->device) {
311 device_set_desc(dev, ident->name);
312 return (BUS_PROBE_DEFAULT);
319 wpi_attach(device_t dev)
321 struct wpi_softc *sc = (struct wpi_softc *)device_get_softc(dev);
322 struct ieee80211com *ic;
327 const struct wpi_ident *ident;
329 uint8_t macaddr[IEEE80211_ADDR_LEN];
334 error = resource_int_value(device_get_name(sc->sc_dev),
335 device_get_unit(sc->sc_dev), "debug", &(sc->sc_debug));
342 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
345 * Get the offset of the PCI Express Capability Structure in PCI
346 * Configuration Space.
348 error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off);
350 device_printf(dev, "PCIe capability structure not found!\n");
355 * Some card's only support 802.11b/g not a, check to see if
356 * this is one such card. A 0x0 in the subdevice table indicates
357 * the entire subdevice range is to be ignored.
360 for (ident = wpi_ident_table; ident->name != NULL; ident++) {
361 if (ident->subdevice &&
362 pci_get_subdevice(dev) == ident->subdevice) {
369 /* Clear device-specific "PCI retry timeout" register (41h). */
370 pci_write_config(dev, 0x41, 0, 1);
372 /* Enable bus-mastering. */
373 pci_enable_busmaster(dev);
376 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
378 if (sc->mem == NULL) {
379 device_printf(dev, "can't map mem space\n");
382 sc->sc_st = rman_get_bustag(sc->mem);
383 sc->sc_sh = rman_get_bushandle(sc->mem);
387 if (pci_alloc_msi(dev, &i) == 0)
389 /* Install interrupt handler. */
390 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
391 (rid != 0 ? 0 : RF_SHAREABLE));
392 if (sc->irq == NULL) {
393 device_printf(dev, "can't map interrupt\n");
399 WPI_TX_LOCK_INIT(sc);
400 WPI_RXON_LOCK_INIT(sc);
401 WPI_NT_LOCK_INIT(sc);
402 WPI_TXQ_LOCK_INIT(sc);
403 WPI_TXQ_STATE_LOCK_INIT(sc);
405 /* Allocate DMA memory for firmware transfers. */
406 if ((error = wpi_alloc_fwmem(sc)) != 0) {
408 "could not allocate memory for firmware, error %d\n",
413 /* Allocate shared page. */
414 if ((error = wpi_alloc_shared(sc)) != 0) {
415 device_printf(dev, "could not allocate shared page\n");
419 /* Allocate TX rings - 4 for QoS purposes, 1 for commands. */
420 for (i = 0; i < WPI_NTXQUEUES; i++) {
421 if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) {
423 "could not allocate TX ring %d, error %d\n", i,
429 /* Allocate RX ring. */
430 if ((error = wpi_alloc_rx_ring(sc)) != 0) {
431 device_printf(dev, "could not allocate RX ring, error %d\n",
436 /* Clear pending interrupts. */
437 WPI_WRITE(sc, WPI_INT, 0xffffffff);
439 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
441 device_printf(dev, "can not allocate ifnet structure\n");
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, 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 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
497 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
498 ifp->if_init = wpi_init;
499 ifp->if_ioctl = wpi_ioctl;
500 ifp->if_start = wpi_start;
501 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
502 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
503 IFQ_SET_READY(&ifp->if_snd);
505 ieee80211_ifattach(ic, macaddr);
506 ic->ic_vap_create = wpi_vap_create;
507 ic->ic_vap_delete = wpi_vap_delete;
508 ic->ic_raw_xmit = wpi_raw_xmit;
509 ic->ic_node_alloc = wpi_node_alloc;
510 sc->sc_node_free = ic->ic_node_free;
511 ic->ic_node_free = wpi_node_free;
512 ic->ic_wme.wme_update = wpi_updateedca;
513 ic->ic_update_promisc = wpi_update_promisc;
514 ic->ic_update_mcast = wpi_update_mcast;
515 ic->ic_newassoc = wpi_newassoc;
516 ic->ic_scan_start = wpi_scan_start;
517 ic->ic_scan_end = wpi_scan_end;
518 ic->ic_set_channel = wpi_set_channel;
519 ic->ic_scan_curchan = wpi_scan_curchan;
520 ic->ic_scan_mindwell = wpi_scan_mindwell;
521 ic->ic_setregdomain = wpi_setregdomain;
523 wpi_radiotap_attach(sc);
525 callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0);
526 callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0);
527 callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0);
528 callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0);
529 TASK_INIT(&sc->sc_reinittask, 0, wpi_hw_reset, sc);
530 TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc);
531 TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc);
532 TASK_INIT(&sc->sc_start_task, 0, wpi_start_task, sc);
534 sc->sc_tq = taskqueue_create("wpi_taskq", M_WAITOK,
535 taskqueue_thread_enqueue, &sc->sc_tq);
536 error = taskqueue_start_threads(&sc->sc_tq, 1, 0, "wpi_taskq");
538 device_printf(dev, "can't start threads, error %d\n", error);
542 wpi_sysctlattach(sc);
545 * Hook our interrupt after all initialization is complete.
547 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
548 NULL, wpi_intr, sc, &sc->sc_ih);
550 device_printf(dev, "can't establish interrupt, error %d\n",
556 ieee80211_announce(ic);
559 if (sc->sc_debug & WPI_DEBUG_HW)
560 ieee80211_announce_channels(ic);
563 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
566 fail: wpi_detach(dev);
567 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
572 * Attach the interface to 802.11 radiotap.
575 wpi_radiotap_attach(struct wpi_softc *sc)
577 struct ifnet *ifp = sc->sc_ifp;
578 struct ieee80211com *ic = ifp->if_l2com;
579 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
580 ieee80211_radiotap_attach(ic,
581 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
582 WPI_TX_RADIOTAP_PRESENT,
583 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
584 WPI_RX_RADIOTAP_PRESENT);
585 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
589 wpi_sysctlattach(struct wpi_softc *sc)
592 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
593 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
595 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
596 "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug,
597 "control debugging printfs");
602 wpi_init_beacon(struct wpi_vap *wvp)
604 struct wpi_buf *bcn = &wvp->wv_bcbuf;
605 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
607 cmd->id = WPI_ID_BROADCAST;
608 cmd->ofdm_mask = 0xff;
609 cmd->cck_mask = 0x0f;
610 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE);
611 cmd->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
613 bcn->code = WPI_CMD_SET_BEACON;
614 bcn->ac = WPI_CMD_QUEUE_NUM;
615 bcn->size = sizeof(struct wpi_cmd_beacon);
618 static struct ieee80211vap *
619 wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
620 enum ieee80211_opmode opmode, int flags,
621 const uint8_t bssid[IEEE80211_ADDR_LEN],
622 const uint8_t mac[IEEE80211_ADDR_LEN])
625 struct ieee80211vap *vap;
627 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
630 wvp = (struct wpi_vap *) malloc(sizeof(struct wpi_vap),
631 M_80211_VAP, M_NOWAIT | M_ZERO);
635 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
637 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
638 WPI_VAP_LOCK_INIT(wvp);
639 wpi_init_beacon(wvp);
642 /* Override with driver methods. */
643 vap->iv_key_set = wpi_key_set;
644 vap->iv_key_delete = wpi_key_delete;
645 wvp->wv_newstate = vap->iv_newstate;
646 vap->iv_newstate = wpi_newstate;
647 vap->iv_update_beacon = wpi_update_beacon;
648 vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1;
650 ieee80211_ratectl_init(vap);
651 /* Complete setup. */
652 ieee80211_vap_attach(vap, ieee80211_media_change,
653 ieee80211_media_status);
654 ic->ic_opmode = opmode;
659 wpi_vap_delete(struct ieee80211vap *vap)
661 struct wpi_vap *wvp = WPI_VAP(vap);
662 struct wpi_buf *bcn = &wvp->wv_bcbuf;
663 enum ieee80211_opmode opmode = vap->iv_opmode;
665 ieee80211_ratectl_deinit(vap);
666 ieee80211_vap_detach(vap);
668 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
672 WPI_VAP_LOCK_DESTROY(wvp);
675 free(wvp, M_80211_VAP);
679 wpi_detach(device_t dev)
681 struct wpi_softc *sc = device_get_softc(dev);
682 struct ifnet *ifp = sc->sc_ifp;
683 struct ieee80211com *ic;
686 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
691 ieee80211_draintask(ic, &sc->sc_radioon_task);
692 ieee80211_draintask(ic, &sc->sc_start_task);
696 taskqueue_drain_all(sc->sc_tq);
697 taskqueue_free(sc->sc_tq);
699 callout_drain(&sc->watchdog_rfkill);
700 callout_drain(&sc->tx_timeout);
701 callout_drain(&sc->scan_timeout);
702 callout_drain(&sc->calib_to);
703 ieee80211_ifdetach(ic);
706 /* Uninstall interrupt handler. */
707 if (sc->irq != NULL) {
708 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
709 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
711 pci_release_msi(dev);
714 if (sc->txq[0].data_dmat) {
715 /* Free DMA resources. */
716 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
717 wpi_free_tx_ring(sc, &sc->txq[qid]);
719 wpi_free_rx_ring(sc);
727 bus_release_resource(dev, SYS_RES_MEMORY,
728 rman_get_rid(sc->mem), sc->mem);
733 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
734 WPI_TXQ_STATE_LOCK_DESTROY(sc);
735 WPI_TXQ_LOCK_DESTROY(sc);
736 WPI_NT_LOCK_DESTROY(sc);
737 WPI_RXON_LOCK_DESTROY(sc);
738 WPI_TX_LOCK_DESTROY(sc);
739 WPI_LOCK_DESTROY(sc);
744 wpi_shutdown(device_t dev)
746 struct wpi_softc *sc = device_get_softc(dev);
753 wpi_suspend(device_t dev)
755 struct wpi_softc *sc = device_get_softc(dev);
756 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
758 ieee80211_suspend_all(ic);
763 wpi_resume(device_t dev)
765 struct wpi_softc *sc = device_get_softc(dev);
766 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
768 /* Clear device-specific "PCI retry timeout" register (41h). */
769 pci_write_config(dev, 0x41, 0, 1);
771 ieee80211_resume_all(ic);
776 * Grab exclusive access to NIC memory.
779 wpi_nic_lock(struct wpi_softc *sc)
783 /* Request exclusive access to NIC. */
784 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
786 /* Spin until we actually get the lock. */
787 for (ntries = 0; ntries < 1000; ntries++) {
788 if ((WPI_READ(sc, WPI_GP_CNTRL) &
789 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
790 WPI_GP_CNTRL_MAC_ACCESS_ENA)
795 device_printf(sc->sc_dev, "could not lock memory\n");
801 * Release lock on NIC memory.
804 wpi_nic_unlock(struct wpi_softc *sc)
806 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
809 static __inline uint32_t
810 wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
812 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
813 WPI_BARRIER_READ_WRITE(sc);
814 return WPI_READ(sc, WPI_PRPH_RDATA);
818 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
820 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
821 WPI_BARRIER_WRITE(sc);
822 WPI_WRITE(sc, WPI_PRPH_WDATA, data);
826 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
828 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
832 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
834 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
838 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
839 const uint32_t *data, int count)
841 for (; count > 0; count--, data++, addr += 4)
842 wpi_prph_write(sc, addr, *data);
845 static __inline uint32_t
846 wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
848 WPI_WRITE(sc, WPI_MEM_RADDR, addr);
849 WPI_BARRIER_READ_WRITE(sc);
850 return WPI_READ(sc, WPI_MEM_RDATA);
854 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
857 for (; count > 0; count--, addr += 4)
858 *data++ = wpi_mem_read(sc, addr);
862 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
868 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
870 if ((error = wpi_nic_lock(sc)) != 0)
873 for (; count > 0; count -= 2, addr++) {
874 WPI_WRITE(sc, WPI_EEPROM, addr << 2);
875 for (ntries = 0; ntries < 10; ntries++) {
876 val = WPI_READ(sc, WPI_EEPROM);
877 if (val & WPI_EEPROM_READ_VALID)
882 device_printf(sc->sc_dev,
883 "timeout reading ROM at 0x%x\n", addr);
893 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
899 wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
903 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
904 *(bus_addr_t *)arg = segs[0].ds_addr;
908 * Allocates a contiguous block of dma memory of the requested size and
912 wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
913 void **kvap, bus_size_t size, bus_size_t alignment)
920 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
921 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
922 1, size, BUS_DMA_NOWAIT, NULL, NULL, &dma->tag);
926 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
927 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
931 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
932 wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
936 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
943 fail: wpi_dma_contig_free(dma);
948 wpi_dma_contig_free(struct wpi_dma_info *dma)
950 if (dma->vaddr != NULL) {
951 bus_dmamap_sync(dma->tag, dma->map,
952 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
953 bus_dmamap_unload(dma->tag, dma->map);
954 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
957 if (dma->tag != NULL) {
958 bus_dma_tag_destroy(dma->tag);
964 * Allocate a shared page between host and NIC.
967 wpi_alloc_shared(struct wpi_softc *sc)
969 /* Shared buffer must be aligned on a 4KB boundary. */
970 return wpi_dma_contig_alloc(sc, &sc->shared_dma,
971 (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
975 wpi_free_shared(struct wpi_softc *sc)
977 wpi_dma_contig_free(&sc->shared_dma);
981 * Allocate DMA-safe memory for firmware transfer.
984 wpi_alloc_fwmem(struct wpi_softc *sc)
986 /* Must be aligned on a 16-byte boundary. */
987 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
988 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
992 wpi_free_fwmem(struct wpi_softc *sc)
994 wpi_dma_contig_free(&sc->fw_dma);
998 wpi_alloc_rx_ring(struct wpi_softc *sc)
1000 struct wpi_rx_ring *ring = &sc->rxq;
1007 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1009 /* Allocate RX descriptors (16KB aligned.) */
1010 size = WPI_RX_RING_COUNT * sizeof (uint32_t);
1011 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1012 (void **)&ring->desc, size, WPI_RING_DMA_ALIGN);
1014 device_printf(sc->sc_dev,
1015 "%s: could not allocate RX ring DMA memory, error %d\n",
1020 /* Create RX buffer DMA tag. */
1021 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1022 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1023 MJUMPAGESIZE, 1, MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL,
1026 device_printf(sc->sc_dev,
1027 "%s: could not create RX buf DMA tag, error %d\n",
1033 * Allocate and map RX buffers.
1035 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1036 struct wpi_rx_data *data = &ring->data[i];
1039 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1041 device_printf(sc->sc_dev,
1042 "%s: could not create RX buf DMA map, error %d\n",
1047 data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1048 if (data->m == NULL) {
1049 device_printf(sc->sc_dev,
1050 "%s: could not allocate RX mbuf\n", __func__);
1055 error = bus_dmamap_load(ring->data_dmat, data->map,
1056 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1057 &paddr, BUS_DMA_NOWAIT);
1058 if (error != 0 && error != EFBIG) {
1059 device_printf(sc->sc_dev,
1060 "%s: can't map mbuf (error %d)\n", __func__,
1065 /* Set physical address of RX buffer. */
1066 ring->desc[i] = htole32(paddr);
1069 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1070 BUS_DMASYNC_PREWRITE);
1072 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1076 fail: wpi_free_rx_ring(sc);
1078 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1084 wpi_update_rx_ring(struct wpi_softc *sc)
1086 struct wpi_rx_ring *ring = &sc->rxq;
1088 if (ring->update != 0) {
1089 /* Wait for INT_WAKEUP event. */
1093 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP) {
1094 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n",
1097 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1100 WPI_WRITE(sc, WPI_FH_RX_WPTR, ring->cur & ~7);
1104 wpi_reset_rx_ring(struct wpi_softc *sc)
1106 struct wpi_rx_ring *ring = &sc->rxq;
1109 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1111 if (wpi_nic_lock(sc) == 0) {
1112 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
1113 for (ntries = 0; ntries < 1000; ntries++) {
1114 if (WPI_READ(sc, WPI_FH_RX_STATUS) &
1115 WPI_FH_RX_STATUS_IDLE)
1127 wpi_free_rx_ring(struct wpi_softc *sc)
1129 struct wpi_rx_ring *ring = &sc->rxq;
1132 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1134 wpi_dma_contig_free(&ring->desc_dma);
1136 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1137 struct wpi_rx_data *data = &ring->data[i];
1139 if (data->m != NULL) {
1140 bus_dmamap_sync(ring->data_dmat, data->map,
1141 BUS_DMASYNC_POSTREAD);
1142 bus_dmamap_unload(ring->data_dmat, data->map);
1146 if (data->map != NULL)
1147 bus_dmamap_destroy(ring->data_dmat, data->map);
1149 if (ring->data_dmat != NULL) {
1150 bus_dma_tag_destroy(ring->data_dmat);
1151 ring->data_dmat = NULL;
1156 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int qid)
1167 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1169 /* Allocate TX descriptors (16KB aligned.) */
1170 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
1171 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc,
1172 size, WPI_RING_DMA_ALIGN);
1174 device_printf(sc->sc_dev,
1175 "%s: could not allocate TX ring DMA memory, error %d\n",
1180 /* Update shared area with ring physical address. */
1181 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1182 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1183 BUS_DMASYNC_PREWRITE);
1186 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
1187 * to allocate commands space for other rings.
1188 * XXX Do we really need to allocate descriptors for other rings?
1190 if (qid > WPI_CMD_QUEUE_NUM) {
1191 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1195 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
1196 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1199 device_printf(sc->sc_dev,
1200 "%s: could not allocate TX cmd DMA memory, error %d\n",
1205 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1206 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1207 WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1210 device_printf(sc->sc_dev,
1211 "%s: could not create TX buf DMA tag, error %d\n",
1216 paddr = ring->cmd_dma.paddr;
1217 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1218 struct wpi_tx_data *data = &ring->data[i];
1220 data->cmd_paddr = paddr;
1221 paddr += sizeof (struct wpi_tx_cmd);
1223 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1225 device_printf(sc->sc_dev,
1226 "%s: could not create TX buf DMA map, error %d\n",
1232 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1236 fail: wpi_free_tx_ring(sc, ring);
1237 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1242 wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1244 if (ring->update != 0) {
1245 /* Wait for INT_WAKEUP event. */
1249 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP) {
1250 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n",
1251 __func__, ring->qid);
1253 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1256 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
1260 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1264 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1266 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1267 struct wpi_tx_data *data = &ring->data[i];
1269 if (data->m != NULL) {
1270 bus_dmamap_sync(ring->data_dmat, data->map,
1271 BUS_DMASYNC_POSTWRITE);
1272 bus_dmamap_unload(ring->data_dmat, data->map);
1277 /* Clear TX descriptors. */
1278 memset(ring->desc, 0, ring->desc_dma.size);
1279 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1280 BUS_DMASYNC_PREWRITE);
1281 sc->qfullmsk &= ~(1 << ring->qid);
1288 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1292 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1294 wpi_dma_contig_free(&ring->desc_dma);
1295 wpi_dma_contig_free(&ring->cmd_dma);
1297 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1298 struct wpi_tx_data *data = &ring->data[i];
1300 if (data->m != NULL) {
1301 bus_dmamap_sync(ring->data_dmat, data->map,
1302 BUS_DMASYNC_POSTWRITE);
1303 bus_dmamap_unload(ring->data_dmat, data->map);
1306 if (data->map != NULL)
1307 bus_dmamap_destroy(ring->data_dmat, data->map);
1309 if (ring->data_dmat != NULL) {
1310 bus_dma_tag_destroy(ring->data_dmat);
1311 ring->data_dmat = NULL;
1316 * Extract various information from EEPROM.
1319 wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1321 #define WPI_CHK(res) do { \
1322 if ((error = res) != 0) \
1327 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1329 /* Adapter has to be powered on for EEPROM access to work. */
1330 if ((error = wpi_apm_init(sc)) != 0) {
1331 device_printf(sc->sc_dev,
1332 "%s: could not power ON adapter, error %d\n", __func__,
1337 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
1338 device_printf(sc->sc_dev, "bad EEPROM signature\n");
1342 /* Clear HW ownership of EEPROM. */
1343 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
1345 /* Read the hardware capabilities, revision and SKU type. */
1346 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap,
1348 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,
1350 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type,
1353 sc->rev = le16toh(sc->rev);
1354 DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap,
1357 /* Read the regulatory domain (4 ASCII characters.) */
1358 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain,
1359 sizeof(sc->domain)));
1361 /* Read MAC address. */
1362 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr,
1363 IEEE80211_ADDR_LEN));
1365 /* Read the list of authorized channels. */
1366 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
1367 WPI_CHK(wpi_read_eeprom_channels(sc, i));
1369 /* Read the list of TX power groups. */
1370 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
1371 WPI_CHK(wpi_read_eeprom_group(sc, i));
1373 fail: wpi_apm_stop(sc); /* Power OFF adapter. */
1375 DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
1383 * Translate EEPROM flags to net80211.
1386 wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel)
1391 if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0)
1392 nflags |= IEEE80211_CHAN_PASSIVE;
1393 if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0)
1394 nflags |= IEEE80211_CHAN_NOADHOC;
1395 if (channel->flags & WPI_EEPROM_CHAN_RADAR) {
1396 nflags |= IEEE80211_CHAN_DFS;
1397 /* XXX apparently IBSS may still be marked */
1398 nflags |= IEEE80211_CHAN_NOADHOC;
1401 /* XXX HOSTAP uses WPI_MODE_IBSS */
1402 if (nflags & IEEE80211_CHAN_NOADHOC)
1403 nflags |= IEEE80211_CHAN_NOHOSTAP;
1409 wpi_read_eeprom_band(struct wpi_softc *sc, int n)
1411 struct ifnet *ifp = sc->sc_ifp;
1412 struct ieee80211com *ic = ifp->if_l2com;
1413 struct wpi_eeprom_chan *channels = sc->eeprom_channels[n];
1414 const struct wpi_chan_band *band = &wpi_bands[n];
1415 struct ieee80211_channel *c;
1419 for (i = 0; i < band->nchan; i++) {
1420 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
1421 DPRINTF(sc, WPI_DEBUG_EEPROM,
1422 "Channel Not Valid: %d, band %d\n",
1427 chan = band->chan[i];
1428 nflags = wpi_eeprom_channel_flags(&channels[i]);
1430 c = &ic->ic_channels[ic->ic_nchans++];
1432 c->ic_maxregpower = channels[i].maxpwr;
1433 c->ic_maxpower = 2*c->ic_maxregpower;
1435 if (n == 0) { /* 2GHz band */
1436 c->ic_freq = ieee80211_ieee2mhz(chan,
1439 /* G =>'s B is supported */
1440 c->ic_flags = IEEE80211_CHAN_B | nflags;
1441 c = &ic->ic_channels[ic->ic_nchans++];
1443 c->ic_flags = IEEE80211_CHAN_G | nflags;
1444 } else { /* 5GHz band */
1445 c->ic_freq = ieee80211_ieee2mhz(chan,
1448 c->ic_flags = IEEE80211_CHAN_A | nflags;
1451 /* Save maximum allowed TX power for this channel. */
1452 sc->maxpwr[chan] = channels[i].maxpwr;
1454 DPRINTF(sc, WPI_DEBUG_EEPROM,
1455 "adding chan %d (%dMHz) flags=0x%x maxpwr=%d passive=%d,"
1456 " offset %d\n", chan, c->ic_freq,
1457 channels[i].flags, sc->maxpwr[chan],
1458 IEEE80211_IS_CHAN_PASSIVE(c), ic->ic_nchans);
1463 * Read the eeprom to find out what channels are valid for the given
1464 * band and update net80211 with what we find.
1467 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
1469 struct ifnet *ifp = sc->sc_ifp;
1470 struct ieee80211com *ic = ifp->if_l2com;
1471 const struct wpi_chan_band *band = &wpi_bands[n];
1474 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1476 error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n],
1477 band->nchan * sizeof (struct wpi_eeprom_chan));
1479 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1483 wpi_read_eeprom_band(sc, n);
1485 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1487 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1492 static struct wpi_eeprom_chan *
1493 wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c)
1497 for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++)
1498 for (i = 0; i < wpi_bands[j].nchan; i++)
1499 if (wpi_bands[j].chan[i] == c->ic_ieee)
1500 return &sc->eeprom_channels[j][i];
1506 * Enforce flags read from EEPROM.
1509 wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
1510 int nchan, struct ieee80211_channel chans[])
1512 struct ifnet *ifp = ic->ic_ifp;
1513 struct wpi_softc *sc = ifp->if_softc;
1516 for (i = 0; i < nchan; i++) {
1517 struct ieee80211_channel *c = &chans[i];
1518 struct wpi_eeprom_chan *channel;
1520 channel = wpi_find_eeprom_channel(sc, c);
1521 if (channel == NULL) {
1522 if_printf(ic->ic_ifp,
1523 "%s: invalid channel %u freq %u/0x%x\n",
1524 __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
1527 c->ic_flags |= wpi_eeprom_channel_flags(channel);
1534 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
1536 struct wpi_power_group *group = &sc->groups[n];
1537 struct wpi_eeprom_group rgroup;
1540 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1542 if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32,
1543 &rgroup, sizeof rgroup)) != 0) {
1544 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1548 /* Save TX power group information. */
1549 group->chan = rgroup.chan;
1550 group->maxpwr = rgroup.maxpwr;
1551 /* Retrieve temperature at which the samples were taken. */
1552 group->temp = (int16_t)le16toh(rgroup.temp);
1554 DPRINTF(sc, WPI_DEBUG_EEPROM,
1555 "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan,
1556 group->maxpwr, group->temp);
1558 for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
1559 group->samples[i].index = rgroup.samples[i].index;
1560 group->samples[i].power = rgroup.samples[i].power;
1562 DPRINTF(sc, WPI_DEBUG_EEPROM,
1563 "\tsample %d: index=%d power=%d\n", i,
1564 group->samples[i].index, group->samples[i].power);
1567 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1573 wpi_add_node_entry_adhoc(struct wpi_softc *sc)
1575 int newid = WPI_ID_IBSS_MIN;
1577 for (; newid <= WPI_ID_IBSS_MAX; newid++) {
1578 if ((sc->nodesmsk & (1 << newid)) == 0) {
1579 sc->nodesmsk |= 1 << newid;
1584 return WPI_ID_UNDEFINED;
1588 wpi_add_node_entry_sta(struct wpi_softc *sc)
1590 sc->nodesmsk |= 1 << WPI_ID_BSS;
1596 wpi_check_node_entry(struct wpi_softc *sc, uint8_t id)
1598 if (id == WPI_ID_UNDEFINED)
1601 return (sc->nodesmsk >> id) & 1;
1604 static __inline void
1605 wpi_clear_node_table(struct wpi_softc *sc)
1610 static __inline void
1611 wpi_del_node_entry(struct wpi_softc *sc, uint8_t id)
1613 sc->nodesmsk &= ~(1 << id);
1616 static struct ieee80211_node *
1617 wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1619 struct wpi_node *wn;
1621 wn = malloc(sizeof (struct wpi_node), M_80211_NODE,
1627 wn->id = WPI_ID_UNDEFINED;
1633 wpi_node_free(struct ieee80211_node *ni)
1635 struct ieee80211com *ic = ni->ni_ic;
1636 struct wpi_softc *sc = ic->ic_ifp->if_softc;
1637 struct wpi_node *wn = WPI_NODE(ni);
1639 if (wn->id != WPI_ID_UNDEFINED) {
1641 if (wpi_check_node_entry(sc, wn->id)) {
1642 wpi_del_node_entry(sc, wn->id);
1643 wpi_del_node(sc, ni);
1648 sc->sc_node_free(ni);
1652 wpi_check_bss_filter(struct wpi_softc *sc)
1654 return (sc->rxon.filter & htole32(WPI_FILTER_BSS)) != 0;
1658 * Called by net80211 when ever there is a change to 80211 state machine
1661 wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1663 struct wpi_vap *wvp = WPI_VAP(vap);
1664 struct ieee80211com *ic = vap->iv_ic;
1665 struct ifnet *ifp = ic->ic_ifp;
1666 struct wpi_softc *sc = ifp->if_softc;
1669 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1671 DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1672 ieee80211_state_name[vap->iv_state],
1673 ieee80211_state_name[nstate]);
1675 if (vap->iv_state == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) {
1676 if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) {
1677 device_printf(sc->sc_dev,
1678 "%s: could not set power saving level\n",
1685 case IEEE80211_S_SCAN:
1687 if (wpi_check_bss_filter(sc) != 0 &&
1688 vap->iv_opmode != IEEE80211_M_STA) {
1689 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1690 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1691 device_printf(sc->sc_dev,
1692 "%s: could not send RXON\n", __func__);
1695 WPI_RXON_UNLOCK(sc);
1698 case IEEE80211_S_ASSOC:
1699 if (vap->iv_state != IEEE80211_S_RUN)
1702 case IEEE80211_S_AUTH:
1704 * The node must be registered in the firmware before auth.
1705 * Also the associd must be cleared on RUN -> ASSOC
1708 if ((error = wpi_auth(sc, vap)) != 0) {
1709 device_printf(sc->sc_dev,
1710 "%s: could not move to AUTH state, error %d\n",
1715 case IEEE80211_S_RUN:
1717 * RUN -> RUN transition; Just restart the timers.
1719 if (vap->iv_state == IEEE80211_S_RUN) {
1721 wpi_calib_timeout(sc);
1722 WPI_RXON_UNLOCK(sc);
1727 * !RUN -> RUN requires setting the association id
1728 * which is done with a firmware cmd. We also defer
1729 * starting the timers until that work is done.
1731 if ((error = wpi_run(sc, vap)) != 0) {
1732 device_printf(sc->sc_dev,
1733 "%s: could not move to RUN state\n", __func__);
1741 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1745 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1747 return wvp->wv_newstate(vap, nstate, arg);
1751 wpi_calib_timeout(void *arg)
1753 struct wpi_softc *sc = arg;
1755 if (wpi_check_bss_filter(sc) == 0)
1758 wpi_power_calibration(sc);
1760 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
1763 static __inline uint8_t
1764 rate2plcp(const uint8_t rate)
1767 case 12: return 0xd;
1768 case 18: return 0xf;
1769 case 24: return 0x5;
1770 case 36: return 0x7;
1771 case 48: return 0x9;
1772 case 72: return 0xb;
1773 case 96: return 0x1;
1774 case 108: return 0x3;
1778 case 22: return 110;
1783 static __inline uint8_t
1784 plcp2rate(const uint8_t plcp)
1787 case 0xd: return 12;
1788 case 0xf: return 18;
1789 case 0x5: return 24;
1790 case 0x7: return 36;
1791 case 0x9: return 48;
1792 case 0xb: return 72;
1793 case 0x1: return 96;
1794 case 0x3: return 108;
1798 case 110: return 22;
1803 /* Quickly determine if a given rate is CCK or OFDM. */
1804 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1807 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1808 struct wpi_rx_data *data)
1810 struct ifnet *ifp = sc->sc_ifp;
1811 struct ieee80211com *ic = ifp->if_l2com;
1812 struct wpi_rx_ring *ring = &sc->rxq;
1813 struct wpi_rx_stat *stat;
1814 struct wpi_rx_head *head;
1815 struct wpi_rx_tail *tail;
1816 struct ieee80211_frame *wh;
1817 struct ieee80211_node *ni;
1818 struct mbuf *m, *m1;
1824 stat = (struct wpi_rx_stat *)(desc + 1);
1826 if (stat->len > WPI_STAT_MAXLEN) {
1827 device_printf(sc->sc_dev, "invalid RX statistic header\n");
1831 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
1832 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1833 len = le16toh(head->len);
1834 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len);
1835 flags = le32toh(tail->flags);
1837 DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d"
1838 " rate %x chan %d tstamp %ju\n", __func__, ring->cur,
1839 le32toh(desc->len), len, (int8_t)stat->rssi,
1840 head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp));
1842 /* Discard frames with a bad FCS early. */
1843 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1844 DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n",
1848 /* Discard frames that are too short. */
1849 if (len < sizeof (*wh)) {
1850 DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n",
1855 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1857 DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n",
1861 bus_dmamap_unload(ring->data_dmat, data->map);
1863 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *),
1864 MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1865 if (error != 0 && error != EFBIG) {
1866 device_printf(sc->sc_dev,
1867 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1870 /* Try to reload the old mbuf. */
1871 error = bus_dmamap_load(ring->data_dmat, data->map,
1872 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1873 &paddr, BUS_DMA_NOWAIT);
1874 if (error != 0 && error != EFBIG) {
1875 panic("%s: could not load old RX mbuf", __func__);
1877 /* Physical address may have changed. */
1878 ring->desc[ring->cur] = htole32(paddr);
1879 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map,
1880 BUS_DMASYNC_PREWRITE);
1886 /* Update RX descriptor. */
1887 ring->desc[ring->cur] = htole32(paddr);
1888 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1889 BUS_DMASYNC_PREWRITE);
1891 /* Finalize mbuf. */
1892 m->m_pkthdr.rcvif = ifp;
1893 m->m_data = (caddr_t)(head + 1);
1894 m->m_pkthdr.len = m->m_len = len;
1896 /* Grab a reference to the source node. */
1897 wh = mtod(m, struct ieee80211_frame *);
1899 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1900 (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) {
1901 /* Check whether decryption was successful or not. */
1902 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
1903 DPRINTF(sc, WPI_DEBUG_RECV,
1904 "CCMP decryption failed 0x%x\n", flags);
1907 m->m_flags |= M_WEP;
1910 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1912 if (ieee80211_radiotap_active(ic)) {
1913 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1916 if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE))
1917 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1918 tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET);
1919 tap->wr_dbm_antnoise = WPI_RSSI_OFFSET;
1920 tap->wr_tsft = tail->tstamp;
1921 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1922 tap->wr_rate = plcp2rate(head->plcp);
1927 /* Send the frame to the 802.11 layer. */
1929 (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET);
1930 /* Node is no longer needed. */
1931 ieee80211_free_node(ni);
1933 (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET);
1941 fail1: if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1945 wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1946 struct wpi_rx_data *data)
1952 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1954 struct ifnet *ifp = sc->sc_ifp;
1955 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1956 struct wpi_tx_data *data = &ring->data[desc->idx];
1957 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1959 struct ieee80211_node *ni;
1960 struct ieee80211vap *vap;
1961 struct ieee80211com *ic;
1962 uint32_t status = le32toh(stat->status);
1963 int ackfailcnt = stat->ackfailcnt / WPI_NTRIES_DEFAULT;
1965 KASSERT(data->ni != NULL, ("no node"));
1966 KASSERT(data->m != NULL, ("no mbuf"));
1968 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1970 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: "
1971 "qid %d idx %d retries %d btkillcnt %d rate %x duration %d "
1972 "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt,
1973 stat->btkillcnt, stat->rate, le32toh(stat->duration), status);
1975 /* Unmap and free mbuf. */
1976 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1977 bus_dmamap_unload(ring->data_dmat, data->map);
1978 m = data->m, data->m = NULL;
1979 ni = data->ni, data->ni = NULL;
1984 * Update rate control statistics for the node.
1986 if (status & WPI_TX_STATUS_FAIL) {
1987 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1988 ieee80211_ratectl_tx_complete(vap, ni,
1989 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL);
1991 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1992 ieee80211_ratectl_tx_complete(vap, ni,
1993 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL);
1996 ieee80211_tx_complete(ni, m, (status & WPI_TX_STATUS_FAIL) != 0);
1998 WPI_TXQ_STATE_LOCK(sc);
2000 if (ring->queued > 0) {
2001 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2003 if (sc->qfullmsk != 0 &&
2004 ring->queued < WPI_TX_RING_LOMARK) {
2005 sc->qfullmsk &= ~(1 << ring->qid);
2006 IF_LOCK(&ifp->if_snd);
2007 if (sc->qfullmsk == 0 &&
2008 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
2009 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2010 IF_UNLOCK(&ifp->if_snd);
2011 ieee80211_runtask(ic, &sc->sc_start_task);
2013 IF_UNLOCK(&ifp->if_snd);
2016 callout_stop(&sc->tx_timeout);
2017 WPI_TXQ_STATE_UNLOCK(sc);
2019 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2023 * Process a "command done" firmware notification. This is where we wakeup
2024 * processes waiting for a synchronous command completion.
2027 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2029 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
2030 struct wpi_tx_data *data;
2032 DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x "
2033 "type %s len %d\n", desc->qid, desc->idx,
2034 desc->flags, wpi_cmd_str(desc->type),
2035 le32toh(desc->len));
2037 if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM)
2038 return; /* Not a command ack. */
2040 KASSERT(ring->queued == 0, ("ring->queued must be 0"));
2042 data = &ring->data[desc->idx];
2044 /* If the command was mapped in an mbuf, free it. */
2045 if (data->m != NULL) {
2046 bus_dmamap_sync(ring->data_dmat, data->map,
2047 BUS_DMASYNC_POSTWRITE);
2048 bus_dmamap_unload(ring->data_dmat, data->map);
2053 wakeup(&ring->cmd[desc->idx]);
2057 wpi_notif_intr(struct wpi_softc *sc)
2059 struct ifnet *ifp = sc->sc_ifp;
2060 struct ieee80211com *ic = ifp->if_l2com;
2061 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2064 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
2065 BUS_DMASYNC_POSTREAD);
2067 hw = le32toh(sc->shared->next);
2068 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
2070 while (sc->rxq.cur != hw) {
2071 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
2073 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2074 struct wpi_rx_desc *desc;
2076 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2077 BUS_DMASYNC_POSTREAD);
2078 desc = mtod(data->m, struct wpi_rx_desc *);
2080 DPRINTF(sc, WPI_DEBUG_NOTIFY,
2081 "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n",
2082 __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags,
2083 desc->type, wpi_cmd_str(desc->type), le32toh(desc->len));
2085 if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) {
2086 /* Reply to a command. */
2087 wpi_cmd_done(sc, desc);
2090 switch (desc->type) {
2092 /* An 802.11 frame has been received. */
2093 wpi_rx_done(sc, desc, data);
2095 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2096 /* wpi_stop() was called. */
2103 /* An 802.11 frame has been transmitted. */
2104 wpi_tx_done(sc, desc);
2107 case WPI_RX_STATISTICS:
2108 case WPI_BEACON_STATISTICS:
2109 wpi_rx_statistics(sc, desc, data);
2112 case WPI_BEACON_MISSED:
2114 struct wpi_beacon_missed *miss =
2115 (struct wpi_beacon_missed *)(desc + 1);
2118 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2119 BUS_DMASYNC_POSTREAD);
2120 misses = le32toh(miss->consecutive);
2122 DPRINTF(sc, WPI_DEBUG_STATE,
2123 "%s: beacons missed %d/%d\n", __func__, misses,
2124 le32toh(miss->total));
2126 if (vap->iv_state == IEEE80211_S_RUN &&
2127 (ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
2128 misses >= vap->iv_bmissthreshold)
2129 ieee80211_beacon_miss(ic);
2135 struct wpi_ucode_info *uc =
2136 (struct wpi_ucode_info *)(desc + 1);
2138 /* The microcontroller is ready. */
2139 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2140 BUS_DMASYNC_POSTREAD);
2141 DPRINTF(sc, WPI_DEBUG_RESET,
2142 "microcode alive notification version=%d.%d "
2143 "subtype=%x alive=%x\n", uc->major, uc->minor,
2144 uc->subtype, le32toh(uc->valid));
2146 if (le32toh(uc->valid) != 1) {
2147 device_printf(sc->sc_dev,
2148 "microcontroller initialization failed\n");
2149 wpi_stop_locked(sc);
2151 /* Save the address of the error log in SRAM. */
2152 sc->errptr = le32toh(uc->errptr);
2155 case WPI_STATE_CHANGED:
2157 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2158 BUS_DMASYNC_POSTREAD);
2160 uint32_t *status = (uint32_t *)(desc + 1);
2162 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n",
2165 if (le32toh(*status) & 1) {
2167 wpi_clear_node_table(sc);
2169 taskqueue_enqueue(sc->sc_tq,
2170 &sc->sc_radiooff_task);
2175 case WPI_START_SCAN:
2177 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2178 BUS_DMASYNC_POSTREAD);
2180 struct wpi_start_scan *scan =
2181 (struct wpi_start_scan *)(desc + 1);
2182 DPRINTF(sc, WPI_DEBUG_SCAN,
2183 "%s: scanning channel %d status %x\n",
2184 __func__, scan->chan, le32toh(scan->status));
2190 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2191 BUS_DMASYNC_POSTREAD);
2193 struct wpi_stop_scan *scan =
2194 (struct wpi_stop_scan *)(desc + 1);
2195 DPRINTF(sc, WPI_DEBUG_SCAN,
2196 "scan finished nchan=%d status=%d chan=%d\n",
2197 scan->nchan, scan->status, scan->chan);
2200 callout_stop(&sc->scan_timeout);
2201 WPI_RXON_UNLOCK(sc);
2202 ieee80211_scan_next(vap);
2207 if (sc->rxq.cur % 8 == 0) {
2208 /* Tell the firmware what we have processed. */
2209 wpi_update_rx_ring(sc);
2215 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2216 * from power-down sleep mode.
2219 wpi_wakeup_intr(struct wpi_softc *sc)
2223 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
2224 "%s: ucode wakeup from power-down sleep\n", __func__);
2226 /* Wakeup RX and TX rings. */
2227 if (sc->rxq.update) {
2229 wpi_update_rx_ring(sc);
2232 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) {
2233 struct wpi_tx_ring *ring = &sc->txq[qid];
2237 wpi_update_tx_ring(sc, ring);
2242 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
2246 * This function prints firmware registers
2250 wpi_debug_registers(struct wpi_softc *sc)
2252 #define COUNTOF(array) (sizeof(array) / sizeof(array[0]))
2254 static const uint32_t csr_tbl[] = {
2271 static const uint32_t prph_tbl[] = {
2278 DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n");
2280 for (i = 0; i < COUNTOF(csr_tbl); i++) {
2281 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2282 wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i]));
2284 if ((i + 1) % 2 == 0)
2285 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2287 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n");
2289 if (wpi_nic_lock(sc) == 0) {
2290 for (i = 0; i < COUNTOF(prph_tbl); i++) {
2291 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2292 wpi_get_prph_string(prph_tbl[i]),
2293 wpi_prph_read(sc, prph_tbl[i]));
2295 if ((i + 1) % 2 == 0)
2296 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2298 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2301 DPRINTF(sc, WPI_DEBUG_REGISTER,
2302 "Cannot access internal registers.\n");
2309 * Dump the error log of the firmware when a firmware panic occurs. Although
2310 * we can't debug the firmware because it is neither open source nor free, it
2311 * can help us to identify certain classes of problems.
2314 wpi_fatal_intr(struct wpi_softc *sc)
2316 struct wpi_fw_dump dump;
2317 uint32_t i, offset, count;
2318 const uint32_t size_errmsg =
2319 (sizeof (wpi_fw_errmsg) / sizeof ((wpi_fw_errmsg)[0]));
2321 /* Check that the error log address is valid. */
2322 if (sc->errptr < WPI_FW_DATA_BASE ||
2323 sc->errptr + sizeof (dump) >
2324 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
2325 printf("%s: bad firmware error log address 0x%08x\n", __func__,
2329 if (wpi_nic_lock(sc) != 0) {
2330 printf("%s: could not read firmware error log\n", __func__);
2333 /* Read number of entries in the log. */
2334 count = wpi_mem_read(sc, sc->errptr);
2335 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
2336 printf("%s: invalid count field (count = %u)\n", __func__,
2341 /* Skip "count" field. */
2342 offset = sc->errptr + sizeof (uint32_t);
2343 printf("firmware error log (count = %u):\n", count);
2344 for (i = 0; i < count; i++) {
2345 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
2346 sizeof (dump) / sizeof (uint32_t));
2348 printf(" error type = \"%s\" (0x%08X)\n",
2349 (dump.desc < size_errmsg) ?
2350 wpi_fw_errmsg[dump.desc] : "UNKNOWN",
2352 printf(" error data = 0x%08X\n",
2354 printf(" branch link = 0x%08X%08X\n",
2355 dump.blink[0], dump.blink[1]);
2356 printf(" interrupt link = 0x%08X%08X\n",
2357 dump.ilink[0], dump.ilink[1]);
2358 printf(" time = %u\n", dump.time);
2360 offset += sizeof (dump);
2363 /* Dump driver status (TX and RX rings) while we're here. */
2364 printf("driver status:\n");
2366 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
2367 struct wpi_tx_ring *ring = &sc->txq[i];
2368 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2369 i, ring->qid, ring->cur, ring->queued);
2372 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2378 struct wpi_softc *sc = arg;
2379 struct ifnet *ifp = sc->sc_ifp;
2384 /* Disable interrupts. */
2385 WPI_WRITE(sc, WPI_INT_MASK, 0);
2387 r1 = WPI_READ(sc, WPI_INT);
2389 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
2390 goto end; /* Hardware gone! */
2392 r2 = WPI_READ(sc, WPI_FH_INT);
2394 DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__,
2397 if (r1 == 0 && r2 == 0)
2398 goto done; /* Interrupt not for us. */
2400 /* Acknowledge interrupts. */
2401 WPI_WRITE(sc, WPI_INT, r1);
2402 WPI_WRITE(sc, WPI_FH_INT, r2);
2404 if (r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR)) {
2405 device_printf(sc->sc_dev, "fatal firmware error\n");
2407 wpi_debug_registers(sc);
2410 DPRINTF(sc, WPI_DEBUG_HW,
2411 "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" :
2412 "(Hardware Error)");
2413 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2417 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
2418 (r2 & WPI_FH_INT_RX))
2421 if (r1 & WPI_INT_ALIVE)
2422 wakeup(sc); /* Firmware is alive. */
2424 if (r1 & WPI_INT_WAKEUP)
2425 wpi_wakeup_intr(sc);
2428 /* Re-enable interrupts. */
2429 if (ifp->if_flags & IFF_UP)
2430 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
2432 end: WPI_UNLOCK(sc);
2436 wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf)
2438 struct ifnet *ifp = sc->sc_ifp;
2439 struct ieee80211_frame *wh;
2440 struct wpi_tx_cmd *cmd;
2441 struct wpi_tx_data *data;
2442 struct wpi_tx_desc *desc;
2443 struct wpi_tx_ring *ring;
2445 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER];
2446 int error, i, hdrlen, nsegs, totlen, pad;
2450 KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow"));
2452 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2454 if (sc->txq_active == 0) {
2455 /* wpi_stop() was called */
2460 wh = mtod(buf->m, struct ieee80211_frame *);
2461 hdrlen = ieee80211_anyhdrsize(wh);
2462 totlen = buf->m->m_pkthdr.len;
2465 /* First segment length must be a multiple of 4. */
2466 pad = 4 - (hdrlen & 3);
2470 ring = &sc->txq[buf->ac];
2471 desc = &ring->desc[ring->cur];
2472 data = &ring->data[ring->cur];
2474 /* Prepare TX firmware command. */
2475 cmd = &ring->cmd[ring->cur];
2476 cmd->code = buf->code;
2478 cmd->qid = ring->qid;
2479 cmd->idx = ring->cur;
2481 memcpy(cmd->data, buf->data, buf->size);
2483 /* Save and trim IEEE802.11 header. */
2484 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen);
2485 m_adj(buf->m, hdrlen);
2487 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m,
2488 segs, &nsegs, BUS_DMA_NOWAIT);
2489 if (error != 0 && error != EFBIG) {
2490 device_printf(sc->sc_dev,
2491 "%s: can't map mbuf (error %d)\n", __func__, error);
2495 /* Too many DMA segments, linearize mbuf. */
2496 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1);
2498 device_printf(sc->sc_dev,
2499 "%s: could not defrag mbuf\n", __func__);
2505 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
2506 buf->m, segs, &nsegs, BUS_DMA_NOWAIT);
2508 device_printf(sc->sc_dev,
2509 "%s: can't map mbuf (error %d)\n", __func__,
2515 KASSERT(nsegs < WPI_MAX_SCATTER,
2516 ("too many DMA segments, nsegs (%d) should be less than %d",
2517 nsegs, WPI_MAX_SCATTER));
2522 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
2523 __func__, ring->qid, ring->cur, totlen, nsegs);
2525 /* Fill TX descriptor. */
2526 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs);
2527 /* First DMA segment is used by the TX command. */
2528 desc->segs[0].addr = htole32(data->cmd_paddr);
2529 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad);
2530 /* Other DMA segments are for data payload. */
2532 for (i = 1; i <= nsegs; i++) {
2533 desc->segs[i].addr = htole32(seg->ds_addr);
2534 desc->segs[i].len = htole32(seg->ds_len);
2538 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2539 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2540 BUS_DMASYNC_PREWRITE);
2541 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2542 BUS_DMASYNC_PREWRITE);
2545 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2546 wpi_update_tx_ring(sc, ring);
2548 if (ring->qid < WPI_CMD_QUEUE_NUM) {
2549 /* Mark TX ring as full if we reach a certain threshold. */
2550 WPI_TXQ_STATE_LOCK(sc);
2551 if (++ring->queued > WPI_TX_RING_HIMARK) {
2552 sc->qfullmsk |= 1 << ring->qid;
2554 IF_LOCK(&ifp->if_snd);
2555 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2556 IF_UNLOCK(&ifp->if_snd);
2559 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2560 WPI_TXQ_STATE_UNLOCK(sc);
2563 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2569 fail: m_freem(buf->m);
2571 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
2579 * Construct the data packet for a transmit buffer.
2582 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2584 const struct ieee80211_txparam *tp;
2585 struct ieee80211vap *vap = ni->ni_vap;
2586 struct ieee80211com *ic = ni->ni_ic;
2587 struct wpi_node *wn = WPI_NODE(ni);
2588 struct ieee80211_channel *chan;
2589 struct ieee80211_frame *wh;
2590 struct ieee80211_key *k = NULL;
2591 struct wpi_buf tx_data;
2592 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2596 int ac, error, swcrypt, rate, ismcast, totlen;
2598 wh = mtod(m, struct ieee80211_frame *);
2599 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2600 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2602 /* Select EDCA Access Category and TX ring for this frame. */
2603 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2604 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2605 tid = qos & IEEE80211_QOS_TID;
2610 ac = M_WME_GETAC(m);
2612 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
2613 ni->ni_chan : ic->ic_curchan;
2614 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2616 /* Choose a TX rate index. */
2617 if (type == IEEE80211_FC0_TYPE_MGT)
2618 rate = tp->mgmtrate;
2620 rate = tp->mcastrate;
2621 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2622 rate = tp->ucastrate;
2623 else if (m->m_flags & M_EAPOL)
2624 rate = tp->mgmtrate;
2626 /* XXX pass pktlen */
2627 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2628 rate = ni->ni_txrate;
2631 /* Encrypt the frame if need be. */
2632 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2633 /* Retrieve key for TX. */
2634 k = ieee80211_crypto_encap(ni, m);
2639 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2641 /* 802.11 header may have moved. */
2642 wh = mtod(m, struct ieee80211_frame *);
2644 totlen = m->m_pkthdr.len;
2646 if (ieee80211_radiotap_active_vap(vap)) {
2647 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2650 tap->wt_rate = rate;
2652 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2654 ieee80211_radiotap_tx(vap, m);
2659 /* Unicast frame, check if an ACK is expected. */
2660 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2661 IEEE80211_QOS_ACKPOLICY_NOACK)
2662 flags |= WPI_TX_NEED_ACK;
2665 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2666 flags |= WPI_TX_MORE_FRAG; /* Cannot happen yet. */
2668 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2670 /* NB: Group frames are sent using CCK in 802.11b/g. */
2671 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2672 flags |= WPI_TX_NEED_RTS;
2673 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2674 WPI_RATE_IS_OFDM(rate)) {
2675 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2676 flags |= WPI_TX_NEED_CTS;
2677 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2678 flags |= WPI_TX_NEED_RTS;
2681 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2682 flags |= WPI_TX_FULL_TXOP;
2685 memset(tx, 0, sizeof (struct wpi_cmd_data));
2686 if (type == IEEE80211_FC0_TYPE_MGT) {
2687 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2689 /* Tell HW to set timestamp in probe responses. */
2690 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2691 flags |= WPI_TX_INSERT_TSTAMP;
2692 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2693 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2694 tx->timeout = htole16(3);
2696 tx->timeout = htole16(2);
2699 if (ismcast || type != IEEE80211_FC0_TYPE_DATA)
2700 tx->id = WPI_ID_BROADCAST;
2702 if (wn->id == WPI_ID_UNDEFINED) {
2703 device_printf(sc->sc_dev,
2704 "%s: undefined node id\n", __func__);
2712 if (k != NULL && !swcrypt) {
2713 switch (k->wk_cipher->ic_cipher) {
2714 case IEEE80211_CIPHER_AES_CCM:
2715 tx->security = WPI_CIPHER_CCMP;
2722 memcpy(tx->key, k->wk_key, k->wk_keylen);
2725 tx->len = htole16(totlen);
2726 tx->flags = htole32(flags);
2727 tx->plcp = rate2plcp(rate);
2729 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2730 tx->ofdm_mask = 0xff;
2731 tx->cck_mask = 0x0f;
2733 tx->data_ntries = tp->maxretry;
2737 tx_data.size = sizeof(struct wpi_cmd_data);
2738 tx_data.code = WPI_CMD_TX_DATA;
2741 return wpi_cmd2(sc, &tx_data);
2748 wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m,
2749 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
2751 struct ieee80211vap *vap = ni->ni_vap;
2752 struct ieee80211_key *k = NULL;
2753 struct ieee80211_frame *wh;
2754 struct wpi_buf tx_data;
2755 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2758 int ac, rate, swcrypt, totlen;
2760 wh = mtod(m, struct ieee80211_frame *);
2761 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2763 ac = params->ibp_pri & 3;
2765 /* Choose a TX rate index. */
2766 rate = params->ibp_rate0;
2769 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
2770 flags |= WPI_TX_NEED_ACK;
2771 if (params->ibp_flags & IEEE80211_BPF_RTS)
2772 flags |= WPI_TX_NEED_RTS;
2773 if (params->ibp_flags & IEEE80211_BPF_CTS)
2774 flags |= WPI_TX_NEED_CTS;
2775 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2776 flags |= WPI_TX_FULL_TXOP;
2778 /* Encrypt the frame if need be. */
2779 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
2780 /* Retrieve key for TX. */
2781 k = ieee80211_crypto_encap(ni, m);
2786 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2788 /* 802.11 header may have moved. */
2789 wh = mtod(m, struct ieee80211_frame *);
2791 totlen = m->m_pkthdr.len;
2793 if (ieee80211_radiotap_active_vap(vap)) {
2794 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2797 tap->wt_rate = rate;
2798 if (params->ibp_flags & IEEE80211_BPF_CRYPTO)
2799 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2801 ieee80211_radiotap_tx(vap, m);
2804 memset(tx, 0, sizeof (struct wpi_cmd_data));
2805 if (type == IEEE80211_FC0_TYPE_MGT) {
2806 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2808 /* Tell HW to set timestamp in probe responses. */
2809 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2810 flags |= WPI_TX_INSERT_TSTAMP;
2811 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2812 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2813 tx->timeout = htole16(3);
2815 tx->timeout = htole16(2);
2818 if (k != NULL && !swcrypt) {
2819 switch (k->wk_cipher->ic_cipher) {
2820 case IEEE80211_CIPHER_AES_CCM:
2821 tx->security = WPI_CIPHER_CCMP;
2828 memcpy(tx->key, k->wk_key, k->wk_keylen);
2831 tx->len = htole16(totlen);
2832 tx->flags = htole32(flags);
2833 tx->plcp = rate2plcp(rate);
2834 tx->id = WPI_ID_BROADCAST;
2835 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2836 tx->rts_ntries = params->ibp_try1;
2837 tx->data_ntries = params->ibp_try0;
2841 tx_data.size = sizeof(struct wpi_cmd_data);
2842 tx_data.code = WPI_CMD_TX_DATA;
2845 return wpi_cmd2(sc, &tx_data);
2849 wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2850 const struct ieee80211_bpf_params *params)
2852 struct ieee80211com *ic = ni->ni_ic;
2853 struct ifnet *ifp = ic->ic_ifp;
2854 struct wpi_softc *sc = ifp->if_softc;
2857 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2859 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2860 ieee80211_free_node(ni);
2866 if (params == NULL) {
2868 * Legacy path; interpret frame contents to decide
2869 * precisely how to send the frame.
2871 error = wpi_tx_data(sc, m, ni);
2874 * Caller supplied explicit parameters to use in
2875 * sending the frame.
2877 error = wpi_tx_data_raw(sc, m, ni, params);
2882 /* NB: m is reclaimed on tx failure */
2883 ieee80211_free_node(ni);
2884 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2886 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
2891 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2897 * Process data waiting to be sent on the IFNET output queue
2900 wpi_start(struct ifnet *ifp)
2902 struct wpi_softc *sc = ifp->if_softc;
2903 struct ieee80211_node *ni;
2907 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
2910 IF_LOCK(&ifp->if_snd);
2911 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2912 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
2913 IF_UNLOCK(&ifp->if_snd);
2916 IF_UNLOCK(&ifp->if_snd);
2918 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2921 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2922 if (wpi_tx_data(sc, m, ni) != 0) {
2923 ieee80211_free_node(ni);
2924 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2928 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
2933 wpi_start_task(void *arg0, int pending)
2935 struct wpi_softc *sc = arg0;
2936 struct ifnet *ifp = sc->sc_ifp;
2942 wpi_watchdog_rfkill(void *arg)
2944 struct wpi_softc *sc = arg;
2945 struct ifnet *ifp = sc->sc_ifp;
2946 struct ieee80211com *ic = ifp->if_l2com;
2948 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n");
2950 /* No need to lock firmware memory. */
2951 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) {
2952 /* Radio kill switch is still off. */
2953 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
2956 ieee80211_runtask(ic, &sc->sc_radioon_task);
2960 wpi_scan_timeout(void *arg)
2962 struct wpi_softc *sc = arg;
2963 struct ifnet *ifp = sc->sc_ifp;
2965 if_printf(ifp, "scan timeout\n");
2966 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2970 wpi_tx_timeout(void *arg)
2972 struct wpi_softc *sc = arg;
2973 struct ifnet *ifp = sc->sc_ifp;
2975 if_printf(ifp, "device timeout\n");
2976 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2977 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2981 wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2983 struct wpi_softc *sc = ifp->if_softc;
2984 struct ieee80211com *ic = ifp->if_l2com;
2985 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2986 struct ifreq *ifr = (struct ifreq *) data;
2991 error = ether_ioctl(ifp, cmd, data);
2994 if (ifp->if_flags & IFF_UP) {
2997 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 &&
2999 ieee80211_stop(vap);
3000 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3004 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3014 * Send a command to the firmware.
3017 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, size_t size,
3020 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
3021 struct wpi_tx_desc *desc;
3022 struct wpi_tx_data *data;
3023 struct wpi_tx_cmd *cmd;
3030 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3032 if (sc->txq_active == 0) {
3033 /* wpi_stop() was called */
3039 WPI_LOCK_ASSERT(sc);
3041 DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %zu async %d\n",
3042 __func__, wpi_cmd_str(code), size, async);
3044 desc = &ring->desc[ring->cur];
3045 data = &ring->data[ring->cur];
3048 if (size > sizeof cmd->data) {
3049 /* Command is too large to fit in a descriptor. */
3050 if (totlen > MCLBYTES) {
3054 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3059 cmd = mtod(m, struct wpi_tx_cmd *);
3060 error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3061 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3068 cmd = &ring->cmd[ring->cur];
3069 paddr = data->cmd_paddr;
3074 cmd->qid = ring->qid;
3075 cmd->idx = ring->cur;
3076 memcpy(cmd->data, buf, size);
3078 desc->nsegs = 1 + (WPI_PAD32(size) << 4);
3079 desc->segs[0].addr = htole32(paddr);
3080 desc->segs[0].len = htole32(totlen);
3082 if (size > sizeof cmd->data) {
3083 bus_dmamap_sync(ring->data_dmat, data->map,
3084 BUS_DMASYNC_PREWRITE);
3086 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3087 BUS_DMASYNC_PREWRITE);
3089 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3090 BUS_DMASYNC_PREWRITE);
3092 /* Kick command ring. */
3093 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
3094 wpi_update_tx_ring(sc, ring);
3096 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3103 return mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
3105 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3113 * Configure HW multi-rate retries.
3116 wpi_mrr_setup(struct wpi_softc *sc)
3118 struct ifnet *ifp = sc->sc_ifp;
3119 struct ieee80211com *ic = ifp->if_l2com;
3120 struct wpi_mrr_setup mrr;
3123 /* CCK rates (not used with 802.11a). */
3124 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
3125 mrr.rates[i].flags = 0;
3126 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3127 /* Fallback to the immediate lower CCK rate (if any.) */
3129 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
3130 /* Try twice at this rate before falling back to "next". */
3131 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3133 /* OFDM rates (not used with 802.11b). */
3134 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
3135 mrr.rates[i].flags = 0;
3136 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3137 /* Fallback to the immediate lower rate (if any.) */
3138 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
3139 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
3140 ((ic->ic_curmode == IEEE80211_MODE_11A) ?
3141 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
3143 /* Try twice at this rate before falling back to "next". */
3144 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3146 /* Setup MRR for control frames. */
3147 mrr.which = htole32(WPI_MRR_CTL);
3148 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3150 device_printf(sc->sc_dev,
3151 "could not setup MRR for control frames\n");
3154 /* Setup MRR for data frames. */
3155 mrr.which = htole32(WPI_MRR_DATA);
3156 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3158 device_printf(sc->sc_dev,
3159 "could not setup MRR for data frames\n");
3166 wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3168 struct ieee80211com *ic = ni->ni_ic;
3169 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
3170 struct wpi_node *wn = WPI_NODE(ni);
3171 struct wpi_node_info node;
3174 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3176 if (wn->id == WPI_ID_UNDEFINED)
3179 memset(&node, 0, sizeof node);
3180 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3182 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3183 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3184 node.action = htole32(WPI_ACTION_SET_RATE);
3185 node.antenna = WPI_ANTENNA_BOTH;
3187 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding node %d (%s)\n", __func__,
3188 wn->id, ether_sprintf(ni->ni_macaddr));
3190 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
3192 device_printf(sc->sc_dev,
3193 "%s: wpi_cmd() call failed with error code %d\n", __func__,
3198 if (wvp->wv_gtk != 0) {
3199 error = wpi_set_global_keys(ni);
3201 device_printf(sc->sc_dev,
3202 "%s: error while setting global keys\n", __func__);
3211 * Broadcast node is used to send group-addressed and management frames.
3214 wpi_add_broadcast_node(struct wpi_softc *sc, int async)
3216 struct ifnet *ifp = sc->sc_ifp;
3217 struct ieee80211com *ic = ifp->if_l2com;
3218 struct wpi_node_info node;
3220 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3222 memset(&node, 0, sizeof node);
3223 IEEE80211_ADDR_COPY(node.macaddr, ifp->if_broadcastaddr);
3224 node.id = WPI_ID_BROADCAST;
3225 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3226 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3227 node.action = htole32(WPI_ACTION_SET_RATE);
3228 node.antenna = WPI_ANTENNA_BOTH;
3230 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__);
3232 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async);
3236 wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3238 struct wpi_node *wn = WPI_NODE(ni);
3241 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3243 wn->id = wpi_add_node_entry_sta(sc);
3245 if ((error = wpi_add_node(sc, ni)) != 0) {
3246 wpi_del_node_entry(sc, wn->id);
3247 wn->id = WPI_ID_UNDEFINED;
3255 wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3257 struct wpi_node *wn = WPI_NODE(ni);
3260 KASSERT(wn->id == WPI_ID_UNDEFINED,
3261 ("the node %d was added before", wn->id));
3263 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3265 if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) {
3266 device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__);
3270 if ((error = wpi_add_node(sc, ni)) != 0) {
3271 wpi_del_node_entry(sc, wn->id);
3272 wn->id = WPI_ID_UNDEFINED;
3280 wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3282 struct wpi_node *wn = WPI_NODE(ni);
3283 struct wpi_cmd_del_node node;
3286 KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed"));
3288 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3290 memset(&node, 0, sizeof node);
3291 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3294 DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__,
3295 wn->id, ether_sprintf(ni->ni_macaddr));
3297 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1);
3299 device_printf(sc->sc_dev,
3300 "%s: could not delete node %u, error %d\n", __func__,
3306 wpi_updateedca(struct ieee80211com *ic)
3308 #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3309 struct wpi_softc *sc = ic->ic_ifp->if_softc;
3310 struct wpi_edca_params cmd;
3313 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3315 memset(&cmd, 0, sizeof cmd);
3316 cmd.flags = htole32(WPI_EDCA_UPDATE);
3317 for (aci = 0; aci < WME_NUM_AC; aci++) {
3318 const struct wmeParams *ac =
3319 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci];
3320 cmd.ac[aci].aifsn = ac->wmep_aifsn;
3321 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin));
3322 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax));
3323 cmd.ac[aci].txoplimit =
3324 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit));
3326 DPRINTF(sc, WPI_DEBUG_EDCA,
3327 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
3328 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn,
3329 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax,
3330 cmd.ac[aci].txoplimit);
3332 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3334 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3341 wpi_set_promisc(struct wpi_softc *sc)
3343 struct ifnet *ifp = sc->sc_ifp;
3344 struct ieee80211com *ic = ifp->if_l2com;
3345 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3346 uint32_t promisc_filter;
3348 promisc_filter = WPI_FILTER_CTL;
3349 if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP)
3350 promisc_filter |= WPI_FILTER_PROMISC;
3352 if (ifp->if_flags & IFF_PROMISC)
3353 sc->rxon.filter |= htole32(promisc_filter);
3355 sc->rxon.filter &= ~htole32(promisc_filter);
3359 wpi_update_promisc(struct ifnet *ifp)
3361 struct wpi_softc *sc = ifp->if_softc;
3364 wpi_set_promisc(sc);
3366 if (wpi_send_rxon(sc, 1, 1) != 0) {
3367 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3370 WPI_RXON_UNLOCK(sc);
3374 wpi_update_mcast(struct ifnet *ifp)
3380 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3382 struct wpi_cmd_led led;
3384 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3387 led.unit = htole32(100000); /* on/off in unit of 100ms */
3390 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
3394 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
3396 struct wpi_cmd_timing cmd;
3399 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3401 memset(&cmd, 0, sizeof cmd);
3402 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3403 cmd.bintval = htole16(ni->ni_intval);
3404 cmd.lintval = htole16(10);
3406 /* Compute remaining time until next beacon. */
3407 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU;
3408 mod = le64toh(cmd.tstamp) % val;
3409 cmd.binitval = htole32((uint32_t)(val - mod));
3411 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3412 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
3414 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
3418 * This function is called periodically (every 60 seconds) to adjust output
3419 * power to temperature changes.
3422 wpi_power_calibration(struct wpi_softc *sc)
3426 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3428 /* Update sensor data. */
3429 temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
3430 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp);
3432 /* Sanity-check read value. */
3433 if (temp < -260 || temp > 25) {
3434 /* This can't be correct, ignore. */
3435 DPRINTF(sc, WPI_DEBUG_TEMP,
3436 "out-of-range temperature reported: %d\n", temp);
3440 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp);
3442 /* Adjust Tx power if need be. */
3443 if (abs(temp - sc->temp) <= 6)
3448 if (wpi_set_txpower(sc, 1) != 0) {
3449 /* just warn, too bad for the automatic calibration... */
3450 device_printf(sc->sc_dev,"could not adjust Tx power\n");
3455 * Set TX power for current channel.
3458 wpi_set_txpower(struct wpi_softc *sc, int async)
3460 struct wpi_power_group *group;
3461 struct wpi_cmd_txpower cmd;
3463 int idx, is_chan_5ghz, i;
3465 /* Retrieve current channel from last RXON. */
3466 chan = sc->rxon.chan;
3467 is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0;
3469 /* Find the TX power group to which this channel belongs. */
3471 for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3472 if (chan <= group->chan)
3475 group = &sc->groups[0];
3477 memset(&cmd, 0, sizeof cmd);
3478 cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ;
3479 cmd.chan = htole16(chan);
3481 /* Set TX power for all OFDM and CCK rates. */
3482 for (i = 0; i <= WPI_RIDX_MAX ; i++) {
3483 /* Retrieve TX power for this channel/rate. */
3484 idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i);
3486 cmd.rates[i].plcp = wpi_ridx_to_plcp[i];
3489 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
3490 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
3492 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
3493 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
3495 DPRINTF(sc, WPI_DEBUG_TEMP,
3496 "chan %d/ridx %d: power index %d\n", chan, i, idx);
3499 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
3503 * Determine Tx power index for a given channel/rate combination.
3504 * This takes into account the regulatory information from EEPROM and the
3505 * current temperature.
3508 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3509 uint8_t chan, int is_chan_5ghz, int ridx)
3511 /* Fixed-point arithmetic division using a n-bit fractional part. */
3512 #define fdivround(a, b, n) \
3513 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3515 /* Linear interpolation. */
3516 #define interpolate(x, x1, y1, x2, y2, n) \
3517 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3519 struct wpi_power_sample *sample;
3522 /* Default TX power is group maximum TX power minus 3dB. */
3523 pwr = group->maxpwr / 2;
3525 /* Decrease TX power for highest OFDM rates to reduce distortion. */
3527 case WPI_RIDX_OFDM36:
3528 pwr -= is_chan_5ghz ? 5 : 0;
3530 case WPI_RIDX_OFDM48:
3531 pwr -= is_chan_5ghz ? 10 : 7;
3533 case WPI_RIDX_OFDM54:
3534 pwr -= is_chan_5ghz ? 12 : 9;
3538 /* Never exceed the channel maximum allowed TX power. */
3539 pwr = min(pwr, sc->maxpwr[chan]);
3541 /* Retrieve TX power index into gain tables from samples. */
3542 for (sample = group->samples; sample < &group->samples[3]; sample++)
3543 if (pwr > sample[1].power)
3545 /* Fixed-point linear interpolation using a 19-bit fractional part. */
3546 idx = interpolate(pwr, sample[0].power, sample[0].index,
3547 sample[1].power, sample[1].index, 19);
3550 * Adjust power index based on current temperature:
3551 * - if cooler than factory-calibrated: decrease output power
3552 * - if warmer than factory-calibrated: increase output power
3554 idx -= (sc->temp - group->temp) * 11 / 100;
3556 /* Decrease TX power for CCK rates (-5dB). */
3557 if (ridx >= WPI_RIDX_CCK1)
3560 /* Make sure idx stays in a valid range. */
3563 if (idx > WPI_MAX_PWR_INDEX)
3564 return WPI_MAX_PWR_INDEX;
3572 * Set STA mode power saving level (between 0 and 5).
3573 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
3576 wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async)
3578 struct wpi_pmgt_cmd cmd;
3579 const struct wpi_pmgt *pmgt;
3580 uint32_t max, skip_dtim;
3584 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
3585 "%s: dtim=%d, level=%d, async=%d\n",
3586 __func__, dtim, level, async);
3588 /* Select which PS parameters to use. */
3590 pmgt = &wpi_pmgt[0][level];
3592 pmgt = &wpi_pmgt[1][level];
3594 memset(&cmd, 0, sizeof cmd);
3595 if (level != 0) /* not CAM */
3596 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
3597 /* Retrieve PCIe Active State Power Management (ASPM). */
3598 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
3599 if (!(reg & 0x1)) /* L0s Entry disabled. */
3600 cmd.flags |= htole16(WPI_PS_PCI_PMGT);
3602 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU);
3603 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU);
3609 skip_dtim = pmgt->skip_dtim;
3611 if (skip_dtim != 0) {
3612 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
3613 max = pmgt->intval[4];
3614 if (max == (uint32_t)-1)
3615 max = dtim * (skip_dtim + 1);
3616 else if (max > dtim)
3617 max = (max / dtim) * dtim;
3621 for (i = 0; i < 5; i++)
3622 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
3624 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
3628 wpi_send_btcoex(struct wpi_softc *sc)
3630 struct wpi_bluetooth cmd;
3632 memset(&cmd, 0, sizeof cmd);
3633 cmd.flags = WPI_BT_COEX_MODE_4WIRE;
3634 cmd.lead_time = WPI_BT_LEAD_TIME_DEF;
3635 cmd.max_kill = WPI_BT_MAX_KILL_DEF;
3636 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n",
3638 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0);
3642 wpi_send_rxon(struct wpi_softc *sc, int assoc, int async)
3647 WPI_RXON_LOCK_ASSERT(sc);
3649 if (assoc && wpi_check_bss_filter(sc) != 0) {
3650 struct wpi_assoc rxon_assoc;
3652 rxon_assoc.flags = sc->rxon.flags;
3653 rxon_assoc.filter = sc->rxon.filter;
3654 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask;
3655 rxon_assoc.cck_mask = sc->rxon.cck_mask;
3656 rxon_assoc.reserved = 0;
3658 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc,
3659 sizeof (struct wpi_assoc), async);
3661 device_printf(sc->sc_dev,
3662 "RXON_ASSOC command failed, error %d\n", error);
3668 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3669 sizeof (struct wpi_rxon), async);
3671 wpi_clear_node_table(sc);
3674 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3675 sizeof (struct wpi_rxon), async);
3677 wpi_clear_node_table(sc);
3681 device_printf(sc->sc_dev,
3682 "RXON command failed, error %d\n", error);
3686 /* Add broadcast node. */
3687 error = wpi_add_broadcast_node(sc, async);
3689 device_printf(sc->sc_dev,
3690 "could not add broadcast node, error %d\n", error);
3695 /* Configuration has changed, set Tx power accordingly. */
3696 if ((error = wpi_set_txpower(sc, async)) != 0) {
3697 device_printf(sc->sc_dev,
3698 "%s: could not set TX power, error %d\n", __func__, error);
3706 * Configure the card to listen to a particular channel, this transisions the
3707 * card in to being able to receive frames from remote devices.
3710 wpi_config(struct wpi_softc *sc)
3712 struct ifnet *ifp = sc->sc_ifp;
3713 struct ieee80211com *ic = ifp->if_l2com;
3714 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3715 struct ieee80211_channel *c = ic->ic_curchan;
3719 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3721 /* Set power saving level to CAM during initialization. */
3722 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
3723 device_printf(sc->sc_dev,
3724 "%s: could not set power saving level\n", __func__);
3728 /* Configure bluetooth coexistence. */
3729 if ((error = wpi_send_btcoex(sc)) != 0) {
3730 device_printf(sc->sc_dev,
3731 "could not configure bluetooth coexistence\n");
3735 /* Configure adapter. */
3736 memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
3737 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr);
3739 /* Set default channel. */
3740 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
3741 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
3742 if (IEEE80211_IS_CHAN_2GHZ(c))
3743 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
3745 sc->rxon.filter = WPI_FILTER_MULTICAST;
3746 switch (ic->ic_opmode) {
3747 case IEEE80211_M_STA:
3748 sc->rxon.mode = WPI_MODE_STA;
3750 case IEEE80211_M_IBSS:
3751 sc->rxon.mode = WPI_MODE_IBSS;
3752 sc->rxon.filter |= WPI_FILTER_BEACON;
3754 case IEEE80211_M_HOSTAP:
3755 /* XXX workaround for beaconing */
3756 sc->rxon.mode = WPI_MODE_IBSS;
3757 sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC;
3759 case IEEE80211_M_AHDEMO:
3760 /* XXX workaround for passive channels selection */
3761 sc->rxon.mode = WPI_MODE_HOSTAP;
3763 case IEEE80211_M_MONITOR:
3764 sc->rxon.mode = WPI_MODE_MONITOR;
3767 device_printf(sc->sc_dev, "unknown opmode %d\n",
3771 sc->rxon.filter = htole32(sc->rxon.filter);
3772 wpi_set_promisc(sc);
3773 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
3774 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
3776 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) {
3777 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3782 /* Setup rate scalling. */
3783 if ((error = wpi_mrr_setup(sc)) != 0) {
3784 device_printf(sc->sc_dev, "could not setup MRR, error %d\n",
3789 /* Disable beacon notifications (unused). */
3790 flags = WPI_STATISTICS_BEACON_DISABLE;
3791 error = wpi_cmd(sc, WPI_CMD_GET_STATISTICS, &flags, sizeof flags, 1);
3793 device_printf(sc->sc_dev,
3794 "could not disable beacon statistics, error %d\n", error);
3798 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3804 wpi_get_active_dwell_time(struct wpi_softc *sc,
3805 struct ieee80211_channel *c, uint8_t n_probes)
3807 /* No channel? Default to 2GHz settings. */
3808 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) {
3809 return (WPI_ACTIVE_DWELL_TIME_2GHZ +
3810 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1));
3813 /* 5GHz dwell time. */
3814 return (WPI_ACTIVE_DWELL_TIME_5GHZ +
3815 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1));
3819 * Limit the total dwell time to 85% of the beacon interval.
3821 * Returns the dwell time in milliseconds.
3824 wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time)
3826 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3827 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3830 /* bintval is in TU (1.024mS) */
3832 bintval = vap->iv_bss->ni_intval;
3835 * If it's non-zero, we should calculate the minimum of
3836 * it and the DWELL_BASE.
3838 * XXX Yes, the math should take into account that bintval
3839 * is 1.024mS, not 1mS..
3842 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__,
3844 return (MIN(WPI_PASSIVE_DWELL_BASE, ((bintval * 85) / 100)));
3847 /* No association context? Default. */
3848 return (WPI_PASSIVE_DWELL_BASE);
3852 wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c)
3856 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c))
3857 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ;
3859 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ;
3861 /* Clamp to the beacon interval if we're associated. */
3862 return (wpi_limit_dwell(sc, passive));
3866 * Send a scan request to the firmware.
3869 wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c)
3871 struct ifnet *ifp = sc->sc_ifp;
3872 struct ieee80211com *ic = ifp->if_l2com;
3873 struct ieee80211_scan_state *ss = ic->ic_scan;
3874 struct ieee80211vap *vap = ss->ss_vap;
3875 struct wpi_scan_hdr *hdr;
3876 struct wpi_cmd_data *tx;
3877 struct wpi_scan_essid *essids;
3878 struct wpi_scan_chan *chan;
3879 struct ieee80211_frame *wh;
3880 struct ieee80211_rateset *rs;
3881 uint16_t dwell_active, dwell_passive;
3883 int buflen, error, i, nssid;
3885 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3888 * We are absolutely not allowed to send a scan command when another
3889 * scan command is pending.
3891 if (callout_pending(&sc->scan_timeout)) {
3892 device_printf(sc->sc_dev, "%s: called whilst scanning!\n",
3895 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3900 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
3902 device_printf(sc->sc_dev,
3903 "%s: could not allocate buffer for scan command\n",
3908 hdr = (struct wpi_scan_hdr *)buf;
3911 * Move to the next channel if no packets are received within 10 msecs
3912 * after sending the probe request.
3914 hdr->quiet_time = htole16(10); /* timeout in milliseconds */
3915 hdr->quiet_threshold = htole16(1); /* min # of packets */
3917 * Max needs to be greater than active and passive and quiet!
3918 * It's also in microseconds!
3920 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU);
3921 hdr->pause_svc = htole32((4 << 24) |
3922 (100 * IEEE80211_DUR_TU)); /* Hardcode for now */
3923 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON);
3925 tx = (struct wpi_cmd_data *)(hdr + 1);
3926 tx->flags = htole32(WPI_TX_AUTO_SEQ);
3927 tx->id = WPI_ID_BROADCAST;
3928 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
3930 if (IEEE80211_IS_CHAN_5GHZ(c)) {
3931 /* Send probe requests at 6Mbps. */
3932 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6];
3933 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
3935 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
3936 /* Send probe requests at 1Mbps. */
3937 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3938 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
3941 essids = (struct wpi_scan_essid *)(tx + 1);
3942 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
3943 for (i = 0; i < nssid; i++) {
3944 essids[i].id = IEEE80211_ELEMID_SSID;
3945 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN);
3946 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len);
3948 if (sc->sc_debug & WPI_DEBUG_SCAN) {
3949 printf("Scanning Essid: ");
3950 ieee80211_print_essid(essids[i].data, essids[i].len);
3957 * Build a probe request frame. Most of the following code is a
3958 * copy & paste of what is done in net80211.
3960 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS);
3961 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3962 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
3963 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3964 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3965 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3966 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
3967 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by h/w */
3968 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by h/w */
3970 frm = (uint8_t *)(wh + 1);
3971 frm = ieee80211_add_ssid(frm, NULL, 0);
3972 frm = ieee80211_add_rates(frm, rs);
3973 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
3974 frm = ieee80211_add_xrates(frm, rs);
3976 /* Set length of probe request. */
3977 tx->len = htole16(frm - (uint8_t *)wh);
3980 * Construct information about the channel that we
3981 * want to scan. The firmware expects this to be directly
3982 * after the scan probe request
3984 chan = (struct wpi_scan_chan *)frm;
3985 chan->chan = htole16(ieee80211_chan2ieee(ic, c));
3988 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT;
3989 chan->flags |= WPI_CHAN_NPBREQS(nssid);
3991 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER;
3993 if (!IEEE80211_IS_CHAN_PASSIVE(c))
3994 chan->flags |= WPI_CHAN_ACTIVE;
3997 * Calculate the active/passive dwell times.
4000 dwell_active = wpi_get_active_dwell_time(sc, c, nssid);
4001 dwell_passive = wpi_get_passive_dwell_time(sc, c);
4003 /* Make sure they're valid. */
4004 if (dwell_passive <= dwell_active)
4005 dwell_passive = dwell_active + 1;
4007 chan->active = htole16(dwell_active);
4008 chan->passive = htole16(dwell_passive);
4010 chan->dsp_gain = 0x6e; /* Default level */
4012 if (IEEE80211_IS_CHAN_5GHZ(c))
4013 chan->rf_gain = 0x3b;
4015 chan->rf_gain = 0x28;
4017 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n",
4018 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c));
4022 if (hdr->nchan == 1 && sc->rxon.chan == chan->chan) {
4023 /* XXX Force probe request transmission. */
4024 memcpy(chan + 1, chan, sizeof (struct wpi_scan_chan));
4028 /* Reduce unnecessary delay. */
4030 chan->passive = chan->active = hdr->quiet_time;
4037 buflen = (uint8_t *)chan - buf;
4038 hdr->len = htole16(buflen);
4040 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n",
4042 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
4043 free(buf, M_DEVBUF);
4048 callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc);
4050 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4054 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
4060 wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
4062 struct ieee80211com *ic = vap->iv_ic;
4063 struct ieee80211_node *ni = vap->iv_bss;
4064 struct ieee80211_channel *c = ni->ni_chan;
4069 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4071 /* Update adapter configuration. */
4072 sc->rxon.associd = 0;
4073 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
4074 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4075 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4076 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4077 if (IEEE80211_IS_CHAN_2GHZ(c))
4078 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4079 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4080 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4081 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4082 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4083 if (IEEE80211_IS_CHAN_A(c)) {
4084 sc->rxon.cck_mask = 0;
4085 sc->rxon.ofdm_mask = 0x15;
4086 } else if (IEEE80211_IS_CHAN_B(c)) {
4087 sc->rxon.cck_mask = 0x03;
4088 sc->rxon.ofdm_mask = 0;
4090 /* Assume 802.11b/g. */
4091 sc->rxon.cck_mask = 0x0f;
4092 sc->rxon.ofdm_mask = 0x15;
4095 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n",
4096 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask,
4097 sc->rxon.ofdm_mask);
4099 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4100 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4104 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4106 WPI_RXON_UNLOCK(sc);
4112 wpi_config_beacon(struct wpi_vap *wvp)
4114 struct ieee80211com *ic = wvp->wv_vap.iv_ic;
4115 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4116 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4117 struct wpi_softc *sc = ic->ic_ifp->if_softc;
4118 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
4119 struct ieee80211_tim_ie *tie;
4124 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4126 WPI_VAP_LOCK_ASSERT(wvp);
4128 cmd->len = htole16(bcn->m->m_pkthdr.len);
4129 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4130 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4132 /* XXX seems to be unused */
4133 if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) {
4134 tie = (struct ieee80211_tim_ie *) bo->bo_tim;
4135 ptr = mtod(bcn->m, uint8_t *);
4137 cmd->tim = htole16(bo->bo_tim - ptr);
4138 cmd->timsz = tie->tim_len;
4141 /* Necessary for recursion in ieee80211_beacon_update(). */
4143 bcn->m = m_dup(m, M_NOWAIT);
4144 if (bcn->m == NULL) {
4145 device_printf(sc->sc_dev,
4146 "%s: could not copy beacon frame\n", __func__);
4151 if ((error = wpi_cmd2(sc, bcn)) != 0) {
4152 device_printf(sc->sc_dev,
4153 "%s: could not update beacon frame, error %d", __func__,
4164 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
4166 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
4167 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4168 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4172 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4174 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
4177 m = ieee80211_beacon_alloc(ni, bo);
4179 device_printf(sc->sc_dev,
4180 "%s: could not allocate beacon frame\n", __func__);
4190 error = wpi_config_beacon(wvp);
4191 WPI_VAP_UNLOCK(wvp);
4197 wpi_update_beacon(struct ieee80211vap *vap, int item)
4199 struct wpi_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4200 struct wpi_vap *wvp = WPI_VAP(vap);
4201 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4202 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4203 struct ieee80211_node *ni = vap->iv_bss;
4206 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4209 if (bcn->m == NULL) {
4210 bcn->m = ieee80211_beacon_alloc(ni, bo);
4211 if (bcn->m == NULL) {
4212 device_printf(sc->sc_dev,
4213 "%s: could not allocate beacon frame\n", __func__);
4215 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR,
4218 WPI_VAP_UNLOCK(wvp);
4222 WPI_VAP_UNLOCK(wvp);
4224 if (item == IEEE80211_BEACON_TIM)
4225 mcast = 1; /* TODO */
4227 setbit(bo->bo_flags, item);
4228 ieee80211_beacon_update(ni, bo, bcn->m, mcast);
4231 wpi_config_beacon(wvp);
4232 WPI_VAP_UNLOCK(wvp);
4234 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4238 wpi_newassoc(struct ieee80211_node *ni, int isnew)
4240 struct ieee80211vap *vap = ni->ni_vap;
4241 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4242 struct wpi_node *wn = WPI_NODE(ni);
4247 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4249 if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) {
4250 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
4251 device_printf(sc->sc_dev,
4252 "%s: could not add IBSS node, error %d\n",
4260 wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
4262 struct ieee80211com *ic = vap->iv_ic;
4263 struct ieee80211_node *ni = vap->iv_bss;
4264 struct ieee80211_channel *c = ni->ni_chan;
4267 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4269 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
4270 /* Link LED blinks while monitoring. */
4271 wpi_set_led(sc, WPI_LED_LINK, 5, 5);
4275 /* XXX kernel panic workaround */
4276 if (c == IEEE80211_CHAN_ANYC) {
4277 device_printf(sc->sc_dev, "%s: incomplete configuration\n",
4282 if ((error = wpi_set_timing(sc, ni)) != 0) {
4283 device_printf(sc->sc_dev,
4284 "%s: could not set timing, error %d\n", __func__, error);
4288 /* Update adapter configuration. */
4290 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4291 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni));
4292 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4293 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4294 if (IEEE80211_IS_CHAN_2GHZ(c))
4295 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4296 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4297 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4298 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4299 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4300 if (IEEE80211_IS_CHAN_A(c)) {
4301 sc->rxon.cck_mask = 0;
4302 sc->rxon.ofdm_mask = 0x15;
4303 } else if (IEEE80211_IS_CHAN_B(c)) {
4304 sc->rxon.cck_mask = 0x03;
4305 sc->rxon.ofdm_mask = 0;
4307 /* Assume 802.11b/g. */
4308 sc->rxon.cck_mask = 0x0f;
4309 sc->rxon.ofdm_mask = 0x15;
4311 sc->rxon.filter |= htole32(WPI_FILTER_BSS);
4313 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n",
4314 sc->rxon.chan, sc->rxon.flags);
4316 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4317 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4322 /* Start periodic calibration timer. */
4323 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
4325 WPI_RXON_UNLOCK(sc);
4327 if (vap->iv_opmode == IEEE80211_M_IBSS ||
4328 vap->iv_opmode == IEEE80211_M_HOSTAP) {
4329 if ((error = wpi_setup_beacon(sc, ni)) != 0) {
4330 device_printf(sc->sc_dev,
4331 "%s: could not setup beacon, error %d\n", __func__,
4337 if (vap->iv_opmode == IEEE80211_M_STA) {
4340 error = wpi_add_sta_node(sc, ni);
4343 device_printf(sc->sc_dev,
4344 "%s: could not add BSS node, error %d\n", __func__,
4350 /* Link LED always on while associated. */
4351 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
4353 /* Enable power-saving mode if requested by user. */
4354 if ((vap->iv_flags & IEEE80211_F_PMGTON) &&
4355 vap->iv_opmode != IEEE80211_M_IBSS)
4356 (void)wpi_set_pslevel(sc, 0, 3, 1);
4358 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4364 wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4366 const struct ieee80211_cipher *cip = k->wk_cipher;
4367 struct ieee80211vap *vap = ni->ni_vap;
4368 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4369 struct wpi_node *wn = WPI_NODE(ni);
4370 struct wpi_node_info node;
4374 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4376 if (wpi_check_node_entry(sc, wn->id) == 0) {
4377 device_printf(sc->sc_dev, "%s: node does not exist\n",
4382 switch (cip->ic_cipher) {
4383 case IEEE80211_CIPHER_AES_CCM:
4384 kflags = WPI_KFLAG_CCMP;
4388 device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__,
4393 kflags |= WPI_KFLAG_KID(k->wk_keyix);
4394 if (k->wk_flags & IEEE80211_KEY_GROUP)
4395 kflags |= WPI_KFLAG_MULTICAST;
4397 memset(&node, 0, sizeof node);
4399 node.control = WPI_NODE_UPDATE;
4400 node.flags = WPI_FLAG_KEY_SET;
4401 node.kflags = htole16(kflags);
4402 memcpy(node.key, k->wk_key, k->wk_keylen);
4404 DPRINTF(sc, WPI_DEBUG_KEY,
4405 "%s: setting %s key id %d for node %d (%s)\n", __func__,
4406 (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix,
4407 node.id, ether_sprintf(ni->ni_macaddr));
4409 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4411 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4416 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4417 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4418 kflags |= WPI_KFLAG_MULTICAST;
4419 node.kflags = htole16(kflags);
4428 wpi_load_key_cb(void *arg, struct ieee80211_node *ni)
4430 const struct ieee80211_key *k = arg;
4431 struct ieee80211vap *vap = ni->ni_vap;
4432 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4433 struct wpi_node *wn = WPI_NODE(ni);
4436 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4440 error = wpi_load_key(ni, k);
4444 device_printf(sc->sc_dev, "%s: error while setting key\n",
4450 wpi_set_global_keys(struct ieee80211_node *ni)
4452 struct ieee80211vap *vap = ni->ni_vap;
4453 struct ieee80211_key *wk = &vap->iv_nw_keys[0];
4456 for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++)
4457 if (wk->wk_keyix != IEEE80211_KEYIX_NONE)
4458 error = wpi_load_key(ni, wk);
4464 wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4466 struct ieee80211vap *vap = ni->ni_vap;
4467 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4468 struct wpi_node *wn = WPI_NODE(ni);
4469 struct wpi_node_info node;
4473 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4475 if (wpi_check_node_entry(sc, wn->id) == 0) {
4476 DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__);
4477 return 1; /* Nothing to do. */
4480 kflags = WPI_KFLAG_KID(k->wk_keyix);
4481 if (k->wk_flags & IEEE80211_KEY_GROUP)
4482 kflags |= WPI_KFLAG_MULTICAST;
4484 memset(&node, 0, sizeof node);
4486 node.control = WPI_NODE_UPDATE;
4487 node.flags = WPI_FLAG_KEY_SET;
4488 node.kflags = htole16(kflags);
4490 DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n",
4491 __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast",
4492 k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr));
4494 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4496 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4501 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4502 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4503 kflags |= WPI_KFLAG_MULTICAST;
4504 node.kflags = htole16(kflags);
4513 wpi_del_key_cb(void *arg, struct ieee80211_node *ni)
4515 const struct ieee80211_key *k = arg;
4516 struct ieee80211vap *vap = ni->ni_vap;
4517 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4518 struct wpi_node *wn = WPI_NODE(ni);
4521 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4525 error = wpi_del_key(ni, k);
4529 device_printf(sc->sc_dev, "%s: error while deleting key\n",
4535 wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
4538 struct ieee80211com *ic = vap->iv_ic;
4539 struct wpi_softc *sc = ic->ic_ifp->if_softc;
4540 struct wpi_vap *wvp = WPI_VAP(vap);
4541 struct ieee80211_node *ni;
4542 int error, ni_ref = 0;
4544 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4546 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
4551 if (!(k->wk_flags & IEEE80211_KEY_RECV)) {
4552 /* XMIT keys are handled in wpi_tx_data(). */
4556 /* Handle group keys. */
4557 if (&vap->iv_nw_keys[0] <= k &&
4558 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4561 wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix);
4563 wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix);
4566 if (vap->iv_state == IEEE80211_S_RUN) {
4567 ieee80211_iterate_nodes(&ic->ic_sta,
4568 set ? wpi_load_key_cb : wpi_del_key_cb,
4569 __DECONST(void *, k));
4575 switch (vap->iv_opmode) {
4576 case IEEE80211_M_STA:
4580 case IEEE80211_M_IBSS:
4581 case IEEE80211_M_AHDEMO:
4582 case IEEE80211_M_HOSTAP:
4583 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
4585 return 0; /* should not happen */
4591 device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__,
4598 error = wpi_load_key(ni, k);
4600 error = wpi_del_key(ni, k);
4604 ieee80211_node_decref(ni);
4610 wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
4611 const uint8_t mac[IEEE80211_ADDR_LEN])
4613 return wpi_process_key(vap, k, 1);
4617 wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4619 return wpi_process_key(vap, k, 0);
4623 * This function is called after the runtime firmware notifies us of its
4624 * readiness (called in a process context).
4627 wpi_post_alive(struct wpi_softc *sc)
4631 /* Check (again) that the radio is not disabled. */
4632 if ((error = wpi_nic_lock(sc)) != 0)
4635 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4637 /* NB: Runtime firmware must be up and running. */
4638 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
4639 device_printf(sc->sc_dev,
4640 "RF switch: radio disabled (%s)\n", __func__);
4642 return EPERM; /* :-) */
4646 /* Wait for thermal sensor to calibrate. */
4647 for (ntries = 0; ntries < 1000; ntries++) {
4648 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
4653 if (ntries == 1000) {
4654 device_printf(sc->sc_dev,
4655 "timeout waiting for thermal sensor calibration\n");
4659 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp);
4664 * The firmware boot code is small and is intended to be copied directly into
4665 * the NIC internal memory (no DMA transfer).
4668 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
4672 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size);
4674 size /= sizeof (uint32_t);
4676 if ((error = wpi_nic_lock(sc)) != 0)
4679 /* Copy microcode image into NIC memory. */
4680 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
4681 (const uint32_t *)ucode, size);
4683 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
4684 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
4685 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
4687 /* Start boot load now. */
4688 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
4690 /* Wait for transfer to complete. */
4691 for (ntries = 0; ntries < 1000; ntries++) {
4692 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS);
4693 DPRINTF(sc, WPI_DEBUG_HW,
4694 "firmware status=0x%x, val=0x%x, result=0x%x\n", status,
4695 WPI_FH_TX_STATUS_IDLE(6),
4696 status & WPI_FH_TX_STATUS_IDLE(6));
4697 if (status & WPI_FH_TX_STATUS_IDLE(6)) {
4698 DPRINTF(sc, WPI_DEBUG_HW,
4699 "Status Match! - ntries = %d\n", ntries);
4704 if (ntries == 1000) {
4705 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4711 /* Enable boot after power up. */
4712 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
4719 wpi_load_firmware(struct wpi_softc *sc)
4721 struct wpi_fw_info *fw = &sc->fw;
4722 struct wpi_dma_info *dma = &sc->fw_dma;
4725 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4727 /* Copy initialization sections into pre-allocated DMA-safe memory. */
4728 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
4729 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4730 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz);
4731 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4733 /* Tell adapter where to find initialization sections. */
4734 if ((error = wpi_nic_lock(sc)) != 0)
4736 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4737 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
4738 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4739 dma->paddr + WPI_FW_DATA_MAXSZ);
4740 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
4743 /* Load firmware boot code. */
4744 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
4746 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4751 /* Now press "execute". */
4752 WPI_WRITE(sc, WPI_RESET, 0);
4754 /* Wait at most one second for first alive notification. */
4755 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
4756 device_printf(sc->sc_dev,
4757 "%s: timeout waiting for adapter to initialize, error %d\n",
4762 /* Copy runtime sections into pre-allocated DMA-safe memory. */
4763 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
4764 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4765 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz);
4766 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4768 /* Tell adapter where to find runtime sections. */
4769 if ((error = wpi_nic_lock(sc)) != 0)
4771 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4772 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
4773 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4774 dma->paddr + WPI_FW_DATA_MAXSZ);
4775 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
4776 WPI_FW_UPDATED | fw->main.textsz);
4783 wpi_read_firmware(struct wpi_softc *sc)
4785 const struct firmware *fp;
4786 struct wpi_fw_info *fw = &sc->fw;
4787 const struct wpi_firmware_hdr *hdr;
4790 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4792 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4793 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME);
4796 fp = firmware_get(WPI_FW_NAME);
4800 device_printf(sc->sc_dev,
4801 "could not load firmware image '%s'\n", WPI_FW_NAME);
4807 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
4808 device_printf(sc->sc_dev,
4809 "firmware file too short: %zu bytes\n", fp->datasize);
4814 fw->size = fp->datasize;
4815 fw->data = (const uint8_t *)fp->data;
4817 /* Extract firmware header information. */
4818 hdr = (const struct wpi_firmware_hdr *)fw->data;
4820 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW |
4821 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
4823 fw->main.textsz = le32toh(hdr->rtextsz);
4824 fw->main.datasz = le32toh(hdr->rdatasz);
4825 fw->init.textsz = le32toh(hdr->itextsz);
4826 fw->init.datasz = le32toh(hdr->idatasz);
4827 fw->boot.textsz = le32toh(hdr->btextsz);
4828 fw->boot.datasz = 0;
4830 /* Sanity-check firmware header. */
4831 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
4832 fw->main.datasz > WPI_FW_DATA_MAXSZ ||
4833 fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
4834 fw->init.datasz > WPI_FW_DATA_MAXSZ ||
4835 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
4836 (fw->boot.textsz & 3) != 0) {
4837 device_printf(sc->sc_dev, "invalid firmware header\n");
4842 /* Check that all firmware sections fit. */
4843 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
4844 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
4845 device_printf(sc->sc_dev,
4846 "firmware file too short: %zu bytes\n", fw->size);
4851 /* Get pointers to firmware sections. */
4852 fw->main.text = (const uint8_t *)(hdr + 1);
4853 fw->main.data = fw->main.text + fw->main.textsz;
4854 fw->init.text = fw->main.data + fw->main.datasz;
4855 fw->init.data = fw->init.text + fw->init.textsz;
4856 fw->boot.text = fw->init.data + fw->init.datasz;
4858 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4859 "Firmware Version: Major %d, Minor %d, Driver %d, \n"
4860 "runtime (text: %u, data: %u) init (text: %u, data %u) "
4861 "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver),
4862 fw->main.textsz, fw->main.datasz,
4863 fw->init.textsz, fw->init.datasz, fw->boot.textsz);
4865 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text);
4866 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data);
4867 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text);
4868 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data);
4869 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text);
4873 fail: wpi_unload_firmware(sc);
4878 * Free the referenced firmware image
4881 wpi_unload_firmware(struct wpi_softc *sc)
4883 if (sc->fw_fp != NULL) {
4884 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
4890 wpi_clock_wait(struct wpi_softc *sc)
4894 /* Set "initialization complete" bit. */
4895 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
4897 /* Wait for clock stabilization. */
4898 for (ntries = 0; ntries < 2500; ntries++) {
4899 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
4903 device_printf(sc->sc_dev,
4904 "%s: timeout waiting for clock stabilization\n", __func__);
4910 wpi_apm_init(struct wpi_softc *sc)
4915 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4917 /* Disable L0s exit timer (NMI bug workaround). */
4918 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER);
4919 /* Don't wait for ICH L0s (ICH bug workaround). */
4920 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
4922 /* Set FH wait threshold to max (HW bug under stress workaround). */
4923 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000);
4925 /* Retrieve PCIe Active State Power Management (ASPM). */
4926 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
4927 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
4928 if (reg & 0x02) /* L1 Entry enabled. */
4929 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
4931 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
4933 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
4935 /* Wait for clock stabilization before accessing prph. */
4936 if ((error = wpi_clock_wait(sc)) != 0)
4939 if ((error = wpi_nic_lock(sc)) != 0)
4942 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400);
4943 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200);
4945 /* Enable DMA and BSM (Bootstrap State Machine). */
4946 wpi_prph_write(sc, WPI_APMG_CLK_EN,
4947 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT);
4949 /* Disable L1-Active. */
4950 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
4957 wpi_apm_stop_master(struct wpi_softc *sc)
4961 /* Stop busmaster DMA activity. */
4962 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
4964 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
4965 WPI_GP_CNTRL_MAC_PS)
4966 return; /* Already asleep. */
4968 for (ntries = 0; ntries < 100; ntries++) {
4969 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
4973 device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
4978 wpi_apm_stop(struct wpi_softc *sc)
4980 wpi_apm_stop_master(sc);
4982 /* Reset the entire device. */
4983 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
4985 /* Clear "initialization complete" bit. */
4986 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
4990 wpi_nic_config(struct wpi_softc *sc)
4994 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4996 /* voodoo from the Linux "driver".. */
4997 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
4998 if ((rev & 0xc0) == 0x40)
4999 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
5000 else if (!(rev & 0x80))
5001 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
5003 if (sc->cap == 0x80)
5004 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
5006 if ((sc->rev & 0xf0) == 0xd0)
5007 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5009 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5012 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
5016 wpi_hw_init(struct wpi_softc *sc)
5018 int chnl, ntries, error;
5020 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5022 /* Clear pending interrupts. */
5023 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5025 if ((error = wpi_apm_init(sc)) != 0) {
5026 device_printf(sc->sc_dev,
5027 "%s: could not power ON adapter, error %d\n", __func__,
5032 /* Select VMAIN power source. */
5033 if ((error = wpi_nic_lock(sc)) != 0)
5035 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
5037 /* Spin until VMAIN gets selected. */
5038 for (ntries = 0; ntries < 5000; ntries++) {
5039 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
5043 if (ntries == 5000) {
5044 device_printf(sc->sc_dev, "timeout selecting power source\n");
5048 /* Perform adapter initialization. */
5051 /* Initialize RX ring. */
5052 if ((error = wpi_nic_lock(sc)) != 0)
5054 /* Set physical address of RX ring. */
5055 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
5056 /* Set physical address of RX read pointer. */
5057 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
5058 offsetof(struct wpi_shared, next));
5059 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
5061 WPI_WRITE(sc, WPI_FH_RX_CONFIG,
5062 WPI_FH_RX_CONFIG_DMA_ENA |
5063 WPI_FH_RX_CONFIG_RDRBD_ENA |
5064 WPI_FH_RX_CONFIG_WRSTATUS_ENA |
5065 WPI_FH_RX_CONFIG_MAXFRAG |
5066 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
5067 WPI_FH_RX_CONFIG_IRQ_DST_HOST |
5068 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1));
5069 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */
5071 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
5073 /* Initialize TX rings. */
5074 if ((error = wpi_nic_lock(sc)) != 0)
5076 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */
5077 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */
5078 /* Enable all 6 TX rings. */
5079 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
5080 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
5081 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
5082 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
5083 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
5084 /* Set physical address of TX rings. */
5085 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
5086 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
5088 /* Enable all DMA channels. */
5089 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5090 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
5091 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
5092 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
5095 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */
5097 /* Clear "radio off" and "commands blocked" bits. */
5098 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5099 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
5101 /* Clear pending interrupts. */
5102 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5103 /* Enable interrupts. */
5104 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
5106 /* _Really_ make sure "radio off" bit is cleared! */
5107 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5108 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5110 if ((error = wpi_load_firmware(sc)) != 0) {
5111 device_printf(sc->sc_dev,
5112 "%s: could not load firmware, error %d\n", __func__,
5116 /* Wait at most one second for firmware alive notification. */
5117 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
5118 device_printf(sc->sc_dev,
5119 "%s: timeout waiting for adapter to initialize, error %d\n",
5124 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5126 /* Do post-firmware initialization. */
5127 return wpi_post_alive(sc);
5131 wpi_hw_stop(struct wpi_softc *sc)
5133 int chnl, qid, ntries;
5135 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5137 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP)
5140 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
5142 /* Disable interrupts. */
5143 WPI_WRITE(sc, WPI_INT_MASK, 0);
5144 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5145 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
5147 /* Make sure we no longer hold the NIC lock. */
5150 if (wpi_nic_lock(sc) == 0) {
5151 /* Stop TX scheduler. */
5152 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
5153 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
5155 /* Stop all DMA channels. */
5156 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5157 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
5158 for (ntries = 0; ntries < 200; ntries++) {
5159 if (WPI_READ(sc, WPI_FH_TX_STATUS) &
5160 WPI_FH_TX_STATUS_IDLE(chnl))
5169 wpi_reset_rx_ring(sc);
5171 /* Reset all TX rings. */
5172 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
5173 wpi_reset_tx_ring(sc, &sc->txq[qid]);
5175 if (wpi_nic_lock(sc) == 0) {
5176 wpi_prph_write(sc, WPI_APMG_CLK_DIS,
5177 WPI_APMG_CLK_CTRL_DMA_CLK_RQT);
5181 /* Power OFF adapter. */
5186 wpi_radio_on(void *arg0, int pending)
5188 struct wpi_softc *sc = arg0;
5189 struct ifnet *ifp = sc->sc_ifp;
5190 struct ieee80211com *ic = ifp->if_l2com;
5191 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5193 device_printf(sc->sc_dev, "RF switch: radio enabled\n");
5197 ieee80211_init(vap);
5200 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL) {
5202 callout_stop(&sc->watchdog_rfkill);
5208 wpi_radio_off(void *arg0, int pending)
5210 struct wpi_softc *sc = arg0;
5211 struct ifnet *ifp = sc->sc_ifp;
5212 struct ieee80211com *ic = ifp->if_l2com;
5213 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5215 device_printf(sc->sc_dev, "RF switch: radio disabled\n");
5219 ieee80211_stop(vap);
5222 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc);
5229 struct wpi_softc *sc = arg;
5230 struct ifnet *ifp = sc->sc_ifp;
5231 struct ieee80211com *ic = ifp->if_l2com;
5236 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5238 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
5241 /* Check that the radio is not disabled by hardware switch. */
5242 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
5243 device_printf(sc->sc_dev,
5244 "RF switch: radio disabled (%s)\n", __func__);
5245 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
5250 /* Read firmware images from the filesystem. */
5251 if ((error = wpi_read_firmware(sc)) != 0) {
5252 device_printf(sc->sc_dev,
5253 "%s: could not read firmware, error %d\n", __func__,
5258 /* Initialize hardware and upload firmware. */
5259 error = wpi_hw_init(sc);
5260 wpi_unload_firmware(sc);
5262 device_printf(sc->sc_dev,
5263 "%s: could not initialize hardware, error %d\n", __func__,
5268 /* Configure adapter now that it is ready. */
5270 if ((error = wpi_config(sc)) != 0) {
5271 device_printf(sc->sc_dev,
5272 "%s: could not configure device, error %d\n", __func__,
5277 IF_LOCK(&ifp->if_snd);
5278 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5279 ifp->if_drv_flags |= IFF_DRV_RUNNING;
5280 IF_UNLOCK(&ifp->if_snd);
5282 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5286 ieee80211_start_all(ic);
5290 fail: wpi_stop_locked(sc);
5291 end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
5296 wpi_stop_locked(struct wpi_softc *sc)
5298 struct ifnet *ifp = sc->sc_ifp;
5300 WPI_LOCK_ASSERT(sc);
5306 WPI_TXQ_STATE_LOCK(sc);
5307 callout_stop(&sc->tx_timeout);
5308 WPI_TXQ_STATE_UNLOCK(sc);
5311 callout_stop(&sc->scan_timeout);
5312 callout_stop(&sc->calib_to);
5313 WPI_RXON_UNLOCK(sc);
5315 IF_LOCK(&ifp->if_snd);
5316 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
5317 IF_UNLOCK(&ifp->if_snd);
5319 /* Power OFF hardware. */
5324 wpi_stop(struct wpi_softc *sc)
5327 wpi_stop_locked(sc);
5332 * Callback from net80211 to start a scan.
5335 wpi_scan_start(struct ieee80211com *ic)
5337 struct wpi_softc *sc = ic->ic_ifp->if_softc;
5339 wpi_set_led(sc, WPI_LED_LINK, 20, 2);
5343 * Callback from net80211 to terminate a scan.
5346 wpi_scan_end(struct ieee80211com *ic)
5348 struct ifnet *ifp = ic->ic_ifp;
5349 struct wpi_softc *sc = ifp->if_softc;
5350 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5352 if (vap->iv_state == IEEE80211_S_RUN)
5353 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
5357 * Called by the net80211 framework to indicate to the driver
5358 * that the channel should be changed
5361 wpi_set_channel(struct ieee80211com *ic)
5363 const struct ieee80211_channel *c = ic->ic_curchan;
5364 struct ifnet *ifp = ic->ic_ifp;
5365 struct wpi_softc *sc = ifp->if_softc;
5368 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5371 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
5372 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
5375 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
5376 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
5380 * Only need to set the channel in Monitor mode. AP scanning and auth
5381 * are already taken care of by their respective firmware commands.
5383 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
5385 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
5386 if (IEEE80211_IS_CHAN_2GHZ(c)) {
5387 sc->rxon.flags |= htole32(WPI_RXON_AUTO |
5390 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO |
5393 if ((error = wpi_send_rxon(sc, 0, 1)) != 0)
5394 device_printf(sc->sc_dev,
5395 "%s: error %d setting channel\n", __func__,
5397 WPI_RXON_UNLOCK(sc);
5402 * Called by net80211 to indicate that we need to scan the current
5403 * channel. The channel is previously be set via the wpi_set_channel
5407 wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
5409 struct ieee80211vap *vap = ss->ss_vap;
5410 struct ieee80211com *ic = vap->iv_ic;
5411 struct wpi_softc *sc = ic->ic_ifp->if_softc;
5415 error = wpi_scan(sc, ic->ic_curchan);
5416 WPI_RXON_UNLOCK(sc);
5418 ieee80211_cancel_scan(vap);
5422 * Called by the net80211 framework to indicate
5423 * the minimum dwell time has been met, terminate the scan.
5424 * We don't actually terminate the scan as the firmware will notify
5425 * us when it's finished and we have no way to interrupt it.
5428 wpi_scan_mindwell(struct ieee80211_scan_state *ss)
5430 /* NB: don't try to abort scan; wait for firmware to finish */
5434 wpi_hw_reset(void *arg, int pending)
5436 struct wpi_softc *sc = arg;
5437 struct ifnet *ifp = sc->sc_ifp;
5438 struct ieee80211com *ic = ifp->if_l2com;
5439 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5441 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5443 if (vap != NULL && (ic->ic_flags & IEEE80211_F_SCAN))
5444 ieee80211_cancel_scan(vap);
5448 ieee80211_stop(vap);
5451 ieee80211_init(vap);