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