iwl-scan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 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 IL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  42. #define IL_ACTIVE_DWELL_TIME_52 (20)
  43. #define IL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  44. #define IL_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 IL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  49. #define IL_PASSIVE_DWELL_TIME_52 (10)
  50. #define IL_PASSIVE_DWELL_BASE (100)
  51. #define IL_CHANNEL_TUNE_TIME 5
  52. static int il_send_scan_abort(struct il_priv *il)
  53. {
  54. int ret;
  55. struct il_rx_pkt *pkt;
  56. struct il_host_cmd cmd = {
  57. .id = REPLY_SCAN_ABORT_CMD,
  58. .flags = CMD_WANT_SKB,
  59. };
  60. /* Exit instantly with error when device is not ready
  61. * to receive scan abort command or it does not perform
  62. * hardware scan currently */
  63. if (!test_bit(STATUS_READY, &il->status) ||
  64. !test_bit(STATUS_GEO_CONFIGURED, &il->status) ||
  65. !test_bit(STATUS_SCAN_HW, &il->status) ||
  66. test_bit(STATUS_FW_ERROR, &il->status) ||
  67. test_bit(STATUS_EXIT_PENDING, &il->status))
  68. return -EIO;
  69. ret = il_send_cmd_sync(il, &cmd);
  70. if (ret)
  71. return ret;
  72. pkt = (struct il_rx_pkt *)cmd.reply_page;
  73. if (pkt->u.status != CAN_ABORT_STATUS) {
  74. /* The scan abort will return 1 for success or
  75. * 2 for "failure". A failure condition can be
  76. * due to simply not being in an active scan which
  77. * can occur if we send the scan abort before we
  78. * the microcode has notified us that a scan is
  79. * completed. */
  80. D_SCAN("SCAN_ABORT ret %d.\n", pkt->u.status);
  81. ret = -EIO;
  82. }
  83. il_free_pages(il, cmd.reply_page);
  84. return ret;
  85. }
  86. static void il_complete_scan(struct il_priv *il, bool aborted)
  87. {
  88. /* check if scan was requested from mac80211 */
  89. if (il->scan_request) {
  90. D_SCAN("Complete scan in mac80211\n");
  91. ieee80211_scan_completed(il->hw, aborted);
  92. }
  93. il->scan_vif = NULL;
  94. il->scan_request = NULL;
  95. }
  96. void il_force_scan_end(struct il_priv *il)
  97. {
  98. lockdep_assert_held(&il->mutex);
  99. if (!test_bit(STATUS_SCANNING, &il->status)) {
  100. D_SCAN("Forcing scan end while not scanning\n");
  101. return;
  102. }
  103. D_SCAN("Forcing scan end\n");
  104. clear_bit(STATUS_SCANNING, &il->status);
  105. clear_bit(STATUS_SCAN_HW, &il->status);
  106. clear_bit(STATUS_SCAN_ABORTING, &il->status);
  107. il_complete_scan(il, true);
  108. }
  109. static void il_do_scan_abort(struct il_priv *il)
  110. {
  111. int ret;
  112. lockdep_assert_held(&il->mutex);
  113. if (!test_bit(STATUS_SCANNING, &il->status)) {
  114. D_SCAN("Not performing scan to abort\n");
  115. return;
  116. }
  117. if (test_and_set_bit(STATUS_SCAN_ABORTING, &il->status)) {
  118. D_SCAN("Scan abort in progress\n");
  119. return;
  120. }
  121. ret = il_send_scan_abort(il);
  122. if (ret) {
  123. D_SCAN("Send scan abort failed %d\n", ret);
  124. il_force_scan_end(il);
  125. } else
  126. D_SCAN("Successfully send scan abort\n");
  127. }
  128. /**
  129. * il_scan_cancel - Cancel any currently executing HW scan
  130. */
  131. int il_scan_cancel(struct il_priv *il)
  132. {
  133. D_SCAN("Queuing abort scan\n");
  134. queue_work(il->workqueue, &il->abort_scan);
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(il_scan_cancel);
  138. /**
  139. * il_scan_cancel_timeout - Cancel any currently executing HW scan
  140. * @ms: amount of time to wait (in milliseconds) for scan to abort
  141. *
  142. */
  143. int il_scan_cancel_timeout(struct il_priv *il, unsigned long ms)
  144. {
  145. unsigned long timeout = jiffies + msecs_to_jiffies(ms);
  146. lockdep_assert_held(&il->mutex);
  147. D_SCAN("Scan cancel timeout\n");
  148. il_do_scan_abort(il);
  149. while (time_before_eq(jiffies, timeout)) {
  150. if (!test_bit(STATUS_SCAN_HW, &il->status))
  151. break;
  152. msleep(20);
  153. }
  154. return test_bit(STATUS_SCAN_HW, &il->status);
  155. }
  156. EXPORT_SYMBOL(il_scan_cancel_timeout);
  157. /* Service response to REPLY_SCAN_CMD (0x80) */
  158. static void il_rx_reply_scan(struct il_priv *il,
  159. struct il_rx_buf *rxb)
  160. {
  161. #ifdef CONFIG_IWLEGACY_DEBUG
  162. struct il_rx_pkt *pkt = rxb_addr(rxb);
  163. struct il_scanreq_notification *notif =
  164. (struct il_scanreq_notification *)pkt->u.raw;
  165. D_SCAN("Scan request status = 0x%x\n", notif->status);
  166. #endif
  167. }
  168. /* Service SCAN_START_NOTIFICATION (0x82) */
  169. static void il_rx_scan_start_notif(struct il_priv *il,
  170. struct il_rx_buf *rxb)
  171. {
  172. struct il_rx_pkt *pkt = rxb_addr(rxb);
  173. struct il_scanstart_notification *notif =
  174. (struct il_scanstart_notification *)pkt->u.raw;
  175. il->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  176. D_SCAN("Scan start: "
  177. "%d [802.11%s] "
  178. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  179. notif->channel,
  180. notif->band ? "bg" : "a",
  181. le32_to_cpu(notif->tsf_high),
  182. le32_to_cpu(notif->tsf_low),
  183. notif->status, notif->beacon_timer);
  184. }
  185. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  186. static void il_rx_scan_results_notif(struct il_priv *il,
  187. struct il_rx_buf *rxb)
  188. {
  189. #ifdef CONFIG_IWLEGACY_DEBUG
  190. struct il_rx_pkt *pkt = rxb_addr(rxb);
  191. struct il_scanresults_notification *notif =
  192. (struct il_scanresults_notification *)pkt->u.raw;
  193. D_SCAN("Scan ch.res: "
  194. "%d [802.11%s] "
  195. "(TSF: 0x%08X:%08X) - %d "
  196. "elapsed=%lu usec\n",
  197. notif->channel,
  198. notif->band ? "bg" : "a",
  199. le32_to_cpu(notif->tsf_high),
  200. le32_to_cpu(notif->tsf_low),
  201. le32_to_cpu(notif->statistics[0]),
  202. le32_to_cpu(notif->tsf_low) - il->scan_start_tsf);
  203. #endif
  204. }
  205. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  206. static void il_rx_scan_complete_notif(struct il_priv *il,
  207. struct il_rx_buf *rxb)
  208. {
  209. #ifdef CONFIG_IWLEGACY_DEBUG
  210. struct il_rx_pkt *pkt = rxb_addr(rxb);
  211. struct il_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  212. #endif
  213. D_SCAN(
  214. "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  215. scan_notif->scanned_channels,
  216. scan_notif->tsf_low,
  217. scan_notif->tsf_high, scan_notif->status);
  218. /* The HW is no longer scanning */
  219. clear_bit(STATUS_SCAN_HW, &il->status);
  220. D_SCAN("Scan on %sGHz took %dms\n",
  221. (il->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  222. jiffies_to_msecs(jiffies - il->scan_start));
  223. queue_work(il->workqueue, &il->scan_completed);
  224. }
  225. void il_setup_rx_scan_handlers(struct il_priv *il)
  226. {
  227. /* scan handlers */
  228. il->rx_handlers[REPLY_SCAN_CMD] = il_rx_reply_scan;
  229. il->rx_handlers[SCAN_START_NOTIFICATION] =
  230. il_rx_scan_start_notif;
  231. il->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  232. il_rx_scan_results_notif;
  233. il->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  234. il_rx_scan_complete_notif;
  235. }
  236. EXPORT_SYMBOL(il_setup_rx_scan_handlers);
  237. inline u16 il_get_active_dwell_time(struct il_priv *il,
  238. enum ieee80211_band band,
  239. u8 n_probes)
  240. {
  241. if (band == IEEE80211_BAND_5GHZ)
  242. return IL_ACTIVE_DWELL_TIME_52 +
  243. IL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  244. else
  245. return IL_ACTIVE_DWELL_TIME_24 +
  246. IL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  247. }
  248. EXPORT_SYMBOL(il_get_active_dwell_time);
  249. u16 il_get_passive_dwell_time(struct il_priv *il,
  250. enum ieee80211_band band,
  251. struct ieee80211_vif *vif)
  252. {
  253. struct il_rxon_context *ctx;
  254. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  255. IL_PASSIVE_DWELL_BASE + IL_PASSIVE_DWELL_TIME_24 :
  256. IL_PASSIVE_DWELL_BASE + IL_PASSIVE_DWELL_TIME_52;
  257. if (il_is_any_associated(il)) {
  258. /*
  259. * If we're associated, we clamp the maximum passive
  260. * dwell time to be 98% of the smallest beacon interval
  261. * (minus 2 * channel tune time)
  262. */
  263. for_each_context(il, ctx) {
  264. u16 value;
  265. if (!il_is_associated_ctx(ctx))
  266. continue;
  267. value = ctx->vif ? ctx->vif->bss_conf.beacon_int : 0;
  268. if (value > IL_PASSIVE_DWELL_BASE || !value)
  269. value = IL_PASSIVE_DWELL_BASE;
  270. value = (value * 98) / 100 - IL_CHANNEL_TUNE_TIME * 2;
  271. passive = min(value, passive);
  272. }
  273. }
  274. return passive;
  275. }
  276. EXPORT_SYMBOL(il_get_passive_dwell_time);
  277. void il_init_scan_params(struct il_priv *il)
  278. {
  279. u8 ant_idx = fls(il->hw_params.valid_tx_ant) - 1;
  280. if (!il->scan_tx_ant[IEEE80211_BAND_5GHZ])
  281. il->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  282. if (!il->scan_tx_ant[IEEE80211_BAND_2GHZ])
  283. il->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  284. }
  285. EXPORT_SYMBOL(il_init_scan_params);
  286. static int il_scan_initiate(struct il_priv *il,
  287. struct ieee80211_vif *vif)
  288. {
  289. int ret;
  290. lockdep_assert_held(&il->mutex);
  291. if (WARN_ON(!il->cfg->ops->utils->request_scan))
  292. return -EOPNOTSUPP;
  293. cancel_delayed_work(&il->scan_check);
  294. if (!il_is_ready_rf(il)) {
  295. IL_WARN("Request scan called when driver not ready.\n");
  296. return -EIO;
  297. }
  298. if (test_bit(STATUS_SCAN_HW, &il->status)) {
  299. D_SCAN(
  300. "Multiple concurrent scan requests in parallel.\n");
  301. return -EBUSY;
  302. }
  303. if (test_bit(STATUS_SCAN_ABORTING, &il->status)) {
  304. D_SCAN("Scan request while abort pending.\n");
  305. return -EBUSY;
  306. }
  307. D_SCAN("Starting scan...\n");
  308. set_bit(STATUS_SCANNING, &il->status);
  309. il->scan_start = jiffies;
  310. ret = il->cfg->ops->utils->request_scan(il, vif);
  311. if (ret) {
  312. clear_bit(STATUS_SCANNING, &il->status);
  313. return ret;
  314. }
  315. queue_delayed_work(il->workqueue, &il->scan_check,
  316. IL_SCAN_CHECK_WATCHDOG);
  317. return 0;
  318. }
  319. int il_mac_hw_scan(struct ieee80211_hw *hw,
  320. struct ieee80211_vif *vif,
  321. struct cfg80211_scan_request *req)
  322. {
  323. struct il_priv *il = hw->priv;
  324. int ret;
  325. D_MAC80211("enter\n");
  326. if (req->n_channels == 0)
  327. return -EINVAL;
  328. mutex_lock(&il->mutex);
  329. if (test_bit(STATUS_SCANNING, &il->status)) {
  330. D_SCAN("Scan already in progress.\n");
  331. ret = -EAGAIN;
  332. goto out_unlock;
  333. }
  334. /* mac80211 will only ask for one band at a time */
  335. il->scan_request = req;
  336. il->scan_vif = vif;
  337. il->scan_band = req->channels[0]->band;
  338. ret = il_scan_initiate(il, vif);
  339. D_MAC80211("leave\n");
  340. out_unlock:
  341. mutex_unlock(&il->mutex);
  342. return ret;
  343. }
  344. EXPORT_SYMBOL(il_mac_hw_scan);
  345. static void il_bg_scan_check(struct work_struct *data)
  346. {
  347. struct il_priv *il =
  348. container_of(data, struct il_priv, scan_check.work);
  349. D_SCAN("Scan check work\n");
  350. /* Since we are here firmware does not finish scan and
  351. * most likely is in bad shape, so we don't bother to
  352. * send abort command, just force scan complete to mac80211 */
  353. mutex_lock(&il->mutex);
  354. il_force_scan_end(il);
  355. mutex_unlock(&il->mutex);
  356. }
  357. /**
  358. * il_fill_probe_req - fill in all required fields and IE for probe request
  359. */
  360. u16
  361. il_fill_probe_req(struct il_priv *il, struct ieee80211_mgmt *frame,
  362. const u8 *ta, 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, il_bcast_addr, ETH_ALEN);
  373. memcpy(frame->sa, ta, ETH_ALEN);
  374. memcpy(frame->bssid, il_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(il_fill_probe_req);
  395. static void il_bg_abort_scan(struct work_struct *work)
  396. {
  397. struct il_priv *il = container_of(work, struct il_priv, abort_scan);
  398. D_SCAN("Abort scan work\n");
  399. /* We keep scan_check work queued in case when firmware will not
  400. * report back scan completed notification */
  401. mutex_lock(&il->mutex);
  402. il_scan_cancel_timeout(il, 200);
  403. mutex_unlock(&il->mutex);
  404. }
  405. static void il_bg_scan_completed(struct work_struct *work)
  406. {
  407. struct il_priv *il =
  408. container_of(work, struct il_priv, scan_completed);
  409. bool aborted;
  410. D_SCAN("Completed scan.\n");
  411. cancel_delayed_work(&il->scan_check);
  412. mutex_lock(&il->mutex);
  413. aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &il->status);
  414. if (aborted)
  415. D_SCAN("Aborted scan completed.\n");
  416. if (!test_and_clear_bit(STATUS_SCANNING, &il->status)) {
  417. D_SCAN("Scan already completed.\n");
  418. goto out_settings;
  419. }
  420. il_complete_scan(il, aborted);
  421. out_settings:
  422. /* Can we still talk to firmware ? */
  423. if (!il_is_ready_rf(il))
  424. goto out;
  425. /*
  426. * We do not commit power settings while scan is pending,
  427. * do it now if the settings changed.
  428. */
  429. il_power_set_mode(il, &il->power_data.sleep_cmd_next, false);
  430. il_set_tx_power(il, il->tx_power_next, false);
  431. il->cfg->ops->utils->post_scan(il);
  432. out:
  433. mutex_unlock(&il->mutex);
  434. }
  435. void il_setup_scan_deferred_work(struct il_priv *il)
  436. {
  437. INIT_WORK(&il->scan_completed, il_bg_scan_completed);
  438. INIT_WORK(&il->abort_scan, il_bg_abort_scan);
  439. INIT_DELAYED_WORK(&il->scan_check, il_bg_scan_check);
  440. }
  441. EXPORT_SYMBOL(il_setup_scan_deferred_work);
  442. void il_cancel_scan_deferred_work(struct il_priv *il)
  443. {
  444. cancel_work_sync(&il->abort_scan);
  445. cancel_work_sync(&il->scan_completed);
  446. if (cancel_delayed_work_sync(&il->scan_check)) {
  447. mutex_lock(&il->mutex);
  448. il_force_scan_end(il);
  449. mutex_unlock(&il->mutex);
  450. }
  451. }
  452. EXPORT_SYMBOL(il_cancel_scan_deferred_work);