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. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  521. return 0;
  522. if (iwl_mvm_is_radio_killed(mvm)) {
  523. ieee80211_scan_completed(mvm->hw, true);
  524. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  525. mvm->scan_status = IWL_MVM_SCAN_NONE;
  526. return 0;
  527. }
  528. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  529. scan_abort_notif,
  530. ARRAY_SIZE(scan_abort_notif),
  531. iwl_mvm_scan_abort_notif, NULL);
  532. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  533. if (ret) {
  534. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  535. /* mac80211's state will be cleaned in the nic_restart flow */
  536. goto out_remove_notif;
  537. }
  538. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  539. out_remove_notif:
  540. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  541. return ret;
  542. }
  543. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  544. struct iwl_rx_cmd_buffer *rxb,
  545. struct iwl_device_cmd *cmd)
  546. {
  547. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  548. u8 status, ebs_status;
  549. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  550. struct iwl_periodic_scan_complete *scan_notif;
  551. scan_notif = (void *)pkt->data;
  552. status = scan_notif->status;
  553. ebs_status = scan_notif->ebs_status;
  554. } else {
  555. struct iwl_scan_offload_complete *scan_notif;
  556. scan_notif = (void *)pkt->data;
  557. status = scan_notif->status;
  558. ebs_status = scan_notif->ebs_status;
  559. }
  560. /* scan status must be locked for proper checking */
  561. lockdep_assert_held(&mvm->mutex);
  562. IWL_DEBUG_SCAN(mvm,
  563. "%s completed, status %s, EBS status %s\n",
  564. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  565. "Scheduled scan" : "Scan",
  566. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  567. "completed" : "aborted",
  568. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  569. "success" : "failed");
  570. /* only call mac80211 completion if the stop was initiated by FW */
  571. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  572. mvm->scan_status = IWL_MVM_SCAN_NONE;
  573. ieee80211_sched_scan_stopped(mvm->hw);
  574. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  575. mvm->scan_status = IWL_MVM_SCAN_NONE;
  576. ieee80211_scan_completed(mvm->hw,
  577. status == IWL_SCAN_OFFLOAD_ABORTED);
  578. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  579. }
  580. mvm->last_ebs_successful = !ebs_status;
  581. return 0;
  582. }
  583. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  584. struct ieee80211_vif *vif,
  585. struct ieee80211_scan_ies *ies,
  586. enum ieee80211_band band,
  587. struct iwl_tx_cmd *cmd,
  588. u8 *data)
  589. {
  590. u16 cmd_len;
  591. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  592. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  593. cmd->sta_id = mvm->aux_sta.sta_id;
  594. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  595. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  596. vif->addr,
  597. 1, NULL, 0,
  598. ies->ies[band], ies->len[band],
  599. ies->common_ies, ies->common_ie_len,
  600. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  601. cmd->len = cpu_to_le16(cmd_len);
  602. }
  603. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  604. struct ieee80211_vif *vif,
  605. struct cfg80211_sched_scan_request *req,
  606. struct iwl_scan_offload_cmd *scan,
  607. struct iwl_mvm_scan_params *params)
  608. {
  609. scan->channel_count = req->n_channels;
  610. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  611. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  612. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  613. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  614. scan->max_out_time = cpu_to_le32(params->max_out_time);
  615. scan->suspend_time = cpu_to_le32(params->suspend_time);
  616. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  617. MAC_FILTER_IN_BEACON);
  618. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  619. scan->rep_count = cpu_to_le32(1);
  620. if (params->passive_fragmented)
  621. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  622. }
  623. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  624. {
  625. int i;
  626. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  627. if (!ssid_list[i].len)
  628. break;
  629. if (ssid_list[i].len == ssid_len &&
  630. !memcmp(ssid_list->ssid, ssid, ssid_len))
  631. return i;
  632. }
  633. return -1;
  634. }
  635. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  636. struct iwl_ssid_ie *direct_scan,
  637. u32 *ssid_bitmap, bool basic_ssid)
  638. {
  639. int i, j;
  640. int index;
  641. /*
  642. * copy SSIDs from match list.
  643. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  644. * config match list.
  645. */
  646. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  647. /* skip empty SSID matchsets */
  648. if (!req->match_sets[i].ssid.ssid_len)
  649. continue;
  650. direct_scan[i].id = WLAN_EID_SSID;
  651. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  652. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  653. direct_scan[i].len);
  654. }
  655. /* add SSIDs from scan SSID list */
  656. *ssid_bitmap = 0;
  657. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  658. index = iwl_ssid_exist(req->ssids[j].ssid,
  659. req->ssids[j].ssid_len,
  660. direct_scan);
  661. if (index < 0) {
  662. if (!req->ssids[j].ssid_len && basic_ssid)
  663. continue;
  664. direct_scan[i].id = WLAN_EID_SSID;
  665. direct_scan[i].len = req->ssids[j].ssid_len;
  666. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  667. direct_scan[i].len);
  668. *ssid_bitmap |= BIT(i + 1);
  669. i++;
  670. } else {
  671. *ssid_bitmap |= BIT(index + 1);
  672. }
  673. }
  674. }
  675. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  676. struct cfg80211_sched_scan_request *req,
  677. u8 *channels_buffer,
  678. enum ieee80211_band band,
  679. int *head,
  680. u32 ssid_bitmap,
  681. struct iwl_mvm_scan_params *params)
  682. {
  683. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  684. __le32 *type = (__le32 *)channels_buffer;
  685. __le16 *channel_number = (__le16 *)(type + n_channels);
  686. __le16 *iter_count = channel_number + n_channels;
  687. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  688. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  689. u8 *passive_dwell = active_dwell + n_channels;
  690. int i, index = 0;
  691. for (i = 0; i < req->n_channels; i++) {
  692. struct ieee80211_channel *chan = req->channels[i];
  693. if (chan->band != band)
  694. continue;
  695. index = *head;
  696. (*head)++;
  697. channel_number[index] = cpu_to_le16(chan->hw_value);
  698. active_dwell[index] = params->dwell[band].active;
  699. passive_dwell[index] = params->dwell[band].passive;
  700. iter_count[index] = cpu_to_le16(1);
  701. iter_interval[index] = 0;
  702. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  703. type[index] |=
  704. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  705. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  706. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  707. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  708. type[index] |=
  709. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  710. /* scan for all SSIDs from req->ssids */
  711. type[index] |= cpu_to_le32(ssid_bitmap);
  712. }
  713. }
  714. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  715. struct ieee80211_vif *vif,
  716. struct cfg80211_sched_scan_request *req,
  717. struct ieee80211_scan_ies *ies)
  718. {
  719. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  720. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  721. int head = 0;
  722. u32 ssid_bitmap;
  723. int cmd_len;
  724. int ret;
  725. u8 *probes;
  726. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  727. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  728. struct iwl_scan_offload_cfg *scan_cfg;
  729. struct iwl_host_cmd cmd = {
  730. .id = SCAN_OFFLOAD_CONFIG_CMD,
  731. };
  732. struct iwl_mvm_scan_params params = {};
  733. lockdep_assert_held(&mvm->mutex);
  734. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  735. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  736. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  737. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  738. if (!scan_cfg)
  739. return -ENOMEM;
  740. probes = scan_cfg->data +
  741. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  742. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  743. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  744. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  745. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  746. &ssid_bitmap, basic_ssid);
  747. /* build tx frames for supported bands */
  748. if (band_2ghz) {
  749. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  750. IEEE80211_BAND_2GHZ,
  751. &scan_cfg->scan_cmd.tx_cmd[0],
  752. probes);
  753. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  754. IEEE80211_BAND_2GHZ, &head,
  755. ssid_bitmap, &params);
  756. }
  757. if (band_5ghz) {
  758. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  759. IEEE80211_BAND_5GHZ,
  760. &scan_cfg->scan_cmd.tx_cmd[1],
  761. probes +
  762. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  763. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  764. IEEE80211_BAND_5GHZ, &head,
  765. ssid_bitmap, &params);
  766. }
  767. cmd.data[0] = scan_cfg;
  768. cmd.len[0] = cmd_len;
  769. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  770. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  771. ret = iwl_mvm_send_cmd(mvm, &cmd);
  772. kfree(scan_cfg);
  773. return ret;
  774. }
  775. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  776. struct cfg80211_sched_scan_request *req)
  777. {
  778. struct iwl_scan_offload_profile *profile;
  779. struct iwl_scan_offload_profile_cfg *profile_cfg;
  780. struct iwl_scan_offload_blacklist *blacklist;
  781. struct iwl_host_cmd cmd = {
  782. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  783. .len[1] = sizeof(*profile_cfg),
  784. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  785. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  786. };
  787. int blacklist_len;
  788. int i;
  789. int ret;
  790. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  791. return -EIO;
  792. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  793. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  794. else
  795. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  796. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  797. if (!blacklist)
  798. return -ENOMEM;
  799. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  800. if (!profile_cfg) {
  801. ret = -ENOMEM;
  802. goto free_blacklist;
  803. }
  804. cmd.data[0] = blacklist;
  805. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  806. cmd.data[1] = profile_cfg;
  807. /* No blacklist configuration */
  808. profile_cfg->num_profiles = req->n_match_sets;
  809. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  810. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  811. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  812. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  813. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  814. for (i = 0; i < req->n_match_sets; i++) {
  815. profile = &profile_cfg->profiles[i];
  816. profile->ssid_index = i;
  817. /* Support any cipher and auth algorithm */
  818. profile->unicast_cipher = 0xff;
  819. profile->auth_alg = 0xff;
  820. profile->network_type = IWL_NETWORK_TYPE_ANY;
  821. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  822. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  823. }
  824. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  825. ret = iwl_mvm_send_cmd(mvm, &cmd);
  826. kfree(profile_cfg);
  827. free_blacklist:
  828. kfree(blacklist);
  829. return ret;
  830. }
  831. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  832. struct cfg80211_sched_scan_request *req)
  833. {
  834. struct iwl_scan_offload_req scan_req = {
  835. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  836. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  837. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  838. .schedule_line[0].full_scan_mul = 1,
  839. .schedule_line[1].iterations = 0xff,
  840. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  841. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  842. };
  843. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  844. IWL_DEBUG_SCAN(mvm,
  845. "Sending scheduled scan with filtering, filter len %d\n",
  846. req->n_match_sets);
  847. } else {
  848. IWL_DEBUG_SCAN(mvm,
  849. "Sending Scheduled scan without filtering\n");
  850. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  851. }
  852. if (mvm->last_ebs_successful &&
  853. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  854. scan_req.flags |=
  855. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  856. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  857. sizeof(scan_req), &scan_req);
  858. }
  859. int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
  860. struct ieee80211_vif *vif,
  861. struct cfg80211_sched_scan_request *req,
  862. struct ieee80211_scan_ies *ies)
  863. {
  864. int ret;
  865. if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  866. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  867. if (ret)
  868. return ret;
  869. ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
  870. } else {
  871. ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
  872. if (ret)
  873. return ret;
  874. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  875. if (ret)
  876. return ret;
  877. ret = iwl_mvm_sched_scan_start(mvm, req);
  878. }
  879. return ret;
  880. }
  881. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  882. {
  883. int ret;
  884. struct iwl_host_cmd cmd = {
  885. .id = SCAN_OFFLOAD_ABORT_CMD,
  886. };
  887. u32 status;
  888. /* Exit instantly with error when device is not ready
  889. * to receive scan abort command or it does not perform
  890. * scheduled scan currently */
  891. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  892. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  893. mvm->scan_status != IWL_MVM_SCAN_OS))
  894. return -EIO;
  895. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  896. if (ret)
  897. return ret;
  898. if (status != CAN_ABORT_STATUS) {
  899. /*
  900. * The scan abort will return 1 for success or
  901. * 2 for "failure". A failure condition can be
  902. * due to simply not being in an active scan which
  903. * can occur if we send the scan abort before the
  904. * microcode has notified us that a scan is completed.
  905. */
  906. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  907. ret = -ENOENT;
  908. }
  909. return ret;
  910. }
  911. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  912. {
  913. int ret;
  914. struct iwl_notification_wait wait_scan_done;
  915. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  916. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  917. lockdep_assert_held(&mvm->mutex);
  918. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  919. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  920. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  921. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  922. return 0;
  923. }
  924. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  925. scan_done_notif,
  926. ARRAY_SIZE(scan_done_notif),
  927. NULL, NULL);
  928. ret = iwl_mvm_send_scan_offload_abort(mvm);
  929. if (ret) {
  930. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  931. sched ? "offloaded " : "", ret);
  932. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  933. return ret;
  934. }
  935. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  936. sched ? "offloaded " : "");
  937. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  938. if (ret)
  939. return ret;
  940. /*
  941. * Clear the scan status so the next scan requests will succeed. This
  942. * also ensures the Rx handler doesn't do anything, as the scan was
  943. * stopped from above. Since the rx handler won't do anything now,
  944. * we have to release the scan reference here.
  945. */
  946. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  947. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  948. mvm->scan_status = IWL_MVM_SCAN_NONE;
  949. if (notify) {
  950. if (sched)
  951. ieee80211_sched_scan_stopped(mvm->hw);
  952. else
  953. ieee80211_scan_completed(mvm->hw, true);
  954. }
  955. return 0;
  956. }
  957. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  958. struct iwl_scan_req_tx_cmd *tx_cmd,
  959. bool no_cck)
  960. {
  961. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  962. TX_CMD_FLG_BT_DIS);
  963. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  964. IEEE80211_BAND_2GHZ,
  965. no_cck);
  966. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  967. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  968. TX_CMD_FLG_BT_DIS);
  969. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  970. IEEE80211_BAND_5GHZ,
  971. no_cck);
  972. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  973. }
  974. static void
  975. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  976. struct ieee80211_channel **channels,
  977. int n_channels, u32 ssid_bitmap,
  978. struct iwl_scan_req_unified_lmac *cmd)
  979. {
  980. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  981. int i;
  982. for (i = 0; i < n_channels; i++) {
  983. channel_cfg[i].channel_num =
  984. cpu_to_le16(channels[i]->hw_value);
  985. channel_cfg[i].iter_count = cpu_to_le16(1);
  986. channel_cfg[i].iter_interval = 0;
  987. channel_cfg[i].flags =
  988. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  989. ssid_bitmap);
  990. }
  991. }
  992. static void
  993. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  994. struct ieee80211_scan_ies *ies,
  995. struct iwl_scan_req_unified_lmac *cmd)
  996. {
  997. struct iwl_scan_probe_req *preq = (void *)(cmd->data +
  998. sizeof(struct iwl_scan_channel_cfg_lmac) *
  999. mvm->fw->ucode_capa.n_scan_channels);
  1000. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  1001. u8 *pos;
  1002. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1003. eth_broadcast_addr(frame->da);
  1004. memcpy(frame->sa, vif->addr, ETH_ALEN);
  1005. eth_broadcast_addr(frame->bssid);
  1006. frame->seq_ctrl = 0;
  1007. pos = frame->u.probe_req.variable;
  1008. *pos++ = WLAN_EID_SSID;
  1009. *pos++ = 0;
  1010. preq->mac_header.offset = 0;
  1011. preq->mac_header.len = cpu_to_le16(24 + 2);
  1012. memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
  1013. ies->len[IEEE80211_BAND_2GHZ]);
  1014. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  1015. preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
  1016. pos += ies->len[IEEE80211_BAND_2GHZ];
  1017. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  1018. ies->len[IEEE80211_BAND_5GHZ]);
  1019. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  1020. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  1021. pos += ies->len[IEEE80211_BAND_5GHZ];
  1022. memcpy(pos, ies->common_ies, ies->common_ie_len);
  1023. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  1024. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  1025. }
  1026. static void
  1027. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  1028. struct iwl_scan_req_unified_lmac *cmd,
  1029. struct iwl_mvm_scan_params *params)
  1030. {
  1031. memset(cmd, 0, ksize(cmd));
  1032. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1033. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1034. if (params->passive_fragmented)
  1035. cmd->fragmented_dwell =
  1036. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1037. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  1038. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1039. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1040. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1041. cmd->iter_num = cpu_to_le32(1);
  1042. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1043. mvm->last_ebs_successful) {
  1044. cmd->channel_opt[0].flags =
  1045. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1046. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1047. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1048. cmd->channel_opt[1].flags =
  1049. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1050. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1051. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1052. }
  1053. if (iwl_mvm_rrm_scan_needed(mvm))
  1054. cmd->scan_flags |=
  1055. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  1056. }
  1057. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  1058. struct ieee80211_vif *vif,
  1059. struct ieee80211_scan_request *req)
  1060. {
  1061. struct iwl_host_cmd hcmd = {
  1062. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1063. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1064. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1065. mvm->fw->ucode_capa.n_scan_channels +
  1066. sizeof(struct iwl_scan_probe_req), },
  1067. .data = { mvm->scan_cmd, },
  1068. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1069. };
  1070. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1071. struct iwl_mvm_scan_params params = {};
  1072. u32 flags;
  1073. int ssid_bitmap = 0;
  1074. int ret, i;
  1075. lockdep_assert_held(&mvm->mutex);
  1076. /* we should have failed registration if scan_cmd was NULL */
  1077. if (WARN_ON(mvm->scan_cmd == NULL))
  1078. return -ENOMEM;
  1079. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  1080. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  1081. req->ies.len[NL80211_BAND_5GHZ] >
  1082. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  1083. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1084. return -ENOBUFS;
  1085. mvm->scan_status = IWL_MVM_SCAN_OS;
  1086. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1087. &params);
  1088. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1089. cmd->n_channels = (u8)req->req.n_channels;
  1090. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1091. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1092. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1093. if (params.passive_fragmented)
  1094. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1095. if (req->req.n_ssids == 0)
  1096. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1097. cmd->scan_flags |= cpu_to_le32(flags);
  1098. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1099. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1100. MAC_FILTER_IN_BEACON);
  1101. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1102. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1103. req->req.n_ssids, 0);
  1104. cmd->schedule[0].delay = 0;
  1105. cmd->schedule[0].iterations = 1;
  1106. cmd->schedule[0].full_scan_mul = 0;
  1107. cmd->schedule[1].delay = 0;
  1108. cmd->schedule[1].iterations = 0;
  1109. cmd->schedule[1].full_scan_mul = 0;
  1110. for (i = 1; i <= req->req.n_ssids; i++)
  1111. ssid_bitmap |= BIT(i);
  1112. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1113. req->req.n_channels, ssid_bitmap,
  1114. cmd);
  1115. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
  1116. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1117. if (!ret) {
  1118. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1119. } else {
  1120. /*
  1121. * If the scan failed, it usually means that the FW was unable
  1122. * to allocate the time events. Warn on it, but maybe we
  1123. * should try to send the command again with different params.
  1124. */
  1125. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1126. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1127. ret = -EIO;
  1128. }
  1129. return ret;
  1130. }
  1131. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1132. struct ieee80211_vif *vif,
  1133. struct cfg80211_sched_scan_request *req,
  1134. struct ieee80211_scan_ies *ies)
  1135. {
  1136. struct iwl_host_cmd hcmd = {
  1137. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1138. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1139. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1140. mvm->fw->ucode_capa.n_scan_channels +
  1141. sizeof(struct iwl_scan_probe_req), },
  1142. .data = { mvm->scan_cmd, },
  1143. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1144. };
  1145. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1146. struct iwl_mvm_scan_params params = {};
  1147. int ret;
  1148. u32 flags = 0, ssid_bitmap = 0;
  1149. lockdep_assert_held(&mvm->mutex);
  1150. /* we should have failed registration if scan_cmd was NULL */
  1151. if (WARN_ON(mvm->scan_cmd == NULL))
  1152. return -ENOMEM;
  1153. if (req->n_ssids > PROBE_OPTION_MAX ||
  1154. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1155. ies->len[NL80211_BAND_5GHZ] >
  1156. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  1157. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1158. return -ENOBUFS;
  1159. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1160. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1161. cmd->n_channels = (u8)req->n_channels;
  1162. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  1163. IWL_DEBUG_SCAN(mvm,
  1164. "Sending scheduled scan with filtering, n_match_sets %d\n",
  1165. req->n_match_sets);
  1166. } else {
  1167. IWL_DEBUG_SCAN(mvm,
  1168. "Sending Scheduled scan without filtering\n");
  1169. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1170. }
  1171. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1172. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1173. if (params.passive_fragmented)
  1174. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1175. if (req->n_ssids == 0)
  1176. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1177. cmd->scan_flags |= cpu_to_le32(flags);
  1178. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1179. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1180. MAC_FILTER_IN_BEACON);
  1181. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1182. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1183. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1184. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1185. cmd->schedule[0].full_scan_mul = 1;
  1186. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1187. cmd->schedule[1].iterations = 0xff;
  1188. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1189. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1190. ssid_bitmap, cmd);
  1191. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
  1192. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1193. if (!ret) {
  1194. IWL_DEBUG_SCAN(mvm,
  1195. "Sched scan request was sent successfully\n");
  1196. } else {
  1197. /*
  1198. * If the scan failed, it usually means that the FW was unable
  1199. * to allocate the time events. Warn on it, but maybe we
  1200. * should try to send the command again with different params.
  1201. */
  1202. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1203. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1204. ret = -EIO;
  1205. }
  1206. return ret;
  1207. }
  1208. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  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. }