scan.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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. #define IWL_DENSE_EBS_SCAN_RATIO 5
  73. #define IWL_SPARSE_EBS_SCAN_RATIO 1
  74. struct iwl_mvm_scan_params {
  75. u32 max_out_time;
  76. u32 suspend_time;
  77. bool passive_fragmented;
  78. struct _dwell {
  79. u16 passive;
  80. u16 active;
  81. u16 fragmented;
  82. } dwell[IEEE80211_NUM_BANDS];
  83. };
  84. enum iwl_umac_scan_uid_type {
  85. IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
  86. IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
  87. IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
  88. IWL_UMAC_SCAN_UID_SCHED_SCAN,
  89. };
  90. static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
  91. enum iwl_umac_scan_uid_type type, bool notify);
  92. static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
  93. {
  94. if (mvm->scan_rx_ant != ANT_NONE)
  95. return mvm->scan_rx_ant;
  96. return iwl_mvm_get_valid_rx_ant(mvm);
  97. }
  98. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  99. {
  100. u16 rx_chain;
  101. u8 rx_ant;
  102. rx_ant = iwl_mvm_scan_rx_ant(mvm);
  103. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  104. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  105. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  106. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  107. return cpu_to_le16(rx_chain);
  108. }
  109. static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
  110. {
  111. if (band == IEEE80211_BAND_2GHZ)
  112. return cpu_to_le32(PHY_BAND_24);
  113. else
  114. return cpu_to_le32(PHY_BAND_5);
  115. }
  116. static inline __le32
  117. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  118. bool no_cck)
  119. {
  120. u32 tx_ant;
  121. mvm->scan_last_antenna_idx =
  122. iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
  123. mvm->scan_last_antenna_idx);
  124. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  125. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  126. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  127. tx_ant);
  128. else
  129. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  130. }
  131. /*
  132. * We insert the SSIDs in an inverted order, because the FW will
  133. * invert it back. The most prioritized SSID, which is first in the
  134. * request list, is not copied here, but inserted directly to the probe
  135. * request.
  136. */
  137. static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
  138. struct cfg80211_ssid *ssids,
  139. int n_ssids, int first)
  140. {
  141. int fw_idx, req_idx;
  142. for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
  143. req_idx--, fw_idx++) {
  144. cmd_ssid[fw_idx].id = WLAN_EID_SSID;
  145. cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
  146. memcpy(cmd_ssid[fw_idx].ssid,
  147. ssids[req_idx].ssid,
  148. ssids[req_idx].ssid_len);
  149. }
  150. }
  151. /*
  152. * If req->n_ssids > 0, it means we should do an active scan.
  153. * In case of active scan w/o directed scan, we receive a zero-length SSID
  154. * just to notify that this scan is active and not passive.
  155. * In order to notify the FW of the number of SSIDs we wish to scan (including
  156. * the zero-length one), we need to set the corresponding bits in chan->type,
  157. * one for each SSID, and set the active bit (first). If the first SSID is
  158. * already included in the probe template, so we need to set only
  159. * req->n_ssids - 1 bits in addition to the first bit.
  160. */
  161. static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
  162. enum ieee80211_band band, int n_ssids)
  163. {
  164. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
  165. return 10;
  166. if (band == IEEE80211_BAND_2GHZ)
  167. return 20 + 3 * (n_ssids + 1);
  168. return 10 + 2 * (n_ssids + 1);
  169. }
  170. static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
  171. enum ieee80211_band band)
  172. {
  173. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
  174. return 110;
  175. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  176. }
  177. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  178. struct ieee80211_vif *vif)
  179. {
  180. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  181. int *global_cnt = data;
  182. if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
  183. mvmvif->phy_ctxt->id < MAX_PHYS)
  184. *global_cnt += 1;
  185. }
  186. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  187. struct ieee80211_vif *vif,
  188. int n_ssids, u32 flags,
  189. struct iwl_mvm_scan_params *params)
  190. {
  191. int global_cnt = 0;
  192. enum ieee80211_band band;
  193. u8 frag_passive_dwell = 0;
  194. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  195. IEEE80211_IFACE_ITER_NORMAL,
  196. iwl_mvm_scan_condition_iterator,
  197. &global_cnt);
  198. if (!global_cnt)
  199. goto not_bound;
  200. params->suspend_time = 30;
  201. params->max_out_time = 120;
  202. if (iwl_mvm_low_latency(mvm)) {
  203. if (mvm->fw->ucode_capa.api[0] &
  204. IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
  205. params->suspend_time = 105;
  206. /*
  207. * If there is more than one active interface make
  208. * passive scan more fragmented.
  209. */
  210. frag_passive_dwell = 40;
  211. params->max_out_time = frag_passive_dwell;
  212. } else {
  213. params->suspend_time = 120;
  214. params->max_out_time = 120;
  215. }
  216. }
  217. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  218. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  219. /*
  220. * P2P device scan should not be fragmented to avoid negative
  221. * impact on P2P device discovery. Configure max_out_time to be
  222. * equal to dwell time on passive channel. Take a longest
  223. * possible value, one that corresponds to 2GHz band
  224. */
  225. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  226. u32 passive_dwell =
  227. iwl_mvm_get_passive_dwell(mvm,
  228. IEEE80211_BAND_2GHZ);
  229. params->max_out_time = passive_dwell;
  230. } else {
  231. params->passive_fragmented = true;
  232. }
  233. }
  234. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  235. params->max_out_time = 200;
  236. not_bound:
  237. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  238. if (params->passive_fragmented)
  239. params->dwell[band].fragmented = frag_passive_dwell;
  240. params->dwell[band].passive = iwl_mvm_get_passive_dwell(mvm,
  241. band);
  242. params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
  243. n_ssids);
  244. }
  245. }
  246. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  247. {
  248. /* require rrm scan whenever the fw supports it */
  249. return mvm->fw->ucode_capa.capa[0] &
  250. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
  251. }
  252. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
  253. bool is_sched_scan)
  254. {
  255. int max_probe_len;
  256. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  257. /* we create the 802.11 header and SSID element */
  258. max_probe_len -= 24 + 2;
  259. /* DS parameter set element is added on 2.4GHZ band if required */
  260. if (iwl_mvm_rrm_scan_needed(mvm))
  261. max_probe_len -= 3;
  262. return max_probe_len;
  263. }
  264. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
  265. {
  266. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
  267. /* TODO: [BUG] This function should return the maximum allowed size of
  268. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  269. * in the same command. So the correct implementation of this function
  270. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  271. * command has only 512 bytes and it would leave us with about 240
  272. * bytes for scan IEs, which is clearly not enough. So meanwhile
  273. * we will report an incorrect value. This may result in a failure to
  274. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  275. * functions with -ENOBUFS, if a large enough probe will be provided.
  276. */
  277. return max_ie_len;
  278. }
  279. int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
  280. struct iwl_rx_cmd_buffer *rxb,
  281. struct iwl_device_cmd *cmd)
  282. {
  283. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  284. struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
  285. IWL_DEBUG_SCAN(mvm,
  286. "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
  287. notif->status, notif->scanned_channels);
  288. return 0;
  289. }
  290. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  291. struct iwl_rx_cmd_buffer *rxb,
  292. struct iwl_device_cmd *cmd)
  293. {
  294. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  295. ieee80211_sched_scan_results(mvm->hw);
  296. return 0;
  297. }
  298. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  299. struct iwl_rx_cmd_buffer *rxb,
  300. struct iwl_device_cmd *cmd)
  301. {
  302. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  303. struct iwl_periodic_scan_complete *scan_notif;
  304. scan_notif = (void *)pkt->data;
  305. /* scan status must be locked for proper checking */
  306. lockdep_assert_held(&mvm->mutex);
  307. IWL_DEBUG_SCAN(mvm,
  308. "%s completed, status %s, EBS status %s\n",
  309. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  310. "Scheduled scan" : "Scan",
  311. scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  312. "completed" : "aborted",
  313. scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
  314. "success" : "failed");
  315. /* only call mac80211 completion if the stop was initiated by FW */
  316. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  317. mvm->scan_status = IWL_MVM_SCAN_NONE;
  318. ieee80211_sched_scan_stopped(mvm->hw);
  319. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  320. mvm->scan_status = IWL_MVM_SCAN_NONE;
  321. ieee80211_scan_completed(mvm->hw,
  322. scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
  323. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  324. }
  325. if (scan_notif->ebs_status)
  326. mvm->last_ebs_successful = false;
  327. return 0;
  328. }
  329. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  330. {
  331. int i;
  332. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  333. if (!ssid_list[i].len)
  334. break;
  335. if (ssid_list[i].len == ssid_len &&
  336. !memcmp(ssid_list->ssid, ssid, ssid_len))
  337. return i;
  338. }
  339. return -1;
  340. }
  341. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  342. struct iwl_ssid_ie *direct_scan,
  343. u32 *ssid_bitmap, bool basic_ssid)
  344. {
  345. int i, j;
  346. int index;
  347. /*
  348. * copy SSIDs from match list.
  349. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  350. * config match list.
  351. */
  352. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  353. /* skip empty SSID matchsets */
  354. if (!req->match_sets[i].ssid.ssid_len)
  355. continue;
  356. direct_scan[i].id = WLAN_EID_SSID;
  357. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  358. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  359. direct_scan[i].len);
  360. }
  361. /* add SSIDs from scan SSID list */
  362. *ssid_bitmap = 0;
  363. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  364. index = iwl_ssid_exist(req->ssids[j].ssid,
  365. req->ssids[j].ssid_len,
  366. direct_scan);
  367. if (index < 0) {
  368. if (!req->ssids[j].ssid_len && basic_ssid)
  369. continue;
  370. direct_scan[i].id = WLAN_EID_SSID;
  371. direct_scan[i].len = req->ssids[j].ssid_len;
  372. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  373. direct_scan[i].len);
  374. *ssid_bitmap |= BIT(i + 1);
  375. i++;
  376. } else {
  377. *ssid_bitmap |= BIT(index + 1);
  378. }
  379. }
  380. }
  381. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  382. struct cfg80211_sched_scan_request *req)
  383. {
  384. struct iwl_scan_offload_profile *profile;
  385. struct iwl_scan_offload_profile_cfg *profile_cfg;
  386. struct iwl_scan_offload_blacklist *blacklist;
  387. struct iwl_host_cmd cmd = {
  388. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  389. .len[1] = sizeof(*profile_cfg),
  390. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  391. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  392. };
  393. int blacklist_len;
  394. int i;
  395. int ret;
  396. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  397. return -EIO;
  398. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  399. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  400. else
  401. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  402. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  403. if (!blacklist)
  404. return -ENOMEM;
  405. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  406. if (!profile_cfg) {
  407. ret = -ENOMEM;
  408. goto free_blacklist;
  409. }
  410. cmd.data[0] = blacklist;
  411. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  412. cmd.data[1] = profile_cfg;
  413. /* No blacklist configuration */
  414. profile_cfg->num_profiles = req->n_match_sets;
  415. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  416. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  417. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  418. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  419. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  420. for (i = 0; i < req->n_match_sets; i++) {
  421. profile = &profile_cfg->profiles[i];
  422. profile->ssid_index = i;
  423. /* Support any cipher and auth algorithm */
  424. profile->unicast_cipher = 0xff;
  425. profile->auth_alg = 0xff;
  426. profile->network_type = IWL_NETWORK_TYPE_ANY;
  427. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  428. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  429. }
  430. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  431. ret = iwl_mvm_send_cmd(mvm, &cmd);
  432. kfree(profile_cfg);
  433. free_blacklist:
  434. kfree(blacklist);
  435. return ret;
  436. }
  437. static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
  438. struct cfg80211_sched_scan_request *req)
  439. {
  440. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  441. IWL_DEBUG_SCAN(mvm,
  442. "Sending scheduled scan with filtering, n_match_sets %d\n",
  443. req->n_match_sets);
  444. return false;
  445. }
  446. IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
  447. return true;
  448. }
  449. int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
  450. struct ieee80211_vif *vif,
  451. struct cfg80211_sched_scan_request *req,
  452. struct ieee80211_scan_ies *ies)
  453. {
  454. int ret;
  455. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
  456. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  457. if (ret)
  458. return ret;
  459. ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
  460. } else {
  461. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  462. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  463. if (ret)
  464. return ret;
  465. ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
  466. }
  467. return ret;
  468. }
  469. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  470. {
  471. int ret;
  472. struct iwl_host_cmd cmd = {
  473. .id = SCAN_OFFLOAD_ABORT_CMD,
  474. };
  475. u32 status;
  476. /* Exit instantly with error when device is not ready
  477. * to receive scan abort command or it does not perform
  478. * scheduled scan currently */
  479. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  480. return -EIO;
  481. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  482. if (ret)
  483. return ret;
  484. if (status != CAN_ABORT_STATUS) {
  485. /*
  486. * The scan abort will return 1 for success or
  487. * 2 for "failure". A failure condition can be
  488. * due to simply not being in an active scan which
  489. * can occur if we send the scan abort before the
  490. * microcode has notified us that a scan is completed.
  491. */
  492. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  493. ret = -ENOENT;
  494. }
  495. return ret;
  496. }
  497. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  498. {
  499. int ret;
  500. struct iwl_notification_wait wait_scan_done;
  501. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  502. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  503. lockdep_assert_held(&mvm->mutex);
  504. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  505. return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
  506. notify);
  507. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  508. return 0;
  509. if (iwl_mvm_is_radio_killed(mvm)) {
  510. ret = 0;
  511. goto out;
  512. }
  513. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  514. scan_done_notif,
  515. ARRAY_SIZE(scan_done_notif),
  516. NULL, NULL);
  517. ret = iwl_mvm_send_scan_offload_abort(mvm);
  518. if (ret) {
  519. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  520. sched ? "offloaded " : "", ret);
  521. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  522. goto out;
  523. }
  524. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  525. sched ? "offloaded " : "");
  526. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  527. out:
  528. /*
  529. * Clear the scan status so the next scan requests will succeed. This
  530. * also ensures the Rx handler doesn't do anything, as the scan was
  531. * stopped from above. Since the rx handler won't do anything now,
  532. * we have to release the scan reference here.
  533. */
  534. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  535. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  536. mvm->scan_status = IWL_MVM_SCAN_NONE;
  537. if (notify) {
  538. if (sched)
  539. ieee80211_sched_scan_stopped(mvm->hw);
  540. else
  541. ieee80211_scan_completed(mvm->hw, true);
  542. }
  543. return ret;
  544. }
  545. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  546. struct iwl_scan_req_tx_cmd *tx_cmd,
  547. bool no_cck)
  548. {
  549. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  550. TX_CMD_FLG_BT_DIS);
  551. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  552. IEEE80211_BAND_2GHZ,
  553. no_cck);
  554. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  555. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  556. TX_CMD_FLG_BT_DIS);
  557. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  558. IEEE80211_BAND_5GHZ,
  559. no_cck);
  560. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  561. }
  562. static void
  563. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  564. struct ieee80211_channel **channels,
  565. int n_channels, u32 ssid_bitmap,
  566. struct iwl_scan_req_unified_lmac *cmd)
  567. {
  568. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  569. int i;
  570. for (i = 0; i < n_channels; i++) {
  571. channel_cfg[i].channel_num =
  572. cpu_to_le16(channels[i]->hw_value);
  573. channel_cfg[i].iter_count = cpu_to_le16(1);
  574. channel_cfg[i].iter_interval = 0;
  575. channel_cfg[i].flags =
  576. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  577. ssid_bitmap);
  578. }
  579. }
  580. static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
  581. size_t len, u8 *const pos)
  582. {
  583. static const u8 before_ds_params[] = {
  584. WLAN_EID_SSID,
  585. WLAN_EID_SUPP_RATES,
  586. WLAN_EID_REQUEST,
  587. WLAN_EID_EXT_SUPP_RATES,
  588. };
  589. size_t offs;
  590. u8 *newpos = pos;
  591. if (!iwl_mvm_rrm_scan_needed(mvm)) {
  592. memcpy(newpos, ies, len);
  593. return newpos + len;
  594. }
  595. offs = ieee80211_ie_split(ies, len,
  596. before_ds_params,
  597. ARRAY_SIZE(before_ds_params),
  598. 0);
  599. memcpy(newpos, ies, offs);
  600. newpos += offs;
  601. /* Add a placeholder for DS Parameter Set element */
  602. *newpos++ = WLAN_EID_DS_PARAMS;
  603. *newpos++ = 1;
  604. *newpos++ = 0;
  605. memcpy(newpos, ies + offs, len - offs);
  606. newpos += len - offs;
  607. return newpos;
  608. }
  609. static void
  610. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  611. struct ieee80211_scan_ies *ies,
  612. struct iwl_scan_probe_req *preq,
  613. const u8 *mac_addr, const u8 *mac_addr_mask)
  614. {
  615. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  616. u8 *pos, *newpos;
  617. /*
  618. * Unfortunately, right now the offload scan doesn't support randomising
  619. * within the firmware, so until the firmware API is ready we implement
  620. * it in the driver. This means that the scan iterations won't really be
  621. * random, only when it's restarted, but at least that helps a bit.
  622. */
  623. if (mac_addr)
  624. get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
  625. else
  626. memcpy(frame->sa, vif->addr, ETH_ALEN);
  627. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  628. eth_broadcast_addr(frame->da);
  629. eth_broadcast_addr(frame->bssid);
  630. frame->seq_ctrl = 0;
  631. pos = frame->u.probe_req.variable;
  632. *pos++ = WLAN_EID_SSID;
  633. *pos++ = 0;
  634. preq->mac_header.offset = 0;
  635. preq->mac_header.len = cpu_to_le16(24 + 2);
  636. /* Insert ds parameter set element on 2.4 GHz band */
  637. newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
  638. ies->ies[IEEE80211_BAND_2GHZ],
  639. ies->len[IEEE80211_BAND_2GHZ],
  640. pos);
  641. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  642. preq->band_data[0].len = cpu_to_le16(newpos - pos);
  643. pos = newpos;
  644. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  645. ies->len[IEEE80211_BAND_5GHZ]);
  646. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  647. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  648. pos += ies->len[IEEE80211_BAND_5GHZ];
  649. memcpy(pos, ies->common_ies, ies->common_ie_len);
  650. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  651. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  652. }
  653. static void
  654. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  655. struct iwl_scan_req_unified_lmac *cmd,
  656. struct iwl_mvm_scan_params *params)
  657. {
  658. memset(cmd, 0, ksize(cmd));
  659. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  660. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  661. if (params->passive_fragmented)
  662. cmd->fragmented_dwell =
  663. params->dwell[IEEE80211_BAND_2GHZ].fragmented;
  664. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  665. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  666. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  667. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  668. cmd->iter_num = cpu_to_le32(1);
  669. if (iwl_mvm_rrm_scan_needed(mvm))
  670. cmd->scan_flags |=
  671. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  672. }
  673. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  674. struct ieee80211_vif *vif,
  675. struct ieee80211_scan_request *req)
  676. {
  677. struct iwl_host_cmd hcmd = {
  678. .id = SCAN_OFFLOAD_REQUEST_CMD,
  679. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  680. sizeof(struct iwl_scan_channel_cfg_lmac) *
  681. mvm->fw->ucode_capa.n_scan_channels +
  682. sizeof(struct iwl_scan_probe_req), },
  683. .data = { mvm->scan_cmd, },
  684. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  685. };
  686. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  687. struct iwl_scan_probe_req *preq;
  688. struct iwl_mvm_scan_params params = {};
  689. u32 flags;
  690. u32 ssid_bitmap = 0;
  691. int ret, i;
  692. lockdep_assert_held(&mvm->mutex);
  693. /* we should have failed registration if scan_cmd was NULL */
  694. if (WARN_ON(mvm->scan_cmd == NULL))
  695. return -ENOMEM;
  696. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  697. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  698. req->ies.len[NL80211_BAND_5GHZ] >
  699. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  700. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  701. return -ENOBUFS;
  702. mvm->scan_status = IWL_MVM_SCAN_OS;
  703. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  704. &params);
  705. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  706. cmd->n_channels = (u8)req->req.n_channels;
  707. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  708. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  709. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  710. if (params.passive_fragmented)
  711. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  712. if (req->req.n_ssids == 0)
  713. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  714. cmd->scan_flags |= cpu_to_le32(flags);
  715. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  716. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  717. MAC_FILTER_IN_BEACON);
  718. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  719. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  720. req->req.n_ssids, 0);
  721. cmd->schedule[0].delay = 0;
  722. cmd->schedule[0].iterations = 1;
  723. cmd->schedule[0].full_scan_mul = 0;
  724. cmd->schedule[1].delay = 0;
  725. cmd->schedule[1].iterations = 0;
  726. cmd->schedule[1].full_scan_mul = 0;
  727. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
  728. mvm->last_ebs_successful) {
  729. cmd->channel_opt[0].flags =
  730. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  731. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  732. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  733. cmd->channel_opt[0].non_ebs_ratio =
  734. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  735. cmd->channel_opt[1].flags =
  736. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  737. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  738. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  739. cmd->channel_opt[1].non_ebs_ratio =
  740. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  741. }
  742. for (i = 1; i <= req->req.n_ssids; i++)
  743. ssid_bitmap |= BIT(i);
  744. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  745. req->req.n_channels, ssid_bitmap,
  746. cmd);
  747. preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  748. mvm->fw->ucode_capa.n_scan_channels);
  749. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
  750. req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  751. req->req.mac_addr : NULL,
  752. req->req.mac_addr_mask);
  753. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  754. if (!ret) {
  755. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  756. } else {
  757. /*
  758. * If the scan failed, it usually means that the FW was unable
  759. * to allocate the time events. Warn on it, but maybe we
  760. * should try to send the command again with different params.
  761. */
  762. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  763. mvm->scan_status = IWL_MVM_SCAN_NONE;
  764. ret = -EIO;
  765. }
  766. return ret;
  767. }
  768. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  769. struct ieee80211_vif *vif,
  770. struct cfg80211_sched_scan_request *req,
  771. struct ieee80211_scan_ies *ies)
  772. {
  773. struct iwl_host_cmd hcmd = {
  774. .id = SCAN_OFFLOAD_REQUEST_CMD,
  775. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  776. sizeof(struct iwl_scan_channel_cfg_lmac) *
  777. mvm->fw->ucode_capa.n_scan_channels +
  778. sizeof(struct iwl_scan_probe_req), },
  779. .data = { mvm->scan_cmd, },
  780. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  781. };
  782. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  783. struct iwl_scan_probe_req *preq;
  784. struct iwl_mvm_scan_params params = {};
  785. int ret;
  786. u32 flags = 0, ssid_bitmap = 0;
  787. lockdep_assert_held(&mvm->mutex);
  788. /* we should have failed registration if scan_cmd was NULL */
  789. if (WARN_ON(mvm->scan_cmd == NULL))
  790. return -ENOMEM;
  791. if (req->n_ssids > PROBE_OPTION_MAX ||
  792. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  793. ies->len[NL80211_BAND_5GHZ] >
  794. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  795. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  796. return -ENOBUFS;
  797. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  798. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  799. cmd->n_channels = (u8)req->n_channels;
  800. cmd->delay = cpu_to_le32(req->delay);
  801. if (iwl_mvm_scan_pass_all(mvm, req))
  802. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  803. else
  804. flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
  805. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  806. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  807. if (params.passive_fragmented)
  808. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  809. if (req->n_ssids == 0)
  810. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  811. #ifdef CONFIG_IWLWIFI_DEBUGFS
  812. if (mvm->scan_iter_notif_enabled)
  813. flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
  814. #endif
  815. cmd->scan_flags |= cpu_to_le32(flags);
  816. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  817. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  818. MAC_FILTER_IN_BEACON);
  819. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  820. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  821. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  822. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  823. cmd->schedule[0].full_scan_mul = 1;
  824. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  825. cmd->schedule[1].iterations = 0xff;
  826. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  827. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  828. mvm->last_ebs_successful) {
  829. cmd->channel_opt[0].flags =
  830. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  831. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  832. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  833. cmd->channel_opt[0].non_ebs_ratio =
  834. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  835. cmd->channel_opt[1].flags =
  836. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  837. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  838. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  839. cmd->channel_opt[1].non_ebs_ratio =
  840. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  841. }
  842. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  843. ssid_bitmap, cmd);
  844. preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  845. mvm->fw->ucode_capa.n_scan_channels);
  846. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
  847. req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  848. req->mac_addr : NULL,
  849. req->mac_addr_mask);
  850. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  851. if (!ret) {
  852. IWL_DEBUG_SCAN(mvm,
  853. "Sched scan request was sent successfully\n");
  854. } else {
  855. /*
  856. * If the scan failed, it usually means that the FW was unable
  857. * to allocate the time events. Warn on it, but maybe we
  858. * should try to send the command again with different params.
  859. */
  860. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  861. mvm->scan_status = IWL_MVM_SCAN_NONE;
  862. ret = -EIO;
  863. }
  864. return ret;
  865. }
  866. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  867. {
  868. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  869. return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
  870. true);
  871. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  872. return 0;
  873. if (iwl_mvm_is_radio_killed(mvm)) {
  874. ieee80211_scan_completed(mvm->hw, true);
  875. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  876. mvm->scan_status = IWL_MVM_SCAN_NONE;
  877. return 0;
  878. }
  879. return iwl_mvm_scan_offload_stop(mvm, true);
  880. }
  881. /* UMAC scan API */
  882. struct iwl_umac_scan_done {
  883. struct iwl_mvm *mvm;
  884. enum iwl_umac_scan_uid_type type;
  885. };
  886. static int rate_to_scan_rate_flag(unsigned int rate)
  887. {
  888. static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
  889. [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
  890. [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
  891. [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
  892. [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
  893. [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
  894. [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
  895. [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
  896. [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
  897. [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
  898. [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
  899. [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
  900. [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
  901. };
  902. return rate_to_scan_rate[rate];
  903. }
  904. static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
  905. {
  906. struct ieee80211_supported_band *band;
  907. unsigned int rates = 0;
  908. int i;
  909. band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  910. for (i = 0; i < band->n_bitrates; i++)
  911. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  912. band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  913. for (i = 0; i < band->n_bitrates; i++)
  914. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  915. /* Set both basic rates and supported rates */
  916. rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
  917. return cpu_to_le32(rates);
  918. }
  919. int iwl_mvm_config_scan(struct iwl_mvm *mvm)
  920. {
  921. struct iwl_scan_config *scan_config;
  922. struct ieee80211_supported_band *band;
  923. int num_channels =
  924. mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
  925. mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  926. int ret, i, j = 0, cmd_size, data_size;
  927. struct iwl_host_cmd cmd = {
  928. .id = SCAN_CFG_CMD,
  929. };
  930. if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
  931. return -ENOBUFS;
  932. cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
  933. scan_config = kzalloc(cmd_size, GFP_KERNEL);
  934. if (!scan_config)
  935. return -ENOMEM;
  936. data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
  937. scan_config->hdr.size = cpu_to_le16(data_size);
  938. scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
  939. SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
  940. SCAN_CONFIG_FLAG_SET_TX_CHAINS |
  941. SCAN_CONFIG_FLAG_SET_RX_CHAINS |
  942. SCAN_CONFIG_FLAG_SET_ALL_TIMES |
  943. SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
  944. SCAN_CONFIG_FLAG_SET_MAC_ADDR |
  945. SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
  946. SCAN_CONFIG_N_CHANNELS(num_channels));
  947. scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  948. scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
  949. scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
  950. scan_config->out_of_channel_time = cpu_to_le32(170);
  951. scan_config->suspend_time = cpu_to_le32(30);
  952. scan_config->dwell_active = 20;
  953. scan_config->dwell_passive = 110;
  954. scan_config->dwell_fragmented = 20;
  955. memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
  956. scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
  957. scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
  958. IWL_CHANNEL_FLAG_ACCURATE_EBS |
  959. IWL_CHANNEL_FLAG_EBS_ADD |
  960. IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
  961. band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  962. for (i = 0; i < band->n_channels; i++, j++)
  963. scan_config->channel_array[j] = band->channels[i].hw_value;
  964. band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  965. for (i = 0; i < band->n_channels; i++, j++)
  966. scan_config->channel_array[j] = band->channels[i].hw_value;
  967. cmd.data[0] = scan_config;
  968. cmd.len[0] = cmd_size;
  969. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  970. IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
  971. ret = iwl_mvm_send_cmd(mvm, &cmd);
  972. kfree(scan_config);
  973. return ret;
  974. }
  975. static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
  976. {
  977. int i;
  978. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  979. if (mvm->scan_uid[i] == uid)
  980. return i;
  981. return i;
  982. }
  983. static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
  984. {
  985. return iwl_mvm_find_scan_uid(mvm, 0);
  986. }
  987. static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
  988. enum iwl_umac_scan_uid_type type)
  989. {
  990. int i;
  991. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  992. if (mvm->scan_uid[i] & type)
  993. return true;
  994. return false;
  995. }
  996. static int iwl_mvm_find_first_scan(struct iwl_mvm *mvm,
  997. enum iwl_umac_scan_uid_type type)
  998. {
  999. int i;
  1000. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  1001. if (mvm->scan_uid[i] & type)
  1002. return i;
  1003. return i;
  1004. }
  1005. static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
  1006. enum iwl_umac_scan_uid_type type)
  1007. {
  1008. u32 uid;
  1009. /* make sure exactly one bit is on in scan type */
  1010. WARN_ON(hweight8(type) != 1);
  1011. /*
  1012. * Make sure scan uids are unique. If one scan lasts long time while
  1013. * others are completing frequently, the seq number will wrap up and
  1014. * we may have more than one scan with the same uid.
  1015. */
  1016. do {
  1017. uid = type | (mvm->scan_seq_num <<
  1018. IWL_UMAC_SCAN_UID_SEQ_OFFSET);
  1019. mvm->scan_seq_num++;
  1020. } while (iwl_mvm_find_scan_uid(mvm, uid) <
  1021. IWL_MVM_MAX_SIMULTANEOUS_SCANS);
  1022. IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
  1023. return uid;
  1024. }
  1025. static void
  1026. iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
  1027. struct iwl_scan_req_umac *cmd,
  1028. struct iwl_mvm_scan_params *params)
  1029. {
  1030. memset(cmd, 0, ksize(cmd));
  1031. cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
  1032. sizeof(struct iwl_mvm_umac_cmd_hdr));
  1033. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1034. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1035. if (params->passive_fragmented)
  1036. cmd->fragmented_dwell =
  1037. params->dwell[IEEE80211_BAND_2GHZ].fragmented;
  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_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1041. }
  1042. static void
  1043. iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
  1044. struct ieee80211_channel **channels,
  1045. int n_channels, u32 ssid_bitmap,
  1046. struct iwl_scan_req_umac *cmd)
  1047. {
  1048. struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
  1049. int i;
  1050. for (i = 0; i < n_channels; i++) {
  1051. channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
  1052. channel_cfg[i].channel_num = channels[i]->hw_value;
  1053. channel_cfg[i].iter_count = 1;
  1054. channel_cfg[i].iter_interval = 0;
  1055. }
  1056. }
  1057. static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
  1058. struct cfg80211_ssid *ssids,
  1059. int fragmented)
  1060. {
  1061. int flags = 0;
  1062. if (n_ssids == 0)
  1063. flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
  1064. if (n_ssids == 1 && ssids[0].ssid_len != 0)
  1065. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
  1066. if (fragmented)
  1067. flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
  1068. if (iwl_mvm_rrm_scan_needed(mvm))
  1069. flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
  1070. return flags;
  1071. }
  1072. int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1073. struct ieee80211_scan_request *req)
  1074. {
  1075. struct iwl_host_cmd hcmd = {
  1076. .id = SCAN_REQ_UMAC,
  1077. .len = { iwl_mvm_scan_size(mvm), },
  1078. .data = { mvm->scan_cmd, },
  1079. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1080. };
  1081. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1082. struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
  1083. sizeof(struct iwl_scan_channel_cfg_umac) *
  1084. mvm->fw->ucode_capa.n_scan_channels;
  1085. struct iwl_mvm_scan_params params = {};
  1086. u32 uid, flags;
  1087. u32 ssid_bitmap = 0;
  1088. int ret, i, uid_idx;
  1089. lockdep_assert_held(&mvm->mutex);
  1090. uid_idx = iwl_mvm_find_free_scan_uid(mvm);
  1091. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1092. return -EBUSY;
  1093. /* we should have failed registration if scan_cmd was NULL */
  1094. if (WARN_ON(mvm->scan_cmd == NULL))
  1095. return -ENOMEM;
  1096. if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
  1097. req->ies.common_ie_len +
  1098. req->ies.len[NL80211_BAND_2GHZ] +
  1099. req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
  1100. SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
  1101. mvm->fw->ucode_capa.n_scan_channels))
  1102. return -ENOBUFS;
  1103. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1104. &params);
  1105. iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
  1106. uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
  1107. mvm->scan_uid[uid_idx] = uid;
  1108. cmd->uid = cpu_to_le32(uid);
  1109. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1110. flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
  1111. req->req.ssids,
  1112. params.passive_fragmented);
  1113. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1114. cmd->general_flags = cpu_to_le32(flags);
  1115. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
  1116. mvm->last_ebs_successful)
  1117. cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
  1118. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1119. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
  1120. cmd->n_channels = req->req.n_channels;
  1121. for (i = 0; i < req->req.n_ssids; i++)
  1122. ssid_bitmap |= BIT(i);
  1123. iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
  1124. req->req.n_channels, ssid_bitmap, cmd);
  1125. sec_part->schedule[0].iter_count = 1;
  1126. sec_part->delay = 0;
  1127. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
  1128. req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1129. req->req.mac_addr : NULL,
  1130. req->req.mac_addr_mask);
  1131. iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
  1132. req->req.n_ssids, 0);
  1133. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1134. if (!ret) {
  1135. IWL_DEBUG_SCAN(mvm,
  1136. "Scan request was sent successfully\n");
  1137. } else {
  1138. /*
  1139. * If the scan failed, it usually means that the FW was unable
  1140. * to allocate the time events. Warn on it, but maybe we
  1141. * should try to send the command again with different params.
  1142. */
  1143. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1144. }
  1145. return ret;
  1146. }
  1147. int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1148. struct cfg80211_sched_scan_request *req,
  1149. struct ieee80211_scan_ies *ies)
  1150. {
  1151. struct iwl_host_cmd hcmd = {
  1152. .id = SCAN_REQ_UMAC,
  1153. .len = { iwl_mvm_scan_size(mvm), },
  1154. .data = { mvm->scan_cmd, },
  1155. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1156. };
  1157. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1158. struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
  1159. sizeof(struct iwl_scan_channel_cfg_umac) *
  1160. mvm->fw->ucode_capa.n_scan_channels;
  1161. struct iwl_mvm_scan_params params = {};
  1162. u32 uid, flags;
  1163. u32 ssid_bitmap = 0;
  1164. int ret, uid_idx;
  1165. lockdep_assert_held(&mvm->mutex);
  1166. uid_idx = iwl_mvm_find_free_scan_uid(mvm);
  1167. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1168. return -EBUSY;
  1169. /* we should have failed registration if scan_cmd was NULL */
  1170. if (WARN_ON(mvm->scan_cmd == NULL))
  1171. return -ENOMEM;
  1172. if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
  1173. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1174. ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
  1175. SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
  1176. mvm->fw->ucode_capa.n_scan_channels))
  1177. return -ENOBUFS;
  1178. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
  1179. &params);
  1180. iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
  1181. cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
  1182. uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1183. mvm->scan_uid[uid_idx] = uid;
  1184. cmd->uid = cpu_to_le32(uid);
  1185. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
  1186. flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
  1187. params.passive_fragmented);
  1188. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
  1189. if (iwl_mvm_scan_pass_all(mvm, req))
  1190. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1191. else
  1192. flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
  1193. cmd->general_flags = cpu_to_le32(flags);
  1194. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1195. mvm->last_ebs_successful)
  1196. cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
  1197. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1198. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
  1199. cmd->n_channels = req->n_channels;
  1200. iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
  1201. false);
  1202. /* This API uses bits 0-19 instead of 1-20. */
  1203. ssid_bitmap = ssid_bitmap >> 1;
  1204. iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1205. ssid_bitmap, cmd);
  1206. sec_part->schedule[0].interval =
  1207. cpu_to_le16(req->interval / MSEC_PER_SEC);
  1208. sec_part->schedule[0].iter_count = 0xff;
  1209. if (req->delay > U16_MAX) {
  1210. IWL_DEBUG_SCAN(mvm,
  1211. "delay value is > 16-bits, set to max possible\n");
  1212. sec_part->delay = cpu_to_le16(U16_MAX);
  1213. } else {
  1214. sec_part->delay = cpu_to_le16(req->delay);
  1215. }
  1216. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
  1217. req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1218. req->mac_addr : NULL,
  1219. req->mac_addr_mask);
  1220. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1221. if (!ret) {
  1222. IWL_DEBUG_SCAN(mvm,
  1223. "Sched scan request was sent successfully\n");
  1224. } else {
  1225. /*
  1226. * If the scan failed, it usually means that the FW was unable
  1227. * to allocate the time events. Warn on it, but maybe we
  1228. * should try to send the command again with different params.
  1229. */
  1230. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1231. }
  1232. return ret;
  1233. }
  1234. int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
  1235. struct iwl_rx_cmd_buffer *rxb,
  1236. struct iwl_device_cmd *cmd)
  1237. {
  1238. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1239. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1240. u32 uid = __le32_to_cpu(notif->uid);
  1241. bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1242. int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
  1243. /*
  1244. * Scan uid may be set to zero in case of scan abort request from above.
  1245. */
  1246. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1247. return 0;
  1248. IWL_DEBUG_SCAN(mvm,
  1249. "Scan completed, uid %u type %s, status %s, EBS status %s\n",
  1250. uid, sched ? "sched" : "regular",
  1251. notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  1252. "completed" : "aborted",
  1253. notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
  1254. "success" : "failed");
  1255. if (notif->ebs_status)
  1256. mvm->last_ebs_successful = false;
  1257. mvm->scan_uid[uid_idx] = 0;
  1258. if (!sched) {
  1259. ieee80211_scan_completed(mvm->hw,
  1260. notif->status ==
  1261. IWL_SCAN_OFFLOAD_ABORTED);
  1262. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1263. } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
  1264. ieee80211_sched_scan_stopped(mvm->hw);
  1265. } else {
  1266. IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
  1267. }
  1268. return 0;
  1269. }
  1270. static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
  1271. struct iwl_rx_packet *pkt, void *data)
  1272. {
  1273. struct iwl_umac_scan_done *scan_done = data;
  1274. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1275. u32 uid = __le32_to_cpu(notif->uid);
  1276. int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
  1277. if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
  1278. return false;
  1279. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1280. return false;
  1281. /*
  1282. * Clear scan uid of scans that was aborted from above and completed
  1283. * in FW so the RX handler does nothing. Set last_ebs_successful here if
  1284. * needed.
  1285. */
  1286. scan_done->mvm->scan_uid[uid_idx] = 0;
  1287. if (notif->ebs_status)
  1288. scan_done->mvm->last_ebs_successful = false;
  1289. return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
  1290. }
  1291. static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
  1292. {
  1293. struct iwl_umac_scan_abort cmd = {
  1294. .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
  1295. sizeof(struct iwl_mvm_umac_cmd_hdr)),
  1296. .uid = cpu_to_le32(uid),
  1297. };
  1298. lockdep_assert_held(&mvm->mutex);
  1299. IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
  1300. return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
  1301. }
  1302. static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
  1303. enum iwl_umac_scan_uid_type type, bool notify)
  1304. {
  1305. struct iwl_notification_wait wait_scan_done;
  1306. static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
  1307. struct iwl_umac_scan_done scan_done = {
  1308. .mvm = mvm,
  1309. .type = type,
  1310. };
  1311. int i, ret = -EIO;
  1312. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  1313. scan_done_notif,
  1314. ARRAY_SIZE(scan_done_notif),
  1315. iwl_scan_umac_done_check, &scan_done);
  1316. IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
  1317. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
  1318. if (mvm->scan_uid[i] & type) {
  1319. int err;
  1320. if (iwl_mvm_is_radio_killed(mvm) &&
  1321. (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
  1322. ieee80211_scan_completed(mvm->hw, true);
  1323. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1324. break;
  1325. }
  1326. err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
  1327. if (!err)
  1328. ret = 0;
  1329. }
  1330. }
  1331. if (ret) {
  1332. IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
  1333. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  1334. return ret;
  1335. }
  1336. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  1337. if (ret)
  1338. return ret;
  1339. if (notify) {
  1340. if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
  1341. ieee80211_sched_scan_stopped(mvm->hw);
  1342. if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
  1343. ieee80211_scan_completed(mvm->hw, true);
  1344. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1345. }
  1346. }
  1347. return ret;
  1348. }
  1349. int iwl_mvm_scan_size(struct iwl_mvm *mvm)
  1350. {
  1351. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  1352. return sizeof(struct iwl_scan_req_umac) +
  1353. sizeof(struct iwl_scan_channel_cfg_umac) *
  1354. mvm->fw->ucode_capa.n_scan_channels +
  1355. sizeof(struct iwl_scan_req_umac_tail);
  1356. return sizeof(struct iwl_scan_req_unified_lmac) +
  1357. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1358. mvm->fw->ucode_capa.n_scan_channels +
  1359. sizeof(struct iwl_scan_probe_req);
  1360. }
  1361. /*
  1362. * This function is used in nic restart flow, to inform mac80211 about scans
  1363. * that was aborted by restart flow or by an assert.
  1364. */
  1365. void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
  1366. {
  1367. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
  1368. u32 uid, i;
  1369. uid = iwl_mvm_find_first_scan(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
  1370. if (uid < IWL_MVM_MAX_SIMULTANEOUS_SCANS) {
  1371. ieee80211_scan_completed(mvm->hw, true);
  1372. mvm->scan_uid[uid] = 0;
  1373. }
  1374. uid = iwl_mvm_find_first_scan(mvm,
  1375. IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1376. if (uid < IWL_MVM_MAX_SIMULTANEOUS_SCANS && !mvm->restart_fw) {
  1377. ieee80211_sched_scan_stopped(mvm->hw);
  1378. mvm->scan_uid[uid] = 0;
  1379. }
  1380. /* We shouldn't have any UIDs still set. Loop over all the
  1381. * UIDs to make sure there's nothing left there and warn if
  1382. * any is found.
  1383. */
  1384. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
  1385. if (WARN_ONCE(mvm->scan_uid[i],
  1386. "UMAC scan UID %d was not cleaned\n",
  1387. mvm->scan_uid[i]))
  1388. mvm->scan_uid[i] = 0;
  1389. }
  1390. } else {
  1391. switch (mvm->scan_status) {
  1392. case IWL_MVM_SCAN_NONE:
  1393. break;
  1394. case IWL_MVM_SCAN_OS:
  1395. ieee80211_scan_completed(mvm->hw, true);
  1396. break;
  1397. case IWL_MVM_SCAN_SCHED:
  1398. /*
  1399. * Sched scan will be restarted by mac80211 in
  1400. * restart_hw, so do not report if FW is about to be
  1401. * restarted.
  1402. */
  1403. if (!mvm->restart_fw)
  1404. ieee80211_sched_scan_stopped(mvm->hw);
  1405. break;
  1406. }
  1407. }
  1408. }