xref: /linux/net/mac80211/scan.c (revision d43c6b6e6f2fcaebf198c499716e5e24d878fdd2)
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>
90a51b27eSJohannes Berg  *
100a51b27eSJohannes Berg  * This program is free software; you can redistribute it and/or modify
110a51b27eSJohannes Berg  * it under the terms of the GNU General Public License version 2 as
120a51b27eSJohannes Berg  * published by the Free Software Foundation.
130a51b27eSJohannes Berg  */
140a51b27eSJohannes Berg 
150a51b27eSJohannes Berg #include <linux/if_arp.h>
16888d04dfSFelix Fietkau #include <linux/etherdevice.h>
17078e1e60SJohannes Berg #include <linux/rtnetlink.h>
18e8db0be1SJean Pihet #include <linux/pm_qos.h>
19df13cce5SHelmut Schaa #include <net/sch_generic.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
21bc3b2d7fSPaul Gortmaker #include <linux/export.h>
220a51b27eSJohannes Berg #include <net/mac80211.h>
230a51b27eSJohannes Berg 
240a51b27eSJohannes Berg #include "ieee80211_i.h"
2524487981SJohannes Berg #include "driver-ops.h"
265484e237SJohannes Berg #include "mesh.h"
270a51b27eSJohannes Berg 
280a51b27eSJohannes Berg #define IEEE80211_PROBE_DELAY (HZ / 33)
290a51b27eSJohannes Berg #define IEEE80211_CHANNEL_TIME (HZ / 33)
303f892b61SStanislaw Gruszka #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
310a51b27eSJohannes Berg 
325484e237SJohannes Berg void ieee80211_rx_bss_put(struct ieee80211_local *local,
33c2b13452SJohannes Berg 			  struct ieee80211_bss *bss)
345484e237SJohannes Berg {
350c1ad2caSJohannes Berg 	if (!bss)
360c1ad2caSJohannes Berg 		return;
375b112d3dSJohannes Berg 	cfg80211_put_bss(local->hw.wiphy,
385b112d3dSJohannes Berg 			 container_of((void *)bss, struct cfg80211_bss, priv));
395484e237SJohannes Berg }
405484e237SJohannes Berg 
41ab13315aSKalle Valo static bool is_uapsd_supported(struct ieee802_11_elems *elems)
42ab13315aSKalle Valo {
43ab13315aSKalle Valo 	u8 qos_info;
44ab13315aSKalle Valo 
45ab13315aSKalle Valo 	if (elems->wmm_info && elems->wmm_info_len == 7
46ab13315aSKalle Valo 	    && elems->wmm_info[5] == 1)
47ab13315aSKalle Valo 		qos_info = elems->wmm_info[6];
48ab13315aSKalle Valo 	else if (elems->wmm_param && elems->wmm_param_len == 24
49ab13315aSKalle Valo 		 && elems->wmm_param[5] == 1)
50ab13315aSKalle Valo 		qos_info = elems->wmm_param[6];
51ab13315aSKalle Valo 	else
52ab13315aSKalle Valo 		/* no valid wmm information or parameter element found */
53ab13315aSKalle Valo 		return false;
54ab13315aSKalle Valo 
55ab13315aSKalle Valo 	return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
56ab13315aSKalle Valo }
57ab13315aSKalle Valo 
58c2b13452SJohannes Berg struct ieee80211_bss *
595484e237SJohannes Berg ieee80211_bss_info_update(struct ieee80211_local *local,
605484e237SJohannes Berg 			  struct ieee80211_rx_status *rx_status,
61d45c4172SEmmanuel Grumbach 			  struct ieee80211_mgmt *mgmt, size_t len,
625484e237SJohannes Berg 			  struct ieee802_11_elems *elems,
63d45c4172SEmmanuel Grumbach 			  struct ieee80211_channel *channel)
645484e237SJohannes Berg {
65d45c4172SEmmanuel Grumbach 	bool beacon = ieee80211_is_beacon(mgmt->frame_control);
660c1ad2caSJohannes Berg 	struct cfg80211_bss *cbss;
67c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
68f0b058b6SStanislaw Gruszka 	int clen, srlen;
697ca15a0aSSimon Wunderlich 	enum nl80211_bss_scan_width scan_width;
702a519311SJohannes Berg 	s32 signal = 0;
712a519311SJohannes Berg 
7277965c97SJohannes Berg 	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
732a519311SJohannes Berg 		signal = rx_status->signal * 100;
7477965c97SJohannes Berg 	else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
752a519311SJohannes Berg 		signal = (rx_status->signal * 100) / local->hw.max_signal;
762a519311SJohannes Berg 
777ca15a0aSSimon Wunderlich 	scan_width = NL80211_BSS_CHAN_WIDTH_20;
787ca15a0aSSimon Wunderlich 	if (rx_status->flag & RX_FLAG_5MHZ)
797ca15a0aSSimon Wunderlich 		scan_width = NL80211_BSS_CHAN_WIDTH_5;
807ca15a0aSSimon Wunderlich 	if (rx_status->flag & RX_FLAG_10MHZ)
817ca15a0aSSimon Wunderlich 		scan_width = NL80211_BSS_CHAN_WIDTH_10;
827ca15a0aSSimon Wunderlich 
837ca15a0aSSimon Wunderlich 	cbss = cfg80211_inform_bss_width_frame(local->hw.wiphy, channel,
847ca15a0aSSimon Wunderlich 					       scan_width, mgmt, len, signal,
857ca15a0aSSimon Wunderlich 					       GFP_ATOMIC);
860c1ad2caSJohannes Berg 	if (!cbss)
875484e237SJohannes Berg 		return NULL;
8800d3f14cSJohannes Berg 
890c1ad2caSJohannes Berg 	bss = (void *)cbss->priv;
905484e237SJohannes Berg 
91ef429dadSJohannes Berg 	if (beacon)
92ef429dadSJohannes Berg 		bss->device_ts_beacon = rx_status->device_timestamp;
93ef429dadSJohannes Berg 	else
94ef429dadSJohannes Berg 		bss->device_ts_presp = rx_status->device_timestamp;
958c358bcdSJohannes Berg 
96fcff4f10SPaul Stewart 	if (elems->parse_error) {
97fcff4f10SPaul Stewart 		if (beacon)
98fcff4f10SPaul Stewart 			bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
99fcff4f10SPaul Stewart 		else
100fcff4f10SPaul Stewart 			bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
101fcff4f10SPaul Stewart 	} else {
102fcff4f10SPaul Stewart 		if (beacon)
103fcff4f10SPaul Stewart 			bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
104fcff4f10SPaul Stewart 		else
105fcff4f10SPaul Stewart 			bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
1065484e237SJohannes Berg 	}
1075484e237SJohannes Berg 
108fcff4f10SPaul Stewart 	/* save the ERP value so that it is available at association time */
1091946bed9SJohannes Berg 	if (elems->erp_info && (!elems->parse_error ||
110fcff4f10SPaul Stewart 				!(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
111fcff4f10SPaul Stewart 		bss->erp_value = elems->erp_info[0];
112fcff4f10SPaul Stewart 		bss->has_erp_value = true;
113fcff4f10SPaul Stewart 		if (!elems->parse_error)
114fcff4f10SPaul Stewart 			bss->valid_data |= IEEE80211_BSS_VALID_ERP;
115fcff4f10SPaul Stewart 	}
116fcff4f10SPaul Stewart 
117f0b058b6SStanislaw Gruszka 	/* replace old supported rates if we get new values */
118fcff4f10SPaul Stewart 	if (!elems->parse_error ||
119fcff4f10SPaul Stewart 	    !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
120f0b058b6SStanislaw Gruszka 		srlen = 0;
1215484e237SJohannes Berg 		if (elems->supp_rates) {
122f0b058b6SStanislaw Gruszka 			clen = IEEE80211_MAX_SUPP_RATES;
1235484e237SJohannes Berg 			if (clen > elems->supp_rates_len)
1245484e237SJohannes Berg 				clen = elems->supp_rates_len;
125f0b058b6SStanislaw Gruszka 			memcpy(bss->supp_rates, elems->supp_rates, clen);
126f0b058b6SStanislaw Gruszka 			srlen += clen;
1275484e237SJohannes Berg 		}
1285484e237SJohannes Berg 		if (elems->ext_supp_rates) {
129f0b058b6SStanislaw Gruszka 			clen = IEEE80211_MAX_SUPP_RATES - srlen;
1305484e237SJohannes Berg 			if (clen > elems->ext_supp_rates_len)
1315484e237SJohannes Berg 				clen = elems->ext_supp_rates_len;
132fcff4f10SPaul Stewart 			memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
133fcff4f10SPaul Stewart 			       clen);
134f0b058b6SStanislaw Gruszka 			srlen += clen;
1355484e237SJohannes Berg 		}
136fcff4f10SPaul Stewart 		if (srlen) {
137f0b058b6SStanislaw Gruszka 			bss->supp_rates_len = srlen;
138fcff4f10SPaul Stewart 			if (!elems->parse_error)
139fcff4f10SPaul Stewart 				bss->valid_data |= IEEE80211_BSS_VALID_RATES;
140fcff4f10SPaul Stewart 		}
141fcff4f10SPaul Stewart 	}
1425484e237SJohannes Berg 
143fcff4f10SPaul Stewart 	if (!elems->parse_error ||
144fcff4f10SPaul Stewart 	    !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
1455484e237SJohannes Berg 		bss->wmm_used = elems->wmm_param || elems->wmm_info;
146ab13315aSKalle Valo 		bss->uapsd_supported = is_uapsd_supported(elems);
147fcff4f10SPaul Stewart 		if (!elems->parse_error)
148fcff4f10SPaul Stewart 			bss->valid_data |= IEEE80211_BSS_VALID_WMM;
149fcff4f10SPaul Stewart 	}
1505484e237SJohannes Berg 
151817cee76SAlexander Bondar 	if (beacon) {
152817cee76SAlexander Bondar 		struct ieee80211_supported_band *sband =
153817cee76SAlexander Bondar 			local->hw.wiphy->bands[rx_status->band];
154817cee76SAlexander Bondar 		if (!(rx_status->flag & RX_FLAG_HT) &&
155817cee76SAlexander Bondar 		    !(rx_status->flag & RX_FLAG_VHT))
156817cee76SAlexander Bondar 			bss->beacon_rate =
157817cee76SAlexander Bondar 				&sband->bitrates[rx_status->rate_idx];
158817cee76SAlexander Bondar 	}
159817cee76SAlexander Bondar 
1605484e237SJohannes Berg 	return bss;
1615484e237SJohannes Berg }
1620a51b27eSJohannes Berg 
163d48b2968SJohannes Berg void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
16498c8fccfSJohannes Berg {
165f1d58c25SJohannes Berg 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
166d48b2968SJohannes Berg 	struct ieee80211_sub_if_data *sdata1, *sdata2;
167d48b2968SJohannes Berg 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
168c2b13452SJohannes Berg 	struct ieee80211_bss *bss;
16998c8fccfSJohannes Berg 	u8 *elements;
17098c8fccfSJohannes Berg 	struct ieee80211_channel *channel;
17198c8fccfSJohannes Berg 	size_t baselen;
17298c8fccfSJohannes Berg 	struct ieee802_11_elems elems;
17398c8fccfSJohannes Berg 
174d48b2968SJohannes Berg 	if (skb->len < 24 ||
175d48b2968SJohannes Berg 	    (!ieee80211_is_probe_resp(mgmt->frame_control) &&
176d48b2968SJohannes Berg 	     !ieee80211_is_beacon(mgmt->frame_control)))
177d48b2968SJohannes Berg 		return;
17898c8fccfSJohannes Berg 
179d48b2968SJohannes Berg 	sdata1 = rcu_dereference(local->scan_sdata);
180d48b2968SJohannes Berg 	sdata2 = rcu_dereference(local->sched_scan_sdata);
18198c8fccfSJohannes Berg 
182d48b2968SJohannes Berg 	if (likely(!sdata1 && !sdata2))
183d48b2968SJohannes Berg 		return;
18498c8fccfSJohannes Berg 
185d48b2968SJohannes Berg 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
18698c8fccfSJohannes Berg 		/* ignore ProbeResp to foreign address */
187d48b2968SJohannes Berg 		if ((!sdata1 || !ether_addr_equal(mgmt->da, sdata1->vif.addr)) &&
188d48b2968SJohannes Berg 		    (!sdata2 || !ether_addr_equal(mgmt->da, sdata2->vif.addr)))
189d48b2968SJohannes Berg 			return;
19098c8fccfSJohannes Berg 
19198c8fccfSJohannes Berg 		elements = mgmt->u.probe_resp.variable;
19298c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
19398c8fccfSJohannes Berg 	} else {
19498c8fccfSJohannes Berg 		baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
19598c8fccfSJohannes Berg 		elements = mgmt->u.beacon.variable;
19698c8fccfSJohannes Berg 	}
19798c8fccfSJohannes Berg 
19898c8fccfSJohannes Berg 	if (baselen > skb->len)
199d48b2968SJohannes Berg 		return;
20098c8fccfSJohannes Berg 
201b2e506bfSJohannes Berg 	ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems);
20298c8fccfSJohannes Berg 
2030172bb75SJohannes Berg 	channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
20498c8fccfSJohannes Berg 
20598c8fccfSJohannes Berg 	if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
206d48b2968SJohannes Berg 		return;
20798c8fccfSJohannes Berg 
208d48b2968SJohannes Berg 	bss = ieee80211_bss_info_update(local, rx_status,
20998c8fccfSJohannes Berg 					mgmt, skb->len, &elems,
210d45c4172SEmmanuel Grumbach 					channel);
211d048e503SJouni Malinen 	if (bss)
212d48b2968SJohannes Berg 		ieee80211_rx_bss_put(local, bss);
21398c8fccfSJohannes Berg }
21498c8fccfSJohannes Berg 
2152103dec1SSimon Wunderlich static void
2162103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
2172103dec1SSimon Wunderlich 			       enum nl80211_bss_scan_width scan_width)
2182103dec1SSimon Wunderlich {
2192103dec1SSimon Wunderlich 	memset(chandef, 0, sizeof(*chandef));
2202103dec1SSimon Wunderlich 	switch (scan_width) {
2212103dec1SSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_5:
2222103dec1SSimon Wunderlich 		chandef->width = NL80211_CHAN_WIDTH_5;
2232103dec1SSimon Wunderlich 		break;
2242103dec1SSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_10:
2252103dec1SSimon Wunderlich 		chandef->width = NL80211_CHAN_WIDTH_10;
2262103dec1SSimon Wunderlich 		break;
2272103dec1SSimon Wunderlich 	default:
2282103dec1SSimon Wunderlich 		chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2292103dec1SSimon Wunderlich 		break;
2302103dec1SSimon Wunderlich 	}
2312103dec1SSimon Wunderlich }
2322103dec1SSimon Wunderlich 
2334d36ec58SJohannes Berg /* return false if no more work */
2344d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
2354d36ec58SJohannes Berg {
2364d36ec58SJohannes Berg 	struct cfg80211_scan_request *req = local->scan_req;
2372103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
2384d36ec58SJohannes Berg 	enum ieee80211_band band;
2394d36ec58SJohannes Berg 	int i, ielen, n_chans;
2404d36ec58SJohannes Berg 
241a754055aSEmmanuel Grumbach 	if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
242a754055aSEmmanuel Grumbach 		return false;
243a754055aSEmmanuel Grumbach 
2444d36ec58SJohannes Berg 	do {
2454d36ec58SJohannes Berg 		if (local->hw_scan_band == IEEE80211_NUM_BANDS)
2464d36ec58SJohannes Berg 			return false;
2474d36ec58SJohannes Berg 
2484d36ec58SJohannes Berg 		band = local->hw_scan_band;
2494d36ec58SJohannes Berg 		n_chans = 0;
2504d36ec58SJohannes Berg 		for (i = 0; i < req->n_channels; i++) {
2514d36ec58SJohannes Berg 			if (req->channels[i]->band == band) {
2524d36ec58SJohannes Berg 				local->hw_scan_req->channels[n_chans] =
2534d36ec58SJohannes Berg 							req->channels[i];
2544d36ec58SJohannes Berg 				n_chans++;
2554d36ec58SJohannes Berg 			}
2564d36ec58SJohannes Berg 		}
2574d36ec58SJohannes Berg 
2584d36ec58SJohannes Berg 		local->hw_scan_band++;
2594d36ec58SJohannes Berg 	} while (!n_chans);
2604d36ec58SJohannes Berg 
2614d36ec58SJohannes Berg 	local->hw_scan_req->n_channels = n_chans;
2622103dec1SSimon Wunderlich 	ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
2634d36ec58SJohannes Berg 
2644d36ec58SJohannes Berg 	ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
265c604b9f2SJohannes Berg 					 local->hw_scan_ies_bufsize,
2668ee31080SJohannes Berg 					 req->ie, req->ie_len, band,
2672103dec1SSimon Wunderlich 					 req->rates[band], &chandef);
2684d36ec58SJohannes Berg 	local->hw_scan_req->ie_len = ielen;
269dcd83976SJohannes Berg 	local->hw_scan_req->no_cck = req->no_cck;
2704d36ec58SJohannes Berg 
2714d36ec58SJohannes Berg 	return true;
2724d36ec58SJohannes Berg }
2734d36ec58SJohannes Berg 
2748bd2a248SEliad Peller static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
275f3b85252SJohannes Berg {
276f3b85252SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
2778bd2a248SEliad Peller 	bool hw_scan = local->ops->hw_scan;
278a2b70e83SEliad Peller 	bool was_scanning = local->scanning;
279f3b85252SJohannes Berg 
280e229f844SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
281f3b85252SJohannes Berg 
2826d3560d4SJohannes Berg 	/*
2836d3560d4SJohannes Berg 	 * It's ok to abort a not-yet-running scan (that
2846d3560d4SJohannes Berg 	 * we have one at all will be verified by checking
2856d3560d4SJohannes Berg 	 * local->scan_req next), but not to complete it
2866d3560d4SJohannes Berg 	 * successfully.
2876d3560d4SJohannes Berg 	 */
2886d3560d4SJohannes Berg 	if (WARN_ON(!local->scanning && !aborted))
2896d3560d4SJohannes Berg 		aborted = true;
290f3b85252SJohannes Berg 
291e229f844SStanislaw Gruszka 	if (WARN_ON(!local->scan_req))
292d07bfd8bSJohannes Berg 		return;
293f3b85252SJohannes Berg 
2948bd2a248SEliad Peller 	if (hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
295e2fd5dbcSJohannes Berg 		int rc;
296e2fd5dbcSJohannes Berg 
297e2fd5dbcSJohannes Berg 		rc = drv_hw_scan(local,
298e2fd5dbcSJohannes Berg 			rcu_dereference_protected(local->scan_sdata,
299e2fd5dbcSJohannes Berg 						  lockdep_is_held(&local->mtx)),
300e2fd5dbcSJohannes Berg 			local->hw_scan_req);
301e2fd5dbcSJohannes Berg 
3026eb11a9aSStanislaw Gruszka 		if (rc == 0)
303d07bfd8bSJohannes Berg 			return;
3044d36ec58SJohannes Berg 	}
3054d36ec58SJohannes Berg 
3064d36ec58SJohannes Berg 	kfree(local->hw_scan_req);
3074d36ec58SJohannes Berg 	local->hw_scan_req = NULL;
308f3b85252SJohannes Berg 
3095ba63533SJohannes Berg 	if (local->scan_req != local->int_scan_req)
3102a519311SJohannes Berg 		cfg80211_scan_done(local->scan_req, aborted);
3112a519311SJohannes Berg 	local->scan_req = NULL;
3123aa569c3SJohannes Berg 	rcu_assign_pointer(local->scan_sdata, NULL);
3132a519311SJohannes Berg 
314fbe9c429SHelmut Schaa 	local->scanning = 0;
3157ca15a0aSSimon Wunderlich 	local->scan_chandef.chan = NULL;
316f3b85252SJohannes Berg 
31759bdf3b0SBen Greear 	/* Set power back to normal operating levels. */
31859bdf3b0SBen Greear 	ieee80211_hw_config(local, 0);
319b23b025fSBen Greear 
3208bd2a248SEliad Peller 	if (!hw_scan) {
321b23b025fSBen Greear 		ieee80211_configure_filter(local);
322b23b025fSBen Greear 		drv_sw_scan_complete(local);
323aacde9eeSStanislaw Gruszka 		ieee80211_offchannel_return(local);
324b23b025fSBen Greear 	}
325b23b025fSBen Greear 
3265cff20e6SJohannes Berg 	ieee80211_recalc_idle(local);
327e229f844SStanislaw Gruszka 
3280a51b27eSJohannes Berg 	ieee80211_mlme_notify_scan_completed(local);
32946900298SJohannes Berg 	ieee80211_ibss_notify_scan_completed(local);
330472dbc45SJohannes Berg 	ieee80211_mesh_notify_scan_completed(local);
331a2b70e83SEliad Peller 	if (was_scanning)
3322eb278e0SJohannes Berg 		ieee80211_start_next_roc(local);
3330a51b27eSJohannes Berg }
3348789d459SJohannes Berg 
3358789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
3368789d459SJohannes Berg {
3378789d459SJohannes Berg 	struct ieee80211_local *local = hw_to_local(hw);
3388789d459SJohannes Berg 
3398789d459SJohannes Berg 	trace_api_scan_completed(local, aborted);
3408789d459SJohannes Berg 
3418789d459SJohannes Berg 	set_bit(SCAN_COMPLETED, &local->scanning);
3428789d459SJohannes Berg 	if (aborted)
3438789d459SJohannes Berg 		set_bit(SCAN_ABORTED, &local->scanning);
3448789d459SJohannes Berg 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
3458789d459SJohannes Berg }
3460a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed);
3470a51b27eSJohannes Berg 
348f3b85252SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local)
349f3b85252SJohannes Berg {
350fe57d9f5SJohannes Berg 	/* Software scan is not supported in multi-channel cases */
351fe57d9f5SJohannes Berg 	if (local->use_chanctx)
352fe57d9f5SJohannes Berg 		return -EOPNOTSUPP;
353fe57d9f5SJohannes Berg 
354f3b85252SJohannes Berg 	/*
355f3b85252SJohannes Berg 	 * Hardware/driver doesn't support hw_scan, so use software
356f3b85252SJohannes Berg 	 * scanning instead. First send a nullfunc frame with power save
357f3b85252SJohannes Berg 	 * bit on so that AP will buffer the frames for us while we are not
358f3b85252SJohannes Berg 	 * listening, then send probe requests to each channel and wait for
359f3b85252SJohannes Berg 	 * the responses. After all channels are scanned, tune back to the
360f3b85252SJohannes Berg 	 * original channel and send a nullfunc frame with power save bit
361f3b85252SJohannes Berg 	 * off to trigger the AP to send us all the buffered frames.
362f3b85252SJohannes Berg 	 *
363f3b85252SJohannes Berg 	 * Note that while local->sw_scanning is true everything else but
364f3b85252SJohannes Berg 	 * nullfunc frames and probe requests will be dropped in
365f3b85252SJohannes Berg 	 * ieee80211_tx_h_check_assoc().
366f3b85252SJohannes Berg 	 */
36724487981SJohannes Berg 	drv_sw_scan_start(local);
368f3b85252SJohannes Berg 
369de312db3SRajkumar Manoharan 	local->leave_oper_channel_time = jiffies;
370977923b0SHelmut Schaa 	local->next_scan_state = SCAN_DECISION;
371f3b85252SJohannes Berg 	local->scan_channel_idx = 0;
372f3b85252SJohannes Berg 
373aacde9eeSStanislaw Gruszka 	ieee80211_offchannel_stop_vifs(local);
374a80f7c0bSJohannes Berg 
3759c35d7d2SSeth Forshee 	/* ensure nullfunc is transmitted before leaving operating channel */
37639ecc01dSJohannes Berg 	ieee80211_flush_queues(local, NULL);
3779c35d7d2SSeth Forshee 
3783ac64beeSJohannes Berg 	ieee80211_configure_filter(local);
379f3b85252SJohannes Berg 
38059bdf3b0SBen Greear 	/* We need to set power level at maximum rate for scanning. */
38159bdf3b0SBen Greear 	ieee80211_hw_config(local, 0);
38259bdf3b0SBen Greear 
38342935ecaSLuis R. Rodriguez 	ieee80211_queue_delayed_work(&local->hw,
38407ef03eeSJohannes Berg 				     &local->scan_work, 0);
385f3b85252SJohannes Berg 
386f3b85252SJohannes Berg 	return 0;
387f3b85252SJohannes Berg }
388f3b85252SJohannes Berg 
389133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local,
390133d40f9SStanislaw Gruszka 			       struct ieee80211_sub_if_data *sdata)
391133d40f9SStanislaw Gruszka {
392164eb02dSSimon Wunderlich 	if (local->radar_detect_enabled)
393164eb02dSSimon Wunderlich 		return false;
394164eb02dSSimon Wunderlich 
3952eb278e0SJohannes Berg 	if (!list_empty(&local->roc_list))
396133d40f9SStanislaw Gruszka 		return false;
397133d40f9SStanislaw Gruszka 
398133d40f9SStanislaw Gruszka 	if (sdata->vif.type == NL80211_IFTYPE_STATION &&
399392b9ffbSStanislaw Gruszka 	    sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
400133d40f9SStanislaw Gruszka 		return false;
401133d40f9SStanislaw Gruszka 
402133d40f9SStanislaw Gruszka 	return true;
403133d40f9SStanislaw Gruszka }
404133d40f9SStanislaw Gruszka 
405133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local)
406133d40f9SStanislaw Gruszka {
407133d40f9SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
408133d40f9SStanislaw Gruszka 
409133d40f9SStanislaw Gruszka 	if (!local->scan_req || local->scanning)
410133d40f9SStanislaw Gruszka 		return;
411133d40f9SStanislaw Gruszka 
412e2fd5dbcSJohannes Berg 	if (!ieee80211_can_scan(local,
413e2fd5dbcSJohannes Berg 				rcu_dereference_protected(
414e2fd5dbcSJohannes Berg 					local->scan_sdata,
415e2fd5dbcSJohannes Berg 					lockdep_is_held(&local->mtx))))
416133d40f9SStanislaw Gruszka 		return;
417133d40f9SStanislaw Gruszka 
418133d40f9SStanislaw Gruszka 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
419133d40f9SStanislaw Gruszka 				     round_jiffies_relative(0));
420133d40f9SStanislaw Gruszka }
421f3b85252SJohannes Berg 
4228a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
4238a690674SBen Greear 					    unsigned long *next_delay)
4248a690674SBen Greear {
4258a690674SBen Greear 	int i;
426e2fd5dbcSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
427675a0b04SKarl Beldan 	enum ieee80211_band band = local->hw.conf.chandef.chan->band;
4286c17b77bSSeth Forshee 	u32 tx_flags;
4296c17b77bSSeth Forshee 
4306c17b77bSSeth Forshee 	tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
4316c17b77bSSeth Forshee 	if (local->scan_req->no_cck)
4326c17b77bSSeth Forshee 		tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
4338a690674SBen Greear 
434e2fd5dbcSJohannes Berg 	sdata = rcu_dereference_protected(local->scan_sdata,
435316b6b5dSPeter Senna Tschudin 					  lockdep_is_held(&local->mtx));
436e2fd5dbcSJohannes Berg 
4378a690674SBen Greear 	for (i = 0; i < local->scan_req->n_ssids; i++)
4388a690674SBen Greear 		ieee80211_send_probe_req(
4398a690674SBen Greear 			sdata, NULL,
4408a690674SBen Greear 			local->scan_req->ssids[i].ssid,
4418a690674SBen Greear 			local->scan_req->ssids[i].ssid_len,
4428a690674SBen Greear 			local->scan_req->ie, local->scan_req->ie_len,
4438a690674SBen Greear 			local->scan_req->rates[band], false,
444675a0b04SKarl Beldan 			tx_flags, local->hw.conf.chandef.chan, true);
4458a690674SBen Greear 
4468a690674SBen Greear 	/*
4478a690674SBen Greear 	 * After sending probe requests, wait for probe responses
4488a690674SBen Greear 	 * on the channel.
4498a690674SBen Greear 	 */
4508a690674SBen Greear 	*next_delay = IEEE80211_CHANNEL_TIME;
4518a690674SBen Greear 	local->next_scan_state = SCAN_DECISION;
4528a690674SBen Greear }
4538a690674SBen Greear 
454f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
455f3b85252SJohannes Berg 				  struct cfg80211_scan_request *req)
456f3b85252SJohannes Berg {
457f3b85252SJohannes Berg 	struct ieee80211_local *local = sdata->local;
458f3b85252SJohannes Berg 	int rc;
459f3b85252SJohannes Berg 
460e229f844SStanislaw Gruszka 	lockdep_assert_held(&local->mtx);
461e229f844SStanislaw Gruszka 
462f3b85252SJohannes Berg 	if (local->scan_req)
463f3b85252SJohannes Berg 		return -EBUSY;
464f3b85252SJohannes Berg 
465133d40f9SStanislaw Gruszka 	if (!ieee80211_can_scan(local, sdata)) {
4666e7e6213SJohn W. Linville 		/* wait for the work to finish/time out */
467c0ce77b8SJohannes Berg 		local->scan_req = req;
468e2fd5dbcSJohannes Berg 		rcu_assign_pointer(local->scan_sdata, sdata);
469c0ce77b8SJohannes Berg 		return 0;
470c0ce77b8SJohannes Berg 	}
471c0ce77b8SJohannes Berg 
472f3b85252SJohannes Berg 	if (local->ops->hw_scan) {
473f3b85252SJohannes Berg 		u8 *ies;
474f3b85252SJohannes Berg 
475c604b9f2SJohannes Berg 		local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN +
476c604b9f2SJohannes Berg 					     local->scan_ies_len +
477c604b9f2SJohannes Berg 					     req->ie_len;
4784d36ec58SJohannes Berg 		local->hw_scan_req = kmalloc(
4794d36ec58SJohannes Berg 				sizeof(*local->hw_scan_req) +
4804d36ec58SJohannes Berg 				req->n_channels * sizeof(req->channels[0]) +
481c604b9f2SJohannes Berg 				local->hw_scan_ies_bufsize, GFP_KERNEL);
4824d36ec58SJohannes Berg 		if (!local->hw_scan_req)
483f3b85252SJohannes Berg 			return -ENOMEM;
484f3b85252SJohannes Berg 
4854d36ec58SJohannes Berg 		local->hw_scan_req->ssids = req->ssids;
4864d36ec58SJohannes Berg 		local->hw_scan_req->n_ssids = req->n_ssids;
4874d36ec58SJohannes Berg 		ies = (u8 *)local->hw_scan_req +
4884d36ec58SJohannes Berg 			sizeof(*local->hw_scan_req) +
4894d36ec58SJohannes Berg 			req->n_channels * sizeof(req->channels[0]);
4904d36ec58SJohannes Berg 		local->hw_scan_req->ie = ies;
491cd2bb512SSam Leffler 		local->hw_scan_req->flags = req->flags;
4924d36ec58SJohannes Berg 
4934d36ec58SJohannes Berg 		local->hw_scan_band = 0;
4946e7e6213SJohn W. Linville 
4956e7e6213SJohn W. Linville 		/*
4966e7e6213SJohn W. Linville 		 * After allocating local->hw_scan_req, we must
4976e7e6213SJohn W. Linville 		 * go through until ieee80211_prep_hw_scan(), so
4986e7e6213SJohn W. Linville 		 * anything that might be changed here and leave
4996e7e6213SJohn W. Linville 		 * this function early must not go after this
5006e7e6213SJohn W. Linville 		 * allocation.
5016e7e6213SJohn W. Linville 		 */
502f3b85252SJohannes Berg 	}
503f3b85252SJohannes Berg 
504f3b85252SJohannes Berg 	local->scan_req = req;
505e2fd5dbcSJohannes Berg 	rcu_assign_pointer(local->scan_sdata, sdata);
506f3b85252SJohannes Berg 
5078a690674SBen Greear 	if (local->ops->hw_scan) {
508fbe9c429SHelmut Schaa 		__set_bit(SCAN_HW_SCANNING, &local->scanning);
5098a690674SBen Greear 	} else if ((req->n_channels == 1) &&
510675a0b04SKarl Beldan 		   (req->channels[0] == local->_oper_chandef.chan)) {
5119b864870SJohannes Berg 		/*
5129b864870SJohannes Berg 		 * If we are scanning only on the operating channel
5139b864870SJohannes Berg 		 * then we do not need to stop normal activities
5148a690674SBen Greear 		 */
5158a690674SBen Greear 		unsigned long next_delay;
5168a690674SBen Greear 
5178a690674SBen Greear 		__set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
5188a690674SBen Greear 
5198a690674SBen Greear 		ieee80211_recalc_idle(local);
5208a690674SBen Greear 
5218a690674SBen Greear 		/* Notify driver scan is starting, keep order of operations
5228a690674SBen Greear 		 * same as normal software scan, in case that matters. */
5238a690674SBen Greear 		drv_sw_scan_start(local);
5248a690674SBen Greear 
5258a690674SBen Greear 		ieee80211_configure_filter(local); /* accept probe-responses */
5268a690674SBen Greear 
5278a690674SBen Greear 		/* We need to ensure power level is at max for scanning. */
5288a690674SBen Greear 		ieee80211_hw_config(local, 0);
5298a690674SBen Greear 
5308a690674SBen Greear 		if ((req->channels[0]->flags &
5318fe02e16SLuis R. Rodriguez 		     IEEE80211_CHAN_NO_IR) ||
5328a690674SBen Greear 		    !local->scan_req->n_ssids) {
5338a690674SBen Greear 			next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
5348a690674SBen Greear 		} else {
5358a690674SBen Greear 			ieee80211_scan_state_send_probe(local, &next_delay);
5368a690674SBen Greear 			next_delay = IEEE80211_CHANNEL_TIME;
5378a690674SBen Greear 		}
5388a690674SBen Greear 
5398a690674SBen Greear 		/* Now, just wait a bit and we are all done! */
5408a690674SBen Greear 		ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
5418a690674SBen Greear 					     next_delay);
5428a690674SBen Greear 		return 0;
5438a690674SBen Greear 	} else {
5448a690674SBen Greear 		/* Do normal software scan */
545fbe9c429SHelmut Schaa 		__set_bit(SCAN_SW_SCANNING, &local->scanning);
5468a690674SBen Greear 	}
5476e7e6213SJohn W. Linville 
5485cff20e6SJohannes Berg 	ieee80211_recalc_idle(local);
549f3b85252SJohannes Berg 
5504d36ec58SJohannes Berg 	if (local->ops->hw_scan) {
5514d36ec58SJohannes Berg 		WARN_ON(!ieee80211_prep_hw_scan(local));
552a060bbfeSJohannes Berg 		rc = drv_hw_scan(local, sdata, local->hw_scan_req);
5534d36ec58SJohannes Berg 	} else
554f3b85252SJohannes Berg 		rc = ieee80211_start_sw_scan(local);
555f3b85252SJohannes Berg 
556f3b85252SJohannes Berg 	if (rc) {
5574d36ec58SJohannes Berg 		kfree(local->hw_scan_req);
5584d36ec58SJohannes Berg 		local->hw_scan_req = NULL;
559fbe9c429SHelmut Schaa 		local->scanning = 0;
560f3b85252SJohannes Berg 
5615cff20e6SJohannes Berg 		ieee80211_recalc_idle(local);
5625cff20e6SJohannes Berg 
563f3b85252SJohannes Berg 		local->scan_req = NULL;
564e2fd5dbcSJohannes Berg 		rcu_assign_pointer(local->scan_sdata, NULL);
565f3b85252SJohannes Berg 	}
566f3b85252SJohannes Berg 
567f3b85252SJohannes Berg 	return rc;
568f3b85252SJohannes Berg }
569f3b85252SJohannes Berg 
570df13cce5SHelmut Schaa static unsigned long
571df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
572df13cce5SHelmut Schaa {
573df13cce5SHelmut Schaa 	/*
574df13cce5SHelmut Schaa 	 * TODO: channel switching also consumes quite some time,
575df13cce5SHelmut Schaa 	 * add that delay as well to get a better estimation
576df13cce5SHelmut Schaa 	 */
5778fe02e16SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_NO_IR)
578df13cce5SHelmut Schaa 		return IEEE80211_PASSIVE_CHANNEL_TIME;
579df13cce5SHelmut Schaa 	return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
580df13cce5SHelmut Schaa }
581df13cce5SHelmut Schaa 
582e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local,
5832fb3f028SHelmut Schaa 					  unsigned long *next_delay)
5842fb3f028SHelmut Schaa {
585142b9f50SHelmut Schaa 	bool associated = false;
586df13cce5SHelmut Schaa 	bool tx_empty = true;
587df13cce5SHelmut Schaa 	bool bad_latency;
588142b9f50SHelmut Schaa 	struct ieee80211_sub_if_data *sdata;
589df13cce5SHelmut Schaa 	struct ieee80211_channel *next_chan;
590cd2bb512SSam Leffler 	enum mac80211_scan_state next_scan_state;
591142b9f50SHelmut Schaa 
592df13cce5SHelmut Schaa 	/*
593df13cce5SHelmut Schaa 	 * check if at least one STA interface is associated,
594df13cce5SHelmut Schaa 	 * check if at least one STA interface has pending tx frames
595df13cce5SHelmut Schaa 	 * and grab the lowest used beacon interval
596df13cce5SHelmut Schaa 	 */
597142b9f50SHelmut Schaa 	mutex_lock(&local->iflist_mtx);
598142b9f50SHelmut Schaa 	list_for_each_entry(sdata, &local->interfaces, list) {
5999607e6b6SJohannes Berg 		if (!ieee80211_sdata_running(sdata))
600142b9f50SHelmut Schaa 			continue;
601142b9f50SHelmut Schaa 
602142b9f50SHelmut Schaa 		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
603142b9f50SHelmut Schaa 			if (sdata->u.mgd.associated) {
604142b9f50SHelmut Schaa 				associated = true;
605df13cce5SHelmut Schaa 
606df13cce5SHelmut Schaa 				if (!qdisc_all_tx_empty(sdata->dev)) {
607df13cce5SHelmut Schaa 					tx_empty = false;
608142b9f50SHelmut Schaa 					break;
609142b9f50SHelmut Schaa 				}
610142b9f50SHelmut Schaa 			}
611142b9f50SHelmut Schaa 		}
612df13cce5SHelmut Schaa 	}
613142b9f50SHelmut Schaa 	mutex_unlock(&local->iflist_mtx);
614142b9f50SHelmut Schaa 
615b23b025fSBen Greear 	next_chan = local->scan_req->channels[local->scan_channel_idx];
616b23b025fSBen Greear 
617142b9f50SHelmut Schaa 	/*
618142b9f50SHelmut Schaa 	 * we're currently scanning a different channel, let's
619df13cce5SHelmut Schaa 	 * see if we can scan another channel without interfering
620df13cce5SHelmut Schaa 	 * with the current traffic situation.
621df13cce5SHelmut Schaa 	 *
6223f892b61SStanislaw Gruszka 	 * Keep good latency, do not stay off-channel more than 125 ms.
623142b9f50SHelmut Schaa 	 */
624df13cce5SHelmut Schaa 
625df13cce5SHelmut Schaa 	bad_latency = time_after(jiffies +
626df13cce5SHelmut Schaa 				 ieee80211_scan_get_channel_time(next_chan),
6273f892b61SStanislaw Gruszka 				 local->leave_oper_channel_time + HZ / 8);
628df13cce5SHelmut Schaa 
629cd2bb512SSam Leffler 	if (associated && !tx_empty) {
630cd2bb512SSam Leffler 		if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
631cd2bb512SSam Leffler 			next_scan_state = SCAN_ABORT;
632142b9f50SHelmut Schaa 		else
633cd2bb512SSam Leffler 			next_scan_state = SCAN_SUSPEND;
6343f892b61SStanislaw Gruszka 	} else if (associated && bad_latency) {
635cd2bb512SSam Leffler 		next_scan_state = SCAN_SUSPEND;
636cd2bb512SSam Leffler 	} else {
637cd2bb512SSam Leffler 		next_scan_state = SCAN_SET_CHANNEL;
638cd2bb512SSam Leffler 	}
639cd2bb512SSam Leffler 
640cd2bb512SSam Leffler 	local->next_scan_state = next_scan_state;
641142b9f50SHelmut Schaa 
642142b9f50SHelmut Schaa 	*next_delay = 0;
6432fb3f028SHelmut Schaa }
6442fb3f028SHelmut Schaa 
6452fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
6467d3be3ccSHelmut Schaa 					     unsigned long *next_delay)
6477d3be3ccSHelmut Schaa {
6487d3be3ccSHelmut Schaa 	int skip;
6497d3be3ccSHelmut Schaa 	struct ieee80211_channel *chan;
6507ca15a0aSSimon Wunderlich 	enum nl80211_bss_scan_width oper_scan_width;
6517d3be3ccSHelmut Schaa 
6527d3be3ccSHelmut Schaa 	skip = 0;
6537d3be3ccSHelmut Schaa 	chan = local->scan_req->channels[local->scan_channel_idx];
6547d3be3ccSHelmut Schaa 
6557ca15a0aSSimon Wunderlich 	local->scan_chandef.chan = chan;
6567ca15a0aSSimon Wunderlich 	local->scan_chandef.center_freq1 = chan->center_freq;
6577ca15a0aSSimon Wunderlich 	local->scan_chandef.center_freq2 = 0;
6587ca15a0aSSimon Wunderlich 	switch (local->scan_req->scan_width) {
6597ca15a0aSSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_5:
6607ca15a0aSSimon Wunderlich 		local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
6617ca15a0aSSimon Wunderlich 		break;
6627ca15a0aSSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_10:
6637ca15a0aSSimon Wunderlich 		local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
6647ca15a0aSSimon Wunderlich 		break;
6657ca15a0aSSimon Wunderlich 	case NL80211_BSS_CHAN_WIDTH_20:
6667ca15a0aSSimon Wunderlich 		/* If scanning on oper channel, use whatever channel-type
6677ca15a0aSSimon Wunderlich 		 * is currently in use.
6687ca15a0aSSimon Wunderlich 		 */
6697ca15a0aSSimon Wunderlich 		oper_scan_width = cfg80211_chandef_to_scan_width(
6707ca15a0aSSimon Wunderlich 					&local->_oper_chandef);
6717ca15a0aSSimon Wunderlich 		if (chan == local->_oper_chandef.chan &&
6727ca15a0aSSimon Wunderlich 		    oper_scan_width == local->scan_req->scan_width)
6737ca15a0aSSimon Wunderlich 			local->scan_chandef = local->_oper_chandef;
6747ca15a0aSSimon Wunderlich 		else
6757ca15a0aSSimon Wunderlich 			local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
6767ca15a0aSSimon Wunderlich 		break;
6777ca15a0aSSimon Wunderlich 	}
678b23b025fSBen Greear 
679584991dcSJohannes Berg 	if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
6807d3be3ccSHelmut Schaa 		skip = 1;
6817d3be3ccSHelmut Schaa 
6827d3be3ccSHelmut Schaa 	/* advance state machine to next channel/band */
6837d3be3ccSHelmut Schaa 	local->scan_channel_idx++;
6847d3be3ccSHelmut Schaa 
6850ee9c13cSHelmut Schaa 	if (skip) {
6860ee9c13cSHelmut Schaa 		/* if we skip this channel return to the decision state */
6870ee9c13cSHelmut Schaa 		local->next_scan_state = SCAN_DECISION;
6882fb3f028SHelmut Schaa 		return;
6890ee9c13cSHelmut Schaa 	}
6907d3be3ccSHelmut Schaa 
6917d3be3ccSHelmut Schaa 	/*
6927d3be3ccSHelmut Schaa 	 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
6937d3be3ccSHelmut Schaa 	 * (which unfortunately doesn't say _why_ step a) is done,
6947d3be3ccSHelmut Schaa 	 * but it waits for the probe delay or until a frame is
6957d3be3ccSHelmut Schaa 	 * received - and the received frame would update the NAV).
6967d3be3ccSHelmut Schaa 	 * For now, we do not support waiting until a frame is
6977d3be3ccSHelmut Schaa 	 * received.
6987d3be3ccSHelmut Schaa 	 *
6997d3be3ccSHelmut Schaa 	 * In any case, it is not necessary for a passive scan.
7007d3be3ccSHelmut Schaa 	 */
7018fe02e16SLuis R. Rodriguez 	if (chan->flags & IEEE80211_CHAN_NO_IR ||
7027d3be3ccSHelmut Schaa 	    !local->scan_req->n_ssids) {
7037d3be3ccSHelmut Schaa 		*next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
704977923b0SHelmut Schaa 		local->next_scan_state = SCAN_DECISION;
7052fb3f028SHelmut Schaa 		return;
7067d3be3ccSHelmut Schaa 	}
7077d3be3ccSHelmut Schaa 
7082fb3f028SHelmut Schaa 	/* active scan, send probes */
7097d3be3ccSHelmut Schaa 	*next_delay = IEEE80211_PROBE_DELAY;
710977923b0SHelmut Schaa 	local->next_scan_state = SCAN_SEND_PROBE;
7117d3be3ccSHelmut Schaa }
7127d3be3ccSHelmut Schaa 
71307ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
71407ef03eeSJohannes Berg 					 unsigned long *next_delay)
71507ef03eeSJohannes Berg {
71607ef03eeSJohannes Berg 	/* switch back to the operating channel */
7177ca15a0aSSimon Wunderlich 	local->scan_chandef.chan = NULL;
71807ef03eeSJohannes Berg 	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
71907ef03eeSJohannes Berg 
720aacde9eeSStanislaw Gruszka 	/* disable PS */
721aacde9eeSStanislaw Gruszka 	ieee80211_offchannel_return(local);
72207ef03eeSJohannes Berg 
72307ef03eeSJohannes Berg 	*next_delay = HZ / 5;
72407ef03eeSJohannes Berg 	/* afterwards, resume scan & go to next channel */
72507ef03eeSJohannes Berg 	local->next_scan_state = SCAN_RESUME;
72607ef03eeSJohannes Berg }
72707ef03eeSJohannes Berg 
72807ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local,
72907ef03eeSJohannes Berg 					unsigned long *next_delay)
73007ef03eeSJohannes Berg {
731aacde9eeSStanislaw Gruszka 	ieee80211_offchannel_stop_vifs(local);
73207ef03eeSJohannes Berg 
73307ef03eeSJohannes Berg 	if (local->ops->flush) {
73439ecc01dSJohannes Berg 		ieee80211_flush_queues(local, NULL);
73507ef03eeSJohannes Berg 		*next_delay = 0;
73607ef03eeSJohannes Berg 	} else
73707ef03eeSJohannes Berg 		*next_delay = HZ / 10;
73807ef03eeSJohannes Berg 
73907ef03eeSJohannes Berg 	/* remember when we left the operating channel */
74007ef03eeSJohannes Berg 	local->leave_oper_channel_time = jiffies;
74107ef03eeSJohannes Berg 
74207ef03eeSJohannes Berg 	/* advance to the next channel to be scanned */
743de2ee84dSMohammed Shafi Shajakhan 	local->next_scan_state = SCAN_SET_CHANNEL;
74407ef03eeSJohannes Berg }
74507ef03eeSJohannes Berg 
746c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work)
7470a51b27eSJohannes Berg {
7480a51b27eSJohannes Berg 	struct ieee80211_local *local =
7490a51b27eSJohannes Berg 		container_of(work, struct ieee80211_local, scan_work.work);
750d07bfd8bSJohannes Berg 	struct ieee80211_sub_if_data *sdata;
7510a51b27eSJohannes Berg 	unsigned long next_delay = 0;
7528bd2a248SEliad Peller 	bool aborted;
7538789d459SJohannes Berg 
754259b62e3SStanislaw Gruszka 	mutex_lock(&local->mtx);
755259b62e3SStanislaw Gruszka 
756e2fd5dbcSJohannes Berg 	sdata = rcu_dereference_protected(local->scan_sdata,
757e2fd5dbcSJohannes Berg 					  lockdep_is_held(&local->mtx));
758d07bfd8bSJohannes Berg 
7598a690674SBen Greear 	/* When scanning on-channel, the first-callback means completed. */
7608a690674SBen Greear 	if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
7618a690674SBen Greear 		aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
7628a690674SBen Greear 		goto out_complete;
7638a690674SBen Greear 	}
7648a690674SBen Greear 
765259b62e3SStanislaw Gruszka 	if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
7668789d459SJohannes Berg 		aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
767259b62e3SStanislaw Gruszka 		goto out_complete;
7688789d459SJohannes Berg 	}
7698789d459SJohannes Berg 
770259b62e3SStanislaw Gruszka 	if (!sdata || !local->scan_req)
771259b62e3SStanislaw Gruszka 		goto out;
772f3b85252SJohannes Berg 
773fbe9c429SHelmut Schaa 	if (local->scan_req && !local->scanning) {
774f3b85252SJohannes Berg 		struct cfg80211_scan_request *req = local->scan_req;
775f3b85252SJohannes Berg 		int rc;
776f3b85252SJohannes Berg 
777f3b85252SJohannes Berg 		local->scan_req = NULL;
778e2fd5dbcSJohannes Berg 		rcu_assign_pointer(local->scan_sdata, NULL);
779f3b85252SJohannes Berg 
780f3b85252SJohannes Berg 		rc = __ieee80211_start_scan(sdata, req);
781259b62e3SStanislaw Gruszka 		if (rc) {
7823aed49efSStanislaw Gruszka 			/* need to complete scan in cfg80211 */
7833aed49efSStanislaw Gruszka 			local->scan_req = req;
784259b62e3SStanislaw Gruszka 			aborted = true;
785259b62e3SStanislaw Gruszka 			goto out_complete;
786259b62e3SStanislaw Gruszka 		} else
787259b62e3SStanislaw Gruszka 			goto out;
788f3b85252SJohannes Berg 	}
789f3b85252SJohannes Berg 
7905bc75728SJohannes Berg 	/*
791f502d09bSHelmut Schaa 	 * as long as no delay is required advance immediately
792f502d09bSHelmut Schaa 	 * without scheduling a new work
793f502d09bSHelmut Schaa 	 */
794f502d09bSHelmut Schaa 	do {
795c29acf20SRajkumar Manoharan 		if (!ieee80211_sdata_running(sdata)) {
796c29acf20SRajkumar Manoharan 			aborted = true;
797c29acf20SRajkumar Manoharan 			goto out_complete;
798c29acf20SRajkumar Manoharan 		}
799c29acf20SRajkumar Manoharan 
800977923b0SHelmut Schaa 		switch (local->next_scan_state) {
8012fb3f028SHelmut Schaa 		case SCAN_DECISION:
802e229f844SStanislaw Gruszka 			/* if no more bands/channels left, complete scan */
803e229f844SStanislaw Gruszka 			if (local->scan_channel_idx >= local->scan_req->n_channels) {
804e229f844SStanislaw Gruszka 				aborted = false;
805e229f844SStanislaw Gruszka 				goto out_complete;
806e229f844SStanislaw Gruszka 			}
807e229f844SStanislaw Gruszka 			ieee80211_scan_state_decision(local, &next_delay);
8080a51b27eSJohannes Berg 			break;
8092fb3f028SHelmut Schaa 		case SCAN_SET_CHANNEL:
8102fb3f028SHelmut Schaa 			ieee80211_scan_state_set_channel(local, &next_delay);
8112fb3f028SHelmut Schaa 			break;
8120a51b27eSJohannes Berg 		case SCAN_SEND_PROBE:
8137d3be3ccSHelmut Schaa 			ieee80211_scan_state_send_probe(local, &next_delay);
8140a51b27eSJohannes Berg 			break;
81507ef03eeSJohannes Berg 		case SCAN_SUSPEND:
81607ef03eeSJohannes Berg 			ieee80211_scan_state_suspend(local, &next_delay);
817142b9f50SHelmut Schaa 			break;
81807ef03eeSJohannes Berg 		case SCAN_RESUME:
81907ef03eeSJohannes Berg 			ieee80211_scan_state_resume(local, &next_delay);
820142b9f50SHelmut Schaa 			break;
821cd2bb512SSam Leffler 		case SCAN_ABORT:
822cd2bb512SSam Leffler 			aborted = true;
823cd2bb512SSam Leffler 			goto out_complete;
8240a51b27eSJohannes Berg 		}
825f502d09bSHelmut Schaa 	} while (next_delay == 0);
8260a51b27eSJohannes Berg 
82742935ecaSLuis R. Rodriguez 	ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
828d07bfd8bSJohannes Berg 	goto out;
829259b62e3SStanislaw Gruszka 
830259b62e3SStanislaw Gruszka out_complete:
8318bd2a248SEliad Peller 	__ieee80211_scan_completed(&local->hw, aborted);
832259b62e3SStanislaw Gruszka out:
833259b62e3SStanislaw Gruszka 	mutex_unlock(&local->mtx);
8340a51b27eSJohannes Berg }
8350a51b27eSJohannes Berg 
836c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
8372a519311SJohannes Berg 			   struct cfg80211_scan_request *req)
8380a51b27eSJohannes Berg {
839f3b85252SJohannes Berg 	int res;
8400a51b27eSJohannes Berg 
841a1699b75SJohannes Berg 	mutex_lock(&sdata->local->mtx);
842f3b85252SJohannes Berg 	res = __ieee80211_start_scan(sdata, req);
843a1699b75SJohannes Berg 	mutex_unlock(&sdata->local->mtx);
8442a519311SJohannes Berg 
845f3b85252SJohannes Berg 	return res;
8460a51b27eSJohannes Berg }
8470a51b27eSJohannes Berg 
84834bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
849be4a4b6aSJohannes Berg 				const u8 *ssid, u8 ssid_len,
8507ca15a0aSSimon Wunderlich 				struct ieee80211_channel *chan,
8517ca15a0aSSimon Wunderlich 				enum nl80211_bss_scan_width scan_width)
852f3b85252SJohannes Berg {
853f3b85252SJohannes Berg 	struct ieee80211_local *local = sdata->local;
854f3b85252SJohannes Berg 	int ret = -EBUSY;
855fb63bc41SGertjan van Wingerde 	enum ieee80211_band band;
8569116dd01SJohannes Berg 
857a1699b75SJohannes Berg 	mutex_lock(&local->mtx);
858f3b85252SJohannes Berg 
859f3b85252SJohannes Berg 	/* busy scanning */
860f3b85252SJohannes Berg 	if (local->scan_req)
861f3b85252SJohannes Berg 		goto unlock;
862f3b85252SJohannes Berg 
863be4a4b6aSJohannes Berg 	/* fill internal scan request */
864be4a4b6aSJohannes Berg 	if (!chan) {
86534bcf715SStanislaw Gruszka 		int i, max_n;
86634bcf715SStanislaw Gruszka 		int n_ch = 0;
867be4a4b6aSJohannes Berg 
868be4a4b6aSJohannes Berg 		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
869be4a4b6aSJohannes Berg 			if (!local->hw.wiphy->bands[band])
870be4a4b6aSJohannes Berg 				continue;
87134bcf715SStanislaw Gruszka 
87234bcf715SStanislaw Gruszka 			max_n = local->hw.wiphy->bands[band]->n_channels;
87334bcf715SStanislaw Gruszka 			for (i = 0; i < max_n; i++) {
87434bcf715SStanislaw Gruszka 				struct ieee80211_channel *tmp_ch =
875be4a4b6aSJohannes Berg 				    &local->hw.wiphy->bands[band]->channels[i];
87634bcf715SStanislaw Gruszka 
8778fe02e16SLuis R. Rodriguez 				if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
87834bcf715SStanislaw Gruszka 						     IEEE80211_CHAN_DISABLED))
87934bcf715SStanislaw Gruszka 					continue;
88034bcf715SStanislaw Gruszka 
88134bcf715SStanislaw Gruszka 				local->int_scan_req->channels[n_ch] = tmp_ch;
88234bcf715SStanislaw Gruszka 				n_ch++;
883be4a4b6aSJohannes Berg 			}
884be4a4b6aSJohannes Berg 		}
885be4a4b6aSJohannes Berg 
88634bcf715SStanislaw Gruszka 		if (WARN_ON_ONCE(n_ch == 0))
88734bcf715SStanislaw Gruszka 			goto unlock;
88834bcf715SStanislaw Gruszka 
88934bcf715SStanislaw Gruszka 		local->int_scan_req->n_channels = n_ch;
890be4a4b6aSJohannes Berg 	} else {
8918fe02e16SLuis R. Rodriguez 		if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IR |
89234bcf715SStanislaw Gruszka 						IEEE80211_CHAN_DISABLED)))
89334bcf715SStanislaw Gruszka 			goto unlock;
89434bcf715SStanislaw Gruszka 
895be4a4b6aSJohannes Berg 		local->int_scan_req->channels[0] = chan;
896be4a4b6aSJohannes Berg 		local->int_scan_req->n_channels = 1;
897be4a4b6aSJohannes Berg 	}
898be4a4b6aSJohannes Berg 
899be4a4b6aSJohannes Berg 	local->int_scan_req->ssids = &local->scan_ssid;
900be4a4b6aSJohannes Berg 	local->int_scan_req->n_ssids = 1;
9017ca15a0aSSimon Wunderlich 	local->int_scan_req->scan_width = scan_width;
9025ba63533SJohannes Berg 	memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
9035ba63533SJohannes Berg 	local->int_scan_req->ssids[0].ssid_len = ssid_len;
904f3b85252SJohannes Berg 
9055ba63533SJohannes Berg 	ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
906f3b85252SJohannes Berg  unlock:
907a1699b75SJohannes Berg 	mutex_unlock(&local->mtx);
908f3b85252SJohannes Berg 	return ret;
9090a51b27eSJohannes Berg }
9105bb644a0SJohannes Berg 
9114136c422SStanislaw Gruszka /*
9124136c422SStanislaw Gruszka  * Only call this function when a scan can't be queued -- under RTNL.
9134136c422SStanislaw Gruszka  */
9145bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local)
9155bb644a0SJohannes Berg {
9165bb644a0SJohannes Berg 	/*
917b856439bSEliad Peller 	 * We are canceling software scan, or deferred scan that was not
9184136c422SStanislaw Gruszka 	 * yet really started (see __ieee80211_start_scan ).
9194136c422SStanislaw Gruszka 	 *
9204136c422SStanislaw Gruszka 	 * Regarding hardware scan:
9214136c422SStanislaw Gruszka 	 * - we can not call  __ieee80211_scan_completed() as when
9224136c422SStanislaw Gruszka 	 *   SCAN_HW_SCANNING bit is set this function change
9234136c422SStanislaw Gruszka 	 *   local->hw_scan_req to operate on 5G band, what race with
9244136c422SStanislaw Gruszka 	 *   driver which can use local->hw_scan_req
9254136c422SStanislaw Gruszka 	 *
9264136c422SStanislaw Gruszka 	 * - we can not cancel scan_work since driver can schedule it
9274136c422SStanislaw Gruszka 	 *   by ieee80211_scan_completed(..., true) to finish scan
9284136c422SStanislaw Gruszka 	 *
929b856439bSEliad Peller 	 * Hence we only call the cancel_hw_scan() callback, but the low-level
930b856439bSEliad Peller 	 * driver is still responsible for calling ieee80211_scan_completed()
931b856439bSEliad Peller 	 * after the scan was completed/aborted.
9325bb644a0SJohannes Berg 	 */
9334136c422SStanislaw Gruszka 
934a1699b75SJohannes Berg 	mutex_lock(&local->mtx);
935b856439bSEliad Peller 	if (!local->scan_req)
936b856439bSEliad Peller 		goto out;
937b856439bSEliad Peller 
938a754055aSEmmanuel Grumbach 	/*
939a754055aSEmmanuel Grumbach 	 * We have a scan running and the driver already reported completion,
940a754055aSEmmanuel Grumbach 	 * but the worker hasn't run yet or is stuck on the mutex - mark it as
941a754055aSEmmanuel Grumbach 	 * cancelled.
942a754055aSEmmanuel Grumbach 	 */
943a754055aSEmmanuel Grumbach 	if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
944a754055aSEmmanuel Grumbach 	    test_bit(SCAN_COMPLETED, &local->scanning)) {
945a754055aSEmmanuel Grumbach 		set_bit(SCAN_HW_CANCELLED, &local->scanning);
946a754055aSEmmanuel Grumbach 		goto out;
947a754055aSEmmanuel Grumbach 	}
948a754055aSEmmanuel Grumbach 
949b856439bSEliad Peller 	if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
950a754055aSEmmanuel Grumbach 		/*
951a754055aSEmmanuel Grumbach 		 * Make sure that __ieee80211_scan_completed doesn't trigger a
952a754055aSEmmanuel Grumbach 		 * scan on another band.
953a754055aSEmmanuel Grumbach 		 */
954a754055aSEmmanuel Grumbach 		set_bit(SCAN_HW_CANCELLED, &local->scanning);
955b856439bSEliad Peller 		if (local->ops->cancel_hw_scan)
956e2fd5dbcSJohannes Berg 			drv_cancel_hw_scan(local,
957e2fd5dbcSJohannes Berg 				rcu_dereference_protected(local->scan_sdata,
958e2fd5dbcSJohannes Berg 						lockdep_is_held(&local->mtx)));
959b856439bSEliad Peller 		goto out;
960b856439bSEliad Peller 	}
961b856439bSEliad Peller 
962d07bfd8bSJohannes Berg 	/*
963d07bfd8bSJohannes Berg 	 * If the work is currently running, it must be blocked on
964d07bfd8bSJohannes Berg 	 * the mutex, but we'll set scan_sdata = NULL and it'll
965d07bfd8bSJohannes Berg 	 * simply exit once it acquires the mutex.
966d07bfd8bSJohannes Berg 	 */
967d07bfd8bSJohannes Berg 	cancel_delayed_work(&local->scan_work);
968d07bfd8bSJohannes Berg 	/* and clean up */
9698bd2a248SEliad Peller 	__ieee80211_scan_completed(&local->hw, true);
970b856439bSEliad Peller out:
971d07bfd8bSJohannes Berg 	mutex_unlock(&local->mtx);
9725bb644a0SJohannes Berg }
97379f460caSLuciano Coelho 
974*d43c6b6eSDavid Spinadel int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
97579f460caSLuciano Coelho 					struct cfg80211_sched_scan_request *req)
97679f460caSLuciano Coelho {
97779f460caSLuciano Coelho 	struct ieee80211_local *local = sdata->local;
978bca1e29fSDavid Spinadel 	struct ieee80211_sched_scan_ies sched_scan_ies = {};
9792103dec1SSimon Wunderlich 	struct cfg80211_chan_def chandef;
980c604b9f2SJohannes Berg 	int ret, i, iebufsz;
981c604b9f2SJohannes Berg 
982c604b9f2SJohannes Berg 	iebufsz = 2 + IEEE80211_MAX_SSID_LEN +
983c604b9f2SJohannes Berg 		  local->scan_ies_len + req->ie_len;
98479f460caSLuciano Coelho 
985*d43c6b6eSDavid Spinadel 	lockdep_assert_held(&local->mtx);
98679f460caSLuciano Coelho 
987*d43c6b6eSDavid Spinadel 	if (!local->ops->sched_scan_start)
988*d43c6b6eSDavid Spinadel 		return -ENOTSUPP;
98979f460caSLuciano Coelho 
99079f460caSLuciano Coelho 	for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
991d811b3d5SArik Nemtsov 		if (!local->hw.wiphy->bands[i])
992d811b3d5SArik Nemtsov 			continue;
993d811b3d5SArik Nemtsov 
994c604b9f2SJohannes Berg 		sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL);
99530dd3edfSJohannes Berg 		if (!sched_scan_ies.ie[i]) {
99679f460caSLuciano Coelho 			ret = -ENOMEM;
99779f460caSLuciano Coelho 			goto out_free;
99879f460caSLuciano Coelho 		}
99979f460caSLuciano Coelho 
10002103dec1SSimon Wunderlich 		ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
10012103dec1SSimon Wunderlich 
100230dd3edfSJohannes Berg 		sched_scan_ies.len[i] =
100330dd3edfSJohannes Berg 			ieee80211_build_preq_ies(local, sched_scan_ies.ie[i],
1004c604b9f2SJohannes Berg 						 iebufsz, req->ie, req->ie_len,
10052103dec1SSimon Wunderlich 						 i, (u32) -1, &chandef);
100679f460caSLuciano Coelho 	}
100779f460caSLuciano Coelho 
100830dd3edfSJohannes Berg 	ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
1009*d43c6b6eSDavid Spinadel 	if (ret == 0) {
10105260a5b2SJohannes Berg 		rcu_assign_pointer(local->sched_scan_sdata, sdata);
1011*d43c6b6eSDavid Spinadel 		local->sched_scan_req = req;
1012*d43c6b6eSDavid Spinadel 	}
101379f460caSLuciano Coelho 
101479f460caSLuciano Coelho out_free:
101579f460caSLuciano Coelho 	while (i > 0)
101630dd3edfSJohannes Berg 		kfree(sched_scan_ies.ie[--i]);
1017*d43c6b6eSDavid Spinadel 
1018*d43c6b6eSDavid Spinadel 	if (ret) {
1019*d43c6b6eSDavid Spinadel 		/* Clean in case of failure after HW restart or upon resume. */
1020*d43c6b6eSDavid Spinadel 		rcu_assign_pointer(local->sched_scan_sdata, NULL);
1021*d43c6b6eSDavid Spinadel 		local->sched_scan_req = NULL;
1022*d43c6b6eSDavid Spinadel 	}
1023*d43c6b6eSDavid Spinadel 
1024*d43c6b6eSDavid Spinadel 	return ret;
1025*d43c6b6eSDavid Spinadel }
1026*d43c6b6eSDavid Spinadel 
1027*d43c6b6eSDavid Spinadel int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1028*d43c6b6eSDavid Spinadel 				       struct cfg80211_sched_scan_request *req)
1029*d43c6b6eSDavid Spinadel {
1030*d43c6b6eSDavid Spinadel 	struct ieee80211_local *local = sdata->local;
1031*d43c6b6eSDavid Spinadel 	int ret;
1032*d43c6b6eSDavid Spinadel 
1033*d43c6b6eSDavid Spinadel 	mutex_lock(&local->mtx);
1034*d43c6b6eSDavid Spinadel 
1035*d43c6b6eSDavid Spinadel 	if (rcu_access_pointer(local->sched_scan_sdata)) {
1036*d43c6b6eSDavid Spinadel 		mutex_unlock(&local->mtx);
1037*d43c6b6eSDavid Spinadel 		return -EBUSY;
1038*d43c6b6eSDavid Spinadel 	}
1039*d43c6b6eSDavid Spinadel 
1040*d43c6b6eSDavid Spinadel 	ret = __ieee80211_request_sched_scan_start(sdata, req);
1041*d43c6b6eSDavid Spinadel 
10425260a5b2SJohannes Berg 	mutex_unlock(&local->mtx);
104379f460caSLuciano Coelho 	return ret;
104479f460caSLuciano Coelho }
104579f460caSLuciano Coelho 
104685a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
104779f460caSLuciano Coelho {
104879f460caSLuciano Coelho 	struct ieee80211_local *local = sdata->local;
104930dd3edfSJohannes Berg 	int ret = 0;
105079f460caSLuciano Coelho 
10515260a5b2SJohannes Berg 	mutex_lock(&local->mtx);
105279f460caSLuciano Coelho 
105379f460caSLuciano Coelho 	if (!local->ops->sched_scan_stop) {
105479f460caSLuciano Coelho 		ret = -ENOTSUPP;
105579f460caSLuciano Coelho 		goto out;
105679f460caSLuciano Coelho 	}
105779f460caSLuciano Coelho 
1058*d43c6b6eSDavid Spinadel 	/* We don't want to restart sched scan anymore. */
1059*d43c6b6eSDavid Spinadel 	local->sched_scan_req = NULL;
1060*d43c6b6eSDavid Spinadel 
106130dd3edfSJohannes Berg 	if (rcu_access_pointer(local->sched_scan_sdata))
106279f460caSLuciano Coelho 		drv_sched_scan_stop(local, sdata);
106330dd3edfSJohannes Berg 
106479f460caSLuciano Coelho out:
10655260a5b2SJohannes Berg 	mutex_unlock(&local->mtx);
106679f460caSLuciano Coelho 
106779f460caSLuciano Coelho 	return ret;
106879f460caSLuciano Coelho }
106979f460caSLuciano Coelho 
107079f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
107179f460caSLuciano Coelho {
107279f460caSLuciano Coelho 	struct ieee80211_local *local = hw_to_local(hw);
107379f460caSLuciano Coelho 
107479f460caSLuciano Coelho 	trace_api_sched_scan_results(local);
107579f460caSLuciano Coelho 
107679f460caSLuciano Coelho 	cfg80211_sched_scan_results(hw->wiphy);
107779f460caSLuciano Coelho }
107879f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results);
107979f460caSLuciano Coelho 
108085a9994aSLuciano Coelho void ieee80211_sched_scan_stopped_work(struct work_struct *work)
108185a9994aSLuciano Coelho {
108285a9994aSLuciano Coelho 	struct ieee80211_local *local =
108385a9994aSLuciano Coelho 		container_of(work, struct ieee80211_local,
108485a9994aSLuciano Coelho 			     sched_scan_stopped_work);
108585a9994aSLuciano Coelho 
108685a9994aSLuciano Coelho 	mutex_lock(&local->mtx);
108785a9994aSLuciano Coelho 
10885260a5b2SJohannes Berg 	if (!rcu_access_pointer(local->sched_scan_sdata)) {
108985a9994aSLuciano Coelho 		mutex_unlock(&local->mtx);
109085a9994aSLuciano Coelho 		return;
109185a9994aSLuciano Coelho 	}
109285a9994aSLuciano Coelho 
10935260a5b2SJohannes Berg 	rcu_assign_pointer(local->sched_scan_sdata, NULL);
109485a9994aSLuciano Coelho 
1095*d43c6b6eSDavid Spinadel 	/* If sched scan was aborted by the driver. */
1096*d43c6b6eSDavid Spinadel 	local->sched_scan_req = NULL;
1097*d43c6b6eSDavid Spinadel 
109885a9994aSLuciano Coelho 	mutex_unlock(&local->mtx);
109985a9994aSLuciano Coelho 
110085a9994aSLuciano Coelho 	cfg80211_sched_scan_stopped(local->hw.wiphy);
110185a9994aSLuciano Coelho }
110285a9994aSLuciano Coelho 
110379f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
110479f460caSLuciano Coelho {
110579f460caSLuciano Coelho 	struct ieee80211_local *local = hw_to_local(hw);
110679f460caSLuciano Coelho 
110779f460caSLuciano Coelho 	trace_api_sched_scan_stopped(local);
110879f460caSLuciano Coelho 
110918db594aSJohannes Berg 	schedule_work(&local->sched_scan_stopped_work);
111079f460caSLuciano Coelho }
111179f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped);
1112