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