iwl-scan.c 28 KB

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