iwl-scan.c 26 KB

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