scan.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/etherdevice.h>
  66. #include <net/mac80211.h>
  67. #include "mvm.h"
  68. #include "iwl-eeprom-parse.h"
  69. #include "fw-api-scan.h"
  70. #define IWL_PLCP_QUIET_THRESH 1
  71. #define IWL_ACTIVE_QUIET_TIME 10
  72. struct iwl_mvm_scan_params {
  73. u32 max_out_time;
  74. u32 suspend_time;
  75. bool passive_fragmented;
  76. struct _dwell {
  77. u16 passive;
  78. u16 active;
  79. } dwell[IEEE80211_NUM_BANDS];
  80. };
  81. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  82. {
  83. u16 rx_chain;
  84. u8 rx_ant;
  85. if (mvm->scan_rx_ant != ANT_NONE)
  86. rx_ant = mvm->scan_rx_ant;
  87. else
  88. rx_ant = mvm->fw->valid_rx_ant;
  89. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  90. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  91. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  92. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  93. return cpu_to_le16(rx_chain);
  94. }
  95. static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
  96. {
  97. if (band == IEEE80211_BAND_2GHZ)
  98. return cpu_to_le32(PHY_BAND_24);
  99. else
  100. return cpu_to_le32(PHY_BAND_5);
  101. }
  102. static inline __le32
  103. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  104. bool no_cck)
  105. {
  106. u32 tx_ant;
  107. mvm->scan_last_antenna_idx =
  108. iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
  109. mvm->scan_last_antenna_idx);
  110. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  111. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  112. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  113. tx_ant);
  114. else
  115. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  116. }
  117. /*
  118. * We insert the SSIDs in an inverted order, because the FW will
  119. * invert it back. The most prioritized SSID, which is first in the
  120. * request list, is not copied here, but inserted directly to the probe
  121. * request.
  122. */
  123. static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
  124. struct cfg80211_ssid *ssids,
  125. int n_ssids, int first)
  126. {
  127. int fw_idx, req_idx;
  128. for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
  129. req_idx--, fw_idx++) {
  130. cmd_ssid[fw_idx].id = WLAN_EID_SSID;
  131. cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
  132. memcpy(cmd_ssid[fw_idx].ssid,
  133. ssids[req_idx].ssid,
  134. ssids[req_idx].ssid_len);
  135. }
  136. }
  137. /*
  138. * If req->n_ssids > 0, it means we should do an active scan.
  139. * In case of active scan w/o directed scan, we receive a zero-length SSID
  140. * just to notify that this scan is active and not passive.
  141. * In order to notify the FW of the number of SSIDs we wish to scan (including
  142. * the zero-length one), we need to set the corresponding bits in chan->type,
  143. * one for each SSID, and set the active bit (first). If the first SSID is
  144. * already included in the probe template, so we need to set only
  145. * req->n_ssids - 1 bits in addition to the first bit.
  146. */
  147. static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
  148. {
  149. if (band == IEEE80211_BAND_2GHZ)
  150. return 20 + 3 * (n_ssids + 1);
  151. return 10 + 2 * (n_ssids + 1);
  152. }
  153. static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
  154. {
  155. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  156. }
  157. static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
  158. struct cfg80211_scan_request *req,
  159. bool basic_ssid,
  160. struct iwl_mvm_scan_params *params)
  161. {
  162. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  163. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  164. int i;
  165. int type = BIT(req->n_ssids) - 1;
  166. enum ieee80211_band band = req->channels[0]->band;
  167. if (!basic_ssid)
  168. type |= BIT(req->n_ssids);
  169. for (i = 0; i < cmd->channel_count; i++) {
  170. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  171. chan->type = cpu_to_le32(type);
  172. if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
  173. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  174. chan->active_dwell = cpu_to_le16(params->dwell[band].active);
  175. chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
  176. chan->iteration_count = cpu_to_le16(1);
  177. chan++;
  178. }
  179. }
  180. /*
  181. * Fill in probe request with the following parameters:
  182. * TA is our vif HW address, which mac80211 ensures we have.
  183. * Packet is broadcasted, so this is both SA and DA.
  184. * The probe request IE is made out of two: first comes the most prioritized
  185. * SSID if a directed scan is requested. Second comes whatever extra
  186. * information was given to us as the scan request IE.
  187. */
  188. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  189. int n_ssids, const u8 *ssid, int ssid_len,
  190. const u8 *band_ie, int band_ie_len,
  191. const u8 *common_ie, int common_ie_len,
  192. int left)
  193. {
  194. int len = 0;
  195. u8 *pos = NULL;
  196. /* Make sure there is enough space for the probe request,
  197. * two mandatory IEs and the data */
  198. left -= 24;
  199. if (left < 0)
  200. return 0;
  201. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  202. eth_broadcast_addr(frame->da);
  203. memcpy(frame->sa, ta, ETH_ALEN);
  204. eth_broadcast_addr(frame->bssid);
  205. frame->seq_ctrl = 0;
  206. len += 24;
  207. /* for passive scans, no need to fill anything */
  208. if (n_ssids == 0)
  209. return (u16)len;
  210. /* points to the payload of the request */
  211. pos = &frame->u.probe_req.variable[0];
  212. /* fill in our SSID IE */
  213. left -= ssid_len + 2;
  214. if (left < 0)
  215. return 0;
  216. *pos++ = WLAN_EID_SSID;
  217. *pos++ = ssid_len;
  218. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  219. memcpy(pos, ssid, ssid_len);
  220. pos += ssid_len;
  221. }
  222. len += ssid_len + 2;
  223. if (WARN_ON(left < band_ie_len + common_ie_len))
  224. return len;
  225. if (band_ie && band_ie_len) {
  226. memcpy(pos, band_ie, band_ie_len);
  227. pos += band_ie_len;
  228. len += band_ie_len;
  229. }
  230. if (common_ie && common_ie_len) {
  231. memcpy(pos, common_ie, common_ie_len);
  232. pos += common_ie_len;
  233. len += common_ie_len;
  234. }
  235. return (u16)len;
  236. }
  237. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  238. struct ieee80211_vif *vif)
  239. {
  240. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  241. bool *global_bound = data;
  242. if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
  243. mvmvif->phy_ctxt->id < MAX_PHYS)
  244. *global_bound = true;
  245. }
  246. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  247. struct ieee80211_vif *vif,
  248. int n_ssids, u32 flags,
  249. struct iwl_mvm_scan_params *params)
  250. {
  251. bool global_bound = false;
  252. enum ieee80211_band band;
  253. u8 frag_passive_dwell = 0;
  254. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  255. IEEE80211_IFACE_ITER_NORMAL,
  256. iwl_mvm_scan_condition_iterator,
  257. &global_bound);
  258. if (!global_bound)
  259. goto not_bound;
  260. params->suspend_time = 30;
  261. params->max_out_time = 170;
  262. if (iwl_mvm_low_latency(mvm)) {
  263. if (mvm->fw->ucode_capa.api[0] &
  264. IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
  265. params->suspend_time = 105;
  266. params->max_out_time = 70;
  267. frag_passive_dwell = 20;
  268. } else {
  269. params->suspend_time = 120;
  270. params->max_out_time = 120;
  271. }
  272. }
  273. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  274. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  275. /*
  276. * P2P device scan should not be fragmented to avoid negative
  277. * impact on P2P device discovery. Configure max_out_time to be
  278. * equal to dwell time on passive channel. Take a longest
  279. * possible value, one that corresponds to 2GHz band
  280. */
  281. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  282. u32 passive_dwell =
  283. iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
  284. params->max_out_time = passive_dwell;
  285. } else {
  286. params->passive_fragmented = true;
  287. }
  288. }
  289. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  290. params->max_out_time = 200;
  291. not_bound:
  292. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  293. if (params->passive_fragmented)
  294. params->dwell[band].passive = frag_passive_dwell;
  295. else
  296. params->dwell[band].passive =
  297. iwl_mvm_get_passive_dwell(band);
  298. params->dwell[band].active = iwl_mvm_get_active_dwell(band,
  299. n_ssids);
  300. }
  301. }
  302. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  303. {
  304. /* require rrm scan whenever the fw supports it */
  305. return mvm->fw->ucode_capa.capa[0] &
  306. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
  307. }
  308. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
  309. bool is_sched_scan)
  310. {
  311. int max_probe_len;
  312. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  313. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  314. else
  315. max_probe_len = mvm->fw->ucode_capa.max_probe_length;
  316. /* we create the 802.11 header and SSID element */
  317. max_probe_len -= 24 + 2;
  318. /* basic ssid is added only for hw_scan with and old api */
  319. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
  320. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
  321. !is_sched_scan)
  322. max_probe_len -= 32;
  323. return max_probe_len;
  324. }
  325. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
  326. {
  327. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
  328. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
  329. return max_ie_len;
  330. /* TODO: [BUG] This function should return the maximum allowed size of
  331. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  332. * in the same command. So the correct implementation of this function
  333. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  334. * command has only 512 bytes and it would leave us with about 240
  335. * bytes for scan IEs, which is clearly not enough. So meanwhile
  336. * we will report an incorrect value. This may result in a failure to
  337. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  338. * functions with -ENOBUFS, if a large enough probe will be provided.
  339. */
  340. return max_ie_len;
  341. }
  342. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  343. struct ieee80211_vif *vif,
  344. struct cfg80211_scan_request *req)
  345. {
  346. struct iwl_host_cmd hcmd = {
  347. .id = SCAN_REQUEST_CMD,
  348. .len = { 0, },
  349. .data = { mvm->scan_cmd, },
  350. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  351. };
  352. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  353. int ret;
  354. u32 status;
  355. int ssid_len = 0;
  356. u8 *ssid = NULL;
  357. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  358. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  359. struct iwl_mvm_scan_params params = {};
  360. lockdep_assert_held(&mvm->mutex);
  361. /* we should have failed registration if scan_cmd was NULL */
  362. if (WARN_ON(mvm->scan_cmd == NULL))
  363. return -ENOMEM;
  364. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  365. mvm->scan_status = IWL_MVM_SCAN_OS;
  366. memset(cmd, 0, ksize(cmd));
  367. cmd->channel_count = (u8)req->n_channels;
  368. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  369. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  370. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  371. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
  372. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  373. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  374. if (params.passive_fragmented)
  375. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  376. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  377. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  378. MAC_FILTER_IN_BEACON);
  379. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  380. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  381. else
  382. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  383. cmd->repeats = cpu_to_le32(1);
  384. /*
  385. * If the user asked for passive scan, don't change to active scan if
  386. * you see any activity on the channel - remain passive.
  387. */
  388. if (req->n_ssids > 0) {
  389. cmd->passive2active = cpu_to_le16(1);
  390. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  391. if (basic_ssid) {
  392. ssid = req->ssids[0].ssid;
  393. ssid_len = req->ssids[0].ssid_len;
  394. }
  395. } else {
  396. cmd->passive2active = 0;
  397. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  398. }
  399. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
  400. basic_ssid ? 1 : 0);
  401. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  402. 3 << TX_CMD_FLG_BT_PRIO_POS);
  403. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  404. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  405. cmd->tx_cmd.rate_n_flags =
  406. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  407. req->no_cck);
  408. cmd->tx_cmd.len =
  409. cpu_to_le16(iwl_mvm_fill_probe_req(
  410. (struct ieee80211_mgmt *)cmd->data,
  411. vif->addr,
  412. req->n_ssids, ssid, ssid_len,
  413. req->ie, req->ie_len, NULL, 0,
  414. mvm->fw->ucode_capa.max_probe_length));
  415. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  416. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  417. le16_to_cpu(cmd->tx_cmd.len) +
  418. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  419. hcmd.len[0] = le16_to_cpu(cmd->len);
  420. status = SCAN_RESPONSE_OK;
  421. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  422. if (!ret && status == SCAN_RESPONSE_OK) {
  423. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  424. } else {
  425. /*
  426. * If the scan failed, it usually means that the FW was unable
  427. * to allocate the time events. Warn on it, but maybe we
  428. * should try to send the command again with different params.
  429. */
  430. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  431. status, ret);
  432. mvm->scan_status = IWL_MVM_SCAN_NONE;
  433. ret = -EIO;
  434. }
  435. return ret;
  436. }
  437. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  438. struct iwl_device_cmd *cmd)
  439. {
  440. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  441. struct iwl_cmd_response *resp = (void *)pkt->data;
  442. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  443. le32_to_cpu(resp->status));
  444. return 0;
  445. }
  446. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  447. struct iwl_device_cmd *cmd)
  448. {
  449. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  450. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  451. lockdep_assert_held(&mvm->mutex);
  452. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  453. notif->status, notif->scanned_channels);
  454. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  455. mvm->scan_status = IWL_MVM_SCAN_NONE;
  456. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  457. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  458. return 0;
  459. }
  460. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  461. struct iwl_rx_cmd_buffer *rxb,
  462. struct iwl_device_cmd *cmd)
  463. {
  464. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  465. u8 client_bitmap = 0;
  466. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  467. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  468. client_bitmap = notif->client_bitmap;
  469. }
  470. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
  471. client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
  472. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  473. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  474. ieee80211_sched_scan_results(mvm->hw);
  475. } else {
  476. IWL_DEBUG_SCAN(mvm, "Scan results\n");
  477. }
  478. }
  479. return 0;
  480. }
  481. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  482. struct iwl_rx_packet *pkt, void *data)
  483. {
  484. struct iwl_mvm *mvm =
  485. container_of(notif_wait, struct iwl_mvm, notif_wait);
  486. struct iwl_scan_complete_notif *notif;
  487. u32 *resp;
  488. switch (pkt->hdr.cmd) {
  489. case SCAN_ABORT_CMD:
  490. resp = (void *)pkt->data;
  491. if (*resp == CAN_ABORT_STATUS) {
  492. IWL_DEBUG_SCAN(mvm,
  493. "Scan can be aborted, wait until completion\n");
  494. return false;
  495. }
  496. /*
  497. * If scan cannot be aborted, it means that we had a
  498. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  499. * ieee80211_scan_completed already.
  500. */
  501. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  502. *resp);
  503. return true;
  504. case SCAN_COMPLETE_NOTIFICATION:
  505. notif = (void *)pkt->data;
  506. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  507. notif->status);
  508. return true;
  509. default:
  510. WARN_ON(1);
  511. return false;
  512. };
  513. }
  514. static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
  515. {
  516. struct iwl_notification_wait wait_scan_abort;
  517. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  518. SCAN_COMPLETE_NOTIFICATION };
  519. int ret;
  520. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  521. scan_abort_notif,
  522. ARRAY_SIZE(scan_abort_notif),
  523. iwl_mvm_scan_abort_notif, NULL);
  524. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  525. if (ret) {
  526. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  527. /* mac80211's state will be cleaned in the nic_restart flow */
  528. goto out_remove_notif;
  529. }
  530. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  531. out_remove_notif:
  532. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  533. return ret;
  534. }
  535. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  536. struct iwl_rx_cmd_buffer *rxb,
  537. struct iwl_device_cmd *cmd)
  538. {
  539. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  540. u8 status, ebs_status;
  541. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  542. struct iwl_periodic_scan_complete *scan_notif;
  543. scan_notif = (void *)pkt->data;
  544. status = scan_notif->status;
  545. ebs_status = scan_notif->ebs_status;
  546. } else {
  547. struct iwl_scan_offload_complete *scan_notif;
  548. scan_notif = (void *)pkt->data;
  549. status = scan_notif->status;
  550. ebs_status = scan_notif->ebs_status;
  551. }
  552. /* scan status must be locked for proper checking */
  553. lockdep_assert_held(&mvm->mutex);
  554. IWL_DEBUG_SCAN(mvm,
  555. "%s completed, status %s, EBS status %s\n",
  556. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  557. "Scheduled scan" : "Scan",
  558. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  559. "completed" : "aborted",
  560. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  561. "success" : "failed");
  562. /* only call mac80211 completion if the stop was initiated by FW */
  563. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  564. mvm->scan_status = IWL_MVM_SCAN_NONE;
  565. ieee80211_sched_scan_stopped(mvm->hw);
  566. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  567. mvm->scan_status = IWL_MVM_SCAN_NONE;
  568. ieee80211_scan_completed(mvm->hw,
  569. status == IWL_SCAN_OFFLOAD_ABORTED);
  570. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  571. }
  572. mvm->last_ebs_successful = !ebs_status;
  573. return 0;
  574. }
  575. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  576. struct ieee80211_vif *vif,
  577. struct ieee80211_scan_ies *ies,
  578. enum ieee80211_band band,
  579. struct iwl_tx_cmd *cmd,
  580. u8 *data)
  581. {
  582. u16 cmd_len;
  583. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  584. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  585. cmd->sta_id = mvm->aux_sta.sta_id;
  586. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  587. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  588. vif->addr,
  589. 1, NULL, 0,
  590. ies->ies[band], ies->len[band],
  591. ies->common_ies, ies->common_ie_len,
  592. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  593. cmd->len = cpu_to_le16(cmd_len);
  594. }
  595. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  596. struct ieee80211_vif *vif,
  597. struct cfg80211_sched_scan_request *req,
  598. struct iwl_scan_offload_cmd *scan,
  599. struct iwl_mvm_scan_params *params)
  600. {
  601. scan->channel_count = req->n_channels;
  602. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  603. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  604. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  605. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  606. scan->max_out_time = cpu_to_le32(params->max_out_time);
  607. scan->suspend_time = cpu_to_le32(params->suspend_time);
  608. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  609. MAC_FILTER_IN_BEACON);
  610. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  611. scan->rep_count = cpu_to_le32(1);
  612. if (params->passive_fragmented)
  613. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  614. }
  615. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  616. {
  617. int i;
  618. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  619. if (!ssid_list[i].len)
  620. break;
  621. if (ssid_list[i].len == ssid_len &&
  622. !memcmp(ssid_list->ssid, ssid, ssid_len))
  623. return i;
  624. }
  625. return -1;
  626. }
  627. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  628. struct iwl_ssid_ie *direct_scan,
  629. u32 *ssid_bitmap, bool basic_ssid)
  630. {
  631. int i, j;
  632. int index;
  633. /*
  634. * copy SSIDs from match list.
  635. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  636. * config match list.
  637. */
  638. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  639. /* skip empty SSID matchsets */
  640. if (!req->match_sets[i].ssid.ssid_len)
  641. continue;
  642. direct_scan[i].id = WLAN_EID_SSID;
  643. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  644. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  645. direct_scan[i].len);
  646. }
  647. /* add SSIDs from scan SSID list */
  648. *ssid_bitmap = 0;
  649. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  650. index = iwl_ssid_exist(req->ssids[j].ssid,
  651. req->ssids[j].ssid_len,
  652. direct_scan);
  653. if (index < 0) {
  654. if (!req->ssids[j].ssid_len && basic_ssid)
  655. continue;
  656. direct_scan[i].id = WLAN_EID_SSID;
  657. direct_scan[i].len = req->ssids[j].ssid_len;
  658. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  659. direct_scan[i].len);
  660. *ssid_bitmap |= BIT(i + 1);
  661. i++;
  662. } else {
  663. *ssid_bitmap |= BIT(index + 1);
  664. }
  665. }
  666. }
  667. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  668. struct cfg80211_sched_scan_request *req,
  669. u8 *channels_buffer,
  670. enum ieee80211_band band,
  671. int *head,
  672. u32 ssid_bitmap,
  673. struct iwl_mvm_scan_params *params)
  674. {
  675. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  676. __le32 *type = (__le32 *)channels_buffer;
  677. __le16 *channel_number = (__le16 *)(type + n_channels);
  678. __le16 *iter_count = channel_number + n_channels;
  679. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  680. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  681. u8 *passive_dwell = active_dwell + n_channels;
  682. int i, index = 0;
  683. for (i = 0; i < req->n_channels; i++) {
  684. struct ieee80211_channel *chan = req->channels[i];
  685. if (chan->band != band)
  686. continue;
  687. index = *head;
  688. (*head)++;
  689. channel_number[index] = cpu_to_le16(chan->hw_value);
  690. active_dwell[index] = params->dwell[band].active;
  691. passive_dwell[index] = params->dwell[band].passive;
  692. iter_count[index] = cpu_to_le16(1);
  693. iter_interval[index] = 0;
  694. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  695. type[index] |=
  696. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  697. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  698. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  699. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  700. type[index] |=
  701. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  702. /* scan for all SSIDs from req->ssids */
  703. type[index] |= cpu_to_le32(ssid_bitmap);
  704. }
  705. }
  706. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  707. struct ieee80211_vif *vif,
  708. struct cfg80211_sched_scan_request *req,
  709. struct ieee80211_scan_ies *ies)
  710. {
  711. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  712. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  713. int head = 0;
  714. u32 ssid_bitmap;
  715. int cmd_len;
  716. int ret;
  717. u8 *probes;
  718. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  719. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  720. struct iwl_scan_offload_cfg *scan_cfg;
  721. struct iwl_host_cmd cmd = {
  722. .id = SCAN_OFFLOAD_CONFIG_CMD,
  723. };
  724. struct iwl_mvm_scan_params params = {};
  725. lockdep_assert_held(&mvm->mutex);
  726. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  727. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  728. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  729. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  730. if (!scan_cfg)
  731. return -ENOMEM;
  732. probes = scan_cfg->data +
  733. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  734. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  735. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  736. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  737. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  738. &ssid_bitmap, basic_ssid);
  739. /* build tx frames for supported bands */
  740. if (band_2ghz) {
  741. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  742. IEEE80211_BAND_2GHZ,
  743. &scan_cfg->scan_cmd.tx_cmd[0],
  744. probes);
  745. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  746. IEEE80211_BAND_2GHZ, &head,
  747. ssid_bitmap, &params);
  748. }
  749. if (band_5ghz) {
  750. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  751. IEEE80211_BAND_5GHZ,
  752. &scan_cfg->scan_cmd.tx_cmd[1],
  753. probes +
  754. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  755. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  756. IEEE80211_BAND_5GHZ, &head,
  757. ssid_bitmap, &params);
  758. }
  759. cmd.data[0] = scan_cfg;
  760. cmd.len[0] = cmd_len;
  761. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  762. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  763. ret = iwl_mvm_send_cmd(mvm, &cmd);
  764. kfree(scan_cfg);
  765. return ret;
  766. }
  767. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  768. struct cfg80211_sched_scan_request *req)
  769. {
  770. struct iwl_scan_offload_profile *profile;
  771. struct iwl_scan_offload_profile_cfg *profile_cfg;
  772. struct iwl_scan_offload_blacklist *blacklist;
  773. struct iwl_host_cmd cmd = {
  774. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  775. .len[1] = sizeof(*profile_cfg),
  776. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  777. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  778. };
  779. int blacklist_len;
  780. int i;
  781. int ret;
  782. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  783. return -EIO;
  784. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  785. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  786. else
  787. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  788. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  789. if (!blacklist)
  790. return -ENOMEM;
  791. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  792. if (!profile_cfg) {
  793. ret = -ENOMEM;
  794. goto free_blacklist;
  795. }
  796. cmd.data[0] = blacklist;
  797. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  798. cmd.data[1] = profile_cfg;
  799. /* No blacklist configuration */
  800. profile_cfg->num_profiles = req->n_match_sets;
  801. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  802. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  803. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  804. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  805. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  806. for (i = 0; i < req->n_match_sets; i++) {
  807. profile = &profile_cfg->profiles[i];
  808. profile->ssid_index = i;
  809. /* Support any cipher and auth algorithm */
  810. profile->unicast_cipher = 0xff;
  811. profile->auth_alg = 0xff;
  812. profile->network_type = IWL_NETWORK_TYPE_ANY;
  813. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  814. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  815. }
  816. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  817. ret = iwl_mvm_send_cmd(mvm, &cmd);
  818. kfree(profile_cfg);
  819. free_blacklist:
  820. kfree(blacklist);
  821. return ret;
  822. }
  823. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  824. struct cfg80211_sched_scan_request *req)
  825. {
  826. struct iwl_scan_offload_req scan_req = {
  827. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  828. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  829. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  830. .schedule_line[0].full_scan_mul = 1,
  831. .schedule_line[1].iterations = 0xff,
  832. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  833. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  834. };
  835. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  836. IWL_DEBUG_SCAN(mvm,
  837. "Sending scheduled scan with filtering, filter len %d\n",
  838. req->n_match_sets);
  839. } else {
  840. IWL_DEBUG_SCAN(mvm,
  841. "Sending Scheduled scan without filtering\n");
  842. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  843. }
  844. if (mvm->last_ebs_successful &&
  845. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  846. scan_req.flags |=
  847. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  848. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  849. sizeof(scan_req), &scan_req);
  850. }
  851. int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
  852. struct ieee80211_vif *vif,
  853. struct cfg80211_sched_scan_request *req,
  854. struct ieee80211_scan_ies *ies)
  855. {
  856. int ret;
  857. if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  858. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  859. if (ret)
  860. return ret;
  861. ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
  862. } else {
  863. ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
  864. if (ret)
  865. return ret;
  866. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  867. if (ret)
  868. return ret;
  869. ret = iwl_mvm_sched_scan_start(mvm, req);
  870. }
  871. return ret;
  872. }
  873. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  874. {
  875. int ret;
  876. struct iwl_host_cmd cmd = {
  877. .id = SCAN_OFFLOAD_ABORT_CMD,
  878. };
  879. u32 status;
  880. /* Exit instantly with error when device is not ready
  881. * to receive scan abort command or it does not perform
  882. * scheduled scan currently */
  883. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  884. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  885. mvm->scan_status != IWL_MVM_SCAN_OS))
  886. return -EIO;
  887. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  888. if (ret)
  889. return ret;
  890. if (status != CAN_ABORT_STATUS) {
  891. /*
  892. * The scan abort will return 1 for success or
  893. * 2 for "failure". A failure condition can be
  894. * due to simply not being in an active scan which
  895. * can occur if we send the scan abort before the
  896. * microcode has notified us that a scan is completed.
  897. */
  898. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  899. ret = -ENOENT;
  900. }
  901. return ret;
  902. }
  903. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  904. {
  905. int ret;
  906. struct iwl_notification_wait wait_scan_done;
  907. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  908. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  909. lockdep_assert_held(&mvm->mutex);
  910. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  911. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  912. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  913. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  914. return 0;
  915. }
  916. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  917. scan_done_notif,
  918. ARRAY_SIZE(scan_done_notif),
  919. NULL, NULL);
  920. ret = iwl_mvm_send_scan_offload_abort(mvm);
  921. if (ret) {
  922. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  923. sched ? "offloaded " : "", ret);
  924. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  925. return ret;
  926. }
  927. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  928. sched ? "offloaded " : "");
  929. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  930. if (ret)
  931. return ret;
  932. /*
  933. * Clear the scan status so the next scan requests will succeed. This
  934. * also ensures the Rx handler doesn't do anything, as the scan was
  935. * stopped from above. Since the rx handler won't do anything now,
  936. * we have to release the scan reference here.
  937. */
  938. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  939. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  940. mvm->scan_status = IWL_MVM_SCAN_NONE;
  941. if (notify) {
  942. if (sched)
  943. ieee80211_sched_scan_stopped(mvm->hw);
  944. else
  945. ieee80211_scan_completed(mvm->hw, true);
  946. }
  947. return 0;
  948. }
  949. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  950. struct iwl_scan_req_tx_cmd *tx_cmd,
  951. bool no_cck)
  952. {
  953. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  954. TX_CMD_FLG_BT_DIS);
  955. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  956. IEEE80211_BAND_2GHZ,
  957. no_cck);
  958. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  959. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  960. TX_CMD_FLG_BT_DIS);
  961. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  962. IEEE80211_BAND_5GHZ,
  963. no_cck);
  964. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  965. }
  966. static void
  967. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  968. struct ieee80211_channel **channels,
  969. int n_channels, u32 ssid_bitmap,
  970. struct iwl_scan_req_unified_lmac *cmd)
  971. {
  972. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  973. int i;
  974. for (i = 0; i < n_channels; i++) {
  975. channel_cfg[i].channel_num =
  976. cpu_to_le16(channels[i]->hw_value);
  977. channel_cfg[i].iter_count = cpu_to_le16(1);
  978. channel_cfg[i].iter_interval = 0;
  979. channel_cfg[i].flags =
  980. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  981. ssid_bitmap);
  982. }
  983. }
  984. static void
  985. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  986. struct ieee80211_scan_ies *ies,
  987. struct iwl_scan_req_unified_lmac *cmd)
  988. {
  989. struct iwl_scan_probe_req *preq = (void *)(cmd->data +
  990. sizeof(struct iwl_scan_channel_cfg_lmac) *
  991. mvm->fw->ucode_capa.n_scan_channels);
  992. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  993. u8 *pos;
  994. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  995. eth_broadcast_addr(frame->da);
  996. memcpy(frame->sa, vif->addr, ETH_ALEN);
  997. eth_broadcast_addr(frame->bssid);
  998. frame->seq_ctrl = 0;
  999. pos = frame->u.probe_req.variable;
  1000. *pos++ = WLAN_EID_SSID;
  1001. *pos++ = 0;
  1002. preq->mac_header.offset = 0;
  1003. preq->mac_header.len = cpu_to_le16(24 + 2);
  1004. memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
  1005. ies->len[IEEE80211_BAND_2GHZ]);
  1006. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  1007. preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
  1008. pos += ies->len[IEEE80211_BAND_2GHZ];
  1009. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  1010. ies->len[IEEE80211_BAND_5GHZ]);
  1011. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  1012. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  1013. pos += ies->len[IEEE80211_BAND_5GHZ];
  1014. memcpy(pos, ies->common_ies, ies->common_ie_len);
  1015. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  1016. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  1017. }
  1018. static void
  1019. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  1020. struct iwl_scan_req_unified_lmac *cmd,
  1021. struct iwl_mvm_scan_params *params)
  1022. {
  1023. memset(cmd, 0, ksize(cmd));
  1024. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1025. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1026. if (params->passive_fragmented)
  1027. cmd->fragmented_dwell =
  1028. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1029. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  1030. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1031. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1032. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1033. cmd->iter_num = cpu_to_le32(1);
  1034. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1035. mvm->last_ebs_successful) {
  1036. cmd->channel_opt[0].flags =
  1037. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1038. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1039. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1040. cmd->channel_opt[1].flags =
  1041. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1042. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1043. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1044. }
  1045. if (iwl_mvm_rrm_scan_needed(mvm))
  1046. cmd->scan_flags |=
  1047. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  1048. }
  1049. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  1050. struct ieee80211_vif *vif,
  1051. struct ieee80211_scan_request *req)
  1052. {
  1053. struct iwl_host_cmd hcmd = {
  1054. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1055. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1056. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1057. mvm->fw->ucode_capa.n_scan_channels +
  1058. sizeof(struct iwl_scan_probe_req), },
  1059. .data = { mvm->scan_cmd, },
  1060. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1061. };
  1062. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1063. struct iwl_mvm_scan_params params = {};
  1064. u32 flags;
  1065. int ssid_bitmap = 0;
  1066. int ret, i;
  1067. lockdep_assert_held(&mvm->mutex);
  1068. /* we should have failed registration if scan_cmd was NULL */
  1069. if (WARN_ON(mvm->scan_cmd == NULL))
  1070. return -ENOMEM;
  1071. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  1072. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  1073. req->ies.len[NL80211_BAND_5GHZ] >
  1074. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  1075. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1076. return -ENOBUFS;
  1077. mvm->scan_status = IWL_MVM_SCAN_OS;
  1078. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1079. &params);
  1080. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1081. cmd->n_channels = (u8)req->req.n_channels;
  1082. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1083. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1084. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1085. if (params.passive_fragmented)
  1086. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1087. if (req->req.n_ssids == 0)
  1088. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1089. cmd->scan_flags |= cpu_to_le32(flags);
  1090. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1091. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1092. MAC_FILTER_IN_BEACON);
  1093. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1094. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1095. req->req.n_ssids, 0);
  1096. cmd->schedule[0].delay = 0;
  1097. cmd->schedule[0].iterations = 1;
  1098. cmd->schedule[0].full_scan_mul = 0;
  1099. cmd->schedule[1].delay = 0;
  1100. cmd->schedule[1].iterations = 0;
  1101. cmd->schedule[1].full_scan_mul = 0;
  1102. for (i = 1; i <= req->req.n_ssids; i++)
  1103. ssid_bitmap |= BIT(i);
  1104. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1105. req->req.n_channels, ssid_bitmap,
  1106. cmd);
  1107. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
  1108. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1109. if (!ret) {
  1110. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1111. } else {
  1112. /*
  1113. * If the scan failed, it usually means that the FW was unable
  1114. * to allocate the time events. Warn on it, but maybe we
  1115. * should try to send the command again with different params.
  1116. */
  1117. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1118. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1119. ret = -EIO;
  1120. }
  1121. return ret;
  1122. }
  1123. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1124. struct ieee80211_vif *vif,
  1125. struct cfg80211_sched_scan_request *req,
  1126. struct ieee80211_scan_ies *ies)
  1127. {
  1128. struct iwl_host_cmd hcmd = {
  1129. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1130. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1131. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1132. mvm->fw->ucode_capa.n_scan_channels +
  1133. sizeof(struct iwl_scan_probe_req), },
  1134. .data = { mvm->scan_cmd, },
  1135. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1136. };
  1137. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1138. struct iwl_mvm_scan_params params = {};
  1139. int ret;
  1140. u32 flags = 0, ssid_bitmap = 0;
  1141. lockdep_assert_held(&mvm->mutex);
  1142. /* we should have failed registration if scan_cmd was NULL */
  1143. if (WARN_ON(mvm->scan_cmd == NULL))
  1144. return -ENOMEM;
  1145. if (req->n_ssids > PROBE_OPTION_MAX ||
  1146. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1147. ies->len[NL80211_BAND_5GHZ] >
  1148. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  1149. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1150. return -ENOBUFS;
  1151. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1152. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1153. cmd->n_channels = (u8)req->n_channels;
  1154. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  1155. IWL_DEBUG_SCAN(mvm,
  1156. "Sending scheduled scan with filtering, n_match_sets %d\n",
  1157. req->n_match_sets);
  1158. } else {
  1159. IWL_DEBUG_SCAN(mvm,
  1160. "Sending Scheduled scan without filtering\n");
  1161. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1162. }
  1163. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1164. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1165. if (params.passive_fragmented)
  1166. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1167. if (req->n_ssids == 0)
  1168. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1169. cmd->scan_flags |= cpu_to_le32(flags);
  1170. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1171. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1172. MAC_FILTER_IN_BEACON);
  1173. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1174. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1175. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1176. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1177. cmd->schedule[0].full_scan_mul = 1;
  1178. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1179. cmd->schedule[1].iterations = 0xff;
  1180. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1181. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1182. ssid_bitmap, cmd);
  1183. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
  1184. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1185. if (!ret) {
  1186. IWL_DEBUG_SCAN(mvm,
  1187. "Sched scan request was sent successfully\n");
  1188. } else {
  1189. /*
  1190. * If the scan failed, it usually means that the FW was unable
  1191. * to allocate the time events. Warn on it, but maybe we
  1192. * should try to send the command again with different params.
  1193. */
  1194. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1195. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1196. ret = -EIO;
  1197. }
  1198. return ret;
  1199. }
  1200. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  1201. {
  1202. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  1203. return 0;
  1204. if (iwl_mvm_is_radio_killed(mvm)) {
  1205. ieee80211_scan_completed(mvm->hw, true);
  1206. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1207. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1208. return 0;
  1209. }
  1210. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1211. return iwl_mvm_scan_offload_stop(mvm, true);
  1212. return iwl_mvm_cancel_regular_scan(mvm);
  1213. }