scan.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. #define LONG_OUT_TIME_PERIOD 600
  71. #define SHORT_OUT_TIME_PERIOD 200
  72. #define SUSPEND_TIME_PERIOD 100
  73. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  74. {
  75. u16 rx_chain;
  76. u8 rx_ant;
  77. if (mvm->scan_rx_ant != ANT_NONE)
  78. rx_ant = mvm->scan_rx_ant;
  79. else
  80. rx_ant = iwl_fw_valid_rx_ant(mvm->fw);
  81. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  82. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  83. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  84. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  85. return cpu_to_le16(rx_chain);
  86. }
  87. static inline __le32 iwl_mvm_scan_max_out_time(struct ieee80211_vif *vif,
  88. u32 flags, bool is_assoc)
  89. {
  90. if (!is_assoc)
  91. return 0;
  92. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  93. return cpu_to_le32(ieee80211_tu_to_usec(SHORT_OUT_TIME_PERIOD));
  94. return cpu_to_le32(ieee80211_tu_to_usec(LONG_OUT_TIME_PERIOD));
  95. }
  96. static inline __le32 iwl_mvm_scan_suspend_time(struct ieee80211_vif *vif,
  97. bool is_assoc)
  98. {
  99. if (!is_assoc)
  100. return 0;
  101. return cpu_to_le32(ieee80211_tu_to_usec(SUSPEND_TIME_PERIOD));
  102. }
  103. static inline __le32
  104. iwl_mvm_scan_rxon_flags(struct cfg80211_scan_request *req)
  105. {
  106. if (req->channels[0]->band == IEEE80211_BAND_2GHZ)
  107. return cpu_to_le32(PHY_BAND_24);
  108. else
  109. return cpu_to_le32(PHY_BAND_5);
  110. }
  111. static inline __le32
  112. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  113. bool no_cck)
  114. {
  115. u32 tx_ant;
  116. mvm->scan_last_antenna_idx =
  117. iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
  118. mvm->scan_last_antenna_idx);
  119. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  120. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  121. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  122. tx_ant);
  123. else
  124. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  125. }
  126. /*
  127. * We insert the SSIDs in an inverted order, because the FW will
  128. * invert it back. The most prioritized SSID, which is first in the
  129. * request list, is not copied here, but inserted directly to the probe
  130. * request.
  131. */
  132. static void iwl_mvm_scan_fill_ssids(struct iwl_scan_cmd *cmd,
  133. struct cfg80211_scan_request *req,
  134. int first)
  135. {
  136. int fw_idx, req_idx;
  137. for (req_idx = req->n_ssids - 1, fw_idx = 0; req_idx >= first;
  138. req_idx--, fw_idx++) {
  139. cmd->direct_scan[fw_idx].id = WLAN_EID_SSID;
  140. cmd->direct_scan[fw_idx].len = req->ssids[req_idx].ssid_len;
  141. memcpy(cmd->direct_scan[fw_idx].ssid,
  142. req->ssids[req_idx].ssid,
  143. req->ssids[req_idx].ssid_len);
  144. }
  145. }
  146. /*
  147. * If req->n_ssids > 0, it means we should do an active scan.
  148. * In case of active scan w/o directed scan, we receive a zero-length SSID
  149. * just to notify that this scan is active and not passive.
  150. * In order to notify the FW of the number of SSIDs we wish to scan (including
  151. * the zero-length one), we need to set the corresponding bits in chan->type,
  152. * one for each SSID, and set the active bit (first). If the first SSID is
  153. * already included in the probe template, so we need to set only
  154. * req->n_ssids - 1 bits in addition to the first bit.
  155. */
  156. static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
  157. {
  158. if (band == IEEE80211_BAND_2GHZ)
  159. return 30 + 3 * (n_ssids + 1);
  160. return 20 + 2 * (n_ssids + 1);
  161. }
  162. static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
  163. {
  164. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  165. }
  166. static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
  167. struct cfg80211_scan_request *req,
  168. bool basic_ssid)
  169. {
  170. u16 passive_dwell = iwl_mvm_get_passive_dwell(req->channels[0]->band);
  171. u16 active_dwell = iwl_mvm_get_active_dwell(req->channels[0]->band,
  172. req->n_ssids);
  173. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  174. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  175. int i;
  176. int type = BIT(req->n_ssids) - 1;
  177. if (!basic_ssid)
  178. type |= BIT(req->n_ssids);
  179. for (i = 0; i < cmd->channel_count; i++) {
  180. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  181. chan->type = cpu_to_le32(type);
  182. if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
  183. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  184. chan->active_dwell = cpu_to_le16(active_dwell);
  185. chan->passive_dwell = cpu_to_le16(passive_dwell);
  186. chan->iteration_count = cpu_to_le16(1);
  187. chan++;
  188. }
  189. }
  190. /*
  191. * Fill in probe request with the following parameters:
  192. * TA is our vif HW address, which mac80211 ensures we have.
  193. * Packet is broadcasted, so this is both SA and DA.
  194. * The probe request IE is made out of two: first comes the most prioritized
  195. * SSID if a directed scan is requested. Second comes whatever extra
  196. * information was given to us as the scan request IE.
  197. */
  198. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  199. int n_ssids, const u8 *ssid, int ssid_len,
  200. const u8 *ie, int ie_len,
  201. int left)
  202. {
  203. int len = 0;
  204. u8 *pos = NULL;
  205. /* Make sure there is enough space for the probe request,
  206. * two mandatory IEs and the data */
  207. left -= 24;
  208. if (left < 0)
  209. return 0;
  210. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  211. eth_broadcast_addr(frame->da);
  212. memcpy(frame->sa, ta, ETH_ALEN);
  213. eth_broadcast_addr(frame->bssid);
  214. frame->seq_ctrl = 0;
  215. len += 24;
  216. /* for passive scans, no need to fill anything */
  217. if (n_ssids == 0)
  218. return (u16)len;
  219. /* points to the payload of the request */
  220. pos = &frame->u.probe_req.variable[0];
  221. /* fill in our SSID IE */
  222. left -= ssid_len + 2;
  223. if (left < 0)
  224. return 0;
  225. *pos++ = WLAN_EID_SSID;
  226. *pos++ = ssid_len;
  227. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  228. memcpy(pos, ssid, ssid_len);
  229. pos += ssid_len;
  230. }
  231. len += ssid_len + 2;
  232. if (WARN_ON(left < ie_len))
  233. return len;
  234. if (ie && ie_len) {
  235. memcpy(pos, ie, ie_len);
  236. len += ie_len;
  237. }
  238. return (u16)len;
  239. }
  240. static void iwl_mvm_vif_assoc_iterator(void *data, u8 *mac,
  241. struct ieee80211_vif *vif)
  242. {
  243. bool *is_assoc = data;
  244. if (vif->bss_conf.assoc)
  245. *is_assoc = true;
  246. }
  247. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  248. struct ieee80211_vif *vif,
  249. struct cfg80211_scan_request *req)
  250. {
  251. struct iwl_host_cmd hcmd = {
  252. .id = SCAN_REQUEST_CMD,
  253. .len = { 0, },
  254. .data = { mvm->scan_cmd, },
  255. .flags = CMD_SYNC,
  256. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  257. };
  258. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  259. bool is_assoc = false;
  260. int ret;
  261. u32 status;
  262. int ssid_len = 0;
  263. u8 *ssid = NULL;
  264. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  265. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  266. lockdep_assert_held(&mvm->mutex);
  267. BUG_ON(mvm->scan_cmd == NULL);
  268. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  269. mvm->scan_status = IWL_MVM_SCAN_OS;
  270. memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
  271. mvm->fw->ucode_capa.max_probe_length +
  272. (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
  273. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  274. IEEE80211_IFACE_ITER_NORMAL,
  275. iwl_mvm_vif_assoc_iterator,
  276. &is_assoc);
  277. cmd->channel_count = (u8)req->n_channels;
  278. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  279. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  280. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  281. cmd->max_out_time = iwl_mvm_scan_max_out_time(vif, req->flags,
  282. is_assoc);
  283. cmd->suspend_time = iwl_mvm_scan_suspend_time(vif, is_assoc);
  284. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
  285. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  286. MAC_FILTER_IN_BEACON);
  287. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  288. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  289. else
  290. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  291. cmd->repeats = cpu_to_le32(1);
  292. /*
  293. * If the user asked for passive scan, don't change to active scan if
  294. * you see any activity on the channel - remain passive.
  295. */
  296. if (req->n_ssids > 0) {
  297. cmd->passive2active = cpu_to_le16(1);
  298. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  299. if (basic_ssid) {
  300. ssid = req->ssids[0].ssid;
  301. ssid_len = req->ssids[0].ssid_len;
  302. }
  303. } else {
  304. cmd->passive2active = 0;
  305. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  306. }
  307. iwl_mvm_scan_fill_ssids(cmd, req, basic_ssid ? 1 : 0);
  308. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  309. TX_CMD_FLG_BT_DIS);
  310. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  311. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  312. cmd->tx_cmd.rate_n_flags =
  313. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  314. req->no_cck);
  315. cmd->tx_cmd.len =
  316. cpu_to_le16(iwl_mvm_fill_probe_req(
  317. (struct ieee80211_mgmt *)cmd->data,
  318. vif->addr,
  319. req->n_ssids, ssid, ssid_len,
  320. req->ie, req->ie_len,
  321. mvm->fw->ucode_capa.max_probe_length));
  322. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid);
  323. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  324. le16_to_cpu(cmd->tx_cmd.len) +
  325. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  326. hcmd.len[0] = le16_to_cpu(cmd->len);
  327. status = SCAN_RESPONSE_OK;
  328. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  329. if (!ret && status == SCAN_RESPONSE_OK) {
  330. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  331. } else {
  332. /*
  333. * If the scan failed, it usually means that the FW was unable
  334. * to allocate the time events. Warn on it, but maybe we
  335. * should try to send the command again with different params.
  336. */
  337. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  338. status, ret);
  339. mvm->scan_status = IWL_MVM_SCAN_NONE;
  340. ret = -EIO;
  341. }
  342. return ret;
  343. }
  344. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  345. struct iwl_device_cmd *cmd)
  346. {
  347. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  348. struct iwl_cmd_response *resp = (void *)pkt->data;
  349. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  350. le32_to_cpu(resp->status));
  351. return 0;
  352. }
  353. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  354. struct iwl_device_cmd *cmd)
  355. {
  356. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  357. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  358. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  359. notif->status, notif->scanned_channels);
  360. mvm->scan_status = IWL_MVM_SCAN_NONE;
  361. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  362. return 0;
  363. }
  364. int iwl_mvm_rx_sched_scan_results(struct iwl_mvm *mvm,
  365. struct iwl_rx_cmd_buffer *rxb,
  366. struct iwl_device_cmd *cmd)
  367. {
  368. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  369. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  370. if (notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
  371. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  372. ieee80211_sched_scan_results(mvm->hw);
  373. }
  374. return 0;
  375. }
  376. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  377. struct iwl_rx_packet *pkt, void *data)
  378. {
  379. struct iwl_mvm *mvm =
  380. container_of(notif_wait, struct iwl_mvm, notif_wait);
  381. struct iwl_scan_complete_notif *notif;
  382. u32 *resp;
  383. switch (pkt->hdr.cmd) {
  384. case SCAN_ABORT_CMD:
  385. resp = (void *)pkt->data;
  386. if (*resp == CAN_ABORT_STATUS) {
  387. IWL_DEBUG_SCAN(mvm,
  388. "Scan can be aborted, wait until completion\n");
  389. return false;
  390. }
  391. /*
  392. * If scan cannot be aborted, it means that we had a
  393. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  394. * ieee80211_scan_completed already.
  395. */
  396. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  397. *resp);
  398. return true;
  399. case SCAN_COMPLETE_NOTIFICATION:
  400. notif = (void *)pkt->data;
  401. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  402. notif->status);
  403. return true;
  404. default:
  405. WARN_ON(1);
  406. return false;
  407. };
  408. }
  409. void iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  410. {
  411. struct iwl_notification_wait wait_scan_abort;
  412. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  413. SCAN_COMPLETE_NOTIFICATION };
  414. int ret;
  415. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  416. return;
  417. if (iwl_mvm_is_radio_killed(mvm)) {
  418. ieee80211_scan_completed(mvm->hw, true);
  419. mvm->scan_status = IWL_MVM_SCAN_NONE;
  420. return;
  421. }
  422. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  423. scan_abort_notif,
  424. ARRAY_SIZE(scan_abort_notif),
  425. iwl_mvm_scan_abort_notif, NULL);
  426. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, CMD_SYNC, 0, NULL);
  427. if (ret) {
  428. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  429. /* mac80211's state will be cleaned in the fw_restart flow */
  430. goto out_remove_notif;
  431. }
  432. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, 1 * HZ);
  433. if (ret)
  434. IWL_ERR(mvm, "%s - failed on timeout\n", __func__);
  435. return;
  436. out_remove_notif:
  437. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  438. }
  439. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  440. struct iwl_rx_cmd_buffer *rxb,
  441. struct iwl_device_cmd *cmd)
  442. {
  443. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  444. struct iwl_scan_offload_complete *scan_notif = (void *)pkt->data;
  445. IWL_DEBUG_SCAN(mvm, "Scheduled scan completed, status %s\n",
  446. scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
  447. "completed" : "aborted");
  448. mvm->scan_status = IWL_MVM_SCAN_NONE;
  449. ieee80211_sched_scan_stopped(mvm->hw);
  450. return 0;
  451. }
  452. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  453. struct ieee80211_vif *vif,
  454. struct ieee80211_sched_scan_ies *ies,
  455. enum ieee80211_band band,
  456. struct iwl_tx_cmd *cmd,
  457. u8 *data)
  458. {
  459. u16 cmd_len;
  460. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  461. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  462. cmd->sta_id = mvm->aux_sta.sta_id;
  463. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  464. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  465. vif->addr,
  466. 1, NULL, 0,
  467. ies->ie[band], ies->len[band],
  468. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  469. cmd->len = cpu_to_le16(cmd_len);
  470. }
  471. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  472. struct ieee80211_vif *vif,
  473. struct cfg80211_sched_scan_request *req,
  474. struct iwl_scan_offload_cmd *scan)
  475. {
  476. bool is_assoc = false;
  477. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  478. IEEE80211_IFACE_ITER_NORMAL,
  479. iwl_mvm_vif_assoc_iterator,
  480. &is_assoc);
  481. scan->channel_count =
  482. mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
  483. mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  484. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  485. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  486. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  487. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  488. scan->max_out_time = iwl_mvm_scan_max_out_time(vif, req->flags,
  489. is_assoc);
  490. scan->suspend_time = iwl_mvm_scan_suspend_time(vif, is_assoc);
  491. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  492. MAC_FILTER_IN_BEACON);
  493. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  494. scan->rep_count = cpu_to_le32(1);
  495. }
  496. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  497. {
  498. int i;
  499. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  500. if (!ssid_list[i].len)
  501. break;
  502. if (ssid_list[i].len == ssid_len &&
  503. !memcmp(ssid_list->ssid, ssid, ssid_len))
  504. return i;
  505. }
  506. return -1;
  507. }
  508. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  509. struct iwl_scan_offload_cmd *scan,
  510. u32 *ssid_bitmap)
  511. {
  512. int i, j;
  513. int index;
  514. /*
  515. * copy SSIDs from match list.
  516. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  517. * config match list.
  518. */
  519. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  520. scan->direct_scan[i].id = WLAN_EID_SSID;
  521. scan->direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  522. memcpy(scan->direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  523. scan->direct_scan[i].len);
  524. }
  525. /* add SSIDs from scan SSID list */
  526. *ssid_bitmap = 0;
  527. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  528. index = iwl_ssid_exist(req->ssids[j].ssid,
  529. req->ssids[j].ssid_len,
  530. scan->direct_scan);
  531. if (index < 0) {
  532. if (!req->ssids[j].ssid_len)
  533. continue;
  534. scan->direct_scan[i].id = WLAN_EID_SSID;
  535. scan->direct_scan[i].len = req->ssids[j].ssid_len;
  536. memcpy(scan->direct_scan[i].ssid, req->ssids[j].ssid,
  537. scan->direct_scan[i].len);
  538. *ssid_bitmap |= BIT(i + 1);
  539. i++;
  540. } else {
  541. *ssid_bitmap |= BIT(index + 1);
  542. }
  543. }
  544. }
  545. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  546. struct cfg80211_sched_scan_request *req,
  547. struct iwl_scan_channel_cfg *channels,
  548. enum ieee80211_band band,
  549. int *head, int *tail,
  550. u32 ssid_bitmap)
  551. {
  552. struct ieee80211_supported_band *s_band;
  553. int n_probes = req->n_ssids;
  554. int n_channels = req->n_channels;
  555. u8 active_dwell, passive_dwell;
  556. int i, j, index = 0;
  557. bool partial;
  558. /*
  559. * We have to configure all supported channels, even if we don't want to
  560. * scan on them, but we have to send channels in the order that we want
  561. * to scan. So add requested channels to head of the list and others to
  562. * the end.
  563. */
  564. active_dwell = iwl_mvm_get_active_dwell(band, n_probes);
  565. passive_dwell = iwl_mvm_get_passive_dwell(band);
  566. s_band = &mvm->nvm_data->bands[band];
  567. for (i = 0; i < s_band->n_channels && *head <= *tail; i++) {
  568. partial = false;
  569. for (j = 0; j < n_channels; j++)
  570. if (s_band->channels[i].center_freq ==
  571. req->channels[j]->center_freq) {
  572. index = *head;
  573. (*head)++;
  574. /*
  575. * Channels that came with the request will be
  576. * in partial scan .
  577. */
  578. partial = true;
  579. break;
  580. }
  581. if (!partial) {
  582. index = *tail;
  583. (*tail)--;
  584. }
  585. channels->channel_number[index] =
  586. cpu_to_le16(ieee80211_frequency_to_channel(
  587. s_band->channels[i].center_freq));
  588. channels->dwell_time[index][0] = active_dwell;
  589. channels->dwell_time[index][1] = passive_dwell;
  590. channels->iter_count[index] = cpu_to_le16(1);
  591. channels->iter_interval[index] = 0;
  592. if (!(s_band->channels[i].flags & IEEE80211_CHAN_NO_IR))
  593. channels->type[index] |=
  594. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  595. channels->type[index] |=
  596. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL);
  597. if (partial)
  598. channels->type[index] |=
  599. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  600. if (s_band->channels[i].flags & IEEE80211_CHAN_NO_HT40)
  601. channels->type[index] |=
  602. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  603. /* scan for all SSIDs from req->ssids */
  604. channels->type[index] |= cpu_to_le32(ssid_bitmap);
  605. }
  606. }
  607. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  608. struct ieee80211_vif *vif,
  609. struct cfg80211_sched_scan_request *req,
  610. struct ieee80211_sched_scan_ies *ies)
  611. {
  612. int supported_bands = 0;
  613. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  614. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  615. int head = 0;
  616. int tail = band_2ghz + band_5ghz;
  617. u32 ssid_bitmap;
  618. int cmd_len;
  619. int ret;
  620. struct iwl_scan_offload_cfg *scan_cfg;
  621. struct iwl_host_cmd cmd = {
  622. .id = SCAN_OFFLOAD_CONFIG_CMD,
  623. .flags = CMD_SYNC,
  624. };
  625. lockdep_assert_held(&mvm->mutex);
  626. if (band_2ghz)
  627. supported_bands++;
  628. if (band_5ghz)
  629. supported_bands++;
  630. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  631. supported_bands * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  632. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  633. if (!scan_cfg)
  634. return -ENOMEM;
  635. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd);
  636. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  637. iwl_scan_offload_build_ssid(req, &scan_cfg->scan_cmd, &ssid_bitmap);
  638. /* build tx frames for supported bands */
  639. if (band_2ghz) {
  640. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  641. IEEE80211_BAND_2GHZ,
  642. &scan_cfg->scan_cmd.tx_cmd[0],
  643. scan_cfg->data);
  644. iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
  645. IEEE80211_BAND_2GHZ, &head, &tail,
  646. ssid_bitmap);
  647. }
  648. if (band_5ghz) {
  649. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  650. IEEE80211_BAND_5GHZ,
  651. &scan_cfg->scan_cmd.tx_cmd[1],
  652. scan_cfg->data +
  653. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  654. iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
  655. IEEE80211_BAND_5GHZ, &head, &tail,
  656. ssid_bitmap);
  657. }
  658. cmd.data[0] = scan_cfg;
  659. cmd.len[0] = cmd_len;
  660. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  661. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  662. ret = iwl_mvm_send_cmd(mvm, &cmd);
  663. kfree(scan_cfg);
  664. return ret;
  665. }
  666. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  667. struct cfg80211_sched_scan_request *req)
  668. {
  669. struct iwl_scan_offload_profile *profile;
  670. struct iwl_scan_offload_profile_cfg *profile_cfg;
  671. struct iwl_scan_offload_blacklist *blacklist;
  672. struct iwl_host_cmd cmd = {
  673. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  674. .flags = CMD_SYNC,
  675. .len[1] = sizeof(*profile_cfg),
  676. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  677. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  678. };
  679. int blacklist_len;
  680. int i;
  681. int ret;
  682. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  683. return -EIO;
  684. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  685. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  686. else
  687. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  688. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  689. if (!blacklist)
  690. return -ENOMEM;
  691. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  692. if (!profile_cfg) {
  693. ret = -ENOMEM;
  694. goto free_blacklist;
  695. }
  696. cmd.data[0] = blacklist;
  697. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  698. cmd.data[1] = profile_cfg;
  699. /* No blacklist configuration */
  700. profile_cfg->num_profiles = req->n_match_sets;
  701. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  702. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  703. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  704. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  705. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  706. for (i = 0; i < req->n_match_sets; i++) {
  707. profile = &profile_cfg->profiles[i];
  708. profile->ssid_index = i;
  709. /* Support any cipher and auth algorithm */
  710. profile->unicast_cipher = 0xff;
  711. profile->auth_alg = 0xff;
  712. profile->network_type = IWL_NETWORK_TYPE_ANY;
  713. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  714. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  715. }
  716. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  717. ret = iwl_mvm_send_cmd(mvm, &cmd);
  718. kfree(profile_cfg);
  719. free_blacklist:
  720. kfree(blacklist);
  721. return ret;
  722. }
  723. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  724. struct cfg80211_sched_scan_request *req)
  725. {
  726. struct iwl_scan_offload_req scan_req = {
  727. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  728. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  729. .schedule_line[0].delay = req->interval / 1000,
  730. .schedule_line[0].full_scan_mul = 1,
  731. .schedule_line[1].iterations = 0xff,
  732. .schedule_line[1].delay = req->interval / 1000,
  733. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  734. };
  735. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  736. IWL_DEBUG_SCAN(mvm,
  737. "Sending scheduled scan with filtering, filter len %d\n",
  738. req->n_match_sets);
  739. } else {
  740. IWL_DEBUG_SCAN(mvm,
  741. "Sending Scheduled scan without filtering\n");
  742. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  743. }
  744. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, CMD_SYNC,
  745. sizeof(scan_req), &scan_req);
  746. }
  747. static int iwl_mvm_send_sched_scan_abort(struct iwl_mvm *mvm)
  748. {
  749. int ret;
  750. struct iwl_host_cmd cmd = {
  751. .id = SCAN_OFFLOAD_ABORT_CMD,
  752. .flags = CMD_SYNC,
  753. };
  754. u32 status;
  755. /* Exit instantly with error when device is not ready
  756. * to receive scan abort command or it does not perform
  757. * scheduled scan currently */
  758. if (mvm->scan_status != IWL_MVM_SCAN_SCHED)
  759. return -EIO;
  760. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  761. if (ret)
  762. return ret;
  763. if (status != CAN_ABORT_STATUS) {
  764. /*
  765. * The scan abort will return 1 for success or
  766. * 2 for "failure". A failure condition can be
  767. * due to simply not being in an active scan which
  768. * can occur if we send the scan abort before the
  769. * microcode has notified us that a scan is completed.
  770. */
  771. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  772. ret = -EIO;
  773. }
  774. return ret;
  775. }
  776. void iwl_mvm_sched_scan_stop(struct iwl_mvm *mvm)
  777. {
  778. int ret;
  779. lockdep_assert_held(&mvm->mutex);
  780. if (mvm->scan_status != IWL_MVM_SCAN_SCHED) {
  781. IWL_DEBUG_SCAN(mvm, "No offloaded scan to stop\n");
  782. return;
  783. }
  784. ret = iwl_mvm_send_sched_scan_abort(mvm);
  785. if (ret)
  786. IWL_DEBUG_SCAN(mvm, "Send stop offload scan failed %d\n", ret);
  787. else
  788. IWL_DEBUG_SCAN(mvm, "Successfully sent stop offload scan\n");
  789. }