xref: /freebsd/sys/dev/iwm/if_iwm_scan.c (revision 6349bdb30d6a53e3dd8e8c75ed1dd051d4cda0b8)
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>
15649fdbf0aSRui Paulo #include <dev/iwm/if_iwm_util.h>
15749fdbf0aSRui Paulo #include <dev/iwm/if_iwm_scan.h>
158d4886179SRui Paulo 
159d4886179SRui Paulo /*
160d4886179SRui Paulo  * BEGIN mvm/scan.c
161d4886179SRui Paulo  */
162d4886179SRui Paulo 
163d4886179SRui Paulo #define IWM_PLCP_QUIET_THRESH 1
164d4886179SRui Paulo #define IWM_ACTIVE_QUIET_TIME 10
165d4886179SRui Paulo #define LONG_OUT_TIME_PERIOD (600 * IEEE80211_DUR_TU)
166d4886179SRui Paulo #define SHORT_OUT_TIME_PERIOD (200 * IEEE80211_DUR_TU)
167d4886179SRui Paulo #define SUSPEND_TIME_PERIOD (100 * IEEE80211_DUR_TU)
168d4886179SRui Paulo 
169d4886179SRui Paulo static uint16_t
170d4886179SRui Paulo iwm_mvm_scan_rx_chain(struct iwm_softc *sc)
171d4886179SRui Paulo {
172d4886179SRui Paulo 	uint16_t rx_chain;
173d4886179SRui Paulo 	uint8_t rx_ant;
174d4886179SRui Paulo 
1756a5bc1d1SSean Bruno 	rx_ant = iwm_fw_valid_rx_ant(sc);
176d4886179SRui Paulo 	rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
177d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
178d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
179d4886179SRui Paulo 	rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
180d4886179SRui Paulo 	return htole16(rx_chain);
181d4886179SRui Paulo }
182d4886179SRui Paulo 
1836a5bc1d1SSean Bruno #if 0
184d4886179SRui Paulo static uint32_t
185d4886179SRui Paulo iwm_mvm_scan_max_out_time(struct iwm_softc *sc, uint32_t flags, int is_assoc)
186d4886179SRui Paulo {
187d4886179SRui Paulo 	if (!is_assoc)
188d4886179SRui Paulo 		return 0;
189d4886179SRui Paulo 	if (flags & 0x1)
190d4886179SRui Paulo 		return htole32(SHORT_OUT_TIME_PERIOD);
191d4886179SRui Paulo 	return htole32(LONG_OUT_TIME_PERIOD);
192d4886179SRui Paulo }
193d4886179SRui Paulo 
194d4886179SRui Paulo static uint32_t
195d4886179SRui Paulo iwm_mvm_scan_suspend_time(struct iwm_softc *sc, int is_assoc)
196d4886179SRui Paulo {
197d4886179SRui Paulo 	if (!is_assoc)
198d4886179SRui Paulo 		return 0;
199d4886179SRui Paulo 	return htole32(SUSPEND_TIME_PERIOD);
200d4886179SRui Paulo }
2016a5bc1d1SSean Bruno #endif
202d4886179SRui Paulo 
203d4886179SRui Paulo static uint32_t
204d4886179SRui Paulo iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
205d4886179SRui Paulo {
206d4886179SRui Paulo 	uint32_t tx_ant;
207d4886179SRui Paulo 	int i, ind;
208d4886179SRui Paulo 
209d4886179SRui Paulo 	for (i = 0, ind = sc->sc_scan_last_antenna;
210d4886179SRui Paulo 	    i < IWM_RATE_MCS_ANT_NUM; i++) {
211d4886179SRui Paulo 		ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
2126a5bc1d1SSean Bruno 		if (iwm_fw_valid_tx_ant(sc) & (1 << ind)) {
213d4886179SRui Paulo 			sc->sc_scan_last_antenna = ind;
214d4886179SRui Paulo 			break;
215d4886179SRui Paulo 		}
216d4886179SRui Paulo 	}
217d4886179SRui Paulo 	tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
218d4886179SRui Paulo 
219d4886179SRui Paulo 	if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
220d4886179SRui Paulo 		return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
221d4886179SRui Paulo 				   tx_ant);
222d4886179SRui Paulo 	else
223d4886179SRui Paulo 		return htole32(IWM_RATE_6M_PLCP | tx_ant);
224d4886179SRui Paulo }
225d4886179SRui Paulo 
2266a5bc1d1SSean Bruno #if 0
227d4886179SRui Paulo /*
228d4886179SRui Paulo  * If req->n_ssids > 0, it means we should do an active scan.
229d4886179SRui Paulo  * In case of active scan w/o directed scan, we receive a zero-length SSID
230d4886179SRui Paulo  * just to notify that this scan is active and not passive.
231d4886179SRui Paulo  * In order to notify the FW of the number of SSIDs we wish to scan (including
232d4886179SRui Paulo  * the zero-length one), we need to set the corresponding bits in chan->type,
233d4886179SRui Paulo  * one for each SSID, and set the active bit (first). If the first SSID is
234d4886179SRui Paulo  * already included in the probe template, so we need to set only
235d4886179SRui Paulo  * req->n_ssids - 1 bits in addition to the first bit.
236d4886179SRui Paulo  */
237d4886179SRui Paulo static uint16_t
238d4886179SRui Paulo iwm_mvm_get_active_dwell(struct iwm_softc *sc, int flags, int n_ssids)
239d4886179SRui Paulo {
240d4886179SRui Paulo 	if (flags & IEEE80211_CHAN_2GHZ)
241d4886179SRui Paulo 		return 30  + 3 * (n_ssids + 1);
242d4886179SRui Paulo 	return 20  + 2 * (n_ssids + 1);
243d4886179SRui Paulo }
244d4886179SRui Paulo 
245d4886179SRui Paulo static uint16_t
246d4886179SRui Paulo iwm_mvm_get_passive_dwell(struct iwm_softc *sc, int flags)
247d4886179SRui Paulo {
248d4886179SRui Paulo 	return (flags & IEEE80211_CHAN_2GHZ) ? 100 + 20 : 100 + 10;
249d4886179SRui Paulo }
2506a5bc1d1SSean Bruno #endif
251d4886179SRui Paulo 
252d4886179SRui Paulo static int
2536a5bc1d1SSean Bruno iwm_mvm_scan_skip_channel(struct ieee80211_channel *c)
2546a5bc1d1SSean Bruno {
2556a5bc1d1SSean Bruno 	if (IEEE80211_IS_CHAN_2GHZ(c) && IEEE80211_IS_CHAN_B(c))
2566a5bc1d1SSean Bruno 		return 0;
2576a5bc1d1SSean Bruno 	else if (IEEE80211_IS_CHAN_5GHZ(c) && IEEE80211_IS_CHAN_A(c))
2586a5bc1d1SSean Bruno 		return 0;
2596a5bc1d1SSean Bruno 	else
2606a5bc1d1SSean Bruno 		return 1;
2616a5bc1d1SSean Bruno }
2626a5bc1d1SSean Bruno 
2636a5bc1d1SSean Bruno static uint8_t
2646a5bc1d1SSean Bruno iwm_mvm_lmac_scan_fill_channels(struct iwm_softc *sc,
2656a5bc1d1SSean Bruno     struct iwm_scan_channel_cfg_lmac *chan, int n_ssids)
266d4886179SRui Paulo {
2677a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
2689afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = ic->ic_scan;
269d4886179SRui Paulo 	struct ieee80211_channel *c;
2706a5bc1d1SSean Bruno 	uint8_t nchan;
2716a5bc1d1SSean Bruno 	int j;
272d4886179SRui Paulo 
2736a5bc1d1SSean Bruno 	for (nchan = j = 0;
2746a5bc1d1SSean Bruno 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
275d4886179SRui Paulo 		c = &ic->ic_channels[j];
276d4886179SRui Paulo 		/* For 2GHz, only populate 11b channels */
277d4886179SRui Paulo 		/* For 5GHz, only populate 11a channels */
278d4886179SRui Paulo 		/*
279d4886179SRui Paulo 		 * Catch other channels, in case we have 900MHz channels or
280d4886179SRui Paulo 		 * something in the chanlist.
281d4886179SRui Paulo 		 */
2826a5bc1d1SSean Bruno 		if (iwm_mvm_scan_skip_channel(c)) {
283d4886179SRui Paulo 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
284d4886179SRui Paulo 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
2856a5bc1d1SSean Bruno 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
2866a5bc1d1SSean Bruno 			continue;
287d4886179SRui Paulo 		}
2886a5bc1d1SSean Bruno 
289d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
290d4886179SRui Paulo 		    "Adding channel %d (%d Mhz) to the list\n",
291d4886179SRui Paulo 		    nchan, c->ic_freq);
2926a5bc1d1SSean Bruno 		chan->channel_num = htole16(ieee80211_mhz2ieee(c->ic_freq, 0));
2936a5bc1d1SSean Bruno 		chan->iter_count = htole16(1);
2946a5bc1d1SSean Bruno 		chan->iter_interval = htole32(0);
2956a5bc1d1SSean Bruno 		chan->flags = htole32(IWM_UNIFIED_SCAN_CHANNEL_PARTIAL);
2969afea60fSAndriy Voskoboinyk 		chan->flags |= htole32(IWM_SCAN_CHANNEL_NSSIDS(n_ssids));
2979afea60fSAndriy Voskoboinyk 		/* XXX IEEE80211_SCAN_NOBCAST flag is never set. */
2989afea60fSAndriy Voskoboinyk 		if (!IEEE80211_IS_CHAN_PASSIVE(c) &&
2999afea60fSAndriy Voskoboinyk 		    (!(ss->ss_flags & IEEE80211_SCAN_NOBCAST) || n_ssids != 0))
3009afea60fSAndriy Voskoboinyk 			chan->flags |= htole32(IWM_SCAN_CHANNEL_TYPE_ACTIVE);
301d4886179SRui Paulo 		chan++;
302d4886179SRui Paulo 		nchan++;
303d4886179SRui Paulo 	}
3046a5bc1d1SSean Bruno 
305d4886179SRui Paulo 	return nchan;
306d4886179SRui Paulo }
307d4886179SRui Paulo 
3086a5bc1d1SSean Bruno static uint8_t
3096a5bc1d1SSean Bruno iwm_mvm_umac_scan_fill_channels(struct iwm_softc *sc,
3106a5bc1d1SSean Bruno     struct iwm_scan_channel_cfg_umac *chan, int n_ssids)
311d4886179SRui Paulo {
3126a5bc1d1SSean Bruno 	struct ieee80211com *ic = &sc->sc_ic;
3136a5bc1d1SSean Bruno 	struct ieee80211_channel *c;
3146a5bc1d1SSean Bruno 	uint8_t nchan;
3156a5bc1d1SSean Bruno 	int j;
316d4886179SRui Paulo 
3176a5bc1d1SSean Bruno 	for (nchan = j = 0;
3186a5bc1d1SSean Bruno 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
3196a5bc1d1SSean Bruno 		c = &ic->ic_channels[j];
3206a5bc1d1SSean Bruno 		/* For 2GHz, only populate 11b channels */
3216a5bc1d1SSean Bruno 		/* For 5GHz, only populate 11a channels */
3226a5bc1d1SSean Bruno 		/*
3236a5bc1d1SSean Bruno 		 * Catch other channels, in case we have 900MHz channels or
3246a5bc1d1SSean Bruno 		 * something in the chanlist.
3256a5bc1d1SSean Bruno 		 */
3266a5bc1d1SSean Bruno 		if (iwm_mvm_scan_skip_channel(c)) {
3276a5bc1d1SSean Bruno 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
3286a5bc1d1SSean Bruno 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
3296a5bc1d1SSean Bruno 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
3306a5bc1d1SSean Bruno 			continue;
331d4886179SRui Paulo 		}
332d4886179SRui Paulo 
3336a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
3346a5bc1d1SSean Bruno 		    "Adding channel %d (%d Mhz) to the list\n",
3356a5bc1d1SSean Bruno 		    nchan, c->ic_freq);
3366a5bc1d1SSean Bruno 		chan->channel_num = ieee80211_mhz2ieee(c->ic_freq, 0);
3376a5bc1d1SSean Bruno 		chan->iter_count = 1;
3386a5bc1d1SSean Bruno 		chan->iter_interval = htole16(0);
3399afea60fSAndriy Voskoboinyk 		chan->flags = htole32(IWM_SCAN_CHANNEL_UMAC_NSSIDS(n_ssids));
3406a5bc1d1SSean Bruno 		chan++;
3416a5bc1d1SSean Bruno 		nchan++;
3426a5bc1d1SSean Bruno 	}
3436a5bc1d1SSean Bruno 
3446a5bc1d1SSean Bruno 	return nchan;
3456a5bc1d1SSean Bruno }
3466a5bc1d1SSean Bruno 
3476a5bc1d1SSean Bruno static int
3486a5bc1d1SSean Bruno iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct iwm_scan_probe_req *preq)
3496a5bc1d1SSean Bruno {
3506a5bc1d1SSean Bruno 	struct ieee80211com *ic = &sc->sc_ic;
3516a5bc1d1SSean Bruno 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3526a5bc1d1SSean Bruno 	struct ieee80211_frame *wh = (struct ieee80211_frame *)preq->buf;
3536a5bc1d1SSean Bruno 	struct ieee80211_rateset *rs;
3546a5bc1d1SSean Bruno 	size_t remain = sizeof(preq->buf);
3556a5bc1d1SSean Bruno 	uint8_t *frm, *pos;
3566a5bc1d1SSean Bruno 
3576a5bc1d1SSean Bruno 	memset(preq, 0, sizeof(*preq));
3586a5bc1d1SSean Bruno 
3596a5bc1d1SSean Bruno 	/* Ensure enough space for header and SSID IE. */
3609afea60fSAndriy Voskoboinyk 	if (remain < sizeof(*wh) + 2)
3616a5bc1d1SSean Bruno 		return ENOBUFS;
3626a5bc1d1SSean Bruno 
3636a5bc1d1SSean Bruno 	/*
3646a5bc1d1SSean Bruno 	 * Build a probe request frame.  Most of the following code is a
3656a5bc1d1SSean Bruno 	 * copy & paste of what is done in net80211.
3666a5bc1d1SSean Bruno 	 */
3676a5bc1d1SSean Bruno 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
3686a5bc1d1SSean Bruno 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
3696a5bc1d1SSean Bruno 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
3706a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
3716a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr2, vap ? vap->iv_myaddr : ic->ic_macaddr);
3726a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
3736a5bc1d1SSean Bruno 	*(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
3746a5bc1d1SSean Bruno 	*(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
3756a5bc1d1SSean Bruno 
3766a5bc1d1SSean Bruno 	frm = (uint8_t *)(wh + 1);
3779afea60fSAndriy Voskoboinyk 	frm = ieee80211_add_ssid(frm, NULL, 0);
3786a5bc1d1SSean Bruno 
3796a5bc1d1SSean Bruno 	/* Tell the firmware where the MAC header is. */
3806a5bc1d1SSean Bruno 	preq->mac_header.offset = 0;
3816a5bc1d1SSean Bruno 	preq->mac_header.len = htole16(frm - (uint8_t *)wh);
3826a5bc1d1SSean Bruno 	remain -= frm - (uint8_t *)wh;
3836a5bc1d1SSean Bruno 
3846a5bc1d1SSean Bruno 	/* Fill in 2GHz IEs and tell firmware where they are. */
3856a5bc1d1SSean Bruno 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
3866a5bc1d1SSean Bruno 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
3876a5bc1d1SSean Bruno 		if (remain < 4 + rs->rs_nrates)
3886a5bc1d1SSean Bruno 			return ENOBUFS;
3896a5bc1d1SSean Bruno 	} else if (remain < 2 + rs->rs_nrates) {
3906a5bc1d1SSean Bruno 		return ENOBUFS;
3916a5bc1d1SSean Bruno 	}
3926a5bc1d1SSean Bruno 	preq->band_data[0].offset = htole16(frm - (uint8_t *)wh);
3936a5bc1d1SSean Bruno 	pos = frm;
3946a5bc1d1SSean Bruno 	frm = ieee80211_add_rates(frm, rs);
3956a5bc1d1SSean Bruno 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
3966a5bc1d1SSean Bruno 		frm = ieee80211_add_xrates(frm, rs);
3976a5bc1d1SSean Bruno 	preq->band_data[0].len = htole16(frm - pos);
3986a5bc1d1SSean Bruno 	remain -= frm - pos;
3996a5bc1d1SSean Bruno 
4006a5bc1d1SSean Bruno 	if (isset(sc->sc_enabled_capa,
4016a5bc1d1SSean Bruno 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT)) {
4026a5bc1d1SSean Bruno 		if (remain < 3)
4036a5bc1d1SSean Bruno 			return ENOBUFS;
4046a5bc1d1SSean Bruno 		*frm++ = IEEE80211_ELEMID_DSPARMS;
4056a5bc1d1SSean Bruno 		*frm++ = 1;
4066a5bc1d1SSean Bruno 		*frm++ = 0;
4076a5bc1d1SSean Bruno 		remain -= 3;
4086a5bc1d1SSean Bruno 	}
4096a5bc1d1SSean Bruno 
410*6349bdb3SAdrian Chadd 	if (sc->nvm_data->sku_cap_band_52GHz_enable) {
4116a5bc1d1SSean Bruno 		/* Fill in 5GHz IEs. */
4126a5bc1d1SSean Bruno 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4136a5bc1d1SSean Bruno 		if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
4146a5bc1d1SSean Bruno 			if (remain < 4 + rs->rs_nrates)
4156a5bc1d1SSean Bruno 				return ENOBUFS;
4166a5bc1d1SSean Bruno 		} else if (remain < 2 + rs->rs_nrates) {
4176a5bc1d1SSean Bruno 			return ENOBUFS;
4186a5bc1d1SSean Bruno 		}
4196a5bc1d1SSean Bruno 		preq->band_data[1].offset = htole16(frm - (uint8_t *)wh);
4206a5bc1d1SSean Bruno 		pos = frm;
4216a5bc1d1SSean Bruno 		frm = ieee80211_add_rates(frm, rs);
4226a5bc1d1SSean Bruno 		if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4236a5bc1d1SSean Bruno 			frm = ieee80211_add_xrates(frm, rs);
4246a5bc1d1SSean Bruno 		preq->band_data[1].len = htole16(frm - pos);
4256a5bc1d1SSean Bruno 		remain -= frm - pos;
4266a5bc1d1SSean Bruno 	}
4276a5bc1d1SSean Bruno 
4286a5bc1d1SSean Bruno 	/* Send 11n IEs on both 2GHz and 5GHz bands. */
4296a5bc1d1SSean Bruno 	preq->common_data.offset = htole16(frm - (uint8_t *)wh);
4306a5bc1d1SSean Bruno 	pos = frm;
4316a5bc1d1SSean Bruno #if 0
4326a5bc1d1SSean Bruno 	if (ic->ic_flags & IEEE80211_F_HTON) {
4336a5bc1d1SSean Bruno 		if (remain < 28)
4346a5bc1d1SSean Bruno 			return ENOBUFS;
4356a5bc1d1SSean Bruno 		frm = ieee80211_add_htcaps(frm, ic);
4366a5bc1d1SSean Bruno 		/* XXX add WME info? */
4376a5bc1d1SSean Bruno 	}
4386a5bc1d1SSean Bruno #endif
4396a5bc1d1SSean Bruno 	preq->common_data.len = htole16(frm - pos);
4406a5bc1d1SSean Bruno 
4416a5bc1d1SSean Bruno 	return 0;
442d4886179SRui Paulo }
443d4886179SRui Paulo 
444d4886179SRui Paulo int
4456a5bc1d1SSean Bruno iwm_mvm_config_umac_scan(struct iwm_softc *sc)
446d4886179SRui Paulo {
447bc6cd01dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
448bc6cd01dSAdrian Chadd 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
449d4886179SRui Paulo 
4506a5bc1d1SSean Bruno 	struct iwm_scan_config *scan_config;
4516a5bc1d1SSean Bruno 	int ret, j, nchan;
4526a5bc1d1SSean Bruno 	size_t cmd_size;
4536a5bc1d1SSean Bruno 	struct ieee80211_channel *c;
4546a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
4556a5bc1d1SSean Bruno 		.id = iwm_cmd_id(IWM_SCAN_CFG_CMD, IWM_ALWAYS_LONG_GROUP, 0),
4566a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
4576a5bc1d1SSean Bruno 	};
4586a5bc1d1SSean Bruno 	static const uint32_t rates = (IWM_SCAN_CONFIG_RATE_1M |
4596a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_2M | IWM_SCAN_CONFIG_RATE_5M |
4606a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_11M | IWM_SCAN_CONFIG_RATE_6M |
4616a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_9M | IWM_SCAN_CONFIG_RATE_12M |
4626a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_18M | IWM_SCAN_CONFIG_RATE_24M |
4636a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_36M | IWM_SCAN_CONFIG_RATE_48M |
4646a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_54M);
4656a5bc1d1SSean Bruno 
4666a5bc1d1SSean Bruno 	cmd_size = sizeof(*scan_config) + sc->sc_capa_n_scan_channels;
4676a5bc1d1SSean Bruno 
4686a5bc1d1SSean Bruno 	scan_config = malloc(cmd_size, M_DEVBUF, M_NOWAIT | M_ZERO);
4696a5bc1d1SSean Bruno 	if (scan_config == NULL)
4706a5bc1d1SSean Bruno 		return ENOMEM;
4716a5bc1d1SSean Bruno 
4726a5bc1d1SSean Bruno 	scan_config->tx_chains = htole32(iwm_fw_valid_tx_ant(sc));
4736a5bc1d1SSean Bruno 	scan_config->rx_chains = htole32(iwm_fw_valid_rx_ant(sc));
4746a5bc1d1SSean Bruno 	scan_config->legacy_rates = htole32(rates |
4756a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_SUPPORTED_RATE(rates));
4766a5bc1d1SSean Bruno 
4776a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
4786a5bc1d1SSean Bruno 	scan_config->dwell_active = 10;
4796a5bc1d1SSean Bruno 	scan_config->dwell_passive = 110;
4806a5bc1d1SSean Bruno 	scan_config->dwell_fragmented = 44;
4816a5bc1d1SSean Bruno 	scan_config->dwell_extended = 90;
4826a5bc1d1SSean Bruno 	scan_config->out_of_channel_time = htole32(0);
4836a5bc1d1SSean Bruno 	scan_config->suspend_time = htole32(0);
4846a5bc1d1SSean Bruno 
4856a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(scan_config->mac_addr,
4866a5bc1d1SSean Bruno 	    vap ? vap->iv_myaddr : ic->ic_macaddr);
4876a5bc1d1SSean Bruno 
4886a5bc1d1SSean Bruno 	scan_config->bcast_sta_id = sc->sc_aux_sta.sta_id;
4896a5bc1d1SSean Bruno 	scan_config->channel_flags = IWM_CHANNEL_FLAG_EBS |
4906a5bc1d1SSean Bruno 	    IWM_CHANNEL_FLAG_ACCURATE_EBS | IWM_CHANNEL_FLAG_EBS_ADD |
4916a5bc1d1SSean Bruno 	    IWM_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
4926a5bc1d1SSean Bruno 
4936a5bc1d1SSean Bruno 	for (nchan = j = 0;
4946a5bc1d1SSean Bruno 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
4956a5bc1d1SSean Bruno 		c = &ic->ic_channels[j];
4966a5bc1d1SSean Bruno 		/* For 2GHz, only populate 11b channels */
4976a5bc1d1SSean Bruno 		/* For 5GHz, only populate 11a channels */
4986a5bc1d1SSean Bruno 		/*
4996a5bc1d1SSean Bruno 		 * Catch other channels, in case we have 900MHz channels or
5006a5bc1d1SSean Bruno 		 * something in the chanlist.
5016a5bc1d1SSean Bruno 		 */
5026a5bc1d1SSean Bruno 		if (iwm_mvm_scan_skip_channel(c))
5036a5bc1d1SSean Bruno 			continue;
5046a5bc1d1SSean Bruno 		scan_config->channel_array[nchan++] =
5056a5bc1d1SSean Bruno 		    ieee80211_mhz2ieee(c->ic_freq, 0);
5066a5bc1d1SSean Bruno 	}
5076a5bc1d1SSean Bruno 
5086a5bc1d1SSean Bruno 	scan_config->flags = htole32(IWM_SCAN_CONFIG_FLAG_ACTIVATE |
5096a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
5106a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_TX_CHAINS |
5116a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_RX_CHAINS |
5126a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
5136a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_ALL_TIMES |
5146a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
5156a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_MAC_ADDR |
5166a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
5176a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_N_CHANNELS(nchan) |
5186a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
5196a5bc1d1SSean Bruno 
5206a5bc1d1SSean Bruno 	hcmd.data[0] = scan_config;
5216a5bc1d1SSean Bruno 	hcmd.len[0] = cmd_size;
5226a5bc1d1SSean Bruno 
5236a5bc1d1SSean Bruno 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending UMAC scan config\n");
5246a5bc1d1SSean Bruno 
5256a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
5266a5bc1d1SSean Bruno 	if (!ret)
5276a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
5286a5bc1d1SSean Bruno 		    "UMAC scan config was sent successfully\n");
5296a5bc1d1SSean Bruno 
5306a5bc1d1SSean Bruno 	free(scan_config, M_DEVBUF);
5316a5bc1d1SSean Bruno 	return ret;
5326a5bc1d1SSean Bruno }
5336a5bc1d1SSean Bruno 
5346a5bc1d1SSean Bruno int
5356a5bc1d1SSean Bruno iwm_mvm_umac_scan(struct iwm_softc *sc)
5366a5bc1d1SSean Bruno {
5376a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
5386a5bc1d1SSean Bruno 		.id = iwm_cmd_id(IWM_SCAN_REQ_UMAC, IWM_ALWAYS_LONG_GROUP, 0),
5396a5bc1d1SSean Bruno 		.len = { 0, },
5406a5bc1d1SSean Bruno 		.data = { NULL, },
5416a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
5426a5bc1d1SSean Bruno 	};
5439afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
5446a5bc1d1SSean Bruno 	struct iwm_scan_req_umac *req;
5456a5bc1d1SSean Bruno 	struct iwm_scan_req_umac_tail *tail;
5466a5bc1d1SSean Bruno 	size_t req_len;
5479afea60fSAndriy Voskoboinyk 	uint8_t i, nssid;
5486a5bc1d1SSean Bruno 	int ret;
5496a5bc1d1SSean Bruno 
5506a5bc1d1SSean Bruno 	req_len = sizeof(struct iwm_scan_req_umac) +
5516a5bc1d1SSean Bruno 	    (sizeof(struct iwm_scan_channel_cfg_umac) *
5526a5bc1d1SSean Bruno 	    sc->sc_capa_n_scan_channels) +
5536a5bc1d1SSean Bruno 	    sizeof(struct iwm_scan_req_umac_tail);
5546a5bc1d1SSean Bruno 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
5556a5bc1d1SSean Bruno 		return ENOMEM;
5566a5bc1d1SSean Bruno 	req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
5576a5bc1d1SSean Bruno 	if (req == NULL)
5586a5bc1d1SSean Bruno 		return ENOMEM;
5596a5bc1d1SSean Bruno 
5606a5bc1d1SSean Bruno 	hcmd.len[0] = (uint16_t)req_len;
5616a5bc1d1SSean Bruno 	hcmd.data[0] = (void *)req;
5626a5bc1d1SSean Bruno 
5636a5bc1d1SSean Bruno 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Handling ieee80211 scan request\n");
5646a5bc1d1SSean Bruno 
5656a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
5666a5bc1d1SSean Bruno 	req->active_dwell = 10;
5676a5bc1d1SSean Bruno 	req->passive_dwell = 110;
5686a5bc1d1SSean Bruno 	req->fragmented_dwell = 44;
5696a5bc1d1SSean Bruno 	req->extended_dwell = 90;
5706a5bc1d1SSean Bruno 	req->max_out_time = 0;
5716a5bc1d1SSean Bruno 	req->suspend_time = 0;
5726a5bc1d1SSean Bruno 
5736a5bc1d1SSean Bruno 	req->scan_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
5746a5bc1d1SSean Bruno 	req->ooc_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
5756a5bc1d1SSean Bruno 
5769afea60fSAndriy Voskoboinyk 	nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
5776a5bc1d1SSean Bruno 	req->n_channels = iwm_mvm_umac_scan_fill_channels(sc,
5789afea60fSAndriy Voskoboinyk 	    (struct iwm_scan_channel_cfg_umac *)req->data, nssid);
5796a5bc1d1SSean Bruno 
5806a5bc1d1SSean Bruno 	req->general_flags = htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASS_ALL |
5816a5bc1d1SSean Bruno 	    IWM_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE |
5826a5bc1d1SSean Bruno 	    IWM_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL);
5836a5bc1d1SSean Bruno 
5846a5bc1d1SSean Bruno 	tail = (void *)((char *)&req->data +
5856a5bc1d1SSean Bruno 		sizeof(struct iwm_scan_channel_cfg_umac) *
5866a5bc1d1SSean Bruno 			sc->sc_capa_n_scan_channels);
5876a5bc1d1SSean Bruno 
5886a5bc1d1SSean Bruno 	/* Check if we're doing an active directed scan. */
5899afea60fSAndriy Voskoboinyk 	for (i = 0; i < nssid; i++) {
5909afea60fSAndriy Voskoboinyk 		tail->direct_scan[i].id = IEEE80211_ELEMID_SSID;
5919afea60fSAndriy Voskoboinyk 		tail->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
5929afea60fSAndriy Voskoboinyk 		    IEEE80211_NWID_LEN);
5939afea60fSAndriy Voskoboinyk 		memcpy(tail->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
5949afea60fSAndriy Voskoboinyk 		    tail->direct_scan[i].len);
5959afea60fSAndriy Voskoboinyk 		/* XXX debug */
5969afea60fSAndriy Voskoboinyk 	}
5979afea60fSAndriy Voskoboinyk 	if (nssid != 0) {
5986a5bc1d1SSean Bruno 		req->general_flags |=
5996a5bc1d1SSean Bruno 		    htole32(IWM_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT);
6009afea60fSAndriy Voskoboinyk 	} else
6016a5bc1d1SSean Bruno 		req->general_flags |= htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASSIVE);
6026a5bc1d1SSean Bruno 
6036a5bc1d1SSean Bruno 	if (isset(sc->sc_enabled_capa,
6046a5bc1d1SSean Bruno 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
6056a5bc1d1SSean Bruno 		req->general_flags |=
6066a5bc1d1SSean Bruno 		    htole32(IWM_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED);
6076a5bc1d1SSean Bruno 
6086a5bc1d1SSean Bruno 	ret = iwm_mvm_fill_probe_req(sc, &tail->preq);
6096a5bc1d1SSean Bruno 	if (ret) {
6106a5bc1d1SSean Bruno 		free(req, M_DEVBUF);
6116a5bc1d1SSean Bruno 		return ret;
6126a5bc1d1SSean Bruno 	}
6136a5bc1d1SSean Bruno 
6146a5bc1d1SSean Bruno 	/* Specify the scan plan: We'll do one iteration. */
6156a5bc1d1SSean Bruno 	tail->schedule[0].interval = 0;
6166a5bc1d1SSean Bruno 	tail->schedule[0].iter_count = 1;
6176a5bc1d1SSean Bruno 
6186a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
6196a5bc1d1SSean Bruno 	if (!ret)
6206a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
6216a5bc1d1SSean Bruno 		    "Scan request was sent successfully\n");
6226a5bc1d1SSean Bruno 	free(req, M_DEVBUF);
6236a5bc1d1SSean Bruno 	return ret;
6246a5bc1d1SSean Bruno }
6256a5bc1d1SSean Bruno 
6266a5bc1d1SSean Bruno int
6276a5bc1d1SSean Bruno iwm_mvm_lmac_scan(struct iwm_softc *sc)
6286a5bc1d1SSean Bruno {
6296a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
6306a5bc1d1SSean Bruno 		.id = IWM_SCAN_OFFLOAD_REQUEST_CMD,
6316a5bc1d1SSean Bruno 		.len = { 0, },
6326a5bc1d1SSean Bruno 		.data = { NULL, },
6336a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
6346a5bc1d1SSean Bruno 	};
6359afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
6366a5bc1d1SSean Bruno 	struct iwm_scan_req_lmac *req;
6376a5bc1d1SSean Bruno 	size_t req_len;
6389afea60fSAndriy Voskoboinyk 	uint8_t i, nssid;
6396a5bc1d1SSean Bruno 	int ret;
640d4886179SRui Paulo 
641d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
642d4886179SRui Paulo 	    "Handling ieee80211 scan request\n");
643d4886179SRui Paulo 
6446a5bc1d1SSean Bruno 	req_len = sizeof(struct iwm_scan_req_lmac) +
6456a5bc1d1SSean Bruno 	    (sizeof(struct iwm_scan_channel_cfg_lmac) *
6466a5bc1d1SSean Bruno 	    sc->sc_capa_n_scan_channels) + sizeof(struct iwm_scan_probe_req);
6476a5bc1d1SSean Bruno 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
6486a5bc1d1SSean Bruno 		return ENOMEM;
6496a5bc1d1SSean Bruno 	req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
6506a5bc1d1SSean Bruno 	if (req == NULL)
6516a5bc1d1SSean Bruno 		return ENOMEM;
652d4886179SRui Paulo 
6536a5bc1d1SSean Bruno 	hcmd.len[0] = (uint16_t)req_len;
6546a5bc1d1SSean Bruno 	hcmd.data[0] = (void *)req;
655d4886179SRui Paulo 
6566a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
6576a5bc1d1SSean Bruno 	req->active_dwell = 10;
6586a5bc1d1SSean Bruno 	req->passive_dwell = 110;
6596a5bc1d1SSean Bruno 	req->fragmented_dwell = 44;
6606a5bc1d1SSean Bruno 	req->extended_dwell = 90;
6616a5bc1d1SSean Bruno 	req->max_out_time = 0;
6626a5bc1d1SSean Bruno 	req->suspend_time = 0;
663d4886179SRui Paulo 
6646a5bc1d1SSean Bruno 	req->scan_prio = htole32(IWM_SCAN_PRIORITY_HIGH);
6656a5bc1d1SSean Bruno 	req->rx_chain_select = iwm_mvm_scan_rx_chain(sc);
6666a5bc1d1SSean Bruno 	req->iter_num = htole32(1);
6676a5bc1d1SSean Bruno 	req->delay = 0;
6686a5bc1d1SSean Bruno 
6696a5bc1d1SSean Bruno 	req->scan_flags = htole32(IWM_MVM_LMAC_SCAN_FLAG_PASS_ALL |
6706a5bc1d1SSean Bruno 	    IWM_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE |
6716a5bc1d1SSean Bruno 	    IWM_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL);
6726a5bc1d1SSean Bruno 	if (isset(sc->sc_enabled_capa,
6736a5bc1d1SSean Bruno 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
6746a5bc1d1SSean Bruno 		req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
6756a5bc1d1SSean Bruno 
6766a5bc1d1SSean Bruno 	req->flags = htole32(IWM_PHY_BAND_24);
677*6349bdb3SAdrian Chadd 	if (sc->nvm_data->sku_cap_band_52GHz_enable)
6786a5bc1d1SSean Bruno 		req->flags |= htole32(IWM_PHY_BAND_5);
6796a5bc1d1SSean Bruno 	req->filter_flags =
6806a5bc1d1SSean Bruno 	    htole32(IWM_MAC_FILTER_ACCEPT_GRP | IWM_MAC_FILTER_IN_BEACON);
6816a5bc1d1SSean Bruno 
6826a5bc1d1SSean Bruno 	/* Tx flags 2 GHz. */
6836a5bc1d1SSean Bruno 	req->tx_cmd[0].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
684d4886179SRui Paulo 	    IWM_TX_CMD_FLG_BT_DIS);
6856a5bc1d1SSean Bruno 	req->tx_cmd[0].rate_n_flags =
6866a5bc1d1SSean Bruno 	    iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_2GHZ, 1/*XXX*/);
6876a5bc1d1SSean Bruno 	req->tx_cmd[0].sta_id = sc->sc_aux_sta.sta_id;
688d4886179SRui Paulo 
6896a5bc1d1SSean Bruno 	/* Tx flags 5 GHz. */
6906a5bc1d1SSean Bruno 	req->tx_cmd[1].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
6916a5bc1d1SSean Bruno 	    IWM_TX_CMD_FLG_BT_DIS);
6926a5bc1d1SSean Bruno 	req->tx_cmd[1].rate_n_flags =
6936a5bc1d1SSean Bruno 	    iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_5GHZ, 1/*XXX*/);
6946a5bc1d1SSean Bruno 	req->tx_cmd[1].sta_id = sc->sc_aux_sta.sta_id;
695d4886179SRui Paulo 
6966a5bc1d1SSean Bruno 	/* Check if we're doing an active directed scan. */
6979afea60fSAndriy Voskoboinyk 	nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
6989afea60fSAndriy Voskoboinyk 	for (i = 0; i < nssid; i++) {
6999afea60fSAndriy Voskoboinyk 		req->direct_scan[i].id = IEEE80211_ELEMID_SSID;
7009afea60fSAndriy Voskoboinyk 		req->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
7019afea60fSAndriy Voskoboinyk 		    IEEE80211_NWID_LEN);
7029afea60fSAndriy Voskoboinyk 		memcpy(req->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
7039afea60fSAndriy Voskoboinyk 		    req->direct_scan[i].len);
7049afea60fSAndriy Voskoboinyk 		/* XXX debug */
7056a5bc1d1SSean Bruno 	}
7069afea60fSAndriy Voskoboinyk 	if (nssid != 0) {
7079afea60fSAndriy Voskoboinyk 		req->scan_flags |=
7089afea60fSAndriy Voskoboinyk 		    htole32(IWM_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION);
7099afea60fSAndriy Voskoboinyk 	} else
7109afea60fSAndriy Voskoboinyk 		req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAG_PASSIVE);
711d4886179SRui Paulo 
7126a5bc1d1SSean Bruno 	req->n_channels = iwm_mvm_lmac_scan_fill_channels(sc,
7139afea60fSAndriy Voskoboinyk 	    (struct iwm_scan_channel_cfg_lmac *)req->data, nssid);
714d4886179SRui Paulo 
7156a5bc1d1SSean Bruno 	ret = iwm_mvm_fill_probe_req(sc,
7166a5bc1d1SSean Bruno 			    (struct iwm_scan_probe_req *)(req->data +
7176a5bc1d1SSean Bruno 			    (sizeof(struct iwm_scan_channel_cfg_lmac) *
7186a5bc1d1SSean Bruno 			    sc->sc_capa_n_scan_channels)));
7196a5bc1d1SSean Bruno 	if (ret) {
7206a5bc1d1SSean Bruno 		free(req, M_DEVBUF);
7216a5bc1d1SSean Bruno 		return ret;
7226a5bc1d1SSean Bruno 	}
7236a5bc1d1SSean Bruno 
7246a5bc1d1SSean Bruno 	/* Specify the scan plan: We'll do one iteration. */
7256a5bc1d1SSean Bruno 	req->schedule[0].iterations = 1;
7266a5bc1d1SSean Bruno 	req->schedule[0].full_scan_mul = 1;
7276a5bc1d1SSean Bruno 
7286a5bc1d1SSean Bruno 	/* Disable EBS. */
7296a5bc1d1SSean Bruno 	req->channel_opt[0].non_ebs_ratio = 1;
7306a5bc1d1SSean Bruno 	req->channel_opt[1].non_ebs_ratio = 1;
7316a5bc1d1SSean Bruno 
7326a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
7336a5bc1d1SSean Bruno 	if (!ret) {
734d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
735d4886179SRui Paulo 		    "Scan request was sent successfully\n");
736d4886179SRui Paulo 	}
7376a5bc1d1SSean Bruno 	free(req, M_DEVBUF);
738d4886179SRui Paulo 	return ret;
739d4886179SRui Paulo }
740