blob: a8437a6bc18e41d83d36e7208c166e28d56f0da4 [file] [log] [blame]
Tomas Winkler2a421b92008-06-12 09:47:10 +08001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Wey-Yi Guyfb4961d2012-01-06 13:16:33 -08005 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
Tomas Winkler2a421b92008-06-12 09:47:10 +08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Tomas Winkler2a421b92008-06-12 09:47:10 +080026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040029#include <linux/types.h>
Tomas Winkler2a421b92008-06-12 09:47:10 +080030#include <linux/etherdevice.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040031#include <net/mac80211.h>
Tomas Winkler2a421b92008-06-12 09:47:10 +080032
33#include "iwl-eeprom.h"
34#include "iwl-dev.h"
Tomas Winkler2a421b92008-06-12 09:47:10 +080035#include "iwl-io.h"
Wey-Yi Guye80d70e2011-06-03 07:54:15 -070036#include "iwl-agn.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070037#include "iwl-trans.h"
Tomas Winkler2a421b92008-06-12 09:47:10 +080038
39/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
40 * sending probe req. This should be set long enough to hear probe responses
41 * from more than one AP. */
Tomas Winklerfe905f12008-07-11 11:53:39 +080042#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
43#define IWL_ACTIVE_DWELL_TIME_52 (20)
44
45#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
46#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Tomas Winkler2a421b92008-06-12 09:47:10 +080047
Tomas Winkler2a421b92008-06-12 09:47:10 +080048/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
49 * Must be set longer than active dwell time.
50 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
51#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
52#define IWL_PASSIVE_DWELL_TIME_52 (10)
53#define IWL_PASSIVE_DWELL_BASE (100)
54#define IWL_CHANNEL_TUNE_TIME 5
55
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020056static int iwl_send_scan_abort(struct iwl_priv *priv)
57{
58 int ret;
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020059 struct iwl_host_cmd cmd = {
60 .id = REPLY_SCAN_ABORT_CMD,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070061 .flags = CMD_SYNC | CMD_WANT_SKB,
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020062 };
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080063 __le32 *status;
Tomas Winklerfe905f12008-07-11 11:53:39 +080064
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020065 /* Exit instantly with error when device is not ready
66 * to receive scan abort command or it does not perform
67 * hardware scan currently */
Don Fry83626402012-03-07 09:52:37 -080068 if (!test_bit(STATUS_READY, &priv->status) ||
69 !test_bit(STATUS_GEO_CONFIGURED, &priv->status) ||
70 !test_bit(STATUS_SCAN_HW, &priv->status) ||
Don Fry17acd0b2012-03-15 13:27:05 -070071 test_bit(STATUS_FW_ERROR, &priv->status))
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020072 return -EIO;
73
Johannes Berge10a0532012-03-06 13:30:39 -080074 ret = iwl_dvm_send_cmd(priv, &cmd);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020075 if (ret)
76 return ret;
77
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080078 status = (void *)cmd.resp_pkt->data;
79 if (*status != CAN_ABORT_STATUS) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020080 /* The scan abort will return 1 for success or
81 * 2 for "failure". A failure condition can be
82 * due to simply not being in an active scan which
83 * can occur if we send the scan abort before we
84 * the microcode has notified us that a scan is
85 * completed. */
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080086 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n",
87 le32_to_cpu(*status));
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020088 ret = -EIO;
89 }
90
Johannes Berg65b94a42012-03-05 11:24:38 -080091 iwl_free_resp(&cmd);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020092 return ret;
93}
94
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +020095static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
96{
97 /* check if scan was requested from mac80211 */
98 if (priv->scan_request) {
99 IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
100 ieee80211_scan_completed(priv->hw, aborted);
101 }
102
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700103 if (priv->scan_type == IWL_SCAN_ROC) {
104 ieee80211_remain_on_channel_expired(priv->hw);
105 priv->hw_roc_channel = NULL;
106 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
107 }
108
Johannes Berg266af4c72011-03-10 20:13:26 -0800109 priv->scan_type = IWL_SCAN_NORMAL;
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +0200110 priv->scan_vif = NULL;
111 priv->scan_request = NULL;
112}
113
Johannes Berga2fa2462011-09-22 15:14:59 -0700114static void iwl_process_scan_complete(struct iwl_priv *priv)
115{
116 bool aborted;
117
Johannes Bergb1eea292012-03-06 13:30:42 -0800118 lockdep_assert_held(&priv->mutex);
Johannes Berg84b1bec2011-09-22 15:15:00 -0700119
Don Fry83626402012-03-07 09:52:37 -0800120 if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status))
Johannes Berg84b1bec2011-09-22 15:15:00 -0700121 return;
122
Johannes Berga2fa2462011-09-22 15:14:59 -0700123 IWL_DEBUG_SCAN(priv, "Completed scan.\n");
124
125 cancel_delayed_work(&priv->scan_check);
126
Don Fry83626402012-03-07 09:52:37 -0800127 aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Johannes Berga2fa2462011-09-22 15:14:59 -0700128 if (aborted)
129 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
130
Don Fry83626402012-03-07 09:52:37 -0800131 if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
Johannes Berga2fa2462011-09-22 15:14:59 -0700132 IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
133 goto out_settings;
134 }
135
136 if (priv->scan_type == IWL_SCAN_ROC) {
137 ieee80211_remain_on_channel_expired(priv->hw);
138 priv->hw_roc_channel = NULL;
139 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
140 }
141
142 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
143 int err;
144
145 /* Check if mac80211 requested scan during our internal scan */
146 if (priv->scan_request == NULL)
147 goto out_complete;
148
149 /* If so request a new scan */
150 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
151 priv->scan_request->channels[0]->band);
152 if (err) {
153 IWL_DEBUG_SCAN(priv,
154 "failed to initiate pending scan: %d\n", err);
155 aborted = true;
156 goto out_complete;
157 }
158
159 return;
160 }
161
162out_complete:
163 iwl_complete_scan(priv, aborted);
164
165out_settings:
166 /* Can we still talk to firmware ? */
Don Fry83626402012-03-07 09:52:37 -0800167 if (!iwl_is_ready_rf(priv))
Johannes Berga2fa2462011-09-22 15:14:59 -0700168 return;
169
170 iwlagn_post_scan(priv);
171}
172
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200173void iwl_force_scan_end(struct iwl_priv *priv)
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200174{
Johannes Bergb1eea292012-03-06 13:30:42 -0800175 lockdep_assert_held(&priv->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200176
Don Fry83626402012-03-07 09:52:37 -0800177 if (!test_bit(STATUS_SCANNING, &priv->status)) {
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200178 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
179 return;
180 }
181
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200182 IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
Don Fry83626402012-03-07 09:52:37 -0800183 clear_bit(STATUS_SCANNING, &priv->status);
184 clear_bit(STATUS_SCAN_HW, &priv->status);
185 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
186 clear_bit(STATUS_SCAN_COMPLETE, &priv->status);
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200187 iwl_complete_scan(priv, true);
188}
189
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200190static void iwl_do_scan_abort(struct iwl_priv *priv)
191{
192 int ret;
193
Johannes Bergb1eea292012-03-06 13:30:42 -0800194 lockdep_assert_held(&priv->mutex);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200195
Don Fry83626402012-03-07 09:52:37 -0800196 if (!test_bit(STATUS_SCANNING, &priv->status)) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200197 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
198 return;
199 }
200
Don Fry83626402012-03-07 09:52:37 -0800201 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200202 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
203 return;
204 }
205
206 ret = iwl_send_scan_abort(priv);
207 if (ret) {
208 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200209 iwl_force_scan_end(priv);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200210 } else
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300211 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200212}
Tomas Winklerfe905f12008-07-11 11:53:39 +0800213
Tomas Winkler2a421b92008-06-12 09:47:10 +0800214/**
215 * iwl_scan_cancel - Cancel any currently executing HW scan
Tomas Winkler2a421b92008-06-12 09:47:10 +0800216 */
217int iwl_scan_cancel(struct iwl_priv *priv)
218{
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200219 IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
Johannes Berg1ee158d2012-02-17 10:07:44 -0800220 queue_work(priv->workqueue, &priv->abort_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800221 return 0;
222}
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200223
Tomas Winkler2a421b92008-06-12 09:47:10 +0800224/**
225 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
226 * @ms: amount of time to wait (in milliseconds) for scan to abort
227 *
Tomas Winkler2a421b92008-06-12 09:47:10 +0800228 */
Johannes Berg98efb4a2011-09-22 15:14:57 -0700229void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800230{
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200231 unsigned long timeout = jiffies + msecs_to_jiffies(ms);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800232
Johannes Bergb1eea292012-03-06 13:30:42 -0800233 lockdep_assert_held(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800234
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200235 IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
236
237 iwl_do_scan_abort(priv);
238
239 while (time_before_eq(jiffies, timeout)) {
Don Fry83626402012-03-07 09:52:37 -0800240 if (!test_bit(STATUS_SCAN_HW, &priv->status))
Johannes Berg84b1bec2011-09-22 15:15:00 -0700241 goto finished;
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200242 msleep(20);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800243 }
Johannes Berg84b1bec2011-09-22 15:15:00 -0700244
245 return;
246
247 finished:
248 /*
249 * Now STATUS_SCAN_HW is clear. This means that the
250 * device finished, but the background work is going
251 * to execute at best as soon as we release the mutex.
252 * Since we need to be able to issue a new scan right
253 * after this function returns, run the complete here.
254 * The STATUS_SCAN_COMPLETE bit will then be cleared
255 * and prevent the background work from "completing"
256 * a possible new scan.
257 */
258 iwl_process_scan_complete(priv);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800259}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800260
Tomas Winkler2a421b92008-06-12 09:47:10 +0800261/* Service response to REPLY_SCAN_CMD (0x80) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700262static int iwl_rx_reply_scan(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800263 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700264 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800265{
266#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800267 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800268 struct iwl_scanreq_notification *notif = (void *)pkt->data;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800269
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200270 IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800271#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700272 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800273}
274
275/* Service SCAN_START_NOTIFICATION (0x82) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700276static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800277 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700278 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800279{
Zhu Yi2f301222009-10-09 17:19:45 +0800280 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800281 struct iwl_scanstart_notification *notif = (void *)pkt->data;
282
Tomas Winkler2a421b92008-06-12 09:47:10 +0800283 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
Tomas Winklere1623442009-01-27 14:27:56 -0800284 IWL_DEBUG_SCAN(priv, "Scan start: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800285 "%d [802.11%s] "
286 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
287 notif->channel,
288 notif->band ? "bg" : "a",
Tomas Winklerfe905f12008-07-11 11:53:39 +0800289 le32_to_cpu(notif->tsf_high),
290 le32_to_cpu(notif->tsf_low),
291 notif->status, notif->beacon_timer);
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700292
Johannes Berg390808d2011-09-20 15:37:22 -0700293 if (priv->scan_type == IWL_SCAN_ROC &&
294 !priv->hw_roc_start_notified) {
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700295 ieee80211_ready_on_channel(priv->hw);
Johannes Berg390808d2011-09-20 15:37:22 -0700296 priv->hw_roc_start_notified = true;
297 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700298
299 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800300}
301
302/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700303static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800304 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700305 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800306{
307#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800308 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800309 struct iwl_scanresults_notification *notif = (void *)pkt->data;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800310
Tomas Winklere1623442009-01-27 14:27:56 -0800311 IWL_DEBUG_SCAN(priv, "Scan ch.res: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800312 "%d [802.11%s] "
Wey-Yi Guy1895b362011-09-22 15:14:51 -0700313 "probe status: %u:%u "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800314 "(TSF: 0x%08X:%08X) - %d "
Henry Zhangh220575f2010-01-22 14:22:44 -0800315 "elapsed=%lu usec\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800316 notif->channel,
317 notif->band ? "bg" : "a",
Wey-Yi Guy1895b362011-09-22 15:14:51 -0700318 notif->probe_status, notif->num_probe_not_sent,
Tomas Winkler2a421b92008-06-12 09:47:10 +0800319 le32_to_cpu(notif->tsf_high),
320 le32_to_cpu(notif->tsf_low),
321 le32_to_cpu(notif->statistics[0]),
Henry Zhangh220575f2010-01-22 14:22:44 -0800322 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800323#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700324 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800325}
326
327/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700328static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800329 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700330 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800331{
Zhu Yi2f301222009-10-09 17:19:45 +0800332 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800333 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800334
Tomas Winklere1623442009-01-27 14:27:56 -0800335 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800336 scan_notif->scanned_channels,
337 scan_notif->tsf_low,
338 scan_notif->tsf_high, scan_notif->status);
339
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200340 IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
Johannes Berg00700ee2010-04-06 04:12:37 -0700341 (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
Stanislaw Gruszkaef1b21f2010-11-12 08:47:07 +0100342 jiffies_to_msecs(jiffies - priv->scan_start));
Tomas Winkler2a421b92008-06-12 09:47:10 +0800343
Johannes Berg84b1bec2011-09-22 15:15:00 -0700344 /*
345 * When aborting, we run the scan completed background work inline
346 * and the background work must then do nothing. The SCAN_COMPLETE
347 * bit helps implement that logic and thus needs to be set before
348 * queueing the work. Also, since the scan abort waits for SCAN_HW
349 * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
350 * to avoid a race there.
351 */
Don Fry83626402012-03-07 09:52:37 -0800352 set_bit(STATUS_SCAN_COMPLETE, &priv->status);
353 clear_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg1ee158d2012-02-17 10:07:44 -0800354 queue_work(priv->workqueue, &priv->scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800355
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700356 if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100357 iwl_advanced_bt_coexist(priv) &&
Johannes Bergd5926d92010-09-13 14:46:34 +0200358 priv->bt_status != scan_notif->bt_status) {
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700359 if (scan_notif->bt_status) {
360 /* BT on */
361 if (!priv->bt_ch_announce)
362 priv->bt_traffic_load =
363 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
364 /*
365 * otherwise, no traffic load information provided
366 * no changes made
367 */
368 } else {
369 /* BT off */
370 priv->bt_traffic_load =
371 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
372 }
373 priv->bt_status = scan_notif->bt_status;
Johannes Berg1ee158d2012-02-17 10:07:44 -0800374 queue_work(priv->workqueue,
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700375 &priv->bt_traffic_change_work);
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700376 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700377 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800378}
379
380void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
381{
382 /* scan handlers */
383 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
384 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
385 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
386 iwl_rx_scan_results_notif;
387 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
388 iwl_rx_scan_complete_notif;
389}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800390
Johannes Berg6e809a12011-09-20 15:37:20 -0700391static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
392 enum ieee80211_band band, u8 n_probes)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800393{
394 if (band == IEEE80211_BAND_5GHZ)
Tomas Winklerfe905f12008-07-11 11:53:39 +0800395 return IWL_ACTIVE_DWELL_TIME_52 +
396 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800397 else
Tomas Winklerfe905f12008-07-11 11:53:39 +0800398 return IWL_ACTIVE_DWELL_TIME_24 +
399 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800400}
401
Johannes Berg390808d2011-09-20 15:37:22 -0700402static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
403{
404 struct iwl_rxon_context *ctx;
405
406 /*
407 * If we're associated, we clamp the dwell time 98%
408 * of the smallest beacon interval (minus 2 * channel
409 * tune time)
410 */
411 for_each_context(priv, ctx) {
412 u16 value;
413
Johannes Berg2ed81712012-01-28 08:30:52 -0800414 switch (ctx->staging.dev_type) {
415 case RXON_DEV_TYPE_P2P:
416 /* no timing constraints */
Johannes Berg390808d2011-09-20 15:37:22 -0700417 continue;
Johannes Berg2ed81712012-01-28 08:30:52 -0800418 case RXON_DEV_TYPE_ESS:
419 default:
420 /* timing constraints if associated */
421 if (!iwl_is_associated_ctx(ctx))
422 continue;
423 break;
424 case RXON_DEV_TYPE_CP:
425 case RXON_DEV_TYPE_2STA:
426 /*
427 * These seem to always have timers for TBTT
428 * active in uCode even when not associated yet.
429 */
430 break;
431 }
432
Johannes Berg390808d2011-09-20 15:37:22 -0700433 value = ctx->beacon_int;
434 if (!value)
435 value = IWL_PASSIVE_DWELL_BASE;
436 value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
437 dwell_time = min(value, dwell_time);
438 }
439
440 return dwell_time;
441}
442
Johannes Berg6e809a12011-09-20 15:37:20 -0700443static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
444 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800445{
Tomas Winklerfe905f12008-07-11 11:53:39 +0800446 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Tomas Winkler2a421b92008-06-12 09:47:10 +0800447 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
448 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
449
Johannes Berg390808d2011-09-20 15:37:22 -0700450 return iwl_limit_dwell(priv, passive);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800451}
452
Meenakshi Venkataramanc736f232012-03-15 13:27:03 -0700453/* Return valid, unused, channel for a passive scan to reset the RF */
454static u8 iwl_get_single_channel_number(struct iwl_priv *priv,
455 enum ieee80211_band band)
456{
457 const struct iwl_channel_info *ch_info;
458 int i;
459 u8 channel = 0;
460 u8 min, max;
461 struct iwl_rxon_context *ctx;
462
463 if (band == IEEE80211_BAND_5GHZ) {
464 min = 14;
465 max = priv->channel_count;
466 } else {
467 min = 0;
468 max = 14;
469 }
470
471 for (i = min; i < max; i++) {
472 bool busy = false;
473
474 for_each_context(priv, ctx) {
475 busy = priv->channel_info[i].channel ==
476 le16_to_cpu(ctx->staging.channel);
477 if (busy)
478 break;
479 }
480
481 if (busy)
482 continue;
483
484 channel = priv->channel_info[i].channel;
485 ch_info = iwl_get_channel_info(priv, band, channel);
486 if (is_channel_valid(ch_info))
487 break;
488 }
489
490 return channel;
491}
492
Johannes Berg6e809a12011-09-20 15:37:20 -0700493static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
494 struct ieee80211_vif *vif,
495 enum ieee80211_band band,
496 struct iwl_scan_channel *scan_ch)
497{
498 const struct ieee80211_supported_band *sband;
499 u16 passive_dwell = 0;
500 u16 active_dwell = 0;
501 int added = 0;
502 u16 channel = 0;
503
504 sband = iwl_get_hw_mode(priv, band);
505 if (!sband) {
506 IWL_ERR(priv, "invalid band\n");
507 return added;
508 }
509
510 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
511 passive_dwell = iwl_get_passive_dwell_time(priv, band);
512
513 if (passive_dwell <= active_dwell)
514 passive_dwell = active_dwell + 1;
515
516 channel = iwl_get_single_channel_number(priv, band);
517 if (channel) {
518 scan_ch->channel = cpu_to_le16(channel);
519 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
520 scan_ch->active_dwell = cpu_to_le16(active_dwell);
521 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
522 /* Set txpower levels to defaults */
523 scan_ch->dsp_atten = 110;
524 if (band == IEEE80211_BAND_5GHZ)
525 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
526 else
527 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
528 added++;
529 } else
530 IWL_ERR(priv, "no valid channel found\n");
531 return added;
532}
533
534static int iwl_get_channels_for_scan(struct iwl_priv *priv,
535 struct ieee80211_vif *vif,
536 enum ieee80211_band band,
537 u8 is_active, u8 n_probes,
538 struct iwl_scan_channel *scan_ch)
539{
540 struct ieee80211_channel *chan;
541 const struct ieee80211_supported_band *sband;
542 const struct iwl_channel_info *ch_info;
543 u16 passive_dwell = 0;
544 u16 active_dwell = 0;
545 int added, i;
546 u16 channel;
547
548 sband = iwl_get_hw_mode(priv, band);
549 if (!sband)
550 return 0;
551
552 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
553 passive_dwell = iwl_get_passive_dwell_time(priv, band);
554
555 if (passive_dwell <= active_dwell)
556 passive_dwell = active_dwell + 1;
557
558 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
559 chan = priv->scan_request->channels[i];
560
561 if (chan->band != band)
562 continue;
563
564 channel = chan->hw_value;
565 scan_ch->channel = cpu_to_le16(channel);
566
567 ch_info = iwl_get_channel_info(priv, band, channel);
568 if (!is_channel_valid(ch_info)) {
569 IWL_DEBUG_SCAN(priv,
570 "Channel %d is INVALID for this band.\n",
571 channel);
572 continue;
573 }
574
575 if (!is_active || is_channel_passive(ch_info) ||
576 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
577 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
578 else
579 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
580
581 if (n_probes)
582 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
583
584 scan_ch->active_dwell = cpu_to_le16(active_dwell);
585 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
586
587 /* Set txpower levels to defaults */
588 scan_ch->dsp_atten = 110;
589
590 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
591 * power level:
592 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
593 */
594 if (band == IEEE80211_BAND_5GHZ)
595 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
596 else
597 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
598
599 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
600 channel, le32_to_cpu(scan_ch->type),
601 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
602 "ACTIVE" : "PASSIVE",
603 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
604 active_dwell : passive_dwell);
605
606 scan_ch++;
607 added++;
608 }
609
610 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
611 return added;
612}
613
Johannes Bergb443d8d2012-03-06 13:31:08 -0800614/**
615 * iwl_fill_probe_req - fill in all required fields and IE for probe request
616 */
617
618static u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
619 const u8 *ies, int ie_len, int left)
620{
621 int len = 0;
622 u8 *pos = NULL;
623
624 /* Make sure there is enough space for the probe request,
625 * two mandatory IEs and the data */
626 left -= 24;
627 if (left < 0)
628 return 0;
629
630 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
631 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
632 memcpy(frame->sa, ta, ETH_ALEN);
633 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
634 frame->seq_ctrl = 0;
635
636 len += 24;
637
638 /* ...next IE... */
639 pos = &frame->u.probe_req.variable[0];
640
641 /* fill in our indirect SSID IE */
642 left -= 2;
643 if (left < 0)
644 return 0;
645 *pos++ = WLAN_EID_SSID;
646 *pos++ = 0;
647
648 len += 2;
649
650 if (WARN_ON(left < ie_len))
651 return len;
652
653 if (ies && ie_len) {
654 memcpy(pos, ies, ie_len);
655 len += ie_len;
656 }
657
658 return (u16)len;
659}
660
Johannes Berg6e809a12011-09-20 15:37:20 -0700661static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
662{
663 struct iwl_host_cmd cmd = {
664 .id = REPLY_SCAN_CMD,
665 .len = { sizeof(struct iwl_scan_cmd), },
666 .flags = CMD_SYNC,
667 };
668 struct iwl_scan_cmd *scan;
669 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
670 u32 rate_flags = 0;
Fabio Estevam331d9302012-01-16 00:47:12 -0200671 u16 cmd_len = 0;
Johannes Berg6e809a12011-09-20 15:37:20 -0700672 u16 rx_chain = 0;
673 enum ieee80211_band band;
674 u8 n_probes = 0;
Johannes Berg9e295112012-04-09 17:46:55 -0700675 u8 rx_ant = priv->hw_params.valid_rx_ant;
Johannes Berg6e809a12011-09-20 15:37:20 -0700676 u8 rate;
677 bool is_active = false;
678 int chan_mod;
679 u8 active_chains;
Johannes Berg9e295112012-04-09 17:46:55 -0700680 u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
Johannes Berg6e809a12011-09-20 15:37:20 -0700681 int ret;
682
Johannes Bergb1eea292012-03-06 13:30:42 -0800683 lockdep_assert_held(&priv->mutex);
Johannes Berg6e809a12011-09-20 15:37:20 -0700684
685 if (vif)
686 ctx = iwl_rxon_ctx_from_vif(vif);
687
688 if (!priv->scan_cmd) {
689 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
690 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
691 if (!priv->scan_cmd) {
692 IWL_DEBUG_SCAN(priv,
693 "fail to allocate memory for scan\n");
694 return -ENOMEM;
695 }
696 }
697 scan = priv->scan_cmd;
698 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
699
700 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
701 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
702
703 if (priv->scan_type != IWL_SCAN_ROC &&
704 iwl_is_any_associated(priv)) {
705 u16 interval = 0;
706 u32 extra;
707 u32 suspend_time = 100;
708 u32 scan_suspend_time = 100;
709
710 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
711 switch (priv->scan_type) {
712 case IWL_SCAN_ROC:
713 WARN_ON(1);
714 break;
715 case IWL_SCAN_RADIO_RESET:
716 interval = 0;
717 break;
718 case IWL_SCAN_NORMAL:
719 interval = vif->bss_conf.beacon_int;
720 break;
721 }
722
723 scan->suspend_time = 0;
724 scan->max_out_time = cpu_to_le32(200 * 1024);
725 if (!interval)
726 interval = suspend_time;
727
728 extra = (suspend_time / interval) << 22;
729 scan_suspend_time = (extra |
730 ((suspend_time % interval) * 1024));
731 scan->suspend_time = cpu_to_le32(scan_suspend_time);
732 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
733 scan_suspend_time, interval);
734 } else if (priv->scan_type == IWL_SCAN_ROC) {
735 scan->suspend_time = 0;
736 scan->max_out_time = 0;
737 scan->quiet_time = 0;
738 scan->quiet_plcp_th = 0;
739 }
740
741 switch (priv->scan_type) {
742 case IWL_SCAN_RADIO_RESET:
743 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
744 break;
745 case IWL_SCAN_NORMAL:
746 if (priv->scan_request->n_ssids) {
747 int i, p = 0;
748 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
749 for (i = 0; i < priv->scan_request->n_ssids; i++) {
750 /* always does wildcard anyway */
751 if (!priv->scan_request->ssids[i].ssid_len)
752 continue;
753 scan->direct_scan[p].id = WLAN_EID_SSID;
754 scan->direct_scan[p].len =
755 priv->scan_request->ssids[i].ssid_len;
756 memcpy(scan->direct_scan[p].ssid,
757 priv->scan_request->ssids[i].ssid,
758 priv->scan_request->ssids[i].ssid_len);
759 n_probes++;
760 p++;
761 }
762 is_active = true;
763 } else
764 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
765 break;
766 case IWL_SCAN_ROC:
767 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
768 break;
769 }
770
771 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
772 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
773 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
774
775 switch (priv->scan_band) {
776 case IEEE80211_BAND_2GHZ:
777 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
778 chan_mod = le32_to_cpu(
779 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
780 RXON_FLG_CHANNEL_MODE_MSK)
781 >> RXON_FLG_CHANNEL_MODE_POS;
Johannes Berg3a8aea02011-10-14 12:54:48 -0700782 if ((priv->scan_request && priv->scan_request->no_cck) ||
783 chan_mod == CHANNEL_MODE_PURE_40) {
Johannes Berg6e809a12011-09-20 15:37:20 -0700784 rate = IWL_RATE_6M_PLCP;
785 } else {
786 rate = IWL_RATE_1M_PLCP;
787 rate_flags = RATE_MCS_CCK_MSK;
788 }
789 /*
790 * Internal scans are passive, so we can indiscriminately set
791 * the BT ignore flag on 2.4 GHz since it applies to TX only.
792 */
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200793 if (priv->cfg->bt_params &&
794 priv->cfg->bt_params->advanced_bt_coexist)
Johannes Berg6e809a12011-09-20 15:37:20 -0700795 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
796 break;
797 case IEEE80211_BAND_5GHZ:
798 rate = IWL_RATE_6M_PLCP;
799 break;
800 default:
801 IWL_WARN(priv, "Invalid scan band\n");
802 return -EIO;
803 }
804
805 /*
806 * If active scanning is requested but a certain channel is
807 * marked passive, we can do active scanning if we detect
808 * transmissions.
809 *
810 * There is an issue with some firmware versions that triggers
811 * a sysassert on a "good CRC threshold" of zero (== disabled),
812 * on a radar channel even though this means that we should NOT
813 * send probes.
814 *
815 * The "good CRC threshold" is the number of frames that we
816 * need to receive during our dwell time on a channel before
817 * sending out probes -- setting this to a huge value will
818 * mean we never reach it, but at the same time work around
819 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
820 * here instead of IWL_GOOD_CRC_TH_DISABLED.
821 *
822 * This was fixed in later versions along with some other
823 * scan changes, and the threshold behaves as a flag in those
824 * versions.
825 */
826 if (priv->new_scan_threshold_behaviour)
827 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
828 IWL_GOOD_CRC_TH_DISABLED;
829 else
830 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
831 IWL_GOOD_CRC_TH_NEVER;
832
833 band = priv->scan_band;
834
Johannes Berg6e809a12011-09-20 15:37:20 -0700835 if (band == IEEE80211_BAND_2GHZ &&
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200836 priv->cfg->bt_params &&
837 priv->cfg->bt_params->advanced_bt_coexist) {
Johannes Berg6e809a12011-09-20 15:37:20 -0700838 /* transmit 2.4 GHz probes only on first antenna */
839 scan_tx_antennas = first_antenna(scan_tx_antennas);
840 }
841
842 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
843 priv->scan_tx_ant[band],
844 scan_tx_antennas);
845 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
846 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
847
Johannes Berge5610382012-03-15 13:26:51 -0700848 /*
849 * In power save mode while associated use one chain,
850 * otherwise use all chains
851 */
Don Fry47107e82012-03-15 13:27:06 -0700852 if (test_bit(STATUS_POWER_PMI, &priv->status) &&
Johannes Berge5610382012-03-15 13:26:51 -0700853 !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
Johannes Berg6e809a12011-09-20 15:37:20 -0700854 /* rx_ant has been set to all valid chains previously */
855 active_chains = rx_ant &
856 ((u8)(priv->chain_noise_data.active_chains));
857 if (!active_chains)
858 active_chains = rx_ant;
859
860 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
861 priv->chain_noise_data.active_chains);
862
863 rx_ant = first_antenna(active_chains);
864 }
Emmanuel Grumbach21522682012-03-22 17:51:44 +0200865 if (priv->cfg->bt_params &&
866 priv->cfg->bt_params->advanced_bt_coexist &&
Johannes Berg6e809a12011-09-20 15:37:20 -0700867 priv->bt_full_concurrent) {
868 /* operated as 1x1 in full concurrency mode */
869 rx_ant = first_antenna(rx_ant);
870 }
871
872 /* MIMO is not used here, but value is required */
873 rx_chain |=
Johannes Berg9e295112012-04-09 17:46:55 -0700874 priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
Johannes Berg6e809a12011-09-20 15:37:20 -0700875 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
876 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
877 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
878 scan->rx_chain = cpu_to_le16(rx_chain);
879 switch (priv->scan_type) {
880 case IWL_SCAN_NORMAL:
Johannes Bergb443d8d2012-03-06 13:31:08 -0800881 cmd_len = iwl_fill_probe_req(
Johannes Berg6e809a12011-09-20 15:37:20 -0700882 (struct ieee80211_mgmt *)scan->data,
883 vif->addr,
884 priv->scan_request->ie,
885 priv->scan_request->ie_len,
886 IWL_MAX_SCAN_SIZE - sizeof(*scan));
887 break;
888 case IWL_SCAN_RADIO_RESET:
889 case IWL_SCAN_ROC:
890 /* use bcast addr, will not be transmitted but must be valid */
Johannes Bergb443d8d2012-03-06 13:31:08 -0800891 cmd_len = iwl_fill_probe_req(
Johannes Berg6e809a12011-09-20 15:37:20 -0700892 (struct ieee80211_mgmt *)scan->data,
893 iwl_bcast_addr, NULL, 0,
894 IWL_MAX_SCAN_SIZE - sizeof(*scan));
895 break;
896 default:
897 BUG();
898 }
899 scan->tx_cmd.len = cpu_to_le16(cmd_len);
900
901 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
902 RXON_FILTER_BCON_AWARE_MSK);
903
904 switch (priv->scan_type) {
905 case IWL_SCAN_RADIO_RESET:
906 scan->channel_count =
907 iwl_get_single_channel_for_scan(priv, vif, band,
908 (void *)&scan->data[cmd_len]);
909 break;
910 case IWL_SCAN_NORMAL:
911 scan->channel_count =
912 iwl_get_channels_for_scan(priv, vif, band,
913 is_active, n_probes,
914 (void *)&scan->data[cmd_len]);
915 break;
916 case IWL_SCAN_ROC: {
917 struct iwl_scan_channel *scan_ch;
Johannes Berg390808d2011-09-20 15:37:22 -0700918 int n_chan, i;
919 u16 dwell;
Johannes Berg6e809a12011-09-20 15:37:20 -0700920
Johannes Berg390808d2011-09-20 15:37:22 -0700921 dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
922 n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
923
924 scan->channel_count = n_chan;
Johannes Berg6e809a12011-09-20 15:37:20 -0700925
926 scan_ch = (void *)&scan->data[cmd_len];
Johannes Berg6e809a12011-09-20 15:37:20 -0700927
Johannes Berg390808d2011-09-20 15:37:22 -0700928 for (i = 0; i < n_chan; i++) {
929 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
930 scan_ch->channel =
931 cpu_to_le16(priv->hw_roc_channel->hw_value);
Johannes Berg6e809a12011-09-20 15:37:20 -0700932
Johannes Berg390808d2011-09-20 15:37:22 -0700933 if (i == n_chan - 1)
934 dwell = priv->hw_roc_duration - i * dwell;
935
936 scan_ch->active_dwell =
937 scan_ch->passive_dwell = cpu_to_le16(dwell);
938
939 /* Set txpower levels to defaults */
940 scan_ch->dsp_atten = 110;
941
942 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
943 * power level:
944 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
945 */
946 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
947 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
948 else
949 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
950
951 scan_ch++;
Johannes Berg6e809a12011-09-20 15:37:20 -0700952 }
Johannes Berg390808d2011-09-20 15:37:22 -0700953 }
954
Johannes Berg6e809a12011-09-20 15:37:20 -0700955 break;
956 }
957
958 if (scan->channel_count == 0) {
959 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
960 return -EIO;
961 }
962
963 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
964 scan->channel_count * sizeof(struct iwl_scan_channel);
965 cmd.data[0] = scan;
966 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
967 scan->len = cpu_to_le16(cmd.len[0]);
968
969 /* set scan bit here for PAN params */
Don Fry83626402012-03-07 09:52:37 -0800970 set_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg6e809a12011-09-20 15:37:20 -0700971
972 ret = iwlagn_set_pan_params(priv);
973 if (ret)
974 return ret;
975
Johannes Berge10a0532012-03-06 13:30:39 -0800976 ret = iwl_dvm_send_cmd(priv, &cmd);
Johannes Berg6e809a12011-09-20 15:37:20 -0700977 if (ret) {
Don Fry83626402012-03-07 09:52:37 -0800978 clear_bit(STATUS_SCAN_HW, &priv->status);
Johannes Berg6e809a12011-09-20 15:37:20 -0700979 iwlagn_set_pan_params(priv);
980 }
981
982 return ret;
983}
984
Tomas Winklerf53696d2008-06-12 09:47:12 +0800985void iwl_init_scan_params(struct iwl_priv *priv)
986{
Johannes Berg9e295112012-04-09 17:46:55 -0700987 u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800988 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700989 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800990 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700991 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800992}
993
Johannes Berg266af4c72011-03-10 20:13:26 -0800994int __must_check iwl_scan_initiate(struct iwl_priv *priv,
995 struct ieee80211_vif *vif,
996 enum iwl_scan_type scan_type,
997 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800998{
Johannes Berg3eecce52010-09-13 14:46:33 +0200999 int ret;
Johannes Berg88be0262010-04-07 00:21:36 -07001000
Johannes Bergb1eea292012-03-06 13:30:42 -08001001 lockdep_assert_held(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001002
Johannes Berg3eecce52010-09-13 14:46:33 +02001003 cancel_delayed_work(&priv->scan_check);
1004
Don Fry83626402012-03-07 09:52:37 -08001005 if (!iwl_is_ready_rf(priv)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001006 IWL_WARN(priv, "Request scan called when driver not ready.\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02001007 return -EIO;
1008 }
1009
Don Fry83626402012-03-07 09:52:37 -08001010 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001011 IWL_DEBUG_SCAN(priv,
Johannes Berg3eecce52010-09-13 14:46:33 +02001012 "Multiple concurrent scan requests in parallel.\n");
1013 return -EBUSY;
1014 }
1015
Don Fry83626402012-03-07 09:52:37 -08001016 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001017 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
Johannes Berg3eecce52010-09-13 14:46:33 +02001018 return -EBUSY;
1019 }
1020
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001021 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
Johannes Berg266af4c72011-03-10 20:13:26 -08001022 scan_type == IWL_SCAN_NORMAL ? "" :
Johannes Bergc6baf7f2011-07-23 10:24:47 -07001023 scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
Johannes Berg266af4c72011-03-10 20:13:26 -08001024 "internal short ");
Johannes Berg3eecce52010-09-13 14:46:33 +02001025
Don Fry83626402012-03-07 09:52:37 -08001026 set_bit(STATUS_SCANNING, &priv->status);
Johannes Berg266af4c72011-03-10 20:13:26 -08001027 priv->scan_type = scan_type;
Johannes Berg3eecce52010-09-13 14:46:33 +02001028 priv->scan_start = jiffies;
1029 priv->scan_band = band;
1030
Don Fry5c3d29f2011-07-08 08:46:29 -07001031 ret = iwlagn_request_scan(priv, vif);
Johannes Berg3eecce52010-09-13 14:46:33 +02001032 if (ret) {
Don Fry83626402012-03-07 09:52:37 -08001033 clear_bit(STATUS_SCANNING, &priv->status);
Johannes Berg266af4c72011-03-10 20:13:26 -08001034 priv->scan_type = IWL_SCAN_NORMAL;
Johannes Berg3eecce52010-09-13 14:46:33 +02001035 return ret;
1036 }
1037
Johannes Berg1ee158d2012-02-17 10:07:44 -08001038 queue_delayed_work(priv->workqueue, &priv->scan_check,
Johannes Berg3eecce52010-09-13 14:46:33 +02001039 IWL_SCAN_CHECK_WATCHDOG);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001040
1041 return 0;
1042}
Tomas Winkler2a421b92008-06-12 09:47:10 +08001043
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -08001044
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001045/*
1046 * internal short scan, this function should only been called while associated.
1047 * It will reset and tune the radio to prevent possible RF related problem
1048 */
Johannes Berg88be0262010-04-07 00:21:36 -07001049void iwl_internal_short_hw_scan(struct iwl_priv *priv)
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001050{
Johannes Berg1ee158d2012-02-17 10:07:44 -08001051 queue_work(priv->workqueue, &priv->start_internal_scan);
Johannes Berg88be0262010-04-07 00:21:36 -07001052}
1053
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02001054static void iwl_bg_start_internal_scan(struct work_struct *work)
Johannes Berg88be0262010-04-07 00:21:36 -07001055{
1056 struct iwl_priv *priv =
1057 container_of(work, struct iwl_priv, start_internal_scan);
1058
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001059 IWL_DEBUG_SCAN(priv, "Start internal scan\n");
1060
Johannes Bergb1eea292012-03-06 13:30:42 -08001061 mutex_lock(&priv->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001062
Johannes Berg266af4c72011-03-10 20:13:26 -08001063 if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
Reinette Chatre073d5ea2010-05-13 14:49:44 -07001064 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
1065 goto unlock;
1066 }
1067
Don Fry83626402012-03-07 09:52:37 -08001068 if (test_bit(STATUS_SCANNING, &priv->status)) {
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001069 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
Johannes Berg88be0262010-04-07 00:21:36 -07001070 goto unlock;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001071 }
Johannes Berg88be0262010-04-07 00:21:36 -07001072
Johannes Berg266af4c72011-03-10 20:13:26 -08001073 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
Johannes Berg3eecce52010-09-13 14:46:33 +02001074 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
Johannes Berg88be0262010-04-07 00:21:36 -07001075 unlock:
Johannes Bergb1eea292012-03-06 13:30:42 -08001076 mutex_unlock(&priv->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001077}
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001078
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02001079static void iwl_bg_scan_check(struct work_struct *data)
Tomas Winkler2a421b92008-06-12 09:47:10 +08001080{
1081 struct iwl_priv *priv =
1082 container_of(data, struct iwl_priv, scan_check.work);
1083
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001084 IWL_DEBUG_SCAN(priv, "Scan check work\n");
1085
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001086 /* Since we are here firmware does not finish scan and
1087 * most likely is in bad shape, so we don't bother to
1088 * send abort command, just force scan complete to mac80211 */
Johannes Bergb1eea292012-03-06 13:30:42 -08001089 mutex_lock(&priv->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001090 iwl_force_scan_end(priv);
Johannes Bergb1eea292012-03-06 13:30:42 -08001091 mutex_unlock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001092}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001093
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02001094static void iwl_bg_abort_scan(struct work_struct *work)
Tomas Winkler2a421b92008-06-12 09:47:10 +08001095{
1096 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1097
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001098 IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1099
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001100 /* We keep scan_check work queued in case when firmware will not
1101 * report back scan completed notification */
Johannes Bergb1eea292012-03-06 13:30:42 -08001102 mutex_lock(&priv->mutex);
Stanislaw Gruszka6bd1758d2010-09-13 14:46:40 +02001103 iwl_scan_cancel_timeout(priv, 200);
Johannes Bergb1eea292012-03-06 13:30:42 -08001104 mutex_unlock(&priv->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001105}
1106
Johannes Bergf2532472011-09-22 15:14:58 -07001107static void iwl_bg_scan_completed(struct work_struct *work)
1108{
1109 struct iwl_priv *priv =
1110 container_of(work, struct iwl_priv, scan_completed);
1111
Johannes Bergb1eea292012-03-06 13:30:42 -08001112 mutex_lock(&priv->mutex);
Johannes Bergf2532472011-09-22 15:14:58 -07001113 iwl_process_scan_complete(priv);
Johannes Bergb1eea292012-03-06 13:30:42 -08001114 mutex_unlock(&priv->mutex);
Tomas Winklereedda362008-10-06 16:05:32 +08001115}
Tomas Winklereedda362008-10-06 16:05:32 +08001116
Tomas Winkler2a421b92008-06-12 09:47:10 +08001117void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1118{
Tomas Winklereedda362008-10-06 16:05:32 +08001119 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001120 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
Johannes Berg88be0262010-04-07 00:21:36 -07001121 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001122 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1123}
Tomas Winkler2a421b92008-06-12 09:47:10 +08001124
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001125void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1126{
1127 cancel_work_sync(&priv->start_internal_scan);
1128 cancel_work_sync(&priv->abort_scan);
1129 cancel_work_sync(&priv->scan_completed);
1130
1131 if (cancel_delayed_work_sync(&priv->scan_check)) {
Johannes Bergb1eea292012-03-06 13:30:42 -08001132 mutex_lock(&priv->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001133 iwl_force_scan_end(priv);
Johannes Bergb1eea292012-03-06 13:30:42 -08001134 mutex_unlock(&priv->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001135 }
1136}