blob: 3904bb45ca61a8d8feaf0d95e4db9cc5d81fcbad [file] [log] [blame]
Tomas Winkler2a421b92008-06-12 09:47:10 +08001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Reinette Chatre1f447802010-01-15 13:43:41 -08005 * Copyright(c) 2008 - 2010 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 *****************************************************************************/
John W. Linville7e272fc2008-09-24 18:13:14 -040028#include <linux/types.h>
Tomas Winkler2a421b92008-06-12 09:47:10 +080029#include <linux/etherdevice.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040030#include <net/mac80211.h>
Tomas Winkler2a421b92008-06-12 09:47:10 +080031
32#include "iwl-eeprom.h"
33#include "iwl-dev.h"
34#include "iwl-core.h"
35#include "iwl-sta.h"
36#include "iwl-io.h"
37#include "iwl-helpers.h"
38
39/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
40 * sending probe req. This should be set long enough to hear probe responses
41 * from more than one AP. */
Tomas Winklerfe905f12008-07-11 11:53:39 +080042#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
43#define IWL_ACTIVE_DWELL_TIME_52 (20)
44
45#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
46#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Tomas Winkler2a421b92008-06-12 09:47:10 +080047
Tomas Winkler2a421b92008-06-12 09:47:10 +080048/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
49 * Must be set longer than active dwell time.
50 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
51#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
52#define IWL_PASSIVE_DWELL_TIME_52 (10)
53#define IWL_PASSIVE_DWELL_BASE (100)
54#define IWL_CHANNEL_TUNE_TIME 5
55
Tomas Winklerfe905f12008-07-11 11:53:39 +080056
57
Tomas Winkler2a421b92008-06-12 09:47:10 +080058/**
59 * iwl_scan_cancel - Cancel any currently executing HW scan
60 *
61 * NOTE: priv->mutex is not required before calling this function
62 */
63int iwl_scan_cancel(struct iwl_priv *priv)
64{
65 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
66 clear_bit(STATUS_SCANNING, &priv->status);
67 return 0;
68 }
69
70 if (test_bit(STATUS_SCANNING, &priv->status)) {
71 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -080072 IWL_DEBUG_SCAN(priv, "Queuing scan abort.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +080073 set_bit(STATUS_SCAN_ABORTING, &priv->status);
74 queue_work(priv->workqueue, &priv->abort_scan);
75
76 } else
Tomas Winklere1623442009-01-27 14:27:56 -080077 IWL_DEBUG_SCAN(priv, "Scan abort already in progress.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +080078
79 return test_bit(STATUS_SCANNING, &priv->status);
80 }
81
82 return 0;
83}
84EXPORT_SYMBOL(iwl_scan_cancel);
85/**
86 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
87 * @ms: amount of time to wait (in milliseconds) for scan to abort
88 *
89 * NOTE: priv->mutex must be held before calling this function
90 */
91int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
92{
93 unsigned long now = jiffies;
94 int ret;
95
96 ret = iwl_scan_cancel(priv);
97 if (ret && ms) {
98 mutex_unlock(&priv->mutex);
99 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
100 test_bit(STATUS_SCANNING, &priv->status))
101 msleep(1);
102 mutex_lock(&priv->mutex);
103
104 return test_bit(STATUS_SCANNING, &priv->status);
105 }
106
107 return ret;
108}
109EXPORT_SYMBOL(iwl_scan_cancel_timeout);
110
Johannes Berg4e05c232009-06-19 13:52:44 -0700111static int iwl_send_scan_abort(struct iwl_priv *priv)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800112{
113 int ret = 0;
Zhu Yi2f301222009-10-09 17:19:45 +0800114 struct iwl_rx_packet *pkt;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800115 struct iwl_host_cmd cmd = {
116 .id = REPLY_SCAN_ABORT_CMD,
Johannes Bergc2acea82009-07-24 11:13:05 -0700117 .flags = CMD_WANT_SKB,
Tomas Winkler2a421b92008-06-12 09:47:10 +0800118 };
119
120 /* If there isn't a scan actively going on in the hardware
121 * then we are in between scan bands and not actually
122 * actively scanning, so don't send the abort command */
123 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
124 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
125 return 0;
126 }
127
128 ret = iwl_send_cmd_sync(priv, &cmd);
129 if (ret) {
130 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
131 return ret;
132 }
133
Zhu Yi2f301222009-10-09 17:19:45 +0800134 pkt = (struct iwl_rx_packet *)cmd.reply_page;
135 if (pkt->u.status != CAN_ABORT_STATUS) {
Tomas Winkler2a421b92008-06-12 09:47:10 +0800136 /* The scan abort will return 1 for success or
137 * 2 for "failure". A failure condition can be
138 * due to simply not being in an active scan which
139 * can occur if we send the scan abort before we
140 * the microcode has notified us that a scan is
141 * completed. */
Zhu Yi2f301222009-10-09 17:19:45 +0800142 IWL_DEBUG_INFO(priv, "SCAN_ABORT returned %d.\n", pkt->u.status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800143 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
144 clear_bit(STATUS_SCAN_HW, &priv->status);
145 }
146
Zhu Yi64a76b52009-12-10 14:37:21 -0800147 iwl_free_pages(priv, cmd.reply_page);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800148
149 return ret;
150}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800151
152/* Service response to REPLY_SCAN_CMD (0x80) */
153static void iwl_rx_reply_scan(struct iwl_priv *priv,
154 struct iwl_rx_mem_buffer *rxb)
155{
156#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800157 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800158 struct iwl_scanreq_notification *notif =
159 (struct iwl_scanreq_notification *)pkt->u.raw;
160
Tomas Winklere1623442009-01-27 14:27:56 -0800161 IWL_DEBUG_RX(priv, "Scan request status = 0x%x\n", notif->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800162#endif
163}
164
165/* Service SCAN_START_NOTIFICATION (0x82) */
166static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
167 struct iwl_rx_mem_buffer *rxb)
168{
Zhu Yi2f301222009-10-09 17:19:45 +0800169 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800170 struct iwl_scanstart_notification *notif =
171 (struct iwl_scanstart_notification *)pkt->u.raw;
172 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
Tomas Winklere1623442009-01-27 14:27:56 -0800173 IWL_DEBUG_SCAN(priv, "Scan start: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800174 "%d [802.11%s] "
175 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
176 notif->channel,
177 notif->band ? "bg" : "a",
Tomas Winklerfe905f12008-07-11 11:53:39 +0800178 le32_to_cpu(notif->tsf_high),
179 le32_to_cpu(notif->tsf_low),
180 notif->status, notif->beacon_timer);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800181}
182
183/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
184static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
185 struct iwl_rx_mem_buffer *rxb)
186{
187#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800188 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800189 struct iwl_scanresults_notification *notif =
190 (struct iwl_scanresults_notification *)pkt->u.raw;
191
Tomas Winklere1623442009-01-27 14:27:56 -0800192 IWL_DEBUG_SCAN(priv, "Scan ch.res: "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800193 "%d [802.11%s] "
194 "(TSF: 0x%08X:%08X) - %d "
Henry Zhangh220575f2010-01-22 14:22:44 -0800195 "elapsed=%lu usec\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800196 notif->channel,
197 notif->band ? "bg" : "a",
198 le32_to_cpu(notif->tsf_high),
199 le32_to_cpu(notif->tsf_low),
200 le32_to_cpu(notif->statistics[0]),
Henry Zhangh220575f2010-01-22 14:22:44 -0800201 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800202#endif
203
Wey-Yi Guyd4d59e82010-01-22 14:22:45 -0800204 if (!priv->is_internal_short_scan)
205 priv->next_scan_jiffies = 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800206}
207
208/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
209static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
210 struct iwl_rx_mem_buffer *rxb)
211{
Denis V. Lunevcb9289c2008-07-18 10:56:12 +0400212#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yi2f301222009-10-09 17:19:45 +0800213 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800214 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
215
Tomas Winklere1623442009-01-27 14:27:56 -0800216 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800217 scan_notif->scanned_channels,
218 scan_notif->tsf_low,
219 scan_notif->tsf_high, scan_notif->status);
Denis V. Lunevcb9289c2008-07-18 10:56:12 +0400220#endif
Tomas Winkler2a421b92008-06-12 09:47:10 +0800221
222 /* The HW is no longer scanning */
223 clear_bit(STATUS_SCAN_HW, &priv->status);
224
Tomas Winklere1623442009-01-27 14:27:56 -0800225 IWL_DEBUG_INFO(priv, "Scan pass on %sGHz took %dms\n",
Johannes Berg00700ee2010-04-06 04:12:37 -0700226 (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800227 jiffies_to_msecs(elapsed_jiffies
228 (priv->scan_pass_start, jiffies)));
229
Tomas Winkler2a421b92008-06-12 09:47:10 +0800230 /* If a request to abort was given, or the scan did not succeed
231 * then we reset the scan state machine and terminate,
232 * re-queuing another scan if one has been requested */
233 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800234 IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800235 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800236 }
237
Wey-Yi Guyd4d59e82010-01-22 14:22:45 -0800238 if (!priv->is_internal_short_scan)
239 priv->next_scan_jiffies = 0;
Wey-Yi Guyd4d59e82010-01-22 14:22:45 -0800240
Tomas Winklere1623442009-01-27 14:27:56 -0800241 IWL_DEBUG_INFO(priv, "Setting scan to off\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800242
243 clear_bit(STATUS_SCANNING, &priv->status);
244
Tomas Winklere1623442009-01-27 14:27:56 -0800245 IWL_DEBUG_INFO(priv, "Scan took %dms\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800246 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
247
248 queue_work(priv->workqueue, &priv->scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800249}
250
251void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
252{
253 /* scan handlers */
254 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
255 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
256 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
257 iwl_rx_scan_results_notif;
258 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
259 iwl_rx_scan_complete_notif;
260}
261EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
262
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800263inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
264 enum ieee80211_band band,
265 u8 n_probes)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800266{
267 if (band == IEEE80211_BAND_5GHZ)
Tomas Winklerfe905f12008-07-11 11:53:39 +0800268 return IWL_ACTIVE_DWELL_TIME_52 +
269 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800270 else
Tomas Winklerfe905f12008-07-11 11:53:39 +0800271 return IWL_ACTIVE_DWELL_TIME_24 +
272 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800273}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800274EXPORT_SYMBOL(iwl_get_active_dwell_time);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800275
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800276u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
277 enum ieee80211_band band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800278{
Tomas Winklerfe905f12008-07-11 11:53:39 +0800279 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Tomas Winkler2a421b92008-06-12 09:47:10 +0800280 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
281 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
282
283 if (iwl_is_associated(priv)) {
284 /* If we're associated, we clamp the maximum passive
285 * dwell time to be 98% of the beacon interval (minus
286 * 2 * channel tune time) */
287 passive = priv->beacon_int;
288 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
289 passive = IWL_PASSIVE_DWELL_BASE;
290 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
291 }
292
Tomas Winkler2a421b92008-06-12 09:47:10 +0800293 return passive;
294}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800295EXPORT_SYMBOL(iwl_get_passive_dwell_time);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800296
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800297static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
298 enum ieee80211_band band,
299 struct iwl_scan_channel *scan_ch)
300{
301 const struct ieee80211_supported_band *sband;
302 const struct iwl_channel_info *ch_info;
303 u16 passive_dwell = 0;
304 u16 active_dwell = 0;
305 int i, added = 0;
306 u16 channel = 0;
307
308 sband = iwl_get_hw_mode(priv, band);
309 if (!sband) {
310 IWL_ERR(priv, "invalid band\n");
311 return added;
312 }
313
314 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
315 passive_dwell = iwl_get_passive_dwell_time(priv, band);
316
317 if (passive_dwell <= active_dwell)
318 passive_dwell = active_dwell + 1;
319
320 /* only scan single channel, good enough to reset the RF */
321 /* pick the first valid not in-use channel */
322 if (band == IEEE80211_BAND_5GHZ) {
323 for (i = 14; i < priv->channel_count; i++) {
324 if (priv->channel_info[i].channel !=
325 le16_to_cpu(priv->staging_rxon.channel)) {
326 channel = priv->channel_info[i].channel;
327 ch_info = iwl_get_channel_info(priv,
328 band, channel);
329 if (is_channel_valid(ch_info))
330 break;
331 }
332 }
333 } else {
334 for (i = 0; i < 14; i++) {
335 if (priv->channel_info[i].channel !=
336 le16_to_cpu(priv->staging_rxon.channel)) {
337 channel =
338 priv->channel_info[i].channel;
339 ch_info = iwl_get_channel_info(priv,
340 band, channel);
341 if (is_channel_valid(ch_info))
342 break;
343 }
344 }
345 }
346 if (channel) {
347 scan_ch->channel = cpu_to_le16(channel);
348 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
349 scan_ch->active_dwell = cpu_to_le16(active_dwell);
350 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
351 /* Set txpower levels to defaults */
352 scan_ch->dsp_atten = 110;
353 if (band == IEEE80211_BAND_5GHZ)
354 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
355 else
356 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
357 added++;
358 } else
359 IWL_ERR(priv, "no valid channel found\n");
360 return added;
361}
362
Tomas Winkler2a421b92008-06-12 09:47:10 +0800363static int iwl_get_channels_for_scan(struct iwl_priv *priv,
364 enum ieee80211_band band,
Tomas Winklerfe905f12008-07-11 11:53:39 +0800365 u8 is_active, u8 n_probes,
Tomas Winkler2a421b92008-06-12 09:47:10 +0800366 struct iwl_scan_channel *scan_ch)
367{
Johannes Berg4e05c232009-06-19 13:52:44 -0700368 struct ieee80211_channel *chan;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800369 const struct ieee80211_supported_band *sband;
370 const struct iwl_channel_info *ch_info;
371 u16 passive_dwell = 0;
372 u16 active_dwell = 0;
373 int added, i;
Tomas Winklerd16dc482008-07-11 11:53:38 +0800374 u16 channel;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800375
376 sband = iwl_get_hw_mode(priv, band);
377 if (!sband)
378 return 0;
379
Tomas Winklerfe905f12008-07-11 11:53:39 +0800380 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800381 passive_dwell = iwl_get_passive_dwell_time(priv, band);
382
Tomas Winklerfe905f12008-07-11 11:53:39 +0800383 if (passive_dwell <= active_dwell)
384 passive_dwell = active_dwell + 1;
385
Johannes Berg4e05c232009-06-19 13:52:44 -0700386 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
387 chan = priv->scan_request->channels[i];
388
389 if (chan->band != band)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800390 continue;
391
Johannes Berg4e05c232009-06-19 13:52:44 -0700392 channel = ieee80211_frequency_to_channel(chan->center_freq);
Tomas Winklerd16dc482008-07-11 11:53:38 +0800393 scan_ch->channel = cpu_to_le16(channel);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800394
Tomas Winklerd16dc482008-07-11 11:53:38 +0800395 ch_info = iwl_get_channel_info(priv, band, channel);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800396 if (!is_channel_valid(ch_info)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800397 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
Tomas Winklerd16dc482008-07-11 11:53:38 +0800398 channel);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800399 continue;
400 }
401
402 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg4e05c232009-06-19 13:52:44 -0700403 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
Tomas Winklerd16dc482008-07-11 11:53:38 +0800404 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800405 else
Tomas Winklerd16dc482008-07-11 11:53:38 +0800406 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800407
Ron Rindjunsky0ffe0142008-09-03 11:18:43 +0800408 if (n_probes)
Tomas Winklerfe905f12008-07-11 11:53:39 +0800409 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800410
411 scan_ch->active_dwell = cpu_to_le16(active_dwell);
412 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
413
414 /* Set txpower levels to defaults */
Tomas Winklerf53696d2008-06-12 09:47:12 +0800415 scan_ch->dsp_atten = 110;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800416
Tomas Winklerfe905f12008-07-11 11:53:39 +0800417 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
418 * power level:
419 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
420 */
Tomas Winkler2a421b92008-06-12 09:47:10 +0800421 if (band == IEEE80211_BAND_5GHZ)
Tomas Winklerf53696d2008-06-12 09:47:12 +0800422 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
Tomas Winklerfe905f12008-07-11 11:53:39 +0800423 else
Tomas Winklerf53696d2008-06-12 09:47:12 +0800424 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
Tomas Winkler2a421b92008-06-12 09:47:10 +0800425
Tomas Winklere1623442009-01-27 14:27:56 -0800426 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
Tomas Winklerfe905f12008-07-11 11:53:39 +0800427 channel, le32_to_cpu(scan_ch->type),
Tomas Winklerd16dc482008-07-11 11:53:38 +0800428 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
429 "ACTIVE" : "PASSIVE",
430 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
Tomas Winkler2a421b92008-06-12 09:47:10 +0800431 active_dwell : passive_dwell);
432
433 scan_ch++;
434 added++;
435 }
436
Frans Pop91dd6c22010-03-24 14:19:58 -0700437 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800438 return added;
439}
440
Tomas Winklerf53696d2008-06-12 09:47:12 +0800441void iwl_init_scan_params(struct iwl_priv *priv)
442{
Tomas Winkler76eff182008-10-14 12:32:45 -0700443 u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800444 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700445 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800446 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
Tomas Winkler76eff182008-10-14 12:32:45 -0700447 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800448}
Reinette Chatre89f186a2009-10-30 14:36:11 -0700449EXPORT_SYMBOL(iwl_init_scan_params);
Tomas Winklerf53696d2008-06-12 09:47:12 +0800450
Johannes Berg4e05c232009-06-19 13:52:44 -0700451static int iwl_scan_initiate(struct iwl_priv *priv)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800452{
Johannes Berg88be0262010-04-07 00:21:36 -0700453 WARN_ON(!mutex_is_locked(&priv->mutex));
454
Tomas Winklere1623442009-01-27 14:27:56 -0800455 IWL_DEBUG_INFO(priv, "Starting scan...\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800456 set_bit(STATUS_SCANNING, &priv->status);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800457 priv->is_internal_short_scan = false;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800458 priv->scan_start = jiffies;
459 priv->scan_pass_start = priv->scan_start;
460
461 queue_work(priv->workqueue, &priv->request_scan);
462
463 return 0;
464}
Tomas Winkler2a421b92008-06-12 09:47:10 +0800465
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800466#define IWL_DELAY_NEXT_SCAN (HZ*2)
467
468int iwl_mac_hw_scan(struct ieee80211_hw *hw,
469 struct cfg80211_scan_request *req)
470{
471 unsigned long flags;
472 struct iwl_priv *priv = hw->priv;
Johannes Berg00700ee2010-04-06 04:12:37 -0700473 int ret;
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800474
475 IWL_DEBUG_MAC80211(priv, "enter\n");
476
Johannes Berg00700ee2010-04-06 04:12:37 -0700477 if (req->n_channels == 0)
478 return -EINVAL;
479
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800480 mutex_lock(&priv->mutex);
481 spin_lock_irqsave(&priv->lock, flags);
482
483 if (!iwl_is_ready_rf(priv)) {
484 ret = -EIO;
485 IWL_DEBUG_MAC80211(priv, "leave - not ready or exit pending\n");
486 goto out_unlock;
487 }
488
Reinette Chatrebbcbb9e2010-02-02 10:57:12 -0800489 if (test_bit(STATUS_SCANNING, &priv->status)) {
490 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
491 ret = -EAGAIN;
492 goto out_unlock;
493 }
494
495 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
496 IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
497 ret = -EAGAIN;
498 goto out_unlock;
499 }
500
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800501 /* We don't schedule scan within next_scan_jiffies period.
502 * Avoid scanning during possible EAPOL exchange, return
503 * success immediately.
504 */
505 if (priv->next_scan_jiffies &&
506 time_after(priv->next_scan_jiffies, jiffies)) {
507 IWL_DEBUG_SCAN(priv, "scan rejected: within next scan period\n");
508 queue_work(priv->workqueue, &priv->scan_completed);
509 ret = 0;
510 goto out_unlock;
511 }
512
Johannes Berg00700ee2010-04-06 04:12:37 -0700513 /* mac80211 will only ask for one band at a time */
514 priv->scan_band = req->channels[0]->band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700515 priv->scan_request = req;
Abhijeet Kolekare9dde6f62009-02-18 15:54:27 -0800516
517 ret = iwl_scan_initiate(priv);
518
519 IWL_DEBUG_MAC80211(priv, "leave\n");
520
521out_unlock:
522 spin_unlock_irqrestore(&priv->lock, flags);
523 mutex_unlock(&priv->mutex);
524
525 return ret;
526}
527EXPORT_SYMBOL(iwl_mac_hw_scan);
528
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800529/*
530 * internal short scan, this function should only been called while associated.
531 * It will reset and tune the radio to prevent possible RF related problem
532 */
Johannes Berg88be0262010-04-07 00:21:36 -0700533void iwl_internal_short_hw_scan(struct iwl_priv *priv)
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800534{
Johannes Berg88be0262010-04-07 00:21:36 -0700535 queue_work(priv->workqueue, &priv->start_internal_scan);
536}
537
538static void iwl_bg_start_internal_scan(struct work_struct *work)
539{
540 struct iwl_priv *priv =
541 container_of(work, struct iwl_priv, start_internal_scan);
542
543 mutex_lock(&priv->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800544
545 if (!iwl_is_ready_rf(priv)) {
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800546 IWL_DEBUG_SCAN(priv, "not ready or exit pending\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700547 goto unlock;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800548 }
Johannes Berg88be0262010-04-07 00:21:36 -0700549
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800550 if (test_bit(STATUS_SCANNING, &priv->status)) {
551 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700552 goto unlock;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800553 }
Johannes Berg88be0262010-04-07 00:21:36 -0700554
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800555 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
556 IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
Johannes Berg88be0262010-04-07 00:21:36 -0700557 goto unlock;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800558 }
Wey-Yi Guyd4d59e82010-01-22 14:22:45 -0800559
Johannes Berg00700ee2010-04-06 04:12:37 -0700560 priv->scan_band = priv->band;
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800561
562 IWL_DEBUG_SCAN(priv, "Start internal short scan...\n");
563 set_bit(STATUS_SCANNING, &priv->status);
564 priv->is_internal_short_scan = true;
565 queue_work(priv->workqueue, &priv->request_scan);
Johannes Berg88be0262010-04-07 00:21:36 -0700566 unlock:
567 mutex_unlock(&priv->mutex);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800568}
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800569
Tomas Winkler2a421b92008-06-12 09:47:10 +0800570#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
571
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800572void iwl_bg_scan_check(struct work_struct *data)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800573{
574 struct iwl_priv *priv =
575 container_of(data, struct iwl_priv, scan_check.work);
576
577 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
578 return;
579
580 mutex_lock(&priv->mutex);
581 if (test_bit(STATUS_SCANNING, &priv->status) ||
582 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800583 IWL_DEBUG_SCAN(priv, "Scan completion watchdog resetting "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800584 "adapter (%dms)\n",
585 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
586
587 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
588 iwl_send_scan_abort(priv);
589 }
590 mutex_unlock(&priv->mutex);
591}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800592EXPORT_SYMBOL(iwl_bg_scan_check);
593
Tomas Winkler2a421b92008-06-12 09:47:10 +0800594/**
Tomas Winkler2a421b92008-06-12 09:47:10 +0800595 * iwl_fill_probe_req - fill in all required fields and IE for probe request
596 */
Tomas Winklerf53696d2008-06-12 09:47:12 +0800597
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700598u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
599 const u8 *ies, int ie_len, int left)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800600{
601 int len = 0;
602 u8 *pos = NULL;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800603
Tomas Winkler2a421b92008-06-12 09:47:10 +0800604 /* Make sure there is enough space for the probe request,
605 * two mandatory IEs and the data */
606 left -= 24;
607 if (left < 0)
608 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800609
610 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
611 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
612 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
613 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
614 frame->seq_ctrl = 0;
615
Tomas Winklerf53696d2008-06-12 09:47:12 +0800616 len += 24;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800617
Tomas Winklerf53696d2008-06-12 09:47:12 +0800618 /* ...next IE... */
619 pos = &frame->u.probe_req.variable[0];
620
621 /* fill in our indirect SSID IE */
Tomas Winkler2a421b92008-06-12 09:47:10 +0800622 left -= 2;
623 if (left < 0)
624 return 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800625 *pos++ = WLAN_EID_SSID;
Reinette Chatre1382c712010-02-25 10:02:19 -0800626 *pos++ = 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800627
Reinette Chatre1382c712010-02-25 10:02:19 -0800628 len += 2;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800629
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700630 if (WARN_ON(left < ie_len))
631 return len;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800632
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800633 if (ies)
634 memcpy(pos, ies, ie_len);
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700635 len += ie_len;
636 left -= ie_len;
Tomas Winklerf53696d2008-06-12 09:47:12 +0800637
Tomas Winkler2a421b92008-06-12 09:47:10 +0800638 return (u16)len;
639}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800640EXPORT_SYMBOL(iwl_fill_probe_req);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800641
642static void iwl_bg_request_scan(struct work_struct *data)
643{
644 struct iwl_priv *priv =
645 container_of(data, struct iwl_priv, request_scan);
646 struct iwl_host_cmd cmd = {
647 .id = REPLY_SCAN_CMD,
648 .len = sizeof(struct iwl_scan_cmd),
Johannes Bergc2acea82009-07-24 11:13:05 -0700649 .flags = CMD_SIZE_HUGE,
Tomas Winkler2a421b92008-06-12 09:47:10 +0800650 };
651 struct iwl_scan_cmd *scan;
652 struct ieee80211_conf *conf = NULL;
Tomas Winkler76eff182008-10-14 12:32:45 -0700653 u32 rate_flags = 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800654 u16 cmd_len;
Wey-Yi Guyfff7a432009-05-08 13:44:42 -0700655 u16 rx_chain = 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800656 enum ieee80211_band band;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700657 u8 n_probes = 0;
Wey-Yi Guyfff7a432009-05-08 13:44:42 -0700658 u8 rx_ant = priv->hw_params.valid_rx_ant;
Tomas Winkler76eff182008-10-14 12:32:45 -0700659 u8 rate;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700660 bool is_active = false;
Wey-Yi Guya2b0f022009-05-22 11:01:49 -0700661 int chan_mod;
Shanyu Zhaof1e3d7d2009-11-06 14:52:47 -0800662 u8 active_chains;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800663
664 conf = ieee80211_get_hw_conf(priv->hw);
665
666 mutex_lock(&priv->mutex);
667
Reinette Chatrefbc9f972009-05-15 16:13:46 -0700668 cancel_delayed_work(&priv->scan_check);
669
Tomas Winkler2a421b92008-06-12 09:47:10 +0800670 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800671 IWL_WARN(priv, "request scan called when driver not ready.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800672 goto done;
673 }
674
Tomas Winklera96a27f2008-10-23 23:48:56 -0700675 /* Make sure the scan wasn't canceled before this queued work
Tomas Winkler2a421b92008-06-12 09:47:10 +0800676 * was given the chance to run... */
677 if (!test_bit(STATUS_SCANNING, &priv->status))
678 goto done;
679
680 /* This should never be called or scheduled if there is currently
681 * a scan active in the hardware. */
682 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800683 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
Tomas Winkler2a421b92008-06-12 09:47:10 +0800684 "Ignoring second request.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800685 goto done;
686 }
687
688 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800689 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800690 goto done;
691 }
692
693 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800694 IWL_DEBUG_HC(priv, "Scan request while abort pending. Queuing.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800695 goto done;
696 }
697
698 if (iwl_is_rfkill(priv)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800699 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800700 goto done;
701 }
702
703 if (!test_bit(STATUS_READY, &priv->status)) {
Tomas Winklere1623442009-01-27 14:27:56 -0800704 IWL_DEBUG_HC(priv, "Scan request while uninitialized. Queuing.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800705 goto done;
706 }
707
Tomas Winkler2a421b92008-06-12 09:47:10 +0800708 if (!priv->scan) {
709 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
710 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
711 if (!priv->scan) {
Wey-Yi Guy13115ba2010-02-23 14:36:13 -0800712 IWL_DEBUG_SCAN(priv,
713 "fail to allocate memory for scan\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800714 goto done;
715 }
716 }
717 scan = priv->scan;
718 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
719
720 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
721 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
722
723 if (iwl_is_associated(priv)) {
724 u16 interval = 0;
725 u32 extra;
726 u32 suspend_time = 100;
727 u32 scan_suspend_time = 100;
728 unsigned long flags;
729
Tomas Winklere1623442009-01-27 14:27:56 -0800730 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800731 spin_lock_irqsave(&priv->lock, flags);
732 interval = priv->beacon_int;
733 spin_unlock_irqrestore(&priv->lock, flags);
734
735 scan->suspend_time = 0;
736 scan->max_out_time = cpu_to_le32(200 * 1024);
737 if (!interval)
738 interval = suspend_time;
739
740 extra = (suspend_time / interval) << 22;
741 scan_suspend_time = (extra |
742 ((suspend_time % interval) * 1024));
743 scan->suspend_time = cpu_to_le32(scan_suspend_time);
Tomas Winklere1623442009-01-27 14:27:56 -0800744 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
Tomas Winkler2a421b92008-06-12 09:47:10 +0800745 scan_suspend_time, interval);
746 }
747
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800748 if (priv->is_internal_short_scan) {
749 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
750 } else if (priv->scan_request->n_ssids) {
Reinette Chatre1382c712010-02-25 10:02:19 -0800751 int i, p = 0;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700752 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
Reinette Chatre1382c712010-02-25 10:02:19 -0800753 for (i = 0; i < priv->scan_request->n_ssids; i++) {
754 /* always does wildcard anyway */
755 if (!priv->scan_request->ssids[i].ssid_len)
756 continue;
757 scan->direct_scan[p].id = WLAN_EID_SSID;
758 scan->direct_scan[p].len =
759 priv->scan_request->ssids[i].ssid_len;
760 memcpy(scan->direct_scan[p].ssid,
761 priv->scan_request->ssids[i].ssid,
762 priv->scan_request->ssids[i].ssid_len);
763 n_probes++;
764 p++;
Johannes Berg1ecf9fc2009-04-20 14:36:56 -0700765 }
766 is_active = true;
767 } else
768 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800769
770 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
771 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
772 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
773
Johannes Berg00700ee2010-04-06 04:12:37 -0700774 switch (priv->scan_band) {
775 case IEEE80211_BAND_2GHZ:
Tomas Winklerf53696d2008-06-12 09:47:12 +0800776 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Wey-Yi Guya2b0f022009-05-22 11:01:49 -0700777 chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
778 >> RXON_FLG_CHANNEL_MODE_POS;
779 if (chan_mod == CHANNEL_MODE_PURE_40) {
Tomas Winkler76eff182008-10-14 12:32:45 -0700780 rate = IWL_RATE_6M_PLCP;
781 } else {
782 rate = IWL_RATE_1M_PLCP;
783 rate_flags = RATE_MCS_CCK_MSK;
784 }
Tomas Winklerf53696d2008-06-12 09:47:12 +0800785 scan->good_CRC_th = 0;
Johannes Berg00700ee2010-04-06 04:12:37 -0700786 break;
787 case IEEE80211_BAND_5GHZ:
Tomas Winkler76eff182008-10-14 12:32:45 -0700788 rate = IWL_RATE_6M_PLCP;
Johannes Bergb097ad22009-04-20 14:36:57 -0700789 /*
790 * If active scaning is requested but a certain channel
791 * is marked passive, we can do active scanning if we
792 * detect transmissions.
793 */
794 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
Tomas Winkler2a421b92008-06-12 09:47:10 +0800795
Wey-Yi Guyd28667f2010-03-19 11:54:49 -0700796 /* Force use of chains B and C (0x6) for scan Rx
797 * Avoid A (0x1) for the device has off-channel reception
798 * on A-band.
Tomas Winkler51183032008-10-06 16:05:30 +0800799 */
Wey-Yi Guyd28667f2010-03-19 11:54:49 -0700800 if (priv->cfg->off_channel_workaround)
Wey-Yi Guyfff7a432009-05-08 13:44:42 -0700801 rx_ant = ANT_BC;
Johannes Berg00700ee2010-04-06 04:12:37 -0700802 break;
803 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800804 IWL_WARN(priv, "Invalid scan band count\n");
Tomas Winkler2a421b92008-06-12 09:47:10 +0800805 goto done;
806 }
807
Johannes Berg00700ee2010-04-06 04:12:37 -0700808 band = priv->scan_band;
809
Tomas Winkler76eff182008-10-14 12:32:45 -0700810 priv->scan_tx_ant[band] =
Johannes Berg00700ee2010-04-06 04:12:37 -0700811 iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
Tomas Winkler76eff182008-10-14 12:32:45 -0700812 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
813 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
814
Shanyu Zhaof1e3d7d2009-11-06 14:52:47 -0800815 /* In power save mode use one chain, otherwise use all chains */
816 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
817 /* rx_ant has been set to all valid chains previously */
818 active_chains = rx_ant &
819 ((u8)(priv->chain_noise_data.active_chains));
820 if (!active_chains)
821 active_chains = rx_ant;
822
823 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
824 priv->chain_noise_data.active_chains);
825
826 rx_ant = first_antenna(active_chains);
827 }
Tomas Winkler51183032008-10-06 16:05:30 +0800828 /* MIMO is not used here, but value is required */
Shanyu Zhaob23aa882009-11-06 14:52:48 -0800829 rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
Shanyu Zhaof1e3d7d2009-11-06 14:52:47 -0800830 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
Wey-Yi Guyfff7a432009-05-08 13:44:42 -0700831 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
832 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
833 scan->rx_chain = cpu_to_le16(rx_chain);
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800834 if (!priv->is_internal_short_scan) {
835 cmd_len = iwl_fill_probe_req(priv,
836 (struct ieee80211_mgmt *)scan->data,
837 priv->scan_request->ie,
838 priv->scan_request->ie_len,
839 IWL_MAX_SCAN_SIZE - sizeof(*scan));
840 } else {
841 cmd_len = iwl_fill_probe_req(priv,
842 (struct ieee80211_mgmt *)scan->data,
843 NULL, 0,
844 IWL_MAX_SCAN_SIZE - sizeof(*scan));
Tomas Winkler2a421b92008-06-12 09:47:10 +0800845
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800846 }
Tomas Winkler2a421b92008-06-12 09:47:10 +0800847 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Wey-Yi Guy279b05d2009-04-20 14:37:00 -0700848 if (iwl_is_monitor_mode(priv))
Tomas Winkler2a421b92008-06-12 09:47:10 +0800849 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
850
Tomas Winklerf53696d2008-06-12 09:47:12 +0800851 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
852 RXON_FILTER_BCON_AWARE_MSK);
853
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800854 if (priv->is_internal_short_scan) {
855 scan->channel_count =
856 iwl_get_single_channel_for_scan(priv, band,
857 (void *)&scan->data[le16_to_cpu(
858 scan->tx_cmd.len)]);
859 } else {
860 scan->channel_count =
861 iwl_get_channels_for_scan(priv, band,
862 is_active, n_probes,
863 (void *)&scan->data[le16_to_cpu(
864 scan->tx_cmd.len)]);
865 }
Tomas Winklerf53696d2008-06-12 09:47:12 +0800866 if (scan->channel_count == 0) {
Tomas Winklere1623442009-01-27 14:27:56 -0800867 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Tomas Winklerf53696d2008-06-12 09:47:12 +0800868 goto done;
869 }
Tomas Winkler2a421b92008-06-12 09:47:10 +0800870
Tomas Winkler2a421b92008-06-12 09:47:10 +0800871 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
872 scan->channel_count * sizeof(struct iwl_scan_channel);
873 cmd.data = scan;
874 scan->len = cpu_to_le16(cmd.len);
875
876 set_bit(STATUS_SCAN_HW, &priv->status);
Wey-Yi Guy13115ba2010-02-23 14:36:13 -0800877 if (iwl_send_cmd_sync(priv, &cmd))
Tomas Winkler2a421b92008-06-12 09:47:10 +0800878 goto done;
879
880 queue_delayed_work(priv->workqueue, &priv->scan_check,
881 IWL_SCAN_CHECK_WATCHDOG);
882
883 mutex_unlock(&priv->mutex);
884 return;
885
886 done:
Mohamed Abbas951891c2008-10-23 23:48:54 -0700887 /* Cannot perform scan. Make sure we clear scanning
888 * bits from status so next scan request can be performed.
889 * If we don't clear scanning status bit here all next scan
890 * will fail
891 */
892 clear_bit(STATUS_SCAN_HW, &priv->status);
893 clear_bit(STATUS_SCANNING, &priv->status);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800894 /* inform mac80211 scan aborted */
895 queue_work(priv->workqueue, &priv->scan_completed);
896 mutex_unlock(&priv->mutex);
897}
898
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800899void iwl_bg_abort_scan(struct work_struct *work)
Tomas Winkler2a421b92008-06-12 09:47:10 +0800900{
901 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
902
Wey-Yi Guy5bddf542009-08-21 13:34:25 -0700903 if (!test_bit(STATUS_READY, &priv->status) ||
904 !test_bit(STATUS_GEO_CONFIGURED, &priv->status))
Tomas Winkler2a421b92008-06-12 09:47:10 +0800905 return;
906
907 mutex_lock(&priv->mutex);
908
909 set_bit(STATUS_SCAN_ABORTING, &priv->status);
910 iwl_send_scan_abort(priv);
911
912 mutex_unlock(&priv->mutex);
913}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800914EXPORT_SYMBOL(iwl_bg_abort_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800915
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800916void iwl_bg_scan_completed(struct work_struct *work)
Tomas Winklereedda362008-10-06 16:05:32 +0800917{
918 struct iwl_priv *priv =
919 container_of(work, struct iwl_priv, scan_completed);
920
Tomas Winklere1623442009-01-27 14:27:56 -0800921 IWL_DEBUG_SCAN(priv, "SCAN complete scan\n");
Tomas Winklereedda362008-10-06 16:05:32 +0800922
Reinette Chatrefbc9f972009-05-15 16:13:46 -0700923 cancel_delayed_work(&priv->scan_check);
924
Wey-Yi Guyafbdd692010-01-22 14:22:43 -0800925 if (!priv->is_internal_short_scan)
926 ieee80211_scan_completed(priv->hw, false);
927 else {
928 priv->is_internal_short_scan = false;
929 IWL_DEBUG_SCAN(priv, "internal short scan completed\n");
930 }
Johannes Berg74aa9be2009-04-23 10:45:04 +0200931
Tomas Winklereedda362008-10-06 16:05:32 +0800932 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
933 return;
934
Tomas Winklereedda362008-10-06 16:05:32 +0800935 /* Since setting the TXPOWER may have been deferred while
936 * performing the scan, fire one off */
937 mutex_lock(&priv->mutex);
938 iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
939 mutex_unlock(&priv->mutex);
940}
Samuel Ortiz77fecfb82009-01-23 13:45:12 -0800941EXPORT_SYMBOL(iwl_bg_scan_completed);
Tomas Winklereedda362008-10-06 16:05:32 +0800942
Tomas Winkler2a421b92008-06-12 09:47:10 +0800943void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
944{
Tomas Winklereedda362008-10-06 16:05:32 +0800945 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800946 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
947 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
Johannes Berg88be0262010-04-07 00:21:36 -0700948 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
Tomas Winkler2a421b92008-06-12 09:47:10 +0800949 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
950}
951EXPORT_SYMBOL(iwl_setup_scan_deferred_work);
952