xref: /dragonfly/sys/dev/netif/iwm/if_iwm_scan.c (revision 7f38fe7b)
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/endian.h>
111 #include <sys/firmware.h>
112 #include <sys/kernel.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/rman.h>
116 #include <sys/sysctl.h>
117 #include <sys/linker.h>
118 
119 #include <machine/endian.h>
120 
121 #include <bus/pci/pcivar.h>
122 #include <bus/pci/pcireg.h>
123 
124 #include <net/bpf.h>
125 
126 #include <net/if.h>
127 #include <net/if_var.h>
128 #include <net/if_arp.h>
129 #include <net/if_dl.h>
130 #include <net/if_media.h>
131 #include <net/if_types.h>
132 
133 #include <netinet/in.h>
134 #include <netinet/in_systm.h>
135 #include <netinet/if_ether.h>
136 #include <netinet/ip.h>
137 
138 #include <netproto/802_11/ieee80211_var.h>
139 #include <netproto/802_11/ieee80211_regdomain.h>
140 #include <netproto/802_11/ieee80211_ratectl.h>
141 #include <netproto/802_11/ieee80211_radiotap.h>
142 
143 #include "if_iwmreg.h"
144 #include "if_iwmvar.h"
145 #include "if_iwm_debug.h"
146 #include "if_iwm_notif_wait.h"
147 #include "if_iwm_util.h"
148 #include "if_iwm_scan.h"
149 
150 #define IWM_PLCP_QUIET_THRESH 1
151 #define IWM_ACTIVE_QUIET_TIME 10
152 #define LONG_OUT_TIME_PERIOD (600 * IEEE80211_DUR_TU)
153 #define SHORT_OUT_TIME_PERIOD (200 * IEEE80211_DUR_TU)
154 #define SUSPEND_TIME_PERIOD (100 * IEEE80211_DUR_TU)
155 
156 static uint16_t
157 iwm_mvm_scan_rx_chain(struct iwm_softc *sc)
158 {
159 	uint16_t rx_chain;
160 	uint8_t rx_ant;
161 
162 	rx_ant = iwm_mvm_get_valid_rx_ant(sc);
163 	rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
164 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
165 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
166 	rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
167 	return htole16(rx_chain);
168 }
169 
170 #if 0
171 static uint32_t
172 iwm_mvm_scan_max_out_time(struct iwm_softc *sc, uint32_t flags, int is_assoc)
173 {
174 	if (!is_assoc)
175 		return 0;
176 	if (flags & 0x1)
177 		return htole32(SHORT_OUT_TIME_PERIOD);
178 	return htole32(LONG_OUT_TIME_PERIOD);
179 }
180 
181 static uint32_t
182 iwm_mvm_scan_suspend_time(struct iwm_softc *sc, int is_assoc)
183 {
184 	if (!is_assoc)
185 		return 0;
186 	return htole32(SUSPEND_TIME_PERIOD);
187 }
188 #endif
189 
190 static uint32_t
191 iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
192 {
193 	uint32_t tx_ant;
194 	int i, ind;
195 
196 	for (i = 0, ind = sc->sc_scan_last_antenna;
197 	    i < IWM_RATE_MCS_ANT_NUM; i++) {
198 		ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
199 		if (iwm_mvm_get_valid_tx_ant(sc) & (1 << ind)) {
200 			sc->sc_scan_last_antenna = ind;
201 			break;
202 		}
203 	}
204 	tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
205 
206 	if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
207 		return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
208 				   tx_ant);
209 	else
210 		return htole32(IWM_RATE_6M_PLCP | tx_ant);
211 }
212 
213 #if 0
214 /*
215  * If req->n_ssids > 0, it means we should do an active scan.
216  * In case of active scan w/o directed scan, we receive a zero-length SSID
217  * just to notify that this scan is active and not passive.
218  * In order to notify the FW of the number of SSIDs we wish to scan (including
219  * the zero-length one), we need to set the corresponding bits in chan->type,
220  * one for each SSID, and set the active bit (first). If the first SSID is
221  * already included in the probe template, so we need to set only
222  * req->n_ssids - 1 bits in addition to the first bit.
223  */
224 static uint16_t
225 iwm_mvm_get_active_dwell(struct iwm_softc *sc, int flags, int n_ssids)
226 {
227 	if (flags & IEEE80211_CHAN_2GHZ)
228 		return 30  + 3 * (n_ssids + 1);
229 	return 20  + 2 * (n_ssids + 1);
230 }
231 
232 static uint16_t
233 iwm_mvm_get_passive_dwell(struct iwm_softc *sc, int flags)
234 {
235 	return (flags & IEEE80211_CHAN_2GHZ) ? 100 + 20 : 100 + 10;
236 }
237 #endif
238 
239 static int
240 iwm_mvm_scan_skip_channel(struct ieee80211_channel *c)
241 {
242 	if (IEEE80211_IS_CHAN_2GHZ(c) && IEEE80211_IS_CHAN_B(c))
243 		return 0;
244 	else if (IEEE80211_IS_CHAN_5GHZ(c) && IEEE80211_IS_CHAN_A(c))
245 		return 0;
246 	else
247 		return 1;
248 }
249 
250 static uint8_t
251 iwm_mvm_lmac_scan_fill_channels(struct iwm_softc *sc,
252     struct iwm_scan_channel_cfg_lmac *chan, int n_ssids)
253 {
254 	struct ieee80211com *ic = &sc->sc_ic;
255 	struct ieee80211_channel *c;
256 	uint8_t nchan;
257 	int j;
258 
259 	for (nchan = j = 0;
260 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
261 		c = &ic->ic_channels[j];
262 		/* For 2GHz, only populate 11b channels */
263 		/* For 5GHz, only populate 11a channels */
264 		/*
265 		 * Catch other channels, in case we have 900MHz channels or
266 		 * something in the chanlist.
267 		 */
268 		if (iwm_mvm_scan_skip_channel(c)) {
269 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
270 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
271 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
272 			continue;
273 		}
274 
275 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
276 		    "Adding channel %d (%d Mhz) to the list\n",
277 		    nchan, c->ic_freq);
278 		chan->channel_num = htole16(ieee80211_mhz2ieee(c->ic_freq, 0));
279 		chan->iter_count = htole16(1);
280 		chan->iter_interval = htole32(0);
281 		chan->flags = htole32(IWM_UNIFIED_SCAN_CHANNEL_PARTIAL);
282 #if 0 /* makes scanning while associated less useful */
283 		if (n_ssids != 0)
284 			chan->flags |= htole32(1 << 1); /* select SSID 0 */
285 #endif
286 		chan++;
287 		nchan++;
288 	}
289 
290 	return nchan;
291 }
292 
293 static uint8_t
294 iwm_mvm_umac_scan_fill_channels(struct iwm_softc *sc,
295     struct iwm_scan_channel_cfg_umac *chan, int n_ssids)
296 {
297 	struct ieee80211com *ic = &sc->sc_ic;
298 	struct ieee80211_channel *c;
299 	uint8_t nchan;
300 	int j;
301 
302 	for (nchan = j = 0;
303 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
304 		c = &ic->ic_channels[j];
305 		/* For 2GHz, only populate 11b channels */
306 		/* For 5GHz, only populate 11a channels */
307 		/*
308 		 * Catch other channels, in case we have 900MHz channels or
309 		 * something in the chanlist.
310 		 */
311 		if (iwm_mvm_scan_skip_channel(c)) {
312 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
313 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
314 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
315 			continue;
316 		}
317 
318 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
319 		    "Adding channel %d (%d Mhz) to the list\n",
320 		    nchan, c->ic_freq);
321 		chan->channel_num = ieee80211_mhz2ieee(c->ic_freq, 0);
322 		chan->iter_count = 1;
323 		chan->iter_interval = htole16(0);
324 		chan->flags = htole32(0);
325 #if 0 /* makes scanning while associated less useful */
326 		if (n_ssids != 0)
327 			chan->flags = htole32(1 << 0); /* select SSID 0 */
328 #endif
329 		chan++;
330 		nchan++;
331 	}
332 
333 	return nchan;
334 }
335 
336 static int
337 iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct iwm_scan_probe_req *preq)
338 {
339 	struct ieee80211com *ic = &sc->sc_ic;
340 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
341 	struct ieee80211_frame *wh = (struct ieee80211_frame *)preq->buf;
342 	struct ieee80211_rateset *rs;
343 	size_t remain = sizeof(preq->buf);
344 	uint8_t *frm, *pos;
345 	int ssid_len = 0;
346 	const uint8_t *ssid = NULL;
347 
348 	memset(preq, 0, sizeof(*preq));
349 
350 	/* Ensure enough space for header and SSID IE. */
351 	if (remain < sizeof(*wh) + 2 + ssid_len)
352 		return ENOBUFS;
353 
354 	/*
355 	 * Build a probe request frame.  Most of the following code is a
356 	 * copy & paste of what is done in net80211.
357 	 */
358 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
359 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
360 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
361 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
362 	IEEE80211_ADDR_COPY(wh->i_addr2, vap ? vap->iv_myaddr : ic->ic_macaddr);
363 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
364 	*(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
365 	*(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
366 
367 	frm = (uint8_t *)(wh + 1);
368 	frm = ieee80211_add_ssid(frm, ssid, ssid_len);
369 
370 	/* Tell the firmware where the MAC header is. */
371 	preq->mac_header.offset = 0;
372 	preq->mac_header.len = htole16(frm - (uint8_t *)wh);
373 	remain -= frm - (uint8_t *)wh;
374 
375 	/* Fill in 2GHz IEs and tell firmware where they are. */
376 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
377 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
378 		if (remain < 4 + rs->rs_nrates)
379 			return ENOBUFS;
380 	} else if (remain < 2 + rs->rs_nrates) {
381 		return ENOBUFS;
382 	}
383 	preq->band_data[0].offset = htole16(frm - (uint8_t *)wh);
384 	pos = frm;
385 	frm = ieee80211_add_rates(frm, rs);
386 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
387 		frm = ieee80211_add_xrates(frm, rs);
388 	preq->band_data[0].len = htole16(frm - pos);
389 	remain -= frm - pos;
390 
391 	if (isset(sc->sc_enabled_capa,
392 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT)) {
393 		if (remain < 3)
394 			return ENOBUFS;
395 		*frm++ = IEEE80211_ELEMID_DSPARMS;
396 		*frm++ = 1;
397 		*frm++ = 0;
398 		remain -= 3;
399 	}
400 
401 	if (sc->nvm_data->sku_cap_band_52GHz_enable) {
402 		/* Fill in 5GHz IEs. */
403 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
404 		if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
405 			if (remain < 4 + rs->rs_nrates)
406 				return ENOBUFS;
407 		} else if (remain < 2 + rs->rs_nrates) {
408 			return ENOBUFS;
409 		}
410 		preq->band_data[1].offset = htole16(frm - (uint8_t *)wh);
411 		pos = frm;
412 		frm = ieee80211_add_rates(frm, rs);
413 		if (rs->rs_nrates > IEEE80211_RATE_SIZE)
414 			frm = ieee80211_add_xrates(frm, rs);
415 		preq->band_data[1].len = htole16(frm - pos);
416 		remain -= frm - pos;
417 	}
418 
419 	/* Send 11n IEs on both 2GHz and 5GHz bands. */
420 	preq->common_data.offset = htole16(frm - (uint8_t *)wh);
421 	pos = frm;
422 #if 0
423 	if (ic->ic_flags & IEEE80211_F_HTON) {
424 		if (remain < 28)
425 			return ENOBUFS;
426 		frm = ieee80211_add_htcaps(frm, ic);
427 		/* XXX add WME info? */
428 	}
429 #endif
430 	preq->common_data.len = htole16(frm - pos);
431 
432 	return 0;
433 }
434 
435 int
436 iwm_mvm_config_umac_scan(struct iwm_softc *sc)
437 {
438 	struct ieee80211com *ic = &sc->sc_ic;
439 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
440 
441 	struct iwm_scan_config *scan_config;
442 	int ret, j, nchan;
443 	size_t cmd_size;
444 	struct ieee80211_channel *c;
445 	struct iwm_host_cmd hcmd = {
446 		.id = iwm_cmd_id(IWM_SCAN_CFG_CMD, IWM_ALWAYS_LONG_GROUP, 0),
447 		.flags = IWM_CMD_SYNC,
448 	};
449 	static const uint32_t rates = (IWM_SCAN_CONFIG_RATE_1M |
450 	    IWM_SCAN_CONFIG_RATE_2M | IWM_SCAN_CONFIG_RATE_5M |
451 	    IWM_SCAN_CONFIG_RATE_11M | IWM_SCAN_CONFIG_RATE_6M |
452 	    IWM_SCAN_CONFIG_RATE_9M | IWM_SCAN_CONFIG_RATE_12M |
453 	    IWM_SCAN_CONFIG_RATE_18M | IWM_SCAN_CONFIG_RATE_24M |
454 	    IWM_SCAN_CONFIG_RATE_36M | IWM_SCAN_CONFIG_RATE_48M |
455 	    IWM_SCAN_CONFIG_RATE_54M);
456 
457 	cmd_size = sizeof(*scan_config) + sc->sc_capa_n_scan_channels;
458 
459 	scan_config = kmalloc(cmd_size, M_DEVBUF, M_INTWAIT | M_ZERO);
460 	if (scan_config == NULL)
461 		return ENOMEM;
462 
463 	scan_config->tx_chains = htole32(iwm_mvm_get_valid_tx_ant(sc));
464 	scan_config->rx_chains = htole32(iwm_mvm_get_valid_rx_ant(sc));
465 	scan_config->legacy_rates = htole32(rates |
466 	    IWM_SCAN_CONFIG_SUPPORTED_RATE(rates));
467 
468 	/* These timings correspond to iwlwifi's UNASSOC scan. */
469 	scan_config->dwell_active = 10;
470 	scan_config->dwell_passive = 110;
471 	scan_config->dwell_fragmented = 44;
472 	scan_config->dwell_extended = 90;
473 	scan_config->out_of_channel_time = htole32(0);
474 	scan_config->suspend_time = htole32(0);
475 
476 	IEEE80211_ADDR_COPY(scan_config->mac_addr,
477 	    vap ? vap->iv_myaddr : ic->ic_macaddr);
478 
479 	scan_config->bcast_sta_id = sc->sc_aux_sta.sta_id;
480 	scan_config->channel_flags = IWM_CHANNEL_FLAG_EBS |
481 	    IWM_CHANNEL_FLAG_ACCURATE_EBS | IWM_CHANNEL_FLAG_EBS_ADD |
482 	    IWM_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
483 
484 	for (nchan = j = 0;
485 	    j < ic->ic_nchans && nchan < sc->sc_capa_n_scan_channels; j++) {
486 		c = &ic->ic_channels[j];
487 		/* For 2GHz, only populate 11b channels */
488 		/* For 5GHz, only populate 11a channels */
489 		/*
490 		 * Catch other channels, in case we have 900MHz channels or
491 		 * something in the chanlist.
492 		 */
493 		if (iwm_mvm_scan_skip_channel(c))
494 			continue;
495 		scan_config->channel_array[nchan++] =
496 		    ieee80211_mhz2ieee(c->ic_freq, 0);
497 	}
498 
499 	scan_config->flags = htole32(IWM_SCAN_CONFIG_FLAG_ACTIVATE |
500 	    IWM_SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
501 	    IWM_SCAN_CONFIG_FLAG_SET_TX_CHAINS |
502 	    IWM_SCAN_CONFIG_FLAG_SET_RX_CHAINS |
503 	    IWM_SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
504 	    IWM_SCAN_CONFIG_FLAG_SET_ALL_TIMES |
505 	    IWM_SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
506 	    IWM_SCAN_CONFIG_FLAG_SET_MAC_ADDR |
507 	    IWM_SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
508 	    IWM_SCAN_CONFIG_N_CHANNELS(nchan) |
509 	    IWM_SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
510 
511 	hcmd.data[0] = scan_config;
512 	hcmd.len[0] = cmd_size;
513 
514 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending UMAC scan config\n");
515 
516 	ret = iwm_send_cmd(sc, &hcmd);
517 	if (!ret)
518 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
519 		    "UMAC scan config was sent successfully\n");
520 
521 	kfree(scan_config, M_DEVBUF);
522 	return ret;
523 }
524 
525 int
526 iwm_mvm_umac_scan(struct iwm_softc *sc)
527 {
528 	struct iwm_host_cmd hcmd = {
529 		.id = iwm_cmd_id(IWM_SCAN_REQ_UMAC, IWM_ALWAYS_LONG_GROUP, 0),
530 		.len = { 0, },
531 		.data = { NULL, },
532 		.flags = IWM_CMD_SYNC,
533 	};
534 	struct iwm_scan_req_umac *req;
535 	struct iwm_scan_req_umac_tail *tail;
536 	size_t req_len;
537 	int ssid_len = 0;
538 	const uint8_t *ssid = NULL;
539 	int ret;
540 
541 	req_len = sizeof(struct iwm_scan_req_umac) +
542 	    (sizeof(struct iwm_scan_channel_cfg_umac) *
543 	    sc->sc_capa_n_scan_channels) +
544 	    sizeof(struct iwm_scan_req_umac_tail);
545 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
546 		return ENOMEM;
547 	req = kmalloc(req_len, M_DEVBUF, M_INTWAIT | M_ZERO);
548 	if (req == NULL)
549 		return ENOMEM;
550 
551 	hcmd.len[0] = (uint16_t)req_len;
552 	hcmd.data[0] = (void *)req;
553 
554 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Handling ieee80211 scan request\n");
555 
556 	/* These timings correspond to iwlwifi's UNASSOC scan. */
557 	req->active_dwell = 10;
558 	req->passive_dwell = 110;
559 	req->fragmented_dwell = 44;
560 	req->extended_dwell = 90;
561 	req->max_out_time = 0;
562 	req->suspend_time = 0;
563 
564 	req->scan_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
565 	req->ooc_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
566 
567 	req->n_channels = iwm_mvm_umac_scan_fill_channels(sc,
568 	    (struct iwm_scan_channel_cfg_umac *)req->data, ssid_len != 0);
569 
570 	req->general_flags = htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASS_ALL |
571 	    IWM_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE |
572 	    IWM_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL);
573 
574 	tail = (void *)((char *)&req->data +
575 		sizeof(struct iwm_scan_channel_cfg_umac) *
576 			sc->sc_capa_n_scan_channels);
577 
578 	/* Check if we're doing an active directed scan. */
579 	if (ssid_len != 0) {
580 		tail->direct_scan[0].id = IEEE80211_ELEMID_SSID;
581 		tail->direct_scan[0].len = ssid_len;
582 		memcpy(tail->direct_scan[0].ssid, ssid, ssid_len);
583 		req->general_flags |=
584 		    htole32(IWM_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT);
585 	} else {
586 		req->general_flags |= htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASSIVE);
587 	}
588 
589 	if (isset(sc->sc_enabled_capa,
590 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
591 		req->general_flags |=
592 		    htole32(IWM_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED);
593 
594 	ret = iwm_mvm_fill_probe_req(sc, &tail->preq);
595 	if (ret) {
596 		kfree(req, M_DEVBUF);
597 		return ret;
598 	}
599 
600 	/* Specify the scan plan: We'll do one iteration. */
601 	tail->schedule[0].interval = 0;
602 	tail->schedule[0].iter_count = 1;
603 
604 	ret = iwm_send_cmd(sc, &hcmd);
605 	if (!ret)
606 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
607 		    "Scan request was sent successfully\n");
608 	kfree(req, M_DEVBUF);
609 	return ret;
610 }
611 
612 int
613 iwm_mvm_lmac_scan(struct iwm_softc *sc)
614 {
615 	struct iwm_host_cmd hcmd = {
616 		.id = IWM_SCAN_OFFLOAD_REQUEST_CMD,
617 		.len = { 0, },
618 		.data = { NULL, },
619 		.flags = IWM_CMD_SYNC,
620 	};
621 	struct iwm_scan_req_lmac *req;
622 	size_t req_len;
623 	int ret;
624 	int ssid_len = 0;
625 	const uint8_t *ssid = NULL;
626 
627 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
628 	    "Handling ieee80211 scan request\n");
629 
630 	req_len = sizeof(struct iwm_scan_req_lmac) +
631 	    (sizeof(struct iwm_scan_channel_cfg_lmac) *
632 	    sc->sc_capa_n_scan_channels) + sizeof(struct iwm_scan_probe_req);
633 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
634 		return ENOMEM;
635 	req = kmalloc(req_len, M_DEVBUF, M_INTWAIT | M_ZERO);
636 	if (req == NULL)
637 		return ENOMEM;
638 
639 	hcmd.len[0] = (uint16_t)req_len;
640 	hcmd.data[0] = (void *)req;
641 
642 	/* These timings correspond to iwlwifi's UNASSOC scan. */
643 	req->active_dwell = 10;
644 	req->passive_dwell = 110;
645 	req->fragmented_dwell = 44;
646 	req->extended_dwell = 90;
647 	req->max_out_time = 0;
648 	req->suspend_time = 0;
649 
650 	req->scan_prio = htole32(IWM_SCAN_PRIORITY_HIGH);
651 	req->rx_chain_select = iwm_mvm_scan_rx_chain(sc);
652 	req->iter_num = htole32(1);
653 	req->delay = 0;
654 
655 	req->scan_flags = htole32(IWM_MVM_LMAC_SCAN_FLAG_PASS_ALL |
656 	    IWM_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE |
657 	    IWM_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL);
658 	if (ssid_len == 0)
659 		req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAG_PASSIVE);
660 	else
661 		req->scan_flags |=
662 		    htole32(IWM_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION);
663 	if (isset(sc->sc_enabled_capa,
664 	    IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
665 		req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
666 
667 	req->flags = htole32(IWM_PHY_BAND_24);
668 	if (sc->nvm_data->sku_cap_band_52GHz_enable)
669 		req->flags |= htole32(IWM_PHY_BAND_5);
670 	req->filter_flags =
671 	    htole32(IWM_MAC_FILTER_ACCEPT_GRP | IWM_MAC_FILTER_IN_BEACON);
672 
673 	/* Tx flags 2 GHz. */
674 	req->tx_cmd[0].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
675 	    IWM_TX_CMD_FLG_BT_DIS);
676 	req->tx_cmd[0].rate_n_flags =
677 	    iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_2GHZ, 1/*XXX*/);
678 	req->tx_cmd[0].sta_id = sc->sc_aux_sta.sta_id;
679 
680 	/* Tx flags 5 GHz. */
681 	req->tx_cmd[1].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
682 	    IWM_TX_CMD_FLG_BT_DIS);
683 	req->tx_cmd[1].rate_n_flags =
684 	    iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_5GHZ, 1/*XXX*/);
685 	req->tx_cmd[1].sta_id = sc->sc_aux_sta.sta_id;
686 
687 	/* Check if we're doing an active directed scan. */
688 	if (ssid_len != 0) {
689 		req->direct_scan[0].id = IEEE80211_ELEMID_SSID;
690 		req->direct_scan[0].len = ssid_len;
691 		memcpy(req->direct_scan[0].ssid, ssid, ssid_len);
692 	}
693 
694 	req->n_channels = iwm_mvm_lmac_scan_fill_channels(sc,
695 	    (struct iwm_scan_channel_cfg_lmac *)req->data,
696 	    ssid_len != 0);
697 
698 	ret = iwm_mvm_fill_probe_req(sc,
699 			    (struct iwm_scan_probe_req *)(req->data +
700 			    (sizeof(struct iwm_scan_channel_cfg_lmac) *
701 			    sc->sc_capa_n_scan_channels)));
702 	if (ret) {
703 		kfree(req, M_DEVBUF);
704 		return ret;
705 	}
706 
707 	/* Specify the scan plan: We'll do one iteration. */
708 	req->schedule[0].iterations = 1;
709 	req->schedule[0].full_scan_mul = 1;
710 
711 	/* Disable EBS. */
712 	req->channel_opt[0].non_ebs_ratio = 1;
713 	req->channel_opt[1].non_ebs_ratio = 1;
714 
715 	ret = iwm_send_cmd(sc, &hcmd);
716 	if (!ret) {
717 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
718 		    "Scan request was sent successfully\n");
719 	}
720 	kfree(req, M_DEVBUF);
721 	return ret;
722 }
723 
724 static int
725 iwm_mvm_lmac_scan_abort(struct iwm_softc *sc)
726 {
727 	int ret;
728 	struct iwm_host_cmd hcmd = {
729 		.id = IWM_SCAN_OFFLOAD_ABORT_CMD,
730 		.len = { 0, },
731 		.data = { NULL, },
732 		.flags = IWM_CMD_SYNC,
733 	};
734 	uint32_t status;
735 
736 	ret = iwm_mvm_send_cmd_status(sc, &hcmd, &status);
737 	if (ret)
738 		return ret;
739 
740 	if (status != IWM_CAN_ABORT_STATUS) {
741 		/*
742 		 * The scan abort will return 1 for success or
743 		 * 2 for "failure".  A failure condition can be
744 		 * due to simply not being in an active scan which
745 		 * can occur if we send the scan abort before the
746 		 * microcode has notified us that a scan is completed.
747 		 */
748 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
749 		    "SCAN OFFLOAD ABORT ret %d.\n", status);
750 		ret = ENOENT;
751 	}
752 
753 	return ret;
754 }
755 
756 static int
757 iwm_mvm_umac_scan_abort(struct iwm_softc *sc)
758 {
759 	struct iwm_umac_scan_abort cmd = {};
760 	int uid, ret;
761 
762 	uid = 0;
763 	cmd.uid = htole32(uid);
764 
765 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending scan abort, uid %u\n", uid);
766 
767 	ret = iwm_mvm_send_cmd_pdu(sc,
768 				   iwm_cmd_id(IWM_SCAN_ABORT_UMAC,
769 					      IWM_ALWAYS_LONG_GROUP, 0),
770 				   0, sizeof(cmd), &cmd);
771 
772 	return ret;
773 }
774 
775 int
776 iwm_mvm_scan_stop_wait(struct iwm_softc *sc)
777 {
778 	struct iwm_notification_wait wait_scan_done;
779 	static const uint16_t scan_done_notif[] = { IWM_SCAN_COMPLETE_UMAC,
780 						   IWM_SCAN_OFFLOAD_COMPLETE, };
781 	int ret;
782 
783 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_scan_done,
784 				   scan_done_notif, NELEM(scan_done_notif),
785 				   NULL, NULL);
786 
787 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Preparing to stop scan\n");
788 
789 	if (isset(sc->sc_enabled_capa, IWM_UCODE_TLV_CAPA_UMAC_SCAN))
790 		ret = iwm_mvm_umac_scan_abort(sc);
791 	else
792 		ret = iwm_mvm_lmac_scan_abort(sc);
793 
794 	if (ret) {
795 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "couldn't stop scan\n");
796 		iwm_remove_notification(sc->sc_notif_wait, &wait_scan_done);
797 		return ret;
798 	}
799 
800 	IWM_UNLOCK(sc);
801 	ret = iwm_wait_notification(sc->sc_notif_wait, &wait_scan_done, hz);
802 	IWM_LOCK(sc);
803 
804 	return ret;
805 }
806