iwl-scan.c 26 KB

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