scan.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/etherdevice.h>
  64. #include <net/mac80211.h>
  65. #include "mvm.h"
  66. #include "iwl-eeprom-parse.h"
  67. #include "fw-api-scan.h"
  68. #define IWL_PLCP_QUIET_THRESH 1
  69. #define IWL_ACTIVE_QUIET_TIME 10
  70. struct iwl_mvm_scan_params {
  71. u32 max_out_time;
  72. u32 suspend_time;
  73. bool passive_fragmented;
  74. struct _dwell {
  75. u16 passive;
  76. u16 active;
  77. } dwell[IEEE80211_NUM_BANDS];
  78. };
  79. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  80. {
  81. u16 rx_chain;
  82. u8 rx_ant;
  83. if (mvm->scan_rx_ant != ANT_NONE)
  84. rx_ant = mvm->scan_rx_ant;
  85. else
  86. rx_ant = mvm->fw->valid_rx_ant;
  87. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  88. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  89. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  90. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  91. return cpu_to_le16(rx_chain);
  92. }
  93. static inline __le32
  94. iwl_mvm_scan_rxon_flags(struct cfg80211_scan_request *req)
  95. {
  96. if (req->channels[0]->band == IEEE80211_BAND_2GHZ)
  97. return cpu_to_le32(PHY_BAND_24);
  98. else
  99. return cpu_to_le32(PHY_BAND_5);
  100. }
  101. static inline __le32
  102. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  103. bool no_cck)
  104. {
  105. u32 tx_ant;
  106. mvm->scan_last_antenna_idx =
  107. iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
  108. mvm->scan_last_antenna_idx);
  109. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  110. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  111. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  112. tx_ant);
  113. else
  114. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  115. }
  116. /*
  117. * We insert the SSIDs in an inverted order, because the FW will
  118. * invert it back. The most prioritized SSID, which is first in the
  119. * request list, is not copied here, but inserted directly to the probe
  120. * request.
  121. */
  122. static void iwl_mvm_scan_fill_ssids(struct iwl_scan_cmd *cmd,
  123. struct cfg80211_scan_request *req,
  124. int first)
  125. {
  126. int fw_idx, req_idx;
  127. for (req_idx = req->n_ssids - 1, fw_idx = 0; req_idx >= first;
  128. req_idx--, fw_idx++) {
  129. cmd->direct_scan[fw_idx].id = WLAN_EID_SSID;
  130. cmd->direct_scan[fw_idx].len = req->ssids[req_idx].ssid_len;
  131. memcpy(cmd->direct_scan[fw_idx].ssid,
  132. req->ssids[req_idx].ssid,
  133. req->ssids[req_idx].ssid_len);
  134. }
  135. }
  136. /*
  137. * If req->n_ssids > 0, it means we should do an active scan.
  138. * In case of active scan w/o directed scan, we receive a zero-length SSID
  139. * just to notify that this scan is active and not passive.
  140. * In order to notify the FW of the number of SSIDs we wish to scan (including
  141. * the zero-length one), we need to set the corresponding bits in chan->type,
  142. * one for each SSID, and set the active bit (first). If the first SSID is
  143. * already included in the probe template, so we need to set only
  144. * req->n_ssids - 1 bits in addition to the first bit.
  145. */
  146. static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
  147. {
  148. if (band == IEEE80211_BAND_2GHZ)
  149. return 30 + 3 * (n_ssids + 1);
  150. return 20 + 2 * (n_ssids + 1);
  151. }
  152. static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
  153. {
  154. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  155. }
  156. static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
  157. struct cfg80211_scan_request *req,
  158. bool basic_ssid,
  159. struct iwl_mvm_scan_params *params)
  160. {
  161. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  162. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  163. int i;
  164. int type = BIT(req->n_ssids) - 1;
  165. enum ieee80211_band band = req->channels[0]->band;
  166. if (!basic_ssid)
  167. type |= BIT(req->n_ssids);
  168. for (i = 0; i < cmd->channel_count; i++) {
  169. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  170. chan->type = cpu_to_le32(type);
  171. if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
  172. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  173. chan->active_dwell = cpu_to_le16(params->dwell[band].active);
  174. chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
  175. chan->iteration_count = cpu_to_le16(1);
  176. chan++;
  177. }
  178. }
  179. /*
  180. * Fill in probe request with the following parameters:
  181. * TA is our vif HW address, which mac80211 ensures we have.
  182. * Packet is broadcasted, so this is both SA and DA.
  183. * The probe request IE is made out of two: first comes the most prioritized
  184. * SSID if a directed scan is requested. Second comes whatever extra
  185. * information was given to us as the scan request IE.
  186. */
  187. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  188. int n_ssids, const u8 *ssid, int ssid_len,
  189. const u8 *ie, int ie_len,
  190. int left)
  191. {
  192. int len = 0;
  193. u8 *pos = NULL;
  194. /* Make sure there is enough space for the probe request,
  195. * two mandatory IEs and the data */
  196. left -= 24;
  197. if (left < 0)
  198. return 0;
  199. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  200. eth_broadcast_addr(frame->da);
  201. memcpy(frame->sa, ta, ETH_ALEN);
  202. eth_broadcast_addr(frame->bssid);
  203. frame->seq_ctrl = 0;
  204. len += 24;
  205. /* for passive scans, no need to fill anything */
  206. if (n_ssids == 0)
  207. return (u16)len;
  208. /* points to the payload of the request */
  209. pos = &frame->u.probe_req.variable[0];
  210. /* fill in our SSID IE */
  211. left -= ssid_len + 2;
  212. if (left < 0)
  213. return 0;
  214. *pos++ = WLAN_EID_SSID;
  215. *pos++ = ssid_len;
  216. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  217. memcpy(pos, ssid, ssid_len);
  218. pos += ssid_len;
  219. }
  220. len += ssid_len + 2;
  221. if (WARN_ON(left < ie_len))
  222. return len;
  223. if (ie && ie_len) {
  224. memcpy(pos, ie, ie_len);
  225. len += ie_len;
  226. }
  227. return (u16)len;
  228. }
  229. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  230. struct ieee80211_vif *vif)
  231. {
  232. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  233. bool *global_bound = data;
  234. if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
  235. *global_bound = true;
  236. }
  237. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  238. struct ieee80211_vif *vif,
  239. int n_ssids,
  240. struct iwl_mvm_scan_params *params)
  241. {
  242. bool global_bound = false;
  243. enum ieee80211_band band;
  244. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  245. IEEE80211_IFACE_ITER_NORMAL,
  246. iwl_mvm_scan_condition_iterator,
  247. &global_bound);
  248. if (!global_bound)
  249. goto not_bound;
  250. params->suspend_time = 100;
  251. params->max_out_time = 600;
  252. if (iwl_mvm_low_latency(mvm)) {
  253. params->suspend_time = 250;
  254. params->max_out_time = 250;
  255. }
  256. not_bound:
  257. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  258. params->dwell[band].passive = iwl_mvm_get_passive_dwell(band);
  259. params->dwell[band].active = iwl_mvm_get_active_dwell(band,
  260. n_ssids);
  261. }
  262. }
  263. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  264. struct ieee80211_vif *vif,
  265. struct cfg80211_scan_request *req)
  266. {
  267. struct iwl_host_cmd hcmd = {
  268. .id = SCAN_REQUEST_CMD,
  269. .len = { 0, },
  270. .data = { mvm->scan_cmd, },
  271. .flags = CMD_SYNC,
  272. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  273. };
  274. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  275. int ret;
  276. u32 status;
  277. int ssid_len = 0;
  278. u8 *ssid = NULL;
  279. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  280. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  281. struct iwl_mvm_scan_params params = {};
  282. lockdep_assert_held(&mvm->mutex);
  283. BUG_ON(mvm->scan_cmd == NULL);
  284. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  285. mvm->scan_status = IWL_MVM_SCAN_OS;
  286. memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
  287. mvm->fw->ucode_capa.max_probe_length +
  288. (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
  289. cmd->channel_count = (u8)req->n_channels;
  290. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  291. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  292. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  293. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, &params);
  294. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  295. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  296. if (params.passive_fragmented)
  297. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  298. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
  299. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  300. MAC_FILTER_IN_BEACON);
  301. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  302. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  303. else
  304. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  305. cmd->repeats = cpu_to_le32(1);
  306. /*
  307. * If the user asked for passive scan, don't change to active scan if
  308. * you see any activity on the channel - remain passive.
  309. */
  310. if (req->n_ssids > 0) {
  311. cmd->passive2active = cpu_to_le16(1);
  312. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  313. if (basic_ssid) {
  314. ssid = req->ssids[0].ssid;
  315. ssid_len = req->ssids[0].ssid_len;
  316. }
  317. } else {
  318. cmd->passive2active = 0;
  319. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  320. }
  321. iwl_mvm_scan_fill_ssids(cmd, req, basic_ssid ? 1 : 0);
  322. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  323. TX_CMD_FLG_BT_DIS);
  324. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  325. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  326. cmd->tx_cmd.rate_n_flags =
  327. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  328. req->no_cck);
  329. cmd->tx_cmd.len =
  330. cpu_to_le16(iwl_mvm_fill_probe_req(
  331. (struct ieee80211_mgmt *)cmd->data,
  332. vif->addr,
  333. req->n_ssids, ssid, ssid_len,
  334. req->ie, req->ie_len,
  335. mvm->fw->ucode_capa.max_probe_length));
  336. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  337. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  338. le16_to_cpu(cmd->tx_cmd.len) +
  339. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  340. hcmd.len[0] = le16_to_cpu(cmd->len);
  341. status = SCAN_RESPONSE_OK;
  342. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  343. if (!ret && status == SCAN_RESPONSE_OK) {
  344. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  345. } else {
  346. /*
  347. * If the scan failed, it usually means that the FW was unable
  348. * to allocate the time events. Warn on it, but maybe we
  349. * should try to send the command again with different params.
  350. */
  351. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  352. status, ret);
  353. mvm->scan_status = IWL_MVM_SCAN_NONE;
  354. ret = -EIO;
  355. }
  356. return ret;
  357. }
  358. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  359. struct iwl_device_cmd *cmd)
  360. {
  361. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  362. struct iwl_cmd_response *resp = (void *)pkt->data;
  363. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  364. le32_to_cpu(resp->status));
  365. return 0;
  366. }
  367. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  368. struct iwl_device_cmd *cmd)
  369. {
  370. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  371. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  372. lockdep_assert_held(&mvm->mutex);
  373. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  374. notif->status, notif->scanned_channels);
  375. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  376. mvm->scan_status = IWL_MVM_SCAN_NONE;
  377. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  378. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  379. return 0;
  380. }
  381. int iwl_mvm_rx_sched_scan_results(struct iwl_mvm *mvm,
  382. struct iwl_rx_cmd_buffer *rxb,
  383. struct iwl_device_cmd *cmd)
  384. {
  385. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  386. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  387. if (notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
  388. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  389. ieee80211_sched_scan_results(mvm->hw);
  390. }
  391. return 0;
  392. }
  393. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  394. struct iwl_rx_packet *pkt, void *data)
  395. {
  396. struct iwl_mvm *mvm =
  397. container_of(notif_wait, struct iwl_mvm, notif_wait);
  398. struct iwl_scan_complete_notif *notif;
  399. u32 *resp;
  400. switch (pkt->hdr.cmd) {
  401. case SCAN_ABORT_CMD:
  402. resp = (void *)pkt->data;
  403. if (*resp == CAN_ABORT_STATUS) {
  404. IWL_DEBUG_SCAN(mvm,
  405. "Scan can be aborted, wait until completion\n");
  406. return false;
  407. }
  408. /*
  409. * If scan cannot be aborted, it means that we had a
  410. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  411. * ieee80211_scan_completed already.
  412. */
  413. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  414. *resp);
  415. return true;
  416. case SCAN_COMPLETE_NOTIFICATION:
  417. notif = (void *)pkt->data;
  418. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  419. notif->status);
  420. return true;
  421. default:
  422. WARN_ON(1);
  423. return false;
  424. };
  425. }
  426. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  427. {
  428. struct iwl_notification_wait wait_scan_abort;
  429. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  430. SCAN_COMPLETE_NOTIFICATION };
  431. int ret;
  432. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  433. return 0;
  434. if (iwl_mvm_is_radio_killed(mvm)) {
  435. ieee80211_scan_completed(mvm->hw, true);
  436. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  437. mvm->scan_status = IWL_MVM_SCAN_NONE;
  438. return 0;
  439. }
  440. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  441. scan_abort_notif,
  442. ARRAY_SIZE(scan_abort_notif),
  443. iwl_mvm_scan_abort_notif, NULL);
  444. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, CMD_SYNC, 0, NULL);
  445. if (ret) {
  446. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  447. /* mac80211's state will be cleaned in the nic_restart flow */
  448. goto out_remove_notif;
  449. }
  450. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  451. out_remove_notif:
  452. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  453. return ret;
  454. }
  455. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  456. struct iwl_rx_cmd_buffer *rxb,
  457. struct iwl_device_cmd *cmd)
  458. {
  459. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  460. struct iwl_scan_offload_complete *scan_notif = (void *)pkt->data;
  461. /* scan status must be locked for proper checking */
  462. lockdep_assert_held(&mvm->mutex);
  463. IWL_DEBUG_SCAN(mvm, "Scheduled scan completed, status %s\n",
  464. scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  465. "completed" : "aborted");
  466. /* only call mac80211 completion if the stop was initiated by FW */
  467. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  468. mvm->scan_status = IWL_MVM_SCAN_NONE;
  469. ieee80211_sched_scan_stopped(mvm->hw);
  470. }
  471. return 0;
  472. }
  473. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  474. struct ieee80211_vif *vif,
  475. struct ieee80211_sched_scan_ies *ies,
  476. enum ieee80211_band band,
  477. struct iwl_tx_cmd *cmd,
  478. u8 *data)
  479. {
  480. u16 cmd_len;
  481. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  482. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  483. cmd->sta_id = mvm->aux_sta.sta_id;
  484. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  485. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  486. vif->addr,
  487. 1, NULL, 0,
  488. ies->ie[band], ies->len[band],
  489. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  490. cmd->len = cpu_to_le16(cmd_len);
  491. }
  492. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  493. struct ieee80211_vif *vif,
  494. struct cfg80211_sched_scan_request *req,
  495. struct iwl_scan_offload_cmd *scan,
  496. struct iwl_mvm_scan_params *params)
  497. {
  498. scan->channel_count =
  499. mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
  500. mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  501. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  502. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  503. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  504. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  505. scan->max_out_time = cpu_to_le32(params->max_out_time);
  506. scan->suspend_time = cpu_to_le32(params->suspend_time);
  507. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  508. MAC_FILTER_IN_BEACON);
  509. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  510. scan->rep_count = cpu_to_le32(1);
  511. if (params->passive_fragmented)
  512. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  513. }
  514. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  515. {
  516. int i;
  517. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  518. if (!ssid_list[i].len)
  519. break;
  520. if (ssid_list[i].len == ssid_len &&
  521. !memcmp(ssid_list->ssid, ssid, ssid_len))
  522. return i;
  523. }
  524. return -1;
  525. }
  526. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  527. struct iwl_scan_offload_cmd *scan,
  528. u32 *ssid_bitmap)
  529. {
  530. int i, j;
  531. int index;
  532. /*
  533. * copy SSIDs from match list.
  534. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  535. * config match list.
  536. */
  537. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  538. /* skip empty SSID matchsets */
  539. if (!req->match_sets[i].ssid.ssid_len)
  540. continue;
  541. scan->direct_scan[i].id = WLAN_EID_SSID;
  542. scan->direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  543. memcpy(scan->direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  544. scan->direct_scan[i].len);
  545. }
  546. /* add SSIDs from scan SSID list */
  547. *ssid_bitmap = 0;
  548. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  549. index = iwl_ssid_exist(req->ssids[j].ssid,
  550. req->ssids[j].ssid_len,
  551. scan->direct_scan);
  552. if (index < 0) {
  553. if (!req->ssids[j].ssid_len)
  554. continue;
  555. scan->direct_scan[i].id = WLAN_EID_SSID;
  556. scan->direct_scan[i].len = req->ssids[j].ssid_len;
  557. memcpy(scan->direct_scan[i].ssid, req->ssids[j].ssid,
  558. scan->direct_scan[i].len);
  559. *ssid_bitmap |= BIT(i + 1);
  560. i++;
  561. } else {
  562. *ssid_bitmap |= BIT(index + 1);
  563. }
  564. }
  565. }
  566. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  567. struct cfg80211_sched_scan_request *req,
  568. struct iwl_scan_channel_cfg *channels,
  569. enum ieee80211_band band,
  570. int *head, int *tail,
  571. u32 ssid_bitmap,
  572. struct iwl_mvm_scan_params *params)
  573. {
  574. struct ieee80211_supported_band *s_band;
  575. int n_channels = req->n_channels;
  576. int i, j, index = 0;
  577. bool partial;
  578. /*
  579. * We have to configure all supported channels, even if we don't want to
  580. * scan on them, but we have to send channels in the order that we want
  581. * to scan. So add requested channels to head of the list and others to
  582. * the end.
  583. */
  584. s_band = &mvm->nvm_data->bands[band];
  585. for (i = 0; i < s_band->n_channels && *head <= *tail; i++) {
  586. partial = false;
  587. for (j = 0; j < n_channels; j++)
  588. if (s_band->channels[i].center_freq ==
  589. req->channels[j]->center_freq) {
  590. index = *head;
  591. (*head)++;
  592. /*
  593. * Channels that came with the request will be
  594. * in partial scan .
  595. */
  596. partial = true;
  597. break;
  598. }
  599. if (!partial) {
  600. index = *tail;
  601. (*tail)--;
  602. }
  603. channels->channel_number[index] =
  604. cpu_to_le16(ieee80211_frequency_to_channel(
  605. s_band->channels[i].center_freq));
  606. channels->dwell_time[index][0] = params->dwell[band].active;
  607. channels->dwell_time[index][1] = params->dwell[band].passive;
  608. channels->iter_count[index] = cpu_to_le16(1);
  609. channels->iter_interval[index] = 0;
  610. if (!(s_band->channels[i].flags & IEEE80211_CHAN_NO_IR))
  611. channels->type[index] |=
  612. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  613. channels->type[index] |=
  614. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL);
  615. if (partial)
  616. channels->type[index] |=
  617. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  618. if (s_band->channels[i].flags & IEEE80211_CHAN_NO_HT40)
  619. channels->type[index] |=
  620. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  621. /* scan for all SSIDs from req->ssids */
  622. channels->type[index] |= cpu_to_le32(ssid_bitmap);
  623. }
  624. }
  625. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  626. struct ieee80211_vif *vif,
  627. struct cfg80211_sched_scan_request *req,
  628. struct ieee80211_sched_scan_ies *ies)
  629. {
  630. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  631. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  632. int head = 0;
  633. int tail = band_2ghz + band_5ghz;
  634. u32 ssid_bitmap;
  635. int cmd_len;
  636. int ret;
  637. struct iwl_scan_offload_cfg *scan_cfg;
  638. struct iwl_host_cmd cmd = {
  639. .id = SCAN_OFFLOAD_CONFIG_CMD,
  640. .flags = CMD_SYNC,
  641. };
  642. struct iwl_mvm_scan_params params = {};
  643. lockdep_assert_held(&mvm->mutex);
  644. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  645. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  646. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  647. if (!scan_cfg)
  648. return -ENOMEM;
  649. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, &params);
  650. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  651. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  652. iwl_scan_offload_build_ssid(req, &scan_cfg->scan_cmd, &ssid_bitmap);
  653. /* build tx frames for supported bands */
  654. if (band_2ghz) {
  655. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  656. IEEE80211_BAND_2GHZ,
  657. &scan_cfg->scan_cmd.tx_cmd[0],
  658. scan_cfg->data);
  659. iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
  660. IEEE80211_BAND_2GHZ, &head, &tail,
  661. ssid_bitmap, &params);
  662. }
  663. if (band_5ghz) {
  664. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  665. IEEE80211_BAND_5GHZ,
  666. &scan_cfg->scan_cmd.tx_cmd[1],
  667. scan_cfg->data +
  668. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  669. iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
  670. IEEE80211_BAND_5GHZ, &head, &tail,
  671. ssid_bitmap, &params);
  672. }
  673. cmd.data[0] = scan_cfg;
  674. cmd.len[0] = cmd_len;
  675. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  676. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  677. ret = iwl_mvm_send_cmd(mvm, &cmd);
  678. kfree(scan_cfg);
  679. return ret;
  680. }
  681. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  682. struct cfg80211_sched_scan_request *req)
  683. {
  684. struct iwl_scan_offload_profile *profile;
  685. struct iwl_scan_offload_profile_cfg *profile_cfg;
  686. struct iwl_scan_offload_blacklist *blacklist;
  687. struct iwl_host_cmd cmd = {
  688. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  689. .flags = CMD_SYNC,
  690. .len[1] = sizeof(*profile_cfg),
  691. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  692. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  693. };
  694. int blacklist_len;
  695. int i;
  696. int ret;
  697. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  698. return -EIO;
  699. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  700. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  701. else
  702. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  703. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  704. if (!blacklist)
  705. return -ENOMEM;
  706. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  707. if (!profile_cfg) {
  708. ret = -ENOMEM;
  709. goto free_blacklist;
  710. }
  711. cmd.data[0] = blacklist;
  712. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  713. cmd.data[1] = profile_cfg;
  714. /* No blacklist configuration */
  715. profile_cfg->num_profiles = req->n_match_sets;
  716. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  717. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  718. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  719. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  720. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  721. for (i = 0; i < req->n_match_sets; i++) {
  722. profile = &profile_cfg->profiles[i];
  723. profile->ssid_index = i;
  724. /* Support any cipher and auth algorithm */
  725. profile->unicast_cipher = 0xff;
  726. profile->auth_alg = 0xff;
  727. profile->network_type = IWL_NETWORK_TYPE_ANY;
  728. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  729. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  730. }
  731. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  732. ret = iwl_mvm_send_cmd(mvm, &cmd);
  733. kfree(profile_cfg);
  734. free_blacklist:
  735. kfree(blacklist);
  736. return ret;
  737. }
  738. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  739. struct cfg80211_sched_scan_request *req)
  740. {
  741. struct iwl_scan_offload_req scan_req = {
  742. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  743. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  744. .schedule_line[0].delay = req->interval / 1000,
  745. .schedule_line[0].full_scan_mul = 1,
  746. .schedule_line[1].iterations = 0xff,
  747. .schedule_line[1].delay = req->interval / 1000,
  748. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  749. };
  750. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  751. IWL_DEBUG_SCAN(mvm,
  752. "Sending scheduled scan with filtering, filter len %d\n",
  753. req->n_match_sets);
  754. } else {
  755. IWL_DEBUG_SCAN(mvm,
  756. "Sending Scheduled scan without filtering\n");
  757. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  758. }
  759. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, CMD_SYNC,
  760. sizeof(scan_req), &scan_req);
  761. }
  762. static int iwl_mvm_send_sched_scan_abort(struct iwl_mvm *mvm)
  763. {
  764. int ret;
  765. struct iwl_host_cmd cmd = {
  766. .id = SCAN_OFFLOAD_ABORT_CMD,
  767. .flags = CMD_SYNC,
  768. };
  769. u32 status;
  770. /* Exit instantly with error when device is not ready
  771. * to receive scan abort command or it does not perform
  772. * scheduled scan currently */
  773. if (mvm->scan_status != IWL_MVM_SCAN_SCHED)
  774. return -EIO;
  775. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  776. if (ret)
  777. return ret;
  778. if (status != CAN_ABORT_STATUS) {
  779. /*
  780. * The scan abort will return 1 for success or
  781. * 2 for "failure". A failure condition can be
  782. * due to simply not being in an active scan which
  783. * can occur if we send the scan abort before the
  784. * microcode has notified us that a scan is completed.
  785. */
  786. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  787. ret = -ENOENT;
  788. }
  789. return ret;
  790. }
  791. int iwl_mvm_sched_scan_stop(struct iwl_mvm *mvm)
  792. {
  793. int ret;
  794. struct iwl_notification_wait wait_scan_done;
  795. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  796. lockdep_assert_held(&mvm->mutex);
  797. if (mvm->scan_status != IWL_MVM_SCAN_SCHED) {
  798. IWL_DEBUG_SCAN(mvm, "No offloaded scan to stop\n");
  799. return 0;
  800. }
  801. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  802. scan_done_notif,
  803. ARRAY_SIZE(scan_done_notif),
  804. NULL, NULL);
  805. ret = iwl_mvm_send_sched_scan_abort(mvm);
  806. if (ret) {
  807. IWL_DEBUG_SCAN(mvm, "Send stop offload scan failed %d\n", ret);
  808. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  809. return ret;
  810. }
  811. IWL_DEBUG_SCAN(mvm, "Successfully sent stop offload scan\n");
  812. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  813. if (ret)
  814. return ret;
  815. /*
  816. * Clear the scan status so the next scan requests will succeed. This
  817. * also ensures the Rx handler doesn't do anything, as the scan was
  818. * stopped from above.
  819. */
  820. mvm->scan_status = IWL_MVM_SCAN_NONE;
  821. return 0;
  822. }