iwl-scan.c 28 KB

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