iwl-scan.c 25 KB

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