scan.c 25 KB

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