scan.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. struct iwl_mvm_scan_params {
  73. u32 max_out_time;
  74. u32 suspend_time;
  75. bool passive_fragmented;
  76. struct _dwell {
  77. u16 passive;
  78. u16 active;
  79. } dwell[IEEE80211_NUM_BANDS];
  80. };
  81. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  82. {
  83. u16 rx_chain;
  84. u8 rx_ant;
  85. if (mvm->scan_rx_ant != ANT_NONE)
  86. rx_ant = mvm->scan_rx_ant;
  87. else
  88. rx_ant = mvm->fw->valid_rx_ant;
  89. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  90. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  91. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  92. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  93. return cpu_to_le16(rx_chain);
  94. }
  95. static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
  96. {
  97. if (band == IEEE80211_BAND_2GHZ)
  98. return cpu_to_le32(PHY_BAND_24);
  99. else
  100. return cpu_to_le32(PHY_BAND_5);
  101. }
  102. static inline __le32
  103. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  104. bool no_cck)
  105. {
  106. u32 tx_ant;
  107. mvm->scan_last_antenna_idx =
  108. iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
  109. mvm->scan_last_antenna_idx);
  110. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  111. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  112. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  113. tx_ant);
  114. else
  115. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  116. }
  117. /*
  118. * We insert the SSIDs in an inverted order, because the FW will
  119. * invert it back. The most prioritized SSID, which is first in the
  120. * request list, is not copied here, but inserted directly to the probe
  121. * request.
  122. */
  123. static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
  124. struct cfg80211_ssid *ssids,
  125. int n_ssids, int first)
  126. {
  127. int fw_idx, req_idx;
  128. for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
  129. req_idx--, fw_idx++) {
  130. cmd_ssid[fw_idx].id = WLAN_EID_SSID;
  131. cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
  132. memcpy(cmd_ssid[fw_idx].ssid,
  133. ssids[req_idx].ssid,
  134. ssids[req_idx].ssid_len);
  135. }
  136. }
  137. /*
  138. * If req->n_ssids > 0, it means we should do an active scan.
  139. * In case of active scan w/o directed scan, we receive a zero-length SSID
  140. * just to notify that this scan is active and not passive.
  141. * In order to notify the FW of the number of SSIDs we wish to scan (including
  142. * the zero-length one), we need to set the corresponding bits in chan->type,
  143. * one for each SSID, and set the active bit (first). If the first SSID is
  144. * already included in the probe template, so we need to set only
  145. * req->n_ssids - 1 bits in addition to the first bit.
  146. */
  147. static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
  148. {
  149. if (band == IEEE80211_BAND_2GHZ)
  150. return 20 + 3 * (n_ssids + 1);
  151. return 10 + 2 * (n_ssids + 1);
  152. }
  153. static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
  154. {
  155. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  156. }
  157. static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
  158. struct cfg80211_scan_request *req,
  159. bool basic_ssid,
  160. struct iwl_mvm_scan_params *params)
  161. {
  162. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  163. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  164. int i;
  165. int type = BIT(req->n_ssids) - 1;
  166. enum ieee80211_band band = req->channels[0]->band;
  167. if (!basic_ssid)
  168. type |= BIT(req->n_ssids);
  169. for (i = 0; i < cmd->channel_count; i++) {
  170. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  171. chan->type = cpu_to_le32(type);
  172. if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
  173. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  174. chan->active_dwell = cpu_to_le16(params->dwell[band].active);
  175. chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
  176. chan->iteration_count = cpu_to_le16(1);
  177. chan++;
  178. }
  179. }
  180. /*
  181. * Fill in probe request with the following parameters:
  182. * TA is our vif HW address, which mac80211 ensures we have.
  183. * Packet is broadcasted, so this is both SA and DA.
  184. * The probe request IE is made out of two: first comes the most prioritized
  185. * SSID if a directed scan is requested. Second comes whatever extra
  186. * information was given to us as the scan request IE.
  187. */
  188. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  189. int n_ssids, const u8 *ssid, int ssid_len,
  190. const u8 *band_ie, int band_ie_len,
  191. const u8 *common_ie, int common_ie_len,
  192. int left)
  193. {
  194. int len = 0;
  195. u8 *pos = NULL;
  196. /* Make sure there is enough space for the probe request,
  197. * two mandatory IEs and the data */
  198. left -= 24;
  199. if (left < 0)
  200. return 0;
  201. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  202. eth_broadcast_addr(frame->da);
  203. memcpy(frame->sa, ta, ETH_ALEN);
  204. eth_broadcast_addr(frame->bssid);
  205. frame->seq_ctrl = 0;
  206. len += 24;
  207. /* for passive scans, no need to fill anything */
  208. if (n_ssids == 0)
  209. return (u16)len;
  210. /* points to the payload of the request */
  211. pos = &frame->u.probe_req.variable[0];
  212. /* fill in our SSID IE */
  213. left -= ssid_len + 2;
  214. if (left < 0)
  215. return 0;
  216. *pos++ = WLAN_EID_SSID;
  217. *pos++ = ssid_len;
  218. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  219. memcpy(pos, ssid, ssid_len);
  220. pos += ssid_len;
  221. }
  222. len += ssid_len + 2;
  223. if (WARN_ON(left < band_ie_len + common_ie_len))
  224. return len;
  225. if (band_ie && band_ie_len) {
  226. memcpy(pos, band_ie, band_ie_len);
  227. pos += band_ie_len;
  228. len += band_ie_len;
  229. }
  230. if (common_ie && common_ie_len) {
  231. memcpy(pos, common_ie, common_ie_len);
  232. pos += common_ie_len;
  233. len += common_ie_len;
  234. }
  235. return (u16)len;
  236. }
  237. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  238. struct ieee80211_vif *vif)
  239. {
  240. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  241. bool *global_bound = data;
  242. if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
  243. *global_bound = true;
  244. }
  245. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  246. struct ieee80211_vif *vif,
  247. int n_ssids, u32 flags,
  248. struct iwl_mvm_scan_params *params)
  249. {
  250. bool global_bound = false;
  251. enum ieee80211_band band;
  252. u8 frag_passive_dwell = 0;
  253. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  254. IEEE80211_IFACE_ITER_NORMAL,
  255. iwl_mvm_scan_condition_iterator,
  256. &global_bound);
  257. if (!global_bound)
  258. goto not_bound;
  259. params->suspend_time = 30;
  260. params->max_out_time = 170;
  261. if (iwl_mvm_low_latency(mvm)) {
  262. if (mvm->fw->ucode_capa.api[0] &
  263. IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
  264. params->suspend_time = 105;
  265. params->max_out_time = 70;
  266. frag_passive_dwell = 20;
  267. } else {
  268. params->suspend_time = 120;
  269. params->max_out_time = 120;
  270. }
  271. }
  272. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  273. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  274. /*
  275. * P2P device scan should not be fragmented to avoid negative
  276. * impact on P2P device discovery. Configure max_out_time to be
  277. * equal to dwell time on passive channel. Take a longest
  278. * possible value, one that corresponds to 2GHz band
  279. */
  280. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  281. u32 passive_dwell =
  282. iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
  283. params->max_out_time = passive_dwell;
  284. } else {
  285. params->passive_fragmented = true;
  286. }
  287. }
  288. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  289. params->max_out_time = 200;
  290. not_bound:
  291. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  292. if (params->passive_fragmented)
  293. params->dwell[band].passive = frag_passive_dwell;
  294. else
  295. params->dwell[band].passive =
  296. iwl_mvm_get_passive_dwell(band);
  297. params->dwell[band].active = iwl_mvm_get_active_dwell(band,
  298. n_ssids);
  299. }
  300. }
  301. static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
  302. {
  303. /* require rrm scan whenever the fw supports it */
  304. return mvm->fw->ucode_capa.capa[0] &
  305. IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
  306. }
  307. static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
  308. bool is_sched_scan)
  309. {
  310. int max_probe_len;
  311. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  312. max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
  313. else
  314. max_probe_len = mvm->fw->ucode_capa.max_probe_length;
  315. /* we create the 802.11 header and SSID element */
  316. max_probe_len -= 24 + 2;
  317. /* basic ssid is added only for hw_scan with and old api */
  318. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
  319. !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
  320. !is_sched_scan)
  321. max_probe_len -= 32;
  322. return max_probe_len;
  323. }
  324. int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
  325. {
  326. int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
  327. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
  328. return max_ie_len;
  329. /* TODO: [BUG] This function should return the maximum allowed size of
  330. * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
  331. * in the same command. So the correct implementation of this function
  332. * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
  333. * command has only 512 bytes and it would leave us with about 240
  334. * bytes for scan IEs, which is clearly not enough. So meanwhile
  335. * we will report an incorrect value. This may result in a failure to
  336. * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
  337. * functions with -ENOBUFS, if a large enough probe will be provided.
  338. */
  339. return max_ie_len;
  340. }
  341. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  342. struct ieee80211_vif *vif,
  343. struct cfg80211_scan_request *req)
  344. {
  345. struct iwl_host_cmd hcmd = {
  346. .id = SCAN_REQUEST_CMD,
  347. .len = { 0, },
  348. .data = { mvm->scan_cmd, },
  349. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  350. };
  351. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  352. int ret;
  353. u32 status;
  354. int ssid_len = 0;
  355. u8 *ssid = NULL;
  356. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  357. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  358. struct iwl_mvm_scan_params params = {};
  359. lockdep_assert_held(&mvm->mutex);
  360. /* we should have failed registration if scan_cmd was NULL */
  361. if (WARN_ON(mvm->scan_cmd == NULL))
  362. return -ENOMEM;
  363. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  364. mvm->scan_status = IWL_MVM_SCAN_OS;
  365. memset(cmd, 0, ksize(cmd));
  366. cmd->channel_count = (u8)req->n_channels;
  367. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  368. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  369. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  370. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
  371. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  372. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  373. if (params.passive_fragmented)
  374. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  375. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  376. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  377. MAC_FILTER_IN_BEACON);
  378. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  379. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  380. else
  381. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  382. cmd->repeats = cpu_to_le32(1);
  383. /*
  384. * If the user asked for passive scan, don't change to active scan if
  385. * you see any activity on the channel - remain passive.
  386. */
  387. if (req->n_ssids > 0) {
  388. cmd->passive2active = cpu_to_le16(1);
  389. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  390. if (basic_ssid) {
  391. ssid = req->ssids[0].ssid;
  392. ssid_len = req->ssids[0].ssid_len;
  393. }
  394. } else {
  395. cmd->passive2active = 0;
  396. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  397. }
  398. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
  399. basic_ssid ? 1 : 0);
  400. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  401. 3 << TX_CMD_FLG_BT_PRIO_POS);
  402. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  403. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  404. cmd->tx_cmd.rate_n_flags =
  405. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  406. req->no_cck);
  407. cmd->tx_cmd.len =
  408. cpu_to_le16(iwl_mvm_fill_probe_req(
  409. (struct ieee80211_mgmt *)cmd->data,
  410. vif->addr,
  411. req->n_ssids, ssid, ssid_len,
  412. req->ie, req->ie_len, NULL, 0,
  413. mvm->fw->ucode_capa.max_probe_length));
  414. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  415. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  416. le16_to_cpu(cmd->tx_cmd.len) +
  417. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  418. hcmd.len[0] = le16_to_cpu(cmd->len);
  419. status = SCAN_RESPONSE_OK;
  420. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  421. if (!ret && status == SCAN_RESPONSE_OK) {
  422. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  423. } else {
  424. /*
  425. * If the scan failed, it usually means that the FW was unable
  426. * to allocate the time events. Warn on it, but maybe we
  427. * should try to send the command again with different params.
  428. */
  429. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  430. status, ret);
  431. mvm->scan_status = IWL_MVM_SCAN_NONE;
  432. ret = -EIO;
  433. }
  434. return ret;
  435. }
  436. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  437. struct iwl_device_cmd *cmd)
  438. {
  439. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  440. struct iwl_cmd_response *resp = (void *)pkt->data;
  441. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  442. le32_to_cpu(resp->status));
  443. return 0;
  444. }
  445. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  446. struct iwl_device_cmd *cmd)
  447. {
  448. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  449. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  450. lockdep_assert_held(&mvm->mutex);
  451. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  452. notif->status, notif->scanned_channels);
  453. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  454. mvm->scan_status = IWL_MVM_SCAN_NONE;
  455. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  456. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  457. return 0;
  458. }
  459. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  460. struct iwl_rx_cmd_buffer *rxb,
  461. struct iwl_device_cmd *cmd)
  462. {
  463. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  464. u8 client_bitmap = 0;
  465. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  466. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  467. client_bitmap = notif->client_bitmap;
  468. }
  469. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
  470. client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
  471. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  472. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  473. ieee80211_sched_scan_results(mvm->hw);
  474. } else {
  475. IWL_DEBUG_SCAN(mvm, "Scan results\n");
  476. }
  477. }
  478. return 0;
  479. }
  480. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  481. struct iwl_rx_packet *pkt, void *data)
  482. {
  483. struct iwl_mvm *mvm =
  484. container_of(notif_wait, struct iwl_mvm, notif_wait);
  485. struct iwl_scan_complete_notif *notif;
  486. u32 *resp;
  487. switch (pkt->hdr.cmd) {
  488. case SCAN_ABORT_CMD:
  489. resp = (void *)pkt->data;
  490. if (*resp == CAN_ABORT_STATUS) {
  491. IWL_DEBUG_SCAN(mvm,
  492. "Scan can be aborted, wait until completion\n");
  493. return false;
  494. }
  495. /*
  496. * If scan cannot be aborted, it means that we had a
  497. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  498. * ieee80211_scan_completed already.
  499. */
  500. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  501. *resp);
  502. return true;
  503. case SCAN_COMPLETE_NOTIFICATION:
  504. notif = (void *)pkt->data;
  505. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  506. notif->status);
  507. return true;
  508. default:
  509. WARN_ON(1);
  510. return false;
  511. };
  512. }
  513. static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
  514. {
  515. struct iwl_notification_wait wait_scan_abort;
  516. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  517. SCAN_COMPLETE_NOTIFICATION };
  518. int ret;
  519. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  520. return 0;
  521. if (iwl_mvm_is_radio_killed(mvm)) {
  522. ieee80211_scan_completed(mvm->hw, true);
  523. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  524. mvm->scan_status = IWL_MVM_SCAN_NONE;
  525. return 0;
  526. }
  527. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  528. scan_abort_notif,
  529. ARRAY_SIZE(scan_abort_notif),
  530. iwl_mvm_scan_abort_notif, NULL);
  531. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  532. if (ret) {
  533. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  534. /* mac80211's state will be cleaned in the nic_restart flow */
  535. goto out_remove_notif;
  536. }
  537. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  538. out_remove_notif:
  539. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  540. return ret;
  541. }
  542. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  543. struct iwl_rx_cmd_buffer *rxb,
  544. struct iwl_device_cmd *cmd)
  545. {
  546. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  547. u8 status, ebs_status;
  548. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  549. struct iwl_periodic_scan_complete *scan_notif;
  550. scan_notif = (void *)pkt->data;
  551. status = scan_notif->status;
  552. ebs_status = scan_notif->ebs_status;
  553. } else {
  554. struct iwl_scan_offload_complete *scan_notif;
  555. scan_notif = (void *)pkt->data;
  556. status = scan_notif->status;
  557. ebs_status = scan_notif->ebs_status;
  558. }
  559. /* scan status must be locked for proper checking */
  560. lockdep_assert_held(&mvm->mutex);
  561. IWL_DEBUG_SCAN(mvm,
  562. "%s completed, status %s, EBS status %s\n",
  563. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  564. "Scheduled scan" : "Scan",
  565. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  566. "completed" : "aborted",
  567. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  568. "success" : "failed");
  569. /* only call mac80211 completion if the stop was initiated by FW */
  570. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  571. mvm->scan_status = IWL_MVM_SCAN_NONE;
  572. ieee80211_sched_scan_stopped(mvm->hw);
  573. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  574. mvm->scan_status = IWL_MVM_SCAN_NONE;
  575. ieee80211_scan_completed(mvm->hw,
  576. status == IWL_SCAN_OFFLOAD_ABORTED);
  577. }
  578. mvm->last_ebs_successful = !ebs_status;
  579. return 0;
  580. }
  581. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  582. struct ieee80211_vif *vif,
  583. struct ieee80211_scan_ies *ies,
  584. enum ieee80211_band band,
  585. struct iwl_tx_cmd *cmd,
  586. u8 *data)
  587. {
  588. u16 cmd_len;
  589. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  590. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  591. cmd->sta_id = mvm->aux_sta.sta_id;
  592. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  593. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  594. vif->addr,
  595. 1, NULL, 0,
  596. ies->ies[band], ies->len[band],
  597. ies->common_ies, ies->common_ie_len,
  598. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  599. cmd->len = cpu_to_le16(cmd_len);
  600. }
  601. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  602. struct ieee80211_vif *vif,
  603. struct cfg80211_sched_scan_request *req,
  604. struct iwl_scan_offload_cmd *scan,
  605. struct iwl_mvm_scan_params *params)
  606. {
  607. scan->channel_count = req->n_channels;
  608. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  609. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  610. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  611. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  612. scan->max_out_time = cpu_to_le32(params->max_out_time);
  613. scan->suspend_time = cpu_to_le32(params->suspend_time);
  614. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  615. MAC_FILTER_IN_BEACON);
  616. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  617. scan->rep_count = cpu_to_le32(1);
  618. if (params->passive_fragmented)
  619. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  620. }
  621. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  622. {
  623. int i;
  624. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  625. if (!ssid_list[i].len)
  626. break;
  627. if (ssid_list[i].len == ssid_len &&
  628. !memcmp(ssid_list->ssid, ssid, ssid_len))
  629. return i;
  630. }
  631. return -1;
  632. }
  633. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  634. struct iwl_ssid_ie *direct_scan,
  635. u32 *ssid_bitmap, bool basic_ssid)
  636. {
  637. int i, j;
  638. int index;
  639. /*
  640. * copy SSIDs from match list.
  641. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  642. * config match list.
  643. */
  644. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  645. /* skip empty SSID matchsets */
  646. if (!req->match_sets[i].ssid.ssid_len)
  647. continue;
  648. direct_scan[i].id = WLAN_EID_SSID;
  649. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  650. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  651. direct_scan[i].len);
  652. }
  653. /* add SSIDs from scan SSID list */
  654. *ssid_bitmap = 0;
  655. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  656. index = iwl_ssid_exist(req->ssids[j].ssid,
  657. req->ssids[j].ssid_len,
  658. direct_scan);
  659. if (index < 0) {
  660. if (!req->ssids[j].ssid_len && basic_ssid)
  661. continue;
  662. direct_scan[i].id = WLAN_EID_SSID;
  663. direct_scan[i].len = req->ssids[j].ssid_len;
  664. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  665. direct_scan[i].len);
  666. *ssid_bitmap |= BIT(i + 1);
  667. i++;
  668. } else {
  669. *ssid_bitmap |= BIT(index + 1);
  670. }
  671. }
  672. }
  673. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  674. struct cfg80211_sched_scan_request *req,
  675. u8 *channels_buffer,
  676. enum ieee80211_band band,
  677. int *head,
  678. u32 ssid_bitmap,
  679. struct iwl_mvm_scan_params *params)
  680. {
  681. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  682. __le32 *type = (__le32 *)channels_buffer;
  683. __le16 *channel_number = (__le16 *)(type + n_channels);
  684. __le16 *iter_count = channel_number + n_channels;
  685. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  686. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  687. u8 *passive_dwell = active_dwell + n_channels;
  688. int i, index = 0;
  689. for (i = 0; i < req->n_channels; i++) {
  690. struct ieee80211_channel *chan = req->channels[i];
  691. if (chan->band != band)
  692. continue;
  693. index = *head;
  694. (*head)++;
  695. channel_number[index] = cpu_to_le16(chan->hw_value);
  696. active_dwell[index] = params->dwell[band].active;
  697. passive_dwell[index] = params->dwell[band].passive;
  698. iter_count[index] = cpu_to_le16(1);
  699. iter_interval[index] = 0;
  700. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  701. type[index] |=
  702. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  703. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  704. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  705. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  706. type[index] |=
  707. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  708. /* scan for all SSIDs from req->ssids */
  709. type[index] |= cpu_to_le32(ssid_bitmap);
  710. }
  711. }
  712. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  713. struct ieee80211_vif *vif,
  714. struct cfg80211_sched_scan_request *req,
  715. struct ieee80211_scan_ies *ies)
  716. {
  717. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  718. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  719. int head = 0;
  720. u32 ssid_bitmap;
  721. int cmd_len;
  722. int ret;
  723. u8 *probes;
  724. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  725. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  726. struct iwl_scan_offload_cfg *scan_cfg;
  727. struct iwl_host_cmd cmd = {
  728. .id = SCAN_OFFLOAD_CONFIG_CMD,
  729. };
  730. struct iwl_mvm_scan_params params = {};
  731. lockdep_assert_held(&mvm->mutex);
  732. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  733. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  734. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  735. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  736. if (!scan_cfg)
  737. return -ENOMEM;
  738. probes = scan_cfg->data +
  739. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  740. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  741. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  742. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  743. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  744. &ssid_bitmap, basic_ssid);
  745. /* build tx frames for supported bands */
  746. if (band_2ghz) {
  747. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  748. IEEE80211_BAND_2GHZ,
  749. &scan_cfg->scan_cmd.tx_cmd[0],
  750. probes);
  751. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  752. IEEE80211_BAND_2GHZ, &head,
  753. ssid_bitmap, &params);
  754. }
  755. if (band_5ghz) {
  756. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  757. IEEE80211_BAND_5GHZ,
  758. &scan_cfg->scan_cmd.tx_cmd[1],
  759. probes +
  760. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  761. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  762. IEEE80211_BAND_5GHZ, &head,
  763. ssid_bitmap, &params);
  764. }
  765. cmd.data[0] = scan_cfg;
  766. cmd.len[0] = cmd_len;
  767. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  768. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  769. ret = iwl_mvm_send_cmd(mvm, &cmd);
  770. kfree(scan_cfg);
  771. return ret;
  772. }
  773. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  774. struct cfg80211_sched_scan_request *req)
  775. {
  776. struct iwl_scan_offload_profile *profile;
  777. struct iwl_scan_offload_profile_cfg *profile_cfg;
  778. struct iwl_scan_offload_blacklist *blacklist;
  779. struct iwl_host_cmd cmd = {
  780. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  781. .len[1] = sizeof(*profile_cfg),
  782. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  783. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  784. };
  785. int blacklist_len;
  786. int i;
  787. int ret;
  788. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  789. return -EIO;
  790. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  791. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  792. else
  793. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  794. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  795. if (!blacklist)
  796. return -ENOMEM;
  797. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  798. if (!profile_cfg) {
  799. ret = -ENOMEM;
  800. goto free_blacklist;
  801. }
  802. cmd.data[0] = blacklist;
  803. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  804. cmd.data[1] = profile_cfg;
  805. /* No blacklist configuration */
  806. profile_cfg->num_profiles = req->n_match_sets;
  807. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  808. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  809. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  810. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  811. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  812. for (i = 0; i < req->n_match_sets; i++) {
  813. profile = &profile_cfg->profiles[i];
  814. profile->ssid_index = i;
  815. /* Support any cipher and auth algorithm */
  816. profile->unicast_cipher = 0xff;
  817. profile->auth_alg = 0xff;
  818. profile->network_type = IWL_NETWORK_TYPE_ANY;
  819. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  820. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  821. }
  822. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  823. ret = iwl_mvm_send_cmd(mvm, &cmd);
  824. kfree(profile_cfg);
  825. free_blacklist:
  826. kfree(blacklist);
  827. return ret;
  828. }
  829. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  830. struct cfg80211_sched_scan_request *req)
  831. {
  832. struct iwl_scan_offload_req scan_req = {
  833. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  834. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  835. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  836. .schedule_line[0].full_scan_mul = 1,
  837. .schedule_line[1].iterations = 0xff,
  838. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  839. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  840. };
  841. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  842. IWL_DEBUG_SCAN(mvm,
  843. "Sending scheduled scan with filtering, filter len %d\n",
  844. req->n_match_sets);
  845. } else {
  846. IWL_DEBUG_SCAN(mvm,
  847. "Sending Scheduled scan without filtering\n");
  848. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  849. }
  850. if (mvm->last_ebs_successful &&
  851. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  852. scan_req.flags |=
  853. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  854. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  855. sizeof(scan_req), &scan_req);
  856. }
  857. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  858. {
  859. int ret;
  860. struct iwl_host_cmd cmd = {
  861. .id = SCAN_OFFLOAD_ABORT_CMD,
  862. };
  863. u32 status;
  864. /* Exit instantly with error when device is not ready
  865. * to receive scan abort command or it does not perform
  866. * scheduled scan currently */
  867. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  868. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  869. mvm->scan_status != IWL_MVM_SCAN_OS))
  870. return -EIO;
  871. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  872. if (ret)
  873. return ret;
  874. if (status != CAN_ABORT_STATUS) {
  875. /*
  876. * The scan abort will return 1 for success or
  877. * 2 for "failure". A failure condition can be
  878. * due to simply not being in an active scan which
  879. * can occur if we send the scan abort before the
  880. * microcode has notified us that a scan is completed.
  881. */
  882. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  883. ret = -ENOENT;
  884. }
  885. return ret;
  886. }
  887. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  888. {
  889. int ret;
  890. struct iwl_notification_wait wait_scan_done;
  891. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  892. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  893. lockdep_assert_held(&mvm->mutex);
  894. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  895. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  896. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  897. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  898. return 0;
  899. }
  900. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  901. scan_done_notif,
  902. ARRAY_SIZE(scan_done_notif),
  903. NULL, NULL);
  904. ret = iwl_mvm_send_scan_offload_abort(mvm);
  905. if (ret) {
  906. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  907. sched ? "offloaded " : "", ret);
  908. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  909. return ret;
  910. }
  911. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  912. sched ? "offloaded " : "");
  913. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  914. if (ret)
  915. return ret;
  916. /*
  917. * Clear the scan status so the next scan requests will succeed. This
  918. * also ensures the Rx handler doesn't do anything, as the scan was
  919. * stopped from above.
  920. */
  921. mvm->scan_status = IWL_MVM_SCAN_NONE;
  922. if (notify) {
  923. if (sched)
  924. ieee80211_sched_scan_stopped(mvm->hw);
  925. else
  926. ieee80211_scan_completed(mvm->hw, true);
  927. }
  928. return 0;
  929. }
  930. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  931. struct iwl_scan_req_tx_cmd *tx_cmd,
  932. bool no_cck)
  933. {
  934. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  935. TX_CMD_FLG_BT_DIS);
  936. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  937. IEEE80211_BAND_2GHZ,
  938. no_cck);
  939. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  940. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  941. TX_CMD_FLG_BT_DIS);
  942. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  943. IEEE80211_BAND_5GHZ,
  944. no_cck);
  945. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  946. }
  947. static void
  948. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  949. struct ieee80211_channel **channels,
  950. int n_channels, u32 ssid_bitmap,
  951. struct iwl_scan_req_unified_lmac *cmd)
  952. {
  953. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  954. int i;
  955. for (i = 0; i < n_channels; i++) {
  956. channel_cfg[i].channel_num =
  957. cpu_to_le16(channels[i]->hw_value);
  958. channel_cfg[i].iter_count = cpu_to_le16(1);
  959. channel_cfg[i].iter_interval = 0;
  960. channel_cfg[i].flags =
  961. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  962. ssid_bitmap);
  963. }
  964. }
  965. static void
  966. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  967. struct ieee80211_scan_ies *ies,
  968. struct iwl_scan_req_unified_lmac *cmd)
  969. {
  970. struct iwl_scan_probe_req *preq = (void *)(cmd->data +
  971. sizeof(struct iwl_scan_channel_cfg_lmac) *
  972. mvm->fw->ucode_capa.n_scan_channels);
  973. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  974. u8 *pos;
  975. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  976. eth_broadcast_addr(frame->da);
  977. memcpy(frame->sa, vif->addr, ETH_ALEN);
  978. eth_broadcast_addr(frame->bssid);
  979. frame->seq_ctrl = 0;
  980. pos = frame->u.probe_req.variable;
  981. *pos++ = WLAN_EID_SSID;
  982. *pos++ = 0;
  983. preq->mac_header.offset = 0;
  984. preq->mac_header.len = cpu_to_le16(24 + 2);
  985. memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
  986. ies->len[IEEE80211_BAND_2GHZ]);
  987. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  988. preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
  989. pos += ies->len[IEEE80211_BAND_2GHZ];
  990. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  991. ies->len[IEEE80211_BAND_5GHZ]);
  992. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  993. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  994. pos += ies->len[IEEE80211_BAND_5GHZ];
  995. memcpy(pos, ies->common_ies, ies->common_ie_len);
  996. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  997. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  998. }
  999. static void
  1000. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  1001. struct iwl_scan_req_unified_lmac *cmd,
  1002. struct iwl_mvm_scan_params *params)
  1003. {
  1004. memset(cmd, 0, ksize(cmd));
  1005. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  1006. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  1007. if (params->passive_fragmented)
  1008. cmd->fragmented_dwell =
  1009. params->dwell[IEEE80211_BAND_2GHZ].passive;
  1010. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  1011. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  1012. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  1013. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  1014. cmd->iter_num = cpu_to_le32(1);
  1015. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  1016. mvm->last_ebs_successful) {
  1017. cmd->channel_opt[0].flags =
  1018. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1019. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1020. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1021. cmd->channel_opt[1].flags =
  1022. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  1023. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  1024. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  1025. }
  1026. if (iwl_mvm_rrm_scan_needed(mvm))
  1027. cmd->scan_flags |=
  1028. cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
  1029. }
  1030. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  1031. struct ieee80211_vif *vif,
  1032. struct ieee80211_scan_request *req)
  1033. {
  1034. struct iwl_host_cmd hcmd = {
  1035. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1036. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1037. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1038. mvm->fw->ucode_capa.n_scan_channels +
  1039. sizeof(struct iwl_scan_probe_req), },
  1040. .data = { mvm->scan_cmd, },
  1041. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1042. };
  1043. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1044. struct iwl_mvm_scan_params params = {};
  1045. u32 flags;
  1046. int ssid_bitmap = 0;
  1047. int ret, i;
  1048. lockdep_assert_held(&mvm->mutex);
  1049. /* we should have failed registration if scan_cmd was NULL */
  1050. if (WARN_ON(mvm->scan_cmd == NULL))
  1051. return -ENOMEM;
  1052. if (req->req.n_ssids > PROBE_OPTION_MAX ||
  1053. req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
  1054. req->ies.len[NL80211_BAND_5GHZ] >
  1055. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
  1056. req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1057. return -ENOBUFS;
  1058. mvm->scan_status = IWL_MVM_SCAN_OS;
  1059. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1060. &params);
  1061. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1062. cmd->n_channels = (u8)req->req.n_channels;
  1063. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1064. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1065. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1066. if (params.passive_fragmented)
  1067. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1068. if (req->req.n_ssids == 0)
  1069. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1070. cmd->scan_flags |= cpu_to_le32(flags);
  1071. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1072. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1073. MAC_FILTER_IN_BEACON);
  1074. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1075. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1076. req->req.n_ssids, 0);
  1077. cmd->schedule[0].delay = 0;
  1078. cmd->schedule[0].iterations = 1;
  1079. cmd->schedule[0].full_scan_mul = 0;
  1080. cmd->schedule[1].delay = 0;
  1081. cmd->schedule[1].iterations = 0;
  1082. cmd->schedule[1].full_scan_mul = 0;
  1083. for (i = 1; i <= req->req.n_ssids; i++)
  1084. ssid_bitmap |= BIT(i);
  1085. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1086. req->req.n_channels, ssid_bitmap,
  1087. cmd);
  1088. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
  1089. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1090. if (!ret) {
  1091. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1092. } else {
  1093. /*
  1094. * If the scan failed, it usually means that the FW was unable
  1095. * to allocate the time events. Warn on it, but maybe we
  1096. * should try to send the command again with different params.
  1097. */
  1098. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1099. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1100. ret = -EIO;
  1101. }
  1102. return ret;
  1103. }
  1104. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1105. struct ieee80211_vif *vif,
  1106. struct cfg80211_sched_scan_request *req,
  1107. struct ieee80211_scan_ies *ies)
  1108. {
  1109. struct iwl_host_cmd hcmd = {
  1110. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1111. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1112. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1113. mvm->fw->ucode_capa.n_scan_channels +
  1114. sizeof(struct iwl_scan_probe_req), },
  1115. .data = { mvm->scan_cmd, },
  1116. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1117. };
  1118. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1119. struct iwl_mvm_scan_params params = {};
  1120. int ret;
  1121. u32 flags = 0, ssid_bitmap = 0;
  1122. lockdep_assert_held(&mvm->mutex);
  1123. /* we should have failed registration if scan_cmd was NULL */
  1124. if (WARN_ON(mvm->scan_cmd == NULL))
  1125. return -ENOMEM;
  1126. if (req->n_ssids > PROBE_OPTION_MAX ||
  1127. ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
  1128. ies->len[NL80211_BAND_5GHZ] >
  1129. iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
  1130. req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
  1131. return -ENOBUFS;
  1132. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1133. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1134. cmd->n_channels = (u8)req->n_channels;
  1135. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  1136. IWL_DEBUG_SCAN(mvm,
  1137. "Sending scheduled scan with filtering, n_match_sets %d\n",
  1138. req->n_match_sets);
  1139. } else {
  1140. IWL_DEBUG_SCAN(mvm,
  1141. "Sending Scheduled scan without filtering\n");
  1142. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1143. }
  1144. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1145. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1146. if (params.passive_fragmented)
  1147. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1148. if (req->n_ssids == 0)
  1149. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1150. cmd->scan_flags |= cpu_to_le32(flags);
  1151. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1152. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1153. MAC_FILTER_IN_BEACON);
  1154. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1155. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1156. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1157. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1158. cmd->schedule[0].full_scan_mul = 1;
  1159. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1160. cmd->schedule[1].iterations = 0xff;
  1161. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1162. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1163. ssid_bitmap, cmd);
  1164. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
  1165. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1166. if (!ret) {
  1167. IWL_DEBUG_SCAN(mvm,
  1168. "Sched scan request was sent successfully\n");
  1169. } else {
  1170. /*
  1171. * If the scan failed, it usually means that the FW was unable
  1172. * to allocate the time events. Warn on it, but maybe we
  1173. * should try to send the command again with different params.
  1174. */
  1175. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1176. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1177. ret = -EIO;
  1178. }
  1179. return ret;
  1180. }
  1181. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  1182. {
  1183. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1184. return iwl_mvm_scan_offload_stop(mvm, true);
  1185. return iwl_mvm_cancel_regular_scan(mvm);
  1186. }