scan.c 57 KB

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