blob: e50338b47593bd1f9a44c1fb7453e791256c956c [file] [log] [blame]
Tomas Winkler2a421b92008-06-12 09:47:10 +08001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07005 * Copyright(c) 2008 - 2011 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"
35#include "iwl-core.h"
36#include "iwl-sta.h"
37#include "iwl-io.h"
38#include "iwl-helpers.h"
Wey-Yi Guye80d70e2011-06-03 07:54:15 -070039#include "iwl-agn.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070040#include "iwl-trans.h"
Tomas Winkler2a421b92008-06-12 09:47:10 +080041
42/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
43 * sending probe req. This should be set long enough to hear probe responses
44 * from more than one AP. */
Tomas Winklerfe905f12008-07-11 11:53:39 +080045#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
46#define IWL_ACTIVE_DWELL_TIME_52 (20)
47
48#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
49#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Tomas Winkler2a421b92008-06-12 09:47:10 +080050
Tomas Winkler2a421b92008-06-12 09:47:10 +080051/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
52 * Must be set longer than active dwell time.
53 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
54#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
55#define IWL_PASSIVE_DWELL_TIME_52 (10)
56#define IWL_PASSIVE_DWELL_BASE (100)
57#define IWL_CHANNEL_TUNE_TIME 5
58
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020059static int iwl_send_scan_abort(struct iwl_priv *priv)
60{
61 int ret;
62 struct iwl_rx_packet *pkt;
63 struct iwl_host_cmd cmd = {
64 .id = REPLY_SCAN_ABORT_CMD,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070065 .flags = CMD_SYNC | CMD_WANT_SKB,
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020066 };
Tomas Winklerfe905f12008-07-11 11:53:39 +080067
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020068 /* Exit instantly with error when device is not ready
69 * to receive scan abort command or it does not perform
70 * hardware scan currently */
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -070071 if (!test_bit(STATUS_READY, &priv->shrd->status) ||
72 !test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) ||
73 !test_bit(STATUS_SCAN_HW, &priv->shrd->status) ||
74 test_bit(STATUS_FW_ERROR, &priv->shrd->status) ||
75 test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020076 return -EIO;
77
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -070078 ret = iwl_trans_send_cmd(trans(priv), &cmd);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020079 if (ret)
80 return ret;
81
82 pkt = (struct iwl_rx_packet *)cmd.reply_page;
83 if (pkt->u.status != CAN_ABORT_STATUS) {
84 /* The scan abort will return 1 for success or
85 * 2 for "failure". A failure condition can be
86 * due to simply not being in an active scan which
87 * can occur if we send the scan abort before we
88 * the microcode has notified us that a scan is
89 * completed. */
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +020090 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n", pkt->u.status);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020091 ret = -EIO;
92 }
93
Emmanuel Grumbach790428b2011-08-25 23:11:05 -070094 iwl_free_pages(priv->shrd, cmd.reply_page);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +020095 return ret;
96}
97
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +020098static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
99{
100 /* check if scan was requested from mac80211 */
101 if (priv->scan_request) {
102 IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
103 ieee80211_scan_completed(priv->hw, aborted);
104 }
105
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700106 if (priv->scan_type == IWL_SCAN_ROC) {
107 ieee80211_remain_on_channel_expired(priv->hw);
108 priv->hw_roc_channel = NULL;
109 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
110 }
111
Johannes Berg266af4c72011-03-10 20:13:26 -0800112 priv->scan_type = IWL_SCAN_NORMAL;
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +0200113 priv->scan_vif = NULL;
114 priv->scan_request = NULL;
115}
116
Johannes Berga2fa2462011-09-22 15:14:59 -0700117static void iwl_process_scan_complete(struct iwl_priv *priv)
118{
119 bool aborted;
120
121 IWL_DEBUG_SCAN(priv, "Completed scan.\n");
122
123 cancel_delayed_work(&priv->scan_check);
124
125 aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
126 if (aborted)
127 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
128
129 if (!test_and_clear_bit(STATUS_SCANNING, &priv->shrd->status)) {
130 IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
131 goto out_settings;
132 }
133
134 if (priv->scan_type == IWL_SCAN_ROC) {
135 ieee80211_remain_on_channel_expired(priv->hw);
136 priv->hw_roc_channel = NULL;
137 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
138 }
139
140 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
141 int err;
142
143 /* Check if mac80211 requested scan during our internal scan */
144 if (priv->scan_request == NULL)
145 goto out_complete;
146
147 /* If so request a new scan */
148 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
149 priv->scan_request->channels[0]->band);
150 if (err) {
151 IWL_DEBUG_SCAN(priv,
152 "failed to initiate pending scan: %d\n", err);
153 aborted = true;
154 goto out_complete;
155 }
156
157 return;
158 }
159
160out_complete:
161 iwl_complete_scan(priv, aborted);
162
163out_settings:
164 /* Can we still talk to firmware ? */
165 if (!iwl_is_ready_rf(priv->shrd))
166 return;
167
168 iwlagn_post_scan(priv);
169}
170
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200171void iwl_force_scan_end(struct iwl_priv *priv)
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200172{
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700173 lockdep_assert_held(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200174
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700175 if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200176 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
177 return;
178 }
179
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200180 IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700181 clear_bit(STATUS_SCANNING, &priv->shrd->status);
182 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
183 clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200184 iwl_complete_scan(priv, true);
185}
186
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200187static void iwl_do_scan_abort(struct iwl_priv *priv)
188{
189 int ret;
190
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700191 lockdep_assert_held(&priv->shrd->mutex);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200192
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700193 if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200194 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
195 return;
196 }
197
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700198 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200199 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
200 return;
201 }
202
203 ret = iwl_send_scan_abort(priv);
204 if (ret) {
205 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200206 iwl_force_scan_end(priv);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200207 } else
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300208 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200209}
Tomas Winklerfe905f12008-07-11 11:53:39 +0800210
Tomas Winkler2a421b92008-06-12 09:47:10 +0800211/**
212 * iwl_scan_cancel - Cancel any currently executing HW scan
Tomas Winkler2a421b92008-06-12 09:47:10 +0800213 */
214int iwl_scan_cancel(struct iwl_priv *priv)
215{
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200216 IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700217 queue_work(priv->shrd->workqueue, &priv->abort_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800218 return 0;
219}
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200220
Tomas Winkler2a421b92008-06-12 09:47:10 +0800221/**
222 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
223 * @ms: amount of time to wait (in milliseconds) for scan to abort
224 *
Tomas Winkler2a421b92008-06-12 09:47:10 +0800225 */
Johannes Berg98efb4a2011-09-22 15:14:57 -0700226void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800227{
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200228 unsigned long timeout = jiffies + msecs_to_jiffies(ms);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800229
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700230 lockdep_assert_held(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800231
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200232 IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
233
234 iwl_do_scan_abort(priv);
235
236 while (time_before_eq(jiffies, timeout)) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700237 if (!test_bit(STATUS_SCAN_HW, &priv->shrd->status))
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200238 break;
239 msleep(20);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800240 }
Tomas Winkler2a421b92008-06-12 09:47:10 +0800241}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800242
Tomas Winkler2a421b92008-06-12 09:47:10 +0800243/* Service response to REPLY_SCAN_CMD (0x80) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700244static int iwl_rx_reply_scan(struct iwl_priv *priv,
245 struct iwl_rx_mem_buffer *rxb,
246 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800247{
248#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800249 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800250 struct iwl_scanreq_notification *notif =
251 (struct iwl_scanreq_notification *)pkt->u.raw;
252
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200253 IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800254#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700255 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800256}
257
258/* Service SCAN_START_NOTIFICATION (0x82) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700259static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
260 struct iwl_rx_mem_buffer *rxb,
261 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800262{
Zhu Yi2f301222009-10-09 17:19:45 +0800263 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800264 struct iwl_scanstart_notification *notif =
265 (struct iwl_scanstart_notification *)pkt->u.raw;
266 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
Tomas Winklere1623442009-01-27 14:27:56 -0800267 IWL_DEBUG_SCAN(priv, "Scan start: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800268 "%d [802.11%s] "
269 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
270 notif->channel,
271 notif->band ? "bg" : "a",
Tomas Winklerfe905f12008-07-11 11:53:39 +0800272 le32_to_cpu(notif->tsf_high),
273 le32_to_cpu(notif->tsf_low),
274 notif->status, notif->beacon_timer);
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700275
Johannes Berg390808d2011-09-20 15:37:22 -0700276 if (priv->scan_type == IWL_SCAN_ROC &&
277 !priv->hw_roc_start_notified) {
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700278 ieee80211_ready_on_channel(priv->hw);
Johannes Berg390808d2011-09-20 15:37:22 -0700279 priv->hw_roc_start_notified = true;
280 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700281
282 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800283}
284
285/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700286static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
287 struct iwl_rx_mem_buffer *rxb,
288 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800289{
290#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800291 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800292 struct iwl_scanresults_notification *notif =
293 (struct iwl_scanresults_notification *)pkt->u.raw;
294
Tomas Winklere1623442009-01-27 14:27:56 -0800295 IWL_DEBUG_SCAN(priv, "Scan ch.res: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800296 "%d [802.11%s] "
Wey-Yi Guy1895b362011-09-22 15:14:51 -0700297 "probe status: %u:%u "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800298 "(TSF: 0x%08X:%08X) - %d "
Henry Zhangh220575f2010-01-22 14:22:44 -0800299 "elapsed=%lu usec\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800300 notif->channel,
301 notif->band ? "bg" : "a",
Wey-Yi Guy1895b362011-09-22 15:14:51 -0700302 notif->probe_status, notif->num_probe_not_sent,
Tomas Winkler2a421b92008-06-12 09:47:10 +0800303 le32_to_cpu(notif->tsf_high),
304 le32_to_cpu(notif->tsf_low),
305 le32_to_cpu(notif->statistics[0]),
Henry Zhangh220575f2010-01-22 14:22:44 -0800306 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800307#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700308 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800309}
310
311/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700312static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
313 struct iwl_rx_mem_buffer *rxb,
314 struct iwl_device_cmd *cmd)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800315{
Zhu Yi2f301222009-10-09 17:19:45 +0800316 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800317 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
318
Tomas Winklere1623442009-01-27 14:27:56 -0800319 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800320 scan_notif->scanned_channels,
321 scan_notif->tsf_low,
322 scan_notif->tsf_high, scan_notif->status);
323
324 /* The HW is no longer scanning */
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700325 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800326
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200327 IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
Johannes Berg00700ee2010-04-06 04:12:37 -0700328 (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
Stanislaw Gruszkaef1b21f2010-11-12 08:47:07 +0100329 jiffies_to_msecs(jiffies - priv->scan_start));
Tomas Winkler2a421b92008-06-12 09:47:10 +0800330
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700331 queue_work(priv->shrd->workqueue, &priv->scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800332
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700333 if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100334 iwl_advanced_bt_coexist(priv) &&
Johannes Bergd5926d92010-09-13 14:46:34 +0200335 priv->bt_status != scan_notif->bt_status) {
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700336 if (scan_notif->bt_status) {
337 /* BT on */
338 if (!priv->bt_ch_announce)
339 priv->bt_traffic_load =
340 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
341 /*
342 * otherwise, no traffic load information provided
343 * no changes made
344 */
345 } else {
346 /* BT off */
347 priv->bt_traffic_load =
348 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
349 }
350 priv->bt_status = scan_notif->bt_status;
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700351 queue_work(priv->shrd->workqueue,
352 &priv->bt_traffic_change_work);
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700353 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700354 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800355}
356
357void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
358{
359 /* scan handlers */
360 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
361 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
362 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
363 iwl_rx_scan_results_notif;
364 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
365 iwl_rx_scan_complete_notif;
366}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800367
Johannes Berg6e809a12011-09-20 15:37:20 -0700368static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
369 enum ieee80211_band band, u8 n_probes)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800370{
371 if (band == IEEE80211_BAND_5GHZ)
Tomas Winklerfe905f12008-07-11 11:53:39 +0800372 return IWL_ACTIVE_DWELL_TIME_52 +
373 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800374 else
Tomas Winklerfe905f12008-07-11 11:53:39 +0800375 return IWL_ACTIVE_DWELL_TIME_24 +
376 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800377}
378
Johannes Berg390808d2011-09-20 15:37:22 -0700379static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
380{
381 struct iwl_rxon_context *ctx;
382
383 /*
384 * If we're associated, we clamp the dwell time 98%
385 * of the smallest beacon interval (minus 2 * channel
386 * tune time)
387 */
388 for_each_context(priv, ctx) {
389 u16 value;
390
391 if (!iwl_is_associated_ctx(ctx))
392 continue;
393 value = ctx->beacon_int;
394 if (!value)
395 value = IWL_PASSIVE_DWELL_BASE;
396 value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
397 dwell_time = min(value, dwell_time);
398 }
399
400 return dwell_time;
401}
402
Johannes Berg6e809a12011-09-20 15:37:20 -0700403static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
404 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800405{
Tomas Winklerfe905f12008-07-11 11:53:39 +0800406 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Tomas Winkler2a421b92008-06-12 09:47:10 +0800407 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
408 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
409
Johannes Berg390808d2011-09-20 15:37:22 -0700410 return iwl_limit_dwell(priv, passive);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800411}
412
Johannes Berg6e809a12011-09-20 15:37:20 -0700413static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
414 struct ieee80211_vif *vif,
415 enum ieee80211_band band,
416 struct iwl_scan_channel *scan_ch)
417{
418 const struct ieee80211_supported_band *sband;
419 u16 passive_dwell = 0;
420 u16 active_dwell = 0;
421 int added = 0;
422 u16 channel = 0;
423
424 sband = iwl_get_hw_mode(priv, band);
425 if (!sband) {
426 IWL_ERR(priv, "invalid band\n");
427 return added;
428 }
429
430 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
431 passive_dwell = iwl_get_passive_dwell_time(priv, band);
432
433 if (passive_dwell <= active_dwell)
434 passive_dwell = active_dwell + 1;
435
436 channel = iwl_get_single_channel_number(priv, band);
437 if (channel) {
438 scan_ch->channel = cpu_to_le16(channel);
439 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
440 scan_ch->active_dwell = cpu_to_le16(active_dwell);
441 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
442 /* Set txpower levels to defaults */
443 scan_ch->dsp_atten = 110;
444 if (band == IEEE80211_BAND_5GHZ)
445 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
446 else
447 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
448 added++;
449 } else
450 IWL_ERR(priv, "no valid channel found\n");
451 return added;
452}
453
454static int iwl_get_channels_for_scan(struct iwl_priv *priv,
455 struct ieee80211_vif *vif,
456 enum ieee80211_band band,
457 u8 is_active, u8 n_probes,
458 struct iwl_scan_channel *scan_ch)
459{
460 struct ieee80211_channel *chan;
461 const struct ieee80211_supported_band *sband;
462 const struct iwl_channel_info *ch_info;
463 u16 passive_dwell = 0;
464 u16 active_dwell = 0;
465 int added, i;
466 u16 channel;
467
468 sband = iwl_get_hw_mode(priv, band);
469 if (!sband)
470 return 0;
471
472 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
473 passive_dwell = iwl_get_passive_dwell_time(priv, band);
474
475 if (passive_dwell <= active_dwell)
476 passive_dwell = active_dwell + 1;
477
478 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
479 chan = priv->scan_request->channels[i];
480
481 if (chan->band != band)
482 continue;
483
484 channel = chan->hw_value;
485 scan_ch->channel = cpu_to_le16(channel);
486
487 ch_info = iwl_get_channel_info(priv, band, channel);
488 if (!is_channel_valid(ch_info)) {
489 IWL_DEBUG_SCAN(priv,
490 "Channel %d is INVALID for this band.\n",
491 channel);
492 continue;
493 }
494
495 if (!is_active || is_channel_passive(ch_info) ||
496 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
497 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
498 else
499 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
500
501 if (n_probes)
502 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
503
504 scan_ch->active_dwell = cpu_to_le16(active_dwell);
505 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
506
507 /* Set txpower levels to defaults */
508 scan_ch->dsp_atten = 110;
509
510 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
511 * power level:
512 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
513 */
514 if (band == IEEE80211_BAND_5GHZ)
515 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
516 else
517 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
518
519 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
520 channel, le32_to_cpu(scan_ch->type),
521 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
522 "ACTIVE" : "PASSIVE",
523 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
524 active_dwell : passive_dwell);
525
526 scan_ch++;
527 added++;
528 }
529
530 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
531 return added;
532}
533
534static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
535{
536 struct iwl_host_cmd cmd = {
537 .id = REPLY_SCAN_CMD,
538 .len = { sizeof(struct iwl_scan_cmd), },
539 .flags = CMD_SYNC,
540 };
541 struct iwl_scan_cmd *scan;
542 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
543 u32 rate_flags = 0;
544 u16 cmd_len;
545 u16 rx_chain = 0;
546 enum ieee80211_band band;
547 u8 n_probes = 0;
548 u8 rx_ant = hw_params(priv).valid_rx_ant;
549 u8 rate;
550 bool is_active = false;
551 int chan_mod;
552 u8 active_chains;
553 u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
554 int ret;
555
556 lockdep_assert_held(&priv->shrd->mutex);
557
558 if (vif)
559 ctx = iwl_rxon_ctx_from_vif(vif);
560
561 if (!priv->scan_cmd) {
562 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
563 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
564 if (!priv->scan_cmd) {
565 IWL_DEBUG_SCAN(priv,
566 "fail to allocate memory for scan\n");
567 return -ENOMEM;
568 }
569 }
570 scan = priv->scan_cmd;
571 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
572
573 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
574 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
575
576 if (priv->scan_type != IWL_SCAN_ROC &&
577 iwl_is_any_associated(priv)) {
578 u16 interval = 0;
579 u32 extra;
580 u32 suspend_time = 100;
581 u32 scan_suspend_time = 100;
582
583 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
584 switch (priv->scan_type) {
585 case IWL_SCAN_ROC:
586 WARN_ON(1);
587 break;
588 case IWL_SCAN_RADIO_RESET:
589 interval = 0;
590 break;
591 case IWL_SCAN_NORMAL:
592 interval = vif->bss_conf.beacon_int;
593 break;
594 }
595
596 scan->suspend_time = 0;
597 scan->max_out_time = cpu_to_le32(200 * 1024);
598 if (!interval)
599 interval = suspend_time;
600
601 extra = (suspend_time / interval) << 22;
602 scan_suspend_time = (extra |
603 ((suspend_time % interval) * 1024));
604 scan->suspend_time = cpu_to_le32(scan_suspend_time);
605 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
606 scan_suspend_time, interval);
607 } else if (priv->scan_type == IWL_SCAN_ROC) {
608 scan->suspend_time = 0;
609 scan->max_out_time = 0;
610 scan->quiet_time = 0;
611 scan->quiet_plcp_th = 0;
612 }
613
614 switch (priv->scan_type) {
615 case IWL_SCAN_RADIO_RESET:
616 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
617 break;
618 case IWL_SCAN_NORMAL:
619 if (priv->scan_request->n_ssids) {
620 int i, p = 0;
621 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
622 for (i = 0; i < priv->scan_request->n_ssids; i++) {
623 /* always does wildcard anyway */
624 if (!priv->scan_request->ssids[i].ssid_len)
625 continue;
626 scan->direct_scan[p].id = WLAN_EID_SSID;
627 scan->direct_scan[p].len =
628 priv->scan_request->ssids[i].ssid_len;
629 memcpy(scan->direct_scan[p].ssid,
630 priv->scan_request->ssids[i].ssid,
631 priv->scan_request->ssids[i].ssid_len);
632 n_probes++;
633 p++;
634 }
635 is_active = true;
636 } else
637 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
638 break;
639 case IWL_SCAN_ROC:
640 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
641 break;
642 }
643
644 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
645 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
646 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
647
648 switch (priv->scan_band) {
649 case IEEE80211_BAND_2GHZ:
650 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
651 chan_mod = le32_to_cpu(
652 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
653 RXON_FLG_CHANNEL_MODE_MSK)
654 >> RXON_FLG_CHANNEL_MODE_POS;
655 if (chan_mod == CHANNEL_MODE_PURE_40) {
656 rate = IWL_RATE_6M_PLCP;
657 } else {
658 rate = IWL_RATE_1M_PLCP;
659 rate_flags = RATE_MCS_CCK_MSK;
660 }
661 /*
662 * Internal scans are passive, so we can indiscriminately set
663 * the BT ignore flag on 2.4 GHz since it applies to TX only.
664 */
665 if (priv->cfg->bt_params &&
666 priv->cfg->bt_params->advanced_bt_coexist)
667 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
668 break;
669 case IEEE80211_BAND_5GHZ:
670 rate = IWL_RATE_6M_PLCP;
671 break;
672 default:
673 IWL_WARN(priv, "Invalid scan band\n");
674 return -EIO;
675 }
676
677 /*
678 * If active scanning is requested but a certain channel is
679 * marked passive, we can do active scanning if we detect
680 * transmissions.
681 *
682 * There is an issue with some firmware versions that triggers
683 * a sysassert on a "good CRC threshold" of zero (== disabled),
684 * on a radar channel even though this means that we should NOT
685 * send probes.
686 *
687 * The "good CRC threshold" is the number of frames that we
688 * need to receive during our dwell time on a channel before
689 * sending out probes -- setting this to a huge value will
690 * mean we never reach it, but at the same time work around
691 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
692 * here instead of IWL_GOOD_CRC_TH_DISABLED.
693 *
694 * This was fixed in later versions along with some other
695 * scan changes, and the threshold behaves as a flag in those
696 * versions.
697 */
698 if (priv->new_scan_threshold_behaviour)
699 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
700 IWL_GOOD_CRC_TH_DISABLED;
701 else
702 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
703 IWL_GOOD_CRC_TH_NEVER;
704
705 band = priv->scan_band;
706
707 if (priv->cfg->scan_rx_antennas[band])
708 rx_ant = priv->cfg->scan_rx_antennas[band];
709
710 if (band == IEEE80211_BAND_2GHZ &&
711 priv->cfg->bt_params &&
712 priv->cfg->bt_params->advanced_bt_coexist) {
713 /* transmit 2.4 GHz probes only on first antenna */
714 scan_tx_antennas = first_antenna(scan_tx_antennas);
715 }
716
717 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
718 priv->scan_tx_ant[band],
719 scan_tx_antennas);
720 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
721 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
722
723 /* In power save mode use one chain, otherwise use all chains */
724 if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
725 /* rx_ant has been set to all valid chains previously */
726 active_chains = rx_ant &
727 ((u8)(priv->chain_noise_data.active_chains));
728 if (!active_chains)
729 active_chains = rx_ant;
730
731 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
732 priv->chain_noise_data.active_chains);
733
734 rx_ant = first_antenna(active_chains);
735 }
736 if (priv->cfg->bt_params &&
737 priv->cfg->bt_params->advanced_bt_coexist &&
738 priv->bt_full_concurrent) {
739 /* operated as 1x1 in full concurrency mode */
740 rx_ant = first_antenna(rx_ant);
741 }
742
743 /* MIMO is not used here, but value is required */
744 rx_chain |=
745 hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
746 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
747 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
748 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
749 scan->rx_chain = cpu_to_le16(rx_chain);
750 switch (priv->scan_type) {
751 case IWL_SCAN_NORMAL:
752 cmd_len = iwl_fill_probe_req(priv,
753 (struct ieee80211_mgmt *)scan->data,
754 vif->addr,
755 priv->scan_request->ie,
756 priv->scan_request->ie_len,
757 IWL_MAX_SCAN_SIZE - sizeof(*scan));
758 break;
759 case IWL_SCAN_RADIO_RESET:
760 case IWL_SCAN_ROC:
761 /* use bcast addr, will not be transmitted but must be valid */
762 cmd_len = iwl_fill_probe_req(priv,
763 (struct ieee80211_mgmt *)scan->data,
764 iwl_bcast_addr, NULL, 0,
765 IWL_MAX_SCAN_SIZE - sizeof(*scan));
766 break;
767 default:
768 BUG();
769 }
770 scan->tx_cmd.len = cpu_to_le16(cmd_len);
771
772 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
773 RXON_FILTER_BCON_AWARE_MSK);
774
775 switch (priv->scan_type) {
776 case IWL_SCAN_RADIO_RESET:
777 scan->channel_count =
778 iwl_get_single_channel_for_scan(priv, vif, band,
779 (void *)&scan->data[cmd_len]);
780 break;
781 case IWL_SCAN_NORMAL:
782 scan->channel_count =
783 iwl_get_channels_for_scan(priv, vif, band,
784 is_active, n_probes,
785 (void *)&scan->data[cmd_len]);
786 break;
787 case IWL_SCAN_ROC: {
788 struct iwl_scan_channel *scan_ch;
Johannes Berg390808d2011-09-20 15:37:22 -0700789 int n_chan, i;
790 u16 dwell;
Johannes Berg6e809a12011-09-20 15:37:20 -0700791
Johannes Berg390808d2011-09-20 15:37:22 -0700792 dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
793 n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
794
795 scan->channel_count = n_chan;
Johannes Berg6e809a12011-09-20 15:37:20 -0700796
797 scan_ch = (void *)&scan->data[cmd_len];
Johannes Berg6e809a12011-09-20 15:37:20 -0700798
Johannes Berg390808d2011-09-20 15:37:22 -0700799 for (i = 0; i < n_chan; i++) {
800 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
801 scan_ch->channel =
802 cpu_to_le16(priv->hw_roc_channel->hw_value);
Johannes Berg6e809a12011-09-20 15:37:20 -0700803
Johannes Berg390808d2011-09-20 15:37:22 -0700804 if (i == n_chan - 1)
805 dwell = priv->hw_roc_duration - i * dwell;
806
807 scan_ch->active_dwell =
808 scan_ch->passive_dwell = cpu_to_le16(dwell);
809
810 /* Set txpower levels to defaults */
811 scan_ch->dsp_atten = 110;
812
813 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
814 * power level:
815 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
816 */
817 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
818 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
819 else
820 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
821
822 scan_ch++;
Johannes Berg6e809a12011-09-20 15:37:20 -0700823 }
Johannes Berg390808d2011-09-20 15:37:22 -0700824 }
825
Johannes Berg6e809a12011-09-20 15:37:20 -0700826 break;
827 }
828
829 if (scan->channel_count == 0) {
830 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
831 return -EIO;
832 }
833
834 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
835 scan->channel_count * sizeof(struct iwl_scan_channel);
836 cmd.data[0] = scan;
837 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
838 scan->len = cpu_to_le16(cmd.len[0]);
839
840 /* set scan bit here for PAN params */
841 set_bit(STATUS_SCAN_HW, &priv->shrd->status);
842
843 ret = iwlagn_set_pan_params(priv);
844 if (ret)
845 return ret;
846
847 ret = iwl_trans_send_cmd(trans(priv), &cmd);
848 if (ret) {
849 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
850 iwlagn_set_pan_params(priv);
851 }
852
853 return ret;
854}
855
Tomas Winklerf53696d2008-06-12 09:47:12 +0800856void iwl_init_scan_params(struct iwl_priv *priv)
857{
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700858 u8 ant_idx = fls(hw_params(priv).valid_tx_ant) - 1;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800859 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700860 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800861 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700862 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800863}
864
Johannes Berg266af4c72011-03-10 20:13:26 -0800865int __must_check iwl_scan_initiate(struct iwl_priv *priv,
866 struct ieee80211_vif *vif,
867 enum iwl_scan_type scan_type,
868 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800869{
Johannes Berg3eecce52010-09-13 14:46:33 +0200870 int ret;
Johannes Berg88be0262010-04-07 00:21:36 -0700871
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700872 lockdep_assert_held(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800873
Johannes Berg3eecce52010-09-13 14:46:33 +0200874 cancel_delayed_work(&priv->scan_check);
875
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700876 if (!iwl_is_ready_rf(priv->shrd)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200877 IWL_WARN(priv, "Request scan called when driver not ready.\n");
Johannes Berg3eecce52010-09-13 14:46:33 +0200878 return -EIO;
879 }
880
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700881 if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200882 IWL_DEBUG_SCAN(priv,
Johannes Berg3eecce52010-09-13 14:46:33 +0200883 "Multiple concurrent scan requests in parallel.\n");
884 return -EBUSY;
885 }
886
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700887 if (test_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200888 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
Johannes Berg3eecce52010-09-13 14:46:33 +0200889 return -EBUSY;
890 }
891
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200892 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
Johannes Berg266af4c72011-03-10 20:13:26 -0800893 scan_type == IWL_SCAN_NORMAL ? "" :
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700894 scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
Johannes Berg266af4c72011-03-10 20:13:26 -0800895 "internal short ");
Johannes Berg3eecce52010-09-13 14:46:33 +0200896
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700897 set_bit(STATUS_SCANNING, &priv->shrd->status);
Johannes Berg266af4c72011-03-10 20:13:26 -0800898 priv->scan_type = scan_type;
Johannes Berg3eecce52010-09-13 14:46:33 +0200899 priv->scan_start = jiffies;
900 priv->scan_band = band;
901
Don Fry5c3d29f2011-07-08 08:46:29 -0700902 ret = iwlagn_request_scan(priv, vif);
Johannes Berg3eecce52010-09-13 14:46:33 +0200903 if (ret) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700904 clear_bit(STATUS_SCANNING, &priv->shrd->status);
Johannes Berg266af4c72011-03-10 20:13:26 -0800905 priv->scan_type = IWL_SCAN_NORMAL;
Johannes Berg3eecce52010-09-13 14:46:33 +0200906 return ret;
907 }
908
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700909 queue_delayed_work(priv->shrd->workqueue, &priv->scan_check,
Johannes Berg3eecce52010-09-13 14:46:33 +0200910 IWL_SCAN_CHECK_WATCHDOG);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800911
912 return 0;
913}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800914
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800915int iwl_mac_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +0200916 struct ieee80211_vif *vif,
917 struct cfg80211_scan_request *req)
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800918{
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800919 struct iwl_priv *priv = hw->priv;
Johannes Berg00700ee2010-04-06 04:12:37 -0700920 int ret;
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800921
922 IWL_DEBUG_MAC80211(priv, "enter\n");
923
Johannes Berg00700ee2010-04-06 04:12:37 -0700924 if (req->n_channels == 0)
925 return -EINVAL;
926
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700927 mutex_lock(&priv->shrd->mutex);
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800928
Johannes Bergf84b29e2010-05-18 02:29:13 -0700929 /*
930 * If an internal scan is in progress, just set
931 * up the scan_request as per above.
932 */
Johannes Berg266af4c72011-03-10 20:13:26 -0800933 if (priv->scan_type != IWL_SCAN_NORMAL) {
Johannes Berg8bd2c1e2011-09-22 15:14:54 -0700934 IWL_DEBUG_SCAN(priv,
935 "SCAN request during internal scan - defer\n");
936 priv->scan_request = req;
937 priv->scan_vif = vif;
Johannes Bergf84b29e2010-05-18 02:29:13 -0700938 ret = 0;
Johannes Berg8bd2c1e2011-09-22 15:14:54 -0700939 } else {
940 priv->scan_request = req;
941 priv->scan_vif = vif;
942 /*
943 * mac80211 will only ask for one band at a time
944 * so using channels[0] here is ok
945 */
Johannes Berg266af4c72011-03-10 20:13:26 -0800946 ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
Johannes Berg3eecce52010-09-13 14:46:33 +0200947 req->channels[0]->band);
Johannes Berg8bd2c1e2011-09-22 15:14:54 -0700948 if (ret) {
949 priv->scan_request = NULL;
950 priv->scan_vif = NULL;
951 }
952 }
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800953
954 IWL_DEBUG_MAC80211(priv, "leave\n");
955
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700956 mutex_unlock(&priv->shrd->mutex);
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800957
958 return ret;
959}
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800960
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800961/*
962 * internal short scan, this function should only been called while associated.
963 * It will reset and tune the radio to prevent possible RF related problem
964 */
Johannes Berg88be0262010-04-07 00:21:36 -0700965void iwl_internal_short_hw_scan(struct iwl_priv *priv)
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800966{
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700967 queue_work(priv->shrd->workqueue, &priv->start_internal_scan);
Johannes Berg88be0262010-04-07 00:21:36 -0700968}
969
Stanislaw Gruszkac2408792010-07-30 16:41:08 +0200970static void iwl_bg_start_internal_scan(struct work_struct *work)
Johannes Berg88be0262010-04-07 00:21:36 -0700971{
972 struct iwl_priv *priv =
973 container_of(work, struct iwl_priv, start_internal_scan);
974
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200975 IWL_DEBUG_SCAN(priv, "Start internal scan\n");
976
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700977 mutex_lock(&priv->shrd->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800978
Johannes Berg266af4c72011-03-10 20:13:26 -0800979 if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
Reinette Chatre073d5ea2010-05-13 14:49:44 -0700980 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
981 goto unlock;
982 }
983
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700984 if (test_bit(STATUS_SCANNING, &priv->shrd->status)) {
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800985 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700986 goto unlock;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800987 }
Johannes Berg88be0262010-04-07 00:21:36 -0700988
Johannes Berg266af4c72011-03-10 20:13:26 -0800989 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
Johannes Berg3eecce52010-09-13 14:46:33 +0200990 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700991 unlock:
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700992 mutex_unlock(&priv->shrd->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800993}
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800994
Stanislaw Gruszkac2408792010-07-30 16:41:08 +0200995static void iwl_bg_scan_check(struct work_struct *data)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800996{
997 struct iwl_priv *priv =
998 container_of(data, struct iwl_priv, scan_check.work);
999
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001000 IWL_DEBUG_SCAN(priv, "Scan check work\n");
1001
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001002 /* Since we are here firmware does not finish scan and
1003 * most likely is in bad shape, so we don't bother to
1004 * send abort command, just force scan complete to mac80211 */
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001005 mutex_lock(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001006 iwl_force_scan_end(priv);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001007 mutex_unlock(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001008}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -08001009
Tomas Winkler2a421b92008-06-12 09:47:10 +08001010/**
Tomas Winkler2a421b92008-06-12 09:47:10 +08001011 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1012 */
Tomas Winklerf53696d2008-06-12 09:47:12 +08001013
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07001014u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07001015 const u8 *ta, const u8 *ies, int ie_len, int left)
Tomas Winkler2a421b92008-06-12 09:47:10 +08001016{
1017 int len = 0;
1018 u8 *pos = NULL;
Tomas Winklerf53696d2008-06-12 09:47:12 +08001019
Tomas Winkler2a421b92008-06-12 09:47:10 +08001020 /* Make sure there is enough space for the probe request,
1021 * two mandatory IEs and the data */
1022 left -= 24;
1023 if (left < 0)
1024 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001025
1026 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1027 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Johannes Berg3a0b9aa2010-05-12 03:33:12 -07001028 memcpy(frame->sa, ta, ETH_ALEN);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001029 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
1030 frame->seq_ctrl = 0;
1031
Tomas Winklerf53696d2008-06-12 09:47:12 +08001032 len += 24;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001033
Tomas Winklerf53696d2008-06-12 09:47:12 +08001034 /* ...next IE... */
1035 pos = &frame->u.probe_req.variable[0];
1036
1037 /* fill in our indirect SSID IE */
Tomas Winkler2a421b92008-06-12 09:47:10 +08001038 left -= 2;
1039 if (left < 0)
1040 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001041 *pos++ = WLAN_EID_SSID;
Reinette Chatre1382c712010-02-25 10:02:19 -08001042 *pos++ = 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001043
Reinette Chatre1382c712010-02-25 10:02:19 -08001044 len += 2;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001045
Johannes Berg1ecf9fc2009-04-20 14:36:56 -07001046 if (WARN_ON(left < ie_len))
1047 return len;
Tomas Winkler2a421b92008-06-12 09:47:10 +08001048
Stanislaw Gruszkaeb2ec0f2010-04-16 15:46:41 +02001049 if (ies && ie_len) {
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001050 memcpy(pos, ies, ie_len);
Stanislaw Gruszkaeb2ec0f2010-04-16 15:46:41 +02001051 len += ie_len;
1052 }
Tomas Winklerf53696d2008-06-12 09:47:12 +08001053
Tomas Winkler2a421b92008-06-12 09:47:10 +08001054 return (u16)len;
1055}
1056
Stanislaw Gruszkac2408792010-07-30 16:41:08 +02001057static void iwl_bg_abort_scan(struct work_struct *work)
Tomas Winkler2a421b92008-06-12 09:47:10 +08001058{
1059 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1060
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001061 IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1062
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001063 /* We keep scan_check work queued in case when firmware will not
1064 * report back scan completed notification */
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001065 mutex_lock(&priv->shrd->mutex);
Stanislaw Gruszka6bd1758d2010-09-13 14:46:40 +02001066 iwl_scan_cancel_timeout(priv, 200);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001067 mutex_unlock(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001068}
1069
Johannes Bergf2532472011-09-22 15:14:58 -07001070static void iwl_bg_scan_completed(struct work_struct *work)
1071{
1072 struct iwl_priv *priv =
1073 container_of(work, struct iwl_priv, scan_completed);
1074
1075 mutex_lock(&priv->shrd->mutex);
1076 iwl_process_scan_complete(priv);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001077 mutex_unlock(&priv->shrd->mutex);
Tomas Winklereedda362008-10-06 16:05:32 +08001078}
Tomas Winklereedda362008-10-06 16:05:32 +08001079
Tomas Winkler2a421b92008-06-12 09:47:10 +08001080void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1081{
Tomas Winklereedda362008-10-06 16:05:32 +08001082 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001083 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
Johannes Berg88be0262010-04-07 00:21:36 -07001084 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001085 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1086}
Tomas Winkler2a421b92008-06-12 09:47:10 +08001087
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001088void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1089{
1090 cancel_work_sync(&priv->start_internal_scan);
1091 cancel_work_sync(&priv->abort_scan);
1092 cancel_work_sync(&priv->scan_completed);
1093
1094 if (cancel_delayed_work_sync(&priv->scan_check)) {
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001095 mutex_lock(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001096 iwl_force_scan_end(priv);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001097 mutex_unlock(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001098 }
1099}