scan.c 50 KB

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