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> 9d98ad83eSJohannes 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)) { 187a344d677SJohannes Berg struct cfg80211_scan_request *scan_req; 188a344d677SJohannes Berg struct cfg80211_sched_scan_request *sched_scan_req; 189a344d677SJohannes Berg 190a344d677SJohannes Berg scan_req = rcu_dereference(local->scan_req); 191a344d677SJohannes Berg sched_scan_req = rcu_dereference(local->sched_scan_req); 192a344d677SJohannes Berg 193a344d677SJohannes Berg /* ignore ProbeResp to foreign address unless scanning 194a344d677SJohannes Berg * with randomised address 195a344d677SJohannes Berg */ 196a344d677SJohannes Berg if (!(sdata1 && 197a344d677SJohannes Berg (ether_addr_equal(mgmt->da, sdata1->vif.addr) || 198a344d677SJohannes Berg scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) && 199a344d677SJohannes Berg !(sdata2 && 200a344d677SJohannes Berg (ether_addr_equal(mgmt->da, sdata2->vif.addr) || 201a344d677SJohannes Berg sched_scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR))) 202d48b2968SJohannes Berg return; 20398c8fccfSJohannes Berg 20498c8fccfSJohannes Berg elements = mgmt->u.probe_resp.variable; 20598c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); 20698c8fccfSJohannes Berg } else { 20798c8fccfSJohannes Berg baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 20898c8fccfSJohannes Berg elements = mgmt->u.beacon.variable; 20998c8fccfSJohannes Berg } 21098c8fccfSJohannes Berg 21198c8fccfSJohannes Berg if (baselen > skb->len) 212d48b2968SJohannes Berg return; 21398c8fccfSJohannes Berg 214b2e506bfSJohannes Berg ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems); 21598c8fccfSJohannes Berg 2160172bb75SJohannes Berg channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 21798c8fccfSJohannes Berg 21898c8fccfSJohannes Berg if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 219d48b2968SJohannes Berg return; 22098c8fccfSJohannes Berg 221d48b2968SJohannes Berg bss = ieee80211_bss_info_update(local, rx_status, 22298c8fccfSJohannes Berg mgmt, skb->len, &elems, 223d45c4172SEmmanuel Grumbach channel); 224d048e503SJouni Malinen if (bss) 225d48b2968SJohannes Berg ieee80211_rx_bss_put(local, bss); 22698c8fccfSJohannes Berg } 22798c8fccfSJohannes Berg 2282103dec1SSimon Wunderlich static void 2292103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef, 2302103dec1SSimon Wunderlich enum nl80211_bss_scan_width scan_width) 2312103dec1SSimon Wunderlich { 2322103dec1SSimon Wunderlich memset(chandef, 0, sizeof(*chandef)); 2332103dec1SSimon Wunderlich switch (scan_width) { 2342103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 2352103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_5; 2362103dec1SSimon Wunderlich break; 2372103dec1SSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 2382103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_10; 2392103dec1SSimon Wunderlich break; 2402103dec1SSimon Wunderlich default: 2412103dec1SSimon Wunderlich chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 2422103dec1SSimon Wunderlich break; 2432103dec1SSimon Wunderlich } 2442103dec1SSimon Wunderlich } 2452103dec1SSimon Wunderlich 2464d36ec58SJohannes Berg /* return false if no more work */ 2474d36ec58SJohannes Berg static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 2484d36ec58SJohannes Berg { 2496ea0a69cSJohannes Berg struct cfg80211_scan_request *req; 2502103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 251c56ef672SDavid Spinadel u8 bands_used = 0; 2524d36ec58SJohannes Berg int i, ielen, n_chans; 2534d36ec58SJohannes Berg 2546ea0a69cSJohannes Berg req = rcu_dereference_protected(local->scan_req, 2556ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 2566ea0a69cSJohannes Berg 257a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 258a754055aSEmmanuel Grumbach return false; 259a754055aSEmmanuel Grumbach 260c56ef672SDavid Spinadel if (local->hw.flags & IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS) { 261c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 262c56ef672SDavid Spinadel local->hw_scan_req->req.channels[i] = req->channels[i]; 263c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 264c56ef672SDavid Spinadel } 265c56ef672SDavid Spinadel 266c56ef672SDavid Spinadel n_chans = req->n_channels; 267c56ef672SDavid Spinadel } else { 2684d36ec58SJohannes Berg do { 2694d36ec58SJohannes Berg if (local->hw_scan_band == IEEE80211_NUM_BANDS) 2704d36ec58SJohannes Berg return false; 2714d36ec58SJohannes Berg 2724d36ec58SJohannes Berg n_chans = 0; 273c56ef672SDavid Spinadel 2744d36ec58SJohannes Berg for (i = 0; i < req->n_channels; i++) { 275c56ef672SDavid Spinadel if (req->channels[i]->band != 276c56ef672SDavid Spinadel local->hw_scan_band) 277c56ef672SDavid Spinadel continue; 278c56ef672SDavid Spinadel local->hw_scan_req->req.channels[n_chans] = 2794d36ec58SJohannes Berg req->channels[i]; 2804d36ec58SJohannes Berg n_chans++; 281c56ef672SDavid Spinadel bands_used |= BIT(req->channels[i]->band); 2824d36ec58SJohannes Berg } 2834d36ec58SJohannes Berg 2844d36ec58SJohannes Berg local->hw_scan_band++; 2854d36ec58SJohannes Berg } while (!n_chans); 286c56ef672SDavid Spinadel } 2874d36ec58SJohannes Berg 288c56ef672SDavid Spinadel local->hw_scan_req->req.n_channels = n_chans; 2892103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 2904d36ec58SJohannes Berg 291c56ef672SDavid Spinadel ielen = ieee80211_build_preq_ies(local, 292c56ef672SDavid Spinadel (u8 *)local->hw_scan_req->req.ie, 293c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, 294c56ef672SDavid Spinadel &local->hw_scan_req->ies, 295c56ef672SDavid Spinadel req->ie, req->ie_len, 296c56ef672SDavid Spinadel bands_used, req->rates, &chandef); 297c56ef672SDavid Spinadel local->hw_scan_req->req.ie_len = ielen; 298c56ef672SDavid Spinadel local->hw_scan_req->req.no_cck = req->no_cck; 299a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr); 300a344d677SJohannes Berg ether_addr_copy(local->hw_scan_req->req.mac_addr_mask, 301a344d677SJohannes Berg req->mac_addr_mask); 3024d36ec58SJohannes Berg 3034d36ec58SJohannes Berg return true; 3044d36ec58SJohannes Berg } 3054d36ec58SJohannes Berg 3068bd2a248SEliad Peller static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 307f3b85252SJohannes Berg { 308f3b85252SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3098bd2a248SEliad Peller bool hw_scan = local->ops->hw_scan; 310a2b70e83SEliad Peller bool was_scanning = local->scanning; 3116ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 312a344d677SJohannes Berg struct ieee80211_sub_if_data *scan_sdata; 313f3b85252SJohannes Berg 314e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 315f3b85252SJohannes Berg 3166d3560d4SJohannes Berg /* 3176d3560d4SJohannes Berg * It's ok to abort a not-yet-running scan (that 3186d3560d4SJohannes Berg * we have one at all will be verified by checking 3196d3560d4SJohannes Berg * local->scan_req next), but not to complete it 3206d3560d4SJohannes Berg * successfully. 3216d3560d4SJohannes Berg */ 3226d3560d4SJohannes Berg if (WARN_ON(!local->scanning && !aborted)) 3236d3560d4SJohannes Berg aborted = true; 324f3b85252SJohannes Berg 325e229f844SStanislaw Gruszka if (WARN_ON(!local->scan_req)) 326d07bfd8bSJohannes Berg return; 327f3b85252SJohannes Berg 328c56ef672SDavid Spinadel if (hw_scan && !aborted && 329c56ef672SDavid Spinadel !(local->hw.flags & IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS) && 330c56ef672SDavid Spinadel ieee80211_prep_hw_scan(local)) { 331e2fd5dbcSJohannes Berg int rc; 332e2fd5dbcSJohannes Berg 333e2fd5dbcSJohannes Berg rc = drv_hw_scan(local, 334e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 335e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)), 336e2fd5dbcSJohannes Berg local->hw_scan_req); 337e2fd5dbcSJohannes Berg 3386eb11a9aSStanislaw Gruszka if (rc == 0) 339d07bfd8bSJohannes Berg return; 3404d36ec58SJohannes Berg } 3414d36ec58SJohannes Berg 3424d36ec58SJohannes Berg kfree(local->hw_scan_req); 3434d36ec58SJohannes Berg local->hw_scan_req = NULL; 344f3b85252SJohannes Berg 3456ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 3466ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 3476ea0a69cSJohannes Berg 3486ea0a69cSJohannes Berg if (scan_req != local->int_scan_req) 3496ea0a69cSJohannes Berg cfg80211_scan_done(scan_req, aborted); 3506ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 351a344d677SJohannes Berg 352a344d677SJohannes Berg scan_sdata = rcu_dereference_protected(local->scan_sdata, 353a344d677SJohannes Berg lockdep_is_held(&local->mtx)); 3540c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 3552a519311SJohannes Berg 356fbe9c429SHelmut Schaa local->scanning = 0; 3577ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 358f3b85252SJohannes Berg 35959bdf3b0SBen Greear /* Set power back to normal operating levels. */ 36059bdf3b0SBen Greear ieee80211_hw_config(local, 0); 361b23b025fSBen Greear 3628bd2a248SEliad Peller if (!hw_scan) { 363b23b025fSBen Greear ieee80211_configure_filter(local); 364a344d677SJohannes Berg drv_sw_scan_complete(local, scan_sdata); 365aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 366b23b025fSBen Greear } 367b23b025fSBen Greear 3685cff20e6SJohannes Berg ieee80211_recalc_idle(local); 369e229f844SStanislaw Gruszka 3700a51b27eSJohannes Berg ieee80211_mlme_notify_scan_completed(local); 37146900298SJohannes Berg ieee80211_ibss_notify_scan_completed(local); 372472dbc45SJohannes Berg ieee80211_mesh_notify_scan_completed(local); 373a2b70e83SEliad Peller if (was_scanning) 3742eb278e0SJohannes Berg ieee80211_start_next_roc(local); 3750a51b27eSJohannes Berg } 3768789d459SJohannes Berg 3778789d459SJohannes Berg void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 3788789d459SJohannes Berg { 3798789d459SJohannes Berg struct ieee80211_local *local = hw_to_local(hw); 3808789d459SJohannes Berg 3818789d459SJohannes Berg trace_api_scan_completed(local, aborted); 3828789d459SJohannes Berg 3838789d459SJohannes Berg set_bit(SCAN_COMPLETED, &local->scanning); 3848789d459SJohannes Berg if (aborted) 3858789d459SJohannes Berg set_bit(SCAN_ABORTED, &local->scanning); 3868789d459SJohannes Berg ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 3878789d459SJohannes Berg } 3880a51b27eSJohannes Berg EXPORT_SYMBOL(ieee80211_scan_completed); 3890a51b27eSJohannes Berg 390a344d677SJohannes Berg static int ieee80211_start_sw_scan(struct ieee80211_local *local, 391a344d677SJohannes Berg struct ieee80211_sub_if_data *sdata) 392f3b85252SJohannes Berg { 393fe57d9f5SJohannes Berg /* Software scan is not supported in multi-channel cases */ 394fe57d9f5SJohannes Berg if (local->use_chanctx) 395fe57d9f5SJohannes Berg return -EOPNOTSUPP; 396fe57d9f5SJohannes Berg 397f3b85252SJohannes Berg /* 398f3b85252SJohannes Berg * Hardware/driver doesn't support hw_scan, so use software 399f3b85252SJohannes Berg * scanning instead. First send a nullfunc frame with power save 400f3b85252SJohannes Berg * bit on so that AP will buffer the frames for us while we are not 401f3b85252SJohannes Berg * listening, then send probe requests to each channel and wait for 402f3b85252SJohannes Berg * the responses. After all channels are scanned, tune back to the 403f3b85252SJohannes Berg * original channel and send a nullfunc frame with power save bit 404f3b85252SJohannes Berg * off to trigger the AP to send us all the buffered frames. 405f3b85252SJohannes Berg * 406f3b85252SJohannes Berg * Note that while local->sw_scanning is true everything else but 407f3b85252SJohannes Berg * nullfunc frames and probe requests will be dropped in 408f3b85252SJohannes Berg * ieee80211_tx_h_check_assoc(). 409f3b85252SJohannes Berg */ 410a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 411f3b85252SJohannes Berg 412de312db3SRajkumar Manoharan local->leave_oper_channel_time = jiffies; 413977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 414f3b85252SJohannes Berg local->scan_channel_idx = 0; 415f3b85252SJohannes Berg 416aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 417a80f7c0bSJohannes Berg 4189c35d7d2SSeth Forshee /* ensure nullfunc is transmitted before leaving operating channel */ 4193b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 4209c35d7d2SSeth Forshee 4213ac64beeSJohannes Berg ieee80211_configure_filter(local); 422f3b85252SJohannes Berg 42359bdf3b0SBen Greear /* We need to set power level at maximum rate for scanning. */ 42459bdf3b0SBen Greear ieee80211_hw_config(local, 0); 42559bdf3b0SBen Greear 42642935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, 42707ef03eeSJohannes Berg &local->scan_work, 0); 428f3b85252SJohannes Berg 429f3b85252SJohannes Berg return 0; 430f3b85252SJohannes Berg } 431f3b85252SJohannes Berg 432133d40f9SStanislaw Gruszka static bool ieee80211_can_scan(struct ieee80211_local *local, 433133d40f9SStanislaw Gruszka struct ieee80211_sub_if_data *sdata) 434133d40f9SStanislaw Gruszka { 4355cbc95a7SEliad Peller if (ieee80211_is_radar_required(local)) 436164eb02dSSimon Wunderlich return false; 437164eb02dSSimon Wunderlich 4382eb278e0SJohannes Berg if (!list_empty(&local->roc_list)) 439133d40f9SStanislaw Gruszka return false; 440133d40f9SStanislaw Gruszka 441133d40f9SStanislaw Gruszka if (sdata->vif.type == NL80211_IFTYPE_STATION && 442392b9ffbSStanislaw Gruszka sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 443133d40f9SStanislaw Gruszka return false; 444133d40f9SStanislaw Gruszka 445133d40f9SStanislaw Gruszka return true; 446133d40f9SStanislaw Gruszka } 447133d40f9SStanislaw Gruszka 448133d40f9SStanislaw Gruszka void ieee80211_run_deferred_scan(struct ieee80211_local *local) 449133d40f9SStanislaw Gruszka { 450133d40f9SStanislaw Gruszka lockdep_assert_held(&local->mtx); 451133d40f9SStanislaw Gruszka 452133d40f9SStanislaw Gruszka if (!local->scan_req || local->scanning) 453133d40f9SStanislaw Gruszka return; 454133d40f9SStanislaw Gruszka 455e2fd5dbcSJohannes Berg if (!ieee80211_can_scan(local, 456e2fd5dbcSJohannes Berg rcu_dereference_protected( 457e2fd5dbcSJohannes Berg local->scan_sdata, 458e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)))) 459133d40f9SStanislaw Gruszka return; 460133d40f9SStanislaw Gruszka 461133d40f9SStanislaw Gruszka ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 462133d40f9SStanislaw Gruszka round_jiffies_relative(0)); 463133d40f9SStanislaw Gruszka } 464f3b85252SJohannes Berg 4658a690674SBen Greear static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 4668a690674SBen Greear unsigned long *next_delay) 4678a690674SBen Greear { 4688a690674SBen Greear int i; 469e2fd5dbcSJohannes Berg struct ieee80211_sub_if_data *sdata; 4706ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 471675a0b04SKarl Beldan enum ieee80211_band band = local->hw.conf.chandef.chan->band; 4726c17b77bSSeth Forshee u32 tx_flags; 4736c17b77bSSeth Forshee 4746ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 4756ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 4766ea0a69cSJohannes Berg 4776c17b77bSSeth Forshee tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 4786ea0a69cSJohannes Berg if (scan_req->no_cck) 4796c17b77bSSeth Forshee tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 4808a690674SBen Greear 481e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 482316b6b5dSPeter Senna Tschudin lockdep_is_held(&local->mtx)); 483e2fd5dbcSJohannes Berg 4846ea0a69cSJohannes Berg for (i = 0; i < scan_req->n_ssids; i++) 4858a690674SBen Greear ieee80211_send_probe_req( 486a344d677SJohannes Berg sdata, local->scan_addr, NULL, 4876ea0a69cSJohannes Berg scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len, 4886ea0a69cSJohannes Berg scan_req->ie, scan_req->ie_len, 4896ea0a69cSJohannes Berg scan_req->rates[band], false, 490675a0b04SKarl Beldan tx_flags, local->hw.conf.chandef.chan, true); 4918a690674SBen Greear 4928a690674SBen Greear /* 4938a690674SBen Greear * After sending probe requests, wait for probe responses 4948a690674SBen Greear * on the channel. 4958a690674SBen Greear */ 4968a690674SBen Greear *next_delay = IEEE80211_CHANNEL_TIME; 4978a690674SBen Greear local->next_scan_state = SCAN_DECISION; 4988a690674SBen Greear } 4998a690674SBen Greear 500f3b85252SJohannes Berg static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 501f3b85252SJohannes Berg struct cfg80211_scan_request *req) 502f3b85252SJohannes Berg { 503f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 504f3b85252SJohannes Berg int rc; 505f3b85252SJohannes Berg 506e229f844SStanislaw Gruszka lockdep_assert_held(&local->mtx); 507e229f844SStanislaw Gruszka 508*2726f23dSEliad Peller if (local->scan_req || ieee80211_is_radar_required(local)) 509f3b85252SJohannes Berg return -EBUSY; 510f3b85252SJohannes Berg 511133d40f9SStanislaw Gruszka if (!ieee80211_can_scan(local, sdata)) { 5126e7e6213SJohn W. Linville /* wait for the work to finish/time out */ 5136ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 514e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 515c0ce77b8SJohannes Berg return 0; 516c0ce77b8SJohannes Berg } 517c0ce77b8SJohannes Berg 518f3b85252SJohannes Berg if (local->ops->hw_scan) { 519f3b85252SJohannes Berg u8 *ies; 520f3b85252SJohannes Berg 521e4dcbb37SDavid Spinadel local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len; 522c56ef672SDavid Spinadel 523c56ef672SDavid Spinadel if (local->hw.flags & IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS) { 524c56ef672SDavid Spinadel int i, n_bands = 0; 525c56ef672SDavid Spinadel u8 bands_counted = 0; 526c56ef672SDavid Spinadel 527c56ef672SDavid Spinadel for (i = 0; i < req->n_channels; i++) { 528c56ef672SDavid Spinadel if (bands_counted & BIT(req->channels[i]->band)) 529c56ef672SDavid Spinadel continue; 530c56ef672SDavid Spinadel bands_counted |= BIT(req->channels[i]->band); 531c56ef672SDavid Spinadel n_bands++; 532c56ef672SDavid Spinadel } 533c56ef672SDavid Spinadel 534c56ef672SDavid Spinadel local->hw_scan_ies_bufsize *= n_bands; 535c56ef672SDavid Spinadel } 536c56ef672SDavid Spinadel 5374d36ec58SJohannes Berg local->hw_scan_req = kmalloc( 5384d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 5394d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]) + 540c604b9f2SJohannes Berg local->hw_scan_ies_bufsize, GFP_KERNEL); 5414d36ec58SJohannes Berg if (!local->hw_scan_req) 542f3b85252SJohannes Berg return -ENOMEM; 543f3b85252SJohannes Berg 544c56ef672SDavid Spinadel local->hw_scan_req->req.ssids = req->ssids; 545c56ef672SDavid Spinadel local->hw_scan_req->req.n_ssids = req->n_ssids; 5464d36ec58SJohannes Berg ies = (u8 *)local->hw_scan_req + 5474d36ec58SJohannes Berg sizeof(*local->hw_scan_req) + 5484d36ec58SJohannes Berg req->n_channels * sizeof(req->channels[0]); 549c56ef672SDavid Spinadel local->hw_scan_req->req.ie = ies; 550c56ef672SDavid Spinadel local->hw_scan_req->req.flags = req->flags; 5514d36ec58SJohannes Berg 5524d36ec58SJohannes Berg local->hw_scan_band = 0; 5536e7e6213SJohn W. Linville 5546e7e6213SJohn W. Linville /* 5556e7e6213SJohn W. Linville * After allocating local->hw_scan_req, we must 5566e7e6213SJohn W. Linville * go through until ieee80211_prep_hw_scan(), so 5576e7e6213SJohn W. Linville * anything that might be changed here and leave 5586e7e6213SJohn W. Linville * this function early must not go after this 5596e7e6213SJohn W. Linville * allocation. 5606e7e6213SJohn W. Linville */ 561f3b85252SJohannes Berg } 562f3b85252SJohannes Berg 5636ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, req); 564e2fd5dbcSJohannes Berg rcu_assign_pointer(local->scan_sdata, sdata); 565f3b85252SJohannes Berg 566a344d677SJohannes Berg if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 567a344d677SJohannes Berg get_random_mask_addr(local->scan_addr, 568a344d677SJohannes Berg req->mac_addr, 569a344d677SJohannes Berg req->mac_addr_mask); 570a344d677SJohannes Berg else 571a344d677SJohannes Berg memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN); 572a344d677SJohannes Berg 5738a690674SBen Greear if (local->ops->hw_scan) { 574fbe9c429SHelmut Schaa __set_bit(SCAN_HW_SCANNING, &local->scanning); 5758a690674SBen Greear } else if ((req->n_channels == 1) && 576675a0b04SKarl Beldan (req->channels[0] == local->_oper_chandef.chan)) { 5779b864870SJohannes Berg /* 5789b864870SJohannes Berg * If we are scanning only on the operating channel 5799b864870SJohannes Berg * then we do not need to stop normal activities 5808a690674SBen Greear */ 5818a690674SBen Greear unsigned long next_delay; 5828a690674SBen Greear 5838a690674SBen Greear __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 5848a690674SBen Greear 5858a690674SBen Greear ieee80211_recalc_idle(local); 5868a690674SBen Greear 5878a690674SBen Greear /* Notify driver scan is starting, keep order of operations 5888a690674SBen Greear * same as normal software scan, in case that matters. */ 589a344d677SJohannes Berg drv_sw_scan_start(local, sdata, local->scan_addr); 5908a690674SBen Greear 5918a690674SBen Greear ieee80211_configure_filter(local); /* accept probe-responses */ 5928a690674SBen Greear 5938a690674SBen Greear /* We need to ensure power level is at max for scanning. */ 5948a690674SBen Greear ieee80211_hw_config(local, 0); 5958a690674SBen Greear 5968a690674SBen Greear if ((req->channels[0]->flags & 5978fe02e16SLuis R. Rodriguez IEEE80211_CHAN_NO_IR) || 5986ea0a69cSJohannes Berg !req->n_ssids) { 5998a690674SBen Greear next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 6008a690674SBen Greear } else { 6018a690674SBen Greear ieee80211_scan_state_send_probe(local, &next_delay); 6028a690674SBen Greear next_delay = IEEE80211_CHANNEL_TIME; 6038a690674SBen Greear } 6048a690674SBen Greear 6058a690674SBen Greear /* Now, just wait a bit and we are all done! */ 6068a690674SBen Greear ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 6078a690674SBen Greear next_delay); 6088a690674SBen Greear return 0; 6098a690674SBen Greear } else { 6108a690674SBen Greear /* Do normal software scan */ 611fbe9c429SHelmut Schaa __set_bit(SCAN_SW_SCANNING, &local->scanning); 6128a690674SBen Greear } 6136e7e6213SJohn W. Linville 6145cff20e6SJohannes Berg ieee80211_recalc_idle(local); 615f3b85252SJohannes Berg 6164d36ec58SJohannes Berg if (local->ops->hw_scan) { 6174d36ec58SJohannes Berg WARN_ON(!ieee80211_prep_hw_scan(local)); 618a060bbfeSJohannes Berg rc = drv_hw_scan(local, sdata, local->hw_scan_req); 619a344d677SJohannes Berg } else { 620a344d677SJohannes Berg rc = ieee80211_start_sw_scan(local, sdata); 621a344d677SJohannes Berg } 622f3b85252SJohannes Berg 623f3b85252SJohannes Berg if (rc) { 6244d36ec58SJohannes Berg kfree(local->hw_scan_req); 6254d36ec58SJohannes Berg local->hw_scan_req = NULL; 626fbe9c429SHelmut Schaa local->scanning = 0; 627f3b85252SJohannes Berg 6285cff20e6SJohannes Berg ieee80211_recalc_idle(local); 6295cff20e6SJohannes Berg 630f3b85252SJohannes Berg local->scan_req = NULL; 6310c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 632f3b85252SJohannes Berg } 633f3b85252SJohannes Berg 634f3b85252SJohannes Berg return rc; 635f3b85252SJohannes Berg } 636f3b85252SJohannes Berg 637df13cce5SHelmut Schaa static unsigned long 638df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 639df13cce5SHelmut Schaa { 640df13cce5SHelmut Schaa /* 641df13cce5SHelmut Schaa * TODO: channel switching also consumes quite some time, 642df13cce5SHelmut Schaa * add that delay as well to get a better estimation 643df13cce5SHelmut Schaa */ 6448fe02e16SLuis R. Rodriguez if (chan->flags & IEEE80211_CHAN_NO_IR) 645df13cce5SHelmut Schaa return IEEE80211_PASSIVE_CHANNEL_TIME; 646df13cce5SHelmut Schaa return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 647df13cce5SHelmut Schaa } 648df13cce5SHelmut Schaa 649e229f844SStanislaw Gruszka static void ieee80211_scan_state_decision(struct ieee80211_local *local, 6502fb3f028SHelmut Schaa unsigned long *next_delay) 6512fb3f028SHelmut Schaa { 652142b9f50SHelmut Schaa bool associated = false; 653df13cce5SHelmut Schaa bool tx_empty = true; 654df13cce5SHelmut Schaa bool bad_latency; 655142b9f50SHelmut Schaa struct ieee80211_sub_if_data *sdata; 656df13cce5SHelmut Schaa struct ieee80211_channel *next_chan; 657cd2bb512SSam Leffler enum mac80211_scan_state next_scan_state; 6586ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 659142b9f50SHelmut Schaa 660df13cce5SHelmut Schaa /* 661df13cce5SHelmut Schaa * check if at least one STA interface is associated, 662df13cce5SHelmut Schaa * check if at least one STA interface has pending tx frames 663df13cce5SHelmut Schaa * and grab the lowest used beacon interval 664df13cce5SHelmut Schaa */ 665142b9f50SHelmut Schaa mutex_lock(&local->iflist_mtx); 666142b9f50SHelmut Schaa list_for_each_entry(sdata, &local->interfaces, list) { 6679607e6b6SJohannes Berg if (!ieee80211_sdata_running(sdata)) 668142b9f50SHelmut Schaa continue; 669142b9f50SHelmut Schaa 670142b9f50SHelmut Schaa if (sdata->vif.type == NL80211_IFTYPE_STATION) { 671142b9f50SHelmut Schaa if (sdata->u.mgd.associated) { 672142b9f50SHelmut Schaa associated = true; 673df13cce5SHelmut Schaa 674df13cce5SHelmut Schaa if (!qdisc_all_tx_empty(sdata->dev)) { 675df13cce5SHelmut Schaa tx_empty = false; 676142b9f50SHelmut Schaa break; 677142b9f50SHelmut Schaa } 678142b9f50SHelmut Schaa } 679142b9f50SHelmut Schaa } 680df13cce5SHelmut Schaa } 681142b9f50SHelmut Schaa mutex_unlock(&local->iflist_mtx); 682142b9f50SHelmut Schaa 6836ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 6846ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 6856ea0a69cSJohannes Berg 6866ea0a69cSJohannes Berg next_chan = scan_req->channels[local->scan_channel_idx]; 687b23b025fSBen Greear 688142b9f50SHelmut Schaa /* 689142b9f50SHelmut Schaa * we're currently scanning a different channel, let's 690df13cce5SHelmut Schaa * see if we can scan another channel without interfering 691df13cce5SHelmut Schaa * with the current traffic situation. 692df13cce5SHelmut Schaa * 6933f892b61SStanislaw Gruszka * Keep good latency, do not stay off-channel more than 125 ms. 694142b9f50SHelmut Schaa */ 695df13cce5SHelmut Schaa 696df13cce5SHelmut Schaa bad_latency = time_after(jiffies + 697df13cce5SHelmut Schaa ieee80211_scan_get_channel_time(next_chan), 6983f892b61SStanislaw Gruszka local->leave_oper_channel_time + HZ / 8); 699df13cce5SHelmut Schaa 700cd2bb512SSam Leffler if (associated && !tx_empty) { 7016ea0a69cSJohannes Berg if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 702cd2bb512SSam Leffler next_scan_state = SCAN_ABORT; 703142b9f50SHelmut Schaa else 704cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 7053f892b61SStanislaw Gruszka } else if (associated && bad_latency) { 706cd2bb512SSam Leffler next_scan_state = SCAN_SUSPEND; 707cd2bb512SSam Leffler } else { 708cd2bb512SSam Leffler next_scan_state = SCAN_SET_CHANNEL; 709cd2bb512SSam Leffler } 710cd2bb512SSam Leffler 711cd2bb512SSam Leffler local->next_scan_state = next_scan_state; 712142b9f50SHelmut Schaa 713142b9f50SHelmut Schaa *next_delay = 0; 7142fb3f028SHelmut Schaa } 7152fb3f028SHelmut Schaa 7162fb3f028SHelmut Schaa static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 7177d3be3ccSHelmut Schaa unsigned long *next_delay) 7187d3be3ccSHelmut Schaa { 7197d3be3ccSHelmut Schaa int skip; 7207d3be3ccSHelmut Schaa struct ieee80211_channel *chan; 7217ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width oper_scan_width; 7226ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 7236ea0a69cSJohannes Berg 7246ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 7256ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 7267d3be3ccSHelmut Schaa 7277d3be3ccSHelmut Schaa skip = 0; 7286ea0a69cSJohannes Berg chan = scan_req->channels[local->scan_channel_idx]; 7297d3be3ccSHelmut Schaa 7307ca15a0aSSimon Wunderlich local->scan_chandef.chan = chan; 7317ca15a0aSSimon Wunderlich local->scan_chandef.center_freq1 = chan->center_freq; 7327ca15a0aSSimon Wunderlich local->scan_chandef.center_freq2 = 0; 7336ea0a69cSJohannes Berg switch (scan_req->scan_width) { 7347ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_5: 7357ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 7367ca15a0aSSimon Wunderlich break; 7377ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_10: 7387ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 7397ca15a0aSSimon Wunderlich break; 7407ca15a0aSSimon Wunderlich case NL80211_BSS_CHAN_WIDTH_20: 7417ca15a0aSSimon Wunderlich /* If scanning on oper channel, use whatever channel-type 7427ca15a0aSSimon Wunderlich * is currently in use. 7437ca15a0aSSimon Wunderlich */ 7447ca15a0aSSimon Wunderlich oper_scan_width = cfg80211_chandef_to_scan_width( 7457ca15a0aSSimon Wunderlich &local->_oper_chandef); 7467ca15a0aSSimon Wunderlich if (chan == local->_oper_chandef.chan && 7476ea0a69cSJohannes Berg oper_scan_width == scan_req->scan_width) 7487ca15a0aSSimon Wunderlich local->scan_chandef = local->_oper_chandef; 7497ca15a0aSSimon Wunderlich else 7507ca15a0aSSimon Wunderlich local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 7517ca15a0aSSimon Wunderlich break; 7527ca15a0aSSimon Wunderlich } 753b23b025fSBen Greear 754584991dcSJohannes Berg if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 7557d3be3ccSHelmut Schaa skip = 1; 7567d3be3ccSHelmut Schaa 7577d3be3ccSHelmut Schaa /* advance state machine to next channel/band */ 7587d3be3ccSHelmut Schaa local->scan_channel_idx++; 7597d3be3ccSHelmut Schaa 7600ee9c13cSHelmut Schaa if (skip) { 7610ee9c13cSHelmut Schaa /* if we skip this channel return to the decision state */ 7620ee9c13cSHelmut Schaa local->next_scan_state = SCAN_DECISION; 7632fb3f028SHelmut Schaa return; 7640ee9c13cSHelmut Schaa } 7657d3be3ccSHelmut Schaa 7667d3be3ccSHelmut Schaa /* 7677d3be3ccSHelmut Schaa * Probe delay is used to update the NAV, cf. 11.1.3.2.2 7687d3be3ccSHelmut Schaa * (which unfortunately doesn't say _why_ step a) is done, 7697d3be3ccSHelmut Schaa * but it waits for the probe delay or until a frame is 7707d3be3ccSHelmut Schaa * received - and the received frame would update the NAV). 7717d3be3ccSHelmut Schaa * For now, we do not support waiting until a frame is 7727d3be3ccSHelmut Schaa * received. 7737d3be3ccSHelmut Schaa * 7747d3be3ccSHelmut Schaa * In any case, it is not necessary for a passive scan. 7757d3be3ccSHelmut Schaa */ 7766ea0a69cSJohannes Berg if (chan->flags & IEEE80211_CHAN_NO_IR || !scan_req->n_ssids) { 7777d3be3ccSHelmut Schaa *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 778977923b0SHelmut Schaa local->next_scan_state = SCAN_DECISION; 7792fb3f028SHelmut Schaa return; 7807d3be3ccSHelmut Schaa } 7817d3be3ccSHelmut Schaa 7822fb3f028SHelmut Schaa /* active scan, send probes */ 7837d3be3ccSHelmut Schaa *next_delay = IEEE80211_PROBE_DELAY; 784977923b0SHelmut Schaa local->next_scan_state = SCAN_SEND_PROBE; 7857d3be3ccSHelmut Schaa } 7867d3be3ccSHelmut Schaa 78707ef03eeSJohannes Berg static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 78807ef03eeSJohannes Berg unsigned long *next_delay) 78907ef03eeSJohannes Berg { 79007ef03eeSJohannes Berg /* switch back to the operating channel */ 7917ca15a0aSSimon Wunderlich local->scan_chandef.chan = NULL; 79207ef03eeSJohannes Berg ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 79307ef03eeSJohannes Berg 794aacde9eeSStanislaw Gruszka /* disable PS */ 795aacde9eeSStanislaw Gruszka ieee80211_offchannel_return(local); 79607ef03eeSJohannes Berg 79707ef03eeSJohannes Berg *next_delay = HZ / 5; 79807ef03eeSJohannes Berg /* afterwards, resume scan & go to next channel */ 79907ef03eeSJohannes Berg local->next_scan_state = SCAN_RESUME; 80007ef03eeSJohannes Berg } 80107ef03eeSJohannes Berg 80207ef03eeSJohannes Berg static void ieee80211_scan_state_resume(struct ieee80211_local *local, 80307ef03eeSJohannes Berg unsigned long *next_delay) 80407ef03eeSJohannes Berg { 805aacde9eeSStanislaw Gruszka ieee80211_offchannel_stop_vifs(local); 80607ef03eeSJohannes Berg 80707ef03eeSJohannes Berg if (local->ops->flush) { 8083b24f4c6SEmmanuel Grumbach ieee80211_flush_queues(local, NULL, false); 80907ef03eeSJohannes Berg *next_delay = 0; 81007ef03eeSJohannes Berg } else 81107ef03eeSJohannes Berg *next_delay = HZ / 10; 81207ef03eeSJohannes Berg 81307ef03eeSJohannes Berg /* remember when we left the operating channel */ 81407ef03eeSJohannes Berg local->leave_oper_channel_time = jiffies; 81507ef03eeSJohannes Berg 81607ef03eeSJohannes Berg /* advance to the next channel to be scanned */ 817de2ee84dSMohammed Shafi Shajakhan local->next_scan_state = SCAN_SET_CHANNEL; 81807ef03eeSJohannes Berg } 81907ef03eeSJohannes Berg 820c2b13452SJohannes Berg void ieee80211_scan_work(struct work_struct *work) 8210a51b27eSJohannes Berg { 8220a51b27eSJohannes Berg struct ieee80211_local *local = 8230a51b27eSJohannes Berg container_of(work, struct ieee80211_local, scan_work.work); 824d07bfd8bSJohannes Berg struct ieee80211_sub_if_data *sdata; 8256ea0a69cSJohannes Berg struct cfg80211_scan_request *scan_req; 8260a51b27eSJohannes Berg unsigned long next_delay = 0; 8278bd2a248SEliad Peller bool aborted; 8288789d459SJohannes Berg 829259b62e3SStanislaw Gruszka mutex_lock(&local->mtx); 830259b62e3SStanislaw Gruszka 831e2fd5dbcSJohannes Berg sdata = rcu_dereference_protected(local->scan_sdata, 832e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx)); 8336ea0a69cSJohannes Berg scan_req = rcu_dereference_protected(local->scan_req, 8346ea0a69cSJohannes Berg lockdep_is_held(&local->mtx)); 835d07bfd8bSJohannes Berg 8368a690674SBen Greear /* When scanning on-channel, the first-callback means completed. */ 8378a690674SBen Greear if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 8388a690674SBen Greear aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 8398a690674SBen Greear goto out_complete; 8408a690674SBen Greear } 8418a690674SBen Greear 842259b62e3SStanislaw Gruszka if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 8438789d459SJohannes Berg aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 844259b62e3SStanislaw Gruszka goto out_complete; 8458789d459SJohannes Berg } 8468789d459SJohannes Berg 8476ea0a69cSJohannes Berg if (!sdata || !scan_req) 848259b62e3SStanislaw Gruszka goto out; 849f3b85252SJohannes Berg 850ff5db439SEliad Peller if (!local->scanning) { 851f3b85252SJohannes Berg int rc; 852f3b85252SJohannes Berg 8536ea0a69cSJohannes Berg RCU_INIT_POINTER(local->scan_req, NULL); 8540c2bef46SMonam Agarwal RCU_INIT_POINTER(local->scan_sdata, NULL); 855f3b85252SJohannes Berg 8566ea0a69cSJohannes Berg rc = __ieee80211_start_scan(sdata, scan_req); 857259b62e3SStanislaw Gruszka if (rc) { 8583aed49efSStanislaw Gruszka /* need to complete scan in cfg80211 */ 8596ea0a69cSJohannes Berg rcu_assign_pointer(local->scan_req, scan_req); 860259b62e3SStanislaw Gruszka aborted = true; 861259b62e3SStanislaw Gruszka goto out_complete; 862259b62e3SStanislaw Gruszka } else 863259b62e3SStanislaw Gruszka goto out; 864f3b85252SJohannes Berg } 865f3b85252SJohannes Berg 8665bc75728SJohannes Berg /* 867f502d09bSHelmut Schaa * as long as no delay is required advance immediately 868f502d09bSHelmut Schaa * without scheduling a new work 869f502d09bSHelmut Schaa */ 870f502d09bSHelmut Schaa do { 871c29acf20SRajkumar Manoharan if (!ieee80211_sdata_running(sdata)) { 872c29acf20SRajkumar Manoharan aborted = true; 873c29acf20SRajkumar Manoharan goto out_complete; 874c29acf20SRajkumar Manoharan } 875c29acf20SRajkumar Manoharan 876977923b0SHelmut Schaa switch (local->next_scan_state) { 8772fb3f028SHelmut Schaa case SCAN_DECISION: 878e229f844SStanislaw Gruszka /* if no more bands/channels left, complete scan */ 8796ea0a69cSJohannes Berg if (local->scan_channel_idx >= scan_req->n_channels) { 880e229f844SStanislaw Gruszka aborted = false; 881e229f844SStanislaw Gruszka goto out_complete; 882e229f844SStanislaw Gruszka } 883e229f844SStanislaw Gruszka ieee80211_scan_state_decision(local, &next_delay); 8840a51b27eSJohannes Berg break; 8852fb3f028SHelmut Schaa case SCAN_SET_CHANNEL: 8862fb3f028SHelmut Schaa ieee80211_scan_state_set_channel(local, &next_delay); 8872fb3f028SHelmut Schaa break; 8880a51b27eSJohannes Berg case SCAN_SEND_PROBE: 8897d3be3ccSHelmut Schaa ieee80211_scan_state_send_probe(local, &next_delay); 8900a51b27eSJohannes Berg break; 89107ef03eeSJohannes Berg case SCAN_SUSPEND: 89207ef03eeSJohannes Berg ieee80211_scan_state_suspend(local, &next_delay); 893142b9f50SHelmut Schaa break; 89407ef03eeSJohannes Berg case SCAN_RESUME: 89507ef03eeSJohannes Berg ieee80211_scan_state_resume(local, &next_delay); 896142b9f50SHelmut Schaa break; 897cd2bb512SSam Leffler case SCAN_ABORT: 898cd2bb512SSam Leffler aborted = true; 899cd2bb512SSam Leffler goto out_complete; 9000a51b27eSJohannes Berg } 901f502d09bSHelmut Schaa } while (next_delay == 0); 9020a51b27eSJohannes Berg 90342935ecaSLuis R. Rodriguez ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 904d07bfd8bSJohannes Berg goto out; 905259b62e3SStanislaw Gruszka 906259b62e3SStanislaw Gruszka out_complete: 9078bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, aborted); 908259b62e3SStanislaw Gruszka out: 909259b62e3SStanislaw Gruszka mutex_unlock(&local->mtx); 9100a51b27eSJohannes Berg } 9110a51b27eSJohannes Berg 912c2b13452SJohannes Berg int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 9132a519311SJohannes Berg struct cfg80211_scan_request *req) 9140a51b27eSJohannes Berg { 915f3b85252SJohannes Berg int res; 9160a51b27eSJohannes Berg 917a1699b75SJohannes Berg mutex_lock(&sdata->local->mtx); 918f3b85252SJohannes Berg res = __ieee80211_start_scan(sdata, req); 919a1699b75SJohannes Berg mutex_unlock(&sdata->local->mtx); 9202a519311SJohannes Berg 921f3b85252SJohannes Berg return res; 9220a51b27eSJohannes Berg } 9230a51b27eSJohannes Berg 92434bcf715SStanislaw Gruszka int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 925be4a4b6aSJohannes Berg const u8 *ssid, u8 ssid_len, 9267ca15a0aSSimon Wunderlich struct ieee80211_channel *chan, 9277ca15a0aSSimon Wunderlich enum nl80211_bss_scan_width scan_width) 928f3b85252SJohannes Berg { 929f3b85252SJohannes Berg struct ieee80211_local *local = sdata->local; 930f3b85252SJohannes Berg int ret = -EBUSY; 931fb63bc41SGertjan van Wingerde enum ieee80211_band band; 9329116dd01SJohannes Berg 933a1699b75SJohannes Berg mutex_lock(&local->mtx); 934f3b85252SJohannes Berg 935f3b85252SJohannes Berg /* busy scanning */ 936f3b85252SJohannes Berg if (local->scan_req) 937f3b85252SJohannes Berg goto unlock; 938f3b85252SJohannes Berg 939be4a4b6aSJohannes Berg /* fill internal scan request */ 940be4a4b6aSJohannes Berg if (!chan) { 94134bcf715SStanislaw Gruszka int i, max_n; 94234bcf715SStanislaw Gruszka int n_ch = 0; 943be4a4b6aSJohannes Berg 944be4a4b6aSJohannes Berg for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 945be4a4b6aSJohannes Berg if (!local->hw.wiphy->bands[band]) 946be4a4b6aSJohannes Berg continue; 94734bcf715SStanislaw Gruszka 94834bcf715SStanislaw Gruszka max_n = local->hw.wiphy->bands[band]->n_channels; 94934bcf715SStanislaw Gruszka for (i = 0; i < max_n; i++) { 95034bcf715SStanislaw Gruszka struct ieee80211_channel *tmp_ch = 951be4a4b6aSJohannes Berg &local->hw.wiphy->bands[band]->channels[i]; 95234bcf715SStanislaw Gruszka 9538fe02e16SLuis R. Rodriguez if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 95434bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED)) 95534bcf715SStanislaw Gruszka continue; 95634bcf715SStanislaw Gruszka 95734bcf715SStanislaw Gruszka local->int_scan_req->channels[n_ch] = tmp_ch; 95834bcf715SStanislaw Gruszka n_ch++; 959be4a4b6aSJohannes Berg } 960be4a4b6aSJohannes Berg } 961be4a4b6aSJohannes Berg 96234bcf715SStanislaw Gruszka if (WARN_ON_ONCE(n_ch == 0)) 96334bcf715SStanislaw Gruszka goto unlock; 96434bcf715SStanislaw Gruszka 96534bcf715SStanislaw Gruszka local->int_scan_req->n_channels = n_ch; 966be4a4b6aSJohannes Berg } else { 9678fe02e16SLuis R. Rodriguez if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IR | 96834bcf715SStanislaw Gruszka IEEE80211_CHAN_DISABLED))) 96934bcf715SStanislaw Gruszka goto unlock; 97034bcf715SStanislaw Gruszka 971be4a4b6aSJohannes Berg local->int_scan_req->channels[0] = chan; 972be4a4b6aSJohannes Berg local->int_scan_req->n_channels = 1; 973be4a4b6aSJohannes Berg } 974be4a4b6aSJohannes Berg 975be4a4b6aSJohannes Berg local->int_scan_req->ssids = &local->scan_ssid; 976be4a4b6aSJohannes Berg local->int_scan_req->n_ssids = 1; 9777ca15a0aSSimon Wunderlich local->int_scan_req->scan_width = scan_width; 9785ba63533SJohannes Berg memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 9795ba63533SJohannes Berg local->int_scan_req->ssids[0].ssid_len = ssid_len; 980f3b85252SJohannes Berg 9815ba63533SJohannes Berg ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 982f3b85252SJohannes Berg unlock: 983a1699b75SJohannes Berg mutex_unlock(&local->mtx); 984f3b85252SJohannes Berg return ret; 9850a51b27eSJohannes Berg } 9865bb644a0SJohannes Berg 9874136c422SStanislaw Gruszka /* 9884136c422SStanislaw Gruszka * Only call this function when a scan can't be queued -- under RTNL. 9894136c422SStanislaw Gruszka */ 9905bb644a0SJohannes Berg void ieee80211_scan_cancel(struct ieee80211_local *local) 9915bb644a0SJohannes Berg { 9925bb644a0SJohannes Berg /* 993b856439bSEliad Peller * We are canceling software scan, or deferred scan that was not 9944136c422SStanislaw Gruszka * yet really started (see __ieee80211_start_scan ). 9954136c422SStanislaw Gruszka * 9964136c422SStanislaw Gruszka * Regarding hardware scan: 9974136c422SStanislaw Gruszka * - we can not call __ieee80211_scan_completed() as when 9984136c422SStanislaw Gruszka * SCAN_HW_SCANNING bit is set this function change 9994136c422SStanislaw Gruszka * local->hw_scan_req to operate on 5G band, what race with 10004136c422SStanislaw Gruszka * driver which can use local->hw_scan_req 10014136c422SStanislaw Gruszka * 10024136c422SStanislaw Gruszka * - we can not cancel scan_work since driver can schedule it 10034136c422SStanislaw Gruszka * by ieee80211_scan_completed(..., true) to finish scan 10044136c422SStanislaw Gruszka * 1005b856439bSEliad Peller * Hence we only call the cancel_hw_scan() callback, but the low-level 1006b856439bSEliad Peller * driver is still responsible for calling ieee80211_scan_completed() 1007b856439bSEliad Peller * after the scan was completed/aborted. 10085bb644a0SJohannes Berg */ 10094136c422SStanislaw Gruszka 1010a1699b75SJohannes Berg mutex_lock(&local->mtx); 1011b856439bSEliad Peller if (!local->scan_req) 1012b856439bSEliad Peller goto out; 1013b856439bSEliad Peller 1014a754055aSEmmanuel Grumbach /* 1015a754055aSEmmanuel Grumbach * We have a scan running and the driver already reported completion, 1016a754055aSEmmanuel Grumbach * but the worker hasn't run yet or is stuck on the mutex - mark it as 1017a754055aSEmmanuel Grumbach * cancelled. 1018a754055aSEmmanuel Grumbach */ 1019a754055aSEmmanuel Grumbach if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 1020a754055aSEmmanuel Grumbach test_bit(SCAN_COMPLETED, &local->scanning)) { 1021a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1022a754055aSEmmanuel Grumbach goto out; 1023a754055aSEmmanuel Grumbach } 1024a754055aSEmmanuel Grumbach 1025b856439bSEliad Peller if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 1026a754055aSEmmanuel Grumbach /* 1027a754055aSEmmanuel Grumbach * Make sure that __ieee80211_scan_completed doesn't trigger a 1028a754055aSEmmanuel Grumbach * scan on another band. 1029a754055aSEmmanuel Grumbach */ 1030a754055aSEmmanuel Grumbach set_bit(SCAN_HW_CANCELLED, &local->scanning); 1031b856439bSEliad Peller if (local->ops->cancel_hw_scan) 1032e2fd5dbcSJohannes Berg drv_cancel_hw_scan(local, 1033e2fd5dbcSJohannes Berg rcu_dereference_protected(local->scan_sdata, 1034e2fd5dbcSJohannes Berg lockdep_is_held(&local->mtx))); 1035b856439bSEliad Peller goto out; 1036b856439bSEliad Peller } 1037b856439bSEliad Peller 1038d07bfd8bSJohannes Berg /* 1039d07bfd8bSJohannes Berg * If the work is currently running, it must be blocked on 1040d07bfd8bSJohannes Berg * the mutex, but we'll set scan_sdata = NULL and it'll 1041d07bfd8bSJohannes Berg * simply exit once it acquires the mutex. 1042d07bfd8bSJohannes Berg */ 1043d07bfd8bSJohannes Berg cancel_delayed_work(&local->scan_work); 1044d07bfd8bSJohannes Berg /* and clean up */ 10458bd2a248SEliad Peller __ieee80211_scan_completed(&local->hw, true); 1046b856439bSEliad Peller out: 1047d07bfd8bSJohannes Berg mutex_unlock(&local->mtx); 10485bb644a0SJohannes Berg } 104979f460caSLuciano Coelho 1050d43c6b6eSDavid Spinadel int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 105179f460caSLuciano Coelho struct cfg80211_sched_scan_request *req) 105279f460caSLuciano Coelho { 105379f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 1054633e2713SDavid Spinadel struct ieee80211_scan_ies sched_scan_ies = {}; 10552103dec1SSimon Wunderlich struct cfg80211_chan_def chandef; 1056633e2713SDavid Spinadel int ret, i, iebufsz, num_bands = 0; 1057633e2713SDavid Spinadel u32 rate_masks[IEEE80211_NUM_BANDS] = {}; 1058633e2713SDavid Spinadel u8 bands_used = 0; 1059633e2713SDavid Spinadel u8 *ie; 1060633e2713SDavid Spinadel size_t len; 1061c604b9f2SJohannes Berg 1062e4dcbb37SDavid Spinadel iebufsz = local->scan_ies_len + req->ie_len; 106379f460caSLuciano Coelho 1064d43c6b6eSDavid Spinadel lockdep_assert_held(&local->mtx); 106579f460caSLuciano Coelho 1066d43c6b6eSDavid Spinadel if (!local->ops->sched_scan_start) 1067d43c6b6eSDavid Spinadel return -ENOTSUPP; 106879f460caSLuciano Coelho 106979f460caSLuciano Coelho for (i = 0; i < IEEE80211_NUM_BANDS; i++) { 1070633e2713SDavid Spinadel if (local->hw.wiphy->bands[i]) { 1071633e2713SDavid Spinadel bands_used |= BIT(i); 1072633e2713SDavid Spinadel rate_masks[i] = (u32) -1; 1073633e2713SDavid Spinadel num_bands++; 1074633e2713SDavid Spinadel } 1075633e2713SDavid Spinadel } 1076c56ef672SDavid Spinadel 1077633e2713SDavid Spinadel ie = kzalloc(num_bands * iebufsz, GFP_KERNEL); 1078633e2713SDavid Spinadel if (!ie) { 107979f460caSLuciano Coelho ret = -ENOMEM; 1080633e2713SDavid Spinadel goto out; 108179f460caSLuciano Coelho } 108279f460caSLuciano Coelho 10832103dec1SSimon Wunderlich ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 10842103dec1SSimon Wunderlich 1085633e2713SDavid Spinadel len = ieee80211_build_preq_ies(local, ie, num_bands * iebufsz, 1086633e2713SDavid Spinadel &sched_scan_ies, req->ie, 1087633e2713SDavid Spinadel req->ie_len, bands_used, 1088c56ef672SDavid Spinadel rate_masks, &chandef); 108979f460caSLuciano Coelho 109030dd3edfSJohannes Berg ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 1091d43c6b6eSDavid Spinadel if (ret == 0) { 10925260a5b2SJohannes Berg rcu_assign_pointer(local->sched_scan_sdata, sdata); 10936ea0a69cSJohannes Berg rcu_assign_pointer(local->sched_scan_req, req); 1094d43c6b6eSDavid Spinadel } 109579f460caSLuciano Coelho 1096633e2713SDavid Spinadel kfree(ie); 1097d43c6b6eSDavid Spinadel 1098633e2713SDavid Spinadel out: 1099d43c6b6eSDavid Spinadel if (ret) { 1100d43c6b6eSDavid Spinadel /* Clean in case of failure after HW restart or upon resume. */ 11010c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 11026ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1103d43c6b6eSDavid Spinadel } 1104d43c6b6eSDavid Spinadel 1105d43c6b6eSDavid Spinadel return ret; 1106d43c6b6eSDavid Spinadel } 1107d43c6b6eSDavid Spinadel 1108d43c6b6eSDavid Spinadel int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1109d43c6b6eSDavid Spinadel struct cfg80211_sched_scan_request *req) 1110d43c6b6eSDavid Spinadel { 1111d43c6b6eSDavid Spinadel struct ieee80211_local *local = sdata->local; 1112d43c6b6eSDavid Spinadel int ret; 1113d43c6b6eSDavid Spinadel 1114d43c6b6eSDavid Spinadel mutex_lock(&local->mtx); 1115d43c6b6eSDavid Spinadel 1116d43c6b6eSDavid Spinadel if (rcu_access_pointer(local->sched_scan_sdata)) { 1117d43c6b6eSDavid Spinadel mutex_unlock(&local->mtx); 1118d43c6b6eSDavid Spinadel return -EBUSY; 1119d43c6b6eSDavid Spinadel } 1120d43c6b6eSDavid Spinadel 1121d43c6b6eSDavid Spinadel ret = __ieee80211_request_sched_scan_start(sdata, req); 1122d43c6b6eSDavid Spinadel 11235260a5b2SJohannes Berg mutex_unlock(&local->mtx); 112479f460caSLuciano Coelho return ret; 112579f460caSLuciano Coelho } 112679f460caSLuciano Coelho 112785a9994aSLuciano Coelho int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata) 112879f460caSLuciano Coelho { 112979f460caSLuciano Coelho struct ieee80211_local *local = sdata->local; 113030dd3edfSJohannes Berg int ret = 0; 113179f460caSLuciano Coelho 11325260a5b2SJohannes Berg mutex_lock(&local->mtx); 113379f460caSLuciano Coelho 113479f460caSLuciano Coelho if (!local->ops->sched_scan_stop) { 113579f460caSLuciano Coelho ret = -ENOTSUPP; 113679f460caSLuciano Coelho goto out; 113779f460caSLuciano Coelho } 113879f460caSLuciano Coelho 1139d43c6b6eSDavid Spinadel /* We don't want to restart sched scan anymore. */ 11406ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1141d43c6b6eSDavid Spinadel 114271228a1eSAlexander Bondar if (rcu_access_pointer(local->sched_scan_sdata)) { 114337e3308cSJohannes Berg ret = drv_sched_scan_stop(local, sdata); 114471228a1eSAlexander Bondar if (!ret) 1145ad053a96SAndreea-Cristina Bernat RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 114671228a1eSAlexander Bondar } 114779f460caSLuciano Coelho out: 11485260a5b2SJohannes Berg mutex_unlock(&local->mtx); 114979f460caSLuciano Coelho 115079f460caSLuciano Coelho return ret; 115179f460caSLuciano Coelho } 115279f460caSLuciano Coelho 115379f460caSLuciano Coelho void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 115479f460caSLuciano Coelho { 115579f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 115679f460caSLuciano Coelho 115779f460caSLuciano Coelho trace_api_sched_scan_results(local); 115879f460caSLuciano Coelho 115979f460caSLuciano Coelho cfg80211_sched_scan_results(hw->wiphy); 116079f460caSLuciano Coelho } 116179f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_results); 116279f460caSLuciano Coelho 1163f6837ba8SJohannes Berg void ieee80211_sched_scan_end(struct ieee80211_local *local) 116485a9994aSLuciano Coelho { 116585a9994aSLuciano Coelho mutex_lock(&local->mtx); 116685a9994aSLuciano Coelho 11675260a5b2SJohannes Berg if (!rcu_access_pointer(local->sched_scan_sdata)) { 116885a9994aSLuciano Coelho mutex_unlock(&local->mtx); 116985a9994aSLuciano Coelho return; 117085a9994aSLuciano Coelho } 117185a9994aSLuciano Coelho 11720c2bef46SMonam Agarwal RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 117385a9994aSLuciano Coelho 1174d43c6b6eSDavid Spinadel /* If sched scan was aborted by the driver. */ 11756ea0a69cSJohannes Berg RCU_INIT_POINTER(local->sched_scan_req, NULL); 1176d43c6b6eSDavid Spinadel 117785a9994aSLuciano Coelho mutex_unlock(&local->mtx); 117885a9994aSLuciano Coelho 117985a9994aSLuciano Coelho cfg80211_sched_scan_stopped(local->hw.wiphy); 118085a9994aSLuciano Coelho } 118185a9994aSLuciano Coelho 1182f6837ba8SJohannes Berg void ieee80211_sched_scan_stopped_work(struct work_struct *work) 1183f6837ba8SJohannes Berg { 1184f6837ba8SJohannes Berg struct ieee80211_local *local = 1185f6837ba8SJohannes Berg container_of(work, struct ieee80211_local, 1186f6837ba8SJohannes Berg sched_scan_stopped_work); 1187f6837ba8SJohannes Berg 1188f6837ba8SJohannes Berg ieee80211_sched_scan_end(local); 1189f6837ba8SJohannes Berg } 1190f6837ba8SJohannes Berg 119179f460caSLuciano Coelho void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 119279f460caSLuciano Coelho { 119379f460caSLuciano Coelho struct ieee80211_local *local = hw_to_local(hw); 119479f460caSLuciano Coelho 119579f460caSLuciano Coelho trace_api_sched_scan_stopped(local); 119679f460caSLuciano Coelho 119718db594aSJohannes Berg schedule_work(&local->sched_scan_stopped_work); 119879f460caSLuciano Coelho } 119979f460caSLuciano Coelho EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1200