blob: 8ac6b05c6c788b5c38ba4641a86a75a980f7b23c [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
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200117void iwl_force_scan_end(struct iwl_priv *priv)
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200118{
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700119 lockdep_assert_held(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200120
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700121 if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200122 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
123 return;
124 }
125
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200126 IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700127 clear_bit(STATUS_SCANNING, &priv->shrd->status);
128 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
129 clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200130 iwl_complete_scan(priv, true);
131}
132
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200133static void iwl_do_scan_abort(struct iwl_priv *priv)
134{
135 int ret;
136
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700137 lockdep_assert_held(&priv->shrd->mutex);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200138
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700139 if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200140 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
141 return;
142 }
143
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700144 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200145 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
146 return;
147 }
148
149 ret = iwl_send_scan_abort(priv);
150 if (ret) {
151 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
Stanislaw Gruszkaf5354c12010-09-13 14:46:39 +0200152 iwl_force_scan_end(priv);
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200153 } else
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300154 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200155}
Tomas Winklerfe905f12008-07-11 11:53:39 +0800156
Tomas Winkler2a421b92008-06-12 09:47:10 +0800157/**
158 * iwl_scan_cancel - Cancel any currently executing HW scan
Tomas Winkler2a421b92008-06-12 09:47:10 +0800159 */
160int iwl_scan_cancel(struct iwl_priv *priv)
161{
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200162 IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700163 queue_work(priv->shrd->workqueue, &priv->abort_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800164 return 0;
165}
Stanislaw Gruszkacd446002010-09-13 14:46:36 +0200166
Tomas Winkler2a421b92008-06-12 09:47:10 +0800167/**
168 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
169 * @ms: amount of time to wait (in milliseconds) for scan to abort
170 *
Tomas Winkler2a421b92008-06-12 09:47:10 +0800171 */
172int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
173{
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200174 unsigned long timeout = jiffies + msecs_to_jiffies(ms);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800175
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700176 lockdep_assert_held(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800177
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200178 IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
179
180 iwl_do_scan_abort(priv);
181
182 while (time_before_eq(jiffies, timeout)) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700183 if (!test_bit(STATUS_SCAN_HW, &priv->shrd->status))
Stanislaw Gruszkae693a802010-09-13 14:46:37 +0200184 break;
185 msleep(20);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800186 }
187
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700188 return test_bit(STATUS_SCAN_HW, &priv->shrd->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800189}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800190
Tomas Winkler2a421b92008-06-12 09:47:10 +0800191/* Service response to REPLY_SCAN_CMD (0x80) */
192static void iwl_rx_reply_scan(struct iwl_priv *priv,
193 struct iwl_rx_mem_buffer *rxb)
194{
195#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800196 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800197 struct iwl_scanreq_notification *notif =
198 (struct iwl_scanreq_notification *)pkt->u.raw;
199
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200200 IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800201#endif
202}
203
204/* Service SCAN_START_NOTIFICATION (0x82) */
205static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
206 struct iwl_rx_mem_buffer *rxb)
207{
Zhu Yi2f301222009-10-09 17:19:45 +0800208 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800209 struct iwl_scanstart_notification *notif =
210 (struct iwl_scanstart_notification *)pkt->u.raw;
211 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
Tomas Winklere1623442009-01-27 14:27:56 -0800212 IWL_DEBUG_SCAN(priv, "Scan start: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800213 "%d [802.11%s] "
214 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
215 notif->channel,
216 notif->band ? "bg" : "a",
Tomas Winklerfe905f12008-07-11 11:53:39 +0800217 le32_to_cpu(notif->tsf_high),
218 le32_to_cpu(notif->tsf_low),
219 notif->status, notif->beacon_timer);
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700220
221 if (priv->scan_type == IWL_SCAN_ROC)
222 ieee80211_ready_on_channel(priv->hw);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800223}
224
225/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
226static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
227 struct iwl_rx_mem_buffer *rxb)
228{
229#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800230 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800231 struct iwl_scanresults_notification *notif =
232 (struct iwl_scanresults_notification *)pkt->u.raw;
233
Tomas Winklere1623442009-01-27 14:27:56 -0800234 IWL_DEBUG_SCAN(priv, "Scan ch.res: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800235 "%d [802.11%s] "
236 "(TSF: 0x%08X:%08X) - %d "
Henry Zhangh220575f2010-01-22 14:22:44 -0800237 "elapsed=%lu usec\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800238 notif->channel,
239 notif->band ? "bg" : "a",
240 le32_to_cpu(notif->tsf_high),
241 le32_to_cpu(notif->tsf_low),
242 le32_to_cpu(notif->statistics[0]),
Henry Zhangh220575f2010-01-22 14:22:44 -0800243 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800244#endif
Tomas Winkler2a421b92008-06-12 09:47:10 +0800245}
246
247/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
248static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
249 struct iwl_rx_mem_buffer *rxb)
250{
Zhu Yi2f301222009-10-09 17:19:45 +0800251 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800252 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
253
Tomas Winklere1623442009-01-27 14:27:56 -0800254 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800255 scan_notif->scanned_channels,
256 scan_notif->tsf_low,
257 scan_notif->tsf_high, scan_notif->status);
258
259 /* The HW is no longer scanning */
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700260 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800261
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200262 IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
Johannes Berg00700ee2010-04-06 04:12:37 -0700263 (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
Stanislaw Gruszkaef1b21f2010-11-12 08:47:07 +0100264 jiffies_to_msecs(jiffies - priv->scan_start));
Tomas Winkler2a421b92008-06-12 09:47:10 +0800265
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700266 queue_work(priv->shrd->workqueue, &priv->scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800267
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700268 if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100269 iwl_advanced_bt_coexist(priv) &&
Johannes Bergd5926d92010-09-13 14:46:34 +0200270 priv->bt_status != scan_notif->bt_status) {
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700271 if (scan_notif->bt_status) {
272 /* BT on */
273 if (!priv->bt_ch_announce)
274 priv->bt_traffic_load =
275 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
276 /*
277 * otherwise, no traffic load information provided
278 * no changes made
279 */
280 } else {
281 /* BT off */
282 priv->bt_traffic_load =
283 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
284 }
285 priv->bt_status = scan_notif->bt_status;
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700286 queue_work(priv->shrd->workqueue,
287 &priv->bt_traffic_change_work);
Wey-Yi Guyf78e5452010-08-23 07:57:15 -0700288 }
Tomas Winkler2a421b92008-06-12 09:47:10 +0800289}
290
291void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
292{
293 /* scan handlers */
294 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
295 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
296 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
297 iwl_rx_scan_results_notif;
298 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
299 iwl_rx_scan_complete_notif;
300}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800301
Johannes Berg6e809a12011-09-20 15:37:20 -0700302static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
303 enum ieee80211_band band, u8 n_probes)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800304{
305 if (band == IEEE80211_BAND_5GHZ)
Tomas Winklerfe905f12008-07-11 11:53:39 +0800306 return IWL_ACTIVE_DWELL_TIME_52 +
307 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800308 else
Tomas Winklerfe905f12008-07-11 11:53:39 +0800309 return IWL_ACTIVE_DWELL_TIME_24 +
310 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800311}
312
Johannes Berg6e809a12011-09-20 15:37:20 -0700313static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
314 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800315{
Johannes Berg95c38dd2010-08-23 10:46:41 +0200316 struct iwl_rxon_context *ctx;
Tomas Winklerfe905f12008-07-11 11:53:39 +0800317 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Tomas Winkler2a421b92008-06-12 09:47:10 +0800318 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
319 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
320
Johannes Berg246ed352010-08-23 10:46:32 +0200321 if (iwl_is_any_associated(priv)) {
Johannes Berg95c38dd2010-08-23 10:46:41 +0200322 /*
323 * If we're associated, we clamp the maximum passive
324 * dwell time to be 98% of the smallest beacon interval
325 * (minus 2 * channel tune time)
326 */
327 for_each_context(priv, ctx) {
328 u16 value;
329
330 if (!iwl_is_associated_ctx(ctx))
331 continue;
332 value = ctx->vif ? ctx->vif->bss_conf.beacon_int : 0;
333 if ((value > IWL_PASSIVE_DWELL_BASE) || !value)
334 value = IWL_PASSIVE_DWELL_BASE;
335 value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
336 passive = min(value, passive);
337 }
Tomas Winkler2a421b92008-06-12 09:47:10 +0800338 }
339
Tomas Winkler2a421b92008-06-12 09:47:10 +0800340 return passive;
341}
342
Johannes Berg6e809a12011-09-20 15:37:20 -0700343static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
344 struct ieee80211_vif *vif,
345 enum ieee80211_band band,
346 struct iwl_scan_channel *scan_ch)
347{
348 const struct ieee80211_supported_band *sband;
349 u16 passive_dwell = 0;
350 u16 active_dwell = 0;
351 int added = 0;
352 u16 channel = 0;
353
354 sband = iwl_get_hw_mode(priv, band);
355 if (!sband) {
356 IWL_ERR(priv, "invalid band\n");
357 return added;
358 }
359
360 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
361 passive_dwell = iwl_get_passive_dwell_time(priv, band);
362
363 if (passive_dwell <= active_dwell)
364 passive_dwell = active_dwell + 1;
365
366 channel = iwl_get_single_channel_number(priv, band);
367 if (channel) {
368 scan_ch->channel = cpu_to_le16(channel);
369 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
370 scan_ch->active_dwell = cpu_to_le16(active_dwell);
371 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
372 /* Set txpower levels to defaults */
373 scan_ch->dsp_atten = 110;
374 if (band == IEEE80211_BAND_5GHZ)
375 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
376 else
377 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
378 added++;
379 } else
380 IWL_ERR(priv, "no valid channel found\n");
381 return added;
382}
383
384static int iwl_get_channels_for_scan(struct iwl_priv *priv,
385 struct ieee80211_vif *vif,
386 enum ieee80211_band band,
387 u8 is_active, u8 n_probes,
388 struct iwl_scan_channel *scan_ch)
389{
390 struct ieee80211_channel *chan;
391 const struct ieee80211_supported_band *sband;
392 const struct iwl_channel_info *ch_info;
393 u16 passive_dwell = 0;
394 u16 active_dwell = 0;
395 int added, i;
396 u16 channel;
397
398 sband = iwl_get_hw_mode(priv, band);
399 if (!sband)
400 return 0;
401
402 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
403 passive_dwell = iwl_get_passive_dwell_time(priv, band);
404
405 if (passive_dwell <= active_dwell)
406 passive_dwell = active_dwell + 1;
407
408 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
409 chan = priv->scan_request->channels[i];
410
411 if (chan->band != band)
412 continue;
413
414 channel = chan->hw_value;
415 scan_ch->channel = cpu_to_le16(channel);
416
417 ch_info = iwl_get_channel_info(priv, band, channel);
418 if (!is_channel_valid(ch_info)) {
419 IWL_DEBUG_SCAN(priv,
420 "Channel %d is INVALID for this band.\n",
421 channel);
422 continue;
423 }
424
425 if (!is_active || is_channel_passive(ch_info) ||
426 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
427 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
428 else
429 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
430
431 if (n_probes)
432 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
433
434 scan_ch->active_dwell = cpu_to_le16(active_dwell);
435 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
436
437 /* Set txpower levels to defaults */
438 scan_ch->dsp_atten = 110;
439
440 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
441 * power level:
442 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
443 */
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
449 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
450 channel, le32_to_cpu(scan_ch->type),
451 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
452 "ACTIVE" : "PASSIVE",
453 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
454 active_dwell : passive_dwell);
455
456 scan_ch++;
457 added++;
458 }
459
460 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
461 return added;
462}
463
464static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
465{
466 struct iwl_host_cmd cmd = {
467 .id = REPLY_SCAN_CMD,
468 .len = { sizeof(struct iwl_scan_cmd), },
469 .flags = CMD_SYNC,
470 };
471 struct iwl_scan_cmd *scan;
472 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
473 u32 rate_flags = 0;
474 u16 cmd_len;
475 u16 rx_chain = 0;
476 enum ieee80211_band band;
477 u8 n_probes = 0;
478 u8 rx_ant = hw_params(priv).valid_rx_ant;
479 u8 rate;
480 bool is_active = false;
481 int chan_mod;
482 u8 active_chains;
483 u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
484 int ret;
485
486 lockdep_assert_held(&priv->shrd->mutex);
487
488 if (vif)
489 ctx = iwl_rxon_ctx_from_vif(vif);
490
491 if (!priv->scan_cmd) {
492 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
493 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
494 if (!priv->scan_cmd) {
495 IWL_DEBUG_SCAN(priv,
496 "fail to allocate memory for scan\n");
497 return -ENOMEM;
498 }
499 }
500 scan = priv->scan_cmd;
501 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
502
503 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
504 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
505
506 if (priv->scan_type != IWL_SCAN_ROC &&
507 iwl_is_any_associated(priv)) {
508 u16 interval = 0;
509 u32 extra;
510 u32 suspend_time = 100;
511 u32 scan_suspend_time = 100;
512
513 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
514 switch (priv->scan_type) {
515 case IWL_SCAN_ROC:
516 WARN_ON(1);
517 break;
518 case IWL_SCAN_RADIO_RESET:
519 interval = 0;
520 break;
521 case IWL_SCAN_NORMAL:
522 interval = vif->bss_conf.beacon_int;
523 break;
524 }
525
526 scan->suspend_time = 0;
527 scan->max_out_time = cpu_to_le32(200 * 1024);
528 if (!interval)
529 interval = suspend_time;
530
531 extra = (suspend_time / interval) << 22;
532 scan_suspend_time = (extra |
533 ((suspend_time % interval) * 1024));
534 scan->suspend_time = cpu_to_le32(scan_suspend_time);
535 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
536 scan_suspend_time, interval);
537 } else if (priv->scan_type == IWL_SCAN_ROC) {
538 scan->suspend_time = 0;
539 scan->max_out_time = 0;
540 scan->quiet_time = 0;
541 scan->quiet_plcp_th = 0;
542 }
543
544 switch (priv->scan_type) {
545 case IWL_SCAN_RADIO_RESET:
546 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
547 break;
548 case IWL_SCAN_NORMAL:
549 if (priv->scan_request->n_ssids) {
550 int i, p = 0;
551 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
552 for (i = 0; i < priv->scan_request->n_ssids; i++) {
553 /* always does wildcard anyway */
554 if (!priv->scan_request->ssids[i].ssid_len)
555 continue;
556 scan->direct_scan[p].id = WLAN_EID_SSID;
557 scan->direct_scan[p].len =
558 priv->scan_request->ssids[i].ssid_len;
559 memcpy(scan->direct_scan[p].ssid,
560 priv->scan_request->ssids[i].ssid,
561 priv->scan_request->ssids[i].ssid_len);
562 n_probes++;
563 p++;
564 }
565 is_active = true;
566 } else
567 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
568 break;
569 case IWL_SCAN_ROC:
570 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
571 break;
572 }
573
574 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
575 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
576 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
577
578 switch (priv->scan_band) {
579 case IEEE80211_BAND_2GHZ:
580 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
581 chan_mod = le32_to_cpu(
582 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
583 RXON_FLG_CHANNEL_MODE_MSK)
584 >> RXON_FLG_CHANNEL_MODE_POS;
585 if (chan_mod == CHANNEL_MODE_PURE_40) {
586 rate = IWL_RATE_6M_PLCP;
587 } else {
588 rate = IWL_RATE_1M_PLCP;
589 rate_flags = RATE_MCS_CCK_MSK;
590 }
591 /*
592 * Internal scans are passive, so we can indiscriminately set
593 * the BT ignore flag on 2.4 GHz since it applies to TX only.
594 */
595 if (priv->cfg->bt_params &&
596 priv->cfg->bt_params->advanced_bt_coexist)
597 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
598 break;
599 case IEEE80211_BAND_5GHZ:
600 rate = IWL_RATE_6M_PLCP;
601 break;
602 default:
603 IWL_WARN(priv, "Invalid scan band\n");
604 return -EIO;
605 }
606
607 /*
608 * If active scanning is requested but a certain channel is
609 * marked passive, we can do active scanning if we detect
610 * transmissions.
611 *
612 * There is an issue with some firmware versions that triggers
613 * a sysassert on a "good CRC threshold" of zero (== disabled),
614 * on a radar channel even though this means that we should NOT
615 * send probes.
616 *
617 * The "good CRC threshold" is the number of frames that we
618 * need to receive during our dwell time on a channel before
619 * sending out probes -- setting this to a huge value will
620 * mean we never reach it, but at the same time work around
621 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
622 * here instead of IWL_GOOD_CRC_TH_DISABLED.
623 *
624 * This was fixed in later versions along with some other
625 * scan changes, and the threshold behaves as a flag in those
626 * versions.
627 */
628 if (priv->new_scan_threshold_behaviour)
629 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
630 IWL_GOOD_CRC_TH_DISABLED;
631 else
632 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
633 IWL_GOOD_CRC_TH_NEVER;
634
635 band = priv->scan_band;
636
637 if (priv->cfg->scan_rx_antennas[band])
638 rx_ant = priv->cfg->scan_rx_antennas[band];
639
640 if (band == IEEE80211_BAND_2GHZ &&
641 priv->cfg->bt_params &&
642 priv->cfg->bt_params->advanced_bt_coexist) {
643 /* transmit 2.4 GHz probes only on first antenna */
644 scan_tx_antennas = first_antenna(scan_tx_antennas);
645 }
646
647 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
648 priv->scan_tx_ant[band],
649 scan_tx_antennas);
650 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
651 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
652
653 /* In power save mode use one chain, otherwise use all chains */
654 if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
655 /* rx_ant has been set to all valid chains previously */
656 active_chains = rx_ant &
657 ((u8)(priv->chain_noise_data.active_chains));
658 if (!active_chains)
659 active_chains = rx_ant;
660
661 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
662 priv->chain_noise_data.active_chains);
663
664 rx_ant = first_antenna(active_chains);
665 }
666 if (priv->cfg->bt_params &&
667 priv->cfg->bt_params->advanced_bt_coexist &&
668 priv->bt_full_concurrent) {
669 /* operated as 1x1 in full concurrency mode */
670 rx_ant = first_antenna(rx_ant);
671 }
672
673 /* MIMO is not used here, but value is required */
674 rx_chain |=
675 hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
676 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
677 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
678 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
679 scan->rx_chain = cpu_to_le16(rx_chain);
680 switch (priv->scan_type) {
681 case IWL_SCAN_NORMAL:
682 cmd_len = iwl_fill_probe_req(priv,
683 (struct ieee80211_mgmt *)scan->data,
684 vif->addr,
685 priv->scan_request->ie,
686 priv->scan_request->ie_len,
687 IWL_MAX_SCAN_SIZE - sizeof(*scan));
688 break;
689 case IWL_SCAN_RADIO_RESET:
690 case IWL_SCAN_ROC:
691 /* use bcast addr, will not be transmitted but must be valid */
692 cmd_len = iwl_fill_probe_req(priv,
693 (struct ieee80211_mgmt *)scan->data,
694 iwl_bcast_addr, NULL, 0,
695 IWL_MAX_SCAN_SIZE - sizeof(*scan));
696 break;
697 default:
698 BUG();
699 }
700 scan->tx_cmd.len = cpu_to_le16(cmd_len);
701
702 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
703 RXON_FILTER_BCON_AWARE_MSK);
704
705 switch (priv->scan_type) {
706 case IWL_SCAN_RADIO_RESET:
707 scan->channel_count =
708 iwl_get_single_channel_for_scan(priv, vif, band,
709 (void *)&scan->data[cmd_len]);
710 break;
711 case IWL_SCAN_NORMAL:
712 scan->channel_count =
713 iwl_get_channels_for_scan(priv, vif, band,
714 is_active, n_probes,
715 (void *)&scan->data[cmd_len]);
716 break;
717 case IWL_SCAN_ROC: {
718 struct iwl_scan_channel *scan_ch;
719
720 scan->channel_count = 1;
721
722 scan_ch = (void *)&scan->data[cmd_len];
723 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
724 scan_ch->channel =
725 cpu_to_le16(priv->hw_roc_channel->hw_value);
726 scan_ch->active_dwell =
727 scan_ch->passive_dwell =
728 cpu_to_le16(priv->hw_roc_duration);
729
730 /* Set txpower levels to defaults */
731 scan_ch->dsp_atten = 110;
732
733 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
734 * power level:
735 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
736 */
737 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
738 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
739 else
740 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
741 }
742 break;
743 }
744
745 if (scan->channel_count == 0) {
746 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
747 return -EIO;
748 }
749
750 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
751 scan->channel_count * sizeof(struct iwl_scan_channel);
752 cmd.data[0] = scan;
753 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
754 scan->len = cpu_to_le16(cmd.len[0]);
755
756 /* set scan bit here for PAN params */
757 set_bit(STATUS_SCAN_HW, &priv->shrd->status);
758
759 ret = iwlagn_set_pan_params(priv);
760 if (ret)
761 return ret;
762
763 ret = iwl_trans_send_cmd(trans(priv), &cmd);
764 if (ret) {
765 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
766 iwlagn_set_pan_params(priv);
767 }
768
769 return ret;
770}
771
Tomas Winklerf53696d2008-06-12 09:47:12 +0800772void iwl_init_scan_params(struct iwl_priv *priv)
773{
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700774 u8 ant_idx = fls(hw_params(priv).valid_tx_ant) - 1;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800775 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700776 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800777 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700778 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800779}
780
Johannes Berg266af4c72011-03-10 20:13:26 -0800781int __must_check iwl_scan_initiate(struct iwl_priv *priv,
782 struct ieee80211_vif *vif,
783 enum iwl_scan_type scan_type,
784 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800785{
Johannes Berg3eecce52010-09-13 14:46:33 +0200786 int ret;
Johannes Berg88be0262010-04-07 00:21:36 -0700787
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700788 lockdep_assert_held(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800789
Johannes Berg3eecce52010-09-13 14:46:33 +0200790 cancel_delayed_work(&priv->scan_check);
791
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -0700792 if (!iwl_is_ready_rf(priv->shrd)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200793 IWL_WARN(priv, "Request scan called when driver not ready.\n");
Johannes Berg3eecce52010-09-13 14:46:33 +0200794 return -EIO;
795 }
796
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700797 if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200798 IWL_DEBUG_SCAN(priv,
Johannes Berg3eecce52010-09-13 14:46:33 +0200799 "Multiple concurrent scan requests in parallel.\n");
800 return -EBUSY;
801 }
802
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700803 if (test_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200804 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
Johannes Berg3eecce52010-09-13 14:46:33 +0200805 return -EBUSY;
806 }
807
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200808 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
Johannes Berg266af4c72011-03-10 20:13:26 -0800809 scan_type == IWL_SCAN_NORMAL ? "" :
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700810 scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
Johannes Berg266af4c72011-03-10 20:13:26 -0800811 "internal short ");
Johannes Berg3eecce52010-09-13 14:46:33 +0200812
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700813 set_bit(STATUS_SCANNING, &priv->shrd->status);
Johannes Berg266af4c72011-03-10 20:13:26 -0800814 priv->scan_type = scan_type;
Johannes Berg3eecce52010-09-13 14:46:33 +0200815 priv->scan_start = jiffies;
816 priv->scan_band = band;
817
Don Fry5c3d29f2011-07-08 08:46:29 -0700818 ret = iwlagn_request_scan(priv, vif);
Johannes Berg3eecce52010-09-13 14:46:33 +0200819 if (ret) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700820 clear_bit(STATUS_SCANNING, &priv->shrd->status);
Johannes Berg266af4c72011-03-10 20:13:26 -0800821 priv->scan_type = IWL_SCAN_NORMAL;
Johannes Berg3eecce52010-09-13 14:46:33 +0200822 return ret;
823 }
824
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700825 queue_delayed_work(priv->shrd->workqueue, &priv->scan_check,
Johannes Berg3eecce52010-09-13 14:46:33 +0200826 IWL_SCAN_CHECK_WATCHDOG);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800827
828 return 0;
829}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800830
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800831int iwl_mac_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +0200832 struct ieee80211_vif *vif,
833 struct cfg80211_scan_request *req)
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800834{
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800835 struct iwl_priv *priv = hw->priv;
Johannes Berg00700ee2010-04-06 04:12:37 -0700836 int ret;
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800837
838 IWL_DEBUG_MAC80211(priv, "enter\n");
839
Johannes Berg00700ee2010-04-06 04:12:37 -0700840 if (req->n_channels == 0)
841 return -EINVAL;
842
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700843 mutex_lock(&priv->shrd->mutex);
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800844
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700845 if (test_bit(STATUS_SCANNING, &priv->shrd->status) &&
Johannes Berg266af4c72011-03-10 20:13:26 -0800846 priv->scan_type != IWL_SCAN_NORMAL) {
Reinette Chatrebbcbb9e2010-02-02 10:57:12 -0800847 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
848 ret = -EAGAIN;
849 goto out_unlock;
850 }
851
Johannes Berg00700ee2010-04-06 04:12:37 -0700852 /* mac80211 will only ask for one band at a time */
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700853 priv->scan_request = req;
Johannes Bergf84b29e2010-05-18 02:29:13 -0700854 priv->scan_vif = vif;
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800855
Johannes Bergf84b29e2010-05-18 02:29:13 -0700856 /*
857 * If an internal scan is in progress, just set
858 * up the scan_request as per above.
859 */
Johannes Berg266af4c72011-03-10 20:13:26 -0800860 if (priv->scan_type != IWL_SCAN_NORMAL) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200861 IWL_DEBUG_SCAN(priv, "SCAN request during internal scan\n");
Johannes Bergf84b29e2010-05-18 02:29:13 -0700862 ret = 0;
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200863 } else
Johannes Berg266af4c72011-03-10 20:13:26 -0800864 ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
Johannes Berg3eecce52010-09-13 14:46:33 +0200865 req->channels[0]->band);
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800866
867 IWL_DEBUG_MAC80211(priv, "leave\n");
868
869out_unlock:
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700870 mutex_unlock(&priv->shrd->mutex);
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800871
872 return ret;
873}
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800874
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800875/*
876 * internal short scan, this function should only been called while associated.
877 * It will reset and tune the radio to prevent possible RF related problem
878 */
Johannes Berg88be0262010-04-07 00:21:36 -0700879void iwl_internal_short_hw_scan(struct iwl_priv *priv)
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800880{
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -0700881 queue_work(priv->shrd->workqueue, &priv->start_internal_scan);
Johannes Berg88be0262010-04-07 00:21:36 -0700882}
883
Stanislaw Gruszkac2408792010-07-30 16:41:08 +0200884static void iwl_bg_start_internal_scan(struct work_struct *work)
Johannes Berg88be0262010-04-07 00:21:36 -0700885{
886 struct iwl_priv *priv =
887 container_of(work, struct iwl_priv, start_internal_scan);
888
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200889 IWL_DEBUG_SCAN(priv, "Start internal scan\n");
890
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700891 mutex_lock(&priv->shrd->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800892
Johannes Berg266af4c72011-03-10 20:13:26 -0800893 if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
Reinette Chatre073d5ea2010-05-13 14:49:44 -0700894 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
895 goto unlock;
896 }
897
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700898 if (test_bit(STATUS_SCANNING, &priv->shrd->status)) {
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800899 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700900 goto unlock;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800901 }
Johannes Berg88be0262010-04-07 00:21:36 -0700902
Johannes Berg266af4c72011-03-10 20:13:26 -0800903 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
Johannes Berg3eecce52010-09-13 14:46:33 +0200904 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700905 unlock:
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700906 mutex_unlock(&priv->shrd->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800907}
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800908
Stanislaw Gruszkac2408792010-07-30 16:41:08 +0200909static void iwl_bg_scan_check(struct work_struct *data)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800910{
911 struct iwl_priv *priv =
912 container_of(data, struct iwl_priv, scan_check.work);
913
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200914 IWL_DEBUG_SCAN(priv, "Scan check work\n");
915
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200916 /* Since we are here firmware does not finish scan and
917 * most likely is in bad shape, so we don't bother to
918 * send abort command, just force scan complete to mac80211 */
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700919 mutex_lock(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200920 iwl_force_scan_end(priv);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700921 mutex_unlock(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800922}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800923
Tomas Winkler2a421b92008-06-12 09:47:10 +0800924/**
Tomas Winkler2a421b92008-06-12 09:47:10 +0800925 * iwl_fill_probe_req - fill in all required fields and IE for probe request
926 */
Tomas Winklerf53696d2008-06-12 09:47:12 +0800927
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700928u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -0700929 const u8 *ta, const u8 *ies, int ie_len, int left)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800930{
931 int len = 0;
932 u8 *pos = NULL;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800933
Tomas Winkler2a421b92008-06-12 09:47:10 +0800934 /* Make sure there is enough space for the probe request,
935 * two mandatory IEs and the data */
936 left -= 24;
937 if (left < 0)
938 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800939
940 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
941 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Johannes Berg3a0b9aa2010-05-12 03:33:12 -0700942 memcpy(frame->sa, ta, ETH_ALEN);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800943 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
944 frame->seq_ctrl = 0;
945
Tomas Winklerf53696d2008-06-12 09:47:12 +0800946 len += 24;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800947
Tomas Winklerf53696d2008-06-12 09:47:12 +0800948 /* ...next IE... */
949 pos = &frame->u.probe_req.variable[0];
950
951 /* fill in our indirect SSID IE */
Tomas Winkler2a421b92008-06-12 09:47:10 +0800952 left -= 2;
953 if (left < 0)
954 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800955 *pos++ = WLAN_EID_SSID;
Reinette Chatre1382c712010-02-25 10:02:19 -0800956 *pos++ = 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800957
Reinette Chatre1382c712010-02-25 10:02:19 -0800958 len += 2;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800959
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700960 if (WARN_ON(left < ie_len))
961 return len;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800962
Stanislaw Gruszkaeb2ec0f2010-04-16 15:46:41 +0200963 if (ies && ie_len) {
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800964 memcpy(pos, ies, ie_len);
Stanislaw Gruszkaeb2ec0f2010-04-16 15:46:41 +0200965 len += ie_len;
966 }
Tomas Winklerf53696d2008-06-12 09:47:12 +0800967
Tomas Winkler2a421b92008-06-12 09:47:10 +0800968 return (u16)len;
969}
970
Stanislaw Gruszkac2408792010-07-30 16:41:08 +0200971static void iwl_bg_abort_scan(struct work_struct *work)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800972{
973 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
974
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200975 IWL_DEBUG_SCAN(priv, "Abort scan work\n");
976
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +0200977 /* We keep scan_check work queued in case when firmware will not
978 * report back scan completed notification */
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700979 mutex_lock(&priv->shrd->mutex);
Stanislaw Gruszka6bd1758d2010-09-13 14:46:40 +0200980 iwl_scan_cancel_timeout(priv, 200);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700981 mutex_unlock(&priv->shrd->mutex);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800982}
983
Stanislaw Gruszkac2408792010-07-30 16:41:08 +0200984static void iwl_bg_scan_completed(struct work_struct *work)
Tomas Winklereedda362008-10-06 16:05:32 +0800985{
986 struct iwl_priv *priv =
987 container_of(work, struct iwl_priv, scan_completed);
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +0200988 bool aborted;
Tomas Winklereedda362008-10-06 16:05:32 +0800989
Johannes Berg266af4c72011-03-10 20:13:26 -0800990 IWL_DEBUG_SCAN(priv, "Completed scan.\n");
Tomas Winklereedda362008-10-06 16:05:32 +0800991
Reinette Chatrefbc9f972009-05-15 16:13:46 -0700992 cancel_delayed_work(&priv->scan_check);
993
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700994 mutex_lock(&priv->shrd->mutex);
Johannes Bergd5926d92010-09-13 14:46:34 +0200995
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700996 aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
Johannes Bergd5926d92010-09-13 14:46:34 +0200997 if (aborted)
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +0200998 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
Johannes Bergd5926d92010-09-13 14:46:34 +0200999
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -07001000 if (!test_and_clear_bit(STATUS_SCANNING, &priv->shrd->status)) {
Stanislaw Gruszka7cf24422010-09-13 14:46:44 +02001001 IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
Stanislaw Gruszka3a160a52010-09-13 14:46:45 +02001002 goto out_settings;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -08001003 }
Johannes Bergf84b29e2010-05-18 02:29:13 -07001004
Johannes Bergc6baf7f2011-07-23 10:24:47 -07001005 if (priv->scan_type == IWL_SCAN_ROC) {
1006 ieee80211_remain_on_channel_expired(priv->hw);
1007 priv->hw_roc_channel = NULL;
1008 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
Johannes Berg266af4c72011-03-10 20:13:26 -08001009 }
1010
1011 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +02001012 int err;
Johannes Bergf84b29e2010-05-18 02:29:13 -07001013
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +02001014 /* Check if mac80211 requested scan during our internal scan */
1015 if (priv->scan_request == NULL)
1016 goto out_complete;
1017
1018 /* If so request a new scan */
Johannes Berg266af4c72011-03-10 20:13:26 -08001019 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
Johannes Berg3eecce52010-09-13 14:46:33 +02001020 priv->scan_request->channels[0]->band);
Johannes Berg3eecce52010-09-13 14:46:33 +02001021 if (err) {
1022 IWL_DEBUG_SCAN(priv,
1023 "failed to initiate pending scan: %d\n", err);
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +02001024 aborted = true;
1025 goto out_complete;
1026 }
1027
1028 goto out;
Johannes Berg3eecce52010-09-13 14:46:33 +02001029 }
Johannes Bergf84b29e2010-05-18 02:29:13 -07001030
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +02001031out_complete:
1032 iwl_complete_scan(priv, aborted);
1033
Stanislaw Gruszka3a160a52010-09-13 14:46:45 +02001034out_settings:
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +02001035 /* Can we still talk to firmware ? */
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -07001036 if (!iwl_is_ready_rf(priv->shrd))
Stanislaw Gruszka02d8c142010-09-13 14:46:38 +02001037 goto out;
1038
Wey-Yi Guye80d70e2011-06-03 07:54:15 -07001039 iwlagn_post_scan(priv);
Johannes Berg52a02d12010-08-27 09:44:50 -07001040
Stanislaw Gruszkaa25a66a2010-10-22 17:04:26 +02001041out:
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001042 mutex_unlock(&priv->shrd->mutex);
Tomas Winklereedda362008-10-06 16:05:32 +08001043}
Tomas Winklereedda362008-10-06 16:05:32 +08001044
Tomas Winkler2a421b92008-06-12 09:47:10 +08001045void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1046{
Tomas Winklereedda362008-10-06 16:05:32 +08001047 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001048 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
Johannes Berg88be0262010-04-07 00:21:36 -07001049 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +08001050 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1051}
Tomas Winkler2a421b92008-06-12 09:47:10 +08001052
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001053void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1054{
1055 cancel_work_sync(&priv->start_internal_scan);
1056 cancel_work_sync(&priv->abort_scan);
1057 cancel_work_sync(&priv->scan_completed);
1058
1059 if (cancel_delayed_work_sync(&priv->scan_check)) {
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001060 mutex_lock(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001061 iwl_force_scan_end(priv);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001062 mutex_unlock(&priv->shrd->mutex);
Stanislaw Gruszkae7e16b92010-09-13 14:46:41 +02001063 }
1064}