scan.c 47 KB

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