xref: /linux/net/mac80211/scan.c (revision d98ad83ee86e523cc00cbf425f456fbd14b4fdc4)
10a51b27eSJohannes Berg /*
25484e237SJohannes Berg  * Scanning implementation
35484e237SJohannes Berg  *
40a51b27eSJohannes Berg  * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
50a51b27eSJohannes Berg  * Copyright 2004, Instant802 Networks, Inc.
60a51b27eSJohannes Berg  * Copyright 2005, Devicescape Software, Inc.
70a51b27eSJohannes Berg  * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
80a51b27eSJohannes Berg  * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9*d98ad83eSJohannes Berg  * Copyright 2013-2014  Intel Mobile Communications GmbH
100a51b27eSJohannes Berg  *
110a51b27eSJohannes Berg  * This program is free software; you can redistribute it and/or modify
120a51b27eSJohannes Berg  * it under the terms of the GNU General Public License version 2 as
130a51b27eSJohannes Berg  * published by the Free Software Foundation.
140a51b27eSJohannes Berg  */
150a51b27eSJohannes Berg 
160a51b27eSJohannes Berg #include <linux/if_arp.h>
17888d04dfSFelix Fietkau #include <linux/etherdevice.h>
18078e1e60SJohannes Berg #include <linux/rtnetlink.h>
19e8db0be1SJean Pihet #include <linux/pm_qos.h>
20df13cce5SHelmut Schaa #include <net/sch_generic.h>
215a0e3ad6STejun Heo #include <linux/slab.h>
22bc3b2d7fSPaul Gortmaker #include <linux/export.h>
230a51b27eSJohannes Berg #include <net/mac80211.h>
240a51b27eSJohannes Berg 
250a51b27eSJohannes Berg #include "ieee80211_i.h"
2624487981SJohannes Berg #include "driver-ops.h"
275484e237SJohannes Berg #include "mesh.h"
280a51b27eSJohannes Berg 
290a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33)
300a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33)
313f892b61SStanislaw Gruszka #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
320a51b27eSJohannes Berg 
335484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local,
34c2b13452SJohannes Berg 			  struct ieee80211_bss *bss)
355484e237SJohannes Berg {
360c1ad2caSJohannes Berg 	if (!bss)
370c1ad2caSJohannes Berg 		return;
385b112d3dSJohannes Berg 	cfg80211_put_bss(local->hw.wiphy,
395b112d3dSJohannes Berg 			 container_of((void *)bss, struct cfg80211_bss, priv));
405484e237SJohannes Berg }
415484e237SJohannes Berg 
42ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems)
43ab13315aSKalle Valo {
44ab13315aSKalle Valo 	u8 qos_info;
45ab13315aSKalle Valo 
46ab13315aSKalle Valo 	if (elems->wmm_info && elems->wmm_info_len == 7
47ab13315aSKalle Valo 	    && elems->wmm_info[5] == 1)
48ab13315aSKalle Valo 		qos_info = elems->wmm_info[6];
49ab13315aSKalle Valo 	else if (elems->wmm_param && elems->wmm_param_len == 24
50ab13315aSKalle Valo 		 && elems->wmm_param[5] == 1)
51ab13315aSKalle Valo 		qos_info = elems->wmm_param[6];
52ab13315aSKalle Valo 	else
53ab13315aSKalle Valo 		/* no valid wmm information or parameter element found */
54ab13315aSKalle Valo 		return false;
55ab13315aSKalle Valo 
56ab13315aSKalle Valo 	return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
57ab13315aSKalle Valo }
58ab13315aSKalle Valo 
59c2b13452SJohannes Berg struct ieee80211_bss *
605484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local,
615484e237SJohannes Berg 			  struct ieee80211_rx_status *rx_status,
62d45c4172SEmmanuel Grumbach 			  struct ieee80211_mgmt *mgmt, size_t len,
635484e237SJohannes Berg 			  struct ieee802_11_elems *elems,
64d45c4172SEmmanuel Grumbach 			  struct ieee80211_channel *channel)
655484e237SJohannes Berg {
66d45c4172SEmmanuel Grumbach 	bool beacon = ieee80211_is_beacon(mgmt->frame_control);
670c1ad2caSJohannes Berg 	struct cfg80211_bss *cbss;
68c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
69f0b058b6SStanislaw Gruszka 	int clen, srlen;
707ca15a0aSSimon Wunderlich 	enum nl80211_bss_scan_width scan_width;
712a519311SJohannes Berg 	s32 signal = 0;
722a519311SJohannes Berg 
7377965c97SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
742a519311SJohannes Berg 		signal = rx_status->signal * 100;
7577965c97SJohannes Berg 	else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
762a519311SJohannes Berg 		signal = (rx_status->signal * 100) / local->hw.max_signal;
772a519311SJohannes Berg 
787ca15a0aSSimon Wunderlich 	scan_width = NL80211_BSS_CHAN_WIDTH_20;
797ca15a0aSSimon Wunderlich 	if (rx_status->flag & RX_FLAG_5MHZ)
807ca15a0aSSimon Wunderlich 		scan_width = NL80211_BSS_CHAN_WIDTH_5;
817ca15a0aSSimon Wunderlich 	if (rx_status->flag & RX_FLAG_10MHZ)
827ca15a0aSSimon Wunderlich 		scan_width = NL80211_BSS_CHAN_WIDTH_10;
837ca15a0aSSimon Wunderlich 
847ca15a0aSSimon Wunderlich 	cbss = cfg80211_inform_bss_width_frame(local->hw.wiphy, channel,
857ca15a0aSSimon Wunderlich 					       scan_width, mgmt, len, signal,
867ca15a0aSSimon Wunderlich 					       GFP_ATOMIC);
870c1ad2caSJohannes Berg 	if (!cbss)
885484e237SJohannes Berg 		return NULL;
8900d3f14cSJohannes Berg 
900c1ad2caSJohannes Berg 	bss = (void *)cbss->priv;
915484e237SJohannes Berg 
92ef429dadSJohannes Berg 	if (beacon)
93ef429dadSJohannes Berg 		bss->device_ts_beacon = rx_status->device_timestamp;
94ef429dadSJohannes Berg 	else
95ef429dadSJohannes Berg 		bss->device_ts_presp = rx_status->device_timestamp;
968c358bcdSJohannes Berg 
97fcff4f10SPaul Stewart 	if (elems->parse_error) {
98fcff4f10SPaul Stewart 		if (beacon)
99fcff4f10SPaul Stewart 			bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
100fcff4f10SPaul Stewart 		else
101fcff4f10SPaul Stewart 			bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
102fcff4f10SPaul Stewart 	} else {
103fcff4f10SPaul Stewart 		if (beacon)
104fcff4f10SPaul Stewart 			bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
105fcff4f10SPaul Stewart 		else
106fcff4f10SPaul Stewart 			bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
1075484e237SJohannes Berg 	}
1085484e237SJohannes Berg 
109fcff4f10SPaul Stewart 	/* save the ERP value so that it is available at association time */
1101946bed9SJohannes Berg 	if (elems->erp_info && (!elems->parse_error ||
111fcff4f10SPaul Stewart 				!(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
112fcff4f10SPaul Stewart 		bss->erp_value = elems->erp_info[0];
113fcff4f10SPaul Stewart 		bss->has_erp_value = true;
114fcff4f10SPaul Stewart 		if (!elems->parse_error)
115fcff4f10SPaul Stewart 			bss->valid_data |= IEEE80211_BSS_VALID_ERP;
116fcff4f10SPaul Stewart 	}
117fcff4f10SPaul Stewart 
118f0b058b6SStanislaw Gruszka 	/* replace old supported rates if we get new values */
119fcff4f10SPaul Stewart 	if (!elems->parse_error ||
120fcff4f10SPaul Stewart 	    !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
121f0b058b6SStanislaw Gruszka 		srlen = 0;
1225484e237SJohannes Berg 		if (elems->supp_rates) {
123f0b058b6SStanislaw Gruszka 			clen = IEEE80211_MAX_SUPP_RATES;
1245484e237SJohannes Berg 			if (clen > elems->supp_rates_len)
1255484e237SJohannes Berg 				clen = elems->supp_rates_len;
126f0b058b6SStanislaw Gruszka 			memcpy(bss->supp_rates, elems->supp_rates, clen);
127f0b058b6SStanislaw Gruszka 			srlen += clen;
1285484e237SJohannes Berg 		}
1295484e237SJohannes Berg 		if (elems->ext_supp_rates) {
130f0b058b6SStanislaw Gruszka 			clen = IEEE80211_MAX_SUPP_RATES - srlen;
1315484e237SJohannes Berg 			if (clen > elems->ext_supp_rates_len)
1325484e237SJohannes Berg 				clen = elems->ext_supp_rates_len;
133fcff4f10SPaul Stewart 			memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
134fcff4f10SPaul Stewart 			       clen);
135f0b058b6SStanislaw Gruszka 			srlen += clen;
1365484e237SJohannes Berg 		}
137fcff4f10SPaul Stewart 		if (srlen) {
138f0b058b6SStanislaw Gruszka 			bss->supp_rates_len = srlen;
139fcff4f10SPaul Stewart 			if (!elems->parse_error)
140fcff4f10SPaul Stewart 				bss->valid_data |= IEEE80211_BSS_VALID_RATES;
141fcff4f10SPaul Stewart 		}
142fcff4f10SPaul Stewart 	}
1435484e237SJohannes Berg 
144fcff4f10SPaul Stewart 	if (!elems->parse_error ||
145fcff4f10SPaul Stewart 	    !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
1465484e237SJohannes Berg 		bss->wmm_used = elems->wmm_param || elems->wmm_info;
147ab13315aSKalle Valo 		bss->uapsd_supported = is_uapsd_supported(elems);
148fcff4f10SPaul Stewart 		if (!elems->parse_error)
149fcff4f10SPaul Stewart 			bss->valid_data |= IEEE80211_BSS_VALID_WMM;
150fcff4f10SPaul Stewart 	}
1515484e237SJohannes Berg 
152817cee76SAlexander Bondar 	if (beacon) {
153817cee76SAlexander Bondar 		struct ieee80211_supported_band *sband =
154817cee76SAlexander Bondar 			local->hw.wiphy->bands[rx_status->band];
155817cee76SAlexander Bondar 		if (!(rx_status->flag & RX_FLAG_HT) &&
156817cee76SAlexander Bondar 		    !(rx_status->flag & RX_FLAG_VHT))
157817cee76SAlexander Bondar 			bss->beacon_rate =
158817cee76SAlexander Bondar 				&sband->bitrates[rx_status->rate_idx];
159817cee76SAlexander Bondar 	}
160817cee76SAlexander Bondar 
1615484e237SJohannes Berg 	return bss;
1625484e237SJohannes Berg }
1630a51b27eSJohannes Berg 
164d48b2968SJohannes Berg void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
16598c8fccfSJohannes Berg {
166f1d58c25SJohannes Berg 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
167d48b2968SJohannes Berg 	struct ieee80211_sub_if_data *sdata1, *sdata2;
168d48b2968SJohannes Berg 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
169c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
17098c8fccfSJohannes Berg 	u8 *elements;
17198c8fccfSJohannes Berg 	struct ieee80211_channel *channel;
17298c8fccfSJohannes Berg 	size_t baselen;
17398c8fccfSJohannes Berg 	struct ieee802_11_elems elems;
17498c8fccfSJohannes Berg 
175d48b2968SJohannes Berg 	if (skb->len < 24 ||
176d48b2968SJohannes Berg 	    (!ieee80211_is_probe_resp(mgmt->frame_control) &&
177d48b2968SJohannes Berg 	     !ieee80211_is_beacon(mgmt->frame_control)))
178d48b2968SJohannes Berg 		return;
17998c8fccfSJohannes Berg 
180d48b2968SJohannes Berg 	sdata1 = rcu_dereference(local->scan_sdata);
181d48b2968SJohannes Berg 	sdata2 = rcu_dereference(local->sched_scan_sdata);
18298c8fccfSJohannes Berg 
183d48b2968SJohannes Berg 	if (likely(!sdata1 && !sdata2))
184d48b2968SJohannes Berg 		return;
18598c8fccfSJohannes Berg 
186d48b2968SJohannes Berg 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
18798c8fccfSJohannes Berg 		/* ignore ProbeResp to foreign address */
188d48b2968SJohannes Berg 		if ((!sdata1 || !ether_addr_equal(mgmt->da, sdata1->vif.addr)) &&
189d48b2968SJohannes Berg 		    (!sdata2 || !ether_addr_equal(mgmt->da, sdata2->vif.addr)))
190d48b2968SJohannes Berg 			return;
19198c8fccfSJohannes Berg 
19298c8fccfSJohannes Berg 		elements = mgmt->u.probe_resp.variable;
19398c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
19498c8fccfSJohannes Berg 	} else {
19598c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
19698c8fccfSJohannes Berg 		elements = mgmt->u.beacon.variable;
19798c8fccfSJohannes Berg 	}
19898c8fccfSJohannes Berg 
19998c8fccfSJohannes Berg 	if (baselen > skb->len)
200d48b2968SJohannes Berg 		return;
20198c8fccfSJohannes Berg 
202b2e506bfSJohannes Berg 	ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems);
20398c8fccfSJohannes Berg 
2040172bb75SJohannes Berg 	channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
20598c8fccfSJohannes Berg 
20698c8fccfSJohannes Berg 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
207d48b2968SJohannes Berg 		return;
20898c8fccfSJohannes Berg 
209d48b2968SJohannes Berg 	bss = ieee80211_bss_info_update(local, rx_status,
21098c8fccfSJohannes Berg 					mgmt, skb->len, &elems,
211d45c4172SEmmanuel Grumbach 					channel);
212d048e503SJouni Malinen 	if (bss)
213d48b2968SJohannes Berg 		ieee80211_rx_bss_put(local, bss);
21498c8fccfSJohannes Berg }
21598c8fccfSJohannes Berg 
2162103dec1SSimon Wunderlich static void
2172103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
2182103dec1SSimon Wunderlich 			       enum nl80211_bss_scan_width scan_width)
2192103dec1SSimon Wunderlich {
2202103dec1SSimon Wunderlich 	memset(chandef, 0, sizeof(*chandef));
2212103dec1SSimon Wunderlich 	switch (scan_width) {
2222103dec1SSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_5:
2232103dec1SSimon Wunderlich 		chandef->width = NL80211_CHAN_WIDTH_5;
2242103dec1SSimon Wunderlich 		break;
2252103dec1SSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_10:
2262103dec1SSimon Wunderlich 		chandef->width = NL80211_CHAN_WIDTH_10;
2272103dec1SSimon Wunderlich 		break;
2282103dec1SSimon Wunderlich 	default:
2292103dec1SSimon Wunderlich 		chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2302103dec1SSimon Wunderlich 		break;
2312103dec1SSimon Wunderlich 	}
2322103dec1SSimon Wunderlich }
2332103dec1SSimon Wunderlich 
2344d36ec58SJohannes Berg /* return false if no more work */
2354d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
2364d36ec58SJohannes Berg {
2374d36ec58SJohannes Berg 	struct cfg80211_scan_request *req = local->scan_req;
2382103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
239c56ef672SDavid Spinadel 	u8 bands_used = 0;
2404d36ec58SJohannes Berg 	int i, ielen, n_chans;
2414d36ec58SJohannes Berg 
242a754055aSEmmanuel Grumbach 	if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
243a754055aSEmmanuel Grumbach 		return false;
244a754055aSEmmanuel Grumbach 
245c56ef672SDavid Spinadel 	if (local->hw.flags & IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS) {
246c56ef672SDavid Spinadel 		for (i = 0; i < req->n_channels; i++) {
247c56ef672SDavid Spinadel 			local->hw_scan_req->req.channels[i] = req->channels[i];
248c56ef672SDavid Spinadel 			bands_used |= BIT(req->channels[i]->band);
249c56ef672SDavid Spinadel 		}
250c56ef672SDavid Spinadel 
251c56ef672SDavid Spinadel 		n_chans = req->n_channels;
252c56ef672SDavid Spinadel 	} else {
2534d36ec58SJohannes Berg 		do {
2544d36ec58SJohannes Berg 			if (local->hw_scan_band == IEEE80211_NUM_BANDS)
2554d36ec58SJohannes Berg 				return false;
2564d36ec58SJohannes Berg 
2574d36ec58SJohannes Berg 			n_chans = 0;
258c56ef672SDavid Spinadel 
2594d36ec58SJohannes Berg 			for (i = 0; i < req->n_channels; i++) {
260c56ef672SDavid Spinadel 				if (req->channels[i]->band !=
261c56ef672SDavid Spinadel 				    local->hw_scan_band)
262c56ef672SDavid Spinadel 					continue;
263c56ef672SDavid Spinadel 				local->hw_scan_req->req.channels[n_chans] =
2644d36ec58SJohannes Berg 							req->channels[i];
2654d36ec58SJohannes Berg 				n_chans++;
266c56ef672SDavid Spinadel 				bands_used |= BIT(req->channels[i]->band);
2674d36ec58SJohannes Berg 			}
2684d36ec58SJohannes Berg 
2694d36ec58SJohannes Berg 			local->hw_scan_band++;
2704d36ec58SJohannes Berg 		} while (!n_chans);
271c56ef672SDavid Spinadel 	}
2724d36ec58SJohannes Berg 
273c56ef672SDavid Spinadel 	local->hw_scan_req->req.n_channels = n_chans;
2742103dec1SSimon Wunderlich 	ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
2754d36ec58SJohannes Berg 
276c56ef672SDavid Spinadel 	ielen = ieee80211_build_preq_ies(local,
277c56ef672SDavid Spinadel 					 (u8 *)local->hw_scan_req->req.ie,
278c604b9f2SJohannes Berg 					 local->hw_scan_ies_bufsize,
279c56ef672SDavid Spinadel 					 &local->hw_scan_req->ies,
280c56ef672SDavid Spinadel 					 req->ie, req->ie_len,
281c56ef672SDavid Spinadel 					 bands_used, req->rates, &chandef);
282c56ef672SDavid Spinadel 	local->hw_scan_req->req.ie_len = ielen;
283c56ef672SDavid Spinadel 	local->hw_scan_req->req.no_cck = req->no_cck;
2844d36ec58SJohannes Berg 
2854d36ec58SJohannes Berg 	return true;
2864d36ec58SJohannes Berg }
2874d36ec58SJohannes Berg 
2888bd2a248SEliad Peller static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
289f3b85252SJohannes Berg {
290f3b85252SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
2918bd2a248SEliad Peller 	bool hw_scan = local->ops->hw_scan;
292a2b70e83SEliad Peller 	bool was_scanning = local->scanning;
293f3b85252SJohannes Berg 
294e229f844SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
295f3b85252SJohannes Berg 
2966d3560d4SJohannes Berg 	/*
2976d3560d4SJohannes Berg 	 * It's ok to abort a not-yet-running scan (that
2986d3560d4SJohannes Berg 	 * we have one at all will be verified by checking
2996d3560d4SJohannes Berg 	 * local->scan_req next), but not to complete it
3006d3560d4SJohannes Berg 	 * successfully.
3016d3560d4SJohannes Berg 	 */
3026d3560d4SJohannes Berg 	if (WARN_ON(!local->scanning && !aborted))
3036d3560d4SJohannes Berg 		aborted = true;
304f3b85252SJohannes Berg 
305e229f844SStanislaw Gruszka 	if (WARN_ON(!local->scan_req))
306d07bfd8bSJohannes Berg 		return;
307f3b85252SJohannes Berg 
308c56ef672SDavid Spinadel 	if (hw_scan && !aborted &&
309c56ef672SDavid Spinadel 	    !(local->hw.flags & IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS) &&
310c56ef672SDavid Spinadel 	    ieee80211_prep_hw_scan(local)) {
311e2fd5dbcSJohannes Berg 		int rc;
312e2fd5dbcSJohannes Berg 
313e2fd5dbcSJohannes Berg 		rc = drv_hw_scan(local,
314e2fd5dbcSJohannes Berg 			rcu_dereference_protected(local->scan_sdata,
315e2fd5dbcSJohannes Berg 						  lockdep_is_held(&local->mtx)),
316e2fd5dbcSJohannes Berg 			local->hw_scan_req);
317e2fd5dbcSJohannes Berg 
3186eb11a9aSStanislaw Gruszka 		if (rc == 0)
319d07bfd8bSJohannes Berg 			return;
3204d36ec58SJohannes Berg 	}
3214d36ec58SJohannes Berg 
3224d36ec58SJohannes Berg 	kfree(local->hw_scan_req);
3234d36ec58SJohannes Berg 	local->hw_scan_req = NULL;
324f3b85252SJohannes Berg 
3255ba63533SJohannes Berg 	if (local->scan_req != local->int_scan_req)
3262a519311SJohannes Berg 		cfg80211_scan_done(local->scan_req, aborted);
3272a519311SJohannes Berg 	local->scan_req = NULL;
3280c2bef46SMonam Agarwal 	RCU_INIT_POINTER(local->scan_sdata, NULL);
3292a519311SJohannes Berg 
330fbe9c429SHelmut Schaa 	local->scanning = 0;
3317ca15a0aSSimon Wunderlich 	local->scan_chandef.chan = NULL;
332f3b85252SJohannes Berg 
33359bdf3b0SBen Greear 	/* Set power back to normal operating levels. */
33459bdf3b0SBen Greear 	ieee80211_hw_config(local, 0);
335b23b025fSBen Greear 
3368bd2a248SEliad Peller 	if (!hw_scan) {
337b23b025fSBen Greear 		ieee80211_configure_filter(local);
338b23b025fSBen Greear 		drv_sw_scan_complete(local);
339aacde9eeSStanislaw Gruszka 		ieee80211_offchannel_return(local);
340b23b025fSBen Greear 	}
341b23b025fSBen Greear 
3425cff20e6SJohannes Berg 	ieee80211_recalc_idle(local);
343e229f844SStanislaw Gruszka 
3440a51b27eSJohannes Berg 	ieee80211_mlme_notify_scan_completed(local);
34546900298SJohannes Berg 	ieee80211_ibss_notify_scan_completed(local);
346472dbc45SJohannes Berg 	ieee80211_mesh_notify_scan_completed(local);
347a2b70e83SEliad Peller 	if (was_scanning)
3482eb278e0SJohannes Berg 		ieee80211_start_next_roc(local);
3490a51b27eSJohannes Berg }
3508789d459SJohannes Berg 
3518789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
3528789d459SJohannes Berg {
3538789d459SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
3548789d459SJohannes Berg 
3558789d459SJohannes Berg 	trace_api_scan_completed(local, aborted);
3568789d459SJohannes Berg 
3578789d459SJohannes Berg 	set_bit(SCAN_COMPLETED, &local->scanning);
3588789d459SJohannes Berg 	if (aborted)
3598789d459SJohannes Berg 		set_bit(SCAN_ABORTED, &local->scanning);
3608789d459SJohannes Berg 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
3618789d459SJohannes Berg }
3620a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed);
3630a51b27eSJohannes Berg 
364f3b85252SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local)
365f3b85252SJohannes Berg {
366fe57d9f5SJohannes Berg 	/* Software scan is not supported in multi-channel cases */
367fe57d9f5SJohannes Berg 	if (local->use_chanctx)
368fe57d9f5SJohannes Berg 		return -EOPNOTSUPP;
369fe57d9f5SJohannes Berg 
370f3b85252SJohannes Berg 	/*
371f3b85252SJohannes Berg 	 * Hardware/driver doesn't support hw_scan, so use software
372f3b85252SJohannes Berg 	 * scanning instead. First send a nullfunc frame with power save
373f3b85252SJohannes Berg 	 * bit on so that AP will buffer the frames for us while we are not
374f3b85252SJohannes Berg 	 * listening, then send probe requests to each channel and wait for
375f3b85252SJohannes Berg 	 * the responses. After all channels are scanned, tune back to the
376f3b85252SJohannes Berg 	 * original channel and send a nullfunc frame with power save bit
377f3b85252SJohannes Berg 	 * off to trigger the AP to send us all the buffered frames.
378f3b85252SJohannes Berg 	 *
379f3b85252SJohannes Berg 	 * Note that while local->sw_scanning is true everything else but
380f3b85252SJohannes Berg 	 * nullfunc frames and probe requests will be dropped in
381f3b85252SJohannes Berg 	 * ieee80211_tx_h_check_assoc().
382f3b85252SJohannes Berg 	 */
38324487981SJohannes Berg 	drv_sw_scan_start(local);
384f3b85252SJohannes Berg 
385de312db3SRajkumar Manoharan 	local->leave_oper_channel_time = jiffies;
386977923b0SHelmut Schaa 	local->next_scan_state = SCAN_DECISION;
387f3b85252SJohannes Berg 	local->scan_channel_idx = 0;
388f3b85252SJohannes Berg 
389aacde9eeSStanislaw Gruszka 	ieee80211_offchannel_stop_vifs(local);
390a80f7c0bSJohannes Berg 
3919c35d7d2SSeth Forshee 	/* ensure nullfunc is transmitted before leaving operating channel */
39239ecc01dSJohannes Berg 	ieee80211_flush_queues(local, NULL);
3939c35d7d2SSeth Forshee 
3943ac64beeSJohannes Berg 	ieee80211_configure_filter(local);
395f3b85252SJohannes Berg 
39659bdf3b0SBen Greear 	/* We need to set power level at maximum rate for scanning. */
39759bdf3b0SBen Greear 	ieee80211_hw_config(local, 0);
39859bdf3b0SBen Greear 
39942935ecaSLuis R. Rodriguez 	ieee80211_queue_delayed_work(&local->hw,
40007ef03eeSJohannes Berg 				     &local->scan_work, 0);
401f3b85252SJohannes Berg 
402f3b85252SJohannes Berg 	return 0;
403f3b85252SJohannes Berg }
404f3b85252SJohannes Berg 
405133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local,
406133d40f9SStanislaw Gruszka 			       struct ieee80211_sub_if_data *sdata)
407133d40f9SStanislaw Gruszka {
408164eb02dSSimon Wunderlich 	if (local->radar_detect_enabled)
409164eb02dSSimon Wunderlich 		return false;
410164eb02dSSimon Wunderlich 
4112eb278e0SJohannes Berg 	if (!list_empty(&local->roc_list))
412133d40f9SStanislaw Gruszka 		return false;
413133d40f9SStanislaw Gruszka 
414133d40f9SStanislaw Gruszka 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
415392b9ffbSStanislaw Gruszka 	    sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
416133d40f9SStanislaw Gruszka 		return false;
417133d40f9SStanislaw Gruszka 
418133d40f9SStanislaw Gruszka 	return true;
419133d40f9SStanislaw Gruszka }
420133d40f9SStanislaw Gruszka 
421133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local)
422133d40f9SStanislaw Gruszka {
423133d40f9SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
424133d40f9SStanislaw Gruszka 
425133d40f9SStanislaw Gruszka 	if (!local->scan_req || local->scanning)
426133d40f9SStanislaw Gruszka 		return;
427133d40f9SStanislaw Gruszka 
428e2fd5dbcSJohannes Berg 	if (!ieee80211_can_scan(local,
429e2fd5dbcSJohannes Berg 				rcu_dereference_protected(
430e2fd5dbcSJohannes Berg 					local->scan_sdata,
431e2fd5dbcSJohannes Berg 					lockdep_is_held(&local->mtx))))
432133d40f9SStanislaw Gruszka 		return;
433133d40f9SStanislaw Gruszka 
434133d40f9SStanislaw Gruszka 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
435133d40f9SStanislaw Gruszka 				     round_jiffies_relative(0));
436133d40f9SStanislaw Gruszka }
437f3b85252SJohannes Berg 
4388a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
4398a690674SBen Greear 					    unsigned long *next_delay)
4408a690674SBen Greear {
4418a690674SBen Greear 	int i;
442e2fd5dbcSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
443675a0b04SKarl Beldan 	enum ieee80211_band band = local->hw.conf.chandef.chan->band;
4446c17b77bSSeth Forshee 	u32 tx_flags;
4456c17b77bSSeth Forshee 
4466c17b77bSSeth Forshee 	tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
4476c17b77bSSeth Forshee 	if (local->scan_req->no_cck)
4486c17b77bSSeth Forshee 		tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
4498a690674SBen Greear 
450e2fd5dbcSJohannes Berg 	sdata = rcu_dereference_protected(local->scan_sdata,
451316b6b5dSPeter Senna Tschudin 					  lockdep_is_held(&local->mtx));
452e2fd5dbcSJohannes Berg 
4538a690674SBen Greear 	for (i = 0; i < local->scan_req->n_ssids; i++)
4548a690674SBen Greear 		ieee80211_send_probe_req(
4558a690674SBen Greear 			sdata, NULL,
4568a690674SBen Greear 			local->scan_req->ssids[i].ssid,
4578a690674SBen Greear 			local->scan_req->ssids[i].ssid_len,
4588a690674SBen Greear 			local->scan_req->ie, local->scan_req->ie_len,
4598a690674SBen Greear 			local->scan_req->rates[band], false,
460675a0b04SKarl Beldan 			tx_flags, local->hw.conf.chandef.chan, true);
4618a690674SBen Greear 
4628a690674SBen Greear 	/*
4638a690674SBen Greear 	 * After sending probe requests, wait for probe responses
4648a690674SBen Greear 	 * on the channel.
4658a690674SBen Greear 	 */
4668a690674SBen Greear 	*next_delay = IEEE80211_CHANNEL_TIME;
4678a690674SBen Greear 	local->next_scan_state = SCAN_DECISION;
4688a690674SBen Greear }
4698a690674SBen Greear 
470f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
471f3b85252SJohannes Berg 				  struct cfg80211_scan_request *req)
472f3b85252SJohannes Berg {
473f3b85252SJohannes Berg 	struct ieee80211_local *local = sdata->local;
474f3b85252SJohannes Berg 	int rc;
475f3b85252SJohannes Berg 
476e229f844SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
477e229f844SStanislaw Gruszka 
478f3b85252SJohannes Berg 	if (local->scan_req)
479f3b85252SJohannes Berg 		return -EBUSY;
480f3b85252SJohannes Berg 
481133d40f9SStanislaw Gruszka 	if (!ieee80211_can_scan(local, sdata)) {
4826e7e6213SJohn W. Linville 		/* wait for the work to finish/time out */
483c0ce77b8SJohannes Berg 		local->scan_req = req;
484e2fd5dbcSJohannes Berg 		rcu_assign_pointer(local->scan_sdata, sdata);
485c0ce77b8SJohannes Berg 		return 0;
486c0ce77b8SJohannes Berg 	}
487c0ce77b8SJohannes Berg 
488f3b85252SJohannes Berg 	if (local->ops->hw_scan) {
489f3b85252SJohannes Berg 		u8 *ies;
490f3b85252SJohannes Berg 
491e4dcbb37SDavid Spinadel 		local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
492c56ef672SDavid Spinadel 
493c56ef672SDavid Spinadel 		if (local->hw.flags & IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS) {
494c56ef672SDavid Spinadel 			int i, n_bands = 0;
495c56ef672SDavid Spinadel 			u8 bands_counted = 0;
496c56ef672SDavid Spinadel 
497c56ef672SDavid Spinadel 			for (i = 0; i < req->n_channels; i++) {
498c56ef672SDavid Spinadel 				if (bands_counted & BIT(req->channels[i]->band))
499c56ef672SDavid Spinadel 					continue;
500c56ef672SDavid Spinadel 				bands_counted |= BIT(req->channels[i]->band);
501c56ef672SDavid Spinadel 				n_bands++;
502c56ef672SDavid Spinadel 			}
503c56ef672SDavid Spinadel 
504c56ef672SDavid Spinadel 			local->hw_scan_ies_bufsize *= n_bands;
505c56ef672SDavid Spinadel 		}
506c56ef672SDavid Spinadel 
5074d36ec58SJohannes Berg 		local->hw_scan_req = kmalloc(
5084d36ec58SJohannes Berg 				sizeof(*local->hw_scan_req) +
5094d36ec58SJohannes Berg 				req->n_channels * sizeof(req->channels[0]) +
510c604b9f2SJohannes Berg 				local->hw_scan_ies_bufsize, GFP_KERNEL);
5114d36ec58SJohannes Berg 		if (!local->hw_scan_req)
512f3b85252SJohannes Berg 			return -ENOMEM;
513f3b85252SJohannes Berg 
514c56ef672SDavid Spinadel 		local->hw_scan_req->req.ssids = req->ssids;
515c56ef672SDavid Spinadel 		local->hw_scan_req->req.n_ssids = req->n_ssids;
5164d36ec58SJohannes Berg 		ies = (u8 *)local->hw_scan_req +
5174d36ec58SJohannes Berg 			sizeof(*local->hw_scan_req) +
5184d36ec58SJohannes Berg 			req->n_channels * sizeof(req->channels[0]);
519c56ef672SDavid Spinadel 		local->hw_scan_req->req.ie = ies;
520c56ef672SDavid Spinadel 		local->hw_scan_req->req.flags = req->flags;
5214d36ec58SJohannes Berg 
5224d36ec58SJohannes Berg 		local->hw_scan_band = 0;
5236e7e6213SJohn W. Linville 
5246e7e6213SJohn W. Linville 		/*
5256e7e6213SJohn W. Linville 		 * After allocating local->hw_scan_req, we must
5266e7e6213SJohn W. Linville 		 * go through until ieee80211_prep_hw_scan(), so
5276e7e6213SJohn W. Linville 		 * anything that might be changed here and leave
5286e7e6213SJohn W. Linville 		 * this function early must not go after this
5296e7e6213SJohn W. Linville 		 * allocation.
5306e7e6213SJohn W. Linville 		 */
531f3b85252SJohannes Berg 	}
532f3b85252SJohannes Berg 
533f3b85252SJohannes Berg 	local->scan_req = req;
534e2fd5dbcSJohannes Berg 	rcu_assign_pointer(local->scan_sdata, sdata);
535f3b85252SJohannes Berg 
5368a690674SBen Greear 	if (local->ops->hw_scan) {
537fbe9c429SHelmut Schaa 		__set_bit(SCAN_HW_SCANNING, &local->scanning);
5388a690674SBen Greear 	} else if ((req->n_channels == 1) &&
539675a0b04SKarl Beldan 		   (req->channels[0] == local->_oper_chandef.chan)) {
5409b864870SJohannes Berg 		/*
5419b864870SJohannes Berg 		 * If we are scanning only on the operating channel
5429b864870SJohannes Berg 		 * then we do not need to stop normal activities
5438a690674SBen Greear 		 */
5448a690674SBen Greear 		unsigned long next_delay;
5458a690674SBen Greear 
5468a690674SBen Greear 		__set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
5478a690674SBen Greear 
5488a690674SBen Greear 		ieee80211_recalc_idle(local);
5498a690674SBen Greear 
5508a690674SBen Greear 		/* Notify driver scan is starting, keep order of operations
5518a690674SBen Greear 		 * same as normal software scan, in case that matters. */
5528a690674SBen Greear 		drv_sw_scan_start(local);
5538a690674SBen Greear 
5548a690674SBen Greear 		ieee80211_configure_filter(local); /* accept probe-responses */
5558a690674SBen Greear 
5568a690674SBen Greear 		/* We need to ensure power level is at max for scanning. */
5578a690674SBen Greear 		ieee80211_hw_config(local, 0);
5588a690674SBen Greear 
5598a690674SBen Greear 		if ((req->channels[0]->flags &
5608fe02e16SLuis R. Rodriguez 		     IEEE80211_CHAN_NO_IR) ||
5618a690674SBen Greear 		    !local->scan_req->n_ssids) {
5628a690674SBen Greear 			next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
5638a690674SBen Greear 		} else {
5648a690674SBen Greear 			ieee80211_scan_state_send_probe(local, &next_delay);
5658a690674SBen Greear 			next_delay = IEEE80211_CHANNEL_TIME;
5668a690674SBen Greear 		}
5678a690674SBen Greear 
5688a690674SBen Greear 		/* Now, just wait a bit and we are all done! */
5698a690674SBen Greear 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
5708a690674SBen Greear 					     next_delay);
5718a690674SBen Greear 		return 0;
5728a690674SBen Greear 	} else {
5738a690674SBen Greear 		/* Do normal software scan */
574fbe9c429SHelmut Schaa 		__set_bit(SCAN_SW_SCANNING, &local->scanning);
5758a690674SBen Greear 	}
5766e7e6213SJohn W. Linville 
5775cff20e6SJohannes Berg 	ieee80211_recalc_idle(local);
578f3b85252SJohannes Berg 
5794d36ec58SJohannes Berg 	if (local->ops->hw_scan) {
5804d36ec58SJohannes Berg 		WARN_ON(!ieee80211_prep_hw_scan(local));
581a060bbfeSJohannes Berg 		rc = drv_hw_scan(local, sdata, local->hw_scan_req);
5824d36ec58SJohannes Berg 	} else
583f3b85252SJohannes Berg 		rc = ieee80211_start_sw_scan(local);
584f3b85252SJohannes Berg 
585f3b85252SJohannes Berg 	if (rc) {
5864d36ec58SJohannes Berg 		kfree(local->hw_scan_req);
5874d36ec58SJohannes Berg 		local->hw_scan_req = NULL;
588fbe9c429SHelmut Schaa 		local->scanning = 0;
589f3b85252SJohannes Berg 
5905cff20e6SJohannes Berg 		ieee80211_recalc_idle(local);
5915cff20e6SJohannes Berg 
592f3b85252SJohannes Berg 		local->scan_req = NULL;
5930c2bef46SMonam Agarwal 		RCU_INIT_POINTER(local->scan_sdata, NULL);
594f3b85252SJohannes Berg 	}
595f3b85252SJohannes Berg 
596f3b85252SJohannes Berg 	return rc;
597f3b85252SJohannes Berg }
598f3b85252SJohannes Berg 
599df13cce5SHelmut Schaa static unsigned long
600df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
601df13cce5SHelmut Schaa {
602df13cce5SHelmut Schaa 	/*
603df13cce5SHelmut Schaa 	 * TODO: channel switching also consumes quite some time,
604df13cce5SHelmut Schaa 	 * add that delay as well to get a better estimation
605df13cce5SHelmut Schaa 	 */
6068fe02e16SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_NO_IR)
607df13cce5SHelmut Schaa 		return IEEE80211_PASSIVE_CHANNEL_TIME;
608df13cce5SHelmut Schaa 	return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
609df13cce5SHelmut Schaa }
610df13cce5SHelmut Schaa 
611e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local,
6122fb3f028SHelmut Schaa 					  unsigned long *next_delay)
6132fb3f028SHelmut Schaa {
614142b9f50SHelmut Schaa 	bool associated = false;
615df13cce5SHelmut Schaa 	bool tx_empty = true;
616df13cce5SHelmut Schaa 	bool bad_latency;
617142b9f50SHelmut Schaa 	struct ieee80211_sub_if_data *sdata;
618df13cce5SHelmut Schaa 	struct ieee80211_channel *next_chan;
619cd2bb512SSam Leffler 	enum mac80211_scan_state next_scan_state;
620142b9f50SHelmut Schaa 
621df13cce5SHelmut Schaa 	/*
622df13cce5SHelmut Schaa 	 * check if at least one STA interface is associated,
623df13cce5SHelmut Schaa 	 * check if at least one STA interface has pending tx frames
624df13cce5SHelmut Schaa 	 * and grab the lowest used beacon interval
625df13cce5SHelmut Schaa 	 */
626142b9f50SHelmut Schaa 	mutex_lock(&local->iflist_mtx);
627142b9f50SHelmut Schaa 	list_for_each_entry(sdata, &local->interfaces, list) {
6289607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
629142b9f50SHelmut Schaa 			continue;
630142b9f50SHelmut Schaa 
631142b9f50SHelmut Schaa 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
632142b9f50SHelmut Schaa 			if (sdata->u.mgd.associated) {
633142b9f50SHelmut Schaa 				associated = true;
634df13cce5SHelmut Schaa 
635df13cce5SHelmut Schaa 				if (!qdisc_all_tx_empty(sdata->dev)) {
636df13cce5SHelmut Schaa 					tx_empty = false;
637142b9f50SHelmut Schaa 					break;
638142b9f50SHelmut Schaa 				}
639142b9f50SHelmut Schaa 			}
640142b9f50SHelmut Schaa 		}
641df13cce5SHelmut Schaa 	}
642142b9f50SHelmut Schaa 	mutex_unlock(&local->iflist_mtx);
643142b9f50SHelmut Schaa 
644b23b025fSBen Greear 	next_chan = local->scan_req->channels[local->scan_channel_idx];
645b23b025fSBen Greear 
646142b9f50SHelmut Schaa 	/*
647142b9f50SHelmut Schaa 	 * we're currently scanning a different channel, let's
648df13cce5SHelmut Schaa 	 * see if we can scan another channel without interfering
649df13cce5SHelmut Schaa 	 * with the current traffic situation.
650df13cce5SHelmut Schaa 	 *
6513f892b61SStanislaw Gruszka 	 * Keep good latency, do not stay off-channel more than 125 ms.
652142b9f50SHelmut Schaa 	 */
653df13cce5SHelmut Schaa 
654df13cce5SHelmut Schaa 	bad_latency = time_after(jiffies +
655df13cce5SHelmut Schaa 				 ieee80211_scan_get_channel_time(next_chan),
6563f892b61SStanislaw Gruszka 				 local->leave_oper_channel_time + HZ / 8);
657df13cce5SHelmut Schaa 
658cd2bb512SSam Leffler 	if (associated && !tx_empty) {
659cd2bb512SSam Leffler 		if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
660cd2bb512SSam Leffler 			next_scan_state = SCAN_ABORT;
661142b9f50SHelmut Schaa 		else
662cd2bb512SSam Leffler 			next_scan_state = SCAN_SUSPEND;
6633f892b61SStanislaw Gruszka 	} else if (associated && bad_latency) {
664cd2bb512SSam Leffler 		next_scan_state = SCAN_SUSPEND;
665cd2bb512SSam Leffler 	} else {
666cd2bb512SSam Leffler 		next_scan_state = SCAN_SET_CHANNEL;
667cd2bb512SSam Leffler 	}
668cd2bb512SSam Leffler 
669cd2bb512SSam Leffler 	local->next_scan_state = next_scan_state;
670142b9f50SHelmut Schaa 
671142b9f50SHelmut Schaa 	*next_delay = 0;
6722fb3f028SHelmut Schaa }
6732fb3f028SHelmut Schaa 
6742fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
6757d3be3ccSHelmut Schaa 					     unsigned long *next_delay)
6767d3be3ccSHelmut Schaa {
6777d3be3ccSHelmut Schaa 	int skip;
6787d3be3ccSHelmut Schaa 	struct ieee80211_channel *chan;
6797ca15a0aSSimon Wunderlich 	enum nl80211_bss_scan_width oper_scan_width;
6807d3be3ccSHelmut Schaa 
6817d3be3ccSHelmut Schaa 	skip = 0;
6827d3be3ccSHelmut Schaa 	chan = local->scan_req->channels[local->scan_channel_idx];
6837d3be3ccSHelmut Schaa 
6847ca15a0aSSimon Wunderlich 	local->scan_chandef.chan = chan;
6857ca15a0aSSimon Wunderlich 	local->scan_chandef.center_freq1 = chan->center_freq;
6867ca15a0aSSimon Wunderlich 	local->scan_chandef.center_freq2 = 0;
6877ca15a0aSSimon Wunderlich 	switch (local->scan_req->scan_width) {
6887ca15a0aSSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_5:
6897ca15a0aSSimon Wunderlich 		local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
6907ca15a0aSSimon Wunderlich 		break;
6917ca15a0aSSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_10:
6927ca15a0aSSimon Wunderlich 		local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
6937ca15a0aSSimon Wunderlich 		break;
6947ca15a0aSSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_20:
6957ca15a0aSSimon Wunderlich 		/* If scanning on oper channel, use whatever channel-type
6967ca15a0aSSimon Wunderlich 		 * is currently in use.
6977ca15a0aSSimon Wunderlich 		 */
6987ca15a0aSSimon Wunderlich 		oper_scan_width = cfg80211_chandef_to_scan_width(
6997ca15a0aSSimon Wunderlich 					&local->_oper_chandef);
7007ca15a0aSSimon Wunderlich 		if (chan == local->_oper_chandef.chan &&
7017ca15a0aSSimon Wunderlich 		    oper_scan_width == local->scan_req->scan_width)
7027ca15a0aSSimon Wunderlich 			local->scan_chandef = local->_oper_chandef;
7037ca15a0aSSimon Wunderlich 		else
7047ca15a0aSSimon Wunderlich 			local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
7057ca15a0aSSimon Wunderlich 		break;
7067ca15a0aSSimon Wunderlich 	}
707b23b025fSBen Greear 
708584991dcSJohannes Berg 	if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
7097d3be3ccSHelmut Schaa 		skip = 1;
7107d3be3ccSHelmut Schaa 
7117d3be3ccSHelmut Schaa 	/* advance state machine to next channel/band */
7127d3be3ccSHelmut Schaa 	local->scan_channel_idx++;
7137d3be3ccSHelmut Schaa 
7140ee9c13cSHelmut Schaa 	if (skip) {
7150ee9c13cSHelmut Schaa 		/* if we skip this channel return to the decision state */
7160ee9c13cSHelmut Schaa 		local->next_scan_state = SCAN_DECISION;
7172fb3f028SHelmut Schaa 		return;
7180ee9c13cSHelmut Schaa 	}
7197d3be3ccSHelmut Schaa 
7207d3be3ccSHelmut Schaa 	/*
7217d3be3ccSHelmut Schaa 	 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
7227d3be3ccSHelmut Schaa 	 * (which unfortunately doesn't say _why_ step a) is done,
7237d3be3ccSHelmut Schaa 	 * but it waits for the probe delay or until a frame is
7247d3be3ccSHelmut Schaa 	 * received - and the received frame would update the NAV).
7257d3be3ccSHelmut Schaa 	 * For now, we do not support waiting until a frame is
7267d3be3ccSHelmut Schaa 	 * received.
7277d3be3ccSHelmut Schaa 	 *
7287d3be3ccSHelmut Schaa 	 * In any case, it is not necessary for a passive scan.
7297d3be3ccSHelmut Schaa 	 */
7308fe02e16SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_NO_IR ||
7317d3be3ccSHelmut Schaa 	    !local->scan_req->n_ssids) {
7327d3be3ccSHelmut Schaa 		*next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
733977923b0SHelmut Schaa 		local->next_scan_state = SCAN_DECISION;
7342fb3f028SHelmut Schaa 		return;
7357d3be3ccSHelmut Schaa 	}
7367d3be3ccSHelmut Schaa 
7372fb3f028SHelmut Schaa 	/* active scan, send probes */
7387d3be3ccSHelmut Schaa 	*next_delay = IEEE80211_PROBE_DELAY;
739977923b0SHelmut Schaa 	local->next_scan_state = SCAN_SEND_PROBE;
7407d3be3ccSHelmut Schaa }
7417d3be3ccSHelmut Schaa 
74207ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
74307ef03eeSJohannes Berg 					 unsigned long *next_delay)
74407ef03eeSJohannes Berg {
74507ef03eeSJohannes Berg 	/* switch back to the operating channel */
7467ca15a0aSSimon Wunderlich 	local->scan_chandef.chan = NULL;
74707ef03eeSJohannes Berg 	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
74807ef03eeSJohannes Berg 
749aacde9eeSStanislaw Gruszka 	/* disable PS */
750aacde9eeSStanislaw Gruszka 	ieee80211_offchannel_return(local);
75107ef03eeSJohannes Berg 
75207ef03eeSJohannes Berg 	*next_delay = HZ / 5;
75307ef03eeSJohannes Berg 	/* afterwards, resume scan & go to next channel */
75407ef03eeSJohannes Berg 	local->next_scan_state = SCAN_RESUME;
75507ef03eeSJohannes Berg }
75607ef03eeSJohannes Berg 
75707ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local,
75807ef03eeSJohannes Berg 					unsigned long *next_delay)
75907ef03eeSJohannes Berg {
760aacde9eeSStanislaw Gruszka 	ieee80211_offchannel_stop_vifs(local);
76107ef03eeSJohannes Berg 
76207ef03eeSJohannes Berg 	if (local->ops->flush) {
76339ecc01dSJohannes Berg 		ieee80211_flush_queues(local, NULL);
76407ef03eeSJohannes Berg 		*next_delay = 0;
76507ef03eeSJohannes Berg 	} else
76607ef03eeSJohannes Berg 		*next_delay = HZ / 10;
76707ef03eeSJohannes Berg 
76807ef03eeSJohannes Berg 	/* remember when we left the operating channel */
76907ef03eeSJohannes Berg 	local->leave_oper_channel_time = jiffies;
77007ef03eeSJohannes Berg 
77107ef03eeSJohannes Berg 	/* advance to the next channel to be scanned */
772de2ee84dSMohammed Shafi Shajakhan 	local->next_scan_state = SCAN_SET_CHANNEL;
77307ef03eeSJohannes Berg }
77407ef03eeSJohannes Berg 
775c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work)
7760a51b27eSJohannes Berg {
7770a51b27eSJohannes Berg 	struct ieee80211_local *local =
7780a51b27eSJohannes Berg 		container_of(work, struct ieee80211_local, scan_work.work);
779d07bfd8bSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7800a51b27eSJohannes Berg 	unsigned long next_delay = 0;
7818bd2a248SEliad Peller 	bool aborted;
7828789d459SJohannes Berg 
783259b62e3SStanislaw Gruszka 	mutex_lock(&local->mtx);
784259b62e3SStanislaw Gruszka 
785e2fd5dbcSJohannes Berg 	sdata = rcu_dereference_protected(local->scan_sdata,
786e2fd5dbcSJohannes Berg 					  lockdep_is_held(&local->mtx));
787d07bfd8bSJohannes Berg 
7888a690674SBen Greear 	/* When scanning on-channel, the first-callback means completed. */
7898a690674SBen Greear 	if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
7908a690674SBen Greear 		aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
7918a690674SBen Greear 		goto out_complete;
7928a690674SBen Greear 	}
7938a690674SBen Greear 
794259b62e3SStanislaw Gruszka 	if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
7958789d459SJohannes Berg 		aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
796259b62e3SStanislaw Gruszka 		goto out_complete;
7978789d459SJohannes Berg 	}
7988789d459SJohannes Berg 
799259b62e3SStanislaw Gruszka 	if (!sdata || !local->scan_req)
800259b62e3SStanislaw Gruszka 		goto out;
801f3b85252SJohannes Berg 
802fbe9c429SHelmut Schaa 	if (local->scan_req && !local->scanning) {
803f3b85252SJohannes Berg 		struct cfg80211_scan_request *req = local->scan_req;
804f3b85252SJohannes Berg 		int rc;
805f3b85252SJohannes Berg 
806f3b85252SJohannes Berg 		local->scan_req = NULL;
8070c2bef46SMonam Agarwal 		RCU_INIT_POINTER(local->scan_sdata, NULL);
808f3b85252SJohannes Berg 
809f3b85252SJohannes Berg 		rc = __ieee80211_start_scan(sdata, req);
810259b62e3SStanislaw Gruszka 		if (rc) {
8113aed49efSStanislaw Gruszka 			/* need to complete scan in cfg80211 */
8123aed49efSStanislaw Gruszka 			local->scan_req = req;
813259b62e3SStanislaw Gruszka 			aborted = true;
814259b62e3SStanislaw Gruszka 			goto out_complete;
815259b62e3SStanislaw Gruszka 		} else
816259b62e3SStanislaw Gruszka 			goto out;
817f3b85252SJohannes Berg 	}
818f3b85252SJohannes Berg 
8195bc75728SJohannes Berg 	/*
820f502d09bSHelmut Schaa 	 * as long as no delay is required advance immediately
821f502d09bSHelmut Schaa 	 * without scheduling a new work
822f502d09bSHelmut Schaa 	 */
823f502d09bSHelmut Schaa 	do {
824c29acf20SRajkumar Manoharan 		if (!ieee80211_sdata_running(sdata)) {
825c29acf20SRajkumar Manoharan 			aborted = true;
826c29acf20SRajkumar Manoharan 			goto out_complete;
827c29acf20SRajkumar Manoharan 		}
828c29acf20SRajkumar Manoharan 
829977923b0SHelmut Schaa 		switch (local->next_scan_state) {
8302fb3f028SHelmut Schaa 		case SCAN_DECISION:
831e229f844SStanislaw Gruszka 			/* if no more bands/channels left, complete scan */
832e229f844SStanislaw Gruszka 			if (local->scan_channel_idx >= local->scan_req->n_channels) {
833e229f844SStanislaw Gruszka 				aborted = false;
834e229f844SStanislaw Gruszka 				goto out_complete;
835e229f844SStanislaw Gruszka 			}
836e229f844SStanislaw Gruszka 			ieee80211_scan_state_decision(local, &next_delay);
8370a51b27eSJohannes Berg 			break;
8382fb3f028SHelmut Schaa 		case SCAN_SET_CHANNEL:
8392fb3f028SHelmut Schaa 			ieee80211_scan_state_set_channel(local, &next_delay);
8402fb3f028SHelmut Schaa 			break;
8410a51b27eSJohannes Berg 		case SCAN_SEND_PROBE:
8427d3be3ccSHelmut Schaa 			ieee80211_scan_state_send_probe(local, &next_delay);
8430a51b27eSJohannes Berg 			break;
84407ef03eeSJohannes Berg 		case SCAN_SUSPEND:
84507ef03eeSJohannes Berg 			ieee80211_scan_state_suspend(local, &next_delay);
846142b9f50SHelmut Schaa 			break;
84707ef03eeSJohannes Berg 		case SCAN_RESUME:
84807ef03eeSJohannes Berg 			ieee80211_scan_state_resume(local, &next_delay);
849142b9f50SHelmut Schaa 			break;
850cd2bb512SSam Leffler 		case SCAN_ABORT:
851cd2bb512SSam Leffler 			aborted = true;
852cd2bb512SSam Leffler 			goto out_complete;
8530a51b27eSJohannes Berg 		}
854f502d09bSHelmut Schaa 	} while (next_delay == 0);
8550a51b27eSJohannes Berg 
85642935ecaSLuis R. Rodriguez 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
857d07bfd8bSJohannes Berg 	goto out;
858259b62e3SStanislaw Gruszka 
859259b62e3SStanislaw Gruszka out_complete:
8608bd2a248SEliad Peller 	__ieee80211_scan_completed(&local->hw, aborted);
861259b62e3SStanislaw Gruszka out:
862259b62e3SStanislaw Gruszka 	mutex_unlock(&local->mtx);
8630a51b27eSJohannes Berg }
8640a51b27eSJohannes Berg 
865c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
8662a519311SJohannes Berg 			   struct cfg80211_scan_request *req)
8670a51b27eSJohannes Berg {
868f3b85252SJohannes Berg 	int res;
8690a51b27eSJohannes Berg 
870a1699b75SJohannes Berg 	mutex_lock(&sdata->local->mtx);
871f3b85252SJohannes Berg 	res = __ieee80211_start_scan(sdata, req);
872a1699b75SJohannes Berg 	mutex_unlock(&sdata->local->mtx);
8732a519311SJohannes Berg 
874f3b85252SJohannes Berg 	return res;
8750a51b27eSJohannes Berg }
8760a51b27eSJohannes Berg 
87734bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
878be4a4b6aSJohannes Berg 				const u8 *ssid, u8 ssid_len,
8797ca15a0aSSimon Wunderlich 				struct ieee80211_channel *chan,
8807ca15a0aSSimon Wunderlich 				enum nl80211_bss_scan_width scan_width)
881f3b85252SJohannes Berg {
882f3b85252SJohannes Berg 	struct ieee80211_local *local = sdata->local;
883f3b85252SJohannes Berg 	int ret = -EBUSY;
884fb63bc41SGertjan van Wingerde 	enum ieee80211_band band;
8859116dd01SJohannes Berg 
886a1699b75SJohannes Berg 	mutex_lock(&local->mtx);
887f3b85252SJohannes Berg 
888f3b85252SJohannes Berg 	/* busy scanning */
889f3b85252SJohannes Berg 	if (local->scan_req)
890f3b85252SJohannes Berg 		goto unlock;
891f3b85252SJohannes Berg 
892be4a4b6aSJohannes Berg 	/* fill internal scan request */
893be4a4b6aSJohannes Berg 	if (!chan) {
89434bcf715SStanislaw Gruszka 		int i, max_n;
89534bcf715SStanislaw Gruszka 		int n_ch = 0;
896be4a4b6aSJohannes Berg 
897be4a4b6aSJohannes Berg 		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
898be4a4b6aSJohannes Berg 			if (!local->hw.wiphy->bands[band])
899be4a4b6aSJohannes Berg 				continue;
90034bcf715SStanislaw Gruszka 
90134bcf715SStanislaw Gruszka 			max_n = local->hw.wiphy->bands[band]->n_channels;
90234bcf715SStanislaw Gruszka 			for (i = 0; i < max_n; i++) {
90334bcf715SStanislaw Gruszka 				struct ieee80211_channel *tmp_ch =
904be4a4b6aSJohannes Berg 				    &local->hw.wiphy->bands[band]->channels[i];
90534bcf715SStanislaw Gruszka 
9068fe02e16SLuis R. Rodriguez 				if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
90734bcf715SStanislaw Gruszka 						     IEEE80211_CHAN_DISABLED))
90834bcf715SStanislaw Gruszka 					continue;
90934bcf715SStanislaw Gruszka 
91034bcf715SStanislaw Gruszka 				local->int_scan_req->channels[n_ch] = tmp_ch;
91134bcf715SStanislaw Gruszka 				n_ch++;
912be4a4b6aSJohannes Berg 			}
913be4a4b6aSJohannes Berg 		}
914be4a4b6aSJohannes Berg 
91534bcf715SStanislaw Gruszka 		if (WARN_ON_ONCE(n_ch == 0))
91634bcf715SStanislaw Gruszka 			goto unlock;
91734bcf715SStanislaw Gruszka 
91834bcf715SStanislaw Gruszka 		local->int_scan_req->n_channels = n_ch;
919be4a4b6aSJohannes Berg 	} else {
9208fe02e16SLuis R. Rodriguez 		if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IR |
92134bcf715SStanislaw Gruszka 						IEEE80211_CHAN_DISABLED)))
92234bcf715SStanislaw Gruszka 			goto unlock;
92334bcf715SStanislaw Gruszka 
924be4a4b6aSJohannes Berg 		local->int_scan_req->channels[0] = chan;
925be4a4b6aSJohannes Berg 		local->int_scan_req->n_channels = 1;
926be4a4b6aSJohannes Berg 	}
927be4a4b6aSJohannes Berg 
928be4a4b6aSJohannes Berg 	local->int_scan_req->ssids = &local->scan_ssid;
929be4a4b6aSJohannes Berg 	local->int_scan_req->n_ssids = 1;
9307ca15a0aSSimon Wunderlich 	local->int_scan_req->scan_width = scan_width;
9315ba63533SJohannes Berg 	memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
9325ba63533SJohannes Berg 	local->int_scan_req->ssids[0].ssid_len = ssid_len;
933f3b85252SJohannes Berg 
9345ba63533SJohannes Berg 	ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
935f3b85252SJohannes Berg  unlock:
936a1699b75SJohannes Berg 	mutex_unlock(&local->mtx);
937f3b85252SJohannes Berg 	return ret;
9380a51b27eSJohannes Berg }
9395bb644a0SJohannes Berg 
9404136c422SStanislaw Gruszka /*
9414136c422SStanislaw Gruszka  * Only call this function when a scan can't be queued -- under RTNL.
9424136c422SStanislaw Gruszka  */
9435bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local)
9445bb644a0SJohannes Berg {
9455bb644a0SJohannes Berg 	/*
946b856439bSEliad Peller 	 * We are canceling software scan, or deferred scan that was not
9474136c422SStanislaw Gruszka 	 * yet really started (see __ieee80211_start_scan ).
9484136c422SStanislaw Gruszka 	 *
9494136c422SStanislaw Gruszka 	 * Regarding hardware scan:
9504136c422SStanislaw Gruszka 	 * - we can not call  __ieee80211_scan_completed() as when
9514136c422SStanislaw Gruszka 	 *   SCAN_HW_SCANNING bit is set this function change
9524136c422SStanislaw Gruszka 	 *   local->hw_scan_req to operate on 5G band, what race with
9534136c422SStanislaw Gruszka 	 *   driver which can use local->hw_scan_req
9544136c422SStanislaw Gruszka 	 *
9554136c422SStanislaw Gruszka 	 * - we can not cancel scan_work since driver can schedule it
9564136c422SStanislaw Gruszka 	 *   by ieee80211_scan_completed(..., true) to finish scan
9574136c422SStanislaw Gruszka 	 *
958b856439bSEliad Peller 	 * Hence we only call the cancel_hw_scan() callback, but the low-level
959b856439bSEliad Peller 	 * driver is still responsible for calling ieee80211_scan_completed()
960b856439bSEliad Peller 	 * after the scan was completed/aborted.
9615bb644a0SJohannes Berg 	 */
9624136c422SStanislaw Gruszka 
963a1699b75SJohannes Berg 	mutex_lock(&local->mtx);
964b856439bSEliad Peller 	if (!local->scan_req)
965b856439bSEliad Peller 		goto out;
966b856439bSEliad Peller 
967a754055aSEmmanuel Grumbach 	/*
968a754055aSEmmanuel Grumbach 	 * We have a scan running and the driver already reported completion,
969a754055aSEmmanuel Grumbach 	 * but the worker hasn't run yet or is stuck on the mutex - mark it as
970a754055aSEmmanuel Grumbach 	 * cancelled.
971a754055aSEmmanuel Grumbach 	 */
972a754055aSEmmanuel Grumbach 	if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
973a754055aSEmmanuel Grumbach 	    test_bit(SCAN_COMPLETED, &local->scanning)) {
974a754055aSEmmanuel Grumbach 		set_bit(SCAN_HW_CANCELLED, &local->scanning);
975a754055aSEmmanuel Grumbach 		goto out;
976a754055aSEmmanuel Grumbach 	}
977a754055aSEmmanuel Grumbach 
978b856439bSEliad Peller 	if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
979a754055aSEmmanuel Grumbach 		/*
980a754055aSEmmanuel Grumbach 		 * Make sure that __ieee80211_scan_completed doesn't trigger a
981a754055aSEmmanuel Grumbach 		 * scan on another band.
982a754055aSEmmanuel Grumbach 		 */
983a754055aSEmmanuel Grumbach 		set_bit(SCAN_HW_CANCELLED, &local->scanning);
984b856439bSEliad Peller 		if (local->ops->cancel_hw_scan)
985e2fd5dbcSJohannes Berg 			drv_cancel_hw_scan(local,
986e2fd5dbcSJohannes Berg 				rcu_dereference_protected(local->scan_sdata,
987e2fd5dbcSJohannes Berg 						lockdep_is_held(&local->mtx)));
988b856439bSEliad Peller 		goto out;
989b856439bSEliad Peller 	}
990b856439bSEliad Peller 
991d07bfd8bSJohannes Berg 	/*
992d07bfd8bSJohannes Berg 	 * If the work is currently running, it must be blocked on
993d07bfd8bSJohannes Berg 	 * the mutex, but we'll set scan_sdata = NULL and it'll
994d07bfd8bSJohannes Berg 	 * simply exit once it acquires the mutex.
995d07bfd8bSJohannes Berg 	 */
996d07bfd8bSJohannes Berg 	cancel_delayed_work(&local->scan_work);
997d07bfd8bSJohannes Berg 	/* and clean up */
9988bd2a248SEliad Peller 	__ieee80211_scan_completed(&local->hw, true);
999b856439bSEliad Peller out:
1000d07bfd8bSJohannes Berg 	mutex_unlock(&local->mtx);
10015bb644a0SJohannes Berg }
100279f460caSLuciano Coelho 
1003d43c6b6eSDavid Spinadel int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
100479f460caSLuciano Coelho 					struct cfg80211_sched_scan_request *req)
100579f460caSLuciano Coelho {
100679f460caSLuciano Coelho 	struct ieee80211_local *local = sdata->local;
1007633e2713SDavid Spinadel 	struct ieee80211_scan_ies sched_scan_ies = {};
10082103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
1009633e2713SDavid Spinadel 	int ret, i, iebufsz, num_bands = 0;
1010633e2713SDavid Spinadel 	u32 rate_masks[IEEE80211_NUM_BANDS] = {};
1011633e2713SDavid Spinadel 	u8 bands_used = 0;
1012633e2713SDavid Spinadel 	u8 *ie;
1013633e2713SDavid Spinadel 	size_t len;
1014c604b9f2SJohannes Berg 
1015e4dcbb37SDavid Spinadel 	iebufsz = local->scan_ies_len + req->ie_len;
101679f460caSLuciano Coelho 
1017d43c6b6eSDavid Spinadel 	lockdep_assert_held(&local->mtx);
101879f460caSLuciano Coelho 
1019d43c6b6eSDavid Spinadel 	if (!local->ops->sched_scan_start)
1020d43c6b6eSDavid Spinadel 		return -ENOTSUPP;
102179f460caSLuciano Coelho 
102279f460caSLuciano Coelho 	for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
1023633e2713SDavid Spinadel 		if (local->hw.wiphy->bands[i]) {
1024633e2713SDavid Spinadel 			bands_used |= BIT(i);
1025633e2713SDavid Spinadel 			rate_masks[i] = (u32) -1;
1026633e2713SDavid Spinadel 			num_bands++;
1027633e2713SDavid Spinadel 		}
1028633e2713SDavid Spinadel 	}
1029c56ef672SDavid Spinadel 
1030633e2713SDavid Spinadel 	ie = kzalloc(num_bands * iebufsz, GFP_KERNEL);
1031633e2713SDavid Spinadel 	if (!ie) {
103279f460caSLuciano Coelho 		ret = -ENOMEM;
1033633e2713SDavid Spinadel 		goto out;
103479f460caSLuciano Coelho 	}
103579f460caSLuciano Coelho 
10362103dec1SSimon Wunderlich 	ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
10372103dec1SSimon Wunderlich 
1038633e2713SDavid Spinadel 	len = ieee80211_build_preq_ies(local, ie, num_bands * iebufsz,
1039633e2713SDavid Spinadel 				       &sched_scan_ies, req->ie,
1040633e2713SDavid Spinadel 				       req->ie_len, bands_used,
1041c56ef672SDavid Spinadel 				       rate_masks, &chandef);
104279f460caSLuciano Coelho 
104330dd3edfSJohannes Berg 	ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
1044d43c6b6eSDavid Spinadel 	if (ret == 0) {
10455260a5b2SJohannes Berg 		rcu_assign_pointer(local->sched_scan_sdata, sdata);
1046d43c6b6eSDavid Spinadel 		local->sched_scan_req = req;
1047d43c6b6eSDavid Spinadel 	}
104879f460caSLuciano Coelho 
1049633e2713SDavid Spinadel 	kfree(ie);
1050d43c6b6eSDavid Spinadel 
1051633e2713SDavid Spinadel out:
1052d43c6b6eSDavid Spinadel 	if (ret) {
1053d43c6b6eSDavid Spinadel 		/* Clean in case of failure after HW restart or upon resume. */
10540c2bef46SMonam Agarwal 		RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
1055d43c6b6eSDavid Spinadel 		local->sched_scan_req = NULL;
1056d43c6b6eSDavid Spinadel 	}
1057d43c6b6eSDavid Spinadel 
1058d43c6b6eSDavid Spinadel 	return ret;
1059d43c6b6eSDavid Spinadel }
1060d43c6b6eSDavid Spinadel 
1061d43c6b6eSDavid Spinadel int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1062d43c6b6eSDavid Spinadel 				       struct cfg80211_sched_scan_request *req)
1063d43c6b6eSDavid Spinadel {
1064d43c6b6eSDavid Spinadel 	struct ieee80211_local *local = sdata->local;
1065d43c6b6eSDavid Spinadel 	int ret;
1066d43c6b6eSDavid Spinadel 
1067d43c6b6eSDavid Spinadel 	mutex_lock(&local->mtx);
1068d43c6b6eSDavid Spinadel 
1069d43c6b6eSDavid Spinadel 	if (rcu_access_pointer(local->sched_scan_sdata)) {
1070d43c6b6eSDavid Spinadel 		mutex_unlock(&local->mtx);
1071d43c6b6eSDavid Spinadel 		return -EBUSY;
1072d43c6b6eSDavid Spinadel 	}
1073d43c6b6eSDavid Spinadel 
1074d43c6b6eSDavid Spinadel 	ret = __ieee80211_request_sched_scan_start(sdata, req);
1075d43c6b6eSDavid Spinadel 
10765260a5b2SJohannes Berg 	mutex_unlock(&local->mtx);
107779f460caSLuciano Coelho 	return ret;
107879f460caSLuciano Coelho }
107979f460caSLuciano Coelho 
108085a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
108179f460caSLuciano Coelho {
108279f460caSLuciano Coelho 	struct ieee80211_local *local = sdata->local;
108330dd3edfSJohannes Berg 	int ret = 0;
108479f460caSLuciano Coelho 
10855260a5b2SJohannes Berg 	mutex_lock(&local->mtx);
108679f460caSLuciano Coelho 
108779f460caSLuciano Coelho 	if (!local->ops->sched_scan_stop) {
108879f460caSLuciano Coelho 		ret = -ENOTSUPP;
108979f460caSLuciano Coelho 		goto out;
109079f460caSLuciano Coelho 	}
109179f460caSLuciano Coelho 
1092d43c6b6eSDavid Spinadel 	/* We don't want to restart sched scan anymore. */
1093d43c6b6eSDavid Spinadel 	local->sched_scan_req = NULL;
1094d43c6b6eSDavid Spinadel 
109571228a1eSAlexander Bondar 	if (rcu_access_pointer(local->sched_scan_sdata)) {
109637e3308cSJohannes Berg 		ret = drv_sched_scan_stop(local, sdata);
109771228a1eSAlexander Bondar 		if (!ret)
1098ad053a96SAndreea-Cristina Bernat 			RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
109971228a1eSAlexander Bondar 	}
110079f460caSLuciano Coelho out:
11015260a5b2SJohannes Berg 	mutex_unlock(&local->mtx);
110279f460caSLuciano Coelho 
110379f460caSLuciano Coelho 	return ret;
110479f460caSLuciano Coelho }
110579f460caSLuciano Coelho 
110679f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
110779f460caSLuciano Coelho {
110879f460caSLuciano Coelho 	struct ieee80211_local *local = hw_to_local(hw);
110979f460caSLuciano Coelho 
111079f460caSLuciano Coelho 	trace_api_sched_scan_results(local);
111179f460caSLuciano Coelho 
111279f460caSLuciano Coelho 	cfg80211_sched_scan_results(hw->wiphy);
111379f460caSLuciano Coelho }
111479f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results);
111579f460caSLuciano Coelho 
1116f6837ba8SJohannes Berg void ieee80211_sched_scan_end(struct ieee80211_local *local)
111785a9994aSLuciano Coelho {
111885a9994aSLuciano Coelho 	mutex_lock(&local->mtx);
111985a9994aSLuciano Coelho 
11205260a5b2SJohannes Berg 	if (!rcu_access_pointer(local->sched_scan_sdata)) {
112185a9994aSLuciano Coelho 		mutex_unlock(&local->mtx);
112285a9994aSLuciano Coelho 		return;
112385a9994aSLuciano Coelho 	}
112485a9994aSLuciano Coelho 
11250c2bef46SMonam Agarwal 	RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
112685a9994aSLuciano Coelho 
1127d43c6b6eSDavid Spinadel 	/* If sched scan was aborted by the driver. */
1128d43c6b6eSDavid Spinadel 	local->sched_scan_req = NULL;
1129d43c6b6eSDavid Spinadel 
113085a9994aSLuciano Coelho 	mutex_unlock(&local->mtx);
113185a9994aSLuciano Coelho 
113285a9994aSLuciano Coelho 	cfg80211_sched_scan_stopped(local->hw.wiphy);
113385a9994aSLuciano Coelho }
113485a9994aSLuciano Coelho 
1135f6837ba8SJohannes Berg void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1136f6837ba8SJohannes Berg {
1137f6837ba8SJohannes Berg 	struct ieee80211_local *local =
1138f6837ba8SJohannes Berg 		container_of(work, struct ieee80211_local,
1139f6837ba8SJohannes Berg 			     sched_scan_stopped_work);
1140f6837ba8SJohannes Berg 
1141f6837ba8SJohannes Berg 	ieee80211_sched_scan_end(local);
1142f6837ba8SJohannes Berg }
1143f6837ba8SJohannes Berg 
114479f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
114579f460caSLuciano Coelho {
114679f460caSLuciano Coelho 	struct ieee80211_local *local = hw_to_local(hw);
114779f460caSLuciano Coelho 
114879f460caSLuciano Coelho 	trace_api_sched_scan_stopped(local);
114979f460caSLuciano Coelho 
115018db594aSJohannes Berg 	schedule_work(&local->sched_scan_stopped_work);
115179f460caSLuciano Coelho }
115279f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped);
1153