xref: /freebsd/sys/dev/iwm/if_iwm_scan.c (revision 243e928310d073338c5ec089f0dce238a80b9866)
1 /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 genua mbh <info@genua.de>
5  * Copyright (c) 2014 Fixup Software Ltd.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*-
21  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22  * which were used as the reference documentation for this implementation.
23  *
24  * Driver version we are currently based off of is
25  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26  *
27  ***********************************************************************
28  *
29  * This file is provided under a dual BSD/GPLv2 license.  When using or
30  * redistributing this file, you may do so under either license.
31  *
32  * GPL LICENSE SUMMARY
33  *
34  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35  *
36  * This program is free software; you can redistribute it and/or modify
37  * it under the terms of version 2 of the GNU General Public License as
38  * published by the Free Software Foundation.
39  *
40  * This program is distributed in the hope that it will be useful, but
41  * WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43  * General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48  * USA
49  *
50  * The full GNU General Public License is included in this distribution
51  * in the file called COPYING.
52  *
53  * Contact Information:
54  *  Intel Linux Wireless <ilw@linux.intel.com>
55  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56  *
57  *
58  * BSD LICENSE
59  *
60  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61  * All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  *  * Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *  * Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in
71  *    the documentation and/or other materials provided with the
72  *    distribution.
73  *  * Neither the name Intel Corporation nor the names of its
74  *    contributors may be used to endorse or promote products derived
75  *    from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  */
89 
90 /*-
91  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92  *
93  * Permission to use, copy, modify, and distribute this software for any
94  * purpose with or without fee is hereby granted, provided that the above
95  * copyright notice and this permission notice appear in all copies.
96  *
97  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104  */
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
107 
108 #include "opt_wlan.h"
109 
110 #include <sys/param.h>
111 #include <sys/bus.h>
112 #include <sys/conf.h>
113 #include <sys/endian.h>
114 #include <sys/firmware.h>
115 #include <sys/kernel.h>
116 #include <sys/malloc.h>
117 #include <sys/mbuf.h>
118 #include <sys/mutex.h>
119 #include <sys/module.h>
120 #include <sys/proc.h>
121 #include <sys/rman.h>
122 #include <sys/socket.h>
123 #include <sys/sockio.h>
124 #include <sys/sysctl.h>
125 #include <sys/linker.h>
126 
127 #include <machine/bus.h>
128 #include <machine/endian.h>
129 #include <machine/resource.h>
130 
131 #include <dev/pci/pcivar.h>
132 #include <dev/pci/pcireg.h>
133 
134 #include <net/bpf.h>
135 
136 #include <net/if.h>
137 #include <net/if_var.h>
138 #include <net/if_arp.h>
139 #include <net/if_dl.h>
140 #include <net/if_media.h>
141 #include <net/if_types.h>
142 
143 #include <netinet/in.h>
144 #include <netinet/in_systm.h>
145 #include <netinet/if_ether.h>
146 #include <netinet/ip.h>
147 
148 #include <net80211/ieee80211_var.h>
149 #include <net80211/ieee80211_regdomain.h>
150 #include <net80211/ieee80211_ratectl.h>
151 #include <net80211/ieee80211_radiotap.h>
152 
153 #include <dev/iwm/if_iwmreg.h>
154 #include <dev/iwm/if_iwmvar.h>
155 #include <dev/iwm/if_iwm_debug.h>
156 #include <dev/iwm/if_iwm_util.h>
157 #include <dev/iwm/if_iwm_scan.h>
158 
159 /*
160  * BEGIN mvm/scan.c
161  */
162 
163 #define IWM_PLCP_QUIET_THRESH 1
164 #define IWM_ACTIVE_QUIET_TIME 10
165 #define LONG_OUT_TIME_PERIOD (600 * IEEE80211_DUR_TU)
166 #define SHORT_OUT_TIME_PERIOD (200 * IEEE80211_DUR_TU)
167 #define SUSPEND_TIME_PERIOD (100 * IEEE80211_DUR_TU)
168 
169 static uint16_t
170 iwm_mvm_scan_rx_chain(struct iwm_softc *sc)
171 {
172 	uint16_t rx_chain;
173 	uint8_t rx_ant;
174 
175 	rx_ant = IWM_FW_VALID_RX_ANT(sc);
176 	rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
177 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
178 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
179 	rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
180 	return htole16(rx_chain);
181 }
182 
183 static uint32_t
184 iwm_mvm_scan_max_out_time(struct iwm_softc *sc, uint32_t flags, int is_assoc)
185 {
186 	if (!is_assoc)
187 		return 0;
188 	if (flags & 0x1)
189 		return htole32(SHORT_OUT_TIME_PERIOD);
190 	return htole32(LONG_OUT_TIME_PERIOD);
191 }
192 
193 static uint32_t
194 iwm_mvm_scan_suspend_time(struct iwm_softc *sc, int is_assoc)
195 {
196 	if (!is_assoc)
197 		return 0;
198 	return htole32(SUSPEND_TIME_PERIOD);
199 }
200 
201 static uint32_t
202 iwm_mvm_scan_rxon_flags(struct iwm_softc *sc, int flags)
203 {
204 	if (flags & IEEE80211_CHAN_2GHZ)
205 		return htole32(IWM_PHY_BAND_24);
206 	else
207 		return htole32(IWM_PHY_BAND_5);
208 }
209 
210 static uint32_t
211 iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
212 {
213 	uint32_t tx_ant;
214 	int i, ind;
215 
216 	for (i = 0, ind = sc->sc_scan_last_antenna;
217 	    i < IWM_RATE_MCS_ANT_NUM; i++) {
218 		ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
219 		if (IWM_FW_VALID_TX_ANT(sc) & (1 << ind)) {
220 			sc->sc_scan_last_antenna = ind;
221 			break;
222 		}
223 	}
224 	tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
225 
226 	if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
227 		return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
228 				   tx_ant);
229 	else
230 		return htole32(IWM_RATE_6M_PLCP | tx_ant);
231 }
232 
233 /*
234  * If req->n_ssids > 0, it means we should do an active scan.
235  * In case of active scan w/o directed scan, we receive a zero-length SSID
236  * just to notify that this scan is active and not passive.
237  * In order to notify the FW of the number of SSIDs we wish to scan (including
238  * the zero-length one), we need to set the corresponding bits in chan->type,
239  * one for each SSID, and set the active bit (first). If the first SSID is
240  * already included in the probe template, so we need to set only
241  * req->n_ssids - 1 bits in addition to the first bit.
242  */
243 static uint16_t
244 iwm_mvm_get_active_dwell(struct iwm_softc *sc, int flags, int n_ssids)
245 {
246 	if (flags & IEEE80211_CHAN_2GHZ)
247 		return 30  + 3 * (n_ssids + 1);
248 	return 20  + 2 * (n_ssids + 1);
249 }
250 
251 static uint16_t
252 iwm_mvm_get_passive_dwell(struct iwm_softc *sc, int flags)
253 {
254 	return (flags & IEEE80211_CHAN_2GHZ) ? 100 + 20 : 100 + 10;
255 }
256 
257 static int
258 iwm_mvm_scan_fill_channels(struct iwm_softc *sc, struct iwm_scan_cmd *cmd,
259 	int flags, int n_ssids, int basic_ssid)
260 {
261 	struct ieee80211com *ic = &sc->sc_ic;
262 	uint16_t passive_dwell = iwm_mvm_get_passive_dwell(sc, flags);
263 	uint16_t active_dwell = iwm_mvm_get_active_dwell(sc, flags, n_ssids);
264 	struct iwm_scan_channel *chan = (struct iwm_scan_channel *)
265 		(cmd->data + le16toh(cmd->tx_cmd.len));
266 	int type = (1 << n_ssids) - 1;
267 	struct ieee80211_channel *c;
268 	int nchan, j;
269 
270 	if (!basic_ssid)
271 		type |= (1 << n_ssids);
272 
273 	for (nchan = j = 0; j < ic->ic_nchans; j++) {
274 		c = &ic->ic_channels[j];
275 		/* For 2GHz, only populate 11b channels */
276 		/* For 5GHz, only populate 11a channels */
277 		/*
278 		 * Catch other channels, in case we have 900MHz channels or
279 		 * something in the chanlist.
280 		 */
281 		if ((flags & IEEE80211_CHAN_2GHZ) && (! IEEE80211_IS_CHAN_B(c))) {
282 			continue;
283 		} else if ((flags & IEEE80211_CHAN_5GHZ) && (! IEEE80211_IS_CHAN_A(c))) {
284 			continue;
285 		} else {
286 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
287 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
288 			    __func__,
289 			    c->ic_freq,
290 			    c->ic_ieee,
291 			    c->ic_flags);
292 		}
293 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
294 		    "Adding channel %d (%d Mhz) to the list\n",
295 			nchan, c->ic_freq);
296 		chan->channel = htole16(ieee80211_mhz2ieee(c->ic_freq, flags));
297 		chan->type = htole32(type);
298 		if (c->ic_flags & IEEE80211_CHAN_PASSIVE)
299 			chan->type &= htole32(~IWM_SCAN_CHANNEL_TYPE_ACTIVE);
300 		chan->active_dwell = htole16(active_dwell);
301 		chan->passive_dwell = htole16(passive_dwell);
302 		chan->iteration_count = htole16(1);
303 		chan++;
304 		nchan++;
305 	}
306 	if (nchan == 0)
307 		device_printf(sc->sc_dev,
308 		    "%s: NO CHANNEL!\n", __func__);
309 	return nchan;
310 }
311 
312 /*
313  * Fill in probe request with the following parameters:
314  * TA is our vif HW address, which mac80211 ensures we have.
315  * Packet is broadcasted, so this is both SA and DA.
316  * The probe request IE is made out of two: first comes the most prioritized
317  * SSID if a directed scan is requested. Second comes whatever extra
318  * information was given to us as the scan request IE.
319  */
320 static uint16_t
321 iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct ieee80211_frame *frame,
322 	const uint8_t *ta, int n_ssids, const uint8_t *ssid, int ssid_len,
323 	const uint8_t *ie, int ie_len, int left)
324 {
325 	uint8_t *pos = NULL;
326 
327 	/* Make sure there is enough space for the probe request,
328 	 * two mandatory IEs and the data */
329 	left -= sizeof(*frame);
330 	if (left < 0)
331 		return 0;
332 
333 	frame->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
334 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
335 	frame->i_fc[1] = IEEE80211_FC1_DIR_NODS;
336 	IEEE80211_ADDR_COPY(frame->i_addr1, ieee80211broadcastaddr);
337 	IEEE80211_ADDR_COPY(frame->i_addr2, ta);
338 	IEEE80211_ADDR_COPY(frame->i_addr3, ieee80211broadcastaddr);
339 
340 	/* for passive scans, no need to fill anything */
341 	if (n_ssids == 0)
342 		return sizeof(*frame);
343 
344 	/* points to the payload of the request */
345 	pos = (uint8_t *)frame + sizeof(*frame);
346 
347 	/* fill in our SSID IE */
348 	left -= ssid_len + 2;
349 	if (left < 0)
350 		return 0;
351 
352 	pos = ieee80211_add_ssid(pos, ssid, ssid_len);
353 
354 	if (ie && ie_len && left >= ie_len) {
355 		memcpy(pos, ie, ie_len);
356 		pos += ie_len;
357 	}
358 
359 	return pos - (uint8_t *)frame;
360 }
361 
362 int
363 iwm_mvm_scan_request(struct iwm_softc *sc, int flags,
364 	int n_ssids, uint8_t *ssid, int ssid_len)
365 {
366 	struct iwm_host_cmd hcmd = {
367 		.id = IWM_SCAN_REQUEST_CMD,
368 		.len = { 0, },
369 		.data = { sc->sc_scan_cmd, },
370 		.flags = IWM_CMD_SYNC,
371 		.dataflags = { IWM_HCMD_DFL_NOCOPY, },
372 	};
373 	struct iwm_scan_cmd *cmd = sc->sc_scan_cmd;
374 	int is_assoc = 0;
375 	int ret;
376 	uint32_t status;
377 	int basic_ssid =
378 	    !(sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_NO_BASIC_SSID);
379 
380 	sc->sc_scanband = flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ);
381 
382 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
383 	    "Handling ieee80211 scan request\n");
384 	memset(cmd, 0, sc->sc_scan_cmd_len);
385 
386 	cmd->quiet_time = htole16(IWM_ACTIVE_QUIET_TIME);
387 	cmd->quiet_plcp_th = htole16(IWM_PLCP_QUIET_THRESH);
388 	cmd->rxchain_sel_flags = iwm_mvm_scan_rx_chain(sc);
389 	cmd->max_out_time = iwm_mvm_scan_max_out_time(sc, 0, is_assoc);
390 	cmd->suspend_time = iwm_mvm_scan_suspend_time(sc, is_assoc);
391 	cmd->rxon_flags = iwm_mvm_scan_rxon_flags(sc, flags);
392 	cmd->filter_flags = htole32(IWM_MAC_FILTER_ACCEPT_GRP |
393 	    IWM_MAC_FILTER_IN_BEACON);
394 
395 	cmd->type = htole32(IWM_SCAN_TYPE_FORCED);
396 	cmd->repeats = htole32(1);
397 
398 	/*
399 	 * If the user asked for passive scan, don't change to active scan if
400 	 * you see any activity on the channel - remain passive.
401 	 */
402 	if (n_ssids > 0) {
403 		cmd->passive2active = htole16(1);
404 		cmd->scan_flags |= IWM_SCAN_FLAGS_PASSIVE2ACTIVE;
405 #if 0
406 		if (basic_ssid) {
407 			ssid = req->ssids[0].ssid;
408 			ssid_len = req->ssids[0].ssid_len;
409 		}
410 #endif
411 	} else {
412 		cmd->passive2active = 0;
413 		cmd->scan_flags &= ~IWM_SCAN_FLAGS_PASSIVE2ACTIVE;
414 	}
415 
416 	cmd->tx_cmd.tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
417 	    IWM_TX_CMD_FLG_BT_DIS);
418 	cmd->tx_cmd.sta_id = sc->sc_aux_sta.sta_id;
419 	cmd->tx_cmd.life_time = htole32(IWM_TX_CMD_LIFE_TIME_INFINITE);
420 	cmd->tx_cmd.rate_n_flags = iwm_mvm_scan_rate_n_flags(sc, flags, 1/*XXX*/);
421 
422 	cmd->tx_cmd.len = htole16(iwm_mvm_fill_probe_req(sc,
423 			    (struct ieee80211_frame *)cmd->data,
424 			    sc->sc_ic.ic_macaddr, n_ssids, ssid, ssid_len,
425 			    NULL, 0, sc->sc_capa_max_probe_len));
426 
427 	cmd->channel_count
428 	    = iwm_mvm_scan_fill_channels(sc, cmd, flags, n_ssids, basic_ssid);
429 
430 	cmd->len = htole16(sizeof(struct iwm_scan_cmd) +
431 		le16toh(cmd->tx_cmd.len) +
432 		(cmd->channel_count * sizeof(struct iwm_scan_channel)));
433 	hcmd.len[0] = le16toh(cmd->len);
434 
435 	status = IWM_SCAN_RESPONSE_OK;
436 	ret = iwm_mvm_send_cmd_status(sc, &hcmd, &status);
437 	if (!ret && status == IWM_SCAN_RESPONSE_OK) {
438 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
439 		    "Scan request was sent successfully\n");
440 	} else {
441 		/*
442 		 * If the scan failed, it usually means that the FW was unable
443 		 * to allocate the time events. Warn on it, but maybe we
444 		 * should try to send the command again with different params.
445 		 */
446 		sc->sc_scanband = 0;
447 		ret = EIO;
448 	}
449 	return ret;
450 }
451