Linux 3.11-rc3
[cris-mirror.git] / drivers / net / wireless / iwlwifi / dvm / scan.c
blob8c686a5b90ac6657ad3e5e4126c61fba67726654
1 /******************************************************************************
3 * GPL LICENSE SUMMARY
5 * Copyright(c) 2008 - 2013 Intel Corporation. All rights reserved.
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.
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.
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
21 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/etherdevice.h>
31 #include <net/mac80211.h>
33 #include "dev.h"
34 #include "agn.h"
36 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
37 * sending probe req. This should be set long enough to hear probe responses
38 * from more than one AP. */
39 #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
40 #define IWL_ACTIVE_DWELL_TIME_52 (20)
42 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
43 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
45 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
46 * Must be set longer than active dwell time.
47 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
48 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
49 #define IWL_PASSIVE_DWELL_TIME_52 (10)
50 #define IWL_PASSIVE_DWELL_BASE (100)
51 #define IWL_CHANNEL_TUNE_TIME 5
52 #define MAX_SCAN_CHANNEL 50
54 /* For reset radio, need minimal dwell time only */
55 #define IWL_RADIO_RESET_DWELL_TIME 5
57 static int iwl_send_scan_abort(struct iwl_priv *priv)
59 int ret;
60 struct iwl_host_cmd cmd = {
61 .id = REPLY_SCAN_ABORT_CMD,
62 .flags = CMD_SYNC | CMD_WANT_SKB,
64 __le32 *status;
66 /* Exit instantly with error when device is not ready
67 * to receive scan abort command or it does not perform
68 * hardware scan currently */
69 if (!test_bit(STATUS_READY, &priv->status) ||
70 !test_bit(STATUS_SCAN_HW, &priv->status) ||
71 test_bit(STATUS_FW_ERROR, &priv->status))
72 return -EIO;
74 ret = iwl_dvm_send_cmd(priv, &cmd);
75 if (ret)
76 return ret;
78 status = (void *)cmd.resp_pkt->data;
79 if (*status != CAN_ABORT_STATUS) {
80 /* The scan abort will return 1 for success or
81 * 2 for "failure". A failure condition can be
82 * due to simply not being in an active scan which
83 * can occur if we send the scan abort before we
84 * the microcode has notified us that a scan is
85 * completed. */
86 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n",
87 le32_to_cpu(*status));
88 ret = -EIO;
91 iwl_free_resp(&cmd);
92 return ret;
95 static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
97 /* check if scan was requested from mac80211 */
98 if (priv->scan_request) {
99 IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
100 ieee80211_scan_completed(priv->hw, aborted);
103 if (priv->scan_type == IWL_SCAN_ROC)
104 iwl_scan_roc_expired(priv);
106 priv->scan_type = IWL_SCAN_NORMAL;
107 priv->scan_vif = NULL;
108 priv->scan_request = NULL;
111 static void iwl_process_scan_complete(struct iwl_priv *priv)
113 bool aborted;
115 lockdep_assert_held(&priv->mutex);
117 if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status))
118 return;
120 IWL_DEBUG_SCAN(priv, "Completed scan.\n");
122 cancel_delayed_work(&priv->scan_check);
124 aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status);
125 if (aborted)
126 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
128 if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
129 IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
130 goto out_settings;
133 if (priv->scan_type == IWL_SCAN_ROC)
134 iwl_scan_roc_expired(priv);
136 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
137 int err;
139 /* Check if mac80211 requested scan during our internal scan */
140 if (priv->scan_request == NULL)
141 goto out_complete;
143 /* If so request a new scan */
144 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
145 priv->scan_request->channels[0]->band);
146 if (err) {
147 IWL_DEBUG_SCAN(priv,
148 "failed to initiate pending scan: %d\n", err);
149 aborted = true;
150 goto out_complete;
153 return;
156 out_complete:
157 iwl_complete_scan(priv, aborted);
159 out_settings:
160 /* Can we still talk to firmware ? */
161 if (!iwl_is_ready_rf(priv))
162 return;
164 iwlagn_post_scan(priv);
167 void iwl_force_scan_end(struct iwl_priv *priv)
169 lockdep_assert_held(&priv->mutex);
171 if (!test_bit(STATUS_SCANNING, &priv->status)) {
172 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
173 return;
176 IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
177 clear_bit(STATUS_SCANNING, &priv->status);
178 clear_bit(STATUS_SCAN_HW, &priv->status);
179 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
180 clear_bit(STATUS_SCAN_COMPLETE, &priv->status);
181 iwl_complete_scan(priv, true);
184 static void iwl_do_scan_abort(struct iwl_priv *priv)
186 int ret;
188 lockdep_assert_held(&priv->mutex);
190 if (!test_bit(STATUS_SCANNING, &priv->status)) {
191 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
192 return;
195 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
196 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
197 return;
200 ret = iwl_send_scan_abort(priv);
201 if (ret) {
202 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
203 iwl_force_scan_end(priv);
204 } else
205 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
209 * iwl_scan_cancel - Cancel any currently executing HW scan
211 int iwl_scan_cancel(struct iwl_priv *priv)
213 IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
214 queue_work(priv->workqueue, &priv->abort_scan);
215 return 0;
219 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
220 * @ms: amount of time to wait (in milliseconds) for scan to abort
223 void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
225 unsigned long timeout = jiffies + msecs_to_jiffies(ms);
227 lockdep_assert_held(&priv->mutex);
229 IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
231 iwl_do_scan_abort(priv);
233 while (time_before_eq(jiffies, timeout)) {
234 if (!test_bit(STATUS_SCAN_HW, &priv->status))
235 goto finished;
236 msleep(20);
239 return;
241 finished:
243 * Now STATUS_SCAN_HW is clear. This means that the
244 * device finished, but the background work is going
245 * to execute at best as soon as we release the mutex.
246 * Since we need to be able to issue a new scan right
247 * after this function returns, run the complete here.
248 * The STATUS_SCAN_COMPLETE bit will then be cleared
249 * and prevent the background work from "completing"
250 * a possible new scan.
252 iwl_process_scan_complete(priv);
255 /* Service response to REPLY_SCAN_CMD (0x80) */
256 static int iwl_rx_reply_scan(struct iwl_priv *priv,
257 struct iwl_rx_cmd_buffer *rxb,
258 struct iwl_device_cmd *cmd)
260 #ifdef CONFIG_IWLWIFI_DEBUG
261 struct iwl_rx_packet *pkt = rxb_addr(rxb);
262 struct iwl_scanreq_notification *notif = (void *)pkt->data;
264 IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
265 #endif
266 return 0;
269 /* Service SCAN_START_NOTIFICATION (0x82) */
270 static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
271 struct iwl_rx_cmd_buffer *rxb,
272 struct iwl_device_cmd *cmd)
274 struct iwl_rx_packet *pkt = rxb_addr(rxb);
275 struct iwl_scanstart_notification *notif = (void *)pkt->data;
277 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
278 IWL_DEBUG_SCAN(priv, "Scan start: "
279 "%d [802.11%s] "
280 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
281 notif->channel,
282 notif->band ? "bg" : "a",
283 le32_to_cpu(notif->tsf_high),
284 le32_to_cpu(notif->tsf_low),
285 notif->status, notif->beacon_timer);
287 if (priv->scan_type == IWL_SCAN_ROC &&
288 !priv->hw_roc_start_notified) {
289 ieee80211_ready_on_channel(priv->hw);
290 priv->hw_roc_start_notified = true;
293 return 0;
296 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
297 static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
298 struct iwl_rx_cmd_buffer *rxb,
299 struct iwl_device_cmd *cmd)
301 #ifdef CONFIG_IWLWIFI_DEBUG
302 struct iwl_rx_packet *pkt = rxb_addr(rxb);
303 struct iwl_scanresults_notification *notif = (void *)pkt->data;
305 IWL_DEBUG_SCAN(priv, "Scan ch.res: "
306 "%d [802.11%s] "
307 "probe status: %u:%u "
308 "(TSF: 0x%08X:%08X) - %d "
309 "elapsed=%lu usec\n",
310 notif->channel,
311 notif->band ? "bg" : "a",
312 notif->probe_status, notif->num_probe_not_sent,
313 le32_to_cpu(notif->tsf_high),
314 le32_to_cpu(notif->tsf_low),
315 le32_to_cpu(notif->statistics[0]),
316 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
317 #endif
318 return 0;
321 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
322 static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
323 struct iwl_rx_cmd_buffer *rxb,
324 struct iwl_device_cmd *cmd)
326 struct iwl_rx_packet *pkt = rxb_addr(rxb);
327 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data;
329 IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
330 scan_notif->scanned_channels,
331 scan_notif->tsf_low,
332 scan_notif->tsf_high, scan_notif->status);
334 IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
335 (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
336 jiffies_to_msecs(jiffies - priv->scan_start));
339 * When aborting, we run the scan completed background work inline
340 * and the background work must then do nothing. The SCAN_COMPLETE
341 * bit helps implement that logic and thus needs to be set before
342 * queueing the work. Also, since the scan abort waits for SCAN_HW
343 * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
344 * to avoid a race there.
346 set_bit(STATUS_SCAN_COMPLETE, &priv->status);
347 clear_bit(STATUS_SCAN_HW, &priv->status);
348 queue_work(priv->workqueue, &priv->scan_completed);
350 if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
351 iwl_advanced_bt_coexist(priv) &&
352 priv->bt_status != scan_notif->bt_status) {
353 if (scan_notif->bt_status) {
354 /* BT on */
355 if (!priv->bt_ch_announce)
356 priv->bt_traffic_load =
357 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
359 * otherwise, no traffic load information provided
360 * no changes made
362 } else {
363 /* BT off */
364 priv->bt_traffic_load =
365 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
367 priv->bt_status = scan_notif->bt_status;
368 queue_work(priv->workqueue,
369 &priv->bt_traffic_change_work);
371 return 0;
374 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
376 /* scan handlers */
377 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
378 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
379 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
380 iwl_rx_scan_results_notif;
381 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
382 iwl_rx_scan_complete_notif;
385 static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
386 enum ieee80211_band band, u8 n_probes)
388 if (band == IEEE80211_BAND_5GHZ)
389 return IWL_ACTIVE_DWELL_TIME_52 +
390 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
391 else
392 return IWL_ACTIVE_DWELL_TIME_24 +
393 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
396 static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
398 struct iwl_rxon_context *ctx;
399 int limits[NUM_IWL_RXON_CTX] = {};
400 int n_active = 0;
401 u16 limit;
403 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
406 * If we're associated, we clamp the dwell time 98%
407 * of the beacon interval (minus 2 * channel tune time)
408 * If both contexts are active, we have to restrict to
409 * 1/2 of the minimum of them, because they might be in
410 * lock-step with the time inbetween only half of what
411 * time we'd have in each of them.
413 for_each_context(priv, ctx) {
414 switch (ctx->staging.dev_type) {
415 case RXON_DEV_TYPE_P2P:
416 /* no timing constraints */
417 continue;
418 case RXON_DEV_TYPE_ESS:
419 default:
420 /* timing constraints if associated */
421 if (!iwl_is_associated_ctx(ctx))
422 continue;
423 break;
424 case RXON_DEV_TYPE_CP:
425 case RXON_DEV_TYPE_2STA:
427 * These seem to always have timers for TBTT
428 * active in uCode even when not associated yet.
430 break;
433 limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE;
436 switch (n_active) {
437 case 0:
438 return dwell_time;
439 case 2:
440 limit = (limits[1] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
441 limit /= 2;
442 dwell_time = min(limit, dwell_time);
443 /* fall through to limit further */
444 case 1:
445 limit = (limits[0] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
446 limit /= n_active;
447 return min(limit, dwell_time);
448 default:
449 WARN_ON_ONCE(1);
450 return dwell_time;
454 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
455 enum ieee80211_band band)
457 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
458 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
459 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
461 return iwl_limit_dwell(priv, passive);
464 /* Return valid, unused, channel for a passive scan to reset the RF */
465 static u8 iwl_get_single_channel_number(struct iwl_priv *priv,
466 enum ieee80211_band band)
468 struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band];
469 struct iwl_rxon_context *ctx;
470 int i;
472 for (i = 0; i < sband->n_channels; i++) {
473 bool busy = false;
475 for_each_context(priv, ctx) {
476 busy = sband->channels[i].hw_value ==
477 le16_to_cpu(ctx->staging.channel);
478 if (busy)
479 break;
482 if (busy)
483 continue;
485 if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED))
486 return sband->channels[i].hw_value;
489 return 0;
492 static int iwl_get_channel_for_reset_scan(struct iwl_priv *priv,
493 struct ieee80211_vif *vif,
494 enum ieee80211_band band,
495 struct iwl_scan_channel *scan_ch)
497 const struct ieee80211_supported_band *sband;
498 u16 channel;
500 sband = iwl_get_hw_mode(priv, band);
501 if (!sband) {
502 IWL_ERR(priv, "invalid band\n");
503 return 0;
506 channel = iwl_get_single_channel_number(priv, band);
507 if (channel) {
508 scan_ch->channel = cpu_to_le16(channel);
509 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
510 scan_ch->active_dwell =
511 cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
512 scan_ch->passive_dwell =
513 cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
514 /* Set txpower levels to defaults */
515 scan_ch->dsp_atten = 110;
516 if (band == IEEE80211_BAND_5GHZ)
517 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
518 else
519 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
520 return 1;
523 IWL_ERR(priv, "no valid channel found\n");
524 return 0;
527 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
528 struct ieee80211_vif *vif,
529 enum ieee80211_band band,
530 u8 is_active, u8 n_probes,
531 struct iwl_scan_channel *scan_ch)
533 struct ieee80211_channel *chan;
534 const struct ieee80211_supported_band *sband;
535 u16 passive_dwell = 0;
536 u16 active_dwell = 0;
537 int added, i;
538 u16 channel;
540 sband = iwl_get_hw_mode(priv, band);
541 if (!sband)
542 return 0;
544 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
545 passive_dwell = iwl_get_passive_dwell_time(priv, band);
547 if (passive_dwell <= active_dwell)
548 passive_dwell = active_dwell + 1;
550 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
551 chan = priv->scan_request->channels[i];
553 if (chan->band != band)
554 continue;
556 channel = chan->hw_value;
557 scan_ch->channel = cpu_to_le16(channel);
559 if (!is_active || (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
560 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
561 else
562 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
564 if (n_probes)
565 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
567 scan_ch->active_dwell = cpu_to_le16(active_dwell);
568 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
570 /* Set txpower levels to defaults */
571 scan_ch->dsp_atten = 110;
573 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
574 * power level:
575 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
577 if (band == IEEE80211_BAND_5GHZ)
578 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
579 else
580 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
582 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
583 channel, le32_to_cpu(scan_ch->type),
584 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
585 "ACTIVE" : "PASSIVE",
586 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
587 active_dwell : passive_dwell);
589 scan_ch++;
590 added++;
593 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
594 return added;
598 * iwl_fill_probe_req - fill in all required fields and IE for probe request
601 static u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
602 const u8 *ies, int ie_len, const u8 *ssid,
603 u8 ssid_len, int left)
605 int len = 0;
606 u8 *pos = NULL;
608 /* Make sure there is enough space for the probe request,
609 * two mandatory IEs and the data */
610 left -= 24;
611 if (left < 0)
612 return 0;
614 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
615 eth_broadcast_addr(frame->da);
616 memcpy(frame->sa, ta, ETH_ALEN);
617 eth_broadcast_addr(frame->bssid);
618 frame->seq_ctrl = 0;
620 len += 24;
622 /* ...next IE... */
623 pos = &frame->u.probe_req.variable[0];
625 /* fill in our SSID IE */
626 left -= ssid_len + 2;
627 if (left < 0)
628 return 0;
629 *pos++ = WLAN_EID_SSID;
630 *pos++ = ssid_len;
631 if (ssid && ssid_len) {
632 memcpy(pos, ssid, ssid_len);
633 pos += ssid_len;
636 len += ssid_len + 2;
638 if (WARN_ON(left < ie_len))
639 return len;
641 if (ies && ie_len) {
642 memcpy(pos, ies, ie_len);
643 len += ie_len;
646 return (u16)len;
649 static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
651 struct iwl_host_cmd cmd = {
652 .id = REPLY_SCAN_CMD,
653 .len = { sizeof(struct iwl_scan_cmd), },
654 .flags = CMD_SYNC,
656 struct iwl_scan_cmd *scan;
657 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
658 u32 rate_flags = 0;
659 u16 cmd_len = 0;
660 u16 rx_chain = 0;
661 enum ieee80211_band band;
662 u8 n_probes = 0;
663 u8 rx_ant = priv->nvm_data->valid_rx_ant;
664 u8 rate;
665 bool is_active = false;
666 int chan_mod;
667 u8 active_chains;
668 u8 scan_tx_antennas = priv->nvm_data->valid_tx_ant;
669 int ret;
670 int scan_cmd_size = sizeof(struct iwl_scan_cmd) +
671 MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) +
672 priv->fw->ucode_capa.max_probe_length;
673 const u8 *ssid = NULL;
674 u8 ssid_len = 0;
676 if (WARN_ON(priv->scan_type == IWL_SCAN_NORMAL &&
677 (!priv->scan_request ||
678 priv->scan_request->n_channels > MAX_SCAN_CHANNEL)))
679 return -EINVAL;
681 lockdep_assert_held(&priv->mutex);
683 if (vif)
684 ctx = iwl_rxon_ctx_from_vif(vif);
686 if (!priv->scan_cmd) {
687 priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
688 if (!priv->scan_cmd) {
689 IWL_DEBUG_SCAN(priv,
690 "fail to allocate memory for scan\n");
691 return -ENOMEM;
694 scan = priv->scan_cmd;
695 memset(scan, 0, scan_cmd_size);
697 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
698 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
700 if (priv->scan_type != IWL_SCAN_ROC &&
701 iwl_is_any_associated(priv)) {
702 u16 interval = 0;
703 u32 extra;
704 u32 suspend_time = 100;
705 u32 scan_suspend_time = 100;
707 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
708 switch (priv->scan_type) {
709 case IWL_SCAN_ROC:
710 WARN_ON(1);
711 break;
712 case IWL_SCAN_RADIO_RESET:
713 interval = 0;
714 break;
715 case IWL_SCAN_NORMAL:
716 interval = vif->bss_conf.beacon_int;
717 break;
720 scan->suspend_time = 0;
721 scan->max_out_time = cpu_to_le32(200 * 1024);
722 if (!interval)
723 interval = suspend_time;
725 extra = (suspend_time / interval) << 22;
726 scan_suspend_time = (extra |
727 ((suspend_time % interval) * 1024));
728 scan->suspend_time = cpu_to_le32(scan_suspend_time);
729 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
730 scan_suspend_time, interval);
731 } else if (priv->scan_type == IWL_SCAN_ROC) {
732 scan->suspend_time = 0;
733 scan->max_out_time = 0;
734 scan->quiet_time = 0;
735 scan->quiet_plcp_th = 0;
738 switch (priv->scan_type) {
739 case IWL_SCAN_RADIO_RESET:
740 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
742 * Override quiet time as firmware checks that active
743 * dwell is >= quiet; since we use passive scan it'll
744 * not actually be used.
746 scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
747 break;
748 case IWL_SCAN_NORMAL:
749 if (priv->scan_request->n_ssids) {
750 int i, p = 0;
751 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
753 * The highest priority SSID is inserted to the
754 * probe request template.
756 ssid_len = priv->scan_request->ssids[0].ssid_len;
757 ssid = priv->scan_request->ssids[0].ssid;
760 * Invert the order of ssids, the firmware will invert
761 * it back.
763 for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) {
764 scan->direct_scan[p].id = WLAN_EID_SSID;
765 scan->direct_scan[p].len =
766 priv->scan_request->ssids[i].ssid_len;
767 memcpy(scan->direct_scan[p].ssid,
768 priv->scan_request->ssids[i].ssid,
769 priv->scan_request->ssids[i].ssid_len);
770 n_probes++;
771 p++;
773 is_active = true;
774 } else
775 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
776 break;
777 case IWL_SCAN_ROC:
778 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
779 break;
782 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
783 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
784 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
786 switch (priv->scan_band) {
787 case IEEE80211_BAND_2GHZ:
788 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
789 chan_mod = le32_to_cpu(
790 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
791 RXON_FLG_CHANNEL_MODE_MSK)
792 >> RXON_FLG_CHANNEL_MODE_POS;
793 if ((priv->scan_request && priv->scan_request->no_cck) ||
794 chan_mod == CHANNEL_MODE_PURE_40) {
795 rate = IWL_RATE_6M_PLCP;
796 } else {
797 rate = IWL_RATE_1M_PLCP;
798 rate_flags = RATE_MCS_CCK_MSK;
801 * Internal scans are passive, so we can indiscriminately set
802 * the BT ignore flag on 2.4 GHz since it applies to TX only.
804 if (priv->lib->bt_params &&
805 priv->lib->bt_params->advanced_bt_coexist)
806 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
807 break;
808 case IEEE80211_BAND_5GHZ:
809 rate = IWL_RATE_6M_PLCP;
810 break;
811 default:
812 IWL_WARN(priv, "Invalid scan band\n");
813 return -EIO;
817 * If active scanning is requested but a certain channel is
818 * marked passive, we can do active scanning if we detect
819 * transmissions.
821 * There is an issue with some firmware versions that triggers
822 * a sysassert on a "good CRC threshold" of zero (== disabled),
823 * on a radar channel even though this means that we should NOT
824 * send probes.
826 * The "good CRC threshold" is the number of frames that we
827 * need to receive during our dwell time on a channel before
828 * sending out probes -- setting this to a huge value will
829 * mean we never reach it, but at the same time work around
830 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
831 * here instead of IWL_GOOD_CRC_TH_DISABLED.
833 * This was fixed in later versions along with some other
834 * scan changes, and the threshold behaves as a flag in those
835 * versions.
837 if (priv->new_scan_threshold_behaviour)
838 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
839 IWL_GOOD_CRC_TH_DISABLED;
840 else
841 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
842 IWL_GOOD_CRC_TH_NEVER;
844 band = priv->scan_band;
846 if (band == IEEE80211_BAND_2GHZ &&
847 priv->lib->bt_params &&
848 priv->lib->bt_params->advanced_bt_coexist) {
849 /* transmit 2.4 GHz probes only on first antenna */
850 scan_tx_antennas = first_antenna(scan_tx_antennas);
853 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
854 priv->scan_tx_ant[band],
855 scan_tx_antennas);
856 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
857 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
860 * In power save mode while associated use one chain,
861 * otherwise use all chains
863 if (test_bit(STATUS_POWER_PMI, &priv->status) &&
864 !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
865 /* rx_ant has been set to all valid chains previously */
866 active_chains = rx_ant &
867 ((u8)(priv->chain_noise_data.active_chains));
868 if (!active_chains)
869 active_chains = rx_ant;
871 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
872 priv->chain_noise_data.active_chains);
874 rx_ant = first_antenna(active_chains);
876 if (priv->lib->bt_params &&
877 priv->lib->bt_params->advanced_bt_coexist &&
878 priv->bt_full_concurrent) {
879 /* operated as 1x1 in full concurrency mode */
880 rx_ant = first_antenna(rx_ant);
883 /* MIMO is not used here, but value is required */
884 rx_chain |=
885 priv->nvm_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
886 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
887 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
888 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
889 scan->rx_chain = cpu_to_le16(rx_chain);
890 switch (priv->scan_type) {
891 case IWL_SCAN_NORMAL:
892 cmd_len = iwl_fill_probe_req(
893 (struct ieee80211_mgmt *)scan->data,
894 vif->addr,
895 priv->scan_request->ie,
896 priv->scan_request->ie_len,
897 ssid, ssid_len,
898 scan_cmd_size - sizeof(*scan));
899 break;
900 case IWL_SCAN_RADIO_RESET:
901 case IWL_SCAN_ROC:
902 /* use bcast addr, will not be transmitted but must be valid */
903 cmd_len = iwl_fill_probe_req(
904 (struct ieee80211_mgmt *)scan->data,
905 iwl_bcast_addr, NULL, 0,
906 NULL, 0,
907 scan_cmd_size - sizeof(*scan));
908 break;
909 default:
910 BUG();
912 scan->tx_cmd.len = cpu_to_le16(cmd_len);
914 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
915 RXON_FILTER_BCON_AWARE_MSK);
917 switch (priv->scan_type) {
918 case IWL_SCAN_RADIO_RESET:
919 scan->channel_count =
920 iwl_get_channel_for_reset_scan(priv, vif, band,
921 (void *)&scan->data[cmd_len]);
922 break;
923 case IWL_SCAN_NORMAL:
924 scan->channel_count =
925 iwl_get_channels_for_scan(priv, vif, band,
926 is_active, n_probes,
927 (void *)&scan->data[cmd_len]);
928 break;
929 case IWL_SCAN_ROC: {
930 struct iwl_scan_channel *scan_ch;
931 int n_chan, i;
932 u16 dwell;
934 dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
935 n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
937 scan->channel_count = n_chan;
939 scan_ch = (void *)&scan->data[cmd_len];
941 for (i = 0; i < n_chan; i++) {
942 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
943 scan_ch->channel =
944 cpu_to_le16(priv->hw_roc_channel->hw_value);
946 if (i == n_chan - 1)
947 dwell = priv->hw_roc_duration - i * dwell;
949 scan_ch->active_dwell =
950 scan_ch->passive_dwell = cpu_to_le16(dwell);
952 /* Set txpower levels to defaults */
953 scan_ch->dsp_atten = 110;
955 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
956 * power level:
957 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
959 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
960 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
961 else
962 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
964 scan_ch++;
968 break;
971 if (scan->channel_count == 0) {
972 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
973 return -EIO;
976 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
977 scan->channel_count * sizeof(struct iwl_scan_channel);
978 cmd.data[0] = scan;
979 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
980 scan->len = cpu_to_le16(cmd.len[0]);
982 /* set scan bit here for PAN params */
983 set_bit(STATUS_SCAN_HW, &priv->status);
985 ret = iwlagn_set_pan_params(priv);
986 if (ret) {
987 clear_bit(STATUS_SCAN_HW, &priv->status);
988 return ret;
991 ret = iwl_dvm_send_cmd(priv, &cmd);
992 if (ret) {
993 clear_bit(STATUS_SCAN_HW, &priv->status);
994 iwlagn_set_pan_params(priv);
997 return ret;
1000 void iwl_init_scan_params(struct iwl_priv *priv)
1002 u8 ant_idx = fls(priv->nvm_data->valid_tx_ant) - 1;
1003 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
1004 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
1005 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
1006 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
1009 int __must_check iwl_scan_initiate(struct iwl_priv *priv,
1010 struct ieee80211_vif *vif,
1011 enum iwl_scan_type scan_type,
1012 enum ieee80211_band band)
1014 int ret;
1016 lockdep_assert_held(&priv->mutex);
1018 cancel_delayed_work(&priv->scan_check);
1020 if (!iwl_is_ready_rf(priv)) {
1021 IWL_WARN(priv, "Request scan called when driver not ready.\n");
1022 return -EIO;
1025 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1026 IWL_DEBUG_SCAN(priv,
1027 "Multiple concurrent scan requests in parallel.\n");
1028 return -EBUSY;
1031 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1032 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
1033 return -EBUSY;
1036 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
1037 scan_type == IWL_SCAN_NORMAL ? "" :
1038 scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
1039 "internal short ");
1041 set_bit(STATUS_SCANNING, &priv->status);
1042 priv->scan_type = scan_type;
1043 priv->scan_start = jiffies;
1044 priv->scan_band = band;
1046 ret = iwlagn_request_scan(priv, vif);
1047 if (ret) {
1048 clear_bit(STATUS_SCANNING, &priv->status);
1049 priv->scan_type = IWL_SCAN_NORMAL;
1050 return ret;
1053 queue_delayed_work(priv->workqueue, &priv->scan_check,
1054 IWL_SCAN_CHECK_WATCHDOG);
1056 return 0;
1061 * internal short scan, this function should only been called while associated.
1062 * It will reset and tune the radio to prevent possible RF related problem
1064 void iwl_internal_short_hw_scan(struct iwl_priv *priv)
1066 queue_work(priv->workqueue, &priv->start_internal_scan);
1069 static void iwl_bg_start_internal_scan(struct work_struct *work)
1071 struct iwl_priv *priv =
1072 container_of(work, struct iwl_priv, start_internal_scan);
1074 IWL_DEBUG_SCAN(priv, "Start internal scan\n");
1076 mutex_lock(&priv->mutex);
1078 if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
1079 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
1080 goto unlock;
1083 if (test_bit(STATUS_SCANNING, &priv->status)) {
1084 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
1085 goto unlock;
1088 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
1089 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
1090 unlock:
1091 mutex_unlock(&priv->mutex);
1094 static void iwl_bg_scan_check(struct work_struct *data)
1096 struct iwl_priv *priv =
1097 container_of(data, struct iwl_priv, scan_check.work);
1099 IWL_DEBUG_SCAN(priv, "Scan check work\n");
1101 /* Since we are here firmware does not finish scan and
1102 * most likely is in bad shape, so we don't bother to
1103 * send abort command, just force scan complete to mac80211 */
1104 mutex_lock(&priv->mutex);
1105 iwl_force_scan_end(priv);
1106 mutex_unlock(&priv->mutex);
1109 static void iwl_bg_abort_scan(struct work_struct *work)
1111 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1113 IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1115 /* We keep scan_check work queued in case when firmware will not
1116 * report back scan completed notification */
1117 mutex_lock(&priv->mutex);
1118 iwl_scan_cancel_timeout(priv, 200);
1119 mutex_unlock(&priv->mutex);
1122 static void iwl_bg_scan_completed(struct work_struct *work)
1124 struct iwl_priv *priv =
1125 container_of(work, struct iwl_priv, scan_completed);
1127 mutex_lock(&priv->mutex);
1128 iwl_process_scan_complete(priv);
1129 mutex_unlock(&priv->mutex);
1132 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1134 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
1135 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
1136 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
1137 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1140 void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1142 cancel_work_sync(&priv->start_internal_scan);
1143 cancel_work_sync(&priv->abort_scan);
1144 cancel_work_sync(&priv->scan_completed);
1146 if (cancel_delayed_work_sync(&priv->scan_check)) {
1147 mutex_lock(&priv->mutex);
1148 iwl_force_scan_end(priv);
1149 mutex_unlock(&priv->mutex);
1153 void iwl_scan_roc_expired(struct iwl_priv *priv)
1156 * The status bit should be set here, to prevent a race
1157 * where the atomic_read returns 1, but before the execution continues
1158 * iwl_scan_offchannel_skb_status() checks if the status bit is set
1160 set_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status);
1162 if (atomic_read(&priv->num_aux_in_flight) == 0) {
1163 ieee80211_remain_on_channel_expired(priv->hw);
1164 priv->hw_roc_channel = NULL;
1165 schedule_delayed_work(&priv->hw_roc_disable_work,
1166 10 * HZ);
1168 clear_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status);
1169 } else {
1170 IWL_DEBUG_SCAN(priv, "ROC done with %d frames in aux\n",
1171 atomic_read(&priv->num_aux_in_flight));
1175 void iwl_scan_offchannel_skb(struct iwl_priv *priv)
1177 WARN_ON(!priv->hw_roc_start_notified);
1178 atomic_inc(&priv->num_aux_in_flight);
1181 void iwl_scan_offchannel_skb_status(struct iwl_priv *priv)
1183 if (atomic_dec_return(&priv->num_aux_in_flight) == 0 &&
1184 test_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status)) {
1185 IWL_DEBUG_SCAN(priv, "0 aux frames. Calling ROC expired\n");
1186 iwl_scan_roc_expired(priv);