xref: /freebsd/sys/dev/iwm/if_iwm_scan.c (revision 685dc743)
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>
106b789292fSAndriy Voskoboinyk #include "opt_wlan.h"
107616201d1SAdrian Chadd #include "opt_iwm.h"
108b789292fSAndriy Voskoboinyk 
109d4886179SRui Paulo #include <sys/param.h>
110d4886179SRui Paulo #include <sys/bus.h>
111d4886179SRui Paulo #include <sys/conf.h>
112d4886179SRui Paulo #include <sys/endian.h>
113d4886179SRui Paulo #include <sys/firmware.h>
114d4886179SRui Paulo #include <sys/kernel.h>
115d4886179SRui Paulo #include <sys/malloc.h>
116d4886179SRui Paulo #include <sys/mbuf.h>
117d4886179SRui Paulo #include <sys/mutex.h>
118d4886179SRui Paulo #include <sys/module.h>
119d4886179SRui Paulo #include <sys/proc.h>
120d4886179SRui Paulo #include <sys/rman.h>
121d4886179SRui Paulo #include <sys/socket.h>
122d4886179SRui Paulo #include <sys/sockio.h>
123d4886179SRui Paulo #include <sys/sysctl.h>
124d4886179SRui Paulo #include <sys/linker.h>
125d4886179SRui Paulo 
126d4886179SRui Paulo #include <machine/bus.h>
127d4886179SRui Paulo #include <machine/endian.h>
128d4886179SRui Paulo #include <machine/resource.h>
129d4886179SRui Paulo 
130d4886179SRui Paulo #include <dev/pci/pcivar.h>
131d4886179SRui Paulo #include <dev/pci/pcireg.h>
132d4886179SRui Paulo 
133d4886179SRui Paulo #include <net/bpf.h>
134d4886179SRui Paulo 
135d4886179SRui Paulo #include <net/if.h>
136d4886179SRui Paulo #include <net/if_var.h>
137d4886179SRui Paulo #include <net/if_arp.h>
138d4886179SRui Paulo #include <net/if_dl.h>
139d4886179SRui Paulo #include <net/if_media.h>
140d4886179SRui Paulo #include <net/if_types.h>
141d4886179SRui Paulo 
142d4886179SRui Paulo #include <netinet/in.h>
143d4886179SRui Paulo #include <netinet/in_systm.h>
144d4886179SRui Paulo #include <netinet/if_ether.h>
145d4886179SRui Paulo #include <netinet/ip.h>
146d4886179SRui Paulo 
147d4886179SRui Paulo #include <net80211/ieee80211_var.h>
148d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
149d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
150d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
151d4886179SRui Paulo 
15249fdbf0aSRui Paulo #include <dev/iwm/if_iwmreg.h>
15349fdbf0aSRui Paulo #include <dev/iwm/if_iwmvar.h>
15449fdbf0aSRui Paulo #include <dev/iwm/if_iwm_debug.h>
1554b17c188SAdrian Chadd #include <dev/iwm/if_iwm_notif_wait.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 
163cd20383eSAdrian Chadd #define IWM_DENSE_EBS_SCAN_RATIO 5
164cd20383eSAdrian Chadd #define IWM_SPARSE_EBS_SCAN_RATIO 1
165cd20383eSAdrian Chadd 
166d4886179SRui Paulo static uint16_t
iwm_scan_rx_chain(struct iwm_softc * sc)167e7065dd1SMark Johnston iwm_scan_rx_chain(struct iwm_softc *sc)
168d4886179SRui Paulo {
169d4886179SRui Paulo 	uint16_t rx_chain;
170d4886179SRui Paulo 	uint8_t rx_ant;
171d4886179SRui Paulo 
172e7065dd1SMark Johnston 	rx_ant = iwm_get_valid_rx_ant(sc);
173d4886179SRui Paulo 	rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
174d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
175d4886179SRui Paulo 	rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
176d4886179SRui Paulo 	rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
177d4886179SRui Paulo 	return htole16(rx_chain);
178d4886179SRui Paulo }
179d4886179SRui Paulo 
180fe845d8eSAdrian Chadd static uint32_t
iwm_scan_rxon_flags(struct ieee80211_channel * c)181e7065dd1SMark Johnston iwm_scan_rxon_flags(struct ieee80211_channel *c)
182fe845d8eSAdrian Chadd {
183fe845d8eSAdrian Chadd 	if (IEEE80211_IS_CHAN_2GHZ(c))
184fe845d8eSAdrian Chadd 		return htole32(IWM_PHY_BAND_24);
185fe845d8eSAdrian Chadd 	else
186fe845d8eSAdrian Chadd 		return htole32(IWM_PHY_BAND_5);
187fe845d8eSAdrian Chadd }
188fe845d8eSAdrian Chadd 
189d4886179SRui Paulo static uint32_t
iwm_scan_rate_n_flags(struct iwm_softc * sc,int flags,int no_cck)190e7065dd1SMark Johnston iwm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
191d4886179SRui Paulo {
192d4886179SRui Paulo 	uint32_t tx_ant;
193d4886179SRui Paulo 	int i, ind;
194d4886179SRui Paulo 
195d4886179SRui Paulo 	for (i = 0, ind = sc->sc_scan_last_antenna;
196d4886179SRui Paulo 	    i < IWM_RATE_MCS_ANT_NUM; i++) {
197d4886179SRui Paulo 		ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
198e7065dd1SMark Johnston 		if (iwm_get_valid_tx_ant(sc) & (1 << ind)) {
199d4886179SRui Paulo 			sc->sc_scan_last_antenna = ind;
200d4886179SRui Paulo 			break;
201d4886179SRui Paulo 		}
202d4886179SRui Paulo 	}
203d4886179SRui Paulo 	tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
204d4886179SRui Paulo 
205d4886179SRui Paulo 	if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
206d4886179SRui Paulo 		return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
207d4886179SRui Paulo 				   tx_ant);
208d4886179SRui Paulo 	else
209d4886179SRui Paulo 		return htole32(IWM_RATE_6M_PLCP | tx_ant);
210d4886179SRui Paulo }
211d4886179SRui Paulo 
212fe845d8eSAdrian Chadd static inline boolean_t
iwm_rrm_scan_needed(struct iwm_softc * sc)213e7065dd1SMark Johnston iwm_rrm_scan_needed(struct iwm_softc *sc)
214fe845d8eSAdrian Chadd {
215fe845d8eSAdrian Chadd 	/* require rrm scan whenever the fw supports it */
216eff8a979SMark Johnston 	return iwm_fw_has_capa(sc, IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
217fe845d8eSAdrian Chadd }
218fe845d8eSAdrian Chadd 
2197beeab43SAndriy Voskoboinyk #ifdef IWM_DEBUG
220cd20383eSAdrian Chadd static const char *
iwm_ebs_status_str(enum iwm_scan_ebs_status status)221e7065dd1SMark Johnston iwm_ebs_status_str(enum iwm_scan_ebs_status status)
222cd20383eSAdrian Chadd {
223cd20383eSAdrian Chadd 	switch (status) {
224cd20383eSAdrian Chadd 	case IWM_SCAN_EBS_SUCCESS:
225cd20383eSAdrian Chadd 		return "successful";
226cd20383eSAdrian Chadd 	case IWM_SCAN_EBS_INACTIVE:
227cd20383eSAdrian Chadd 		return "inactive";
228cd20383eSAdrian Chadd 	case IWM_SCAN_EBS_FAILED:
229cd20383eSAdrian Chadd 	case IWM_SCAN_EBS_CHAN_NOT_FOUND:
230cd20383eSAdrian Chadd 	default:
231cd20383eSAdrian Chadd 		return "failed";
232cd20383eSAdrian Chadd 	}
233cd20383eSAdrian Chadd }
234cd20383eSAdrian Chadd 
2357beeab43SAndriy Voskoboinyk static const char *
iwm_offload_status_str(enum iwm_scan_offload_complete_status status)236e7065dd1SMark Johnston iwm_offload_status_str(enum iwm_scan_offload_complete_status status)
2377beeab43SAndriy Voskoboinyk {
2387beeab43SAndriy Voskoboinyk 	return (status == IWM_SCAN_OFFLOAD_ABORTED) ? "aborted" : "completed";
2397beeab43SAndriy Voskoboinyk }
2407beeab43SAndriy Voskoboinyk #endif
2417beeab43SAndriy Voskoboinyk 
242cd20383eSAdrian Chadd void
iwm_rx_lmac_scan_complete_notif(struct iwm_softc * sc,struct iwm_rx_packet * pkt)243e7065dd1SMark Johnston iwm_rx_lmac_scan_complete_notif(struct iwm_softc *sc,
244cd20383eSAdrian Chadd     struct iwm_rx_packet *pkt)
245cd20383eSAdrian Chadd {
246cd20383eSAdrian Chadd 	struct iwm_periodic_scan_complete *scan_notif = (void *)pkt->data;
247cd20383eSAdrian Chadd 
248cd20383eSAdrian Chadd 	/* If this happens, the firmware has mistakenly sent an LMAC
249cd20383eSAdrian Chadd 	 * notification during UMAC scans -- warn and ignore it.
250cd20383eSAdrian Chadd 	 */
251eff8a979SMark Johnston 	if (iwm_fw_has_capa(sc, IWM_UCODE_TLV_CAPA_UMAC_SCAN)) {
252cd20383eSAdrian Chadd 		device_printf(sc->sc_dev,
253cd20383eSAdrian Chadd 		    "%s: Mistakenly got LMAC notification during UMAC scan\n",
254cd20383eSAdrian Chadd 		    __func__);
255cd20383eSAdrian Chadd 		return;
256cd20383eSAdrian Chadd 	}
257cd20383eSAdrian Chadd 
258cd20383eSAdrian Chadd 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Regular scan %s, EBS status %s (FW)\n",
259e7065dd1SMark Johnston 	    iwm_offload_status_str(scan_notif->status),
260e7065dd1SMark Johnston 	    iwm_ebs_status_str(scan_notif->ebs_status));
261cd20383eSAdrian Chadd 
262cd20383eSAdrian Chadd 	sc->last_ebs_successful =
263cd20383eSAdrian Chadd 			scan_notif->ebs_status == IWM_SCAN_EBS_SUCCESS ||
264cd20383eSAdrian Chadd 			scan_notif->ebs_status == IWM_SCAN_EBS_INACTIVE;
265cd20383eSAdrian Chadd 
266cd20383eSAdrian Chadd }
267cd20383eSAdrian Chadd 
268cd20383eSAdrian Chadd void
iwm_rx_umac_scan_complete_notif(struct iwm_softc * sc,struct iwm_rx_packet * pkt)269e7065dd1SMark Johnston iwm_rx_umac_scan_complete_notif(struct iwm_softc *sc,
270cd20383eSAdrian Chadd     struct iwm_rx_packet *pkt)
271cd20383eSAdrian Chadd {
272cd20383eSAdrian Chadd 	struct iwm_umac_scan_complete *notif = (void *)pkt->data;
273cd20383eSAdrian Chadd 
274cd20383eSAdrian Chadd 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
275cd20383eSAdrian Chadd 	    "Scan completed, uid %u, status %s, EBS status %s\n",
2767beeab43SAndriy Voskoboinyk 	    le32toh(notif->uid),
277e7065dd1SMark Johnston 	    iwm_offload_status_str(notif->status),
278e7065dd1SMark Johnston 	    iwm_ebs_status_str(notif->ebs_status));
279cd20383eSAdrian Chadd 
280cd20383eSAdrian Chadd 	if (notif->ebs_status != IWM_SCAN_EBS_SUCCESS &&
281cd20383eSAdrian Chadd 	    notif->ebs_status != IWM_SCAN_EBS_INACTIVE)
282cd20383eSAdrian Chadd 		sc->last_ebs_successful = FALSE;
283cd20383eSAdrian Chadd }
284cd20383eSAdrian Chadd 
285d4886179SRui Paulo static int
iwm_scan_skip_channel(struct ieee80211_channel * c)286e7065dd1SMark Johnston iwm_scan_skip_channel(struct ieee80211_channel *c)
2876a5bc1d1SSean Bruno {
2886a5bc1d1SSean Bruno 	if (IEEE80211_IS_CHAN_2GHZ(c) && IEEE80211_IS_CHAN_B(c))
2896a5bc1d1SSean Bruno 		return 0;
2906a5bc1d1SSean Bruno 	else if (IEEE80211_IS_CHAN_5GHZ(c) && IEEE80211_IS_CHAN_A(c))
2916a5bc1d1SSean Bruno 		return 0;
2926a5bc1d1SSean Bruno 	else
2936a5bc1d1SSean Bruno 		return 1;
2946a5bc1d1SSean Bruno }
2956a5bc1d1SSean Bruno 
2966a5bc1d1SSean Bruno static uint8_t
iwm_lmac_scan_fill_channels(struct iwm_softc * sc,struct iwm_scan_channel_cfg_lmac * chan,int n_ssids)297e7065dd1SMark Johnston iwm_lmac_scan_fill_channels(struct iwm_softc *sc,
2986a5bc1d1SSean Bruno     struct iwm_scan_channel_cfg_lmac *chan, int n_ssids)
299d4886179SRui Paulo {
3007a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
3019afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = ic->ic_scan;
302d4886179SRui Paulo 	struct ieee80211_channel *c;
3036a5bc1d1SSean Bruno 	uint8_t nchan;
3046a5bc1d1SSean Bruno 	int j;
305d4886179SRui Paulo 
3066a5bc1d1SSean Bruno 	for (nchan = j = 0;
307f33c8309SKyle Evans 	    j < ss->ss_last && nchan < sc->sc_fw.ucode_capa.n_scan_channels;
308f33c8309SKyle Evans 	    j++) {
3092aa3dbb0SAdrian Chadd 		c = ss->ss_chans[j];
310d4886179SRui Paulo 		/*
311d4886179SRui Paulo 		 * Catch other channels, in case we have 900MHz channels or
312d4886179SRui Paulo 		 * something in the chanlist.
313d4886179SRui Paulo 		 */
3142aa3dbb0SAdrian Chadd 		if (!IEEE80211_IS_CHAN_2GHZ(c) && !IEEE80211_IS_CHAN_5GHZ(c)) {
315d4886179SRui Paulo 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
316d4886179SRui Paulo 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
3176a5bc1d1SSean Bruno 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
3186a5bc1d1SSean Bruno 			continue;
319d4886179SRui Paulo 		}
3206a5bc1d1SSean Bruno 
321d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
322d4886179SRui Paulo 		    "Adding channel %d (%d Mhz) to the list\n",
323d4886179SRui Paulo 		    nchan, c->ic_freq);
3246a5bc1d1SSean Bruno 		chan->channel_num = htole16(ieee80211_mhz2ieee(c->ic_freq, 0));
3256a5bc1d1SSean Bruno 		chan->iter_count = htole16(1);
3266a5bc1d1SSean Bruno 		chan->iter_interval = htole32(0);
3276a5bc1d1SSean Bruno 		chan->flags = htole32(IWM_UNIFIED_SCAN_CHANNEL_PARTIAL);
3289afea60fSAndriy Voskoboinyk 		chan->flags |= htole32(IWM_SCAN_CHANNEL_NSSIDS(n_ssids));
3299afea60fSAndriy Voskoboinyk 		/* XXX IEEE80211_SCAN_NOBCAST flag is never set. */
3309afea60fSAndriy Voskoboinyk 		if (!IEEE80211_IS_CHAN_PASSIVE(c) &&
3319afea60fSAndriy Voskoboinyk 		    (!(ss->ss_flags & IEEE80211_SCAN_NOBCAST) || n_ssids != 0))
3329afea60fSAndriy Voskoboinyk 			chan->flags |= htole32(IWM_SCAN_CHANNEL_TYPE_ACTIVE);
333d4886179SRui Paulo 		chan++;
334d4886179SRui Paulo 		nchan++;
335d4886179SRui Paulo 	}
3366a5bc1d1SSean Bruno 
337d4886179SRui Paulo 	return nchan;
338d4886179SRui Paulo }
339d4886179SRui Paulo 
3406a5bc1d1SSean Bruno static uint8_t
iwm_umac_scan_fill_channels(struct iwm_softc * sc,struct iwm_scan_channel_cfg_umac * chan,int n_ssids)341e7065dd1SMark Johnston iwm_umac_scan_fill_channels(struct iwm_softc *sc,
3426a5bc1d1SSean Bruno     struct iwm_scan_channel_cfg_umac *chan, int n_ssids)
343d4886179SRui Paulo {
3446a5bc1d1SSean Bruno 	struct ieee80211com *ic = &sc->sc_ic;
3452aa3dbb0SAdrian Chadd 	struct ieee80211_scan_state *ss = ic->ic_scan;
3466a5bc1d1SSean Bruno 	struct ieee80211_channel *c;
3476a5bc1d1SSean Bruno 	uint8_t nchan;
3486a5bc1d1SSean Bruno 	int j;
349d4886179SRui Paulo 
3506a5bc1d1SSean Bruno 	for (nchan = j = 0;
351f33c8309SKyle Evans 	    j < ss->ss_last && nchan < sc->sc_fw.ucode_capa.n_scan_channels;
352f33c8309SKyle Evans 	    j++) {
3532aa3dbb0SAdrian Chadd 		c = ss->ss_chans[j];
3546a5bc1d1SSean Bruno 		/*
3556a5bc1d1SSean Bruno 		 * Catch other channels, in case we have 900MHz channels or
3566a5bc1d1SSean Bruno 		 * something in the chanlist.
3576a5bc1d1SSean Bruno 		 */
3582aa3dbb0SAdrian Chadd 		if (!IEEE80211_IS_CHAN_2GHZ(c) && !IEEE80211_IS_CHAN_5GHZ(c)) {
3596a5bc1d1SSean Bruno 			IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
3606a5bc1d1SSean Bruno 			    "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
3616a5bc1d1SSean Bruno 			    __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
3626a5bc1d1SSean Bruno 			continue;
363d4886179SRui Paulo 		}
364d4886179SRui Paulo 
3656a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
3666a5bc1d1SSean Bruno 		    "Adding channel %d (%d Mhz) to the list\n",
3676a5bc1d1SSean Bruno 		    nchan, c->ic_freq);
3686a5bc1d1SSean Bruno 		chan->channel_num = ieee80211_mhz2ieee(c->ic_freq, 0);
3696a5bc1d1SSean Bruno 		chan->iter_count = 1;
3706a5bc1d1SSean Bruno 		chan->iter_interval = htole16(0);
3719afea60fSAndriy Voskoboinyk 		chan->flags = htole32(IWM_SCAN_CHANNEL_UMAC_NSSIDS(n_ssids));
3726a5bc1d1SSean Bruno 		chan++;
3736a5bc1d1SSean Bruno 		nchan++;
3746a5bc1d1SSean Bruno 	}
3756a5bc1d1SSean Bruno 
3766a5bc1d1SSean Bruno 	return nchan;
3776a5bc1d1SSean Bruno }
3786a5bc1d1SSean Bruno 
3796a5bc1d1SSean Bruno static int
iwm_fill_probe_req(struct iwm_softc * sc,struct iwm_scan_probe_req_v1 * preq)380355c1513SAdrian Chadd iwm_fill_probe_req(struct iwm_softc *sc, struct iwm_scan_probe_req_v1 *preq)
3816a5bc1d1SSean Bruno {
3826a5bc1d1SSean Bruno 	struct ieee80211com *ic = &sc->sc_ic;
3836a5bc1d1SSean Bruno 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3846a5bc1d1SSean Bruno 	struct ieee80211_frame *wh = (struct ieee80211_frame *)preq->buf;
3856a5bc1d1SSean Bruno 	struct ieee80211_rateset *rs;
3866a5bc1d1SSean Bruno 	size_t remain = sizeof(preq->buf);
3876a5bc1d1SSean Bruno 	uint8_t *frm, *pos;
3886a5bc1d1SSean Bruno 
3896a5bc1d1SSean Bruno 	memset(preq, 0, sizeof(*preq));
3906a5bc1d1SSean Bruno 
3916a5bc1d1SSean Bruno 	/* Ensure enough space for header and SSID IE. */
3929afea60fSAndriy Voskoboinyk 	if (remain < sizeof(*wh) + 2)
3936a5bc1d1SSean Bruno 		return ENOBUFS;
3946a5bc1d1SSean Bruno 
3956a5bc1d1SSean Bruno 	/*
3966a5bc1d1SSean Bruno 	 * Build a probe request frame.  Most of the following code is a
3976a5bc1d1SSean Bruno 	 * copy & paste of what is done in net80211.
3986a5bc1d1SSean Bruno 	 */
3996a5bc1d1SSean Bruno 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4006a5bc1d1SSean Bruno 	    IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4016a5bc1d1SSean Bruno 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
4026a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
4036a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr2, vap ? vap->iv_myaddr : ic->ic_macaddr);
4046a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
4056a5bc1d1SSean Bruno 	*(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
4066a5bc1d1SSean Bruno 	*(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
4076a5bc1d1SSean Bruno 
4086a5bc1d1SSean Bruno 	frm = (uint8_t *)(wh + 1);
4099afea60fSAndriy Voskoboinyk 	frm = ieee80211_add_ssid(frm, NULL, 0);
4106a5bc1d1SSean Bruno 
4116a5bc1d1SSean Bruno 	/* Tell the firmware where the MAC header is. */
4126a5bc1d1SSean Bruno 	preq->mac_header.offset = 0;
4136a5bc1d1SSean Bruno 	preq->mac_header.len = htole16(frm - (uint8_t *)wh);
4146a5bc1d1SSean Bruno 	remain -= frm - (uint8_t *)wh;
4156a5bc1d1SSean Bruno 
4166a5bc1d1SSean Bruno 	/* Fill in 2GHz IEs and tell firmware where they are. */
4176a5bc1d1SSean Bruno 	rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4186a5bc1d1SSean Bruno 	if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
4196a5bc1d1SSean Bruno 		if (remain < 4 + rs->rs_nrates)
4206a5bc1d1SSean Bruno 			return ENOBUFS;
4216a5bc1d1SSean Bruno 	} else if (remain < 2 + rs->rs_nrates) {
4226a5bc1d1SSean Bruno 		return ENOBUFS;
4236a5bc1d1SSean Bruno 	}
4246a5bc1d1SSean Bruno 	preq->band_data[0].offset = htole16(frm - (uint8_t *)wh);
4256a5bc1d1SSean Bruno 	pos = frm;
4266a5bc1d1SSean Bruno 	frm = ieee80211_add_rates(frm, rs);
4276a5bc1d1SSean Bruno 	if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4286a5bc1d1SSean Bruno 		frm = ieee80211_add_xrates(frm, rs);
4296a5bc1d1SSean Bruno 	preq->band_data[0].len = htole16(frm - pos);
4306a5bc1d1SSean Bruno 	remain -= frm - pos;
4316a5bc1d1SSean Bruno 
432e7065dd1SMark Johnston 	if (iwm_rrm_scan_needed(sc)) {
4336a5bc1d1SSean Bruno 		if (remain < 3)
4346a5bc1d1SSean Bruno 			return ENOBUFS;
4356a5bc1d1SSean Bruno 		*frm++ = IEEE80211_ELEMID_DSPARMS;
4366a5bc1d1SSean Bruno 		*frm++ = 1;
4376a5bc1d1SSean Bruno 		*frm++ = 0;
4386a5bc1d1SSean Bruno 		remain -= 3;
4396a5bc1d1SSean Bruno 	}
4406a5bc1d1SSean Bruno 
4416349bdb3SAdrian Chadd 	if (sc->nvm_data->sku_cap_band_52GHz_enable) {
4426a5bc1d1SSean Bruno 		/* Fill in 5GHz IEs. */
4436a5bc1d1SSean Bruno 		rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4446a5bc1d1SSean Bruno 		if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
4456a5bc1d1SSean Bruno 			if (remain < 4 + rs->rs_nrates)
4466a5bc1d1SSean Bruno 				return ENOBUFS;
4476a5bc1d1SSean Bruno 		} else if (remain < 2 + rs->rs_nrates) {
4486a5bc1d1SSean Bruno 			return ENOBUFS;
4496a5bc1d1SSean Bruno 		}
4506a5bc1d1SSean Bruno 		preq->band_data[1].offset = htole16(frm - (uint8_t *)wh);
4516a5bc1d1SSean Bruno 		pos = frm;
4526a5bc1d1SSean Bruno 		frm = ieee80211_add_rates(frm, rs);
4536a5bc1d1SSean Bruno 		if (rs->rs_nrates > IEEE80211_RATE_SIZE)
4546a5bc1d1SSean Bruno 			frm = ieee80211_add_xrates(frm, rs);
4556a5bc1d1SSean Bruno 		preq->band_data[1].len = htole16(frm - pos);
4566a5bc1d1SSean Bruno 		remain -= frm - pos;
4576a5bc1d1SSean Bruno 	}
4586a5bc1d1SSean Bruno 
4596a5bc1d1SSean Bruno 	/* Send 11n IEs on both 2GHz and 5GHz bands. */
4606a5bc1d1SSean Bruno 	preq->common_data.offset = htole16(frm - (uint8_t *)wh);
4616a5bc1d1SSean Bruno 	pos = frm;
4626a5bc1d1SSean Bruno #if 0
4636a5bc1d1SSean Bruno 	if (ic->ic_flags & IEEE80211_F_HTON) {
4646a5bc1d1SSean Bruno 		if (remain < 28)
4656a5bc1d1SSean Bruno 			return ENOBUFS;
4666a5bc1d1SSean Bruno 		frm = ieee80211_add_htcaps(frm, ic);
4676a5bc1d1SSean Bruno 		/* XXX add WME info? */
4686a5bc1d1SSean Bruno 	}
4696a5bc1d1SSean Bruno #endif
4706a5bc1d1SSean Bruno 	preq->common_data.len = htole16(frm - pos);
4716a5bc1d1SSean Bruno 
4726a5bc1d1SSean Bruno 	return 0;
473d4886179SRui Paulo }
474d4886179SRui Paulo 
475d4886179SRui Paulo int
iwm_config_umac_scan(struct iwm_softc * sc)476e7065dd1SMark Johnston iwm_config_umac_scan(struct iwm_softc *sc)
477d4886179SRui Paulo {
478bc6cd01dSAdrian Chadd 	struct ieee80211com *ic = &sc->sc_ic;
479bc6cd01dSAdrian Chadd 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
480d4886179SRui Paulo 
4816a5bc1d1SSean Bruno 	struct iwm_scan_config *scan_config;
4826a5bc1d1SSean Bruno 	int ret, j, nchan;
4836a5bc1d1SSean Bruno 	size_t cmd_size;
4846a5bc1d1SSean Bruno 	struct ieee80211_channel *c;
4856a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
4866a5bc1d1SSean Bruno 		.id = iwm_cmd_id(IWM_SCAN_CFG_CMD, IWM_ALWAYS_LONG_GROUP, 0),
4876a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
4886a5bc1d1SSean Bruno 	};
4896a5bc1d1SSean Bruno 	static const uint32_t rates = (IWM_SCAN_CONFIG_RATE_1M |
4906a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_2M | IWM_SCAN_CONFIG_RATE_5M |
4916a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_11M | IWM_SCAN_CONFIG_RATE_6M |
4926a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_9M | IWM_SCAN_CONFIG_RATE_12M |
4936a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_18M | IWM_SCAN_CONFIG_RATE_24M |
4946a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_36M | IWM_SCAN_CONFIG_RATE_48M |
4956a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_RATE_54M);
4966a5bc1d1SSean Bruno 
497f33c8309SKyle Evans 	cmd_size = sizeof(*scan_config) + sc->sc_fw.ucode_capa.n_scan_channels;
4986a5bc1d1SSean Bruno 
4996a5bc1d1SSean Bruno 	scan_config = malloc(cmd_size, M_DEVBUF, M_NOWAIT | M_ZERO);
5006a5bc1d1SSean Bruno 	if (scan_config == NULL)
5016a5bc1d1SSean Bruno 		return ENOMEM;
5026a5bc1d1SSean Bruno 
503e7065dd1SMark Johnston 	scan_config->tx_chains = htole32(iwm_get_valid_tx_ant(sc));
504e7065dd1SMark Johnston 	scan_config->rx_chains = htole32(iwm_get_valid_rx_ant(sc));
5056a5bc1d1SSean Bruno 	scan_config->legacy_rates = htole32(rates |
5066a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_SUPPORTED_RATE(rates));
5076a5bc1d1SSean Bruno 
5086a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
5096a5bc1d1SSean Bruno 	scan_config->dwell_active = 10;
5106a5bc1d1SSean Bruno 	scan_config->dwell_passive = 110;
5116a5bc1d1SSean Bruno 	scan_config->dwell_fragmented = 44;
5126a5bc1d1SSean Bruno 	scan_config->dwell_extended = 90;
5136a5bc1d1SSean Bruno 	scan_config->out_of_channel_time = htole32(0);
5146a5bc1d1SSean Bruno 	scan_config->suspend_time = htole32(0);
5156a5bc1d1SSean Bruno 
5166a5bc1d1SSean Bruno 	IEEE80211_ADDR_COPY(scan_config->mac_addr,
5176a5bc1d1SSean Bruno 	    vap ? vap->iv_myaddr : ic->ic_macaddr);
5186a5bc1d1SSean Bruno 
5196a5bc1d1SSean Bruno 	scan_config->bcast_sta_id = sc->sc_aux_sta.sta_id;
5206a5bc1d1SSean Bruno 	scan_config->channel_flags = IWM_CHANNEL_FLAG_EBS |
5216a5bc1d1SSean Bruno 	    IWM_CHANNEL_FLAG_ACCURATE_EBS | IWM_CHANNEL_FLAG_EBS_ADD |
5226a5bc1d1SSean Bruno 	    IWM_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
5236a5bc1d1SSean Bruno 
5246a5bc1d1SSean Bruno 	for (nchan = j = 0;
525f33c8309SKyle Evans 	    j < ic->ic_nchans && nchan < sc->sc_fw.ucode_capa.n_scan_channels;
526f33c8309SKyle Evans 	    j++) {
5276a5bc1d1SSean Bruno 		c = &ic->ic_channels[j];
5286a5bc1d1SSean Bruno 		/* For 2GHz, only populate 11b channels */
5296a5bc1d1SSean Bruno 		/* For 5GHz, only populate 11a channels */
5306a5bc1d1SSean Bruno 		/*
5316a5bc1d1SSean Bruno 		 * Catch other channels, in case we have 900MHz channels or
5326a5bc1d1SSean Bruno 		 * something in the chanlist.
5336a5bc1d1SSean Bruno 		 */
534e7065dd1SMark Johnston 		if (iwm_scan_skip_channel(c))
5356a5bc1d1SSean Bruno 			continue;
5366a5bc1d1SSean Bruno 		scan_config->channel_array[nchan++] =
5376a5bc1d1SSean Bruno 		    ieee80211_mhz2ieee(c->ic_freq, 0);
5386a5bc1d1SSean Bruno 	}
5396a5bc1d1SSean Bruno 
5406a5bc1d1SSean Bruno 	scan_config->flags = htole32(IWM_SCAN_CONFIG_FLAG_ACTIVATE |
5416a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
5426a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_TX_CHAINS |
5436a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_RX_CHAINS |
5446a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
5456a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_ALL_TIMES |
5466a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
5476a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_MAC_ADDR |
5486a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
5496a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_N_CHANNELS(nchan) |
5506a5bc1d1SSean Bruno 	    IWM_SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
5516a5bc1d1SSean Bruno 
5526a5bc1d1SSean Bruno 	hcmd.data[0] = scan_config;
5536a5bc1d1SSean Bruno 	hcmd.len[0] = cmd_size;
5546a5bc1d1SSean Bruno 
5556a5bc1d1SSean Bruno 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending UMAC scan config\n");
5566a5bc1d1SSean Bruno 
5576a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
5586a5bc1d1SSean Bruno 	if (!ret)
5596a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
5606a5bc1d1SSean Bruno 		    "UMAC scan config was sent successfully\n");
5616a5bc1d1SSean Bruno 
5626a5bc1d1SSean Bruno 	free(scan_config, M_DEVBUF);
5636a5bc1d1SSean Bruno 	return ret;
5646a5bc1d1SSean Bruno }
5656a5bc1d1SSean Bruno 
566cd20383eSAdrian Chadd static boolean_t
iwm_scan_use_ebs(struct iwm_softc * sc)567e7065dd1SMark Johnston iwm_scan_use_ebs(struct iwm_softc *sc)
568cd20383eSAdrian Chadd {
569f33c8309SKyle Evans 	const struct iwm_ucode_capabilities *capa = &sc->sc_fw.ucode_capa;
570cd20383eSAdrian Chadd 
571cd20383eSAdrian Chadd 	/* We can only use EBS if:
572cd20383eSAdrian Chadd 	 *	1. the feature is supported;
573cd20383eSAdrian Chadd 	 *	2. the last EBS was successful;
574cd20383eSAdrian Chadd 	 *	3. if only single scan, the single scan EBS API is supported;
575cd20383eSAdrian Chadd 	 *	4. it's not a p2p find operation.
576cd20383eSAdrian Chadd 	 */
577cd20383eSAdrian Chadd 	return ((capa->flags & IWM_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
578cd20383eSAdrian Chadd 		sc->last_ebs_successful);
579cd20383eSAdrian Chadd }
580cd20383eSAdrian Chadd 
581666c8655SMark Johnston static int
iwm_scan_size(struct iwm_softc * sc)582e7065dd1SMark Johnston iwm_scan_size(struct iwm_softc *sc)
583666c8655SMark Johnston {
584666c8655SMark Johnston 	int base_size;
585666c8655SMark Johnston 
586666c8655SMark Johnston 	if (iwm_fw_has_capa(sc, IWM_UCODE_TLV_CAPA_UMAC_SCAN)) {
587666c8655SMark Johnston 		if (iwm_fw_has_api(sc, IWM_UCODE_TLV_API_ADAPTIVE_DWELL))
588666c8655SMark Johnston 			base_size = IWM_SCAN_REQ_UMAC_SIZE_V7;
589666c8655SMark Johnston 		else
590666c8655SMark Johnston 			base_size = IWM_SCAN_REQ_UMAC_SIZE_V1;
591666c8655SMark Johnston 
592666c8655SMark Johnston 		return base_size +
593666c8655SMark Johnston 		    sizeof(struct iwm_scan_channel_cfg_umac) *
594666c8655SMark Johnston 		    sc->sc_fw.ucode_capa.n_scan_channels +
595355c1513SAdrian Chadd 		    sizeof(struct iwm_scan_req_umac_tail_v1);
596666c8655SMark Johnston 	} else {
597666c8655SMark Johnston 		return sizeof(struct iwm_scan_req_lmac) +
598666c8655SMark Johnston 		    sizeof(struct iwm_scan_channel_cfg_lmac) *
599666c8655SMark Johnston 		    sc->sc_fw.ucode_capa.n_scan_channels +
600355c1513SAdrian Chadd 		    sizeof(struct iwm_scan_probe_req_v1);
601666c8655SMark Johnston 	}
602666c8655SMark Johnston }
603666c8655SMark Johnston 
6046a5bc1d1SSean Bruno int
iwm_umac_scan(struct iwm_softc * sc)605e7065dd1SMark Johnston iwm_umac_scan(struct iwm_softc *sc)
6066a5bc1d1SSean Bruno {
6076a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
6086a5bc1d1SSean Bruno 		.id = iwm_cmd_id(IWM_SCAN_REQ_UMAC, IWM_ALWAYS_LONG_GROUP, 0),
6096a5bc1d1SSean Bruno 		.len = { 0, },
6106a5bc1d1SSean Bruno 		.data = { NULL, },
6116a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
6126a5bc1d1SSean Bruno 	};
6139afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
6146a5bc1d1SSean Bruno 	struct iwm_scan_req_umac *req;
615355c1513SAdrian Chadd 	struct iwm_scan_req_umac_tail_v1 *tail;
6166a5bc1d1SSean Bruno 	size_t req_len;
617666c8655SMark Johnston 	uint16_t general_flags;
618666c8655SMark Johnston 	uint8_t channel_flags, i, nssid;
6196a5bc1d1SSean Bruno 	int ret;
6206a5bc1d1SSean Bruno 
621e7065dd1SMark Johnston 	req_len = iwm_scan_size(sc);
6226a5bc1d1SSean Bruno 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
6236a5bc1d1SSean Bruno 		return ENOMEM;
6246a5bc1d1SSean Bruno 	req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
6256a5bc1d1SSean Bruno 	if (req == NULL)
6266a5bc1d1SSean Bruno 		return ENOMEM;
6276a5bc1d1SSean Bruno 
6286a5bc1d1SSean Bruno 	hcmd.len[0] = (uint16_t)req_len;
6296a5bc1d1SSean Bruno 	hcmd.data[0] = (void *)req;
6306a5bc1d1SSean Bruno 
6316a5bc1d1SSean Bruno 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Handling ieee80211 scan request\n");
6326a5bc1d1SSean Bruno 
633666c8655SMark Johnston 	nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
6346a5bc1d1SSean Bruno 
635666c8655SMark Johnston 	general_flags = IWM_UMAC_SCAN_GEN_FLAGS_PASS_ALL |
636666c8655SMark Johnston 	    IWM_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
637666c8655SMark Johnston 	if (!iwm_fw_has_api(sc, IWM_UCODE_TLV_API_ADAPTIVE_DWELL))
638666c8655SMark Johnston 		general_flags |= IWM_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
639e7065dd1SMark Johnston 	if (iwm_rrm_scan_needed(sc))
640666c8655SMark Johnston 		general_flags |= IWM_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
641666c8655SMark Johnston 	if (nssid != 0)
642666c8655SMark Johnston 		general_flags |= IWM_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
643666c8655SMark Johnston 	else
644666c8655SMark Johnston 		general_flags |= IWM_UMAC_SCAN_GEN_FLAGS_PASSIVE;
645666c8655SMark Johnston 
646666c8655SMark Johnston 	channel_flags = 0;
647e7065dd1SMark Johnston 	if (iwm_scan_use_ebs(sc))
648666c8655SMark Johnston 		channel_flags = IWM_SCAN_CHANNEL_FLAG_EBS |
649666c8655SMark Johnston 		    IWM_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
650666c8655SMark Johnston 		    IWM_SCAN_CHANNEL_FLAG_CACHE_ADD;
651666c8655SMark Johnston 
652666c8655SMark Johnston 	req->general_flags = htole16(general_flags);
6536a5bc1d1SSean Bruno 	req->ooc_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
6546a5bc1d1SSean Bruno 
655666c8655SMark Johnston 	/* These timings correspond to iwlwifi's UNASSOC scan. */
656666c8655SMark Johnston 	if (iwm_fw_has_api(sc, IWM_UCODE_TLV_API_ADAPTIVE_DWELL)) {
657666c8655SMark Johnston 		req->v7.active_dwell = 10;
658666c8655SMark Johnston 		req->v7.passive_dwell = 110;
659666c8655SMark Johnston 		req->v7.fragmented_dwell = 44;
660666c8655SMark Johnston 		req->v7.adwell_default_n_aps_social = 10;
661666c8655SMark Johnston 		req->v7.adwell_default_n_aps = 2;
662666c8655SMark Johnston 		req->v7.adwell_max_budget = htole16(300);
663666c8655SMark Johnston 		req->v7.scan_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
664666c8655SMark Johnston 		req->v7.channel.flags = channel_flags;
665e7065dd1SMark Johnston 		req->v7.channel.count = iwm_umac_scan_fill_channels(sc,
666666c8655SMark Johnston 		    (struct iwm_scan_channel_cfg_umac *)req->v7.data, nssid);
6676a5bc1d1SSean Bruno 
668666c8655SMark Johnston 		tail = (void *)((char *)&req->v7.data +
6696a5bc1d1SSean Bruno 		    sizeof(struct iwm_scan_channel_cfg_umac) *
670f33c8309SKyle Evans 		    sc->sc_fw.ucode_capa.n_scan_channels);
671666c8655SMark Johnston 	} else {
672666c8655SMark Johnston 		req->v1.active_dwell = 10;
673666c8655SMark Johnston 		req->v1.passive_dwell = 110;
674666c8655SMark Johnston 		req->v1.fragmented_dwell = 44;
675666c8655SMark Johnston 		req->v1.extended_dwell = 90;
676666c8655SMark Johnston 		req->v1.scan_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
677666c8655SMark Johnston 		req->v1.channel.flags = channel_flags;
678e7065dd1SMark Johnston 		req->v1.channel.count = iwm_umac_scan_fill_channels(sc,
679666c8655SMark Johnston 		    (struct iwm_scan_channel_cfg_umac *)req->v1.data, nssid);
680666c8655SMark Johnston 
681666c8655SMark Johnston 		tail = (void *)((char *)&req->v1.data +
682666c8655SMark Johnston 		    sizeof(struct iwm_scan_channel_cfg_umac) *
683666c8655SMark Johnston 		    sc->sc_fw.ucode_capa.n_scan_channels);
684666c8655SMark Johnston 	}
6856a5bc1d1SSean Bruno 
6866a5bc1d1SSean Bruno 	/* Check if we're doing an active directed scan. */
6879afea60fSAndriy Voskoboinyk 	for (i = 0; i < nssid; i++) {
6889afea60fSAndriy Voskoboinyk 		tail->direct_scan[i].id = IEEE80211_ELEMID_SSID;
6899afea60fSAndriy Voskoboinyk 		tail->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
6909afea60fSAndriy Voskoboinyk 		    IEEE80211_NWID_LEN);
6919afea60fSAndriy Voskoboinyk 		memcpy(tail->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
6929afea60fSAndriy Voskoboinyk 		    tail->direct_scan[i].len);
6939afea60fSAndriy Voskoboinyk 		/* XXX debug */
6949afea60fSAndriy Voskoboinyk 	}
6956a5bc1d1SSean Bruno 
696e7065dd1SMark Johnston 	ret = iwm_fill_probe_req(sc, &tail->preq);
6976a5bc1d1SSean Bruno 	if (ret) {
6986a5bc1d1SSean Bruno 		free(req, M_DEVBUF);
6996a5bc1d1SSean Bruno 		return ret;
7006a5bc1d1SSean Bruno 	}
7016a5bc1d1SSean Bruno 
7026a5bc1d1SSean Bruno 	/* Specify the scan plan: We'll do one iteration. */
7036a5bc1d1SSean Bruno 	tail->schedule[0].interval = 0;
7046a5bc1d1SSean Bruno 	tail->schedule[0].iter_count = 1;
7056a5bc1d1SSean Bruno 
7066a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
7076a5bc1d1SSean Bruno 	if (!ret)
7086a5bc1d1SSean Bruno 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
7096a5bc1d1SSean Bruno 		    "Scan request was sent successfully\n");
7106a5bc1d1SSean Bruno 	free(req, M_DEVBUF);
7116a5bc1d1SSean Bruno 	return ret;
7126a5bc1d1SSean Bruno }
7136a5bc1d1SSean Bruno 
7146a5bc1d1SSean Bruno int
iwm_lmac_scan(struct iwm_softc * sc)715e7065dd1SMark Johnston iwm_lmac_scan(struct iwm_softc *sc)
7166a5bc1d1SSean Bruno {
7176a5bc1d1SSean Bruno 	struct iwm_host_cmd hcmd = {
7186a5bc1d1SSean Bruno 		.id = IWM_SCAN_OFFLOAD_REQUEST_CMD,
7196a5bc1d1SSean Bruno 		.len = { 0, },
7206a5bc1d1SSean Bruno 		.data = { NULL, },
7216a5bc1d1SSean Bruno 		.flags = IWM_CMD_SYNC,
7226a5bc1d1SSean Bruno 	};
7239afea60fSAndriy Voskoboinyk 	struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
7246a5bc1d1SSean Bruno 	struct iwm_scan_req_lmac *req;
7256a5bc1d1SSean Bruno 	size_t req_len;
7269afea60fSAndriy Voskoboinyk 	uint8_t i, nssid;
7276a5bc1d1SSean Bruno 	int ret;
728d4886179SRui Paulo 
729d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
730d4886179SRui Paulo 	    "Handling ieee80211 scan request\n");
731d4886179SRui Paulo 
732e7065dd1SMark Johnston 	req_len = iwm_scan_size(sc);
7336a5bc1d1SSean Bruno 	if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
7346a5bc1d1SSean Bruno 		return ENOMEM;
7356a5bc1d1SSean Bruno 	req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
7366a5bc1d1SSean Bruno 	if (req == NULL)
7376a5bc1d1SSean Bruno 		return ENOMEM;
738d4886179SRui Paulo 
7396a5bc1d1SSean Bruno 	hcmd.len[0] = (uint16_t)req_len;
7406a5bc1d1SSean Bruno 	hcmd.data[0] = (void *)req;
741d4886179SRui Paulo 
7426a5bc1d1SSean Bruno 	/* These timings correspond to iwlwifi's UNASSOC scan. */
7436a5bc1d1SSean Bruno 	req->active_dwell = 10;
7446a5bc1d1SSean Bruno 	req->passive_dwell = 110;
7456a5bc1d1SSean Bruno 	req->fragmented_dwell = 44;
7466a5bc1d1SSean Bruno 	req->extended_dwell = 90;
7476a5bc1d1SSean Bruno 	req->max_out_time = 0;
7486a5bc1d1SSean Bruno 	req->suspend_time = 0;
749d4886179SRui Paulo 
7506a5bc1d1SSean Bruno 	req->scan_prio = htole32(IWM_SCAN_PRIORITY_HIGH);
751e7065dd1SMark Johnston 	req->rx_chain_select = iwm_scan_rx_chain(sc);
7526a5bc1d1SSean Bruno 	req->iter_num = htole32(1);
7536a5bc1d1SSean Bruno 	req->delay = 0;
7546a5bc1d1SSean Bruno 
755e7065dd1SMark Johnston 	req->scan_flags = htole32(IWM_LMAC_SCAN_FLAG_PASS_ALL |
756e7065dd1SMark Johnston 	    IWM_LMAC_SCAN_FLAG_ITER_COMPLETE |
757e7065dd1SMark Johnston 	    IWM_LMAC_SCAN_FLAG_EXTENDED_DWELL);
758e7065dd1SMark Johnston 	if (iwm_rrm_scan_needed(sc))
759e7065dd1SMark Johnston 		req->scan_flags |= htole32(IWM_LMAC_SCAN_FLAGS_RRM_ENABLED);
7606a5bc1d1SSean Bruno 
761e7065dd1SMark Johnston 	req->flags = iwm_scan_rxon_flags(sc->sc_ic.ic_scan->ss_chans[0]);
762fe845d8eSAdrian Chadd 
7636a5bc1d1SSean Bruno 	req->filter_flags =
7646a5bc1d1SSean Bruno 	    htole32(IWM_MAC_FILTER_ACCEPT_GRP | IWM_MAC_FILTER_IN_BEACON);
7656a5bc1d1SSean Bruno 
7666a5bc1d1SSean Bruno 	/* Tx flags 2 GHz. */
7676a5bc1d1SSean Bruno 	req->tx_cmd[0].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
768d4886179SRui Paulo 	    IWM_TX_CMD_FLG_BT_DIS);
7696a5bc1d1SSean Bruno 	req->tx_cmd[0].rate_n_flags =
770e7065dd1SMark Johnston 	    iwm_scan_rate_n_flags(sc, IEEE80211_CHAN_2GHZ, 1/*XXX*/);
7716a5bc1d1SSean Bruno 	req->tx_cmd[0].sta_id = sc->sc_aux_sta.sta_id;
772d4886179SRui Paulo 
7736a5bc1d1SSean Bruno 	/* Tx flags 5 GHz. */
7746a5bc1d1SSean Bruno 	req->tx_cmd[1].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
7756a5bc1d1SSean Bruno 	    IWM_TX_CMD_FLG_BT_DIS);
7766a5bc1d1SSean Bruno 	req->tx_cmd[1].rate_n_flags =
777e7065dd1SMark Johnston 	    iwm_scan_rate_n_flags(sc, IEEE80211_CHAN_5GHZ, 1/*XXX*/);
7786a5bc1d1SSean Bruno 	req->tx_cmd[1].sta_id = sc->sc_aux_sta.sta_id;
779d4886179SRui Paulo 
7806a5bc1d1SSean Bruno 	/* Check if we're doing an active directed scan. */
7819afea60fSAndriy Voskoboinyk 	nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
7829afea60fSAndriy Voskoboinyk 	for (i = 0; i < nssid; i++) {
7839afea60fSAndriy Voskoboinyk 		req->direct_scan[i].id = IEEE80211_ELEMID_SSID;
7849afea60fSAndriy Voskoboinyk 		req->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
7859afea60fSAndriy Voskoboinyk 		    IEEE80211_NWID_LEN);
7869afea60fSAndriy Voskoboinyk 		memcpy(req->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
7879afea60fSAndriy Voskoboinyk 		    req->direct_scan[i].len);
7889afea60fSAndriy Voskoboinyk 		/* XXX debug */
7896a5bc1d1SSean Bruno 	}
7909afea60fSAndriy Voskoboinyk 	if (nssid != 0) {
7919afea60fSAndriy Voskoboinyk 		req->scan_flags |=
792e7065dd1SMark Johnston 		    htole32(IWM_LMAC_SCAN_FLAG_PRE_CONNECTION);
7939afea60fSAndriy Voskoboinyk 	} else
794e7065dd1SMark Johnston 		req->scan_flags |= htole32(IWM_LMAC_SCAN_FLAG_PASSIVE);
795d4886179SRui Paulo 
796e7065dd1SMark Johnston 	req->n_channels = iwm_lmac_scan_fill_channels(sc,
7979afea60fSAndriy Voskoboinyk 	    (struct iwm_scan_channel_cfg_lmac *)req->data, nssid);
798d4886179SRui Paulo 
799e7065dd1SMark Johnston 	ret = iwm_fill_probe_req(sc,
800355c1513SAdrian Chadd 			    (struct iwm_scan_probe_req_v1 *)(req->data +
8016a5bc1d1SSean Bruno 			    (sizeof(struct iwm_scan_channel_cfg_lmac) *
802f33c8309SKyle Evans 			    sc->sc_fw.ucode_capa.n_scan_channels)));
8036a5bc1d1SSean Bruno 	if (ret) {
8046a5bc1d1SSean Bruno 		free(req, M_DEVBUF);
8056a5bc1d1SSean Bruno 		return ret;
8066a5bc1d1SSean Bruno 	}
8076a5bc1d1SSean Bruno 
8086a5bc1d1SSean Bruno 	/* Specify the scan plan: We'll do one iteration. */
8096a5bc1d1SSean Bruno 	req->schedule[0].iterations = 1;
8106a5bc1d1SSean Bruno 	req->schedule[0].full_scan_mul = 1;
8116a5bc1d1SSean Bruno 
812e7065dd1SMark Johnston 	if (iwm_scan_use_ebs(sc)) {
813cd20383eSAdrian Chadd 		req->channel_opt[0].flags =
814cd20383eSAdrian Chadd 			htole16(IWM_SCAN_CHANNEL_FLAG_EBS |
815cd20383eSAdrian Chadd 				IWM_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
816cd20383eSAdrian Chadd 				IWM_SCAN_CHANNEL_FLAG_CACHE_ADD);
817cd20383eSAdrian Chadd 		req->channel_opt[0].non_ebs_ratio =
818cd20383eSAdrian Chadd 			htole16(IWM_DENSE_EBS_SCAN_RATIO);
819cd20383eSAdrian Chadd 		req->channel_opt[1].flags =
820cd20383eSAdrian Chadd 			htole16(IWM_SCAN_CHANNEL_FLAG_EBS |
821cd20383eSAdrian Chadd 				IWM_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
822cd20383eSAdrian Chadd 				IWM_SCAN_CHANNEL_FLAG_CACHE_ADD);
823cd20383eSAdrian Chadd 		req->channel_opt[1].non_ebs_ratio =
824cd20383eSAdrian Chadd 			htole16(IWM_SPARSE_EBS_SCAN_RATIO);
825cd20383eSAdrian Chadd 	}
8266a5bc1d1SSean Bruno 
8276a5bc1d1SSean Bruno 	ret = iwm_send_cmd(sc, &hcmd);
8286a5bc1d1SSean Bruno 	if (!ret) {
829d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
830d4886179SRui Paulo 		    "Scan request was sent successfully\n");
831d4886179SRui Paulo 	}
8326a5bc1d1SSean Bruno 	free(req, M_DEVBUF);
833d4886179SRui Paulo 	return ret;
834d4886179SRui Paulo }
8354b17c188SAdrian Chadd 
8364b17c188SAdrian Chadd static int
iwm_lmac_scan_abort(struct iwm_softc * sc)837e7065dd1SMark Johnston iwm_lmac_scan_abort(struct iwm_softc *sc)
8384b17c188SAdrian Chadd {
8394b17c188SAdrian Chadd 	int ret;
8404b17c188SAdrian Chadd 	struct iwm_host_cmd hcmd = {
8414b17c188SAdrian Chadd 		.id = IWM_SCAN_OFFLOAD_ABORT_CMD,
8424b17c188SAdrian Chadd 		.len = { 0, },
8434b17c188SAdrian Chadd 		.data = { NULL, },
8444b17c188SAdrian Chadd 		.flags = IWM_CMD_SYNC,
8454b17c188SAdrian Chadd 	};
8464b17c188SAdrian Chadd 	uint32_t status;
8474b17c188SAdrian Chadd 
848e7065dd1SMark Johnston 	ret = iwm_send_cmd_status(sc, &hcmd, &status);
8494b17c188SAdrian Chadd 	if (ret)
8504b17c188SAdrian Chadd 		return ret;
8514b17c188SAdrian Chadd 
8524b17c188SAdrian Chadd 	if (status != IWM_CAN_ABORT_STATUS) {
8534b17c188SAdrian Chadd 		/*
8544b17c188SAdrian Chadd 		 * The scan abort will return 1 for success or
8554b17c188SAdrian Chadd 		 * 2 for "failure".  A failure condition can be
8564b17c188SAdrian Chadd 		 * due to simply not being in an active scan which
8574b17c188SAdrian Chadd 		 * can occur if we send the scan abort before the
8584b17c188SAdrian Chadd 		 * microcode has notified us that a scan is completed.
8594b17c188SAdrian Chadd 		 */
8604b17c188SAdrian Chadd 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
8614b17c188SAdrian Chadd 		    "SCAN OFFLOAD ABORT ret %d.\n", status);
8624b17c188SAdrian Chadd 		ret = ENOENT;
8634b17c188SAdrian Chadd 	}
8644b17c188SAdrian Chadd 
8654b17c188SAdrian Chadd 	return ret;
8664b17c188SAdrian Chadd }
8674b17c188SAdrian Chadd 
8684b17c188SAdrian Chadd static int
iwm_umac_scan_abort(struct iwm_softc * sc)869e7065dd1SMark Johnston iwm_umac_scan_abort(struct iwm_softc *sc)
8704b17c188SAdrian Chadd {
8714b17c188SAdrian Chadd 	struct iwm_umac_scan_abort cmd = {};
8724b17c188SAdrian Chadd 	int uid, ret;
8734b17c188SAdrian Chadd 
8744b17c188SAdrian Chadd 	uid = 0;
8754b17c188SAdrian Chadd 	cmd.uid = htole32(uid);
8764b17c188SAdrian Chadd 
8774b17c188SAdrian Chadd 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending scan abort, uid %u\n", uid);
8784b17c188SAdrian Chadd 
879e7065dd1SMark Johnston 	ret = iwm_send_cmd_pdu(sc,
8804b17c188SAdrian Chadd 				   iwm_cmd_id(IWM_SCAN_ABORT_UMAC,
8814b17c188SAdrian Chadd 					      IWM_ALWAYS_LONG_GROUP, 0),
8824b17c188SAdrian Chadd 				   0, sizeof(cmd), &cmd);
8834b17c188SAdrian Chadd 
8844b17c188SAdrian Chadd 	return ret;
8854b17c188SAdrian Chadd }
8864b17c188SAdrian Chadd 
8874b17c188SAdrian Chadd int
iwm_scan_stop_wait(struct iwm_softc * sc)888e7065dd1SMark Johnston iwm_scan_stop_wait(struct iwm_softc *sc)
8894b17c188SAdrian Chadd {
8904b17c188SAdrian Chadd 	struct iwm_notification_wait wait_scan_done;
8914b17c188SAdrian Chadd 	static const uint16_t scan_done_notif[] = { IWM_SCAN_COMPLETE_UMAC,
8924b17c188SAdrian Chadd 						   IWM_SCAN_OFFLOAD_COMPLETE, };
8934b17c188SAdrian Chadd 	int ret;
8944b17c188SAdrian Chadd 
8954b17c188SAdrian Chadd 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_scan_done,
8964b17c188SAdrian Chadd 				   scan_done_notif, nitems(scan_done_notif),
8974b17c188SAdrian Chadd 				   NULL, NULL);
8984b17c188SAdrian Chadd 
8994b17c188SAdrian Chadd 	IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Preparing to stop scan\n");
9004b17c188SAdrian Chadd 
901eff8a979SMark Johnston 	if (iwm_fw_has_capa(sc, IWM_UCODE_TLV_CAPA_UMAC_SCAN))
902e7065dd1SMark Johnston 		ret = iwm_umac_scan_abort(sc);
9034b17c188SAdrian Chadd 	else
904e7065dd1SMark Johnston 		ret = iwm_lmac_scan_abort(sc);
9054b17c188SAdrian Chadd 
9064b17c188SAdrian Chadd 	if (ret) {
9074b17c188SAdrian Chadd 		IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "couldn't stop scan\n");
9084b17c188SAdrian Chadd 		iwm_remove_notification(sc->sc_notif_wait, &wait_scan_done);
9094b17c188SAdrian Chadd 		return ret;
9104b17c188SAdrian Chadd 	}
9114b17c188SAdrian Chadd 
9124b17c188SAdrian Chadd 	IWM_UNLOCK(sc);
9134b17c188SAdrian Chadd 	ret = iwm_wait_notification(sc->sc_notif_wait, &wait_scan_done, hz);
9144b17c188SAdrian Chadd 	IWM_LOCK(sc);
9154b17c188SAdrian Chadd 
9164b17c188SAdrian Chadd 	return ret;
9174b17c188SAdrian Chadd }
918