scan.c 13 KB

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