scan.c 53 KB

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