iwl-scan.c 26 KB

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