scan.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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 (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
  243. *global_bound = true;
  244. }
  245. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  246. struct ieee80211_vif *vif,
  247. int n_ssids, u32 flags,
  248. struct iwl_mvm_scan_params *params)
  249. {
  250. bool global_bound = false;
  251. enum ieee80211_band band;
  252. u8 frag_passive_dwell = 0;
  253. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  254. IEEE80211_IFACE_ITER_NORMAL,
  255. iwl_mvm_scan_condition_iterator,
  256. &global_bound);
  257. if (!global_bound)
  258. goto not_bound;
  259. params->suspend_time = 30;
  260. params->max_out_time = 170;
  261. if (iwl_mvm_low_latency(mvm)) {
  262. if (mvm->fw->ucode_capa.api[0] &
  263. IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
  264. params->suspend_time = 105;
  265. params->max_out_time = 70;
  266. frag_passive_dwell = 20;
  267. } else {
  268. params->suspend_time = 120;
  269. params->max_out_time = 120;
  270. }
  271. }
  272. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  273. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  274. /*
  275. * P2P device scan should not be fragmented to avoid negative
  276. * impact on P2P device discovery. Configure max_out_time to be
  277. * equal to dwell time on passive channel. Take a longest
  278. * possible value, one that corresponds to 2GHz band
  279. */
  280. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  281. u32 passive_dwell =
  282. iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
  283. params->max_out_time = passive_dwell;
  284. } else {
  285. params->passive_fragmented = true;
  286. }
  287. }
  288. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  289. params->max_out_time = 200;
  290. not_bound:
  291. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  292. if (params->passive_fragmented)
  293. params->dwell[band].passive = frag_passive_dwell;
  294. else
  295. params->dwell[band].passive =
  296. iwl_mvm_get_passive_dwell(band);
  297. params->dwell[band].active = iwl_mvm_get_active_dwell(band,
  298. n_ssids);
  299. }
  300. }
  301. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  302. {
  303. /* require rrm scan whenever the fw supports it */
  304. return mvm->fw->ucode_capa.capa[0] &
  305. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
  306. }
  307. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
  308. bool is_sched_scan)
  309. {
  310. int max_probe_len;
  311. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  312. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  313. else
  314. max_probe_len = mvm->fw->ucode_capa.max_probe_length;
  315. /* we create the 802.11 header and SSID element */
  316. max_probe_len -= 24 + 2;
  317. /* basic ssid is added only for hw_scan with and old api */
  318. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
  319. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
  320. !is_sched_scan)
  321. max_probe_len -= 32;
  322. return max_probe_len;
  323. }
  324. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
  325. {
  326. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
  327. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
  328. return max_ie_len;
  329. /* TODO: [BUG] This function should return the maximum allowed size of
  330. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  331. * in the same command. So the correct implementation of this function
  332. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  333. * command has only 512 bytes and it would leave us with about 240
  334. * bytes for scan IEs, which is clearly not enough. So meanwhile
  335. * we will report an incorrect value. This may result in a failure to
  336. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  337. * functions with -ENOBUFS, if a large enough probe will be provided.
  338. */
  339. return max_ie_len;
  340. }
  341. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  342. struct ieee80211_vif *vif,
  343. struct cfg80211_scan_request *req)
  344. {
  345. struct iwl_host_cmd hcmd = {
  346. .id = SCAN_REQUEST_CMD,
  347. .len = { 0, },
  348. .data = { mvm->scan_cmd, },
  349. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  350. };
  351. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  352. int ret;
  353. u32 status;
  354. int ssid_len = 0;
  355. u8 *ssid = NULL;
  356. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  357. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  358. struct iwl_mvm_scan_params params = {};
  359. lockdep_assert_held(&mvm->mutex);
  360. /* we should have failed registration if scan_cmd was NULL */
  361. if (WARN_ON(mvm->scan_cmd == NULL))
  362. return -ENOMEM;
  363. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  364. mvm->scan_status = IWL_MVM_SCAN_OS;
  365. memset(cmd, 0, ksize(cmd));
  366. cmd->channel_count = (u8)req->n_channels;
  367. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  368. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  369. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  370. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
  371. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  372. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  373. if (params.passive_fragmented)
  374. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  375. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  376. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  377. MAC_FILTER_IN_BEACON);
  378. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  379. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  380. else
  381. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  382. cmd->repeats = cpu_to_le32(1);
  383. /*
  384. * If the user asked for passive scan, don't change to active scan if
  385. * you see any activity on the channel - remain passive.
  386. */
  387. if (req->n_ssids > 0) {
  388. cmd->passive2active = cpu_to_le16(1);
  389. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  390. if (basic_ssid) {
  391. ssid = req->ssids[0].ssid;
  392. ssid_len = req->ssids[0].ssid_len;
  393. }
  394. } else {
  395. cmd->passive2active = 0;
  396. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  397. }
  398. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
  399. basic_ssid ? 1 : 0);
  400. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  401. 3 << TX_CMD_FLG_BT_PRIO_POS);
  402. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  403. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  404. cmd->tx_cmd.rate_n_flags =
  405. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  406. req->no_cck);
  407. cmd->tx_cmd.len =
  408. cpu_to_le16(iwl_mvm_fill_probe_req(
  409. (struct ieee80211_mgmt *)cmd->data,
  410. vif->addr,
  411. req->n_ssids, ssid, ssid_len,
  412. req->ie, req->ie_len, NULL, 0,
  413. mvm->fw->ucode_capa.max_probe_length));
  414. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  415. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  416. le16_to_cpu(cmd->tx_cmd.len) +
  417. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  418. hcmd.len[0] = le16_to_cpu(cmd->len);
  419. status = SCAN_RESPONSE_OK;
  420. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  421. if (!ret && status == SCAN_RESPONSE_OK) {
  422. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  423. } else {
  424. /*
  425. * If the scan failed, it usually means that the FW was unable
  426. * to allocate the time events. Warn on it, but maybe we
  427. * should try to send the command again with different params.
  428. */
  429. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  430. status, ret);
  431. mvm->scan_status = IWL_MVM_SCAN_NONE;
  432. ret = -EIO;
  433. }
  434. return ret;
  435. }
  436. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  437. struct iwl_device_cmd *cmd)
  438. {
  439. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  440. struct iwl_cmd_response *resp = (void *)pkt->data;
  441. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  442. le32_to_cpu(resp->status));
  443. return 0;
  444. }
  445. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  446. struct iwl_device_cmd *cmd)
  447. {
  448. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  449. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  450. lockdep_assert_held(&mvm->mutex);
  451. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  452. notif->status, notif->scanned_channels);
  453. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  454. mvm->scan_status = IWL_MVM_SCAN_NONE;
  455. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  456. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  457. return 0;
  458. }
  459. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  460. struct iwl_rx_cmd_buffer *rxb,
  461. struct iwl_device_cmd *cmd)
  462. {
  463. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  464. u8 client_bitmap = 0;
  465. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  466. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  467. client_bitmap = notif->client_bitmap;
  468. }
  469. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
  470. client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
  471. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  472. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  473. ieee80211_sched_scan_results(mvm->hw);
  474. } else {
  475. IWL_DEBUG_SCAN(mvm, "Scan results\n");
  476. }
  477. }
  478. return 0;
  479. }
  480. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  481. struct iwl_rx_packet *pkt, void *data)
  482. {
  483. struct iwl_mvm *mvm =
  484. container_of(notif_wait, struct iwl_mvm, notif_wait);
  485. struct iwl_scan_complete_notif *notif;
  486. u32 *resp;
  487. switch (pkt->hdr.cmd) {
  488. case SCAN_ABORT_CMD:
  489. resp = (void *)pkt->data;
  490. if (*resp == CAN_ABORT_STATUS) {
  491. IWL_DEBUG_SCAN(mvm,
  492. "Scan can be aborted, wait until completion\n");
  493. return false;
  494. }
  495. /*
  496. * If scan cannot be aborted, it means that we had a
  497. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  498. * ieee80211_scan_completed already.
  499. */
  500. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  501. *resp);
  502. return true;
  503. case SCAN_COMPLETE_NOTIFICATION:
  504. notif = (void *)pkt->data;
  505. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  506. notif->status);
  507. return true;
  508. default:
  509. WARN_ON(1);
  510. return false;
  511. };
  512. }
  513. static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
  514. {
  515. struct iwl_notification_wait wait_scan_abort;
  516. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  517. SCAN_COMPLETE_NOTIFICATION };
  518. int ret;
  519. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  520. scan_abort_notif,
  521. ARRAY_SIZE(scan_abort_notif),
  522. iwl_mvm_scan_abort_notif, NULL);
  523. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  524. if (ret) {
  525. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  526. /* mac80211's state will be cleaned in the nic_restart flow */
  527. goto out_remove_notif;
  528. }
  529. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  530. out_remove_notif:
  531. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  532. return ret;
  533. }
  534. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  535. struct iwl_rx_cmd_buffer *rxb,
  536. struct iwl_device_cmd *cmd)
  537. {
  538. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  539. u8 status, ebs_status;
  540. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  541. struct iwl_periodic_scan_complete *scan_notif;
  542. scan_notif = (void *)pkt->data;
  543. status = scan_notif->status;
  544. ebs_status = scan_notif->ebs_status;
  545. } else {
  546. struct iwl_scan_offload_complete *scan_notif;
  547. scan_notif = (void *)pkt->data;
  548. status = scan_notif->status;
  549. ebs_status = scan_notif->ebs_status;
  550. }
  551. /* scan status must be locked for proper checking */
  552. lockdep_assert_held(&mvm->mutex);
  553. IWL_DEBUG_SCAN(mvm,
  554. "%s completed, status %s, EBS status %s\n",
  555. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  556. "Scheduled scan" : "Scan",
  557. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  558. "completed" : "aborted",
  559. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  560. "success" : "failed");
  561. /* only call mac80211 completion if the stop was initiated by FW */
  562. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  563. mvm->scan_status = IWL_MVM_SCAN_NONE;
  564. ieee80211_sched_scan_stopped(mvm->hw);
  565. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  566. mvm->scan_status = IWL_MVM_SCAN_NONE;
  567. ieee80211_scan_completed(mvm->hw,
  568. status == IWL_SCAN_OFFLOAD_ABORTED);
  569. }
  570. mvm->last_ebs_successful = !ebs_status;
  571. return 0;
  572. }
  573. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  574. struct ieee80211_vif *vif,
  575. struct ieee80211_scan_ies *ies,
  576. enum ieee80211_band band,
  577. struct iwl_tx_cmd *cmd,
  578. u8 *data)
  579. {
  580. u16 cmd_len;
  581. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  582. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  583. cmd->sta_id = mvm->aux_sta.sta_id;
  584. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  585. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  586. vif->addr,
  587. 1, NULL, 0,
  588. ies->ies[band], ies->len[band],
  589. ies->common_ies, ies->common_ie_len,
  590. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  591. cmd->len = cpu_to_le16(cmd_len);
  592. }
  593. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  594. struct ieee80211_vif *vif,
  595. struct cfg80211_sched_scan_request *req,
  596. struct iwl_scan_offload_cmd *scan,
  597. struct iwl_mvm_scan_params *params)
  598. {
  599. scan->channel_count = req->n_channels;
  600. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  601. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  602. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  603. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  604. scan->max_out_time = cpu_to_le32(params->max_out_time);
  605. scan->suspend_time = cpu_to_le32(params->suspend_time);
  606. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  607. MAC_FILTER_IN_BEACON);
  608. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  609. scan->rep_count = cpu_to_le32(1);
  610. if (params->passive_fragmented)
  611. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  612. }
  613. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  614. {
  615. int i;
  616. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  617. if (!ssid_list[i].len)
  618. break;
  619. if (ssid_list[i].len == ssid_len &&
  620. !memcmp(ssid_list->ssid, ssid, ssid_len))
  621. return i;
  622. }
  623. return -1;
  624. }
  625. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  626. struct iwl_ssid_ie *direct_scan,
  627. u32 *ssid_bitmap, bool basic_ssid)
  628. {
  629. int i, j;
  630. int index;
  631. /*
  632. * copy SSIDs from match list.
  633. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  634. * config match list.
  635. */
  636. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  637. /* skip empty SSID matchsets */
  638. if (!req->match_sets[i].ssid.ssid_len)
  639. continue;
  640. direct_scan[i].id = WLAN_EID_SSID;
  641. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  642. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  643. direct_scan[i].len);
  644. }
  645. /* add SSIDs from scan SSID list */
  646. *ssid_bitmap = 0;
  647. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  648. index = iwl_ssid_exist(req->ssids[j].ssid,
  649. req->ssids[j].ssid_len,
  650. direct_scan);
  651. if (index < 0) {
  652. if (!req->ssids[j].ssid_len && basic_ssid)
  653. continue;
  654. direct_scan[i].id = WLAN_EID_SSID;
  655. direct_scan[i].len = req->ssids[j].ssid_len;
  656. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  657. direct_scan[i].len);
  658. *ssid_bitmap |= BIT(i + 1);
  659. i++;
  660. } else {
  661. *ssid_bitmap |= BIT(index + 1);
  662. }
  663. }
  664. }
  665. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  666. struct cfg80211_sched_scan_request *req,
  667. u8 *channels_buffer,
  668. enum ieee80211_band band,
  669. int *head,
  670. u32 ssid_bitmap,
  671. struct iwl_mvm_scan_params *params)
  672. {
  673. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  674. __le32 *type = (__le32 *)channels_buffer;
  675. __le16 *channel_number = (__le16 *)(type + n_channels);
  676. __le16 *iter_count = channel_number + n_channels;
  677. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  678. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  679. u8 *passive_dwell = active_dwell + n_channels;
  680. int i, index = 0;
  681. for (i = 0; i < req->n_channels; i++) {
  682. struct ieee80211_channel *chan = req->channels[i];
  683. if (chan->band != band)
  684. continue;
  685. index = *head;
  686. (*head)++;
  687. channel_number[index] = cpu_to_le16(chan->hw_value);
  688. active_dwell[index] = params->dwell[band].active;
  689. passive_dwell[index] = params->dwell[band].passive;
  690. iter_count[index] = cpu_to_le16(1);
  691. iter_interval[index] = 0;
  692. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  693. type[index] |=
  694. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  695. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  696. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  697. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  698. type[index] |=
  699. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  700. /* scan for all SSIDs from req->ssids */
  701. type[index] |= cpu_to_le32(ssid_bitmap);
  702. }
  703. }
  704. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  705. struct ieee80211_vif *vif,
  706. struct cfg80211_sched_scan_request *req,
  707. struct ieee80211_scan_ies *ies)
  708. {
  709. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  710. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  711. int head = 0;
  712. u32 ssid_bitmap;
  713. int cmd_len;
  714. int ret;
  715. u8 *probes;
  716. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  717. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  718. struct iwl_scan_offload_cfg *scan_cfg;
  719. struct iwl_host_cmd cmd = {
  720. .id = SCAN_OFFLOAD_CONFIG_CMD,
  721. };
  722. struct iwl_mvm_scan_params params = {};
  723. lockdep_assert_held(&mvm->mutex);
  724. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  725. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  726. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  727. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  728. if (!scan_cfg)
  729. return -ENOMEM;
  730. probes = scan_cfg->data +
  731. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  732. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  733. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  734. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  735. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  736. &ssid_bitmap, basic_ssid);
  737. /* build tx frames for supported bands */
  738. if (band_2ghz) {
  739. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  740. IEEE80211_BAND_2GHZ,
  741. &scan_cfg->scan_cmd.tx_cmd[0],
  742. probes);
  743. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  744. IEEE80211_BAND_2GHZ, &head,
  745. ssid_bitmap, &params);
  746. }
  747. if (band_5ghz) {
  748. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  749. IEEE80211_BAND_5GHZ,
  750. &scan_cfg->scan_cmd.tx_cmd[1],
  751. probes +
  752. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  753. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  754. IEEE80211_BAND_5GHZ, &head,
  755. ssid_bitmap, &params);
  756. }
  757. cmd.data[0] = scan_cfg;
  758. cmd.len[0] = cmd_len;
  759. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  760. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  761. ret = iwl_mvm_send_cmd(mvm, &cmd);
  762. kfree(scan_cfg);
  763. return ret;
  764. }
  765. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  766. struct cfg80211_sched_scan_request *req)
  767. {
  768. struct iwl_scan_offload_profile *profile;
  769. struct iwl_scan_offload_profile_cfg *profile_cfg;
  770. struct iwl_scan_offload_blacklist *blacklist;
  771. struct iwl_host_cmd cmd = {
  772. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  773. .len[1] = sizeof(*profile_cfg),
  774. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  775. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  776. };
  777. int blacklist_len;
  778. int i;
  779. int ret;
  780. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  781. return -EIO;
  782. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  783. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  784. else
  785. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  786. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  787. if (!blacklist)
  788. return -ENOMEM;
  789. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  790. if (!profile_cfg) {
  791. ret = -ENOMEM;
  792. goto free_blacklist;
  793. }
  794. cmd.data[0] = blacklist;
  795. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  796. cmd.data[1] = profile_cfg;
  797. /* No blacklist configuration */
  798. profile_cfg->num_profiles = req->n_match_sets;
  799. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  800. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  801. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  802. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  803. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  804. for (i = 0; i < req->n_match_sets; i++) {
  805. profile = &profile_cfg->profiles[i];
  806. profile->ssid_index = i;
  807. /* Support any cipher and auth algorithm */
  808. profile->unicast_cipher = 0xff;
  809. profile->auth_alg = 0xff;
  810. profile->network_type = IWL_NETWORK_TYPE_ANY;
  811. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  812. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  813. }
  814. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  815. ret = iwl_mvm_send_cmd(mvm, &cmd);
  816. kfree(profile_cfg);
  817. free_blacklist:
  818. kfree(blacklist);
  819. return ret;
  820. }
  821. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  822. struct cfg80211_sched_scan_request *req)
  823. {
  824. struct iwl_scan_offload_req scan_req = {
  825. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  826. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  827. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  828. .schedule_line[0].full_scan_mul = 1,
  829. .schedule_line[1].iterations = 0xff,
  830. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  831. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  832. };
  833. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  834. IWL_DEBUG_SCAN(mvm,
  835. "Sending scheduled scan with filtering, filter len %d\n",
  836. req->n_match_sets);
  837. } else {
  838. IWL_DEBUG_SCAN(mvm,
  839. "Sending Scheduled scan without filtering\n");
  840. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  841. }
  842. if (mvm->last_ebs_successful &&
  843. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  844. scan_req.flags |=
  845. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  846. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  847. sizeof(scan_req), &scan_req);
  848. }
  849. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  850. {
  851. int ret;
  852. struct iwl_host_cmd cmd = {
  853. .id = SCAN_OFFLOAD_ABORT_CMD,
  854. };
  855. u32 status;
  856. /* Exit instantly with error when device is not ready
  857. * to receive scan abort command or it does not perform
  858. * scheduled scan currently */
  859. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  860. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  861. mvm->scan_status != IWL_MVM_SCAN_OS))
  862. return -EIO;
  863. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  864. if (ret)
  865. return ret;
  866. if (status != CAN_ABORT_STATUS) {
  867. /*
  868. * The scan abort will return 1 for success or
  869. * 2 for "failure". A failure condition can be
  870. * due to simply not being in an active scan which
  871. * can occur if we send the scan abort before the
  872. * microcode has notified us that a scan is completed.
  873. */
  874. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  875. ret = -ENOENT;
  876. }
  877. return ret;
  878. }
  879. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  880. {
  881. int ret;
  882. struct iwl_notification_wait wait_scan_done;
  883. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  884. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  885. lockdep_assert_held(&mvm->mutex);
  886. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  887. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  888. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  889. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  890. return 0;
  891. }
  892. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  893. scan_done_notif,
  894. ARRAY_SIZE(scan_done_notif),
  895. NULL, NULL);
  896. ret = iwl_mvm_send_scan_offload_abort(mvm);
  897. if (ret) {
  898. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  899. sched ? "offloaded " : "", ret);
  900. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  901. return ret;
  902. }
  903. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  904. sched ? "offloaded " : "");
  905. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  906. if (ret)
  907. return ret;
  908. /*
  909. * Clear the scan status so the next scan requests will succeed. This
  910. * also ensures the Rx handler doesn't do anything, as the scan was
  911. * stopped from above.
  912. */
  913. mvm->scan_status = IWL_MVM_SCAN_NONE;
  914. if (notify) {
  915. if (sched)
  916. ieee80211_sched_scan_stopped(mvm->hw);
  917. else
  918. ieee80211_scan_completed(mvm->hw, true);
  919. }
  920. return 0;
  921. }
  922. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  923. struct iwl_scan_req_tx_cmd *tx_cmd,
  924. bool no_cck)
  925. {
  926. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  927. TX_CMD_FLG_BT_DIS);
  928. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  929. IEEE80211_BAND_2GHZ,
  930. no_cck);
  931. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  932. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  933. TX_CMD_FLG_BT_DIS);
  934. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  935. IEEE80211_BAND_5GHZ,
  936. no_cck);
  937. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  938. }
  939. static void
  940. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  941. struct ieee80211_channel **channels,
  942. int n_channels, u32 ssid_bitmap,
  943. struct iwl_scan_req_unified_lmac *cmd)
  944. {
  945. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  946. int i;
  947. for (i = 0; i < n_channels; i++) {
  948. channel_cfg[i].channel_num =
  949. cpu_to_le16(channels[i]->hw_value);
  950. channel_cfg[i].iter_count = cpu_to_le16(1);
  951. channel_cfg[i].iter_interval = 0;
  952. channel_cfg[i].flags =
  953. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  954. ssid_bitmap);
  955. }
  956. }
  957. static void
  958. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  959. struct ieee80211_scan_ies *ies,
  960. struct iwl_scan_req_unified_lmac *cmd)
  961. {
  962. struct iwl_scan_probe_req *preq = (void *)(cmd->data +
  963. sizeof(struct iwl_scan_channel_cfg_lmac) *
  964. mvm->fw->ucode_capa.n_scan_channels);
  965. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  966. u8 *pos;
  967. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  968. eth_broadcast_addr(frame->da);
  969. memcpy(frame->sa, vif->addr, ETH_ALEN);
  970. eth_broadcast_addr(frame->bssid);
  971. frame->seq_ctrl = 0;
  972. pos = frame->u.probe_req.variable;
  973. *pos++ = WLAN_EID_SSID;
  974. *pos++ = 0;
  975. preq->mac_header.offset = 0;
  976. preq->mac_header.len = cpu_to_le16(24 + 2);
  977. memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
  978. ies->len[IEEE80211_BAND_2GHZ]);
  979. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  980. preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
  981. pos += ies->len[IEEE80211_BAND_2GHZ];
  982. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  983. ies->len[IEEE80211_BAND_5GHZ]);
  984. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  985. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  986. pos += ies->len[IEEE80211_BAND_5GHZ];
  987. memcpy(pos, ies->common_ies, ies->common_ie_len);
  988. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  989. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  990. }
  991. static void
  992. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  993. struct iwl_scan_req_unified_lmac *cmd,
  994. struct iwl_mvm_scan_params *params)
  995. {
  996. memset(cmd, 0, ksize(cmd));
  997. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  998. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  999. if (params->passive_fragmented)
  1000. cmd->fragmented_dwell =
  1001. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1002. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  1003. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1004. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1005. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1006. cmd->iter_num = cpu_to_le32(1);
  1007. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1008. mvm->last_ebs_successful) {
  1009. cmd->channel_opt[0].flags =
  1010. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1011. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1012. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1013. cmd->channel_opt[1].flags =
  1014. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1015. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1016. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1017. }
  1018. if (iwl_mvm_rrm_scan_needed(mvm))
  1019. cmd->scan_flags |=
  1020. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  1021. }
  1022. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  1023. struct ieee80211_vif *vif,
  1024. struct ieee80211_scan_request *req)
  1025. {
  1026. struct iwl_host_cmd hcmd = {
  1027. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1028. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1029. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1030. mvm->fw->ucode_capa.n_scan_channels +
  1031. sizeof(struct iwl_scan_probe_req), },
  1032. .data = { mvm->scan_cmd, },
  1033. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1034. };
  1035. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1036. struct iwl_mvm_scan_params params = {};
  1037. u32 flags;
  1038. int ssid_bitmap = 0;
  1039. int ret, i;
  1040. lockdep_assert_held(&mvm->mutex);
  1041. /* we should have failed registration if scan_cmd was NULL */
  1042. if (WARN_ON(mvm->scan_cmd == NULL))
  1043. return -ENOMEM;
  1044. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  1045. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  1046. req->ies.len[NL80211_BAND_5GHZ] >
  1047. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  1048. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1049. return -ENOBUFS;
  1050. mvm->scan_status = IWL_MVM_SCAN_OS;
  1051. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1052. &params);
  1053. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1054. cmd->n_channels = (u8)req->req.n_channels;
  1055. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1056. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1057. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1058. if (params.passive_fragmented)
  1059. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1060. if (req->req.n_ssids == 0)
  1061. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1062. cmd->scan_flags |= cpu_to_le32(flags);
  1063. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1064. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1065. MAC_FILTER_IN_BEACON);
  1066. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1067. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1068. req->req.n_ssids, 0);
  1069. cmd->schedule[0].delay = 0;
  1070. cmd->schedule[0].iterations = 1;
  1071. cmd->schedule[0].full_scan_mul = 0;
  1072. cmd->schedule[1].delay = 0;
  1073. cmd->schedule[1].iterations = 0;
  1074. cmd->schedule[1].full_scan_mul = 0;
  1075. for (i = 1; i <= req->req.n_ssids; i++)
  1076. ssid_bitmap |= BIT(i);
  1077. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1078. req->req.n_channels, ssid_bitmap,
  1079. cmd);
  1080. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
  1081. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1082. if (!ret) {
  1083. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1084. } else {
  1085. /*
  1086. * If the scan failed, it usually means that the FW was unable
  1087. * to allocate the time events. Warn on it, but maybe we
  1088. * should try to send the command again with different params.
  1089. */
  1090. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1091. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1092. ret = -EIO;
  1093. }
  1094. return ret;
  1095. }
  1096. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1097. struct ieee80211_vif *vif,
  1098. struct cfg80211_sched_scan_request *req,
  1099. struct ieee80211_scan_ies *ies)
  1100. {
  1101. struct iwl_host_cmd hcmd = {
  1102. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1103. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1104. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1105. mvm->fw->ucode_capa.n_scan_channels +
  1106. sizeof(struct iwl_scan_probe_req), },
  1107. .data = { mvm->scan_cmd, },
  1108. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1109. };
  1110. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1111. struct iwl_mvm_scan_params params = {};
  1112. int ret;
  1113. u32 flags = 0, ssid_bitmap = 0;
  1114. lockdep_assert_held(&mvm->mutex);
  1115. /* we should have failed registration if scan_cmd was NULL */
  1116. if (WARN_ON(mvm->scan_cmd == NULL))
  1117. return -ENOMEM;
  1118. if (req->n_ssids > PROBE_OPTION_MAX ||
  1119. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1120. ies->len[NL80211_BAND_5GHZ] >
  1121. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  1122. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1123. return -ENOBUFS;
  1124. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1125. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1126. cmd->n_channels = (u8)req->n_channels;
  1127. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  1128. IWL_DEBUG_SCAN(mvm,
  1129. "Sending scheduled scan with filtering, n_match_sets %d\n",
  1130. req->n_match_sets);
  1131. flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
  1132. } else {
  1133. IWL_DEBUG_SCAN(mvm,
  1134. "Sending Scheduled scan without filtering\n");
  1135. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1136. }
  1137. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1138. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1139. if (params.passive_fragmented)
  1140. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1141. if (req->n_ssids == 0)
  1142. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1143. cmd->scan_flags |= cpu_to_le32(flags);
  1144. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1145. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1146. MAC_FILTER_IN_BEACON);
  1147. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1148. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1149. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1150. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1151. cmd->schedule[0].full_scan_mul = 1;
  1152. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1153. cmd->schedule[1].iterations = 0xff;
  1154. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1155. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1156. ssid_bitmap, cmd);
  1157. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
  1158. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1159. if (!ret) {
  1160. IWL_DEBUG_SCAN(mvm,
  1161. "Sched scan request was sent successfully\n");
  1162. } else {
  1163. /*
  1164. * If the scan failed, it usually means that the FW was unable
  1165. * to allocate the time events. Warn on it, but maybe we
  1166. * should try to send the command again with different params.
  1167. */
  1168. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1169. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1170. ret = -EIO;
  1171. }
  1172. return ret;
  1173. }
  1174. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  1175. {
  1176. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  1177. return 0;
  1178. if (iwl_mvm_is_radio_killed(mvm)) {
  1179. ieee80211_scan_completed(mvm->hw, true);
  1180. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1181. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1182. return 0;
  1183. }
  1184. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1185. return iwl_mvm_scan_offload_stop(mvm, true);
  1186. return iwl_mvm_cancel_regular_scan(mvm);
  1187. }