scan.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/etherdevice.h>
  66. #include <net/mac80211.h>
  67. #include "mvm.h"
  68. #include "iwl-eeprom-parse.h"
  69. #include "fw-api-scan.h"
  70. #define IWL_PLCP_QUIET_THRESH 1
  71. #define IWL_ACTIVE_QUIET_TIME 10
  72. #define IWL_DENSE_EBS_SCAN_RATIO 5
  73. #define IWL_SPARSE_EBS_SCAN_RATIO 1
  74. struct iwl_mvm_scan_params {
  75. u32 max_out_time;
  76. u32 suspend_time;
  77. bool passive_fragmented;
  78. struct _dwell {
  79. u16 passive;
  80. u16 active;
  81. } dwell[IEEE80211_NUM_BANDS];
  82. };
  83. enum iwl_umac_scan_uid_type {
  84. IWL_UMAC_SCAN_UID_REG_SCAN = BIT(0),
  85. IWL_UMAC_SCAN_UID_SCHED_SCAN = BIT(1),
  86. IWL_UMAC_SCAN_UID_ALL = IWL_UMAC_SCAN_UID_REG_SCAN |
  87. IWL_UMAC_SCAN_UID_SCHED_SCAN,
  88. };
  89. static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
  90. enum iwl_umac_scan_uid_type type, bool notify);
  91. static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
  92. {
  93. if (mvm->scan_rx_ant != ANT_NONE)
  94. return mvm->scan_rx_ant;
  95. return mvm->fw->valid_rx_ant;
  96. }
  97. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  98. {
  99. u16 rx_chain;
  100. u8 rx_ant;
  101. rx_ant = iwl_mvm_scan_rx_ant(mvm);
  102. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  103. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  104. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  105. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  106. return cpu_to_le16(rx_chain);
  107. }
  108. static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
  109. {
  110. if (band == IEEE80211_BAND_2GHZ)
  111. return cpu_to_le32(PHY_BAND_24);
  112. else
  113. return cpu_to_le32(PHY_BAND_5);
  114. }
  115. static inline __le32
  116. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  117. bool no_cck)
  118. {
  119. u32 tx_ant;
  120. mvm->scan_last_antenna_idx =
  121. iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
  122. mvm->scan_last_antenna_idx);
  123. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  124. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  125. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  126. tx_ant);
  127. else
  128. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  129. }
  130. /*
  131. * We insert the SSIDs in an inverted order, because the FW will
  132. * invert it back. The most prioritized SSID, which is first in the
  133. * request list, is not copied here, but inserted directly to the probe
  134. * request.
  135. */
  136. static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
  137. struct cfg80211_ssid *ssids,
  138. int n_ssids, int first)
  139. {
  140. int fw_idx, req_idx;
  141. for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
  142. req_idx--, fw_idx++) {
  143. cmd_ssid[fw_idx].id = WLAN_EID_SSID;
  144. cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
  145. memcpy(cmd_ssid[fw_idx].ssid,
  146. ssids[req_idx].ssid,
  147. ssids[req_idx].ssid_len);
  148. }
  149. }
  150. /*
  151. * If req->n_ssids > 0, it means we should do an active scan.
  152. * In case of active scan w/o directed scan, we receive a zero-length SSID
  153. * just to notify that this scan is active and not passive.
  154. * In order to notify the FW of the number of SSIDs we wish to scan (including
  155. * the zero-length one), we need to set the corresponding bits in chan->type,
  156. * one for each SSID, and set the active bit (first). If the first SSID is
  157. * already included in the probe template, so we need to set only
  158. * req->n_ssids - 1 bits in addition to the first bit.
  159. */
  160. static u16 iwl_mvm_get_active_dwell(struct iwl_mvm *mvm,
  161. enum ieee80211_band band, int n_ssids)
  162. {
  163. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
  164. return 10;
  165. if (band == IEEE80211_BAND_2GHZ)
  166. return 20 + 3 * (n_ssids + 1);
  167. return 10 + 2 * (n_ssids + 1);
  168. }
  169. static u16 iwl_mvm_get_passive_dwell(struct iwl_mvm *mvm,
  170. enum ieee80211_band band)
  171. {
  172. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_BASIC_DWELL)
  173. return 110;
  174. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  175. }
  176. static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
  177. struct cfg80211_scan_request *req,
  178. bool basic_ssid,
  179. struct iwl_mvm_scan_params *params)
  180. {
  181. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  182. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  183. int i;
  184. int type = BIT(req->n_ssids) - 1;
  185. enum ieee80211_band band = req->channels[0]->band;
  186. if (!basic_ssid)
  187. type |= BIT(req->n_ssids);
  188. for (i = 0; i < cmd->channel_count; i++) {
  189. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  190. chan->type = cpu_to_le32(type);
  191. if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
  192. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  193. chan->active_dwell = cpu_to_le16(params->dwell[band].active);
  194. chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
  195. chan->iteration_count = cpu_to_le16(1);
  196. chan++;
  197. }
  198. }
  199. /*
  200. * Fill in probe request with the following parameters:
  201. * TA is our vif HW address, which mac80211 ensures we have.
  202. * Packet is broadcasted, so this is both SA and DA.
  203. * The probe request IE is made out of two: first comes the most prioritized
  204. * SSID if a directed scan is requested. Second comes whatever extra
  205. * information was given to us as the scan request IE.
  206. */
  207. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  208. int n_ssids, const u8 *ssid, int ssid_len,
  209. const u8 *band_ie, int band_ie_len,
  210. const u8 *common_ie, int common_ie_len,
  211. int left)
  212. {
  213. int len = 0;
  214. u8 *pos = NULL;
  215. /* Make sure there is enough space for the probe request,
  216. * two mandatory IEs and the data */
  217. left -= 24;
  218. if (left < 0)
  219. return 0;
  220. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  221. eth_broadcast_addr(frame->da);
  222. memcpy(frame->sa, ta, ETH_ALEN);
  223. eth_broadcast_addr(frame->bssid);
  224. frame->seq_ctrl = 0;
  225. len += 24;
  226. /* for passive scans, no need to fill anything */
  227. if (n_ssids == 0)
  228. return (u16)len;
  229. /* points to the payload of the request */
  230. pos = &frame->u.probe_req.variable[0];
  231. /* fill in our SSID IE */
  232. left -= ssid_len + 2;
  233. if (left < 0)
  234. return 0;
  235. *pos++ = WLAN_EID_SSID;
  236. *pos++ = ssid_len;
  237. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  238. memcpy(pos, ssid, ssid_len);
  239. pos += ssid_len;
  240. }
  241. len += ssid_len + 2;
  242. if (WARN_ON(left < band_ie_len + common_ie_len))
  243. return len;
  244. if (band_ie && band_ie_len) {
  245. memcpy(pos, band_ie, band_ie_len);
  246. pos += band_ie_len;
  247. len += band_ie_len;
  248. }
  249. if (common_ie && common_ie_len) {
  250. memcpy(pos, common_ie, common_ie_len);
  251. pos += common_ie_len;
  252. len += common_ie_len;
  253. }
  254. return (u16)len;
  255. }
  256. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  257. struct ieee80211_vif *vif)
  258. {
  259. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  260. bool *global_bound = data;
  261. if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
  262. mvmvif->phy_ctxt->id < MAX_PHYS)
  263. *global_bound = true;
  264. }
  265. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  266. struct ieee80211_vif *vif,
  267. int n_ssids, u32 flags,
  268. struct iwl_mvm_scan_params *params)
  269. {
  270. bool global_bound = false;
  271. enum ieee80211_band band;
  272. u8 frag_passive_dwell = 0;
  273. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  274. IEEE80211_IFACE_ITER_NORMAL,
  275. iwl_mvm_scan_condition_iterator,
  276. &global_bound);
  277. if (!global_bound)
  278. goto not_bound;
  279. params->suspend_time = 30;
  280. params->max_out_time = 170;
  281. if (iwl_mvm_low_latency(mvm)) {
  282. if (mvm->fw->ucode_capa.api[0] &
  283. IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
  284. params->suspend_time = 105;
  285. params->max_out_time = 70;
  286. frag_passive_dwell = 20;
  287. } else {
  288. params->suspend_time = 120;
  289. params->max_out_time = 120;
  290. }
  291. }
  292. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  293. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  294. /*
  295. * P2P device scan should not be fragmented to avoid negative
  296. * impact on P2P device discovery. Configure max_out_time to be
  297. * equal to dwell time on passive channel. Take a longest
  298. * possible value, one that corresponds to 2GHz band
  299. */
  300. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  301. u32 passive_dwell =
  302. iwl_mvm_get_passive_dwell(mvm,
  303. IEEE80211_BAND_2GHZ);
  304. params->max_out_time = passive_dwell;
  305. } else {
  306. params->passive_fragmented = true;
  307. }
  308. }
  309. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  310. params->max_out_time = 200;
  311. not_bound:
  312. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  313. if (params->passive_fragmented)
  314. params->dwell[band].passive = frag_passive_dwell;
  315. else
  316. params->dwell[band].passive =
  317. iwl_mvm_get_passive_dwell(mvm, band);
  318. params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
  319. n_ssids);
  320. }
  321. }
  322. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  323. {
  324. /* require rrm scan whenever the fw supports it */
  325. return mvm->fw->ucode_capa.capa[0] &
  326. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
  327. }
  328. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
  329. bool is_sched_scan)
  330. {
  331. int max_probe_len;
  332. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  333. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  334. else
  335. max_probe_len = mvm->fw->ucode_capa.max_probe_length;
  336. /* we create the 802.11 header and SSID element */
  337. max_probe_len -= 24 + 2;
  338. /* basic ssid is added only for hw_scan with and old api */
  339. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
  340. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
  341. !is_sched_scan)
  342. max_probe_len -= 32;
  343. /* DS parameter set element is added on 2.4GHZ band if required */
  344. if (iwl_mvm_rrm_scan_needed(mvm))
  345. max_probe_len -= 3;
  346. return max_probe_len;
  347. }
  348. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
  349. {
  350. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
  351. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
  352. return max_ie_len;
  353. /* TODO: [BUG] This function should return the maximum allowed size of
  354. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  355. * in the same command. So the correct implementation of this function
  356. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  357. * command has only 512 bytes and it would leave us with about 240
  358. * bytes for scan IEs, which is clearly not enough. So meanwhile
  359. * we will report an incorrect value. This may result in a failure to
  360. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  361. * functions with -ENOBUFS, if a large enough probe will be provided.
  362. */
  363. return max_ie_len;
  364. }
  365. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  366. struct ieee80211_vif *vif,
  367. struct cfg80211_scan_request *req)
  368. {
  369. struct iwl_host_cmd hcmd = {
  370. .id = SCAN_REQUEST_CMD,
  371. .len = { 0, },
  372. .data = { mvm->scan_cmd, },
  373. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  374. };
  375. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  376. int ret;
  377. u32 status;
  378. int ssid_len = 0;
  379. u8 *ssid = NULL;
  380. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  381. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  382. struct iwl_mvm_scan_params params = {};
  383. lockdep_assert_held(&mvm->mutex);
  384. /* we should have failed registration if scan_cmd was NULL */
  385. if (WARN_ON(mvm->scan_cmd == NULL))
  386. return -ENOMEM;
  387. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  388. mvm->scan_status = IWL_MVM_SCAN_OS;
  389. memset(cmd, 0, ksize(cmd));
  390. cmd->channel_count = (u8)req->n_channels;
  391. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  392. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  393. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  394. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
  395. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  396. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  397. if (params.passive_fragmented)
  398. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  399. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  400. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  401. MAC_FILTER_IN_BEACON);
  402. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  403. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  404. else
  405. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  406. cmd->repeats = cpu_to_le32(1);
  407. /*
  408. * If the user asked for passive scan, don't change to active scan if
  409. * you see any activity on the channel - remain passive.
  410. */
  411. if (req->n_ssids > 0) {
  412. cmd->passive2active = cpu_to_le16(1);
  413. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  414. if (basic_ssid) {
  415. ssid = req->ssids[0].ssid;
  416. ssid_len = req->ssids[0].ssid_len;
  417. }
  418. } else {
  419. cmd->passive2active = 0;
  420. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  421. }
  422. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
  423. basic_ssid ? 1 : 0);
  424. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  425. 3 << TX_CMD_FLG_BT_PRIO_POS);
  426. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  427. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  428. cmd->tx_cmd.rate_n_flags =
  429. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  430. req->no_cck);
  431. cmd->tx_cmd.len =
  432. cpu_to_le16(iwl_mvm_fill_probe_req(
  433. (struct ieee80211_mgmt *)cmd->data,
  434. vif->addr,
  435. req->n_ssids, ssid, ssid_len,
  436. req->ie, req->ie_len, NULL, 0,
  437. mvm->fw->ucode_capa.max_probe_length));
  438. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  439. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  440. le16_to_cpu(cmd->tx_cmd.len) +
  441. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  442. hcmd.len[0] = le16_to_cpu(cmd->len);
  443. status = SCAN_RESPONSE_OK;
  444. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  445. if (!ret && status == SCAN_RESPONSE_OK) {
  446. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  447. } else {
  448. /*
  449. * If the scan failed, it usually means that the FW was unable
  450. * to allocate the time events. Warn on it, but maybe we
  451. * should try to send the command again with different params.
  452. */
  453. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  454. status, ret);
  455. mvm->scan_status = IWL_MVM_SCAN_NONE;
  456. ret = -EIO;
  457. }
  458. return ret;
  459. }
  460. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  461. struct iwl_device_cmd *cmd)
  462. {
  463. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  464. struct iwl_cmd_response *resp = (void *)pkt->data;
  465. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  466. le32_to_cpu(resp->status));
  467. return 0;
  468. }
  469. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  470. struct iwl_device_cmd *cmd)
  471. {
  472. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  473. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  474. lockdep_assert_held(&mvm->mutex);
  475. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  476. notif->status, notif->scanned_channels);
  477. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  478. mvm->scan_status = IWL_MVM_SCAN_NONE;
  479. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  480. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  481. return 0;
  482. }
  483. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  484. struct iwl_rx_cmd_buffer *rxb,
  485. struct iwl_device_cmd *cmd)
  486. {
  487. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  488. if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) &&
  489. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  490. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  491. if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN))
  492. return 0;
  493. }
  494. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  495. ieee80211_sched_scan_results(mvm->hw);
  496. return 0;
  497. }
  498. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  499. struct iwl_rx_packet *pkt, void *data)
  500. {
  501. struct iwl_mvm *mvm =
  502. container_of(notif_wait, struct iwl_mvm, notif_wait);
  503. struct iwl_scan_complete_notif *notif;
  504. u32 *resp;
  505. switch (pkt->hdr.cmd) {
  506. case SCAN_ABORT_CMD:
  507. resp = (void *)pkt->data;
  508. if (*resp == CAN_ABORT_STATUS) {
  509. IWL_DEBUG_SCAN(mvm,
  510. "Scan can be aborted, wait until completion\n");
  511. return false;
  512. }
  513. /*
  514. * If scan cannot be aborted, it means that we had a
  515. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  516. * ieee80211_scan_completed already.
  517. */
  518. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  519. *resp);
  520. return true;
  521. case SCAN_COMPLETE_NOTIFICATION:
  522. notif = (void *)pkt->data;
  523. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  524. notif->status);
  525. return true;
  526. default:
  527. WARN_ON(1);
  528. return false;
  529. };
  530. }
  531. static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
  532. {
  533. struct iwl_notification_wait wait_scan_abort;
  534. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  535. SCAN_COMPLETE_NOTIFICATION };
  536. int ret;
  537. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  538. scan_abort_notif,
  539. ARRAY_SIZE(scan_abort_notif),
  540. iwl_mvm_scan_abort_notif, NULL);
  541. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  542. if (ret) {
  543. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  544. /* mac80211's state will be cleaned in the nic_restart flow */
  545. goto out_remove_notif;
  546. }
  547. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  548. out_remove_notif:
  549. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  550. return ret;
  551. }
  552. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  553. struct iwl_rx_cmd_buffer *rxb,
  554. struct iwl_device_cmd *cmd)
  555. {
  556. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  557. u8 status, ebs_status;
  558. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  559. struct iwl_periodic_scan_complete *scan_notif;
  560. scan_notif = (void *)pkt->data;
  561. status = scan_notif->status;
  562. ebs_status = scan_notif->ebs_status;
  563. } else {
  564. struct iwl_scan_offload_complete *scan_notif;
  565. scan_notif = (void *)pkt->data;
  566. status = scan_notif->status;
  567. ebs_status = scan_notif->ebs_status;
  568. }
  569. /* scan status must be locked for proper checking */
  570. lockdep_assert_held(&mvm->mutex);
  571. IWL_DEBUG_SCAN(mvm,
  572. "%s completed, status %s, EBS status %s\n",
  573. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  574. "Scheduled scan" : "Scan",
  575. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  576. "completed" : "aborted",
  577. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  578. "success" : "failed");
  579. /* only call mac80211 completion if the stop was initiated by FW */
  580. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  581. mvm->scan_status = IWL_MVM_SCAN_NONE;
  582. ieee80211_sched_scan_stopped(mvm->hw);
  583. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  584. mvm->scan_status = IWL_MVM_SCAN_NONE;
  585. ieee80211_scan_completed(mvm->hw,
  586. status == IWL_SCAN_OFFLOAD_ABORTED);
  587. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  588. }
  589. mvm->last_ebs_successful = !ebs_status;
  590. return 0;
  591. }
  592. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  593. struct ieee80211_vif *vif,
  594. struct ieee80211_scan_ies *ies,
  595. enum ieee80211_band band,
  596. struct iwl_tx_cmd *cmd,
  597. u8 *data)
  598. {
  599. u16 cmd_len;
  600. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  601. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  602. cmd->sta_id = mvm->aux_sta.sta_id;
  603. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  604. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  605. vif->addr,
  606. 1, NULL, 0,
  607. ies->ies[band], ies->len[band],
  608. ies->common_ies, ies->common_ie_len,
  609. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  610. cmd->len = cpu_to_le16(cmd_len);
  611. }
  612. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  613. struct ieee80211_vif *vif,
  614. struct cfg80211_sched_scan_request *req,
  615. struct iwl_scan_offload_cmd *scan,
  616. struct iwl_mvm_scan_params *params)
  617. {
  618. scan->channel_count = req->n_channels;
  619. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  620. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  621. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  622. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  623. scan->max_out_time = cpu_to_le32(params->max_out_time);
  624. scan->suspend_time = cpu_to_le32(params->suspend_time);
  625. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  626. MAC_FILTER_IN_BEACON);
  627. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  628. scan->rep_count = cpu_to_le32(1);
  629. if (params->passive_fragmented)
  630. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  631. }
  632. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  633. {
  634. int i;
  635. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  636. if (!ssid_list[i].len)
  637. break;
  638. if (ssid_list[i].len == ssid_len &&
  639. !memcmp(ssid_list->ssid, ssid, ssid_len))
  640. return i;
  641. }
  642. return -1;
  643. }
  644. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  645. struct iwl_ssid_ie *direct_scan,
  646. u32 *ssid_bitmap, bool basic_ssid)
  647. {
  648. int i, j;
  649. int index;
  650. /*
  651. * copy SSIDs from match list.
  652. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  653. * config match list.
  654. */
  655. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  656. /* skip empty SSID matchsets */
  657. if (!req->match_sets[i].ssid.ssid_len)
  658. continue;
  659. direct_scan[i].id = WLAN_EID_SSID;
  660. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  661. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  662. direct_scan[i].len);
  663. }
  664. /* add SSIDs from scan SSID list */
  665. *ssid_bitmap = 0;
  666. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  667. index = iwl_ssid_exist(req->ssids[j].ssid,
  668. req->ssids[j].ssid_len,
  669. direct_scan);
  670. if (index < 0) {
  671. if (!req->ssids[j].ssid_len && basic_ssid)
  672. continue;
  673. direct_scan[i].id = WLAN_EID_SSID;
  674. direct_scan[i].len = req->ssids[j].ssid_len;
  675. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  676. direct_scan[i].len);
  677. *ssid_bitmap |= BIT(i + 1);
  678. i++;
  679. } else {
  680. *ssid_bitmap |= BIT(index + 1);
  681. }
  682. }
  683. }
  684. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  685. struct cfg80211_sched_scan_request *req,
  686. u8 *channels_buffer,
  687. enum ieee80211_band band,
  688. int *head,
  689. u32 ssid_bitmap,
  690. struct iwl_mvm_scan_params *params)
  691. {
  692. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  693. __le32 *type = (__le32 *)channels_buffer;
  694. __le16 *channel_number = (__le16 *)(type + n_channels);
  695. __le16 *iter_count = channel_number + n_channels;
  696. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  697. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  698. u8 *passive_dwell = active_dwell + n_channels;
  699. int i, index = 0;
  700. for (i = 0; i < req->n_channels; i++) {
  701. struct ieee80211_channel *chan = req->channels[i];
  702. if (chan->band != band)
  703. continue;
  704. index = *head;
  705. (*head)++;
  706. channel_number[index] = cpu_to_le16(chan->hw_value);
  707. active_dwell[index] = params->dwell[band].active;
  708. passive_dwell[index] = params->dwell[band].passive;
  709. iter_count[index] = cpu_to_le16(1);
  710. iter_interval[index] = 0;
  711. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  712. type[index] |=
  713. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  714. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  715. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  716. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  717. type[index] |=
  718. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  719. /* scan for all SSIDs from req->ssids */
  720. type[index] |= cpu_to_le32(ssid_bitmap);
  721. }
  722. }
  723. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  724. struct ieee80211_vif *vif,
  725. struct cfg80211_sched_scan_request *req,
  726. struct ieee80211_scan_ies *ies)
  727. {
  728. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  729. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  730. int head = 0;
  731. u32 ssid_bitmap;
  732. int cmd_len;
  733. int ret;
  734. u8 *probes;
  735. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  736. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  737. struct iwl_scan_offload_cfg *scan_cfg;
  738. struct iwl_host_cmd cmd = {
  739. .id = SCAN_OFFLOAD_CONFIG_CMD,
  740. };
  741. struct iwl_mvm_scan_params params = {};
  742. lockdep_assert_held(&mvm->mutex);
  743. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  744. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  745. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  746. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  747. if (!scan_cfg)
  748. return -ENOMEM;
  749. probes = scan_cfg->data +
  750. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  751. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  752. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  753. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  754. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  755. &ssid_bitmap, basic_ssid);
  756. /* build tx frames for supported bands */
  757. if (band_2ghz) {
  758. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  759. IEEE80211_BAND_2GHZ,
  760. &scan_cfg->scan_cmd.tx_cmd[0],
  761. probes);
  762. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  763. IEEE80211_BAND_2GHZ, &head,
  764. ssid_bitmap, &params);
  765. }
  766. if (band_5ghz) {
  767. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  768. IEEE80211_BAND_5GHZ,
  769. &scan_cfg->scan_cmd.tx_cmd[1],
  770. probes +
  771. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  772. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  773. IEEE80211_BAND_5GHZ, &head,
  774. ssid_bitmap, &params);
  775. }
  776. cmd.data[0] = scan_cfg;
  777. cmd.len[0] = cmd_len;
  778. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  779. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  780. ret = iwl_mvm_send_cmd(mvm, &cmd);
  781. kfree(scan_cfg);
  782. return ret;
  783. }
  784. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  785. struct cfg80211_sched_scan_request *req)
  786. {
  787. struct iwl_scan_offload_profile *profile;
  788. struct iwl_scan_offload_profile_cfg *profile_cfg;
  789. struct iwl_scan_offload_blacklist *blacklist;
  790. struct iwl_host_cmd cmd = {
  791. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  792. .len[1] = sizeof(*profile_cfg),
  793. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  794. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  795. };
  796. int blacklist_len;
  797. int i;
  798. int ret;
  799. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  800. return -EIO;
  801. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  802. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  803. else
  804. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  805. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  806. if (!blacklist)
  807. return -ENOMEM;
  808. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  809. if (!profile_cfg) {
  810. ret = -ENOMEM;
  811. goto free_blacklist;
  812. }
  813. cmd.data[0] = blacklist;
  814. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  815. cmd.data[1] = profile_cfg;
  816. /* No blacklist configuration */
  817. profile_cfg->num_profiles = req->n_match_sets;
  818. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  819. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  820. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  821. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  822. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  823. for (i = 0; i < req->n_match_sets; i++) {
  824. profile = &profile_cfg->profiles[i];
  825. profile->ssid_index = i;
  826. /* Support any cipher and auth algorithm */
  827. profile->unicast_cipher = 0xff;
  828. profile->auth_alg = 0xff;
  829. profile->network_type = IWL_NETWORK_TYPE_ANY;
  830. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  831. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  832. }
  833. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  834. ret = iwl_mvm_send_cmd(mvm, &cmd);
  835. kfree(profile_cfg);
  836. free_blacklist:
  837. kfree(blacklist);
  838. return ret;
  839. }
  840. static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
  841. struct cfg80211_sched_scan_request *req)
  842. {
  843. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  844. IWL_DEBUG_SCAN(mvm,
  845. "Sending scheduled scan with filtering, n_match_sets %d\n",
  846. req->n_match_sets);
  847. return false;
  848. }
  849. IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
  850. return true;
  851. }
  852. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  853. struct cfg80211_sched_scan_request *req)
  854. {
  855. struct iwl_scan_offload_req scan_req = {
  856. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  857. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  858. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  859. .schedule_line[0].full_scan_mul = 1,
  860. .schedule_line[1].iterations = 0xff,
  861. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  862. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  863. };
  864. if (iwl_mvm_scan_pass_all(mvm, req))
  865. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  866. if (mvm->last_ebs_successful &&
  867. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  868. scan_req.flags |=
  869. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  870. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  871. sizeof(scan_req), &scan_req);
  872. }
  873. int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
  874. struct ieee80211_vif *vif,
  875. struct cfg80211_sched_scan_request *req,
  876. struct ieee80211_scan_ies *ies)
  877. {
  878. int ret;
  879. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
  880. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  881. if (ret)
  882. return ret;
  883. ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
  884. } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  885. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  886. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  887. if (ret)
  888. return ret;
  889. ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
  890. } else {
  891. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  892. ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
  893. if (ret)
  894. return ret;
  895. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  896. if (ret)
  897. return ret;
  898. ret = iwl_mvm_sched_scan_start(mvm, req);
  899. }
  900. return ret;
  901. }
  902. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  903. {
  904. int ret;
  905. struct iwl_host_cmd cmd = {
  906. .id = SCAN_OFFLOAD_ABORT_CMD,
  907. };
  908. u32 status;
  909. /* Exit instantly with error when device is not ready
  910. * to receive scan abort command or it does not perform
  911. * scheduled scan currently */
  912. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  913. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  914. mvm->scan_status != IWL_MVM_SCAN_OS))
  915. return -EIO;
  916. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  917. if (ret)
  918. return ret;
  919. if (status != CAN_ABORT_STATUS) {
  920. /*
  921. * The scan abort will return 1 for success or
  922. * 2 for "failure". A failure condition can be
  923. * due to simply not being in an active scan which
  924. * can occur if we send the scan abort before the
  925. * microcode has notified us that a scan is completed.
  926. */
  927. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  928. ret = -ENOENT;
  929. }
  930. return ret;
  931. }
  932. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  933. {
  934. int ret;
  935. struct iwl_notification_wait wait_scan_done;
  936. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  937. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  938. lockdep_assert_held(&mvm->mutex);
  939. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  940. return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
  941. notify);
  942. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  943. return 0;
  944. if (iwl_mvm_is_radio_killed(mvm))
  945. goto out;
  946. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  947. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  948. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  949. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  950. return 0;
  951. }
  952. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  953. scan_done_notif,
  954. ARRAY_SIZE(scan_done_notif),
  955. NULL, NULL);
  956. ret = iwl_mvm_send_scan_offload_abort(mvm);
  957. if (ret) {
  958. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  959. sched ? "offloaded " : "", ret);
  960. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  961. return ret;
  962. }
  963. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  964. sched ? "offloaded " : "");
  965. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  966. if (ret)
  967. return ret;
  968. /*
  969. * Clear the scan status so the next scan requests will succeed. This
  970. * also ensures the Rx handler doesn't do anything, as the scan was
  971. * stopped from above. Since the rx handler won't do anything now,
  972. * we have to release the scan reference here.
  973. */
  974. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  975. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  976. out:
  977. mvm->scan_status = IWL_MVM_SCAN_NONE;
  978. if (notify) {
  979. if (sched)
  980. ieee80211_sched_scan_stopped(mvm->hw);
  981. else
  982. ieee80211_scan_completed(mvm->hw, true);
  983. }
  984. return 0;
  985. }
  986. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  987. struct iwl_scan_req_tx_cmd *tx_cmd,
  988. bool no_cck)
  989. {
  990. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  991. TX_CMD_FLG_BT_DIS);
  992. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  993. IEEE80211_BAND_2GHZ,
  994. no_cck);
  995. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  996. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  997. TX_CMD_FLG_BT_DIS);
  998. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  999. IEEE80211_BAND_5GHZ,
  1000. no_cck);
  1001. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  1002. }
  1003. static void
  1004. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  1005. struct ieee80211_channel **channels,
  1006. int n_channels, u32 ssid_bitmap,
  1007. struct iwl_scan_req_unified_lmac *cmd)
  1008. {
  1009. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  1010. int i;
  1011. for (i = 0; i < n_channels; i++) {
  1012. channel_cfg[i].channel_num =
  1013. cpu_to_le16(channels[i]->hw_value);
  1014. channel_cfg[i].iter_count = cpu_to_le16(1);
  1015. channel_cfg[i].iter_interval = 0;
  1016. channel_cfg[i].flags =
  1017. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  1018. ssid_bitmap);
  1019. }
  1020. }
  1021. static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
  1022. size_t len, u8 *const pos)
  1023. {
  1024. static const u8 before_ds_params[] = {
  1025. WLAN_EID_SSID,
  1026. WLAN_EID_SUPP_RATES,
  1027. WLAN_EID_REQUEST,
  1028. WLAN_EID_EXT_SUPP_RATES,
  1029. };
  1030. size_t offs;
  1031. u8 *newpos = pos;
  1032. if (!iwl_mvm_rrm_scan_needed(mvm)) {
  1033. memcpy(newpos, ies, len);
  1034. return newpos + len;
  1035. }
  1036. offs = ieee80211_ie_split(ies, len,
  1037. before_ds_params,
  1038. ARRAY_SIZE(before_ds_params),
  1039. 0);
  1040. memcpy(newpos, ies, offs);
  1041. newpos += offs;
  1042. /* Add a placeholder for DS Parameter Set element */
  1043. *newpos++ = WLAN_EID_DS_PARAMS;
  1044. *newpos++ = 1;
  1045. *newpos++ = 0;
  1046. memcpy(newpos, ies + offs, len - offs);
  1047. newpos += len - offs;
  1048. return newpos;
  1049. }
  1050. static void
  1051. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1052. struct ieee80211_scan_ies *ies,
  1053. struct iwl_scan_probe_req *preq,
  1054. const u8 *mac_addr, const u8 *mac_addr_mask)
  1055. {
  1056. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  1057. u8 *pos, *newpos;
  1058. /*
  1059. * Unfortunately, right now the offload scan doesn't support randomising
  1060. * within the firmware, so until the firmware API is ready we implement
  1061. * it in the driver. This means that the scan iterations won't really be
  1062. * random, only when it's restarted, but at least that helps a bit.
  1063. */
  1064. if (mac_addr)
  1065. get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
  1066. else
  1067. memcpy(frame->sa, vif->addr, ETH_ALEN);
  1068. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1069. eth_broadcast_addr(frame->da);
  1070. eth_broadcast_addr(frame->bssid);
  1071. frame->seq_ctrl = 0;
  1072. pos = frame->u.probe_req.variable;
  1073. *pos++ = WLAN_EID_SSID;
  1074. *pos++ = 0;
  1075. preq->mac_header.offset = 0;
  1076. preq->mac_header.len = cpu_to_le16(24 + 2);
  1077. /* Insert ds parameter set element on 2.4 GHz band */
  1078. newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
  1079. ies->ies[IEEE80211_BAND_2GHZ],
  1080. ies->len[IEEE80211_BAND_2GHZ],
  1081. pos);
  1082. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  1083. preq->band_data[0].len = cpu_to_le16(newpos - pos);
  1084. pos = newpos;
  1085. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  1086. ies->len[IEEE80211_BAND_5GHZ]);
  1087. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  1088. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  1089. pos += ies->len[IEEE80211_BAND_5GHZ];
  1090. memcpy(pos, ies->common_ies, ies->common_ie_len);
  1091. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  1092. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  1093. }
  1094. static void
  1095. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  1096. struct iwl_scan_req_unified_lmac *cmd,
  1097. struct iwl_mvm_scan_params *params)
  1098. {
  1099. memset(cmd, 0, ksize(cmd));
  1100. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1101. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1102. if (params->passive_fragmented)
  1103. cmd->fragmented_dwell =
  1104. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1105. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  1106. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1107. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1108. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1109. cmd->iter_num = cpu_to_le32(1);
  1110. if (iwl_mvm_rrm_scan_needed(mvm))
  1111. cmd->scan_flags |=
  1112. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  1113. }
  1114. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  1115. struct ieee80211_vif *vif,
  1116. struct ieee80211_scan_request *req)
  1117. {
  1118. struct iwl_host_cmd hcmd = {
  1119. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1120. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1121. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1122. mvm->fw->ucode_capa.n_scan_channels +
  1123. sizeof(struct iwl_scan_probe_req), },
  1124. .data = { mvm->scan_cmd, },
  1125. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1126. };
  1127. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1128. struct iwl_scan_probe_req *preq;
  1129. struct iwl_mvm_scan_params params = {};
  1130. u32 flags;
  1131. u32 ssid_bitmap = 0;
  1132. int ret, i;
  1133. lockdep_assert_held(&mvm->mutex);
  1134. /* we should have failed registration if scan_cmd was NULL */
  1135. if (WARN_ON(mvm->scan_cmd == NULL))
  1136. return -ENOMEM;
  1137. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  1138. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  1139. req->ies.len[NL80211_BAND_5GHZ] >
  1140. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  1141. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1142. return -ENOBUFS;
  1143. mvm->scan_status = IWL_MVM_SCAN_OS;
  1144. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1145. &params);
  1146. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1147. cmd->n_channels = (u8)req->req.n_channels;
  1148. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1149. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1150. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1151. if (params.passive_fragmented)
  1152. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1153. if (req->req.n_ssids == 0)
  1154. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1155. cmd->scan_flags |= cpu_to_le32(flags);
  1156. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1157. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1158. MAC_FILTER_IN_BEACON);
  1159. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1160. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1161. req->req.n_ssids, 0);
  1162. cmd->schedule[0].delay = 0;
  1163. cmd->schedule[0].iterations = 1;
  1164. cmd->schedule[0].full_scan_mul = 0;
  1165. cmd->schedule[1].delay = 0;
  1166. cmd->schedule[1].iterations = 0;
  1167. cmd->schedule[1].full_scan_mul = 0;
  1168. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
  1169. mvm->last_ebs_successful) {
  1170. cmd->channel_opt[0].flags =
  1171. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1172. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1173. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1174. cmd->channel_opt[0].non_ebs_ratio =
  1175. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  1176. cmd->channel_opt[1].flags =
  1177. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1178. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1179. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1180. cmd->channel_opt[1].non_ebs_ratio =
  1181. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  1182. }
  1183. for (i = 1; i <= req->req.n_ssids; i++)
  1184. ssid_bitmap |= BIT(i);
  1185. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1186. req->req.n_channels, ssid_bitmap,
  1187. cmd);
  1188. preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  1189. mvm->fw->ucode_capa.n_scan_channels);
  1190. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
  1191. req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1192. req->req.mac_addr : NULL,
  1193. req->req.mac_addr_mask);
  1194. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1195. if (!ret) {
  1196. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1197. } else {
  1198. /*
  1199. * If the scan failed, it usually means that the FW was unable
  1200. * to allocate the time events. Warn on it, but maybe we
  1201. * should try to send the command again with different params.
  1202. */
  1203. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1204. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1205. ret = -EIO;
  1206. }
  1207. return ret;
  1208. }
  1209. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1210. struct ieee80211_vif *vif,
  1211. struct cfg80211_sched_scan_request *req,
  1212. struct ieee80211_scan_ies *ies)
  1213. {
  1214. struct iwl_host_cmd hcmd = {
  1215. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1216. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1217. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1218. mvm->fw->ucode_capa.n_scan_channels +
  1219. sizeof(struct iwl_scan_probe_req), },
  1220. .data = { mvm->scan_cmd, },
  1221. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1222. };
  1223. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1224. struct iwl_scan_probe_req *preq;
  1225. struct iwl_mvm_scan_params params = {};
  1226. int ret;
  1227. u32 flags = 0, ssid_bitmap = 0;
  1228. lockdep_assert_held(&mvm->mutex);
  1229. /* we should have failed registration if scan_cmd was NULL */
  1230. if (WARN_ON(mvm->scan_cmd == NULL))
  1231. return -ENOMEM;
  1232. if (req->n_ssids > PROBE_OPTION_MAX ||
  1233. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1234. ies->len[NL80211_BAND_5GHZ] >
  1235. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  1236. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1237. return -ENOBUFS;
  1238. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1239. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1240. cmd->n_channels = (u8)req->n_channels;
  1241. if (iwl_mvm_scan_pass_all(mvm, req))
  1242. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1243. else
  1244. flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
  1245. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1246. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1247. if (params.passive_fragmented)
  1248. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1249. if (req->n_ssids == 0)
  1250. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1251. cmd->scan_flags |= cpu_to_le32(flags);
  1252. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1253. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1254. MAC_FILTER_IN_BEACON);
  1255. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1256. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1257. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1258. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1259. cmd->schedule[0].full_scan_mul = 1;
  1260. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1261. cmd->schedule[1].iterations = 0xff;
  1262. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1263. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1264. mvm->last_ebs_successful) {
  1265. cmd->channel_opt[0].flags =
  1266. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1267. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1268. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1269. cmd->channel_opt[0].non_ebs_ratio =
  1270. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  1271. cmd->channel_opt[1].flags =
  1272. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1273. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1274. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1275. cmd->channel_opt[1].non_ebs_ratio =
  1276. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  1277. }
  1278. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1279. ssid_bitmap, cmd);
  1280. preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  1281. mvm->fw->ucode_capa.n_scan_channels);
  1282. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
  1283. req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1284. req->mac_addr : NULL,
  1285. req->mac_addr_mask);
  1286. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1287. if (!ret) {
  1288. IWL_DEBUG_SCAN(mvm,
  1289. "Sched scan request was sent successfully\n");
  1290. } else {
  1291. /*
  1292. * If the scan failed, it usually means that the FW was unable
  1293. * to allocate the time events. Warn on it, but maybe we
  1294. * should try to send the command again with different params.
  1295. */
  1296. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1297. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1298. ret = -EIO;
  1299. }
  1300. return ret;
  1301. }
  1302. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  1303. {
  1304. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  1305. return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
  1306. true);
  1307. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  1308. return 0;
  1309. if (iwl_mvm_is_radio_killed(mvm)) {
  1310. ieee80211_scan_completed(mvm->hw, true);
  1311. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1312. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1313. return 0;
  1314. }
  1315. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1316. return iwl_mvm_scan_offload_stop(mvm, true);
  1317. return iwl_mvm_cancel_regular_scan(mvm);
  1318. }
  1319. /* UMAC scan API */
  1320. struct iwl_umac_scan_done {
  1321. struct iwl_mvm *mvm;
  1322. enum iwl_umac_scan_uid_type type;
  1323. };
  1324. static int rate_to_scan_rate_flag(unsigned int rate)
  1325. {
  1326. static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
  1327. [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
  1328. [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
  1329. [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
  1330. [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
  1331. [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
  1332. [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
  1333. [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
  1334. [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
  1335. [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
  1336. [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
  1337. [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
  1338. [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
  1339. };
  1340. return rate_to_scan_rate[rate];
  1341. }
  1342. static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
  1343. {
  1344. struct ieee80211_supported_band *band;
  1345. unsigned int rates = 0;
  1346. int i;
  1347. band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  1348. for (i = 0; i < band->n_bitrates; i++)
  1349. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  1350. band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  1351. for (i = 0; i < band->n_bitrates; i++)
  1352. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  1353. /* Set both basic rates and supported rates */
  1354. rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
  1355. return cpu_to_le32(rates);
  1356. }
  1357. int iwl_mvm_config_scan(struct iwl_mvm *mvm)
  1358. {
  1359. struct iwl_scan_config *scan_config;
  1360. struct ieee80211_supported_band *band;
  1361. int num_channels =
  1362. mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
  1363. mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  1364. int ret, i, j = 0, cmd_size, data_size;
  1365. struct iwl_host_cmd cmd = {
  1366. .id = SCAN_CFG_CMD,
  1367. };
  1368. if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
  1369. return -ENOBUFS;
  1370. cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
  1371. scan_config = kzalloc(cmd_size, GFP_KERNEL);
  1372. if (!scan_config)
  1373. return -ENOMEM;
  1374. data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
  1375. scan_config->hdr.size = cpu_to_le16(data_size);
  1376. scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
  1377. SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
  1378. SCAN_CONFIG_FLAG_SET_TX_CHAINS |
  1379. SCAN_CONFIG_FLAG_SET_RX_CHAINS |
  1380. SCAN_CONFIG_FLAG_SET_ALL_TIMES |
  1381. SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
  1382. SCAN_CONFIG_FLAG_SET_MAC_ADDR |
  1383. SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
  1384. SCAN_CONFIG_N_CHANNELS(num_channels));
  1385. scan_config->tx_chains = cpu_to_le32(mvm->fw->valid_tx_ant);
  1386. scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
  1387. scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
  1388. scan_config->out_of_channel_time = cpu_to_le32(170);
  1389. scan_config->suspend_time = cpu_to_le32(30);
  1390. scan_config->dwell_active = 20;
  1391. scan_config->dwell_passive = 110;
  1392. scan_config->dwell_fragmented = 20;
  1393. memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
  1394. scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
  1395. scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
  1396. IWL_CHANNEL_FLAG_ACCURATE_EBS |
  1397. IWL_CHANNEL_FLAG_EBS_ADD |
  1398. IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
  1399. band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  1400. for (i = 0; i < band->n_channels; i++, j++)
  1401. scan_config->channel_array[j] = band->channels[i].center_freq;
  1402. band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  1403. for (i = 0; i < band->n_channels; i++, j++)
  1404. scan_config->channel_array[j] = band->channels[i].center_freq;
  1405. cmd.data[0] = scan_config;
  1406. cmd.len[0] = cmd_size;
  1407. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  1408. IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
  1409. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1410. kfree(scan_config);
  1411. return ret;
  1412. }
  1413. static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
  1414. {
  1415. int i;
  1416. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  1417. if (mvm->scan_uid[i] == uid)
  1418. return i;
  1419. return i;
  1420. }
  1421. static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
  1422. {
  1423. return iwl_mvm_find_scan_uid(mvm, 0);
  1424. }
  1425. static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
  1426. enum iwl_umac_scan_uid_type type)
  1427. {
  1428. int i;
  1429. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  1430. if (mvm->scan_uid[i] & type)
  1431. return true;
  1432. return false;
  1433. }
  1434. static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
  1435. enum iwl_umac_scan_uid_type type)
  1436. {
  1437. u32 uid;
  1438. /* make sure exactly one bit is on in scan type */
  1439. WARN_ON(hweight8(type) != 1);
  1440. /*
  1441. * Make sure scan uids are unique. If one scan lasts long time while
  1442. * others are completing frequently, the seq number will wrap up and
  1443. * we may have more than one scan with the same uid.
  1444. */
  1445. do {
  1446. uid = type | (mvm->scan_seq_num <<
  1447. IWL_UMAC_SCAN_UID_SEQ_OFFSET);
  1448. mvm->scan_seq_num++;
  1449. } while (iwl_mvm_find_scan_uid(mvm, uid) <
  1450. IWL_MVM_MAX_SIMULTANEOUS_SCANS);
  1451. IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
  1452. return uid;
  1453. }
  1454. static void
  1455. iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
  1456. struct iwl_scan_req_umac *cmd,
  1457. struct iwl_mvm_scan_params *params)
  1458. {
  1459. memset(cmd, 0, ksize(cmd));
  1460. cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
  1461. sizeof(struct iwl_mvm_umac_cmd_hdr));
  1462. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1463. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1464. if (params->passive_fragmented)
  1465. cmd->fragmented_dwell =
  1466. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1467. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1468. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1469. cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1470. }
  1471. static void
  1472. iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
  1473. struct ieee80211_channel **channels,
  1474. int n_channels, u32 ssid_bitmap,
  1475. struct iwl_scan_req_umac *cmd)
  1476. {
  1477. struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
  1478. int i;
  1479. for (i = 0; i < n_channels; i++) {
  1480. channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
  1481. channel_cfg[i].channel_num = channels[i]->hw_value;
  1482. channel_cfg[i].iter_count = 1;
  1483. channel_cfg[i].iter_interval = 0;
  1484. }
  1485. }
  1486. static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
  1487. struct cfg80211_ssid *ssids,
  1488. int fragmented)
  1489. {
  1490. int flags = 0;
  1491. if (n_ssids == 0)
  1492. flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
  1493. if (n_ssids == 1 && ssids[0].ssid_len != 0)
  1494. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
  1495. if (fragmented)
  1496. flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
  1497. if (iwl_mvm_rrm_scan_needed(mvm))
  1498. flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
  1499. return flags;
  1500. }
  1501. int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1502. struct ieee80211_scan_request *req)
  1503. {
  1504. struct iwl_host_cmd hcmd = {
  1505. .id = SCAN_REQ_UMAC,
  1506. .len = { iwl_mvm_scan_size(mvm), },
  1507. .data = { mvm->scan_cmd, },
  1508. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1509. };
  1510. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1511. struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
  1512. sizeof(struct iwl_scan_channel_cfg_umac) *
  1513. mvm->fw->ucode_capa.n_scan_channels;
  1514. struct iwl_mvm_scan_params params = {};
  1515. u32 uid, flags;
  1516. u32 ssid_bitmap = 0;
  1517. int ret, i, uid_idx;
  1518. lockdep_assert_held(&mvm->mutex);
  1519. uid_idx = iwl_mvm_find_free_scan_uid(mvm);
  1520. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1521. return -EBUSY;
  1522. /* we should have failed registration if scan_cmd was NULL */
  1523. if (WARN_ON(mvm->scan_cmd == NULL))
  1524. return -ENOMEM;
  1525. if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
  1526. req->ies.common_ie_len +
  1527. req->ies.len[NL80211_BAND_2GHZ] +
  1528. req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
  1529. SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
  1530. mvm->fw->ucode_capa.n_scan_channels))
  1531. return -ENOBUFS;
  1532. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1533. &params);
  1534. iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
  1535. uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
  1536. mvm->scan_uid[uid_idx] = uid;
  1537. cmd->uid = cpu_to_le32(uid);
  1538. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1539. flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
  1540. req->req.ssids,
  1541. params.passive_fragmented);
  1542. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1543. cmd->general_flags = cpu_to_le32(flags);
  1544. cmd->n_channels = req->req.n_channels;
  1545. for (i = 0; i < req->req.n_ssids; i++)
  1546. ssid_bitmap |= BIT(i);
  1547. iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
  1548. req->req.n_channels, ssid_bitmap, cmd);
  1549. sec_part->schedule[0].iter_count = 1;
  1550. sec_part->delay = 0;
  1551. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
  1552. req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1553. req->req.mac_addr : NULL,
  1554. req->req.mac_addr_mask);
  1555. iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
  1556. req->req.n_ssids, 0);
  1557. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1558. if (!ret) {
  1559. IWL_DEBUG_SCAN(mvm,
  1560. "Scan request was sent successfully\n");
  1561. } else {
  1562. /*
  1563. * If the scan failed, it usually means that the FW was unable
  1564. * to allocate the time events. Warn on it, but maybe we
  1565. * should try to send the command again with different params.
  1566. */
  1567. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1568. }
  1569. return ret;
  1570. }
  1571. int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1572. struct cfg80211_sched_scan_request *req,
  1573. struct ieee80211_scan_ies *ies)
  1574. {
  1575. struct iwl_host_cmd hcmd = {
  1576. .id = SCAN_REQ_UMAC,
  1577. .len = { iwl_mvm_scan_size(mvm), },
  1578. .data = { mvm->scan_cmd, },
  1579. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1580. };
  1581. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1582. struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
  1583. sizeof(struct iwl_scan_channel_cfg_umac) *
  1584. mvm->fw->ucode_capa.n_scan_channels;
  1585. struct iwl_mvm_scan_params params = {};
  1586. u32 uid, flags;
  1587. u32 ssid_bitmap = 0;
  1588. int ret, uid_idx;
  1589. lockdep_assert_held(&mvm->mutex);
  1590. uid_idx = iwl_mvm_find_free_scan_uid(mvm);
  1591. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1592. return -EBUSY;
  1593. /* we should have failed registration if scan_cmd was NULL */
  1594. if (WARN_ON(mvm->scan_cmd == NULL))
  1595. return -ENOMEM;
  1596. if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
  1597. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1598. ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
  1599. SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
  1600. mvm->fw->ucode_capa.n_scan_channels))
  1601. return -ENOBUFS;
  1602. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
  1603. &params);
  1604. iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
  1605. cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
  1606. uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1607. mvm->scan_uid[uid_idx] = uid;
  1608. cmd->uid = cpu_to_le32(uid);
  1609. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
  1610. flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
  1611. params.passive_fragmented);
  1612. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
  1613. if (iwl_mvm_scan_pass_all(mvm, req))
  1614. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1615. else
  1616. flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
  1617. cmd->general_flags = cpu_to_le32(flags);
  1618. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1619. mvm->last_ebs_successful)
  1620. cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
  1621. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1622. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
  1623. cmd->n_channels = req->n_channels;
  1624. iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
  1625. false);
  1626. /* This API uses bits 0-19 instead of 1-20. */
  1627. ssid_bitmap = ssid_bitmap >> 1;
  1628. iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1629. ssid_bitmap, cmd);
  1630. sec_part->schedule[0].interval =
  1631. cpu_to_le16(req->interval / MSEC_PER_SEC);
  1632. sec_part->schedule[0].iter_count = 0xff;
  1633. sec_part->delay = 0;
  1634. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
  1635. req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1636. req->mac_addr : NULL,
  1637. req->mac_addr_mask);
  1638. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1639. if (!ret) {
  1640. IWL_DEBUG_SCAN(mvm,
  1641. "Sched scan request was sent successfully\n");
  1642. } else {
  1643. /*
  1644. * If the scan failed, it usually means that the FW was unable
  1645. * to allocate the time events. Warn on it, but maybe we
  1646. * should try to send the command again with different params.
  1647. */
  1648. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1649. }
  1650. return ret;
  1651. }
  1652. int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
  1653. struct iwl_rx_cmd_buffer *rxb,
  1654. struct iwl_device_cmd *cmd)
  1655. {
  1656. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1657. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1658. u32 uid = __le32_to_cpu(notif->uid);
  1659. bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1660. int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
  1661. /*
  1662. * Scan uid may be set to zero in case of scan abort request from above.
  1663. */
  1664. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1665. return 0;
  1666. IWL_DEBUG_SCAN(mvm,
  1667. "Scan completed, uid %u type %s, status %s, EBS status %s\n",
  1668. uid, sched ? "sched" : "regular",
  1669. notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  1670. "completed" : "aborted",
  1671. notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
  1672. "success" : "failed");
  1673. mvm->last_ebs_successful = !notif->ebs_status;
  1674. mvm->scan_uid[uid_idx] = 0;
  1675. if (!sched) {
  1676. ieee80211_scan_completed(mvm->hw,
  1677. notif->status ==
  1678. IWL_SCAN_OFFLOAD_ABORTED);
  1679. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1680. } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
  1681. ieee80211_sched_scan_stopped(mvm->hw);
  1682. } else {
  1683. IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
  1684. }
  1685. return 0;
  1686. }
  1687. static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
  1688. struct iwl_rx_packet *pkt, void *data)
  1689. {
  1690. struct iwl_umac_scan_done *scan_done = data;
  1691. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1692. u32 uid = __le32_to_cpu(notif->uid);
  1693. int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
  1694. if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
  1695. return false;
  1696. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1697. return false;
  1698. /*
  1699. * Clear scan uid of scans that was aborted from above and completed
  1700. * in FW so the RX handler does nothing.
  1701. */
  1702. scan_done->mvm->scan_uid[uid_idx] = 0;
  1703. return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
  1704. }
  1705. static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
  1706. {
  1707. struct iwl_umac_scan_abort cmd = {
  1708. .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
  1709. sizeof(struct iwl_mvm_umac_cmd_hdr)),
  1710. .uid = cpu_to_le32(uid),
  1711. };
  1712. lockdep_assert_held(&mvm->mutex);
  1713. IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
  1714. return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
  1715. }
  1716. static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
  1717. enum iwl_umac_scan_uid_type type, bool notify)
  1718. {
  1719. struct iwl_notification_wait wait_scan_done;
  1720. static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
  1721. struct iwl_umac_scan_done scan_done = {
  1722. .mvm = mvm,
  1723. .type = type,
  1724. };
  1725. int i, ret = -EIO;
  1726. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  1727. scan_done_notif,
  1728. ARRAY_SIZE(scan_done_notif),
  1729. iwl_scan_umac_done_check, &scan_done);
  1730. IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
  1731. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
  1732. if (mvm->scan_uid[i] & type) {
  1733. int err;
  1734. if (iwl_mvm_is_radio_killed(mvm) &&
  1735. (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
  1736. ieee80211_scan_completed(mvm->hw, true);
  1737. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1738. break;
  1739. }
  1740. err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
  1741. if (!err)
  1742. ret = 0;
  1743. }
  1744. }
  1745. if (ret) {
  1746. IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
  1747. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  1748. return ret;
  1749. }
  1750. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  1751. if (ret)
  1752. return ret;
  1753. if (notify) {
  1754. if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
  1755. ieee80211_sched_scan_stopped(mvm->hw);
  1756. if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
  1757. ieee80211_scan_completed(mvm->hw, true);
  1758. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1759. }
  1760. }
  1761. return ret;
  1762. }
  1763. int iwl_mvm_scan_size(struct iwl_mvm *mvm)
  1764. {
  1765. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  1766. return sizeof(struct iwl_scan_req_umac) +
  1767. sizeof(struct iwl_scan_channel_cfg_umac) *
  1768. mvm->fw->ucode_capa.n_scan_channels +
  1769. sizeof(struct iwl_scan_req_umac_tail);
  1770. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1771. return sizeof(struct iwl_scan_req_unified_lmac) +
  1772. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1773. mvm->fw->ucode_capa.n_scan_channels +
  1774. sizeof(struct iwl_scan_probe_req);
  1775. return sizeof(struct iwl_scan_cmd) +
  1776. mvm->fw->ucode_capa.max_probe_length +
  1777. mvm->fw->ucode_capa.n_scan_channels *
  1778. sizeof(struct iwl_scan_channel);
  1779. }