iwl-scan.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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/types.h>
  29. #include <linux/etherdevice.h>
  30. #include <net/mac80211.h>
  31. #include "iwl-eeprom.h"
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  38. * sending probe req. This should be set long enough to hear probe responses
  39. * from more than one AP. */
  40. #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  41. #define IWL_ACTIVE_DWELL_TIME_52 (20)
  42. #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  43. #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  44. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  45. * Must be set longer than active dwell time.
  46. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  47. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  48. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  49. #define IWL_PASSIVE_DWELL_BASE (100)
  50. #define IWL_CHANNEL_TUNE_TIME 5
  51. /**
  52. * iwl_scan_cancel - Cancel any currently executing HW scan
  53. *
  54. * NOTE: priv->mutex is not required before calling this function
  55. */
  56. int iwl_scan_cancel(struct iwl_priv *priv)
  57. {
  58. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  59. clear_bit(STATUS_SCANNING, &priv->status);
  60. return 0;
  61. }
  62. if (test_bit(STATUS_SCANNING, &priv->status)) {
  63. if (!test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  64. IWL_DEBUG_SCAN(priv, "Queuing scan abort.\n");
  65. queue_work(priv->workqueue, &priv->abort_scan);
  66. } else
  67. IWL_DEBUG_SCAN(priv, "Scan abort already in progress.\n");
  68. return test_bit(STATUS_SCANNING, &priv->status);
  69. }
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(iwl_scan_cancel);
  73. /**
  74. * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  75. * @ms: amount of time to wait (in milliseconds) for scan to abort
  76. *
  77. * NOTE: priv->mutex must be held before calling this function
  78. */
  79. int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  80. {
  81. unsigned long now = jiffies;
  82. int ret;
  83. ret = iwl_scan_cancel(priv);
  84. if (ret && ms) {
  85. mutex_unlock(&priv->mutex);
  86. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  87. test_bit(STATUS_SCANNING, &priv->status))
  88. msleep(1);
  89. mutex_lock(&priv->mutex);
  90. return test_bit(STATUS_SCANNING, &priv->status);
  91. }
  92. return ret;
  93. }
  94. EXPORT_SYMBOL(iwl_scan_cancel_timeout);
  95. static int iwl_send_scan_abort(struct iwl_priv *priv)
  96. {
  97. int ret = 0;
  98. struct iwl_rx_packet *pkt;
  99. struct iwl_host_cmd cmd = {
  100. .id = REPLY_SCAN_ABORT_CMD,
  101. .flags = CMD_WANT_SKB,
  102. };
  103. /* If there isn't a scan actively going on in the hardware
  104. * then we are in between scan bands and not actually
  105. * actively scanning, so don't send the abort command */
  106. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  107. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  108. return 0;
  109. }
  110. ret = iwl_send_cmd_sync(priv, &cmd);
  111. if (ret) {
  112. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  113. return ret;
  114. }
  115. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  116. if (pkt->u.status != CAN_ABORT_STATUS) {
  117. /* The scan abort will return 1 for success or
  118. * 2 for "failure". A failure condition can be
  119. * due to simply not being in an active scan which
  120. * can occur if we send the scan abort before we
  121. * the microcode has notified us that a scan is
  122. * completed. */
  123. IWL_DEBUG_INFO(priv, "SCAN_ABORT returned %d.\n", pkt->u.status);
  124. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  125. clear_bit(STATUS_SCAN_HW, &priv->status);
  126. }
  127. iwl_free_pages(priv, cmd.reply_page);
  128. return ret;
  129. }
  130. /* Service response to REPLY_SCAN_CMD (0x80) */
  131. static void iwl_rx_reply_scan(struct iwl_priv *priv,
  132. struct iwl_rx_mem_buffer *rxb)
  133. {
  134. #ifdef CONFIG_IWLWIFI_DEBUG
  135. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  136. struct iwl_scanreq_notification *notif =
  137. (struct iwl_scanreq_notification *)pkt->u.raw;
  138. IWL_DEBUG_RX(priv, "Scan request status = 0x%x\n", notif->status);
  139. #endif
  140. }
  141. /* Service SCAN_START_NOTIFICATION (0x82) */
  142. static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
  143. struct iwl_rx_mem_buffer *rxb)
  144. {
  145. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  146. struct iwl_scanstart_notification *notif =
  147. (struct iwl_scanstart_notification *)pkt->u.raw;
  148. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  149. IWL_DEBUG_SCAN(priv, "Scan start: "
  150. "%d [802.11%s] "
  151. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  152. notif->channel,
  153. notif->band ? "bg" : "a",
  154. le32_to_cpu(notif->tsf_high),
  155. le32_to_cpu(notif->tsf_low),
  156. notif->status, notif->beacon_timer);
  157. }
  158. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  159. static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
  160. struct iwl_rx_mem_buffer *rxb)
  161. {
  162. #ifdef CONFIG_IWLWIFI_DEBUG
  163. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  164. struct iwl_scanresults_notification *notif =
  165. (struct iwl_scanresults_notification *)pkt->u.raw;
  166. IWL_DEBUG_SCAN(priv, "Scan ch.res: "
  167. "%d [802.11%s] "
  168. "(TSF: 0x%08X:%08X) - %d "
  169. "elapsed=%lu usec\n",
  170. notif->channel,
  171. notif->band ? "bg" : "a",
  172. le32_to_cpu(notif->tsf_high),
  173. le32_to_cpu(notif->tsf_low),
  174. le32_to_cpu(notif->statistics[0]),
  175. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
  176. #endif
  177. if (!priv->is_internal_short_scan)
  178. priv->next_scan_jiffies = 0;
  179. }
  180. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  181. static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  182. struct iwl_rx_mem_buffer *rxb)
  183. {
  184. #ifdef CONFIG_IWLWIFI_DEBUG
  185. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  186. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  187. IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  188. scan_notif->scanned_channels,
  189. scan_notif->tsf_low,
  190. scan_notif->tsf_high, scan_notif->status);
  191. #endif
  192. /* The HW is no longer scanning */
  193. clear_bit(STATUS_SCAN_HW, &priv->status);
  194. IWL_DEBUG_INFO(priv, "Scan pass on %sGHz took %dms\n",
  195. (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  196. jiffies_to_msecs(elapsed_jiffies
  197. (priv->scan_pass_start, jiffies)));
  198. /*
  199. * If a request to abort was given, or the scan did not succeed
  200. * then we reset the scan state machine and terminate,
  201. * re-queuing another scan if one has been requested
  202. */
  203. if (test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status))
  204. IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
  205. if (!priv->is_internal_short_scan)
  206. priv->next_scan_jiffies = 0;
  207. IWL_DEBUG_INFO(priv, "Setting scan to off\n");
  208. clear_bit(STATUS_SCANNING, &priv->status);
  209. IWL_DEBUG_INFO(priv, "Scan took %dms\n",
  210. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  211. queue_work(priv->workqueue, &priv->scan_completed);
  212. }
  213. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  214. {
  215. /* scan handlers */
  216. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  217. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  218. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  219. iwl_rx_scan_results_notif;
  220. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  221. iwl_rx_scan_complete_notif;
  222. }
  223. EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
  224. inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  225. enum ieee80211_band band,
  226. u8 n_probes)
  227. {
  228. if (band == IEEE80211_BAND_5GHZ)
  229. return IWL_ACTIVE_DWELL_TIME_52 +
  230. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  231. else
  232. return IWL_ACTIVE_DWELL_TIME_24 +
  233. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  234. }
  235. EXPORT_SYMBOL(iwl_get_active_dwell_time);
  236. u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  237. enum ieee80211_band band)
  238. {
  239. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  240. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  241. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  242. if (iwl_is_associated(priv)) {
  243. /* If we're associated, we clamp the maximum passive
  244. * dwell time to be 98% of the beacon interval (minus
  245. * 2 * channel tune time) */
  246. passive = priv->beacon_int;
  247. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  248. passive = IWL_PASSIVE_DWELL_BASE;
  249. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  250. }
  251. return passive;
  252. }
  253. EXPORT_SYMBOL(iwl_get_passive_dwell_time);
  254. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  255. enum ieee80211_band band,
  256. struct iwl_scan_channel *scan_ch)
  257. {
  258. const struct ieee80211_supported_band *sband;
  259. const struct iwl_channel_info *ch_info;
  260. u16 passive_dwell = 0;
  261. u16 active_dwell = 0;
  262. int i, added = 0;
  263. u16 channel = 0;
  264. sband = iwl_get_hw_mode(priv, band);
  265. if (!sband) {
  266. IWL_ERR(priv, "invalid band\n");
  267. return added;
  268. }
  269. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  270. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  271. if (passive_dwell <= active_dwell)
  272. passive_dwell = active_dwell + 1;
  273. /* only scan single channel, good enough to reset the RF */
  274. /* pick the first valid not in-use channel */
  275. if (band == IEEE80211_BAND_5GHZ) {
  276. for (i = 14; i < priv->channel_count; i++) {
  277. if (priv->channel_info[i].channel !=
  278. le16_to_cpu(priv->staging_rxon.channel)) {
  279. channel = priv->channel_info[i].channel;
  280. ch_info = iwl_get_channel_info(priv,
  281. band, channel);
  282. if (is_channel_valid(ch_info))
  283. break;
  284. }
  285. }
  286. } else {
  287. for (i = 0; i < 14; i++) {
  288. if (priv->channel_info[i].channel !=
  289. le16_to_cpu(priv->staging_rxon.channel)) {
  290. channel =
  291. priv->channel_info[i].channel;
  292. ch_info = iwl_get_channel_info(priv,
  293. band, channel);
  294. if (is_channel_valid(ch_info))
  295. break;
  296. }
  297. }
  298. }
  299. if (channel) {
  300. scan_ch->channel = cpu_to_le16(channel);
  301. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  302. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  303. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  304. /* Set txpower levels to defaults */
  305. scan_ch->dsp_atten = 110;
  306. if (band == IEEE80211_BAND_5GHZ)
  307. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  308. else
  309. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  310. added++;
  311. } else
  312. IWL_ERR(priv, "no valid channel found\n");
  313. return added;
  314. }
  315. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  316. enum ieee80211_band band,
  317. u8 is_active, u8 n_probes,
  318. struct iwl_scan_channel *scan_ch)
  319. {
  320. struct ieee80211_channel *chan;
  321. const struct ieee80211_supported_band *sband;
  322. const struct iwl_channel_info *ch_info;
  323. u16 passive_dwell = 0;
  324. u16 active_dwell = 0;
  325. int added, i;
  326. u16 channel;
  327. sband = iwl_get_hw_mode(priv, band);
  328. if (!sband)
  329. return 0;
  330. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  331. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  332. if (passive_dwell <= active_dwell)
  333. passive_dwell = active_dwell + 1;
  334. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  335. chan = priv->scan_request->channels[i];
  336. if (chan->band != band)
  337. continue;
  338. channel = ieee80211_frequency_to_channel(chan->center_freq);
  339. scan_ch->channel = cpu_to_le16(channel);
  340. ch_info = iwl_get_channel_info(priv, band, channel);
  341. if (!is_channel_valid(ch_info)) {
  342. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  343. channel);
  344. continue;
  345. }
  346. if (!is_active || is_channel_passive(ch_info) ||
  347. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  348. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  349. else
  350. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  351. if (n_probes)
  352. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  353. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  354. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  355. /* Set txpower levels to defaults */
  356. scan_ch->dsp_atten = 110;
  357. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  358. * power level:
  359. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  360. */
  361. if (band == IEEE80211_BAND_5GHZ)
  362. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  363. else
  364. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  365. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  366. channel, le32_to_cpu(scan_ch->type),
  367. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  368. "ACTIVE" : "PASSIVE",
  369. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  370. active_dwell : passive_dwell);
  371. scan_ch++;
  372. added++;
  373. }
  374. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  375. return added;
  376. }
  377. void iwl_init_scan_params(struct iwl_priv *priv)
  378. {
  379. u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
  380. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  381. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  382. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  383. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  384. }
  385. EXPORT_SYMBOL(iwl_init_scan_params);
  386. static int iwl_scan_initiate(struct iwl_priv *priv)
  387. {
  388. WARN_ON(!mutex_is_locked(&priv->mutex));
  389. IWL_DEBUG_INFO(priv, "Starting scan...\n");
  390. set_bit(STATUS_SCANNING, &priv->status);
  391. priv->is_internal_short_scan = false;
  392. priv->scan_start = jiffies;
  393. priv->scan_pass_start = priv->scan_start;
  394. queue_work(priv->workqueue, &priv->request_scan);
  395. return 0;
  396. }
  397. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  398. int iwl_mac_hw_scan(struct ieee80211_hw *hw,
  399. struct cfg80211_scan_request *req)
  400. {
  401. unsigned long flags;
  402. struct iwl_priv *priv = hw->priv;
  403. int ret;
  404. IWL_DEBUG_MAC80211(priv, "enter\n");
  405. if (req->n_channels == 0)
  406. return -EINVAL;
  407. mutex_lock(&priv->mutex);
  408. spin_lock_irqsave(&priv->lock, flags);
  409. if (!iwl_is_ready_rf(priv)) {
  410. ret = -EIO;
  411. IWL_DEBUG_MAC80211(priv, "leave - not ready or exit pending\n");
  412. goto out_unlock;
  413. }
  414. if (test_bit(STATUS_SCANNING, &priv->status)) {
  415. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  416. ret = -EAGAIN;
  417. goto out_unlock;
  418. }
  419. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  420. IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
  421. ret = -EAGAIN;
  422. goto out_unlock;
  423. }
  424. /* We don't schedule scan within next_scan_jiffies period.
  425. * Avoid scanning during possible EAPOL exchange, return
  426. * success immediately.
  427. */
  428. if (priv->next_scan_jiffies &&
  429. time_after(priv->next_scan_jiffies, jiffies)) {
  430. IWL_DEBUG_SCAN(priv, "scan rejected: within next scan period\n");
  431. queue_work(priv->workqueue, &priv->scan_completed);
  432. ret = 0;
  433. goto out_unlock;
  434. }
  435. /* mac80211 will only ask for one band at a time */
  436. priv->scan_band = req->channels[0]->band;
  437. priv->scan_request = req;
  438. ret = iwl_scan_initiate(priv);
  439. IWL_DEBUG_MAC80211(priv, "leave\n");
  440. out_unlock:
  441. spin_unlock_irqrestore(&priv->lock, flags);
  442. mutex_unlock(&priv->mutex);
  443. return ret;
  444. }
  445. EXPORT_SYMBOL(iwl_mac_hw_scan);
  446. /*
  447. * internal short scan, this function should only been called while associated.
  448. * It will reset and tune the radio to prevent possible RF related problem
  449. */
  450. void iwl_internal_short_hw_scan(struct iwl_priv *priv)
  451. {
  452. queue_work(priv->workqueue, &priv->start_internal_scan);
  453. }
  454. static void iwl_bg_start_internal_scan(struct work_struct *work)
  455. {
  456. struct iwl_priv *priv =
  457. container_of(work, struct iwl_priv, start_internal_scan);
  458. mutex_lock(&priv->mutex);
  459. if (!iwl_is_ready_rf(priv)) {
  460. IWL_DEBUG_SCAN(priv, "not ready or exit pending\n");
  461. goto unlock;
  462. }
  463. if (test_bit(STATUS_SCANNING, &priv->status)) {
  464. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  465. goto unlock;
  466. }
  467. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  468. IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
  469. goto unlock;
  470. }
  471. priv->scan_band = priv->band;
  472. IWL_DEBUG_SCAN(priv, "Start internal short scan...\n");
  473. set_bit(STATUS_SCANNING, &priv->status);
  474. priv->is_internal_short_scan = true;
  475. queue_work(priv->workqueue, &priv->request_scan);
  476. unlock:
  477. mutex_unlock(&priv->mutex);
  478. }
  479. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  480. void iwl_bg_scan_check(struct work_struct *data)
  481. {
  482. struct iwl_priv *priv =
  483. container_of(data, struct iwl_priv, scan_check.work);
  484. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  485. return;
  486. mutex_lock(&priv->mutex);
  487. if (test_bit(STATUS_SCANNING, &priv->status) ||
  488. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  489. IWL_DEBUG_SCAN(priv, "Scan completion watchdog resetting "
  490. "adapter (%dms)\n",
  491. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  492. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  493. iwl_send_scan_abort(priv);
  494. }
  495. mutex_unlock(&priv->mutex);
  496. }
  497. EXPORT_SYMBOL(iwl_bg_scan_check);
  498. /**
  499. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  500. */
  501. u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
  502. const u8 *ies, int ie_len, int left)
  503. {
  504. int len = 0;
  505. u8 *pos = NULL;
  506. /* Make sure there is enough space for the probe request,
  507. * two mandatory IEs and the data */
  508. left -= 24;
  509. if (left < 0)
  510. return 0;
  511. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  512. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  513. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  514. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  515. frame->seq_ctrl = 0;
  516. len += 24;
  517. /* ...next IE... */
  518. pos = &frame->u.probe_req.variable[0];
  519. /* fill in our indirect SSID IE */
  520. left -= 2;
  521. if (left < 0)
  522. return 0;
  523. *pos++ = WLAN_EID_SSID;
  524. *pos++ = 0;
  525. len += 2;
  526. if (WARN_ON(left < ie_len))
  527. return len;
  528. if (ies)
  529. memcpy(pos, ies, ie_len);
  530. len += ie_len;
  531. left -= ie_len;
  532. return (u16)len;
  533. }
  534. EXPORT_SYMBOL(iwl_fill_probe_req);
  535. static void iwl_bg_request_scan(struct work_struct *data)
  536. {
  537. struct iwl_priv *priv =
  538. container_of(data, struct iwl_priv, request_scan);
  539. struct iwl_host_cmd cmd = {
  540. .id = REPLY_SCAN_CMD,
  541. .len = sizeof(struct iwl_scan_cmd),
  542. .flags = CMD_SIZE_HUGE,
  543. };
  544. struct iwl_scan_cmd *scan;
  545. struct ieee80211_conf *conf = NULL;
  546. u32 rate_flags = 0;
  547. u16 cmd_len;
  548. u16 rx_chain = 0;
  549. enum ieee80211_band band;
  550. u8 n_probes = 0;
  551. u8 rx_ant = priv->hw_params.valid_rx_ant;
  552. u8 rate;
  553. bool is_active = false;
  554. int chan_mod;
  555. u8 active_chains;
  556. conf = ieee80211_get_hw_conf(priv->hw);
  557. mutex_lock(&priv->mutex);
  558. cancel_delayed_work(&priv->scan_check);
  559. if (!iwl_is_ready(priv)) {
  560. IWL_WARN(priv, "request scan called when driver not ready.\n");
  561. goto done;
  562. }
  563. /* Make sure the scan wasn't canceled before this queued work
  564. * was given the chance to run... */
  565. if (!test_bit(STATUS_SCANNING, &priv->status))
  566. goto done;
  567. /* This should never be called or scheduled if there is currently
  568. * a scan active in the hardware. */
  569. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  570. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
  571. "Ignoring second request.\n");
  572. goto done;
  573. }
  574. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  575. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  576. goto done;
  577. }
  578. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  579. IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
  580. goto done;
  581. }
  582. if (iwl_is_rfkill(priv)) {
  583. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  584. goto done;
  585. }
  586. if (!test_bit(STATUS_READY, &priv->status)) {
  587. IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
  588. goto done;
  589. }
  590. if (!priv->scan) {
  591. priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
  592. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  593. if (!priv->scan) {
  594. IWL_DEBUG_SCAN(priv,
  595. "fail to allocate memory for scan\n");
  596. goto done;
  597. }
  598. }
  599. scan = priv->scan;
  600. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  601. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  602. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  603. if (iwl_is_associated(priv)) {
  604. u16 interval = 0;
  605. u32 extra;
  606. u32 suspend_time = 100;
  607. u32 scan_suspend_time = 100;
  608. unsigned long flags;
  609. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  610. spin_lock_irqsave(&priv->lock, flags);
  611. interval = priv->beacon_int;
  612. spin_unlock_irqrestore(&priv->lock, flags);
  613. scan->suspend_time = 0;
  614. scan->max_out_time = cpu_to_le32(200 * 1024);
  615. if (!interval)
  616. interval = suspend_time;
  617. extra = (suspend_time / interval) << 22;
  618. scan_suspend_time = (extra |
  619. ((suspend_time % interval) * 1024));
  620. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  621. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  622. scan_suspend_time, interval);
  623. }
  624. if (priv->is_internal_short_scan) {
  625. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  626. } else if (priv->scan_request->n_ssids) {
  627. int i, p = 0;
  628. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  629. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  630. /* always does wildcard anyway */
  631. if (!priv->scan_request->ssids[i].ssid_len)
  632. continue;
  633. scan->direct_scan[p].id = WLAN_EID_SSID;
  634. scan->direct_scan[p].len =
  635. priv->scan_request->ssids[i].ssid_len;
  636. memcpy(scan->direct_scan[p].ssid,
  637. priv->scan_request->ssids[i].ssid,
  638. priv->scan_request->ssids[i].ssid_len);
  639. n_probes++;
  640. p++;
  641. }
  642. is_active = true;
  643. } else
  644. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  645. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  646. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  647. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  648. switch (priv->scan_band) {
  649. case IEEE80211_BAND_2GHZ:
  650. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  651. chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
  652. >> RXON_FLG_CHANNEL_MODE_POS;
  653. if (chan_mod == CHANNEL_MODE_PURE_40) {
  654. rate = IWL_RATE_6M_PLCP;
  655. } else {
  656. rate = IWL_RATE_1M_PLCP;
  657. rate_flags = RATE_MCS_CCK_MSK;
  658. }
  659. scan->good_CRC_th = 0;
  660. break;
  661. case IEEE80211_BAND_5GHZ:
  662. rate = IWL_RATE_6M_PLCP;
  663. /*
  664. * If active scaning is requested but a certain channel
  665. * is marked passive, we can do active scanning if we
  666. * detect transmissions.
  667. */
  668. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
  669. /* Force use of chains B and C (0x6) for scan Rx
  670. * Avoid A (0x1) for the device has off-channel reception
  671. * on A-band.
  672. */
  673. if (priv->cfg->off_channel_workaround)
  674. rx_ant = ANT_BC;
  675. break;
  676. default:
  677. IWL_WARN(priv, "Invalid scan band count\n");
  678. goto done;
  679. }
  680. band = priv->scan_band;
  681. priv->scan_tx_ant[band] =
  682. iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
  683. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  684. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  685. /* In power save mode use one chain, otherwise use all chains */
  686. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  687. /* rx_ant has been set to all valid chains previously */
  688. active_chains = rx_ant &
  689. ((u8)(priv->chain_noise_data.active_chains));
  690. if (!active_chains)
  691. active_chains = rx_ant;
  692. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  693. priv->chain_noise_data.active_chains);
  694. rx_ant = first_antenna(active_chains);
  695. }
  696. /* MIMO is not used here, but value is required */
  697. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  698. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  699. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  700. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  701. scan->rx_chain = cpu_to_le16(rx_chain);
  702. if (!priv->is_internal_short_scan) {
  703. cmd_len = iwl_fill_probe_req(priv,
  704. (struct ieee80211_mgmt *)scan->data,
  705. priv->scan_request->ie,
  706. priv->scan_request->ie_len,
  707. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  708. } else {
  709. cmd_len = iwl_fill_probe_req(priv,
  710. (struct ieee80211_mgmt *)scan->data,
  711. NULL, 0,
  712. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  713. }
  714. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  715. if (iwl_is_monitor_mode(priv))
  716. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  717. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  718. RXON_FILTER_BCON_AWARE_MSK);
  719. if (priv->is_internal_short_scan) {
  720. scan->channel_count =
  721. iwl_get_single_channel_for_scan(priv, band,
  722. (void *)&scan->data[le16_to_cpu(
  723. scan->tx_cmd.len)]);
  724. } else {
  725. scan->channel_count =
  726. iwl_get_channels_for_scan(priv, band,
  727. is_active, n_probes,
  728. (void *)&scan->data[le16_to_cpu(
  729. scan->tx_cmd.len)]);
  730. }
  731. if (scan->channel_count == 0) {
  732. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  733. goto done;
  734. }
  735. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  736. scan->channel_count * sizeof(struct iwl_scan_channel);
  737. cmd.data = scan;
  738. scan->len = cpu_to_le16(cmd.len);
  739. set_bit(STATUS_SCAN_HW, &priv->status);
  740. if (iwl_send_cmd_sync(priv, &cmd))
  741. goto done;
  742. queue_delayed_work(priv->workqueue, &priv->scan_check,
  743. IWL_SCAN_CHECK_WATCHDOG);
  744. mutex_unlock(&priv->mutex);
  745. return;
  746. done:
  747. /* Cannot perform scan. Make sure we clear scanning
  748. * bits from status so next scan request can be performed.
  749. * If we don't clear scanning status bit here all next scan
  750. * will fail
  751. */
  752. clear_bit(STATUS_SCAN_HW, &priv->status);
  753. clear_bit(STATUS_SCANNING, &priv->status);
  754. /* inform mac80211 scan aborted */
  755. queue_work(priv->workqueue, &priv->scan_completed);
  756. mutex_unlock(&priv->mutex);
  757. }
  758. void iwl_bg_abort_scan(struct work_struct *work)
  759. {
  760. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  761. if (!test_bit(STATUS_READY, &priv->status) ||
  762. !test_bit(STATUS_GEO_CONFIGURED, &priv->status))
  763. return;
  764. mutex_lock(&priv->mutex);
  765. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  766. iwl_send_scan_abort(priv);
  767. mutex_unlock(&priv->mutex);
  768. }
  769. EXPORT_SYMBOL(iwl_bg_abort_scan);
  770. void iwl_bg_scan_completed(struct work_struct *work)
  771. {
  772. struct iwl_priv *priv =
  773. container_of(work, struct iwl_priv, scan_completed);
  774. IWL_DEBUG_SCAN(priv, "SCAN complete scan\n");
  775. cancel_delayed_work(&priv->scan_check);
  776. if (!priv->is_internal_short_scan)
  777. ieee80211_scan_completed(priv->hw, false);
  778. else {
  779. priv->is_internal_short_scan = false;
  780. IWL_DEBUG_SCAN(priv, "internal short scan completed\n");
  781. }
  782. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  783. return;
  784. /* Since setting the TXPOWER may have been deferred while
  785. * performing the scan, fire one off */
  786. mutex_lock(&priv->mutex);
  787. iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
  788. mutex_unlock(&priv->mutex);
  789. }
  790. EXPORT_SYMBOL(iwl_bg_scan_completed);
  791. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  792. {
  793. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  794. INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
  795. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  796. INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
  797. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  798. }
  799. EXPORT_SYMBOL(iwl_setup_scan_deferred_work);