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 sc->sc_scan_curchan = ic->ic_scan_curchan;
520 ic->ic_scan_curchan = wpi_scan_curchan;
521 ic->ic_scan_mindwell = wpi_scan_mindwell;
522 ic->ic_setregdomain = wpi_setregdomain;
524 wpi_radiotap_attach(sc);
526 callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0);
527 callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0);
528 callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0);
529 callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0);
530 TASK_INIT(&sc->sc_reinittask, 0, wpi_hw_reset, sc);
531 TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc);
532 TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc);
533 TASK_INIT(&sc->sc_start_task, 0, wpi_start_task, sc);
535 sc->sc_tq = taskqueue_create("wpi_taskq", M_WAITOK,
536 taskqueue_thread_enqueue, &sc->sc_tq);
537 error = taskqueue_start_threads(&sc->sc_tq, 1, 0, "wpi_taskq");
539 device_printf(dev, "can't start threads, error %d\n", error);
543 wpi_sysctlattach(sc);
546 * Hook our interrupt after all initialization is complete.
548 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
549 NULL, wpi_intr, sc, &sc->sc_ih);
551 device_printf(dev, "can't establish interrupt, error %d\n",
557 ieee80211_announce(ic);
560 if (sc->sc_debug & WPI_DEBUG_HW)
561 ieee80211_announce_channels(ic);
564 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
567 fail: wpi_detach(dev);
568 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
573 * Attach the interface to 802.11 radiotap.
576 wpi_radiotap_attach(struct wpi_softc *sc)
578 struct ifnet *ifp = sc->sc_ifp;
579 struct ieee80211com *ic = ifp->if_l2com;
580 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
581 ieee80211_radiotap_attach(ic,
582 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
583 WPI_TX_RADIOTAP_PRESENT,
584 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
585 WPI_RX_RADIOTAP_PRESENT);
586 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
590 wpi_sysctlattach(struct wpi_softc *sc)
593 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
594 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
596 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
597 "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug,
598 "control debugging printfs");
603 wpi_init_beacon(struct wpi_vap *wvp)
605 struct wpi_buf *bcn = &wvp->wv_bcbuf;
606 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
608 cmd->id = WPI_ID_BROADCAST;
609 cmd->ofdm_mask = 0xff;
610 cmd->cck_mask = 0x0f;
611 cmd->lifetime = htole32(WPI_LIFETIME_INFINITE);
612 cmd->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
614 bcn->code = WPI_CMD_SET_BEACON;
615 bcn->ac = WPI_CMD_QUEUE_NUM;
616 bcn->size = sizeof(struct wpi_cmd_beacon);
619 static struct ieee80211vap *
620 wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
621 enum ieee80211_opmode opmode, int flags,
622 const uint8_t bssid[IEEE80211_ADDR_LEN],
623 const uint8_t mac[IEEE80211_ADDR_LEN])
626 struct ieee80211vap *vap;
628 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
631 wvp = (struct wpi_vap *) malloc(sizeof(struct wpi_vap),
632 M_80211_VAP, M_NOWAIT | M_ZERO);
636 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
638 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
639 WPI_VAP_LOCK_INIT(wvp);
640 wpi_init_beacon(wvp);
643 /* Override with driver methods. */
644 vap->iv_key_set = wpi_key_set;
645 vap->iv_key_delete = wpi_key_delete;
646 wvp->wv_newstate = vap->iv_newstate;
647 vap->iv_newstate = wpi_newstate;
648 vap->iv_update_beacon = wpi_update_beacon;
649 vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1;
651 ieee80211_ratectl_init(vap);
652 /* Complete setup. */
653 ieee80211_vap_attach(vap, ieee80211_media_change,
654 ieee80211_media_status);
655 ic->ic_opmode = opmode;
660 wpi_vap_delete(struct ieee80211vap *vap)
662 struct wpi_vap *wvp = WPI_VAP(vap);
663 struct wpi_buf *bcn = &wvp->wv_bcbuf;
664 enum ieee80211_opmode opmode = vap->iv_opmode;
666 ieee80211_ratectl_deinit(vap);
667 ieee80211_vap_detach(vap);
669 if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
673 WPI_VAP_LOCK_DESTROY(wvp);
676 free(wvp, M_80211_VAP);
680 wpi_detach(device_t dev)
682 struct wpi_softc *sc = device_get_softc(dev);
683 struct ifnet *ifp = sc->sc_ifp;
684 struct ieee80211com *ic;
687 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
692 ieee80211_draintask(ic, &sc->sc_reinittask);
693 ieee80211_draintask(ic, &sc->sc_radiooff_task);
694 ieee80211_draintask(ic, &sc->sc_radioon_task);
695 ieee80211_draintask(ic, &sc->sc_start_task);
699 taskqueue_drain_all(sc->sc_tq);
700 taskqueue_free(sc->sc_tq);
702 callout_drain(&sc->watchdog_rfkill);
703 callout_drain(&sc->tx_timeout);
704 callout_drain(&sc->scan_timeout);
705 callout_drain(&sc->calib_to);
706 ieee80211_ifdetach(ic);
709 /* Uninstall interrupt handler. */
710 if (sc->irq != NULL) {
711 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
712 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
714 pci_release_msi(dev);
717 if (sc->txq[0].data_dmat) {
718 /* Free DMA resources. */
719 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
720 wpi_free_tx_ring(sc, &sc->txq[qid]);
722 wpi_free_rx_ring(sc);
730 bus_release_resource(dev, SYS_RES_MEMORY,
731 rman_get_rid(sc->mem), sc->mem);
736 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
737 WPI_TXQ_STATE_LOCK_DESTROY(sc);
738 WPI_TXQ_LOCK_DESTROY(sc);
739 WPI_NT_LOCK_DESTROY(sc);
740 WPI_RXON_LOCK_DESTROY(sc);
741 WPI_TX_LOCK_DESTROY(sc);
742 WPI_LOCK_DESTROY(sc);
747 wpi_shutdown(device_t dev)
749 struct wpi_softc *sc = device_get_softc(dev);
756 wpi_suspend(device_t dev)
758 struct wpi_softc *sc = device_get_softc(dev);
759 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
761 ieee80211_suspend_all(ic);
766 wpi_resume(device_t dev)
768 struct wpi_softc *sc = device_get_softc(dev);
769 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
771 /* Clear device-specific "PCI retry timeout" register (41h). */
772 pci_write_config(dev, 0x41, 0, 1);
774 ieee80211_resume_all(ic);
779 * Grab exclusive access to NIC memory.
782 wpi_nic_lock(struct wpi_softc *sc)
786 /* Request exclusive access to NIC. */
787 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
789 /* Spin until we actually get the lock. */
790 for (ntries = 0; ntries < 1000; ntries++) {
791 if ((WPI_READ(sc, WPI_GP_CNTRL) &
792 (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
793 WPI_GP_CNTRL_MAC_ACCESS_ENA)
798 device_printf(sc->sc_dev, "could not lock memory\n");
804 * Release lock on NIC memory.
807 wpi_nic_unlock(struct wpi_softc *sc)
809 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
812 static __inline uint32_t
813 wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
815 WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
816 WPI_BARRIER_READ_WRITE(sc);
817 return WPI_READ(sc, WPI_PRPH_RDATA);
821 wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
823 WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
824 WPI_BARRIER_WRITE(sc);
825 WPI_WRITE(sc, WPI_PRPH_WDATA, data);
829 wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
831 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
835 wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
837 wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
841 wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
842 const uint32_t *data, int count)
844 for (; count > 0; count--, data++, addr += 4)
845 wpi_prph_write(sc, addr, *data);
848 static __inline uint32_t
849 wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
851 WPI_WRITE(sc, WPI_MEM_RADDR, addr);
852 WPI_BARRIER_READ_WRITE(sc);
853 return WPI_READ(sc, WPI_MEM_RDATA);
857 wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
860 for (; count > 0; count--, addr += 4)
861 *data++ = wpi_mem_read(sc, addr);
865 wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
871 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
873 if ((error = wpi_nic_lock(sc)) != 0)
876 for (; count > 0; count -= 2, addr++) {
877 WPI_WRITE(sc, WPI_EEPROM, addr << 2);
878 for (ntries = 0; ntries < 10; ntries++) {
879 val = WPI_READ(sc, WPI_EEPROM);
880 if (val & WPI_EEPROM_READ_VALID)
885 device_printf(sc->sc_dev,
886 "timeout reading ROM at 0x%x\n", addr);
896 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
902 wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
906 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
907 *(bus_addr_t *)arg = segs[0].ds_addr;
911 * Allocates a contiguous block of dma memory of the requested size and
915 wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
916 void **kvap, bus_size_t size, bus_size_t alignment)
923 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
924 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
925 1, size, BUS_DMA_NOWAIT, NULL, NULL, &dma->tag);
929 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
930 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
934 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
935 wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
939 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
946 fail: wpi_dma_contig_free(dma);
951 wpi_dma_contig_free(struct wpi_dma_info *dma)
953 if (dma->vaddr != NULL) {
954 bus_dmamap_sync(dma->tag, dma->map,
955 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
956 bus_dmamap_unload(dma->tag, dma->map);
957 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
960 if (dma->tag != NULL) {
961 bus_dma_tag_destroy(dma->tag);
967 * Allocate a shared page between host and NIC.
970 wpi_alloc_shared(struct wpi_softc *sc)
972 /* Shared buffer must be aligned on a 4KB boundary. */
973 return wpi_dma_contig_alloc(sc, &sc->shared_dma,
974 (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
978 wpi_free_shared(struct wpi_softc *sc)
980 wpi_dma_contig_free(&sc->shared_dma);
984 * Allocate DMA-safe memory for firmware transfer.
987 wpi_alloc_fwmem(struct wpi_softc *sc)
989 /* Must be aligned on a 16-byte boundary. */
990 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
991 WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
995 wpi_free_fwmem(struct wpi_softc *sc)
997 wpi_dma_contig_free(&sc->fw_dma);
1001 wpi_alloc_rx_ring(struct wpi_softc *sc)
1003 struct wpi_rx_ring *ring = &sc->rxq;
1010 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1012 /* Allocate RX descriptors (16KB aligned.) */
1013 size = WPI_RX_RING_COUNT * sizeof (uint32_t);
1014 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1015 (void **)&ring->desc, size, WPI_RING_DMA_ALIGN);
1017 device_printf(sc->sc_dev,
1018 "%s: could not allocate RX ring DMA memory, error %d\n",
1023 /* Create RX buffer DMA tag. */
1024 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1025 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1026 MJUMPAGESIZE, 1, MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL,
1029 device_printf(sc->sc_dev,
1030 "%s: could not create RX buf DMA tag, error %d\n",
1036 * Allocate and map RX buffers.
1038 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1039 struct wpi_rx_data *data = &ring->data[i];
1042 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1044 device_printf(sc->sc_dev,
1045 "%s: could not create RX buf DMA map, error %d\n",
1050 data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1051 if (data->m == NULL) {
1052 device_printf(sc->sc_dev,
1053 "%s: could not allocate RX mbuf\n", __func__);
1058 error = bus_dmamap_load(ring->data_dmat, data->map,
1059 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1060 &paddr, BUS_DMA_NOWAIT);
1061 if (error != 0 && error != EFBIG) {
1062 device_printf(sc->sc_dev,
1063 "%s: can't map mbuf (error %d)\n", __func__,
1068 /* Set physical address of RX buffer. */
1069 ring->desc[i] = htole32(paddr);
1072 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1073 BUS_DMASYNC_PREWRITE);
1075 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1079 fail: wpi_free_rx_ring(sc);
1081 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1087 wpi_update_rx_ring(struct wpi_softc *sc)
1089 struct wpi_rx_ring *ring = &sc->rxq;
1091 if (ring->update != 0) {
1092 /* Wait for INT_WAKEUP event. */
1096 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP) {
1097 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n",
1100 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1103 WPI_WRITE(sc, WPI_FH_RX_WPTR, ring->cur & ~7);
1107 wpi_reset_rx_ring(struct wpi_softc *sc)
1109 struct wpi_rx_ring *ring = &sc->rxq;
1112 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1114 if (wpi_nic_lock(sc) == 0) {
1115 WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
1116 for (ntries = 0; ntries < 1000; ntries++) {
1117 if (WPI_READ(sc, WPI_FH_RX_STATUS) &
1118 WPI_FH_RX_STATUS_IDLE)
1130 wpi_free_rx_ring(struct wpi_softc *sc)
1132 struct wpi_rx_ring *ring = &sc->rxq;
1135 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1137 wpi_dma_contig_free(&ring->desc_dma);
1139 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1140 struct wpi_rx_data *data = &ring->data[i];
1142 if (data->m != NULL) {
1143 bus_dmamap_sync(ring->data_dmat, data->map,
1144 BUS_DMASYNC_POSTREAD);
1145 bus_dmamap_unload(ring->data_dmat, data->map);
1149 if (data->map != NULL)
1150 bus_dmamap_destroy(ring->data_dmat, data->map);
1152 if (ring->data_dmat != NULL) {
1153 bus_dma_tag_destroy(ring->data_dmat);
1154 ring->data_dmat = NULL;
1159 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int qid)
1170 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1172 /* Allocate TX descriptors (16KB aligned.) */
1173 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
1174 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc,
1175 size, WPI_RING_DMA_ALIGN);
1177 device_printf(sc->sc_dev,
1178 "%s: could not allocate TX ring DMA memory, error %d\n",
1183 /* Update shared area with ring physical address. */
1184 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1185 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1186 BUS_DMASYNC_PREWRITE);
1189 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
1190 * to allocate commands space for other rings.
1191 * XXX Do we really need to allocate descriptors for other rings?
1193 if (qid > WPI_CMD_QUEUE_NUM) {
1194 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1198 size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
1199 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1202 device_printf(sc->sc_dev,
1203 "%s: could not allocate TX cmd DMA memory, error %d\n",
1208 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1209 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1210 WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL,
1213 device_printf(sc->sc_dev,
1214 "%s: could not create TX buf DMA tag, error %d\n",
1219 paddr = ring->cmd_dma.paddr;
1220 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1221 struct wpi_tx_data *data = &ring->data[i];
1223 data->cmd_paddr = paddr;
1224 paddr += sizeof (struct wpi_tx_cmd);
1226 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1228 device_printf(sc->sc_dev,
1229 "%s: could not create TX buf DMA map, error %d\n",
1235 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1239 fail: wpi_free_tx_ring(sc, ring);
1240 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1245 wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1247 if (ring->update != 0) {
1248 /* Wait for INT_WAKEUP event. */
1252 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP) {
1253 DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n",
1254 __func__, ring->qid);
1256 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
1259 WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
1263 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1267 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1269 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1270 struct wpi_tx_data *data = &ring->data[i];
1272 if (data->m != NULL) {
1273 bus_dmamap_sync(ring->data_dmat, data->map,
1274 BUS_DMASYNC_POSTWRITE);
1275 bus_dmamap_unload(ring->data_dmat, data->map);
1280 /* Clear TX descriptors. */
1281 memset(ring->desc, 0, ring->desc_dma.size);
1282 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1283 BUS_DMASYNC_PREWRITE);
1284 sc->qfullmsk &= ~(1 << ring->qid);
1291 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1295 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1297 wpi_dma_contig_free(&ring->desc_dma);
1298 wpi_dma_contig_free(&ring->cmd_dma);
1300 for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1301 struct wpi_tx_data *data = &ring->data[i];
1303 if (data->m != NULL) {
1304 bus_dmamap_sync(ring->data_dmat, data->map,
1305 BUS_DMASYNC_POSTWRITE);
1306 bus_dmamap_unload(ring->data_dmat, data->map);
1309 if (data->map != NULL)
1310 bus_dmamap_destroy(ring->data_dmat, data->map);
1312 if (ring->data_dmat != NULL) {
1313 bus_dma_tag_destroy(ring->data_dmat);
1314 ring->data_dmat = NULL;
1319 * Extract various information from EEPROM.
1322 wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1324 #define WPI_CHK(res) do { \
1325 if ((error = res) != 0) \
1330 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1332 /* Adapter has to be powered on for EEPROM access to work. */
1333 if ((error = wpi_apm_init(sc)) != 0) {
1334 device_printf(sc->sc_dev,
1335 "%s: could not power ON adapter, error %d\n", __func__,
1340 if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
1341 device_printf(sc->sc_dev, "bad EEPROM signature\n");
1345 /* Clear HW ownership of EEPROM. */
1346 WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
1348 /* Read the hardware capabilities, revision and SKU type. */
1349 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap,
1351 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,
1353 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type,
1356 sc->rev = le16toh(sc->rev);
1357 DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap,
1360 /* Read the regulatory domain (4 ASCII characters.) */
1361 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain,
1362 sizeof(sc->domain)));
1364 /* Read MAC address. */
1365 WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr,
1366 IEEE80211_ADDR_LEN));
1368 /* Read the list of authorized channels. */
1369 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
1370 WPI_CHK(wpi_read_eeprom_channels(sc, i));
1372 /* Read the list of TX power groups. */
1373 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
1374 WPI_CHK(wpi_read_eeprom_group(sc, i));
1376 fail: wpi_apm_stop(sc); /* Power OFF adapter. */
1378 DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
1386 * Translate EEPROM flags to net80211.
1389 wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel)
1394 if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0)
1395 nflags |= IEEE80211_CHAN_PASSIVE;
1396 if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0)
1397 nflags |= IEEE80211_CHAN_NOADHOC;
1398 if (channel->flags & WPI_EEPROM_CHAN_RADAR) {
1399 nflags |= IEEE80211_CHAN_DFS;
1400 /* XXX apparently IBSS may still be marked */
1401 nflags |= IEEE80211_CHAN_NOADHOC;
1404 /* XXX HOSTAP uses WPI_MODE_IBSS */
1405 if (nflags & IEEE80211_CHAN_NOADHOC)
1406 nflags |= IEEE80211_CHAN_NOHOSTAP;
1412 wpi_read_eeprom_band(struct wpi_softc *sc, int n)
1414 struct ifnet *ifp = sc->sc_ifp;
1415 struct ieee80211com *ic = ifp->if_l2com;
1416 struct wpi_eeprom_chan *channels = sc->eeprom_channels[n];
1417 const struct wpi_chan_band *band = &wpi_bands[n];
1418 struct ieee80211_channel *c;
1422 for (i = 0; i < band->nchan; i++) {
1423 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
1424 DPRINTF(sc, WPI_DEBUG_EEPROM,
1425 "Channel Not Valid: %d, band %d\n",
1430 chan = band->chan[i];
1431 nflags = wpi_eeprom_channel_flags(&channels[i]);
1433 c = &ic->ic_channels[ic->ic_nchans++];
1435 c->ic_maxregpower = channels[i].maxpwr;
1436 c->ic_maxpower = 2*c->ic_maxregpower;
1438 if (n == 0) { /* 2GHz band */
1439 c->ic_freq = ieee80211_ieee2mhz(chan,
1442 /* G =>'s B is supported */
1443 c->ic_flags = IEEE80211_CHAN_B | nflags;
1444 c = &ic->ic_channels[ic->ic_nchans++];
1446 c->ic_flags = IEEE80211_CHAN_G | nflags;
1447 } else { /* 5GHz band */
1448 c->ic_freq = ieee80211_ieee2mhz(chan,
1451 c->ic_flags = IEEE80211_CHAN_A | nflags;
1454 /* Save maximum allowed TX power for this channel. */
1455 sc->maxpwr[chan] = channels[i].maxpwr;
1457 DPRINTF(sc, WPI_DEBUG_EEPROM,
1458 "adding chan %d (%dMHz) flags=0x%x maxpwr=%d passive=%d,"
1459 " offset %d\n", chan, c->ic_freq,
1460 channels[i].flags, sc->maxpwr[chan],
1461 IEEE80211_IS_CHAN_PASSIVE(c), ic->ic_nchans);
1466 * Read the eeprom to find out what channels are valid for the given
1467 * band and update net80211 with what we find.
1470 wpi_read_eeprom_channels(struct wpi_softc *sc, int n)
1472 struct ifnet *ifp = sc->sc_ifp;
1473 struct ieee80211com *ic = ifp->if_l2com;
1474 const struct wpi_chan_band *band = &wpi_bands[n];
1477 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1479 error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n],
1480 band->nchan * sizeof (struct wpi_eeprom_chan));
1482 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1486 wpi_read_eeprom_band(sc, n);
1488 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1490 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1495 static struct wpi_eeprom_chan *
1496 wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c)
1500 for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++)
1501 for (i = 0; i < wpi_bands[j].nchan; i++)
1502 if (wpi_bands[j].chan[i] == c->ic_ieee)
1503 return &sc->eeprom_channels[j][i];
1509 * Enforce flags read from EEPROM.
1512 wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
1513 int nchan, struct ieee80211_channel chans[])
1515 struct ifnet *ifp = ic->ic_ifp;
1516 struct wpi_softc *sc = ifp->if_softc;
1519 for (i = 0; i < nchan; i++) {
1520 struct ieee80211_channel *c = &chans[i];
1521 struct wpi_eeprom_chan *channel;
1523 channel = wpi_find_eeprom_channel(sc, c);
1524 if (channel == NULL) {
1525 if_printf(ic->ic_ifp,
1526 "%s: invalid channel %u freq %u/0x%x\n",
1527 __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
1530 c->ic_flags |= wpi_eeprom_channel_flags(channel);
1537 wpi_read_eeprom_group(struct wpi_softc *sc, int n)
1539 struct wpi_power_group *group = &sc->groups[n];
1540 struct wpi_eeprom_group rgroup;
1543 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1545 if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32,
1546 &rgroup, sizeof rgroup)) != 0) {
1547 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1551 /* Save TX power group information. */
1552 group->chan = rgroup.chan;
1553 group->maxpwr = rgroup.maxpwr;
1554 /* Retrieve temperature at which the samples were taken. */
1555 group->temp = (int16_t)le16toh(rgroup.temp);
1557 DPRINTF(sc, WPI_DEBUG_EEPROM,
1558 "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan,
1559 group->maxpwr, group->temp);
1561 for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
1562 group->samples[i].index = rgroup.samples[i].index;
1563 group->samples[i].power = rgroup.samples[i].power;
1565 DPRINTF(sc, WPI_DEBUG_EEPROM,
1566 "\tsample %d: index=%d power=%d\n", i,
1567 group->samples[i].index, group->samples[i].power);
1570 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1576 wpi_add_node_entry_adhoc(struct wpi_softc *sc)
1578 int newid = WPI_ID_IBSS_MIN;
1580 for (; newid <= WPI_ID_IBSS_MAX; newid++) {
1581 if ((sc->nodesmsk & (1 << newid)) == 0) {
1582 sc->nodesmsk |= 1 << newid;
1587 return WPI_ID_UNDEFINED;
1591 wpi_add_node_entry_sta(struct wpi_softc *sc)
1593 sc->nodesmsk |= 1 << WPI_ID_BSS;
1599 wpi_check_node_entry(struct wpi_softc *sc, uint8_t id)
1601 if (id == WPI_ID_UNDEFINED)
1604 return (sc->nodesmsk >> id) & 1;
1607 static __inline void
1608 wpi_clear_node_table(struct wpi_softc *sc)
1613 static __inline void
1614 wpi_del_node_entry(struct wpi_softc *sc, uint8_t id)
1616 sc->nodesmsk &= ~(1 << id);
1619 static struct ieee80211_node *
1620 wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1622 struct wpi_node *wn;
1624 wn = malloc(sizeof (struct wpi_node), M_80211_NODE,
1630 wn->id = WPI_ID_UNDEFINED;
1636 wpi_node_free(struct ieee80211_node *ni)
1638 struct ieee80211com *ic = ni->ni_ic;
1639 struct wpi_softc *sc = ic->ic_ifp->if_softc;
1640 struct wpi_node *wn = WPI_NODE(ni);
1642 if (wn->id != WPI_ID_UNDEFINED) {
1644 if (wpi_check_node_entry(sc, wn->id)) {
1645 wpi_del_node_entry(sc, wn->id);
1646 wpi_del_node(sc, ni);
1651 sc->sc_node_free(ni);
1655 * Called by net80211 when ever there is a change to 80211 state machine
1658 wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1660 struct wpi_vap *wvp = WPI_VAP(vap);
1661 struct ieee80211com *ic = vap->iv_ic;
1662 struct ifnet *ifp = ic->ic_ifp;
1663 struct wpi_softc *sc = ifp->if_softc;
1666 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1668 DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1669 ieee80211_state_name[vap->iv_state],
1670 ieee80211_state_name[nstate]);
1672 if (vap->iv_state == IEEE80211_S_RUN && nstate != IEEE80211_S_RUN) {
1673 if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) {
1674 device_printf(sc->sc_dev,
1675 "%s: could not set power saving level\n",
1682 case IEEE80211_S_SCAN:
1684 if ((sc->rxon.filter & htole32(WPI_FILTER_BSS)) &&
1685 vap->iv_opmode != IEEE80211_M_STA) {
1686 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1687 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1688 device_printf(sc->sc_dev,
1689 "%s: could not send RXON\n", __func__);
1692 WPI_RXON_UNLOCK(sc);
1695 case IEEE80211_S_ASSOC:
1696 if (vap->iv_state != IEEE80211_S_RUN)
1699 case IEEE80211_S_AUTH:
1701 * The node must be registered in the firmware before auth.
1702 * Also the associd must be cleared on RUN -> ASSOC
1705 if ((error = wpi_auth(sc, vap)) != 0) {
1706 device_printf(sc->sc_dev,
1707 "%s: could not move to AUTH state, error %d\n",
1712 case IEEE80211_S_RUN:
1714 * RUN -> RUN transition; Just restart the timers.
1716 if (vap->iv_state == IEEE80211_S_RUN) {
1718 wpi_calib_timeout(sc);
1719 WPI_RXON_UNLOCK(sc);
1724 * !RUN -> RUN requires setting the association id
1725 * which is done with a firmware cmd. We also defer
1726 * starting the timers until that work is done.
1728 if ((error = wpi_run(sc, vap)) != 0) {
1729 device_printf(sc->sc_dev,
1730 "%s: could not move to RUN state\n", __func__);
1738 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1742 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1744 return wvp->wv_newstate(vap, nstate, arg);
1748 wpi_calib_timeout(void *arg)
1750 struct wpi_softc *sc = arg;
1752 if (!(sc->rxon.filter & htole32(WPI_FILTER_BSS)))
1755 wpi_power_calibration(sc);
1757 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
1760 static __inline uint8_t
1761 rate2plcp(const uint8_t rate)
1764 case 12: return 0xd;
1765 case 18: return 0xf;
1766 case 24: return 0x5;
1767 case 36: return 0x7;
1768 case 48: return 0x9;
1769 case 72: return 0xb;
1770 case 96: return 0x1;
1771 case 108: return 0x3;
1775 case 22: return 110;
1780 static __inline uint8_t
1781 plcp2rate(const uint8_t plcp)
1784 case 0xd: return 12;
1785 case 0xf: return 18;
1786 case 0x5: return 24;
1787 case 0x7: return 36;
1788 case 0x9: return 48;
1789 case 0xb: return 72;
1790 case 0x1: return 96;
1791 case 0x3: return 108;
1795 case 110: return 22;
1800 /* Quickly determine if a given rate is CCK or OFDM. */
1801 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1804 wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1805 struct wpi_rx_data *data)
1807 struct ifnet *ifp = sc->sc_ifp;
1808 struct ieee80211com *ic = ifp->if_l2com;
1809 struct wpi_rx_ring *ring = &sc->rxq;
1810 struct wpi_rx_stat *stat;
1811 struct wpi_rx_head *head;
1812 struct wpi_rx_tail *tail;
1813 struct ieee80211_frame *wh;
1814 struct ieee80211_node *ni;
1815 struct mbuf *m, *m1;
1821 stat = (struct wpi_rx_stat *)(desc + 1);
1823 if (stat->len > WPI_STAT_MAXLEN) {
1824 device_printf(sc->sc_dev, "invalid RX statistic header\n");
1828 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
1829 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1830 len = le16toh(head->len);
1831 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len);
1832 flags = le32toh(tail->flags);
1834 DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d"
1835 " rate %x chan %d tstamp %ju\n", __func__, ring->cur,
1836 le32toh(desc->len), len, (int8_t)stat->rssi,
1837 head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp));
1839 /* Discard frames with a bad FCS early. */
1840 if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1841 DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n",
1845 /* Discard frames that are too short. */
1846 if (len < sizeof (*wh)) {
1847 DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n",
1852 m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1854 DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n",
1858 bus_dmamap_unload(ring->data_dmat, data->map);
1860 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *),
1861 MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1862 if (error != 0 && error != EFBIG) {
1863 device_printf(sc->sc_dev,
1864 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1867 /* Try to reload the old mbuf. */
1868 error = bus_dmamap_load(ring->data_dmat, data->map,
1869 mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1870 &paddr, BUS_DMA_NOWAIT);
1871 if (error != 0 && error != EFBIG) {
1872 panic("%s: could not load old RX mbuf", __func__);
1874 /* Physical address may have changed. */
1875 ring->desc[ring->cur] = htole32(paddr);
1876 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map,
1877 BUS_DMASYNC_PREWRITE);
1883 /* Update RX descriptor. */
1884 ring->desc[ring->cur] = htole32(paddr);
1885 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1886 BUS_DMASYNC_PREWRITE);
1888 /* Finalize mbuf. */
1889 m->m_pkthdr.rcvif = ifp;
1890 m->m_data = (caddr_t)(head + 1);
1891 m->m_pkthdr.len = m->m_len = len;
1893 /* Grab a reference to the source node. */
1894 wh = mtod(m, struct ieee80211_frame *);
1896 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1897 (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) {
1898 /* Check whether decryption was successful or not. */
1899 if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
1900 DPRINTF(sc, WPI_DEBUG_RECV,
1901 "CCMP decryption failed 0x%x\n", flags);
1904 m->m_flags |= M_WEP;
1907 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1909 if (ieee80211_radiotap_active(ic)) {
1910 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1913 if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE))
1914 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1915 tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET);
1916 tap->wr_dbm_antnoise = WPI_RSSI_OFFSET;
1917 tap->wr_tsft = tail->tstamp;
1918 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1919 tap->wr_rate = plcp2rate(head->plcp);
1924 /* Send the frame to the 802.11 layer. */
1926 (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET);
1927 /* Node is no longer needed. */
1928 ieee80211_free_node(ni);
1930 (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET);
1938 fail1: if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1942 wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1943 struct wpi_rx_data *data)
1949 wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1951 struct ifnet *ifp = sc->sc_ifp;
1952 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1953 struct wpi_tx_data *data = &ring->data[desc->idx];
1954 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1956 struct ieee80211_node *ni;
1957 struct ieee80211vap *vap;
1958 struct ieee80211com *ic;
1959 uint32_t status = le32toh(stat->status);
1960 int ackfailcnt = stat->ackfailcnt / WPI_NTRIES_DEFAULT;
1962 KASSERT(data->ni != NULL, ("no node"));
1963 KASSERT(data->m != NULL, ("no mbuf"));
1965 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1967 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: "
1968 "qid %d idx %d retries %d btkillcnt %d rate %x duration %d "
1969 "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt,
1970 stat->btkillcnt, stat->rate, le32toh(stat->duration), status);
1972 /* Unmap and free mbuf. */
1973 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1974 bus_dmamap_unload(ring->data_dmat, data->map);
1975 m = data->m, data->m = NULL;
1976 ni = data->ni, data->ni = NULL;
1981 * Update rate control statistics for the node.
1983 if ((status & 0xff) != 1) {
1984 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1985 ieee80211_ratectl_tx_complete(vap, ni,
1986 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL);
1988 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1989 ieee80211_ratectl_tx_complete(vap, ni,
1990 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL);
1993 ieee80211_tx_complete(ni, m, (status & 0xff) != 1);
1995 WPI_TXQ_STATE_LOCK(sc);
1997 if (ring->queued > 0) {
1998 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2000 if (sc->qfullmsk != 0 &&
2001 ring->queued < WPI_TX_RING_LOMARK) {
2002 sc->qfullmsk &= ~(1 << ring->qid);
2003 IF_LOCK(&ifp->if_snd);
2004 if (sc->qfullmsk == 0 &&
2005 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
2006 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2007 IF_UNLOCK(&ifp->if_snd);
2008 ieee80211_runtask(ic, &sc->sc_start_task);
2010 IF_UNLOCK(&ifp->if_snd);
2013 callout_stop(&sc->tx_timeout);
2014 WPI_TXQ_STATE_UNLOCK(sc);
2016 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2020 * Process a "command done" firmware notification. This is where we wakeup
2021 * processes waiting for a synchronous command completion.
2024 wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2026 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
2027 struct wpi_tx_data *data;
2029 DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x "
2030 "type %s len %d\n", desc->qid, desc->idx,
2031 desc->flags, wpi_cmd_str(desc->type),
2032 le32toh(desc->len));
2034 if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM)
2035 return; /* Not a command ack. */
2037 KASSERT(ring->queued == 0, ("ring->queued must be 0"));
2039 data = &ring->data[desc->idx];
2041 /* If the command was mapped in an mbuf, free it. */
2042 if (data->m != NULL) {
2043 bus_dmamap_sync(ring->data_dmat, data->map,
2044 BUS_DMASYNC_POSTWRITE);
2045 bus_dmamap_unload(ring->data_dmat, data->map);
2050 wakeup(&ring->cmd[desc->idx]);
2054 wpi_notif_intr(struct wpi_softc *sc)
2056 struct ifnet *ifp = sc->sc_ifp;
2057 struct ieee80211com *ic = ifp->if_l2com;
2058 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2061 bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
2062 BUS_DMASYNC_POSTREAD);
2064 hw = le32toh(sc->shared->next);
2065 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
2067 while (sc->rxq.cur != hw) {
2068 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
2070 struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2071 struct wpi_rx_desc *desc;
2073 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2074 BUS_DMASYNC_POSTREAD);
2075 desc = mtod(data->m, struct wpi_rx_desc *);
2077 DPRINTF(sc, WPI_DEBUG_NOTIFY,
2078 "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n",
2079 __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags,
2080 desc->type, wpi_cmd_str(desc->type), le32toh(desc->len));
2082 if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) {
2083 /* Reply to a command. */
2084 wpi_cmd_done(sc, desc);
2087 switch (desc->type) {
2089 /* An 802.11 frame has been received. */
2090 wpi_rx_done(sc, desc, data);
2092 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2093 /* wpi_stop() was called. */
2100 /* An 802.11 frame has been transmitted. */
2101 wpi_tx_done(sc, desc);
2104 case WPI_RX_STATISTICS:
2105 case WPI_BEACON_STATISTICS:
2106 wpi_rx_statistics(sc, desc, data);
2109 case WPI_BEACON_MISSED:
2111 struct wpi_beacon_missed *miss =
2112 (struct wpi_beacon_missed *)(desc + 1);
2115 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2116 BUS_DMASYNC_POSTREAD);
2117 misses = le32toh(miss->consecutive);
2119 DPRINTF(sc, WPI_DEBUG_STATE,
2120 "%s: beacons missed %d/%d\n", __func__, misses,
2121 le32toh(miss->total));
2123 if (vap->iv_state == IEEE80211_S_RUN &&
2124 (ic->ic_flags & IEEE80211_F_SCAN) == 0 &&
2125 misses >= vap->iv_bmissthreshold)
2126 ieee80211_beacon_miss(ic);
2132 struct wpi_ucode_info *uc =
2133 (struct wpi_ucode_info *)(desc + 1);
2135 /* The microcontroller is ready. */
2136 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2137 BUS_DMASYNC_POSTREAD);
2138 DPRINTF(sc, WPI_DEBUG_RESET,
2139 "microcode alive notification version=%d.%d "
2140 "subtype=%x alive=%x\n", uc->major, uc->minor,
2141 uc->subtype, le32toh(uc->valid));
2143 if (le32toh(uc->valid) != 1) {
2144 device_printf(sc->sc_dev,
2145 "microcontroller initialization failed\n");
2146 wpi_stop_locked(sc);
2148 /* Save the address of the error log in SRAM. */
2149 sc->errptr = le32toh(uc->errptr);
2152 case WPI_STATE_CHANGED:
2154 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2155 BUS_DMASYNC_POSTREAD);
2157 uint32_t *status = (uint32_t *)(desc + 1);
2159 DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n",
2162 if (le32toh(*status) & 1) {
2164 wpi_clear_node_table(sc);
2166 ieee80211_runtask(ic, &sc->sc_radiooff_task);
2171 case WPI_START_SCAN:
2173 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2174 BUS_DMASYNC_POSTREAD);
2176 struct wpi_start_scan *scan =
2177 (struct wpi_start_scan *)(desc + 1);
2178 DPRINTF(sc, WPI_DEBUG_SCAN,
2179 "%s: scanning channel %d status %x\n",
2180 __func__, scan->chan, le32toh(scan->status));
2186 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2187 BUS_DMASYNC_POSTREAD);
2189 struct wpi_stop_scan *scan =
2190 (struct wpi_stop_scan *)(desc + 1);
2191 DPRINTF(sc, WPI_DEBUG_SCAN,
2192 "scan finished nchan=%d status=%d chan=%d\n",
2193 scan->nchan, scan->status, scan->chan);
2196 callout_stop(&sc->scan_timeout);
2197 WPI_RXON_UNLOCK(sc);
2198 ieee80211_scan_next(vap);
2203 if (sc->rxq.cur % 8 == 0) {
2204 /* Tell the firmware what we have processed. */
2205 wpi_update_rx_ring(sc);
2211 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2212 * from power-down sleep mode.
2215 wpi_wakeup_intr(struct wpi_softc *sc)
2219 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
2220 "%s: ucode wakeup from power-down sleep\n", __func__);
2222 /* Wakeup RX and TX rings. */
2223 if (sc->rxq.update) {
2225 wpi_update_rx_ring(sc);
2228 for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) {
2229 struct wpi_tx_ring *ring = &sc->txq[qid];
2233 wpi_update_tx_ring(sc, ring);
2238 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
2242 * This function prints firmware registers
2246 wpi_debug_registers(struct wpi_softc *sc)
2248 #define COUNTOF(array) (sizeof(array) / sizeof(array[0]))
2250 static const uint32_t csr_tbl[] = {
2267 static const uint32_t prph_tbl[] = {
2274 DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n");
2276 for (i = 0; i < COUNTOF(csr_tbl); i++) {
2277 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2278 wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i]));
2280 if ((i + 1) % 2 == 0)
2281 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2283 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n");
2285 if (wpi_nic_lock(sc) == 0) {
2286 for (i = 0; i < COUNTOF(prph_tbl); i++) {
2287 DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2288 wpi_get_prph_string(prph_tbl[i]),
2289 wpi_prph_read(sc, prph_tbl[i]));
2291 if ((i + 1) % 2 == 0)
2292 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2294 DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2297 DPRINTF(sc, WPI_DEBUG_REGISTER,
2298 "Cannot access internal registers.\n");
2305 * Dump the error log of the firmware when a firmware panic occurs. Although
2306 * we can't debug the firmware because it is neither open source nor free, it
2307 * can help us to identify certain classes of problems.
2310 wpi_fatal_intr(struct wpi_softc *sc)
2312 struct wpi_fw_dump dump;
2313 uint32_t i, offset, count;
2314 const uint32_t size_errmsg =
2315 (sizeof (wpi_fw_errmsg) / sizeof ((wpi_fw_errmsg)[0]));
2317 /* Check that the error log address is valid. */
2318 if (sc->errptr < WPI_FW_DATA_BASE ||
2319 sc->errptr + sizeof (dump) >
2320 WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
2321 printf("%s: bad firmware error log address 0x%08x\n", __func__,
2325 if (wpi_nic_lock(sc) != 0) {
2326 printf("%s: could not read firmware error log\n", __func__);
2329 /* Read number of entries in the log. */
2330 count = wpi_mem_read(sc, sc->errptr);
2331 if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
2332 printf("%s: invalid count field (count = %u)\n", __func__,
2337 /* Skip "count" field. */
2338 offset = sc->errptr + sizeof (uint32_t);
2339 printf("firmware error log (count = %u):\n", count);
2340 for (i = 0; i < count; i++) {
2341 wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
2342 sizeof (dump) / sizeof (uint32_t));
2344 printf(" error type = \"%s\" (0x%08X)\n",
2345 (dump.desc < size_errmsg) ?
2346 wpi_fw_errmsg[dump.desc] : "UNKNOWN",
2348 printf(" error data = 0x%08X\n",
2350 printf(" branch link = 0x%08X%08X\n",
2351 dump.blink[0], dump.blink[1]);
2352 printf(" interrupt link = 0x%08X%08X\n",
2353 dump.ilink[0], dump.ilink[1]);
2354 printf(" time = %u\n", dump.time);
2356 offset += sizeof (dump);
2359 /* Dump driver status (TX and RX rings) while we're here. */
2360 printf("driver status:\n");
2362 for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
2363 struct wpi_tx_ring *ring = &sc->txq[i];
2364 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2365 i, ring->qid, ring->cur, ring->queued);
2368 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2374 struct wpi_softc *sc = arg;
2375 struct ifnet *ifp = sc->sc_ifp;
2380 /* Disable interrupts. */
2381 WPI_WRITE(sc, WPI_INT_MASK, 0);
2383 r1 = WPI_READ(sc, WPI_INT);
2385 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
2386 goto end; /* Hardware gone! */
2388 r2 = WPI_READ(sc, WPI_FH_INT);
2390 DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__,
2393 if (r1 == 0 && r2 == 0)
2394 goto done; /* Interrupt not for us. */
2396 /* Acknowledge interrupts. */
2397 WPI_WRITE(sc, WPI_INT, r1);
2398 WPI_WRITE(sc, WPI_FH_INT, r2);
2400 if (r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR)) {
2401 device_printf(sc->sc_dev, "fatal firmware error\n");
2403 wpi_debug_registers(sc);
2406 DPRINTF(sc, WPI_DEBUG_HW,
2407 "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" :
2408 "(Hardware Error)");
2409 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2413 if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
2414 (r2 & WPI_FH_INT_RX))
2417 if (r1 & WPI_INT_ALIVE)
2418 wakeup(sc); /* Firmware is alive. */
2420 if (r1 & WPI_INT_WAKEUP)
2421 wpi_wakeup_intr(sc);
2424 /* Re-enable interrupts. */
2425 if (ifp->if_flags & IFF_UP)
2426 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
2428 end: WPI_UNLOCK(sc);
2432 wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf)
2434 struct ifnet *ifp = sc->sc_ifp;
2435 struct ieee80211_frame *wh;
2436 struct wpi_tx_cmd *cmd;
2437 struct wpi_tx_data *data;
2438 struct wpi_tx_desc *desc;
2439 struct wpi_tx_ring *ring;
2441 bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER];
2442 int error, i, hdrlen, nsegs, totlen, pad;
2446 KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow"));
2448 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2450 if (sc->txq_active == 0) {
2451 /* wpi_stop() was called */
2456 wh = mtod(buf->m, struct ieee80211_frame *);
2457 hdrlen = ieee80211_anyhdrsize(wh);
2458 totlen = buf->m->m_pkthdr.len;
2461 /* First segment length must be a multiple of 4. */
2462 pad = 4 - (hdrlen & 3);
2466 ring = &sc->txq[buf->ac];
2467 desc = &ring->desc[ring->cur];
2468 data = &ring->data[ring->cur];
2470 /* Prepare TX firmware command. */
2471 cmd = &ring->cmd[ring->cur];
2472 cmd->code = buf->code;
2474 cmd->qid = ring->qid;
2475 cmd->idx = ring->cur;
2477 memcpy(cmd->data, buf->data, buf->size);
2479 /* Save and trim IEEE802.11 header. */
2480 memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen);
2481 m_adj(buf->m, hdrlen);
2483 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m,
2484 segs, &nsegs, BUS_DMA_NOWAIT);
2485 if (error != 0 && error != EFBIG) {
2486 device_printf(sc->sc_dev,
2487 "%s: can't map mbuf (error %d)\n", __func__, error);
2491 /* Too many DMA segments, linearize mbuf. */
2492 m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1);
2494 device_printf(sc->sc_dev,
2495 "%s: could not defrag mbuf\n", __func__);
2501 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
2502 buf->m, segs, &nsegs, BUS_DMA_NOWAIT);
2504 device_printf(sc->sc_dev,
2505 "%s: can't map mbuf (error %d)\n", __func__,
2511 KASSERT(nsegs < WPI_MAX_SCATTER,
2512 ("too many DMA segments, nsegs (%d) should be less than %d",
2513 nsegs, WPI_MAX_SCATTER));
2518 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
2519 __func__, ring->qid, ring->cur, totlen, nsegs);
2521 /* Fill TX descriptor. */
2522 desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs);
2523 /* First DMA segment is used by the TX command. */
2524 desc->segs[0].addr = htole32(data->cmd_paddr);
2525 desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad);
2526 /* Other DMA segments are for data payload. */
2528 for (i = 1; i <= nsegs; i++) {
2529 desc->segs[i].addr = htole32(seg->ds_addr);
2530 desc->segs[i].len = htole32(seg->ds_len);
2534 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2535 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2536 BUS_DMASYNC_PREWRITE);
2537 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2538 BUS_DMASYNC_PREWRITE);
2541 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
2542 wpi_update_tx_ring(sc, ring);
2544 if (ring->qid < WPI_CMD_QUEUE_NUM) {
2545 /* Mark TX ring as full if we reach a certain threshold. */
2546 WPI_TXQ_STATE_LOCK(sc);
2547 if (++ring->queued > WPI_TX_RING_HIMARK) {
2548 sc->qfullmsk |= 1 << ring->qid;
2550 IF_LOCK(&ifp->if_snd);
2551 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2552 IF_UNLOCK(&ifp->if_snd);
2555 callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2556 WPI_TXQ_STATE_UNLOCK(sc);
2559 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2565 fail: m_freem(buf->m);
2567 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
2575 * Construct the data packet for a transmit buffer.
2578 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2580 const struct ieee80211_txparam *tp;
2581 struct ieee80211vap *vap = ni->ni_vap;
2582 struct ieee80211com *ic = ni->ni_ic;
2583 struct wpi_node *wn = WPI_NODE(ni);
2584 struct ieee80211_channel *chan;
2585 struct ieee80211_frame *wh;
2586 struct ieee80211_key *k = NULL;
2587 struct wpi_buf tx_data;
2588 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2592 int ac, error, swcrypt, rate, ismcast, totlen;
2594 wh = mtod(m, struct ieee80211_frame *);
2595 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2596 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2598 /* Select EDCA Access Category and TX ring for this frame. */
2599 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2600 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2601 tid = qos & IEEE80211_QOS_TID;
2606 ac = M_WME_GETAC(m);
2608 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC) ?
2609 ni->ni_chan : ic->ic_curchan;
2610 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2612 /* Choose a TX rate index. */
2613 if (type == IEEE80211_FC0_TYPE_MGT)
2614 rate = tp->mgmtrate;
2616 rate = tp->mcastrate;
2617 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2618 rate = tp->ucastrate;
2619 else if (m->m_flags & M_EAPOL)
2620 rate = tp->mgmtrate;
2622 /* XXX pass pktlen */
2623 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2624 rate = ni->ni_txrate;
2627 /* Encrypt the frame if need be. */
2628 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2629 /* Retrieve key for TX. */
2630 k = ieee80211_crypto_encap(ni, m);
2635 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2637 /* 802.11 header may have moved. */
2638 wh = mtod(m, struct ieee80211_frame *);
2640 totlen = m->m_pkthdr.len;
2642 if (ieee80211_radiotap_active_vap(vap)) {
2643 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2646 tap->wt_rate = rate;
2648 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2650 ieee80211_radiotap_tx(vap, m);
2655 /* Unicast frame, check if an ACK is expected. */
2656 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2657 IEEE80211_QOS_ACKPOLICY_NOACK)
2658 flags |= WPI_TX_NEED_ACK;
2661 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2662 flags |= WPI_TX_MORE_FRAG; /* Cannot happen yet. */
2664 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2666 /* NB: Group frames are sent using CCK in 802.11b/g. */
2667 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2668 flags |= WPI_TX_NEED_RTS;
2669 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2670 WPI_RATE_IS_OFDM(rate)) {
2671 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2672 flags |= WPI_TX_NEED_CTS;
2673 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2674 flags |= WPI_TX_NEED_RTS;
2677 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2678 flags |= WPI_TX_FULL_TXOP;
2681 memset(tx, 0, sizeof (struct wpi_cmd_data));
2682 if (type == IEEE80211_FC0_TYPE_MGT) {
2683 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2685 /* Tell HW to set timestamp in probe responses. */
2686 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2687 flags |= WPI_TX_INSERT_TSTAMP;
2688 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2689 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2690 tx->timeout = htole16(3);
2692 tx->timeout = htole16(2);
2695 if (ismcast || type != IEEE80211_FC0_TYPE_DATA)
2696 tx->id = WPI_ID_BROADCAST;
2698 if (wn->id == WPI_ID_UNDEFINED) {
2699 device_printf(sc->sc_dev,
2700 "%s: undefined node id\n", __func__);
2708 if (type != IEEE80211_FC0_TYPE_MGT)
2709 tx->data_ntries = tp->maxretry;
2711 if (k != NULL && !swcrypt) {
2712 switch (k->wk_cipher->ic_cipher) {
2713 case IEEE80211_CIPHER_AES_CCM:
2714 tx->security = WPI_CIPHER_CCMP;
2721 memcpy(tx->key, k->wk_key, k->wk_keylen);
2724 tx->len = htole16(totlen);
2725 tx->flags = htole32(flags);
2726 tx->plcp = rate2plcp(rate);
2728 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2729 tx->ofdm_mask = 0xff;
2730 tx->cck_mask = 0x0f;
2735 tx_data.size = sizeof(struct wpi_cmd_data);
2736 tx_data.code = WPI_CMD_TX_DATA;
2739 return wpi_cmd2(sc, &tx_data);
2746 wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m,
2747 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
2749 struct ieee80211vap *vap = ni->ni_vap;
2750 struct ieee80211_key *k = NULL;
2751 struct ieee80211_frame *wh;
2752 struct wpi_buf tx_data;
2753 struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2756 int ac, rate, swcrypt, totlen;
2758 wh = mtod(m, struct ieee80211_frame *);
2759 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2761 ac = params->ibp_pri & 3;
2763 /* Choose a TX rate index. */
2764 rate = params->ibp_rate0;
2767 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
2768 flags |= WPI_TX_NEED_ACK;
2769 if (params->ibp_flags & IEEE80211_BPF_RTS)
2770 flags |= WPI_TX_NEED_RTS;
2771 if (params->ibp_flags & IEEE80211_BPF_CTS)
2772 flags |= WPI_TX_NEED_CTS;
2773 if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2774 flags |= WPI_TX_FULL_TXOP;
2776 /* Encrypt the frame if need be. */
2777 if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
2778 /* Retrieve key for TX. */
2779 k = ieee80211_crypto_encap(ni, m);
2784 swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2786 /* 802.11 header may have moved. */
2787 wh = mtod(m, struct ieee80211_frame *);
2789 totlen = m->m_pkthdr.len;
2791 if (ieee80211_radiotap_active_vap(vap)) {
2792 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2795 tap->wt_rate = rate;
2796 if (params->ibp_flags & IEEE80211_BPF_CRYPTO)
2797 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2799 ieee80211_radiotap_tx(vap, m);
2802 memset(tx, 0, sizeof (struct wpi_cmd_data));
2803 if (type == IEEE80211_FC0_TYPE_MGT) {
2804 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2806 /* Tell HW to set timestamp in probe responses. */
2807 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2808 flags |= WPI_TX_INSERT_TSTAMP;
2809 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2810 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2811 tx->timeout = htole16(3);
2813 tx->timeout = htole16(2);
2816 if (k != NULL && !swcrypt) {
2817 switch (k->wk_cipher->ic_cipher) {
2818 case IEEE80211_CIPHER_AES_CCM:
2819 tx->security = WPI_CIPHER_CCMP;
2826 memcpy(tx->key, k->wk_key, k->wk_keylen);
2829 tx->len = htole16(totlen);
2830 tx->flags = htole32(flags);
2831 tx->plcp = rate2plcp(rate);
2832 tx->id = WPI_ID_BROADCAST;
2833 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2834 tx->rts_ntries = params->ibp_try1;
2835 tx->data_ntries = params->ibp_try0;
2839 tx_data.size = sizeof(struct wpi_cmd_data);
2840 tx_data.code = WPI_CMD_TX_DATA;
2843 return wpi_cmd2(sc, &tx_data);
2847 wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2848 const struct ieee80211_bpf_params *params)
2850 struct ieee80211com *ic = ni->ni_ic;
2851 struct ifnet *ifp = ic->ic_ifp;
2852 struct wpi_softc *sc = ifp->if_softc;
2855 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2857 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2858 ieee80211_free_node(ni);
2864 if (params == NULL) {
2866 * Legacy path; interpret frame contents to decide
2867 * precisely how to send the frame.
2869 error = wpi_tx_data(sc, m, ni);
2872 * Caller supplied explicit parameters to use in
2873 * sending the frame.
2875 error = wpi_tx_data_raw(sc, m, ni, params);
2880 /* NB: m is reclaimed on tx failure */
2881 ieee80211_free_node(ni);
2882 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2884 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
2889 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2895 * Process data waiting to be sent on the IFNET output queue
2898 wpi_start(struct ifnet *ifp)
2900 struct wpi_softc *sc = ifp->if_softc;
2901 struct ieee80211_node *ni;
2905 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
2908 IF_LOCK(&ifp->if_snd);
2909 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2910 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
2911 IF_UNLOCK(&ifp->if_snd);
2914 IF_UNLOCK(&ifp->if_snd);
2916 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
2919 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2920 if (wpi_tx_data(sc, m, ni) != 0) {
2921 ieee80211_free_node(ni);
2922 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2926 DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
2931 wpi_start_task(void *arg0, int pending)
2933 struct wpi_softc *sc = arg0;
2934 struct ifnet *ifp = sc->sc_ifp;
2940 wpi_watchdog_rfkill(void *arg)
2942 struct wpi_softc *sc = arg;
2943 struct ifnet *ifp = sc->sc_ifp;
2944 struct ieee80211com *ic = ifp->if_l2com;
2946 DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n");
2948 /* No need to lock firmware memory. */
2949 if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) {
2950 /* Radio kill switch is still off. */
2951 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
2954 ieee80211_runtask(ic, &sc->sc_radioon_task);
2958 wpi_scan_timeout(void *arg)
2960 struct wpi_softc *sc = arg;
2961 struct ifnet *ifp = sc->sc_ifp;
2963 if_printf(ifp, "scan timeout\n");
2964 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2968 wpi_tx_timeout(void *arg)
2970 struct wpi_softc *sc = arg;
2971 struct ifnet *ifp = sc->sc_ifp;
2973 if_printf(ifp, "device timeout\n");
2974 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2975 taskqueue_enqueue(sc->sc_tq, &sc->sc_reinittask);
2979 wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2981 struct wpi_softc *sc = ifp->if_softc;
2982 struct ieee80211com *ic = ifp->if_l2com;
2983 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2984 struct ifreq *ifr = (struct ifreq *) data;
2989 error = ether_ioctl(ifp, cmd, data);
2992 if (ifp->if_flags & IFF_UP) {
2995 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 &&
2997 ieee80211_stop(vap);
2998 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3002 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3012 * Send a command to the firmware.
3015 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, size_t size,
3018 struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
3019 struct wpi_tx_desc *desc;
3020 struct wpi_tx_data *data;
3021 struct wpi_tx_cmd *cmd;
3028 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3030 if (sc->txq_active == 0) {
3031 /* wpi_stop() was called */
3037 WPI_LOCK_ASSERT(sc);
3039 DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %zu async %d\n",
3040 __func__, wpi_cmd_str(code), size, async);
3042 desc = &ring->desc[ring->cur];
3043 data = &ring->data[ring->cur];
3046 if (size > sizeof cmd->data) {
3047 /* Command is too large to fit in a descriptor. */
3048 if (totlen > MCLBYTES) {
3052 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3057 cmd = mtod(m, struct wpi_tx_cmd *);
3058 error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3059 totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3066 cmd = &ring->cmd[ring->cur];
3067 paddr = data->cmd_paddr;
3072 cmd->qid = ring->qid;
3073 cmd->idx = ring->cur;
3074 memcpy(cmd->data, buf, size);
3076 desc->nsegs = 1 + (WPI_PAD32(size) << 4);
3077 desc->segs[0].addr = htole32(paddr);
3078 desc->segs[0].len = htole32(totlen);
3080 if (size > sizeof cmd->data) {
3081 bus_dmamap_sync(ring->data_dmat, data->map,
3082 BUS_DMASYNC_PREWRITE);
3084 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3085 BUS_DMASYNC_PREWRITE);
3087 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3088 BUS_DMASYNC_PREWRITE);
3090 /* Kick command ring. */
3091 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
3092 wpi_update_tx_ring(sc, ring);
3094 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3101 return mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
3103 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3111 * Configure HW multi-rate retries.
3114 wpi_mrr_setup(struct wpi_softc *sc)
3116 struct ifnet *ifp = sc->sc_ifp;
3117 struct ieee80211com *ic = ifp->if_l2com;
3118 struct wpi_mrr_setup mrr;
3121 /* CCK rates (not used with 802.11a). */
3122 for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
3123 mrr.rates[i].flags = 0;
3124 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3125 /* Fallback to the immediate lower CCK rate (if any.) */
3127 (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
3128 /* Try twice at this rate before falling back to "next". */
3129 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3131 /* OFDM rates (not used with 802.11b). */
3132 for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
3133 mrr.rates[i].flags = 0;
3134 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3135 /* Fallback to the immediate lower rate (if any.) */
3136 /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
3137 mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
3138 ((ic->ic_curmode == IEEE80211_MODE_11A) ?
3139 WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
3141 /* Try twice at this rate before falling back to "next". */
3142 mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
3144 /* Setup MRR for control frames. */
3145 mrr.which = htole32(WPI_MRR_CTL);
3146 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3148 device_printf(sc->sc_dev,
3149 "could not setup MRR for control frames\n");
3152 /* Setup MRR for data frames. */
3153 mrr.which = htole32(WPI_MRR_DATA);
3154 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
3156 device_printf(sc->sc_dev,
3157 "could not setup MRR for data frames\n");
3164 wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3166 struct ieee80211com *ic = ni->ni_ic;
3167 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
3168 struct wpi_node *wn = WPI_NODE(ni);
3169 struct wpi_node_info node;
3172 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3174 if (wn->id == WPI_ID_UNDEFINED)
3177 memset(&node, 0, sizeof node);
3178 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3180 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3181 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3182 node.action = htole32(WPI_ACTION_SET_RATE);
3183 node.antenna = WPI_ANTENNA_BOTH;
3185 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding node %d (%s)\n", __func__,
3186 wn->id, ether_sprintf(ni->ni_macaddr));
3188 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
3190 device_printf(sc->sc_dev,
3191 "%s: wpi_cmd() call failed with error code %d\n", __func__,
3196 if (wvp->wv_gtk != 0) {
3197 error = wpi_set_global_keys(ni);
3199 device_printf(sc->sc_dev,
3200 "%s: error while setting global keys\n", __func__);
3209 * Broadcast node is used to send group-addressed and management frames.
3212 wpi_add_broadcast_node(struct wpi_softc *sc, int async)
3214 struct ifnet *ifp = sc->sc_ifp;
3215 struct ieee80211com *ic = ifp->if_l2com;
3216 struct wpi_node_info node;
3218 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3220 memset(&node, 0, sizeof node);
3221 IEEE80211_ADDR_COPY(node.macaddr, ifp->if_broadcastaddr);
3222 node.id = WPI_ID_BROADCAST;
3223 node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3224 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3225 node.action = htole32(WPI_ACTION_SET_RATE);
3226 node.antenna = WPI_ANTENNA_BOTH;
3228 DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__);
3230 return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async);
3234 wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3236 struct wpi_node *wn = WPI_NODE(ni);
3239 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3241 wn->id = wpi_add_node_entry_sta(sc);
3243 if ((error = wpi_add_node(sc, ni)) != 0) {
3244 wpi_del_node_entry(sc, wn->id);
3245 wn->id = WPI_ID_UNDEFINED;
3253 wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3255 struct wpi_node *wn = WPI_NODE(ni);
3258 KASSERT(wn->id == WPI_ID_UNDEFINED,
3259 ("the node %d was added before", wn->id));
3261 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3263 if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) {
3264 device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__);
3268 if ((error = wpi_add_node(sc, ni)) != 0) {
3269 wpi_del_node_entry(sc, wn->id);
3270 wn->id = WPI_ID_UNDEFINED;
3278 wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3280 struct wpi_node *wn = WPI_NODE(ni);
3281 struct wpi_cmd_del_node node;
3284 KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed"));
3286 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3288 memset(&node, 0, sizeof node);
3289 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3292 DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__,
3293 wn->id, ether_sprintf(ni->ni_macaddr));
3295 error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1);
3297 device_printf(sc->sc_dev,
3298 "%s: could not delete node %u, error %d\n", __func__,
3304 wpi_updateedca(struct ieee80211com *ic)
3306 #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3307 struct wpi_softc *sc = ic->ic_ifp->if_softc;
3308 struct wpi_edca_params cmd;
3311 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3313 memset(&cmd, 0, sizeof cmd);
3314 cmd.flags = htole32(WPI_EDCA_UPDATE);
3315 for (aci = 0; aci < WME_NUM_AC; aci++) {
3316 const struct wmeParams *ac =
3317 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci];
3318 cmd.ac[aci].aifsn = ac->wmep_aifsn;
3319 cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin));
3320 cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax));
3321 cmd.ac[aci].txoplimit =
3322 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit));
3324 DPRINTF(sc, WPI_DEBUG_EDCA,
3325 "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
3326 "txoplimit=%d\n", aci, cmd.ac[aci].aifsn,
3327 cmd.ac[aci].cwmin, cmd.ac[aci].cwmax,
3328 cmd.ac[aci].txoplimit);
3330 error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3332 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3339 wpi_set_promisc(struct wpi_softc *sc)
3341 struct ifnet *ifp = sc->sc_ifp;
3342 struct ieee80211com *ic = ifp->if_l2com;
3343 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3344 uint32_t promisc_filter;
3346 promisc_filter = WPI_FILTER_CTL;
3347 if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP)
3348 promisc_filter |= WPI_FILTER_PROMISC;
3350 if (ifp->if_flags & IFF_PROMISC)
3351 sc->rxon.filter |= htole32(promisc_filter);
3353 sc->rxon.filter &= ~htole32(promisc_filter);
3357 wpi_update_promisc(struct ifnet *ifp)
3359 struct wpi_softc *sc = ifp->if_softc;
3362 wpi_set_promisc(sc);
3364 if (wpi_send_rxon(sc, 1, 1) != 0) {
3365 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3368 WPI_RXON_UNLOCK(sc);
3372 wpi_update_mcast(struct ifnet *ifp)
3378 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3380 struct wpi_cmd_led led;
3382 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3385 led.unit = htole32(100000); /* on/off in unit of 100ms */
3388 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
3392 wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
3394 struct wpi_cmd_timing cmd;
3397 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3399 memset(&cmd, 0, sizeof cmd);
3400 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3401 cmd.bintval = htole16(ni->ni_intval);
3402 cmd.lintval = htole16(10);
3404 /* Compute remaining time until next beacon. */
3405 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU;
3406 mod = le64toh(cmd.tstamp) % val;
3407 cmd.binitval = htole32((uint32_t)(val - mod));
3409 DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3410 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
3412 return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
3416 * This function is called periodically (every 60 seconds) to adjust output
3417 * power to temperature changes.
3420 wpi_power_calibration(struct wpi_softc *sc)
3424 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3426 /* Update sensor data. */
3427 temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
3428 DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp);
3430 /* Sanity-check read value. */
3431 if (temp < -260 || temp > 25) {
3432 /* This can't be correct, ignore. */
3433 DPRINTF(sc, WPI_DEBUG_TEMP,
3434 "out-of-range temperature reported: %d\n", temp);
3438 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp);
3440 /* Adjust Tx power if need be. */
3441 if (abs(temp - sc->temp) <= 6)
3446 if (wpi_set_txpower(sc, 1) != 0) {
3447 /* just warn, too bad for the automatic calibration... */
3448 device_printf(sc->sc_dev,"could not adjust Tx power\n");
3453 * Set TX power for current channel.
3456 wpi_set_txpower(struct wpi_softc *sc, int async)
3458 struct wpi_power_group *group;
3459 struct wpi_cmd_txpower cmd;
3461 int idx, is_chan_5ghz, i;
3463 /* Retrieve current channel from last RXON. */
3464 chan = sc->rxon.chan;
3465 is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0;
3467 /* Find the TX power group to which this channel belongs. */
3469 for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3470 if (chan <= group->chan)
3473 group = &sc->groups[0];
3475 memset(&cmd, 0, sizeof cmd);
3476 cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ;
3477 cmd.chan = htole16(chan);
3479 /* Set TX power for all OFDM and CCK rates. */
3480 for (i = 0; i <= WPI_RIDX_MAX ; i++) {
3481 /* Retrieve TX power for this channel/rate. */
3482 idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i);
3484 cmd.rates[i].plcp = wpi_ridx_to_plcp[i];
3487 cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
3488 cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
3490 cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
3491 cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
3493 DPRINTF(sc, WPI_DEBUG_TEMP,
3494 "chan %d/ridx %d: power index %d\n", chan, i, idx);
3497 return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
3501 * Determine Tx power index for a given channel/rate combination.
3502 * This takes into account the regulatory information from EEPROM and the
3503 * current temperature.
3506 wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3507 uint8_t chan, int is_chan_5ghz, int ridx)
3509 /* Fixed-point arithmetic division using a n-bit fractional part. */
3510 #define fdivround(a, b, n) \
3511 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3513 /* Linear interpolation. */
3514 #define interpolate(x, x1, y1, x2, y2, n) \
3515 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3517 struct wpi_power_sample *sample;
3520 /* Default TX power is group maximum TX power minus 3dB. */
3521 pwr = group->maxpwr / 2;
3523 /* Decrease TX power for highest OFDM rates to reduce distortion. */
3525 case WPI_RIDX_OFDM36:
3526 pwr -= is_chan_5ghz ? 5 : 0;
3528 case WPI_RIDX_OFDM48:
3529 pwr -= is_chan_5ghz ? 10 : 7;
3531 case WPI_RIDX_OFDM54:
3532 pwr -= is_chan_5ghz ? 12 : 9;
3536 /* Never exceed the channel maximum allowed TX power. */
3537 pwr = min(pwr, sc->maxpwr[chan]);
3539 /* Retrieve TX power index into gain tables from samples. */
3540 for (sample = group->samples; sample < &group->samples[3]; sample++)
3541 if (pwr > sample[1].power)
3543 /* Fixed-point linear interpolation using a 19-bit fractional part. */
3544 idx = interpolate(pwr, sample[0].power, sample[0].index,
3545 sample[1].power, sample[1].index, 19);
3548 * Adjust power index based on current temperature:
3549 * - if cooler than factory-calibrated: decrease output power
3550 * - if warmer than factory-calibrated: increase output power
3552 idx -= (sc->temp - group->temp) * 11 / 100;
3554 /* Decrease TX power for CCK rates (-5dB). */
3555 if (ridx >= WPI_RIDX_CCK1)
3558 /* Make sure idx stays in a valid range. */
3561 if (idx > WPI_MAX_PWR_INDEX)
3562 return WPI_MAX_PWR_INDEX;
3570 * Set STA mode power saving level (between 0 and 5).
3571 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
3574 wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async)
3576 struct wpi_pmgt_cmd cmd;
3577 const struct wpi_pmgt *pmgt;
3578 uint32_t max, skip_dtim;
3582 DPRINTF(sc, WPI_DEBUG_PWRSAVE,
3583 "%s: dtim=%d, level=%d, async=%d\n",
3584 __func__, dtim, level, async);
3586 /* Select which PS parameters to use. */
3588 pmgt = &wpi_pmgt[0][level];
3590 pmgt = &wpi_pmgt[1][level];
3592 memset(&cmd, 0, sizeof cmd);
3593 if (level != 0) /* not CAM */
3594 cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
3595 /* Retrieve PCIe Active State Power Management (ASPM). */
3596 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
3597 if (!(reg & 0x1)) /* L0s Entry disabled. */
3598 cmd.flags |= htole16(WPI_PS_PCI_PMGT);
3600 cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU);
3601 cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU);
3607 skip_dtim = pmgt->skip_dtim;
3609 if (skip_dtim != 0) {
3610 cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
3611 max = pmgt->intval[4];
3612 if (max == (uint32_t)-1)
3613 max = dtim * (skip_dtim + 1);
3614 else if (max > dtim)
3615 max = (max / dtim) * dtim;
3619 for (i = 0; i < 5; i++)
3620 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
3622 return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
3626 wpi_send_btcoex(struct wpi_softc *sc)
3628 struct wpi_bluetooth cmd;
3630 memset(&cmd, 0, sizeof cmd);
3631 cmd.flags = WPI_BT_COEX_MODE_4WIRE;
3632 cmd.lead_time = WPI_BT_LEAD_TIME_DEF;
3633 cmd.max_kill = WPI_BT_MAX_KILL_DEF;
3634 DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n",
3636 return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0);
3640 wpi_send_rxon(struct wpi_softc *sc, int assoc, int async)
3645 WPI_RXON_LOCK_ASSERT(sc);
3647 if (assoc && (sc->rxon.filter & htole32(WPI_FILTER_BSS))) {
3648 struct wpi_assoc rxon_assoc;
3650 rxon_assoc.flags = sc->rxon.flags;
3651 rxon_assoc.filter = sc->rxon.filter;
3652 rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask;
3653 rxon_assoc.cck_mask = sc->rxon.cck_mask;
3654 rxon_assoc.reserved = 0;
3656 error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc,
3657 sizeof (struct wpi_assoc), async);
3659 device_printf(sc->sc_dev,
3660 "RXON_ASSOC command failed, error %d\n", error);
3666 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3667 sizeof (struct wpi_rxon), async);
3669 wpi_clear_node_table(sc);
3672 error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3673 sizeof (struct wpi_rxon), async);
3675 wpi_clear_node_table(sc);
3679 device_printf(sc->sc_dev,
3680 "RXON command failed, error %d\n", error);
3684 /* Add broadcast node. */
3685 error = wpi_add_broadcast_node(sc, async);
3687 device_printf(sc->sc_dev,
3688 "could not add broadcast node, error %d\n", error);
3693 /* Configuration has changed, set Tx power accordingly. */
3694 if ((error = wpi_set_txpower(sc, async)) != 0) {
3695 device_printf(sc->sc_dev,
3696 "%s: could not set TX power, error %d\n", __func__, error);
3704 * Configure the card to listen to a particular channel, this transisions the
3705 * card in to being able to receive frames from remote devices.
3708 wpi_config(struct wpi_softc *sc)
3710 struct ifnet *ifp = sc->sc_ifp;
3711 struct ieee80211com *ic = ifp->if_l2com;
3712 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3716 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3718 /* Set power saving level to CAM during initialization. */
3719 if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
3720 device_printf(sc->sc_dev,
3721 "%s: could not set power saving level\n", __func__);
3725 /* Configure bluetooth coexistence. */
3726 if ((error = wpi_send_btcoex(sc)) != 0) {
3727 device_printf(sc->sc_dev,
3728 "could not configure bluetooth coexistence\n");
3732 /* Configure adapter. */
3733 memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
3734 IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr);
3736 /* Set default channel. */
3737 sc->rxon.chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
3738 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
3739 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3740 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
3742 sc->rxon.filter = WPI_FILTER_MULTICAST;
3743 switch (ic->ic_opmode) {
3744 case IEEE80211_M_STA:
3745 sc->rxon.mode = WPI_MODE_STA;
3747 case IEEE80211_M_IBSS:
3748 sc->rxon.mode = WPI_MODE_IBSS;
3749 sc->rxon.filter |= WPI_FILTER_BEACON;
3751 case IEEE80211_M_HOSTAP:
3752 /* XXX workaround for beaconing */
3753 sc->rxon.mode = WPI_MODE_IBSS;
3754 sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC;
3756 case IEEE80211_M_AHDEMO:
3757 /* XXX workaround for passive channels selection */
3758 sc->rxon.mode = WPI_MODE_HOSTAP;
3760 case IEEE80211_M_MONITOR:
3761 sc->rxon.mode = WPI_MODE_MONITOR;
3764 device_printf(sc->sc_dev, "unknown opmode %d\n",
3768 sc->rxon.filter = htole32(sc->rxon.filter);
3769 wpi_set_promisc(sc);
3770 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
3771 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
3773 if ((error = wpi_send_rxon(sc, 0, 0)) != 0) {
3774 device_printf(sc->sc_dev, "%s: could not send RXON\n",
3779 /* Setup rate scalling. */
3780 if ((error = wpi_mrr_setup(sc)) != 0) {
3781 device_printf(sc->sc_dev, "could not setup MRR, error %d\n",
3786 /* Disable beacon notifications (unused). */
3787 flags = WPI_STATISTICS_BEACON_DISABLE;
3788 error = wpi_cmd(sc, WPI_CMD_GET_STATISTICS, &flags, sizeof flags, 1);
3790 device_printf(sc->sc_dev,
3791 "could not disable beacon statistics, error %d\n", error);
3795 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3801 wpi_get_active_dwell_time(struct wpi_softc *sc,
3802 struct ieee80211_channel *c, uint8_t n_probes)
3804 /* No channel? Default to 2GHz settings. */
3805 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) {
3806 return (WPI_ACTIVE_DWELL_TIME_2GHZ +
3807 WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1));
3810 /* 5GHz dwell time. */
3811 return (WPI_ACTIVE_DWELL_TIME_5GHZ +
3812 WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1));
3816 * Limit the total dwell time to 85% of the beacon interval.
3818 * Returns the dwell time in milliseconds.
3821 wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time)
3823 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3824 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3827 /* bintval is in TU (1.024mS) */
3829 bintval = vap->iv_bss->ni_intval;
3832 * If it's non-zero, we should calculate the minimum of
3833 * it and the DWELL_BASE.
3835 * XXX Yes, the math should take into account that bintval
3836 * is 1.024mS, not 1mS..
3839 DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__,
3841 return (MIN(WPI_PASSIVE_DWELL_BASE, ((bintval * 85) / 100)));
3844 /* No association context? Default. */
3845 return (WPI_PASSIVE_DWELL_BASE);
3849 wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c)
3853 if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c))
3854 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ;
3856 passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ;
3858 /* Clamp to the beacon interval if we're associated. */
3859 return (wpi_limit_dwell(sc, passive));
3863 * Send a scan request to the firmware.
3866 wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c)
3868 struct ifnet *ifp = sc->sc_ifp;
3869 struct ieee80211com *ic = ifp->if_l2com;
3870 struct ieee80211_scan_state *ss = ic->ic_scan;
3871 struct ieee80211vap *vap = ss->ss_vap;
3872 struct wpi_scan_hdr *hdr;
3873 struct wpi_cmd_data *tx;
3874 struct wpi_scan_essid *essids;
3875 struct wpi_scan_chan *chan;
3876 struct ieee80211_frame *wh;
3877 struct ieee80211_rateset *rs;
3878 uint16_t dwell_active, dwell_passive;
3880 int buflen, error, i, nssid;
3882 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3885 * We are absolutely not allowed to send a scan command when another
3886 * scan command is pending.
3888 if (callout_pending(&sc->scan_timeout)) {
3889 device_printf(sc->sc_dev, "%s: called whilst scanning!\n",
3892 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3897 buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
3899 device_printf(sc->sc_dev,
3900 "%s: could not allocate buffer for scan command\n",
3905 hdr = (struct wpi_scan_hdr *)buf;
3908 * Move to the next channel if no packets are received within 10 msecs
3909 * after sending the probe request.
3911 hdr->quiet_time = htole16(10); /* timeout in milliseconds */
3912 hdr->quiet_threshold = htole16(1); /* min # of packets */
3914 * Max needs to be greater than active and passive and quiet!
3915 * It's also in microseconds!
3917 hdr->max_svc = htole32(250 * IEEE80211_DUR_TU);
3918 hdr->pause_svc = htole32((4 << 24) |
3919 (100 * IEEE80211_DUR_TU)); /* Hardcode for now */
3920 hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON);
3922 tx = (struct wpi_cmd_data *)(hdr + 1);
3923 tx->flags = htole32(WPI_TX_AUTO_SEQ);
3924 tx->id = WPI_ID_BROADCAST;
3925 tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
3927 if (IEEE80211_IS_CHAN_5GHZ(c)) {
3928 /* Send probe requests at 6Mbps. */
3929 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6];
3930 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
3932 hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
3933 /* Send probe requests at 1Mbps. */
3934 tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3935 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
3938 essids = (struct wpi_scan_essid *)(tx + 1);
3939 nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
3940 for (i = 0; i < nssid; i++) {
3941 essids[i].id = IEEE80211_ELEMID_SSID;
3942 essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN);
3943 memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len);
3945 if (sc->sc_debug & WPI_DEBUG_SCAN) {
3946 printf("Scanning Essid: ");
3947 ieee80211_print_essid(essids[i].data, essids[i].len);
3954 * Build a probe request frame. Most of the following code is a
3955 * copy & paste of what is done in net80211.
3957 wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS);
3958 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3959 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
3960 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3961 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
3962 IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
3963 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
3964 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by h/w */
3965 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by h/w */
3967 frm = (uint8_t *)(wh + 1);
3968 frm = ieee80211_add_ssid(frm, NULL, 0);
3969 frm = ieee80211_add_rates(frm, rs);
3970 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
3971 frm = ieee80211_add_xrates(frm, rs);
3973 /* Set length of probe request. */
3974 tx->len = htole16(frm - (uint8_t *)wh);
3977 * Construct information about the channel that we
3978 * want to scan. The firmware expects this to be directly
3979 * after the scan probe request
3981 chan = (struct wpi_scan_chan *)frm;
3982 chan->chan = htole16(ieee80211_chan2ieee(ic, c));
3985 hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT;
3986 chan->flags |= WPI_CHAN_NPBREQS(nssid);
3988 hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER;
3990 if (!IEEE80211_IS_CHAN_PASSIVE(c))
3991 chan->flags |= WPI_CHAN_ACTIVE;
3994 * Calculate the active/passive dwell times.
3997 dwell_active = wpi_get_active_dwell_time(sc, c, nssid);
3998 dwell_passive = wpi_get_passive_dwell_time(sc, c);
4000 /* Make sure they're valid. */
4001 if (dwell_passive <= dwell_active)
4002 dwell_passive = dwell_active + 1;
4004 chan->active = htole16(dwell_active);
4005 chan->passive = htole16(dwell_passive);
4007 chan->dsp_gain = 0x6e; /* Default level */
4009 if (IEEE80211_IS_CHAN_5GHZ(c))
4010 chan->rf_gain = 0x3b;
4012 chan->rf_gain = 0x28;
4014 DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n",
4015 chan->chan, IEEE80211_IS_CHAN_PASSIVE(c));
4020 buflen = (uint8_t *)chan - buf;
4021 hdr->len = htole16(buflen);
4023 DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n",
4025 error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
4026 free(buf, M_DEVBUF);
4031 callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc);
4033 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4037 fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
4043 wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
4045 struct ieee80211com *ic = vap->iv_ic;
4046 struct ieee80211_node *ni = vap->iv_bss;
4051 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4053 /* Update adapter configuration. */
4054 sc->rxon.associd = 0;
4055 sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
4056 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4057 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
4058 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4059 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4060 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4061 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4062 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4063 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4064 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4065 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
4066 sc->rxon.cck_mask = 0;
4067 sc->rxon.ofdm_mask = 0x15;
4068 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
4069 sc->rxon.cck_mask = 0x03;
4070 sc->rxon.ofdm_mask = 0;
4072 /* Assume 802.11b/g. */
4073 sc->rxon.cck_mask = 0x0f;
4074 sc->rxon.ofdm_mask = 0x15;
4077 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n",
4078 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask,
4079 sc->rxon.ofdm_mask);
4081 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4082 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4086 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4088 WPI_RXON_UNLOCK(sc);
4094 wpi_config_beacon(struct wpi_vap *wvp)
4096 struct ieee80211com *ic = wvp->wv_vap.iv_ic;
4097 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4098 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4099 struct wpi_softc *sc = ic->ic_ifp->if_softc;
4100 struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
4101 struct ieee80211_tim_ie *tie;
4106 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4108 WPI_VAP_LOCK_ASSERT(wvp);
4110 cmd->len = htole16(bcn->m->m_pkthdr.len);
4111 cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4112 wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4114 /* XXX seems to be unused */
4115 if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) {
4116 tie = (struct ieee80211_tim_ie *) bo->bo_tim;
4117 ptr = mtod(bcn->m, uint8_t *);
4119 cmd->tim = htole16(bo->bo_tim - ptr);
4120 cmd->timsz = tie->tim_len;
4123 /* Necessary for recursion in ieee80211_beacon_update(). */
4125 bcn->m = m_dup(m, M_NOWAIT);
4126 if (bcn->m == NULL) {
4127 device_printf(sc->sc_dev,
4128 "%s: could not copy beacon frame\n", __func__);
4133 if ((error = wpi_cmd2(sc, bcn)) != 0) {
4134 device_printf(sc->sc_dev,
4135 "%s: could not update beacon frame, error %d", __func__,
4146 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
4148 struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
4149 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4150 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4154 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4156 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
4159 m = ieee80211_beacon_alloc(ni, bo);
4161 device_printf(sc->sc_dev,
4162 "%s: could not allocate beacon frame\n", __func__);
4172 error = wpi_config_beacon(wvp);
4173 WPI_VAP_UNLOCK(wvp);
4179 wpi_update_beacon(struct ieee80211vap *vap, int item)
4181 struct wpi_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4182 struct wpi_vap *wvp = WPI_VAP(vap);
4183 struct wpi_buf *bcn = &wvp->wv_bcbuf;
4184 struct ieee80211_beacon_offsets *bo = &wvp->wv_boff;
4185 struct ieee80211_node *ni = vap->iv_bss;
4188 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4191 if (bcn->m == NULL) {
4192 bcn->m = ieee80211_beacon_alloc(ni, bo);
4193 if (bcn->m == NULL) {
4194 device_printf(sc->sc_dev,
4195 "%s: could not allocate beacon frame\n", __func__);
4197 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR,
4200 WPI_VAP_UNLOCK(wvp);
4204 WPI_VAP_UNLOCK(wvp);
4206 if (item == IEEE80211_BEACON_TIM)
4207 mcast = 1; /* TODO */
4209 setbit(bo->bo_flags, item);
4210 ieee80211_beacon_update(ni, bo, bcn->m, mcast);
4213 wpi_config_beacon(wvp);
4214 WPI_VAP_UNLOCK(wvp);
4216 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4220 wpi_newassoc(struct ieee80211_node *ni, int isnew)
4222 struct ieee80211vap *vap = ni->ni_vap;
4223 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4224 struct wpi_node *wn = WPI_NODE(ni);
4229 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4231 if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) {
4232 if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
4233 device_printf(sc->sc_dev,
4234 "%s: could not add IBSS node, error %d\n",
4242 wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
4244 struct ieee80211com *ic = vap->iv_ic;
4245 struct ieee80211_node *ni = vap->iv_bss;
4248 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4250 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
4251 /* Link LED blinks while monitoring. */
4252 wpi_set_led(sc, WPI_LED_LINK, 5, 5);
4256 /* XXX kernel panic workaround */
4257 if (ni->ni_chan == IEEE80211_CHAN_ANYC) {
4258 device_printf(sc->sc_dev, "%s: incomplete configuration\n",
4263 if ((error = wpi_set_timing(sc, ni)) != 0) {
4264 device_printf(sc->sc_dev,
4265 "%s: could not set timing, error %d\n", __func__, error);
4269 /* Update adapter configuration. */
4271 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4272 sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni));
4273 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
4274 sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
4275 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4276 sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4277 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4278 sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4279 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4280 sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
4281 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
4282 sc->rxon.cck_mask = 0;
4283 sc->rxon.ofdm_mask = 0x15;
4284 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
4285 sc->rxon.cck_mask = 0x03;
4286 sc->rxon.ofdm_mask = 0;
4288 /* Assume 802.11b/g. */
4289 sc->rxon.cck_mask = 0x0f;
4290 sc->rxon.ofdm_mask = 0x15;
4292 sc->rxon.filter |= htole32(WPI_FILTER_BSS);
4294 DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n",
4295 sc->rxon.chan, sc->rxon.flags);
4297 if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4298 device_printf(sc->sc_dev, "%s: could not send RXON\n",
4303 /* Start periodic calibration timer. */
4304 callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
4306 WPI_RXON_UNLOCK(sc);
4308 if (vap->iv_opmode == IEEE80211_M_IBSS ||
4309 vap->iv_opmode == IEEE80211_M_HOSTAP) {
4310 if ((error = wpi_setup_beacon(sc, ni)) != 0) {
4311 device_printf(sc->sc_dev,
4312 "%s: could not setup beacon, error %d\n", __func__,
4318 if (vap->iv_opmode == IEEE80211_M_STA) {
4321 error = wpi_add_sta_node(sc, ni);
4324 device_printf(sc->sc_dev,
4325 "%s: could not add BSS node, error %d\n", __func__,
4331 /* Link LED always on while associated. */
4332 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
4334 /* Enable power-saving mode if requested by user. */
4335 if ((vap->iv_flags & IEEE80211_F_PMGTON) &&
4336 vap->iv_opmode != IEEE80211_M_IBSS)
4337 (void)wpi_set_pslevel(sc, 0, 3, 1);
4339 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4345 wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4347 const struct ieee80211_cipher *cip = k->wk_cipher;
4348 struct ieee80211vap *vap = ni->ni_vap;
4349 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4350 struct wpi_node *wn = WPI_NODE(ni);
4351 struct wpi_node_info node;
4355 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4357 if (wpi_check_node_entry(sc, wn->id) == 0) {
4358 device_printf(sc->sc_dev, "%s: node does not exist\n",
4363 switch (cip->ic_cipher) {
4364 case IEEE80211_CIPHER_AES_CCM:
4365 kflags = WPI_KFLAG_CCMP;
4369 device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__,
4374 kflags |= WPI_KFLAG_KID(k->wk_keyix);
4375 if (k->wk_flags & IEEE80211_KEY_GROUP)
4376 kflags |= WPI_KFLAG_MULTICAST;
4378 memset(&node, 0, sizeof node);
4380 node.control = WPI_NODE_UPDATE;
4381 node.flags = WPI_FLAG_KEY_SET;
4382 node.kflags = htole16(kflags);
4383 memcpy(node.key, k->wk_key, k->wk_keylen);
4385 DPRINTF(sc, WPI_DEBUG_KEY,
4386 "%s: setting %s key id %d for node %d (%s)\n", __func__,
4387 (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix,
4388 node.id, ether_sprintf(ni->ni_macaddr));
4390 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4392 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4397 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4398 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4399 kflags |= WPI_KFLAG_MULTICAST;
4400 node.kflags = htole16(kflags);
4409 wpi_load_key_cb(void *arg, struct ieee80211_node *ni)
4411 const struct ieee80211_key *k = arg;
4412 struct ieee80211vap *vap = ni->ni_vap;
4413 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4414 struct wpi_node *wn = WPI_NODE(ni);
4417 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4421 error = wpi_load_key(ni, k);
4425 device_printf(sc->sc_dev, "%s: error while setting key\n",
4431 wpi_set_global_keys(struct ieee80211_node *ni)
4433 struct ieee80211vap *vap = ni->ni_vap;
4434 struct ieee80211_key *wk = &vap->iv_nw_keys[0];
4437 for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++)
4438 if (wk->wk_keyix != IEEE80211_KEYIX_NONE)
4439 error = wpi_load_key(ni, wk);
4445 wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4447 struct ieee80211vap *vap = ni->ni_vap;
4448 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4449 struct wpi_node *wn = WPI_NODE(ni);
4450 struct wpi_node_info node;
4454 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4456 if (wpi_check_node_entry(sc, wn->id) == 0) {
4457 DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__);
4458 return 1; /* Nothing to do. */
4461 kflags = WPI_KFLAG_KID(k->wk_keyix);
4462 if (k->wk_flags & IEEE80211_KEY_GROUP)
4463 kflags |= WPI_KFLAG_MULTICAST;
4465 memset(&node, 0, sizeof node);
4467 node.control = WPI_NODE_UPDATE;
4468 node.flags = WPI_FLAG_KEY_SET;
4469 node.kflags = htole16(kflags);
4471 DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n",
4472 __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast",
4473 k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr));
4475 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4477 device_printf(sc->sc_dev, "can't update node info, error %d\n",
4482 if (!(kflags & WPI_KFLAG_MULTICAST) && &vap->iv_nw_keys[0] <= k &&
4483 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4484 kflags |= WPI_KFLAG_MULTICAST;
4485 node.kflags = htole16(kflags);
4494 wpi_del_key_cb(void *arg, struct ieee80211_node *ni)
4496 const struct ieee80211_key *k = arg;
4497 struct ieee80211vap *vap = ni->ni_vap;
4498 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
4499 struct wpi_node *wn = WPI_NODE(ni);
4502 if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4506 error = wpi_del_key(ni, k);
4510 device_printf(sc->sc_dev, "%s: error while deleting key\n",
4516 wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
4519 struct ieee80211com *ic = vap->iv_ic;
4520 struct wpi_softc *sc = ic->ic_ifp->if_softc;
4521 struct wpi_vap *wvp = WPI_VAP(vap);
4522 struct ieee80211_node *ni;
4523 int error, ni_ref = 0;
4525 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4527 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
4532 if (!(k->wk_flags & IEEE80211_KEY_RECV)) {
4533 /* XMIT keys are handled in wpi_tx_data(). */
4537 /* Handle group keys. */
4538 if (&vap->iv_nw_keys[0] <= k &&
4539 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
4542 wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix);
4544 wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix);
4547 if (vap->iv_state == IEEE80211_S_RUN) {
4548 ieee80211_iterate_nodes(&ic->ic_sta,
4549 set ? wpi_load_key_cb : wpi_del_key_cb,
4550 __DECONST(void *, k));
4556 switch (vap->iv_opmode) {
4557 case IEEE80211_M_STA:
4561 case IEEE80211_M_IBSS:
4562 case IEEE80211_M_AHDEMO:
4563 case IEEE80211_M_HOSTAP:
4564 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
4566 return 0; /* should not happen */
4572 device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__,
4579 error = wpi_load_key(ni, k);
4581 error = wpi_del_key(ni, k);
4585 ieee80211_node_decref(ni);
4591 wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
4592 const uint8_t mac[IEEE80211_ADDR_LEN])
4594 return wpi_process_key(vap, k, 1);
4598 wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4600 return wpi_process_key(vap, k, 0);
4604 * This function is called after the runtime firmware notifies us of its
4605 * readiness (called in a process context).
4608 wpi_post_alive(struct wpi_softc *sc)
4612 /* Check (again) that the radio is not disabled. */
4613 if ((error = wpi_nic_lock(sc)) != 0)
4616 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4618 /* NB: Runtime firmware must be up and running. */
4619 if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
4620 device_printf(sc->sc_dev,
4621 "RF switch: radio disabled (%s)\n", __func__);
4623 return EPERM; /* :-) */
4627 /* Wait for thermal sensor to calibrate. */
4628 for (ntries = 0; ntries < 1000; ntries++) {
4629 if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
4634 if (ntries == 1000) {
4635 device_printf(sc->sc_dev,
4636 "timeout waiting for thermal sensor calibration\n");
4640 DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp);
4645 * The firmware boot code is small and is intended to be copied directly into
4646 * the NIC internal memory (no DMA transfer).
4649 wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, int size)
4653 DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size);
4655 size /= sizeof (uint32_t);
4657 if ((error = wpi_nic_lock(sc)) != 0)
4660 /* Copy microcode image into NIC memory. */
4661 wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
4662 (const uint32_t *)ucode, size);
4664 wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
4665 wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
4666 wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
4668 /* Start boot load now. */
4669 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
4671 /* Wait for transfer to complete. */
4672 for (ntries = 0; ntries < 1000; ntries++) {
4673 uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS);
4674 DPRINTF(sc, WPI_DEBUG_HW,
4675 "firmware status=0x%x, val=0x%x, result=0x%x\n", status,
4676 WPI_FH_TX_STATUS_IDLE(6),
4677 status & WPI_FH_TX_STATUS_IDLE(6));
4678 if (status & WPI_FH_TX_STATUS_IDLE(6)) {
4679 DPRINTF(sc, WPI_DEBUG_HW,
4680 "Status Match! - ntries = %d\n", ntries);
4685 if (ntries == 1000) {
4686 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4692 /* Enable boot after power up. */
4693 wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
4700 wpi_load_firmware(struct wpi_softc *sc)
4702 struct wpi_fw_info *fw = &sc->fw;
4703 struct wpi_dma_info *dma = &sc->fw_dma;
4706 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4708 /* Copy initialization sections into pre-allocated DMA-safe memory. */
4709 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
4710 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4711 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz);
4712 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4714 /* Tell adapter where to find initialization sections. */
4715 if ((error = wpi_nic_lock(sc)) != 0)
4717 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4718 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
4719 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4720 dma->paddr + WPI_FW_DATA_MAXSZ);
4721 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
4724 /* Load firmware boot code. */
4725 error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
4727 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4732 /* Now press "execute". */
4733 WPI_WRITE(sc, WPI_RESET, 0);
4735 /* Wait at most one second for first alive notification. */
4736 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
4737 device_printf(sc->sc_dev,
4738 "%s: timeout waiting for adapter to initialize, error %d\n",
4743 /* Copy runtime sections into pre-allocated DMA-safe memory. */
4744 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
4745 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4746 memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz);
4747 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4749 /* Tell adapter where to find runtime sections. */
4750 if ((error = wpi_nic_lock(sc)) != 0)
4752 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4753 wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
4754 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4755 dma->paddr + WPI_FW_DATA_MAXSZ);
4756 wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
4757 WPI_FW_UPDATED | fw->main.textsz);
4764 wpi_read_firmware(struct wpi_softc *sc)
4766 const struct firmware *fp;
4767 struct wpi_fw_info *fw = &sc->fw;
4768 const struct wpi_firmware_hdr *hdr;
4771 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4773 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4774 "Attempting Loading Firmware from %s module\n", WPI_FW_NAME);
4777 fp = firmware_get(WPI_FW_NAME);
4781 device_printf(sc->sc_dev,
4782 "could not load firmware image '%s'\n", WPI_FW_NAME);
4788 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
4789 device_printf(sc->sc_dev,
4790 "firmware file too short: %zu bytes\n", fp->datasize);
4795 fw->size = fp->datasize;
4796 fw->data = (const uint8_t *)fp->data;
4798 /* Extract firmware header information. */
4799 hdr = (const struct wpi_firmware_hdr *)fw->data;
4801 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW |
4802 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
4804 fw->main.textsz = le32toh(hdr->rtextsz);
4805 fw->main.datasz = le32toh(hdr->rdatasz);
4806 fw->init.textsz = le32toh(hdr->itextsz);
4807 fw->init.datasz = le32toh(hdr->idatasz);
4808 fw->boot.textsz = le32toh(hdr->btextsz);
4809 fw->boot.datasz = 0;
4811 /* Sanity-check firmware header. */
4812 if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
4813 fw->main.datasz > WPI_FW_DATA_MAXSZ ||
4814 fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
4815 fw->init.datasz > WPI_FW_DATA_MAXSZ ||
4816 fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
4817 (fw->boot.textsz & 3) != 0) {
4818 device_printf(sc->sc_dev, "invalid firmware header\n");
4823 /* Check that all firmware sections fit. */
4824 if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
4825 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
4826 device_printf(sc->sc_dev,
4827 "firmware file too short: %zu bytes\n", fw->size);
4832 /* Get pointers to firmware sections. */
4833 fw->main.text = (const uint8_t *)(hdr + 1);
4834 fw->main.data = fw->main.text + fw->main.textsz;
4835 fw->init.text = fw->main.data + fw->main.datasz;
4836 fw->init.data = fw->init.text + fw->init.textsz;
4837 fw->boot.text = fw->init.data + fw->init.datasz;
4839 DPRINTF(sc, WPI_DEBUG_FIRMWARE,
4840 "Firmware Version: Major %d, Minor %d, Driver %d, \n"
4841 "runtime (text: %u, data: %u) init (text: %u, data %u) "
4842 "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver),
4843 fw->main.textsz, fw->main.datasz,
4844 fw->init.textsz, fw->init.datasz, fw->boot.textsz);
4846 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text);
4847 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data);
4848 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text);
4849 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data);
4850 DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text);
4854 fail: wpi_unload_firmware(sc);
4859 * Free the referenced firmware image
4862 wpi_unload_firmware(struct wpi_softc *sc)
4864 if (sc->fw_fp != NULL) {
4865 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
4871 wpi_clock_wait(struct wpi_softc *sc)
4875 /* Set "initialization complete" bit. */
4876 WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
4878 /* Wait for clock stabilization. */
4879 for (ntries = 0; ntries < 2500; ntries++) {
4880 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
4884 device_printf(sc->sc_dev,
4885 "%s: timeout waiting for clock stabilization\n", __func__);
4891 wpi_apm_init(struct wpi_softc *sc)
4896 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4898 /* Disable L0s exit timer (NMI bug workaround). */
4899 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER);
4900 /* Don't wait for ICH L0s (ICH bug workaround). */
4901 WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
4903 /* Set FH wait threshold to max (HW bug under stress workaround). */
4904 WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000);
4906 /* Retrieve PCIe Active State Power Management (ASPM). */
4907 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
4908 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
4909 if (reg & 0x02) /* L1 Entry enabled. */
4910 WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
4912 WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
4914 WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
4916 /* Wait for clock stabilization before accessing prph. */
4917 if ((error = wpi_clock_wait(sc)) != 0)
4920 if ((error = wpi_nic_lock(sc)) != 0)
4923 wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400);
4924 wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200);
4926 /* Enable DMA and BSM (Bootstrap State Machine). */
4927 wpi_prph_write(sc, WPI_APMG_CLK_EN,
4928 WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT);
4930 /* Disable L1-Active. */
4931 wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
4938 wpi_apm_stop_master(struct wpi_softc *sc)
4942 /* Stop busmaster DMA activity. */
4943 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
4945 if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
4946 WPI_GP_CNTRL_MAC_PS)
4947 return; /* Already asleep. */
4949 for (ntries = 0; ntries < 100; ntries++) {
4950 if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
4954 device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
4959 wpi_apm_stop(struct wpi_softc *sc)
4961 wpi_apm_stop_master(sc);
4963 /* Reset the entire device. */
4964 WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
4966 /* Clear "initialization complete" bit. */
4967 WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
4971 wpi_nic_config(struct wpi_softc *sc)
4975 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4977 /* voodoo from the Linux "driver".. */
4978 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
4979 if ((rev & 0xc0) == 0x40)
4980 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
4981 else if (!(rev & 0x80))
4982 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
4984 if (sc->cap == 0x80)
4985 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
4987 if ((sc->rev & 0xf0) == 0xd0)
4988 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
4990 WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
4993 WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
4997 wpi_hw_init(struct wpi_softc *sc)
4999 int chnl, ntries, error;
5001 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5003 /* Clear pending interrupts. */
5004 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5006 if ((error = wpi_apm_init(sc)) != 0) {
5007 device_printf(sc->sc_dev,
5008 "%s: could not power ON adapter, error %d\n", __func__,
5013 /* Select VMAIN power source. */
5014 if ((error = wpi_nic_lock(sc)) != 0)
5016 wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
5018 /* Spin until VMAIN gets selected. */
5019 for (ntries = 0; ntries < 5000; ntries++) {
5020 if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
5024 if (ntries == 5000) {
5025 device_printf(sc->sc_dev, "timeout selecting power source\n");
5029 /* Perform adapter initialization. */
5032 /* Initialize RX ring. */
5033 if ((error = wpi_nic_lock(sc)) != 0)
5035 /* Set physical address of RX ring. */
5036 WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
5037 /* Set physical address of RX read pointer. */
5038 WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
5039 offsetof(struct wpi_shared, next));
5040 WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
5042 WPI_WRITE(sc, WPI_FH_RX_CONFIG,
5043 WPI_FH_RX_CONFIG_DMA_ENA |
5044 WPI_FH_RX_CONFIG_RDRBD_ENA |
5045 WPI_FH_RX_CONFIG_WRSTATUS_ENA |
5046 WPI_FH_RX_CONFIG_MAXFRAG |
5047 WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
5048 WPI_FH_RX_CONFIG_IRQ_DST_HOST |
5049 WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1));
5050 (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */
5052 WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
5054 /* Initialize TX rings. */
5055 if ((error = wpi_nic_lock(sc)) != 0)
5057 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */
5058 wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */
5059 /* Enable all 6 TX rings. */
5060 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
5061 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
5062 wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
5063 wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
5064 wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
5065 /* Set physical address of TX rings. */
5066 WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
5067 WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
5069 /* Enable all DMA channels. */
5070 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5071 WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
5072 WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
5073 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
5076 (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */
5078 /* Clear "radio off" and "commands blocked" bits. */
5079 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5080 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
5082 /* Clear pending interrupts. */
5083 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5084 /* Enable interrupts. */
5085 WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
5087 /* _Really_ make sure "radio off" bit is cleared! */
5088 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5089 WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5091 if ((error = wpi_load_firmware(sc)) != 0) {
5092 device_printf(sc->sc_dev,
5093 "%s: could not load firmware, error %d\n", __func__,
5097 /* Wait at most one second for firmware alive notification. */
5098 if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
5099 device_printf(sc->sc_dev,
5100 "%s: timeout waiting for adapter to initialize, error %d\n",
5105 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5107 /* Do post-firmware initialization. */
5108 return wpi_post_alive(sc);
5112 wpi_hw_stop(struct wpi_softc *sc)
5114 int chnl, qid, ntries;
5116 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5118 if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP)
5121 WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
5123 /* Disable interrupts. */
5124 WPI_WRITE(sc, WPI_INT_MASK, 0);
5125 WPI_WRITE(sc, WPI_INT, 0xffffffff);
5126 WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
5128 /* Make sure we no longer hold the NIC lock. */
5131 if (wpi_nic_lock(sc) == 0) {
5132 /* Stop TX scheduler. */
5133 wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
5134 wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
5136 /* Stop all DMA channels. */
5137 for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5138 WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
5139 for (ntries = 0; ntries < 200; ntries++) {
5140 if (WPI_READ(sc, WPI_FH_TX_STATUS) &
5141 WPI_FH_TX_STATUS_IDLE(chnl))
5150 wpi_reset_rx_ring(sc);
5152 /* Reset all TX rings. */
5153 for (qid = 0; qid < WPI_NTXQUEUES; qid++)
5154 wpi_reset_tx_ring(sc, &sc->txq[qid]);
5156 if (wpi_nic_lock(sc) == 0) {
5157 wpi_prph_write(sc, WPI_APMG_CLK_DIS,
5158 WPI_APMG_CLK_CTRL_DMA_CLK_RQT);
5162 /* Power OFF adapter. */
5167 wpi_radio_on(void *arg0, int pending)
5169 struct wpi_softc *sc = arg0;
5170 struct ifnet *ifp = sc->sc_ifp;
5171 struct ieee80211com *ic = ifp->if_l2com;
5172 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5174 device_printf(sc->sc_dev, "RF switch: radio enabled\n");
5178 ieee80211_init(vap);
5181 if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL) {
5183 callout_stop(&sc->watchdog_rfkill);
5189 wpi_radio_off(void *arg0, int pending)
5191 struct wpi_softc *sc = arg0;
5192 struct ifnet *ifp = sc->sc_ifp;
5193 struct ieee80211com *ic = ifp->if_l2com;
5194 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5196 device_printf(sc->sc_dev, "RF switch: radio disabled\n");
5200 ieee80211_stop(vap);
5203 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc);
5210 struct wpi_softc *sc = arg;
5211 struct ifnet *ifp = sc->sc_ifp;
5212 struct ieee80211com *ic = ifp->if_l2com;
5217 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5219 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
5222 /* Check that the radio is not disabled by hardware switch. */
5223 if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
5224 device_printf(sc->sc_dev,
5225 "RF switch: radio disabled (%s)\n", __func__);
5226 callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
5231 /* Read firmware images from the filesystem. */
5232 if ((error = wpi_read_firmware(sc)) != 0) {
5233 device_printf(sc->sc_dev,
5234 "%s: could not read firmware, error %d\n", __func__,
5239 /* Initialize hardware and upload firmware. */
5240 error = wpi_hw_init(sc);
5241 wpi_unload_firmware(sc);
5243 device_printf(sc->sc_dev,
5244 "%s: could not initialize hardware, error %d\n", __func__,
5249 /* Configure adapter now that it is ready. */
5251 if ((error = wpi_config(sc)) != 0) {
5252 device_printf(sc->sc_dev,
5253 "%s: could not configure device, error %d\n", __func__,
5258 IF_LOCK(&ifp->if_snd);
5259 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5260 ifp->if_drv_flags |= IFF_DRV_RUNNING;
5261 IF_UNLOCK(&ifp->if_snd);
5263 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5267 ieee80211_start_all(ic);
5271 fail: wpi_stop_locked(sc);
5272 end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
5277 wpi_stop_locked(struct wpi_softc *sc)
5279 struct ifnet *ifp = sc->sc_ifp;
5281 WPI_LOCK_ASSERT(sc);
5287 WPI_TXQ_STATE_LOCK(sc);
5288 callout_stop(&sc->tx_timeout);
5289 WPI_TXQ_STATE_UNLOCK(sc);
5292 callout_stop(&sc->scan_timeout);
5293 callout_stop(&sc->calib_to);
5294 WPI_RXON_UNLOCK(sc);
5296 IF_LOCK(&ifp->if_snd);
5297 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
5298 IF_UNLOCK(&ifp->if_snd);
5300 /* Power OFF hardware. */
5305 wpi_stop(struct wpi_softc *sc)
5308 wpi_stop_locked(sc);
5313 * Callback from net80211 to start a scan.
5316 wpi_scan_start(struct ieee80211com *ic)
5318 struct wpi_softc *sc = ic->ic_ifp->if_softc;
5320 wpi_set_led(sc, WPI_LED_LINK, 20, 2);
5324 * Callback from net80211 to terminate a scan.
5327 wpi_scan_end(struct ieee80211com *ic)
5329 struct ifnet *ifp = ic->ic_ifp;
5330 struct wpi_softc *sc = ifp->if_softc;
5331 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5333 if (vap->iv_state == IEEE80211_S_RUN)
5334 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
5338 * Called by the net80211 framework to indicate to the driver
5339 * that the channel should be changed
5342 wpi_set_channel(struct ieee80211com *ic)
5344 const struct ieee80211_channel *c = ic->ic_curchan;
5345 struct ifnet *ifp = ic->ic_ifp;
5346 struct wpi_softc *sc = ifp->if_softc;
5349 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5352 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
5353 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
5356 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
5357 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
5361 * Only need to set the channel in Monitor mode. AP scanning and auth
5362 * are already taken care of by their respective firmware commands.
5364 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
5366 sc->rxon.chan = ieee80211_chan2ieee(ic, c);
5367 if (IEEE80211_IS_CHAN_2GHZ(c)) {
5368 sc->rxon.flags |= htole32(WPI_RXON_AUTO |
5371 sc->rxon.flags &= ~htole32(WPI_RXON_AUTO |
5374 if ((error = wpi_send_rxon(sc, 0, 1)) != 0)
5375 device_printf(sc->sc_dev,
5376 "%s: error %d setting channel\n", __func__,
5378 WPI_RXON_UNLOCK(sc);
5383 * Called by net80211 to indicate that we need to scan the current
5384 * channel. The channel is previously be set via the wpi_set_channel
5388 wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
5390 struct ieee80211vap *vap = ss->ss_vap;
5391 struct ieee80211com *ic = vap->iv_ic;
5392 struct wpi_softc *sc = ic->ic_ifp->if_softc;
5396 if (sc->rxon.chan != ieee80211_chan2ieee(ic, ic->ic_curchan)) {
5397 error = wpi_scan(sc, ic->ic_curchan);
5398 WPI_RXON_UNLOCK(sc);
5400 ieee80211_cancel_scan(vap);
5402 WPI_RXON_UNLOCK(sc);
5403 /* Send probe request when associated. */
5404 sc->sc_scan_curchan(ss, maxdwell);
5409 * Called by the net80211 framework to indicate
5410 * the minimum dwell time has been met, terminate the scan.
5411 * We don't actually terminate the scan as the firmware will notify
5412 * us when it's finished and we have no way to interrupt it.
5415 wpi_scan_mindwell(struct ieee80211_scan_state *ss)
5417 /* NB: don't try to abort scan; wait for firmware to finish */
5421 wpi_hw_reset(void *arg, int pending)
5423 struct wpi_softc *sc = arg;
5424 struct ifnet *ifp = sc->sc_ifp;
5425 struct ieee80211com *ic = ifp->if_l2com;
5426 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5428 DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5430 if (vap != NULL && (ic->ic_flags & IEEE80211_F_SCAN))
5431 ieee80211_cancel_scan(vap);
5435 ieee80211_stop(vap);
5438 ieee80211_init(vap);