scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 = iwl_fw_valid_rx_ant(mvm->fw);
  74. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  75. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  76. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  77. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  78. return cpu_to_le16(rx_chain);
  79. }
  80. static inline __le32 iwl_mvm_scan_max_out_time(struct ieee80211_vif *vif)
  81. {
  82. if (vif->bss_conf.assoc)
  83. return cpu_to_le32(200 * 1024);
  84. else
  85. return 0;
  86. }
  87. static inline __le32 iwl_mvm_scan_suspend_time(struct ieee80211_vif *vif)
  88. {
  89. if (vif->bss_conf.assoc)
  90. return cpu_to_le32(vif->bss_conf.beacon_int);
  91. else
  92. return 0;
  93. }
  94. static inline __le32
  95. iwl_mvm_scan_rxon_flags(struct cfg80211_scan_request *req)
  96. {
  97. if (req->channels[0]->band == IEEE80211_BAND_2GHZ)
  98. return cpu_to_le32(PHY_BAND_24);
  99. else
  100. return cpu_to_le32(PHY_BAND_5);
  101. }
  102. static inline __le32
  103. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  104. bool no_cck)
  105. {
  106. u32 tx_ant;
  107. mvm->scan_last_antenna_idx =
  108. iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
  109. mvm->scan_last_antenna_idx);
  110. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  111. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  112. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  113. tx_ant);
  114. else
  115. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  116. }
  117. /*
  118. * We insert the SSIDs in an inverted order, because the FW will
  119. * invert it back. The most prioritized SSID, which is first in the
  120. * request list, is not copied here, but inserted directly to the probe
  121. * request.
  122. */
  123. static void iwl_mvm_scan_fill_ssids(struct iwl_scan_cmd *cmd,
  124. struct cfg80211_scan_request *req)
  125. {
  126. int fw_idx, req_idx;
  127. for (req_idx = req->n_ssids - 1, fw_idx = 0; req_idx > 0;
  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). The first SSID is already
  143. * included in the probe template, so we need to set only req->n_ssids - 1 bits
  144. * 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. {
  159. u16 passive_dwell = iwl_mvm_get_passive_dwell(req->channels[0]->band);
  160. u16 active_dwell = iwl_mvm_get_active_dwell(req->channels[0]->band,
  161. req->n_ssids);
  162. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  163. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  164. int i;
  165. for (i = 0; i < cmd->channel_count; i++) {
  166. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  167. chan->type = cpu_to_le32(BIT(req->n_ssids) - 1);
  168. if (req->channels[i]->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  169. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  170. chan->active_dwell = cpu_to_le16(active_dwell);
  171. chan->passive_dwell = cpu_to_le16(passive_dwell);
  172. chan->iteration_count = cpu_to_le16(1);
  173. chan++;
  174. }
  175. }
  176. /*
  177. * Fill in probe request with the following parameters:
  178. * TA is our vif HW address, which mac80211 ensures we have.
  179. * Packet is broadcasted, so this is both SA and DA.
  180. * The probe request IE is made out of two: first comes the most prioritized
  181. * SSID if a directed scan is requested. Second comes whatever extra
  182. * information was given to us as the scan request IE.
  183. */
  184. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  185. int n_ssids, const u8 *ssid, int ssid_len,
  186. const u8 *ie, int ie_len,
  187. int left)
  188. {
  189. int len = 0;
  190. u8 *pos = NULL;
  191. /* Make sure there is enough space for the probe request,
  192. * two mandatory IEs and the data */
  193. left -= 24;
  194. if (left < 0)
  195. return 0;
  196. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  197. eth_broadcast_addr(frame->da);
  198. memcpy(frame->sa, ta, ETH_ALEN);
  199. eth_broadcast_addr(frame->bssid);
  200. frame->seq_ctrl = 0;
  201. len += 24;
  202. /* for passive scans, no need to fill anything */
  203. if (n_ssids == 0)
  204. return (u16)len;
  205. /* points to the payload of the request */
  206. pos = &frame->u.probe_req.variable[0];
  207. /* fill in our SSID IE */
  208. left -= ssid_len + 2;
  209. if (left < 0)
  210. return 0;
  211. *pos++ = WLAN_EID_SSID;
  212. *pos++ = ssid_len;
  213. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  214. memcpy(pos, ssid, ssid_len);
  215. pos += ssid_len;
  216. }
  217. len += ssid_len + 2;
  218. if (WARN_ON(left < ie_len))
  219. return len;
  220. if (ie && ie_len) {
  221. memcpy(pos, ie, ie_len);
  222. len += ie_len;
  223. }
  224. return (u16)len;
  225. }
  226. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  227. struct ieee80211_vif *vif,
  228. struct cfg80211_scan_request *req)
  229. {
  230. struct iwl_host_cmd hcmd = {
  231. .id = SCAN_REQUEST_CMD,
  232. .len = { 0, },
  233. .data = { mvm->scan_cmd, },
  234. .flags = CMD_SYNC,
  235. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  236. };
  237. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  238. int ret;
  239. u32 status;
  240. int ssid_len = 0;
  241. u8 *ssid = NULL;
  242. lockdep_assert_held(&mvm->mutex);
  243. BUG_ON(mvm->scan_cmd == NULL);
  244. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  245. mvm->scan_status = IWL_MVM_SCAN_OS;
  246. memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
  247. mvm->fw->ucode_capa.max_probe_length +
  248. (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
  249. cmd->channel_count = (u8)req->n_channels;
  250. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  251. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  252. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  253. cmd->max_out_time = iwl_mvm_scan_max_out_time(vif);
  254. cmd->suspend_time = iwl_mvm_scan_suspend_time(vif);
  255. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
  256. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  257. MAC_FILTER_IN_BEACON);
  258. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  259. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  260. else
  261. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  262. cmd->repeats = cpu_to_le32(1);
  263. /*
  264. * If the user asked for passive scan, don't change to active scan if
  265. * you see any activity on the channel - remain passive.
  266. */
  267. if (req->n_ssids > 0) {
  268. cmd->passive2active = cpu_to_le16(1);
  269. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  270. ssid = req->ssids[0].ssid;
  271. ssid_len = req->ssids[0].ssid_len;
  272. } else {
  273. cmd->passive2active = 0;
  274. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  275. }
  276. iwl_mvm_scan_fill_ssids(cmd, req);
  277. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  278. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  279. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  280. cmd->tx_cmd.rate_n_flags =
  281. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  282. req->no_cck);
  283. cmd->tx_cmd.len =
  284. cpu_to_le16(iwl_mvm_fill_probe_req(
  285. (struct ieee80211_mgmt *)cmd->data,
  286. vif->addr,
  287. req->n_ssids, ssid, ssid_len,
  288. req->ie, req->ie_len,
  289. mvm->fw->ucode_capa.max_probe_length));
  290. iwl_mvm_scan_fill_channels(cmd, req);
  291. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  292. le16_to_cpu(cmd->tx_cmd.len) +
  293. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  294. hcmd.len[0] = le16_to_cpu(cmd->len);
  295. status = SCAN_RESPONSE_OK;
  296. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  297. if (!ret && status == SCAN_RESPONSE_OK) {
  298. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  299. } else {
  300. /*
  301. * If the scan failed, it usually means that the FW was unable
  302. * to allocate the time events. Warn on it, but maybe we
  303. * should try to send the command again with different params.
  304. */
  305. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  306. status, ret);
  307. mvm->scan_status = IWL_MVM_SCAN_NONE;
  308. ret = -EIO;
  309. }
  310. return ret;
  311. }
  312. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  313. struct iwl_device_cmd *cmd)
  314. {
  315. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  316. struct iwl_cmd_response *resp = (void *)pkt->data;
  317. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  318. le32_to_cpu(resp->status));
  319. return 0;
  320. }
  321. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  322. struct iwl_device_cmd *cmd)
  323. {
  324. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  325. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  326. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  327. notif->status, notif->scanned_channels);
  328. mvm->scan_status = IWL_MVM_SCAN_NONE;
  329. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  330. return 0;
  331. }
  332. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  333. struct iwl_rx_packet *pkt, void *data)
  334. {
  335. struct iwl_mvm *mvm =
  336. container_of(notif_wait, struct iwl_mvm, notif_wait);
  337. struct iwl_scan_complete_notif *notif;
  338. u32 *resp;
  339. switch (pkt->hdr.cmd) {
  340. case SCAN_ABORT_CMD:
  341. resp = (void *)pkt->data;
  342. if (*resp == CAN_ABORT_STATUS) {
  343. IWL_DEBUG_SCAN(mvm,
  344. "Scan can be aborted, wait until completion\n");
  345. return false;
  346. }
  347. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  348. *resp);
  349. return true;
  350. case SCAN_COMPLETE_NOTIFICATION:
  351. notif = (void *)pkt->data;
  352. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  353. notif->status);
  354. return true;
  355. default:
  356. WARN_ON(1);
  357. return false;
  358. };
  359. }
  360. void iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  361. {
  362. struct iwl_notification_wait wait_scan_abort;
  363. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  364. SCAN_COMPLETE_NOTIFICATION };
  365. int ret;
  366. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  367. scan_abort_notif,
  368. ARRAY_SIZE(scan_abort_notif),
  369. iwl_mvm_scan_abort_notif, NULL);
  370. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, CMD_SYNC, 0, NULL);
  371. if (ret) {
  372. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  373. goto out_remove_notif;
  374. }
  375. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, 1 * HZ);
  376. if (ret)
  377. IWL_ERR(mvm, "%s - failed on timeout\n", __func__);
  378. return;
  379. out_remove_notif:
  380. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  381. }