xref: /freebsd/sys/dev/iwm/if_iwm_scan.c (revision 49fdbf0a)
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 
108d4886179SRui Paulo #include <sys/param.h>
109d4886179SRui Paulo #include <sys/bus.h>
110d4886179SRui Paulo #include <sys/conf.h>
111d4886179SRui Paulo #include <sys/endian.h>
112d4886179SRui Paulo #include <sys/firmware.h>
113d4886179SRui Paulo #include <sys/kernel.h>
114d4886179SRui Paulo #include <sys/malloc.h>
115d4886179SRui Paulo #include <sys/mbuf.h>
116d4886179SRui Paulo #include <sys/mutex.h>
117d4886179SRui Paulo #include <sys/module.h>
118d4886179SRui Paulo #include <sys/proc.h>
119d4886179SRui Paulo #include <sys/rman.h>
120d4886179SRui Paulo #include <sys/socket.h>
121d4886179SRui Paulo #include <sys/sockio.h>
122d4886179SRui Paulo #include <sys/sysctl.h>
123d4886179SRui Paulo #include <sys/linker.h>
124d4886179SRui Paulo 
125d4886179SRui Paulo #include <machine/bus.h>
126d4886179SRui Paulo #include <machine/endian.h>
127d4886179SRui Paulo #include <machine/resource.h>
128d4886179SRui Paulo 
129d4886179SRui Paulo #include <dev/pci/pcivar.h>
130d4886179SRui Paulo #include <dev/pci/pcireg.h>
131d4886179SRui Paulo 
132d4886179SRui Paulo #include <net/bpf.h>
133d4886179SRui Paulo 
134d4886179SRui Paulo #include <net/if.h>
135d4886179SRui Paulo #include <net/if_var.h>
136d4886179SRui Paulo #include <net/if_arp.h>
137d4886179SRui Paulo #include <net/if_dl.h>
138d4886179SRui Paulo #include <net/if_media.h>
139d4886179SRui Paulo #include <net/if_types.h>
140d4886179SRui Paulo 
141d4886179SRui Paulo #include <netinet/in.h>
142d4886179SRui Paulo #include <netinet/in_systm.h>
143d4886179SRui Paulo #include <netinet/if_ether.h>
144d4886179SRui Paulo #include <netinet/ip.h>
145d4886179SRui Paulo 
146d4886179SRui Paulo #include <net80211/ieee80211_var.h>
147d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
148d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
149d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
150d4886179SRui Paulo 
15149fdbf0aSRui Paulo #include <dev/iwm/if_iwmreg.h>
15249fdbf0aSRui Paulo #include <dev/iwm/if_iwmvar.h>
15349fdbf0aSRui Paulo #include <dev/iwm/if_iwm_debug.h>
15449fdbf0aSRui Paulo #include <dev/iwm/if_iwm_util.h>
15549fdbf0aSRui Paulo #include <dev/iwm/if_iwm_scan.h>
156d4886179SRui Paulo 
157d4886179SRui Paulo /*
158d4886179SRui Paulo  * BEGIN mvm/scan.c
159d4886179SRui Paulo  */
160d4886179SRui Paulo 
161d4886179SRui Paulo #define IWM_PLCP_QUIET_THRESH 1
162d4886179SRui Paulo #define IWM_ACTIVE_QUIET_TIME 10
163d4886179SRui Paulo #define LONG_OUT_TIME_PERIOD (600 * IEEE80211_DUR_TU)
164d4886179SRui Paulo #define SHORT_OUT_TIME_PERIOD (200 * IEEE80211_DUR_TU)
165d4886179SRui Paulo #define SUSPEND_TIME_PERIOD (100 * IEEE80211_DUR_TU)
166d4886179SRui Paulo 
167d4886179SRui Paulo static uint16_t
168d4886179SRui Paulo iwm_mvm_scan_rx_chain(struct iwm_softc *sc)
169d4886179SRui Paulo {
170d4886179SRui Paulo 	uint16_t rx_chain;
171d4886179SRui Paulo 	uint8_t rx_ant;
172d4886179SRui Paulo 
173d4886179SRui Paulo 	rx_ant = IWM_FW_VALID_RX_ANT(sc);
174d4886179SRui Paulo 	rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
175d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
176d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
177d4886179SRui Paulo 	rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
178d4886179SRui Paulo 	return htole16(rx_chain);
179d4886179SRui Paulo }
180d4886179SRui Paulo 
181d4886179SRui Paulo static uint32_t
182d4886179SRui Paulo iwm_mvm_scan_max_out_time(struct iwm_softc *sc, uint32_t flags, int is_assoc)
183d4886179SRui Paulo {
184d4886179SRui Paulo 	if (!is_assoc)
185d4886179SRui Paulo 		return 0;
186d4886179SRui Paulo 	if (flags & 0x1)
187d4886179SRui Paulo 		return htole32(SHORT_OUT_TIME_PERIOD);
188d4886179SRui Paulo 	return htole32(LONG_OUT_TIME_PERIOD);
189d4886179SRui Paulo }
190d4886179SRui Paulo 
191d4886179SRui Paulo static uint32_t
192d4886179SRui Paulo iwm_mvm_scan_suspend_time(struct iwm_softc *sc, int is_assoc)
193d4886179SRui Paulo {
194d4886179SRui Paulo 	if (!is_assoc)
195d4886179SRui Paulo 		return 0;
196d4886179SRui Paulo 	return htole32(SUSPEND_TIME_PERIOD);
197d4886179SRui Paulo }
198d4886179SRui Paulo 
199d4886179SRui Paulo static uint32_t
200d4886179SRui Paulo iwm_mvm_scan_rxon_flags(struct iwm_softc *sc, int flags)
201d4886179SRui Paulo {
202d4886179SRui Paulo 	if (flags & IEEE80211_CHAN_2GHZ)
203d4886179SRui Paulo 		return htole32(IWM_PHY_BAND_24);
204d4886179SRui Paulo 	else
205d4886179SRui Paulo 		return htole32(IWM_PHY_BAND_5);
206d4886179SRui Paulo }
207d4886179SRui Paulo 
208d4886179SRui Paulo static uint32_t
209d4886179SRui Paulo iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
210d4886179SRui Paulo {
211d4886179SRui Paulo 	uint32_t tx_ant;
212d4886179SRui Paulo 	int i, ind;
213d4886179SRui Paulo 
214d4886179SRui Paulo 	for (i = 0, ind = sc->sc_scan_last_antenna;
215d4886179SRui Paulo 	    i < IWM_RATE_MCS_ANT_NUM; i++) {
216d4886179SRui Paulo 		ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
217d4886179SRui Paulo 		if (IWM_FW_VALID_TX_ANT(sc) & (1 << ind)) {
218d4886179SRui Paulo 			sc->sc_scan_last_antenna = ind;
219d4886179SRui Paulo 			break;
220d4886179SRui Paulo 		}
221d4886179SRui Paulo 	}
222d4886179SRui Paulo 	tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
223d4886179SRui Paulo 
224d4886179SRui Paulo 	if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
225d4886179SRui Paulo 		return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
226d4886179SRui Paulo 				   tx_ant);
227d4886179SRui Paulo 	else
228d4886179SRui Paulo 		return htole32(IWM_RATE_6M_PLCP | tx_ant);
229d4886179SRui Paulo }
230d4886179SRui Paulo 
231d4886179SRui Paulo /*
232d4886179SRui Paulo  * If req->n_ssids > 0, it means we should do an active scan.
233d4886179SRui Paulo  * In case of active scan w/o directed scan, we receive a zero-length SSID
234d4886179SRui Paulo  * just to notify that this scan is active and not passive.
235d4886179SRui Paulo  * In order to notify the FW of the number of SSIDs we wish to scan (including
236d4886179SRui Paulo  * the zero-length one), we need to set the corresponding bits in chan->type,
237d4886179SRui Paulo  * one for each SSID, and set the active bit (first). If the first SSID is
238d4886179SRui Paulo  * already included in the probe template, so we need to set only
239d4886179SRui Paulo  * req->n_ssids - 1 bits in addition to the first bit.
240d4886179SRui Paulo  */
241d4886179SRui Paulo static uint16_t
242d4886179SRui Paulo iwm_mvm_get_active_dwell(struct iwm_softc *sc, int flags, int n_ssids)
243d4886179SRui Paulo {
244d4886179SRui Paulo 	if (flags & IEEE80211_CHAN_2GHZ)
245d4886179SRui Paulo 		return 30  + 3 * (n_ssids + 1);
246d4886179SRui Paulo 	return 20  + 2 * (n_ssids + 1);
247d4886179SRui Paulo }
248d4886179SRui Paulo 
249d4886179SRui Paulo static uint16_t
250d4886179SRui Paulo iwm_mvm_get_passive_dwell(struct iwm_softc *sc, int flags)
251d4886179SRui Paulo {
252d4886179SRui Paulo 	return (flags & IEEE80211_CHAN_2GHZ) ? 100 + 20 : 100 + 10;
253d4886179SRui Paulo }
254d4886179SRui Paulo 
255d4886179SRui Paulo static int
256d4886179SRui Paulo iwm_mvm_scan_fill_channels(struct iwm_softc *sc, struct iwm_scan_cmd *cmd,
257d4886179SRui Paulo 	int flags, int n_ssids, int basic_ssid)
258d4886179SRui Paulo {
259d4886179SRui Paulo 	struct ieee80211com *ic = sc->sc_ic;
260d4886179SRui Paulo 	uint16_t passive_dwell = iwm_mvm_get_passive_dwell(sc, flags);
261d4886179SRui Paulo 	uint16_t active_dwell = iwm_mvm_get_active_dwell(sc, flags, n_ssids);
262d4886179SRui Paulo 	struct iwm_scan_channel *chan = (struct iwm_scan_channel *)
263d4886179SRui Paulo 		(cmd->data + le16toh(cmd->tx_cmd.len));
264d4886179SRui Paulo 	int type = (1 << n_ssids) - 1;
265d4886179SRui Paulo 	struct ieee80211_channel *c;
266d4886179SRui Paulo 	int nchan, j;
267d4886179SRui Paulo 
268d4886179SRui Paulo 	if (!basic_ssid)
269d4886179SRui Paulo 		type |= (1 << n_ssids);
270d4886179SRui Paulo 
271d4886179SRui Paulo 	for (nchan = j = 0; j < ic->ic_nchans; j++) {
272d4886179SRui Paulo 		c = &ic->ic_channels[j];
273d4886179SRui Paulo 		/* For 2GHz, only populate 11b channels */
274d4886179SRui Paulo 		/* For 5GHz, only populate 11a channels */
275d4886179SRui Paulo 		/*
276d4886179SRui Paulo 		 * Catch other channels, in case we have 900MHz channels or
277d4886179SRui Paulo 		 * something in the chanlist.
278d4886179SRui Paulo 		 */
279d4886179SRui Paulo 		if ((flags & IEEE80211_CHAN_2GHZ) && (! IEEE80211_IS_CHAN_B(c))) {
280d4886179SRui Paulo 			continue;
281d4886179SRui Paulo 		} else if ((flags & IEEE80211_CHAN_5GHZ) && (! IEEE80211_IS_CHAN_A(c))) {
282d4886179SRui Paulo 			continue;
283d4886179SRui Paulo 		} else {
284d4886179SRui Paulo 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
285d4886179SRui Paulo 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
286d4886179SRui Paulo 			    __func__,
287d4886179SRui Paulo 			    c->ic_freq,
288d4886179SRui Paulo 			    c->ic_ieee,
289d4886179SRui Paulo 			    c->ic_flags);
290d4886179SRui Paulo 		}
291d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
292d4886179SRui Paulo 		    "Adding channel %d (%d Mhz) to the list\n",
293d4886179SRui Paulo 			nchan, c->ic_freq);
294d4886179SRui Paulo 		chan->channel = htole16(ieee80211_mhz2ieee(c->ic_freq, flags));
295d4886179SRui Paulo 		chan->type = htole32(type);
296d4886179SRui Paulo 		if (c->ic_flags & IEEE80211_CHAN_PASSIVE)
297d4886179SRui Paulo 			chan->type &= htole32(~IWM_SCAN_CHANNEL_TYPE_ACTIVE);
298d4886179SRui Paulo 		chan->active_dwell = htole16(active_dwell);
299d4886179SRui Paulo 		chan->passive_dwell = htole16(passive_dwell);
300d4886179SRui Paulo 		chan->iteration_count = htole16(1);
301d4886179SRui Paulo 		chan++;
302d4886179SRui Paulo 		nchan++;
303d4886179SRui Paulo 	}
304d4886179SRui Paulo 	if (nchan == 0)
305d4886179SRui Paulo 		device_printf(sc->sc_dev,
306d4886179SRui Paulo 		    "%s: NO CHANNEL!\n", __func__);
307d4886179SRui Paulo 	return nchan;
308d4886179SRui Paulo }
309d4886179SRui Paulo 
310d4886179SRui Paulo /*
311d4886179SRui Paulo  * Fill in probe request with the following parameters:
312d4886179SRui Paulo  * TA is our vif HW address, which mac80211 ensures we have.
313d4886179SRui Paulo  * Packet is broadcasted, so this is both SA and DA.
314d4886179SRui Paulo  * The probe request IE is made out of two: first comes the most prioritized
315d4886179SRui Paulo  * SSID if a directed scan is requested. Second comes whatever extra
316d4886179SRui Paulo  * information was given to us as the scan request IE.
317d4886179SRui Paulo  */
318d4886179SRui Paulo static uint16_t
319d4886179SRui Paulo iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct ieee80211_frame *frame,
320d4886179SRui Paulo 	const uint8_t *ta, int n_ssids, const uint8_t *ssid, int ssid_len,
321d4886179SRui Paulo 	const uint8_t *ie, int ie_len, int left)
322d4886179SRui Paulo {
323d4886179SRui Paulo 	uint8_t *pos = NULL;
324d4886179SRui Paulo 
325d4886179SRui Paulo 	/* Make sure there is enough space for the probe request,
326d4886179SRui Paulo 	 * two mandatory IEs and the data */
327d4886179SRui Paulo 	left -= sizeof(*frame);
328d4886179SRui Paulo 	if (left < 0)
329d4886179SRui Paulo 		return 0;
330d4886179SRui Paulo 
331d4886179SRui Paulo 	frame->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
332d4886179SRui Paulo 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
333d4886179SRui Paulo 	frame->i_fc[1] = IEEE80211_FC1_DIR_NODS;
334d4886179SRui Paulo 	IEEE80211_ADDR_COPY(frame->i_addr1, ieee80211broadcastaddr);
335d4886179SRui Paulo 	IEEE80211_ADDR_COPY(frame->i_addr2, ta);
336d4886179SRui Paulo 	IEEE80211_ADDR_COPY(frame->i_addr3, ieee80211broadcastaddr);
337d4886179SRui Paulo 
338d4886179SRui Paulo 	/* for passive scans, no need to fill anything */
339d4886179SRui Paulo 	if (n_ssids == 0)
340d4886179SRui Paulo 		return sizeof(*frame);
341d4886179SRui Paulo 
342d4886179SRui Paulo 	/* points to the payload of the request */
343d4886179SRui Paulo 	pos = (uint8_t *)frame + sizeof(*frame);
344d4886179SRui Paulo 
345d4886179SRui Paulo 	/* fill in our SSID IE */
346d4886179SRui Paulo 	left -= ssid_len + 2;
347d4886179SRui Paulo 	if (left < 0)
348d4886179SRui Paulo 		return 0;
349d4886179SRui Paulo 
350d4886179SRui Paulo 	pos = ieee80211_add_ssid(pos, ssid, ssid_len);
351d4886179SRui Paulo 
352d4886179SRui Paulo 	if (ie && ie_len && left >= ie_len) {
353d4886179SRui Paulo 		memcpy(pos, ie, ie_len);
354d4886179SRui Paulo 		pos += ie_len;
355d4886179SRui Paulo 	}
356d4886179SRui Paulo 
357d4886179SRui Paulo 	return pos - (uint8_t *)frame;
358d4886179SRui Paulo }
359d4886179SRui Paulo 
360d4886179SRui Paulo int
361d4886179SRui Paulo iwm_mvm_scan_request(struct iwm_softc *sc, int flags,
362d4886179SRui Paulo 	int n_ssids, uint8_t *ssid, int ssid_len)
363d4886179SRui Paulo {
364d4886179SRui Paulo 	struct iwm_host_cmd hcmd = {
365d4886179SRui Paulo 		.id = IWM_SCAN_REQUEST_CMD,
366d4886179SRui Paulo 		.len = { 0, },
367d4886179SRui Paulo 		.data = { sc->sc_scan_cmd, },
368d4886179SRui Paulo 		.flags = IWM_CMD_SYNC,
369d4886179SRui Paulo 		.dataflags = { IWM_HCMD_DFL_NOCOPY, },
370d4886179SRui Paulo 	};
371d4886179SRui Paulo 	struct iwm_scan_cmd *cmd = sc->sc_scan_cmd;
372d4886179SRui Paulo 	int is_assoc = 0;
373d4886179SRui Paulo 	int ret;
374d4886179SRui Paulo 	uint32_t status;
375d4886179SRui Paulo 	int basic_ssid =
376d4886179SRui Paulo 	    !(sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_NO_BASIC_SSID);
377d4886179SRui Paulo 
378d4886179SRui Paulo 	sc->sc_scanband = flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ);
379d4886179SRui Paulo 
380d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
381d4886179SRui Paulo 	    "Handling ieee80211 scan request\n");
382d4886179SRui Paulo 	memset(cmd, 0, sc->sc_scan_cmd_len);
383d4886179SRui Paulo 
384d4886179SRui Paulo 	cmd->quiet_time = htole16(IWM_ACTIVE_QUIET_TIME);
385d4886179SRui Paulo 	cmd->quiet_plcp_th = htole16(IWM_PLCP_QUIET_THRESH);
386d4886179SRui Paulo 	cmd->rxchain_sel_flags = iwm_mvm_scan_rx_chain(sc);
387d4886179SRui Paulo 	cmd->max_out_time = iwm_mvm_scan_max_out_time(sc, 0, is_assoc);
388d4886179SRui Paulo 	cmd->suspend_time = iwm_mvm_scan_suspend_time(sc, is_assoc);
389d4886179SRui Paulo 	cmd->rxon_flags = iwm_mvm_scan_rxon_flags(sc, flags);
390d4886179SRui Paulo 	cmd->filter_flags = htole32(IWM_MAC_FILTER_ACCEPT_GRP |
391d4886179SRui Paulo 	    IWM_MAC_FILTER_IN_BEACON);
392d4886179SRui Paulo 
393d4886179SRui Paulo 	cmd->type = htole32(IWM_SCAN_TYPE_FORCED);
394d4886179SRui Paulo 	cmd->repeats = htole32(1);
395d4886179SRui Paulo 
396d4886179SRui Paulo 	/*
397d4886179SRui Paulo 	 * If the user asked for passive scan, don't change to active scan if
398d4886179SRui Paulo 	 * you see any activity on the channel - remain passive.
399d4886179SRui Paulo 	 */
400d4886179SRui Paulo 	if (n_ssids > 0) {
401d4886179SRui Paulo 		cmd->passive2active = htole16(1);
402d4886179SRui Paulo 		cmd->scan_flags |= IWM_SCAN_FLAGS_PASSIVE2ACTIVE;
403d4886179SRui Paulo #if 0
404d4886179SRui Paulo 		if (basic_ssid) {
405d4886179SRui Paulo 			ssid = req->ssids[0].ssid;
406d4886179SRui Paulo 			ssid_len = req->ssids[0].ssid_len;
407d4886179SRui Paulo 		}
408d4886179SRui Paulo #endif
409d4886179SRui Paulo 	} else {
410d4886179SRui Paulo 		cmd->passive2active = 0;
411d4886179SRui Paulo 		cmd->scan_flags &= ~IWM_SCAN_FLAGS_PASSIVE2ACTIVE;
412d4886179SRui Paulo 	}
413d4886179SRui Paulo 
414d4886179SRui Paulo 	cmd->tx_cmd.tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
415d4886179SRui Paulo 	    IWM_TX_CMD_FLG_BT_DIS);
416d4886179SRui Paulo 	cmd->tx_cmd.sta_id = sc->sc_aux_sta.sta_id;
417d4886179SRui Paulo 	cmd->tx_cmd.life_time = htole32(IWM_TX_CMD_LIFE_TIME_INFINITE);
418d4886179SRui Paulo 	cmd->tx_cmd.rate_n_flags = iwm_mvm_scan_rate_n_flags(sc, flags, 1/*XXX*/);
419d4886179SRui Paulo 
420d4886179SRui Paulo 	cmd->tx_cmd.len = htole16(iwm_mvm_fill_probe_req(sc,
421d4886179SRui Paulo 			    (struct ieee80211_frame *)cmd->data,
422d4886179SRui Paulo 			    sc->sc_bssid, n_ssids, ssid, ssid_len,
423d4886179SRui Paulo 			    NULL, 0, sc->sc_capa_max_probe_len));
424d4886179SRui Paulo 
425d4886179SRui Paulo 	cmd->channel_count
426d4886179SRui Paulo 	    = iwm_mvm_scan_fill_channels(sc, cmd, flags, n_ssids, basic_ssid);
427d4886179SRui Paulo 
428d4886179SRui Paulo 	cmd->len = htole16(sizeof(struct iwm_scan_cmd) +
429d4886179SRui Paulo 		le16toh(cmd->tx_cmd.len) +
430d4886179SRui Paulo 		(cmd->channel_count * sizeof(struct iwm_scan_channel)));
431d4886179SRui Paulo 	hcmd.len[0] = le16toh(cmd->len);
432d4886179SRui Paulo 
433d4886179SRui Paulo 	status = IWM_SCAN_RESPONSE_OK;
434d4886179SRui Paulo 	ret = iwm_mvm_send_cmd_status(sc, &hcmd, &status);
435d4886179SRui Paulo 	if (!ret && status == IWM_SCAN_RESPONSE_OK) {
436d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
437d4886179SRui Paulo 		    "Scan request was sent successfully\n");
438d4886179SRui Paulo 	} else {
439d4886179SRui Paulo 		/*
440d4886179SRui Paulo 		 * If the scan failed, it usually means that the FW was unable
441d4886179SRui Paulo 		 * to allocate the time events. Warn on it, but maybe we
442d4886179SRui Paulo 		 * should try to send the command again with different params.
443d4886179SRui Paulo 		 */
444d4886179SRui Paulo 		sc->sc_scanband = 0;
445d4886179SRui Paulo 		ret = EIO;
446d4886179SRui Paulo 	}
447d4886179SRui Paulo 	return ret;
448d4886179SRui Paulo }
449