scan.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/etherdevice.h>
  68. #include <net/mac80211.h>
  69. #include "mvm.h"
  70. #include "fw/api/scan.h"
  71. #include "iwl-io.h"
  72. #define IWL_DENSE_EBS_SCAN_RATIO 5
  73. #define IWL_SPARSE_EBS_SCAN_RATIO 1
  74. enum iwl_mvm_traffic_load {
  75. IWL_MVM_TRAFFIC_LOW,
  76. IWL_MVM_TRAFFIC_MEDIUM,
  77. IWL_MVM_TRAFFIC_HIGH,
  78. };
  79. #define IWL_SCAN_DWELL_ACTIVE 10
  80. #define IWL_SCAN_DWELL_PASSIVE 110
  81. #define IWL_SCAN_DWELL_FRAGMENTED 44
  82. #define IWL_SCAN_DWELL_EXTENDED 90
  83. struct iwl_mvm_scan_timing_params {
  84. u32 suspend_time;
  85. u32 max_out_time;
  86. };
  87. static struct iwl_mvm_scan_timing_params scan_timing[] = {
  88. [IWL_SCAN_TYPE_UNASSOC] = {
  89. .suspend_time = 0,
  90. .max_out_time = 0,
  91. },
  92. [IWL_SCAN_TYPE_WILD] = {
  93. .suspend_time = 30,
  94. .max_out_time = 120,
  95. },
  96. [IWL_SCAN_TYPE_MILD] = {
  97. .suspend_time = 120,
  98. .max_out_time = 120,
  99. },
  100. [IWL_SCAN_TYPE_FRAGMENTED] = {
  101. .suspend_time = 95,
  102. .max_out_time = 44,
  103. },
  104. };
  105. struct iwl_mvm_scan_params {
  106. enum iwl_mvm_scan_type type;
  107. u32 n_channels;
  108. u16 delay;
  109. int n_ssids;
  110. struct cfg80211_ssid *ssids;
  111. struct ieee80211_channel **channels;
  112. u32 flags;
  113. u8 *mac_addr;
  114. u8 *mac_addr_mask;
  115. bool no_cck;
  116. bool pass_all;
  117. int n_match_sets;
  118. struct iwl_scan_probe_req preq;
  119. struct cfg80211_match_set *match_sets;
  120. int n_scan_plans;
  121. struct cfg80211_sched_scan_plan *scan_plans;
  122. u32 measurement_dwell;
  123. };
  124. static inline void *iwl_mvm_get_scan_req_umac_data(struct iwl_mvm *mvm)
  125. {
  126. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  127. if (iwl_mvm_is_adaptive_dwell_supported(mvm))
  128. return (void *)&cmd->v7.data;
  129. if (iwl_mvm_has_new_tx_api(mvm))
  130. return (void *)&cmd->v6.data;
  131. return (void *)&cmd->v1.data;
  132. }
  133. static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
  134. {
  135. if (mvm->scan_rx_ant != ANT_NONE)
  136. return mvm->scan_rx_ant;
  137. return iwl_mvm_get_valid_rx_ant(mvm);
  138. }
  139. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  140. {
  141. u16 rx_chain;
  142. u8 rx_ant;
  143. rx_ant = iwl_mvm_scan_rx_ant(mvm);
  144. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  145. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  146. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  147. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  148. return cpu_to_le16(rx_chain);
  149. }
  150. static __le32 iwl_mvm_scan_rxon_flags(enum nl80211_band band)
  151. {
  152. if (band == NL80211_BAND_2GHZ)
  153. return cpu_to_le32(PHY_BAND_24);
  154. else
  155. return cpu_to_le32(PHY_BAND_5);
  156. }
  157. static inline __le32
  158. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum nl80211_band band,
  159. bool no_cck)
  160. {
  161. u32 tx_ant;
  162. mvm->scan_last_antenna_idx =
  163. iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
  164. mvm->scan_last_antenna_idx);
  165. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  166. if (band == NL80211_BAND_2GHZ && !no_cck)
  167. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  168. tx_ant);
  169. else
  170. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  171. }
  172. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  173. struct ieee80211_vif *vif)
  174. {
  175. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  176. int *global_cnt = data;
  177. if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
  178. mvmvif->phy_ctxt->id < NUM_PHY_CTX)
  179. *global_cnt += 1;
  180. }
  181. static enum iwl_mvm_traffic_load iwl_mvm_get_traffic_load(struct iwl_mvm *mvm)
  182. {
  183. return IWL_MVM_TRAFFIC_LOW;
  184. }
  185. static enum
  186. iwl_mvm_scan_type iwl_mvm_get_scan_type(struct iwl_mvm *mvm, bool p2p_device)
  187. {
  188. int global_cnt = 0;
  189. enum iwl_mvm_traffic_load load;
  190. bool low_latency;
  191. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  192. IEEE80211_IFACE_ITER_NORMAL,
  193. iwl_mvm_scan_condition_iterator,
  194. &global_cnt);
  195. if (!global_cnt)
  196. return IWL_SCAN_TYPE_UNASSOC;
  197. load = iwl_mvm_get_traffic_load(mvm);
  198. low_latency = iwl_mvm_low_latency(mvm);
  199. if ((load == IWL_MVM_TRAFFIC_HIGH || low_latency) && !p2p_device &&
  200. fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_FRAGMENTED_SCAN))
  201. return IWL_SCAN_TYPE_FRAGMENTED;
  202. if (load >= IWL_MVM_TRAFFIC_MEDIUM || low_latency)
  203. return IWL_SCAN_TYPE_MILD;
  204. return IWL_SCAN_TYPE_WILD;
  205. }
  206. static int
  207. iwl_mvm_get_measurement_dwell(struct iwl_mvm *mvm,
  208. struct cfg80211_scan_request *req,
  209. struct iwl_mvm_scan_params *params)
  210. {
  211. if (!req->duration)
  212. return 0;
  213. if (req->duration_mandatory &&
  214. req->duration > scan_timing[params->type].max_out_time) {
  215. IWL_DEBUG_SCAN(mvm,
  216. "Measurement scan - too long dwell %hu (max out time %u)\n",
  217. req->duration,
  218. scan_timing[params->type].max_out_time);
  219. return -EOPNOTSUPP;
  220. }
  221. return min_t(u32, (u32)req->duration,
  222. scan_timing[params->type].max_out_time);
  223. }
  224. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  225. {
  226. /* require rrm scan whenever the fw supports it */
  227. return fw_has_capa(&mvm->fw->ucode_capa,
  228. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT);
  229. }
  230. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm)
  231. {
  232. int max_probe_len;
  233. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  234. /* we create the 802.11 header and SSID element */
  235. max_probe_len -= 24 + 2;
  236. /* DS parameter set element is added on 2.4GHZ band if required */
  237. if (iwl_mvm_rrm_scan_needed(mvm))
  238. max_probe_len -= 3;
  239. return max_probe_len;
  240. }
  241. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
  242. {
  243. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm);
  244. /* TODO: [BUG] This function should return the maximum allowed size of
  245. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  246. * in the same command. So the correct implementation of this function
  247. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  248. * command has only 512 bytes and it would leave us with about 240
  249. * bytes for scan IEs, which is clearly not enough. So meanwhile
  250. * we will report an incorrect value. This may result in a failure to
  251. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  252. * functions with -ENOBUFS, if a large enough probe will be provided.
  253. */
  254. return max_ie_len;
  255. }
  256. void iwl_mvm_rx_lmac_scan_iter_complete_notif(struct iwl_mvm *mvm,
  257. struct iwl_rx_cmd_buffer *rxb)
  258. {
  259. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  260. struct iwl_lmac_scan_complete_notif *notif = (void *)pkt->data;
  261. IWL_DEBUG_SCAN(mvm,
  262. "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
  263. notif->status, notif->scanned_channels);
  264. if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
  265. IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
  266. ieee80211_sched_scan_results(mvm->hw);
  267. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
  268. }
  269. }
  270. void iwl_mvm_rx_scan_match_found(struct iwl_mvm *mvm,
  271. struct iwl_rx_cmd_buffer *rxb)
  272. {
  273. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  274. ieee80211_sched_scan_results(mvm->hw);
  275. }
  276. static const char *iwl_mvm_ebs_status_str(enum iwl_scan_ebs_status status)
  277. {
  278. switch (status) {
  279. case IWL_SCAN_EBS_SUCCESS:
  280. return "successful";
  281. case IWL_SCAN_EBS_INACTIVE:
  282. return "inactive";
  283. case IWL_SCAN_EBS_FAILED:
  284. case IWL_SCAN_EBS_CHAN_NOT_FOUND:
  285. default:
  286. return "failed";
  287. }
  288. }
  289. void iwl_mvm_rx_lmac_scan_complete_notif(struct iwl_mvm *mvm,
  290. struct iwl_rx_cmd_buffer *rxb)
  291. {
  292. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  293. struct iwl_periodic_scan_complete *scan_notif = (void *)pkt->data;
  294. bool aborted = (scan_notif->status == IWL_SCAN_OFFLOAD_ABORTED);
  295. /* If this happens, the firmware has mistakenly sent an LMAC
  296. * notification during UMAC scans -- warn and ignore it.
  297. */
  298. if (WARN_ON_ONCE(fw_has_capa(&mvm->fw->ucode_capa,
  299. IWL_UCODE_TLV_CAPA_UMAC_SCAN)))
  300. return;
  301. /* scan status must be locked for proper checking */
  302. lockdep_assert_held(&mvm->mutex);
  303. /* We first check if we were stopping a scan, in which case we
  304. * just clear the stopping flag. Then we check if it was a
  305. * firmware initiated stop, in which case we need to inform
  306. * mac80211.
  307. * Note that we can have a stopping and a running scan
  308. * simultaneously, but we can't have two different types of
  309. * scans stopping or running at the same time (since LMAC
  310. * doesn't support it).
  311. */
  312. if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_SCHED) {
  313. WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR);
  314. IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
  315. aborted ? "aborted" : "completed",
  316. iwl_mvm_ebs_status_str(scan_notif->ebs_status));
  317. IWL_DEBUG_SCAN(mvm,
  318. "Last line %d, Last iteration %d, Time after last iteration %d\n",
  319. scan_notif->last_schedule_line,
  320. scan_notif->last_schedule_iteration,
  321. __le32_to_cpu(scan_notif->time_after_last_iter));
  322. mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_SCHED;
  323. } else if (mvm->scan_status & IWL_MVM_SCAN_STOPPING_REGULAR) {
  324. IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s\n",
  325. aborted ? "aborted" : "completed",
  326. iwl_mvm_ebs_status_str(scan_notif->ebs_status));
  327. mvm->scan_status &= ~IWL_MVM_SCAN_STOPPING_REGULAR;
  328. } else if (mvm->scan_status & IWL_MVM_SCAN_SCHED) {
  329. WARN_ON_ONCE(mvm->scan_status & IWL_MVM_SCAN_REGULAR);
  330. IWL_DEBUG_SCAN(mvm, "Scheduled scan %s, EBS status %s\n",
  331. aborted ? "aborted" : "completed",
  332. iwl_mvm_ebs_status_str(scan_notif->ebs_status));
  333. IWL_DEBUG_SCAN(mvm,
  334. "Last line %d, Last iteration %d, Time after last iteration %d (FW)\n",
  335. scan_notif->last_schedule_line,
  336. scan_notif->last_schedule_iteration,
  337. __le32_to_cpu(scan_notif->time_after_last_iter));
  338. mvm->scan_status &= ~IWL_MVM_SCAN_SCHED;
  339. ieee80211_sched_scan_stopped(mvm->hw);
  340. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
  341. } else if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
  342. struct cfg80211_scan_info info = {
  343. .aborted = aborted,
  344. };
  345. IWL_DEBUG_SCAN(mvm, "Regular scan %s, EBS status %s (FW)\n",
  346. aborted ? "aborted" : "completed",
  347. iwl_mvm_ebs_status_str(scan_notif->ebs_status));
  348. mvm->scan_status &= ~IWL_MVM_SCAN_REGULAR;
  349. ieee80211_scan_completed(mvm->hw, &info);
  350. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  351. cancel_delayed_work(&mvm->scan_timeout_dwork);
  352. } else {
  353. IWL_ERR(mvm,
  354. "got scan complete notification but no scan is running\n");
  355. }
  356. mvm->last_ebs_successful =
  357. scan_notif->ebs_status == IWL_SCAN_EBS_SUCCESS ||
  358. scan_notif->ebs_status == IWL_SCAN_EBS_INACTIVE;
  359. }
  360. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  361. {
  362. int i;
  363. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  364. if (!ssid_list[i].len)
  365. break;
  366. if (ssid_list[i].len == ssid_len &&
  367. !memcmp(ssid_list->ssid, ssid, ssid_len))
  368. return i;
  369. }
  370. return -1;
  371. }
  372. /* We insert the SSIDs in an inverted order, because the FW will
  373. * invert it back.
  374. */
  375. static void iwl_scan_build_ssids(struct iwl_mvm_scan_params *params,
  376. struct iwl_ssid_ie *ssids,
  377. u32 *ssid_bitmap)
  378. {
  379. int i, j;
  380. int index;
  381. /*
  382. * copy SSIDs from match list.
  383. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  384. * config match list.
  385. */
  386. for (i = 0, j = params->n_match_sets - 1;
  387. j >= 0 && i < PROBE_OPTION_MAX;
  388. i++, j--) {
  389. /* skip empty SSID matchsets */
  390. if (!params->match_sets[j].ssid.ssid_len)
  391. continue;
  392. ssids[i].id = WLAN_EID_SSID;
  393. ssids[i].len = params->match_sets[j].ssid.ssid_len;
  394. memcpy(ssids[i].ssid, params->match_sets[j].ssid.ssid,
  395. ssids[i].len);
  396. }
  397. /* add SSIDs from scan SSID list */
  398. *ssid_bitmap = 0;
  399. for (j = params->n_ssids - 1;
  400. j >= 0 && i < PROBE_OPTION_MAX;
  401. i++, j--) {
  402. index = iwl_ssid_exist(params->ssids[j].ssid,
  403. params->ssids[j].ssid_len,
  404. ssids);
  405. if (index < 0) {
  406. ssids[i].id = WLAN_EID_SSID;
  407. ssids[i].len = params->ssids[j].ssid_len;
  408. memcpy(ssids[i].ssid, params->ssids[j].ssid,
  409. ssids[i].len);
  410. *ssid_bitmap |= BIT(i);
  411. } else {
  412. *ssid_bitmap |= BIT(index);
  413. }
  414. }
  415. }
  416. static int
  417. iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  418. struct cfg80211_sched_scan_request *req)
  419. {
  420. struct iwl_scan_offload_profile *profile;
  421. struct iwl_scan_offload_profile_cfg *profile_cfg;
  422. struct iwl_scan_offload_blacklist *blacklist;
  423. struct iwl_host_cmd cmd = {
  424. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  425. .len[1] = sizeof(*profile_cfg),
  426. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  427. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  428. };
  429. int blacklist_len;
  430. int i;
  431. int ret;
  432. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  433. return -EIO;
  434. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  435. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  436. else
  437. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  438. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  439. if (!blacklist)
  440. return -ENOMEM;
  441. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  442. if (!profile_cfg) {
  443. ret = -ENOMEM;
  444. goto free_blacklist;
  445. }
  446. cmd.data[0] = blacklist;
  447. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  448. cmd.data[1] = profile_cfg;
  449. /* No blacklist configuration */
  450. profile_cfg->num_profiles = req->n_match_sets;
  451. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  452. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  453. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  454. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  455. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  456. for (i = 0; i < req->n_match_sets; i++) {
  457. profile = &profile_cfg->profiles[i];
  458. profile->ssid_index = i;
  459. /* Support any cipher and auth algorithm */
  460. profile->unicast_cipher = 0xff;
  461. profile->auth_alg = 0xff;
  462. profile->network_type = IWL_NETWORK_TYPE_ANY;
  463. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  464. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  465. }
  466. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  467. ret = iwl_mvm_send_cmd(mvm, &cmd);
  468. kfree(profile_cfg);
  469. free_blacklist:
  470. kfree(blacklist);
  471. return ret;
  472. }
  473. static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
  474. struct cfg80211_sched_scan_request *req)
  475. {
  476. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  477. IWL_DEBUG_SCAN(mvm,
  478. "Sending scheduled scan with filtering, n_match_sets %d\n",
  479. req->n_match_sets);
  480. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
  481. return false;
  482. }
  483. IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
  484. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
  485. return true;
  486. }
  487. static int iwl_mvm_lmac_scan_abort(struct iwl_mvm *mvm)
  488. {
  489. int ret;
  490. struct iwl_host_cmd cmd = {
  491. .id = SCAN_OFFLOAD_ABORT_CMD,
  492. };
  493. u32 status = CAN_ABORT_STATUS;
  494. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  495. if (ret)
  496. return ret;
  497. if (status != CAN_ABORT_STATUS) {
  498. /*
  499. * The scan abort will return 1 for success or
  500. * 2 for "failure". A failure condition can be
  501. * due to simply not being in an active scan which
  502. * can occur if we send the scan abort before the
  503. * microcode has notified us that a scan is completed.
  504. */
  505. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  506. ret = -ENOENT;
  507. }
  508. return ret;
  509. }
  510. static void iwl_mvm_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  511. struct iwl_scan_req_tx_cmd *tx_cmd,
  512. bool no_cck)
  513. {
  514. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  515. TX_CMD_FLG_BT_DIS);
  516. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  517. NL80211_BAND_2GHZ,
  518. no_cck);
  519. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  520. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  521. TX_CMD_FLG_BT_DIS);
  522. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  523. NL80211_BAND_5GHZ,
  524. no_cck);
  525. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  526. }
  527. static void
  528. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  529. struct ieee80211_channel **channels,
  530. int n_channels, u32 ssid_bitmap,
  531. struct iwl_scan_req_lmac *cmd)
  532. {
  533. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  534. int i;
  535. for (i = 0; i < n_channels; i++) {
  536. channel_cfg[i].channel_num =
  537. cpu_to_le16(channels[i]->hw_value);
  538. channel_cfg[i].iter_count = cpu_to_le16(1);
  539. channel_cfg[i].iter_interval = 0;
  540. channel_cfg[i].flags =
  541. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  542. ssid_bitmap);
  543. }
  544. }
  545. static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
  546. size_t len, u8 *const pos)
  547. {
  548. static const u8 before_ds_params[] = {
  549. WLAN_EID_SSID,
  550. WLAN_EID_SUPP_RATES,
  551. WLAN_EID_REQUEST,
  552. WLAN_EID_EXT_SUPP_RATES,
  553. };
  554. size_t offs;
  555. u8 *newpos = pos;
  556. if (!iwl_mvm_rrm_scan_needed(mvm)) {
  557. memcpy(newpos, ies, len);
  558. return newpos + len;
  559. }
  560. offs = ieee80211_ie_split(ies, len,
  561. before_ds_params,
  562. ARRAY_SIZE(before_ds_params),
  563. 0);
  564. memcpy(newpos, ies, offs);
  565. newpos += offs;
  566. /* Add a placeholder for DS Parameter Set element */
  567. *newpos++ = WLAN_EID_DS_PARAMS;
  568. *newpos++ = 1;
  569. *newpos++ = 0;
  570. memcpy(newpos, ies + offs, len - offs);
  571. newpos += len - offs;
  572. return newpos;
  573. }
  574. static void
  575. iwl_mvm_build_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  576. struct ieee80211_scan_ies *ies,
  577. struct iwl_mvm_scan_params *params)
  578. {
  579. struct ieee80211_mgmt *frame = (void *)params->preq.buf;
  580. u8 *pos, *newpos;
  581. const u8 *mac_addr = params->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  582. params->mac_addr : NULL;
  583. /*
  584. * Unfortunately, right now the offload scan doesn't support randomising
  585. * within the firmware, so until the firmware API is ready we implement
  586. * it in the driver. This means that the scan iterations won't really be
  587. * random, only when it's restarted, but at least that helps a bit.
  588. */
  589. if (mac_addr)
  590. get_random_mask_addr(frame->sa, mac_addr,
  591. params->mac_addr_mask);
  592. else
  593. memcpy(frame->sa, vif->addr, ETH_ALEN);
  594. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  595. eth_broadcast_addr(frame->da);
  596. eth_broadcast_addr(frame->bssid);
  597. frame->seq_ctrl = 0;
  598. pos = frame->u.probe_req.variable;
  599. *pos++ = WLAN_EID_SSID;
  600. *pos++ = 0;
  601. params->preq.mac_header.offset = 0;
  602. params->preq.mac_header.len = cpu_to_le16(24 + 2);
  603. /* Insert ds parameter set element on 2.4 GHz band */
  604. newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
  605. ies->ies[NL80211_BAND_2GHZ],
  606. ies->len[NL80211_BAND_2GHZ],
  607. pos);
  608. params->preq.band_data[0].offset = cpu_to_le16(pos - params->preq.buf);
  609. params->preq.band_data[0].len = cpu_to_le16(newpos - pos);
  610. pos = newpos;
  611. memcpy(pos, ies->ies[NL80211_BAND_5GHZ],
  612. ies->len[NL80211_BAND_5GHZ]);
  613. params->preq.band_data[1].offset = cpu_to_le16(pos - params->preq.buf);
  614. params->preq.band_data[1].len =
  615. cpu_to_le16(ies->len[NL80211_BAND_5GHZ]);
  616. pos += ies->len[NL80211_BAND_5GHZ];
  617. memcpy(pos, ies->common_ies, ies->common_ie_len);
  618. params->preq.common_data.offset = cpu_to_le16(pos - params->preq.buf);
  619. params->preq.common_data.len = cpu_to_le16(ies->common_ie_len);
  620. }
  621. static void iwl_mvm_scan_lmac_dwell(struct iwl_mvm *mvm,
  622. struct iwl_scan_req_lmac *cmd,
  623. struct iwl_mvm_scan_params *params)
  624. {
  625. cmd->active_dwell = IWL_SCAN_DWELL_ACTIVE;
  626. cmd->passive_dwell = IWL_SCAN_DWELL_PASSIVE;
  627. cmd->fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
  628. cmd->extended_dwell = IWL_SCAN_DWELL_EXTENDED;
  629. cmd->max_out_time = cpu_to_le32(scan_timing[params->type].max_out_time);
  630. cmd->suspend_time = cpu_to_le32(scan_timing[params->type].suspend_time);
  631. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
  632. }
  633. static inline bool iwl_mvm_scan_fits(struct iwl_mvm *mvm, int n_ssids,
  634. struct ieee80211_scan_ies *ies,
  635. int n_channels)
  636. {
  637. return ((n_ssids <= PROBE_OPTION_MAX) &&
  638. (n_channels <= mvm->fw->ucode_capa.n_scan_channels) &
  639. (ies->common_ie_len +
  640. ies->len[NL80211_BAND_2GHZ] +
  641. ies->len[NL80211_BAND_5GHZ] <=
  642. iwl_mvm_max_scan_ie_fw_cmd_room(mvm)));
  643. }
  644. static inline bool iwl_mvm_scan_use_ebs(struct iwl_mvm *mvm,
  645. struct ieee80211_vif *vif)
  646. {
  647. const struct iwl_ucode_capabilities *capa = &mvm->fw->ucode_capa;
  648. /* We can only use EBS if:
  649. * 1. the feature is supported;
  650. * 2. the last EBS was successful;
  651. * 3. if only single scan, the single scan EBS API is supported;
  652. * 4. it's not a p2p find operation.
  653. */
  654. return ((capa->flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
  655. mvm->last_ebs_successful && IWL_MVM_ENABLE_EBS &&
  656. vif->type != NL80211_IFTYPE_P2P_DEVICE);
  657. }
  658. static inline bool iwl_mvm_is_regular_scan(struct iwl_mvm_scan_params *params)
  659. {
  660. return params->n_scan_plans == 1 &&
  661. params->scan_plans[0].iterations == 1;
  662. }
  663. static int iwl_mvm_scan_lmac_flags(struct iwl_mvm *mvm,
  664. struct iwl_mvm_scan_params *params,
  665. struct ieee80211_vif *vif)
  666. {
  667. int flags = 0;
  668. if (params->n_ssids == 0)
  669. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  670. if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
  671. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  672. if (params->type == IWL_SCAN_TYPE_FRAGMENTED)
  673. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  674. if (iwl_mvm_rrm_scan_needed(mvm))
  675. flags |= IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED;
  676. if (params->pass_all)
  677. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  678. else
  679. flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
  680. #ifdef CONFIG_IWLWIFI_DEBUGFS
  681. if (mvm->scan_iter_notif_enabled)
  682. flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
  683. #endif
  684. if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
  685. flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
  686. if (iwl_mvm_is_regular_scan(params) &&
  687. vif->type != NL80211_IFTYPE_P2P_DEVICE &&
  688. params->type != IWL_SCAN_TYPE_FRAGMENTED)
  689. flags |= IWL_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL;
  690. return flags;
  691. }
  692. static int iwl_mvm_scan_lmac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  693. struct iwl_mvm_scan_params *params)
  694. {
  695. struct iwl_scan_req_lmac *cmd = mvm->scan_cmd;
  696. struct iwl_scan_probe_req *preq =
  697. (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  698. mvm->fw->ucode_capa.n_scan_channels);
  699. u32 ssid_bitmap = 0;
  700. int i;
  701. lockdep_assert_held(&mvm->mutex);
  702. memset(cmd, 0, ksize(cmd));
  703. if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
  704. return -EINVAL;
  705. iwl_mvm_scan_lmac_dwell(mvm, cmd, params);
  706. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  707. cmd->iter_num = cpu_to_le32(1);
  708. cmd->n_channels = (u8)params->n_channels;
  709. cmd->delay = cpu_to_le32(params->delay);
  710. cmd->scan_flags = cpu_to_le32(iwl_mvm_scan_lmac_flags(mvm, params,
  711. vif));
  712. cmd->flags = iwl_mvm_scan_rxon_flags(params->channels[0]->band);
  713. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  714. MAC_FILTER_IN_BEACON);
  715. iwl_mvm_scan_fill_tx_cmd(mvm, cmd->tx_cmd, params->no_cck);
  716. iwl_scan_build_ssids(params, cmd->direct_scan, &ssid_bitmap);
  717. /* this API uses bits 1-20 instead of 0-19 */
  718. ssid_bitmap <<= 1;
  719. for (i = 0; i < params->n_scan_plans; i++) {
  720. struct cfg80211_sched_scan_plan *scan_plan =
  721. &params->scan_plans[i];
  722. cmd->schedule[i].delay =
  723. cpu_to_le16(scan_plan->interval);
  724. cmd->schedule[i].iterations = scan_plan->iterations;
  725. cmd->schedule[i].full_scan_mul = 1;
  726. }
  727. /*
  728. * If the number of iterations of the last scan plan is set to
  729. * zero, it should run infinitely. However, this is not always the case.
  730. * For example, when regular scan is requested the driver sets one scan
  731. * plan with one iteration.
  732. */
  733. if (!cmd->schedule[i - 1].iterations)
  734. cmd->schedule[i - 1].iterations = 0xff;
  735. if (iwl_mvm_scan_use_ebs(mvm, vif)) {
  736. cmd->channel_opt[0].flags =
  737. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  738. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  739. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  740. cmd->channel_opt[0].non_ebs_ratio =
  741. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  742. cmd->channel_opt[1].flags =
  743. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  744. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  745. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  746. cmd->channel_opt[1].non_ebs_ratio =
  747. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  748. }
  749. iwl_mvm_lmac_scan_cfg_channels(mvm, params->channels,
  750. params->n_channels, ssid_bitmap, cmd);
  751. *preq = params->preq;
  752. return 0;
  753. }
  754. static int rate_to_scan_rate_flag(unsigned int rate)
  755. {
  756. static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
  757. [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
  758. [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
  759. [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
  760. [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
  761. [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
  762. [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
  763. [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
  764. [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
  765. [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
  766. [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
  767. [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
  768. [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
  769. };
  770. return rate_to_scan_rate[rate];
  771. }
  772. static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
  773. {
  774. struct ieee80211_supported_band *band;
  775. unsigned int rates = 0;
  776. int i;
  777. band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
  778. for (i = 0; i < band->n_bitrates; i++)
  779. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  780. band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
  781. for (i = 0; i < band->n_bitrates; i++)
  782. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  783. /* Set both basic rates and supported rates */
  784. rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
  785. return cpu_to_le32(rates);
  786. }
  787. static void iwl_mvm_fill_scan_dwell(struct iwl_mvm *mvm,
  788. struct iwl_scan_dwell *dwell)
  789. {
  790. dwell->active = IWL_SCAN_DWELL_ACTIVE;
  791. dwell->passive = IWL_SCAN_DWELL_PASSIVE;
  792. dwell->fragmented = IWL_SCAN_DWELL_FRAGMENTED;
  793. dwell->extended = IWL_SCAN_DWELL_EXTENDED;
  794. }
  795. static void iwl_mvm_fill_channels(struct iwl_mvm *mvm, u8 *channels)
  796. {
  797. struct ieee80211_supported_band *band;
  798. int i, j = 0;
  799. band = &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
  800. for (i = 0; i < band->n_channels; i++, j++)
  801. channels[j] = band->channels[i].hw_value;
  802. band = &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
  803. for (i = 0; i < band->n_channels; i++, j++)
  804. channels[j] = band->channels[i].hw_value;
  805. }
  806. static void iwl_mvm_fill_scan_config_v1(struct iwl_mvm *mvm, void *config,
  807. u32 flags, u8 channel_flags)
  808. {
  809. enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
  810. struct iwl_scan_config_v1 *cfg = config;
  811. cfg->flags = cpu_to_le32(flags);
  812. cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  813. cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
  814. cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
  815. cfg->out_of_channel_time = cpu_to_le32(scan_timing[type].max_out_time);
  816. cfg->suspend_time = cpu_to_le32(scan_timing[type].suspend_time);
  817. iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
  818. memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
  819. cfg->bcast_sta_id = mvm->aux_sta.sta_id;
  820. cfg->channel_flags = channel_flags;
  821. iwl_mvm_fill_channels(mvm, cfg->channel_array);
  822. }
  823. static void iwl_mvm_fill_scan_config(struct iwl_mvm *mvm, void *config,
  824. u32 flags, u8 channel_flags)
  825. {
  826. enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
  827. struct iwl_scan_config *cfg = config;
  828. cfg->flags = cpu_to_le32(flags);
  829. cfg->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  830. cfg->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
  831. cfg->legacy_rates = iwl_mvm_scan_config_rates(mvm);
  832. cfg->out_of_channel_time[0] =
  833. cpu_to_le32(scan_timing[type].max_out_time);
  834. cfg->suspend_time[0] = cpu_to_le32(scan_timing[type].suspend_time);
  835. if (iwl_mvm_is_cdb_supported(mvm)) {
  836. cfg->suspend_time[1] =
  837. cpu_to_le32(scan_timing[type].suspend_time);
  838. cfg->out_of_channel_time[1] =
  839. cpu_to_le32(scan_timing[type].max_out_time);
  840. }
  841. iwl_mvm_fill_scan_dwell(mvm, &cfg->dwell);
  842. memcpy(&cfg->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
  843. cfg->bcast_sta_id = mvm->aux_sta.sta_id;
  844. cfg->channel_flags = channel_flags;
  845. iwl_mvm_fill_channels(mvm, cfg->channel_array);
  846. }
  847. int iwl_mvm_config_scan(struct iwl_mvm *mvm)
  848. {
  849. void *cfg;
  850. int ret, cmd_size;
  851. struct iwl_host_cmd cmd = {
  852. .id = iwl_cmd_id(SCAN_CFG_CMD, IWL_ALWAYS_LONG_GROUP, 0),
  853. };
  854. enum iwl_mvm_scan_type type = iwl_mvm_get_scan_type(mvm, false);
  855. int num_channels =
  856. mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels +
  857. mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels;
  858. u32 flags;
  859. u8 channel_flags;
  860. if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
  861. return -ENOBUFS;
  862. if (type == mvm->scan_type)
  863. return 0;
  864. if (iwl_mvm_has_new_tx_api(mvm))
  865. cmd_size = sizeof(struct iwl_scan_config);
  866. else
  867. cmd_size = sizeof(struct iwl_scan_config_v1);
  868. cmd_size += mvm->fw->ucode_capa.n_scan_channels;
  869. cfg = kzalloc(cmd_size, GFP_KERNEL);
  870. if (!cfg)
  871. return -ENOMEM;
  872. flags = SCAN_CONFIG_FLAG_ACTIVATE |
  873. SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
  874. SCAN_CONFIG_FLAG_SET_TX_CHAINS |
  875. SCAN_CONFIG_FLAG_SET_RX_CHAINS |
  876. SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
  877. SCAN_CONFIG_FLAG_SET_ALL_TIMES |
  878. SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
  879. SCAN_CONFIG_FLAG_SET_MAC_ADDR |
  880. SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS |
  881. SCAN_CONFIG_N_CHANNELS(num_channels) |
  882. (type == IWL_SCAN_TYPE_FRAGMENTED ?
  883. SCAN_CONFIG_FLAG_SET_FRAGMENTED :
  884. SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
  885. channel_flags = IWL_CHANNEL_FLAG_EBS |
  886. IWL_CHANNEL_FLAG_ACCURATE_EBS |
  887. IWL_CHANNEL_FLAG_EBS_ADD |
  888. IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
  889. if (iwl_mvm_has_new_tx_api(mvm)) {
  890. flags |= (type == IWL_SCAN_TYPE_FRAGMENTED) ?
  891. SCAN_CONFIG_FLAG_SET_LMAC2_FRAGMENTED :
  892. SCAN_CONFIG_FLAG_CLEAR_LMAC2_FRAGMENTED;
  893. iwl_mvm_fill_scan_config(mvm, cfg, flags, channel_flags);
  894. } else {
  895. iwl_mvm_fill_scan_config_v1(mvm, cfg, flags, channel_flags);
  896. }
  897. cmd.data[0] = cfg;
  898. cmd.len[0] = cmd_size;
  899. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  900. IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
  901. ret = iwl_mvm_send_cmd(mvm, &cmd);
  902. if (!ret)
  903. mvm->scan_type = type;
  904. kfree(cfg);
  905. return ret;
  906. }
  907. static int iwl_mvm_scan_uid_by_status(struct iwl_mvm *mvm, int status)
  908. {
  909. int i;
  910. for (i = 0; i < mvm->max_scans; i++)
  911. if (mvm->scan_uid_status[i] == status)
  912. return i;
  913. return -ENOENT;
  914. }
  915. static void iwl_mvm_scan_umac_dwell(struct iwl_mvm *mvm,
  916. struct iwl_scan_req_umac *cmd,
  917. struct iwl_mvm_scan_params *params)
  918. {
  919. struct iwl_mvm_scan_timing_params *timing = &scan_timing[params->type];
  920. if (iwl_mvm_is_regular_scan(params))
  921. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
  922. else
  923. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_2);
  924. if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
  925. if (params->measurement_dwell) {
  926. cmd->v7.active_dwell = params->measurement_dwell;
  927. cmd->v7.passive_dwell = params->measurement_dwell;
  928. } else {
  929. cmd->v7.active_dwell = IWL_SCAN_DWELL_ACTIVE;
  930. cmd->v7.passive_dwell = IWL_SCAN_DWELL_PASSIVE;
  931. }
  932. cmd->v7.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
  933. cmd->v7.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
  934. cmd->v7.max_out_time[SCAN_LB_LMAC_IDX] =
  935. cpu_to_le32(timing->max_out_time);
  936. cmd->v7.suspend_time[SCAN_LB_LMAC_IDX] =
  937. cpu_to_le32(timing->suspend_time);
  938. if (iwl_mvm_is_cdb_supported(mvm)) {
  939. cmd->v7.max_out_time[SCAN_HB_LMAC_IDX] =
  940. cpu_to_le32(timing->max_out_time);
  941. cmd->v7.suspend_time[SCAN_HB_LMAC_IDX] =
  942. cpu_to_le32(timing->suspend_time);
  943. }
  944. return;
  945. }
  946. if (params->measurement_dwell) {
  947. cmd->v1.active_dwell = params->measurement_dwell;
  948. cmd->v1.passive_dwell = params->measurement_dwell;
  949. cmd->v1.extended_dwell = params->measurement_dwell;
  950. } else {
  951. cmd->v1.active_dwell = IWL_SCAN_DWELL_ACTIVE;
  952. cmd->v1.passive_dwell = IWL_SCAN_DWELL_PASSIVE;
  953. cmd->v1.extended_dwell = IWL_SCAN_DWELL_EXTENDED;
  954. }
  955. cmd->v1.fragmented_dwell = IWL_SCAN_DWELL_FRAGMENTED;
  956. if (iwl_mvm_has_new_tx_api(mvm)) {
  957. cmd->v6.scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
  958. cmd->v6.max_out_time[SCAN_LB_LMAC_IDX] =
  959. cpu_to_le32(timing->max_out_time);
  960. cmd->v6.suspend_time[SCAN_LB_LMAC_IDX] =
  961. cpu_to_le32(timing->suspend_time);
  962. if (iwl_mvm_is_cdb_supported(mvm)) {
  963. cmd->v6.max_out_time[SCAN_HB_LMAC_IDX] =
  964. cpu_to_le32(timing->max_out_time);
  965. cmd->v6.suspend_time[SCAN_HB_LMAC_IDX] =
  966. cpu_to_le32(timing->suspend_time);
  967. }
  968. } else {
  969. cmd->v1.max_out_time = cpu_to_le32(timing->max_out_time);
  970. cmd->v1.suspend_time = cpu_to_le32(timing->suspend_time);
  971. cmd->v1.scan_priority =
  972. cpu_to_le32(IWL_SCAN_PRIORITY_EXT_6);
  973. }
  974. }
  975. static void
  976. iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
  977. struct ieee80211_channel **channels,
  978. int n_channels, u32 ssid_bitmap,
  979. struct iwl_scan_channel_cfg_umac *channel_cfg)
  980. {
  981. int i;
  982. for (i = 0; i < n_channels; i++) {
  983. channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
  984. channel_cfg[i].channel_num = channels[i]->hw_value;
  985. channel_cfg[i].iter_count = 1;
  986. channel_cfg[i].iter_interval = 0;
  987. }
  988. }
  989. static u16 iwl_mvm_scan_umac_flags(struct iwl_mvm *mvm,
  990. struct iwl_mvm_scan_params *params,
  991. struct ieee80211_vif *vif)
  992. {
  993. u16 flags = 0;
  994. if (params->n_ssids == 0)
  995. flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
  996. if (params->n_ssids == 1 && params->ssids[0].ssid_len != 0)
  997. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
  998. if (params->type == IWL_SCAN_TYPE_FRAGMENTED) {
  999. flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
  1000. if (iwl_mvm_is_cdb_supported(mvm))
  1001. flags |= IWL_UMAC_SCAN_GEN_FLAGS_LMAC2_FRAGMENTED;
  1002. }
  1003. if (iwl_mvm_rrm_scan_needed(mvm))
  1004. flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
  1005. if (params->pass_all)
  1006. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1007. else
  1008. flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
  1009. if (!iwl_mvm_is_regular_scan(params))
  1010. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
  1011. if (params->measurement_dwell)
  1012. flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
  1013. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1014. if (mvm->scan_iter_notif_enabled)
  1015. flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
  1016. #endif
  1017. if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)
  1018. flags |= IWL_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE;
  1019. if (iwl_mvm_is_regular_scan(params) &&
  1020. vif->type != NL80211_IFTYPE_P2P_DEVICE &&
  1021. params->type != IWL_SCAN_TYPE_FRAGMENTED)
  1022. flags |= IWL_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL;
  1023. return flags;
  1024. }
  1025. static int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1026. struct iwl_mvm_scan_params *params,
  1027. int type)
  1028. {
  1029. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1030. void *cmd_data = iwl_mvm_get_scan_req_umac_data(mvm);
  1031. struct iwl_scan_req_umac_tail *sec_part = cmd_data +
  1032. sizeof(struct iwl_scan_channel_cfg_umac) *
  1033. mvm->fw->ucode_capa.n_scan_channels;
  1034. int uid, i;
  1035. u32 ssid_bitmap = 0;
  1036. u8 channel_flags = 0;
  1037. struct iwl_mvm_vif *scan_vif = iwl_mvm_vif_from_mac80211(vif);
  1038. lockdep_assert_held(&mvm->mutex);
  1039. if (WARN_ON(params->n_scan_plans > IWL_MAX_SCHED_SCAN_PLANS))
  1040. return -EINVAL;
  1041. uid = iwl_mvm_scan_uid_by_status(mvm, 0);
  1042. if (uid < 0)
  1043. return uid;
  1044. memset(cmd, 0, ksize(cmd));
  1045. iwl_mvm_scan_umac_dwell(mvm, cmd, params);
  1046. mvm->scan_uid_status[uid] = type;
  1047. cmd->uid = cpu_to_le32(uid);
  1048. cmd->general_flags = cpu_to_le16(iwl_mvm_scan_umac_flags(mvm, params,
  1049. vif));
  1050. cmd->scan_start_mac_id = scan_vif->id;
  1051. if (type == IWL_MVM_SCAN_SCHED || type == IWL_MVM_SCAN_NETDETECT)
  1052. cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
  1053. if (iwl_mvm_scan_use_ebs(mvm, vif))
  1054. channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
  1055. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1056. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
  1057. if (iwl_mvm_is_adaptive_dwell_supported(mvm)) {
  1058. cmd->v7.channel_flags = channel_flags;
  1059. cmd->v7.n_channels = params->n_channels;
  1060. } else if (iwl_mvm_has_new_tx_api(mvm)) {
  1061. cmd->v6.channel_flags = channel_flags;
  1062. cmd->v6.n_channels = params->n_channels;
  1063. } else {
  1064. cmd->v1.channel_flags = channel_flags;
  1065. cmd->v1.n_channels = params->n_channels;
  1066. }
  1067. iwl_scan_build_ssids(params, sec_part->direct_scan, &ssid_bitmap);
  1068. iwl_mvm_umac_scan_cfg_channels(mvm, params->channels,
  1069. params->n_channels, ssid_bitmap,
  1070. cmd_data);
  1071. for (i = 0; i < params->n_scan_plans; i++) {
  1072. struct cfg80211_sched_scan_plan *scan_plan =
  1073. &params->scan_plans[i];
  1074. sec_part->schedule[i].iter_count = scan_plan->iterations;
  1075. sec_part->schedule[i].interval =
  1076. cpu_to_le16(scan_plan->interval);
  1077. }
  1078. /*
  1079. * If the number of iterations of the last scan plan is set to
  1080. * zero, it should run infinitely. However, this is not always the case.
  1081. * For example, when regular scan is requested the driver sets one scan
  1082. * plan with one iteration.
  1083. */
  1084. if (!sec_part->schedule[i - 1].iter_count)
  1085. sec_part->schedule[i - 1].iter_count = 0xff;
  1086. sec_part->delay = cpu_to_le16(params->delay);
  1087. sec_part->preq = params->preq;
  1088. return 0;
  1089. }
  1090. static int iwl_mvm_num_scans(struct iwl_mvm *mvm)
  1091. {
  1092. return hweight32(mvm->scan_status & IWL_MVM_SCAN_MASK);
  1093. }
  1094. static int iwl_mvm_check_running_scans(struct iwl_mvm *mvm, int type)
  1095. {
  1096. bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
  1097. IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
  1098. /* This looks a bit arbitrary, but the idea is that if we run
  1099. * out of possible simultaneous scans and the userspace is
  1100. * trying to run a scan type that is already running, we
  1101. * return -EBUSY. But if the userspace wants to start a
  1102. * different type of scan, we stop the opposite type to make
  1103. * space for the new request. The reason is backwards
  1104. * compatibility with old wpa_supplicant that wouldn't stop a
  1105. * scheduled scan before starting a normal scan.
  1106. */
  1107. if (iwl_mvm_num_scans(mvm) < mvm->max_scans)
  1108. return 0;
  1109. /* Use a switch, even though this is a bitmask, so that more
  1110. * than one bits set will fall in default and we will warn.
  1111. */
  1112. switch (type) {
  1113. case IWL_MVM_SCAN_REGULAR:
  1114. if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
  1115. return -EBUSY;
  1116. return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
  1117. case IWL_MVM_SCAN_SCHED:
  1118. if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
  1119. return -EBUSY;
  1120. return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
  1121. case IWL_MVM_SCAN_NETDETECT:
  1122. /* For non-unified images, there's no need to stop
  1123. * anything for net-detect since the firmware is
  1124. * restarted anyway. This way, any sched scans that
  1125. * were running will be restarted when we resume.
  1126. */
  1127. if (!unified_image)
  1128. return 0;
  1129. /* If this is a unified image and we ran out of scans,
  1130. * we need to stop something. Prefer stopping regular
  1131. * scans, because the results are useless at this
  1132. * point, and we should be able to keep running
  1133. * another scheduled scan while suspended.
  1134. */
  1135. if (mvm->scan_status & IWL_MVM_SCAN_REGULAR_MASK)
  1136. return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR,
  1137. true);
  1138. if (mvm->scan_status & IWL_MVM_SCAN_SCHED_MASK)
  1139. return iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED,
  1140. true);
  1141. /* fall through, something is wrong if no scan was
  1142. * running but we ran out of scans.
  1143. */
  1144. default:
  1145. WARN_ON(1);
  1146. break;
  1147. }
  1148. return -EIO;
  1149. }
  1150. #define SCAN_TIMEOUT 20000
  1151. void iwl_mvm_scan_timeout_wk(struct work_struct *work)
  1152. {
  1153. struct delayed_work *delayed_work = to_delayed_work(work);
  1154. struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
  1155. scan_timeout_dwork);
  1156. IWL_ERR(mvm, "regular scan timed out\n");
  1157. iwl_force_nmi(mvm->trans);
  1158. }
  1159. int iwl_mvm_reg_scan_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1160. struct cfg80211_scan_request *req,
  1161. struct ieee80211_scan_ies *ies)
  1162. {
  1163. struct iwl_host_cmd hcmd = {
  1164. .len = { iwl_mvm_scan_size(mvm), },
  1165. .data = { mvm->scan_cmd, },
  1166. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1167. };
  1168. struct iwl_mvm_scan_params params = {};
  1169. int ret;
  1170. struct cfg80211_sched_scan_plan scan_plan = { .iterations = 1 };
  1171. lockdep_assert_held(&mvm->mutex);
  1172. if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
  1173. IWL_ERR(mvm, "scan while LAR regdomain is not set\n");
  1174. return -EBUSY;
  1175. }
  1176. ret = iwl_mvm_check_running_scans(mvm, IWL_MVM_SCAN_REGULAR);
  1177. if (ret)
  1178. return ret;
  1179. /* we should have failed registration if scan_cmd was NULL */
  1180. if (WARN_ON(!mvm->scan_cmd))
  1181. return -ENOMEM;
  1182. if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
  1183. return -ENOBUFS;
  1184. params.n_ssids = req->n_ssids;
  1185. params.flags = req->flags;
  1186. params.n_channels = req->n_channels;
  1187. params.delay = 0;
  1188. params.ssids = req->ssids;
  1189. params.channels = req->channels;
  1190. params.mac_addr = req->mac_addr;
  1191. params.mac_addr_mask = req->mac_addr_mask;
  1192. params.no_cck = req->no_cck;
  1193. params.pass_all = true;
  1194. params.n_match_sets = 0;
  1195. params.match_sets = NULL;
  1196. params.scan_plans = &scan_plan;
  1197. params.n_scan_plans = 1;
  1198. params.type =
  1199. iwl_mvm_get_scan_type(mvm,
  1200. vif->type == NL80211_IFTYPE_P2P_DEVICE);
  1201. ret = iwl_mvm_get_measurement_dwell(mvm, req, &params);
  1202. if (ret < 0)
  1203. return ret;
  1204. params.measurement_dwell = ret;
  1205. iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
  1206. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
  1207. hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
  1208. ret = iwl_mvm_scan_umac(mvm, vif, &params,
  1209. IWL_MVM_SCAN_REGULAR);
  1210. } else {
  1211. hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
  1212. ret = iwl_mvm_scan_lmac(mvm, vif, &params);
  1213. }
  1214. if (ret)
  1215. return ret;
  1216. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1217. if (ret) {
  1218. /* If the scan failed, it usually means that the FW was unable
  1219. * to allocate the time events. Warn on it, but maybe we
  1220. * should try to send the command again with different params.
  1221. */
  1222. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1223. return ret;
  1224. }
  1225. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1226. mvm->scan_status |= IWL_MVM_SCAN_REGULAR;
  1227. mvm->scan_vif = iwl_mvm_vif_from_mac80211(vif);
  1228. iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
  1229. schedule_delayed_work(&mvm->scan_timeout_dwork,
  1230. msecs_to_jiffies(SCAN_TIMEOUT));
  1231. return 0;
  1232. }
  1233. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  1234. struct ieee80211_vif *vif,
  1235. struct cfg80211_sched_scan_request *req,
  1236. struct ieee80211_scan_ies *ies,
  1237. int type)
  1238. {
  1239. struct iwl_host_cmd hcmd = {
  1240. .len = { iwl_mvm_scan_size(mvm), },
  1241. .data = { mvm->scan_cmd, },
  1242. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1243. };
  1244. struct iwl_mvm_scan_params params = {};
  1245. int ret;
  1246. lockdep_assert_held(&mvm->mutex);
  1247. if (iwl_mvm_is_lar_supported(mvm) && !mvm->lar_regdom_set) {
  1248. IWL_ERR(mvm, "sched-scan while LAR regdomain is not set\n");
  1249. return -EBUSY;
  1250. }
  1251. ret = iwl_mvm_check_running_scans(mvm, type);
  1252. if (ret)
  1253. return ret;
  1254. /* we should have failed registration if scan_cmd was NULL */
  1255. if (WARN_ON(!mvm->scan_cmd))
  1256. return -ENOMEM;
  1257. if (!iwl_mvm_scan_fits(mvm, req->n_ssids, ies, req->n_channels))
  1258. return -ENOBUFS;
  1259. params.n_ssids = req->n_ssids;
  1260. params.flags = req->flags;
  1261. params.n_channels = req->n_channels;
  1262. params.ssids = req->ssids;
  1263. params.channels = req->channels;
  1264. params.mac_addr = req->mac_addr;
  1265. params.mac_addr_mask = req->mac_addr_mask;
  1266. params.no_cck = false;
  1267. params.pass_all = iwl_mvm_scan_pass_all(mvm, req);
  1268. params.n_match_sets = req->n_match_sets;
  1269. params.match_sets = req->match_sets;
  1270. if (!req->n_scan_plans)
  1271. return -EINVAL;
  1272. params.n_scan_plans = req->n_scan_plans;
  1273. params.scan_plans = req->scan_plans;
  1274. params.type =
  1275. iwl_mvm_get_scan_type(mvm,
  1276. vif->type == NL80211_IFTYPE_P2P_DEVICE);
  1277. /* In theory, LMAC scans can handle a 32-bit delay, but since
  1278. * waiting for over 18 hours to start the scan is a bit silly
  1279. * and to keep it aligned with UMAC scans (which only support
  1280. * 16-bit delays), trim it down to 16-bits.
  1281. */
  1282. if (req->delay > U16_MAX) {
  1283. IWL_DEBUG_SCAN(mvm,
  1284. "delay value is > 16-bits, set to max possible\n");
  1285. params.delay = U16_MAX;
  1286. } else {
  1287. params.delay = req->delay;
  1288. }
  1289. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  1290. if (ret)
  1291. return ret;
  1292. iwl_mvm_build_scan_probe(mvm, vif, ies, &params);
  1293. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
  1294. hcmd.id = iwl_cmd_id(SCAN_REQ_UMAC, IWL_ALWAYS_LONG_GROUP, 0);
  1295. ret = iwl_mvm_scan_umac(mvm, vif, &params, type);
  1296. } else {
  1297. hcmd.id = SCAN_OFFLOAD_REQUEST_CMD;
  1298. ret = iwl_mvm_scan_lmac(mvm, vif, &params);
  1299. }
  1300. if (ret)
  1301. return ret;
  1302. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1303. if (!ret) {
  1304. IWL_DEBUG_SCAN(mvm,
  1305. "Sched scan request was sent successfully\n");
  1306. mvm->scan_status |= type;
  1307. } else {
  1308. /* If the scan failed, it usually means that the FW was unable
  1309. * to allocate the time events. Warn on it, but maybe we
  1310. * should try to send the command again with different params.
  1311. */
  1312. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1313. }
  1314. return ret;
  1315. }
  1316. void iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
  1317. struct iwl_rx_cmd_buffer *rxb)
  1318. {
  1319. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1320. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1321. u32 uid = __le32_to_cpu(notif->uid);
  1322. bool aborted = (notif->status == IWL_SCAN_OFFLOAD_ABORTED);
  1323. if (WARN_ON(!(mvm->scan_uid_status[uid] & mvm->scan_status)))
  1324. return;
  1325. /* if the scan is already stopping, we don't need to notify mac80211 */
  1326. if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_REGULAR) {
  1327. struct cfg80211_scan_info info = {
  1328. .aborted = aborted,
  1329. .scan_start_tsf = mvm->scan_start,
  1330. };
  1331. memcpy(info.tsf_bssid, mvm->scan_vif->bssid, ETH_ALEN);
  1332. ieee80211_scan_completed(mvm->hw, &info);
  1333. mvm->scan_vif = NULL;
  1334. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1335. cancel_delayed_work(&mvm->scan_timeout_dwork);
  1336. } else if (mvm->scan_uid_status[uid] == IWL_MVM_SCAN_SCHED) {
  1337. ieee80211_sched_scan_stopped(mvm->hw);
  1338. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
  1339. }
  1340. mvm->scan_status &= ~mvm->scan_uid_status[uid];
  1341. IWL_DEBUG_SCAN(mvm,
  1342. "Scan completed, uid %u type %u, status %s, EBS status %s\n",
  1343. uid, mvm->scan_uid_status[uid],
  1344. notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  1345. "completed" : "aborted",
  1346. iwl_mvm_ebs_status_str(notif->ebs_status));
  1347. IWL_DEBUG_SCAN(mvm,
  1348. "Last line %d, Last iteration %d, Time from last iteration %d\n",
  1349. notif->last_schedule, notif->last_iter,
  1350. __le32_to_cpu(notif->time_from_last_iter));
  1351. if (notif->ebs_status != IWL_SCAN_EBS_SUCCESS &&
  1352. notif->ebs_status != IWL_SCAN_EBS_INACTIVE)
  1353. mvm->last_ebs_successful = false;
  1354. mvm->scan_uid_status[uid] = 0;
  1355. }
  1356. void iwl_mvm_rx_umac_scan_iter_complete_notif(struct iwl_mvm *mvm,
  1357. struct iwl_rx_cmd_buffer *rxb)
  1358. {
  1359. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1360. struct iwl_umac_scan_iter_complete_notif *notif = (void *)pkt->data;
  1361. mvm->scan_start = le64_to_cpu(notif->start_tsf);
  1362. IWL_DEBUG_SCAN(mvm,
  1363. "UMAC Scan iteration complete: status=0x%x scanned_channels=%d\n",
  1364. notif->status, notif->scanned_channels);
  1365. if (mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_FOUND) {
  1366. IWL_DEBUG_SCAN(mvm, "Pass all scheduled scan results found\n");
  1367. ieee80211_sched_scan_results(mvm->hw);
  1368. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_ENABLED;
  1369. }
  1370. IWL_DEBUG_SCAN(mvm,
  1371. "UMAC Scan iteration complete: scan started at %llu (TSF)\n",
  1372. mvm->scan_start);
  1373. }
  1374. static int iwl_mvm_umac_scan_abort(struct iwl_mvm *mvm, int type)
  1375. {
  1376. struct iwl_umac_scan_abort cmd = {};
  1377. int uid, ret;
  1378. lockdep_assert_held(&mvm->mutex);
  1379. /* We should always get a valid index here, because we already
  1380. * checked that this type of scan was running in the generic
  1381. * code.
  1382. */
  1383. uid = iwl_mvm_scan_uid_by_status(mvm, type);
  1384. if (WARN_ON_ONCE(uid < 0))
  1385. return uid;
  1386. cmd.uid = cpu_to_le32(uid);
  1387. IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
  1388. ret = iwl_mvm_send_cmd_pdu(mvm,
  1389. iwl_cmd_id(SCAN_ABORT_UMAC,
  1390. IWL_ALWAYS_LONG_GROUP, 0),
  1391. 0, sizeof(cmd), &cmd);
  1392. if (!ret)
  1393. mvm->scan_uid_status[uid] = type << IWL_MVM_SCAN_STOPPING_SHIFT;
  1394. return ret;
  1395. }
  1396. static int iwl_mvm_scan_stop_wait(struct iwl_mvm *mvm, int type)
  1397. {
  1398. struct iwl_notification_wait wait_scan_done;
  1399. static const u16 scan_done_notif[] = { SCAN_COMPLETE_UMAC,
  1400. SCAN_OFFLOAD_COMPLETE, };
  1401. int ret;
  1402. lockdep_assert_held(&mvm->mutex);
  1403. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  1404. scan_done_notif,
  1405. ARRAY_SIZE(scan_done_notif),
  1406. NULL, NULL);
  1407. IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
  1408. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
  1409. ret = iwl_mvm_umac_scan_abort(mvm, type);
  1410. else
  1411. ret = iwl_mvm_lmac_scan_abort(mvm);
  1412. if (ret) {
  1413. IWL_DEBUG_SCAN(mvm, "couldn't stop scan type %d\n", type);
  1414. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  1415. return ret;
  1416. }
  1417. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  1418. return ret;
  1419. }
  1420. int iwl_mvm_scan_size(struct iwl_mvm *mvm)
  1421. {
  1422. int base_size = IWL_SCAN_REQ_UMAC_SIZE_V1;
  1423. if (iwl_mvm_is_adaptive_dwell_supported(mvm))
  1424. base_size = IWL_SCAN_REQ_UMAC_SIZE_V7;
  1425. else if (iwl_mvm_has_new_tx_api(mvm))
  1426. base_size = IWL_SCAN_REQ_UMAC_SIZE_V6;
  1427. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
  1428. return base_size +
  1429. sizeof(struct iwl_scan_channel_cfg_umac) *
  1430. mvm->fw->ucode_capa.n_scan_channels +
  1431. sizeof(struct iwl_scan_req_umac_tail);
  1432. return sizeof(struct iwl_scan_req_lmac) +
  1433. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1434. mvm->fw->ucode_capa.n_scan_channels +
  1435. sizeof(struct iwl_scan_probe_req);
  1436. }
  1437. /*
  1438. * This function is used in nic restart flow, to inform mac80211 about scans
  1439. * that was aborted by restart flow or by an assert.
  1440. */
  1441. void iwl_mvm_report_scan_aborted(struct iwl_mvm *mvm)
  1442. {
  1443. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
  1444. int uid, i;
  1445. uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_REGULAR);
  1446. if (uid >= 0) {
  1447. struct cfg80211_scan_info info = {
  1448. .aborted = true,
  1449. };
  1450. ieee80211_scan_completed(mvm->hw, &info);
  1451. mvm->scan_uid_status[uid] = 0;
  1452. }
  1453. uid = iwl_mvm_scan_uid_by_status(mvm, IWL_MVM_SCAN_SCHED);
  1454. if (uid >= 0 && !mvm->fw_restart) {
  1455. ieee80211_sched_scan_stopped(mvm->hw);
  1456. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
  1457. mvm->scan_uid_status[uid] = 0;
  1458. }
  1459. /* We shouldn't have any UIDs still set. Loop over all the
  1460. * UIDs to make sure there's nothing left there and warn if
  1461. * any is found.
  1462. */
  1463. for (i = 0; i < mvm->max_scans; i++) {
  1464. if (WARN_ONCE(mvm->scan_uid_status[i],
  1465. "UMAC scan UID %d status was not cleaned\n",
  1466. i))
  1467. mvm->scan_uid_status[i] = 0;
  1468. }
  1469. } else {
  1470. if (mvm->scan_status & IWL_MVM_SCAN_REGULAR) {
  1471. struct cfg80211_scan_info info = {
  1472. .aborted = true,
  1473. };
  1474. ieee80211_scan_completed(mvm->hw, &info);
  1475. }
  1476. /* Sched scan will be restarted by mac80211 in
  1477. * restart_hw, so do not report if FW is about to be
  1478. * restarted.
  1479. */
  1480. if ((mvm->scan_status & IWL_MVM_SCAN_SCHED) &&
  1481. !mvm->fw_restart) {
  1482. ieee80211_sched_scan_stopped(mvm->hw);
  1483. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
  1484. }
  1485. }
  1486. }
  1487. int iwl_mvm_scan_stop(struct iwl_mvm *mvm, int type, bool notify)
  1488. {
  1489. int ret;
  1490. if (!(mvm->scan_status & type))
  1491. return 0;
  1492. if (iwl_mvm_is_radio_killed(mvm)) {
  1493. ret = 0;
  1494. goto out;
  1495. }
  1496. ret = iwl_mvm_scan_stop_wait(mvm, type);
  1497. if (!ret)
  1498. mvm->scan_status |= type << IWL_MVM_SCAN_STOPPING_SHIFT;
  1499. out:
  1500. /* Clear the scan status so the next scan requests will
  1501. * succeed and mark the scan as stopping, so that the Rx
  1502. * handler doesn't do anything, as the scan was stopped from
  1503. * above.
  1504. */
  1505. mvm->scan_status &= ~type;
  1506. if (type == IWL_MVM_SCAN_REGULAR) {
  1507. /* Since the rx handler won't do anything now, we have
  1508. * to release the scan reference here.
  1509. */
  1510. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1511. cancel_delayed_work(&mvm->scan_timeout_dwork);
  1512. if (notify) {
  1513. struct cfg80211_scan_info info = {
  1514. .aborted = true,
  1515. };
  1516. ieee80211_scan_completed(mvm->hw, &info);
  1517. }
  1518. } else if (notify) {
  1519. ieee80211_sched_scan_stopped(mvm->hw);
  1520. mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_DISABLED;
  1521. }
  1522. return ret;
  1523. }