scan.c 14 KB

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