iwl-scan.c 26 KB

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