iwl-scan.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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 == 2) ? "2.4" : "5.2",
  241. jiffies_to_msecs(elapsed_jiffies
  242. (priv->scan_pass_start, jiffies)));
  243. /* Remove this scanned band from the list
  244. * of pending bands to scan */
  245. priv->scan_bands--;
  246. /* If a request to abort was given, or the scan did not succeed
  247. * then we reset the scan state machine and terminate,
  248. * re-queuing another scan if one has been requested */
  249. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  250. IWL_DEBUG_INFO("Aborted scan completed.\n");
  251. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  252. } else {
  253. /* If there are more bands on this scan pass reschedule */
  254. if (priv->scan_bands > 0)
  255. goto reschedule;
  256. }
  257. priv->last_scan_jiffies = jiffies;
  258. priv->next_scan_jiffies = 0;
  259. IWL_DEBUG_INFO("Setting scan to off\n");
  260. clear_bit(STATUS_SCANNING, &priv->status);
  261. IWL_DEBUG_INFO("Scan took %dms\n",
  262. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  263. queue_work(priv->workqueue, &priv->scan_completed);
  264. return;
  265. reschedule:
  266. priv->scan_pass_start = jiffies;
  267. queue_work(priv->workqueue, &priv->request_scan);
  268. }
  269. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  270. {
  271. /* scan handlers */
  272. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  273. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  274. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  275. iwl_rx_scan_results_notif;
  276. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  277. iwl_rx_scan_complete_notif;
  278. }
  279. EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
  280. static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  281. enum ieee80211_band band)
  282. {
  283. if (band == IEEE80211_BAND_5GHZ)
  284. return IWL_ACTIVE_DWELL_TIME_52;
  285. else
  286. return IWL_ACTIVE_DWELL_TIME_24;
  287. }
  288. static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  289. enum ieee80211_band band)
  290. {
  291. u16 active = iwl_get_active_dwell_time(priv, band);
  292. u16 passive = (band != IEEE80211_BAND_5GHZ) ?
  293. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  294. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  295. if (iwl_is_associated(priv)) {
  296. /* If we're associated, we clamp the maximum passive
  297. * dwell time to be 98% of the beacon interval (minus
  298. * 2 * channel tune time) */
  299. passive = priv->beacon_int;
  300. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  301. passive = IWL_PASSIVE_DWELL_BASE;
  302. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  303. }
  304. if (passive <= active)
  305. passive = active + 1;
  306. return passive;
  307. }
  308. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  309. enum ieee80211_band band,
  310. u8 is_active, u8 direct_mask,
  311. struct iwl_scan_channel *scan_ch)
  312. {
  313. const struct ieee80211_channel *channels = NULL;
  314. const struct ieee80211_supported_band *sband;
  315. const struct iwl_channel_info *ch_info;
  316. u16 passive_dwell = 0;
  317. u16 active_dwell = 0;
  318. int added, i;
  319. sband = iwl_get_hw_mode(priv, band);
  320. if (!sband)
  321. return 0;
  322. channels = sband->channels;
  323. active_dwell = iwl_get_active_dwell_time(priv, band);
  324. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  325. for (i = 0, added = 0; i < sband->n_channels; i++) {
  326. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  327. continue;
  328. scan_ch->channel =
  329. ieee80211_frequency_to_channel(channels[i].center_freq);
  330. ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
  331. if (!is_channel_valid(ch_info)) {
  332. IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
  333. scan_ch->channel);
  334. continue;
  335. }
  336. if (!is_active || is_channel_passive(ch_info) ||
  337. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
  338. scan_ch->type = 0;
  339. else
  340. scan_ch->type = 1;
  341. if (scan_ch->type & 1)
  342. scan_ch->type |= (direct_mask << 1);
  343. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  344. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  345. /* Set txpower levels to defaults */
  346. scan_ch->dsp_atten = 110;
  347. if (band == IEEE80211_BAND_5GHZ)
  348. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  349. else {
  350. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  351. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  352. * power level:
  353. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  354. */
  355. }
  356. IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
  357. scan_ch->channel,
  358. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  359. (scan_ch->type & 1) ?
  360. active_dwell : passive_dwell);
  361. scan_ch++;
  362. added++;
  363. }
  364. IWL_DEBUG_SCAN("total channels to scan %d \n", added);
  365. return added;
  366. }
  367. void iwl_init_scan_params(struct iwl_priv *priv)
  368. {
  369. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  370. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = RATE_MCS_ANT_INIT_IND;
  371. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  372. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = RATE_MCS_ANT_INIT_IND;
  373. }
  374. int iwl_scan_initiate(struct iwl_priv *priv)
  375. {
  376. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  377. IWL_ERROR("APs don't scan.\n");
  378. return 0;
  379. }
  380. if (!iwl_is_ready_rf(priv)) {
  381. IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
  382. return -EIO;
  383. }
  384. if (test_bit(STATUS_SCANNING, &priv->status)) {
  385. IWL_DEBUG_SCAN("Scan already in progress.\n");
  386. return -EAGAIN;
  387. }
  388. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  389. IWL_DEBUG_SCAN("Scan request while abort pending. "
  390. "Queuing.\n");
  391. return -EAGAIN;
  392. }
  393. IWL_DEBUG_INFO("Starting scan...\n");
  394. priv->scan_bands = 2;
  395. set_bit(STATUS_SCANNING, &priv->status);
  396. priv->scan_start = jiffies;
  397. priv->scan_pass_start = priv->scan_start;
  398. queue_work(priv->workqueue, &priv->request_scan);
  399. return 0;
  400. }
  401. EXPORT_SYMBOL(iwl_scan_initiate);
  402. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  403. static void iwl_bg_scan_check(struct work_struct *data)
  404. {
  405. struct iwl_priv *priv =
  406. container_of(data, struct iwl_priv, scan_check.work);
  407. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  408. return;
  409. mutex_lock(&priv->mutex);
  410. if (test_bit(STATUS_SCANNING, &priv->status) ||
  411. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  412. IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
  413. "adapter (%dms)\n",
  414. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  415. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  416. iwl_send_scan_abort(priv);
  417. }
  418. mutex_unlock(&priv->mutex);
  419. }
  420. /**
  421. * iwl_supported_rate_to_ie - fill in the supported rate in IE field
  422. *
  423. * return : set the bit for each supported rate insert in ie
  424. */
  425. static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
  426. u16 basic_rate, int *left)
  427. {
  428. u16 ret_rates = 0, bit;
  429. int i;
  430. u8 *cnt = ie;
  431. u8 *rates = ie + 1;
  432. for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
  433. if (bit & supported_rate) {
  434. ret_rates |= bit;
  435. rates[*cnt] = iwl_rates[i].ieee |
  436. ((bit & basic_rate) ? 0x80 : 0x00);
  437. (*cnt)++;
  438. (*left)--;
  439. if ((*left <= 0) ||
  440. (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
  441. break;
  442. }
  443. }
  444. return ret_rates;
  445. }
  446. static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
  447. u8 *pos, int *left)
  448. {
  449. struct ieee80211_ht_cap *ht_cap;
  450. if (!sband || !sband->ht_info.ht_supported)
  451. return;
  452. if (*left < sizeof(struct ieee80211_ht_cap))
  453. return;
  454. *pos++ = sizeof(struct ieee80211_ht_cap);
  455. ht_cap = (struct ieee80211_ht_cap *) pos;
  456. ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
  457. memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
  458. ht_cap->ampdu_params_info =
  459. (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
  460. ((sband->ht_info.ampdu_density << 2) &
  461. IEEE80211_HT_CAP_AMPDU_DENSITY);
  462. *left -= sizeof(struct ieee80211_ht_cap);
  463. }
  464. /**
  465. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  466. */
  467. static u16 iwl_fill_probe_req(struct iwl_priv *priv,
  468. enum ieee80211_band band,
  469. struct ieee80211_mgmt *frame,
  470. int left)
  471. {
  472. int len = 0;
  473. u8 *pos = NULL;
  474. u16 active_rates, ret_rates, cck_rates, active_rate_basic;
  475. const struct ieee80211_supported_band *sband =
  476. iwl_get_hw_mode(priv, band);
  477. /* Make sure there is enough space for the probe request,
  478. * two mandatory IEs and the data */
  479. left -= 24;
  480. if (left < 0)
  481. return 0;
  482. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  483. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  484. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  485. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  486. frame->seq_ctrl = 0;
  487. len += 24;
  488. /* ...next IE... */
  489. pos = &frame->u.probe_req.variable[0];
  490. /* fill in our indirect SSID IE */
  491. left -= 2;
  492. if (left < 0)
  493. return 0;
  494. *pos++ = WLAN_EID_SSID;
  495. *pos++ = 0;
  496. len += 2;
  497. /* fill in supported rate */
  498. left -= 2;
  499. if (left < 0)
  500. return 0;
  501. *pos++ = WLAN_EID_SUPP_RATES;
  502. *pos = 0;
  503. /* exclude 60M rate */
  504. active_rates = priv->rates_mask;
  505. active_rates &= ~IWL_RATE_60M_MASK;
  506. active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
  507. cck_rates = IWL_CCK_RATES_MASK & active_rates;
  508. ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
  509. active_rate_basic, &left);
  510. active_rates &= ~ret_rates;
  511. ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
  512. active_rate_basic, &left);
  513. active_rates &= ~ret_rates;
  514. len += 2 + *pos;
  515. pos += (*pos) + 1;
  516. if (active_rates == 0)
  517. goto fill_end;
  518. /* fill in supported extended rate */
  519. /* ...next IE... */
  520. left -= 2;
  521. if (left < 0)
  522. return 0;
  523. /* ... fill it in... */
  524. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  525. *pos = 0;
  526. iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left);
  527. if (*pos > 0) {
  528. len += 2 + *pos;
  529. pos += (*pos) + 1;
  530. } else {
  531. pos--;
  532. }
  533. fill_end:
  534. left -= 2;
  535. if (left < 0)
  536. return 0;
  537. *pos++ = WLAN_EID_HT_CAPABILITY;
  538. *pos = 0;
  539. iwl_ht_cap_to_ie(sband, pos, &left);
  540. if (*pos > 0)
  541. len += 2 + *pos;
  542. return (u16)len;
  543. }
  544. static u32 iwl_scan_tx_ant(struct iwl_priv *priv, enum ieee80211_band band)
  545. {
  546. int i, ind;
  547. ind = priv->scan_tx_ant[band];
  548. for (i = 0; i < priv->hw_params.tx_chains_num; i++) {
  549. ind = (ind+1) >= priv->hw_params.tx_chains_num ? 0 : ind+1;
  550. if (priv->hw_params.valid_tx_ant & (1 << ind)) {
  551. priv->scan_tx_ant[band] = ind;
  552. break;
  553. }
  554. }
  555. return scan_tx_ant[ind];
  556. }
  557. static void iwl_bg_request_scan(struct work_struct *data)
  558. {
  559. struct iwl_priv *priv =
  560. container_of(data, struct iwl_priv, request_scan);
  561. struct iwl_host_cmd cmd = {
  562. .id = REPLY_SCAN_CMD,
  563. .len = sizeof(struct iwl_scan_cmd),
  564. .meta.flags = CMD_SIZE_HUGE,
  565. };
  566. struct iwl_scan_cmd *scan;
  567. struct ieee80211_conf *conf = NULL;
  568. int ret = 0;
  569. u32 tx_ant;
  570. u16 cmd_len;
  571. enum ieee80211_band band;
  572. u8 direct_mask;
  573. u8 rx_chain = 0x7; /* bitmap: ABC chains */
  574. conf = ieee80211_get_hw_conf(priv->hw);
  575. mutex_lock(&priv->mutex);
  576. if (!iwl_is_ready(priv)) {
  577. IWL_WARNING("request scan called when driver not ready.\n");
  578. goto done;
  579. }
  580. /* Make sure the scan wasn't cancelled before this queued work
  581. * was given the chance to run... */
  582. if (!test_bit(STATUS_SCANNING, &priv->status))
  583. goto done;
  584. /* This should never be called or scheduled if there is currently
  585. * a scan active in the hardware. */
  586. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  587. IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
  588. "Ignoring second request.\n");
  589. ret = -EIO;
  590. goto done;
  591. }
  592. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  593. IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
  594. goto done;
  595. }
  596. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  597. IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
  598. goto done;
  599. }
  600. if (iwl_is_rfkill(priv)) {
  601. IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
  602. goto done;
  603. }
  604. if (!test_bit(STATUS_READY, &priv->status)) {
  605. IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
  606. goto done;
  607. }
  608. if (!priv->scan_bands) {
  609. IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
  610. goto done;
  611. }
  612. if (!priv->scan) {
  613. priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
  614. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  615. if (!priv->scan) {
  616. ret = -ENOMEM;
  617. goto done;
  618. }
  619. }
  620. scan = priv->scan;
  621. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  622. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  623. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  624. if (iwl_is_associated(priv)) {
  625. u16 interval = 0;
  626. u32 extra;
  627. u32 suspend_time = 100;
  628. u32 scan_suspend_time = 100;
  629. unsigned long flags;
  630. IWL_DEBUG_INFO("Scanning while associated...\n");
  631. spin_lock_irqsave(&priv->lock, flags);
  632. interval = priv->beacon_int;
  633. spin_unlock_irqrestore(&priv->lock, flags);
  634. scan->suspend_time = 0;
  635. scan->max_out_time = cpu_to_le32(200 * 1024);
  636. if (!interval)
  637. interval = suspend_time;
  638. extra = (suspend_time / interval) << 22;
  639. scan_suspend_time = (extra |
  640. ((suspend_time % interval) * 1024));
  641. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  642. IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
  643. scan_suspend_time, interval);
  644. }
  645. /* We should add the ability for user to lock to PASSIVE ONLY */
  646. if (priv->one_direct_scan) {
  647. IWL_DEBUG_SCAN("Start direct scan for '%s'\n",
  648. iwl_escape_essid(priv->direct_ssid,
  649. priv->direct_ssid_len));
  650. scan->direct_scan[0].id = WLAN_EID_SSID;
  651. scan->direct_scan[0].len = priv->direct_ssid_len;
  652. memcpy(scan->direct_scan[0].ssid,
  653. priv->direct_ssid, priv->direct_ssid_len);
  654. direct_mask = 1;
  655. } else if (!iwl_is_associated(priv) && priv->essid_len) {
  656. IWL_DEBUG_SCAN("Start direct scan for '%s' (not associated)\n",
  657. iwl_escape_essid(priv->essid, priv->essid_len));
  658. scan->direct_scan[0].id = WLAN_EID_SSID;
  659. scan->direct_scan[0].len = priv->essid_len;
  660. memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
  661. direct_mask = 1;
  662. } else {
  663. IWL_DEBUG_SCAN("Start indirect scan.\n");
  664. direct_mask = 0;
  665. }
  666. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  667. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  668. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  669. switch (priv->scan_bands) {
  670. case 2:
  671. band = IEEE80211_BAND_2GHZ;
  672. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  673. tx_ant = iwl_scan_tx_ant(priv, band);
  674. if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK)
  675. scan->tx_cmd.rate_n_flags =
  676. iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
  677. tx_ant);
  678. else
  679. scan->tx_cmd.rate_n_flags =
  680. iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
  681. tx_ant |
  682. RATE_MCS_CCK_MSK);
  683. scan->good_CRC_th = 0;
  684. break;
  685. case 1:
  686. band = IEEE80211_BAND_5GHZ;
  687. tx_ant = iwl_scan_tx_ant(priv, band);
  688. scan->tx_cmd.rate_n_flags =
  689. iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
  690. tx_ant);
  691. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  692. /* Force use of chains B and C (0x6) for scan Rx for 4965
  693. * Avoid A (0x1) because of its off-channel reception on A-band.
  694. * MIMO is not used here, but value is required */
  695. if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
  696. rx_chain = 0x6;
  697. break;
  698. default:
  699. IWL_WARNING("Invalid scan band count\n");
  700. goto done;
  701. }
  702. scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
  703. cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
  704. (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) |
  705. (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
  706. cmd_len = iwl_fill_probe_req(priv, band,
  707. (struct ieee80211_mgmt *)scan->data,
  708. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  709. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  710. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
  711. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  712. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  713. RXON_FILTER_BCON_AWARE_MSK);
  714. if (direct_mask)
  715. scan->channel_count =
  716. iwl_get_channels_for_scan(priv, band, 1, /* active */
  717. direct_mask,
  718. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  719. else
  720. scan->channel_count =
  721. iwl_get_channels_for_scan(priv, band, 0, /* passive */
  722. direct_mask,
  723. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  724. if (scan->channel_count == 0) {
  725. IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
  726. goto done;
  727. }
  728. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  729. scan->channel_count * sizeof(struct iwl_scan_channel);
  730. cmd.data = scan;
  731. scan->len = cpu_to_le16(cmd.len);
  732. set_bit(STATUS_SCAN_HW, &priv->status);
  733. ret = iwl_send_cmd_sync(priv, &cmd);
  734. if (ret)
  735. goto done;
  736. queue_delayed_work(priv->workqueue, &priv->scan_check,
  737. IWL_SCAN_CHECK_WATCHDOG);
  738. mutex_unlock(&priv->mutex);
  739. return;
  740. done:
  741. /* inform mac80211 scan aborted */
  742. queue_work(priv->workqueue, &priv->scan_completed);
  743. mutex_unlock(&priv->mutex);
  744. }
  745. static void iwl_bg_abort_scan(struct work_struct *work)
  746. {
  747. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  748. if (!iwl_is_ready(priv))
  749. return;
  750. mutex_lock(&priv->mutex);
  751. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  752. iwl_send_scan_abort(priv);
  753. mutex_unlock(&priv->mutex);
  754. }
  755. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  756. {
  757. /* FIXME: move here when resolved PENDING
  758. * INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed); */
  759. INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
  760. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  761. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  762. }
  763. EXPORT_SYMBOL(iwl_setup_scan_deferred_work);