scan.c 48 KB

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