mac80211: select and adjust bitrates according to channel mode
[linux-2.6.git] / net / mac80211 / scan.c
blob819d0956eb3b6099587f0302032310d79f1bb508
1 /*
2 * Scanning implementation
4 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/if_arp.h>
16 #include <linux/etherdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/pm_qos.h>
19 #include <net/sch_generic.h>
20 #include <linux/slab.h>
21 #include <linux/export.h>
22 #include <net/mac80211.h>
24 #include "ieee80211_i.h"
25 #include "driver-ops.h"
26 #include "mesh.h"
28 #define IEEE80211_PROBE_DELAY (HZ / 33)
29 #define IEEE80211_CHANNEL_TIME (HZ / 33)
30 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
32 void ieee80211_rx_bss_put(struct ieee80211_local *local,
33 struct ieee80211_bss *bss)
35 if (!bss)
36 return;
37 cfg80211_put_bss(local->hw.wiphy,
38 container_of((void *)bss, struct cfg80211_bss, priv));
41 static bool is_uapsd_supported(struct ieee802_11_elems *elems)
43 u8 qos_info;
45 if (elems->wmm_info && elems->wmm_info_len == 7
46 && elems->wmm_info[5] == 1)
47 qos_info = elems->wmm_info[6];
48 else if (elems->wmm_param && elems->wmm_param_len == 24
49 && elems->wmm_param[5] == 1)
50 qos_info = elems->wmm_param[6];
51 else
52 /* no valid wmm information or parameter element found */
53 return false;
55 return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
58 struct ieee80211_bss *
59 ieee80211_bss_info_update(struct ieee80211_local *local,
60 struct ieee80211_rx_status *rx_status,
61 struct ieee80211_mgmt *mgmt, size_t len,
62 struct ieee802_11_elems *elems,
63 struct ieee80211_channel *channel)
65 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
66 struct cfg80211_bss *cbss;
67 struct ieee80211_bss *bss;
68 int clen, srlen;
69 s32 signal = 0;
71 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
72 signal = rx_status->signal * 100;
73 else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
74 signal = (rx_status->signal * 100) / local->hw.max_signal;
76 cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
77 mgmt, len, signal, GFP_ATOMIC);
78 if (!cbss)
79 return NULL;
81 bss = (void *)cbss->priv;
83 if (beacon)
84 bss->device_ts_beacon = rx_status->device_timestamp;
85 else
86 bss->device_ts_presp = rx_status->device_timestamp;
88 if (elems->parse_error) {
89 if (beacon)
90 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
91 else
92 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
93 } else {
94 if (beacon)
95 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
96 else
97 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
100 /* save the ERP value so that it is available at association time */
101 if (elems->erp_info && (!elems->parse_error ||
102 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
103 bss->erp_value = elems->erp_info[0];
104 bss->has_erp_value = true;
105 if (!elems->parse_error)
106 bss->valid_data |= IEEE80211_BSS_VALID_ERP;
109 /* replace old supported rates if we get new values */
110 if (!elems->parse_error ||
111 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
112 srlen = 0;
113 if (elems->supp_rates) {
114 clen = IEEE80211_MAX_SUPP_RATES;
115 if (clen > elems->supp_rates_len)
116 clen = elems->supp_rates_len;
117 memcpy(bss->supp_rates, elems->supp_rates, clen);
118 srlen += clen;
120 if (elems->ext_supp_rates) {
121 clen = IEEE80211_MAX_SUPP_RATES - srlen;
122 if (clen > elems->ext_supp_rates_len)
123 clen = elems->ext_supp_rates_len;
124 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
125 clen);
126 srlen += clen;
128 if (srlen) {
129 bss->supp_rates_len = srlen;
130 if (!elems->parse_error)
131 bss->valid_data |= IEEE80211_BSS_VALID_RATES;
135 if (!elems->parse_error ||
136 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
137 bss->wmm_used = elems->wmm_param || elems->wmm_info;
138 bss->uapsd_supported = is_uapsd_supported(elems);
139 if (!elems->parse_error)
140 bss->valid_data |= IEEE80211_BSS_VALID_WMM;
143 if (beacon) {
144 struct ieee80211_supported_band *sband =
145 local->hw.wiphy->bands[rx_status->band];
146 if (!(rx_status->flag & RX_FLAG_HT) &&
147 !(rx_status->flag & RX_FLAG_VHT))
148 bss->beacon_rate =
149 &sband->bitrates[rx_status->rate_idx];
152 return bss;
155 void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
157 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
158 struct ieee80211_sub_if_data *sdata1, *sdata2;
159 struct ieee80211_mgmt *mgmt = (void *)skb->data;
160 struct ieee80211_bss *bss;
161 u8 *elements;
162 struct ieee80211_channel *channel;
163 size_t baselen;
164 struct ieee802_11_elems elems;
166 if (skb->len < 24 ||
167 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
168 !ieee80211_is_beacon(mgmt->frame_control)))
169 return;
171 sdata1 = rcu_dereference(local->scan_sdata);
172 sdata2 = rcu_dereference(local->sched_scan_sdata);
174 if (likely(!sdata1 && !sdata2))
175 return;
177 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
178 /* ignore ProbeResp to foreign address */
179 if ((!sdata1 || !ether_addr_equal(mgmt->da, sdata1->vif.addr)) &&
180 (!sdata2 || !ether_addr_equal(mgmt->da, sdata2->vif.addr)))
181 return;
183 elements = mgmt->u.probe_resp.variable;
184 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
185 } else {
186 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
187 elements = mgmt->u.beacon.variable;
190 if (baselen > skb->len)
191 return;
193 ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems);
195 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
197 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
198 return;
200 bss = ieee80211_bss_info_update(local, rx_status,
201 mgmt, skb->len, &elems,
202 channel);
203 if (bss)
204 ieee80211_rx_bss_put(local, bss);
207 static void
208 ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
209 enum nl80211_bss_scan_width scan_width)
211 memset(chandef, 0, sizeof(*chandef));
212 switch (scan_width) {
213 case NL80211_BSS_CHAN_WIDTH_5:
214 chandef->width = NL80211_CHAN_WIDTH_5;
215 break;
216 case NL80211_BSS_CHAN_WIDTH_10:
217 chandef->width = NL80211_CHAN_WIDTH_10;
218 break;
219 default:
220 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
221 break;
225 /* return false if no more work */
226 static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
228 struct cfg80211_scan_request *req = local->scan_req;
229 struct cfg80211_chan_def chandef;
230 enum ieee80211_band band;
231 int i, ielen, n_chans;
233 do {
234 if (local->hw_scan_band == IEEE80211_NUM_BANDS)
235 return false;
237 band = local->hw_scan_band;
238 n_chans = 0;
239 for (i = 0; i < req->n_channels; i++) {
240 if (req->channels[i]->band == band) {
241 local->hw_scan_req->channels[n_chans] =
242 req->channels[i];
243 n_chans++;
247 local->hw_scan_band++;
248 } while (!n_chans);
250 local->hw_scan_req->n_channels = n_chans;
251 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
253 ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
254 local->hw_scan_ies_bufsize,
255 req->ie, req->ie_len, band,
256 req->rates[band], &chandef);
257 local->hw_scan_req->ie_len = ielen;
258 local->hw_scan_req->no_cck = req->no_cck;
260 return true;
263 static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted,
264 bool was_hw_scan)
266 struct ieee80211_local *local = hw_to_local(hw);
268 lockdep_assert_held(&local->mtx);
271 * It's ok to abort a not-yet-running scan (that
272 * we have one at all will be verified by checking
273 * local->scan_req next), but not to complete it
274 * successfully.
276 if (WARN_ON(!local->scanning && !aborted))
277 aborted = true;
279 if (WARN_ON(!local->scan_req))
280 return;
282 if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
283 int rc;
285 rc = drv_hw_scan(local,
286 rcu_dereference_protected(local->scan_sdata,
287 lockdep_is_held(&local->mtx)),
288 local->hw_scan_req);
290 if (rc == 0)
291 return;
294 kfree(local->hw_scan_req);
295 local->hw_scan_req = NULL;
297 if (local->scan_req != local->int_scan_req)
298 cfg80211_scan_done(local->scan_req, aborted);
299 local->scan_req = NULL;
300 rcu_assign_pointer(local->scan_sdata, NULL);
302 local->scanning = 0;
303 local->scan_channel = NULL;
305 /* Set power back to normal operating levels. */
306 ieee80211_hw_config(local, 0);
308 if (!was_hw_scan) {
309 ieee80211_configure_filter(local);
310 drv_sw_scan_complete(local);
311 ieee80211_offchannel_return(local);
314 ieee80211_recalc_idle(local);
316 ieee80211_mlme_notify_scan_completed(local);
317 ieee80211_ibss_notify_scan_completed(local);
318 ieee80211_mesh_notify_scan_completed(local);
319 ieee80211_start_next_roc(local);
322 void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
324 struct ieee80211_local *local = hw_to_local(hw);
326 trace_api_scan_completed(local, aborted);
328 set_bit(SCAN_COMPLETED, &local->scanning);
329 if (aborted)
330 set_bit(SCAN_ABORTED, &local->scanning);
331 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
333 EXPORT_SYMBOL(ieee80211_scan_completed);
335 static int ieee80211_start_sw_scan(struct ieee80211_local *local)
337 /* Software scan is not supported in multi-channel cases */
338 if (local->use_chanctx)
339 return -EOPNOTSUPP;
342 * Hardware/driver doesn't support hw_scan, so use software
343 * scanning instead. First send a nullfunc frame with power save
344 * bit on so that AP will buffer the frames for us while we are not
345 * listening, then send probe requests to each channel and wait for
346 * the responses. After all channels are scanned, tune back to the
347 * original channel and send a nullfunc frame with power save bit
348 * off to trigger the AP to send us all the buffered frames.
350 * Note that while local->sw_scanning is true everything else but
351 * nullfunc frames and probe requests will be dropped in
352 * ieee80211_tx_h_check_assoc().
354 drv_sw_scan_start(local);
356 local->leave_oper_channel_time = jiffies;
357 local->next_scan_state = SCAN_DECISION;
358 local->scan_channel_idx = 0;
360 ieee80211_offchannel_stop_vifs(local);
362 /* ensure nullfunc is transmitted before leaving operating channel */
363 ieee80211_flush_queues(local, NULL);
365 ieee80211_configure_filter(local);
367 /* We need to set power level at maximum rate for scanning. */
368 ieee80211_hw_config(local, 0);
370 ieee80211_queue_delayed_work(&local->hw,
371 &local->scan_work, 0);
373 return 0;
376 static bool ieee80211_can_scan(struct ieee80211_local *local,
377 struct ieee80211_sub_if_data *sdata)
379 if (local->radar_detect_enabled)
380 return false;
382 if (!list_empty(&local->roc_list))
383 return false;
385 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
386 sdata->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
387 IEEE80211_STA_CONNECTION_POLL))
388 return false;
390 return true;
393 void ieee80211_run_deferred_scan(struct ieee80211_local *local)
395 lockdep_assert_held(&local->mtx);
397 if (!local->scan_req || local->scanning)
398 return;
400 if (!ieee80211_can_scan(local,
401 rcu_dereference_protected(
402 local->scan_sdata,
403 lockdep_is_held(&local->mtx))))
404 return;
406 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
407 round_jiffies_relative(0));
410 static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
411 unsigned long *next_delay)
413 int i;
414 struct ieee80211_sub_if_data *sdata;
415 enum ieee80211_band band = local->hw.conf.chandef.chan->band;
416 u32 tx_flags;
418 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
419 if (local->scan_req->no_cck)
420 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
422 sdata = rcu_dereference_protected(local->scan_sdata,
423 lockdep_is_held(&local->mtx));
425 for (i = 0; i < local->scan_req->n_ssids; i++)
426 ieee80211_send_probe_req(
427 sdata, NULL,
428 local->scan_req->ssids[i].ssid,
429 local->scan_req->ssids[i].ssid_len,
430 local->scan_req->ie, local->scan_req->ie_len,
431 local->scan_req->rates[band], false,
432 tx_flags, local->hw.conf.chandef.chan, true);
435 * After sending probe requests, wait for probe responses
436 * on the channel.
438 *next_delay = IEEE80211_CHANNEL_TIME;
439 local->next_scan_state = SCAN_DECISION;
442 static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
443 struct cfg80211_scan_request *req)
445 struct ieee80211_local *local = sdata->local;
446 int rc;
448 lockdep_assert_held(&local->mtx);
450 if (local->scan_req)
451 return -EBUSY;
453 if (!ieee80211_can_scan(local, sdata)) {
454 /* wait for the work to finish/time out */
455 local->scan_req = req;
456 rcu_assign_pointer(local->scan_sdata, sdata);
457 return 0;
460 if (local->ops->hw_scan) {
461 u8 *ies;
463 local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN +
464 local->scan_ies_len +
465 req->ie_len;
466 local->hw_scan_req = kmalloc(
467 sizeof(*local->hw_scan_req) +
468 req->n_channels * sizeof(req->channels[0]) +
469 local->hw_scan_ies_bufsize, GFP_KERNEL);
470 if (!local->hw_scan_req)
471 return -ENOMEM;
473 local->hw_scan_req->ssids = req->ssids;
474 local->hw_scan_req->n_ssids = req->n_ssids;
475 ies = (u8 *)local->hw_scan_req +
476 sizeof(*local->hw_scan_req) +
477 req->n_channels * sizeof(req->channels[0]);
478 local->hw_scan_req->ie = ies;
479 local->hw_scan_req->flags = req->flags;
481 local->hw_scan_band = 0;
484 * After allocating local->hw_scan_req, we must
485 * go through until ieee80211_prep_hw_scan(), so
486 * anything that might be changed here and leave
487 * this function early must not go after this
488 * allocation.
492 local->scan_req = req;
493 rcu_assign_pointer(local->scan_sdata, sdata);
495 if (local->ops->hw_scan) {
496 __set_bit(SCAN_HW_SCANNING, &local->scanning);
497 } else if ((req->n_channels == 1) &&
498 (req->channels[0] == local->_oper_chandef.chan)) {
500 * If we are scanning only on the operating channel
501 * then we do not need to stop normal activities
503 unsigned long next_delay;
505 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
507 ieee80211_recalc_idle(local);
509 /* Notify driver scan is starting, keep order of operations
510 * same as normal software scan, in case that matters. */
511 drv_sw_scan_start(local);
513 ieee80211_configure_filter(local); /* accept probe-responses */
515 /* We need to ensure power level is at max for scanning. */
516 ieee80211_hw_config(local, 0);
518 if ((req->channels[0]->flags &
519 IEEE80211_CHAN_PASSIVE_SCAN) ||
520 !local->scan_req->n_ssids) {
521 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
522 } else {
523 ieee80211_scan_state_send_probe(local, &next_delay);
524 next_delay = IEEE80211_CHANNEL_TIME;
527 /* Now, just wait a bit and we are all done! */
528 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
529 next_delay);
530 return 0;
531 } else {
532 /* Do normal software scan */
533 __set_bit(SCAN_SW_SCANNING, &local->scanning);
536 ieee80211_recalc_idle(local);
538 if (local->ops->hw_scan) {
539 WARN_ON(!ieee80211_prep_hw_scan(local));
540 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
541 } else
542 rc = ieee80211_start_sw_scan(local);
544 if (rc) {
545 kfree(local->hw_scan_req);
546 local->hw_scan_req = NULL;
547 local->scanning = 0;
549 ieee80211_recalc_idle(local);
551 local->scan_req = NULL;
552 rcu_assign_pointer(local->scan_sdata, NULL);
555 return rc;
558 static unsigned long
559 ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
562 * TODO: channel switching also consumes quite some time,
563 * add that delay as well to get a better estimation
565 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
566 return IEEE80211_PASSIVE_CHANNEL_TIME;
567 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
570 static void ieee80211_scan_state_decision(struct ieee80211_local *local,
571 unsigned long *next_delay)
573 bool associated = false;
574 bool tx_empty = true;
575 bool bad_latency;
576 struct ieee80211_sub_if_data *sdata;
577 struct ieee80211_channel *next_chan;
578 enum mac80211_scan_state next_scan_state;
581 * check if at least one STA interface is associated,
582 * check if at least one STA interface has pending tx frames
583 * and grab the lowest used beacon interval
585 mutex_lock(&local->iflist_mtx);
586 list_for_each_entry(sdata, &local->interfaces, list) {
587 if (!ieee80211_sdata_running(sdata))
588 continue;
590 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
591 if (sdata->u.mgd.associated) {
592 associated = true;
594 if (!qdisc_all_tx_empty(sdata->dev)) {
595 tx_empty = false;
596 break;
601 mutex_unlock(&local->iflist_mtx);
603 next_chan = local->scan_req->channels[local->scan_channel_idx];
606 * we're currently scanning a different channel, let's
607 * see if we can scan another channel without interfering
608 * with the current traffic situation.
610 * Keep good latency, do not stay off-channel more than 125 ms.
613 bad_latency = time_after(jiffies +
614 ieee80211_scan_get_channel_time(next_chan),
615 local->leave_oper_channel_time + HZ / 8);
617 if (associated && !tx_empty) {
618 if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
619 next_scan_state = SCAN_ABORT;
620 else
621 next_scan_state = SCAN_SUSPEND;
622 } else if (associated && bad_latency) {
623 next_scan_state = SCAN_SUSPEND;
624 } else {
625 next_scan_state = SCAN_SET_CHANNEL;
628 local->next_scan_state = next_scan_state;
630 *next_delay = 0;
633 static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
634 unsigned long *next_delay)
636 int skip;
637 struct ieee80211_channel *chan;
639 skip = 0;
640 chan = local->scan_req->channels[local->scan_channel_idx];
642 local->scan_channel = chan;
644 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
645 skip = 1;
647 /* advance state machine to next channel/band */
648 local->scan_channel_idx++;
650 if (skip) {
651 /* if we skip this channel return to the decision state */
652 local->next_scan_state = SCAN_DECISION;
653 return;
657 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
658 * (which unfortunately doesn't say _why_ step a) is done,
659 * but it waits for the probe delay or until a frame is
660 * received - and the received frame would update the NAV).
661 * For now, we do not support waiting until a frame is
662 * received.
664 * In any case, it is not necessary for a passive scan.
666 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
667 !local->scan_req->n_ssids) {
668 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
669 local->next_scan_state = SCAN_DECISION;
670 return;
673 /* active scan, send probes */
674 *next_delay = IEEE80211_PROBE_DELAY;
675 local->next_scan_state = SCAN_SEND_PROBE;
678 static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
679 unsigned long *next_delay)
681 /* switch back to the operating channel */
682 local->scan_channel = NULL;
683 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
685 /* disable PS */
686 ieee80211_offchannel_return(local);
688 *next_delay = HZ / 5;
689 /* afterwards, resume scan & go to next channel */
690 local->next_scan_state = SCAN_RESUME;
693 static void ieee80211_scan_state_resume(struct ieee80211_local *local,
694 unsigned long *next_delay)
696 ieee80211_offchannel_stop_vifs(local);
698 if (local->ops->flush) {
699 ieee80211_flush_queues(local, NULL);
700 *next_delay = 0;
701 } else
702 *next_delay = HZ / 10;
704 /* remember when we left the operating channel */
705 local->leave_oper_channel_time = jiffies;
707 /* advance to the next channel to be scanned */
708 local->next_scan_state = SCAN_SET_CHANNEL;
711 void ieee80211_scan_work(struct work_struct *work)
713 struct ieee80211_local *local =
714 container_of(work, struct ieee80211_local, scan_work.work);
715 struct ieee80211_sub_if_data *sdata;
716 unsigned long next_delay = 0;
717 bool aborted, hw_scan;
719 mutex_lock(&local->mtx);
721 sdata = rcu_dereference_protected(local->scan_sdata,
722 lockdep_is_held(&local->mtx));
724 /* When scanning on-channel, the first-callback means completed. */
725 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
726 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
727 goto out_complete;
730 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
731 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
732 goto out_complete;
735 if (!sdata || !local->scan_req)
736 goto out;
738 if (local->scan_req && !local->scanning) {
739 struct cfg80211_scan_request *req = local->scan_req;
740 int rc;
742 local->scan_req = NULL;
743 rcu_assign_pointer(local->scan_sdata, NULL);
745 rc = __ieee80211_start_scan(sdata, req);
746 if (rc) {
747 /* need to complete scan in cfg80211 */
748 local->scan_req = req;
749 aborted = true;
750 goto out_complete;
751 } else
752 goto out;
756 * Avoid re-scheduling when the sdata is going away.
758 if (!ieee80211_sdata_running(sdata)) {
759 aborted = true;
760 goto out_complete;
764 * as long as no delay is required advance immediately
765 * without scheduling a new work
767 do {
768 if (!ieee80211_sdata_running(sdata)) {
769 aborted = true;
770 goto out_complete;
773 switch (local->next_scan_state) {
774 case SCAN_DECISION:
775 /* if no more bands/channels left, complete scan */
776 if (local->scan_channel_idx >= local->scan_req->n_channels) {
777 aborted = false;
778 goto out_complete;
780 ieee80211_scan_state_decision(local, &next_delay);
781 break;
782 case SCAN_SET_CHANNEL:
783 ieee80211_scan_state_set_channel(local, &next_delay);
784 break;
785 case SCAN_SEND_PROBE:
786 ieee80211_scan_state_send_probe(local, &next_delay);
787 break;
788 case SCAN_SUSPEND:
789 ieee80211_scan_state_suspend(local, &next_delay);
790 break;
791 case SCAN_RESUME:
792 ieee80211_scan_state_resume(local, &next_delay);
793 break;
794 case SCAN_ABORT:
795 aborted = true;
796 goto out_complete;
798 } while (next_delay == 0);
800 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
801 goto out;
803 out_complete:
804 hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
805 __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
806 out:
807 mutex_unlock(&local->mtx);
810 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
811 struct cfg80211_scan_request *req)
813 int res;
815 mutex_lock(&sdata->local->mtx);
816 res = __ieee80211_start_scan(sdata, req);
817 mutex_unlock(&sdata->local->mtx);
819 return res;
822 int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
823 const u8 *ssid, u8 ssid_len,
824 struct ieee80211_channel *chan)
826 struct ieee80211_local *local = sdata->local;
827 int ret = -EBUSY;
828 enum ieee80211_band band;
830 mutex_lock(&local->mtx);
832 /* busy scanning */
833 if (local->scan_req)
834 goto unlock;
836 /* fill internal scan request */
837 if (!chan) {
838 int i, max_n;
839 int n_ch = 0;
841 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
842 if (!local->hw.wiphy->bands[band])
843 continue;
845 max_n = local->hw.wiphy->bands[band]->n_channels;
846 for (i = 0; i < max_n; i++) {
847 struct ieee80211_channel *tmp_ch =
848 &local->hw.wiphy->bands[band]->channels[i];
850 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IBSS |
851 IEEE80211_CHAN_DISABLED))
852 continue;
854 local->int_scan_req->channels[n_ch] = tmp_ch;
855 n_ch++;
859 if (WARN_ON_ONCE(n_ch == 0))
860 goto unlock;
862 local->int_scan_req->n_channels = n_ch;
863 } else {
864 if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IBSS |
865 IEEE80211_CHAN_DISABLED)))
866 goto unlock;
868 local->int_scan_req->channels[0] = chan;
869 local->int_scan_req->n_channels = 1;
872 local->int_scan_req->ssids = &local->scan_ssid;
873 local->int_scan_req->n_ssids = 1;
874 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
875 local->int_scan_req->ssids[0].ssid_len = ssid_len;
877 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
878 unlock:
879 mutex_unlock(&local->mtx);
880 return ret;
884 * Only call this function when a scan can't be queued -- under RTNL.
886 void ieee80211_scan_cancel(struct ieee80211_local *local)
889 * We are canceling software scan, or deferred scan that was not
890 * yet really started (see __ieee80211_start_scan ).
892 * Regarding hardware scan:
893 * - we can not call __ieee80211_scan_completed() as when
894 * SCAN_HW_SCANNING bit is set this function change
895 * local->hw_scan_req to operate on 5G band, what race with
896 * driver which can use local->hw_scan_req
898 * - we can not cancel scan_work since driver can schedule it
899 * by ieee80211_scan_completed(..., true) to finish scan
901 * Hence we only call the cancel_hw_scan() callback, but the low-level
902 * driver is still responsible for calling ieee80211_scan_completed()
903 * after the scan was completed/aborted.
906 mutex_lock(&local->mtx);
907 if (!local->scan_req)
908 goto out;
910 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
911 if (local->ops->cancel_hw_scan)
912 drv_cancel_hw_scan(local,
913 rcu_dereference_protected(local->scan_sdata,
914 lockdep_is_held(&local->mtx)));
915 goto out;
919 * If the work is currently running, it must be blocked on
920 * the mutex, but we'll set scan_sdata = NULL and it'll
921 * simply exit once it acquires the mutex.
923 cancel_delayed_work(&local->scan_work);
924 /* and clean up */
925 __ieee80211_scan_completed(&local->hw, true, false);
926 out:
927 mutex_unlock(&local->mtx);
930 int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
931 struct cfg80211_sched_scan_request *req)
933 struct ieee80211_local *local = sdata->local;
934 struct ieee80211_sched_scan_ies sched_scan_ies = {};
935 struct cfg80211_chan_def chandef;
936 int ret, i, iebufsz;
938 iebufsz = 2 + IEEE80211_MAX_SSID_LEN +
939 local->scan_ies_len + req->ie_len;
941 mutex_lock(&local->mtx);
943 if (rcu_access_pointer(local->sched_scan_sdata)) {
944 ret = -EBUSY;
945 goto out;
948 if (!local->ops->sched_scan_start) {
949 ret = -ENOTSUPP;
950 goto out;
953 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
954 if (!local->hw.wiphy->bands[i])
955 continue;
957 sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL);
958 if (!sched_scan_ies.ie[i]) {
959 ret = -ENOMEM;
960 goto out_free;
963 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
965 sched_scan_ies.len[i] =
966 ieee80211_build_preq_ies(local, sched_scan_ies.ie[i],
967 iebufsz, req->ie, req->ie_len,
968 i, (u32) -1, &chandef);
971 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
972 if (ret == 0)
973 rcu_assign_pointer(local->sched_scan_sdata, sdata);
975 out_free:
976 while (i > 0)
977 kfree(sched_scan_ies.ie[--i]);
978 out:
979 mutex_unlock(&local->mtx);
980 return ret;
983 int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
985 struct ieee80211_local *local = sdata->local;
986 int ret = 0;
988 mutex_lock(&local->mtx);
990 if (!local->ops->sched_scan_stop) {
991 ret = -ENOTSUPP;
992 goto out;
995 if (rcu_access_pointer(local->sched_scan_sdata))
996 drv_sched_scan_stop(local, sdata);
998 out:
999 mutex_unlock(&local->mtx);
1001 return ret;
1004 void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1006 struct ieee80211_local *local = hw_to_local(hw);
1008 trace_api_sched_scan_results(local);
1010 cfg80211_sched_scan_results(hw->wiphy);
1012 EXPORT_SYMBOL(ieee80211_sched_scan_results);
1014 void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1016 struct ieee80211_local *local =
1017 container_of(work, struct ieee80211_local,
1018 sched_scan_stopped_work);
1020 mutex_lock(&local->mtx);
1022 if (!rcu_access_pointer(local->sched_scan_sdata)) {
1023 mutex_unlock(&local->mtx);
1024 return;
1027 rcu_assign_pointer(local->sched_scan_sdata, NULL);
1029 mutex_unlock(&local->mtx);
1031 cfg80211_sched_scan_stopped(local->hw.wiphy);
1034 void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1036 struct ieee80211_local *local = hw_to_local(hw);
1038 trace_api_sched_scan_stopped(local);
1040 ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
1042 EXPORT_SYMBOL(ieee80211_sched_scan_stopped);