18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/******************************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
58c2ecf20Sopenharmony_ci * Copyright(c) 2018        Intel Corporation
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Contact Information:
88c2ecf20Sopenharmony_ci *  Intel Linux Wireless <linuxwifi@intel.com>
98c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
108c2ecf20Sopenharmony_ci *****************************************************************************/
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
148c2ecf20Sopenharmony_ci#include <net/mac80211.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "dev.h"
178c2ecf20Sopenharmony_ci#include "agn.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
208c2ecf20Sopenharmony_ci * sending probe req.  This should be set long enough to hear probe responses
218c2ecf20Sopenharmony_ci * from more than one AP.  */
228c2ecf20Sopenharmony_ci#define IWL_ACTIVE_DWELL_TIME_24    (30)       /* all times in msec */
238c2ecf20Sopenharmony_ci#define IWL_ACTIVE_DWELL_TIME_52    (20)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
268c2ecf20Sopenharmony_ci#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
298c2ecf20Sopenharmony_ci * Must be set longer than active dwell time.
308c2ecf20Sopenharmony_ci * For the most reliable scan, set > AP beacon interval (typically 100msec). */
318c2ecf20Sopenharmony_ci#define IWL_PASSIVE_DWELL_TIME_24   (20)       /* all times in msec */
328c2ecf20Sopenharmony_ci#define IWL_PASSIVE_DWELL_TIME_52   (10)
338c2ecf20Sopenharmony_ci#define IWL_PASSIVE_DWELL_BASE      (100)
348c2ecf20Sopenharmony_ci#define IWL_CHANNEL_TUNE_TIME       5
358c2ecf20Sopenharmony_ci#define MAX_SCAN_CHANNEL	    50
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* For reset radio, need minimal dwell time only */
388c2ecf20Sopenharmony_ci#define IWL_RADIO_RESET_DWELL_TIME	5
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int iwl_send_scan_abort(struct iwl_priv *priv)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	int ret;
438c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
448c2ecf20Sopenharmony_ci		.id = REPLY_SCAN_ABORT_CMD,
458c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
468c2ecf20Sopenharmony_ci	};
478c2ecf20Sopenharmony_ci	__le32 *status;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/* Exit instantly with error when device is not ready
508c2ecf20Sopenharmony_ci	 * to receive scan abort command or it does not perform
518c2ecf20Sopenharmony_ci	 * hardware scan currently */
528c2ecf20Sopenharmony_ci	if (!test_bit(STATUS_READY, &priv->status) ||
538c2ecf20Sopenharmony_ci	    !test_bit(STATUS_SCAN_HW, &priv->status) ||
548c2ecf20Sopenharmony_ci	    test_bit(STATUS_FW_ERROR, &priv->status))
558c2ecf20Sopenharmony_ci		return -EIO;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	ret = iwl_dvm_send_cmd(priv, &cmd);
588c2ecf20Sopenharmony_ci	if (ret)
598c2ecf20Sopenharmony_ci		return ret;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	status = (void *)cmd.resp_pkt->data;
628c2ecf20Sopenharmony_ci	if (*status != CAN_ABORT_STATUS) {
638c2ecf20Sopenharmony_ci		/* The scan abort will return 1 for success or
648c2ecf20Sopenharmony_ci		 * 2 for "failure".  A failure condition can be
658c2ecf20Sopenharmony_ci		 * due to simply not being in an active scan which
668c2ecf20Sopenharmony_ci		 * can occur if we send the scan abort before we
678c2ecf20Sopenharmony_ci		 * the microcode has notified us that a scan is
688c2ecf20Sopenharmony_ci		 * completed. */
698c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n",
708c2ecf20Sopenharmony_ci			       le32_to_cpu(*status));
718c2ecf20Sopenharmony_ci		ret = -EIO;
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	iwl_free_resp(&cmd);
758c2ecf20Sopenharmony_ci	return ret;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	struct cfg80211_scan_info info = {
818c2ecf20Sopenharmony_ci		.aborted = aborted,
828c2ecf20Sopenharmony_ci	};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* check if scan was requested from mac80211 */
858c2ecf20Sopenharmony_ci	if (priv->scan_request) {
868c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
878c2ecf20Sopenharmony_ci		ieee80211_scan_completed(priv->hw, &info);
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	priv->scan_type = IWL_SCAN_NORMAL;
918c2ecf20Sopenharmony_ci	priv->scan_vif = NULL;
928c2ecf20Sopenharmony_ci	priv->scan_request = NULL;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic void iwl_process_scan_complete(struct iwl_priv *priv)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	bool aborted;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status))
1028c2ecf20Sopenharmony_ci		return;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Completed scan.\n");
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	cancel_delayed_work(&priv->scan_check);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1098c2ecf20Sopenharmony_ci	if (aborted)
1108c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
1138c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
1148c2ecf20Sopenharmony_ci		goto out_settings;
1158c2ecf20Sopenharmony_ci	}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
1188c2ecf20Sopenharmony_ci		int err;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		/* Check if mac80211 requested scan during our internal scan */
1218c2ecf20Sopenharmony_ci		if (priv->scan_request == NULL)
1228c2ecf20Sopenharmony_ci			goto out_complete;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		/* If so request a new scan */
1258c2ecf20Sopenharmony_ci		err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
1268c2ecf20Sopenharmony_ci					priv->scan_request->channels[0]->band);
1278c2ecf20Sopenharmony_ci		if (err) {
1288c2ecf20Sopenharmony_ci			IWL_DEBUG_SCAN(priv,
1298c2ecf20Sopenharmony_ci				"failed to initiate pending scan: %d\n", err);
1308c2ecf20Sopenharmony_ci			aborted = true;
1318c2ecf20Sopenharmony_ci			goto out_complete;
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		return;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciout_complete:
1388c2ecf20Sopenharmony_ci	iwl_complete_scan(priv, aborted);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciout_settings:
1418c2ecf20Sopenharmony_ci	/* Can we still talk to firmware ? */
1428c2ecf20Sopenharmony_ci	if (!iwl_is_ready_rf(priv))
1438c2ecf20Sopenharmony_ci		return;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	iwlagn_post_scan(priv);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_civoid iwl_force_scan_end(struct iwl_priv *priv)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	if (!test_bit(STATUS_SCANNING, &priv->status)) {
1538c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
1548c2ecf20Sopenharmony_ci		return;
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
1588c2ecf20Sopenharmony_ci	clear_bit(STATUS_SCANNING, &priv->status);
1598c2ecf20Sopenharmony_ci	clear_bit(STATUS_SCAN_HW, &priv->status);
1608c2ecf20Sopenharmony_ci	clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1618c2ecf20Sopenharmony_ci	clear_bit(STATUS_SCAN_COMPLETE, &priv->status);
1628c2ecf20Sopenharmony_ci	iwl_complete_scan(priv, true);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic void iwl_do_scan_abort(struct iwl_priv *priv)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	int ret;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (!test_bit(STATUS_SCANNING, &priv->status)) {
1728c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
1738c2ecf20Sopenharmony_ci		return;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1778c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
1788c2ecf20Sopenharmony_ci		return;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	ret = iwl_send_scan_abort(priv);
1828c2ecf20Sopenharmony_ci	if (ret) {
1838c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
1848c2ecf20Sopenharmony_ci		iwl_force_scan_end(priv);
1858c2ecf20Sopenharmony_ci	} else
1868c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/*
1908c2ecf20Sopenharmony_ci * iwl_scan_cancel - Cancel any currently executing HW scan
1918c2ecf20Sopenharmony_ci */
1928c2ecf20Sopenharmony_ciint iwl_scan_cancel(struct iwl_priv *priv)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
1958c2ecf20Sopenharmony_ci	queue_work(priv->workqueue, &priv->abort_scan);
1968c2ecf20Sopenharmony_ci	return 0;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/*
2008c2ecf20Sopenharmony_ci * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2018c2ecf20Sopenharmony_ci * @ms: amount of time to wait (in milliseconds) for scan to abort
2028c2ecf20Sopenharmony_ci */
2038c2ecf20Sopenharmony_civoid iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + msecs_to_jiffies(ms);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	iwl_do_scan_abort(priv);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	while (time_before_eq(jiffies, timeout)) {
2148c2ecf20Sopenharmony_ci		if (!test_bit(STATUS_SCAN_HW, &priv->status))
2158c2ecf20Sopenharmony_ci			goto finished;
2168c2ecf20Sopenharmony_ci		msleep(20);
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	return;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci finished:
2228c2ecf20Sopenharmony_ci	/*
2238c2ecf20Sopenharmony_ci	 * Now STATUS_SCAN_HW is clear. This means that the
2248c2ecf20Sopenharmony_ci	 * device finished, but the background work is going
2258c2ecf20Sopenharmony_ci	 * to execute at best as soon as we release the mutex.
2268c2ecf20Sopenharmony_ci	 * Since we need to be able to issue a new scan right
2278c2ecf20Sopenharmony_ci	 * after this function returns, run the complete here.
2288c2ecf20Sopenharmony_ci	 * The STATUS_SCAN_COMPLETE bit will then be cleared
2298c2ecf20Sopenharmony_ci	 * and prevent the background work from "completing"
2308c2ecf20Sopenharmony_ci	 * a possible new scan.
2318c2ecf20Sopenharmony_ci	 */
2328c2ecf20Sopenharmony_ci	iwl_process_scan_complete(priv);
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci/* Service response to REPLY_SCAN_CMD (0x80) */
2368c2ecf20Sopenharmony_cistatic void iwl_rx_reply_scan(struct iwl_priv *priv,
2378c2ecf20Sopenharmony_ci			      struct iwl_rx_cmd_buffer *rxb)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUG
2408c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2418c2ecf20Sopenharmony_ci	struct iwl_scanreq_notification *notif = (void *)pkt->data;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
2448c2ecf20Sopenharmony_ci#endif
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* Service SCAN_START_NOTIFICATION (0x82) */
2488c2ecf20Sopenharmony_cistatic void iwl_rx_scan_start_notif(struct iwl_priv *priv,
2498c2ecf20Sopenharmony_ci				    struct iwl_rx_cmd_buffer *rxb)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2528c2ecf20Sopenharmony_ci	struct iwl_scanstart_notification *notif = (void *)pkt->data;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2558c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan start: "
2568c2ecf20Sopenharmony_ci		       "%d [802.11%s] "
2578c2ecf20Sopenharmony_ci		       "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2588c2ecf20Sopenharmony_ci		       notif->channel,
2598c2ecf20Sopenharmony_ci		       notif->band ? "bg" : "a",
2608c2ecf20Sopenharmony_ci		       le32_to_cpu(notif->tsf_high),
2618c2ecf20Sopenharmony_ci		       le32_to_cpu(notif->tsf_low),
2628c2ecf20Sopenharmony_ci		       notif->status, notif->beacon_timer);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
2668c2ecf20Sopenharmony_cistatic void iwl_rx_scan_results_notif(struct iwl_priv *priv,
2678c2ecf20Sopenharmony_ci				      struct iwl_rx_cmd_buffer *rxb)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUG
2708c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2718c2ecf20Sopenharmony_ci	struct iwl_scanresults_notification *notif = (void *)pkt->data;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan ch.res: "
2748c2ecf20Sopenharmony_ci		       "%d [802.11%s] "
2758c2ecf20Sopenharmony_ci		       "probe status: %u:%u "
2768c2ecf20Sopenharmony_ci		       "(TSF: 0x%08X:%08X) - %d "
2778c2ecf20Sopenharmony_ci		       "elapsed=%lu usec\n",
2788c2ecf20Sopenharmony_ci		       notif->channel,
2798c2ecf20Sopenharmony_ci		       notif->band ? "bg" : "a",
2808c2ecf20Sopenharmony_ci		       notif->probe_status, notif->num_probe_not_sent,
2818c2ecf20Sopenharmony_ci		       le32_to_cpu(notif->tsf_high),
2828c2ecf20Sopenharmony_ci		       le32_to_cpu(notif->tsf_low),
2838c2ecf20Sopenharmony_ci		       le32_to_cpu(notif->statistics[0]),
2848c2ecf20Sopenharmony_ci		       le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
2858c2ecf20Sopenharmony_ci#endif
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
2898c2ecf20Sopenharmony_cistatic void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
2908c2ecf20Sopenharmony_ci				       struct iwl_rx_cmd_buffer *rxb)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2938c2ecf20Sopenharmony_ci	struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2968c2ecf20Sopenharmony_ci		       scan_notif->scanned_channels,
2978c2ecf20Sopenharmony_ci		       scan_notif->tsf_low,
2988c2ecf20Sopenharmony_ci		       scan_notif->tsf_high, scan_notif->status);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
3018c2ecf20Sopenharmony_ci		       (priv->scan_band == NL80211_BAND_2GHZ) ? "2.4" : "5.2",
3028c2ecf20Sopenharmony_ci		       jiffies_to_msecs(jiffies - priv->scan_start));
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/*
3058c2ecf20Sopenharmony_ci	 * When aborting, we run the scan completed background work inline
3068c2ecf20Sopenharmony_ci	 * and the background work must then do nothing. The SCAN_COMPLETE
3078c2ecf20Sopenharmony_ci	 * bit helps implement that logic and thus needs to be set before
3088c2ecf20Sopenharmony_ci	 * queueing the work. Also, since the scan abort waits for SCAN_HW
3098c2ecf20Sopenharmony_ci	 * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
3108c2ecf20Sopenharmony_ci	 * to avoid a race there.
3118c2ecf20Sopenharmony_ci	 */
3128c2ecf20Sopenharmony_ci	set_bit(STATUS_SCAN_COMPLETE, &priv->status);
3138c2ecf20Sopenharmony_ci	clear_bit(STATUS_SCAN_HW, &priv->status);
3148c2ecf20Sopenharmony_ci	queue_work(priv->workqueue, &priv->scan_completed);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
3178c2ecf20Sopenharmony_ci	    iwl_advanced_bt_coexist(priv) &&
3188c2ecf20Sopenharmony_ci	    priv->bt_status != scan_notif->bt_status) {
3198c2ecf20Sopenharmony_ci		if (scan_notif->bt_status) {
3208c2ecf20Sopenharmony_ci			/* BT on */
3218c2ecf20Sopenharmony_ci			if (!priv->bt_ch_announce)
3228c2ecf20Sopenharmony_ci				priv->bt_traffic_load =
3238c2ecf20Sopenharmony_ci					IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
3248c2ecf20Sopenharmony_ci			/*
3258c2ecf20Sopenharmony_ci			 * otherwise, no traffic load information provided
3268c2ecf20Sopenharmony_ci			 * no changes made
3278c2ecf20Sopenharmony_ci			 */
3288c2ecf20Sopenharmony_ci		} else {
3298c2ecf20Sopenharmony_ci			/* BT off */
3308c2ecf20Sopenharmony_ci			priv->bt_traffic_load =
3318c2ecf20Sopenharmony_ci				IWL_BT_COEX_TRAFFIC_LOAD_NONE;
3328c2ecf20Sopenharmony_ci		}
3338c2ecf20Sopenharmony_ci		priv->bt_status = scan_notif->bt_status;
3348c2ecf20Sopenharmony_ci		queue_work(priv->workqueue,
3358c2ecf20Sopenharmony_ci			   &priv->bt_traffic_change_work);
3368c2ecf20Sopenharmony_ci	}
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_civoid iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	/* scan handlers */
3428c2ecf20Sopenharmony_ci	priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
3438c2ecf20Sopenharmony_ci	priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
3448c2ecf20Sopenharmony_ci	priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
3458c2ecf20Sopenharmony_ci					iwl_rx_scan_results_notif;
3468c2ecf20Sopenharmony_ci	priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
3478c2ecf20Sopenharmony_ci					iwl_rx_scan_complete_notif;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
3518c2ecf20Sopenharmony_ci				     enum nl80211_band band, u8 n_probes)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	if (band == NL80211_BAND_5GHZ)
3548c2ecf20Sopenharmony_ci		return IWL_ACTIVE_DWELL_TIME_52 +
3558c2ecf20Sopenharmony_ci			IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
3568c2ecf20Sopenharmony_ci	else
3578c2ecf20Sopenharmony_ci		return IWL_ACTIVE_DWELL_TIME_24 +
3588c2ecf20Sopenharmony_ci			IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	struct iwl_rxon_context *ctx;
3648c2ecf20Sopenharmony_ci	int limits[NUM_IWL_RXON_CTX] = {};
3658c2ecf20Sopenharmony_ci	int n_active = 0;
3668c2ecf20Sopenharmony_ci	u16 limit;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	/*
3718c2ecf20Sopenharmony_ci	 * If we're associated, we clamp the dwell time 98%
3728c2ecf20Sopenharmony_ci	 * of the beacon interval (minus 2 * channel tune time)
3738c2ecf20Sopenharmony_ci	 * If both contexts are active, we have to restrict to
3748c2ecf20Sopenharmony_ci	 * 1/2 of the minimum of them, because they might be in
3758c2ecf20Sopenharmony_ci	 * lock-step with the time inbetween only half of what
3768c2ecf20Sopenharmony_ci	 * time we'd have in each of them.
3778c2ecf20Sopenharmony_ci	 */
3788c2ecf20Sopenharmony_ci	for_each_context(priv, ctx) {
3798c2ecf20Sopenharmony_ci		switch (ctx->staging.dev_type) {
3808c2ecf20Sopenharmony_ci		case RXON_DEV_TYPE_P2P:
3818c2ecf20Sopenharmony_ci			/* no timing constraints */
3828c2ecf20Sopenharmony_ci			continue;
3838c2ecf20Sopenharmony_ci		case RXON_DEV_TYPE_ESS:
3848c2ecf20Sopenharmony_ci		default:
3858c2ecf20Sopenharmony_ci			/* timing constraints if associated */
3868c2ecf20Sopenharmony_ci			if (!iwl_is_associated_ctx(ctx))
3878c2ecf20Sopenharmony_ci				continue;
3888c2ecf20Sopenharmony_ci			break;
3898c2ecf20Sopenharmony_ci		case RXON_DEV_TYPE_CP:
3908c2ecf20Sopenharmony_ci		case RXON_DEV_TYPE_2STA:
3918c2ecf20Sopenharmony_ci			/*
3928c2ecf20Sopenharmony_ci			 * These seem to always have timers for TBTT
3938c2ecf20Sopenharmony_ci			 * active in uCode even when not associated yet.
3948c2ecf20Sopenharmony_ci			 */
3958c2ecf20Sopenharmony_ci			break;
3968c2ecf20Sopenharmony_ci		}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci		limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE;
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	switch (n_active) {
4028c2ecf20Sopenharmony_ci	case 0:
4038c2ecf20Sopenharmony_ci		return dwell_time;
4048c2ecf20Sopenharmony_ci	case 2:
4058c2ecf20Sopenharmony_ci		limit = (limits[1] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4068c2ecf20Sopenharmony_ci		limit /= 2;
4078c2ecf20Sopenharmony_ci		dwell_time = min(limit, dwell_time);
4088c2ecf20Sopenharmony_ci		/* fall through */
4098c2ecf20Sopenharmony_ci	case 1:
4108c2ecf20Sopenharmony_ci		limit = (limits[0] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4118c2ecf20Sopenharmony_ci		limit /= n_active;
4128c2ecf20Sopenharmony_ci		return min(limit, dwell_time);
4138c2ecf20Sopenharmony_ci	default:
4148c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
4158c2ecf20Sopenharmony_ci		return dwell_time;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
4208c2ecf20Sopenharmony_ci				      enum nl80211_band band)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	u16 passive = (band == NL80211_BAND_2GHZ) ?
4238c2ecf20Sopenharmony_ci	    IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4248c2ecf20Sopenharmony_ci	    IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	return iwl_limit_dwell(priv, passive);
4278c2ecf20Sopenharmony_ci}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci/* Return valid, unused, channel for a passive scan to reset the RF */
4308c2ecf20Sopenharmony_cistatic u8 iwl_get_single_channel_number(struct iwl_priv *priv,
4318c2ecf20Sopenharmony_ci					enum nl80211_band band)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band];
4348c2ecf20Sopenharmony_ci	struct iwl_rxon_context *ctx;
4358c2ecf20Sopenharmony_ci	int i;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	for (i = 0; i < sband->n_channels; i++) {
4388c2ecf20Sopenharmony_ci		bool busy = false;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci		for_each_context(priv, ctx) {
4418c2ecf20Sopenharmony_ci			busy = sband->channels[i].hw_value ==
4428c2ecf20Sopenharmony_ci				le16_to_cpu(ctx->staging.channel);
4438c2ecf20Sopenharmony_ci			if (busy)
4448c2ecf20Sopenharmony_ci				break;
4458c2ecf20Sopenharmony_ci		}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		if (busy)
4488c2ecf20Sopenharmony_ci			continue;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci		if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED))
4518c2ecf20Sopenharmony_ci			return sband->channels[i].hw_value;
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	return 0;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic int iwl_get_channel_for_reset_scan(struct iwl_priv *priv,
4588c2ecf20Sopenharmony_ci					  struct ieee80211_vif *vif,
4598c2ecf20Sopenharmony_ci					  enum nl80211_band band,
4608c2ecf20Sopenharmony_ci					  struct iwl_scan_channel *scan_ch)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	const struct ieee80211_supported_band *sband;
4638c2ecf20Sopenharmony_ci	u16 channel;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	sband = iwl_get_hw_mode(priv, band);
4668c2ecf20Sopenharmony_ci	if (!sband) {
4678c2ecf20Sopenharmony_ci		IWL_ERR(priv, "invalid band\n");
4688c2ecf20Sopenharmony_ci		return 0;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	channel = iwl_get_single_channel_number(priv, band);
4728c2ecf20Sopenharmony_ci	if (channel) {
4738c2ecf20Sopenharmony_ci		scan_ch->channel = cpu_to_le16(channel);
4748c2ecf20Sopenharmony_ci		scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
4758c2ecf20Sopenharmony_ci		scan_ch->active_dwell =
4768c2ecf20Sopenharmony_ci			cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
4778c2ecf20Sopenharmony_ci		scan_ch->passive_dwell =
4788c2ecf20Sopenharmony_ci			cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
4798c2ecf20Sopenharmony_ci		/* Set txpower levels to defaults */
4808c2ecf20Sopenharmony_ci		scan_ch->dsp_atten = 110;
4818c2ecf20Sopenharmony_ci		if (band == NL80211_BAND_5GHZ)
4828c2ecf20Sopenharmony_ci			scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
4838c2ecf20Sopenharmony_ci		else
4848c2ecf20Sopenharmony_ci			scan_ch->tx_gain = ((1 << 5) | (5 << 3));
4858c2ecf20Sopenharmony_ci		return 1;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	IWL_ERR(priv, "no valid channel found\n");
4898c2ecf20Sopenharmony_ci	return 0;
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int iwl_get_channels_for_scan(struct iwl_priv *priv,
4938c2ecf20Sopenharmony_ci				     struct ieee80211_vif *vif,
4948c2ecf20Sopenharmony_ci				     enum nl80211_band band,
4958c2ecf20Sopenharmony_ci				     u8 is_active, u8 n_probes,
4968c2ecf20Sopenharmony_ci				     struct iwl_scan_channel *scan_ch)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct ieee80211_channel *chan;
4998c2ecf20Sopenharmony_ci	const struct ieee80211_supported_band *sband;
5008c2ecf20Sopenharmony_ci	u16 passive_dwell = 0;
5018c2ecf20Sopenharmony_ci	u16 active_dwell = 0;
5028c2ecf20Sopenharmony_ci	int added, i;
5038c2ecf20Sopenharmony_ci	u16 channel;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	sband = iwl_get_hw_mode(priv, band);
5068c2ecf20Sopenharmony_ci	if (!sband)
5078c2ecf20Sopenharmony_ci		return 0;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
5108c2ecf20Sopenharmony_ci	passive_dwell = iwl_get_passive_dwell_time(priv, band);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	if (passive_dwell <= active_dwell)
5138c2ecf20Sopenharmony_ci		passive_dwell = active_dwell + 1;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
5168c2ecf20Sopenharmony_ci		chan = priv->scan_request->channels[i];
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci		if (chan->band != band)
5198c2ecf20Sopenharmony_ci			continue;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci		channel = chan->hw_value;
5228c2ecf20Sopenharmony_ci		scan_ch->channel = cpu_to_le16(channel);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci		if (!is_active || (chan->flags & IEEE80211_CHAN_NO_IR))
5258c2ecf20Sopenharmony_ci			scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
5268c2ecf20Sopenharmony_ci		else
5278c2ecf20Sopenharmony_ci			scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		if (n_probes)
5308c2ecf20Sopenharmony_ci			scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		scan_ch->active_dwell = cpu_to_le16(active_dwell);
5338c2ecf20Sopenharmony_ci		scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci		/* Set txpower levels to defaults */
5368c2ecf20Sopenharmony_ci		scan_ch->dsp_atten = 110;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci		/* NOTE: if we were doing 6Mb OFDM for scans we'd use
5398c2ecf20Sopenharmony_ci		 * power level:
5408c2ecf20Sopenharmony_ci		 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
5418c2ecf20Sopenharmony_ci		 */
5428c2ecf20Sopenharmony_ci		if (band == NL80211_BAND_5GHZ)
5438c2ecf20Sopenharmony_ci			scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
5448c2ecf20Sopenharmony_ci		else
5458c2ecf20Sopenharmony_ci			scan_ch->tx_gain = ((1 << 5) | (5 << 3));
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
5488c2ecf20Sopenharmony_ci			       channel, le32_to_cpu(scan_ch->type),
5498c2ecf20Sopenharmony_ci			       (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
5508c2ecf20Sopenharmony_ci				"ACTIVE" : "PASSIVE",
5518c2ecf20Sopenharmony_ci			       (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
5528c2ecf20Sopenharmony_ci			       active_dwell : passive_dwell);
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		scan_ch++;
5558c2ecf20Sopenharmony_ci		added++;
5568c2ecf20Sopenharmony_ci	}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
5598c2ecf20Sopenharmony_ci	return added;
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci/*
5638c2ecf20Sopenharmony_ci * iwl_fill_probe_req - fill in all required fields and IE for probe request
5648c2ecf20Sopenharmony_ci */
5658c2ecf20Sopenharmony_cistatic u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
5668c2ecf20Sopenharmony_ci			      const u8 *ies, int ie_len, const u8 *ssid,
5678c2ecf20Sopenharmony_ci			      u8 ssid_len, int left)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	int len = 0;
5708c2ecf20Sopenharmony_ci	u8 *pos = NULL;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	/* Make sure there is enough space for the probe request,
5738c2ecf20Sopenharmony_ci	 * two mandatory IEs and the data */
5748c2ecf20Sopenharmony_ci	left -= 24;
5758c2ecf20Sopenharmony_ci	if (left < 0)
5768c2ecf20Sopenharmony_ci		return 0;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
5798c2ecf20Sopenharmony_ci	eth_broadcast_addr(frame->da);
5808c2ecf20Sopenharmony_ci	memcpy(frame->sa, ta, ETH_ALEN);
5818c2ecf20Sopenharmony_ci	eth_broadcast_addr(frame->bssid);
5828c2ecf20Sopenharmony_ci	frame->seq_ctrl = 0;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	len += 24;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	/* ...next IE... */
5878c2ecf20Sopenharmony_ci	pos = &frame->u.probe_req.variable[0];
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	/* fill in our SSID IE */
5908c2ecf20Sopenharmony_ci	left -= ssid_len + 2;
5918c2ecf20Sopenharmony_ci	if (left < 0)
5928c2ecf20Sopenharmony_ci		return 0;
5938c2ecf20Sopenharmony_ci	*pos++ = WLAN_EID_SSID;
5948c2ecf20Sopenharmony_ci	*pos++ = ssid_len;
5958c2ecf20Sopenharmony_ci	if (ssid && ssid_len) {
5968c2ecf20Sopenharmony_ci		memcpy(pos, ssid, ssid_len);
5978c2ecf20Sopenharmony_ci		pos += ssid_len;
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	len += ssid_len + 2;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	if (WARN_ON(left < ie_len))
6038c2ecf20Sopenharmony_ci		return len;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	if (ies && ie_len) {
6068c2ecf20Sopenharmony_ci		memcpy(pos, ies, ie_len);
6078c2ecf20Sopenharmony_ci		len += ie_len;
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	return (u16)len;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
6168c2ecf20Sopenharmony_ci		.id = REPLY_SCAN_CMD,
6178c2ecf20Sopenharmony_ci		.len = { sizeof(struct iwl_scan_cmd), },
6188c2ecf20Sopenharmony_ci	};
6198c2ecf20Sopenharmony_ci	struct iwl_scan_cmd *scan;
6208c2ecf20Sopenharmony_ci	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
6218c2ecf20Sopenharmony_ci	u32 rate_flags = 0;
6228c2ecf20Sopenharmony_ci	u16 cmd_len = 0;
6238c2ecf20Sopenharmony_ci	u16 rx_chain = 0;
6248c2ecf20Sopenharmony_ci	enum nl80211_band band;
6258c2ecf20Sopenharmony_ci	u8 n_probes = 0;
6268c2ecf20Sopenharmony_ci	u8 rx_ant = priv->nvm_data->valid_rx_ant;
6278c2ecf20Sopenharmony_ci	u8 rate;
6288c2ecf20Sopenharmony_ci	bool is_active = false;
6298c2ecf20Sopenharmony_ci	int  chan_mod;
6308c2ecf20Sopenharmony_ci	u8 active_chains;
6318c2ecf20Sopenharmony_ci	u8 scan_tx_antennas = priv->nvm_data->valid_tx_ant;
6328c2ecf20Sopenharmony_ci	int ret;
6338c2ecf20Sopenharmony_ci	int scan_cmd_size = sizeof(struct iwl_scan_cmd) +
6348c2ecf20Sopenharmony_ci			    MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) +
6358c2ecf20Sopenharmony_ci			    priv->fw->ucode_capa.max_probe_length;
6368c2ecf20Sopenharmony_ci	const u8 *ssid = NULL;
6378c2ecf20Sopenharmony_ci	u8 ssid_len = 0;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	if (WARN_ON(priv->scan_type == IWL_SCAN_NORMAL &&
6408c2ecf20Sopenharmony_ci		    (!priv->scan_request ||
6418c2ecf20Sopenharmony_ci		     priv->scan_request->n_channels > MAX_SCAN_CHANNEL)))
6428c2ecf20Sopenharmony_ci		return -EINVAL;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (vif)
6478c2ecf20Sopenharmony_ci		ctx = iwl_rxon_ctx_from_vif(vif);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	if (!priv->scan_cmd) {
6508c2ecf20Sopenharmony_ci		priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
6518c2ecf20Sopenharmony_ci		if (!priv->scan_cmd) {
6528c2ecf20Sopenharmony_ci			IWL_DEBUG_SCAN(priv,
6538c2ecf20Sopenharmony_ci				       "fail to allocate memory for scan\n");
6548c2ecf20Sopenharmony_ci			return -ENOMEM;
6558c2ecf20Sopenharmony_ci		}
6568c2ecf20Sopenharmony_ci	}
6578c2ecf20Sopenharmony_ci	scan = priv->scan_cmd;
6588c2ecf20Sopenharmony_ci	memset(scan, 0, scan_cmd_size);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6618c2ecf20Sopenharmony_ci	scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	if (iwl_is_any_associated(priv)) {
6648c2ecf20Sopenharmony_ci		u16 interval = 0;
6658c2ecf20Sopenharmony_ci		u32 extra;
6668c2ecf20Sopenharmony_ci		u32 suspend_time = 100;
6678c2ecf20Sopenharmony_ci		u32 scan_suspend_time = 100;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci		IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
6708c2ecf20Sopenharmony_ci		switch (priv->scan_type) {
6718c2ecf20Sopenharmony_ci		case IWL_SCAN_RADIO_RESET:
6728c2ecf20Sopenharmony_ci			interval = 0;
6738c2ecf20Sopenharmony_ci			break;
6748c2ecf20Sopenharmony_ci		case IWL_SCAN_NORMAL:
6758c2ecf20Sopenharmony_ci			interval = vif->bss_conf.beacon_int;
6768c2ecf20Sopenharmony_ci			break;
6778c2ecf20Sopenharmony_ci		}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		scan->suspend_time = 0;
6808c2ecf20Sopenharmony_ci		scan->max_out_time = cpu_to_le32(200 * 1024);
6818c2ecf20Sopenharmony_ci		if (!interval)
6828c2ecf20Sopenharmony_ci			interval = suspend_time;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci		extra = (suspend_time / interval) << 22;
6858c2ecf20Sopenharmony_ci		scan_suspend_time = (extra |
6868c2ecf20Sopenharmony_ci		    ((suspend_time % interval) * 1024));
6878c2ecf20Sopenharmony_ci		scan->suspend_time = cpu_to_le32(scan_suspend_time);
6888c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
6898c2ecf20Sopenharmony_ci			       scan_suspend_time, interval);
6908c2ecf20Sopenharmony_ci	}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	switch (priv->scan_type) {
6938c2ecf20Sopenharmony_ci	case IWL_SCAN_RADIO_RESET:
6948c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
6958c2ecf20Sopenharmony_ci		/*
6968c2ecf20Sopenharmony_ci		 * Override quiet time as firmware checks that active
6978c2ecf20Sopenharmony_ci		 * dwell is >= quiet; since we use passive scan it'll
6988c2ecf20Sopenharmony_ci		 * not actually be used.
6998c2ecf20Sopenharmony_ci		 */
7008c2ecf20Sopenharmony_ci		scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
7018c2ecf20Sopenharmony_ci		break;
7028c2ecf20Sopenharmony_ci	case IWL_SCAN_NORMAL:
7038c2ecf20Sopenharmony_ci		if (priv->scan_request->n_ssids) {
7048c2ecf20Sopenharmony_ci			int i, p = 0;
7058c2ecf20Sopenharmony_ci			IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
7068c2ecf20Sopenharmony_ci			/*
7078c2ecf20Sopenharmony_ci			 * The highest priority SSID is inserted to the
7088c2ecf20Sopenharmony_ci			 * probe request template.
7098c2ecf20Sopenharmony_ci			 */
7108c2ecf20Sopenharmony_ci			ssid_len = priv->scan_request->ssids[0].ssid_len;
7118c2ecf20Sopenharmony_ci			ssid = priv->scan_request->ssids[0].ssid;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci			/*
7148c2ecf20Sopenharmony_ci			 * Invert the order of ssids, the firmware will invert
7158c2ecf20Sopenharmony_ci			 * it back.
7168c2ecf20Sopenharmony_ci			 */
7178c2ecf20Sopenharmony_ci			for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) {
7188c2ecf20Sopenharmony_ci				scan->direct_scan[p].id = WLAN_EID_SSID;
7198c2ecf20Sopenharmony_ci				scan->direct_scan[p].len =
7208c2ecf20Sopenharmony_ci					priv->scan_request->ssids[i].ssid_len;
7218c2ecf20Sopenharmony_ci				memcpy(scan->direct_scan[p].ssid,
7228c2ecf20Sopenharmony_ci				       priv->scan_request->ssids[i].ssid,
7238c2ecf20Sopenharmony_ci				       priv->scan_request->ssids[i].ssid_len);
7248c2ecf20Sopenharmony_ci				n_probes++;
7258c2ecf20Sopenharmony_ci				p++;
7268c2ecf20Sopenharmony_ci			}
7278c2ecf20Sopenharmony_ci			is_active = true;
7288c2ecf20Sopenharmony_ci		} else
7298c2ecf20Sopenharmony_ci			IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
7308c2ecf20Sopenharmony_ci		break;
7318c2ecf20Sopenharmony_ci	}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
7348c2ecf20Sopenharmony_ci	scan->tx_cmd.sta_id = ctx->bcast_sta_id;
7358c2ecf20Sopenharmony_ci	scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	switch (priv->scan_band) {
7388c2ecf20Sopenharmony_ci	case NL80211_BAND_2GHZ:
7398c2ecf20Sopenharmony_ci		scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
7408c2ecf20Sopenharmony_ci		chan_mod = le32_to_cpu(
7418c2ecf20Sopenharmony_ci			priv->contexts[IWL_RXON_CTX_BSS].active.flags &
7428c2ecf20Sopenharmony_ci						RXON_FLG_CHANNEL_MODE_MSK)
7438c2ecf20Sopenharmony_ci				       >> RXON_FLG_CHANNEL_MODE_POS;
7448c2ecf20Sopenharmony_ci		if ((priv->scan_request && priv->scan_request->no_cck) ||
7458c2ecf20Sopenharmony_ci		    chan_mod == CHANNEL_MODE_PURE_40) {
7468c2ecf20Sopenharmony_ci			rate = IWL_RATE_6M_PLCP;
7478c2ecf20Sopenharmony_ci		} else {
7488c2ecf20Sopenharmony_ci			rate = IWL_RATE_1M_PLCP;
7498c2ecf20Sopenharmony_ci			rate_flags = RATE_MCS_CCK_MSK;
7508c2ecf20Sopenharmony_ci		}
7518c2ecf20Sopenharmony_ci		/*
7528c2ecf20Sopenharmony_ci		 * Internal scans are passive, so we can indiscriminately set
7538c2ecf20Sopenharmony_ci		 * the BT ignore flag on 2.4 GHz since it applies to TX only.
7548c2ecf20Sopenharmony_ci		 */
7558c2ecf20Sopenharmony_ci		if (priv->lib->bt_params &&
7568c2ecf20Sopenharmony_ci		    priv->lib->bt_params->advanced_bt_coexist)
7578c2ecf20Sopenharmony_ci			scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
7588c2ecf20Sopenharmony_ci		break;
7598c2ecf20Sopenharmony_ci	case NL80211_BAND_5GHZ:
7608c2ecf20Sopenharmony_ci		rate = IWL_RATE_6M_PLCP;
7618c2ecf20Sopenharmony_ci		break;
7628c2ecf20Sopenharmony_ci	default:
7638c2ecf20Sopenharmony_ci		IWL_WARN(priv, "Invalid scan band\n");
7648c2ecf20Sopenharmony_ci		return -EIO;
7658c2ecf20Sopenharmony_ci	}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	/*
7688c2ecf20Sopenharmony_ci	 * If active scanning is requested but a certain channel is
7698c2ecf20Sopenharmony_ci	 * marked passive, we can do active scanning if we detect
7708c2ecf20Sopenharmony_ci	 * transmissions.
7718c2ecf20Sopenharmony_ci	 *
7728c2ecf20Sopenharmony_ci	 * There is an issue with some firmware versions that triggers
7738c2ecf20Sopenharmony_ci	 * a sysassert on a "good CRC threshold" of zero (== disabled),
7748c2ecf20Sopenharmony_ci	 * on a radar channel even though this means that we should NOT
7758c2ecf20Sopenharmony_ci	 * send probes.
7768c2ecf20Sopenharmony_ci	 *
7778c2ecf20Sopenharmony_ci	 * The "good CRC threshold" is the number of frames that we
7788c2ecf20Sopenharmony_ci	 * need to receive during our dwell time on a channel before
7798c2ecf20Sopenharmony_ci	 * sending out probes -- setting this to a huge value will
7808c2ecf20Sopenharmony_ci	 * mean we never reach it, but at the same time work around
7818c2ecf20Sopenharmony_ci	 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
7828c2ecf20Sopenharmony_ci	 * here instead of IWL_GOOD_CRC_TH_DISABLED.
7838c2ecf20Sopenharmony_ci	 *
7848c2ecf20Sopenharmony_ci	 * This was fixed in later versions along with some other
7858c2ecf20Sopenharmony_ci	 * scan changes, and the threshold behaves as a flag in those
7868c2ecf20Sopenharmony_ci	 * versions.
7878c2ecf20Sopenharmony_ci	 */
7888c2ecf20Sopenharmony_ci	if (priv->new_scan_threshold_behaviour)
7898c2ecf20Sopenharmony_ci		scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
7908c2ecf20Sopenharmony_ci						IWL_GOOD_CRC_TH_DISABLED;
7918c2ecf20Sopenharmony_ci	else
7928c2ecf20Sopenharmony_ci		scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
7938c2ecf20Sopenharmony_ci						IWL_GOOD_CRC_TH_NEVER;
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	band = priv->scan_band;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	if (band == NL80211_BAND_2GHZ &&
7988c2ecf20Sopenharmony_ci	    priv->lib->bt_params &&
7998c2ecf20Sopenharmony_ci	    priv->lib->bt_params->advanced_bt_coexist) {
8008c2ecf20Sopenharmony_ci		/* transmit 2.4 GHz probes only on first antenna */
8018c2ecf20Sopenharmony_ci		scan_tx_antennas = first_antenna(scan_tx_antennas);
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
8058c2ecf20Sopenharmony_ci						    priv->scan_tx_ant[band],
8068c2ecf20Sopenharmony_ci						    scan_tx_antennas);
8078c2ecf20Sopenharmony_ci	rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
8088c2ecf20Sopenharmony_ci	scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/*
8118c2ecf20Sopenharmony_ci	 * In power save mode while associated use one chain,
8128c2ecf20Sopenharmony_ci	 * otherwise use all chains
8138c2ecf20Sopenharmony_ci	 */
8148c2ecf20Sopenharmony_ci	if (test_bit(STATUS_POWER_PMI, &priv->status) &&
8158c2ecf20Sopenharmony_ci	    !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
8168c2ecf20Sopenharmony_ci		/* rx_ant has been set to all valid chains previously */
8178c2ecf20Sopenharmony_ci		active_chains = rx_ant &
8188c2ecf20Sopenharmony_ci				((u8)(priv->chain_noise_data.active_chains));
8198c2ecf20Sopenharmony_ci		if (!active_chains)
8208c2ecf20Sopenharmony_ci			active_chains = rx_ant;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
8238c2ecf20Sopenharmony_ci				priv->chain_noise_data.active_chains);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci		rx_ant = first_antenna(active_chains);
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci	if (priv->lib->bt_params &&
8288c2ecf20Sopenharmony_ci	    priv->lib->bt_params->advanced_bt_coexist &&
8298c2ecf20Sopenharmony_ci	    priv->bt_full_concurrent) {
8308c2ecf20Sopenharmony_ci		/* operated as 1x1 in full concurrency mode */
8318c2ecf20Sopenharmony_ci		rx_ant = first_antenna(rx_ant);
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	/* MIMO is not used here, but value is required */
8358c2ecf20Sopenharmony_ci	rx_chain |=
8368c2ecf20Sopenharmony_ci		priv->nvm_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
8378c2ecf20Sopenharmony_ci	rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
8388c2ecf20Sopenharmony_ci	rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
8398c2ecf20Sopenharmony_ci	rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
8408c2ecf20Sopenharmony_ci	scan->rx_chain = cpu_to_le16(rx_chain);
8418c2ecf20Sopenharmony_ci	switch (priv->scan_type) {
8428c2ecf20Sopenharmony_ci	case IWL_SCAN_NORMAL:
8438c2ecf20Sopenharmony_ci		cmd_len = iwl_fill_probe_req(
8448c2ecf20Sopenharmony_ci					(struct ieee80211_mgmt *)scan->data,
8458c2ecf20Sopenharmony_ci					vif->addr,
8468c2ecf20Sopenharmony_ci					priv->scan_request->ie,
8478c2ecf20Sopenharmony_ci					priv->scan_request->ie_len,
8488c2ecf20Sopenharmony_ci					ssid, ssid_len,
8498c2ecf20Sopenharmony_ci					scan_cmd_size - sizeof(*scan));
8508c2ecf20Sopenharmony_ci		break;
8518c2ecf20Sopenharmony_ci	case IWL_SCAN_RADIO_RESET:
8528c2ecf20Sopenharmony_ci		/* use bcast addr, will not be transmitted but must be valid */
8538c2ecf20Sopenharmony_ci		cmd_len = iwl_fill_probe_req(
8548c2ecf20Sopenharmony_ci					(struct ieee80211_mgmt *)scan->data,
8558c2ecf20Sopenharmony_ci					iwl_bcast_addr, NULL, 0,
8568c2ecf20Sopenharmony_ci					NULL, 0,
8578c2ecf20Sopenharmony_ci					scan_cmd_size - sizeof(*scan));
8588c2ecf20Sopenharmony_ci		break;
8598c2ecf20Sopenharmony_ci	default:
8608c2ecf20Sopenharmony_ci		BUG();
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci	scan->tx_cmd.len = cpu_to_le16(cmd_len);
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
8658c2ecf20Sopenharmony_ci			       RXON_FILTER_BCON_AWARE_MSK);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	switch (priv->scan_type) {
8688c2ecf20Sopenharmony_ci	case IWL_SCAN_RADIO_RESET:
8698c2ecf20Sopenharmony_ci		scan->channel_count =
8708c2ecf20Sopenharmony_ci			iwl_get_channel_for_reset_scan(priv, vif, band,
8718c2ecf20Sopenharmony_ci				(void *)&scan->data[cmd_len]);
8728c2ecf20Sopenharmony_ci		break;
8738c2ecf20Sopenharmony_ci	case IWL_SCAN_NORMAL:
8748c2ecf20Sopenharmony_ci		scan->channel_count =
8758c2ecf20Sopenharmony_ci			iwl_get_channels_for_scan(priv, vif, band,
8768c2ecf20Sopenharmony_ci				is_active, n_probes,
8778c2ecf20Sopenharmony_ci				(void *)&scan->data[cmd_len]);
8788c2ecf20Sopenharmony_ci		break;
8798c2ecf20Sopenharmony_ci	}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	if (scan->channel_count == 0) {
8828c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
8838c2ecf20Sopenharmony_ci		return -EIO;
8848c2ecf20Sopenharmony_ci	}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
8878c2ecf20Sopenharmony_ci	    scan->channel_count * sizeof(struct iwl_scan_channel);
8888c2ecf20Sopenharmony_ci	cmd.data[0] = scan;
8898c2ecf20Sopenharmony_ci	cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
8908c2ecf20Sopenharmony_ci	scan->len = cpu_to_le16(cmd.len[0]);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	/* set scan bit here for PAN params */
8938c2ecf20Sopenharmony_ci	set_bit(STATUS_SCAN_HW, &priv->status);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	ret = iwlagn_set_pan_params(priv);
8968c2ecf20Sopenharmony_ci	if (ret) {
8978c2ecf20Sopenharmony_ci		clear_bit(STATUS_SCAN_HW, &priv->status);
8988c2ecf20Sopenharmony_ci		return ret;
8998c2ecf20Sopenharmony_ci	}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	ret = iwl_dvm_send_cmd(priv, &cmd);
9028c2ecf20Sopenharmony_ci	if (ret) {
9038c2ecf20Sopenharmony_ci		clear_bit(STATUS_SCAN_HW, &priv->status);
9048c2ecf20Sopenharmony_ci		iwlagn_set_pan_params(priv);
9058c2ecf20Sopenharmony_ci	}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	return ret;
9088c2ecf20Sopenharmony_ci}
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_civoid iwl_init_scan_params(struct iwl_priv *priv)
9118c2ecf20Sopenharmony_ci{
9128c2ecf20Sopenharmony_ci	u8 ant_idx = fls(priv->nvm_data->valid_tx_ant) - 1;
9138c2ecf20Sopenharmony_ci	if (!priv->scan_tx_ant[NL80211_BAND_5GHZ])
9148c2ecf20Sopenharmony_ci		priv->scan_tx_ant[NL80211_BAND_5GHZ] = ant_idx;
9158c2ecf20Sopenharmony_ci	if (!priv->scan_tx_ant[NL80211_BAND_2GHZ])
9168c2ecf20Sopenharmony_ci		priv->scan_tx_ant[NL80211_BAND_2GHZ] = ant_idx;
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ciint __must_check iwl_scan_initiate(struct iwl_priv *priv,
9208c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif,
9218c2ecf20Sopenharmony_ci				   enum iwl_scan_type scan_type,
9228c2ecf20Sopenharmony_ci				   enum nl80211_band band)
9238c2ecf20Sopenharmony_ci{
9248c2ecf20Sopenharmony_ci	int ret;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	lockdep_assert_held(&priv->mutex);
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	cancel_delayed_work(&priv->scan_check);
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	if (!iwl_is_ready_rf(priv)) {
9318c2ecf20Sopenharmony_ci		IWL_WARN(priv, "Request scan called when driver not ready.\n");
9328c2ecf20Sopenharmony_ci		return -EIO;
9338c2ecf20Sopenharmony_ci	}
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	if (test_bit(STATUS_SCAN_HW, &priv->status)) {
9368c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv,
9378c2ecf20Sopenharmony_ci			"Multiple concurrent scan requests in parallel.\n");
9388c2ecf20Sopenharmony_ci		return -EBUSY;
9398c2ecf20Sopenharmony_ci	}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
9428c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
9438c2ecf20Sopenharmony_ci		return -EBUSY;
9448c2ecf20Sopenharmony_ci	}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
9478c2ecf20Sopenharmony_ci			scan_type == IWL_SCAN_NORMAL ? "" :
9488c2ecf20Sopenharmony_ci			"internal short ");
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	set_bit(STATUS_SCANNING, &priv->status);
9518c2ecf20Sopenharmony_ci	priv->scan_type = scan_type;
9528c2ecf20Sopenharmony_ci	priv->scan_start = jiffies;
9538c2ecf20Sopenharmony_ci	priv->scan_band = band;
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	ret = iwlagn_request_scan(priv, vif);
9568c2ecf20Sopenharmony_ci	if (ret) {
9578c2ecf20Sopenharmony_ci		clear_bit(STATUS_SCANNING, &priv->status);
9588c2ecf20Sopenharmony_ci		priv->scan_type = IWL_SCAN_NORMAL;
9598c2ecf20Sopenharmony_ci		return ret;
9608c2ecf20Sopenharmony_ci	}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	queue_delayed_work(priv->workqueue, &priv->scan_check,
9638c2ecf20Sopenharmony_ci			   IWL_SCAN_CHECK_WATCHDOG);
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	return 0;
9668c2ecf20Sopenharmony_ci}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci/*
9708c2ecf20Sopenharmony_ci * internal short scan, this function should only been called while associated.
9718c2ecf20Sopenharmony_ci * It will reset and tune the radio to prevent possible RF related problem
9728c2ecf20Sopenharmony_ci */
9738c2ecf20Sopenharmony_civoid iwl_internal_short_hw_scan(struct iwl_priv *priv)
9748c2ecf20Sopenharmony_ci{
9758c2ecf20Sopenharmony_ci	queue_work(priv->workqueue, &priv->start_internal_scan);
9768c2ecf20Sopenharmony_ci}
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_cistatic void iwl_bg_start_internal_scan(struct work_struct *work)
9798c2ecf20Sopenharmony_ci{
9808c2ecf20Sopenharmony_ci	struct iwl_priv *priv =
9818c2ecf20Sopenharmony_ci		container_of(work, struct iwl_priv, start_internal_scan);
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Start internal scan\n");
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	mutex_lock(&priv->mutex);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
9888c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
9898c2ecf20Sopenharmony_ci		goto unlock;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	if (test_bit(STATUS_SCANNING, &priv->status)) {
9938c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
9948c2ecf20Sopenharmony_ci		goto unlock;
9958c2ecf20Sopenharmony_ci	}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
9988c2ecf20Sopenharmony_ci		IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
9998c2ecf20Sopenharmony_ci unlock:
10008c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mutex);
10018c2ecf20Sopenharmony_ci}
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cistatic void iwl_bg_scan_check(struct work_struct *data)
10048c2ecf20Sopenharmony_ci{
10058c2ecf20Sopenharmony_ci	struct iwl_priv *priv =
10068c2ecf20Sopenharmony_ci	    container_of(data, struct iwl_priv, scan_check.work);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Scan check work\n");
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	/* Since we are here firmware does not finish scan and
10118c2ecf20Sopenharmony_ci	 * most likely is in bad shape, so we don't bother to
10128c2ecf20Sopenharmony_ci	 * send abort command, just force scan complete to mac80211 */
10138c2ecf20Sopenharmony_ci	mutex_lock(&priv->mutex);
10148c2ecf20Sopenharmony_ci	iwl_force_scan_end(priv);
10158c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mutex);
10168c2ecf20Sopenharmony_ci}
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_cistatic void iwl_bg_abort_scan(struct work_struct *work)
10198c2ecf20Sopenharmony_ci{
10208c2ecf20Sopenharmony_ci	struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	IWL_DEBUG_SCAN(priv, "Abort scan work\n");
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	/* We keep scan_check work queued in case when firmware will not
10258c2ecf20Sopenharmony_ci	 * report back scan completed notification */
10268c2ecf20Sopenharmony_ci	mutex_lock(&priv->mutex);
10278c2ecf20Sopenharmony_ci	iwl_scan_cancel_timeout(priv, 200);
10288c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mutex);
10298c2ecf20Sopenharmony_ci}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_cistatic void iwl_bg_scan_completed(struct work_struct *work)
10328c2ecf20Sopenharmony_ci{
10338c2ecf20Sopenharmony_ci	struct iwl_priv *priv =
10348c2ecf20Sopenharmony_ci		container_of(work, struct iwl_priv, scan_completed);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	mutex_lock(&priv->mutex);
10378c2ecf20Sopenharmony_ci	iwl_process_scan_complete(priv);
10388c2ecf20Sopenharmony_ci	mutex_unlock(&priv->mutex);
10398c2ecf20Sopenharmony_ci}
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_civoid iwl_setup_scan_deferred_work(struct iwl_priv *priv)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
10448c2ecf20Sopenharmony_ci	INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
10458c2ecf20Sopenharmony_ci	INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
10468c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
10478c2ecf20Sopenharmony_ci}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_civoid iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	cancel_work_sync(&priv->start_internal_scan);
10528c2ecf20Sopenharmony_ci	cancel_work_sync(&priv->abort_scan);
10538c2ecf20Sopenharmony_ci	cancel_work_sync(&priv->scan_completed);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	if (cancel_delayed_work_sync(&priv->scan_check)) {
10568c2ecf20Sopenharmony_ci		mutex_lock(&priv->mutex);
10578c2ecf20Sopenharmony_ci		iwl_force_scan_end(priv);
10588c2ecf20Sopenharmony_ci		mutex_unlock(&priv->mutex);
10598c2ecf20Sopenharmony_ci	}
10608c2ecf20Sopenharmony_ci}
1061