16607310bSBenjamin Close /*-
26607310bSBenjamin Close * Copyright (c) 2006,2007
36607310bSBenjamin Close * Damien Bergamini <damien.bergamini@free.fr>
46607310bSBenjamin Close * Benjamin Close <Benjamin.Close@clearchain.com>
5ce036b51SAndriy Voskoboinyk * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
66607310bSBenjamin Close *
76607310bSBenjamin Close * Permission to use, copy, modify, and distribute this software for any
86607310bSBenjamin Close * purpose with or without fee is hereby granted, provided that the above
96607310bSBenjamin Close * copyright notice and this permission notice appear in all copies.
106607310bSBenjamin Close *
116607310bSBenjamin Close * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
126607310bSBenjamin Close * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
136607310bSBenjamin Close * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
146607310bSBenjamin Close * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
156607310bSBenjamin Close * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
166607310bSBenjamin Close * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
176607310bSBenjamin Close * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
186607310bSBenjamin Close */
196607310bSBenjamin Close
206607310bSBenjamin Close #include <sys/cdefs.h>
216607310bSBenjamin Close /*
226607310bSBenjamin Close * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
236607310bSBenjamin Close *
246607310bSBenjamin Close * The 3945ABG network adapter doesn't use traditional hardware as
256607310bSBenjamin Close * many other adaptors do. Instead at run time the eeprom is set into a known
266607310bSBenjamin Close * state and told to load boot firmware. The boot firmware loads an init and a
276607310bSBenjamin Close * main binary firmware image into SRAM on the card via DMA.
286607310bSBenjamin Close * Once the firmware is loaded, the driver/hw then
296bccea7cSRebecca Cran * communicate by way of circular dma rings via the SRAM to the firmware.
306607310bSBenjamin Close *
316607310bSBenjamin Close * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings.
326607310bSBenjamin Close * The 4 tx data rings allow for prioritization QoS.
336607310bSBenjamin Close *
346607310bSBenjamin Close * The rx data ring consists of 32 dma buffers. Two registers are used to
356607310bSBenjamin Close * indicate where in the ring the driver and the firmware are up to. The
366607310bSBenjamin Close * driver sets the initial read index (reg1) and the initial write index (reg2),
376607310bSBenjamin Close * the firmware updates the read index (reg1) on rx of a packet and fires an
386607310bSBenjamin Close * interrupt. The driver then processes the buffers starting at reg1 indicating
396607310bSBenjamin Close * to the firmware which buffers have been accessed by updating reg2. At the
406607310bSBenjamin Close * same time allocating new memory for the processed buffer.
416607310bSBenjamin Close *
426607310bSBenjamin Close * A similar thing happens with the tx rings. The difference is the firmware
436607310bSBenjamin Close * stop processing buffers once the queue is full and until confirmation
44fae61c69SAdrian Chadd * of a successful transmition (tx_done) has occurred.
456607310bSBenjamin Close *
466607310bSBenjamin Close * The command ring operates in the same manner as the tx queues.
476607310bSBenjamin Close *
486607310bSBenjamin Close * All communication direct to the card (ie eeprom) is classed as Stage1
496607310bSBenjamin Close * communication
506607310bSBenjamin Close *
516607310bSBenjamin Close * All communication via the firmware to the card is classed as State2.
526607310bSBenjamin Close * The firmware consists of 2 parts. A bootstrap firmware and a runtime
536607310bSBenjamin Close * firmware. The bootstrap firmware and runtime firmware are loaded
546607310bSBenjamin Close * from host memory via dma to the card then told to execute. From this point
556607310bSBenjamin Close * on the majority of communications between the driver and the card goes
566607310bSBenjamin Close * via the firmware.
576607310bSBenjamin Close */
586607310bSBenjamin Close
5981c2214dSAdrian Chadd #include "opt_wlan.h"
60692ad7caSAdrian Chadd #include "opt_wpi.h"
6181c2214dSAdrian Chadd
626607310bSBenjamin Close #include <sys/param.h>
636607310bSBenjamin Close #include <sys/sysctl.h>
646607310bSBenjamin Close #include <sys/sockio.h>
656607310bSBenjamin Close #include <sys/mbuf.h>
666607310bSBenjamin Close #include <sys/kernel.h>
676607310bSBenjamin Close #include <sys/socket.h>
686607310bSBenjamin Close #include <sys/systm.h>
696607310bSBenjamin Close #include <sys/malloc.h>
706607310bSBenjamin Close #include <sys/queue.h>
716607310bSBenjamin Close #include <sys/taskqueue.h>
726607310bSBenjamin Close #include <sys/module.h>
736607310bSBenjamin Close #include <sys/bus.h>
746607310bSBenjamin Close #include <sys/endian.h>
756607310bSBenjamin Close #include <sys/linker.h>
766607310bSBenjamin Close #include <sys/firmware.h>
776607310bSBenjamin Close
786607310bSBenjamin Close #include <machine/bus.h>
796607310bSBenjamin Close #include <machine/resource.h>
806607310bSBenjamin Close #include <sys/rman.h>
816607310bSBenjamin Close
826607310bSBenjamin Close #include <dev/pci/pcireg.h>
836607310bSBenjamin Close #include <dev/pci/pcivar.h>
846607310bSBenjamin Close
856607310bSBenjamin Close #include <net/bpf.h>
866607310bSBenjamin Close #include <net/if.h>
8776039bc8SGleb Smirnoff #include <net/if_var.h>
886607310bSBenjamin Close #include <net/if_arp.h>
896607310bSBenjamin Close #include <net/ethernet.h>
906607310bSBenjamin Close #include <net/if_dl.h>
916607310bSBenjamin Close #include <net/if_media.h>
926607310bSBenjamin Close #include <net/if_types.h>
936607310bSBenjamin Close
94692ad7caSAdrian Chadd #include <netinet/in.h>
95692ad7caSAdrian Chadd #include <netinet/in_systm.h>
96692ad7caSAdrian Chadd #include <netinet/in_var.h>
97692ad7caSAdrian Chadd #include <netinet/if_ether.h>
98692ad7caSAdrian Chadd #include <netinet/ip.h>
99692ad7caSAdrian Chadd
1006607310bSBenjamin Close #include <net80211/ieee80211_var.h>
1016607310bSBenjamin Close #include <net80211/ieee80211_radiotap.h>
1026607310bSBenjamin Close #include <net80211/ieee80211_regdomain.h>
103b6108616SRui Paulo #include <net80211/ieee80211_ratectl.h>
1046607310bSBenjamin Close
1056607310bSBenjamin Close #include <dev/wpi/if_wpireg.h>
1066607310bSBenjamin Close #include <dev/wpi/if_wpivar.h>
107692ad7caSAdrian Chadd #include <dev/wpi/if_wpi_debug.h>
1086607310bSBenjamin Close
1096607310bSBenjamin Close struct wpi_ident {
1106607310bSBenjamin Close uint16_t vendor;
1116607310bSBenjamin Close uint16_t device;
1126607310bSBenjamin Close uint16_t subdevice;
1136607310bSBenjamin Close const char *name;
1146607310bSBenjamin Close };
1156607310bSBenjamin Close
1166607310bSBenjamin Close static const struct wpi_ident wpi_ident_table[] = {
1176607310bSBenjamin Close /* The below entries support ABG regardless of the subid */
1186607310bSBenjamin Close { 0x8086, 0x4222, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
1196607310bSBenjamin Close { 0x8086, 0x4227, 0x0, "Intel(R) PRO/Wireless 3945ABG" },
1206607310bSBenjamin Close /* The below entries only support BG */
12134f004ceSBenjamin Close { 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945BG" },
12234f004ceSBenjamin Close { 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945BG" },
12334f004ceSBenjamin Close { 0x8086, 0x4227, 0x1014, "Intel(R) PRO/Wireless 3945BG" },
12434f004ceSBenjamin Close { 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945BG" },
1256607310bSBenjamin Close { 0, 0, 0, NULL }
1266607310bSBenjamin Close };
1276607310bSBenjamin Close
128692ad7caSAdrian Chadd static int wpi_probe(device_t);
129692ad7caSAdrian Chadd static int wpi_attach(device_t);
130692ad7caSAdrian Chadd static void wpi_radiotap_attach(struct wpi_softc *);
131692ad7caSAdrian Chadd static void wpi_sysctlattach(struct wpi_softc *);
13221f6580eSAdrian Chadd static void wpi_init_beacon(struct wpi_vap *);
133b032f27cSSam Leffler static struct ieee80211vap *wpi_vap_create(struct ieee80211com *,
134fcd9500fSBernhard Schmidt const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
135fcd9500fSBernhard Schmidt const uint8_t [IEEE80211_ADDR_LEN],
136fcd9500fSBernhard Schmidt const uint8_t [IEEE80211_ADDR_LEN]);
137b032f27cSSam Leffler static void wpi_vap_delete(struct ieee80211vap *);
138692ad7caSAdrian Chadd static int wpi_detach(device_t);
139692ad7caSAdrian Chadd static int wpi_shutdown(device_t);
140692ad7caSAdrian Chadd static int wpi_suspend(device_t);
141692ad7caSAdrian Chadd static int wpi_resume(device_t);
142692ad7caSAdrian Chadd static int wpi_nic_lock(struct wpi_softc *);
143692ad7caSAdrian Chadd static int wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int);
1446607310bSBenjamin Close static void wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
145692ad7caSAdrian Chadd static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
146692ad7caSAdrian Chadd void **, bus_size_t, bus_size_t);
147692ad7caSAdrian Chadd static void wpi_dma_contig_free(struct wpi_dma_info *);
1486607310bSBenjamin Close static int wpi_alloc_shared(struct wpi_softc *);
1496607310bSBenjamin Close static void wpi_free_shared(struct wpi_softc *);
1506607310bSBenjamin Close static int wpi_alloc_fwmem(struct wpi_softc *);
1516607310bSBenjamin Close static void wpi_free_fwmem(struct wpi_softc *);
152692ad7caSAdrian Chadd static int wpi_alloc_rx_ring(struct wpi_softc *);
153692ad7caSAdrian Chadd static void wpi_update_rx_ring(struct wpi_softc *);
1545cacb17fSAdrian Chadd static void wpi_update_rx_ring_ps(struct wpi_softc *);
155692ad7caSAdrian Chadd static void wpi_reset_rx_ring(struct wpi_softc *);
156692ad7caSAdrian Chadd static void wpi_free_rx_ring(struct wpi_softc *);
157692ad7caSAdrian Chadd static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *,
158525a3d47SAdrian Chadd uint8_t);
159692ad7caSAdrian Chadd static void wpi_update_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
1605cacb17fSAdrian Chadd static void wpi_update_tx_ring_ps(struct wpi_softc *,
1615cacb17fSAdrian Chadd struct wpi_tx_ring *);
162692ad7caSAdrian Chadd static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
163692ad7caSAdrian Chadd static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
164692ad7caSAdrian Chadd static int wpi_read_eeprom(struct wpi_softc *,
165692ad7caSAdrian Chadd uint8_t macaddr[IEEE80211_ADDR_LEN]);
166692ad7caSAdrian Chadd static uint32_t wpi_eeprom_channel_flags(struct wpi_eeprom_chan *);
1671c6b43dfSAndriy Voskoboinyk static void wpi_read_eeprom_band(struct wpi_softc *, uint8_t, int, int *,
1681c6b43dfSAndriy Voskoboinyk struct ieee80211_channel[]);
169525a3d47SAdrian Chadd static int wpi_read_eeprom_channels(struct wpi_softc *, uint8_t);
170692ad7caSAdrian Chadd static struct wpi_eeprom_chan *wpi_find_eeprom_channel(struct wpi_softc *,
171692ad7caSAdrian Chadd struct ieee80211_channel *);
1721c6b43dfSAndriy Voskoboinyk static void wpi_getradiocaps(struct ieee80211com *, int, int *,
1731c6b43dfSAndriy Voskoboinyk struct ieee80211_channel[]);
174692ad7caSAdrian Chadd static int wpi_setregdomain(struct ieee80211com *,
175692ad7caSAdrian Chadd struct ieee80211_regdomain *, int,
176692ad7caSAdrian Chadd struct ieee80211_channel[]);
177525a3d47SAdrian Chadd static int wpi_read_eeprom_group(struct wpi_softc *, uint8_t);
178692ad7caSAdrian Chadd static struct ieee80211_node *wpi_node_alloc(struct ieee80211vap *,
179692ad7caSAdrian Chadd const uint8_t mac[IEEE80211_ADDR_LEN]);
180a20c9affSAdrian Chadd static void wpi_node_free(struct ieee80211_node *);
18157c61f5dSAdrian Chadd static void wpi_ibss_recv_mgmt(struct ieee80211_node *, struct mbuf *, int,
182b66af287SAdrian Chadd const struct ieee80211_rx_stats *,
183b66af287SAdrian Chadd int, int);
184a20c9affSAdrian Chadd static void wpi_restore_node(void *, struct ieee80211_node *);
185a20c9affSAdrian Chadd static void wpi_restore_node_table(struct wpi_softc *, struct wpi_vap *);
186692ad7caSAdrian Chadd static int wpi_newstate(struct ieee80211vap *, enum ieee80211_state, int);
187692ad7caSAdrian Chadd static void wpi_calib_timeout(void *);
188692ad7caSAdrian Chadd static void wpi_rx_done(struct wpi_softc *, struct wpi_rx_desc *,
1896607310bSBenjamin Close struct wpi_rx_data *);
190692ad7caSAdrian Chadd static void wpi_rx_statistics(struct wpi_softc *, struct wpi_rx_desc *,
191692ad7caSAdrian Chadd struct wpi_rx_data *);
192692ad7caSAdrian Chadd static void wpi_tx_done(struct wpi_softc *, struct wpi_rx_desc *);
193692ad7caSAdrian Chadd static void wpi_cmd_done(struct wpi_softc *, struct wpi_rx_desc *);
1946607310bSBenjamin Close static void wpi_notif_intr(struct wpi_softc *);
195692ad7caSAdrian Chadd static void wpi_wakeup_intr(struct wpi_softc *);
196a6df6cfeSAdrian Chadd #ifdef WPI_DEBUG
197a6df6cfeSAdrian Chadd static void wpi_debug_registers(struct wpi_softc *);
198a6df6cfeSAdrian Chadd #endif
199692ad7caSAdrian Chadd static void wpi_fatal_intr(struct wpi_softc *);
2006607310bSBenjamin Close static void wpi_intr(void *);
201940aa808SAdrian Chadd static void wpi_free_txfrags(struct wpi_softc *, uint16_t);
202692ad7caSAdrian Chadd static int wpi_cmd2(struct wpi_softc *, struct wpi_buf *);
2036607310bSBenjamin Close static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
204692ad7caSAdrian Chadd struct ieee80211_node *);
205692ad7caSAdrian Chadd static int wpi_tx_data_raw(struct wpi_softc *, struct mbuf *,
206692ad7caSAdrian Chadd struct ieee80211_node *,
207692ad7caSAdrian Chadd const struct ieee80211_bpf_params *);
208b032f27cSSam Leffler static int wpi_raw_xmit(struct ieee80211_node *, struct mbuf *,
209b032f27cSSam Leffler const struct ieee80211_bpf_params *);
2107a79cebfSGleb Smirnoff static int wpi_transmit(struct ieee80211com *, struct mbuf *);
211692ad7caSAdrian Chadd static void wpi_watchdog_rfkill(void *);
2120bc39fc6SAdrian Chadd static void wpi_scan_timeout(void *);
213d74e544aSAdrian Chadd static void wpi_tx_timeout(void *);
2147a79cebfSGleb Smirnoff static void wpi_parent(struct ieee80211com *);
215525a3d47SAdrian Chadd static int wpi_cmd(struct wpi_softc *, uint8_t, const void *, uint16_t,
216525a3d47SAdrian Chadd int);
217692ad7caSAdrian Chadd static int wpi_mrr_setup(struct wpi_softc *);
218692ad7caSAdrian Chadd static int wpi_add_node(struct wpi_softc *, struct ieee80211_node *);
219692ad7caSAdrian Chadd static int wpi_add_broadcast_node(struct wpi_softc *, int);
220692ad7caSAdrian Chadd static int wpi_add_ibss_node(struct wpi_softc *, struct ieee80211_node *);
221692ad7caSAdrian Chadd static void wpi_del_node(struct wpi_softc *, struct ieee80211_node *);
222692ad7caSAdrian Chadd static int wpi_updateedca(struct ieee80211com *);
223692ad7caSAdrian Chadd static void wpi_set_promisc(struct wpi_softc *);
224272f6adeSGleb Smirnoff static void wpi_update_promisc(struct ieee80211com *);
225272f6adeSGleb Smirnoff static void wpi_update_mcast(struct ieee80211com *);
226692ad7caSAdrian Chadd static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
227692ad7caSAdrian Chadd static int wpi_set_timing(struct wpi_softc *, struct ieee80211_node *);
228692ad7caSAdrian Chadd static void wpi_power_calibration(struct wpi_softc *);
229692ad7caSAdrian Chadd static int wpi_set_txpower(struct wpi_softc *, int);
230692ad7caSAdrian Chadd static int wpi_get_power_index(struct wpi_softc *,
231655e8ba8SAdrian Chadd struct wpi_power_group *, uint8_t, int, int);
232692ad7caSAdrian Chadd static int wpi_set_pslevel(struct wpi_softc *, uint8_t, int, int);
233692ad7caSAdrian Chadd static int wpi_send_btcoex(struct wpi_softc *);
234692ad7caSAdrian Chadd static int wpi_send_rxon(struct wpi_softc *, int, int);
235692ad7caSAdrian Chadd static int wpi_config(struct wpi_softc *);
236692ad7caSAdrian Chadd static uint16_t wpi_get_active_dwell_time(struct wpi_softc *,
237692ad7caSAdrian Chadd struct ieee80211_channel *, uint8_t);
238692ad7caSAdrian Chadd static uint16_t wpi_limit_dwell(struct wpi_softc *, uint16_t);
239692ad7caSAdrian Chadd static uint16_t wpi_get_passive_dwell_time(struct wpi_softc *,
240692ad7caSAdrian Chadd struct ieee80211_channel *);
241efccc3c6SAdrian Chadd static uint32_t wpi_get_scan_pause_time(uint32_t, uint16_t);
242692ad7caSAdrian Chadd static int wpi_scan(struct wpi_softc *, struct ieee80211_channel *);
243692ad7caSAdrian Chadd static int wpi_auth(struct wpi_softc *, struct ieee80211vap *);
2446f8421f8SAdrian Chadd static int wpi_config_beacon(struct wpi_vap *);
245692ad7caSAdrian Chadd static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
2466f8421f8SAdrian Chadd static void wpi_update_beacon(struct ieee80211vap *, int);
247d138d3baSAdrian Chadd static void wpi_newassoc(struct ieee80211_node *, int);
248692ad7caSAdrian Chadd static int wpi_run(struct wpi_softc *, struct ieee80211vap *);
249d29fcfb7SAdrian Chadd static int wpi_load_key(struct ieee80211_node *,
250d29fcfb7SAdrian Chadd const struct ieee80211_key *);
251d29fcfb7SAdrian Chadd static void wpi_load_key_cb(void *, struct ieee80211_node *);
252d29fcfb7SAdrian Chadd static int wpi_set_global_keys(struct ieee80211_node *);
253d29fcfb7SAdrian Chadd static int wpi_del_key(struct ieee80211_node *,
254d29fcfb7SAdrian Chadd const struct ieee80211_key *);
255d29fcfb7SAdrian Chadd static void wpi_del_key_cb(void *, struct ieee80211_node *);
256d29fcfb7SAdrian Chadd static int wpi_process_key(struct ieee80211vap *,
257d29fcfb7SAdrian Chadd const struct ieee80211_key *, int);
258692ad7caSAdrian Chadd static int wpi_key_set(struct ieee80211vap *,
259bc813c40SAdrian Chadd const struct ieee80211_key *);
260692ad7caSAdrian Chadd static int wpi_key_delete(struct ieee80211vap *,
261692ad7caSAdrian Chadd const struct ieee80211_key *);
262692ad7caSAdrian Chadd static int wpi_post_alive(struct wpi_softc *);
263525a3d47SAdrian Chadd static int wpi_load_bootcode(struct wpi_softc *, const uint8_t *,
264525a3d47SAdrian Chadd uint32_t);
265692ad7caSAdrian Chadd static int wpi_load_firmware(struct wpi_softc *);
266692ad7caSAdrian Chadd static int wpi_read_firmware(struct wpi_softc *);
267692ad7caSAdrian Chadd static void wpi_unload_firmware(struct wpi_softc *);
268692ad7caSAdrian Chadd static int wpi_clock_wait(struct wpi_softc *);
269692ad7caSAdrian Chadd static int wpi_apm_init(struct wpi_softc *);
270692ad7caSAdrian Chadd static void wpi_apm_stop_master(struct wpi_softc *);
271692ad7caSAdrian Chadd static void wpi_apm_stop(struct wpi_softc *);
272692ad7caSAdrian Chadd static void wpi_nic_config(struct wpi_softc *);
273692ad7caSAdrian Chadd static int wpi_hw_init(struct wpi_softc *);
274692ad7caSAdrian Chadd static void wpi_hw_stop(struct wpi_softc *);
275692ad7caSAdrian Chadd static void wpi_radio_on(void *, int);
276692ad7caSAdrian Chadd static void wpi_radio_off(void *, int);
2777a79cebfSGleb Smirnoff static int wpi_init(struct wpi_softc *);
278692ad7caSAdrian Chadd static void wpi_stop_locked(struct wpi_softc *);
279692ad7caSAdrian Chadd static void wpi_stop(struct wpi_softc *);
2806607310bSBenjamin Close static void wpi_scan_start(struct ieee80211com *);
2816607310bSBenjamin Close static void wpi_scan_end(struct ieee80211com *);
2826607310bSBenjamin Close static void wpi_set_channel(struct ieee80211com *);
283b032f27cSSam Leffler static void wpi_scan_curchan(struct ieee80211_scan_state *, unsigned long);
284b032f27cSSam Leffler static void wpi_scan_mindwell(struct ieee80211_scan_state *);
2856607310bSBenjamin Close
2866607310bSBenjamin Close static device_method_t wpi_methods[] = {
2876607310bSBenjamin Close /* Device interface */
2886607310bSBenjamin Close DEVMETHOD(device_probe, wpi_probe),
2896607310bSBenjamin Close DEVMETHOD(device_attach, wpi_attach),
2906607310bSBenjamin Close DEVMETHOD(device_detach, wpi_detach),
2916607310bSBenjamin Close DEVMETHOD(device_shutdown, wpi_shutdown),
2926607310bSBenjamin Close DEVMETHOD(device_suspend, wpi_suspend),
2936607310bSBenjamin Close DEVMETHOD(device_resume, wpi_resume),
2946607310bSBenjamin Close
295da67681dSMarius Strobl DEVMETHOD_END
2966607310bSBenjamin Close };
2976607310bSBenjamin Close
2986607310bSBenjamin Close static driver_t wpi_driver = {
2996607310bSBenjamin Close "wpi",
3006607310bSBenjamin Close wpi_methods,
3016607310bSBenjamin Close sizeof (struct wpi_softc)
3026607310bSBenjamin Close };
3036607310bSBenjamin Close
30451776a92SJohn Baldwin DRIVER_MODULE(wpi, pci, wpi_driver, NULL, NULL);
3056607310bSBenjamin Close
306129145a4SBernhard Schmidt MODULE_VERSION(wpi, 1);
307129145a4SBernhard Schmidt
308692ad7caSAdrian Chadd MODULE_DEPEND(wpi, pci, 1, 1, 1);
309692ad7caSAdrian Chadd MODULE_DEPEND(wpi, wlan, 1, 1, 1);
310692ad7caSAdrian Chadd MODULE_DEPEND(wpi, firmware, 1, 1, 1);
3116607310bSBenjamin Close
3126607310bSBenjamin Close static int
wpi_probe(device_t dev)3136607310bSBenjamin Close wpi_probe(device_t dev)
3146607310bSBenjamin Close {
3156607310bSBenjamin Close const struct wpi_ident *ident;
3166607310bSBenjamin Close
3176607310bSBenjamin Close for (ident = wpi_ident_table; ident->name != NULL; ident++) {
3186607310bSBenjamin Close if (pci_get_vendor(dev) == ident->vendor &&
3196607310bSBenjamin Close pci_get_device(dev) == ident->device) {
3206607310bSBenjamin Close device_set_desc(dev, ident->name);
321da67681dSMarius Strobl return (BUS_PROBE_DEFAULT);
3226607310bSBenjamin Close }
3236607310bSBenjamin Close }
3246607310bSBenjamin Close return ENXIO;
3256607310bSBenjamin Close }
3266607310bSBenjamin Close
3276607310bSBenjamin Close static int
wpi_attach(device_t dev)3286607310bSBenjamin Close wpi_attach(device_t dev)
3296607310bSBenjamin Close {
330692ad7caSAdrian Chadd struct wpi_softc *sc = (struct wpi_softc *)device_get_softc(dev);
331b032f27cSSam Leffler struct ieee80211com *ic;
332525a3d47SAdrian Chadd uint8_t i;
333525a3d47SAdrian Chadd int error, rid;
334cb0de40fSAdrian Chadd #ifdef WPI_DEBUG
335cb0de40fSAdrian Chadd int supportsa = 1;
3366607310bSBenjamin Close const struct wpi_ident *ident;
337cb0de40fSAdrian Chadd #endif
3386607310bSBenjamin Close
3396607310bSBenjamin Close sc->sc_dev = dev;
3406607310bSBenjamin Close
341692ad7caSAdrian Chadd #ifdef WPI_DEBUG
342692ad7caSAdrian Chadd error = resource_int_value(device_get_name(sc->sc_dev),
343692ad7caSAdrian Chadd device_get_unit(sc->sc_dev), "debug", &(sc->sc_debug));
344692ad7caSAdrian Chadd if (error != 0)
345692ad7caSAdrian Chadd sc->sc_debug = 0;
346692ad7caSAdrian Chadd #else
347692ad7caSAdrian Chadd sc->sc_debug = 0;
348692ad7caSAdrian Chadd #endif
349692ad7caSAdrian Chadd
350692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
351692ad7caSAdrian Chadd
352692ad7caSAdrian Chadd /*
353692ad7caSAdrian Chadd * Get the offset of the PCI Express Capability Structure in PCI
354692ad7caSAdrian Chadd * Configuration Space.
355692ad7caSAdrian Chadd */
356692ad7caSAdrian Chadd error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off);
357692ad7caSAdrian Chadd if (error != 0) {
358692ad7caSAdrian Chadd device_printf(dev, "PCIe capability structure not found!\n");
359692ad7caSAdrian Chadd return error;
360692ad7caSAdrian Chadd }
3616607310bSBenjamin Close
3626607310bSBenjamin Close /*
3636607310bSBenjamin Close * Some card's only support 802.11b/g not a, check to see if
3646607310bSBenjamin Close * this is one such card. A 0x0 in the subdevice table indicates
3656607310bSBenjamin Close * the entire subdevice range is to be ignored.
3666607310bSBenjamin Close */
367cb0de40fSAdrian Chadd #ifdef WPI_DEBUG
3686607310bSBenjamin Close for (ident = wpi_ident_table; ident->name != NULL; ident++) {
3696607310bSBenjamin Close if (ident->subdevice &&
3706607310bSBenjamin Close pci_get_subdevice(dev) == ident->subdevice) {
3716607310bSBenjamin Close supportsa = 0;
3726607310bSBenjamin Close break;
3736607310bSBenjamin Close }
3746607310bSBenjamin Close }
375cb0de40fSAdrian Chadd #endif
3766607310bSBenjamin Close
377692ad7caSAdrian Chadd /* Clear device-specific "PCI retry timeout" register (41h). */
3786607310bSBenjamin Close pci_write_config(dev, 0x41, 0, 1);
3796607310bSBenjamin Close
380692ad7caSAdrian Chadd /* Enable bus-mastering. */
3816607310bSBenjamin Close pci_enable_busmaster(dev);
3826607310bSBenjamin Close
383da67681dSMarius Strobl rid = PCIR_BAR(0);
384da67681dSMarius Strobl sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3856607310bSBenjamin Close RF_ACTIVE);
3866607310bSBenjamin Close if (sc->mem == NULL) {
387692ad7caSAdrian Chadd device_printf(dev, "can't map mem space\n");
388419435b8SAdrian Chadd return ENOMEM;
3896607310bSBenjamin Close }
3906607310bSBenjamin Close sc->sc_st = rman_get_bustag(sc->mem);
3916607310bSBenjamin Close sc->sc_sh = rman_get_bushandle(sc->mem);
3926607310bSBenjamin Close
393692ad7caSAdrian Chadd rid = 1;
394525a3d47SAdrian Chadd if (pci_alloc_msi(dev, &rid) == 0)
395525a3d47SAdrian Chadd rid = 1;
396525a3d47SAdrian Chadd else
397525a3d47SAdrian Chadd rid = 0;
398692ad7caSAdrian Chadd /* Install interrupt handler. */
399692ad7caSAdrian Chadd sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
400692ad7caSAdrian Chadd (rid != 0 ? 0 : RF_SHAREABLE));
4016607310bSBenjamin Close if (sc->irq == NULL) {
402692ad7caSAdrian Chadd device_printf(dev, "can't map interrupt\n");
4036607310bSBenjamin Close error = ENOMEM;
4046607310bSBenjamin Close goto fail;
4056607310bSBenjamin Close }
4066607310bSBenjamin Close
407692ad7caSAdrian Chadd WPI_LOCK_INIT(sc);
4085effcbfdSAdrian Chadd WPI_TX_LOCK_INIT(sc);
409446685e4SAdrian Chadd WPI_RXON_LOCK_INIT(sc);
4107964dd56SAdrian Chadd WPI_NT_LOCK_INIT(sc);
41163f57699SAdrian Chadd WPI_TXQ_LOCK_INIT(sc);
412fc5870b4SAdrian Chadd WPI_TXQ_STATE_LOCK_INIT(sc);
413692ad7caSAdrian Chadd
414692ad7caSAdrian Chadd /* Allocate DMA memory for firmware transfers. */
4156607310bSBenjamin Close if ((error = wpi_alloc_fwmem(sc)) != 0) {
416692ad7caSAdrian Chadd device_printf(dev,
417692ad7caSAdrian Chadd "could not allocate memory for firmware, error %d\n",
418692ad7caSAdrian Chadd error);
4196607310bSBenjamin Close goto fail;
4206607310bSBenjamin Close }
4216607310bSBenjamin Close
422692ad7caSAdrian Chadd /* Allocate shared page. */
4236607310bSBenjamin Close if ((error = wpi_alloc_shared(sc)) != 0) {
4246607310bSBenjamin Close device_printf(dev, "could not allocate shared page\n");
4256607310bSBenjamin Close goto fail;
4266607310bSBenjamin Close }
4276607310bSBenjamin Close
428692ad7caSAdrian Chadd /* Allocate TX rings - 4 for QoS purposes, 1 for commands. */
42919053a54SAdrian Chadd for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
430692ad7caSAdrian Chadd if ((error = wpi_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) {
431692ad7caSAdrian Chadd device_printf(dev,
432692ad7caSAdrian Chadd "could not allocate TX ring %d, error %d\n", i,
433692ad7caSAdrian Chadd error);
4346607310bSBenjamin Close goto fail;
4356607310bSBenjamin Close }
4366607310bSBenjamin Close }
4376607310bSBenjamin Close
438692ad7caSAdrian Chadd /* Allocate RX ring. */
439692ad7caSAdrian Chadd if ((error = wpi_alloc_rx_ring(sc)) != 0) {
440692ad7caSAdrian Chadd device_printf(dev, "could not allocate RX ring, error %d\n",
441692ad7caSAdrian Chadd error);
4426607310bSBenjamin Close goto fail;
4436607310bSBenjamin Close }
4446607310bSBenjamin Close
445692ad7caSAdrian Chadd /* Clear pending interrupts. */
446692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT, 0xffffffff);
4476607310bSBenjamin Close
4487a79cebfSGleb Smirnoff ic = &sc->sc_ic;
44959686fe9SGleb Smirnoff ic->ic_softc = sc;
450c8550c02SGleb Smirnoff ic->ic_name = device_get_nameunit(dev);
4516607310bSBenjamin Close ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
4526607310bSBenjamin Close ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
4536607310bSBenjamin Close
454692ad7caSAdrian Chadd /* Set device capabilities. */
4556607310bSBenjamin Close ic->ic_caps =
456c43feedeSSam Leffler IEEE80211_C_STA /* station mode supported */
457692ad7caSAdrian Chadd | IEEE80211_C_IBSS /* IBSS mode supported */
458726ddc26SAdrian Chadd | IEEE80211_C_HOSTAP /* Host access point mode */
459c43feedeSSam Leffler | IEEE80211_C_MONITOR /* monitor mode supported */
460692ad7caSAdrian Chadd | IEEE80211_C_AHDEMO /* adhoc demo mode */
461692ad7caSAdrian Chadd | IEEE80211_C_BGSCAN /* capable of bg scanning */
462940aa808SAdrian Chadd | IEEE80211_C_TXFRAG /* handle tx frags */
4636607310bSBenjamin Close | IEEE80211_C_TXPMGT /* tx power management */
4646607310bSBenjamin Close | IEEE80211_C_SHSLOT /* short slot time supported */
4656607310bSBenjamin Close | IEEE80211_C_WPA /* 802.11i */
466692ad7caSAdrian Chadd | IEEE80211_C_SHPREAMBLE /* short preamble supported */
467692ad7caSAdrian Chadd | IEEE80211_C_WME /* 802.11e */
468692ad7caSAdrian Chadd | IEEE80211_C_PMGT /* Station-side power mgmt */
4696607310bSBenjamin Close ;
4706607310bSBenjamin Close
471692ad7caSAdrian Chadd ic->ic_cryptocaps =
472692ad7caSAdrian Chadd IEEE80211_CRYPTO_AES_CCM;
473692ad7caSAdrian Chadd
4746607310bSBenjamin Close /*
4756607310bSBenjamin Close * Read in the eeprom and also setup the channels for
4766607310bSBenjamin Close * net80211. We don't set the rates as net80211 does this for us
4776607310bSBenjamin Close */
4787a79cebfSGleb Smirnoff if ((error = wpi_read_eeprom(sc, ic->ic_macaddr)) != 0) {
479692ad7caSAdrian Chadd device_printf(dev, "could not read EEPROM, error %d\n",
480692ad7caSAdrian Chadd error);
481692ad7caSAdrian Chadd goto fail;
482692ad7caSAdrian Chadd }
4836607310bSBenjamin Close
484692ad7caSAdrian Chadd #ifdef WPI_DEBUG
485692ad7caSAdrian Chadd if (bootverbose) {
486bb086d4eSAdrian Chadd device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n",
487bb086d4eSAdrian Chadd sc->domain);
4886607310bSBenjamin Close device_printf(sc->sc_dev, "Hardware Type: %c\n",
4896607310bSBenjamin Close sc->type > 1 ? 'B': '?');
4906607310bSBenjamin Close device_printf(sc->sc_dev, "Hardware Revision: %c\n",
491f736d898SAdrian Chadd ((sc->rev & 0xf0) == 0xd0) ? 'D': '?');
4926607310bSBenjamin Close device_printf(sc->sc_dev, "SKU %s support 802.11a\n",
4936607310bSBenjamin Close supportsa ? "does" : "does not");
4946607310bSBenjamin Close
495bb086d4eSAdrian Chadd /* XXX hw_config uses the PCIDEV for the Hardware rev. Must
496bb086d4eSAdrian Chadd check what sc->rev really represents - benjsc 20070615 */
4976607310bSBenjamin Close }
498692ad7caSAdrian Chadd #endif
4996607310bSBenjamin Close
5007a79cebfSGleb Smirnoff ieee80211_ifattach(ic);
501692ad7caSAdrian Chadd ic->ic_vap_create = wpi_vap_create;
502692ad7caSAdrian Chadd ic->ic_vap_delete = wpi_vap_delete;
5037a79cebfSGleb Smirnoff ic->ic_parent = wpi_parent;
504b032f27cSSam Leffler ic->ic_raw_xmit = wpi_raw_xmit;
5057a79cebfSGleb Smirnoff ic->ic_transmit = wpi_transmit;
506692ad7caSAdrian Chadd ic->ic_node_alloc = wpi_node_alloc;
507692ad7caSAdrian Chadd sc->sc_node_free = ic->ic_node_free;
508692ad7caSAdrian Chadd ic->ic_node_free = wpi_node_free;
509692ad7caSAdrian Chadd ic->ic_wme.wme_update = wpi_updateedca;
510692ad7caSAdrian Chadd ic->ic_update_promisc = wpi_update_promisc;
511692ad7caSAdrian Chadd ic->ic_update_mcast = wpi_update_mcast;
512d138d3baSAdrian Chadd ic->ic_newassoc = wpi_newassoc;
5136607310bSBenjamin Close ic->ic_scan_start = wpi_scan_start;
5146607310bSBenjamin Close ic->ic_scan_end = wpi_scan_end;
5156607310bSBenjamin Close ic->ic_set_channel = wpi_set_channel;
5166607310bSBenjamin Close ic->ic_scan_curchan = wpi_scan_curchan;
5176607310bSBenjamin Close ic->ic_scan_mindwell = wpi_scan_mindwell;
5181c6b43dfSAndriy Voskoboinyk ic->ic_getradiocaps = wpi_getradiocaps;
519692ad7caSAdrian Chadd ic->ic_setregdomain = wpi_setregdomain;
5206607310bSBenjamin Close
5215cacb17fSAdrian Chadd sc->sc_update_rx_ring = wpi_update_rx_ring;
5225cacb17fSAdrian Chadd sc->sc_update_tx_ring = wpi_update_tx_ring;
5235cacb17fSAdrian Chadd
524692ad7caSAdrian Chadd wpi_radiotap_attach(sc);
5256607310bSBenjamin Close
526f6930becSAndriy Voskoboinyk /* Setup Tx status flags (constant). */
5273f3d4d61SAndriy Voskoboinyk sc->sc_txs.flags = IEEE80211_RATECTL_STATUS_PKTLEN |
5283f3d4d61SAndriy Voskoboinyk IEEE80211_RATECTL_STATUS_SHORT_RETRY |
529f6930becSAndriy Voskoboinyk IEEE80211_RATECTL_STATUS_LONG_RETRY;
530f6930becSAndriy Voskoboinyk
531446685e4SAdrian Chadd callout_init_mtx(&sc->calib_to, &sc->rxon_mtx, 0);
532446685e4SAdrian Chadd callout_init_mtx(&sc->scan_timeout, &sc->rxon_mtx, 0);
533fc5870b4SAdrian Chadd callout_init_mtx(&sc->tx_timeout, &sc->txq_state_mtx, 0);
534692ad7caSAdrian Chadd callout_init_mtx(&sc->watchdog_rfkill, &sc->sc_mtx, 0);
535692ad7caSAdrian Chadd TASK_INIT(&sc->sc_radiooff_task, 0, wpi_radio_off, sc);
536692ad7caSAdrian Chadd TASK_INIT(&sc->sc_radioon_task, 0, wpi_radio_on, sc);
537692ad7caSAdrian Chadd
538692ad7caSAdrian Chadd wpi_sysctlattach(sc);
5396607310bSBenjamin Close
5406607310bSBenjamin Close /*
5416607310bSBenjamin Close * Hook our interrupt after all initialization is complete.
5426607310bSBenjamin Close */
5436607310bSBenjamin Close error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
54482f1b132SAndrew Thompson NULL, wpi_intr, sc, &sc->sc_ih);
5456607310bSBenjamin Close if (error != 0) {
546692ad7caSAdrian Chadd device_printf(dev, "can't establish interrupt, error %d\n",
547692ad7caSAdrian Chadd error);
5486607310bSBenjamin Close goto fail;
5496607310bSBenjamin Close }
5506607310bSBenjamin Close
55182f1b132SAndrew Thompson if (bootverbose)
5526607310bSBenjamin Close ieee80211_announce(ic);
553692ad7caSAdrian Chadd
554692ad7caSAdrian Chadd #ifdef WPI_DEBUG
555692ad7caSAdrian Chadd if (sc->sc_debug & WPI_DEBUG_HW)
5566607310bSBenjamin Close ieee80211_announce_channels(ic);
5576607310bSBenjamin Close #endif
558692ad7caSAdrian Chadd
559692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5606607310bSBenjamin Close return 0;
5616607310bSBenjamin Close
5626607310bSBenjamin Close fail: wpi_detach(dev);
563692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
564692ad7caSAdrian Chadd return error;
5656607310bSBenjamin Close }
5666607310bSBenjamin Close
567692ad7caSAdrian Chadd /*
568692ad7caSAdrian Chadd * Attach the interface to 802.11 radiotap.
569692ad7caSAdrian Chadd */
570692ad7caSAdrian Chadd static void
wpi_radiotap_attach(struct wpi_softc * sc)571692ad7caSAdrian Chadd wpi_radiotap_attach(struct wpi_softc *sc)
5726607310bSBenjamin Close {
5737a79cebfSGleb Smirnoff struct wpi_rx_radiotap_header *rxtap = &sc->sc_rxtap;
5747a79cebfSGleb Smirnoff struct wpi_tx_radiotap_header *txtap = &sc->sc_txtap;
5757a79cebfSGleb Smirnoff
576692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
5777a79cebfSGleb Smirnoff ieee80211_radiotap_attach(&sc->sc_ic,
5787a79cebfSGleb Smirnoff &txtap->wt_ihdr, sizeof(*txtap), WPI_TX_RADIOTAP_PRESENT,
5797a79cebfSGleb Smirnoff &rxtap->wr_ihdr, sizeof(*rxtap), WPI_RX_RADIOTAP_PRESENT);
580692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5816607310bSBenjamin Close }
5826607310bSBenjamin Close
583692ad7caSAdrian Chadd static void
wpi_sysctlattach(struct wpi_softc * sc)584692ad7caSAdrian Chadd wpi_sysctlattach(struct wpi_softc *sc)
585692ad7caSAdrian Chadd {
586692ad7caSAdrian Chadd #ifdef WPI_DEBUG
587692ad7caSAdrian Chadd struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
588692ad7caSAdrian Chadd struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
5896607310bSBenjamin Close
590692ad7caSAdrian Chadd SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
591692ad7caSAdrian Chadd "debug", CTLFLAG_RW, &sc->sc_debug, sc->sc_debug,
592692ad7caSAdrian Chadd "control debugging printfs");
593692ad7caSAdrian Chadd #endif
5946607310bSBenjamin Close }
5956607310bSBenjamin Close
59621f6580eSAdrian Chadd static void
wpi_init_beacon(struct wpi_vap * wvp)59721f6580eSAdrian Chadd wpi_init_beacon(struct wpi_vap *wvp)
59821f6580eSAdrian Chadd {
59921f6580eSAdrian Chadd struct wpi_buf *bcn = &wvp->wv_bcbuf;
60021f6580eSAdrian Chadd struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
60121f6580eSAdrian Chadd
60221f6580eSAdrian Chadd cmd->id = WPI_ID_BROADCAST;
60321f6580eSAdrian Chadd cmd->ofdm_mask = 0xff;
60421f6580eSAdrian Chadd cmd->cck_mask = 0x0f;
60521f6580eSAdrian Chadd cmd->lifetime = htole32(WPI_LIFETIME_INFINITE);
606c39df5f4SAdrian Chadd
607c39df5f4SAdrian Chadd /*
608c39df5f4SAdrian Chadd * XXX WPI_TX_AUTO_SEQ seems to be ignored - workaround this issue
609c39df5f4SAdrian Chadd * XXX by using WPI_TX_NEED_ACK instead (with some side effects).
610c39df5f4SAdrian Chadd */
611c39df5f4SAdrian Chadd cmd->flags = htole32(WPI_TX_NEED_ACK | WPI_TX_INSERT_TSTAMP);
61221f6580eSAdrian Chadd
61321f6580eSAdrian Chadd bcn->code = WPI_CMD_SET_BEACON;
61421f6580eSAdrian Chadd bcn->ac = WPI_CMD_QUEUE_NUM;
61521f6580eSAdrian Chadd bcn->size = sizeof(struct wpi_cmd_beacon);
61621f6580eSAdrian Chadd }
61721f6580eSAdrian Chadd
618b032f27cSSam Leffler static struct ieee80211vap *
wpi_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])619fcd9500fSBernhard Schmidt wpi_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
620fcd9500fSBernhard Schmidt enum ieee80211_opmode opmode, int flags,
621b032f27cSSam Leffler const uint8_t bssid[IEEE80211_ADDR_LEN],
622b032f27cSSam Leffler const uint8_t mac[IEEE80211_ADDR_LEN])
623b032f27cSSam Leffler {
624b032f27cSSam Leffler struct wpi_vap *wvp;
625b032f27cSSam Leffler struct ieee80211vap *vap;
626b032f27cSSam Leffler
627b032f27cSSam Leffler if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
628b032f27cSSam Leffler return NULL;
629692ad7caSAdrian Chadd
6307a79cebfSGleb Smirnoff wvp = malloc(sizeof(struct wpi_vap), M_80211_VAP, M_WAITOK | M_ZERO);
631d036bb20SAdrian Chadd vap = &wvp->wv_vap;
6327a79cebfSGleb Smirnoff ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
633692ad7caSAdrian Chadd
634726ddc26SAdrian Chadd if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
635b56fed3dSAdrian Chadd WPI_VAP_LOCK_INIT(wvp);
63621f6580eSAdrian Chadd wpi_init_beacon(wvp);
637b56fed3dSAdrian Chadd }
63821f6580eSAdrian Chadd
639692ad7caSAdrian Chadd /* Override with driver methods. */
640692ad7caSAdrian Chadd vap->iv_key_set = wpi_key_set;
641692ad7caSAdrian Chadd vap->iv_key_delete = wpi_key_delete;
64257c61f5dSAdrian Chadd if (opmode == IEEE80211_M_IBSS) {
643a20c9affSAdrian Chadd wvp->wv_recv_mgmt = vap->iv_recv_mgmt;
64457c61f5dSAdrian Chadd vap->iv_recv_mgmt = wpi_ibss_recv_mgmt;
64557c61f5dSAdrian Chadd }
646d036bb20SAdrian Chadd wvp->wv_newstate = vap->iv_newstate;
647b032f27cSSam Leffler vap->iv_newstate = wpi_newstate;
648692ad7caSAdrian Chadd vap->iv_update_beacon = wpi_update_beacon;
649726ddc26SAdrian Chadd vap->iv_max_aid = WPI_ID_IBSS_MAX - WPI_ID_IBSS_MIN + 1;
650b032f27cSSam Leffler
651b6108616SRui Paulo ieee80211_ratectl_init(vap);
652692ad7caSAdrian Chadd /* Complete setup. */
6537167f5fcSAdrian Chadd ieee80211_vap_attach(vap, ieee80211_media_change,
6547a79cebfSGleb Smirnoff ieee80211_media_status, mac);
655b032f27cSSam Leffler ic->ic_opmode = opmode;
656b032f27cSSam Leffler return vap;
657b032f27cSSam Leffler }
658b032f27cSSam Leffler
659b032f27cSSam Leffler static void
wpi_vap_delete(struct ieee80211vap * vap)660b032f27cSSam Leffler wpi_vap_delete(struct ieee80211vap *vap)
661b032f27cSSam Leffler {
662b032f27cSSam Leffler struct wpi_vap *wvp = WPI_VAP(vap);
6636f8421f8SAdrian Chadd struct wpi_buf *bcn = &wvp->wv_bcbuf;
6646f8421f8SAdrian Chadd enum ieee80211_opmode opmode = vap->iv_opmode;
665b032f27cSSam Leffler
666b6108616SRui Paulo ieee80211_ratectl_deinit(vap);
667b032f27cSSam Leffler ieee80211_vap_detach(vap);
668692ad7caSAdrian Chadd
669726ddc26SAdrian Chadd if (opmode == IEEE80211_M_IBSS || opmode == IEEE80211_M_HOSTAP) {
6706f8421f8SAdrian Chadd if (bcn->m != NULL)
6716f8421f8SAdrian Chadd m_freem(bcn->m);
672b56fed3dSAdrian Chadd
673b56fed3dSAdrian Chadd WPI_VAP_LOCK_DESTROY(wvp);
6746f8421f8SAdrian Chadd }
6756f8421f8SAdrian Chadd
676b032f27cSSam Leffler free(wvp, M_80211_VAP);
677b032f27cSSam Leffler }
678b032f27cSSam Leffler
6796607310bSBenjamin Close static int
wpi_detach(device_t dev)680692ad7caSAdrian Chadd wpi_detach(device_t dev)
6816607310bSBenjamin Close {
682692ad7caSAdrian Chadd struct wpi_softc *sc = device_get_softc(dev);
6837a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
684525a3d47SAdrian Chadd uint8_t qid;
6856607310bSBenjamin Close
686692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
6876607310bSBenjamin Close
6887a79cebfSGleb Smirnoff if (ic->ic_vap_create == wpi_vap_create) {
6890c66281cSAdrian Chadd ieee80211_draintask(ic, &sc->sc_radioon_task);
690abd89aa2SAndriy Voskoboinyk ieee80211_draintask(ic, &sc->sc_radiooff_task);
691692ad7caSAdrian Chadd
692692ad7caSAdrian Chadd wpi_stop(sc);
693692ad7caSAdrian Chadd
694692ad7caSAdrian Chadd callout_drain(&sc->watchdog_rfkill);
695d74e544aSAdrian Chadd callout_drain(&sc->tx_timeout);
6960bc39fc6SAdrian Chadd callout_drain(&sc->scan_timeout);
697692ad7caSAdrian Chadd callout_drain(&sc->calib_to);
698692ad7caSAdrian Chadd ieee80211_ifdetach(ic);
6996607310bSBenjamin Close }
7006607310bSBenjamin Close
701692ad7caSAdrian Chadd /* Uninstall interrupt handler. */
702692ad7caSAdrian Chadd if (sc->irq != NULL) {
703692ad7caSAdrian Chadd bus_teardown_intr(dev, sc->irq, sc->sc_ih);
704692ad7caSAdrian Chadd bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq),
705692ad7caSAdrian Chadd sc->irq);
706692ad7caSAdrian Chadd pci_release_msi(dev);
70782f1b132SAndrew Thompson }
7086607310bSBenjamin Close
709692ad7caSAdrian Chadd if (sc->txq[0].data_dmat) {
710692ad7caSAdrian Chadd /* Free DMA resources. */
71119053a54SAdrian Chadd for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++)
712692ad7caSAdrian Chadd wpi_free_tx_ring(sc, &sc->txq[qid]);
713692ad7caSAdrian Chadd
714692ad7caSAdrian Chadd wpi_free_rx_ring(sc);
715692ad7caSAdrian Chadd wpi_free_shared(sc);
7166607310bSBenjamin Close }
7176607310bSBenjamin Close
718692ad7caSAdrian Chadd if (sc->fw_dma.tag)
719692ad7caSAdrian Chadd wpi_free_fwmem(sc);
7206607310bSBenjamin Close
721692ad7caSAdrian Chadd if (sc->mem != NULL)
722692ad7caSAdrian Chadd bus_release_resource(dev, SYS_RES_MEMORY,
723692ad7caSAdrian Chadd rman_get_rid(sc->mem), sc->mem);
724692ad7caSAdrian Chadd
725692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
726fc5870b4SAdrian Chadd WPI_TXQ_STATE_LOCK_DESTROY(sc);
72763f57699SAdrian Chadd WPI_TXQ_LOCK_DESTROY(sc);
7287964dd56SAdrian Chadd WPI_NT_LOCK_DESTROY(sc);
729446685e4SAdrian Chadd WPI_RXON_LOCK_DESTROY(sc);
7305effcbfdSAdrian Chadd WPI_TX_LOCK_DESTROY(sc);
731692ad7caSAdrian Chadd WPI_LOCK_DESTROY(sc);
7326607310bSBenjamin Close return 0;
7336607310bSBenjamin Close }
7346607310bSBenjamin Close
7356607310bSBenjamin Close static int
wpi_shutdown(device_t dev)7366607310bSBenjamin Close wpi_shutdown(device_t dev)
7376607310bSBenjamin Close {
7386607310bSBenjamin Close struct wpi_softc *sc = device_get_softc(dev);
7396607310bSBenjamin Close
740692ad7caSAdrian Chadd wpi_stop(sc);
7416607310bSBenjamin Close return 0;
7426607310bSBenjamin Close }
7436607310bSBenjamin Close
7446607310bSBenjamin Close static int
wpi_suspend(device_t dev)7456607310bSBenjamin Close wpi_suspend(device_t dev)
7466607310bSBenjamin Close {
7476607310bSBenjamin Close struct wpi_softc *sc = device_get_softc(dev);
7487a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
7496607310bSBenjamin Close
750ada977b1SBernhard Schmidt ieee80211_suspend_all(ic);
7516607310bSBenjamin Close return 0;
7526607310bSBenjamin Close }
7536607310bSBenjamin Close
7546607310bSBenjamin Close static int
wpi_resume(device_t dev)7556607310bSBenjamin Close wpi_resume(device_t dev)
7566607310bSBenjamin Close {
7576607310bSBenjamin Close struct wpi_softc *sc = device_get_softc(dev);
7587a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
7596607310bSBenjamin Close
760692ad7caSAdrian Chadd /* Clear device-specific "PCI retry timeout" register (41h). */
7616607310bSBenjamin Close pci_write_config(dev, 0x41, 0, 1);
7626607310bSBenjamin Close
763ada977b1SBernhard Schmidt ieee80211_resume_all(ic);
7646607310bSBenjamin Close return 0;
7656607310bSBenjamin Close }
7666607310bSBenjamin Close
767692ad7caSAdrian Chadd /*
768692ad7caSAdrian Chadd * Grab exclusive access to NIC memory.
769692ad7caSAdrian Chadd */
770692ad7caSAdrian Chadd static int
wpi_nic_lock(struct wpi_softc * sc)771692ad7caSAdrian Chadd wpi_nic_lock(struct wpi_softc *sc)
772692ad7caSAdrian Chadd {
773692ad7caSAdrian Chadd int ntries;
774692ad7caSAdrian Chadd
775692ad7caSAdrian Chadd /* Request exclusive access to NIC. */
776692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
777692ad7caSAdrian Chadd
778692ad7caSAdrian Chadd /* Spin until we actually get the lock. */
779692ad7caSAdrian Chadd for (ntries = 0; ntries < 1000; ntries++) {
780692ad7caSAdrian Chadd if ((WPI_READ(sc, WPI_GP_CNTRL) &
781692ad7caSAdrian Chadd (WPI_GP_CNTRL_MAC_ACCESS_ENA | WPI_GP_CNTRL_SLEEP)) ==
782692ad7caSAdrian Chadd WPI_GP_CNTRL_MAC_ACCESS_ENA)
783692ad7caSAdrian Chadd return 0;
784692ad7caSAdrian Chadd DELAY(10);
785692ad7caSAdrian Chadd }
786692ad7caSAdrian Chadd
787692ad7caSAdrian Chadd device_printf(sc->sc_dev, "could not lock memory\n");
788692ad7caSAdrian Chadd
789692ad7caSAdrian Chadd return ETIMEDOUT;
790692ad7caSAdrian Chadd }
791692ad7caSAdrian Chadd
792692ad7caSAdrian Chadd /*
793692ad7caSAdrian Chadd * Release lock on NIC memory.
794692ad7caSAdrian Chadd */
795692ad7caSAdrian Chadd static __inline void
wpi_nic_unlock(struct wpi_softc * sc)796692ad7caSAdrian Chadd wpi_nic_unlock(struct wpi_softc *sc)
797692ad7caSAdrian Chadd {
798692ad7caSAdrian Chadd WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
799692ad7caSAdrian Chadd }
800692ad7caSAdrian Chadd
801692ad7caSAdrian Chadd static __inline uint32_t
wpi_prph_read(struct wpi_softc * sc,uint32_t addr)802692ad7caSAdrian Chadd wpi_prph_read(struct wpi_softc *sc, uint32_t addr)
803692ad7caSAdrian Chadd {
804692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_PRPH_RADDR, WPI_PRPH_DWORD | addr);
805692ad7caSAdrian Chadd WPI_BARRIER_READ_WRITE(sc);
806692ad7caSAdrian Chadd return WPI_READ(sc, WPI_PRPH_RDATA);
807692ad7caSAdrian Chadd }
808692ad7caSAdrian Chadd
809692ad7caSAdrian Chadd static __inline void
wpi_prph_write(struct wpi_softc * sc,uint32_t addr,uint32_t data)810692ad7caSAdrian Chadd wpi_prph_write(struct wpi_softc *sc, uint32_t addr, uint32_t data)
811692ad7caSAdrian Chadd {
812692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_PRPH_WADDR, WPI_PRPH_DWORD | addr);
813692ad7caSAdrian Chadd WPI_BARRIER_WRITE(sc);
814692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_PRPH_WDATA, data);
815692ad7caSAdrian Chadd }
816692ad7caSAdrian Chadd
817692ad7caSAdrian Chadd static __inline void
wpi_prph_setbits(struct wpi_softc * sc,uint32_t addr,uint32_t mask)818692ad7caSAdrian Chadd wpi_prph_setbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
819692ad7caSAdrian Chadd {
820692ad7caSAdrian Chadd wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) | mask);
821692ad7caSAdrian Chadd }
822692ad7caSAdrian Chadd
823692ad7caSAdrian Chadd static __inline void
wpi_prph_clrbits(struct wpi_softc * sc,uint32_t addr,uint32_t mask)824692ad7caSAdrian Chadd wpi_prph_clrbits(struct wpi_softc *sc, uint32_t addr, uint32_t mask)
825692ad7caSAdrian Chadd {
826692ad7caSAdrian Chadd wpi_prph_write(sc, addr, wpi_prph_read(sc, addr) & ~mask);
827692ad7caSAdrian Chadd }
828692ad7caSAdrian Chadd
829692ad7caSAdrian Chadd static __inline void
wpi_prph_write_region_4(struct wpi_softc * sc,uint32_t addr,const uint32_t * data,uint32_t count)830692ad7caSAdrian Chadd wpi_prph_write_region_4(struct wpi_softc *sc, uint32_t addr,
831525a3d47SAdrian Chadd const uint32_t *data, uint32_t count)
832692ad7caSAdrian Chadd {
833525a3d47SAdrian Chadd for (; count != 0; count--, data++, addr += 4)
834692ad7caSAdrian Chadd wpi_prph_write(sc, addr, *data);
835692ad7caSAdrian Chadd }
836692ad7caSAdrian Chadd
837692ad7caSAdrian Chadd static __inline uint32_t
wpi_mem_read(struct wpi_softc * sc,uint32_t addr)838692ad7caSAdrian Chadd wpi_mem_read(struct wpi_softc *sc, uint32_t addr)
839692ad7caSAdrian Chadd {
840692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_MEM_RADDR, addr);
841692ad7caSAdrian Chadd WPI_BARRIER_READ_WRITE(sc);
842692ad7caSAdrian Chadd return WPI_READ(sc, WPI_MEM_RDATA);
843692ad7caSAdrian Chadd }
844692ad7caSAdrian Chadd
845692ad7caSAdrian Chadd static __inline void
wpi_mem_read_region_4(struct wpi_softc * sc,uint32_t addr,uint32_t * data,int count)846692ad7caSAdrian Chadd wpi_mem_read_region_4(struct wpi_softc *sc, uint32_t addr, uint32_t *data,
847692ad7caSAdrian Chadd int count)
848692ad7caSAdrian Chadd {
849692ad7caSAdrian Chadd for (; count > 0; count--, addr += 4)
850692ad7caSAdrian Chadd *data++ = wpi_mem_read(sc, addr);
851692ad7caSAdrian Chadd }
852692ad7caSAdrian Chadd
853692ad7caSAdrian Chadd static int
wpi_read_prom_data(struct wpi_softc * sc,uint32_t addr,void * data,int count)854692ad7caSAdrian Chadd wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int count)
855692ad7caSAdrian Chadd {
856692ad7caSAdrian Chadd uint8_t *out = data;
857692ad7caSAdrian Chadd uint32_t val;
858692ad7caSAdrian Chadd int error, ntries;
859692ad7caSAdrian Chadd
860692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
861692ad7caSAdrian Chadd
862692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
863692ad7caSAdrian Chadd return error;
864692ad7caSAdrian Chadd
865692ad7caSAdrian Chadd for (; count > 0; count -= 2, addr++) {
866692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_EEPROM, addr << 2);
867692ad7caSAdrian Chadd for (ntries = 0; ntries < 10; ntries++) {
868692ad7caSAdrian Chadd val = WPI_READ(sc, WPI_EEPROM);
869692ad7caSAdrian Chadd if (val & WPI_EEPROM_READ_VALID)
870692ad7caSAdrian Chadd break;
871692ad7caSAdrian Chadd DELAY(5);
872692ad7caSAdrian Chadd }
873692ad7caSAdrian Chadd if (ntries == 10) {
874692ad7caSAdrian Chadd device_printf(sc->sc_dev,
875692ad7caSAdrian Chadd "timeout reading ROM at 0x%x\n", addr);
876692ad7caSAdrian Chadd return ETIMEDOUT;
877692ad7caSAdrian Chadd }
878692ad7caSAdrian Chadd *out++= val >> 16;
879692ad7caSAdrian Chadd if (count > 1)
880692ad7caSAdrian Chadd *out ++= val >> 24;
881692ad7caSAdrian Chadd }
882692ad7caSAdrian Chadd
883692ad7caSAdrian Chadd wpi_nic_unlock(sc);
884692ad7caSAdrian Chadd
885692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
886692ad7caSAdrian Chadd
887692ad7caSAdrian Chadd return 0;
888692ad7caSAdrian Chadd }
889692ad7caSAdrian Chadd
890692ad7caSAdrian Chadd static void
wpi_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nsegs,int error)891692ad7caSAdrian Chadd wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
892692ad7caSAdrian Chadd {
893692ad7caSAdrian Chadd if (error != 0)
894692ad7caSAdrian Chadd return;
895692ad7caSAdrian Chadd KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
896692ad7caSAdrian Chadd *(bus_addr_t *)arg = segs[0].ds_addr;
897692ad7caSAdrian Chadd }
898692ad7caSAdrian Chadd
899692ad7caSAdrian Chadd /*
900692ad7caSAdrian Chadd * Allocates a contiguous block of dma memory of the requested size and
901692ad7caSAdrian Chadd * alignment.
902692ad7caSAdrian Chadd */
903692ad7caSAdrian Chadd static int
wpi_dma_contig_alloc(struct wpi_softc * sc,struct wpi_dma_info * dma,void ** kvap,bus_size_t size,bus_size_t alignment)904692ad7caSAdrian Chadd wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
905692ad7caSAdrian Chadd void **kvap, bus_size_t size, bus_size_t alignment)
906692ad7caSAdrian Chadd {
907692ad7caSAdrian Chadd int error;
908692ad7caSAdrian Chadd
909692ad7caSAdrian Chadd dma->tag = NULL;
910692ad7caSAdrian Chadd dma->size = size;
911692ad7caSAdrian Chadd
912692ad7caSAdrian Chadd error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
913692ad7caSAdrian Chadd 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
914b020e15eSKevin Lo 1, size, 0, NULL, NULL, &dma->tag);
915692ad7caSAdrian Chadd if (error != 0)
916692ad7caSAdrian Chadd goto fail;
917692ad7caSAdrian Chadd
918692ad7caSAdrian Chadd error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
919692ad7caSAdrian Chadd BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
920692ad7caSAdrian Chadd if (error != 0)
921692ad7caSAdrian Chadd goto fail;
922692ad7caSAdrian Chadd
923692ad7caSAdrian Chadd error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
924692ad7caSAdrian Chadd wpi_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
925692ad7caSAdrian Chadd if (error != 0)
926692ad7caSAdrian Chadd goto fail;
927692ad7caSAdrian Chadd
928692ad7caSAdrian Chadd bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
929692ad7caSAdrian Chadd
930692ad7caSAdrian Chadd if (kvap != NULL)
931692ad7caSAdrian Chadd *kvap = dma->vaddr;
932692ad7caSAdrian Chadd
933692ad7caSAdrian Chadd return 0;
934692ad7caSAdrian Chadd
935692ad7caSAdrian Chadd fail: wpi_dma_contig_free(dma);
936692ad7caSAdrian Chadd return error;
937692ad7caSAdrian Chadd }
938692ad7caSAdrian Chadd
939692ad7caSAdrian Chadd static void
wpi_dma_contig_free(struct wpi_dma_info * dma)940692ad7caSAdrian Chadd wpi_dma_contig_free(struct wpi_dma_info *dma)
941692ad7caSAdrian Chadd {
942692ad7caSAdrian Chadd if (dma->vaddr != NULL) {
943692ad7caSAdrian Chadd bus_dmamap_sync(dma->tag, dma->map,
944692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
945692ad7caSAdrian Chadd bus_dmamap_unload(dma->tag, dma->map);
946692ad7caSAdrian Chadd bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
947692ad7caSAdrian Chadd dma->vaddr = NULL;
948692ad7caSAdrian Chadd }
949692ad7caSAdrian Chadd if (dma->tag != NULL) {
950692ad7caSAdrian Chadd bus_dma_tag_destroy(dma->tag);
951692ad7caSAdrian Chadd dma->tag = NULL;
952692ad7caSAdrian Chadd }
953692ad7caSAdrian Chadd }
954692ad7caSAdrian Chadd
955692ad7caSAdrian Chadd /*
956692ad7caSAdrian Chadd * Allocate a shared page between host and NIC.
957692ad7caSAdrian Chadd */
958692ad7caSAdrian Chadd static int
wpi_alloc_shared(struct wpi_softc * sc)959692ad7caSAdrian Chadd wpi_alloc_shared(struct wpi_softc *sc)
960692ad7caSAdrian Chadd {
961692ad7caSAdrian Chadd /* Shared buffer must be aligned on a 4KB boundary. */
962692ad7caSAdrian Chadd return wpi_dma_contig_alloc(sc, &sc->shared_dma,
963692ad7caSAdrian Chadd (void **)&sc->shared, sizeof (struct wpi_shared), 4096);
964692ad7caSAdrian Chadd }
965692ad7caSAdrian Chadd
966692ad7caSAdrian Chadd static void
wpi_free_shared(struct wpi_softc * sc)967692ad7caSAdrian Chadd wpi_free_shared(struct wpi_softc *sc)
968692ad7caSAdrian Chadd {
969692ad7caSAdrian Chadd wpi_dma_contig_free(&sc->shared_dma);
970692ad7caSAdrian Chadd }
971692ad7caSAdrian Chadd
972692ad7caSAdrian Chadd /*
973692ad7caSAdrian Chadd * Allocate DMA-safe memory for firmware transfer.
974692ad7caSAdrian Chadd */
975692ad7caSAdrian Chadd static int
wpi_alloc_fwmem(struct wpi_softc * sc)976692ad7caSAdrian Chadd wpi_alloc_fwmem(struct wpi_softc *sc)
977692ad7caSAdrian Chadd {
978692ad7caSAdrian Chadd /* Must be aligned on a 16-byte boundary. */
979692ad7caSAdrian Chadd return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL,
980692ad7caSAdrian Chadd WPI_FW_TEXT_MAXSZ + WPI_FW_DATA_MAXSZ, 16);
981692ad7caSAdrian Chadd }
982692ad7caSAdrian Chadd
983692ad7caSAdrian Chadd static void
wpi_free_fwmem(struct wpi_softc * sc)984692ad7caSAdrian Chadd wpi_free_fwmem(struct wpi_softc *sc)
985692ad7caSAdrian Chadd {
986692ad7caSAdrian Chadd wpi_dma_contig_free(&sc->fw_dma);
987692ad7caSAdrian Chadd }
988692ad7caSAdrian Chadd
989692ad7caSAdrian Chadd static int
wpi_alloc_rx_ring(struct wpi_softc * sc)990692ad7caSAdrian Chadd wpi_alloc_rx_ring(struct wpi_softc *sc)
991692ad7caSAdrian Chadd {
992692ad7caSAdrian Chadd struct wpi_rx_ring *ring = &sc->rxq;
993692ad7caSAdrian Chadd bus_size_t size;
994692ad7caSAdrian Chadd int i, error;
995692ad7caSAdrian Chadd
996692ad7caSAdrian Chadd ring->cur = 0;
997692ad7caSAdrian Chadd ring->update = 0;
998692ad7caSAdrian Chadd
999692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1000692ad7caSAdrian Chadd
1001692ad7caSAdrian Chadd /* Allocate RX descriptors (16KB aligned.) */
1002692ad7caSAdrian Chadd size = WPI_RX_RING_COUNT * sizeof (uint32_t);
1003692ad7caSAdrian Chadd error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
1004692ad7caSAdrian Chadd (void **)&ring->desc, size, WPI_RING_DMA_ALIGN);
1005692ad7caSAdrian Chadd if (error != 0) {
1006692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1007692ad7caSAdrian Chadd "%s: could not allocate RX ring DMA memory, error %d\n",
1008692ad7caSAdrian Chadd __func__, error);
1009692ad7caSAdrian Chadd goto fail;
1010692ad7caSAdrian Chadd }
1011692ad7caSAdrian Chadd
1012692ad7caSAdrian Chadd /* Create RX buffer DMA tag. */
1013692ad7caSAdrian Chadd error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1014692ad7caSAdrian Chadd BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1015b020e15eSKevin Lo MJUMPAGESIZE, 1, MJUMPAGESIZE, 0, NULL, NULL, &ring->data_dmat);
1016692ad7caSAdrian Chadd if (error != 0) {
1017692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1018692ad7caSAdrian Chadd "%s: could not create RX buf DMA tag, error %d\n",
1019692ad7caSAdrian Chadd __func__, error);
1020692ad7caSAdrian Chadd goto fail;
1021692ad7caSAdrian Chadd }
1022692ad7caSAdrian Chadd
1023692ad7caSAdrian Chadd /*
1024692ad7caSAdrian Chadd * Allocate and map RX buffers.
1025692ad7caSAdrian Chadd */
1026692ad7caSAdrian Chadd for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1027692ad7caSAdrian Chadd struct wpi_rx_data *data = &ring->data[i];
1028692ad7caSAdrian Chadd bus_addr_t paddr;
1029692ad7caSAdrian Chadd
1030692ad7caSAdrian Chadd error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1031692ad7caSAdrian Chadd if (error != 0) {
1032692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1033692ad7caSAdrian Chadd "%s: could not create RX buf DMA map, error %d\n",
1034692ad7caSAdrian Chadd __func__, error);
1035692ad7caSAdrian Chadd goto fail;
1036692ad7caSAdrian Chadd }
1037692ad7caSAdrian Chadd
1038692ad7caSAdrian Chadd data->m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1039692ad7caSAdrian Chadd if (data->m == NULL) {
1040692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1041692ad7caSAdrian Chadd "%s: could not allocate RX mbuf\n", __func__);
1042692ad7caSAdrian Chadd error = ENOBUFS;
1043692ad7caSAdrian Chadd goto fail;
1044692ad7caSAdrian Chadd }
1045692ad7caSAdrian Chadd
1046692ad7caSAdrian Chadd error = bus_dmamap_load(ring->data_dmat, data->map,
1047692ad7caSAdrian Chadd mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1048692ad7caSAdrian Chadd &paddr, BUS_DMA_NOWAIT);
1049692ad7caSAdrian Chadd if (error != 0 && error != EFBIG) {
1050692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1051692ad7caSAdrian Chadd "%s: can't map mbuf (error %d)\n", __func__,
1052692ad7caSAdrian Chadd error);
1053692ad7caSAdrian Chadd goto fail;
1054692ad7caSAdrian Chadd }
1055692ad7caSAdrian Chadd
1056692ad7caSAdrian Chadd /* Set physical address of RX buffer. */
1057692ad7caSAdrian Chadd ring->desc[i] = htole32(paddr);
1058692ad7caSAdrian Chadd }
1059692ad7caSAdrian Chadd
1060692ad7caSAdrian Chadd bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1061692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
1062692ad7caSAdrian Chadd
1063692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1064692ad7caSAdrian Chadd
1065692ad7caSAdrian Chadd return 0;
1066692ad7caSAdrian Chadd
1067692ad7caSAdrian Chadd fail: wpi_free_rx_ring(sc);
1068692ad7caSAdrian Chadd
1069692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1070692ad7caSAdrian Chadd
1071692ad7caSAdrian Chadd return error;
1072692ad7caSAdrian Chadd }
1073692ad7caSAdrian Chadd
1074692ad7caSAdrian Chadd static void
wpi_update_rx_ring(struct wpi_softc * sc)1075692ad7caSAdrian Chadd wpi_update_rx_ring(struct wpi_softc *sc)
1076692ad7caSAdrian Chadd {
10775cacb17fSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_WPTR, sc->rxq.cur & ~7);
10785cacb17fSAdrian Chadd }
10795cacb17fSAdrian Chadd
10805cacb17fSAdrian Chadd static void
wpi_update_rx_ring_ps(struct wpi_softc * sc)10815cacb17fSAdrian Chadd wpi_update_rx_ring_ps(struct wpi_softc *sc)
10825cacb17fSAdrian Chadd {
1083692ad7caSAdrian Chadd struct wpi_rx_ring *ring = &sc->rxq;
1084692ad7caSAdrian Chadd
108501729242SAdrian Chadd if (ring->update != 0) {
108601729242SAdrian Chadd /* Wait for INT_WAKEUP event. */
108701729242SAdrian Chadd return;
108801729242SAdrian Chadd }
108901729242SAdrian Chadd
1090431ebc90SAdrian Chadd WPI_TXQ_LOCK(sc);
10915cacb17fSAdrian Chadd WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
10925cacb17fSAdrian Chadd if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1093692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s: wakeup request\n",
1094692ad7caSAdrian Chadd __func__);
1095692ad7caSAdrian Chadd ring->update = 1;
10965cacb17fSAdrian Chadd } else {
10975cacb17fSAdrian Chadd wpi_update_rx_ring(sc);
10985cacb17fSAdrian Chadd WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
10995cacb17fSAdrian Chadd }
1100431ebc90SAdrian Chadd WPI_TXQ_UNLOCK(sc);
1101692ad7caSAdrian Chadd }
1102692ad7caSAdrian Chadd
1103692ad7caSAdrian Chadd static void
wpi_reset_rx_ring(struct wpi_softc * sc)1104692ad7caSAdrian Chadd wpi_reset_rx_ring(struct wpi_softc *sc)
1105692ad7caSAdrian Chadd {
1106692ad7caSAdrian Chadd struct wpi_rx_ring *ring = &sc->rxq;
1107692ad7caSAdrian Chadd int ntries;
1108692ad7caSAdrian Chadd
1109692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1110692ad7caSAdrian Chadd
1111692ad7caSAdrian Chadd if (wpi_nic_lock(sc) == 0) {
1112692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_CONFIG, 0);
1113692ad7caSAdrian Chadd for (ntries = 0; ntries < 1000; ntries++) {
1114692ad7caSAdrian Chadd if (WPI_READ(sc, WPI_FH_RX_STATUS) &
1115692ad7caSAdrian Chadd WPI_FH_RX_STATUS_IDLE)
1116692ad7caSAdrian Chadd break;
1117692ad7caSAdrian Chadd DELAY(10);
1118692ad7caSAdrian Chadd }
1119692ad7caSAdrian Chadd wpi_nic_unlock(sc);
1120692ad7caSAdrian Chadd }
1121692ad7caSAdrian Chadd
1122692ad7caSAdrian Chadd ring->cur = 0;
1123692ad7caSAdrian Chadd ring->update = 0;
1124692ad7caSAdrian Chadd }
1125692ad7caSAdrian Chadd
1126692ad7caSAdrian Chadd static void
wpi_free_rx_ring(struct wpi_softc * sc)1127692ad7caSAdrian Chadd wpi_free_rx_ring(struct wpi_softc *sc)
1128692ad7caSAdrian Chadd {
1129692ad7caSAdrian Chadd struct wpi_rx_ring *ring = &sc->rxq;
1130692ad7caSAdrian Chadd int i;
1131692ad7caSAdrian Chadd
1132692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1133692ad7caSAdrian Chadd
1134692ad7caSAdrian Chadd wpi_dma_contig_free(&ring->desc_dma);
1135692ad7caSAdrian Chadd
1136692ad7caSAdrian Chadd for (i = 0; i < WPI_RX_RING_COUNT; i++) {
1137692ad7caSAdrian Chadd struct wpi_rx_data *data = &ring->data[i];
1138692ad7caSAdrian Chadd
1139692ad7caSAdrian Chadd if (data->m != NULL) {
1140692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map,
1141692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD);
1142692ad7caSAdrian Chadd bus_dmamap_unload(ring->data_dmat, data->map);
1143692ad7caSAdrian Chadd m_freem(data->m);
1144692ad7caSAdrian Chadd data->m = NULL;
1145692ad7caSAdrian Chadd }
1146692ad7caSAdrian Chadd if (data->map != NULL)
1147692ad7caSAdrian Chadd bus_dmamap_destroy(ring->data_dmat, data->map);
1148692ad7caSAdrian Chadd }
1149692ad7caSAdrian Chadd if (ring->data_dmat != NULL) {
1150692ad7caSAdrian Chadd bus_dma_tag_destroy(ring->data_dmat);
1151692ad7caSAdrian Chadd ring->data_dmat = NULL;
1152692ad7caSAdrian Chadd }
1153692ad7caSAdrian Chadd }
1154692ad7caSAdrian Chadd
1155692ad7caSAdrian Chadd static int
wpi_alloc_tx_ring(struct wpi_softc * sc,struct wpi_tx_ring * ring,uint8_t qid)1156525a3d47SAdrian Chadd wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, uint8_t qid)
1157692ad7caSAdrian Chadd {
1158692ad7caSAdrian Chadd bus_addr_t paddr;
1159692ad7caSAdrian Chadd bus_size_t size;
1160692ad7caSAdrian Chadd int i, error;
1161692ad7caSAdrian Chadd
1162692ad7caSAdrian Chadd ring->qid = qid;
1163692ad7caSAdrian Chadd ring->queued = 0;
1164692ad7caSAdrian Chadd ring->cur = 0;
1165940aa808SAdrian Chadd ring->pending = 0;
1166692ad7caSAdrian Chadd ring->update = 0;
1167692ad7caSAdrian Chadd
1168692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1169692ad7caSAdrian Chadd
1170692ad7caSAdrian Chadd /* Allocate TX descriptors (16KB aligned.) */
1171692ad7caSAdrian Chadd size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_desc);
1172692ad7caSAdrian Chadd error = wpi_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc,
1173692ad7caSAdrian Chadd size, WPI_RING_DMA_ALIGN);
1174692ad7caSAdrian Chadd if (error != 0) {
1175692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1176692ad7caSAdrian Chadd "%s: could not allocate TX ring DMA memory, error %d\n",
1177692ad7caSAdrian Chadd __func__, error);
1178692ad7caSAdrian Chadd goto fail;
1179692ad7caSAdrian Chadd }
1180692ad7caSAdrian Chadd
1181692ad7caSAdrian Chadd /* Update shared area with ring physical address. */
1182692ad7caSAdrian Chadd sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
1183692ad7caSAdrian Chadd bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
1184692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
1185692ad7caSAdrian Chadd
1186692ad7caSAdrian Chadd size = WPI_TX_RING_COUNT * sizeof (struct wpi_tx_cmd);
1187692ad7caSAdrian Chadd error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
1188692ad7caSAdrian Chadd size, 4);
1189692ad7caSAdrian Chadd if (error != 0) {
1190692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1191692ad7caSAdrian Chadd "%s: could not allocate TX cmd DMA memory, error %d\n",
1192692ad7caSAdrian Chadd __func__, error);
1193692ad7caSAdrian Chadd goto fail;
1194692ad7caSAdrian Chadd }
1195692ad7caSAdrian Chadd
1196692ad7caSAdrian Chadd error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1197692ad7caSAdrian Chadd BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
1198b020e15eSKevin Lo WPI_MAX_SCATTER - 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
1199692ad7caSAdrian Chadd if (error != 0) {
1200692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1201692ad7caSAdrian Chadd "%s: could not create TX buf DMA tag, error %d\n",
1202692ad7caSAdrian Chadd __func__, error);
1203692ad7caSAdrian Chadd goto fail;
1204692ad7caSAdrian Chadd }
1205692ad7caSAdrian Chadd
1206692ad7caSAdrian Chadd paddr = ring->cmd_dma.paddr;
1207692ad7caSAdrian Chadd for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1208692ad7caSAdrian Chadd struct wpi_tx_data *data = &ring->data[i];
1209692ad7caSAdrian Chadd
1210692ad7caSAdrian Chadd data->cmd_paddr = paddr;
1211692ad7caSAdrian Chadd paddr += sizeof (struct wpi_tx_cmd);
1212692ad7caSAdrian Chadd
1213692ad7caSAdrian Chadd error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1214692ad7caSAdrian Chadd if (error != 0) {
1215692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1216692ad7caSAdrian Chadd "%s: could not create TX buf DMA map, error %d\n",
1217692ad7caSAdrian Chadd __func__, error);
1218692ad7caSAdrian Chadd goto fail;
1219692ad7caSAdrian Chadd }
1220692ad7caSAdrian Chadd }
1221692ad7caSAdrian Chadd
1222692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1223692ad7caSAdrian Chadd
1224692ad7caSAdrian Chadd return 0;
1225692ad7caSAdrian Chadd
1226692ad7caSAdrian Chadd fail: wpi_free_tx_ring(sc, ring);
1227692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1228692ad7caSAdrian Chadd return error;
1229692ad7caSAdrian Chadd }
1230692ad7caSAdrian Chadd
1231692ad7caSAdrian Chadd static void
wpi_update_tx_ring(struct wpi_softc * sc,struct wpi_tx_ring * ring)1232692ad7caSAdrian Chadd wpi_update_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1233692ad7caSAdrian Chadd {
12345cacb17fSAdrian Chadd WPI_WRITE(sc, WPI_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
12355cacb17fSAdrian Chadd }
12365cacb17fSAdrian Chadd
12375cacb17fSAdrian Chadd static void
wpi_update_tx_ring_ps(struct wpi_softc * sc,struct wpi_tx_ring * ring)12385cacb17fSAdrian Chadd wpi_update_tx_ring_ps(struct wpi_softc *sc, struct wpi_tx_ring *ring)
12395cacb17fSAdrian Chadd {
12405cacb17fSAdrian Chadd
124101729242SAdrian Chadd if (ring->update != 0) {
124201729242SAdrian Chadd /* Wait for INT_WAKEUP event. */
124301729242SAdrian Chadd return;
124401729242SAdrian Chadd }
124501729242SAdrian Chadd
12465cacb17fSAdrian Chadd WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
12475cacb17fSAdrian Chadd if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_SLEEP) {
1248692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_PWRSAVE, "%s (%d): requesting wakeup\n",
1249692ad7caSAdrian Chadd __func__, ring->qid);
1250692ad7caSAdrian Chadd ring->update = 1;
12515cacb17fSAdrian Chadd } else {
12525cacb17fSAdrian Chadd wpi_update_tx_ring(sc, ring);
12535cacb17fSAdrian Chadd WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
12545cacb17fSAdrian Chadd }
1255692ad7caSAdrian Chadd }
1256692ad7caSAdrian Chadd
1257692ad7caSAdrian Chadd static void
wpi_reset_tx_ring(struct wpi_softc * sc,struct wpi_tx_ring * ring)1258692ad7caSAdrian Chadd wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1259692ad7caSAdrian Chadd {
1260692ad7caSAdrian Chadd int i;
1261692ad7caSAdrian Chadd
1262692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1263692ad7caSAdrian Chadd
1264692ad7caSAdrian Chadd for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1265692ad7caSAdrian Chadd struct wpi_tx_data *data = &ring->data[i];
1266692ad7caSAdrian Chadd
1267692ad7caSAdrian Chadd if (data->m != NULL) {
1268692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map,
1269692ad7caSAdrian Chadd BUS_DMASYNC_POSTWRITE);
1270692ad7caSAdrian Chadd bus_dmamap_unload(ring->data_dmat, data->map);
1271692ad7caSAdrian Chadd m_freem(data->m);
1272692ad7caSAdrian Chadd data->m = NULL;
1273692ad7caSAdrian Chadd }
1274978d805fSAdrian Chadd if (data->ni != NULL) {
1275978d805fSAdrian Chadd ieee80211_free_node(data->ni);
1276978d805fSAdrian Chadd data->ni = NULL;
1277978d805fSAdrian Chadd }
1278692ad7caSAdrian Chadd }
1279692ad7caSAdrian Chadd /* Clear TX descriptors. */
1280692ad7caSAdrian Chadd memset(ring->desc, 0, ring->desc_dma.size);
1281692ad7caSAdrian Chadd bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1282692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
1283692ad7caSAdrian Chadd ring->queued = 0;
1284692ad7caSAdrian Chadd ring->cur = 0;
1285940aa808SAdrian Chadd ring->pending = 0;
1286692ad7caSAdrian Chadd ring->update = 0;
1287692ad7caSAdrian Chadd }
1288692ad7caSAdrian Chadd
1289692ad7caSAdrian Chadd static void
wpi_free_tx_ring(struct wpi_softc * sc,struct wpi_tx_ring * ring)1290692ad7caSAdrian Chadd wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
1291692ad7caSAdrian Chadd {
1292692ad7caSAdrian Chadd int i;
1293692ad7caSAdrian Chadd
1294692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
1295692ad7caSAdrian Chadd
1296692ad7caSAdrian Chadd wpi_dma_contig_free(&ring->desc_dma);
1297692ad7caSAdrian Chadd wpi_dma_contig_free(&ring->cmd_dma);
1298692ad7caSAdrian Chadd
1299692ad7caSAdrian Chadd for (i = 0; i < WPI_TX_RING_COUNT; i++) {
1300692ad7caSAdrian Chadd struct wpi_tx_data *data = &ring->data[i];
1301692ad7caSAdrian Chadd
1302692ad7caSAdrian Chadd if (data->m != NULL) {
1303692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map,
1304692ad7caSAdrian Chadd BUS_DMASYNC_POSTWRITE);
1305692ad7caSAdrian Chadd bus_dmamap_unload(ring->data_dmat, data->map);
1306692ad7caSAdrian Chadd m_freem(data->m);
1307692ad7caSAdrian Chadd }
1308692ad7caSAdrian Chadd if (data->map != NULL)
1309692ad7caSAdrian Chadd bus_dmamap_destroy(ring->data_dmat, data->map);
1310692ad7caSAdrian Chadd }
1311692ad7caSAdrian Chadd if (ring->data_dmat != NULL) {
1312692ad7caSAdrian Chadd bus_dma_tag_destroy(ring->data_dmat);
1313692ad7caSAdrian Chadd ring->data_dmat = NULL;
1314692ad7caSAdrian Chadd }
1315692ad7caSAdrian Chadd }
1316692ad7caSAdrian Chadd
1317692ad7caSAdrian Chadd /*
1318692ad7caSAdrian Chadd * Extract various information from EEPROM.
1319692ad7caSAdrian Chadd */
1320692ad7caSAdrian Chadd static int
wpi_read_eeprom(struct wpi_softc * sc,uint8_t macaddr[IEEE80211_ADDR_LEN])1321692ad7caSAdrian Chadd wpi_read_eeprom(struct wpi_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1322692ad7caSAdrian Chadd {
1323692ad7caSAdrian Chadd #define WPI_CHK(res) do { \
1324692ad7caSAdrian Chadd if ((error = res) != 0) \
1325692ad7caSAdrian Chadd goto fail; \
1326692ad7caSAdrian Chadd } while (0)
1327525a3d47SAdrian Chadd uint8_t i;
1328525a3d47SAdrian Chadd int error;
1329692ad7caSAdrian Chadd
1330692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1331692ad7caSAdrian Chadd
1332692ad7caSAdrian Chadd /* Adapter has to be powered on for EEPROM access to work. */
1333692ad7caSAdrian Chadd if ((error = wpi_apm_init(sc)) != 0) {
1334692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1335692ad7caSAdrian Chadd "%s: could not power ON adapter, error %d\n", __func__,
1336692ad7caSAdrian Chadd error);
1337692ad7caSAdrian Chadd return error;
1338692ad7caSAdrian Chadd }
1339692ad7caSAdrian Chadd
1340692ad7caSAdrian Chadd if ((WPI_READ(sc, WPI_EEPROM_GP) & 0x6) == 0) {
1341692ad7caSAdrian Chadd device_printf(sc->sc_dev, "bad EEPROM signature\n");
1342692ad7caSAdrian Chadd error = EIO;
1343692ad7caSAdrian Chadd goto fail;
1344692ad7caSAdrian Chadd }
1345692ad7caSAdrian Chadd /* Clear HW ownership of EEPROM. */
1346692ad7caSAdrian Chadd WPI_CLRBITS(sc, WPI_EEPROM_GP, WPI_EEPROM_GP_IF_OWNER);
1347692ad7caSAdrian Chadd
1348692ad7caSAdrian Chadd /* Read the hardware capabilities, revision and SKU type. */
1349692ad7caSAdrian Chadd WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_SKU_CAP, &sc->cap,
1350692ad7caSAdrian Chadd sizeof(sc->cap)));
1351692ad7caSAdrian Chadd WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,
1352692ad7caSAdrian Chadd sizeof(sc->rev)));
1353692ad7caSAdrian Chadd WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type,
1354692ad7caSAdrian Chadd sizeof(sc->type)));
1355692ad7caSAdrian Chadd
1356f736d898SAdrian Chadd sc->rev = le16toh(sc->rev);
13577167f5fcSAdrian Chadd DPRINTF(sc, WPI_DEBUG_EEPROM, "cap=%x rev=%x type=%x\n", sc->cap,
1358f736d898SAdrian Chadd sc->rev, sc->type);
1359692ad7caSAdrian Chadd
1360692ad7caSAdrian Chadd /* Read the regulatory domain (4 ASCII characters.) */
1361692ad7caSAdrian Chadd WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain,
1362692ad7caSAdrian Chadd sizeof(sc->domain)));
1363692ad7caSAdrian Chadd
1364692ad7caSAdrian Chadd /* Read MAC address. */
1365692ad7caSAdrian Chadd WPI_CHK(wpi_read_prom_data(sc, WPI_EEPROM_MAC, macaddr,
1366692ad7caSAdrian Chadd IEEE80211_ADDR_LEN));
1367692ad7caSAdrian Chadd
1368692ad7caSAdrian Chadd /* Read the list of authorized channels. */
1369692ad7caSAdrian Chadd for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++)
1370692ad7caSAdrian Chadd WPI_CHK(wpi_read_eeprom_channels(sc, i));
1371692ad7caSAdrian Chadd
1372692ad7caSAdrian Chadd /* Read the list of TX power groups. */
1373692ad7caSAdrian Chadd for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++)
1374692ad7caSAdrian Chadd WPI_CHK(wpi_read_eeprom_group(sc, i));
1375692ad7caSAdrian Chadd
1376692ad7caSAdrian Chadd fail: wpi_apm_stop(sc); /* Power OFF adapter. */
1377692ad7caSAdrian Chadd
1378692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
1379692ad7caSAdrian Chadd __func__);
1380692ad7caSAdrian Chadd
1381692ad7caSAdrian Chadd return error;
1382692ad7caSAdrian Chadd #undef WPI_CHK
1383692ad7caSAdrian Chadd }
1384692ad7caSAdrian Chadd
1385692ad7caSAdrian Chadd /*
1386692ad7caSAdrian Chadd * Translate EEPROM flags to net80211.
1387692ad7caSAdrian Chadd */
1388692ad7caSAdrian Chadd static uint32_t
wpi_eeprom_channel_flags(struct wpi_eeprom_chan * channel)1389692ad7caSAdrian Chadd wpi_eeprom_channel_flags(struct wpi_eeprom_chan *channel)
1390692ad7caSAdrian Chadd {
1391692ad7caSAdrian Chadd uint32_t nflags;
1392692ad7caSAdrian Chadd
1393692ad7caSAdrian Chadd nflags = 0;
1394692ad7caSAdrian Chadd if ((channel->flags & WPI_EEPROM_CHAN_ACTIVE) == 0)
1395692ad7caSAdrian Chadd nflags |= IEEE80211_CHAN_PASSIVE;
1396692ad7caSAdrian Chadd if ((channel->flags & WPI_EEPROM_CHAN_IBSS) == 0)
1397692ad7caSAdrian Chadd nflags |= IEEE80211_CHAN_NOADHOC;
1398692ad7caSAdrian Chadd if (channel->flags & WPI_EEPROM_CHAN_RADAR) {
1399692ad7caSAdrian Chadd nflags |= IEEE80211_CHAN_DFS;
1400692ad7caSAdrian Chadd /* XXX apparently IBSS may still be marked */
1401692ad7caSAdrian Chadd nflags |= IEEE80211_CHAN_NOADHOC;
1402692ad7caSAdrian Chadd }
1403692ad7caSAdrian Chadd
1404726ddc26SAdrian Chadd /* XXX HOSTAP uses WPI_MODE_IBSS */
1405726ddc26SAdrian Chadd if (nflags & IEEE80211_CHAN_NOADHOC)
1406726ddc26SAdrian Chadd nflags |= IEEE80211_CHAN_NOHOSTAP;
1407726ddc26SAdrian Chadd
1408692ad7caSAdrian Chadd return nflags;
1409692ad7caSAdrian Chadd }
1410692ad7caSAdrian Chadd
1411692ad7caSAdrian Chadd static void
wpi_read_eeprom_band(struct wpi_softc * sc,uint8_t n,int maxchans,int * nchans,struct ieee80211_channel chans[])14121c6b43dfSAndriy Voskoboinyk wpi_read_eeprom_band(struct wpi_softc *sc, uint8_t n, int maxchans,
14131c6b43dfSAndriy Voskoboinyk int *nchans, struct ieee80211_channel chans[])
1414692ad7caSAdrian Chadd {
1415692ad7caSAdrian Chadd struct wpi_eeprom_chan *channels = sc->eeprom_channels[n];
1416692ad7caSAdrian Chadd const struct wpi_chan_band *band = &wpi_bands[n];
1417525a3d47SAdrian Chadd uint32_t nflags;
14185763d9dbSAndriy Voskoboinyk uint8_t bands[IEEE80211_MODE_BYTES];
1419525a3d47SAdrian Chadd uint8_t chan, i;
14205763d9dbSAndriy Voskoboinyk int error;
14215763d9dbSAndriy Voskoboinyk
14225763d9dbSAndriy Voskoboinyk memset(bands, 0, sizeof(bands));
14235763d9dbSAndriy Voskoboinyk
14245763d9dbSAndriy Voskoboinyk if (n == 0) {
14255763d9dbSAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11B);
14265763d9dbSAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11G);
14275763d9dbSAndriy Voskoboinyk } else
14285763d9dbSAndriy Voskoboinyk setbit(bands, IEEE80211_MODE_11A);
1429692ad7caSAdrian Chadd
1430692ad7caSAdrian Chadd for (i = 0; i < band->nchan; i++) {
1431692ad7caSAdrian Chadd if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
14327e0d5232SAdrian Chadd DPRINTF(sc, WPI_DEBUG_EEPROM,
1433692ad7caSAdrian Chadd "Channel Not Valid: %d, band %d\n",
1434692ad7caSAdrian Chadd band->chan[i],n);
1435692ad7caSAdrian Chadd continue;
1436692ad7caSAdrian Chadd }
1437692ad7caSAdrian Chadd
1438692ad7caSAdrian Chadd chan = band->chan[i];
1439692ad7caSAdrian Chadd nflags = wpi_eeprom_channel_flags(&channels[i]);
14405763d9dbSAndriy Voskoboinyk error = ieee80211_add_channel(chans, maxchans, nchans,
14415763d9dbSAndriy Voskoboinyk chan, 0, channels[i].maxpwr, nflags, bands);
14425763d9dbSAndriy Voskoboinyk if (error != 0)
14431c6b43dfSAndriy Voskoboinyk break;
14441c6b43dfSAndriy Voskoboinyk
1445692ad7caSAdrian Chadd /* Save maximum allowed TX power for this channel. */
1446692ad7caSAdrian Chadd sc->maxpwr[chan] = channels[i].maxpwr;
1447692ad7caSAdrian Chadd
1448692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_EEPROM,
14495763d9dbSAndriy Voskoboinyk "adding chan %d flags=0x%x maxpwr=%d, offset %d\n",
14505763d9dbSAndriy Voskoboinyk chan, channels[i].flags, sc->maxpwr[chan], *nchans);
1451692ad7caSAdrian Chadd }
1452692ad7caSAdrian Chadd }
1453692ad7caSAdrian Chadd
1454692ad7caSAdrian Chadd /**
1455692ad7caSAdrian Chadd * Read the eeprom to find out what channels are valid for the given
1456692ad7caSAdrian Chadd * band and update net80211 with what we find.
1457692ad7caSAdrian Chadd */
1458692ad7caSAdrian Chadd static int
wpi_read_eeprom_channels(struct wpi_softc * sc,uint8_t n)1459525a3d47SAdrian Chadd wpi_read_eeprom_channels(struct wpi_softc *sc, uint8_t n)
1460692ad7caSAdrian Chadd {
14617a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
1462692ad7caSAdrian Chadd const struct wpi_chan_band *band = &wpi_bands[n];
1463692ad7caSAdrian Chadd int error;
1464692ad7caSAdrian Chadd
1465692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1466692ad7caSAdrian Chadd
1467692ad7caSAdrian Chadd error = wpi_read_prom_data(sc, band->addr, &sc->eeprom_channels[n],
1468692ad7caSAdrian Chadd band->nchan * sizeof (struct wpi_eeprom_chan));
1469692ad7caSAdrian Chadd if (error != 0) {
1470692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1471692ad7caSAdrian Chadd return error;
1472692ad7caSAdrian Chadd }
1473692ad7caSAdrian Chadd
14741c6b43dfSAndriy Voskoboinyk wpi_read_eeprom_band(sc, n, IEEE80211_CHAN_MAX, &ic->ic_nchans,
14751c6b43dfSAndriy Voskoboinyk ic->ic_channels);
1476692ad7caSAdrian Chadd
1477692ad7caSAdrian Chadd ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1478692ad7caSAdrian Chadd
1479692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1480692ad7caSAdrian Chadd
1481692ad7caSAdrian Chadd return 0;
1482692ad7caSAdrian Chadd }
1483692ad7caSAdrian Chadd
1484692ad7caSAdrian Chadd static struct wpi_eeprom_chan *
wpi_find_eeprom_channel(struct wpi_softc * sc,struct ieee80211_channel * c)1485692ad7caSAdrian Chadd wpi_find_eeprom_channel(struct wpi_softc *sc, struct ieee80211_channel *c)
1486692ad7caSAdrian Chadd {
1487692ad7caSAdrian Chadd int i, j;
1488692ad7caSAdrian Chadd
1489692ad7caSAdrian Chadd for (j = 0; j < WPI_CHAN_BANDS_COUNT; j++)
1490692ad7caSAdrian Chadd for (i = 0; i < wpi_bands[j].nchan; i++)
149186d9f8fcSAndriy Voskoboinyk if (wpi_bands[j].chan[i] == c->ic_ieee &&
149286d9f8fcSAndriy Voskoboinyk ((j == 0) ^ IEEE80211_IS_CHAN_A(c)) == 1)
1493692ad7caSAdrian Chadd return &sc->eeprom_channels[j][i];
1494692ad7caSAdrian Chadd
1495692ad7caSAdrian Chadd return NULL;
1496692ad7caSAdrian Chadd }
1497692ad7caSAdrian Chadd
14981c6b43dfSAndriy Voskoboinyk static void
wpi_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])14991c6b43dfSAndriy Voskoboinyk wpi_getradiocaps(struct ieee80211com *ic,
15001c6b43dfSAndriy Voskoboinyk int maxchans, int *nchans, struct ieee80211_channel chans[])
15011c6b43dfSAndriy Voskoboinyk {
15021c6b43dfSAndriy Voskoboinyk struct wpi_softc *sc = ic->ic_softc;
15031c6b43dfSAndriy Voskoboinyk int i;
15041c6b43dfSAndriy Voskoboinyk
15051c6b43dfSAndriy Voskoboinyk /* Parse the list of authorized channels. */
15061c6b43dfSAndriy Voskoboinyk for (i = 0; i < WPI_CHAN_BANDS_COUNT && *nchans < maxchans; i++)
15071c6b43dfSAndriy Voskoboinyk wpi_read_eeprom_band(sc, i, maxchans, nchans, chans);
15081c6b43dfSAndriy Voskoboinyk }
15091c6b43dfSAndriy Voskoboinyk
1510692ad7caSAdrian Chadd /*
1511692ad7caSAdrian Chadd * Enforce flags read from EEPROM.
1512692ad7caSAdrian Chadd */
1513692ad7caSAdrian Chadd static int
wpi_setregdomain(struct ieee80211com * ic,struct ieee80211_regdomain * rd,int nchan,struct ieee80211_channel chans[])1514692ad7caSAdrian Chadd wpi_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
1515692ad7caSAdrian Chadd int nchan, struct ieee80211_channel chans[])
1516692ad7caSAdrian Chadd {
15172cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
1518692ad7caSAdrian Chadd int i;
1519692ad7caSAdrian Chadd
1520692ad7caSAdrian Chadd for (i = 0; i < nchan; i++) {
1521692ad7caSAdrian Chadd struct ieee80211_channel *c = &chans[i];
1522692ad7caSAdrian Chadd struct wpi_eeprom_chan *channel;
1523692ad7caSAdrian Chadd
1524692ad7caSAdrian Chadd channel = wpi_find_eeprom_channel(sc, c);
1525692ad7caSAdrian Chadd if (channel == NULL) {
15267a79cebfSGleb Smirnoff ic_printf(ic, "%s: invalid channel %u freq %u/0x%x\n",
1527692ad7caSAdrian Chadd __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
1528692ad7caSAdrian Chadd return EINVAL;
1529692ad7caSAdrian Chadd }
1530692ad7caSAdrian Chadd c->ic_flags |= wpi_eeprom_channel_flags(channel);
1531692ad7caSAdrian Chadd }
1532692ad7caSAdrian Chadd
1533692ad7caSAdrian Chadd return 0;
1534692ad7caSAdrian Chadd }
1535692ad7caSAdrian Chadd
1536692ad7caSAdrian Chadd static int
wpi_read_eeprom_group(struct wpi_softc * sc,uint8_t n)1537525a3d47SAdrian Chadd wpi_read_eeprom_group(struct wpi_softc *sc, uint8_t n)
1538692ad7caSAdrian Chadd {
1539692ad7caSAdrian Chadd struct wpi_power_group *group = &sc->groups[n];
1540692ad7caSAdrian Chadd struct wpi_eeprom_group rgroup;
1541692ad7caSAdrian Chadd int i, error;
1542692ad7caSAdrian Chadd
1543692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1544692ad7caSAdrian Chadd
1545692ad7caSAdrian Chadd if ((error = wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32,
1546692ad7caSAdrian Chadd &rgroup, sizeof rgroup)) != 0) {
1547692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1548692ad7caSAdrian Chadd return error;
1549692ad7caSAdrian Chadd }
1550692ad7caSAdrian Chadd
1551692ad7caSAdrian Chadd /* Save TX power group information. */
1552692ad7caSAdrian Chadd group->chan = rgroup.chan;
1553692ad7caSAdrian Chadd group->maxpwr = rgroup.maxpwr;
1554692ad7caSAdrian Chadd /* Retrieve temperature at which the samples were taken. */
1555692ad7caSAdrian Chadd group->temp = (int16_t)le16toh(rgroup.temp);
1556692ad7caSAdrian Chadd
1557692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_EEPROM,
1558692ad7caSAdrian Chadd "power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan,
1559692ad7caSAdrian Chadd group->maxpwr, group->temp);
1560692ad7caSAdrian Chadd
1561692ad7caSAdrian Chadd for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
1562692ad7caSAdrian Chadd group->samples[i].index = rgroup.samples[i].index;
1563692ad7caSAdrian Chadd group->samples[i].power = rgroup.samples[i].power;
1564692ad7caSAdrian Chadd
1565692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_EEPROM,
1566692ad7caSAdrian Chadd "\tsample %d: index=%d power=%d\n", i,
1567692ad7caSAdrian Chadd group->samples[i].index, group->samples[i].power);
1568692ad7caSAdrian Chadd }
1569692ad7caSAdrian Chadd
1570692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1571692ad7caSAdrian Chadd
1572692ad7caSAdrian Chadd return 0;
1573692ad7caSAdrian Chadd }
1574692ad7caSAdrian Chadd
1575525a3d47SAdrian Chadd static __inline uint8_t
wpi_add_node_entry_adhoc(struct wpi_softc * sc)1576eca2eb39SAdrian Chadd wpi_add_node_entry_adhoc(struct wpi_softc *sc)
1577eca2eb39SAdrian Chadd {
1578525a3d47SAdrian Chadd uint8_t newid = WPI_ID_IBSS_MIN;
1579eca2eb39SAdrian Chadd
1580eca2eb39SAdrian Chadd for (; newid <= WPI_ID_IBSS_MAX; newid++) {
1581eca2eb39SAdrian Chadd if ((sc->nodesmsk & (1 << newid)) == 0) {
1582eca2eb39SAdrian Chadd sc->nodesmsk |= 1 << newid;
1583eca2eb39SAdrian Chadd return newid;
1584eca2eb39SAdrian Chadd }
1585eca2eb39SAdrian Chadd }
1586eca2eb39SAdrian Chadd
1587eca2eb39SAdrian Chadd return WPI_ID_UNDEFINED;
1588eca2eb39SAdrian Chadd }
1589eca2eb39SAdrian Chadd
1590525a3d47SAdrian Chadd static __inline uint8_t
wpi_add_node_entry_sta(struct wpi_softc * sc)15910ebca245SAdrian Chadd wpi_add_node_entry_sta(struct wpi_softc *sc)
15920ebca245SAdrian Chadd {
15930ebca245SAdrian Chadd sc->nodesmsk |= 1 << WPI_ID_BSS;
15940ebca245SAdrian Chadd
15950ebca245SAdrian Chadd return WPI_ID_BSS;
15960ebca245SAdrian Chadd }
15970ebca245SAdrian Chadd
15980ebca245SAdrian Chadd static __inline int
wpi_check_node_entry(struct wpi_softc * sc,uint8_t id)1599eca2eb39SAdrian Chadd wpi_check_node_entry(struct wpi_softc *sc, uint8_t id)
1600eca2eb39SAdrian Chadd {
1601eca2eb39SAdrian Chadd if (id == WPI_ID_UNDEFINED)
1602eca2eb39SAdrian Chadd return 0;
1603eca2eb39SAdrian Chadd
1604eca2eb39SAdrian Chadd return (sc->nodesmsk >> id) & 1;
1605eca2eb39SAdrian Chadd }
1606eca2eb39SAdrian Chadd
1607eca2eb39SAdrian Chadd static __inline void
wpi_clear_node_table(struct wpi_softc * sc)1608eca2eb39SAdrian Chadd wpi_clear_node_table(struct wpi_softc *sc)
1609eca2eb39SAdrian Chadd {
1610eca2eb39SAdrian Chadd sc->nodesmsk = 0;
1611eca2eb39SAdrian Chadd }
1612eca2eb39SAdrian Chadd
1613eca2eb39SAdrian Chadd static __inline void
wpi_del_node_entry(struct wpi_softc * sc,uint8_t id)1614eca2eb39SAdrian Chadd wpi_del_node_entry(struct wpi_softc *sc, uint8_t id)
1615eca2eb39SAdrian Chadd {
1616eca2eb39SAdrian Chadd sc->nodesmsk &= ~(1 << id);
1617eca2eb39SAdrian Chadd }
1618eca2eb39SAdrian Chadd
1619692ad7caSAdrian Chadd static struct ieee80211_node *
wpi_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])1620692ad7caSAdrian Chadd wpi_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1621692ad7caSAdrian Chadd {
1622692ad7caSAdrian Chadd struct wpi_node *wn;
1623692ad7caSAdrian Chadd
1624692ad7caSAdrian Chadd wn = malloc(sizeof (struct wpi_node), M_80211_NODE,
1625692ad7caSAdrian Chadd M_NOWAIT | M_ZERO);
1626692ad7caSAdrian Chadd
1627692ad7caSAdrian Chadd if (wn == NULL)
1628692ad7caSAdrian Chadd return NULL;
1629692ad7caSAdrian Chadd
1630692ad7caSAdrian Chadd wn->id = WPI_ID_UNDEFINED;
1631692ad7caSAdrian Chadd
1632692ad7caSAdrian Chadd return &wn->ni;
1633692ad7caSAdrian Chadd }
1634692ad7caSAdrian Chadd
1635692ad7caSAdrian Chadd static void
wpi_node_free(struct ieee80211_node * ni)1636692ad7caSAdrian Chadd wpi_node_free(struct ieee80211_node *ni)
1637692ad7caSAdrian Chadd {
16387a79cebfSGleb Smirnoff struct wpi_softc *sc = ni->ni_ic->ic_softc;
1639a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
1640692ad7caSAdrian Chadd
16410ebca245SAdrian Chadd if (wn->id != WPI_ID_UNDEFINED) {
16427964dd56SAdrian Chadd WPI_NT_LOCK(sc);
1643eca2eb39SAdrian Chadd if (wpi_check_node_entry(sc, wn->id)) {
1644eca2eb39SAdrian Chadd wpi_del_node_entry(sc, wn->id);
1645692ad7caSAdrian Chadd wpi_del_node(sc, ni);
1646eca2eb39SAdrian Chadd }
16477964dd56SAdrian Chadd WPI_NT_UNLOCK(sc);
1648692ad7caSAdrian Chadd }
1649692ad7caSAdrian Chadd
1650692ad7caSAdrian Chadd sc->sc_node_free(ni);
1651692ad7caSAdrian Chadd }
1652692ad7caSAdrian Chadd
1653621f1a75SAdrian Chadd static __inline int
wpi_check_bss_filter(struct wpi_softc * sc)1654621f1a75SAdrian Chadd wpi_check_bss_filter(struct wpi_softc *sc)
1655621f1a75SAdrian Chadd {
1656621f1a75SAdrian Chadd return (sc->rxon.filter & htole32(WPI_FILTER_BSS)) != 0;
1657621f1a75SAdrian Chadd }
1658621f1a75SAdrian Chadd
1659a20c9affSAdrian Chadd static void
wpi_ibss_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)166057c61f5dSAdrian Chadd wpi_ibss_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
1661b66af287SAdrian Chadd const struct ieee80211_rx_stats *rxs,
1662b66af287SAdrian Chadd int rssi, int nf)
1663a20c9affSAdrian Chadd {
1664a20c9affSAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
16652cb71df1SAdrian Chadd struct wpi_softc *sc = vap->iv_ic->ic_softc;
1666a20c9affSAdrian Chadd struct wpi_vap *wvp = WPI_VAP(vap);
1667a20c9affSAdrian Chadd uint64_t ni_tstamp, rx_tstamp;
1668a20c9affSAdrian Chadd
1669b66af287SAdrian Chadd wvp->wv_recv_mgmt(ni, m, subtype, rxs, rssi, nf);
1670a20c9affSAdrian Chadd
167157c61f5dSAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN &&
1672a20c9affSAdrian Chadd (subtype == IEEE80211_FC0_SUBTYPE_BEACON ||
1673a20c9affSAdrian Chadd subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)) {
1674a20c9affSAdrian Chadd ni_tstamp = le64toh(ni->ni_tstamp.tsf);
1675a20c9affSAdrian Chadd rx_tstamp = le64toh(sc->rx_tstamp);
1676a20c9affSAdrian Chadd
1677a20c9affSAdrian Chadd if (ni_tstamp >= rx_tstamp) {
1678a20c9affSAdrian Chadd DPRINTF(sc, WPI_DEBUG_STATE,
1679a20c9affSAdrian Chadd "ibss merge, tsf %ju tstamp %ju\n",
1680a20c9affSAdrian Chadd (uintmax_t)rx_tstamp, (uintmax_t)ni_tstamp);
1681a20c9affSAdrian Chadd (void) ieee80211_ibss_merge(ni);
1682a20c9affSAdrian Chadd }
1683a20c9affSAdrian Chadd }
1684a20c9affSAdrian Chadd }
1685a20c9affSAdrian Chadd
1686a20c9affSAdrian Chadd static void
wpi_restore_node(void * arg,struct ieee80211_node * ni)1687a20c9affSAdrian Chadd wpi_restore_node(void *arg, struct ieee80211_node *ni)
1688a20c9affSAdrian Chadd {
1689a20c9affSAdrian Chadd struct wpi_softc *sc = arg;
1690a20c9affSAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
1691a20c9affSAdrian Chadd int error;
1692a20c9affSAdrian Chadd
1693a20c9affSAdrian Chadd WPI_NT_LOCK(sc);
1694a20c9affSAdrian Chadd if (wn->id != WPI_ID_UNDEFINED) {
1695a20c9affSAdrian Chadd wn->id = WPI_ID_UNDEFINED;
1696a20c9affSAdrian Chadd if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
1697a20c9affSAdrian Chadd device_printf(sc->sc_dev,
1698a20c9affSAdrian Chadd "%s: could not add IBSS node, error %d\n",
1699a20c9affSAdrian Chadd __func__, error);
1700a20c9affSAdrian Chadd }
1701a20c9affSAdrian Chadd }
1702a20c9affSAdrian Chadd WPI_NT_UNLOCK(sc);
1703a20c9affSAdrian Chadd }
1704a20c9affSAdrian Chadd
1705a20c9affSAdrian Chadd static void
wpi_restore_node_table(struct wpi_softc * sc,struct wpi_vap * wvp)1706a20c9affSAdrian Chadd wpi_restore_node_table(struct wpi_softc *sc, struct wpi_vap *wvp)
1707a20c9affSAdrian Chadd {
17087a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
1709a20c9affSAdrian Chadd
1710a20c9affSAdrian Chadd /* Set group keys once. */
1711a20c9affSAdrian Chadd WPI_NT_LOCK(sc);
1712a20c9affSAdrian Chadd wvp->wv_gtk = 0;
1713a20c9affSAdrian Chadd WPI_NT_UNLOCK(sc);
1714a20c9affSAdrian Chadd
1715a20c9affSAdrian Chadd ieee80211_iterate_nodes(&ic->ic_sta, wpi_restore_node, sc);
1716a20c9affSAdrian Chadd ieee80211_crypto_reload_keys(ic);
1717a20c9affSAdrian Chadd }
1718a20c9affSAdrian Chadd
17196607310bSBenjamin Close /**
17206607310bSBenjamin Close * Called by net80211 when ever there is a change to 80211 state machine
17216607310bSBenjamin Close */
17226607310bSBenjamin Close static int
wpi_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1723b032f27cSSam Leffler wpi_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
17246607310bSBenjamin Close {
1725b032f27cSSam Leffler struct wpi_vap *wvp = WPI_VAP(vap);
1726b032f27cSSam Leffler struct ieee80211com *ic = vap->iv_ic;
17272cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
1728692ad7caSAdrian Chadd int error = 0;
17296607310bSBenjamin Close
1730692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
1731692ad7caSAdrian Chadd
17327a79cebfSGleb Smirnoff WPI_TXQ_LOCK(sc);
17337a79cebfSGleb Smirnoff if (nstate > IEEE80211_S_INIT && sc->sc_running == 0) {
17347a79cebfSGleb Smirnoff DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
17357a79cebfSGleb Smirnoff WPI_TXQ_UNLOCK(sc);
17367a79cebfSGleb Smirnoff
17377a79cebfSGleb Smirnoff return ENXIO;
17387a79cebfSGleb Smirnoff }
17397a79cebfSGleb Smirnoff WPI_TXQ_UNLOCK(sc);
17407a79cebfSGleb Smirnoff
1741692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1742b032f27cSSam Leffler ieee80211_state_name[vap->iv_state],
1743692ad7caSAdrian Chadd ieee80211_state_name[nstate]);
17446607310bSBenjamin Close
17453289bbc9SAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN && nstate < IEEE80211_S_RUN) {
17462e79e293SAdrian Chadd if ((error = wpi_set_pslevel(sc, 0, 0, 1)) != 0) {
17472e79e293SAdrian Chadd device_printf(sc->sc_dev,
17482e79e293SAdrian Chadd "%s: could not set power saving level\n",
17492e79e293SAdrian Chadd __func__);
17502e79e293SAdrian Chadd return error;
17512e79e293SAdrian Chadd }
17523289bbc9SAdrian Chadd
17533289bbc9SAdrian Chadd wpi_set_led(sc, WPI_LED_LINK, 1, 0);
17542e79e293SAdrian Chadd }
17552e79e293SAdrian Chadd
1756692ad7caSAdrian Chadd switch (nstate) {
1757692ad7caSAdrian Chadd case IEEE80211_S_SCAN:
1758446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
1759a4703831SAdrian Chadd if (wpi_check_bss_filter(sc) != 0) {
1760692ad7caSAdrian Chadd sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1761692ad7caSAdrian Chadd if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
17623934c8a5SBernhard Schmidt device_printf(sc->sc_dev,
1763692ad7caSAdrian Chadd "%s: could not send RXON\n", __func__);
17643934c8a5SBernhard Schmidt }
17653934c8a5SBernhard Schmidt }
1766446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
1767692ad7caSAdrian Chadd break;
1768692ad7caSAdrian Chadd
1769692ad7caSAdrian Chadd case IEEE80211_S_ASSOC:
1770692ad7caSAdrian Chadd if (vap->iv_state != IEEE80211_S_RUN)
1771692ad7caSAdrian Chadd break;
1772692ad7caSAdrian Chadd /* FALLTHROUGH */
1773692ad7caSAdrian Chadd case IEEE80211_S_AUTH:
17743934c8a5SBernhard Schmidt /*
177589023da2SAdrian Chadd * NB: do not optimize AUTH -> AUTH state transmission -
177689023da2SAdrian Chadd * this will break powersave with non-QoS AP!
177789023da2SAdrian Chadd */
177889023da2SAdrian Chadd
177989023da2SAdrian Chadd /*
17803934c8a5SBernhard Schmidt * The node must be registered in the firmware before auth.
17813934c8a5SBernhard Schmidt * Also the associd must be cleared on RUN -> ASSOC
17823934c8a5SBernhard Schmidt * transitions.
17833934c8a5SBernhard Schmidt */
1784692ad7caSAdrian Chadd if ((error = wpi_auth(sc, vap)) != 0) {
17855efea30fSAndrew Thompson device_printf(sc->sc_dev,
1786692ad7caSAdrian Chadd "%s: could not move to AUTH state, error %d\n",
17875efea30fSAndrew Thompson __func__, error);
17885efea30fSAndrew Thompson }
1789692ad7caSAdrian Chadd break;
1790692ad7caSAdrian Chadd
1791692ad7caSAdrian Chadd case IEEE80211_S_RUN:
1792692ad7caSAdrian Chadd /*
1793a20c9affSAdrian Chadd * RUN -> RUN transition:
1794a20c9affSAdrian Chadd * STA mode: Just restart the timers.
1795a20c9affSAdrian Chadd * IBSS mode: Process IBSS merge.
1796692ad7caSAdrian Chadd */
1797692ad7caSAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN) {
1798a20c9affSAdrian Chadd if (vap->iv_opmode != IEEE80211_M_IBSS) {
1799446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
1800b032f27cSSam Leffler wpi_calib_timeout(sc);
1801446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
1802692ad7caSAdrian Chadd break;
1803a20c9affSAdrian Chadd } else {
1804a20c9affSAdrian Chadd /*
1805a20c9affSAdrian Chadd * Drop the BSS_FILTER bit
1806a20c9affSAdrian Chadd * (there is no another way to change bssid).
1807a20c9affSAdrian Chadd */
1808a20c9affSAdrian Chadd WPI_RXON_LOCK(sc);
1809a20c9affSAdrian Chadd sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
1810a20c9affSAdrian Chadd if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
1811a20c9affSAdrian Chadd device_printf(sc->sc_dev,
1812a20c9affSAdrian Chadd "%s: could not send RXON\n",
1813a20c9affSAdrian Chadd __func__);
1814a20c9affSAdrian Chadd }
1815a20c9affSAdrian Chadd WPI_RXON_UNLOCK(sc);
1816a20c9affSAdrian Chadd
1817a20c9affSAdrian Chadd /* Restore all what was lost. */
1818a20c9affSAdrian Chadd wpi_restore_node_table(sc, wvp);
1819a20c9affSAdrian Chadd
1820a20c9affSAdrian Chadd /* XXX set conditionally? */
1821a20c9affSAdrian Chadd wpi_updateedca(ic);
1822a20c9affSAdrian Chadd }
1823692ad7caSAdrian Chadd }
1824692ad7caSAdrian Chadd
1825692ad7caSAdrian Chadd /*
1826692ad7caSAdrian Chadd * !RUN -> RUN requires setting the association id
1827692ad7caSAdrian Chadd * which is done with a firmware cmd. We also defer
1828692ad7caSAdrian Chadd * starting the timers until that work is done.
1829692ad7caSAdrian Chadd */
1830692ad7caSAdrian Chadd if ((error = wpi_run(sc, vap)) != 0) {
1831692ad7caSAdrian Chadd device_printf(sc->sc_dev,
1832692ad7caSAdrian Chadd "%s: could not move to RUN state\n", __func__);
1833692ad7caSAdrian Chadd }
1834692ad7caSAdrian Chadd break;
1835692ad7caSAdrian Chadd
1836692ad7caSAdrian Chadd default:
1837692ad7caSAdrian Chadd break;
1838b032f27cSSam Leffler }
1839692ad7caSAdrian Chadd if (error != 0) {
1840692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
1841692ad7caSAdrian Chadd return error;
1842692ad7caSAdrian Chadd }
1843692ad7caSAdrian Chadd
1844692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
1845692ad7caSAdrian Chadd
1846d036bb20SAdrian Chadd return wvp->wv_newstate(vap, nstate, arg);
18476607310bSBenjamin Close }
18486607310bSBenjamin Close
18496607310bSBenjamin Close static void
wpi_calib_timeout(void * arg)1850692ad7caSAdrian Chadd wpi_calib_timeout(void *arg)
18516607310bSBenjamin Close {
1852692ad7caSAdrian Chadd struct wpi_softc *sc = arg;
18536607310bSBenjamin Close
1854621f1a75SAdrian Chadd if (wpi_check_bss_filter(sc) == 0)
1855692ad7caSAdrian Chadd return;
18566607310bSBenjamin Close
1857692ad7caSAdrian Chadd wpi_power_calibration(sc);
1858692ad7caSAdrian Chadd
1859692ad7caSAdrian Chadd callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
18606607310bSBenjamin Close }
18616607310bSBenjamin Close
1862692ad7caSAdrian Chadd static __inline uint8_t
rate2plcp(const uint8_t rate)1863692ad7caSAdrian Chadd rate2plcp(const uint8_t rate)
18646607310bSBenjamin Close {
1865692ad7caSAdrian Chadd switch (rate) {
1866692ad7caSAdrian Chadd case 12: return 0xd;
1867692ad7caSAdrian Chadd case 18: return 0xf;
1868692ad7caSAdrian Chadd case 24: return 0x5;
1869692ad7caSAdrian Chadd case 36: return 0x7;
1870692ad7caSAdrian Chadd case 48: return 0x9;
1871692ad7caSAdrian Chadd case 72: return 0xb;
1872692ad7caSAdrian Chadd case 96: return 0x1;
1873692ad7caSAdrian Chadd case 108: return 0x3;
1874692ad7caSAdrian Chadd case 2: return 10;
1875692ad7caSAdrian Chadd case 4: return 20;
1876692ad7caSAdrian Chadd case 11: return 55;
1877692ad7caSAdrian Chadd case 22: return 110;
1878692ad7caSAdrian Chadd default: return 0;
1879692ad7caSAdrian Chadd }
18806607310bSBenjamin Close }
18816607310bSBenjamin Close
1882692ad7caSAdrian Chadd static __inline uint8_t
plcp2rate(const uint8_t plcp)1883692ad7caSAdrian Chadd plcp2rate(const uint8_t plcp)
18846607310bSBenjamin Close {
1885692ad7caSAdrian Chadd switch (plcp) {
1886692ad7caSAdrian Chadd case 0xd: return 12;
1887692ad7caSAdrian Chadd case 0xf: return 18;
1888692ad7caSAdrian Chadd case 0x5: return 24;
1889692ad7caSAdrian Chadd case 0x7: return 36;
1890692ad7caSAdrian Chadd case 0x9: return 48;
1891692ad7caSAdrian Chadd case 0xb: return 72;
1892692ad7caSAdrian Chadd case 0x1: return 96;
1893692ad7caSAdrian Chadd case 0x3: return 108;
1894692ad7caSAdrian Chadd case 10: return 2;
1895692ad7caSAdrian Chadd case 20: return 4;
1896692ad7caSAdrian Chadd case 55: return 11;
1897692ad7caSAdrian Chadd case 110: return 22;
1898692ad7caSAdrian Chadd default: return 0;
18996607310bSBenjamin Close }
1900692ad7caSAdrian Chadd }
1901692ad7caSAdrian Chadd
1902692ad7caSAdrian Chadd /* Quickly determine if a given rate is CCK or OFDM. */
1903692ad7caSAdrian Chadd #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
19046607310bSBenjamin Close
19056607310bSBenjamin Close static void
wpi_rx_done(struct wpi_softc * sc,struct wpi_rx_desc * desc,struct wpi_rx_data * data)1906692ad7caSAdrian Chadd wpi_rx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc,
19076607310bSBenjamin Close struct wpi_rx_data *data)
19086607310bSBenjamin Close {
19097a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
19106607310bSBenjamin Close struct wpi_rx_ring *ring = &sc->rxq;
19116607310bSBenjamin Close struct wpi_rx_stat *stat;
19126607310bSBenjamin Close struct wpi_rx_head *head;
19136607310bSBenjamin Close struct wpi_rx_tail *tail;
1914692ad7caSAdrian Chadd struct ieee80211_frame *wh;
19156607310bSBenjamin Close struct ieee80211_node *ni;
1916692ad7caSAdrian Chadd struct mbuf *m, *m1;
191782f1b132SAndrew Thompson bus_addr_t paddr;
1918692ad7caSAdrian Chadd uint32_t flags;
1919692ad7caSAdrian Chadd uint16_t len;
192082f1b132SAndrew Thompson int error;
19216607310bSBenjamin Close
19226607310bSBenjamin Close stat = (struct wpi_rx_stat *)(desc + 1);
19236607310bSBenjamin Close
1924859f6ddcSAdrian Chadd if (__predict_false(stat->len > WPI_STAT_MAXLEN)) {
1925692ad7caSAdrian Chadd device_printf(sc->sc_dev, "invalid RX statistic header\n");
1926fae61c69SAdrian Chadd goto fail1;
19276607310bSBenjamin Close }
19286607310bSBenjamin Close
1929a71ad787SBernhard Schmidt bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
19306607310bSBenjamin Close head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1931692ad7caSAdrian Chadd len = le16toh(head->len);
1932692ad7caSAdrian Chadd tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + len);
1933692ad7caSAdrian Chadd flags = le32toh(tail->flags);
19346607310bSBenjamin Close
1935692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RECV, "%s: idx %d len %d stat len %u rssi %d"
1936692ad7caSAdrian Chadd " rate %x chan %d tstamp %ju\n", __func__, ring->cur,
1937692ad7caSAdrian Chadd le32toh(desc->len), len, (int8_t)stat->rssi,
1938692ad7caSAdrian Chadd head->plcp, head->chan, (uintmax_t)le64toh(tail->tstamp));
19396607310bSBenjamin Close
1940692ad7caSAdrian Chadd /* Discard frames with a bad FCS early. */
1941692ad7caSAdrian Chadd if ((flags & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1942692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RECV, "%s: RX flags error %x\n",
1943692ad7caSAdrian Chadd __func__, flags);
1944fae61c69SAdrian Chadd goto fail1;
1945fcec677dSJuli Mallett }
1946692ad7caSAdrian Chadd /* Discard frames that are too short. */
19473077c104SAdrian Chadd if (len < sizeof (struct ieee80211_frame_ack)) {
1948692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RECV, "%s: frame too short: %d\n",
1949692ad7caSAdrian Chadd __func__, len);
1950fae61c69SAdrian Chadd goto fail1;
1951fcec677dSJuli Mallett }
1952fcec677dSJuli Mallett
1953692ad7caSAdrian Chadd m1 = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1954859f6ddcSAdrian Chadd if (__predict_false(m1 == NULL)) {
1955692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_ANY, "%s: no mbuf to restock ring\n",
1956692ad7caSAdrian Chadd __func__);
1957fae61c69SAdrian Chadd goto fail1;
195882f1b132SAndrew Thompson }
1959a71ad787SBernhard Schmidt bus_dmamap_unload(ring->data_dmat, data->map);
1960a71ad787SBernhard Schmidt
1961692ad7caSAdrian Chadd error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *),
1962692ad7caSAdrian Chadd MJUMPAGESIZE, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1963859f6ddcSAdrian Chadd if (__predict_false(error != 0 && error != EFBIG)) {
196482f1b132SAndrew Thompson device_printf(sc->sc_dev,
196582f1b132SAndrew Thompson "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1966692ad7caSAdrian Chadd m_freem(m1);
1967692ad7caSAdrian Chadd
1968692ad7caSAdrian Chadd /* Try to reload the old mbuf. */
1969692ad7caSAdrian Chadd error = bus_dmamap_load(ring->data_dmat, data->map,
1970692ad7caSAdrian Chadd mtod(data->m, void *), MJUMPAGESIZE, wpi_dma_map_addr,
1971692ad7caSAdrian Chadd &paddr, BUS_DMA_NOWAIT);
1972692ad7caSAdrian Chadd if (error != 0 && error != EFBIG) {
1973692ad7caSAdrian Chadd panic("%s: could not load old RX mbuf", __func__);
1974692ad7caSAdrian Chadd }
1975692ad7caSAdrian Chadd /* Physical address may have changed. */
1976692ad7caSAdrian Chadd ring->desc[ring->cur] = htole32(paddr);
1977692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map,
1978692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
1979fae61c69SAdrian Chadd goto fail1;
198082f1b132SAndrew Thompson }
19816607310bSBenjamin Close
198282f1b132SAndrew Thompson m = data->m;
1983692ad7caSAdrian Chadd data->m = m1;
1984692ad7caSAdrian Chadd /* Update RX descriptor. */
1985692ad7caSAdrian Chadd ring->desc[ring->cur] = htole32(paddr);
1986692ad7caSAdrian Chadd bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1987692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
1988692ad7caSAdrian Chadd
1989692ad7caSAdrian Chadd /* Finalize mbuf. */
19906607310bSBenjamin Close m->m_data = (caddr_t)(head + 1);
1991692ad7caSAdrian Chadd m->m_pkthdr.len = m->m_len = len;
19926607310bSBenjamin Close
1993692ad7caSAdrian Chadd /* Grab a reference to the source node. */
1994692ad7caSAdrian Chadd wh = mtod(m, struct ieee80211_frame *);
1995692ad7caSAdrian Chadd
1996692ad7caSAdrian Chadd if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1997d29fcfb7SAdrian Chadd (flags & WPI_RX_CIPHER_MASK) == WPI_RX_CIPHER_CCMP) {
1998692ad7caSAdrian Chadd /* Check whether decryption was successful or not. */
1999692ad7caSAdrian Chadd if ((flags & WPI_RX_DECRYPT_MASK) != WPI_RX_DECRYPT_OK) {
2000692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RECV,
2001692ad7caSAdrian Chadd "CCMP decryption failed 0x%x\n", flags);
2002fae61c69SAdrian Chadd goto fail2;
2003692ad7caSAdrian Chadd }
2004692ad7caSAdrian Chadd m->m_flags |= M_WEP;
2005692ad7caSAdrian Chadd }
20066607310bSBenjamin Close
20073077c104SAdrian Chadd if (len >= sizeof(struct ieee80211_frame_min))
2008d29fcfb7SAdrian Chadd ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
20093077c104SAdrian Chadd else
20103077c104SAdrian Chadd ni = NULL;
20113077c104SAdrian Chadd
2012a20c9affSAdrian Chadd sc->rx_tstamp = tail->tstamp;
2013d29fcfb7SAdrian Chadd
20145463c4a4SSam Leffler if (ieee80211_radiotap_active(ic)) {
20156607310bSBenjamin Close struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
20166607310bSBenjamin Close
20176607310bSBenjamin Close tap->wr_flags = 0;
2018692ad7caSAdrian Chadd if (head->flags & htole16(WPI_STAT_FLAG_SHPREAMBLE))
2019692ad7caSAdrian Chadd tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
20208e4cf38aSAdrian Chadd tap->wr_dbm_antsignal = (int8_t)(stat->rssi + WPI_RSSI_OFFSET);
20210b455e8fSAdrian Chadd tap->wr_dbm_antnoise = WPI_RSSI_OFFSET;
20226607310bSBenjamin Close tap->wr_tsft = tail->tstamp;
20236607310bSBenjamin Close tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
2024692ad7caSAdrian Chadd tap->wr_rate = plcp2rate(head->plcp);
20256607310bSBenjamin Close }
20266607310bSBenjamin Close
20276607310bSBenjamin Close WPI_UNLOCK(sc);
20286607310bSBenjamin Close
2029692ad7caSAdrian Chadd /* Send the frame to the 802.11 layer. */
2030b032f27cSSam Leffler if (ni != NULL) {
20318e4cf38aSAdrian Chadd (void)ieee80211_input(ni, m, stat->rssi, WPI_RSSI_OFFSET);
2032692ad7caSAdrian Chadd /* Node is no longer needed. */
20336607310bSBenjamin Close ieee80211_free_node(ni);
2034b032f27cSSam Leffler } else
20358e4cf38aSAdrian Chadd (void)ieee80211_input_all(ic, m, stat->rssi, WPI_RSSI_OFFSET);
2036b032f27cSSam Leffler
20376607310bSBenjamin Close WPI_LOCK(sc);
2038fae61c69SAdrian Chadd
2039fae61c69SAdrian Chadd return;
2040fae61c69SAdrian Chadd
2041d29fcfb7SAdrian Chadd fail2: m_freem(m);
2042fae61c69SAdrian Chadd
20437a79cebfSGleb Smirnoff fail1: counter_u64_add(ic->ic_ierrors, 1);
20446607310bSBenjamin Close }
20456607310bSBenjamin Close
20466607310bSBenjamin Close static void
wpi_rx_statistics(struct wpi_softc * sc,struct wpi_rx_desc * desc,struct wpi_rx_data * data)2047692ad7caSAdrian Chadd wpi_rx_statistics(struct wpi_softc *sc, struct wpi_rx_desc *desc,
2048692ad7caSAdrian Chadd struct wpi_rx_data *data)
20496607310bSBenjamin Close {
2050692ad7caSAdrian Chadd /* Ignore */
20516607310bSBenjamin Close }
20526607310bSBenjamin Close
20536607310bSBenjamin Close static void
wpi_tx_done(struct wpi_softc * sc,struct wpi_rx_desc * desc)2054692ad7caSAdrian Chadd wpi_tx_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
20556607310bSBenjamin Close {
2056f6930becSAndriy Voskoboinyk struct ieee80211_ratectl_tx_status *txs = &sc->sc_txs;
2057692ad7caSAdrian Chadd struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
2058692ad7caSAdrian Chadd struct wpi_tx_data *data = &ring->data[desc->idx];
2059692ad7caSAdrian Chadd struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
2060692ad7caSAdrian Chadd struct mbuf *m;
2061692ad7caSAdrian Chadd struct ieee80211_node *ni;
2062b52207dbSAdrian Chadd uint32_t status = le32toh(stat->status);
2063692ad7caSAdrian Chadd
2064692ad7caSAdrian Chadd KASSERT(data->ni != NULL, ("no node"));
206539060fa9SAdrian Chadd KASSERT(data->m != NULL, ("no mbuf"));
2066692ad7caSAdrian Chadd
2067692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2068692ad7caSAdrian Chadd
2069692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_XMIT, "%s: "
2070692ad7caSAdrian Chadd "qid %d idx %d retries %d btkillcnt %d rate %x duration %d "
207130b60481SAdrian Chadd "status %x\n", __func__, desc->qid, desc->idx, stat->ackfailcnt,
2072692ad7caSAdrian Chadd stat->btkillcnt, stat->rate, le32toh(stat->duration), status);
2073692ad7caSAdrian Chadd
2074692ad7caSAdrian Chadd /* Unmap and free mbuf. */
2075692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
2076692ad7caSAdrian Chadd bus_dmamap_unload(ring->data_dmat, data->map);
2077692ad7caSAdrian Chadd m = data->m, data->m = NULL;
2078692ad7caSAdrian Chadd ni = data->ni, data->ni = NULL;
2079692ad7caSAdrian Chadd
20803f3d4d61SAndriy Voskoboinyk /* Restore frame header. */
20813f3d4d61SAndriy Voskoboinyk KASSERT(M_LEADINGSPACE(m) >= data->hdrlen, ("no frame header!"));
20823f3d4d61SAndriy Voskoboinyk M_PREPEND(m, data->hdrlen, M_NOWAIT);
20833f3d4d61SAndriy Voskoboinyk KASSERT(m != NULL, ("%s: m is NULL\n", __func__));
20843f3d4d61SAndriy Voskoboinyk
2085692ad7caSAdrian Chadd /*
2086692ad7caSAdrian Chadd * Update rate control statistics for the node.
2087692ad7caSAdrian Chadd */
20883f3d4d61SAndriy Voskoboinyk txs->pktlen = m->m_pkthdr.len;
2089f6930becSAndriy Voskoboinyk txs->short_retries = stat->rtsfailcnt;
2090f6930becSAndriy Voskoboinyk txs->long_retries = stat->ackfailcnt / WPI_NTRIES_DEFAULT;
2091f6930becSAndriy Voskoboinyk if (!(status & WPI_TX_STATUS_FAIL))
2092f6930becSAndriy Voskoboinyk txs->status = IEEE80211_RATECTL_TX_SUCCESS;
2093f6930becSAndriy Voskoboinyk else {
2094f6930becSAndriy Voskoboinyk switch (status & 0xff) {
2095f6930becSAndriy Voskoboinyk case WPI_TX_STATUS_FAIL_SHORT_LIMIT:
2096f6930becSAndriy Voskoboinyk txs->status = IEEE80211_RATECTL_TX_FAIL_SHORT;
2097f6930becSAndriy Voskoboinyk break;
2098f6930becSAndriy Voskoboinyk case WPI_TX_STATUS_FAIL_LONG_LIMIT:
2099f6930becSAndriy Voskoboinyk txs->status = IEEE80211_RATECTL_TX_FAIL_LONG;
2100f6930becSAndriy Voskoboinyk break;
2101f6930becSAndriy Voskoboinyk case WPI_TX_STATUS_FAIL_LIFE_EXPIRE:
2102f6930becSAndriy Voskoboinyk txs->status = IEEE80211_RATECTL_TX_FAIL_EXPIRED;
2103f6930becSAndriy Voskoboinyk break;
2104f6930becSAndriy Voskoboinyk default:
2105f6930becSAndriy Voskoboinyk txs->status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
2106f6930becSAndriy Voskoboinyk break;
2107f6930becSAndriy Voskoboinyk }
2108f6930becSAndriy Voskoboinyk }
2109692ad7caSAdrian Chadd
2110f6930becSAndriy Voskoboinyk ieee80211_ratectl_tx_complete(ni, txs);
21113d9089a6SAdrian Chadd ieee80211_tx_complete(ni, m, (status & WPI_TX_STATUS_FAIL) != 0);
2112692ad7caSAdrian Chadd
2113fc5870b4SAdrian Chadd WPI_TXQ_STATE_LOCK(sc);
2114753ca9efSAdrian Chadd if (--ring->queued > 0)
2115d74e544aSAdrian Chadd callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout, sc);
2116753ca9efSAdrian Chadd else
2117a6b6ca52SAdrian Chadd callout_stop(&sc->tx_timeout);
2118fc5870b4SAdrian Chadd WPI_TXQ_STATE_UNLOCK(sc);
2119692ad7caSAdrian Chadd
2120692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
2121692ad7caSAdrian Chadd }
2122692ad7caSAdrian Chadd
2123692ad7caSAdrian Chadd /*
2124692ad7caSAdrian Chadd * Process a "command done" firmware notification. This is where we wakeup
2125692ad7caSAdrian Chadd * processes waiting for a synchronous command completion.
2126692ad7caSAdrian Chadd */
2127692ad7caSAdrian Chadd static void
wpi_cmd_done(struct wpi_softc * sc,struct wpi_rx_desc * desc)2128692ad7caSAdrian Chadd wpi_cmd_done(struct wpi_softc *sc, struct wpi_rx_desc *desc)
2129692ad7caSAdrian Chadd {
21308e4cf38aSAdrian Chadd struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
21316607310bSBenjamin Close struct wpi_tx_data *data;
2132c34db420SAdrian Chadd struct wpi_tx_cmd *cmd;
21336607310bSBenjamin Close
2134cec45f04SAdrian Chadd DPRINTF(sc, WPI_DEBUG_CMD, "cmd notification qid %x idx %d flags %x "
2135cec45f04SAdrian Chadd "type %s len %d\n", desc->qid, desc->idx,
21366607310bSBenjamin Close desc->flags, wpi_cmd_str(desc->type),
2137692ad7caSAdrian Chadd le32toh(desc->len));
21386607310bSBenjamin Close
21398e4cf38aSAdrian Chadd if ((desc->qid & WPI_RX_DESC_QID_MSK) != WPI_CMD_QUEUE_NUM)
2140692ad7caSAdrian Chadd return; /* Not a command ack. */
21416607310bSBenjamin Close
214245ac43f3SAdrian Chadd KASSERT(ring->queued == 0, ("ring->queued must be 0"));
214345ac43f3SAdrian Chadd
21446607310bSBenjamin Close data = &ring->data[desc->idx];
2145c34db420SAdrian Chadd cmd = &ring->cmd[desc->idx];
21466607310bSBenjamin Close
2147692ad7caSAdrian Chadd /* If the command was mapped in an mbuf, free it. */
21486607310bSBenjamin Close if (data->m != NULL) {
2149692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map,
2150692ad7caSAdrian Chadd BUS_DMASYNC_POSTWRITE);
21516607310bSBenjamin Close bus_dmamap_unload(ring->data_dmat, data->map);
21526607310bSBenjamin Close m_freem(data->m);
21536607310bSBenjamin Close data->m = NULL;
21546607310bSBenjamin Close }
21556607310bSBenjamin Close
2156c34db420SAdrian Chadd wakeup(cmd);
21575cacb17fSAdrian Chadd
21585cacb17fSAdrian Chadd if (desc->type == WPI_CMD_SET_POWER_MODE) {
2159c34db420SAdrian Chadd struct wpi_pmgt_cmd *pcmd = (struct wpi_pmgt_cmd *)cmd->data;
2160c34db420SAdrian Chadd
2161c34db420SAdrian Chadd bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2162c34db420SAdrian Chadd BUS_DMASYNC_POSTREAD);
2163c34db420SAdrian Chadd
21645cacb17fSAdrian Chadd WPI_TXQ_LOCK(sc);
2165c34db420SAdrian Chadd if (le16toh(pcmd->flags) & WPI_PS_ALLOW_SLEEP) {
21665cacb17fSAdrian Chadd sc->sc_update_rx_ring = wpi_update_rx_ring_ps;
21675cacb17fSAdrian Chadd sc->sc_update_tx_ring = wpi_update_tx_ring_ps;
21685cacb17fSAdrian Chadd } else {
21695cacb17fSAdrian Chadd sc->sc_update_rx_ring = wpi_update_rx_ring;
21705cacb17fSAdrian Chadd sc->sc_update_tx_ring = wpi_update_tx_ring;
21715cacb17fSAdrian Chadd }
21725cacb17fSAdrian Chadd WPI_TXQ_UNLOCK(sc);
21735cacb17fSAdrian Chadd }
21746607310bSBenjamin Close }
21756607310bSBenjamin Close
21766607310bSBenjamin Close static void
wpi_notif_intr(struct wpi_softc * sc)21776607310bSBenjamin Close wpi_notif_intr(struct wpi_softc *sc)
21786607310bSBenjamin Close {
21797a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
2180692ad7caSAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2181b52207dbSAdrian Chadd uint32_t hw;
21826607310bSBenjamin Close
2183f015cb78SBernhard Schmidt bus_dmamap_sync(sc->shared_dma.tag, sc->shared_dma.map,
2184f015cb78SBernhard Schmidt BUS_DMASYNC_POSTREAD);
2185f015cb78SBernhard Schmidt
2186e43b274aSAdrian Chadd hw = le32toh(sc->shared->next) & 0xfff;
2187692ad7caSAdrian Chadd hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
2188692ad7caSAdrian Chadd
2189fae61c69SAdrian Chadd while (sc->rxq.cur != hw) {
2190692ad7caSAdrian Chadd sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
2191692ad7caSAdrian Chadd
2192692ad7caSAdrian Chadd struct wpi_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2193692ad7caSAdrian Chadd struct wpi_rx_desc *desc;
2194f015cb78SBernhard Schmidt
2195f015cb78SBernhard Schmidt bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2196f015cb78SBernhard Schmidt BUS_DMASYNC_POSTREAD);
2197692ad7caSAdrian Chadd desc = mtod(data->m, struct wpi_rx_desc *);
21986607310bSBenjamin Close
2199692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_NOTIFY,
2200692ad7caSAdrian Chadd "%s: cur=%d; qid %x idx %d flags %x type %d(%s) len %d\n",
2201692ad7caSAdrian Chadd __func__, sc->rxq.cur, desc->qid, desc->idx, desc->flags,
2202692ad7caSAdrian Chadd desc->type, wpi_cmd_str(desc->type), le32toh(desc->len));
22036607310bSBenjamin Close
22048e4cf38aSAdrian Chadd if (!(desc->qid & WPI_UNSOLICITED_RX_NOTIF)) {
22058e4cf38aSAdrian Chadd /* Reply to a command. */
2206692ad7caSAdrian Chadd wpi_cmd_done(sc, desc);
22078e4cf38aSAdrian Chadd }
22086607310bSBenjamin Close
22096607310bSBenjamin Close switch (desc->type) {
22106607310bSBenjamin Close case WPI_RX_DONE:
2211692ad7caSAdrian Chadd /* An 802.11 frame has been received. */
2212692ad7caSAdrian Chadd wpi_rx_done(sc, desc, data);
221371258eb2SAdrian Chadd
2214859f6ddcSAdrian Chadd if (__predict_false(sc->sc_running == 0)) {
221571258eb2SAdrian Chadd /* wpi_stop() was called. */
221671258eb2SAdrian Chadd return;
221771258eb2SAdrian Chadd }
221871258eb2SAdrian Chadd
22196607310bSBenjamin Close break;
22206607310bSBenjamin Close
22216607310bSBenjamin Close case WPI_TX_DONE:
2222692ad7caSAdrian Chadd /* An 802.11 frame has been transmitted. */
2223692ad7caSAdrian Chadd wpi_tx_done(sc, desc);
22246607310bSBenjamin Close break;
22256607310bSBenjamin Close
2226692ad7caSAdrian Chadd case WPI_RX_STATISTICS:
2227692ad7caSAdrian Chadd case WPI_BEACON_STATISTICS:
2228692ad7caSAdrian Chadd wpi_rx_statistics(sc, desc, data);
2229692ad7caSAdrian Chadd break;
2230692ad7caSAdrian Chadd
2231692ad7caSAdrian Chadd case WPI_BEACON_MISSED:
2232692ad7caSAdrian Chadd {
2233692ad7caSAdrian Chadd struct wpi_beacon_missed *miss =
2234692ad7caSAdrian Chadd (struct wpi_beacon_missed *)(desc + 1);
22355c015919SAdrian Chadd uint32_t expected, misses, received, threshold;
2236692ad7caSAdrian Chadd
2237692ad7caSAdrian Chadd bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2238692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD);
2239b4324a50SAdrian Chadd
2240692ad7caSAdrian Chadd misses = le32toh(miss->consecutive);
2241b4324a50SAdrian Chadd expected = le32toh(miss->expected);
2242b4324a50SAdrian Chadd received = le32toh(miss->received);
22435c015919SAdrian Chadd threshold = MAX(2, vap->iv_bmissthreshold);
2244692ad7caSAdrian Chadd
22453e54d9fdSAdrian Chadd DPRINTF(sc, WPI_DEBUG_BMISS,
2246cf990903SAdrian Chadd "%s: beacons missed %u(%u) (received %u/%u)\n",
2247cf990903SAdrian Chadd __func__, misses, le32toh(miss->total), received,
2248cf990903SAdrian Chadd expected);
2249692ad7caSAdrian Chadd
22506f506674SAdrian Chadd if (misses >= threshold ||
22516f506674SAdrian Chadd (received == 0 && expected >= threshold)) {
22526f506674SAdrian Chadd WPI_RXON_LOCK(sc);
22536f506674SAdrian Chadd if (callout_pending(&sc->scan_timeout)) {
22546f506674SAdrian Chadd wpi_cmd(sc, WPI_CMD_SCAN_ABORT, NULL,
22556f506674SAdrian Chadd 0, 1);
22566f506674SAdrian Chadd }
22576f506674SAdrian Chadd WPI_RXON_UNLOCK(sc);
2258692ad7caSAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN &&
22596f506674SAdrian Chadd (ic->ic_flags & IEEE80211_F_SCAN) == 0)
2260692ad7caSAdrian Chadd ieee80211_beacon_miss(ic);
22616f506674SAdrian Chadd }
2262fd06b537SAdrian Chadd
2263692ad7caSAdrian Chadd break;
2264692ad7caSAdrian Chadd }
22656ddc7d4dSAdrian Chadd #ifdef WPI_DEBUG
22666ddc7d4dSAdrian Chadd case WPI_BEACON_SENT:
22676ddc7d4dSAdrian Chadd {
22686ddc7d4dSAdrian Chadd struct wpi_tx_stat *stat =
22696ddc7d4dSAdrian Chadd (struct wpi_tx_stat *)(desc + 1);
22706ddc7d4dSAdrian Chadd uint64_t *tsf = (uint64_t *)(stat + 1);
22716ddc7d4dSAdrian Chadd uint32_t *mode = (uint32_t *)(tsf + 1);
22726ddc7d4dSAdrian Chadd
22736ddc7d4dSAdrian Chadd bus_dmamap_sync(sc->rxq.data_dmat, data->map,
22746ddc7d4dSAdrian Chadd BUS_DMASYNC_POSTREAD);
22756ddc7d4dSAdrian Chadd
22766ddc7d4dSAdrian Chadd DPRINTF(sc, WPI_DEBUG_BEACON,
22776ddc7d4dSAdrian Chadd "beacon sent: rts %u, ack %u, btkill %u, rate %u, "
22786ddc7d4dSAdrian Chadd "duration %u, status %x, tsf %ju, mode %x\n",
22796ddc7d4dSAdrian Chadd stat->rtsfailcnt, stat->ackfailcnt,
22806ddc7d4dSAdrian Chadd stat->btkillcnt, stat->rate, le32toh(stat->duration),
22814c09e592SAdrian Chadd le32toh(stat->status), le64toh(*tsf),
22824c09e592SAdrian Chadd le32toh(*mode));
22836ddc7d4dSAdrian Chadd
22846ddc7d4dSAdrian Chadd break;
22856ddc7d4dSAdrian Chadd }
22866ddc7d4dSAdrian Chadd #endif
22876607310bSBenjamin Close case WPI_UC_READY:
22886607310bSBenjamin Close {
22896607310bSBenjamin Close struct wpi_ucode_info *uc =
22906607310bSBenjamin Close (struct wpi_ucode_info *)(desc + 1);
22916607310bSBenjamin Close
2292692ad7caSAdrian Chadd /* The microcontroller is ready. */
2293692ad7caSAdrian Chadd bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2294692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD);
2295692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RESET,
2296692ad7caSAdrian Chadd "microcode alive notification version=%d.%d "
2297692ad7caSAdrian Chadd "subtype=%x alive=%x\n", uc->major, uc->minor,
2298692ad7caSAdrian Chadd uc->subtype, le32toh(uc->valid));
22996607310bSBenjamin Close
23006607310bSBenjamin Close if (le32toh(uc->valid) != 1) {
23016607310bSBenjamin Close device_printf(sc->sc_dev,
23026607310bSBenjamin Close "microcontroller initialization failed\n");
23036607310bSBenjamin Close wpi_stop_locked(sc);
23047a79cebfSGleb Smirnoff return;
23056607310bSBenjamin Close }
2306692ad7caSAdrian Chadd /* Save the address of the error log in SRAM. */
2307692ad7caSAdrian Chadd sc->errptr = le32toh(uc->errptr);
23086607310bSBenjamin Close break;
23096607310bSBenjamin Close }
23106607310bSBenjamin Close case WPI_STATE_CHANGED:
23116607310bSBenjamin Close {
2312692ad7caSAdrian Chadd bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2313692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD);
2314692ad7caSAdrian Chadd
23156607310bSBenjamin Close uint32_t *status = (uint32_t *)(desc + 1);
2316fae61c69SAdrian Chadd
2317692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_STATE, "state changed to %x\n",
2318692ad7caSAdrian Chadd le32toh(*status));
2319fae61c69SAdrian Chadd
23206607310bSBenjamin Close if (le32toh(*status) & 1) {
23217964dd56SAdrian Chadd WPI_NT_LOCK(sc);
2322eca2eb39SAdrian Chadd wpi_clear_node_table(sc);
23237964dd56SAdrian Chadd WPI_NT_UNLOCK(sc);
2324abd89aa2SAndriy Voskoboinyk ieee80211_runtask(ic,
2325b3cd88daSAdrian Chadd &sc->sc_radiooff_task);
2326692ad7caSAdrian Chadd return;
23276607310bSBenjamin Close }
23286607310bSBenjamin Close break;
23296607310bSBenjamin Close }
2330cbaa7080SAdrian Chadd #ifdef WPI_DEBUG
23316607310bSBenjamin Close case WPI_START_SCAN:
23326607310bSBenjamin Close {
2333692ad7caSAdrian Chadd bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2334692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD);
2335cbaa7080SAdrian Chadd
23366607310bSBenjamin Close struct wpi_start_scan *scan =
23376607310bSBenjamin Close (struct wpi_start_scan *)(desc + 1);
2338692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_SCAN,
2339692ad7caSAdrian Chadd "%s: scanning channel %d status %x\n",
2340692ad7caSAdrian Chadd __func__, scan->chan, le32toh(scan->status));
2341cbaa7080SAdrian Chadd
23426607310bSBenjamin Close break;
23436607310bSBenjamin Close }
2344cbaa7080SAdrian Chadd #endif
23456607310bSBenjamin Close case WPI_STOP_SCAN:
23466607310bSBenjamin Close {
2347692ad7caSAdrian Chadd bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2348692ad7caSAdrian Chadd BUS_DMASYNC_POSTREAD);
23496f506674SAdrian Chadd
23506607310bSBenjamin Close struct wpi_stop_scan *scan =
23516607310bSBenjamin Close (struct wpi_stop_scan *)(desc + 1);
23526f506674SAdrian Chadd
2353692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_SCAN,
2354692ad7caSAdrian Chadd "scan finished nchan=%d status=%d chan=%d\n",
2355692ad7caSAdrian Chadd scan->nchan, scan->status, scan->chan);
23566f506674SAdrian Chadd
2357446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
23580bc39fc6SAdrian Chadd callout_stop(&sc->scan_timeout);
2359446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
23606f506674SAdrian Chadd if (scan->status == WPI_SCAN_ABORTED)
23616f506674SAdrian Chadd ieee80211_cancel_scan(vap);
23626f506674SAdrian Chadd else
2363b032f27cSSam Leffler ieee80211_scan_next(vap);
23646607310bSBenjamin Close break;
23656607310bSBenjamin Close }
2366692ad7caSAdrian Chadd }
2367692ad7caSAdrian Chadd
2368a5403e31SAdrian Chadd if (sc->rxq.cur % 8 == 0) {
2369692ad7caSAdrian Chadd /* Tell the firmware what we have processed. */
23705cacb17fSAdrian Chadd sc->sc_update_rx_ring(sc);
2371692ad7caSAdrian Chadd }
2372a5403e31SAdrian Chadd }
2373a5403e31SAdrian Chadd }
2374692ad7caSAdrian Chadd
2375692ad7caSAdrian Chadd /*
2376692ad7caSAdrian Chadd * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2377692ad7caSAdrian Chadd * from power-down sleep mode.
2378692ad7caSAdrian Chadd */
2379692ad7caSAdrian Chadd static void
wpi_wakeup_intr(struct wpi_softc * sc)2380692ad7caSAdrian Chadd wpi_wakeup_intr(struct wpi_softc *sc)
2381a7099588SBenjamin Close {
2382692ad7caSAdrian Chadd int qid;
2383a7099588SBenjamin Close
2384692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_PWRSAVE,
2385692ad7caSAdrian Chadd "%s: ucode wakeup from power-down sleep\n", __func__);
2386692ad7caSAdrian Chadd
2387692ad7caSAdrian Chadd /* Wakeup RX and TX rings. */
2388692ad7caSAdrian Chadd if (sc->rxq.update) {
2389692ad7caSAdrian Chadd sc->rxq.update = 0;
2390fae61c69SAdrian Chadd wpi_update_rx_ring(sc);
2391a7099588SBenjamin Close }
239263f57699SAdrian Chadd WPI_TXQ_LOCK(sc);
2393ebf62e08SAdrian Chadd for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++) {
2394692ad7caSAdrian Chadd struct wpi_tx_ring *ring = &sc->txq[qid];
2395692ad7caSAdrian Chadd
2396692ad7caSAdrian Chadd if (ring->update) {
2397692ad7caSAdrian Chadd ring->update = 0;
2398fae61c69SAdrian Chadd wpi_update_tx_ring(sc, ring);
2399a7099588SBenjamin Close }
24006607310bSBenjamin Close }
2401692ad7caSAdrian Chadd WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_MAC_ACCESS_REQ);
24025cacb17fSAdrian Chadd WPI_TXQ_UNLOCK(sc);
24036607310bSBenjamin Close }
24046607310bSBenjamin Close
2405692ad7caSAdrian Chadd /*
2406a6df6cfeSAdrian Chadd * This function prints firmware registers
2407a6df6cfeSAdrian Chadd */
2408a6df6cfeSAdrian Chadd #ifdef WPI_DEBUG
2409a6df6cfeSAdrian Chadd static void
wpi_debug_registers(struct wpi_softc * sc)2410a6df6cfeSAdrian Chadd wpi_debug_registers(struct wpi_softc *sc)
2411a6df6cfeSAdrian Chadd {
2412e43b274aSAdrian Chadd size_t i;
2413a6df6cfeSAdrian Chadd static const uint32_t csr_tbl[] = {
2414a6df6cfeSAdrian Chadd WPI_HW_IF_CONFIG,
2415a6df6cfeSAdrian Chadd WPI_INT,
2416a6df6cfeSAdrian Chadd WPI_INT_MASK,
2417a6df6cfeSAdrian Chadd WPI_FH_INT,
2418a6df6cfeSAdrian Chadd WPI_GPIO_IN,
2419a6df6cfeSAdrian Chadd WPI_RESET,
2420a6df6cfeSAdrian Chadd WPI_GP_CNTRL,
2421a6df6cfeSAdrian Chadd WPI_EEPROM,
2422a6df6cfeSAdrian Chadd WPI_EEPROM_GP,
2423a6df6cfeSAdrian Chadd WPI_GIO,
2424a6df6cfeSAdrian Chadd WPI_UCODE_GP1,
2425a6df6cfeSAdrian Chadd WPI_UCODE_GP2,
2426a6df6cfeSAdrian Chadd WPI_GIO_CHICKEN,
2427a6df6cfeSAdrian Chadd WPI_ANA_PLL,
2428a6df6cfeSAdrian Chadd WPI_DBG_HPET_MEM,
2429a6df6cfeSAdrian Chadd };
2430a6df6cfeSAdrian Chadd static const uint32_t prph_tbl[] = {
2431a6df6cfeSAdrian Chadd WPI_APMG_CLK_CTRL,
2432a6df6cfeSAdrian Chadd WPI_APMG_PS,
2433a6df6cfeSAdrian Chadd WPI_APMG_PCI_STT,
2434a6df6cfeSAdrian Chadd WPI_APMG_RFKILL,
2435a6df6cfeSAdrian Chadd };
2436a6df6cfeSAdrian Chadd
2437a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER,"%s","\n");
2438a6df6cfeSAdrian Chadd
243912e0ff3bSAdrian Chadd for (i = 0; i < nitems(csr_tbl); i++) {
2440a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2441a6df6cfeSAdrian Chadd wpi_get_csr_string(csr_tbl[i]), WPI_READ(sc, csr_tbl[i]));
2442a6df6cfeSAdrian Chadd
2443a6df6cfeSAdrian Chadd if ((i + 1) % 2 == 0)
2444a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2445a6df6cfeSAdrian Chadd }
2446a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER, "\n\n");
2447a6df6cfeSAdrian Chadd
2448a6df6cfeSAdrian Chadd if (wpi_nic_lock(sc) == 0) {
244912e0ff3bSAdrian Chadd for (i = 0; i < nitems(prph_tbl); i++) {
2450a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER, " %-18s: 0x%08x ",
2451a6df6cfeSAdrian Chadd wpi_get_prph_string(prph_tbl[i]),
2452a6df6cfeSAdrian Chadd wpi_prph_read(sc, prph_tbl[i]));
2453a6df6cfeSAdrian Chadd
2454a6df6cfeSAdrian Chadd if ((i + 1) % 2 == 0)
2455a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2456a6df6cfeSAdrian Chadd }
2457a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER, "\n");
2458a6df6cfeSAdrian Chadd wpi_nic_unlock(sc);
2459a6df6cfeSAdrian Chadd } else {
2460a6df6cfeSAdrian Chadd DPRINTF(sc, WPI_DEBUG_REGISTER,
2461a6df6cfeSAdrian Chadd "Cannot access internal registers.\n");
2462a6df6cfeSAdrian Chadd }
2463a6df6cfeSAdrian Chadd }
2464a6df6cfeSAdrian Chadd #endif
2465a6df6cfeSAdrian Chadd
2466a6df6cfeSAdrian Chadd /*
2467692ad7caSAdrian Chadd * Dump the error log of the firmware when a firmware panic occurs. Although
2468692ad7caSAdrian Chadd * we can't debug the firmware because it is neither open source nor free, it
2469692ad7caSAdrian Chadd * can help us to identify certain classes of problems.
2470692ad7caSAdrian Chadd */
2471692ad7caSAdrian Chadd static void
wpi_fatal_intr(struct wpi_softc * sc)2472692ad7caSAdrian Chadd wpi_fatal_intr(struct wpi_softc *sc)
2473692ad7caSAdrian Chadd {
2474692ad7caSAdrian Chadd struct wpi_fw_dump dump;
2475692ad7caSAdrian Chadd uint32_t i, offset, count;
2476692ad7caSAdrian Chadd
2477692ad7caSAdrian Chadd /* Check that the error log address is valid. */
2478692ad7caSAdrian Chadd if (sc->errptr < WPI_FW_DATA_BASE ||
2479692ad7caSAdrian Chadd sc->errptr + sizeof (dump) >
2480692ad7caSAdrian Chadd WPI_FW_DATA_BASE + WPI_FW_DATA_MAXSZ) {
2481692ad7caSAdrian Chadd printf("%s: bad firmware error log address 0x%08x\n", __func__,
2482692ad7caSAdrian Chadd sc->errptr);
2483692ad7caSAdrian Chadd return;
2484692ad7caSAdrian Chadd }
2485692ad7caSAdrian Chadd if (wpi_nic_lock(sc) != 0) {
2486692ad7caSAdrian Chadd printf("%s: could not read firmware error log\n", __func__);
2487692ad7caSAdrian Chadd return;
2488692ad7caSAdrian Chadd }
2489692ad7caSAdrian Chadd /* Read number of entries in the log. */
2490692ad7caSAdrian Chadd count = wpi_mem_read(sc, sc->errptr);
2491692ad7caSAdrian Chadd if (count == 0 || count * sizeof (dump) > WPI_FW_DATA_MAXSZ) {
2492692ad7caSAdrian Chadd printf("%s: invalid count field (count = %u)\n", __func__,
2493692ad7caSAdrian Chadd count);
2494692ad7caSAdrian Chadd wpi_nic_unlock(sc);
2495692ad7caSAdrian Chadd return;
2496692ad7caSAdrian Chadd }
2497692ad7caSAdrian Chadd /* Skip "count" field. */
2498692ad7caSAdrian Chadd offset = sc->errptr + sizeof (uint32_t);
2499692ad7caSAdrian Chadd printf("firmware error log (count = %u):\n", count);
2500692ad7caSAdrian Chadd for (i = 0; i < count; i++) {
2501692ad7caSAdrian Chadd wpi_mem_read_region_4(sc, offset, (uint32_t *)&dump,
2502692ad7caSAdrian Chadd sizeof (dump) / sizeof (uint32_t));
2503692ad7caSAdrian Chadd
2504692ad7caSAdrian Chadd printf(" error type = \"%s\" (0x%08X)\n",
250512e0ff3bSAdrian Chadd (dump.desc < nitems(wpi_fw_errmsg)) ?
2506692ad7caSAdrian Chadd wpi_fw_errmsg[dump.desc] : "UNKNOWN",
2507692ad7caSAdrian Chadd dump.desc);
2508692ad7caSAdrian Chadd printf(" error data = 0x%08X\n",
2509692ad7caSAdrian Chadd dump.data);
2510692ad7caSAdrian Chadd printf(" branch link = 0x%08X%08X\n",
2511692ad7caSAdrian Chadd dump.blink[0], dump.blink[1]);
2512692ad7caSAdrian Chadd printf(" interrupt link = 0x%08X%08X\n",
2513692ad7caSAdrian Chadd dump.ilink[0], dump.ilink[1]);
2514692ad7caSAdrian Chadd printf(" time = %u\n", dump.time);
2515692ad7caSAdrian Chadd
2516692ad7caSAdrian Chadd offset += sizeof (dump);
2517692ad7caSAdrian Chadd }
2518692ad7caSAdrian Chadd wpi_nic_unlock(sc);
2519692ad7caSAdrian Chadd /* Dump driver status (TX and RX rings) while we're here. */
2520692ad7caSAdrian Chadd printf("driver status:\n");
252163f57699SAdrian Chadd WPI_TXQ_LOCK(sc);
2522ebf62e08SAdrian Chadd for (i = 0; i < WPI_DRV_NTXQUEUES; i++) {
2523692ad7caSAdrian Chadd struct wpi_tx_ring *ring = &sc->txq[i];
2524692ad7caSAdrian Chadd printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2525692ad7caSAdrian Chadd i, ring->qid, ring->cur, ring->queued);
2526692ad7caSAdrian Chadd }
252763f57699SAdrian Chadd WPI_TXQ_UNLOCK(sc);
2528692ad7caSAdrian Chadd printf(" rx ring: cur=%d\n", sc->rxq.cur);
25296607310bSBenjamin Close }
25306607310bSBenjamin Close
25316607310bSBenjamin Close static void
wpi_intr(void * arg)25326607310bSBenjamin Close wpi_intr(void *arg)
25336607310bSBenjamin Close {
25346607310bSBenjamin Close struct wpi_softc *sc = arg;
2535692ad7caSAdrian Chadd uint32_t r1, r2;
25366607310bSBenjamin Close
25376607310bSBenjamin Close WPI_LOCK(sc);
25386607310bSBenjamin Close
2539692ad7caSAdrian Chadd /* Disable interrupts. */
2540692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT_MASK, 0);
2541692ad7caSAdrian Chadd
2542692ad7caSAdrian Chadd r1 = WPI_READ(sc, WPI_INT);
2543692ad7caSAdrian Chadd
2544859f6ddcSAdrian Chadd if (__predict_false(r1 == 0xffffffff ||
2545859f6ddcSAdrian Chadd (r1 & 0xfffffff0) == 0xa5a5a5a0))
25467f1299c5SAdrian Chadd goto end; /* Hardware gone! */
25476607310bSBenjamin Close
2548692ad7caSAdrian Chadd r2 = WPI_READ(sc, WPI_FH_INT);
25496607310bSBenjamin Close
2550692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_INTR, "%s: reg1=0x%08x reg2=0x%08x\n", __func__,
2551692ad7caSAdrian Chadd r1, r2);
2552692ad7caSAdrian Chadd
2553692ad7caSAdrian Chadd if (r1 == 0 && r2 == 0)
2554692ad7caSAdrian Chadd goto done; /* Interrupt not for us. */
2555692ad7caSAdrian Chadd
2556692ad7caSAdrian Chadd /* Acknowledge interrupts. */
2557692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT, r1);
2558692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_INT, r2);
2559692ad7caSAdrian Chadd
2560859f6ddcSAdrian Chadd if (__predict_false(r1 & (WPI_INT_SW_ERR | WPI_INT_HW_ERR))) {
2561abd89aa2SAndriy Voskoboinyk struct ieee80211com *ic = &sc->sc_ic;
2562abd89aa2SAndriy Voskoboinyk
25636607310bSBenjamin Close device_printf(sc->sc_dev, "fatal firmware error\n");
2564a6df6cfeSAdrian Chadd #ifdef WPI_DEBUG
2565a6df6cfeSAdrian Chadd wpi_debug_registers(sc);
2566a6df6cfeSAdrian Chadd #endif
2567692ad7caSAdrian Chadd wpi_fatal_intr(sc);
2568692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_HW,
2569692ad7caSAdrian Chadd "(%s)\n", (r1 & WPI_INT_SW_ERR) ? "(Software Error)" :
2570692ad7caSAdrian Chadd "(Hardware Error)");
2571abd89aa2SAndriy Voskoboinyk ieee80211_restart_all(ic);
25727f1299c5SAdrian Chadd goto end;
25736607310bSBenjamin Close }
25746607310bSBenjamin Close
2575692ad7caSAdrian Chadd if ((r1 & (WPI_INT_FH_RX | WPI_INT_SW_RX)) ||
2576692ad7caSAdrian Chadd (r2 & WPI_FH_INT_RX))
25776607310bSBenjamin Close wpi_notif_intr(sc);
25786607310bSBenjamin Close
2579692ad7caSAdrian Chadd if (r1 & WPI_INT_ALIVE)
2580692ad7caSAdrian Chadd wakeup(sc); /* Firmware is alive. */
25816607310bSBenjamin Close
2582692ad7caSAdrian Chadd if (r1 & WPI_INT_WAKEUP)
2583692ad7caSAdrian Chadd wpi_wakeup_intr(sc);
2584692ad7caSAdrian Chadd
2585692ad7caSAdrian Chadd done:
2586692ad7caSAdrian Chadd /* Re-enable interrupts. */
2587859f6ddcSAdrian Chadd if (__predict_true(sc->sc_running))
2588692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
25896607310bSBenjamin Close
25907f1299c5SAdrian Chadd end: WPI_UNLOCK(sc);
25916607310bSBenjamin Close }
25926607310bSBenjamin Close
2593940aa808SAdrian Chadd static void
wpi_free_txfrags(struct wpi_softc * sc,uint16_t ac)2594940aa808SAdrian Chadd wpi_free_txfrags(struct wpi_softc *sc, uint16_t ac)
2595940aa808SAdrian Chadd {
2596940aa808SAdrian Chadd struct wpi_tx_ring *ring;
2597940aa808SAdrian Chadd struct wpi_tx_data *data;
2598940aa808SAdrian Chadd uint8_t cur;
2599940aa808SAdrian Chadd
2600940aa808SAdrian Chadd WPI_TXQ_LOCK(sc);
2601940aa808SAdrian Chadd ring = &sc->txq[ac];
2602940aa808SAdrian Chadd
2603940aa808SAdrian Chadd while (ring->pending != 0) {
2604940aa808SAdrian Chadd ring->pending--;
2605940aa808SAdrian Chadd cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT;
2606940aa808SAdrian Chadd data = &ring->data[cur];
2607940aa808SAdrian Chadd
2608940aa808SAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map,
2609940aa808SAdrian Chadd BUS_DMASYNC_POSTWRITE);
2610940aa808SAdrian Chadd bus_dmamap_unload(ring->data_dmat, data->map);
2611940aa808SAdrian Chadd m_freem(data->m);
2612940aa808SAdrian Chadd data->m = NULL;
2613940aa808SAdrian Chadd
2614940aa808SAdrian Chadd ieee80211_node_decref(data->ni);
2615940aa808SAdrian Chadd data->ni = NULL;
2616940aa808SAdrian Chadd }
2617940aa808SAdrian Chadd
2618940aa808SAdrian Chadd WPI_TXQ_UNLOCK(sc);
2619940aa808SAdrian Chadd }
2620940aa808SAdrian Chadd
26216607310bSBenjamin Close static int
wpi_cmd2(struct wpi_softc * sc,struct wpi_buf * buf)2622692ad7caSAdrian Chadd wpi_cmd2(struct wpi_softc *sc, struct wpi_buf *buf)
26236607310bSBenjamin Close {
26246607310bSBenjamin Close struct ieee80211_frame *wh;
2625692ad7caSAdrian Chadd struct wpi_tx_cmd *cmd;
2626692ad7caSAdrian Chadd struct wpi_tx_data *data;
2627692ad7caSAdrian Chadd struct wpi_tx_desc *desc;
2628692ad7caSAdrian Chadd struct wpi_tx_ring *ring;
2629692ad7caSAdrian Chadd struct mbuf *m1;
2630692ad7caSAdrian Chadd bus_dma_segment_t *seg, segs[WPI_MAX_SCATTER];
2631940aa808SAdrian Chadd uint8_t cur, pad;
2632525a3d47SAdrian Chadd uint16_t hdrlen;
2633940aa808SAdrian Chadd int error, i, nsegs, totlen, frag;
26346607310bSBenjamin Close
263563f57699SAdrian Chadd WPI_TXQ_LOCK(sc);
263663f57699SAdrian Chadd
2637565248e2SAdrian Chadd KASSERT(buf->size <= sizeof(buf->data), ("buffer overflow"));
2638565248e2SAdrian Chadd
2639692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
2640692ad7caSAdrian Chadd
2641859f6ddcSAdrian Chadd if (__predict_false(sc->sc_running == 0)) {
264263f57699SAdrian Chadd /* wpi_stop() was called */
264363f57699SAdrian Chadd error = ENETDOWN;
2644823e17dbSAdrian Chadd goto end;
264563f57699SAdrian Chadd }
264663f57699SAdrian Chadd
2647692ad7caSAdrian Chadd wh = mtod(buf->m, struct ieee80211_frame *);
26488a28176aSAdrian Chadd hdrlen = ieee80211_anyhdrsize(wh);
2649692ad7caSAdrian Chadd totlen = buf->m->m_pkthdr.len;
2650940aa808SAdrian Chadd frag = ((buf->m->m_flags & (M_FRAG | M_LASTFRAG)) == M_FRAG);
2651692ad7caSAdrian Chadd
2652fbcf1662SAdrian Chadd if (__predict_false(totlen < sizeof(struct ieee80211_frame_min))) {
2653fbcf1662SAdrian Chadd error = EINVAL;
2654823e17dbSAdrian Chadd goto end;
2655fbcf1662SAdrian Chadd }
2656fbcf1662SAdrian Chadd
26578a28176aSAdrian Chadd if (hdrlen & 3) {
26588a28176aSAdrian Chadd /* First segment length must be a multiple of 4. */
26598a28176aSAdrian Chadd pad = 4 - (hdrlen & 3);
26608a28176aSAdrian Chadd } else
26618a28176aSAdrian Chadd pad = 0;
26628a28176aSAdrian Chadd
2663692ad7caSAdrian Chadd ring = &sc->txq[buf->ac];
2664940aa808SAdrian Chadd cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT;
2665940aa808SAdrian Chadd desc = &ring->desc[cur];
2666940aa808SAdrian Chadd data = &ring->data[cur];
26676607310bSBenjamin Close
2668692ad7caSAdrian Chadd /* Prepare TX firmware command. */
2669940aa808SAdrian Chadd cmd = &ring->cmd[cur];
2670692ad7caSAdrian Chadd cmd->code = buf->code;
26716607310bSBenjamin Close cmd->flags = 0;
26726607310bSBenjamin Close cmd->qid = ring->qid;
2673940aa808SAdrian Chadd cmd->idx = cur;
26746607310bSBenjamin Close
2675692ad7caSAdrian Chadd memcpy(cmd->data, buf->data, buf->size);
26766607310bSBenjamin Close
2677692ad7caSAdrian Chadd /* Save and trim IEEE802.11 header. */
26788a28176aSAdrian Chadd memcpy((uint8_t *)(cmd->data + buf->size), wh, hdrlen);
26798a28176aSAdrian Chadd m_adj(buf->m, hdrlen);
26806607310bSBenjamin Close
2681692ad7caSAdrian Chadd error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, buf->m,
2682692ad7caSAdrian Chadd segs, &nsegs, BUS_DMA_NOWAIT);
26836607310bSBenjamin Close if (error != 0 && error != EFBIG) {
2684692ad7caSAdrian Chadd device_printf(sc->sc_dev,
2685692ad7caSAdrian Chadd "%s: can't map mbuf (error %d)\n", __func__, error);
2686823e17dbSAdrian Chadd goto end;
26876607310bSBenjamin Close }
26886607310bSBenjamin Close if (error != 0) {
2689692ad7caSAdrian Chadd /* Too many DMA segments, linearize mbuf. */
2690893edbeaSAdrian Chadd m1 = m_collapse(buf->m, M_NOWAIT, WPI_MAX_SCATTER - 1);
2691692ad7caSAdrian Chadd if (m1 == NULL) {
26926607310bSBenjamin Close device_printf(sc->sc_dev,
2693692ad7caSAdrian Chadd "%s: could not defrag mbuf\n", __func__);
269463f57699SAdrian Chadd error = ENOBUFS;
2695823e17dbSAdrian Chadd goto end;
26966607310bSBenjamin Close }
2697692ad7caSAdrian Chadd buf->m = m1;
26986607310bSBenjamin Close
26996607310bSBenjamin Close error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
2700692ad7caSAdrian Chadd buf->m, segs, &nsegs, BUS_DMA_NOWAIT);
2701859f6ddcSAdrian Chadd if (__predict_false(error != 0)) {
2702823e17dbSAdrian Chadd /* XXX fix this (applicable to the iwn(4) too) */
2703823e17dbSAdrian Chadd /*
2704823e17dbSAdrian Chadd * NB: Do not return error;
2705823e17dbSAdrian Chadd * original mbuf does not exist anymore.
2706823e17dbSAdrian Chadd */
27076607310bSBenjamin Close device_printf(sc->sc_dev,
27087167f5fcSAdrian Chadd "%s: can't map mbuf (error %d)\n", __func__,
27097167f5fcSAdrian Chadd error);
2710823e17dbSAdrian Chadd if (ring->qid < WPI_CMD_QUEUE_NUM) {
2711823e17dbSAdrian Chadd if_inc_counter(buf->ni->ni_vap->iv_ifp,
2712823e17dbSAdrian Chadd IFCOUNTER_OERRORS, 1);
2713940aa808SAdrian Chadd if (!frag)
2714823e17dbSAdrian Chadd ieee80211_free_node(buf->ni);
2715823e17dbSAdrian Chadd }
2716823e17dbSAdrian Chadd m_freem(buf->m);
2717823e17dbSAdrian Chadd error = 0;
2718823e17dbSAdrian Chadd goto end;
27196607310bSBenjamin Close }
27206607310bSBenjamin Close }
27216607310bSBenjamin Close
2722893edbeaSAdrian Chadd KASSERT(nsegs < WPI_MAX_SCATTER,
2723893edbeaSAdrian Chadd ("too many DMA segments, nsegs (%d) should be less than %d",
2724893edbeaSAdrian Chadd nsegs, WPI_MAX_SCATTER));
2725893edbeaSAdrian Chadd
2726692ad7caSAdrian Chadd data->m = buf->m;
2727692ad7caSAdrian Chadd data->ni = buf->ni;
27283f3d4d61SAndriy Voskoboinyk data->hdrlen = hdrlen;
27296607310bSBenjamin Close
2730692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
2731940aa808SAdrian Chadd __func__, ring->qid, cur, totlen, nsegs);
27326607310bSBenjamin Close
2733692ad7caSAdrian Chadd /* Fill TX descriptor. */
27348a28176aSAdrian Chadd desc->nsegs = WPI_PAD32(totlen + pad) << 4 | (1 + nsegs);
2735692ad7caSAdrian Chadd /* First DMA segment is used by the TX command. */
2736692ad7caSAdrian Chadd desc->segs[0].addr = htole32(data->cmd_paddr);
27378a28176aSAdrian Chadd desc->segs[0].len = htole32(4 + buf->size + hdrlen + pad);
2738692ad7caSAdrian Chadd /* Other DMA segments are for data payload. */
2739692ad7caSAdrian Chadd seg = &segs[0];
27406607310bSBenjamin Close for (i = 1; i <= nsegs; i++) {
2741692ad7caSAdrian Chadd desc->segs[i].addr = htole32(seg->ds_addr);
2742692ad7caSAdrian Chadd desc->segs[i].len = htole32(seg->ds_len);
2743692ad7caSAdrian Chadd seg++;
27446607310bSBenjamin Close }
27456607310bSBenjamin Close
27466607310bSBenjamin Close bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
2747692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
2748692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
27496607310bSBenjamin Close bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
27506607310bSBenjamin Close BUS_DMASYNC_PREWRITE);
27516607310bSBenjamin Close
2752940aa808SAdrian Chadd ring->pending += 1;
2753692ad7caSAdrian Chadd
2754940aa808SAdrian Chadd if (!frag) {
275545ac43f3SAdrian Chadd if (ring->qid < WPI_CMD_QUEUE_NUM) {
2756fc5870b4SAdrian Chadd WPI_TXQ_STATE_LOCK(sc);
2757940aa808SAdrian Chadd ring->queued += ring->pending;
2758940aa808SAdrian Chadd callout_reset(&sc->tx_timeout, 5*hz, wpi_tx_timeout,
2759940aa808SAdrian Chadd sc);
2760fc5870b4SAdrian Chadd WPI_TXQ_STATE_UNLOCK(sc);
276145ac43f3SAdrian Chadd }
276210550f61SAdrian Chadd
2763940aa808SAdrian Chadd /* Kick TX ring. */
2764940aa808SAdrian Chadd ring->cur = (ring->cur + ring->pending) % WPI_TX_RING_COUNT;
2765940aa808SAdrian Chadd ring->pending = 0;
2766940aa808SAdrian Chadd sc->sc_update_tx_ring(sc, ring);
2767940aa808SAdrian Chadd } else
2768f156cd89SBjoern A. Zeeb (void) ieee80211_ref_node(data->ni);
2769940aa808SAdrian Chadd
2770823e17dbSAdrian Chadd end: DPRINTF(sc, WPI_DEBUG_TRACE, error ? TRACE_STR_END_ERR : TRACE_STR_END,
2771823e17dbSAdrian Chadd __func__);
2772692ad7caSAdrian Chadd
277363f57699SAdrian Chadd WPI_TXQ_UNLOCK(sc);
277463f57699SAdrian Chadd
2775823e17dbSAdrian Chadd return (error);
2776692ad7caSAdrian Chadd }
2777692ad7caSAdrian Chadd
2778692ad7caSAdrian Chadd /*
2779692ad7caSAdrian Chadd * Construct the data packet for a transmit buffer.
2780692ad7caSAdrian Chadd */
2781692ad7caSAdrian Chadd static int
wpi_tx_data(struct wpi_softc * sc,struct mbuf * m,struct ieee80211_node * ni)2782692ad7caSAdrian Chadd wpi_tx_data(struct wpi_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2783692ad7caSAdrian Chadd {
2784f6313575SAndriy Voskoboinyk const struct ieee80211_txparam *tp = ni->ni_txparms;
2785692ad7caSAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
2786692ad7caSAdrian Chadd struct ieee80211com *ic = ni->ni_ic;
2787a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
2788692ad7caSAdrian Chadd struct ieee80211_frame *wh;
2789692ad7caSAdrian Chadd struct ieee80211_key *k = NULL;
2790692ad7caSAdrian Chadd struct wpi_buf tx_data;
2791565248e2SAdrian Chadd struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2792692ad7caSAdrian Chadd uint32_t flags;
2793525a3d47SAdrian Chadd uint16_t ac, qos;
2794525a3d47SAdrian Chadd uint8_t tid, type, rate;
2795823e17dbSAdrian Chadd int swcrypt, ismcast, totlen;
2796692ad7caSAdrian Chadd
2797692ad7caSAdrian Chadd wh = mtod(m, struct ieee80211_frame *);
2798692ad7caSAdrian Chadd type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2799692ad7caSAdrian Chadd ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
280019ea23a0SAdrian Chadd swcrypt = 1;
2801692ad7caSAdrian Chadd
2802692ad7caSAdrian Chadd /* Select EDCA Access Category and TX ring for this frame. */
2803692ad7caSAdrian Chadd if (IEEE80211_QOS_HAS_SEQ(wh)) {
2804692ad7caSAdrian Chadd qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2805692ad7caSAdrian Chadd tid = qos & IEEE80211_QOS_TID;
2806692ad7caSAdrian Chadd } else {
2807692ad7caSAdrian Chadd qos = 0;
2808692ad7caSAdrian Chadd tid = 0;
2809692ad7caSAdrian Chadd }
2810692ad7caSAdrian Chadd ac = M_WME_GETAC(m);
2811692ad7caSAdrian Chadd
2812692ad7caSAdrian Chadd /* Choose a TX rate index. */
2813f6313575SAndriy Voskoboinyk if (type == IEEE80211_FC0_TYPE_MGT ||
2814f6313575SAndriy Voskoboinyk type == IEEE80211_FC0_TYPE_CTL ||
2815f6313575SAndriy Voskoboinyk (m->m_flags & M_EAPOL) != 0)
2816692ad7caSAdrian Chadd rate = tp->mgmtrate;
2817692ad7caSAdrian Chadd else if (ismcast)
2818692ad7caSAdrian Chadd rate = tp->mcastrate;
2819692ad7caSAdrian Chadd else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2820692ad7caSAdrian Chadd rate = tp->ucastrate;
2821692ad7caSAdrian Chadd else {
2822692ad7caSAdrian Chadd /* XXX pass pktlen */
2823692ad7caSAdrian Chadd (void) ieee80211_ratectl_rate(ni, NULL, 0);
2824*70674500SAdrian Chadd rate = ieee80211_node_get_txrate_dot11rate(ni);
2825692ad7caSAdrian Chadd }
2826692ad7caSAdrian Chadd
2827692ad7caSAdrian Chadd /* Encrypt the frame if need be. */
2828692ad7caSAdrian Chadd if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2829692ad7caSAdrian Chadd /* Retrieve key for TX. */
2830692ad7caSAdrian Chadd k = ieee80211_crypto_encap(ni, m);
2831823e17dbSAdrian Chadd if (k == NULL)
2832823e17dbSAdrian Chadd return (ENOBUFS);
2833823e17dbSAdrian Chadd
2834f15520d9SAdrian Chadd swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2835f15520d9SAdrian Chadd
2836692ad7caSAdrian Chadd /* 802.11 header may have moved. */
2837692ad7caSAdrian Chadd wh = mtod(m, struct ieee80211_frame *);
2838692ad7caSAdrian Chadd }
28398a28176aSAdrian Chadd totlen = m->m_pkthdr.len;
2840692ad7caSAdrian Chadd
2841692ad7caSAdrian Chadd if (ieee80211_radiotap_active_vap(vap)) {
2842692ad7caSAdrian Chadd struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
2843692ad7caSAdrian Chadd
28448a28176aSAdrian Chadd tap->wt_flags = 0;
2845692ad7caSAdrian Chadd tap->wt_rate = rate;
2846692ad7caSAdrian Chadd if (k != NULL)
2847692ad7caSAdrian Chadd tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2848940aa808SAdrian Chadd if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2849940aa808SAdrian Chadd tap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2850692ad7caSAdrian Chadd
2851692ad7caSAdrian Chadd ieee80211_radiotap_tx(vap, m);
2852692ad7caSAdrian Chadd }
2853692ad7caSAdrian Chadd
2854692ad7caSAdrian Chadd flags = 0;
2855692ad7caSAdrian Chadd if (!ismcast) {
2856692ad7caSAdrian Chadd /* Unicast frame, check if an ACK is expected. */
2857692ad7caSAdrian Chadd if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2858692ad7caSAdrian Chadd IEEE80211_QOS_ACKPOLICY_NOACK)
2859692ad7caSAdrian Chadd flags |= WPI_TX_NEED_ACK;
2860692ad7caSAdrian Chadd }
2861692ad7caSAdrian Chadd
28629b7cc717SAdrian Chadd if (!IEEE80211_QOS_HAS_SEQ(wh))
28639b7cc717SAdrian Chadd flags |= WPI_TX_AUTO_SEQ;
2864fae61c69SAdrian Chadd if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2865940aa808SAdrian Chadd flags |= WPI_TX_MORE_FRAG;
2866fae61c69SAdrian Chadd
2867692ad7caSAdrian Chadd /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2868692ad7caSAdrian Chadd if (!ismcast) {
2869692ad7caSAdrian Chadd /* NB: Group frames are sent using CCK in 802.11b/g. */
2870692ad7caSAdrian Chadd if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2871692ad7caSAdrian Chadd flags |= WPI_TX_NEED_RTS;
2872692ad7caSAdrian Chadd } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2873692ad7caSAdrian Chadd WPI_RATE_IS_OFDM(rate)) {
2874692ad7caSAdrian Chadd if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2875692ad7caSAdrian Chadd flags |= WPI_TX_NEED_CTS;
2876692ad7caSAdrian Chadd else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2877692ad7caSAdrian Chadd flags |= WPI_TX_NEED_RTS;
2878692ad7caSAdrian Chadd }
2879692ad7caSAdrian Chadd
2880692ad7caSAdrian Chadd if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2881692ad7caSAdrian Chadd flags |= WPI_TX_FULL_TXOP;
2882692ad7caSAdrian Chadd }
2883692ad7caSAdrian Chadd
2884565248e2SAdrian Chadd memset(tx, 0, sizeof (struct wpi_cmd_data));
2885692ad7caSAdrian Chadd if (type == IEEE80211_FC0_TYPE_MGT) {
2886692ad7caSAdrian Chadd uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2887692ad7caSAdrian Chadd
2888692ad7caSAdrian Chadd /* Tell HW to set timestamp in probe responses. */
2889692ad7caSAdrian Chadd if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2890692ad7caSAdrian Chadd flags |= WPI_TX_INSERT_TSTAMP;
2891692ad7caSAdrian Chadd if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2892692ad7caSAdrian Chadd subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2893565248e2SAdrian Chadd tx->timeout = htole16(3);
2894692ad7caSAdrian Chadd else
2895565248e2SAdrian Chadd tx->timeout = htole16(2);
2896692ad7caSAdrian Chadd }
2897692ad7caSAdrian Chadd
2898692ad7caSAdrian Chadd if (ismcast || type != IEEE80211_FC0_TYPE_DATA)
2899565248e2SAdrian Chadd tx->id = WPI_ID_BROADCAST;
2900692ad7caSAdrian Chadd else {
2901692ad7caSAdrian Chadd if (wn->id == WPI_ID_UNDEFINED) {
2902692ad7caSAdrian Chadd device_printf(sc->sc_dev,
2903692ad7caSAdrian Chadd "%s: undefined node id\n", __func__);
2904823e17dbSAdrian Chadd return (EINVAL);
2905692ad7caSAdrian Chadd }
2906692ad7caSAdrian Chadd
2907565248e2SAdrian Chadd tx->id = wn->id;
2908692ad7caSAdrian Chadd }
2909692ad7caSAdrian Chadd
291019ea23a0SAdrian Chadd if (!swcrypt) {
2911f15520d9SAdrian Chadd switch (k->wk_cipher->ic_cipher) {
2912f15520d9SAdrian Chadd case IEEE80211_CIPHER_AES_CCM:
2913565248e2SAdrian Chadd tx->security = WPI_CIPHER_CCMP;
2914f15520d9SAdrian Chadd break;
2915f15520d9SAdrian Chadd
2916f15520d9SAdrian Chadd default:
2917f15520d9SAdrian Chadd break;
2918f15520d9SAdrian Chadd }
2919f15520d9SAdrian Chadd
2920565248e2SAdrian Chadd memcpy(tx->key, k->wk_key, k->wk_keylen);
2921f15520d9SAdrian Chadd }
2922f15520d9SAdrian Chadd
2923940aa808SAdrian Chadd if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
2924940aa808SAdrian Chadd struct mbuf *next = m->m_nextpkt;
2925940aa808SAdrian Chadd
2926940aa808SAdrian Chadd tx->lnext = htole16(next->m_pkthdr.len);
2927940aa808SAdrian Chadd tx->fnext = htole32(tx->security |
2928940aa808SAdrian Chadd (flags & WPI_TX_NEED_ACK) |
2929940aa808SAdrian Chadd WPI_NEXT_STA_ID(tx->id));
2930940aa808SAdrian Chadd }
2931940aa808SAdrian Chadd
2932565248e2SAdrian Chadd tx->len = htole16(totlen);
2933565248e2SAdrian Chadd tx->flags = htole32(flags);
2934565248e2SAdrian Chadd tx->plcp = rate2plcp(rate);
2935565248e2SAdrian Chadd tx->tid = tid;
2936565248e2SAdrian Chadd tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
2937565248e2SAdrian Chadd tx->ofdm_mask = 0xff;
2938565248e2SAdrian Chadd tx->cck_mask = 0x0f;
2939565248e2SAdrian Chadd tx->rts_ntries = 7;
2940b625a56aSAdrian Chadd tx->data_ntries = tp->maxretry;
2941692ad7caSAdrian Chadd
2942692ad7caSAdrian Chadd tx_data.ni = ni;
2943692ad7caSAdrian Chadd tx_data.m = m;
2944565248e2SAdrian Chadd tx_data.size = sizeof(struct wpi_cmd_data);
2945692ad7caSAdrian Chadd tx_data.code = WPI_CMD_TX_DATA;
2946692ad7caSAdrian Chadd tx_data.ac = ac;
2947692ad7caSAdrian Chadd
2948692ad7caSAdrian Chadd return wpi_cmd2(sc, &tx_data);
2949692ad7caSAdrian Chadd }
2950692ad7caSAdrian Chadd
2951692ad7caSAdrian Chadd static int
wpi_tx_data_raw(struct wpi_softc * sc,struct mbuf * m,struct ieee80211_node * ni,const struct ieee80211_bpf_params * params)29527167f5fcSAdrian Chadd wpi_tx_data_raw(struct wpi_softc *sc, struct mbuf *m,
29537167f5fcSAdrian Chadd struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
2954692ad7caSAdrian Chadd {
2955692ad7caSAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
2956735c6067SAdrian Chadd struct ieee80211_key *k = NULL;
2957692ad7caSAdrian Chadd struct ieee80211_frame *wh;
2958692ad7caSAdrian Chadd struct wpi_buf tx_data;
2959565248e2SAdrian Chadd struct wpi_cmd_data *tx = (struct wpi_cmd_data *)&tx_data.data;
2960692ad7caSAdrian Chadd uint32_t flags;
2961525a3d47SAdrian Chadd uint8_t ac, type, rate;
2962525a3d47SAdrian Chadd int swcrypt, totlen;
2963692ad7caSAdrian Chadd
2964692ad7caSAdrian Chadd wh = mtod(m, struct ieee80211_frame *);
2965692ad7caSAdrian Chadd type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
296619ea23a0SAdrian Chadd swcrypt = 1;
2967692ad7caSAdrian Chadd
2968692ad7caSAdrian Chadd ac = params->ibp_pri & 3;
2969692ad7caSAdrian Chadd
2970692ad7caSAdrian Chadd /* Choose a TX rate index. */
2971692ad7caSAdrian Chadd rate = params->ibp_rate0;
2972692ad7caSAdrian Chadd
2973692ad7caSAdrian Chadd flags = 0;
29749b7cc717SAdrian Chadd if (!IEEE80211_QOS_HAS_SEQ(wh))
29759b7cc717SAdrian Chadd flags |= WPI_TX_AUTO_SEQ;
2976692ad7caSAdrian Chadd if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
2977692ad7caSAdrian Chadd flags |= WPI_TX_NEED_ACK;
2978692ad7caSAdrian Chadd if (params->ibp_flags & IEEE80211_BPF_RTS)
2979692ad7caSAdrian Chadd flags |= WPI_TX_NEED_RTS;
2980692ad7caSAdrian Chadd if (params->ibp_flags & IEEE80211_BPF_CTS)
2981692ad7caSAdrian Chadd flags |= WPI_TX_NEED_CTS;
2982692ad7caSAdrian Chadd if (flags & (WPI_TX_NEED_RTS | WPI_TX_NEED_CTS))
2983692ad7caSAdrian Chadd flags |= WPI_TX_FULL_TXOP;
2984692ad7caSAdrian Chadd
2985735c6067SAdrian Chadd /* Encrypt the frame if need be. */
2986735c6067SAdrian Chadd if (params->ibp_flags & IEEE80211_BPF_CRYPTO) {
2987735c6067SAdrian Chadd /* Retrieve key for TX. */
2988735c6067SAdrian Chadd k = ieee80211_crypto_encap(ni, m);
2989823e17dbSAdrian Chadd if (k == NULL)
2990823e17dbSAdrian Chadd return (ENOBUFS);
2991823e17dbSAdrian Chadd
2992735c6067SAdrian Chadd swcrypt = k->wk_flags & IEEE80211_KEY_SWCRYPT;
2993735c6067SAdrian Chadd
2994735c6067SAdrian Chadd /* 802.11 header may have moved. */
2995735c6067SAdrian Chadd wh = mtod(m, struct ieee80211_frame *);
2996735c6067SAdrian Chadd }
2997735c6067SAdrian Chadd totlen = m->m_pkthdr.len;
2998735c6067SAdrian Chadd
2999692ad7caSAdrian Chadd if (ieee80211_radiotap_active_vap(vap)) {
3000692ad7caSAdrian Chadd struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
3001692ad7caSAdrian Chadd
3002692ad7caSAdrian Chadd tap->wt_flags = 0;
3003692ad7caSAdrian Chadd tap->wt_rate = rate;
3004735c6067SAdrian Chadd if (params->ibp_flags & IEEE80211_BPF_CRYPTO)
3005735c6067SAdrian Chadd tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3006692ad7caSAdrian Chadd
3007692ad7caSAdrian Chadd ieee80211_radiotap_tx(vap, m);
3008692ad7caSAdrian Chadd }
3009692ad7caSAdrian Chadd
3010565248e2SAdrian Chadd memset(tx, 0, sizeof (struct wpi_cmd_data));
3011692ad7caSAdrian Chadd if (type == IEEE80211_FC0_TYPE_MGT) {
3012692ad7caSAdrian Chadd uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
3013692ad7caSAdrian Chadd
3014692ad7caSAdrian Chadd /* Tell HW to set timestamp in probe responses. */
3015692ad7caSAdrian Chadd if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
3016692ad7caSAdrian Chadd flags |= WPI_TX_INSERT_TSTAMP;
3017692ad7caSAdrian Chadd if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
3018692ad7caSAdrian Chadd subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
3019565248e2SAdrian Chadd tx->timeout = htole16(3);
3020692ad7caSAdrian Chadd else
3021565248e2SAdrian Chadd tx->timeout = htole16(2);
3022692ad7caSAdrian Chadd }
3023692ad7caSAdrian Chadd
302419ea23a0SAdrian Chadd if (!swcrypt) {
3025735c6067SAdrian Chadd switch (k->wk_cipher->ic_cipher) {
3026735c6067SAdrian Chadd case IEEE80211_CIPHER_AES_CCM:
3027735c6067SAdrian Chadd tx->security = WPI_CIPHER_CCMP;
3028735c6067SAdrian Chadd break;
3029735c6067SAdrian Chadd
3030735c6067SAdrian Chadd default:
3031735c6067SAdrian Chadd break;
3032735c6067SAdrian Chadd }
3033735c6067SAdrian Chadd
3034735c6067SAdrian Chadd memcpy(tx->key, k->wk_key, k->wk_keylen);
3035735c6067SAdrian Chadd }
3036735c6067SAdrian Chadd
3037565248e2SAdrian Chadd tx->len = htole16(totlen);
3038565248e2SAdrian Chadd tx->flags = htole32(flags);
3039565248e2SAdrian Chadd tx->plcp = rate2plcp(rate);
3040565248e2SAdrian Chadd tx->id = WPI_ID_BROADCAST;
3041565248e2SAdrian Chadd tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
3042565248e2SAdrian Chadd tx->rts_ntries = params->ibp_try1;
3043565248e2SAdrian Chadd tx->data_ntries = params->ibp_try0;
3044692ad7caSAdrian Chadd
3045692ad7caSAdrian Chadd tx_data.ni = ni;
3046692ad7caSAdrian Chadd tx_data.m = m;
3047565248e2SAdrian Chadd tx_data.size = sizeof(struct wpi_cmd_data);
3048692ad7caSAdrian Chadd tx_data.code = WPI_CMD_TX_DATA;
3049692ad7caSAdrian Chadd tx_data.ac = ac;
3050692ad7caSAdrian Chadd
3051692ad7caSAdrian Chadd return wpi_cmd2(sc, &tx_data);
3052692ad7caSAdrian Chadd }
3053692ad7caSAdrian Chadd
30547a79cebfSGleb Smirnoff static __inline int
wpi_tx_ring_free_space(struct wpi_softc * sc,uint16_t ac)3055940aa808SAdrian Chadd wpi_tx_ring_free_space(struct wpi_softc *sc, uint16_t ac)
30567a79cebfSGleb Smirnoff {
30577a79cebfSGleb Smirnoff struct wpi_tx_ring *ring = &sc->txq[ac];
30587a79cebfSGleb Smirnoff int retval;
30597a79cebfSGleb Smirnoff
30607a79cebfSGleb Smirnoff WPI_TXQ_STATE_LOCK(sc);
3061940aa808SAdrian Chadd retval = WPI_TX_RING_HIMARK - ring->queued;
30627a79cebfSGleb Smirnoff WPI_TXQ_STATE_UNLOCK(sc);
30637a79cebfSGleb Smirnoff
30647a79cebfSGleb Smirnoff return retval;
30657a79cebfSGleb Smirnoff }
30667a79cebfSGleb Smirnoff
3067692ad7caSAdrian Chadd static int
wpi_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)3068692ad7caSAdrian Chadd wpi_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3069692ad7caSAdrian Chadd const struct ieee80211_bpf_params *params)
3070692ad7caSAdrian Chadd {
3071692ad7caSAdrian Chadd struct ieee80211com *ic = ni->ni_ic;
30722cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
3073525a3d47SAdrian Chadd uint16_t ac;
3074525a3d47SAdrian Chadd int error = 0;
3075692ad7caSAdrian Chadd
3076692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3077692ad7caSAdrian Chadd
30787a79cebfSGleb Smirnoff ac = M_WME_GETAC(m);
307979d2c5e8SGleb Smirnoff
3080ba2c1fbcSAdrian Chadd WPI_TX_LOCK(sc);
30817a79cebfSGleb Smirnoff
3082940aa808SAdrian Chadd /* NB: no fragments here */
3083940aa808SAdrian Chadd if (sc->sc_running == 0 || wpi_tx_ring_free_space(sc, ac) < 1) {
30847a79cebfSGleb Smirnoff error = sc->sc_running ? ENOBUFS : ENETDOWN;
30857a79cebfSGleb Smirnoff goto unlock;
30867a79cebfSGleb Smirnoff }
30877a79cebfSGleb Smirnoff
3088692ad7caSAdrian Chadd if (params == NULL) {
3089692ad7caSAdrian Chadd /*
3090692ad7caSAdrian Chadd * Legacy path; interpret frame contents to decide
3091692ad7caSAdrian Chadd * precisely how to send the frame.
3092692ad7caSAdrian Chadd */
3093692ad7caSAdrian Chadd error = wpi_tx_data(sc, m, ni);
3094692ad7caSAdrian Chadd } else {
3095692ad7caSAdrian Chadd /*
3096692ad7caSAdrian Chadd * Caller supplied explicit parameters to use in
3097692ad7caSAdrian Chadd * sending the frame.
3098692ad7caSAdrian Chadd */
3099692ad7caSAdrian Chadd error = wpi_tx_data_raw(sc, m, ni, params);
3100692ad7caSAdrian Chadd }
31017a79cebfSGleb Smirnoff
31027a79cebfSGleb Smirnoff unlock: WPI_TX_UNLOCK(sc);
3103692ad7caSAdrian Chadd
3104692ad7caSAdrian Chadd if (error != 0) {
3105823e17dbSAdrian Chadd m_freem(m);
3106692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
3107692ad7caSAdrian Chadd
3108692ad7caSAdrian Chadd return error;
3109692ad7caSAdrian Chadd }
3110692ad7caSAdrian Chadd
3111692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
31126607310bSBenjamin Close
31136607310bSBenjamin Close return 0;
31146607310bSBenjamin Close }
31156607310bSBenjamin Close
31167a79cebfSGleb Smirnoff static int
wpi_transmit(struct ieee80211com * ic,struct mbuf * m)31177a79cebfSGleb Smirnoff wpi_transmit(struct ieee80211com *ic, struct mbuf *m)
311879d2c5e8SGleb Smirnoff {
31197a79cebfSGleb Smirnoff struct wpi_softc *sc = ic->ic_softc;
312079d2c5e8SGleb Smirnoff struct ieee80211_node *ni;
3121940aa808SAdrian Chadd struct mbuf *mnext;
3122525a3d47SAdrian Chadd uint16_t ac;
3123940aa808SAdrian Chadd int error, nmbufs;
312479d2c5e8SGleb Smirnoff
312579d2c5e8SGleb Smirnoff WPI_TX_LOCK(sc);
312679d2c5e8SGleb Smirnoff DPRINTF(sc, WPI_DEBUG_XMIT, "%s: called\n", __func__);
312779d2c5e8SGleb Smirnoff
31287a79cebfSGleb Smirnoff /* Check if interface is up & running. */
3129859f6ddcSAdrian Chadd if (__predict_false(sc->sc_running == 0)) {
31307a79cebfSGleb Smirnoff error = ENXIO;
31317a79cebfSGleb Smirnoff goto unlock;
3132ba2c1fbcSAdrian Chadd }
313379d2c5e8SGleb Smirnoff
3134940aa808SAdrian Chadd nmbufs = 1;
3135940aa808SAdrian Chadd for (mnext = m->m_nextpkt; mnext != NULL; mnext = mnext->m_nextpkt)
3136940aa808SAdrian Chadd nmbufs++;
3137940aa808SAdrian Chadd
31387a79cebfSGleb Smirnoff /* Check for available space. */
31397a79cebfSGleb Smirnoff ac = M_WME_GETAC(m);
3140940aa808SAdrian Chadd if (wpi_tx_ring_free_space(sc, ac) < nmbufs) {
3141753ca9efSAdrian Chadd error = ENOBUFS;
31427a79cebfSGleb Smirnoff goto unlock;
31437a79cebfSGleb Smirnoff }
31447a79cebfSGleb Smirnoff
31457a79cebfSGleb Smirnoff error = 0;
314679d2c5e8SGleb Smirnoff ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3147940aa808SAdrian Chadd do {
3148940aa808SAdrian Chadd mnext = m->m_nextpkt;
314979d2c5e8SGleb Smirnoff if (wpi_tx_data(sc, m, ni) != 0) {
3150940aa808SAdrian Chadd if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS,
3151940aa808SAdrian Chadd nmbufs);
3152940aa808SAdrian Chadd wpi_free_txfrags(sc, ac);
3153940aa808SAdrian Chadd ieee80211_free_mbuf(m);
3154f7cd3e18SAdrian Chadd ieee80211_free_node(ni);
3155940aa808SAdrian Chadd break;
31567a79cebfSGleb Smirnoff }
3157940aa808SAdrian Chadd } while((m = mnext) != NULL);
31587a79cebfSGleb Smirnoff
31597a79cebfSGleb Smirnoff DPRINTF(sc, WPI_DEBUG_XMIT, "%s: done\n", __func__);
31607a79cebfSGleb Smirnoff
31617a79cebfSGleb Smirnoff unlock: WPI_TX_UNLOCK(sc);
31627a79cebfSGleb Smirnoff
31637a79cebfSGleb Smirnoff return (error);
31647a79cebfSGleb Smirnoff }
31657a79cebfSGleb Smirnoff
31664eaf2ee3SAdrian Chadd static void
wpi_watchdog_rfkill(void * arg)3167692ad7caSAdrian Chadd wpi_watchdog_rfkill(void *arg)
3168692ad7caSAdrian Chadd {
3169692ad7caSAdrian Chadd struct wpi_softc *sc = arg;
31707a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
3171692ad7caSAdrian Chadd
3172692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_WATCHDOG, "RFkill Watchdog: tick\n");
3173692ad7caSAdrian Chadd
3174692ad7caSAdrian Chadd /* No need to lock firmware memory. */
3175692ad7caSAdrian Chadd if ((wpi_prph_read(sc, WPI_APMG_RFKILL) & 0x1) == 0) {
3176692ad7caSAdrian Chadd /* Radio kill switch is still off. */
3177692ad7caSAdrian Chadd callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
3178692ad7caSAdrian Chadd sc);
3179692ad7caSAdrian Chadd } else
3180692ad7caSAdrian Chadd ieee80211_runtask(ic, &sc->sc_radioon_task);
3181692ad7caSAdrian Chadd }
3182692ad7caSAdrian Chadd
31830bc39fc6SAdrian Chadd static void
wpi_scan_timeout(void * arg)31840bc39fc6SAdrian Chadd wpi_scan_timeout(void *arg)
31850bc39fc6SAdrian Chadd {
31860bc39fc6SAdrian Chadd struct wpi_softc *sc = arg;
31877a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
31880bc39fc6SAdrian Chadd
31897a79cebfSGleb Smirnoff ic_printf(ic, "scan timeout\n");
3190abd89aa2SAndriy Voskoboinyk ieee80211_restart_all(ic);
31910bc39fc6SAdrian Chadd }
31920bc39fc6SAdrian Chadd
3193692ad7caSAdrian Chadd static void
wpi_tx_timeout(void * arg)3194d74e544aSAdrian Chadd wpi_tx_timeout(void *arg)
3195692ad7caSAdrian Chadd {
3196692ad7caSAdrian Chadd struct wpi_softc *sc = arg;
31977a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
3198692ad7caSAdrian Chadd
31997a79cebfSGleb Smirnoff ic_printf(ic, "device timeout\n");
3200abd89aa2SAndriy Voskoboinyk ieee80211_restart_all(ic);
3201692ad7caSAdrian Chadd }
32026607310bSBenjamin Close
32037a79cebfSGleb Smirnoff static void
wpi_parent(struct ieee80211com * ic)32047a79cebfSGleb Smirnoff wpi_parent(struct ieee80211com *ic)
32056607310bSBenjamin Close {
32067a79cebfSGleb Smirnoff struct wpi_softc *sc = ic->ic_softc;
3207692ad7caSAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
32086607310bSBenjamin Close
32097a79cebfSGleb Smirnoff if (ic->ic_nrunning > 0) {
32107a79cebfSGleb Smirnoff if (wpi_init(sc) == 0) {
32117a79cebfSGleb Smirnoff ieee80211_notify_radio(ic, 1);
32127a79cebfSGleb Smirnoff ieee80211_start_all(ic);
32137a79cebfSGleb Smirnoff } else {
32147a79cebfSGleb Smirnoff ieee80211_notify_radio(ic, 0);
3215692ad7caSAdrian Chadd ieee80211_stop(vap);
3216ba2c1fbcSAdrian Chadd }
3217abd89aa2SAndriy Voskoboinyk } else {
3218abd89aa2SAndriy Voskoboinyk ieee80211_notify_radio(ic, 0);
32197a79cebfSGleb Smirnoff wpi_stop(sc);
32206607310bSBenjamin Close }
3221abd89aa2SAndriy Voskoboinyk }
32226607310bSBenjamin Close
32236607310bSBenjamin Close /*
32246607310bSBenjamin Close * Send a command to the firmware.
32256607310bSBenjamin Close */
32266607310bSBenjamin Close static int
wpi_cmd(struct wpi_softc * sc,uint8_t code,const void * buf,uint16_t size,int async)3227525a3d47SAdrian Chadd wpi_cmd(struct wpi_softc *sc, uint8_t code, const void *buf, uint16_t size,
3228692ad7caSAdrian Chadd int async)
32296607310bSBenjamin Close {
32308e4cf38aSAdrian Chadd struct wpi_tx_ring *ring = &sc->txq[WPI_CMD_QUEUE_NUM];
32316607310bSBenjamin Close struct wpi_tx_desc *desc;
3232692ad7caSAdrian Chadd struct wpi_tx_data *data;
32336607310bSBenjamin Close struct wpi_tx_cmd *cmd;
3234692ad7caSAdrian Chadd struct mbuf *m;
3235692ad7caSAdrian Chadd bus_addr_t paddr;
3236525a3d47SAdrian Chadd uint16_t totlen;
3237525a3d47SAdrian Chadd int error;
32386607310bSBenjamin Close
323963f57699SAdrian Chadd WPI_TXQ_LOCK(sc);
324063f57699SAdrian Chadd
3241692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3242692ad7caSAdrian Chadd
3243859f6ddcSAdrian Chadd if (__predict_false(sc->sc_running == 0)) {
324463f57699SAdrian Chadd /* wpi_stop() was called */
32457a79cebfSGleb Smirnoff if (code == WPI_CMD_SCAN)
32467a79cebfSGleb Smirnoff error = ENETDOWN;
32477a79cebfSGleb Smirnoff else
324863f57699SAdrian Chadd error = 0;
32497a79cebfSGleb Smirnoff
325063f57699SAdrian Chadd goto fail;
325163f57699SAdrian Chadd }
325263f57699SAdrian Chadd
3253692ad7caSAdrian Chadd if (async == 0)
32546607310bSBenjamin Close WPI_LOCK_ASSERT(sc);
32556607310bSBenjamin Close
3256525a3d47SAdrian Chadd DPRINTF(sc, WPI_DEBUG_CMD, "%s: cmd %s size %u async %d\n",
3257cec45f04SAdrian Chadd __func__, wpi_cmd_str(code), size, async);
32586607310bSBenjamin Close
32596607310bSBenjamin Close desc = &ring->desc[ring->cur];
3260692ad7caSAdrian Chadd data = &ring->data[ring->cur];
3261692ad7caSAdrian Chadd totlen = 4 + size;
3262692ad7caSAdrian Chadd
3263692ad7caSAdrian Chadd if (size > sizeof cmd->data) {
3264692ad7caSAdrian Chadd /* Command is too large to fit in a descriptor. */
326563f57699SAdrian Chadd if (totlen > MCLBYTES) {
326663f57699SAdrian Chadd error = EINVAL;
326763f57699SAdrian Chadd goto fail;
326863f57699SAdrian Chadd }
3269692ad7caSAdrian Chadd m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
327063f57699SAdrian Chadd if (m == NULL) {
327163f57699SAdrian Chadd error = ENOMEM;
327263f57699SAdrian Chadd goto fail;
327363f57699SAdrian Chadd }
3274692ad7caSAdrian Chadd cmd = mtod(m, struct wpi_tx_cmd *);
3275692ad7caSAdrian Chadd error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3276692ad7caSAdrian Chadd totlen, wpi_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3277692ad7caSAdrian Chadd if (error != 0) {
3278692ad7caSAdrian Chadd m_freem(m);
327963f57699SAdrian Chadd goto fail;
3280692ad7caSAdrian Chadd }
3281692ad7caSAdrian Chadd data->m = m;
3282692ad7caSAdrian Chadd } else {
32836607310bSBenjamin Close cmd = &ring->cmd[ring->cur];
3284692ad7caSAdrian Chadd paddr = data->cmd_paddr;
3285692ad7caSAdrian Chadd }
32866607310bSBenjamin Close
32876607310bSBenjamin Close cmd->code = code;
32886607310bSBenjamin Close cmd->flags = 0;
32896607310bSBenjamin Close cmd->qid = ring->qid;
32906607310bSBenjamin Close cmd->idx = ring->cur;
32916607310bSBenjamin Close memcpy(cmd->data, buf, size);
32926607310bSBenjamin Close
3293692ad7caSAdrian Chadd desc->nsegs = 1 + (WPI_PAD32(size) << 4);
3294692ad7caSAdrian Chadd desc->segs[0].addr = htole32(paddr);
3295692ad7caSAdrian Chadd desc->segs[0].len = htole32(totlen);
32966607310bSBenjamin Close
3297692ad7caSAdrian Chadd if (size > sizeof cmd->data) {
3298692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, data->map,
3299692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
3300692ad7caSAdrian Chadd } else {
3301692ad7caSAdrian Chadd bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3302692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
3303692ad7caSAdrian Chadd }
3304692ad7caSAdrian Chadd bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3305692ad7caSAdrian Chadd BUS_DMASYNC_PREWRITE);
3306692ad7caSAdrian Chadd
3307692ad7caSAdrian Chadd /* Kick command ring. */
3308692ad7caSAdrian Chadd ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
33095cacb17fSAdrian Chadd sc->sc_update_tx_ring(sc, ring);
3310692ad7caSAdrian Chadd
3311692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
33126607310bSBenjamin Close
331363f57699SAdrian Chadd WPI_TXQ_UNLOCK(sc);
331463f57699SAdrian Chadd
33157a79cebfSGleb Smirnoff return async ? 0 : mtx_sleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz);
331663f57699SAdrian Chadd
331763f57699SAdrian Chadd fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
331863f57699SAdrian Chadd
331963f57699SAdrian Chadd WPI_TXQ_UNLOCK(sc);
332063f57699SAdrian Chadd
332163f57699SAdrian Chadd return error;
33226607310bSBenjamin Close }
33236607310bSBenjamin Close
33246607310bSBenjamin Close /*
3325692ad7caSAdrian Chadd * Configure HW multi-rate retries.
33266607310bSBenjamin Close */
33276607310bSBenjamin Close static int
wpi_mrr_setup(struct wpi_softc * sc)33286607310bSBenjamin Close wpi_mrr_setup(struct wpi_softc *sc)
33296607310bSBenjamin Close {
33307a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
33316607310bSBenjamin Close struct wpi_mrr_setup mrr;
3332525a3d47SAdrian Chadd uint8_t i;
3333525a3d47SAdrian Chadd int error;
33346607310bSBenjamin Close
3335692ad7caSAdrian Chadd /* CCK rates (not used with 802.11a). */
3336692ad7caSAdrian Chadd for (i = WPI_RIDX_CCK1; i <= WPI_RIDX_CCK11; i++) {
33376607310bSBenjamin Close mrr.rates[i].flags = 0;
3338692ad7caSAdrian Chadd mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3339692ad7caSAdrian Chadd /* Fallback to the immediate lower CCK rate (if any.) */
3340692ad7caSAdrian Chadd mrr.rates[i].next =
3341692ad7caSAdrian Chadd (i == WPI_RIDX_CCK1) ? WPI_RIDX_CCK1 : i - 1;
334230b60481SAdrian Chadd /* Try twice at this rate before falling back to "next". */
334330b60481SAdrian Chadd mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
33446607310bSBenjamin Close }
3345692ad7caSAdrian Chadd /* OFDM rates (not used with 802.11b). */
3346692ad7caSAdrian Chadd for (i = WPI_RIDX_OFDM6; i <= WPI_RIDX_OFDM54; i++) {
33476607310bSBenjamin Close mrr.rates[i].flags = 0;
3348692ad7caSAdrian Chadd mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
3349692ad7caSAdrian Chadd /* Fallback to the immediate lower rate (if any.) */
3350692ad7caSAdrian Chadd /* We allow fallback from OFDM/6 to CCK/2 in 11b/g mode. */
3351692ad7caSAdrian Chadd mrr.rates[i].next = (i == WPI_RIDX_OFDM6) ?
33526607310bSBenjamin Close ((ic->ic_curmode == IEEE80211_MODE_11A) ?
3353692ad7caSAdrian Chadd WPI_RIDX_OFDM6 : WPI_RIDX_CCK2) :
33546607310bSBenjamin Close i - 1;
335530b60481SAdrian Chadd /* Try twice at this rate before falling back to "next". */
335630b60481SAdrian Chadd mrr.rates[i].ntries = WPI_NTRIES_DEFAULT;
33576607310bSBenjamin Close }
3358692ad7caSAdrian Chadd /* Setup MRR for control frames. */
3359692ad7caSAdrian Chadd mrr.which = htole32(WPI_MRR_CTL);
33606607310bSBenjamin Close error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
33616607310bSBenjamin Close if (error != 0) {
33626607310bSBenjamin Close device_printf(sc->sc_dev,
33636607310bSBenjamin Close "could not setup MRR for control frames\n");
33646607310bSBenjamin Close return error;
33656607310bSBenjamin Close }
3366692ad7caSAdrian Chadd /* Setup MRR for data frames. */
3367692ad7caSAdrian Chadd mrr.which = htole32(WPI_MRR_DATA);
33686607310bSBenjamin Close error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0);
33696607310bSBenjamin Close if (error != 0) {
33706607310bSBenjamin Close device_printf(sc->sc_dev,
33716607310bSBenjamin Close "could not setup MRR for data frames\n");
33726607310bSBenjamin Close return error;
33736607310bSBenjamin Close }
33746607310bSBenjamin Close return 0;
33756607310bSBenjamin Close }
33766607310bSBenjamin Close
3377692ad7caSAdrian Chadd static int
wpi_add_node(struct wpi_softc * sc,struct ieee80211_node * ni)3378692ad7caSAdrian Chadd wpi_add_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3379692ad7caSAdrian Chadd {
3380692ad7caSAdrian Chadd struct ieee80211com *ic = ni->ni_ic;
3381d29fcfb7SAdrian Chadd struct wpi_vap *wvp = WPI_VAP(ni->ni_vap);
3382a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
3383692ad7caSAdrian Chadd struct wpi_node_info node;
3384d29fcfb7SAdrian Chadd int error;
3385692ad7caSAdrian Chadd
3386692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3387692ad7caSAdrian Chadd
3388692ad7caSAdrian Chadd if (wn->id == WPI_ID_UNDEFINED)
3389692ad7caSAdrian Chadd return EINVAL;
3390692ad7caSAdrian Chadd
3391692ad7caSAdrian Chadd memset(&node, 0, sizeof node);
33923de934a7SAdrian Chadd IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3393692ad7caSAdrian Chadd node.id = wn->id;
3394692ad7caSAdrian Chadd node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3395692ad7caSAdrian Chadd wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3396692ad7caSAdrian Chadd node.action = htole32(WPI_ACTION_SET_RATE);
3397692ad7caSAdrian Chadd node.antenna = WPI_ANTENNA_BOTH;
3398692ad7caSAdrian Chadd
33990528191aSAdrian Chadd DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding node %d (%s)\n", __func__,
34000528191aSAdrian Chadd wn->id, ether_sprintf(ni->ni_macaddr));
34010528191aSAdrian Chadd
3402d29fcfb7SAdrian Chadd error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
3403d29fcfb7SAdrian Chadd if (error != 0) {
3404d29fcfb7SAdrian Chadd device_printf(sc->sc_dev,
3405d29fcfb7SAdrian Chadd "%s: wpi_cmd() call failed with error code %d\n", __func__,
3406d29fcfb7SAdrian Chadd error);
3407d29fcfb7SAdrian Chadd return error;
3408d29fcfb7SAdrian Chadd }
3409d29fcfb7SAdrian Chadd
3410d29fcfb7SAdrian Chadd if (wvp->wv_gtk != 0) {
3411d29fcfb7SAdrian Chadd error = wpi_set_global_keys(ni);
3412d29fcfb7SAdrian Chadd if (error != 0) {
3413d29fcfb7SAdrian Chadd device_printf(sc->sc_dev,
3414d29fcfb7SAdrian Chadd "%s: error while setting global keys\n", __func__);
3415d29fcfb7SAdrian Chadd return ENXIO;
3416d29fcfb7SAdrian Chadd }
3417d29fcfb7SAdrian Chadd }
3418d29fcfb7SAdrian Chadd
3419d29fcfb7SAdrian Chadd return 0;
3420692ad7caSAdrian Chadd }
3421692ad7caSAdrian Chadd
3422692ad7caSAdrian Chadd /*
3423692ad7caSAdrian Chadd * Broadcast node is used to send group-addressed and management frames.
3424692ad7caSAdrian Chadd */
3425692ad7caSAdrian Chadd static int
wpi_add_broadcast_node(struct wpi_softc * sc,int async)3426692ad7caSAdrian Chadd wpi_add_broadcast_node(struct wpi_softc *sc, int async)
3427692ad7caSAdrian Chadd {
34287a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
3429692ad7caSAdrian Chadd struct wpi_node_info node;
3430692ad7caSAdrian Chadd
3431692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3432692ad7caSAdrian Chadd
3433692ad7caSAdrian Chadd memset(&node, 0, sizeof node);
34347a79cebfSGleb Smirnoff IEEE80211_ADDR_COPY(node.macaddr, ieee80211broadcastaddr);
3435692ad7caSAdrian Chadd node.id = WPI_ID_BROADCAST;
3436692ad7caSAdrian Chadd node.plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3437692ad7caSAdrian Chadd wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
3438692ad7caSAdrian Chadd node.action = htole32(WPI_ACTION_SET_RATE);
3439692ad7caSAdrian Chadd node.antenna = WPI_ANTENNA_BOTH;
3440692ad7caSAdrian Chadd
34410528191aSAdrian Chadd DPRINTF(sc, WPI_DEBUG_NODE, "%s: adding broadcast node\n", __func__);
34420528191aSAdrian Chadd
3443692ad7caSAdrian Chadd return wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, async);
3444692ad7caSAdrian Chadd }
3445692ad7caSAdrian Chadd
3446692ad7caSAdrian Chadd static int
wpi_add_sta_node(struct wpi_softc * sc,struct ieee80211_node * ni)34470ebca245SAdrian Chadd wpi_add_sta_node(struct wpi_softc *sc, struct ieee80211_node *ni)
34480ebca245SAdrian Chadd {
34490ebca245SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
34500ebca245SAdrian Chadd int error;
34510ebca245SAdrian Chadd
34520ebca245SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
34530ebca245SAdrian Chadd
34540ebca245SAdrian Chadd wn->id = wpi_add_node_entry_sta(sc);
34550ebca245SAdrian Chadd
34560ebca245SAdrian Chadd if ((error = wpi_add_node(sc, ni)) != 0) {
34570ebca245SAdrian Chadd wpi_del_node_entry(sc, wn->id);
34580ebca245SAdrian Chadd wn->id = WPI_ID_UNDEFINED;
34590ebca245SAdrian Chadd return error;
34600ebca245SAdrian Chadd }
34610ebca245SAdrian Chadd
34620ebca245SAdrian Chadd return 0;
34630ebca245SAdrian Chadd }
34640ebca245SAdrian Chadd
34650ebca245SAdrian Chadd static int
wpi_add_ibss_node(struct wpi_softc * sc,struct ieee80211_node * ni)3466692ad7caSAdrian Chadd wpi_add_ibss_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3467692ad7caSAdrian Chadd {
3468a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
3469eca2eb39SAdrian Chadd int error;
3470692ad7caSAdrian Chadd
347139060fa9SAdrian Chadd KASSERT(wn->id == WPI_ID_UNDEFINED,
347239060fa9SAdrian Chadd ("the node %d was added before", wn->id));
3473692ad7caSAdrian Chadd
347439060fa9SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3475692ad7caSAdrian Chadd
3476eca2eb39SAdrian Chadd if ((wn->id = wpi_add_node_entry_adhoc(sc)) == WPI_ID_UNDEFINED) {
3477eca2eb39SAdrian Chadd device_printf(sc->sc_dev, "%s: h/w table is full\n", __func__);
3478eca2eb39SAdrian Chadd return ENOMEM;
3479eca2eb39SAdrian Chadd }
3480692ad7caSAdrian Chadd
3481eca2eb39SAdrian Chadd if ((error = wpi_add_node(sc, ni)) != 0) {
3482eca2eb39SAdrian Chadd wpi_del_node_entry(sc, wn->id);
3483eca2eb39SAdrian Chadd wn->id = WPI_ID_UNDEFINED;
3484eca2eb39SAdrian Chadd return error;
3485eca2eb39SAdrian Chadd }
3486692ad7caSAdrian Chadd
3487eca2eb39SAdrian Chadd return 0;
3488692ad7caSAdrian Chadd }
3489692ad7caSAdrian Chadd
3490692ad7caSAdrian Chadd static void
wpi_del_node(struct wpi_softc * sc,struct ieee80211_node * ni)3491692ad7caSAdrian Chadd wpi_del_node(struct wpi_softc *sc, struct ieee80211_node *ni)
3492692ad7caSAdrian Chadd {
3493a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
3494692ad7caSAdrian Chadd struct wpi_cmd_del_node node;
3495692ad7caSAdrian Chadd int error;
3496692ad7caSAdrian Chadd
349739060fa9SAdrian Chadd KASSERT(wn->id != WPI_ID_UNDEFINED, ("undefined node id passed"));
3498692ad7caSAdrian Chadd
349939060fa9SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3500692ad7caSAdrian Chadd
3501692ad7caSAdrian Chadd memset(&node, 0, sizeof node);
35023de934a7SAdrian Chadd IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
3503692ad7caSAdrian Chadd node.count = 1;
3504692ad7caSAdrian Chadd
35050528191aSAdrian Chadd DPRINTF(sc, WPI_DEBUG_NODE, "%s: deleting node %d (%s)\n", __func__,
35060528191aSAdrian Chadd wn->id, ether_sprintf(ni->ni_macaddr));
35070528191aSAdrian Chadd
3508692ad7caSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_DEL_NODE, &node, sizeof node, 1);
3509692ad7caSAdrian Chadd if (error != 0) {
3510692ad7caSAdrian Chadd device_printf(sc->sc_dev,
3511692ad7caSAdrian Chadd "%s: could not delete node %u, error %d\n", __func__,
3512692ad7caSAdrian Chadd wn->id, error);
3513692ad7caSAdrian Chadd }
3514692ad7caSAdrian Chadd }
3515692ad7caSAdrian Chadd
3516692ad7caSAdrian Chadd static int
wpi_updateedca(struct ieee80211com * ic)3517692ad7caSAdrian Chadd wpi_updateedca(struct ieee80211com *ic)
3518692ad7caSAdrian Chadd {
3519692ad7caSAdrian Chadd #define WPI_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
35202cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
35219fbe631aSAdrian Chadd struct chanAccParams chp;
3522692ad7caSAdrian Chadd struct wpi_edca_params cmd;
3523692ad7caSAdrian Chadd int aci, error;
3524692ad7caSAdrian Chadd
35259fbe631aSAdrian Chadd ieee80211_wme_ic_getparams(ic, &chp);
35269fbe631aSAdrian Chadd
3527692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3528692ad7caSAdrian Chadd
3529692ad7caSAdrian Chadd memset(&cmd, 0, sizeof cmd);
3530692ad7caSAdrian Chadd cmd.flags = htole32(WPI_EDCA_UPDATE);
3531692ad7caSAdrian Chadd for (aci = 0; aci < WME_NUM_AC; aci++) {
35329fbe631aSAdrian Chadd const struct wmeParams *ac = &chp.cap_wmeParams[aci];
3533692ad7caSAdrian Chadd cmd.ac[aci].aifsn = ac->wmep_aifsn;
3534692ad7caSAdrian Chadd cmd.ac[aci].cwmin = htole16(WPI_EXP2(ac->wmep_logcwmin));
3535692ad7caSAdrian Chadd cmd.ac[aci].cwmax = htole16(WPI_EXP2(ac->wmep_logcwmax));
3536692ad7caSAdrian Chadd cmd.ac[aci].txoplimit =
3537692ad7caSAdrian Chadd htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit));
3538692ad7caSAdrian Chadd
3539692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_EDCA,
3540692ad7caSAdrian Chadd "setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
3541692ad7caSAdrian Chadd "txoplimit=%d\n", aci, cmd.ac[aci].aifsn,
3542692ad7caSAdrian Chadd cmd.ac[aci].cwmin, cmd.ac[aci].cwmax,
3543692ad7caSAdrian Chadd cmd.ac[aci].txoplimit);
3544692ad7caSAdrian Chadd }
3545692ad7caSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1);
3546692ad7caSAdrian Chadd
3547692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
3548692ad7caSAdrian Chadd
3549692ad7caSAdrian Chadd return error;
3550692ad7caSAdrian Chadd #undef WPI_EXP2
3551692ad7caSAdrian Chadd }
3552692ad7caSAdrian Chadd
3553692ad7caSAdrian Chadd static void
wpi_set_promisc(struct wpi_softc * sc)3554692ad7caSAdrian Chadd wpi_set_promisc(struct wpi_softc *sc)
3555692ad7caSAdrian Chadd {
35567a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
3557726ddc26SAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3558692ad7caSAdrian Chadd uint32_t promisc_filter;
3559692ad7caSAdrian Chadd
3560726ddc26SAdrian Chadd promisc_filter = WPI_FILTER_CTL;
3561726ddc26SAdrian Chadd if (vap != NULL && vap->iv_opmode != IEEE80211_M_HOSTAP)
3562726ddc26SAdrian Chadd promisc_filter |= WPI_FILTER_PROMISC;
3563692ad7caSAdrian Chadd
35647a79cebfSGleb Smirnoff if (ic->ic_promisc > 0)
3565692ad7caSAdrian Chadd sc->rxon.filter |= htole32(promisc_filter);
3566692ad7caSAdrian Chadd else
3567692ad7caSAdrian Chadd sc->rxon.filter &= ~htole32(promisc_filter);
3568692ad7caSAdrian Chadd }
3569692ad7caSAdrian Chadd
3570692ad7caSAdrian Chadd static void
wpi_update_promisc(struct ieee80211com * ic)3571272f6adeSGleb Smirnoff wpi_update_promisc(struct ieee80211com *ic)
3572692ad7caSAdrian Chadd {
3573272f6adeSGleb Smirnoff struct wpi_softc *sc = ic->ic_softc;
3574692ad7caSAdrian Chadd
357554b27b35SAndriy Voskoboinyk WPI_LOCK(sc);
357654b27b35SAndriy Voskoboinyk if (sc->sc_running == 0) {
357754b27b35SAndriy Voskoboinyk WPI_UNLOCK(sc);
357854b27b35SAndriy Voskoboinyk return;
357954b27b35SAndriy Voskoboinyk }
358054b27b35SAndriy Voskoboinyk WPI_UNLOCK(sc);
358154b27b35SAndriy Voskoboinyk
3582446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
3583692ad7caSAdrian Chadd wpi_set_promisc(sc);
3584692ad7caSAdrian Chadd
3585692ad7caSAdrian Chadd if (wpi_send_rxon(sc, 1, 1) != 0) {
3586692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: could not send RXON\n",
3587692ad7caSAdrian Chadd __func__);
3588692ad7caSAdrian Chadd }
3589446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
3590692ad7caSAdrian Chadd }
3591692ad7caSAdrian Chadd
3592692ad7caSAdrian Chadd static void
wpi_update_mcast(struct ieee80211com * ic)3593272f6adeSGleb Smirnoff wpi_update_mcast(struct ieee80211com *ic)
3594692ad7caSAdrian Chadd {
3595692ad7caSAdrian Chadd /* Ignore */
3596692ad7caSAdrian Chadd }
3597692ad7caSAdrian Chadd
35986607310bSBenjamin Close static void
wpi_set_led(struct wpi_softc * sc,uint8_t which,uint8_t off,uint8_t on)35996607310bSBenjamin Close wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
36006607310bSBenjamin Close {
36016607310bSBenjamin Close struct wpi_cmd_led led;
36026607310bSBenjamin Close
3603692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3604692ad7caSAdrian Chadd
36056607310bSBenjamin Close led.which = which;
36066607310bSBenjamin Close led.unit = htole32(100000); /* on/off in unit of 100ms */
36076607310bSBenjamin Close led.off = off;
36086607310bSBenjamin Close led.on = on;
36096607310bSBenjamin Close (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
36106607310bSBenjamin Close }
36116607310bSBenjamin Close
3612692ad7caSAdrian Chadd static int
wpi_set_timing(struct wpi_softc * sc,struct ieee80211_node * ni)3613692ad7caSAdrian Chadd wpi_set_timing(struct wpi_softc *sc, struct ieee80211_node *ni)
36146607310bSBenjamin Close {
3615692ad7caSAdrian Chadd struct wpi_cmd_timing cmd;
36166607310bSBenjamin Close uint64_t val, mod;
36176607310bSBenjamin Close
3618692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
36196607310bSBenjamin Close
3620692ad7caSAdrian Chadd memset(&cmd, 0, sizeof cmd);
3621692ad7caSAdrian Chadd memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3622692ad7caSAdrian Chadd cmd.bintval = htole16(ni->ni_intval);
3623692ad7caSAdrian Chadd cmd.lintval = htole16(10);
36246607310bSBenjamin Close
3625692ad7caSAdrian Chadd /* Compute remaining time until next beacon. */
3626692ad7caSAdrian Chadd val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU;
3627692ad7caSAdrian Chadd mod = le64toh(cmd.tstamp) % val;
3628692ad7caSAdrian Chadd cmd.binitval = htole32((uint32_t)(val - mod));
36296607310bSBenjamin Close
3630692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3631692ad7caSAdrian Chadd ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
36326607310bSBenjamin Close
3633692ad7caSAdrian Chadd return wpi_cmd(sc, WPI_CMD_TIMING, &cmd, sizeof cmd, 1);
36346607310bSBenjamin Close }
36356607310bSBenjamin Close
36366607310bSBenjamin Close /*
3637692ad7caSAdrian Chadd * This function is called periodically (every 60 seconds) to adjust output
3638692ad7caSAdrian Chadd * power to temperature changes.
3639692ad7caSAdrian Chadd */
3640692ad7caSAdrian Chadd static void
wpi_power_calibration(struct wpi_softc * sc)3641692ad7caSAdrian Chadd wpi_power_calibration(struct wpi_softc *sc)
3642692ad7caSAdrian Chadd {
3643692ad7caSAdrian Chadd int temp;
3644692ad7caSAdrian Chadd
3645692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
3646692ad7caSAdrian Chadd
3647692ad7caSAdrian Chadd /* Update sensor data. */
3648692ad7caSAdrian Chadd temp = (int)WPI_READ(sc, WPI_UCODE_GP2);
3649692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TEMP, "Temp in calibration is: %d\n", temp);
3650692ad7caSAdrian Chadd
3651692ad7caSAdrian Chadd /* Sanity-check read value. */
3652692ad7caSAdrian Chadd if (temp < -260 || temp > 25) {
3653692ad7caSAdrian Chadd /* This can't be correct, ignore. */
3654692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TEMP,
3655692ad7caSAdrian Chadd "out-of-range temperature reported: %d\n", temp);
3656692ad7caSAdrian Chadd return;
3657692ad7caSAdrian Chadd }
3658692ad7caSAdrian Chadd
3659692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d->%d\n", sc->temp, temp);
3660692ad7caSAdrian Chadd
3661692ad7caSAdrian Chadd /* Adjust Tx power if need be. */
3662692ad7caSAdrian Chadd if (abs(temp - sc->temp) <= 6)
3663692ad7caSAdrian Chadd return;
3664692ad7caSAdrian Chadd
3665692ad7caSAdrian Chadd sc->temp = temp;
3666692ad7caSAdrian Chadd
3667692ad7caSAdrian Chadd if (wpi_set_txpower(sc, 1) != 0) {
3668692ad7caSAdrian Chadd /* just warn, too bad for the automatic calibration... */
3669692ad7caSAdrian Chadd device_printf(sc->sc_dev,"could not adjust Tx power\n");
3670692ad7caSAdrian Chadd }
3671692ad7caSAdrian Chadd }
3672692ad7caSAdrian Chadd
3673692ad7caSAdrian Chadd /*
3674692ad7caSAdrian Chadd * Set TX power for current channel.
36756607310bSBenjamin Close */
36766607310bSBenjamin Close static int
wpi_set_txpower(struct wpi_softc * sc,int async)3677692ad7caSAdrian Chadd wpi_set_txpower(struct wpi_softc *sc, int async)
36786607310bSBenjamin Close {
3679692ad7caSAdrian Chadd struct wpi_power_group *group;
3680692ad7caSAdrian Chadd struct wpi_cmd_txpower cmd;
3681692ad7caSAdrian Chadd uint8_t chan;
3682655e8ba8SAdrian Chadd int idx, is_chan_5ghz, i;
36836607310bSBenjamin Close
3684692ad7caSAdrian Chadd /* Retrieve current channel from last RXON. */
3685692ad7caSAdrian Chadd chan = sc->rxon.chan;
3686655e8ba8SAdrian Chadd is_chan_5ghz = (sc->rxon.flags & htole32(WPI_RXON_24GHZ)) == 0;
36876607310bSBenjamin Close
3688692ad7caSAdrian Chadd /* Find the TX power group to which this channel belongs. */
3689655e8ba8SAdrian Chadd if (is_chan_5ghz) {
3690692ad7caSAdrian Chadd for (group = &sc->groups[1]; group < &sc->groups[4]; group++)
3691692ad7caSAdrian Chadd if (chan <= group->chan)
3692692ad7caSAdrian Chadd break;
3693692ad7caSAdrian Chadd } else
3694692ad7caSAdrian Chadd group = &sc->groups[0];
3695692ad7caSAdrian Chadd
3696692ad7caSAdrian Chadd memset(&cmd, 0, sizeof cmd);
3697655e8ba8SAdrian Chadd cmd.band = is_chan_5ghz ? WPI_BAND_5GHZ : WPI_BAND_2GHZ;
3698692ad7caSAdrian Chadd cmd.chan = htole16(chan);
3699692ad7caSAdrian Chadd
3700692ad7caSAdrian Chadd /* Set TX power for all OFDM and CCK rates. */
3701692ad7caSAdrian Chadd for (i = 0; i <= WPI_RIDX_MAX ; i++) {
3702692ad7caSAdrian Chadd /* Retrieve TX power for this channel/rate. */
3703655e8ba8SAdrian Chadd idx = wpi_get_power_index(sc, group, chan, is_chan_5ghz, i);
3704692ad7caSAdrian Chadd
3705692ad7caSAdrian Chadd cmd.rates[i].plcp = wpi_ridx_to_plcp[i];
3706692ad7caSAdrian Chadd
3707655e8ba8SAdrian Chadd if (is_chan_5ghz) {
3708692ad7caSAdrian Chadd cmd.rates[i].rf_gain = wpi_rf_gain_5ghz[idx];
3709692ad7caSAdrian Chadd cmd.rates[i].dsp_gain = wpi_dsp_gain_5ghz[idx];
3710692ad7caSAdrian Chadd } else {
3711692ad7caSAdrian Chadd cmd.rates[i].rf_gain = wpi_rf_gain_2ghz[idx];
3712692ad7caSAdrian Chadd cmd.rates[i].dsp_gain = wpi_dsp_gain_2ghz[idx];
3713692ad7caSAdrian Chadd }
3714692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TEMP,
3715692ad7caSAdrian Chadd "chan %d/ridx %d: power index %d\n", chan, i, idx);
3716692ad7caSAdrian Chadd }
3717692ad7caSAdrian Chadd
3718692ad7caSAdrian Chadd return wpi_cmd(sc, WPI_CMD_TXPOWER, &cmd, sizeof cmd, async);
3719692ad7caSAdrian Chadd }
3720692ad7caSAdrian Chadd
3721692ad7caSAdrian Chadd /*
3722692ad7caSAdrian Chadd * Determine Tx power index for a given channel/rate combination.
3723692ad7caSAdrian Chadd * This takes into account the regulatory information from EEPROM and the
3724692ad7caSAdrian Chadd * current temperature.
3725692ad7caSAdrian Chadd */
3726692ad7caSAdrian Chadd static int
wpi_get_power_index(struct wpi_softc * sc,struct wpi_power_group * group,uint8_t chan,int is_chan_5ghz,int ridx)3727692ad7caSAdrian Chadd wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group,
3728655e8ba8SAdrian Chadd uint8_t chan, int is_chan_5ghz, int ridx)
3729692ad7caSAdrian Chadd {
3730692ad7caSAdrian Chadd /* Fixed-point arithmetic division using a n-bit fractional part. */
3731692ad7caSAdrian Chadd #define fdivround(a, b, n) \
3732692ad7caSAdrian Chadd ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3733692ad7caSAdrian Chadd
3734692ad7caSAdrian Chadd /* Linear interpolation. */
3735692ad7caSAdrian Chadd #define interpolate(x, x1, y1, x2, y2, n) \
3736692ad7caSAdrian Chadd ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3737692ad7caSAdrian Chadd
3738692ad7caSAdrian Chadd struct wpi_power_sample *sample;
3739692ad7caSAdrian Chadd int pwr, idx;
3740692ad7caSAdrian Chadd
3741692ad7caSAdrian Chadd /* Default TX power is group maximum TX power minus 3dB. */
3742692ad7caSAdrian Chadd pwr = group->maxpwr / 2;
3743692ad7caSAdrian Chadd
3744692ad7caSAdrian Chadd /* Decrease TX power for highest OFDM rates to reduce distortion. */
3745692ad7caSAdrian Chadd switch (ridx) {
3746692ad7caSAdrian Chadd case WPI_RIDX_OFDM36:
3747655e8ba8SAdrian Chadd pwr -= is_chan_5ghz ? 5 : 0;
3748692ad7caSAdrian Chadd break;
3749692ad7caSAdrian Chadd case WPI_RIDX_OFDM48:
3750655e8ba8SAdrian Chadd pwr -= is_chan_5ghz ? 10 : 7;
3751692ad7caSAdrian Chadd break;
3752692ad7caSAdrian Chadd case WPI_RIDX_OFDM54:
3753655e8ba8SAdrian Chadd pwr -= is_chan_5ghz ? 12 : 9;
3754692ad7caSAdrian Chadd break;
3755692ad7caSAdrian Chadd }
3756692ad7caSAdrian Chadd
3757692ad7caSAdrian Chadd /* Never exceed the channel maximum allowed TX power. */
3758692ad7caSAdrian Chadd pwr = min(pwr, sc->maxpwr[chan]);
3759692ad7caSAdrian Chadd
3760692ad7caSAdrian Chadd /* Retrieve TX power index into gain tables from samples. */
3761692ad7caSAdrian Chadd for (sample = group->samples; sample < &group->samples[3]; sample++)
3762692ad7caSAdrian Chadd if (pwr > sample[1].power)
3763692ad7caSAdrian Chadd break;
3764692ad7caSAdrian Chadd /* Fixed-point linear interpolation using a 19-bit fractional part. */
3765692ad7caSAdrian Chadd idx = interpolate(pwr, sample[0].power, sample[0].index,
3766692ad7caSAdrian Chadd sample[1].power, sample[1].index, 19);
3767692ad7caSAdrian Chadd
3768692ad7caSAdrian Chadd /*-
3769692ad7caSAdrian Chadd * Adjust power index based on current temperature:
3770692ad7caSAdrian Chadd * - if cooler than factory-calibrated: decrease output power
3771692ad7caSAdrian Chadd * - if warmer than factory-calibrated: increase output power
3772692ad7caSAdrian Chadd */
3773692ad7caSAdrian Chadd idx -= (sc->temp - group->temp) * 11 / 100;
3774692ad7caSAdrian Chadd
3775692ad7caSAdrian Chadd /* Decrease TX power for CCK rates (-5dB). */
3776692ad7caSAdrian Chadd if (ridx >= WPI_RIDX_CCK1)
3777692ad7caSAdrian Chadd idx += 10;
3778692ad7caSAdrian Chadd
3779692ad7caSAdrian Chadd /* Make sure idx stays in a valid range. */
3780692ad7caSAdrian Chadd if (idx < 0)
3781692ad7caSAdrian Chadd return 0;
3782692ad7caSAdrian Chadd if (idx > WPI_MAX_PWR_INDEX)
3783692ad7caSAdrian Chadd return WPI_MAX_PWR_INDEX;
3784692ad7caSAdrian Chadd return idx;
3785692ad7caSAdrian Chadd
3786692ad7caSAdrian Chadd #undef interpolate
3787692ad7caSAdrian Chadd #undef fdivround
3788692ad7caSAdrian Chadd }
3789692ad7caSAdrian Chadd
3790692ad7caSAdrian Chadd /*
3791692ad7caSAdrian Chadd * Set STA mode power saving level (between 0 and 5).
3792692ad7caSAdrian Chadd * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
3793692ad7caSAdrian Chadd */
3794692ad7caSAdrian Chadd static int
wpi_set_pslevel(struct wpi_softc * sc,uint8_t dtim,int level,int async)3795692ad7caSAdrian Chadd wpi_set_pslevel(struct wpi_softc *sc, uint8_t dtim, int level, int async)
3796692ad7caSAdrian Chadd {
3797692ad7caSAdrian Chadd struct wpi_pmgt_cmd cmd;
3798692ad7caSAdrian Chadd const struct wpi_pmgt *pmgt;
3799525a3d47SAdrian Chadd uint32_t max, reg;
3800525a3d47SAdrian Chadd uint8_t skip_dtim;
3801692ad7caSAdrian Chadd int i;
3802692ad7caSAdrian Chadd
3803692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_PWRSAVE,
3804692ad7caSAdrian Chadd "%s: dtim=%d, level=%d, async=%d\n",
3805692ad7caSAdrian Chadd __func__, dtim, level, async);
3806692ad7caSAdrian Chadd
3807692ad7caSAdrian Chadd /* Select which PS parameters to use. */
3808692ad7caSAdrian Chadd if (dtim <= 10)
3809692ad7caSAdrian Chadd pmgt = &wpi_pmgt[0][level];
3810692ad7caSAdrian Chadd else
3811692ad7caSAdrian Chadd pmgt = &wpi_pmgt[1][level];
3812692ad7caSAdrian Chadd
3813692ad7caSAdrian Chadd memset(&cmd, 0, sizeof cmd);
3814c34db420SAdrian Chadd if (level != 0) /* not CAM */
3815692ad7caSAdrian Chadd cmd.flags |= htole16(WPI_PS_ALLOW_SLEEP);
3816692ad7caSAdrian Chadd /* Retrieve PCIe Active State Power Management (ASPM). */
38177c2e8d33SAndriy Voskoboinyk reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + PCIER_LINK_CTL, 1);
38187c2e8d33SAndriy Voskoboinyk if (!(reg & PCIEM_LINK_CTL_ASPMC_L0S)) /* L0s Entry disabled. */
3819692ad7caSAdrian Chadd cmd.flags |= htole16(WPI_PS_PCI_PMGT);
3820692ad7caSAdrian Chadd
3821692ad7caSAdrian Chadd cmd.rxtimeout = htole32(pmgt->rxtimeout * IEEE80211_DUR_TU);
3822692ad7caSAdrian Chadd cmd.txtimeout = htole32(pmgt->txtimeout * IEEE80211_DUR_TU);
3823692ad7caSAdrian Chadd
3824692ad7caSAdrian Chadd if (dtim == 0) {
3825692ad7caSAdrian Chadd dtim = 1;
3826692ad7caSAdrian Chadd skip_dtim = 0;
3827692ad7caSAdrian Chadd } else
3828692ad7caSAdrian Chadd skip_dtim = pmgt->skip_dtim;
3829692ad7caSAdrian Chadd
3830692ad7caSAdrian Chadd if (skip_dtim != 0) {
3831692ad7caSAdrian Chadd cmd.flags |= htole16(WPI_PS_SLEEP_OVER_DTIM);
3832692ad7caSAdrian Chadd max = pmgt->intval[4];
3833692ad7caSAdrian Chadd if (max == (uint32_t)-1)
3834692ad7caSAdrian Chadd max = dtim * (skip_dtim + 1);
3835692ad7caSAdrian Chadd else if (max > dtim)
38364ed3c0e7SPedro F. Giffuni max = rounddown(max, dtim);
3837692ad7caSAdrian Chadd } else
3838692ad7caSAdrian Chadd max = dtim;
3839692ad7caSAdrian Chadd
3840692ad7caSAdrian Chadd for (i = 0; i < 5; i++)
3841692ad7caSAdrian Chadd cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
3842692ad7caSAdrian Chadd
3843692ad7caSAdrian Chadd return wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
3844692ad7caSAdrian Chadd }
3845692ad7caSAdrian Chadd
3846692ad7caSAdrian Chadd static int
wpi_send_btcoex(struct wpi_softc * sc)3847692ad7caSAdrian Chadd wpi_send_btcoex(struct wpi_softc *sc)
3848692ad7caSAdrian Chadd {
3849692ad7caSAdrian Chadd struct wpi_bluetooth cmd;
3850692ad7caSAdrian Chadd
3851692ad7caSAdrian Chadd memset(&cmd, 0, sizeof cmd);
3852692ad7caSAdrian Chadd cmd.flags = WPI_BT_COEX_MODE_4WIRE;
3853692ad7caSAdrian Chadd cmd.lead_time = WPI_BT_LEAD_TIME_DEF;
3854692ad7caSAdrian Chadd cmd.max_kill = WPI_BT_MAX_KILL_DEF;
3855692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_RESET, "%s: configuring bluetooth coexistence\n",
3856692ad7caSAdrian Chadd __func__);
3857692ad7caSAdrian Chadd return wpi_cmd(sc, WPI_CMD_BT_COEX, &cmd, sizeof(cmd), 0);
3858692ad7caSAdrian Chadd }
3859692ad7caSAdrian Chadd
3860692ad7caSAdrian Chadd static int
wpi_send_rxon(struct wpi_softc * sc,int assoc,int async)3861692ad7caSAdrian Chadd wpi_send_rxon(struct wpi_softc *sc, int assoc, int async)
3862692ad7caSAdrian Chadd {
3863692ad7caSAdrian Chadd int error;
3864692ad7caSAdrian Chadd
3865446685e4SAdrian Chadd if (async)
3866446685e4SAdrian Chadd WPI_RXON_LOCK_ASSERT(sc);
3867446685e4SAdrian Chadd
3868621f1a75SAdrian Chadd if (assoc && wpi_check_bss_filter(sc) != 0) {
3869692ad7caSAdrian Chadd struct wpi_assoc rxon_assoc;
3870692ad7caSAdrian Chadd
3871692ad7caSAdrian Chadd rxon_assoc.flags = sc->rxon.flags;
3872692ad7caSAdrian Chadd rxon_assoc.filter = sc->rxon.filter;
3873692ad7caSAdrian Chadd rxon_assoc.ofdm_mask = sc->rxon.ofdm_mask;
3874692ad7caSAdrian Chadd rxon_assoc.cck_mask = sc->rxon.cck_mask;
3875692ad7caSAdrian Chadd rxon_assoc.reserved = 0;
3876692ad7caSAdrian Chadd
3877692ad7caSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_RXON_ASSOC, &rxon_assoc,
3878692ad7caSAdrian Chadd sizeof (struct wpi_assoc), async);
3879cfe9b1ceSAdrian Chadd if (error != 0) {
3880cfe9b1ceSAdrian Chadd device_printf(sc->sc_dev,
3881cfe9b1ceSAdrian Chadd "RXON_ASSOC command failed, error %d\n", error);
3882cfe9b1ceSAdrian Chadd return error;
3883cfe9b1ceSAdrian Chadd }
3884692ad7caSAdrian Chadd } else {
3885cfe9b1ceSAdrian Chadd if (async) {
38867964dd56SAdrian Chadd WPI_NT_LOCK(sc);
3887692ad7caSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3888692ad7caSAdrian Chadd sizeof (struct wpi_rxon), async);
3889cfe9b1ceSAdrian Chadd if (error == 0)
3890eca2eb39SAdrian Chadd wpi_clear_node_table(sc);
38917964dd56SAdrian Chadd WPI_NT_UNLOCK(sc);
3892cfe9b1ceSAdrian Chadd } else {
3893cfe9b1ceSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_RXON, &sc->rxon,
3894cfe9b1ceSAdrian Chadd sizeof (struct wpi_rxon), async);
3895cfe9b1ceSAdrian Chadd if (error == 0)
3896cfe9b1ceSAdrian Chadd wpi_clear_node_table(sc);
3897692ad7caSAdrian Chadd }
3898cfe9b1ceSAdrian Chadd
3899692ad7caSAdrian Chadd if (error != 0) {
39006607310bSBenjamin Close device_printf(sc->sc_dev,
3901cfe9b1ceSAdrian Chadd "RXON command failed, error %d\n", error);
39026607310bSBenjamin Close return error;
39036607310bSBenjamin Close }
39046607310bSBenjamin Close
3905692ad7caSAdrian Chadd /* Add broadcast node. */
3906692ad7caSAdrian Chadd error = wpi_add_broadcast_node(sc, async);
3907692ad7caSAdrian Chadd if (error != 0) {
3908692ad7caSAdrian Chadd device_printf(sc->sc_dev,
3909692ad7caSAdrian Chadd "could not add broadcast node, error %d\n", error);
3910692ad7caSAdrian Chadd return error;
3911692ad7caSAdrian Chadd }
3912692ad7caSAdrian Chadd }
39136607310bSBenjamin Close
3914cfe9b1ceSAdrian Chadd /* Configuration has changed, set Tx power accordingly. */
3915cfe9b1ceSAdrian Chadd if ((error = wpi_set_txpower(sc, async)) != 0) {
3916cfe9b1ceSAdrian Chadd device_printf(sc->sc_dev,
3917cfe9b1ceSAdrian Chadd "%s: could not set TX power, error %d\n", __func__, error);
3918cfe9b1ceSAdrian Chadd return error;
3919cfe9b1ceSAdrian Chadd }
3920cfe9b1ceSAdrian Chadd
3921692ad7caSAdrian Chadd return 0;
39226607310bSBenjamin Close }
39236607310bSBenjamin Close
39246607310bSBenjamin Close /**
39256607310bSBenjamin Close * Configure the card to listen to a particular channel, this transisions the
39266607310bSBenjamin Close * card in to being able to receive frames from remote devices.
39276607310bSBenjamin Close */
39286607310bSBenjamin Close static int
wpi_config(struct wpi_softc * sc)39296607310bSBenjamin Close wpi_config(struct wpi_softc *sc)
39306607310bSBenjamin Close {
39317a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
39323de934a7SAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
39336b997e0cSAdrian Chadd struct ieee80211_channel *c = ic->ic_curchan;
39346607310bSBenjamin Close int error;
39356607310bSBenjamin Close
3936692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
3937692ad7caSAdrian Chadd
3938692ad7caSAdrian Chadd /* Set power saving level to CAM during initialization. */
3939692ad7caSAdrian Chadd if ((error = wpi_set_pslevel(sc, 0, 0, 0)) != 0) {
3940692ad7caSAdrian Chadd device_printf(sc->sc_dev,
3941692ad7caSAdrian Chadd "%s: could not set power saving level\n", __func__);
39426607310bSBenjamin Close return error;
39436607310bSBenjamin Close }
39446607310bSBenjamin Close
3945692ad7caSAdrian Chadd /* Configure bluetooth coexistence. */
3946692ad7caSAdrian Chadd if ((error = wpi_send_btcoex(sc)) != 0) {
39476607310bSBenjamin Close device_printf(sc->sc_dev,
39486607310bSBenjamin Close "could not configure bluetooth coexistence\n");
39496607310bSBenjamin Close return error;
39506607310bSBenjamin Close }
39516607310bSBenjamin Close
3952692ad7caSAdrian Chadd /* Configure adapter. */
3953692ad7caSAdrian Chadd memset(&sc->rxon, 0, sizeof (struct wpi_rxon));
39543de934a7SAdrian Chadd IEEE80211_ADDR_COPY(sc->rxon.myaddr, vap->iv_myaddr);
3955692ad7caSAdrian Chadd
3956692ad7caSAdrian Chadd /* Set default channel. */
39576b997e0cSAdrian Chadd sc->rxon.chan = ieee80211_chan2ieee(ic, c);
3958692ad7caSAdrian Chadd sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
39596b997e0cSAdrian Chadd if (IEEE80211_IS_CHAN_2GHZ(c))
3960692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
3961692ad7caSAdrian Chadd
3962fae61c69SAdrian Chadd sc->rxon.filter = WPI_FILTER_MULTICAST;
39636607310bSBenjamin Close switch (ic->ic_opmode) {
39646607310bSBenjamin Close case IEEE80211_M_STA:
3965692ad7caSAdrian Chadd sc->rxon.mode = WPI_MODE_STA;
39666607310bSBenjamin Close break;
39676607310bSBenjamin Close case IEEE80211_M_IBSS:
3968692ad7caSAdrian Chadd sc->rxon.mode = WPI_MODE_IBSS;
3969fae61c69SAdrian Chadd sc->rxon.filter |= WPI_FILTER_BEACON;
39706607310bSBenjamin Close break;
39716607310bSBenjamin Close case IEEE80211_M_HOSTAP:
3972726ddc26SAdrian Chadd /* XXX workaround for beaconing */
3973726ddc26SAdrian Chadd sc->rxon.mode = WPI_MODE_IBSS;
3974726ddc26SAdrian Chadd sc->rxon.filter |= WPI_FILTER_ASSOC | WPI_FILTER_PROMISC;
3975726ddc26SAdrian Chadd break;
3976726ddc26SAdrian Chadd case IEEE80211_M_AHDEMO:
3977692ad7caSAdrian Chadd sc->rxon.mode = WPI_MODE_HOSTAP;
39786607310bSBenjamin Close break;
39796607310bSBenjamin Close case IEEE80211_M_MONITOR:
3980692ad7caSAdrian Chadd sc->rxon.mode = WPI_MODE_MONITOR;
39816607310bSBenjamin Close break;
3982820e6a1fSRui Paulo default:
39837167f5fcSAdrian Chadd device_printf(sc->sc_dev, "unknown opmode %d\n",
39847167f5fcSAdrian Chadd ic->ic_opmode);
3985820e6a1fSRui Paulo return EINVAL;
39866607310bSBenjamin Close }
3987fae61c69SAdrian Chadd sc->rxon.filter = htole32(sc->rxon.filter);
3988692ad7caSAdrian Chadd wpi_set_promisc(sc);
3989692ad7caSAdrian Chadd sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
3990692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
3991692ad7caSAdrian Chadd
3992692ad7caSAdrian Chadd if ((error = wpi_send_rxon(sc, 0, 0)) != 0) {
3993692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: could not send RXON\n",
3994692ad7caSAdrian Chadd __func__);
3995692ad7caSAdrian Chadd return error;
3996692ad7caSAdrian Chadd }
3997692ad7caSAdrian Chadd
3998692ad7caSAdrian Chadd /* Setup rate scalling. */
3999692ad7caSAdrian Chadd if ((error = wpi_mrr_setup(sc)) != 0) {
4000692ad7caSAdrian Chadd device_printf(sc->sc_dev, "could not setup MRR, error %d\n",
4001692ad7caSAdrian Chadd error);
4002692ad7caSAdrian Chadd return error;
4003692ad7caSAdrian Chadd }
4004692ad7caSAdrian Chadd
4005692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4006692ad7caSAdrian Chadd
4007692ad7caSAdrian Chadd return 0;
4008692ad7caSAdrian Chadd }
4009692ad7caSAdrian Chadd
4010692ad7caSAdrian Chadd static uint16_t
wpi_get_active_dwell_time(struct wpi_softc * sc,struct ieee80211_channel * c,uint8_t n_probes)4011692ad7caSAdrian Chadd wpi_get_active_dwell_time(struct wpi_softc *sc,
4012692ad7caSAdrian Chadd struct ieee80211_channel *c, uint8_t n_probes)
4013692ad7caSAdrian Chadd {
4014692ad7caSAdrian Chadd /* No channel? Default to 2GHz settings. */
4015692ad7caSAdrian Chadd if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c)) {
4016692ad7caSAdrian Chadd return (WPI_ACTIVE_DWELL_TIME_2GHZ +
4017692ad7caSAdrian Chadd WPI_ACTIVE_DWELL_FACTOR_2GHZ * (n_probes + 1));
4018692ad7caSAdrian Chadd }
4019692ad7caSAdrian Chadd
4020692ad7caSAdrian Chadd /* 5GHz dwell time. */
4021692ad7caSAdrian Chadd return (WPI_ACTIVE_DWELL_TIME_5GHZ +
4022692ad7caSAdrian Chadd WPI_ACTIVE_DWELL_FACTOR_5GHZ * (n_probes + 1));
4023692ad7caSAdrian Chadd }
4024692ad7caSAdrian Chadd
4025692ad7caSAdrian Chadd /*
402642946b7dSAdrian Chadd * Limit the total dwell time.
4027692ad7caSAdrian Chadd *
4028692ad7caSAdrian Chadd * Returns the dwell time in milliseconds.
4029692ad7caSAdrian Chadd */
4030692ad7caSAdrian Chadd static uint16_t
wpi_limit_dwell(struct wpi_softc * sc,uint16_t dwell_time)4031692ad7caSAdrian Chadd wpi_limit_dwell(struct wpi_softc *sc, uint16_t dwell_time)
4032692ad7caSAdrian Chadd {
40337a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
40347d4ee533SAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4035525a3d47SAdrian Chadd uint16_t bintval = 0;
4036692ad7caSAdrian Chadd
4037692ad7caSAdrian Chadd /* bintval is in TU (1.024mS) */
40387d4ee533SAdrian Chadd if (vap != NULL)
4039692ad7caSAdrian Chadd bintval = vap->iv_bss->ni_intval;
4040692ad7caSAdrian Chadd
4041692ad7caSAdrian Chadd /*
4042692ad7caSAdrian Chadd * If it's non-zero, we should calculate the minimum of
4043692ad7caSAdrian Chadd * it and the DWELL_BASE.
4044692ad7caSAdrian Chadd *
4045692ad7caSAdrian Chadd * XXX Yes, the math should take into account that bintval
4046692ad7caSAdrian Chadd * is 1.024mS, not 1mS..
4047692ad7caSAdrian Chadd */
4048692ad7caSAdrian Chadd if (bintval > 0) {
4049692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_SCAN, "%s: bintval=%d\n", __func__,
4050692ad7caSAdrian Chadd bintval);
405142946b7dSAdrian Chadd return (MIN(dwell_time, bintval - WPI_CHANNEL_TUNE_TIME * 2));
4052692ad7caSAdrian Chadd }
4053692ad7caSAdrian Chadd
4054692ad7caSAdrian Chadd /* No association context? Default. */
405542946b7dSAdrian Chadd return dwell_time;
4056692ad7caSAdrian Chadd }
4057692ad7caSAdrian Chadd
4058692ad7caSAdrian Chadd static uint16_t
wpi_get_passive_dwell_time(struct wpi_softc * sc,struct ieee80211_channel * c)4059692ad7caSAdrian Chadd wpi_get_passive_dwell_time(struct wpi_softc *sc, struct ieee80211_channel *c)
4060692ad7caSAdrian Chadd {
4061692ad7caSAdrian Chadd uint16_t passive;
4062692ad7caSAdrian Chadd
4063692ad7caSAdrian Chadd if (c == NULL || IEEE80211_IS_CHAN_2GHZ(c))
4064692ad7caSAdrian Chadd passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_2GHZ;
4065692ad7caSAdrian Chadd else
4066692ad7caSAdrian Chadd passive = WPI_PASSIVE_DWELL_BASE + WPI_PASSIVE_DWELL_TIME_5GHZ;
4067692ad7caSAdrian Chadd
4068692ad7caSAdrian Chadd /* Clamp to the beacon interval if we're associated. */
4069692ad7caSAdrian Chadd return (wpi_limit_dwell(sc, passive));
4070692ad7caSAdrian Chadd }
4071692ad7caSAdrian Chadd
4072efccc3c6SAdrian Chadd static uint32_t
wpi_get_scan_pause_time(uint32_t time,uint16_t bintval)4073efccc3c6SAdrian Chadd wpi_get_scan_pause_time(uint32_t time, uint16_t bintval)
4074efccc3c6SAdrian Chadd {
4075efccc3c6SAdrian Chadd uint32_t mod = (time % bintval) * IEEE80211_DUR_TU;
4076efccc3c6SAdrian Chadd uint32_t nbeacons = time / bintval;
4077efccc3c6SAdrian Chadd
4078efccc3c6SAdrian Chadd if (mod > WPI_PAUSE_MAX_TIME)
4079efccc3c6SAdrian Chadd mod = WPI_PAUSE_MAX_TIME;
4080efccc3c6SAdrian Chadd
4081efccc3c6SAdrian Chadd return WPI_PAUSE_SCAN(nbeacons, mod);
4082efccc3c6SAdrian Chadd }
4083efccc3c6SAdrian Chadd
4084692ad7caSAdrian Chadd /*
4085692ad7caSAdrian Chadd * Send a scan request to the firmware.
4086692ad7caSAdrian Chadd */
4087692ad7caSAdrian Chadd static int
wpi_scan(struct wpi_softc * sc,struct ieee80211_channel * c)4088692ad7caSAdrian Chadd wpi_scan(struct wpi_softc *sc, struct ieee80211_channel *c)
4089692ad7caSAdrian Chadd {
40907a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
4091692ad7caSAdrian Chadd struct ieee80211_scan_state *ss = ic->ic_scan;
40923de934a7SAdrian Chadd struct ieee80211vap *vap = ss->ss_vap;
4093692ad7caSAdrian Chadd struct wpi_scan_hdr *hdr;
4094692ad7caSAdrian Chadd struct wpi_cmd_data *tx;
4095692ad7caSAdrian Chadd struct wpi_scan_essid *essids;
4096692ad7caSAdrian Chadd struct wpi_scan_chan *chan;
4097692ad7caSAdrian Chadd struct ieee80211_frame *wh;
4098692ad7caSAdrian Chadd struct ieee80211_rateset *rs;
4099525a3d47SAdrian Chadd uint16_t bintval, buflen, dwell_active, dwell_passive;
4100525a3d47SAdrian Chadd uint8_t *buf, *frm, i, nssid;
4101525a3d47SAdrian Chadd int bgscan, error;
4102692ad7caSAdrian Chadd
4103692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4104692ad7caSAdrian Chadd
4105692ad7caSAdrian Chadd /*
4106692ad7caSAdrian Chadd * We are absolutely not allowed to send a scan command when another
4107692ad7caSAdrian Chadd * scan command is pending.
4108692ad7caSAdrian Chadd */
41090bc39fc6SAdrian Chadd if (callout_pending(&sc->scan_timeout)) {
4110692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: called whilst scanning!\n",
4111692ad7caSAdrian Chadd __func__);
411242946b7dSAdrian Chadd error = EAGAIN;
411342946b7dSAdrian Chadd goto fail;
411442946b7dSAdrian Chadd }
411596dadeb0SAdrian Chadd
411642946b7dSAdrian Chadd bgscan = wpi_check_bss_filter(sc);
411742946b7dSAdrian Chadd bintval = vap->iv_bss->ni_intval;
411842946b7dSAdrian Chadd if (bgscan != 0 &&
411942946b7dSAdrian Chadd bintval < WPI_QUIET_TIME_DEFAULT + WPI_CHANNEL_TUNE_TIME * 2) {
412042946b7dSAdrian Chadd error = EOPNOTSUPP;
412142946b7dSAdrian Chadd goto fail;
4122692ad7caSAdrian Chadd }
4123692ad7caSAdrian Chadd
4124692ad7caSAdrian Chadd buf = malloc(WPI_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
4125692ad7caSAdrian Chadd if (buf == NULL) {
4126692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4127692ad7caSAdrian Chadd "%s: could not allocate buffer for scan command\n",
4128692ad7caSAdrian Chadd __func__);
412996dadeb0SAdrian Chadd error = ENOMEM;
413096dadeb0SAdrian Chadd goto fail;
4131692ad7caSAdrian Chadd }
4132692ad7caSAdrian Chadd hdr = (struct wpi_scan_hdr *)buf;
4133692ad7caSAdrian Chadd
4134692ad7caSAdrian Chadd /*
4135692ad7caSAdrian Chadd * Move to the next channel if no packets are received within 10 msecs
4136692ad7caSAdrian Chadd * after sending the probe request.
4137692ad7caSAdrian Chadd */
413842946b7dSAdrian Chadd hdr->quiet_time = htole16(WPI_QUIET_TIME_DEFAULT);
413942946b7dSAdrian Chadd hdr->quiet_threshold = htole16(1);
4140efccc3c6SAdrian Chadd
4141efccc3c6SAdrian Chadd if (bgscan != 0) {
4142692ad7caSAdrian Chadd /*
4143692ad7caSAdrian Chadd * Max needs to be greater than active and passive and quiet!
4144692ad7caSAdrian Chadd * It's also in microseconds!
4145692ad7caSAdrian Chadd */
4146692ad7caSAdrian Chadd hdr->max_svc = htole32(250 * IEEE80211_DUR_TU);
4147efccc3c6SAdrian Chadd hdr->pause_svc = htole32(wpi_get_scan_pause_time(100,
4148efccc3c6SAdrian Chadd bintval));
4149efccc3c6SAdrian Chadd }
4150efccc3c6SAdrian Chadd
4151692ad7caSAdrian Chadd hdr->filter = htole32(WPI_FILTER_MULTICAST | WPI_FILTER_BEACON);
4152692ad7caSAdrian Chadd
4153692ad7caSAdrian Chadd tx = (struct wpi_cmd_data *)(hdr + 1);
4154692ad7caSAdrian Chadd tx->flags = htole32(WPI_TX_AUTO_SEQ);
4155692ad7caSAdrian Chadd tx->id = WPI_ID_BROADCAST;
4156692ad7caSAdrian Chadd tx->lifetime = htole32(WPI_LIFETIME_INFINITE);
4157692ad7caSAdrian Chadd
4158692ad7caSAdrian Chadd if (IEEE80211_IS_CHAN_5GHZ(c)) {
4159692ad7caSAdrian Chadd /* Send probe requests at 6Mbps. */
4160692ad7caSAdrian Chadd tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_OFDM6];
4161692ad7caSAdrian Chadd rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4162692ad7caSAdrian Chadd } else {
4163692ad7caSAdrian Chadd hdr->flags = htole32(WPI_RXON_24GHZ | WPI_RXON_AUTO);
4164692ad7caSAdrian Chadd /* Send probe requests at 1Mbps. */
4165692ad7caSAdrian Chadd tx->plcp = wpi_ridx_to_plcp[WPI_RIDX_CCK1];
4166692ad7caSAdrian Chadd rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4167692ad7caSAdrian Chadd }
4168692ad7caSAdrian Chadd
4169692ad7caSAdrian Chadd essids = (struct wpi_scan_essid *)(tx + 1);
4170692ad7caSAdrian Chadd nssid = MIN(ss->ss_nssid, WPI_SCAN_MAX_ESSIDS);
4171692ad7caSAdrian Chadd for (i = 0; i < nssid; i++) {
4172692ad7caSAdrian Chadd essids[i].id = IEEE80211_ELEMID_SSID;
4173692ad7caSAdrian Chadd essids[i].len = MIN(ss->ss_ssid[i].len, IEEE80211_NWID_LEN);
4174692ad7caSAdrian Chadd memcpy(essids[i].data, ss->ss_ssid[i].ssid, essids[i].len);
4175692ad7caSAdrian Chadd #ifdef WPI_DEBUG
4176692ad7caSAdrian Chadd if (sc->sc_debug & WPI_DEBUG_SCAN) {
4177692ad7caSAdrian Chadd printf("Scanning Essid: ");
4178692ad7caSAdrian Chadd ieee80211_print_essid(essids[i].data, essids[i].len);
4179692ad7caSAdrian Chadd printf("\n");
4180692ad7caSAdrian Chadd }
4181692ad7caSAdrian Chadd #endif
4182692ad7caSAdrian Chadd }
4183692ad7caSAdrian Chadd
4184692ad7caSAdrian Chadd /*
4185692ad7caSAdrian Chadd * Build a probe request frame. Most of the following code is a
4186692ad7caSAdrian Chadd * copy & paste of what is done in net80211.
4187692ad7caSAdrian Chadd */
4188692ad7caSAdrian Chadd wh = (struct ieee80211_frame *)(essids + WPI_SCAN_MAX_ESSIDS);
4189692ad7caSAdrian Chadd wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4190692ad7caSAdrian Chadd IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4191692ad7caSAdrian Chadd wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
41927a79cebfSGleb Smirnoff IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
41933de934a7SAdrian Chadd IEEE80211_ADDR_COPY(wh->i_addr2, vap->iv_myaddr);
41947a79cebfSGleb Smirnoff IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4195692ad7caSAdrian Chadd
4196692ad7caSAdrian Chadd frm = (uint8_t *)(wh + 1);
4197692ad7caSAdrian Chadd frm = ieee80211_add_ssid(frm, NULL, 0);
4198692ad7caSAdrian Chadd frm = ieee80211_add_rates(frm, rs);
4199692ad7caSAdrian Chadd if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4200692ad7caSAdrian Chadd frm = ieee80211_add_xrates(frm, rs);
4201692ad7caSAdrian Chadd
4202692ad7caSAdrian Chadd /* Set length of probe request. */
4203692ad7caSAdrian Chadd tx->len = htole16(frm - (uint8_t *)wh);
4204692ad7caSAdrian Chadd
4205692ad7caSAdrian Chadd /*
4206692ad7caSAdrian Chadd * Construct information about the channel that we
4207692ad7caSAdrian Chadd * want to scan. The firmware expects this to be directly
4208692ad7caSAdrian Chadd * after the scan probe request
4209692ad7caSAdrian Chadd */
4210692ad7caSAdrian Chadd chan = (struct wpi_scan_chan *)frm;
42114c09e592SAdrian Chadd chan->chan = ieee80211_chan2ieee(ic, c);
4212692ad7caSAdrian Chadd chan->flags = 0;
4213692ad7caSAdrian Chadd if (nssid) {
4214692ad7caSAdrian Chadd hdr->crc_threshold = WPI_SCAN_CRC_TH_DEFAULT;
4215692ad7caSAdrian Chadd chan->flags |= WPI_CHAN_NPBREQS(nssid);
4216692ad7caSAdrian Chadd } else
4217692ad7caSAdrian Chadd hdr->crc_threshold = WPI_SCAN_CRC_TH_NEVER;
4218692ad7caSAdrian Chadd
4219fae61c69SAdrian Chadd if (!IEEE80211_IS_CHAN_PASSIVE(c))
4220692ad7caSAdrian Chadd chan->flags |= WPI_CHAN_ACTIVE;
4221692ad7caSAdrian Chadd
4222692ad7caSAdrian Chadd /*
4223692ad7caSAdrian Chadd * Calculate the active/passive dwell times.
4224692ad7caSAdrian Chadd */
4225692ad7caSAdrian Chadd dwell_active = wpi_get_active_dwell_time(sc, c, nssid);
4226692ad7caSAdrian Chadd dwell_passive = wpi_get_passive_dwell_time(sc, c);
4227692ad7caSAdrian Chadd
4228692ad7caSAdrian Chadd /* Make sure they're valid. */
422942946b7dSAdrian Chadd if (dwell_active > dwell_passive)
423042946b7dSAdrian Chadd dwell_active = dwell_passive;
4231692ad7caSAdrian Chadd
4232692ad7caSAdrian Chadd chan->active = htole16(dwell_active);
4233692ad7caSAdrian Chadd chan->passive = htole16(dwell_passive);
4234692ad7caSAdrian Chadd
4235692ad7caSAdrian Chadd chan->dsp_gain = 0x6e; /* Default level */
4236692ad7caSAdrian Chadd
4237692ad7caSAdrian Chadd if (IEEE80211_IS_CHAN_5GHZ(c))
4238692ad7caSAdrian Chadd chan->rf_gain = 0x3b;
4239692ad7caSAdrian Chadd else
4240692ad7caSAdrian Chadd chan->rf_gain = 0x28;
4241692ad7caSAdrian Chadd
4242692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_SCAN, "Scanning %u Passive: %d\n",
4243fae61c69SAdrian Chadd chan->chan, IEEE80211_IS_CHAN_PASSIVE(c));
4244692ad7caSAdrian Chadd
4245692ad7caSAdrian Chadd hdr->nchan++;
4246e948ddb6SAdrian Chadd
4247e948ddb6SAdrian Chadd if (hdr->nchan == 1 && sc->rxon.chan == chan->chan) {
4248e948ddb6SAdrian Chadd /* XXX Force probe request transmission. */
4249e948ddb6SAdrian Chadd memcpy(chan + 1, chan, sizeof (struct wpi_scan_chan));
4250e948ddb6SAdrian Chadd
4251e948ddb6SAdrian Chadd chan++;
4252e948ddb6SAdrian Chadd
4253e948ddb6SAdrian Chadd /* Reduce unnecessary delay. */
4254e948ddb6SAdrian Chadd chan->flags = 0;
4255e948ddb6SAdrian Chadd chan->passive = chan->active = hdr->quiet_time;
4256e948ddb6SAdrian Chadd
4257e948ddb6SAdrian Chadd hdr->nchan++;
4258e948ddb6SAdrian Chadd }
4259e948ddb6SAdrian Chadd
4260692ad7caSAdrian Chadd chan++;
4261692ad7caSAdrian Chadd
4262692ad7caSAdrian Chadd buflen = (uint8_t *)chan - buf;
4263692ad7caSAdrian Chadd hdr->len = htole16(buflen);
4264692ad7caSAdrian Chadd
4265692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_CMD, "sending scan command nchan=%d\n",
4266692ad7caSAdrian Chadd hdr->nchan);
4267692ad7caSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_SCAN, buf, buflen, 1);
4268692ad7caSAdrian Chadd free(buf, M_DEVBUF);
4269692ad7caSAdrian Chadd
427096dadeb0SAdrian Chadd if (error != 0)
427196dadeb0SAdrian Chadd goto fail;
427296dadeb0SAdrian Chadd
42730bc39fc6SAdrian Chadd callout_reset(&sc->scan_timeout, 5*hz, wpi_scan_timeout, sc);
4274692ad7caSAdrian Chadd
4275692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4276692ad7caSAdrian Chadd
427796dadeb0SAdrian Chadd return 0;
427896dadeb0SAdrian Chadd
427996dadeb0SAdrian Chadd fail: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
428096dadeb0SAdrian Chadd
42816607310bSBenjamin Close return error;
42826607310bSBenjamin Close }
42836607310bSBenjamin Close
4284692ad7caSAdrian Chadd static int
wpi_auth(struct wpi_softc * sc,struct ieee80211vap * vap)4285692ad7caSAdrian Chadd wpi_auth(struct wpi_softc *sc, struct ieee80211vap *vap)
4286692ad7caSAdrian Chadd {
4287692ad7caSAdrian Chadd struct ieee80211com *ic = vap->iv_ic;
4288692ad7caSAdrian Chadd struct ieee80211_node *ni = vap->iv_bss;
42896b997e0cSAdrian Chadd struct ieee80211_channel *c = ni->ni_chan;
4290692ad7caSAdrian Chadd int error;
4291692ad7caSAdrian Chadd
4292446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
4293446685e4SAdrian Chadd
4294692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4295692ad7caSAdrian Chadd
4296692ad7caSAdrian Chadd /* Update adapter configuration. */
4297692ad7caSAdrian Chadd sc->rxon.associd = 0;
4298692ad7caSAdrian Chadd sc->rxon.filter &= ~htole32(WPI_FILTER_BSS);
4299692ad7caSAdrian Chadd IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
43006b997e0cSAdrian Chadd sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4301692ad7caSAdrian Chadd sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
43026b997e0cSAdrian Chadd if (IEEE80211_IS_CHAN_2GHZ(c))
4303692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4304692ad7caSAdrian Chadd if (ic->ic_flags & IEEE80211_F_SHSLOT)
4305692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4306692ad7caSAdrian Chadd if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4307692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
43086b997e0cSAdrian Chadd if (IEEE80211_IS_CHAN_A(c)) {
4309692ad7caSAdrian Chadd sc->rxon.cck_mask = 0;
4310692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0x15;
43116b997e0cSAdrian Chadd } else if (IEEE80211_IS_CHAN_B(c)) {
4312692ad7caSAdrian Chadd sc->rxon.cck_mask = 0x03;
4313692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0;
4314692ad7caSAdrian Chadd } else {
4315692ad7caSAdrian Chadd /* Assume 802.11b/g. */
4316692ad7caSAdrian Chadd sc->rxon.cck_mask = 0x0f;
4317692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0x15;
4318692ad7caSAdrian Chadd }
4319692ad7caSAdrian Chadd
4320692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n",
4321692ad7caSAdrian Chadd sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask,
4322692ad7caSAdrian Chadd sc->rxon.ofdm_mask);
4323692ad7caSAdrian Chadd
4324692ad7caSAdrian Chadd if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
4325692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: could not send RXON\n",
4326692ad7caSAdrian Chadd __func__);
4327692ad7caSAdrian Chadd }
4328692ad7caSAdrian Chadd
4329692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4330692ad7caSAdrian Chadd
4331446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
4332446685e4SAdrian Chadd
4333692ad7caSAdrian Chadd return error;
4334692ad7caSAdrian Chadd }
4335692ad7caSAdrian Chadd
4336692ad7caSAdrian Chadd static int
wpi_config_beacon(struct wpi_vap * wvp)43376f8421f8SAdrian Chadd wpi_config_beacon(struct wpi_vap *wvp)
43386f8421f8SAdrian Chadd {
43390cf00015SAdrian Chadd struct ieee80211vap *vap = &wvp->wv_vap;
43400cf00015SAdrian Chadd struct ieee80211com *ic = vap->iv_ic;
43410cf00015SAdrian Chadd struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
43426f8421f8SAdrian Chadd struct wpi_buf *bcn = &wvp->wv_bcbuf;
43432cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
43446f8421f8SAdrian Chadd struct wpi_cmd_beacon *cmd = (struct wpi_cmd_beacon *)&bcn->data;
43456f8421f8SAdrian Chadd struct ieee80211_tim_ie *tie;
43466f8421f8SAdrian Chadd struct mbuf *m;
43476f8421f8SAdrian Chadd uint8_t *ptr;
43486f8421f8SAdrian Chadd int error;
43496f8421f8SAdrian Chadd
43506f8421f8SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
43516f8421f8SAdrian Chadd
4352b56fed3dSAdrian Chadd WPI_VAP_LOCK_ASSERT(wvp);
43536f8421f8SAdrian Chadd
43546f8421f8SAdrian Chadd cmd->len = htole16(bcn->m->m_pkthdr.len);
43556f8421f8SAdrian Chadd cmd->plcp = (ic->ic_curmode == IEEE80211_MODE_11A) ?
43566f8421f8SAdrian Chadd wpi_ridx_to_plcp[WPI_RIDX_OFDM6] : wpi_ridx_to_plcp[WPI_RIDX_CCK1];
43576f8421f8SAdrian Chadd
43586f8421f8SAdrian Chadd /* XXX seems to be unused */
43596f8421f8SAdrian Chadd if (*(bo->bo_tim) == IEEE80211_ELEMID_TIM) {
43606f8421f8SAdrian Chadd tie = (struct ieee80211_tim_ie *) bo->bo_tim;
43616f8421f8SAdrian Chadd ptr = mtod(bcn->m, uint8_t *);
43626f8421f8SAdrian Chadd
43636f8421f8SAdrian Chadd cmd->tim = htole16(bo->bo_tim - ptr);
43646f8421f8SAdrian Chadd cmd->timsz = tie->tim_len;
43656f8421f8SAdrian Chadd }
43666f8421f8SAdrian Chadd
43676f8421f8SAdrian Chadd /* Necessary for recursion in ieee80211_beacon_update(). */
43686f8421f8SAdrian Chadd m = bcn->m;
43696f8421f8SAdrian Chadd bcn->m = m_dup(m, M_NOWAIT);
43706f8421f8SAdrian Chadd if (bcn->m == NULL) {
43716f8421f8SAdrian Chadd device_printf(sc->sc_dev,
43726f8421f8SAdrian Chadd "%s: could not copy beacon frame\n", __func__);
43736f8421f8SAdrian Chadd error = ENOMEM;
43746f8421f8SAdrian Chadd goto end;
43756f8421f8SAdrian Chadd }
43766f8421f8SAdrian Chadd
43776f8421f8SAdrian Chadd if ((error = wpi_cmd2(sc, bcn)) != 0) {
43786f8421f8SAdrian Chadd device_printf(sc->sc_dev,
43796f8421f8SAdrian Chadd "%s: could not update beacon frame, error %d", __func__,
43806f8421f8SAdrian Chadd error);
4381823e17dbSAdrian Chadd m_freem(bcn->m);
43826f8421f8SAdrian Chadd }
43836f8421f8SAdrian Chadd
43846f8421f8SAdrian Chadd /* Restore mbuf. */
43856f8421f8SAdrian Chadd end: bcn->m = m;
43866f8421f8SAdrian Chadd
43876f8421f8SAdrian Chadd return error;
43886f8421f8SAdrian Chadd }
43896f8421f8SAdrian Chadd
43906f8421f8SAdrian Chadd static int
wpi_setup_beacon(struct wpi_softc * sc,struct ieee80211_node * ni)4391692ad7caSAdrian Chadd wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
4392692ad7caSAdrian Chadd {
43930cf00015SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
43940cf00015SAdrian Chadd struct wpi_vap *wvp = WPI_VAP(vap);
4395692ad7caSAdrian Chadd struct wpi_buf *bcn = &wvp->wv_bcbuf;
4396692ad7caSAdrian Chadd struct mbuf *m;
43976f8421f8SAdrian Chadd int error;
4398692ad7caSAdrian Chadd
4399692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4400692ad7caSAdrian Chadd
4401692ad7caSAdrian Chadd if (ni->ni_chan == IEEE80211_CHAN_ANYC)
4402692ad7caSAdrian Chadd return EINVAL;
4403692ad7caSAdrian Chadd
4404210ab3c2SAdrian Chadd m = ieee80211_beacon_alloc(ni);
4405692ad7caSAdrian Chadd if (m == NULL) {
4406692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4407692ad7caSAdrian Chadd "%s: could not allocate beacon frame\n", __func__);
4408692ad7caSAdrian Chadd return ENOMEM;
4409692ad7caSAdrian Chadd }
4410692ad7caSAdrian Chadd
4411b56fed3dSAdrian Chadd WPI_VAP_LOCK(wvp);
44126f8421f8SAdrian Chadd if (bcn->m != NULL)
44136f8421f8SAdrian Chadd m_freem(bcn->m);
4414692ad7caSAdrian Chadd
4415692ad7caSAdrian Chadd bcn->m = m;
4416692ad7caSAdrian Chadd
44176f8421f8SAdrian Chadd error = wpi_config_beacon(wvp);
4418b56fed3dSAdrian Chadd WPI_VAP_UNLOCK(wvp);
44196f8421f8SAdrian Chadd
44206f8421f8SAdrian Chadd return error;
4421692ad7caSAdrian Chadd }
4422692ad7caSAdrian Chadd
4423692ad7caSAdrian Chadd static void
wpi_update_beacon(struct ieee80211vap * vap,int item)4424692ad7caSAdrian Chadd wpi_update_beacon(struct ieee80211vap *vap, int item)
4425692ad7caSAdrian Chadd {
44262cb71df1SAdrian Chadd struct wpi_softc *sc = vap->iv_ic->ic_softc;
44276f8421f8SAdrian Chadd struct wpi_vap *wvp = WPI_VAP(vap);
44286f8421f8SAdrian Chadd struct wpi_buf *bcn = &wvp->wv_bcbuf;
44290cf00015SAdrian Chadd struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
4430692ad7caSAdrian Chadd struct ieee80211_node *ni = vap->iv_bss;
44316f8421f8SAdrian Chadd int mcast = 0;
44326f8421f8SAdrian Chadd
44336f8421f8SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4434692ad7caSAdrian Chadd
4435b56fed3dSAdrian Chadd WPI_VAP_LOCK(wvp);
44366f8421f8SAdrian Chadd if (bcn->m == NULL) {
4437210ab3c2SAdrian Chadd bcn->m = ieee80211_beacon_alloc(ni);
44386f8421f8SAdrian Chadd if (bcn->m == NULL) {
4439692ad7caSAdrian Chadd device_printf(sc->sc_dev,
44406f8421f8SAdrian Chadd "%s: could not allocate beacon frame\n", __func__);
44416f8421f8SAdrian Chadd
44426f8421f8SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR,
44436f8421f8SAdrian Chadd __func__);
44446f8421f8SAdrian Chadd
4445b56fed3dSAdrian Chadd WPI_VAP_UNLOCK(wvp);
44466f8421f8SAdrian Chadd return;
44476f8421f8SAdrian Chadd }
4448692ad7caSAdrian Chadd }
4449b56fed3dSAdrian Chadd WPI_VAP_UNLOCK(wvp);
44506f8421f8SAdrian Chadd
44516f8421f8SAdrian Chadd if (item == IEEE80211_BEACON_TIM)
44526f8421f8SAdrian Chadd mcast = 1; /* TODO */
44536f8421f8SAdrian Chadd
44546f8421f8SAdrian Chadd setbit(bo->bo_flags, item);
4455210ab3c2SAdrian Chadd ieee80211_beacon_update(ni, bcn->m, mcast);
44566f8421f8SAdrian Chadd
4457b56fed3dSAdrian Chadd WPI_VAP_LOCK(wvp);
44586f8421f8SAdrian Chadd wpi_config_beacon(wvp);
4459b56fed3dSAdrian Chadd WPI_VAP_UNLOCK(wvp);
44606f8421f8SAdrian Chadd
44616f8421f8SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4462692ad7caSAdrian Chadd }
4463692ad7caSAdrian Chadd
4464d138d3baSAdrian Chadd static void
wpi_newassoc(struct ieee80211_node * ni,int isnew)4465d138d3baSAdrian Chadd wpi_newassoc(struct ieee80211_node *ni, int isnew)
4466d138d3baSAdrian Chadd {
4467d138d3baSAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
44682cb71df1SAdrian Chadd struct wpi_softc *sc = ni->ni_ic->ic_softc;
4469d138d3baSAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
4470d138d3baSAdrian Chadd int error;
4471d138d3baSAdrian Chadd
4472d138d3baSAdrian Chadd WPI_NT_LOCK(sc);
4473d138d3baSAdrian Chadd
4474d138d3baSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4475d138d3baSAdrian Chadd
4476d138d3baSAdrian Chadd if (vap->iv_opmode != IEEE80211_M_STA && wn->id == WPI_ID_UNDEFINED) {
4477d138d3baSAdrian Chadd if ((error = wpi_add_ibss_node(sc, ni)) != 0) {
4478d138d3baSAdrian Chadd device_printf(sc->sc_dev,
4479d138d3baSAdrian Chadd "%s: could not add IBSS node, error %d\n",
4480d138d3baSAdrian Chadd __func__, error);
4481d138d3baSAdrian Chadd }
4482d138d3baSAdrian Chadd }
4483d138d3baSAdrian Chadd WPI_NT_UNLOCK(sc);
4484d138d3baSAdrian Chadd }
4485d138d3baSAdrian Chadd
4486692ad7caSAdrian Chadd static int
wpi_run(struct wpi_softc * sc,struct ieee80211vap * vap)4487692ad7caSAdrian Chadd wpi_run(struct wpi_softc *sc, struct ieee80211vap *vap)
4488692ad7caSAdrian Chadd {
4489692ad7caSAdrian Chadd struct ieee80211com *ic = vap->iv_ic;
4490692ad7caSAdrian Chadd struct ieee80211_node *ni = vap->iv_bss;
44916b997e0cSAdrian Chadd struct ieee80211_channel *c = ni->ni_chan;
4492692ad7caSAdrian Chadd int error;
4493692ad7caSAdrian Chadd
4494692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
4495692ad7caSAdrian Chadd
4496692ad7caSAdrian Chadd if (vap->iv_opmode == IEEE80211_M_MONITOR) {
4497692ad7caSAdrian Chadd /* Link LED blinks while monitoring. */
4498692ad7caSAdrian Chadd wpi_set_led(sc, WPI_LED_LINK, 5, 5);
4499692ad7caSAdrian Chadd return 0;
4500692ad7caSAdrian Chadd }
4501692ad7caSAdrian Chadd
4502692ad7caSAdrian Chadd /* XXX kernel panic workaround */
45036b997e0cSAdrian Chadd if (c == IEEE80211_CHAN_ANYC) {
4504692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: incomplete configuration\n",
4505692ad7caSAdrian Chadd __func__);
4506692ad7caSAdrian Chadd return EINVAL;
4507692ad7caSAdrian Chadd }
4508692ad7caSAdrian Chadd
4509692ad7caSAdrian Chadd if ((error = wpi_set_timing(sc, ni)) != 0) {
4510692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4511692ad7caSAdrian Chadd "%s: could not set timing, error %d\n", __func__, error);
4512692ad7caSAdrian Chadd return error;
4513692ad7caSAdrian Chadd }
4514692ad7caSAdrian Chadd
4515692ad7caSAdrian Chadd /* Update adapter configuration. */
4516446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
4517692ad7caSAdrian Chadd IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4518692ad7caSAdrian Chadd sc->rxon.associd = htole16(IEEE80211_NODE_AID(ni));
45196b997e0cSAdrian Chadd sc->rxon.chan = ieee80211_chan2ieee(ic, c);
4520692ad7caSAdrian Chadd sc->rxon.flags = htole32(WPI_RXON_TSF | WPI_RXON_CTS_TO_SELF);
45216b997e0cSAdrian Chadd if (IEEE80211_IS_CHAN_2GHZ(c))
4522692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_AUTO | WPI_RXON_24GHZ);
4523692ad7caSAdrian Chadd if (ic->ic_flags & IEEE80211_F_SHSLOT)
4524692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_SHSLOT);
4525692ad7caSAdrian Chadd if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4526692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_SHPREAMBLE);
45276b997e0cSAdrian Chadd if (IEEE80211_IS_CHAN_A(c)) {
4528692ad7caSAdrian Chadd sc->rxon.cck_mask = 0;
4529692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0x15;
45306b997e0cSAdrian Chadd } else if (IEEE80211_IS_CHAN_B(c)) {
4531692ad7caSAdrian Chadd sc->rxon.cck_mask = 0x03;
4532692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0;
4533692ad7caSAdrian Chadd } else {
4534692ad7caSAdrian Chadd /* Assume 802.11b/g. */
4535692ad7caSAdrian Chadd sc->rxon.cck_mask = 0x0f;
4536692ad7caSAdrian Chadd sc->rxon.ofdm_mask = 0x15;
4537692ad7caSAdrian Chadd }
4538692ad7caSAdrian Chadd sc->rxon.filter |= htole32(WPI_FILTER_BSS);
4539692ad7caSAdrian Chadd
4540692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_STATE, "rxon chan %d flags %x\n",
4541692ad7caSAdrian Chadd sc->rxon.chan, sc->rxon.flags);
4542692ad7caSAdrian Chadd
4543692ad7caSAdrian Chadd if ((error = wpi_send_rxon(sc, 0, 1)) != 0) {
45446d042d7cSMark Johnston WPI_RXON_UNLOCK(sc);
4545692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: could not send RXON\n",
4546692ad7caSAdrian Chadd __func__);
4547692ad7caSAdrian Chadd return error;
4548692ad7caSAdrian Chadd }
4549692ad7caSAdrian Chadd
4550446685e4SAdrian Chadd /* Start periodic calibration timer. */
4551446685e4SAdrian Chadd callout_reset(&sc->calib_to, 60*hz, wpi_calib_timeout, sc);
4552446685e4SAdrian Chadd
4553446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
4554446685e4SAdrian Chadd
4555726ddc26SAdrian Chadd if (vap->iv_opmode == IEEE80211_M_IBSS ||
4556726ddc26SAdrian Chadd vap->iv_opmode == IEEE80211_M_HOSTAP) {
4557692ad7caSAdrian Chadd if ((error = wpi_setup_beacon(sc, ni)) != 0) {
4558692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4559692ad7caSAdrian Chadd "%s: could not setup beacon, error %d\n", __func__,
4560692ad7caSAdrian Chadd error);
4561692ad7caSAdrian Chadd return error;
4562692ad7caSAdrian Chadd }
4563692ad7caSAdrian Chadd }
4564692ad7caSAdrian Chadd
4565692ad7caSAdrian Chadd if (vap->iv_opmode == IEEE80211_M_STA) {
4566692ad7caSAdrian Chadd /* Add BSS node. */
45677964dd56SAdrian Chadd WPI_NT_LOCK(sc);
45680ebca245SAdrian Chadd error = wpi_add_sta_node(sc, ni);
45697964dd56SAdrian Chadd WPI_NT_UNLOCK(sc);
45700ebca245SAdrian Chadd if (error != 0) {
4571692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4572692ad7caSAdrian Chadd "%s: could not add BSS node, error %d\n", __func__,
4573692ad7caSAdrian Chadd error);
4574692ad7caSAdrian Chadd return error;
4575692ad7caSAdrian Chadd }
4576692ad7caSAdrian Chadd }
4577692ad7caSAdrian Chadd
4578692ad7caSAdrian Chadd /* Link LED always on while associated. */
4579692ad7caSAdrian Chadd wpi_set_led(sc, WPI_LED_LINK, 0, 1);
4580692ad7caSAdrian Chadd
4581692ad7caSAdrian Chadd /* Enable power-saving mode if requested by user. */
45820b90e845SAdrian Chadd if ((vap->iv_flags & IEEE80211_F_PMGTON) &&
45830b90e845SAdrian Chadd vap->iv_opmode != IEEE80211_M_IBSS)
4584692ad7caSAdrian Chadd (void)wpi_set_pslevel(sc, 0, 3, 1);
4585692ad7caSAdrian Chadd
4586692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
4587692ad7caSAdrian Chadd
4588692ad7caSAdrian Chadd return 0;
4589692ad7caSAdrian Chadd }
4590692ad7caSAdrian Chadd
4591692ad7caSAdrian Chadd static int
wpi_load_key(struct ieee80211_node * ni,const struct ieee80211_key * k)4592d29fcfb7SAdrian Chadd wpi_load_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4593692ad7caSAdrian Chadd {
4594692ad7caSAdrian Chadd const struct ieee80211_cipher *cip = k->wk_cipher;
4595d29fcfb7SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
45962cb71df1SAdrian Chadd struct wpi_softc *sc = ni->ni_ic->ic_softc;
4597a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
4598692ad7caSAdrian Chadd struct wpi_node_info node;
4599692ad7caSAdrian Chadd uint16_t kflags;
4600692ad7caSAdrian Chadd int error;
4601692ad7caSAdrian Chadd
4602692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4603692ad7caSAdrian Chadd
4604d29fcfb7SAdrian Chadd if (wpi_check_node_entry(sc, wn->id) == 0) {
4605d29fcfb7SAdrian Chadd device_printf(sc->sc_dev, "%s: node does not exist\n",
4606d29fcfb7SAdrian Chadd __func__);
4607d29fcfb7SAdrian Chadd return 0;
4608692ad7caSAdrian Chadd }
4609692ad7caSAdrian Chadd
4610d29fcfb7SAdrian Chadd switch (cip->ic_cipher) {
4611d29fcfb7SAdrian Chadd case IEEE80211_CIPHER_AES_CCM:
4612d29fcfb7SAdrian Chadd kflags = WPI_KFLAG_CCMP;
4613d29fcfb7SAdrian Chadd break;
4614d29fcfb7SAdrian Chadd
4615d29fcfb7SAdrian Chadd default:
4616d29fcfb7SAdrian Chadd device_printf(sc->sc_dev, "%s: unknown cipher %d\n", __func__,
4617d29fcfb7SAdrian Chadd cip->ic_cipher);
4618692ad7caSAdrian Chadd return 0;
4619d29fcfb7SAdrian Chadd }
4620692ad7caSAdrian Chadd
4621692ad7caSAdrian Chadd kflags |= WPI_KFLAG_KID(k->wk_keyix);
4622692ad7caSAdrian Chadd if (k->wk_flags & IEEE80211_KEY_GROUP)
4623692ad7caSAdrian Chadd kflags |= WPI_KFLAG_MULTICAST;
4624692ad7caSAdrian Chadd
46256607310bSBenjamin Close memset(&node, 0, sizeof node);
4626692ad7caSAdrian Chadd node.id = wn->id;
4627692ad7caSAdrian Chadd node.control = WPI_NODE_UPDATE;
4628692ad7caSAdrian Chadd node.flags = WPI_FLAG_KEY_SET;
4629692ad7caSAdrian Chadd node.kflags = htole16(kflags);
4630692ad7caSAdrian Chadd memcpy(node.key, k->wk_key, k->wk_keylen);
4631d29fcfb7SAdrian Chadd again:
4632d29fcfb7SAdrian Chadd DPRINTF(sc, WPI_DEBUG_KEY,
4633d29fcfb7SAdrian Chadd "%s: setting %s key id %d for node %d (%s)\n", __func__,
4634d29fcfb7SAdrian Chadd (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast", k->wk_keyix,
4635d29fcfb7SAdrian Chadd node.id, ether_sprintf(ni->ni_macaddr));
4636692ad7caSAdrian Chadd
4637692ad7caSAdrian Chadd error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
46386607310bSBenjamin Close if (error != 0) {
4639692ad7caSAdrian Chadd device_printf(sc->sc_dev, "can't update node info, error %d\n",
4640692ad7caSAdrian Chadd error);
4641d29fcfb7SAdrian Chadd return !error;
4642d29fcfb7SAdrian Chadd }
4643d29fcfb7SAdrian Chadd
46442589197aSAdrian Chadd if (!(kflags & WPI_KFLAG_MULTICAST) &&
46452589197aSAdrian Chadd ieee80211_is_key_global(vap, k)) {
4646d29fcfb7SAdrian Chadd kflags |= WPI_KFLAG_MULTICAST;
4647d29fcfb7SAdrian Chadd node.kflags = htole16(kflags);
4648d29fcfb7SAdrian Chadd
4649d29fcfb7SAdrian Chadd goto again;
4650692ad7caSAdrian Chadd }
4651692ad7caSAdrian Chadd
4652692ad7caSAdrian Chadd return 1;
4653692ad7caSAdrian Chadd }
4654692ad7caSAdrian Chadd
4655d29fcfb7SAdrian Chadd static void
wpi_load_key_cb(void * arg,struct ieee80211_node * ni)4656d29fcfb7SAdrian Chadd wpi_load_key_cb(void *arg, struct ieee80211_node *ni)
4657d29fcfb7SAdrian Chadd {
4658d29fcfb7SAdrian Chadd const struct ieee80211_key *k = arg;
4659d29fcfb7SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
46602cb71df1SAdrian Chadd struct wpi_softc *sc = ni->ni_ic->ic_softc;
4661d29fcfb7SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
4662d29fcfb7SAdrian Chadd int error;
4663d29fcfb7SAdrian Chadd
4664d29fcfb7SAdrian Chadd if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4665d29fcfb7SAdrian Chadd return;
4666d29fcfb7SAdrian Chadd
4667d29fcfb7SAdrian Chadd WPI_NT_LOCK(sc);
4668d29fcfb7SAdrian Chadd error = wpi_load_key(ni, k);
4669d29fcfb7SAdrian Chadd WPI_NT_UNLOCK(sc);
4670d29fcfb7SAdrian Chadd
4671d29fcfb7SAdrian Chadd if (error == 0) {
4672d29fcfb7SAdrian Chadd device_printf(sc->sc_dev, "%s: error while setting key\n",
4673d29fcfb7SAdrian Chadd __func__);
4674d29fcfb7SAdrian Chadd }
4675d29fcfb7SAdrian Chadd }
4676d29fcfb7SAdrian Chadd
4677692ad7caSAdrian Chadd static int
wpi_set_global_keys(struct ieee80211_node * ni)4678d29fcfb7SAdrian Chadd wpi_set_global_keys(struct ieee80211_node *ni)
4679692ad7caSAdrian Chadd {
4680d29fcfb7SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
4681d29fcfb7SAdrian Chadd struct ieee80211_key *wk = &vap->iv_nw_keys[0];
4682d29fcfb7SAdrian Chadd int error = 1;
4683d29fcfb7SAdrian Chadd
4684d29fcfb7SAdrian Chadd for (; wk < &vap->iv_nw_keys[IEEE80211_WEP_NKID] && error; wk++)
4685d29fcfb7SAdrian Chadd if (wk->wk_keyix != IEEE80211_KEYIX_NONE)
4686d29fcfb7SAdrian Chadd error = wpi_load_key(ni, wk);
4687d29fcfb7SAdrian Chadd
4688d29fcfb7SAdrian Chadd return !error;
4689d29fcfb7SAdrian Chadd }
4690d29fcfb7SAdrian Chadd
4691d29fcfb7SAdrian Chadd static int
wpi_del_key(struct ieee80211_node * ni,const struct ieee80211_key * k)4692d29fcfb7SAdrian Chadd wpi_del_key(struct ieee80211_node *ni, const struct ieee80211_key *k)
4693d29fcfb7SAdrian Chadd {
4694d29fcfb7SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
46952cb71df1SAdrian Chadd struct wpi_softc *sc = ni->ni_ic->ic_softc;
4696a5e093b0SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
4697692ad7caSAdrian Chadd struct wpi_node_info node;
4698d29fcfb7SAdrian Chadd uint16_t kflags;
4699d29fcfb7SAdrian Chadd int error;
4700692ad7caSAdrian Chadd
4701692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4702692ad7caSAdrian Chadd
4703d29fcfb7SAdrian Chadd if (wpi_check_node_entry(sc, wn->id) == 0) {
4704d29fcfb7SAdrian Chadd DPRINTF(sc, WPI_DEBUG_KEY, "%s: node was removed\n", __func__);
4705d29fcfb7SAdrian Chadd return 1; /* Nothing to do. */
4706692ad7caSAdrian Chadd }
4707692ad7caSAdrian Chadd
4708d29fcfb7SAdrian Chadd kflags = WPI_KFLAG_KID(k->wk_keyix);
4709d29fcfb7SAdrian Chadd if (k->wk_flags & IEEE80211_KEY_GROUP)
4710d29fcfb7SAdrian Chadd kflags |= WPI_KFLAG_MULTICAST;
4711692ad7caSAdrian Chadd
4712692ad7caSAdrian Chadd memset(&node, 0, sizeof node);
4713692ad7caSAdrian Chadd node.id = wn->id;
4714692ad7caSAdrian Chadd node.control = WPI_NODE_UPDATE;
4715692ad7caSAdrian Chadd node.flags = WPI_FLAG_KEY_SET;
4716d29fcfb7SAdrian Chadd node.kflags = htole16(kflags);
4717d29fcfb7SAdrian Chadd again:
4718d29fcfb7SAdrian Chadd DPRINTF(sc, WPI_DEBUG_KEY, "%s: deleting %s key %d for node %d (%s)\n",
4719d29fcfb7SAdrian Chadd __func__, (kflags & WPI_KFLAG_MULTICAST) ? "group" : "ucast",
4720d29fcfb7SAdrian Chadd k->wk_keyix, node.id, ether_sprintf(ni->ni_macaddr));
4721692ad7caSAdrian Chadd
4722d29fcfb7SAdrian Chadd error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
4723d29fcfb7SAdrian Chadd if (error != 0) {
4724d29fcfb7SAdrian Chadd device_printf(sc->sc_dev, "can't update node info, error %d\n",
4725d29fcfb7SAdrian Chadd error);
4726d29fcfb7SAdrian Chadd return !error;
4727d29fcfb7SAdrian Chadd }
4728d29fcfb7SAdrian Chadd
47292589197aSAdrian Chadd if (!(kflags & WPI_KFLAG_MULTICAST) &&
47302589197aSAdrian Chadd ieee80211_is_key_global(vap, k)) {
4731d29fcfb7SAdrian Chadd kflags |= WPI_KFLAG_MULTICAST;
4732d29fcfb7SAdrian Chadd node.kflags = htole16(kflags);
4733d29fcfb7SAdrian Chadd
4734d29fcfb7SAdrian Chadd goto again;
4735d29fcfb7SAdrian Chadd }
4736692ad7caSAdrian Chadd
4737692ad7caSAdrian Chadd return 1;
4738692ad7caSAdrian Chadd }
4739692ad7caSAdrian Chadd
4740d29fcfb7SAdrian Chadd static void
wpi_del_key_cb(void * arg,struct ieee80211_node * ni)4741d29fcfb7SAdrian Chadd wpi_del_key_cb(void *arg, struct ieee80211_node *ni)
4742d29fcfb7SAdrian Chadd {
4743d29fcfb7SAdrian Chadd const struct ieee80211_key *k = arg;
4744d29fcfb7SAdrian Chadd struct ieee80211vap *vap = ni->ni_vap;
47452cb71df1SAdrian Chadd struct wpi_softc *sc = ni->ni_ic->ic_softc;
4746d29fcfb7SAdrian Chadd struct wpi_node *wn = WPI_NODE(ni);
4747d29fcfb7SAdrian Chadd int error;
4748d29fcfb7SAdrian Chadd
4749d29fcfb7SAdrian Chadd if (vap->iv_bss == ni && wn->id == WPI_ID_UNDEFINED)
4750d29fcfb7SAdrian Chadd return;
4751d29fcfb7SAdrian Chadd
4752d29fcfb7SAdrian Chadd WPI_NT_LOCK(sc);
4753d29fcfb7SAdrian Chadd error = wpi_del_key(ni, k);
4754d29fcfb7SAdrian Chadd WPI_NT_UNLOCK(sc);
4755d29fcfb7SAdrian Chadd
4756d29fcfb7SAdrian Chadd if (error == 0) {
4757d29fcfb7SAdrian Chadd device_printf(sc->sc_dev, "%s: error while deleting key\n",
4758d29fcfb7SAdrian Chadd __func__);
4759d29fcfb7SAdrian Chadd }
4760d29fcfb7SAdrian Chadd }
4761d29fcfb7SAdrian Chadd
4762d29fcfb7SAdrian Chadd static int
wpi_process_key(struct ieee80211vap * vap,const struct ieee80211_key * k,int set)4763d29fcfb7SAdrian Chadd wpi_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
4764d29fcfb7SAdrian Chadd int set)
4765d29fcfb7SAdrian Chadd {
4766d29fcfb7SAdrian Chadd struct ieee80211com *ic = vap->iv_ic;
47672cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
4768d29fcfb7SAdrian Chadd struct wpi_vap *wvp = WPI_VAP(vap);
4769d29fcfb7SAdrian Chadd struct ieee80211_node *ni;
4770d29fcfb7SAdrian Chadd int error, ni_ref = 0;
4771d29fcfb7SAdrian Chadd
4772d29fcfb7SAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4773d29fcfb7SAdrian Chadd
4774d29fcfb7SAdrian Chadd if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
4775d29fcfb7SAdrian Chadd /* Not for us. */
4776d29fcfb7SAdrian Chadd return 1;
4777d29fcfb7SAdrian Chadd }
4778d29fcfb7SAdrian Chadd
4779d29fcfb7SAdrian Chadd if (!(k->wk_flags & IEEE80211_KEY_RECV)) {
4780d29fcfb7SAdrian Chadd /* XMIT keys are handled in wpi_tx_data(). */
4781d29fcfb7SAdrian Chadd return 1;
4782d29fcfb7SAdrian Chadd }
4783d29fcfb7SAdrian Chadd
4784d29fcfb7SAdrian Chadd /* Handle group keys. */
47852589197aSAdrian Chadd if (ieee80211_is_key_global(vap, k)) {
4786d29fcfb7SAdrian Chadd WPI_NT_LOCK(sc);
4787d29fcfb7SAdrian Chadd if (set)
4788d29fcfb7SAdrian Chadd wvp->wv_gtk |= WPI_VAP_KEY(k->wk_keyix);
4789d29fcfb7SAdrian Chadd else
4790d29fcfb7SAdrian Chadd wvp->wv_gtk &= ~WPI_VAP_KEY(k->wk_keyix);
4791d29fcfb7SAdrian Chadd WPI_NT_UNLOCK(sc);
4792d29fcfb7SAdrian Chadd
4793d29fcfb7SAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN) {
4794d29fcfb7SAdrian Chadd ieee80211_iterate_nodes(&ic->ic_sta,
479569a2ec4aSKonstantin Belousov set ? wpi_load_key_cb : wpi_del_key_cb,
479669a2ec4aSKonstantin Belousov __DECONST(void *, k));
4797d29fcfb7SAdrian Chadd }
4798d29fcfb7SAdrian Chadd
4799d29fcfb7SAdrian Chadd return 1;
4800d29fcfb7SAdrian Chadd }
4801d29fcfb7SAdrian Chadd
4802d29fcfb7SAdrian Chadd switch (vap->iv_opmode) {
4803d29fcfb7SAdrian Chadd case IEEE80211_M_STA:
4804d29fcfb7SAdrian Chadd ni = vap->iv_bss;
4805d29fcfb7SAdrian Chadd break;
4806d29fcfb7SAdrian Chadd
4807d29fcfb7SAdrian Chadd case IEEE80211_M_IBSS:
4808d29fcfb7SAdrian Chadd case IEEE80211_M_AHDEMO:
4809726ddc26SAdrian Chadd case IEEE80211_M_HOSTAP:
4810d29fcfb7SAdrian Chadd ni = ieee80211_find_vap_node(&ic->ic_sta, vap, k->wk_macaddr);
4811d29fcfb7SAdrian Chadd if (ni == NULL)
4812d29fcfb7SAdrian Chadd return 0; /* should not happen */
4813d29fcfb7SAdrian Chadd
4814d29fcfb7SAdrian Chadd ni_ref = 1;
4815d29fcfb7SAdrian Chadd break;
4816d29fcfb7SAdrian Chadd
4817d29fcfb7SAdrian Chadd default:
4818d29fcfb7SAdrian Chadd device_printf(sc->sc_dev, "%s: unknown opmode %d\n", __func__,
4819d29fcfb7SAdrian Chadd vap->iv_opmode);
4820d29fcfb7SAdrian Chadd return 0;
4821d29fcfb7SAdrian Chadd }
4822d29fcfb7SAdrian Chadd
4823d29fcfb7SAdrian Chadd WPI_NT_LOCK(sc);
4824d29fcfb7SAdrian Chadd if (set)
4825d29fcfb7SAdrian Chadd error = wpi_load_key(ni, k);
4826d29fcfb7SAdrian Chadd else
4827d29fcfb7SAdrian Chadd error = wpi_del_key(ni, k);
4828d29fcfb7SAdrian Chadd WPI_NT_UNLOCK(sc);
4829d29fcfb7SAdrian Chadd
4830d29fcfb7SAdrian Chadd if (ni_ref)
4831d29fcfb7SAdrian Chadd ieee80211_node_decref(ni);
4832d29fcfb7SAdrian Chadd
4833d29fcfb7SAdrian Chadd return error;
4834d29fcfb7SAdrian Chadd }
4835d29fcfb7SAdrian Chadd
4836d29fcfb7SAdrian Chadd static int
wpi_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)4837bc813c40SAdrian Chadd wpi_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
4838d29fcfb7SAdrian Chadd {
4839d29fcfb7SAdrian Chadd return wpi_process_key(vap, k, 1);
4840d29fcfb7SAdrian Chadd }
4841d29fcfb7SAdrian Chadd
4842d29fcfb7SAdrian Chadd static int
wpi_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)4843d29fcfb7SAdrian Chadd wpi_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
4844d29fcfb7SAdrian Chadd {
4845d29fcfb7SAdrian Chadd return wpi_process_key(vap, k, 0);
4846d29fcfb7SAdrian Chadd }
4847d29fcfb7SAdrian Chadd
4848692ad7caSAdrian Chadd /*
4849692ad7caSAdrian Chadd * This function is called after the runtime firmware notifies us of its
4850692ad7caSAdrian Chadd * readiness (called in a process context).
4851692ad7caSAdrian Chadd */
4852692ad7caSAdrian Chadd static int
wpi_post_alive(struct wpi_softc * sc)4853692ad7caSAdrian Chadd wpi_post_alive(struct wpi_softc *sc)
4854692ad7caSAdrian Chadd {
4855692ad7caSAdrian Chadd int ntries, error;
4856692ad7caSAdrian Chadd
4857692ad7caSAdrian Chadd /* Check (again) that the radio is not disabled. */
4858692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
4859692ad7caSAdrian Chadd return error;
4860692ad7caSAdrian Chadd
4861692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4862692ad7caSAdrian Chadd
4863692ad7caSAdrian Chadd /* NB: Runtime firmware must be up and running. */
4864692ad7caSAdrian Chadd if (!(wpi_prph_read(sc, WPI_APMG_RFKILL) & 1)) {
4865692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4866692ad7caSAdrian Chadd "RF switch: radio disabled (%s)\n", __func__);
4867692ad7caSAdrian Chadd wpi_nic_unlock(sc);
4868692ad7caSAdrian Chadd return EPERM; /* :-) */
4869692ad7caSAdrian Chadd }
4870692ad7caSAdrian Chadd wpi_nic_unlock(sc);
4871692ad7caSAdrian Chadd
4872692ad7caSAdrian Chadd /* Wait for thermal sensor to calibrate. */
4873692ad7caSAdrian Chadd for (ntries = 0; ntries < 1000; ntries++) {
4874692ad7caSAdrian Chadd if ((sc->temp = (int)WPI_READ(sc, WPI_UCODE_GP2)) != 0)
4875692ad7caSAdrian Chadd break;
4876692ad7caSAdrian Chadd DELAY(10);
4877692ad7caSAdrian Chadd }
4878692ad7caSAdrian Chadd
4879692ad7caSAdrian Chadd if (ntries == 1000) {
4880692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4881692ad7caSAdrian Chadd "timeout waiting for thermal sensor calibration\n");
4882692ad7caSAdrian Chadd return ETIMEDOUT;
4883692ad7caSAdrian Chadd }
4884692ad7caSAdrian Chadd
4885692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TEMP, "temperature %d\n", sc->temp);
4886692ad7caSAdrian Chadd return 0;
4887692ad7caSAdrian Chadd }
4888692ad7caSAdrian Chadd
4889692ad7caSAdrian Chadd /*
4890692ad7caSAdrian Chadd * The firmware boot code is small and is intended to be copied directly into
4891692ad7caSAdrian Chadd * the NIC internal memory (no DMA transfer).
4892692ad7caSAdrian Chadd */
4893692ad7caSAdrian Chadd static int
wpi_load_bootcode(struct wpi_softc * sc,const uint8_t * ucode,uint32_t size)4894525a3d47SAdrian Chadd wpi_load_bootcode(struct wpi_softc *sc, const uint8_t *ucode, uint32_t size)
4895692ad7caSAdrian Chadd {
4896692ad7caSAdrian Chadd int error, ntries;
4897692ad7caSAdrian Chadd
4898692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_HW, "Loading microcode size 0x%x\n", size);
4899692ad7caSAdrian Chadd
4900692ad7caSAdrian Chadd size /= sizeof (uint32_t);
4901692ad7caSAdrian Chadd
4902692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
4903692ad7caSAdrian Chadd return error;
4904692ad7caSAdrian Chadd
4905692ad7caSAdrian Chadd /* Copy microcode image into NIC memory. */
4906692ad7caSAdrian Chadd wpi_prph_write_region_4(sc, WPI_BSM_SRAM_BASE,
4907692ad7caSAdrian Chadd (const uint32_t *)ucode, size);
4908692ad7caSAdrian Chadd
4909692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_WR_MEM_SRC, 0);
4910692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_WR_MEM_DST, WPI_FW_TEXT_BASE);
4911692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_WR_DWCOUNT, size);
4912692ad7caSAdrian Chadd
4913692ad7caSAdrian Chadd /* Start boot load now. */
4914692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START);
4915692ad7caSAdrian Chadd
4916692ad7caSAdrian Chadd /* Wait for transfer to complete. */
4917692ad7caSAdrian Chadd for (ntries = 0; ntries < 1000; ntries++) {
4918692ad7caSAdrian Chadd uint32_t status = WPI_READ(sc, WPI_FH_TX_STATUS);
4919692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_HW,
4920692ad7caSAdrian Chadd "firmware status=0x%x, val=0x%x, result=0x%x\n", status,
4921692ad7caSAdrian Chadd WPI_FH_TX_STATUS_IDLE(6),
4922692ad7caSAdrian Chadd status & WPI_FH_TX_STATUS_IDLE(6));
4923692ad7caSAdrian Chadd if (status & WPI_FH_TX_STATUS_IDLE(6)) {
4924692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_HW,
4925692ad7caSAdrian Chadd "Status Match! - ntries = %d\n", ntries);
4926692ad7caSAdrian Chadd break;
4927692ad7caSAdrian Chadd }
4928692ad7caSAdrian Chadd DELAY(10);
4929692ad7caSAdrian Chadd }
4930692ad7caSAdrian Chadd if (ntries == 1000) {
4931692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4932692ad7caSAdrian Chadd __func__);
4933692ad7caSAdrian Chadd wpi_nic_unlock(sc);
4934692ad7caSAdrian Chadd return ETIMEDOUT;
4935692ad7caSAdrian Chadd }
4936692ad7caSAdrian Chadd
4937692ad7caSAdrian Chadd /* Enable boot after power up. */
4938692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_WR_CTRL, WPI_BSM_WR_CTRL_START_EN);
4939692ad7caSAdrian Chadd
4940692ad7caSAdrian Chadd wpi_nic_unlock(sc);
4941692ad7caSAdrian Chadd return 0;
4942692ad7caSAdrian Chadd }
4943692ad7caSAdrian Chadd
4944692ad7caSAdrian Chadd static int
wpi_load_firmware(struct wpi_softc * sc)4945692ad7caSAdrian Chadd wpi_load_firmware(struct wpi_softc *sc)
4946692ad7caSAdrian Chadd {
4947692ad7caSAdrian Chadd struct wpi_fw_info *fw = &sc->fw;
4948692ad7caSAdrian Chadd struct wpi_dma_info *dma = &sc->fw_dma;
4949692ad7caSAdrian Chadd int error;
4950692ad7caSAdrian Chadd
4951692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
4952692ad7caSAdrian Chadd
4953692ad7caSAdrian Chadd /* Copy initialization sections into pre-allocated DMA-safe memory. */
4954692ad7caSAdrian Chadd memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
4955692ad7caSAdrian Chadd bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4956692ad7caSAdrian Chadd memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->init.text, fw->init.textsz);
4957692ad7caSAdrian Chadd bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4958692ad7caSAdrian Chadd
4959692ad7caSAdrian Chadd /* Tell adapter where to find initialization sections. */
4960692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
4961692ad7caSAdrian Chadd return error;
4962692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4963692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->init.datasz);
4964692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
4965692ad7caSAdrian Chadd dma->paddr + WPI_FW_DATA_MAXSZ);
4966692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
4967692ad7caSAdrian Chadd wpi_nic_unlock(sc);
4968692ad7caSAdrian Chadd
4969692ad7caSAdrian Chadd /* Load firmware boot code. */
4970692ad7caSAdrian Chadd error = wpi_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
4971692ad7caSAdrian Chadd if (error != 0) {
4972692ad7caSAdrian Chadd device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
4973692ad7caSAdrian Chadd __func__);
49746607310bSBenjamin Close return error;
49756607310bSBenjamin Close }
49766607310bSBenjamin Close
4977692ad7caSAdrian Chadd /* Now press "execute". */
4978692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_RESET, 0);
4979692ad7caSAdrian Chadd
4980692ad7caSAdrian Chadd /* Wait at most one second for first alive notification. */
498176fdf7ebSAdrian Chadd if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
4982692ad7caSAdrian Chadd device_printf(sc->sc_dev,
4983692ad7caSAdrian Chadd "%s: timeout waiting for adapter to initialize, error %d\n",
4984692ad7caSAdrian Chadd __func__, error);
49856607310bSBenjamin Close return error;
49866607310bSBenjamin Close }
49876607310bSBenjamin Close
4988692ad7caSAdrian Chadd /* Copy runtime sections into pre-allocated DMA-safe memory. */
4989692ad7caSAdrian Chadd memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
4990692ad7caSAdrian Chadd bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4991692ad7caSAdrian Chadd memcpy(dma->vaddr + WPI_FW_DATA_MAXSZ, fw->main.text, fw->main.textsz);
4992692ad7caSAdrian Chadd bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
4993692ad7caSAdrian Chadd
4994692ad7caSAdrian Chadd /* Tell adapter where to find runtime sections. */
4995692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
4996692ad7caSAdrian Chadd return error;
4997692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_DATA_ADDR, dma->paddr);
4998692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_DATA_SIZE, fw->main.datasz);
4999692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_ADDR,
5000692ad7caSAdrian Chadd dma->paddr + WPI_FW_DATA_MAXSZ);
5001692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_BSM_DRAM_TEXT_SIZE,
5002692ad7caSAdrian Chadd WPI_FW_UPDATED | fw->main.textsz);
5003692ad7caSAdrian Chadd wpi_nic_unlock(sc);
5004692ad7caSAdrian Chadd
5005692ad7caSAdrian Chadd return 0;
5006692ad7caSAdrian Chadd }
5007692ad7caSAdrian Chadd
5008692ad7caSAdrian Chadd static int
wpi_read_firmware(struct wpi_softc * sc)5009692ad7caSAdrian Chadd wpi_read_firmware(struct wpi_softc *sc)
5010692ad7caSAdrian Chadd {
5011692ad7caSAdrian Chadd const struct firmware *fp;
5012692ad7caSAdrian Chadd struct wpi_fw_info *fw = &sc->fw;
5013692ad7caSAdrian Chadd const struct wpi_firmware_hdr *hdr;
5014692ad7caSAdrian Chadd int error;
5015692ad7caSAdrian Chadd
5016692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5017692ad7caSAdrian Chadd
5018692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE,
5019692ad7caSAdrian Chadd "Attempting Loading Firmware from %s module\n", WPI_FW_NAME);
5020692ad7caSAdrian Chadd
5021692ad7caSAdrian Chadd WPI_UNLOCK(sc);
5022692ad7caSAdrian Chadd fp = firmware_get(WPI_FW_NAME);
5023692ad7caSAdrian Chadd WPI_LOCK(sc);
5024692ad7caSAdrian Chadd
5025692ad7caSAdrian Chadd if (fp == NULL) {
5026692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5027692ad7caSAdrian Chadd "could not load firmware image '%s'\n", WPI_FW_NAME);
5028692ad7caSAdrian Chadd return EINVAL;
5029692ad7caSAdrian Chadd }
5030692ad7caSAdrian Chadd
5031692ad7caSAdrian Chadd sc->fw_fp = fp;
5032692ad7caSAdrian Chadd
5033692ad7caSAdrian Chadd if (fp->datasize < sizeof (struct wpi_firmware_hdr)) {
5034692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5035692ad7caSAdrian Chadd "firmware file too short: %zu bytes\n", fp->datasize);
5036692ad7caSAdrian Chadd error = EINVAL;
5037692ad7caSAdrian Chadd goto fail;
5038692ad7caSAdrian Chadd }
5039692ad7caSAdrian Chadd
5040692ad7caSAdrian Chadd fw->size = fp->datasize;
5041692ad7caSAdrian Chadd fw->data = (const uint8_t *)fp->data;
5042692ad7caSAdrian Chadd
5043692ad7caSAdrian Chadd /* Extract firmware header information. */
5044692ad7caSAdrian Chadd hdr = (const struct wpi_firmware_hdr *)fw->data;
5045692ad7caSAdrian Chadd
5046692ad7caSAdrian Chadd /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW |
5047692ad7caSAdrian Chadd |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */
5048692ad7caSAdrian Chadd
5049692ad7caSAdrian Chadd fw->main.textsz = le32toh(hdr->rtextsz);
5050692ad7caSAdrian Chadd fw->main.datasz = le32toh(hdr->rdatasz);
5051692ad7caSAdrian Chadd fw->init.textsz = le32toh(hdr->itextsz);
5052692ad7caSAdrian Chadd fw->init.datasz = le32toh(hdr->idatasz);
5053692ad7caSAdrian Chadd fw->boot.textsz = le32toh(hdr->btextsz);
5054692ad7caSAdrian Chadd fw->boot.datasz = 0;
5055692ad7caSAdrian Chadd
5056692ad7caSAdrian Chadd /* Sanity-check firmware header. */
5057692ad7caSAdrian Chadd if (fw->main.textsz > WPI_FW_TEXT_MAXSZ ||
5058692ad7caSAdrian Chadd fw->main.datasz > WPI_FW_DATA_MAXSZ ||
5059692ad7caSAdrian Chadd fw->init.textsz > WPI_FW_TEXT_MAXSZ ||
5060692ad7caSAdrian Chadd fw->init.datasz > WPI_FW_DATA_MAXSZ ||
5061692ad7caSAdrian Chadd fw->boot.textsz > WPI_FW_BOOT_TEXT_MAXSZ ||
5062692ad7caSAdrian Chadd (fw->boot.textsz & 3) != 0) {
5063692ad7caSAdrian Chadd device_printf(sc->sc_dev, "invalid firmware header\n");
5064692ad7caSAdrian Chadd error = EINVAL;
5065692ad7caSAdrian Chadd goto fail;
5066692ad7caSAdrian Chadd }
5067692ad7caSAdrian Chadd
5068692ad7caSAdrian Chadd /* Check that all firmware sections fit. */
5069692ad7caSAdrian Chadd if (fw->size < sizeof (*hdr) + fw->main.textsz + fw->main.datasz +
5070692ad7caSAdrian Chadd fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
5071692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5072692ad7caSAdrian Chadd "firmware file too short: %zu bytes\n", fw->size);
5073692ad7caSAdrian Chadd error = EINVAL;
5074692ad7caSAdrian Chadd goto fail;
5075692ad7caSAdrian Chadd }
5076692ad7caSAdrian Chadd
5077692ad7caSAdrian Chadd /* Get pointers to firmware sections. */
5078692ad7caSAdrian Chadd fw->main.text = (const uint8_t *)(hdr + 1);
5079692ad7caSAdrian Chadd fw->main.data = fw->main.text + fw->main.textsz;
5080692ad7caSAdrian Chadd fw->init.text = fw->main.data + fw->main.datasz;
5081692ad7caSAdrian Chadd fw->init.data = fw->init.text + fw->init.textsz;
5082692ad7caSAdrian Chadd fw->boot.text = fw->init.data + fw->init.datasz;
5083692ad7caSAdrian Chadd
5084692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE,
5085692ad7caSAdrian Chadd "Firmware Version: Major %d, Minor %d, Driver %d, \n"
50867167f5fcSAdrian Chadd "runtime (text: %u, data: %u) init (text: %u, data %u) "
50877167f5fcSAdrian Chadd "boot (text %u)\n", hdr->major, hdr->minor, le32toh(hdr->driver),
5088692ad7caSAdrian Chadd fw->main.textsz, fw->main.datasz,
5089692ad7caSAdrian Chadd fw->init.textsz, fw->init.datasz, fw->boot.textsz);
5090692ad7caSAdrian Chadd
5091692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.text %p\n", fw->main.text);
5092692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->main.data %p\n", fw->main.data);
5093692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.text %p\n", fw->init.text);
5094692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->init.data %p\n", fw->init.data);
5095692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_FIRMWARE, "fw->boot.text %p\n", fw->boot.text);
5096692ad7caSAdrian Chadd
5097692ad7caSAdrian Chadd return 0;
5098692ad7caSAdrian Chadd
5099692ad7caSAdrian Chadd fail: wpi_unload_firmware(sc);
5100692ad7caSAdrian Chadd return error;
5101692ad7caSAdrian Chadd }
5102692ad7caSAdrian Chadd
5103692ad7caSAdrian Chadd /**
5104692ad7caSAdrian Chadd * Free the referenced firmware image
5105692ad7caSAdrian Chadd */
5106692ad7caSAdrian Chadd static void
wpi_unload_firmware(struct wpi_softc * sc)5107692ad7caSAdrian Chadd wpi_unload_firmware(struct wpi_softc *sc)
5108692ad7caSAdrian Chadd {
5109692ad7caSAdrian Chadd if (sc->fw_fp != NULL) {
5110692ad7caSAdrian Chadd firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
5111692ad7caSAdrian Chadd sc->fw_fp = NULL;
5112692ad7caSAdrian Chadd }
5113692ad7caSAdrian Chadd }
5114692ad7caSAdrian Chadd
5115692ad7caSAdrian Chadd static int
wpi_clock_wait(struct wpi_softc * sc)5116692ad7caSAdrian Chadd wpi_clock_wait(struct wpi_softc *sc)
5117692ad7caSAdrian Chadd {
5118692ad7caSAdrian Chadd int ntries;
5119692ad7caSAdrian Chadd
5120692ad7caSAdrian Chadd /* Set "initialization complete" bit. */
5121692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5122692ad7caSAdrian Chadd
5123692ad7caSAdrian Chadd /* Wait for clock stabilization. */
5124692ad7caSAdrian Chadd for (ntries = 0; ntries < 2500; ntries++) {
5125692ad7caSAdrian Chadd if (WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_MAC_CLOCK_READY)
5126692ad7caSAdrian Chadd return 0;
5127692ad7caSAdrian Chadd DELAY(100);
5128692ad7caSAdrian Chadd }
5129692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5130692ad7caSAdrian Chadd "%s: timeout waiting for clock stabilization\n", __func__);
5131692ad7caSAdrian Chadd
5132692ad7caSAdrian Chadd return ETIMEDOUT;
5133692ad7caSAdrian Chadd }
5134692ad7caSAdrian Chadd
5135692ad7caSAdrian Chadd static int
wpi_apm_init(struct wpi_softc * sc)5136692ad7caSAdrian Chadd wpi_apm_init(struct wpi_softc *sc)
5137692ad7caSAdrian Chadd {
5138692ad7caSAdrian Chadd uint32_t reg;
5139692ad7caSAdrian Chadd int error;
5140692ad7caSAdrian Chadd
5141692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5142692ad7caSAdrian Chadd
5143692ad7caSAdrian Chadd /* Disable L0s exit timer (NMI bug workaround). */
5144692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_DIS_L0S_TIMER);
5145692ad7caSAdrian Chadd /* Don't wait for ICH L0s (ICH bug workaround). */
5146692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_GIO_CHICKEN, WPI_GIO_CHICKEN_L1A_NO_L0S_RX);
5147692ad7caSAdrian Chadd
5148692ad7caSAdrian Chadd /* Set FH wait threshold to max (HW bug under stress workaround). */
5149692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_DBG_HPET_MEM, 0xffff0000);
5150692ad7caSAdrian Chadd
5151692ad7caSAdrian Chadd /* Retrieve PCIe Active State Power Management (ASPM). */
51527c2e8d33SAndriy Voskoboinyk reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + PCIER_LINK_CTL, 1);
5153692ad7caSAdrian Chadd /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
51547c2e8d33SAndriy Voskoboinyk if (reg & PCIEM_LINK_CTL_ASPMC_L1) /* L1 Entry enabled. */
5155692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5156692ad7caSAdrian Chadd else
5157692ad7caSAdrian Chadd WPI_CLRBITS(sc, WPI_GIO, WPI_GIO_L0S_ENA);
5158692ad7caSAdrian Chadd
5159692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_ANA_PLL, WPI_ANA_PLL_INIT);
5160692ad7caSAdrian Chadd
5161692ad7caSAdrian Chadd /* Wait for clock stabilization before accessing prph. */
5162692ad7caSAdrian Chadd if ((error = wpi_clock_wait(sc)) != 0)
5163692ad7caSAdrian Chadd return error;
5164692ad7caSAdrian Chadd
5165692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
5166692ad7caSAdrian Chadd return error;
516702d49915SAdrian Chadd /* Cleanup. */
516802d49915SAdrian Chadd wpi_prph_write(sc, WPI_APMG_CLK_DIS, 0x00000400);
516902d49915SAdrian Chadd wpi_prph_clrbits(sc, WPI_APMG_PS, 0x00000200);
517002d49915SAdrian Chadd
5171692ad7caSAdrian Chadd /* Enable DMA and BSM (Bootstrap State Machine). */
5172692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_APMG_CLK_EN,
5173692ad7caSAdrian Chadd WPI_APMG_CLK_CTRL_DMA_CLK_RQT | WPI_APMG_CLK_CTRL_BSM_CLK_RQT);
5174692ad7caSAdrian Chadd DELAY(20);
5175692ad7caSAdrian Chadd /* Disable L1-Active. */
5176692ad7caSAdrian Chadd wpi_prph_setbits(sc, WPI_APMG_PCI_STT, WPI_APMG_PCI_STT_L1A_DIS);
5177692ad7caSAdrian Chadd wpi_nic_unlock(sc);
5178692ad7caSAdrian Chadd
51796607310bSBenjamin Close return 0;
51806607310bSBenjamin Close }
51816607310bSBenjamin Close
51826607310bSBenjamin Close static void
wpi_apm_stop_master(struct wpi_softc * sc)5183692ad7caSAdrian Chadd wpi_apm_stop_master(struct wpi_softc *sc)
51846607310bSBenjamin Close {
51856607310bSBenjamin Close int ntries;
51866607310bSBenjamin Close
5187692ad7caSAdrian Chadd /* Stop busmaster DMA activity. */
5188692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_RESET, WPI_RESET_STOP_MASTER);
51896607310bSBenjamin Close
5190692ad7caSAdrian Chadd if ((WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_PS_MASK) ==
5191692ad7caSAdrian Chadd WPI_GP_CNTRL_MAC_PS)
5192692ad7caSAdrian Chadd return; /* Already asleep. */
51936607310bSBenjamin Close
51946607310bSBenjamin Close for (ntries = 0; ntries < 100; ntries++) {
5195692ad7caSAdrian Chadd if (WPI_READ(sc, WPI_RESET) & WPI_RESET_MASTER_DISABLED)
5196692ad7caSAdrian Chadd return;
51976607310bSBenjamin Close DELAY(10);
51986607310bSBenjamin Close }
51997167f5fcSAdrian Chadd device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
52007167f5fcSAdrian Chadd __func__);
52016607310bSBenjamin Close }
5202692ad7caSAdrian Chadd
5203692ad7caSAdrian Chadd static void
wpi_apm_stop(struct wpi_softc * sc)5204692ad7caSAdrian Chadd wpi_apm_stop(struct wpi_softc *sc)
5205692ad7caSAdrian Chadd {
5206692ad7caSAdrian Chadd wpi_apm_stop_master(sc);
5207692ad7caSAdrian Chadd
5208692ad7caSAdrian Chadd /* Reset the entire device. */
5209692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_RESET, WPI_RESET_SW);
5210692ad7caSAdrian Chadd DELAY(10);
5211692ad7caSAdrian Chadd /* Clear "initialization complete" bit. */
5212692ad7caSAdrian Chadd WPI_CLRBITS(sc, WPI_GP_CNTRL, WPI_GP_CNTRL_INIT_DONE);
5213692ad7caSAdrian Chadd }
5214692ad7caSAdrian Chadd
5215692ad7caSAdrian Chadd static void
wpi_nic_config(struct wpi_softc * sc)5216692ad7caSAdrian Chadd wpi_nic_config(struct wpi_softc *sc)
5217692ad7caSAdrian Chadd {
5218692ad7caSAdrian Chadd uint32_t rev;
5219692ad7caSAdrian Chadd
5220692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5221692ad7caSAdrian Chadd
5222692ad7caSAdrian Chadd /* voodoo from the Linux "driver".. */
5223692ad7caSAdrian Chadd rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1);
5224692ad7caSAdrian Chadd if ((rev & 0xc0) == 0x40)
5225692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MB);
5226692ad7caSAdrian Chadd else if (!(rev & 0x80))
5227692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_ALM_MM);
5228692ad7caSAdrian Chadd
5229692ad7caSAdrian Chadd if (sc->cap == 0x80)
5230692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_SKU_MRC);
5231692ad7caSAdrian Chadd
5232f736d898SAdrian Chadd if ((sc->rev & 0xf0) == 0xd0)
5233692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5234692ad7caSAdrian Chadd else
5235692ad7caSAdrian Chadd WPI_CLRBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_REV_D);
5236692ad7caSAdrian Chadd
5237692ad7caSAdrian Chadd if (sc->type > 1)
5238692ad7caSAdrian Chadd WPI_SETBITS(sc, WPI_HW_IF_CONFIG, WPI_HW_IF_CONFIG_TYPE_B);
52396607310bSBenjamin Close }
52406607310bSBenjamin Close
52416607310bSBenjamin Close static int
wpi_hw_init(struct wpi_softc * sc)5242692ad7caSAdrian Chadd wpi_hw_init(struct wpi_softc *sc)
52436607310bSBenjamin Close {
5244525a3d47SAdrian Chadd uint8_t chnl;
5245525a3d47SAdrian Chadd int ntries, error;
52466607310bSBenjamin Close
5247692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
52486607310bSBenjamin Close
5249692ad7caSAdrian Chadd /* Clear pending interrupts. */
5250692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT, 0xffffffff);
5251692ad7caSAdrian Chadd
5252692ad7caSAdrian Chadd if ((error = wpi_apm_init(sc)) != 0) {
5253692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5254692ad7caSAdrian Chadd "%s: could not power ON adapter, error %d\n", __func__,
5255692ad7caSAdrian Chadd error);
5256692ad7caSAdrian Chadd return error;
5257692ad7caSAdrian Chadd }
5258692ad7caSAdrian Chadd
5259692ad7caSAdrian Chadd /* Select VMAIN power source. */
5260692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
5261692ad7caSAdrian Chadd return error;
5262692ad7caSAdrian Chadd wpi_prph_clrbits(sc, WPI_APMG_PS, WPI_APMG_PS_PWR_SRC_MASK);
5263692ad7caSAdrian Chadd wpi_nic_unlock(sc);
5264692ad7caSAdrian Chadd /* Spin until VMAIN gets selected. */
52656607310bSBenjamin Close for (ntries = 0; ntries < 5000; ntries++) {
5266692ad7caSAdrian Chadd if (WPI_READ(sc, WPI_GPIO_IN) & WPI_GPIO_IN_VMAIN)
52676607310bSBenjamin Close break;
52686607310bSBenjamin Close DELAY(10);
52696607310bSBenjamin Close }
52706607310bSBenjamin Close if (ntries == 5000) {
5271692ad7caSAdrian Chadd device_printf(sc->sc_dev, "timeout selecting power source\n");
52726607310bSBenjamin Close return ETIMEDOUT;
52736607310bSBenjamin Close }
5274692ad7caSAdrian Chadd
5275692ad7caSAdrian Chadd /* Perform adapter initialization. */
5276692ad7caSAdrian Chadd wpi_nic_config(sc);
5277692ad7caSAdrian Chadd
5278692ad7caSAdrian Chadd /* Initialize RX ring. */
5279692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
5280692ad7caSAdrian Chadd return error;
5281692ad7caSAdrian Chadd /* Set physical address of RX ring. */
5282692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_BASE, sc->rxq.desc_dma.paddr);
5283692ad7caSAdrian Chadd /* Set physical address of RX read pointer. */
5284692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_RPTR_ADDR, sc->shared_dma.paddr +
5285692ad7caSAdrian Chadd offsetof(struct wpi_shared, next));
5286692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_WPTR, 0);
5287692ad7caSAdrian Chadd /* Enable RX. */
5288692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_CONFIG,
5289692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_DMA_ENA |
5290692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_RDRBD_ENA |
5291692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_WRSTATUS_ENA |
5292692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_MAXFRAG |
5293692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_NRBD(WPI_RX_RING_COUNT_LOG) |
5294692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_IRQ_DST_HOST |
5295692ad7caSAdrian Chadd WPI_FH_RX_CONFIG_IRQ_TIMEOUT(1));
5296692ad7caSAdrian Chadd (void)WPI_READ(sc, WPI_FH_RSSR_TBL); /* barrier */
5297692ad7caSAdrian Chadd wpi_nic_unlock(sc);
5298692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_RX_WPTR, (WPI_RX_RING_COUNT - 1) & ~7);
5299692ad7caSAdrian Chadd
5300692ad7caSAdrian Chadd /* Initialize TX rings. */
5301692ad7caSAdrian Chadd if ((error = wpi_nic_lock(sc)) != 0)
5302692ad7caSAdrian Chadd return error;
5303692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 2); /* bypass mode */
5304692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_ARASTAT, 1); /* enable RA0 */
5305692ad7caSAdrian Chadd /* Enable all 6 TX rings. */
5306692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0x3f);
5307692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE1, 0x10000);
5308692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_SBYPASS_MODE2, 0x30002);
5309692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_TXF4MF, 4);
5310692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_TXF5MF, 5);
5311692ad7caSAdrian Chadd /* Set physical address of TX rings. */
5312692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_TX_BASE, sc->shared_dma.paddr);
5313692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_MSG_CONFIG, 0xffff05a5);
5314692ad7caSAdrian Chadd
5315692ad7caSAdrian Chadd /* Enable all DMA channels. */
5316692ad7caSAdrian Chadd for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5317692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_CBBC_CTRL(chnl), 0);
5318692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_CBBC_BASE(chnl), 0);
5319692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0x80200008);
5320692ad7caSAdrian Chadd }
5321692ad7caSAdrian Chadd wpi_nic_unlock(sc);
5322692ad7caSAdrian Chadd (void)WPI_READ(sc, WPI_FH_TX_BASE); /* barrier */
5323692ad7caSAdrian Chadd
5324692ad7caSAdrian Chadd /* Clear "radio off" and "commands blocked" bits. */
5325692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5326692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_CMD_BLOCKED);
5327692ad7caSAdrian Chadd
5328692ad7caSAdrian Chadd /* Clear pending interrupts. */
5329692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT, 0xffffffff);
5330692ad7caSAdrian Chadd /* Enable interrupts. */
5331692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT_MASK, WPI_INT_MASK_DEF);
5332692ad7caSAdrian Chadd
5333692ad7caSAdrian Chadd /* _Really_ make sure "radio off" bit is cleared! */
5334692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5335692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_UCODE_GP1_CLR, WPI_UCODE_GP1_RFKILL);
5336692ad7caSAdrian Chadd
5337692ad7caSAdrian Chadd if ((error = wpi_load_firmware(sc)) != 0) {
5338692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5339692ad7caSAdrian Chadd "%s: could not load firmware, error %d\n", __func__,
5340692ad7caSAdrian Chadd error);
5341692ad7caSAdrian Chadd return error;
5342692ad7caSAdrian Chadd }
5343692ad7caSAdrian Chadd /* Wait at most one second for firmware alive notification. */
534476fdf7ebSAdrian Chadd if ((error = mtx_sleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) {
5345692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5346692ad7caSAdrian Chadd "%s: timeout waiting for adapter to initialize, error %d\n",
5347692ad7caSAdrian Chadd __func__, error);
5348692ad7caSAdrian Chadd return error;
53496607310bSBenjamin Close }
53506607310bSBenjamin Close
5351692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5352692ad7caSAdrian Chadd
5353692ad7caSAdrian Chadd /* Do post-firmware initialization. */
5354692ad7caSAdrian Chadd return wpi_post_alive(sc);
5355692ad7caSAdrian Chadd }
5356692ad7caSAdrian Chadd
5357692ad7caSAdrian Chadd static void
wpi_hw_stop(struct wpi_softc * sc)5358692ad7caSAdrian Chadd wpi_hw_stop(struct wpi_softc *sc)
53596607310bSBenjamin Close {
5360525a3d47SAdrian Chadd uint8_t chnl, qid;
5361525a3d47SAdrian Chadd int ntries;
53626607310bSBenjamin Close
5363692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
53646607310bSBenjamin Close
5365692ad7caSAdrian Chadd if (WPI_READ(sc, WPI_UCODE_GP1) & WPI_UCODE_GP1_MAC_SLEEP)
5366692ad7caSAdrian Chadd wpi_nic_lock(sc);
53676607310bSBenjamin Close
5368692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_RESET, WPI_RESET_NEVO);
53696607310bSBenjamin Close
5370692ad7caSAdrian Chadd /* Disable interrupts. */
5371692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT_MASK, 0);
5372692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_INT, 0xffffffff);
5373692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_INT, 0xffffffff);
53746607310bSBenjamin Close
5375692ad7caSAdrian Chadd /* Make sure we no longer hold the NIC lock. */
5376692ad7caSAdrian Chadd wpi_nic_unlock(sc);
53776607310bSBenjamin Close
5378692ad7caSAdrian Chadd if (wpi_nic_lock(sc) == 0) {
5379692ad7caSAdrian Chadd /* Stop TX scheduler. */
5380692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_MODE, 0);
5381692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_ALM_SCHED_TXFACT, 0);
5382692ad7caSAdrian Chadd
5383692ad7caSAdrian Chadd /* Stop all DMA channels. */
5384692ad7caSAdrian Chadd for (chnl = 0; chnl < WPI_NDMACHNLS; chnl++) {
5385692ad7caSAdrian Chadd WPI_WRITE(sc, WPI_FH_TX_CONFIG(chnl), 0);
5386692ad7caSAdrian Chadd for (ntries = 0; ntries < 200; ntries++) {
5387692ad7caSAdrian Chadd if (WPI_READ(sc, WPI_FH_TX_STATUS) &
5388692ad7caSAdrian Chadd WPI_FH_TX_STATUS_IDLE(chnl))
53896607310bSBenjamin Close break;
53906607310bSBenjamin Close DELAY(10);
53916607310bSBenjamin Close }
5392692ad7caSAdrian Chadd }
5393692ad7caSAdrian Chadd wpi_nic_unlock(sc);
53946607310bSBenjamin Close }
53956607310bSBenjamin Close
5396692ad7caSAdrian Chadd /* Stop RX ring. */
5397692ad7caSAdrian Chadd wpi_reset_rx_ring(sc);
53986607310bSBenjamin Close
5399692ad7caSAdrian Chadd /* Reset all TX rings. */
540019053a54SAdrian Chadd for (qid = 0; qid < WPI_DRV_NTXQUEUES; qid++)
5401692ad7caSAdrian Chadd wpi_reset_tx_ring(sc, &sc->txq[qid]);
5402692ad7caSAdrian Chadd
5403692ad7caSAdrian Chadd if (wpi_nic_lock(sc) == 0) {
5404692ad7caSAdrian Chadd wpi_prph_write(sc, WPI_APMG_CLK_DIS,
5405692ad7caSAdrian Chadd WPI_APMG_CLK_CTRL_DMA_CLK_RQT);
5406692ad7caSAdrian Chadd wpi_nic_unlock(sc);
54076607310bSBenjamin Close }
5408692ad7caSAdrian Chadd DELAY(5);
5409692ad7caSAdrian Chadd /* Power OFF adapter. */
5410692ad7caSAdrian Chadd wpi_apm_stop(sc);
54116607310bSBenjamin Close }
54126607310bSBenjamin Close
54136607310bSBenjamin Close static void
wpi_radio_on(void * arg0,int pending)5414692ad7caSAdrian Chadd wpi_radio_on(void *arg0, int pending)
54156607310bSBenjamin Close {
5416692ad7caSAdrian Chadd struct wpi_softc *sc = arg0;
54177a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
5418b032f27cSSam Leffler struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
541982f1b132SAndrew Thompson
5420692ad7caSAdrian Chadd device_printf(sc->sc_dev, "RF switch: radio enabled\n");
542182f1b132SAndrew Thompson
5422692ad7caSAdrian Chadd WPI_LOCK(sc);
5423692ad7caSAdrian Chadd callout_stop(&sc->watchdog_rfkill);
5424692ad7caSAdrian Chadd WPI_UNLOCK(sc);
54257a79cebfSGleb Smirnoff
54267a79cebfSGleb Smirnoff if (vap != NULL)
54277a79cebfSGleb Smirnoff ieee80211_init(vap);
542882f1b132SAndrew Thompson }
542982f1b132SAndrew Thompson
543082f1b132SAndrew Thompson static void
wpi_radio_off(void * arg0,int pending)5431692ad7caSAdrian Chadd wpi_radio_off(void *arg0, int pending)
5432692ad7caSAdrian Chadd {
5433692ad7caSAdrian Chadd struct wpi_softc *sc = arg0;
54347a79cebfSGleb Smirnoff struct ieee80211com *ic = &sc->sc_ic;
5435692ad7caSAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5436692ad7caSAdrian Chadd
5437692ad7caSAdrian Chadd device_printf(sc->sc_dev, "RF switch: radio disabled\n");
5438692ad7caSAdrian Chadd
54397a79cebfSGleb Smirnoff ieee80211_notify_radio(ic, 0);
5440692ad7caSAdrian Chadd wpi_stop(sc);
5441692ad7caSAdrian Chadd if (vap != NULL)
5442692ad7caSAdrian Chadd ieee80211_stop(vap);
5443692ad7caSAdrian Chadd
5444811fe0a8SAdrian Chadd WPI_LOCK(sc);
5445692ad7caSAdrian Chadd callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill, sc);
5446811fe0a8SAdrian Chadd WPI_UNLOCK(sc);
5447692ad7caSAdrian Chadd }
5448692ad7caSAdrian Chadd
54497a79cebfSGleb Smirnoff static int
wpi_init(struct wpi_softc * sc)54507a79cebfSGleb Smirnoff wpi_init(struct wpi_softc *sc)
545182f1b132SAndrew Thompson {
54527a79cebfSGleb Smirnoff int error = 0;
54536607310bSBenjamin Close
545477cf0b82SAdrian Chadd WPI_LOCK(sc);
545577cf0b82SAdrian Chadd
5456692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_BEGIN, __func__);
54576607310bSBenjamin Close
54587a79cebfSGleb Smirnoff if (sc->sc_running != 0)
545977cf0b82SAdrian Chadd goto end;
54606607310bSBenjamin Close
5461692ad7caSAdrian Chadd /* Check that the radio is not disabled by hardware switch. */
5462692ad7caSAdrian Chadd if (!(WPI_READ(sc, WPI_GP_CNTRL) & WPI_GP_CNTRL_RFKILL)) {
54636607310bSBenjamin Close device_printf(sc->sc_dev,
5464692ad7caSAdrian Chadd "RF switch: radio disabled (%s)\n", __func__);
5465692ad7caSAdrian Chadd callout_reset(&sc->watchdog_rfkill, hz, wpi_watchdog_rfkill,
5466692ad7caSAdrian Chadd sc);
54677a79cebfSGleb Smirnoff error = EINPROGRESS;
546877cf0b82SAdrian Chadd goto end;
54696607310bSBenjamin Close }
54706607310bSBenjamin Close
5471692ad7caSAdrian Chadd /* Read firmware images from the filesystem. */
5472692ad7caSAdrian Chadd if ((error = wpi_read_firmware(sc)) != 0) {
54736607310bSBenjamin Close device_printf(sc->sc_dev,
5474692ad7caSAdrian Chadd "%s: could not read firmware, error %d\n", __func__,
5475692ad7caSAdrian Chadd error);
54767a79cebfSGleb Smirnoff goto end;
54776607310bSBenjamin Close }
54786607310bSBenjamin Close
54797a79cebfSGleb Smirnoff sc->sc_running = 1;
54807a79cebfSGleb Smirnoff
5481692ad7caSAdrian Chadd /* Initialize hardware and upload firmware. */
5482692ad7caSAdrian Chadd error = wpi_hw_init(sc);
5483692ad7caSAdrian Chadd wpi_unload_firmware(sc);
5484692ad7caSAdrian Chadd if (error != 0) {
5485692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5486692ad7caSAdrian Chadd "%s: could not initialize hardware, error %d\n", __func__,
5487692ad7caSAdrian Chadd error);
5488692ad7caSAdrian Chadd goto fail;
5489692ad7caSAdrian Chadd }
5490692ad7caSAdrian Chadd
5491692ad7caSAdrian Chadd /* Configure adapter now that it is ready. */
5492692ad7caSAdrian Chadd if ((error = wpi_config(sc)) != 0) {
5493692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5494692ad7caSAdrian Chadd "%s: could not configure device, error %d\n", __func__,
5495692ad7caSAdrian Chadd error);
5496692ad7caSAdrian Chadd goto fail;
549782f1b132SAndrew Thompson }
549882f1b132SAndrew Thompson
5499692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END, __func__);
5500692ad7caSAdrian Chadd
550177cf0b82SAdrian Chadd WPI_UNLOCK(sc);
550277cf0b82SAdrian Chadd
55037a79cebfSGleb Smirnoff return 0;
5504692ad7caSAdrian Chadd
5505692ad7caSAdrian Chadd fail: wpi_stop_locked(sc);
55067a79cebfSGleb Smirnoff
550777cf0b82SAdrian Chadd end: DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_END_ERR, __func__);
55086607310bSBenjamin Close WPI_UNLOCK(sc);
55097a79cebfSGleb Smirnoff
55107a79cebfSGleb Smirnoff return error;
55116607310bSBenjamin Close }
5512b032f27cSSam Leffler
55136607310bSBenjamin Close static void
wpi_stop_locked(struct wpi_softc * sc)55146607310bSBenjamin Close wpi_stop_locked(struct wpi_softc *sc)
55156607310bSBenjamin Close {
55166607310bSBenjamin Close
5517692ad7caSAdrian Chadd WPI_LOCK_ASSERT(sc);
5518692ad7caSAdrian Chadd
55197a79cebfSGleb Smirnoff if (sc->sc_running == 0)
55207a79cebfSGleb Smirnoff return;
55217a79cebfSGleb Smirnoff
55227a79cebfSGleb Smirnoff WPI_TX_LOCK(sc);
552363f57699SAdrian Chadd WPI_TXQ_LOCK(sc);
55247a79cebfSGleb Smirnoff sc->sc_running = 0;
552563f57699SAdrian Chadd WPI_TXQ_UNLOCK(sc);
55267a79cebfSGleb Smirnoff WPI_TX_UNLOCK(sc);
552763f57699SAdrian Chadd
5528fc5870b4SAdrian Chadd WPI_TXQ_STATE_LOCK(sc);
5529d74e544aSAdrian Chadd callout_stop(&sc->tx_timeout);
5530fc5870b4SAdrian Chadd WPI_TXQ_STATE_UNLOCK(sc);
5531446685e4SAdrian Chadd
5532446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
55330bc39fc6SAdrian Chadd callout_stop(&sc->scan_timeout);
553482f1b132SAndrew Thompson callout_stop(&sc->calib_to);
5535446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
5536b9858d86SAdrian Chadd
5537692ad7caSAdrian Chadd /* Power OFF hardware. */
5538692ad7caSAdrian Chadd wpi_hw_stop(sc);
55396607310bSBenjamin Close }
55406607310bSBenjamin Close
55416607310bSBenjamin Close static void
wpi_stop(struct wpi_softc * sc)5542b032f27cSSam Leffler wpi_stop(struct wpi_softc *sc)
55436607310bSBenjamin Close {
5544b032f27cSSam Leffler WPI_LOCK(sc);
5545b032f27cSSam Leffler wpi_stop_locked(sc);
5546b032f27cSSam Leffler WPI_UNLOCK(sc);
55476607310bSBenjamin Close }
55486607310bSBenjamin Close
55496607310bSBenjamin Close /*
5550692ad7caSAdrian Chadd * Callback from net80211 to start a scan.
55516607310bSBenjamin Close */
55526607310bSBenjamin Close static void
wpi_scan_start(struct ieee80211com * ic)55536607310bSBenjamin Close wpi_scan_start(struct ieee80211com *ic)
55546607310bSBenjamin Close {
55552cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
55566607310bSBenjamin Close
55575efea30fSAndrew Thompson wpi_set_led(sc, WPI_LED_LINK, 20, 2);
55586607310bSBenjamin Close }
55596607310bSBenjamin Close
5560692ad7caSAdrian Chadd /*
5561692ad7caSAdrian Chadd * Callback from net80211 to terminate a scan.
55626607310bSBenjamin Close */
55636607310bSBenjamin Close static void
wpi_scan_end(struct ieee80211com * ic)55646607310bSBenjamin Close wpi_scan_end(struct ieee80211com *ic)
55656607310bSBenjamin Close {
55662cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
5567692ad7caSAdrian Chadd struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5568692ad7caSAdrian Chadd
55690b093122SAdrian Chadd if (vap->iv_state == IEEE80211_S_RUN)
5570692ad7caSAdrian Chadd wpi_set_led(sc, WPI_LED_LINK, 0, 1);
55716607310bSBenjamin Close }
55726607310bSBenjamin Close
55736607310bSBenjamin Close /**
55746607310bSBenjamin Close * Called by the net80211 framework to indicate to the driver
55756607310bSBenjamin Close * that the channel should be changed
55766607310bSBenjamin Close */
55776607310bSBenjamin Close static void
wpi_set_channel(struct ieee80211com * ic)55786607310bSBenjamin Close wpi_set_channel(struct ieee80211com *ic)
55796607310bSBenjamin Close {
5580692ad7caSAdrian Chadd const struct ieee80211_channel *c = ic->ic_curchan;
55812cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
55825efea30fSAndrew Thompson int error;
55836607310bSBenjamin Close
5584692ad7caSAdrian Chadd DPRINTF(sc, WPI_DEBUG_TRACE, TRACE_STR_DOING, __func__);
5585692ad7caSAdrian Chadd
5586692ad7caSAdrian Chadd WPI_LOCK(sc);
5587692ad7caSAdrian Chadd sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
5588692ad7caSAdrian Chadd sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
55895effcbfdSAdrian Chadd WPI_UNLOCK(sc);
55905effcbfdSAdrian Chadd WPI_TX_LOCK(sc);
5591692ad7caSAdrian Chadd sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
5592692ad7caSAdrian Chadd sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
55935effcbfdSAdrian Chadd WPI_TX_UNLOCK(sc);
5594692ad7caSAdrian Chadd
559582f1b132SAndrew Thompson /*
559682f1b132SAndrew Thompson * Only need to set the channel in Monitor mode. AP scanning and auth
559782f1b132SAndrew Thompson * are already taken care of by their respective firmware commands.
559882f1b132SAndrew Thompson */
55995efea30fSAndrew Thompson if (ic->ic_opmode == IEEE80211_M_MONITOR) {
5600446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
5601692ad7caSAdrian Chadd sc->rxon.chan = ieee80211_chan2ieee(ic, c);
5602692ad7caSAdrian Chadd if (IEEE80211_IS_CHAN_2GHZ(c)) {
5603692ad7caSAdrian Chadd sc->rxon.flags |= htole32(WPI_RXON_AUTO |
5604692ad7caSAdrian Chadd WPI_RXON_24GHZ);
5605692ad7caSAdrian Chadd } else {
5606692ad7caSAdrian Chadd sc->rxon.flags &= ~htole32(WPI_RXON_AUTO |
5607692ad7caSAdrian Chadd WPI_RXON_24GHZ);
56085efea30fSAndrew Thompson }
5609446685e4SAdrian Chadd if ((error = wpi_send_rxon(sc, 0, 1)) != 0)
5610692ad7caSAdrian Chadd device_printf(sc->sc_dev,
5611cec45f04SAdrian Chadd "%s: error %d setting channel\n", __func__,
5612692ad7caSAdrian Chadd error);
5613446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
5614692ad7caSAdrian Chadd }
56156607310bSBenjamin Close }
56166607310bSBenjamin Close
56176607310bSBenjamin Close /**
56186607310bSBenjamin Close * Called by net80211 to indicate that we need to scan the current
56196607310bSBenjamin Close * channel. The channel is previously be set via the wpi_set_channel
56206607310bSBenjamin Close * callback.
56216607310bSBenjamin Close */
56226607310bSBenjamin Close static void
wpi_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)5623b032f27cSSam Leffler wpi_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
56246607310bSBenjamin Close {
5625b032f27cSSam Leffler struct ieee80211vap *vap = ss->ss_vap;
5626692ad7caSAdrian Chadd struct ieee80211com *ic = vap->iv_ic;
56272cb71df1SAdrian Chadd struct wpi_softc *sc = ic->ic_softc;
5628692ad7caSAdrian Chadd int error;
56296607310bSBenjamin Close
5630446685e4SAdrian Chadd WPI_RXON_LOCK(sc);
5631692ad7caSAdrian Chadd error = wpi_scan(sc, ic->ic_curchan);
5632446685e4SAdrian Chadd WPI_RXON_UNLOCK(sc);
5633692ad7caSAdrian Chadd if (error != 0)
5634692ad7caSAdrian Chadd ieee80211_cancel_scan(vap);
56356607310bSBenjamin Close }
56366607310bSBenjamin Close
56376607310bSBenjamin Close /**
56386607310bSBenjamin Close * Called by the net80211 framework to indicate
56396607310bSBenjamin Close * the minimum dwell time has been met, terminate the scan.
56406607310bSBenjamin Close * We don't actually terminate the scan as the firmware will notify
56416607310bSBenjamin Close * us when it's finished and we have no way to interrupt it.
56426607310bSBenjamin Close */
56436607310bSBenjamin Close static void
wpi_scan_mindwell(struct ieee80211_scan_state * ss)5644b032f27cSSam Leffler wpi_scan_mindwell(struct ieee80211_scan_state *ss)
56456607310bSBenjamin Close {
56466607310bSBenjamin Close /* NB: don't try to abort scan; wait for firmware to finish */
56476607310bSBenjamin Close }
5648