1 /******************************************************************************
5 * Copyright(c) 2008 - 2012 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,
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
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>
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)
60 struct iwl_host_cmd cmd = {
61 .id = REPLY_SCAN_ABORT_CMD,
62 .flags = CMD_SYNC | CMD_WANT_SKB,
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))
74 ret = iwl_dvm_send_cmd(priv, &cmd);
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
86 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n",
87 le32_to_cpu(*status));
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)
115 lockdep_assert_held(&priv->mutex);
117 if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status))
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);
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");
133 if (priv->scan_type == IWL_SCAN_ROC)
134 iwl_scan_roc_expired(priv);
136 if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
139 /* Check if mac80211 requested scan during our internal scan */
140 if (priv->scan_request == NULL)
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);
148 "failed to initiate pending scan: %d\n", err);
157 iwl_complete_scan(priv, aborted);
160 /* Can we still talk to firmware ? */
161 if (!iwl_is_ready_rf(priv))
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");
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)
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");
195 if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
196 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
200 ret = iwl_send_scan_abort(priv);
202 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
203 iwl_force_scan_end(priv);
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);
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))
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);
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: "
280 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
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;
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: "
307 "probe status: %u:%u "
308 "(TSF: 0x%08X:%08X) - %d "
309 "elapsed=%lu usec\n",
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);
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,
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) {
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
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);
374 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
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);
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] = {};
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 */
418 case RXON_DEV_TYPE_ESS:
420 /* timing constraints if associated */
421 if (!iwl_is_associated_ctx(ctx))
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.
433 limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE;
440 limit = (limits[1] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
442 dwell_time = min(limit, dwell_time);
443 /* fall through to limit further */
445 limit = (limits[0] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
447 return min(limit, 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;
472 for (i = 0; i < sband->n_channels; i++) {
475 for_each_context(priv, ctx) {
476 busy = sband->channels[i].hw_value ==
477 le16_to_cpu(ctx->staging.channel);
485 if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED))
486 return sband->channels[i].hw_value;
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;
500 sband = iwl_get_hw_mode(priv, band);
502 IWL_ERR(priv, "invalid band\n");
506 channel = iwl_get_single_channel_number(priv, band);
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;
519 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
523 IWL_ERR(priv, "no valid channel found\n");
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;
540 sband = iwl_get_hw_mode(priv, band);
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)
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;
562 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
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
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;
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);
593 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", 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)
608 /* Make sure there is enough space for the probe request,
609 * two mandatory IEs and the data */
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);
623 pos = &frame->u.probe_req.variable[0];
625 /* fill in our SSID IE */
626 left -= ssid_len + 2;
629 *pos++ = WLAN_EID_SSID;
631 if (ssid && ssid_len) {
632 memcpy(pos, ssid, ssid_len);
638 if (WARN_ON(left < ie_len))
642 memcpy(pos, ies, ie_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), },
656 struct iwl_scan_cmd *scan;
657 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
661 enum ieee80211_band band;
663 u8 rx_ant = priv->eeprom_data->valid_rx_ant;
665 bool is_active = false;
668 u8 scan_tx_antennas = priv->eeprom_data->valid_tx_ant;
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;
676 if (WARN_ON_ONCE(priv->scan_request &&
677 priv->scan_request->n_channels > MAX_SCAN_CHANNEL))
680 lockdep_assert_held(&priv->mutex);
683 ctx = iwl_rxon_ctx_from_vif(vif);
685 if (!priv->scan_cmd) {
686 priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
687 if (!priv->scan_cmd) {
689 "fail to allocate memory for scan\n");
693 scan = priv->scan_cmd;
694 memset(scan, 0, scan_cmd_size);
696 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
697 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
699 if (priv->scan_type != IWL_SCAN_ROC &&
700 iwl_is_any_associated(priv)) {
703 u32 suspend_time = 100;
704 u32 scan_suspend_time = 100;
706 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
707 switch (priv->scan_type) {
711 case IWL_SCAN_RADIO_RESET:
714 case IWL_SCAN_NORMAL:
715 interval = vif->bss_conf.beacon_int;
719 scan->suspend_time = 0;
720 scan->max_out_time = cpu_to_le32(200 * 1024);
722 interval = suspend_time;
724 extra = (suspend_time / interval) << 22;
725 scan_suspend_time = (extra |
726 ((suspend_time % interval) * 1024));
727 scan->suspend_time = cpu_to_le32(scan_suspend_time);
728 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
729 scan_suspend_time, interval);
730 } else if (priv->scan_type == IWL_SCAN_ROC) {
731 scan->suspend_time = 0;
732 scan->max_out_time = 0;
733 scan->quiet_time = 0;
734 scan->quiet_plcp_th = 0;
737 switch (priv->scan_type) {
738 case IWL_SCAN_RADIO_RESET:
739 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
741 * Override quiet time as firmware checks that active
742 * dwell is >= quiet; since we use passive scan it'll
743 * not actually be used.
745 scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
747 case IWL_SCAN_NORMAL:
748 if (priv->scan_request->n_ssids) {
750 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
752 * The highest priority SSID is inserted to the
753 * probe request template.
755 ssid_len = priv->scan_request->ssids[0].ssid_len;
756 ssid = priv->scan_request->ssids[0].ssid;
759 * Invert the order of ssids, the firmware will invert
762 for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) {
763 scan->direct_scan[p].id = WLAN_EID_SSID;
764 scan->direct_scan[p].len =
765 priv->scan_request->ssids[i].ssid_len;
766 memcpy(scan->direct_scan[p].ssid,
767 priv->scan_request->ssids[i].ssid,
768 priv->scan_request->ssids[i].ssid_len);
774 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
777 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
781 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
782 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
783 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
785 switch (priv->scan_band) {
786 case IEEE80211_BAND_2GHZ:
787 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
788 chan_mod = le32_to_cpu(
789 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
790 RXON_FLG_CHANNEL_MODE_MSK)
791 >> RXON_FLG_CHANNEL_MODE_POS;
792 if ((priv->scan_request && priv->scan_request->no_cck) ||
793 chan_mod == CHANNEL_MODE_PURE_40) {
794 rate = IWL_RATE_6M_PLCP;
796 rate = IWL_RATE_1M_PLCP;
797 rate_flags = RATE_MCS_CCK_MSK;
800 * Internal scans are passive, so we can indiscriminately set
801 * the BT ignore flag on 2.4 GHz since it applies to TX only.
803 if (priv->cfg->bt_params &&
804 priv->cfg->bt_params->advanced_bt_coexist)
805 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
807 case IEEE80211_BAND_5GHZ:
808 rate = IWL_RATE_6M_PLCP;
811 IWL_WARN(priv, "Invalid scan band\n");
816 * If active scanning is requested but a certain channel is
817 * marked passive, we can do active scanning if we detect
820 * There is an issue with some firmware versions that triggers
821 * a sysassert on a "good CRC threshold" of zero (== disabled),
822 * on a radar channel even though this means that we should NOT
825 * The "good CRC threshold" is the number of frames that we
826 * need to receive during our dwell time on a channel before
827 * sending out probes -- setting this to a huge value will
828 * mean we never reach it, but at the same time work around
829 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
830 * here instead of IWL_GOOD_CRC_TH_DISABLED.
832 * This was fixed in later versions along with some other
833 * scan changes, and the threshold behaves as a flag in those
836 if (priv->new_scan_threshold_behaviour)
837 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
838 IWL_GOOD_CRC_TH_DISABLED;
840 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
841 IWL_GOOD_CRC_TH_NEVER;
843 band = priv->scan_band;
845 if (band == IEEE80211_BAND_2GHZ &&
846 priv->cfg->bt_params &&
847 priv->cfg->bt_params->advanced_bt_coexist) {
848 /* transmit 2.4 GHz probes only on first antenna */
849 scan_tx_antennas = first_antenna(scan_tx_antennas);
852 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
853 priv->scan_tx_ant[band],
855 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
856 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
859 * In power save mode while associated use one chain,
860 * otherwise use all chains
862 if (test_bit(STATUS_POWER_PMI, &priv->status) &&
863 !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
864 /* rx_ant has been set to all valid chains previously */
865 active_chains = rx_ant &
866 ((u8)(priv->chain_noise_data.active_chains));
868 active_chains = rx_ant;
870 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
871 priv->chain_noise_data.active_chains);
873 rx_ant = first_antenna(active_chains);
875 if (priv->cfg->bt_params &&
876 priv->cfg->bt_params->advanced_bt_coexist &&
877 priv->bt_full_concurrent) {
878 /* operated as 1x1 in full concurrency mode */
879 rx_ant = first_antenna(rx_ant);
882 /* MIMO is not used here, but value is required */
884 priv->eeprom_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
885 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
886 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
887 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
888 scan->rx_chain = cpu_to_le16(rx_chain);
889 switch (priv->scan_type) {
890 case IWL_SCAN_NORMAL:
891 cmd_len = iwl_fill_probe_req(
892 (struct ieee80211_mgmt *)scan->data,
894 priv->scan_request->ie,
895 priv->scan_request->ie_len,
897 scan_cmd_size - sizeof(*scan));
899 case IWL_SCAN_RADIO_RESET:
901 /* use bcast addr, will not be transmitted but must be valid */
902 cmd_len = iwl_fill_probe_req(
903 (struct ieee80211_mgmt *)scan->data,
904 iwl_bcast_addr, NULL, 0,
906 scan_cmd_size - sizeof(*scan));
911 scan->tx_cmd.len = cpu_to_le16(cmd_len);
913 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
914 RXON_FILTER_BCON_AWARE_MSK);
916 switch (priv->scan_type) {
917 case IWL_SCAN_RADIO_RESET:
918 scan->channel_count =
919 iwl_get_channel_for_reset_scan(priv, vif, band,
920 (void *)&scan->data[cmd_len]);
922 case IWL_SCAN_NORMAL:
923 scan->channel_count =
924 iwl_get_channels_for_scan(priv, vif, band,
926 (void *)&scan->data[cmd_len]);
929 struct iwl_scan_channel *scan_ch;
933 dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
934 n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
936 scan->channel_count = n_chan;
938 scan_ch = (void *)&scan->data[cmd_len];
940 for (i = 0; i < n_chan; i++) {
941 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
943 cpu_to_le16(priv->hw_roc_channel->hw_value);
946 dwell = priv->hw_roc_duration - i * dwell;
948 scan_ch->active_dwell =
949 scan_ch->passive_dwell = cpu_to_le16(dwell);
951 /* Set txpower levels to defaults */
952 scan_ch->dsp_atten = 110;
954 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
956 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
958 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
959 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
961 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
970 if (scan->channel_count == 0) {
971 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
975 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
976 scan->channel_count * sizeof(struct iwl_scan_channel);
978 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
979 scan->len = cpu_to_le16(cmd.len[0]);
981 /* set scan bit here for PAN params */
982 set_bit(STATUS_SCAN_HW, &priv->status);
984 ret = iwlagn_set_pan_params(priv);
986 clear_bit(STATUS_SCAN_HW, &priv->status);
990 ret = iwl_dvm_send_cmd(priv, &cmd);
992 clear_bit(STATUS_SCAN_HW, &priv->status);
993 iwlagn_set_pan_params(priv);
999 void iwl_init_scan_params(struct iwl_priv *priv)
1001 u8 ant_idx = fls(priv->eeprom_data->valid_tx_ant) - 1;
1002 if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
1003 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
1004 if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
1005 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
1008 int __must_check iwl_scan_initiate(struct iwl_priv *priv,
1009 struct ieee80211_vif *vif,
1010 enum iwl_scan_type scan_type,
1011 enum ieee80211_band band)
1015 lockdep_assert_held(&priv->mutex);
1017 cancel_delayed_work(&priv->scan_check);
1019 if (!iwl_is_ready_rf(priv)) {
1020 IWL_WARN(priv, "Request scan called when driver not ready.\n");
1024 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1025 IWL_DEBUG_SCAN(priv,
1026 "Multiple concurrent scan requests in parallel.\n");
1030 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1031 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
1035 IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
1036 scan_type == IWL_SCAN_NORMAL ? "" :
1037 scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
1040 set_bit(STATUS_SCANNING, &priv->status);
1041 priv->scan_type = scan_type;
1042 priv->scan_start = jiffies;
1043 priv->scan_band = band;
1045 ret = iwlagn_request_scan(priv, vif);
1047 clear_bit(STATUS_SCANNING, &priv->status);
1048 priv->scan_type = IWL_SCAN_NORMAL;
1052 queue_delayed_work(priv->workqueue, &priv->scan_check,
1053 IWL_SCAN_CHECK_WATCHDOG);
1060 * internal short scan, this function should only been called while associated.
1061 * It will reset and tune the radio to prevent possible RF related problem
1063 void iwl_internal_short_hw_scan(struct iwl_priv *priv)
1065 queue_work(priv->workqueue, &priv->start_internal_scan);
1068 static void iwl_bg_start_internal_scan(struct work_struct *work)
1070 struct iwl_priv *priv =
1071 container_of(work, struct iwl_priv, start_internal_scan);
1073 IWL_DEBUG_SCAN(priv, "Start internal scan\n");
1075 mutex_lock(&priv->mutex);
1077 if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
1078 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
1082 if (test_bit(STATUS_SCANNING, &priv->status)) {
1083 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
1087 if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
1088 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
1090 mutex_unlock(&priv->mutex);
1093 static void iwl_bg_scan_check(struct work_struct *data)
1095 struct iwl_priv *priv =
1096 container_of(data, struct iwl_priv, scan_check.work);
1098 IWL_DEBUG_SCAN(priv, "Scan check work\n");
1100 /* Since we are here firmware does not finish scan and
1101 * most likely is in bad shape, so we don't bother to
1102 * send abort command, just force scan complete to mac80211 */
1103 mutex_lock(&priv->mutex);
1104 iwl_force_scan_end(priv);
1105 mutex_unlock(&priv->mutex);
1108 static void iwl_bg_abort_scan(struct work_struct *work)
1110 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1112 IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1114 /* We keep scan_check work queued in case when firmware will not
1115 * report back scan completed notification */
1116 mutex_lock(&priv->mutex);
1117 iwl_scan_cancel_timeout(priv, 200);
1118 mutex_unlock(&priv->mutex);
1121 static void iwl_bg_scan_completed(struct work_struct *work)
1123 struct iwl_priv *priv =
1124 container_of(work, struct iwl_priv, scan_completed);
1126 mutex_lock(&priv->mutex);
1127 iwl_process_scan_complete(priv);
1128 mutex_unlock(&priv->mutex);
1131 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1133 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
1134 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
1135 INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
1136 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1139 void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1141 cancel_work_sync(&priv->start_internal_scan);
1142 cancel_work_sync(&priv->abort_scan);
1143 cancel_work_sync(&priv->scan_completed);
1145 if (cancel_delayed_work_sync(&priv->scan_check)) {
1146 mutex_lock(&priv->mutex);
1147 iwl_force_scan_end(priv);
1148 mutex_unlock(&priv->mutex);
1152 void iwl_scan_roc_expired(struct iwl_priv *priv)
1155 * The status bit should be set here, to prevent a race
1156 * where the atomic_read returns 1, but before the execution continues
1157 * iwl_scan_offchannel_skb_status() checks if the status bit is set
1159 set_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status);
1161 if (atomic_read(&priv->num_aux_in_flight) == 0) {
1162 ieee80211_remain_on_channel_expired(priv->hw);
1163 priv->hw_roc_channel = NULL;
1164 schedule_delayed_work(&priv->hw_roc_disable_work,
1167 clear_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status);
1169 IWL_DEBUG_SCAN(priv, "ROC done with %d frames in aux\n",
1170 atomic_read(&priv->num_aux_in_flight));
1174 void iwl_scan_offchannel_skb(struct iwl_priv *priv)
1176 WARN_ON(!priv->hw_roc_start_notified);
1177 atomic_inc(&priv->num_aux_in_flight);
1180 void iwl_scan_offchannel_skb_status(struct iwl_priv *priv)
1182 if (atomic_dec_return(&priv->num_aux_in_flight) == 0 &&
1183 test_bit(STATUS_SCAN_ROC_EXPIRED, &priv->status)) {
1184 IWL_DEBUG_SCAN(priv, "0 aux frames. Calling ROC expired\n");
1185 iwl_scan_roc_expired(priv);