xref: /freebsd/sys/dev/iwm/if_iwm_scan.c (revision 4b17c188)
1d4886179SRui Paulo /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2d4886179SRui Paulo 
3d4886179SRui Paulo /*
4d4886179SRui Paulo  * Copyright (c) 2014 genua mbh <info@genua.de>
5d4886179SRui Paulo  * Copyright (c) 2014 Fixup Software Ltd.
6d4886179SRui Paulo  *
7d4886179SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
8d4886179SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
9d4886179SRui Paulo  * copyright notice and this permission notice appear in all copies.
10d4886179SRui Paulo  *
11d4886179SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12d4886179SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13d4886179SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14d4886179SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15d4886179SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16d4886179SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17d4886179SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18d4886179SRui Paulo  */
19d4886179SRui Paulo 
20d4886179SRui Paulo /*-
21d4886179SRui Paulo  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22d4886179SRui Paulo  * which were used as the reference documentation for this implementation.
23d4886179SRui Paulo  *
24d4886179SRui Paulo  * Driver version we are currently based off of is
25d4886179SRui Paulo  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26d4886179SRui Paulo  *
27d4886179SRui Paulo  ***********************************************************************
28d4886179SRui Paulo  *
29d4886179SRui Paulo  * This file is provided under a dual BSD/GPLv2 license.  When using or
30d4886179SRui Paulo  * redistributing this file, you may do so under either license.
31d4886179SRui Paulo  *
32d4886179SRui Paulo  * GPL LICENSE SUMMARY
33d4886179SRui Paulo  *
34d4886179SRui Paulo  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35d4886179SRui Paulo  *
36d4886179SRui Paulo  * This program is free software; you can redistribute it and/or modify
37d4886179SRui Paulo  * it under the terms of version 2 of the GNU General Public License as
38d4886179SRui Paulo  * published by the Free Software Foundation.
39d4886179SRui Paulo  *
40d4886179SRui Paulo  * This program is distributed in the hope that it will be useful, but
41d4886179SRui Paulo  * WITHOUT ANY WARRANTY; without even the implied warranty of
42d4886179SRui Paulo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43d4886179SRui Paulo  * General Public License for more details.
44d4886179SRui Paulo  *
45d4886179SRui Paulo  * You should have received a copy of the GNU General Public License
46d4886179SRui Paulo  * along with this program; if not, write to the Free Software
47d4886179SRui Paulo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48d4886179SRui Paulo  * USA
49d4886179SRui Paulo  *
50d4886179SRui Paulo  * The full GNU General Public License is included in this distribution
51d4886179SRui Paulo  * in the file called COPYING.
52d4886179SRui Paulo  *
53d4886179SRui Paulo  * Contact Information:
54d4886179SRui Paulo  *  Intel Linux Wireless <ilw@linux.intel.com>
55d4886179SRui Paulo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56d4886179SRui Paulo  *
57d4886179SRui Paulo  *
58d4886179SRui Paulo  * BSD LICENSE
59d4886179SRui Paulo  *
60d4886179SRui Paulo  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61d4886179SRui Paulo  * All rights reserved.
62d4886179SRui Paulo  *
63d4886179SRui Paulo  * Redistribution and use in source and binary forms, with or without
64d4886179SRui Paulo  * modification, are permitted provided that the following conditions
65d4886179SRui Paulo  * are met:
66d4886179SRui Paulo  *
67d4886179SRui Paulo  *  * Redistributions of source code must retain the above copyright
68d4886179SRui Paulo  *    notice, this list of conditions and the following disclaimer.
69d4886179SRui Paulo  *  * Redistributions in binary form must reproduce the above copyright
70d4886179SRui Paulo  *    notice, this list of conditions and the following disclaimer in
71d4886179SRui Paulo  *    the documentation and/or other materials provided with the
72d4886179SRui Paulo  *    distribution.
73d4886179SRui Paulo  *  * Neither the name Intel Corporation nor the names of its
74d4886179SRui Paulo  *    contributors may be used to endorse or promote products derived
75d4886179SRui Paulo  *    from this software without specific prior written permission.
76d4886179SRui Paulo  *
77d4886179SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78d4886179SRui Paulo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79d4886179SRui Paulo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80d4886179SRui Paulo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81d4886179SRui Paulo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82d4886179SRui Paulo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83d4886179SRui Paulo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84d4886179SRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85d4886179SRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86d4886179SRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87d4886179SRui Paulo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88d4886179SRui Paulo  */
89d4886179SRui Paulo 
90d4886179SRui Paulo /*-
91d4886179SRui Paulo  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92d4886179SRui Paulo  *
93d4886179SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
94d4886179SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
95d4886179SRui Paulo  * copyright notice and this permission notice appear in all copies.
96d4886179SRui Paulo  *
97d4886179SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98d4886179SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99d4886179SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100d4886179SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101d4886179SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102d4886179SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103d4886179SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104d4886179SRui Paulo  */
105d4886179SRui Paulo #include <sys/cdefs.h>
106d4886179SRui Paulo __FBSDID("$FreeBSD$");
107d4886179SRui Paulo 
108b789292fSAndriy Voskoboinyk #include "opt_wlan.h"
109b789292fSAndriy Voskoboinyk 
110d4886179SRui Paulo #include <sys/param.h>
111d4886179SRui Paulo #include <sys/bus.h>
112d4886179SRui Paulo #include <sys/conf.h>
113d4886179SRui Paulo #include <sys/endian.h>
114d4886179SRui Paulo #include <sys/firmware.h>
115d4886179SRui Paulo #include <sys/kernel.h>
116d4886179SRui Paulo #include <sys/malloc.h>
117d4886179SRui Paulo #include <sys/mbuf.h>
118d4886179SRui Paulo #include <sys/mutex.h>
119d4886179SRui Paulo #include <sys/module.h>
120d4886179SRui Paulo #include <sys/proc.h>
121d4886179SRui Paulo #include <sys/rman.h>
122d4886179SRui Paulo #include <sys/socket.h>
123d4886179SRui Paulo #include <sys/sockio.h>
124d4886179SRui Paulo #include <sys/sysctl.h>
125d4886179SRui Paulo #include <sys/linker.h>
126d4886179SRui Paulo 
127d4886179SRui Paulo #include <machine/bus.h>
128d4886179SRui Paulo #include <machine/endian.h>
129d4886179SRui Paulo #include <machine/resource.h>
130d4886179SRui Paulo 
131d4886179SRui Paulo #include <dev/pci/pcivar.h>
132d4886179SRui Paulo #include <dev/pci/pcireg.h>
133d4886179SRui Paulo 
134d4886179SRui Paulo #include <net/bpf.h>
135d4886179SRui Paulo 
136d4886179SRui Paulo #include <net/if.h>
137d4886179SRui Paulo #include <net/if_var.h>
138d4886179SRui Paulo #include <net/if_arp.h>
139d4886179SRui Paulo #include <net/if_dl.h>
140d4886179SRui Paulo #include <net/if_media.h>
141d4886179SRui Paulo #include <net/if_types.h>
142d4886179SRui Paulo 
143d4886179SRui Paulo #include <netinet/in.h>
144d4886179SRui Paulo #include <netinet/in_systm.h>
145d4886179SRui Paulo #include <netinet/if_ether.h>
146d4886179SRui Paulo #include <netinet/ip.h>
147d4886179SRui Paulo 
148d4886179SRui Paulo #include <net80211/ieee80211_var.h>
149d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
150d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
151d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
152d4886179SRui Paulo 
15349fdbf0aSRui Paulo #include <dev/iwm/if_iwmreg.h>
15449fdbf0aSRui Paulo #include <dev/iwm/if_iwmvar.h>
15549fdbf0aSRui Paulo #include <dev/iwm/if_iwm_debug.h>
1564b17c188SAdrian Chadd #include <dev/iwm/if_iwm_notif_wait.h>
15749fdbf0aSRui Paulo #include <dev/iwm/if_iwm_util.h>
15849fdbf0aSRui Paulo #include <dev/iwm/if_iwm_scan.h>
159d4886179SRui Paulo 
160d4886179SRui Paulo /*
161d4886179SRui Paulo  * BEGIN mvm/scan.c
162d4886179SRui Paulo  */
163d4886179SRui Paulo 
164d4886179SRui Paulo #define IWM_PLCP_QUIET_THRESH 1
165d4886179SRui Paulo #define IWM_ACTIVE_QUIET_TIME 10
166d4886179SRui Paulo #define LONG_OUT_TIME_PERIOD (600 * IEEE80211_DUR_TU)
167d4886179SRui Paulo #define SHORT_OUT_TIME_PERIOD (200 * IEEE80211_DUR_TU)
168d4886179SRui Paulo #define SUSPEND_TIME_PERIOD (100 * IEEE80211_DUR_TU)
169d4886179SRui Paulo 
170d4886179SRui Paulo static uint16_t
171d4886179SRui Paulo iwm_mvm_scan_rx_chain(struct iwm_softc *sc)
172d4886179SRui Paulo {
173d4886179SRui Paulo 	uint16_t rx_chain;
174d4886179SRui Paulo 	uint8_t rx_ant;
175d4886179SRui Paulo 
1762a2476b3SAdrian Chadd 	rx_ant = iwm_mvm_get_valid_rx_ant(sc);
177d4886179SRui Paulo 	rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
178d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
179d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
180d4886179SRui Paulo 	rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
181d4886179SRui Paulo 	return htole16(rx_chain);
182d4886179SRui Paulo }
183d4886179SRui Paulo 
1846a5bc1d1SSean Bruno #if 0
185d4886179SRui Paulo static uint32_t
186d4886179SRui Paulo iwm_mvm_scan_max_out_time(struct iwm_softc *sc, uint32_t flags, int is_assoc)
187d4886179SRui Paulo {
188d4886179SRui Paulo 	if (!is_assoc)
189d4886179SRui Paulo 		return 0;
190d4886179SRui Paulo 	if (flags & 0x1)
191d4886179SRui Paulo 		return htole32(SHORT_OUT_TIME_PERIOD);
192d4886179SRui Paulo 	return htole32(LONG_OUT_TIME_PERIOD);
193d4886179SRui Paulo }
194d4886179SRui Paulo 
195d4886179SRui Paulo static uint32_t
196d4886179SRui Paulo iwm_mvm_scan_suspend_time(struct iwm_softc *sc, int is_assoc)
197d4886179SRui Paulo {
198d4886179SRui Paulo 	if (!is_assoc)
199d4886179SRui Paulo 		return 0;
200d4886179SRui Paulo 	return htole32(SUSPEND_TIME_PERIOD);
201d4886179SRui Paulo }
2026a5bc1d1SSean Bruno #endif
203d4886179SRui Paulo 
204d4886179SRui Paulo static uint32_t
205d4886179SRui Paulo iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
206d4886179SRui Paulo {
207d4886179SRui Paulo 	uint32_t tx_ant;
208d4886179SRui Paulo 	int i, ind;
209d4886179SRui Paulo 
210d4886179SRui Paulo 	for (i = 0, ind = sc->sc_scan_last_antenna;
211d4886179SRui Paulo 	    i < IWM_RATE_MCS_ANT_NUM; i++) {
212d4886179SRui Paulo 		ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
2132a2476b3SAdrian Chadd 		if (iwm_mvm_get_valid_tx_ant(sc) & (1 << ind)) {
214d4886179SRui Paulo 			sc->sc_scan_last_antenna = ind;
215d4886179SRui Paulo 			break;
216d4886179SRui Paulo 		}
217d4886179SRui Paulo 	}
218d4886179SRui Paulo 	tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
219d4886179SRui Paulo 
220d4886179SRui Paulo 	if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
221d4886179SRui Paulo 		return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
222d4886179SRui Paulo 				   tx_ant);
223d4886179SRui Paulo 	else
224d4886179SRui Paulo 		return htole32(IWM_RATE_6M_PLCP | tx_ant);
225d4886179SRui Paulo }
226d4886179SRui Paulo 
2276a5bc1d1SSean Bruno #if 0
228d4886179SRui Paulo /*
229d4886179SRui Paulo  * If req->n_ssids > 0, it means we should do an active scan.
230d4886179SRui Paulo  * In case of active scan w/o directed scan, we receive a zero-length SSID
231d4886179SRui Paulo  * just to notify that this scan is active and not passive.
232d4886179SRui Paulo  * In order to notify the FW of the number of SSIDs we wish to scan (including
233d4886179SRui Paulo  * the zero-length one), we need to set the corresponding bits in chan->type,
234d4886179SRui Paulo  * one for each SSID, and set the active bit (first). If the first SSID is
235d4886179SRui Paulo  * already included in the probe template, so we need to set only
236d4886179SRui Paulo  * req->n_ssids - 1 bits in addition to the first bit.
237d4886179SRui Paulo  */
238d4886179SRui Paulo static uint16_t
239d4886179SRui Paulo iwm_mvm_get_active_dwell(struct iwm_softc *sc, int flags, int n_ssids)
240d4886179SRui Paulo {
241d4886179SRui Paulo 	if (flags & IEEE80211_CHAN_2GHZ)
242d4886179SRui Paulo 		return 30  + 3 * (n_ssids + 1);
243d4886179SRui Paulo 	return 20  + 2 * (n_ssids + 1);
244d4886179SRui Paulo }
245d4886179SRui Paulo 
246d4886179SRui Paulo static uint16_t
247d4886179SRui Paulo iwm_mvm_get_passive_dwell(struct iwm_softc *sc, int flags)
248d4886179SRui Paulo {
249d4886179SRui Paulo 	return (flags & IEEE80211_CHAN_2GHZ) ? 100 + 20 : 100 + 10;
250d4886179SRui Paulo }
2516a5bc1d1SSean Bruno #endif
252d4886179SRui Paulo 
253d4886179SRui Paulo static int
2546a5bc1d1SSean Bruno iwm_mvm_scan_skip_channel(struct ieee80211_channel *c)
2556a5bc1d1SSean Bruno {
2566a5bc1d1SSean Bruno 	if (IEEE80211_IS_CHAN_2GHZ(c) && IEEE80211_IS_CHAN_B(c))
2576a5bc1d1SSean Bruno 		return 0;
2586a5bc1d1SSean Bruno 	else if (IEEE80211_IS_CHAN_5GHZ(c) && IEEE80211_IS_CHAN_A(c))
2596a5bc1d1SSean Bruno 		return 0;
2606a5bc1d1SSean Bruno 	else
2616a5bc1d1SSean Bruno 		return 1;
2626a5bc1d1SSean Bruno }
2636a5bc1d1SSean Bruno 
2646a5bc1d1SSean Bruno static uint8_t
2656a5bc1d1SSean Bruno iwm_mvm_lmac_scan_fill_channels(struct iwm_softc *sc,
2666a5bc1d1SSean Bruno     struct iwm_scan_channel_cfg_lmac *chan, int n_ssids)
267d4886179SRui Paulo {
2687a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2699afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = ic->ic_scan;
270d4886179SRui Paulo 	struct ieee80211_channel *c;
2716a5bc1d1SSean Bruno 	uint8_t nchan;
2726a5bc1d1SSean Bruno 	int j;
273d4886179SRui Paulo 
2746a5bc1d1SSean Bruno 	for (nchan = j = 0;
2756a5bc1d1SSean Bruno 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
276d4886179SRui Paulo 		c = &ic->ic_channels[j];
277d4886179SRui Paulo 		/* For 2GHz, only populate 11b channels */
278d4886179SRui Paulo 		/* For 5GHz, only populate 11a channels */
279d4886179SRui Paulo 		/*
280d4886179SRui Paulo 		 * Catch other channels, in case we have 900MHz channels or
281d4886179SRui Paulo 		 * something in the chanlist.
282d4886179SRui Paulo 		 */
2836a5bc1d1SSean Bruno 		if (iwm_mvm_scan_skip_channel(c)) {
284d4886179SRui Paulo 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
285d4886179SRui Paulo 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
2866a5bc1d1SSean Bruno 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
2876a5bc1d1SSean Bruno 			continue;
288d4886179SRui Paulo 		}
2896a5bc1d1SSean Bruno 
290d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
291d4886179SRui Paulo 		    "Adding channel %d (%d Mhz) to the list\n",
292d4886179SRui Paulo 		    nchan, c->ic_freq);
2936a5bc1d1SSean Bruno 		chan->channel_num = htole16(ieee80211_mhz2ieee(c->ic_freq, 0));
2946a5bc1d1SSean Bruno 		chan->iter_count = htole16(1);
2956a5bc1d1SSean Bruno 		chan->iter_interval = htole32(0);
2966a5bc1d1SSean Bruno 		chan->flags = htole32(IWM_UNIFIED_SCAN_CHANNEL_PARTIAL);
2979afea60fSAndriy Voskoboinyk 		chan->flags |= htole32(IWM_SCAN_CHANNEL_NSSIDS(n_ssids));
2989afea60fSAndriy Voskoboinyk 		/* XXX IEEE80211_SCAN_NOBCAST flag is never set. */
2999afea60fSAndriy Voskoboinyk 		if (!IEEE80211_IS_CHAN_PASSIVE(c) &&
3009afea60fSAndriy Voskoboinyk 		    (!(ss->ss_flags & IEEE80211_SCAN_NOBCAST) || n_ssids != 0))
3019afea60fSAndriy Voskoboinyk 			chan->flags |= htole32(IWM_SCAN_CHANNEL_TYPE_ACTIVE);
302d4886179SRui Paulo 		chan++;
303d4886179SRui Paulo 		nchan++;
304d4886179SRui Paulo 	}
3056a5bc1d1SSean Bruno 
306d4886179SRui Paulo 	return nchan;
307d4886179SRui Paulo }
308d4886179SRui Paulo 
3096a5bc1d1SSean Bruno static uint8_t
3106a5bc1d1SSean Bruno iwm_mvm_umac_scan_fill_channels(struct iwm_softc *sc,
3116a5bc1d1SSean Bruno     struct iwm_scan_channel_cfg_umac *chan, int n_ssids)
312d4886179SRui Paulo {
3136a5bc1d1SSean Bruno 	struct ieee80211com *ic = &sc->sc_ic;
3146a5bc1d1SSean Bruno 	struct ieee80211_channel *c;
3156a5bc1d1SSean Bruno 	uint8_t nchan;
3166a5bc1d1SSean Bruno 	int j;
317d4886179SRui Paulo 
3186a5bc1d1SSean Bruno 	for (nchan = j = 0;
3196a5bc1d1SSean Bruno 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
3206a5bc1d1SSean Bruno 		c = &ic->ic_channels[j];
3216a5bc1d1SSean Bruno 		/* For 2GHz, only populate 11b channels */
3226a5bc1d1SSean Bruno 		/* For 5GHz, only populate 11a channels */
3236a5bc1d1SSean Bruno 		/*
3246a5bc1d1SSean Bruno 		 * Catch other channels, in case we have 900MHz channels or
3256a5bc1d1SSean Bruno 		 * something in the chanlist.
3266a5bc1d1SSean Bruno 		 */
3276a5bc1d1SSean Bruno 		if (iwm_mvm_scan_skip_channel(c)) {
3286a5bc1d1SSean Bruno 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
3296a5bc1d1SSean Bruno 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
3306a5bc1d1SSean Bruno 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
3316a5bc1d1SSean Bruno 			continue;
332d4886179SRui Paulo 		}
333d4886179SRui Paulo 
3346a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
3356a5bc1d1SSean Bruno 		    "Adding channel %d (%d Mhz) to the list\n",
3366a5bc1d1SSean Bruno 		    nchan, c->ic_freq);
3376a5bc1d1SSean Bruno 		chan->channel_num = ieee80211_mhz2ieee(c->ic_freq, 0);
3386a5bc1d1SSean Bruno 		chan->iter_count = 1;
3396a5bc1d1SSean Bruno 		chan->iter_interval = htole16(0);
3409afea60fSAndriy Voskoboinyk 		chan->flags = htole32(IWM_SCAN_CHANNEL_UMAC_NSSIDS(n_ssids));
3416a5bc1d1SSean Bruno 		chan++;
3426a5bc1d1SSean Bruno 		nchan++;
3436a5bc1d1SSean Bruno 	}
3446a5bc1d1SSean Bruno 
3456a5bc1d1SSean Bruno 	return nchan;
3466a5bc1d1SSean Bruno }
3476a5bc1d1SSean Bruno 
3486a5bc1d1SSean Bruno static int
3496a5bc1d1SSean Bruno iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct iwm_scan_probe_req *preq)
3506a5bc1d1SSean Bruno {
3516a5bc1d1SSean Bruno 	struct ieee80211com *ic = &sc->sc_ic;
3526a5bc1d1SSean Bruno 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3536a5bc1d1SSean Bruno 	struct ieee80211_frame *wh = (struct ieee80211_frame *)preq->buf;
3546a5bc1d1SSean Bruno 	struct ieee80211_rateset *rs;
3556a5bc1d1SSean Bruno 	size_t remain = sizeof(preq->buf);
3566a5bc1d1SSean Bruno 	uint8_t *frm, *pos;
3576a5bc1d1SSean Bruno 
3586a5bc1d1SSean Bruno 	memset(preq, 0, sizeof(*preq));
3596a5bc1d1SSean Bruno 
3606a5bc1d1SSean Bruno 	/* Ensure enough space for header and SSID IE. */
3619afea60fSAndriy Voskoboinyk 	if (remain < sizeof(*wh) + 2)
3626a5bc1d1SSean Bruno 		return ENOBUFS;
3636a5bc1d1SSean Bruno 
3646a5bc1d1SSean Bruno 	/*
3656a5bc1d1SSean Bruno 	 * Build a probe request frame.  Most of the following code is a
3666a5bc1d1SSean Bruno 	 * copy & paste of what is done in net80211.
3676a5bc1d1SSean Bruno 	 */
3686a5bc1d1SSean Bruno 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3696a5bc1d1SSean Bruno 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
3706a5bc1d1SSean Bruno 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3716a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
3726a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr2, vap ? vap->iv_myaddr : ic->ic_macaddr);
3736a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
3746a5bc1d1SSean Bruno 	*(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
3756a5bc1d1SSean Bruno 	*(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
3766a5bc1d1SSean Bruno 
3776a5bc1d1SSean Bruno 	frm = (uint8_t *)(wh + 1);
3789afea60fSAndriy Voskoboinyk 	frm = ieee80211_add_ssid(frm, NULL, 0);
3796a5bc1d1SSean Bruno 
3806a5bc1d1SSean Bruno 	/* Tell the firmware where the MAC header is. */
3816a5bc1d1SSean Bruno 	preq->mac_header.offset = 0;
3826a5bc1d1SSean Bruno 	preq->mac_header.len = htole16(frm - (uint8_t *)wh);
3836a5bc1d1SSean Bruno 	remain -= frm - (uint8_t *)wh;
3846a5bc1d1SSean Bruno 
3856a5bc1d1SSean Bruno 	/* Fill in 2GHz IEs and tell firmware where they are. */
3866a5bc1d1SSean Bruno 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
3876a5bc1d1SSean Bruno 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
3886a5bc1d1SSean Bruno 		if (remain < 4 + rs->rs_nrates)
3896a5bc1d1SSean Bruno 			return ENOBUFS;
3906a5bc1d1SSean Bruno 	} else if (remain < 2 + rs->rs_nrates) {
3916a5bc1d1SSean Bruno 		return ENOBUFS;
3926a5bc1d1SSean Bruno 	}
3936a5bc1d1SSean Bruno 	preq->band_data[0].offset = htole16(frm - (uint8_t *)wh);
3946a5bc1d1SSean Bruno 	pos = frm;
3956a5bc1d1SSean Bruno 	frm = ieee80211_add_rates(frm, rs);
3966a5bc1d1SSean Bruno 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
3976a5bc1d1SSean Bruno 		frm = ieee80211_add_xrates(frm, rs);
3986a5bc1d1SSean Bruno 	preq->band_data[0].len = htole16(frm - pos);
3996a5bc1d1SSean Bruno 	remain -= frm - pos;
4006a5bc1d1SSean Bruno 
4016a5bc1d1SSean Bruno 	if (isset(sc->sc_enabled_capa,
4026a5bc1d1SSean Bruno 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT)) {
4036a5bc1d1SSean Bruno 		if (remain < 3)
4046a5bc1d1SSean Bruno 			return ENOBUFS;
4056a5bc1d1SSean Bruno 		*frm++ = IEEE80211_ELEMID_DSPARMS;
4066a5bc1d1SSean Bruno 		*frm++ = 1;
4076a5bc1d1SSean Bruno 		*frm++ = 0;
4086a5bc1d1SSean Bruno 		remain -= 3;
4096a5bc1d1SSean Bruno 	}
4106a5bc1d1SSean Bruno 
4116349bdb3SAdrian Chadd 	if (sc->nvm_data->sku_cap_band_52GHz_enable) {
4126a5bc1d1SSean Bruno 		/* Fill in 5GHz IEs. */
4136a5bc1d1SSean Bruno 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4146a5bc1d1SSean Bruno 		if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
4156a5bc1d1SSean Bruno 			if (remain < 4 + rs->rs_nrates)
4166a5bc1d1SSean Bruno 				return ENOBUFS;
4176a5bc1d1SSean Bruno 		} else if (remain < 2 + rs->rs_nrates) {
4186a5bc1d1SSean Bruno 			return ENOBUFS;
4196a5bc1d1SSean Bruno 		}
4206a5bc1d1SSean Bruno 		preq->band_data[1].offset = htole16(frm - (uint8_t *)wh);
4216a5bc1d1SSean Bruno 		pos = frm;
4226a5bc1d1SSean Bruno 		frm = ieee80211_add_rates(frm, rs);
4236a5bc1d1SSean Bruno 		if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4246a5bc1d1SSean Bruno 			frm = ieee80211_add_xrates(frm, rs);
4256a5bc1d1SSean Bruno 		preq->band_data[1].len = htole16(frm - pos);
4266a5bc1d1SSean Bruno 		remain -= frm - pos;
4276a5bc1d1SSean Bruno 	}
4286a5bc1d1SSean Bruno 
4296a5bc1d1SSean Bruno 	/* Send 11n IEs on both 2GHz and 5GHz bands. */
4306a5bc1d1SSean Bruno 	preq->common_data.offset = htole16(frm - (uint8_t *)wh);
4316a5bc1d1SSean Bruno 	pos = frm;
4326a5bc1d1SSean Bruno #if 0
4336a5bc1d1SSean Bruno 	if (ic->ic_flags & IEEE80211_F_HTON) {
4346a5bc1d1SSean Bruno 		if (remain < 28)
4356a5bc1d1SSean Bruno 			return ENOBUFS;
4366a5bc1d1SSean Bruno 		frm = ieee80211_add_htcaps(frm, ic);
4376a5bc1d1SSean Bruno 		/* XXX add WME info? */
4386a5bc1d1SSean Bruno 	}
4396a5bc1d1SSean Bruno #endif
4406a5bc1d1SSean Bruno 	preq->common_data.len = htole16(frm - pos);
4416a5bc1d1SSean Bruno 
4426a5bc1d1SSean Bruno 	return 0;
443d4886179SRui Paulo }
444d4886179SRui Paulo 
445d4886179SRui Paulo int
4466a5bc1d1SSean Bruno iwm_mvm_config_umac_scan(struct iwm_softc *sc)
447d4886179SRui Paulo {
448bc6cd01dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
449bc6cd01dSAdrian Chadd 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
450d4886179SRui Paulo 
4516a5bc1d1SSean Bruno 	struct iwm_scan_config *scan_config;
4526a5bc1d1SSean Bruno 	int ret, j, nchan;
4536a5bc1d1SSean Bruno 	size_t cmd_size;
4546a5bc1d1SSean Bruno 	struct ieee80211_channel *c;
4556a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
4566a5bc1d1SSean Bruno 		.id = iwm_cmd_id(IWM_SCAN_CFG_CMD, IWM_ALWAYS_LONG_GROUP, 0),
4576a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
4586a5bc1d1SSean Bruno 	};
4596a5bc1d1SSean Bruno 	static const uint32_t rates = (IWM_SCAN_CONFIG_RATE_1M |
4606a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_2M | IWM_SCAN_CONFIG_RATE_5M |
4616a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_11M | IWM_SCAN_CONFIG_RATE_6M |
4626a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_9M | IWM_SCAN_CONFIG_RATE_12M |
4636a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_18M | IWM_SCAN_CONFIG_RATE_24M |
4646a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_36M | IWM_SCAN_CONFIG_RATE_48M |
4656a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_54M);
4666a5bc1d1SSean Bruno 
4676a5bc1d1SSean Bruno 	cmd_size = sizeof(*scan_config) + sc->sc_capa_n_scan_channels;
4686a5bc1d1SSean Bruno 
4696a5bc1d1SSean Bruno 	scan_config = malloc(cmd_size, M_DEVBUF, M_NOWAIT | M_ZERO);
4706a5bc1d1SSean Bruno 	if (scan_config == NULL)
4716a5bc1d1SSean Bruno 		return ENOMEM;
4726a5bc1d1SSean Bruno 
4732a2476b3SAdrian Chadd 	scan_config->tx_chains = htole32(iwm_mvm_get_valid_tx_ant(sc));
4742a2476b3SAdrian Chadd 	scan_config->rx_chains = htole32(iwm_mvm_get_valid_rx_ant(sc));
4756a5bc1d1SSean Bruno 	scan_config->legacy_rates = htole32(rates |
4766a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_SUPPORTED_RATE(rates));
4776a5bc1d1SSean Bruno 
4786a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
4796a5bc1d1SSean Bruno 	scan_config->dwell_active = 10;
4806a5bc1d1SSean Bruno 	scan_config->dwell_passive = 110;
4816a5bc1d1SSean Bruno 	scan_config->dwell_fragmented = 44;
4826a5bc1d1SSean Bruno 	scan_config->dwell_extended = 90;
4836a5bc1d1SSean Bruno 	scan_config->out_of_channel_time = htole32(0);
4846a5bc1d1SSean Bruno 	scan_config->suspend_time = htole32(0);
4856a5bc1d1SSean Bruno 
4866a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(scan_config->mac_addr,
4876a5bc1d1SSean Bruno 	    vap ? vap->iv_myaddr : ic->ic_macaddr);
4886a5bc1d1SSean Bruno 
4896a5bc1d1SSean Bruno 	scan_config->bcast_sta_id = sc->sc_aux_sta.sta_id;
4906a5bc1d1SSean Bruno 	scan_config->channel_flags = IWM_CHANNEL_FLAG_EBS |
4916a5bc1d1SSean Bruno 	    IWM_CHANNEL_FLAG_ACCURATE_EBS | IWM_CHANNEL_FLAG_EBS_ADD |
4926a5bc1d1SSean Bruno 	    IWM_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
4936a5bc1d1SSean Bruno 
4946a5bc1d1SSean Bruno 	for (nchan = j = 0;
4956a5bc1d1SSean Bruno 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
4966a5bc1d1SSean Bruno 		c = &ic->ic_channels[j];
4976a5bc1d1SSean Bruno 		/* For 2GHz, only populate 11b channels */
4986a5bc1d1SSean Bruno 		/* For 5GHz, only populate 11a channels */
4996a5bc1d1SSean Bruno 		/*
5006a5bc1d1SSean Bruno 		 * Catch other channels, in case we have 900MHz channels or
5016a5bc1d1SSean Bruno 		 * something in the chanlist.
5026a5bc1d1SSean Bruno 		 */
5036a5bc1d1SSean Bruno 		if (iwm_mvm_scan_skip_channel(c))
5046a5bc1d1SSean Bruno 			continue;
5056a5bc1d1SSean Bruno 		scan_config->channel_array[nchan++] =
5066a5bc1d1SSean Bruno 		    ieee80211_mhz2ieee(c->ic_freq, 0);
5076a5bc1d1SSean Bruno 	}
5086a5bc1d1SSean Bruno 
5096a5bc1d1SSean Bruno 	scan_config->flags = htole32(IWM_SCAN_CONFIG_FLAG_ACTIVATE |
5106a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
5116a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_TX_CHAINS |
5126a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_RX_CHAINS |
5136a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
5146a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_ALL_TIMES |
5156a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
5166a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_MAC_ADDR |
5176a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
5186a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_N_CHANNELS(nchan) |
5196a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
5206a5bc1d1SSean Bruno 
5216a5bc1d1SSean Bruno 	hcmd.data[0] = scan_config;
5226a5bc1d1SSean Bruno 	hcmd.len[0] = cmd_size;
5236a5bc1d1SSean Bruno 
5246a5bc1d1SSean Bruno 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending UMAC scan config\n");
5256a5bc1d1SSean Bruno 
5266a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
5276a5bc1d1SSean Bruno 	if (!ret)
5286a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
5296a5bc1d1SSean Bruno 		    "UMAC scan config was sent successfully\n");
5306a5bc1d1SSean Bruno 
5316a5bc1d1SSean Bruno 	free(scan_config, M_DEVBUF);
5326a5bc1d1SSean Bruno 	return ret;
5336a5bc1d1SSean Bruno }
5346a5bc1d1SSean Bruno 
5356a5bc1d1SSean Bruno int
5366a5bc1d1SSean Bruno iwm_mvm_umac_scan(struct iwm_softc *sc)
5376a5bc1d1SSean Bruno {
5386a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
5396a5bc1d1SSean Bruno 		.id = iwm_cmd_id(IWM_SCAN_REQ_UMAC, IWM_ALWAYS_LONG_GROUP, 0),
5406a5bc1d1SSean Bruno 		.len = { 0, },
5416a5bc1d1SSean Bruno 		.data = { NULL, },
5426a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
5436a5bc1d1SSean Bruno 	};
5449afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
5456a5bc1d1SSean Bruno 	struct iwm_scan_req_umac *req;
5466a5bc1d1SSean Bruno 	struct iwm_scan_req_umac_tail *tail;
5476a5bc1d1SSean Bruno 	size_t req_len;
5489afea60fSAndriy Voskoboinyk 	uint8_t i, nssid;
5496a5bc1d1SSean Bruno 	int ret;
5506a5bc1d1SSean Bruno 
5516a5bc1d1SSean Bruno 	req_len = sizeof(struct iwm_scan_req_umac) +
5526a5bc1d1SSean Bruno 	    (sizeof(struct iwm_scan_channel_cfg_umac) *
5536a5bc1d1SSean Bruno 	    sc->sc_capa_n_scan_channels) +
5546a5bc1d1SSean Bruno 	    sizeof(struct iwm_scan_req_umac_tail);
5556a5bc1d1SSean Bruno 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
5566a5bc1d1SSean Bruno 		return ENOMEM;
5576a5bc1d1SSean Bruno 	req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
5586a5bc1d1SSean Bruno 	if (req == NULL)
5596a5bc1d1SSean Bruno 		return ENOMEM;
5606a5bc1d1SSean Bruno 
5616a5bc1d1SSean Bruno 	hcmd.len[0] = (uint16_t)req_len;
5626a5bc1d1SSean Bruno 	hcmd.data[0] = (void *)req;
5636a5bc1d1SSean Bruno 
5646a5bc1d1SSean Bruno 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Handling ieee80211 scan request\n");
5656a5bc1d1SSean Bruno 
5666a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
5676a5bc1d1SSean Bruno 	req->active_dwell = 10;
5686a5bc1d1SSean Bruno 	req->passive_dwell = 110;
5696a5bc1d1SSean Bruno 	req->fragmented_dwell = 44;
5706a5bc1d1SSean Bruno 	req->extended_dwell = 90;
5716a5bc1d1SSean Bruno 	req->max_out_time = 0;
5726a5bc1d1SSean Bruno 	req->suspend_time = 0;
5736a5bc1d1SSean Bruno 
5746a5bc1d1SSean Bruno 	req->scan_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
5756a5bc1d1SSean Bruno 	req->ooc_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
5766a5bc1d1SSean Bruno 
5779afea60fSAndriy Voskoboinyk 	nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
5786a5bc1d1SSean Bruno 	req->n_channels = iwm_mvm_umac_scan_fill_channels(sc,
5799afea60fSAndriy Voskoboinyk 	    (struct iwm_scan_channel_cfg_umac *)req->data, nssid);
5806a5bc1d1SSean Bruno 
5816a5bc1d1SSean Bruno 	req->general_flags = htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASS_ALL |
5826a5bc1d1SSean Bruno 	    IWM_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE |
5836a5bc1d1SSean Bruno 	    IWM_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL);
5846a5bc1d1SSean Bruno 
5856a5bc1d1SSean Bruno 	tail = (void *)((char *)&req->data +
5866a5bc1d1SSean Bruno 		sizeof(struct iwm_scan_channel_cfg_umac) *
5876a5bc1d1SSean Bruno 			sc->sc_capa_n_scan_channels);
5886a5bc1d1SSean Bruno 
5896a5bc1d1SSean Bruno 	/* Check if we're doing an active directed scan. */
5909afea60fSAndriy Voskoboinyk 	for (i = 0; i < nssid; i++) {
5919afea60fSAndriy Voskoboinyk 		tail->direct_scan[i].id = IEEE80211_ELEMID_SSID;
5929afea60fSAndriy Voskoboinyk 		tail->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
5939afea60fSAndriy Voskoboinyk 		    IEEE80211_NWID_LEN);
5949afea60fSAndriy Voskoboinyk 		memcpy(tail->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
5959afea60fSAndriy Voskoboinyk 		    tail->direct_scan[i].len);
5969afea60fSAndriy Voskoboinyk 		/* XXX debug */
5979afea60fSAndriy Voskoboinyk 	}
5989afea60fSAndriy Voskoboinyk 	if (nssid != 0) {
5996a5bc1d1SSean Bruno 		req->general_flags |=
6006a5bc1d1SSean Bruno 		    htole32(IWM_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT);
6019afea60fSAndriy Voskoboinyk 	} else
6026a5bc1d1SSean Bruno 		req->general_flags |= htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASSIVE);
6036a5bc1d1SSean Bruno 
6046a5bc1d1SSean Bruno 	if (isset(sc->sc_enabled_capa,
6056a5bc1d1SSean Bruno 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
6066a5bc1d1SSean Bruno 		req->general_flags |=
6076a5bc1d1SSean Bruno 		    htole32(IWM_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED);
6086a5bc1d1SSean Bruno 
6096a5bc1d1SSean Bruno 	ret = iwm_mvm_fill_probe_req(sc, &tail->preq);
6106a5bc1d1SSean Bruno 	if (ret) {
6116a5bc1d1SSean Bruno 		free(req, M_DEVBUF);
6126a5bc1d1SSean Bruno 		return ret;
6136a5bc1d1SSean Bruno 	}
6146a5bc1d1SSean Bruno 
6156a5bc1d1SSean Bruno 	/* Specify the scan plan: We'll do one iteration. */
6166a5bc1d1SSean Bruno 	tail->schedule[0].interval = 0;
6176a5bc1d1SSean Bruno 	tail->schedule[0].iter_count = 1;
6186a5bc1d1SSean Bruno 
6196a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
6206a5bc1d1SSean Bruno 	if (!ret)
6216a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
6226a5bc1d1SSean Bruno 		    "Scan request was sent successfully\n");
6236a5bc1d1SSean Bruno 	free(req, M_DEVBUF);
6246a5bc1d1SSean Bruno 	return ret;
6256a5bc1d1SSean Bruno }
6266a5bc1d1SSean Bruno 
6276a5bc1d1SSean Bruno int
6286a5bc1d1SSean Bruno iwm_mvm_lmac_scan(struct iwm_softc *sc)
6296a5bc1d1SSean Bruno {
6306a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
6316a5bc1d1SSean Bruno 		.id = IWM_SCAN_OFFLOAD_REQUEST_CMD,
6326a5bc1d1SSean Bruno 		.len = { 0, },
6336a5bc1d1SSean Bruno 		.data = { NULL, },
6346a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
6356a5bc1d1SSean Bruno 	};
6369afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
6376a5bc1d1SSean Bruno 	struct iwm_scan_req_lmac *req;
6386a5bc1d1SSean Bruno 	size_t req_len;
6399afea60fSAndriy Voskoboinyk 	uint8_t i, nssid;
6406a5bc1d1SSean Bruno 	int ret;
641d4886179SRui Paulo 
642d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
643d4886179SRui Paulo 	    "Handling ieee80211 scan request\n");
644d4886179SRui Paulo 
6456a5bc1d1SSean Bruno 	req_len = sizeof(struct iwm_scan_req_lmac) +
6466a5bc1d1SSean Bruno 	    (sizeof(struct iwm_scan_channel_cfg_lmac) *
6476a5bc1d1SSean Bruno 	    sc->sc_capa_n_scan_channels) + sizeof(struct iwm_scan_probe_req);
6486a5bc1d1SSean Bruno 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
6496a5bc1d1SSean Bruno 		return ENOMEM;
6506a5bc1d1SSean Bruno 	req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
6516a5bc1d1SSean Bruno 	if (req == NULL)
6526a5bc1d1SSean Bruno 		return ENOMEM;
653d4886179SRui Paulo 
6546a5bc1d1SSean Bruno 	hcmd.len[0] = (uint16_t)req_len;
6556a5bc1d1SSean Bruno 	hcmd.data[0] = (void *)req;
656d4886179SRui Paulo 
6576a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
6586a5bc1d1SSean Bruno 	req->active_dwell = 10;
6596a5bc1d1SSean Bruno 	req->passive_dwell = 110;
6606a5bc1d1SSean Bruno 	req->fragmented_dwell = 44;
6616a5bc1d1SSean Bruno 	req->extended_dwell = 90;
6626a5bc1d1SSean Bruno 	req->max_out_time = 0;
6636a5bc1d1SSean Bruno 	req->suspend_time = 0;
664d4886179SRui Paulo 
6656a5bc1d1SSean Bruno 	req->scan_prio = htole32(IWM_SCAN_PRIORITY_HIGH);
6666a5bc1d1SSean Bruno 	req->rx_chain_select = iwm_mvm_scan_rx_chain(sc);
6676a5bc1d1SSean Bruno 	req->iter_num = htole32(1);
6686a5bc1d1SSean Bruno 	req->delay = 0;
6696a5bc1d1SSean Bruno 
6706a5bc1d1SSean Bruno 	req->scan_flags = htole32(IWM_MVM_LMAC_SCAN_FLAG_PASS_ALL |
6716a5bc1d1SSean Bruno 	    IWM_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE |
6726a5bc1d1SSean Bruno 	    IWM_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL);
6736a5bc1d1SSean Bruno 	if (isset(sc->sc_enabled_capa,
6746a5bc1d1SSean Bruno 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
6756a5bc1d1SSean Bruno 		req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
6766a5bc1d1SSean Bruno 
6776a5bc1d1SSean Bruno 	req->flags = htole32(IWM_PHY_BAND_24);
6786349bdb3SAdrian Chadd 	if (sc->nvm_data->sku_cap_band_52GHz_enable)
6796a5bc1d1SSean Bruno 		req->flags |= htole32(IWM_PHY_BAND_5);
6806a5bc1d1SSean Bruno 	req->filter_flags =
6816a5bc1d1SSean Bruno 	    htole32(IWM_MAC_FILTER_ACCEPT_GRP | IWM_MAC_FILTER_IN_BEACON);
6826a5bc1d1SSean Bruno 
6836a5bc1d1SSean Bruno 	/* Tx flags 2 GHz. */
6846a5bc1d1SSean Bruno 	req->tx_cmd[0].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
685d4886179SRui Paulo 	    IWM_TX_CMD_FLG_BT_DIS);
6866a5bc1d1SSean Bruno 	req->tx_cmd[0].rate_n_flags =
6876a5bc1d1SSean Bruno 	    iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_2GHZ, 1/*XXX*/);
6886a5bc1d1SSean Bruno 	req->tx_cmd[0].sta_id = sc->sc_aux_sta.sta_id;
689d4886179SRui Paulo 
6906a5bc1d1SSean Bruno 	/* Tx flags 5 GHz. */
6916a5bc1d1SSean Bruno 	req->tx_cmd[1].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
6926a5bc1d1SSean Bruno 	    IWM_TX_CMD_FLG_BT_DIS);
6936a5bc1d1SSean Bruno 	req->tx_cmd[1].rate_n_flags =
6946a5bc1d1SSean Bruno 	    iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_5GHZ, 1/*XXX*/);
6956a5bc1d1SSean Bruno 	req->tx_cmd[1].sta_id = sc->sc_aux_sta.sta_id;
696d4886179SRui Paulo 
6976a5bc1d1SSean Bruno 	/* Check if we're doing an active directed scan. */
6989afea60fSAndriy Voskoboinyk 	nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
6999afea60fSAndriy Voskoboinyk 	for (i = 0; i < nssid; i++) {
7009afea60fSAndriy Voskoboinyk 		req->direct_scan[i].id = IEEE80211_ELEMID_SSID;
7019afea60fSAndriy Voskoboinyk 		req->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
7029afea60fSAndriy Voskoboinyk 		    IEEE80211_NWID_LEN);
7039afea60fSAndriy Voskoboinyk 		memcpy(req->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
7049afea60fSAndriy Voskoboinyk 		    req->direct_scan[i].len);
7059afea60fSAndriy Voskoboinyk 		/* XXX debug */
7066a5bc1d1SSean Bruno 	}
7079afea60fSAndriy Voskoboinyk 	if (nssid != 0) {
7089afea60fSAndriy Voskoboinyk 		req->scan_flags |=
7099afea60fSAndriy Voskoboinyk 		    htole32(IWM_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION);
7109afea60fSAndriy Voskoboinyk 	} else
7119afea60fSAndriy Voskoboinyk 		req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAG_PASSIVE);
712d4886179SRui Paulo 
7136a5bc1d1SSean Bruno 	req->n_channels = iwm_mvm_lmac_scan_fill_channels(sc,
7149afea60fSAndriy Voskoboinyk 	    (struct iwm_scan_channel_cfg_lmac *)req->data, nssid);
715d4886179SRui Paulo 
7166a5bc1d1SSean Bruno 	ret = iwm_mvm_fill_probe_req(sc,
7176a5bc1d1SSean Bruno 			    (struct iwm_scan_probe_req *)(req->data +
7186a5bc1d1SSean Bruno 			    (sizeof(struct iwm_scan_channel_cfg_lmac) *
7196a5bc1d1SSean Bruno 			    sc->sc_capa_n_scan_channels)));
7206a5bc1d1SSean Bruno 	if (ret) {
7216a5bc1d1SSean Bruno 		free(req, M_DEVBUF);
7226a5bc1d1SSean Bruno 		return ret;
7236a5bc1d1SSean Bruno 	}
7246a5bc1d1SSean Bruno 
7256a5bc1d1SSean Bruno 	/* Specify the scan plan: We'll do one iteration. */
7266a5bc1d1SSean Bruno 	req->schedule[0].iterations = 1;
7276a5bc1d1SSean Bruno 	req->schedule[0].full_scan_mul = 1;
7286a5bc1d1SSean Bruno 
7296a5bc1d1SSean Bruno 	/* Disable EBS. */
7306a5bc1d1SSean Bruno 	req->channel_opt[0].non_ebs_ratio = 1;
7316a5bc1d1SSean Bruno 	req->channel_opt[1].non_ebs_ratio = 1;
7326a5bc1d1SSean Bruno 
7336a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
7346a5bc1d1SSean Bruno 	if (!ret) {
735d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
736d4886179SRui Paulo 		    "Scan request was sent successfully\n");
737d4886179SRui Paulo 	}
7386a5bc1d1SSean Bruno 	free(req, M_DEVBUF);
739d4886179SRui Paulo 	return ret;
740d4886179SRui Paulo }
7414b17c188SAdrian Chadd 
7424b17c188SAdrian Chadd static int
7434b17c188SAdrian Chadd iwm_mvm_lmac_scan_abort(struct iwm_softc *sc)
7444b17c188SAdrian Chadd {
7454b17c188SAdrian Chadd 	int ret;
7464b17c188SAdrian Chadd 	struct iwm_host_cmd hcmd = {
7474b17c188SAdrian Chadd 		.id = IWM_SCAN_OFFLOAD_ABORT_CMD,
7484b17c188SAdrian Chadd 		.len = { 0, },
7494b17c188SAdrian Chadd 		.data = { NULL, },
7504b17c188SAdrian Chadd 		.flags = IWM_CMD_SYNC,
7514b17c188SAdrian Chadd 	};
7524b17c188SAdrian Chadd 	uint32_t status;
7534b17c188SAdrian Chadd 
7544b17c188SAdrian Chadd 	ret = iwm_mvm_send_cmd_status(sc, &hcmd, &status);
7554b17c188SAdrian Chadd 	if (ret)
7564b17c188SAdrian Chadd 		return ret;
7574b17c188SAdrian Chadd 
7584b17c188SAdrian Chadd 	if (status != IWM_CAN_ABORT_STATUS) {
7594b17c188SAdrian Chadd 		/*
7604b17c188SAdrian Chadd 		 * The scan abort will return 1 for success or
7614b17c188SAdrian Chadd 		 * 2 for "failure".  A failure condition can be
7624b17c188SAdrian Chadd 		 * due to simply not being in an active scan which
7634b17c188SAdrian Chadd 		 * can occur if we send the scan abort before the
7644b17c188SAdrian Chadd 		 * microcode has notified us that a scan is completed.
7654b17c188SAdrian Chadd 		 */
7664b17c188SAdrian Chadd 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
7674b17c188SAdrian Chadd 		    "SCAN OFFLOAD ABORT ret %d.\n", status);
7684b17c188SAdrian Chadd 		ret = ENOENT;
7694b17c188SAdrian Chadd 	}
7704b17c188SAdrian Chadd 
7714b17c188SAdrian Chadd 	return ret;
7724b17c188SAdrian Chadd }
7734b17c188SAdrian Chadd 
7744b17c188SAdrian Chadd static int
7754b17c188SAdrian Chadd iwm_mvm_umac_scan_abort(struct iwm_softc *sc)
7764b17c188SAdrian Chadd {
7774b17c188SAdrian Chadd 	struct iwm_umac_scan_abort cmd = {};
7784b17c188SAdrian Chadd 	int uid, ret;
7794b17c188SAdrian Chadd 
7804b17c188SAdrian Chadd 	uid = 0;
7814b17c188SAdrian Chadd 	cmd.uid = htole32(uid);
7824b17c188SAdrian Chadd 
7834b17c188SAdrian Chadd 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending scan abort, uid %u\n", uid);
7844b17c188SAdrian Chadd 
7854b17c188SAdrian Chadd 	ret = iwm_mvm_send_cmd_pdu(sc,
7864b17c188SAdrian Chadd 				   iwm_cmd_id(IWM_SCAN_ABORT_UMAC,
7874b17c188SAdrian Chadd 					      IWM_ALWAYS_LONG_GROUP, 0),
7884b17c188SAdrian Chadd 				   0, sizeof(cmd), &cmd);
7894b17c188SAdrian Chadd 
7904b17c188SAdrian Chadd 	return ret;
7914b17c188SAdrian Chadd }
7924b17c188SAdrian Chadd 
7934b17c188SAdrian Chadd int
7944b17c188SAdrian Chadd iwm_mvm_scan_stop_wait(struct iwm_softc *sc)
7954b17c188SAdrian Chadd {
7964b17c188SAdrian Chadd 	struct iwm_notification_wait wait_scan_done;
7974b17c188SAdrian Chadd 	static const uint16_t scan_done_notif[] = { IWM_SCAN_COMPLETE_UMAC,
7984b17c188SAdrian Chadd 						   IWM_SCAN_OFFLOAD_COMPLETE, };
7994b17c188SAdrian Chadd 	int ret;
8004b17c188SAdrian Chadd 
8014b17c188SAdrian Chadd 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_scan_done,
8024b17c188SAdrian Chadd 				   scan_done_notif, nitems(scan_done_notif),
8034b17c188SAdrian Chadd 				   NULL, NULL);
8044b17c188SAdrian Chadd 
8054b17c188SAdrian Chadd 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Preparing to stop scan\n");
8064b17c188SAdrian Chadd 
8074b17c188SAdrian Chadd 	if (isset(sc->sc_enabled_capa, IWM_UCODE_TLV_CAPA_UMAC_SCAN))
8084b17c188SAdrian Chadd 		ret = iwm_mvm_umac_scan_abort(sc);
8094b17c188SAdrian Chadd 	else
8104b17c188SAdrian Chadd 		ret = iwm_mvm_lmac_scan_abort(sc);
8114b17c188SAdrian Chadd 
8124b17c188SAdrian Chadd 	if (ret) {
8134b17c188SAdrian Chadd 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "couldn't stop scan\n");
8144b17c188SAdrian Chadd 		iwm_remove_notification(sc->sc_notif_wait, &wait_scan_done);
8154b17c188SAdrian Chadd 		return ret;
8164b17c188SAdrian Chadd 	}
8174b17c188SAdrian Chadd 
8184b17c188SAdrian Chadd 	IWM_UNLOCK(sc);
8194b17c188SAdrian Chadd 	ret = iwm_wait_notification(sc->sc_notif_wait, &wait_scan_done, hz);
8204b17c188SAdrian Chadd 	IWM_LOCK(sc);
8214b17c188SAdrian Chadd 
8224b17c188SAdrian Chadd 	return ret;
8234b17c188SAdrian Chadd }
824