scan.c 49 KB

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