iwl-scan.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/slab.h>
  29. #include <linux/types.h>
  30. #include <linux/etherdevice.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-io.h"
  36. #include "iwl-agn.h"
  37. #include "iwl-trans.h"
  38. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  39. * sending probe req. This should be set long enough to hear probe responses
  40. * from more than one AP. */
  41. #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  42. #define IWL_ACTIVE_DWELL_TIME_52 (20)
  43. #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  44. #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  45. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  46. * Must be set longer than active dwell time.
  47. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  48. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  49. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  50. #define IWL_PASSIVE_DWELL_BASE (100)
  51. #define IWL_CHANNEL_TUNE_TIME 5
  52. static int iwl_send_scan_abort(struct iwl_priv *priv)
  53. {
  54. int ret;
  55. struct iwl_rx_packet *pkt;
  56. struct iwl_host_cmd cmd = {
  57. .id = REPLY_SCAN_ABORT_CMD,
  58. .flags = CMD_SYNC | CMD_WANT_SKB,
  59. };
  60. /* Exit instantly with error when device is not ready
  61. * to receive scan abort command or it does not perform
  62. * hardware scan currently */
  63. if (!test_bit(STATUS_READY, &priv->shrd->status) ||
  64. !test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) ||
  65. !test_bit(STATUS_SCAN_HW, &priv->shrd->status) ||
  66. test_bit(STATUS_FW_ERROR, &priv->shrd->status) ||
  67. test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  68. return -EIO;
  69. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  70. if (ret)
  71. return ret;
  72. pkt = (struct iwl_rx_packet *)cmd.reply_page;
  73. if (pkt->u.status != CAN_ABORT_STATUS) {
  74. /* The scan abort will return 1 for success or
  75. * 2 for "failure". A failure condition can be
  76. * due to simply not being in an active scan which
  77. * can occur if we send the scan abort before we
  78. * the microcode has notified us that a scan is
  79. * completed. */
  80. IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n", pkt->u.status);
  81. ret = -EIO;
  82. }
  83. iwl_free_pages(priv->shrd, cmd.reply_page);
  84. return ret;
  85. }
  86. static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
  87. {
  88. /* check if scan was requested from mac80211 */
  89. if (priv->scan_request) {
  90. IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
  91. ieee80211_scan_completed(priv->hw, aborted);
  92. }
  93. if (priv->scan_type == IWL_SCAN_ROC) {
  94. ieee80211_remain_on_channel_expired(priv->hw);
  95. priv->hw_roc_channel = NULL;
  96. schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
  97. }
  98. priv->scan_type = IWL_SCAN_NORMAL;
  99. priv->scan_vif = NULL;
  100. priv->scan_request = NULL;
  101. }
  102. static void iwl_process_scan_complete(struct iwl_priv *priv)
  103. {
  104. bool aborted;
  105. lockdep_assert_held(&priv->shrd->mutex);
  106. if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->shrd->status))
  107. return;
  108. IWL_DEBUG_SCAN(priv, "Completed scan.\n");
  109. cancel_delayed_work(&priv->scan_check);
  110. aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
  111. if (aborted)
  112. IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
  113. if (!test_and_clear_bit(STATUS_SCANNING, &priv->shrd->status)) {
  114. IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
  115. goto out_settings;
  116. }
  117. if (priv->scan_type == IWL_SCAN_ROC) {
  118. ieee80211_remain_on_channel_expired(priv->hw);
  119. priv->hw_roc_channel = NULL;
  120. schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
  121. }
  122. if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
  123. int err;
  124. /* Check if mac80211 requested scan during our internal scan */
  125. if (priv->scan_request == NULL)
  126. goto out_complete;
  127. /* If so request a new scan */
  128. err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
  129. priv->scan_request->channels[0]->band);
  130. if (err) {
  131. IWL_DEBUG_SCAN(priv,
  132. "failed to initiate pending scan: %d\n", err);
  133. aborted = true;
  134. goto out_complete;
  135. }
  136. return;
  137. }
  138. out_complete:
  139. iwl_complete_scan(priv, aborted);
  140. out_settings:
  141. /* Can we still talk to firmware ? */
  142. if (!iwl_is_ready_rf(priv->shrd))
  143. return;
  144. iwlagn_post_scan(priv);
  145. }
  146. void iwl_force_scan_end(struct iwl_priv *priv)
  147. {
  148. lockdep_assert_held(&priv->shrd->mutex);
  149. if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  150. IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
  151. return;
  152. }
  153. IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
  154. clear_bit(STATUS_SCANNING, &priv->shrd->status);
  155. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  156. clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
  157. clear_bit(STATUS_SCAN_COMPLETE, &priv->shrd->status);
  158. iwl_complete_scan(priv, true);
  159. }
  160. static void iwl_do_scan_abort(struct iwl_priv *priv)
  161. {
  162. int ret;
  163. lockdep_assert_held(&priv->shrd->mutex);
  164. if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  165. IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
  166. return;
  167. }
  168. if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
  169. IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
  170. return;
  171. }
  172. ret = iwl_send_scan_abort(priv);
  173. if (ret) {
  174. IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
  175. iwl_force_scan_end(priv);
  176. } else
  177. IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
  178. }
  179. /**
  180. * iwl_scan_cancel - Cancel any currently executing HW scan
  181. */
  182. int iwl_scan_cancel(struct iwl_priv *priv)
  183. {
  184. IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
  185. queue_work(priv->shrd->workqueue, &priv->abort_scan);
  186. return 0;
  187. }
  188. /**
  189. * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  190. * @ms: amount of time to wait (in milliseconds) for scan to abort
  191. *
  192. */
  193. void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  194. {
  195. unsigned long timeout = jiffies + msecs_to_jiffies(ms);
  196. lockdep_assert_held(&priv->shrd->mutex);
  197. IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
  198. iwl_do_scan_abort(priv);
  199. while (time_before_eq(jiffies, timeout)) {
  200. if (!test_bit(STATUS_SCAN_HW, &priv->shrd->status))
  201. goto finished;
  202. msleep(20);
  203. }
  204. return;
  205. finished:
  206. /*
  207. * Now STATUS_SCAN_HW is clear. This means that the
  208. * device finished, but the background work is going
  209. * to execute at best as soon as we release the mutex.
  210. * Since we need to be able to issue a new scan right
  211. * after this function returns, run the complete here.
  212. * The STATUS_SCAN_COMPLETE bit will then be cleared
  213. * and prevent the background work from "completing"
  214. * a possible new scan.
  215. */
  216. iwl_process_scan_complete(priv);
  217. }
  218. /* Service response to REPLY_SCAN_CMD (0x80) */
  219. static int iwl_rx_reply_scan(struct iwl_priv *priv,
  220. struct iwl_rx_mem_buffer *rxb,
  221. struct iwl_device_cmd *cmd)
  222. {
  223. #ifdef CONFIG_IWLWIFI_DEBUG
  224. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  225. struct iwl_scanreq_notification *notif =
  226. (struct iwl_scanreq_notification *)pkt->u.raw;
  227. IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
  228. #endif
  229. return 0;
  230. }
  231. /* Service SCAN_START_NOTIFICATION (0x82) */
  232. static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
  233. struct iwl_rx_mem_buffer *rxb,
  234. struct iwl_device_cmd *cmd)
  235. {
  236. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  237. struct iwl_scanstart_notification *notif =
  238. (struct iwl_scanstart_notification *)pkt->u.raw;
  239. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  240. IWL_DEBUG_SCAN(priv, "Scan start: "
  241. "%d [802.11%s] "
  242. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  243. notif->channel,
  244. notif->band ? "bg" : "a",
  245. le32_to_cpu(notif->tsf_high),
  246. le32_to_cpu(notif->tsf_low),
  247. notif->status, notif->beacon_timer);
  248. if (priv->scan_type == IWL_SCAN_ROC &&
  249. !priv->hw_roc_start_notified) {
  250. ieee80211_ready_on_channel(priv->hw);
  251. priv->hw_roc_start_notified = true;
  252. }
  253. return 0;
  254. }
  255. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  256. static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
  257. struct iwl_rx_mem_buffer *rxb,
  258. struct iwl_device_cmd *cmd)
  259. {
  260. #ifdef CONFIG_IWLWIFI_DEBUG
  261. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  262. struct iwl_scanresults_notification *notif =
  263. (struct iwl_scanresults_notification *)pkt->u.raw;
  264. IWL_DEBUG_SCAN(priv, "Scan ch.res: "
  265. "%d [802.11%s] "
  266. "probe status: %u:%u "
  267. "(TSF: 0x%08X:%08X) - %d "
  268. "elapsed=%lu usec\n",
  269. notif->channel,
  270. notif->band ? "bg" : "a",
  271. notif->probe_status, notif->num_probe_not_sent,
  272. le32_to_cpu(notif->tsf_high),
  273. le32_to_cpu(notif->tsf_low),
  274. le32_to_cpu(notif->statistics[0]),
  275. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
  276. #endif
  277. return 0;
  278. }
  279. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  280. static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  281. struct iwl_rx_mem_buffer *rxb,
  282. struct iwl_device_cmd *cmd)
  283. {
  284. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  285. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  286. IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  287. scan_notif->scanned_channels,
  288. scan_notif->tsf_low,
  289. scan_notif->tsf_high, scan_notif->status);
  290. IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
  291. (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  292. jiffies_to_msecs(jiffies - priv->scan_start));
  293. /*
  294. * When aborting, we run the scan completed background work inline
  295. * and the background work must then do nothing. The SCAN_COMPLETE
  296. * bit helps implement that logic and thus needs to be set before
  297. * queueing the work. Also, since the scan abort waits for SCAN_HW
  298. * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
  299. * to avoid a race there.
  300. */
  301. set_bit(STATUS_SCAN_COMPLETE, &priv->shrd->status);
  302. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  303. queue_work(priv->shrd->workqueue, &priv->scan_completed);
  304. if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
  305. iwl_advanced_bt_coexist(priv) &&
  306. priv->bt_status != scan_notif->bt_status) {
  307. if (scan_notif->bt_status) {
  308. /* BT on */
  309. if (!priv->bt_ch_announce)
  310. priv->bt_traffic_load =
  311. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  312. /*
  313. * otherwise, no traffic load information provided
  314. * no changes made
  315. */
  316. } else {
  317. /* BT off */
  318. priv->bt_traffic_load =
  319. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  320. }
  321. priv->bt_status = scan_notif->bt_status;
  322. queue_work(priv->shrd->workqueue,
  323. &priv->bt_traffic_change_work);
  324. }
  325. return 0;
  326. }
  327. void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
  328. {
  329. /* scan handlers */
  330. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  331. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  332. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  333. iwl_rx_scan_results_notif;
  334. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  335. iwl_rx_scan_complete_notif;
  336. }
  337. static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
  338. enum ieee80211_band band, u8 n_probes)
  339. {
  340. if (band == IEEE80211_BAND_5GHZ)
  341. return IWL_ACTIVE_DWELL_TIME_52 +
  342. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  343. else
  344. return IWL_ACTIVE_DWELL_TIME_24 +
  345. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  346. }
  347. static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
  348. {
  349. struct iwl_rxon_context *ctx;
  350. /*
  351. * If we're associated, we clamp the dwell time 98%
  352. * of the smallest beacon interval (minus 2 * channel
  353. * tune time)
  354. */
  355. for_each_context(priv, ctx) {
  356. u16 value;
  357. if (!iwl_is_associated_ctx(ctx))
  358. continue;
  359. value = ctx->beacon_int;
  360. if (!value)
  361. value = IWL_PASSIVE_DWELL_BASE;
  362. value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  363. dwell_time = min(value, dwell_time);
  364. }
  365. return dwell_time;
  366. }
  367. static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
  368. enum ieee80211_band band)
  369. {
  370. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  371. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  372. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  373. return iwl_limit_dwell(priv, passive);
  374. }
  375. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  376. struct ieee80211_vif *vif,
  377. enum ieee80211_band band,
  378. struct iwl_scan_channel *scan_ch)
  379. {
  380. const struct ieee80211_supported_band *sband;
  381. u16 passive_dwell = 0;
  382. u16 active_dwell = 0;
  383. int added = 0;
  384. u16 channel = 0;
  385. sband = iwl_get_hw_mode(priv, band);
  386. if (!sband) {
  387. IWL_ERR(priv, "invalid band\n");
  388. return added;
  389. }
  390. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  391. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  392. if (passive_dwell <= active_dwell)
  393. passive_dwell = active_dwell + 1;
  394. channel = iwl_get_single_channel_number(priv, band);
  395. if (channel) {
  396. scan_ch->channel = cpu_to_le16(channel);
  397. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  398. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  399. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  400. /* Set txpower levels to defaults */
  401. scan_ch->dsp_atten = 110;
  402. if (band == IEEE80211_BAND_5GHZ)
  403. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  404. else
  405. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  406. added++;
  407. } else
  408. IWL_ERR(priv, "no valid channel found\n");
  409. return added;
  410. }
  411. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  412. struct ieee80211_vif *vif,
  413. enum ieee80211_band band,
  414. u8 is_active, u8 n_probes,
  415. struct iwl_scan_channel *scan_ch)
  416. {
  417. struct ieee80211_channel *chan;
  418. const struct ieee80211_supported_band *sband;
  419. const struct iwl_channel_info *ch_info;
  420. u16 passive_dwell = 0;
  421. u16 active_dwell = 0;
  422. int added, i;
  423. u16 channel;
  424. sband = iwl_get_hw_mode(priv, band);
  425. if (!sband)
  426. return 0;
  427. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  428. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  429. if (passive_dwell <= active_dwell)
  430. passive_dwell = active_dwell + 1;
  431. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  432. chan = priv->scan_request->channels[i];
  433. if (chan->band != band)
  434. continue;
  435. channel = chan->hw_value;
  436. scan_ch->channel = cpu_to_le16(channel);
  437. ch_info = iwl_get_channel_info(priv, band, channel);
  438. if (!is_channel_valid(ch_info)) {
  439. IWL_DEBUG_SCAN(priv,
  440. "Channel %d is INVALID for this band.\n",
  441. channel);
  442. continue;
  443. }
  444. if (!is_active || is_channel_passive(ch_info) ||
  445. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  446. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  447. else
  448. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  449. if (n_probes)
  450. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  451. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  452. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  453. /* Set txpower levels to defaults */
  454. scan_ch->dsp_atten = 110;
  455. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  456. * power level:
  457. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  458. */
  459. if (band == IEEE80211_BAND_5GHZ)
  460. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  461. else
  462. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  463. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  464. channel, le32_to_cpu(scan_ch->type),
  465. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  466. "ACTIVE" : "PASSIVE",
  467. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  468. active_dwell : passive_dwell);
  469. scan_ch++;
  470. added++;
  471. }
  472. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  473. return added;
  474. }
  475. static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  476. {
  477. struct iwl_host_cmd cmd = {
  478. .id = REPLY_SCAN_CMD,
  479. .len = { sizeof(struct iwl_scan_cmd), },
  480. .flags = CMD_SYNC,
  481. };
  482. struct iwl_scan_cmd *scan;
  483. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  484. u32 rate_flags = 0;
  485. u16 cmd_len;
  486. u16 rx_chain = 0;
  487. enum ieee80211_band band;
  488. u8 n_probes = 0;
  489. u8 rx_ant = hw_params(priv).valid_rx_ant;
  490. u8 rate;
  491. bool is_active = false;
  492. int chan_mod;
  493. u8 active_chains;
  494. u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
  495. int ret;
  496. lockdep_assert_held(&priv->shrd->mutex);
  497. if (vif)
  498. ctx = iwl_rxon_ctx_from_vif(vif);
  499. if (!priv->scan_cmd) {
  500. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  501. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  502. if (!priv->scan_cmd) {
  503. IWL_DEBUG_SCAN(priv,
  504. "fail to allocate memory for scan\n");
  505. return -ENOMEM;
  506. }
  507. }
  508. scan = priv->scan_cmd;
  509. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  510. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  511. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  512. if (priv->scan_type != IWL_SCAN_ROC &&
  513. iwl_is_any_associated(priv)) {
  514. u16 interval = 0;
  515. u32 extra;
  516. u32 suspend_time = 100;
  517. u32 scan_suspend_time = 100;
  518. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  519. switch (priv->scan_type) {
  520. case IWL_SCAN_ROC:
  521. WARN_ON(1);
  522. break;
  523. case IWL_SCAN_RADIO_RESET:
  524. interval = 0;
  525. break;
  526. case IWL_SCAN_NORMAL:
  527. interval = vif->bss_conf.beacon_int;
  528. break;
  529. }
  530. scan->suspend_time = 0;
  531. scan->max_out_time = cpu_to_le32(200 * 1024);
  532. if (!interval)
  533. interval = suspend_time;
  534. extra = (suspend_time / interval) << 22;
  535. scan_suspend_time = (extra |
  536. ((suspend_time % interval) * 1024));
  537. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  538. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  539. scan_suspend_time, interval);
  540. } else if (priv->scan_type == IWL_SCAN_ROC) {
  541. scan->suspend_time = 0;
  542. scan->max_out_time = 0;
  543. scan->quiet_time = 0;
  544. scan->quiet_plcp_th = 0;
  545. }
  546. switch (priv->scan_type) {
  547. case IWL_SCAN_RADIO_RESET:
  548. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  549. break;
  550. case IWL_SCAN_NORMAL:
  551. if (priv->scan_request->n_ssids) {
  552. int i, p = 0;
  553. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  554. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  555. /* always does wildcard anyway */
  556. if (!priv->scan_request->ssids[i].ssid_len)
  557. continue;
  558. scan->direct_scan[p].id = WLAN_EID_SSID;
  559. scan->direct_scan[p].len =
  560. priv->scan_request->ssids[i].ssid_len;
  561. memcpy(scan->direct_scan[p].ssid,
  562. priv->scan_request->ssids[i].ssid,
  563. priv->scan_request->ssids[i].ssid_len);
  564. n_probes++;
  565. p++;
  566. }
  567. is_active = true;
  568. } else
  569. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  570. break;
  571. case IWL_SCAN_ROC:
  572. IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
  573. break;
  574. }
  575. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  576. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  577. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  578. switch (priv->scan_band) {
  579. case IEEE80211_BAND_2GHZ:
  580. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  581. chan_mod = le32_to_cpu(
  582. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  583. RXON_FLG_CHANNEL_MODE_MSK)
  584. >> RXON_FLG_CHANNEL_MODE_POS;
  585. if (chan_mod == CHANNEL_MODE_PURE_40) {
  586. rate = IWL_RATE_6M_PLCP;
  587. } else {
  588. rate = IWL_RATE_1M_PLCP;
  589. rate_flags = RATE_MCS_CCK_MSK;
  590. }
  591. /*
  592. * Internal scans are passive, so we can indiscriminately set
  593. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  594. */
  595. if (priv->cfg->bt_params &&
  596. priv->cfg->bt_params->advanced_bt_coexist)
  597. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  598. break;
  599. case IEEE80211_BAND_5GHZ:
  600. rate = IWL_RATE_6M_PLCP;
  601. break;
  602. default:
  603. IWL_WARN(priv, "Invalid scan band\n");
  604. return -EIO;
  605. }
  606. /*
  607. * If active scanning is requested but a certain channel is
  608. * marked passive, we can do active scanning if we detect
  609. * transmissions.
  610. *
  611. * There is an issue with some firmware versions that triggers
  612. * a sysassert on a "good CRC threshold" of zero (== disabled),
  613. * on a radar channel even though this means that we should NOT
  614. * send probes.
  615. *
  616. * The "good CRC threshold" is the number of frames that we
  617. * need to receive during our dwell time on a channel before
  618. * sending out probes -- setting this to a huge value will
  619. * mean we never reach it, but at the same time work around
  620. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  621. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  622. *
  623. * This was fixed in later versions along with some other
  624. * scan changes, and the threshold behaves as a flag in those
  625. * versions.
  626. */
  627. if (priv->new_scan_threshold_behaviour)
  628. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  629. IWL_GOOD_CRC_TH_DISABLED;
  630. else
  631. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  632. IWL_GOOD_CRC_TH_NEVER;
  633. band = priv->scan_band;
  634. if (priv->cfg->scan_rx_antennas[band])
  635. rx_ant = priv->cfg->scan_rx_antennas[band];
  636. if (band == IEEE80211_BAND_2GHZ &&
  637. priv->cfg->bt_params &&
  638. priv->cfg->bt_params->advanced_bt_coexist) {
  639. /* transmit 2.4 GHz probes only on first antenna */
  640. scan_tx_antennas = first_antenna(scan_tx_antennas);
  641. }
  642. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
  643. priv->scan_tx_ant[band],
  644. scan_tx_antennas);
  645. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  646. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  647. /* In power save mode use one chain, otherwise use all chains */
  648. if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
  649. /* rx_ant has been set to all valid chains previously */
  650. active_chains = rx_ant &
  651. ((u8)(priv->chain_noise_data.active_chains));
  652. if (!active_chains)
  653. active_chains = rx_ant;
  654. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  655. priv->chain_noise_data.active_chains);
  656. rx_ant = first_antenna(active_chains);
  657. }
  658. if (priv->cfg->bt_params &&
  659. priv->cfg->bt_params->advanced_bt_coexist &&
  660. priv->bt_full_concurrent) {
  661. /* operated as 1x1 in full concurrency mode */
  662. rx_ant = first_antenna(rx_ant);
  663. }
  664. /* MIMO is not used here, but value is required */
  665. rx_chain |=
  666. hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  667. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  668. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  669. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  670. scan->rx_chain = cpu_to_le16(rx_chain);
  671. switch (priv->scan_type) {
  672. case IWL_SCAN_NORMAL:
  673. cmd_len = iwl_fill_probe_req(priv,
  674. (struct ieee80211_mgmt *)scan->data,
  675. vif->addr,
  676. priv->scan_request->ie,
  677. priv->scan_request->ie_len,
  678. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  679. break;
  680. case IWL_SCAN_RADIO_RESET:
  681. case IWL_SCAN_ROC:
  682. /* use bcast addr, will not be transmitted but must be valid */
  683. cmd_len = iwl_fill_probe_req(priv,
  684. (struct ieee80211_mgmt *)scan->data,
  685. iwl_bcast_addr, NULL, 0,
  686. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  687. break;
  688. default:
  689. BUG();
  690. }
  691. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  692. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  693. RXON_FILTER_BCON_AWARE_MSK);
  694. switch (priv->scan_type) {
  695. case IWL_SCAN_RADIO_RESET:
  696. scan->channel_count =
  697. iwl_get_single_channel_for_scan(priv, vif, band,
  698. (void *)&scan->data[cmd_len]);
  699. break;
  700. case IWL_SCAN_NORMAL:
  701. scan->channel_count =
  702. iwl_get_channels_for_scan(priv, vif, band,
  703. is_active, n_probes,
  704. (void *)&scan->data[cmd_len]);
  705. break;
  706. case IWL_SCAN_ROC: {
  707. struct iwl_scan_channel *scan_ch;
  708. int n_chan, i;
  709. u16 dwell;
  710. dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
  711. n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
  712. scan->channel_count = n_chan;
  713. scan_ch = (void *)&scan->data[cmd_len];
  714. for (i = 0; i < n_chan; i++) {
  715. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  716. scan_ch->channel =
  717. cpu_to_le16(priv->hw_roc_channel->hw_value);
  718. if (i == n_chan - 1)
  719. dwell = priv->hw_roc_duration - i * dwell;
  720. scan_ch->active_dwell =
  721. scan_ch->passive_dwell = cpu_to_le16(dwell);
  722. /* Set txpower levels to defaults */
  723. scan_ch->dsp_atten = 110;
  724. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  725. * power level:
  726. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  727. */
  728. if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
  729. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  730. else
  731. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  732. scan_ch++;
  733. }
  734. }
  735. break;
  736. }
  737. if (scan->channel_count == 0) {
  738. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  739. return -EIO;
  740. }
  741. cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
  742. scan->channel_count * sizeof(struct iwl_scan_channel);
  743. cmd.data[0] = scan;
  744. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  745. scan->len = cpu_to_le16(cmd.len[0]);
  746. /* set scan bit here for PAN params */
  747. set_bit(STATUS_SCAN_HW, &priv->shrd->status);
  748. ret = iwlagn_set_pan_params(priv);
  749. if (ret)
  750. return ret;
  751. ret = iwl_trans_send_cmd(trans(priv), &cmd);
  752. if (ret) {
  753. clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
  754. iwlagn_set_pan_params(priv);
  755. }
  756. return ret;
  757. }
  758. void iwl_init_scan_params(struct iwl_priv *priv)
  759. {
  760. u8 ant_idx = fls(hw_params(priv).valid_tx_ant) - 1;
  761. if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
  762. priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  763. if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
  764. priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  765. }
  766. int __must_check iwl_scan_initiate(struct iwl_priv *priv,
  767. struct ieee80211_vif *vif,
  768. enum iwl_scan_type scan_type,
  769. enum ieee80211_band band)
  770. {
  771. int ret;
  772. lockdep_assert_held(&priv->shrd->mutex);
  773. cancel_delayed_work(&priv->scan_check);
  774. if (!iwl_is_ready_rf(priv->shrd)) {
  775. IWL_WARN(priv, "Request scan called when driver not ready.\n");
  776. return -EIO;
  777. }
  778. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  779. IWL_DEBUG_SCAN(priv,
  780. "Multiple concurrent scan requests in parallel.\n");
  781. return -EBUSY;
  782. }
  783. if (test_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
  784. IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
  785. return -EBUSY;
  786. }
  787. IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
  788. scan_type == IWL_SCAN_NORMAL ? "" :
  789. scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
  790. "internal short ");
  791. set_bit(STATUS_SCANNING, &priv->shrd->status);
  792. priv->scan_type = scan_type;
  793. priv->scan_start = jiffies;
  794. priv->scan_band = band;
  795. ret = iwlagn_request_scan(priv, vif);
  796. if (ret) {
  797. clear_bit(STATUS_SCANNING, &priv->shrd->status);
  798. priv->scan_type = IWL_SCAN_NORMAL;
  799. return ret;
  800. }
  801. queue_delayed_work(priv->shrd->workqueue, &priv->scan_check,
  802. IWL_SCAN_CHECK_WATCHDOG);
  803. return 0;
  804. }
  805. int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
  806. struct ieee80211_vif *vif,
  807. struct cfg80211_scan_request *req)
  808. {
  809. struct iwl_priv *priv = hw->priv;
  810. int ret;
  811. IWL_DEBUG_MAC80211(priv, "enter\n");
  812. if (req->n_channels == 0)
  813. return -EINVAL;
  814. mutex_lock(&priv->shrd->mutex);
  815. /*
  816. * If an internal scan is in progress, just set
  817. * up the scan_request as per above.
  818. */
  819. if (priv->scan_type != IWL_SCAN_NORMAL) {
  820. IWL_DEBUG_SCAN(priv,
  821. "SCAN request during internal scan - defer\n");
  822. priv->scan_request = req;
  823. priv->scan_vif = vif;
  824. ret = 0;
  825. } else {
  826. priv->scan_request = req;
  827. priv->scan_vif = vif;
  828. /*
  829. * mac80211 will only ask for one band at a time
  830. * so using channels[0] here is ok
  831. */
  832. ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
  833. req->channels[0]->band);
  834. if (ret) {
  835. priv->scan_request = NULL;
  836. priv->scan_vif = NULL;
  837. }
  838. }
  839. IWL_DEBUG_MAC80211(priv, "leave\n");
  840. mutex_unlock(&priv->shrd->mutex);
  841. return ret;
  842. }
  843. /*
  844. * internal short scan, this function should only been called while associated.
  845. * It will reset and tune the radio to prevent possible RF related problem
  846. */
  847. void iwl_internal_short_hw_scan(struct iwl_priv *priv)
  848. {
  849. queue_work(priv->shrd->workqueue, &priv->start_internal_scan);
  850. }
  851. static void iwl_bg_start_internal_scan(struct work_struct *work)
  852. {
  853. struct iwl_priv *priv =
  854. container_of(work, struct iwl_priv, start_internal_scan);
  855. IWL_DEBUG_SCAN(priv, "Start internal scan\n");
  856. mutex_lock(&priv->shrd->mutex);
  857. if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
  858. IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
  859. goto unlock;
  860. }
  861. if (test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  862. IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
  863. goto unlock;
  864. }
  865. if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
  866. IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
  867. unlock:
  868. mutex_unlock(&priv->shrd->mutex);
  869. }
  870. static void iwl_bg_scan_check(struct work_struct *data)
  871. {
  872. struct iwl_priv *priv =
  873. container_of(data, struct iwl_priv, scan_check.work);
  874. IWL_DEBUG_SCAN(priv, "Scan check work\n");
  875. /* Since we are here firmware does not finish scan and
  876. * most likely is in bad shape, so we don't bother to
  877. * send abort command, just force scan complete to mac80211 */
  878. mutex_lock(&priv->shrd->mutex);
  879. iwl_force_scan_end(priv);
  880. mutex_unlock(&priv->shrd->mutex);
  881. }
  882. /**
  883. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  884. */
  885. u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
  886. const u8 *ta, const u8 *ies, int ie_len, int left)
  887. {
  888. int len = 0;
  889. u8 *pos = NULL;
  890. /* Make sure there is enough space for the probe request,
  891. * two mandatory IEs and the data */
  892. left -= 24;
  893. if (left < 0)
  894. return 0;
  895. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  896. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  897. memcpy(frame->sa, ta, ETH_ALEN);
  898. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  899. frame->seq_ctrl = 0;
  900. len += 24;
  901. /* ...next IE... */
  902. pos = &frame->u.probe_req.variable[0];
  903. /* fill in our indirect SSID IE */
  904. left -= 2;
  905. if (left < 0)
  906. return 0;
  907. *pos++ = WLAN_EID_SSID;
  908. *pos++ = 0;
  909. len += 2;
  910. if (WARN_ON(left < ie_len))
  911. return len;
  912. if (ies && ie_len) {
  913. memcpy(pos, ies, ie_len);
  914. len += ie_len;
  915. }
  916. return (u16)len;
  917. }
  918. static void iwl_bg_abort_scan(struct work_struct *work)
  919. {
  920. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  921. IWL_DEBUG_SCAN(priv, "Abort scan work\n");
  922. /* We keep scan_check work queued in case when firmware will not
  923. * report back scan completed notification */
  924. mutex_lock(&priv->shrd->mutex);
  925. iwl_scan_cancel_timeout(priv, 200);
  926. mutex_unlock(&priv->shrd->mutex);
  927. }
  928. static void iwl_bg_scan_completed(struct work_struct *work)
  929. {
  930. struct iwl_priv *priv =
  931. container_of(work, struct iwl_priv, scan_completed);
  932. mutex_lock(&priv->shrd->mutex);
  933. iwl_process_scan_complete(priv);
  934. mutex_unlock(&priv->shrd->mutex);
  935. }
  936. void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
  937. {
  938. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  939. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  940. INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
  941. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  942. }
  943. void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
  944. {
  945. cancel_work_sync(&priv->start_internal_scan);
  946. cancel_work_sync(&priv->abort_scan);
  947. cancel_work_sync(&priv->scan_completed);
  948. if (cancel_delayed_work_sync(&priv->scan_check)) {
  949. mutex_lock(&priv->shrd->mutex);
  950. iwl_force_scan_end(priv);
  951. mutex_unlock(&priv->shrd->mutex);
  952. }
  953. }