iwl-scan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/etherdevice.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  39. * sending probe req. This should be set long enough to hear probe responses
  40. * from more than one AP. */
  41. #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  42. #define IWL_ACTIVE_DWELL_TIME_52 (20)
  43. #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  44. #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  45. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  46. * Must be set longer than active dwell time.
  47. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  48. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  49. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  50. #define IWL_PASSIVE_DWELL_BASE (100)
  51. #define IWL_CHANNEL_TUNE_TIME 5
  52. /**
  53. * iwl_scan_cancel - Cancel any currently executing HW scan
  54. *
  55. * NOTE: priv->mutex is not required before calling this function
  56. */
  57. int iwl_scan_cancel(struct iwl_priv *priv)
  58. {
  59. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  60. clear_bit(STATUS_SCANNING, &priv->status);
  61. return 0;
  62. }
  63. if (test_bit(STATUS_SCANNING, &priv->status)) {
  64. if (!test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  65. IWL_DEBUG_SCAN(priv, "Queuing scan abort.\n");
  66. queue_work(priv->workqueue, &priv->abort_scan);
  67. } else
  68. IWL_DEBUG_SCAN(priv, "Scan abort already in progress.\n");
  69. return test_bit(STATUS_SCANNING, &priv->status);
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(iwl_scan_cancel);
  74. /**
  75. * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  76. * @ms: amount of time to wait (in milliseconds) for scan to abort
  77. *
  78. * NOTE: priv->mutex must be held before calling this function
  79. */
  80. int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  81. {
  82. unsigned long now = jiffies;
  83. int ret;
  84. ret = iwl_scan_cancel(priv);
  85. if (ret && ms) {
  86. mutex_unlock(&priv->mutex);
  87. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  88. test_bit(STATUS_SCANNING, &priv->status))
  89. msleep(1);
  90. mutex_lock(&priv->mutex);
  91. return test_bit(STATUS_SCANNING, &priv->status);
  92. }
  93. return ret;
  94. }
  95. EXPORT_SYMBOL(iwl_scan_cancel_timeout);
  96. static int iwl_send_scan_abort(struct iwl_priv *priv)
  97. {
  98. int ret = 0;
  99. struct iwl_rx_packet *pkt;
  100. struct iwl_host_cmd cmd = {
  101. .id = REPLY_SCAN_ABORT_CMD,
  102. .flags = CMD_WANT_SKB,
  103. };
  104. /* If there isn't a scan actively going on in the hardware
  105. * then we are in between scan bands and not actually
  106. * actively scanning, so don't send the abort command */
  107. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  108. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  109. return 0;
  110. }
  111. ret = iwl_send_cmd_sync(priv, &cmd);
  112. if (ret) {
  113. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  114. return ret;
  115. }
  116. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  117. if (pkt->u.status != CAN_ABORT_STATUS) {
  118. /* The scan abort will return 1 for success or
  119. * 2 for "failure". A failure condition can be
  120. * due to simply not being in an active scan which
  121. * can occur if we send the scan abort before we
  122. * the microcode has notified us that a scan is
  123. * completed. */
  124. IWL_DEBUG_INFO(priv, "SCAN_ABORT returned %d.\n", pkt->u.status);
  125. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  126. clear_bit(STATUS_SCAN_HW, &priv->status);
  127. }
  128. iwl_free_pages(priv, cmd.reply_page);
  129. return ret;
  130. }
  131. /* Service response to REPLY_SCAN_CMD (0x80) */
  132. static void iwl_rx_reply_scan(struct iwl_priv *priv,
  133. struct iwl_rx_mem_buffer *rxb)
  134. {
  135. #ifdef CONFIG_IWLWIFI_DEBUG
  136. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  137. struct iwl_scanreq_notification *notif =
  138. (struct iwl_scanreq_notification *)pkt->u.raw;
  139. IWL_DEBUG_RX(priv, "Scan request status = 0x%x\n", notif->status);
  140. #endif
  141. }
  142. /* Service SCAN_START_NOTIFICATION (0x82) */
  143. static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
  144. struct iwl_rx_mem_buffer *rxb)
  145. {
  146. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  147. struct iwl_scanstart_notification *notif =
  148. (struct iwl_scanstart_notification *)pkt->u.raw;
  149. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  150. IWL_DEBUG_SCAN(priv, "Scan start: "
  151. "%d [802.11%s] "
  152. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  153. notif->channel,
  154. notif->band ? "bg" : "a",
  155. le32_to_cpu(notif->tsf_high),
  156. le32_to_cpu(notif->tsf_low),
  157. notif->status, notif->beacon_timer);
  158. }
  159. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  160. static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
  161. struct iwl_rx_mem_buffer *rxb)
  162. {
  163. #ifdef CONFIG_IWLWIFI_DEBUG
  164. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  165. struct iwl_scanresults_notification *notif =
  166. (struct iwl_scanresults_notification *)pkt->u.raw;
  167. IWL_DEBUG_SCAN(priv, "Scan ch.res: "
  168. "%d [802.11%s] "
  169. "(TSF: 0x%08X:%08X) - %d "
  170. "elapsed=%lu usec\n",
  171. notif->channel,
  172. notif->band ? "bg" : "a",
  173. le32_to_cpu(notif->tsf_high),
  174. le32_to_cpu(notif->tsf_low),
  175. le32_to_cpu(notif->statistics[0]),
  176. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
  177. #endif
  178. }
  179. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  180. static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  181. struct iwl_rx_mem_buffer *rxb)
  182. {
  183. #ifdef CONFIG_IWLWIFI_DEBUG
  184. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  185. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  186. IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  187. scan_notif->scanned_channels,
  188. scan_notif->tsf_low,
  189. scan_notif->tsf_high, scan_notif->status);
  190. #endif
  191. /* The HW is no longer scanning */
  192. clear_bit(STATUS_SCAN_HW, &priv->status);
  193. IWL_DEBUG_INFO(priv, "Scan on %sGHz took %dms\n",
  194. (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  195. jiffies_to_msecs(elapsed_jiffies
  196. (priv->scan_start, jiffies)));
  197. /*
  198. * If a request to abort was given, or the scan did not succeed
  199. * then we reset the scan state machine and terminate,
  200. * re-queuing another scan if one has been requested
  201. */
  202. if (test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status))
  203. IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
  204. IWL_DEBUG_INFO(priv, "Setting scan to off\n");
  205. clear_bit(STATUS_SCANNING, &priv->status);
  206. queue_work(priv->workqueue, &priv->scan_completed);
  207. }
  208. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  209. {
  210. /* scan handlers */
  211. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  212. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  213. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  214. iwl_rx_scan_results_notif;
  215. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  216. iwl_rx_scan_complete_notif;
  217. }
  218. EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
  219. inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  220. enum ieee80211_band band,
  221. u8 n_probes)
  222. {
  223. if (band == IEEE80211_BAND_5GHZ)
  224. return IWL_ACTIVE_DWELL_TIME_52 +
  225. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  226. else
  227. return IWL_ACTIVE_DWELL_TIME_24 +
  228. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  229. }
  230. EXPORT_SYMBOL(iwl_get_active_dwell_time);
  231. u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  232. enum ieee80211_band band)
  233. {
  234. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  235. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  236. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  237. if (iwl_is_associated(priv)) {
  238. /* If we're associated, we clamp the maximum passive
  239. * dwell time to be 98% of the beacon interval (minus
  240. * 2 * channel tune time) */
  241. passive = priv->beacon_int;
  242. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  243. passive = IWL_PASSIVE_DWELL_BASE;
  244. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  245. }
  246. return passive;
  247. }
  248. EXPORT_SYMBOL(iwl_get_passive_dwell_time);
  249. void iwl_init_scan_params(struct iwl_priv *priv)
  250. {
  251. u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
  252. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  253. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  254. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  255. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  256. }
  257. EXPORT_SYMBOL(iwl_init_scan_params);
  258. static int iwl_scan_initiate(struct iwl_priv *priv)
  259. {
  260. WARN_ON(!mutex_is_locked(&priv->mutex));
  261. IWL_DEBUG_INFO(priv, "Starting scan...\n");
  262. set_bit(STATUS_SCANNING, &priv->status);
  263. priv->is_internal_short_scan = false;
  264. priv->scan_start = jiffies;
  265. if (WARN_ON(!priv->cfg->ops->utils->request_scan))
  266. return -EOPNOTSUPP;
  267. priv->cfg->ops->utils->request_scan(priv);
  268. return 0;
  269. }
  270. int iwl_mac_hw_scan(struct ieee80211_hw *hw,
  271. struct ieee80211_vif *vif,
  272. struct cfg80211_scan_request *req)
  273. {
  274. struct iwl_priv *priv = hw->priv;
  275. int ret;
  276. IWL_DEBUG_MAC80211(priv, "enter\n");
  277. if (req->n_channels == 0)
  278. return -EINVAL;
  279. mutex_lock(&priv->mutex);
  280. if (!iwl_is_ready_rf(priv)) {
  281. ret = -EIO;
  282. IWL_DEBUG_MAC80211(priv, "leave - not ready or exit pending\n");
  283. goto out_unlock;
  284. }
  285. if (test_bit(STATUS_SCANNING, &priv->status)) {
  286. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  287. ret = -EAGAIN;
  288. goto out_unlock;
  289. }
  290. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  291. IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
  292. ret = -EAGAIN;
  293. goto out_unlock;
  294. }
  295. /* mac80211 will only ask for one band at a time */
  296. priv->scan_band = req->channels[0]->band;
  297. priv->scan_request = req;
  298. ret = iwl_scan_initiate(priv);
  299. IWL_DEBUG_MAC80211(priv, "leave\n");
  300. out_unlock:
  301. mutex_unlock(&priv->mutex);
  302. return ret;
  303. }
  304. EXPORT_SYMBOL(iwl_mac_hw_scan);
  305. /*
  306. * internal short scan, this function should only been called while associated.
  307. * It will reset and tune the radio to prevent possible RF related problem
  308. */
  309. void iwl_internal_short_hw_scan(struct iwl_priv *priv)
  310. {
  311. queue_work(priv->workqueue, &priv->start_internal_scan);
  312. }
  313. static void iwl_bg_start_internal_scan(struct work_struct *work)
  314. {
  315. struct iwl_priv *priv =
  316. container_of(work, struct iwl_priv, start_internal_scan);
  317. mutex_lock(&priv->mutex);
  318. if (!iwl_is_ready_rf(priv)) {
  319. IWL_DEBUG_SCAN(priv, "not ready or exit pending\n");
  320. goto unlock;
  321. }
  322. if (test_bit(STATUS_SCANNING, &priv->status)) {
  323. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  324. goto unlock;
  325. }
  326. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  327. IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
  328. goto unlock;
  329. }
  330. priv->scan_band = priv->band;
  331. IWL_DEBUG_SCAN(priv, "Start internal short scan...\n");
  332. set_bit(STATUS_SCANNING, &priv->status);
  333. priv->is_internal_short_scan = true;
  334. if (WARN_ON(!priv->cfg->ops->utils->request_scan))
  335. goto unlock;
  336. priv->cfg->ops->utils->request_scan(priv);
  337. unlock:
  338. mutex_unlock(&priv->mutex);
  339. }
  340. void iwl_bg_scan_check(struct work_struct *data)
  341. {
  342. struct iwl_priv *priv =
  343. container_of(data, struct iwl_priv, scan_check.work);
  344. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  345. return;
  346. mutex_lock(&priv->mutex);
  347. if (test_bit(STATUS_SCANNING, &priv->status) ||
  348. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  349. IWL_DEBUG_SCAN(priv, "Scan completion watchdog resetting "
  350. "adapter (%dms)\n",
  351. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  352. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  353. iwl_send_scan_abort(priv);
  354. }
  355. mutex_unlock(&priv->mutex);
  356. }
  357. EXPORT_SYMBOL(iwl_bg_scan_check);
  358. /**
  359. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  360. */
  361. u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
  362. const u8 *ies, int ie_len, int left)
  363. {
  364. int len = 0;
  365. u8 *pos = NULL;
  366. /* Make sure there is enough space for the probe request,
  367. * two mandatory IEs and the data */
  368. left -= 24;
  369. if (left < 0)
  370. return 0;
  371. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  372. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  373. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  374. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  375. frame->seq_ctrl = 0;
  376. len += 24;
  377. /* ...next IE... */
  378. pos = &frame->u.probe_req.variable[0];
  379. /* fill in our indirect SSID IE */
  380. left -= 2;
  381. if (left < 0)
  382. return 0;
  383. *pos++ = WLAN_EID_SSID;
  384. *pos++ = 0;
  385. len += 2;
  386. if (WARN_ON(left < ie_len))
  387. return len;
  388. if (ies && ie_len) {
  389. memcpy(pos, ies, ie_len);
  390. len += ie_len;
  391. }
  392. return (u16)len;
  393. }
  394. EXPORT_SYMBOL(iwl_fill_probe_req);
  395. void iwl_bg_abort_scan(struct work_struct *work)
  396. {
  397. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  398. if (!test_bit(STATUS_READY, &priv->status) ||
  399. !test_bit(STATUS_GEO_CONFIGURED, &priv->status))
  400. return;
  401. mutex_lock(&priv->mutex);
  402. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  403. iwl_send_scan_abort(priv);
  404. mutex_unlock(&priv->mutex);
  405. }
  406. EXPORT_SYMBOL(iwl_bg_abort_scan);
  407. void iwl_bg_scan_completed(struct work_struct *work)
  408. {
  409. struct iwl_priv *priv =
  410. container_of(work, struct iwl_priv, scan_completed);
  411. IWL_DEBUG_SCAN(priv, "SCAN complete scan\n");
  412. cancel_delayed_work(&priv->scan_check);
  413. if (!priv->is_internal_short_scan)
  414. ieee80211_scan_completed(priv->hw, false);
  415. else {
  416. priv->is_internal_short_scan = false;
  417. IWL_DEBUG_SCAN(priv, "internal short scan completed\n");
  418. }
  419. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  420. return;
  421. /* Since setting the TXPOWER may have been deferred while
  422. * performing the scan, fire one off */
  423. mutex_lock(&priv->mutex);
  424. iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
  425. mutex_unlock(&priv->mutex);
  426. }
  427. EXPORT_SYMBOL(iwl_bg_scan_completed);
  428. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  429. {
  430. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  431. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  432. INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
  433. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  434. }
  435. EXPORT_SYMBOL(iwl_setup_scan_deferred_work);