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