scan.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  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 iwl_mvm_get_valid_rx_ant(mvm);
  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, iwl_mvm_get_valid_tx_ant(mvm),
  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. int *global_cnt = data;
  261. if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
  262. mvmvif->phy_ctxt->id < MAX_PHYS)
  263. *global_cnt += 1;
  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. int global_cnt = 0;
  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_cnt);
  277. if (!global_cnt)
  278. goto not_bound;
  279. params->suspend_time = 30;
  280. params->max_out_time = 120;
  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. /*
  286. * If there is more than one active interface make
  287. * passive scan more fragmented.
  288. */
  289. frag_passive_dwell = (global_cnt < 2) ? 40 : 20;
  290. params->max_out_time = frag_passive_dwell;
  291. } else {
  292. params->suspend_time = 120;
  293. params->max_out_time = 120;
  294. }
  295. }
  296. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  297. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  298. /*
  299. * P2P device scan should not be fragmented to avoid negative
  300. * impact on P2P device discovery. Configure max_out_time to be
  301. * equal to dwell time on passive channel. Take a longest
  302. * possible value, one that corresponds to 2GHz band
  303. */
  304. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  305. u32 passive_dwell =
  306. iwl_mvm_get_passive_dwell(mvm,
  307. IEEE80211_BAND_2GHZ);
  308. params->max_out_time = passive_dwell;
  309. } else {
  310. params->passive_fragmented = true;
  311. }
  312. }
  313. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  314. params->max_out_time = 200;
  315. not_bound:
  316. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  317. if (params->passive_fragmented)
  318. params->dwell[band].passive = frag_passive_dwell;
  319. else
  320. params->dwell[band].passive =
  321. iwl_mvm_get_passive_dwell(mvm, band);
  322. params->dwell[band].active = iwl_mvm_get_active_dwell(mvm, band,
  323. n_ssids);
  324. }
  325. }
  326. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  327. {
  328. /* require rrm scan whenever the fw supports it */
  329. return mvm->fw->ucode_capa.capa[0] &
  330. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
  331. }
  332. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
  333. bool is_sched_scan)
  334. {
  335. int max_probe_len;
  336. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  337. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  338. else
  339. max_probe_len = mvm->fw->ucode_capa.max_probe_length;
  340. /* we create the 802.11 header and SSID element */
  341. max_probe_len -= 24 + 2;
  342. /* basic ssid is added only for hw_scan with and old api */
  343. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
  344. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
  345. !is_sched_scan)
  346. max_probe_len -= 32;
  347. /* DS parameter set element is added on 2.4GHZ band if required */
  348. if (iwl_mvm_rrm_scan_needed(mvm))
  349. max_probe_len -= 3;
  350. return max_probe_len;
  351. }
  352. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
  353. {
  354. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
  355. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
  356. return max_ie_len;
  357. /* TODO: [BUG] This function should return the maximum allowed size of
  358. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  359. * in the same command. So the correct implementation of this function
  360. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  361. * command has only 512 bytes and it would leave us with about 240
  362. * bytes for scan IEs, which is clearly not enough. So meanwhile
  363. * we will report an incorrect value. This may result in a failure to
  364. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  365. * functions with -ENOBUFS, if a large enough probe will be provided.
  366. */
  367. return max_ie_len;
  368. }
  369. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  370. struct ieee80211_vif *vif,
  371. struct cfg80211_scan_request *req)
  372. {
  373. struct iwl_host_cmd hcmd = {
  374. .id = SCAN_REQUEST_CMD,
  375. .len = { 0, },
  376. .data = { mvm->scan_cmd, },
  377. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  378. };
  379. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  380. int ret;
  381. u32 status;
  382. int ssid_len = 0;
  383. u8 *ssid = NULL;
  384. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  385. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  386. struct iwl_mvm_scan_params params = {};
  387. lockdep_assert_held(&mvm->mutex);
  388. /* we should have failed registration if scan_cmd was NULL */
  389. if (WARN_ON(mvm->scan_cmd == NULL))
  390. return -ENOMEM;
  391. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  392. mvm->scan_status = IWL_MVM_SCAN_OS;
  393. memset(cmd, 0, ksize(cmd));
  394. cmd->channel_count = (u8)req->n_channels;
  395. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  396. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  397. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  398. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
  399. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  400. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  401. if (params.passive_fragmented)
  402. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  403. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  404. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  405. MAC_FILTER_IN_BEACON);
  406. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  407. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  408. else
  409. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  410. cmd->repeats = cpu_to_le32(1);
  411. /*
  412. * If the user asked for passive scan, don't change to active scan if
  413. * you see any activity on the channel - remain passive.
  414. */
  415. if (req->n_ssids > 0) {
  416. cmd->passive2active = cpu_to_le16(1);
  417. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  418. if (basic_ssid) {
  419. ssid = req->ssids[0].ssid;
  420. ssid_len = req->ssids[0].ssid_len;
  421. }
  422. } else {
  423. cmd->passive2active = 0;
  424. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  425. }
  426. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
  427. basic_ssid ? 1 : 0);
  428. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  429. 3 << TX_CMD_FLG_BT_PRIO_POS);
  430. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  431. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  432. cmd->tx_cmd.rate_n_flags =
  433. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  434. req->no_cck);
  435. cmd->tx_cmd.len =
  436. cpu_to_le16(iwl_mvm_fill_probe_req(
  437. (struct ieee80211_mgmt *)cmd->data,
  438. vif->addr,
  439. req->n_ssids, ssid, ssid_len,
  440. req->ie, req->ie_len, NULL, 0,
  441. mvm->fw->ucode_capa.max_probe_length));
  442. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  443. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  444. le16_to_cpu(cmd->tx_cmd.len) +
  445. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  446. hcmd.len[0] = le16_to_cpu(cmd->len);
  447. status = SCAN_RESPONSE_OK;
  448. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  449. if (!ret && status == SCAN_RESPONSE_OK) {
  450. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  451. } else {
  452. /*
  453. * If the scan failed, it usually means that the FW was unable
  454. * to allocate the time events. Warn on it, but maybe we
  455. * should try to send the command again with different params.
  456. */
  457. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  458. status, ret);
  459. mvm->scan_status = IWL_MVM_SCAN_NONE;
  460. ret = -EIO;
  461. }
  462. return ret;
  463. }
  464. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  465. struct iwl_device_cmd *cmd)
  466. {
  467. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  468. struct iwl_cmd_response *resp = (void *)pkt->data;
  469. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  470. le32_to_cpu(resp->status));
  471. return 0;
  472. }
  473. int iwl_mvm_rx_scan_offload_iter_complete_notif(struct iwl_mvm *mvm,
  474. struct iwl_rx_cmd_buffer *rxb,
  475. struct iwl_device_cmd *cmd)
  476. {
  477. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  478. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  479. IWL_DEBUG_SCAN(mvm,
  480. "Scan offload iteration complete: status=0x%x scanned channels=%d\n",
  481. notif->status, notif->scanned_channels);
  482. return 0;
  483. }
  484. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  485. struct iwl_device_cmd *cmd)
  486. {
  487. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  488. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  489. lockdep_assert_held(&mvm->mutex);
  490. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  491. notif->status, notif->scanned_channels);
  492. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  493. mvm->scan_status = IWL_MVM_SCAN_NONE;
  494. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  495. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  496. return 0;
  497. }
  498. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  499. struct iwl_rx_cmd_buffer *rxb,
  500. struct iwl_device_cmd *cmd)
  501. {
  502. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  503. if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) &&
  504. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  505. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  506. if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN))
  507. return 0;
  508. }
  509. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  510. ieee80211_sched_scan_results(mvm->hw);
  511. return 0;
  512. }
  513. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  514. struct iwl_rx_packet *pkt, void *data)
  515. {
  516. struct iwl_mvm *mvm =
  517. container_of(notif_wait, struct iwl_mvm, notif_wait);
  518. struct iwl_scan_complete_notif *notif;
  519. u32 *resp;
  520. switch (pkt->hdr.cmd) {
  521. case SCAN_ABORT_CMD:
  522. resp = (void *)pkt->data;
  523. if (*resp == CAN_ABORT_STATUS) {
  524. IWL_DEBUG_SCAN(mvm,
  525. "Scan can be aborted, wait until completion\n");
  526. return false;
  527. }
  528. /*
  529. * If scan cannot be aborted, it means that we had a
  530. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  531. * ieee80211_scan_completed already.
  532. */
  533. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  534. *resp);
  535. return true;
  536. case SCAN_COMPLETE_NOTIFICATION:
  537. notif = (void *)pkt->data;
  538. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  539. notif->status);
  540. return true;
  541. default:
  542. WARN_ON(1);
  543. return false;
  544. };
  545. }
  546. static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
  547. {
  548. struct iwl_notification_wait wait_scan_abort;
  549. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  550. SCAN_COMPLETE_NOTIFICATION };
  551. int ret;
  552. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  553. scan_abort_notif,
  554. ARRAY_SIZE(scan_abort_notif),
  555. iwl_mvm_scan_abort_notif, NULL);
  556. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  557. if (ret) {
  558. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  559. /* mac80211's state will be cleaned in the nic_restart flow */
  560. goto out_remove_notif;
  561. }
  562. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  563. out_remove_notif:
  564. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  565. return ret;
  566. }
  567. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  568. struct iwl_rx_cmd_buffer *rxb,
  569. struct iwl_device_cmd *cmd)
  570. {
  571. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  572. u8 status, ebs_status;
  573. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  574. struct iwl_periodic_scan_complete *scan_notif;
  575. scan_notif = (void *)pkt->data;
  576. status = scan_notif->status;
  577. ebs_status = scan_notif->ebs_status;
  578. } else {
  579. struct iwl_scan_offload_complete *scan_notif;
  580. scan_notif = (void *)pkt->data;
  581. status = scan_notif->status;
  582. ebs_status = scan_notif->ebs_status;
  583. }
  584. /* scan status must be locked for proper checking */
  585. lockdep_assert_held(&mvm->mutex);
  586. IWL_DEBUG_SCAN(mvm,
  587. "%s completed, status %s, EBS status %s\n",
  588. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  589. "Scheduled scan" : "Scan",
  590. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  591. "completed" : "aborted",
  592. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  593. "success" : "failed");
  594. /* only call mac80211 completion if the stop was initiated by FW */
  595. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  596. mvm->scan_status = IWL_MVM_SCAN_NONE;
  597. ieee80211_sched_scan_stopped(mvm->hw);
  598. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  599. mvm->scan_status = IWL_MVM_SCAN_NONE;
  600. ieee80211_scan_completed(mvm->hw,
  601. status == IWL_SCAN_OFFLOAD_ABORTED);
  602. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  603. }
  604. if (ebs_status)
  605. mvm->last_ebs_successful = false;
  606. return 0;
  607. }
  608. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  609. struct ieee80211_vif *vif,
  610. struct ieee80211_scan_ies *ies,
  611. enum ieee80211_band band,
  612. struct iwl_tx_cmd *cmd,
  613. u8 *data)
  614. {
  615. u16 cmd_len;
  616. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  617. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  618. cmd->sta_id = mvm->aux_sta.sta_id;
  619. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  620. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  621. vif->addr,
  622. 1, NULL, 0,
  623. ies->ies[band], ies->len[band],
  624. ies->common_ies, ies->common_ie_len,
  625. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  626. cmd->len = cpu_to_le16(cmd_len);
  627. }
  628. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  629. struct ieee80211_vif *vif,
  630. struct cfg80211_sched_scan_request *req,
  631. struct iwl_scan_offload_cmd *scan,
  632. struct iwl_mvm_scan_params *params)
  633. {
  634. scan->channel_count = req->n_channels;
  635. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  636. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  637. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  638. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  639. scan->max_out_time = cpu_to_le32(params->max_out_time);
  640. scan->suspend_time = cpu_to_le32(params->suspend_time);
  641. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  642. MAC_FILTER_IN_BEACON);
  643. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  644. scan->rep_count = cpu_to_le32(1);
  645. if (params->passive_fragmented)
  646. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  647. }
  648. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  649. {
  650. int i;
  651. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  652. if (!ssid_list[i].len)
  653. break;
  654. if (ssid_list[i].len == ssid_len &&
  655. !memcmp(ssid_list->ssid, ssid, ssid_len))
  656. return i;
  657. }
  658. return -1;
  659. }
  660. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  661. struct iwl_ssid_ie *direct_scan,
  662. u32 *ssid_bitmap, bool basic_ssid)
  663. {
  664. int i, j;
  665. int index;
  666. /*
  667. * copy SSIDs from match list.
  668. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  669. * config match list.
  670. */
  671. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  672. /* skip empty SSID matchsets */
  673. if (!req->match_sets[i].ssid.ssid_len)
  674. continue;
  675. direct_scan[i].id = WLAN_EID_SSID;
  676. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  677. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  678. direct_scan[i].len);
  679. }
  680. /* add SSIDs from scan SSID list */
  681. *ssid_bitmap = 0;
  682. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  683. index = iwl_ssid_exist(req->ssids[j].ssid,
  684. req->ssids[j].ssid_len,
  685. direct_scan);
  686. if (index < 0) {
  687. if (!req->ssids[j].ssid_len && basic_ssid)
  688. continue;
  689. direct_scan[i].id = WLAN_EID_SSID;
  690. direct_scan[i].len = req->ssids[j].ssid_len;
  691. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  692. direct_scan[i].len);
  693. *ssid_bitmap |= BIT(i + 1);
  694. i++;
  695. } else {
  696. *ssid_bitmap |= BIT(index + 1);
  697. }
  698. }
  699. }
  700. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  701. struct cfg80211_sched_scan_request *req,
  702. u8 *channels_buffer,
  703. enum ieee80211_band band,
  704. int *head,
  705. u32 ssid_bitmap,
  706. struct iwl_mvm_scan_params *params)
  707. {
  708. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  709. __le32 *type = (__le32 *)channels_buffer;
  710. __le16 *channel_number = (__le16 *)(type + n_channels);
  711. __le16 *iter_count = channel_number + n_channels;
  712. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  713. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  714. u8 *passive_dwell = active_dwell + n_channels;
  715. int i, index = 0;
  716. for (i = 0; i < req->n_channels; i++) {
  717. struct ieee80211_channel *chan = req->channels[i];
  718. if (chan->band != band)
  719. continue;
  720. index = *head;
  721. (*head)++;
  722. channel_number[index] = cpu_to_le16(chan->hw_value);
  723. active_dwell[index] = params->dwell[band].active;
  724. passive_dwell[index] = params->dwell[band].passive;
  725. iter_count[index] = cpu_to_le16(1);
  726. iter_interval[index] = 0;
  727. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  728. type[index] |=
  729. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  730. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  731. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  732. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  733. type[index] |=
  734. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  735. /* scan for all SSIDs from req->ssids */
  736. type[index] |= cpu_to_le32(ssid_bitmap);
  737. }
  738. }
  739. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  740. struct ieee80211_vif *vif,
  741. struct cfg80211_sched_scan_request *req,
  742. struct ieee80211_scan_ies *ies)
  743. {
  744. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  745. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  746. int head = 0;
  747. u32 ssid_bitmap;
  748. int cmd_len;
  749. int ret;
  750. u8 *probes;
  751. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  752. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  753. struct iwl_scan_offload_cfg *scan_cfg;
  754. struct iwl_host_cmd cmd = {
  755. .id = SCAN_OFFLOAD_CONFIG_CMD,
  756. };
  757. struct iwl_mvm_scan_params params = {};
  758. lockdep_assert_held(&mvm->mutex);
  759. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  760. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  761. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  762. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  763. if (!scan_cfg)
  764. return -ENOMEM;
  765. probes = scan_cfg->data +
  766. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  767. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  768. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  769. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  770. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  771. &ssid_bitmap, basic_ssid);
  772. /* build tx frames for supported bands */
  773. if (band_2ghz) {
  774. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  775. IEEE80211_BAND_2GHZ,
  776. &scan_cfg->scan_cmd.tx_cmd[0],
  777. probes);
  778. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  779. IEEE80211_BAND_2GHZ, &head,
  780. ssid_bitmap, &params);
  781. }
  782. if (band_5ghz) {
  783. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  784. IEEE80211_BAND_5GHZ,
  785. &scan_cfg->scan_cmd.tx_cmd[1],
  786. probes +
  787. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  788. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  789. IEEE80211_BAND_5GHZ, &head,
  790. ssid_bitmap, &params);
  791. }
  792. cmd.data[0] = scan_cfg;
  793. cmd.len[0] = cmd_len;
  794. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  795. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  796. ret = iwl_mvm_send_cmd(mvm, &cmd);
  797. kfree(scan_cfg);
  798. return ret;
  799. }
  800. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  801. struct cfg80211_sched_scan_request *req)
  802. {
  803. struct iwl_scan_offload_profile *profile;
  804. struct iwl_scan_offload_profile_cfg *profile_cfg;
  805. struct iwl_scan_offload_blacklist *blacklist;
  806. struct iwl_host_cmd cmd = {
  807. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  808. .len[1] = sizeof(*profile_cfg),
  809. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  810. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  811. };
  812. int blacklist_len;
  813. int i;
  814. int ret;
  815. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  816. return -EIO;
  817. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  818. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  819. else
  820. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  821. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  822. if (!blacklist)
  823. return -ENOMEM;
  824. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  825. if (!profile_cfg) {
  826. ret = -ENOMEM;
  827. goto free_blacklist;
  828. }
  829. cmd.data[0] = blacklist;
  830. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  831. cmd.data[1] = profile_cfg;
  832. /* No blacklist configuration */
  833. profile_cfg->num_profiles = req->n_match_sets;
  834. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  835. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  836. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  837. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  838. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  839. for (i = 0; i < req->n_match_sets; i++) {
  840. profile = &profile_cfg->profiles[i];
  841. profile->ssid_index = i;
  842. /* Support any cipher and auth algorithm */
  843. profile->unicast_cipher = 0xff;
  844. profile->auth_alg = 0xff;
  845. profile->network_type = IWL_NETWORK_TYPE_ANY;
  846. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  847. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  848. }
  849. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  850. ret = iwl_mvm_send_cmd(mvm, &cmd);
  851. kfree(profile_cfg);
  852. free_blacklist:
  853. kfree(blacklist);
  854. return ret;
  855. }
  856. static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
  857. struct cfg80211_sched_scan_request *req)
  858. {
  859. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  860. IWL_DEBUG_SCAN(mvm,
  861. "Sending scheduled scan with filtering, n_match_sets %d\n",
  862. req->n_match_sets);
  863. return false;
  864. }
  865. IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
  866. return true;
  867. }
  868. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  869. struct cfg80211_sched_scan_request *req)
  870. {
  871. struct iwl_scan_offload_req scan_req = {
  872. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  873. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  874. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  875. .schedule_line[0].full_scan_mul = 1,
  876. .schedule_line[1].iterations = 0xff,
  877. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  878. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  879. };
  880. if (iwl_mvm_scan_pass_all(mvm, req))
  881. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  882. if (mvm->last_ebs_successful &&
  883. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  884. scan_req.flags |=
  885. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  886. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  887. sizeof(scan_req), &scan_req);
  888. }
  889. int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
  890. struct ieee80211_vif *vif,
  891. struct cfg80211_sched_scan_request *req,
  892. struct ieee80211_scan_ies *ies)
  893. {
  894. int ret;
  895. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
  896. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  897. if (ret)
  898. return ret;
  899. ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
  900. } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  901. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  902. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  903. if (ret)
  904. return ret;
  905. ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
  906. } else {
  907. mvm->scan_status = IWL_MVM_SCAN_SCHED;
  908. ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
  909. if (ret)
  910. return ret;
  911. ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
  912. if (ret)
  913. return ret;
  914. ret = iwl_mvm_sched_scan_start(mvm, req);
  915. }
  916. return ret;
  917. }
  918. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  919. {
  920. int ret;
  921. struct iwl_host_cmd cmd = {
  922. .id = SCAN_OFFLOAD_ABORT_CMD,
  923. };
  924. u32 status;
  925. /* Exit instantly with error when device is not ready
  926. * to receive scan abort command or it does not perform
  927. * scheduled scan currently */
  928. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  929. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  930. mvm->scan_status != IWL_MVM_SCAN_OS))
  931. return -EIO;
  932. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  933. if (ret)
  934. return ret;
  935. if (status != CAN_ABORT_STATUS) {
  936. /*
  937. * The scan abort will return 1 for success or
  938. * 2 for "failure". A failure condition can be
  939. * due to simply not being in an active scan which
  940. * can occur if we send the scan abort before the
  941. * microcode has notified us that a scan is completed.
  942. */
  943. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  944. ret = -ENOENT;
  945. }
  946. return ret;
  947. }
  948. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  949. {
  950. int ret;
  951. struct iwl_notification_wait wait_scan_done;
  952. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  953. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  954. lockdep_assert_held(&mvm->mutex);
  955. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  956. return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
  957. notify);
  958. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  959. return 0;
  960. if (iwl_mvm_is_radio_killed(mvm))
  961. goto out;
  962. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  963. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  964. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  965. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  966. return 0;
  967. }
  968. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  969. scan_done_notif,
  970. ARRAY_SIZE(scan_done_notif),
  971. NULL, NULL);
  972. ret = iwl_mvm_send_scan_offload_abort(mvm);
  973. if (ret) {
  974. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  975. sched ? "offloaded " : "", ret);
  976. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  977. return ret;
  978. }
  979. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  980. sched ? "offloaded " : "");
  981. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  982. if (ret)
  983. return ret;
  984. /*
  985. * Clear the scan status so the next scan requests will succeed. This
  986. * also ensures the Rx handler doesn't do anything, as the scan was
  987. * stopped from above. Since the rx handler won't do anything now,
  988. * we have to release the scan reference here.
  989. */
  990. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  991. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  992. out:
  993. mvm->scan_status = IWL_MVM_SCAN_NONE;
  994. if (notify) {
  995. if (sched)
  996. ieee80211_sched_scan_stopped(mvm->hw);
  997. else
  998. ieee80211_scan_completed(mvm->hw, true);
  999. }
  1000. return 0;
  1001. }
  1002. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  1003. struct iwl_scan_req_tx_cmd *tx_cmd,
  1004. bool no_cck)
  1005. {
  1006. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  1007. TX_CMD_FLG_BT_DIS);
  1008. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  1009. IEEE80211_BAND_2GHZ,
  1010. no_cck);
  1011. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  1012. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  1013. TX_CMD_FLG_BT_DIS);
  1014. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  1015. IEEE80211_BAND_5GHZ,
  1016. no_cck);
  1017. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  1018. }
  1019. static void
  1020. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  1021. struct ieee80211_channel **channels,
  1022. int n_channels, u32 ssid_bitmap,
  1023. struct iwl_scan_req_unified_lmac *cmd)
  1024. {
  1025. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  1026. int i;
  1027. for (i = 0; i < n_channels; i++) {
  1028. channel_cfg[i].channel_num =
  1029. cpu_to_le16(channels[i]->hw_value);
  1030. channel_cfg[i].iter_count = cpu_to_le16(1);
  1031. channel_cfg[i].iter_interval = 0;
  1032. channel_cfg[i].flags =
  1033. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  1034. ssid_bitmap);
  1035. }
  1036. }
  1037. static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
  1038. size_t len, u8 *const pos)
  1039. {
  1040. static const u8 before_ds_params[] = {
  1041. WLAN_EID_SSID,
  1042. WLAN_EID_SUPP_RATES,
  1043. WLAN_EID_REQUEST,
  1044. WLAN_EID_EXT_SUPP_RATES,
  1045. };
  1046. size_t offs;
  1047. u8 *newpos = pos;
  1048. if (!iwl_mvm_rrm_scan_needed(mvm)) {
  1049. memcpy(newpos, ies, len);
  1050. return newpos + len;
  1051. }
  1052. offs = ieee80211_ie_split(ies, len,
  1053. before_ds_params,
  1054. ARRAY_SIZE(before_ds_params),
  1055. 0);
  1056. memcpy(newpos, ies, offs);
  1057. newpos += offs;
  1058. /* Add a placeholder for DS Parameter Set element */
  1059. *newpos++ = WLAN_EID_DS_PARAMS;
  1060. *newpos++ = 1;
  1061. *newpos++ = 0;
  1062. memcpy(newpos, ies + offs, len - offs);
  1063. newpos += len - offs;
  1064. return newpos;
  1065. }
  1066. static void
  1067. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1068. struct ieee80211_scan_ies *ies,
  1069. struct iwl_scan_probe_req *preq,
  1070. const u8 *mac_addr, const u8 *mac_addr_mask)
  1071. {
  1072. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  1073. u8 *pos, *newpos;
  1074. /*
  1075. * Unfortunately, right now the offload scan doesn't support randomising
  1076. * within the firmware, so until the firmware API is ready we implement
  1077. * it in the driver. This means that the scan iterations won't really be
  1078. * random, only when it's restarted, but at least that helps a bit.
  1079. */
  1080. if (mac_addr)
  1081. get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
  1082. else
  1083. memcpy(frame->sa, vif->addr, ETH_ALEN);
  1084. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1085. eth_broadcast_addr(frame->da);
  1086. eth_broadcast_addr(frame->bssid);
  1087. frame->seq_ctrl = 0;
  1088. pos = frame->u.probe_req.variable;
  1089. *pos++ = WLAN_EID_SSID;
  1090. *pos++ = 0;
  1091. preq->mac_header.offset = 0;
  1092. preq->mac_header.len = cpu_to_le16(24 + 2);
  1093. /* Insert ds parameter set element on 2.4 GHz band */
  1094. newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
  1095. ies->ies[IEEE80211_BAND_2GHZ],
  1096. ies->len[IEEE80211_BAND_2GHZ],
  1097. pos);
  1098. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  1099. preq->band_data[0].len = cpu_to_le16(newpos - pos);
  1100. pos = newpos;
  1101. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  1102. ies->len[IEEE80211_BAND_5GHZ]);
  1103. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  1104. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  1105. pos += ies->len[IEEE80211_BAND_5GHZ];
  1106. memcpy(pos, ies->common_ies, ies->common_ie_len);
  1107. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  1108. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  1109. }
  1110. static void
  1111. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  1112. struct iwl_scan_req_unified_lmac *cmd,
  1113. struct iwl_mvm_scan_params *params)
  1114. {
  1115. memset(cmd, 0, ksize(cmd));
  1116. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1117. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1118. if (params->passive_fragmented)
  1119. cmd->fragmented_dwell =
  1120. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1121. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  1122. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1123. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1124. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1125. cmd->iter_num = cpu_to_le32(1);
  1126. if (iwl_mvm_rrm_scan_needed(mvm))
  1127. cmd->scan_flags |=
  1128. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  1129. }
  1130. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  1131. struct ieee80211_vif *vif,
  1132. struct ieee80211_scan_request *req)
  1133. {
  1134. struct iwl_host_cmd hcmd = {
  1135. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1136. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1137. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1138. mvm->fw->ucode_capa.n_scan_channels +
  1139. sizeof(struct iwl_scan_probe_req), },
  1140. .data = { mvm->scan_cmd, },
  1141. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1142. };
  1143. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1144. struct iwl_scan_probe_req *preq;
  1145. struct iwl_mvm_scan_params params = {};
  1146. u32 flags;
  1147. u32 ssid_bitmap = 0;
  1148. int ret, i;
  1149. lockdep_assert_held(&mvm->mutex);
  1150. /* we should have failed registration if scan_cmd was NULL */
  1151. if (WARN_ON(mvm->scan_cmd == NULL))
  1152. return -ENOMEM;
  1153. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  1154. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  1155. req->ies.len[NL80211_BAND_5GHZ] >
  1156. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  1157. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1158. return -ENOBUFS;
  1159. mvm->scan_status = IWL_MVM_SCAN_OS;
  1160. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1161. &params);
  1162. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1163. cmd->n_channels = (u8)req->req.n_channels;
  1164. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1165. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1166. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1167. if (params.passive_fragmented)
  1168. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1169. if (req->req.n_ssids == 0)
  1170. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1171. cmd->scan_flags |= cpu_to_le32(flags);
  1172. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1173. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1174. MAC_FILTER_IN_BEACON);
  1175. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1176. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1177. req->req.n_ssids, 0);
  1178. cmd->schedule[0].delay = 0;
  1179. cmd->schedule[0].iterations = 1;
  1180. cmd->schedule[0].full_scan_mul = 0;
  1181. cmd->schedule[1].delay = 0;
  1182. cmd->schedule[1].iterations = 0;
  1183. cmd->schedule[1].full_scan_mul = 0;
  1184. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
  1185. mvm->last_ebs_successful) {
  1186. cmd->channel_opt[0].flags =
  1187. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1188. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1189. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1190. cmd->channel_opt[0].non_ebs_ratio =
  1191. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  1192. cmd->channel_opt[1].flags =
  1193. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1194. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1195. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1196. cmd->channel_opt[1].non_ebs_ratio =
  1197. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  1198. }
  1199. for (i = 1; i <= req->req.n_ssids; i++)
  1200. ssid_bitmap |= BIT(i);
  1201. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1202. req->req.n_channels, ssid_bitmap,
  1203. cmd);
  1204. preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  1205. mvm->fw->ucode_capa.n_scan_channels);
  1206. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
  1207. req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1208. req->req.mac_addr : NULL,
  1209. req->req.mac_addr_mask);
  1210. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1211. if (!ret) {
  1212. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1213. } else {
  1214. /*
  1215. * If the scan failed, it usually means that the FW was unable
  1216. * to allocate the time events. Warn on it, but maybe we
  1217. * should try to send the command again with different params.
  1218. */
  1219. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1220. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1221. ret = -EIO;
  1222. }
  1223. return ret;
  1224. }
  1225. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1226. struct ieee80211_vif *vif,
  1227. struct cfg80211_sched_scan_request *req,
  1228. struct ieee80211_scan_ies *ies)
  1229. {
  1230. struct iwl_host_cmd hcmd = {
  1231. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1232. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1233. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1234. mvm->fw->ucode_capa.n_scan_channels +
  1235. sizeof(struct iwl_scan_probe_req), },
  1236. .data = { mvm->scan_cmd, },
  1237. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1238. };
  1239. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1240. struct iwl_scan_probe_req *preq;
  1241. struct iwl_mvm_scan_params params = {};
  1242. int ret;
  1243. u32 flags = 0, ssid_bitmap = 0;
  1244. lockdep_assert_held(&mvm->mutex);
  1245. /* we should have failed registration if scan_cmd was NULL */
  1246. if (WARN_ON(mvm->scan_cmd == NULL))
  1247. return -ENOMEM;
  1248. if (req->n_ssids > PROBE_OPTION_MAX ||
  1249. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1250. ies->len[NL80211_BAND_5GHZ] >
  1251. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  1252. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1253. return -ENOBUFS;
  1254. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1255. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1256. cmd->n_channels = (u8)req->n_channels;
  1257. if (iwl_mvm_scan_pass_all(mvm, req))
  1258. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1259. else
  1260. flags |= IWL_MVM_LMAC_SCAN_FLAG_MATCH;
  1261. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1262. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1263. if (params.passive_fragmented)
  1264. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1265. if (req->n_ssids == 0)
  1266. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1267. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1268. if (mvm->scan_iter_notif_enabled)
  1269. flags |= IWL_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE;
  1270. #endif
  1271. cmd->scan_flags |= cpu_to_le32(flags);
  1272. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1273. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1274. MAC_FILTER_IN_BEACON);
  1275. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1276. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1277. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1278. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1279. cmd->schedule[0].full_scan_mul = 1;
  1280. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1281. cmd->schedule[1].iterations = 0xff;
  1282. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1283. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1284. mvm->last_ebs_successful) {
  1285. cmd->channel_opt[0].flags =
  1286. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1287. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1288. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1289. cmd->channel_opt[0].non_ebs_ratio =
  1290. cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
  1291. cmd->channel_opt[1].flags =
  1292. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1293. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1294. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1295. cmd->channel_opt[1].non_ebs_ratio =
  1296. cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
  1297. }
  1298. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1299. ssid_bitmap, cmd);
  1300. preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
  1301. mvm->fw->ucode_capa.n_scan_channels);
  1302. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
  1303. req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1304. req->mac_addr : NULL,
  1305. req->mac_addr_mask);
  1306. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1307. if (!ret) {
  1308. IWL_DEBUG_SCAN(mvm,
  1309. "Sched scan request was sent successfully\n");
  1310. } else {
  1311. /*
  1312. * If the scan failed, it usually means that the FW was unable
  1313. * to allocate the time events. Warn on it, but maybe we
  1314. * should try to send the command again with different params.
  1315. */
  1316. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1317. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1318. ret = -EIO;
  1319. }
  1320. return ret;
  1321. }
  1322. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  1323. {
  1324. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  1325. return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
  1326. true);
  1327. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  1328. return 0;
  1329. if (iwl_mvm_is_radio_killed(mvm)) {
  1330. ieee80211_scan_completed(mvm->hw, true);
  1331. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1332. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1333. return 0;
  1334. }
  1335. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1336. return iwl_mvm_scan_offload_stop(mvm, true);
  1337. return iwl_mvm_cancel_regular_scan(mvm);
  1338. }
  1339. /* UMAC scan API */
  1340. struct iwl_umac_scan_done {
  1341. struct iwl_mvm *mvm;
  1342. enum iwl_umac_scan_uid_type type;
  1343. };
  1344. static int rate_to_scan_rate_flag(unsigned int rate)
  1345. {
  1346. static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
  1347. [IWL_RATE_1M_INDEX] = SCAN_CONFIG_RATE_1M,
  1348. [IWL_RATE_2M_INDEX] = SCAN_CONFIG_RATE_2M,
  1349. [IWL_RATE_5M_INDEX] = SCAN_CONFIG_RATE_5M,
  1350. [IWL_RATE_11M_INDEX] = SCAN_CONFIG_RATE_11M,
  1351. [IWL_RATE_6M_INDEX] = SCAN_CONFIG_RATE_6M,
  1352. [IWL_RATE_9M_INDEX] = SCAN_CONFIG_RATE_9M,
  1353. [IWL_RATE_12M_INDEX] = SCAN_CONFIG_RATE_12M,
  1354. [IWL_RATE_18M_INDEX] = SCAN_CONFIG_RATE_18M,
  1355. [IWL_RATE_24M_INDEX] = SCAN_CONFIG_RATE_24M,
  1356. [IWL_RATE_36M_INDEX] = SCAN_CONFIG_RATE_36M,
  1357. [IWL_RATE_48M_INDEX] = SCAN_CONFIG_RATE_48M,
  1358. [IWL_RATE_54M_INDEX] = SCAN_CONFIG_RATE_54M,
  1359. };
  1360. return rate_to_scan_rate[rate];
  1361. }
  1362. static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
  1363. {
  1364. struct ieee80211_supported_band *band;
  1365. unsigned int rates = 0;
  1366. int i;
  1367. band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  1368. for (i = 0; i < band->n_bitrates; i++)
  1369. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  1370. band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  1371. for (i = 0; i < band->n_bitrates; i++)
  1372. rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
  1373. /* Set both basic rates and supported rates */
  1374. rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
  1375. return cpu_to_le32(rates);
  1376. }
  1377. int iwl_mvm_config_scan(struct iwl_mvm *mvm)
  1378. {
  1379. struct iwl_scan_config *scan_config;
  1380. struct ieee80211_supported_band *band;
  1381. int num_channels =
  1382. mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
  1383. mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  1384. int ret, i, j = 0, cmd_size, data_size;
  1385. struct iwl_host_cmd cmd = {
  1386. .id = SCAN_CFG_CMD,
  1387. };
  1388. if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
  1389. return -ENOBUFS;
  1390. cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
  1391. scan_config = kzalloc(cmd_size, GFP_KERNEL);
  1392. if (!scan_config)
  1393. return -ENOMEM;
  1394. data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
  1395. scan_config->hdr.size = cpu_to_le16(data_size);
  1396. scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
  1397. SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
  1398. SCAN_CONFIG_FLAG_SET_TX_CHAINS |
  1399. SCAN_CONFIG_FLAG_SET_RX_CHAINS |
  1400. SCAN_CONFIG_FLAG_SET_ALL_TIMES |
  1401. SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
  1402. SCAN_CONFIG_FLAG_SET_MAC_ADDR |
  1403. SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
  1404. SCAN_CONFIG_N_CHANNELS(num_channels));
  1405. scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
  1406. scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
  1407. scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
  1408. scan_config->out_of_channel_time = cpu_to_le32(170);
  1409. scan_config->suspend_time = cpu_to_le32(30);
  1410. scan_config->dwell_active = 20;
  1411. scan_config->dwell_passive = 110;
  1412. scan_config->dwell_fragmented = 20;
  1413. memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
  1414. scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
  1415. scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
  1416. IWL_CHANNEL_FLAG_ACCURATE_EBS |
  1417. IWL_CHANNEL_FLAG_EBS_ADD |
  1418. IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
  1419. band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
  1420. for (i = 0; i < band->n_channels; i++, j++)
  1421. scan_config->channel_array[j] = band->channels[i].hw_value;
  1422. band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
  1423. for (i = 0; i < band->n_channels; i++, j++)
  1424. scan_config->channel_array[j] = band->channels[i].hw_value;
  1425. cmd.data[0] = scan_config;
  1426. cmd.len[0] = cmd_size;
  1427. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  1428. IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
  1429. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1430. kfree(scan_config);
  1431. return ret;
  1432. }
  1433. static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
  1434. {
  1435. int i;
  1436. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  1437. if (mvm->scan_uid[i] == uid)
  1438. return i;
  1439. return i;
  1440. }
  1441. static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
  1442. {
  1443. return iwl_mvm_find_scan_uid(mvm, 0);
  1444. }
  1445. static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
  1446. enum iwl_umac_scan_uid_type type)
  1447. {
  1448. int i;
  1449. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
  1450. if (mvm->scan_uid[i] & type)
  1451. return true;
  1452. return false;
  1453. }
  1454. static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
  1455. enum iwl_umac_scan_uid_type type)
  1456. {
  1457. u32 uid;
  1458. /* make sure exactly one bit is on in scan type */
  1459. WARN_ON(hweight8(type) != 1);
  1460. /*
  1461. * Make sure scan uids are unique. If one scan lasts long time while
  1462. * others are completing frequently, the seq number will wrap up and
  1463. * we may have more than one scan with the same uid.
  1464. */
  1465. do {
  1466. uid = type | (mvm->scan_seq_num <<
  1467. IWL_UMAC_SCAN_UID_SEQ_OFFSET);
  1468. mvm->scan_seq_num++;
  1469. } while (iwl_mvm_find_scan_uid(mvm, uid) <
  1470. IWL_MVM_MAX_SIMULTANEOUS_SCANS);
  1471. IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
  1472. return uid;
  1473. }
  1474. static void
  1475. iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
  1476. struct iwl_scan_req_umac *cmd,
  1477. struct iwl_mvm_scan_params *params)
  1478. {
  1479. memset(cmd, 0, ksize(cmd));
  1480. cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
  1481. sizeof(struct iwl_mvm_umac_cmd_hdr));
  1482. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1483. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1484. if (params->passive_fragmented)
  1485. cmd->fragmented_dwell =
  1486. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1487. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1488. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1489. cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1490. }
  1491. static void
  1492. iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
  1493. struct ieee80211_channel **channels,
  1494. int n_channels, u32 ssid_bitmap,
  1495. struct iwl_scan_req_umac *cmd)
  1496. {
  1497. struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
  1498. int i;
  1499. for (i = 0; i < n_channels; i++) {
  1500. channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
  1501. channel_cfg[i].channel_num = channels[i]->hw_value;
  1502. channel_cfg[i].iter_count = 1;
  1503. channel_cfg[i].iter_interval = 0;
  1504. }
  1505. }
  1506. static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
  1507. struct cfg80211_ssid *ssids,
  1508. int fragmented)
  1509. {
  1510. int flags = 0;
  1511. if (n_ssids == 0)
  1512. flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
  1513. if (n_ssids == 1 && ssids[0].ssid_len != 0)
  1514. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
  1515. if (fragmented)
  1516. flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
  1517. if (iwl_mvm_rrm_scan_needed(mvm))
  1518. flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
  1519. return flags;
  1520. }
  1521. int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1522. struct ieee80211_scan_request *req)
  1523. {
  1524. struct iwl_host_cmd hcmd = {
  1525. .id = SCAN_REQ_UMAC,
  1526. .len = { iwl_mvm_scan_size(mvm), },
  1527. .data = { mvm->scan_cmd, },
  1528. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1529. };
  1530. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1531. struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
  1532. sizeof(struct iwl_scan_channel_cfg_umac) *
  1533. mvm->fw->ucode_capa.n_scan_channels;
  1534. struct iwl_mvm_scan_params params = {};
  1535. u32 uid, flags;
  1536. u32 ssid_bitmap = 0;
  1537. int ret, i, uid_idx;
  1538. lockdep_assert_held(&mvm->mutex);
  1539. uid_idx = iwl_mvm_find_free_scan_uid(mvm);
  1540. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1541. return -EBUSY;
  1542. /* we should have failed registration if scan_cmd was NULL */
  1543. if (WARN_ON(mvm->scan_cmd == NULL))
  1544. return -ENOMEM;
  1545. if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
  1546. req->ies.common_ie_len +
  1547. req->ies.len[NL80211_BAND_2GHZ] +
  1548. req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
  1549. SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
  1550. mvm->fw->ucode_capa.n_scan_channels))
  1551. return -ENOBUFS;
  1552. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1553. &params);
  1554. iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
  1555. uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
  1556. mvm->scan_uid[uid_idx] = uid;
  1557. cmd->uid = cpu_to_le32(uid);
  1558. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1559. flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
  1560. req->req.ssids,
  1561. params.passive_fragmented);
  1562. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1563. cmd->general_flags = cpu_to_le32(flags);
  1564. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_SINGLE_SCAN_EBS &&
  1565. mvm->last_ebs_successful)
  1566. cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
  1567. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1568. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
  1569. cmd->n_channels = req->req.n_channels;
  1570. for (i = 0; i < req->req.n_ssids; i++)
  1571. ssid_bitmap |= BIT(i);
  1572. iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
  1573. req->req.n_channels, ssid_bitmap, cmd);
  1574. sec_part->schedule[0].iter_count = 1;
  1575. sec_part->delay = 0;
  1576. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
  1577. req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1578. req->req.mac_addr : NULL,
  1579. req->req.mac_addr_mask);
  1580. iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
  1581. req->req.n_ssids, 0);
  1582. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1583. if (!ret) {
  1584. IWL_DEBUG_SCAN(mvm,
  1585. "Scan request was sent successfully\n");
  1586. } else {
  1587. /*
  1588. * If the scan failed, it usually means that the FW was unable
  1589. * to allocate the time events. Warn on it, but maybe we
  1590. * should try to send the command again with different params.
  1591. */
  1592. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1593. }
  1594. return ret;
  1595. }
  1596. int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1597. struct cfg80211_sched_scan_request *req,
  1598. struct ieee80211_scan_ies *ies)
  1599. {
  1600. struct iwl_host_cmd hcmd = {
  1601. .id = SCAN_REQ_UMAC,
  1602. .len = { iwl_mvm_scan_size(mvm), },
  1603. .data = { mvm->scan_cmd, },
  1604. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1605. };
  1606. struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
  1607. struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
  1608. sizeof(struct iwl_scan_channel_cfg_umac) *
  1609. mvm->fw->ucode_capa.n_scan_channels;
  1610. struct iwl_mvm_scan_params params = {};
  1611. u32 uid, flags;
  1612. u32 ssid_bitmap = 0;
  1613. int ret, uid_idx;
  1614. lockdep_assert_held(&mvm->mutex);
  1615. uid_idx = iwl_mvm_find_free_scan_uid(mvm);
  1616. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1617. return -EBUSY;
  1618. /* we should have failed registration if scan_cmd was NULL */
  1619. if (WARN_ON(mvm->scan_cmd == NULL))
  1620. return -ENOMEM;
  1621. if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
  1622. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1623. ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
  1624. SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
  1625. mvm->fw->ucode_capa.n_scan_channels))
  1626. return -ENOBUFS;
  1627. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
  1628. &params);
  1629. iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
  1630. cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
  1631. uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1632. mvm->scan_uid[uid_idx] = uid;
  1633. cmd->uid = cpu_to_le32(uid);
  1634. cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
  1635. flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
  1636. params.passive_fragmented);
  1637. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
  1638. if (iwl_mvm_scan_pass_all(mvm, req))
  1639. flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
  1640. else
  1641. flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
  1642. cmd->general_flags = cpu_to_le32(flags);
  1643. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1644. mvm->last_ebs_successful)
  1645. cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
  1646. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1647. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
  1648. cmd->n_channels = req->n_channels;
  1649. iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
  1650. false);
  1651. /* This API uses bits 0-19 instead of 1-20. */
  1652. ssid_bitmap = ssid_bitmap >> 1;
  1653. iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1654. ssid_bitmap, cmd);
  1655. sec_part->schedule[0].interval =
  1656. cpu_to_le16(req->interval / MSEC_PER_SEC);
  1657. sec_part->schedule[0].iter_count = 0xff;
  1658. sec_part->delay = 0;
  1659. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
  1660. req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
  1661. req->mac_addr : NULL,
  1662. req->mac_addr_mask);
  1663. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1664. if (!ret) {
  1665. IWL_DEBUG_SCAN(mvm,
  1666. "Sched scan request was sent successfully\n");
  1667. } else {
  1668. /*
  1669. * If the scan failed, it usually means that the FW was unable
  1670. * to allocate the time events. Warn on it, but maybe we
  1671. * should try to send the command again with different params.
  1672. */
  1673. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1674. }
  1675. return ret;
  1676. }
  1677. int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
  1678. struct iwl_rx_cmd_buffer *rxb,
  1679. struct iwl_device_cmd *cmd)
  1680. {
  1681. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1682. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1683. u32 uid = __le32_to_cpu(notif->uid);
  1684. bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
  1685. int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
  1686. /*
  1687. * Scan uid may be set to zero in case of scan abort request from above.
  1688. */
  1689. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1690. return 0;
  1691. IWL_DEBUG_SCAN(mvm,
  1692. "Scan completed, uid %u type %s, status %s, EBS status %s\n",
  1693. uid, sched ? "sched" : "regular",
  1694. notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  1695. "completed" : "aborted",
  1696. notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
  1697. "success" : "failed");
  1698. if (notif->ebs_status)
  1699. mvm->last_ebs_successful = false;
  1700. mvm->scan_uid[uid_idx] = 0;
  1701. if (!sched) {
  1702. ieee80211_scan_completed(mvm->hw,
  1703. notif->status ==
  1704. IWL_SCAN_OFFLOAD_ABORTED);
  1705. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1706. } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
  1707. ieee80211_sched_scan_stopped(mvm->hw);
  1708. } else {
  1709. IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
  1710. }
  1711. return 0;
  1712. }
  1713. static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
  1714. struct iwl_rx_packet *pkt, void *data)
  1715. {
  1716. struct iwl_umac_scan_done *scan_done = data;
  1717. struct iwl_umac_scan_complete *notif = (void *)pkt->data;
  1718. u32 uid = __le32_to_cpu(notif->uid);
  1719. int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
  1720. if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
  1721. return false;
  1722. if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
  1723. return false;
  1724. /*
  1725. * Clear scan uid of scans that was aborted from above and completed
  1726. * in FW so the RX handler does nothing. Set last_ebs_successful here if
  1727. * needed.
  1728. */
  1729. scan_done->mvm->scan_uid[uid_idx] = 0;
  1730. if (notif->ebs_status)
  1731. scan_done->mvm->last_ebs_successful = false;
  1732. return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
  1733. }
  1734. static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
  1735. {
  1736. struct iwl_umac_scan_abort cmd = {
  1737. .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
  1738. sizeof(struct iwl_mvm_umac_cmd_hdr)),
  1739. .uid = cpu_to_le32(uid),
  1740. };
  1741. lockdep_assert_held(&mvm->mutex);
  1742. IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
  1743. return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
  1744. }
  1745. static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
  1746. enum iwl_umac_scan_uid_type type, bool notify)
  1747. {
  1748. struct iwl_notification_wait wait_scan_done;
  1749. static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
  1750. struct iwl_umac_scan_done scan_done = {
  1751. .mvm = mvm,
  1752. .type = type,
  1753. };
  1754. int i, ret = -EIO;
  1755. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  1756. scan_done_notif,
  1757. ARRAY_SIZE(scan_done_notif),
  1758. iwl_scan_umac_done_check, &scan_done);
  1759. IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
  1760. for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
  1761. if (mvm->scan_uid[i] & type) {
  1762. int err;
  1763. if (iwl_mvm_is_radio_killed(mvm) &&
  1764. (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
  1765. ieee80211_scan_completed(mvm->hw, true);
  1766. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1767. break;
  1768. }
  1769. err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
  1770. if (!err)
  1771. ret = 0;
  1772. }
  1773. }
  1774. if (ret) {
  1775. IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
  1776. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  1777. return ret;
  1778. }
  1779. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  1780. if (ret)
  1781. return ret;
  1782. if (notify) {
  1783. if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
  1784. ieee80211_sched_scan_stopped(mvm->hw);
  1785. if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
  1786. ieee80211_scan_completed(mvm->hw, true);
  1787. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  1788. }
  1789. }
  1790. return ret;
  1791. }
  1792. int iwl_mvm_scan_size(struct iwl_mvm *mvm)
  1793. {
  1794. if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
  1795. return sizeof(struct iwl_scan_req_umac) +
  1796. sizeof(struct iwl_scan_channel_cfg_umac) *
  1797. mvm->fw->ucode_capa.n_scan_channels +
  1798. sizeof(struct iwl_scan_req_umac_tail);
  1799. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1800. return sizeof(struct iwl_scan_req_unified_lmac) +
  1801. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1802. mvm->fw->ucode_capa.n_scan_channels +
  1803. sizeof(struct iwl_scan_probe_req);
  1804. return sizeof(struct iwl_scan_cmd) +
  1805. mvm->fw->ucode_capa.max_probe_length +
  1806. mvm->fw->ucode_capa.n_scan_channels *
  1807. sizeof(struct iwl_scan_channel);
  1808. }