iwl-scan.c 24 KB

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