iwl-scan.c 25 KB

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