scan.c 29 KB

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