scan.c 52 KB

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