Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
scan.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6  *
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:
25  * Intel Linux Wireless <[email protected]>
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>
32 
33 #include "dev.h"
34 #include "agn.h"
35 
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)
41 
42 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
43 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
44 
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
53 
54 /* For reset radio, need minimal dwell time only */
55 #define IWL_RADIO_RESET_DWELL_TIME 5
56 
57 static int iwl_send_scan_abort(struct iwl_priv *priv)
58 {
59  int ret;
60  struct iwl_host_cmd cmd = {
62  .flags = CMD_SYNC | CMD_WANT_SKB,
63  };
64  __le32 *status;
65 
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) ||
72  return -EIO;
73 
74  ret = iwl_dvm_send_cmd(priv, &cmd);
75  if (ret)
76  return ret;
77 
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;
89  }
90 
91  iwl_free_resp(&cmd);
92  return ret;
93 }
94 
95 static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
96 {
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);
101  }
102 
103  if (priv->scan_type == IWL_SCAN_ROC)
104  iwl_scan_roc_expired(priv);
105 
106  priv->scan_type = IWL_SCAN_NORMAL;
107  priv->scan_vif = NULL;
108  priv->scan_request = NULL;
109 }
110 
111 static void iwl_process_scan_complete(struct iwl_priv *priv)
112 {
113  bool aborted;
114 
115  lockdep_assert_held(&priv->mutex);
116 
118  return;
119 
120  IWL_DEBUG_SCAN(priv, "Completed scan.\n");
121 
123 
125  if (aborted)
126  IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
127 
128  if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
129  IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
130  goto out_settings;
131  }
132 
133  if (priv->scan_type == IWL_SCAN_ROC)
134  iwl_scan_roc_expired(priv);
135 
136  if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
137  int err;
138 
139  /* Check if mac80211 requested scan during our internal scan */
140  if (priv->scan_request == NULL)
141  goto out_complete;
142 
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;
151  }
152 
153  return;
154  }
155 
156 out_complete:
157  iwl_complete_scan(priv, aborted);
158 
159 out_settings:
160  /* Can we still talk to firmware ? */
161  if (!iwl_is_ready_rf(priv))
162  return;
163 
164  iwlagn_post_scan(priv);
165 }
166 
167 void iwl_force_scan_end(struct iwl_priv *priv)
168 {
169  lockdep_assert_held(&priv->mutex);
170 
171  if (!test_bit(STATUS_SCANNING, &priv->status)) {
172  IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
173  return;
174  }
175 
176  IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
181  iwl_complete_scan(priv, true);
182 }
183 
184 static void iwl_do_scan_abort(struct iwl_priv *priv)
185 {
186  int ret;
187 
188  lockdep_assert_held(&priv->mutex);
189 
190  if (!test_bit(STATUS_SCANNING, &priv->status)) {
191  IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
192  return;
193  }
194 
196  IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
197  return;
198  }
199 
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");
206 }
207 
211 int iwl_scan_cancel(struct iwl_priv *priv)
212 {
213  IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
214  queue_work(priv->workqueue, &priv->abort_scan);
215  return 0;
216 }
217 
223 void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
224 {
225  unsigned long timeout = jiffies + msecs_to_jiffies(ms);
226 
227  lockdep_assert_held(&priv->mutex);
228 
229  IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
230 
231  iwl_do_scan_abort(priv);
232 
233  while (time_before_eq(jiffies, timeout)) {
234  if (!test_bit(STATUS_SCAN_HW, &priv->status))
235  goto finished;
236  msleep(20);
237  }
238 
239  return;
240 
241  finished:
242  /*
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.
251  */
252  iwl_process_scan_complete(priv);
253 }
254 
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)
259 {
260 #ifdef CONFIG_IWLWIFI_DEBUG
261  struct iwl_rx_packet *pkt = rxb_addr(rxb);
262  struct iwl_scanreq_notification *notif = (void *)pkt->data;
263 
264  IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
265 #endif
266  return 0;
267 }
268 
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)
273 {
274  struct iwl_rx_packet *pkt = rxb_addr(rxb);
275  struct iwl_scanstart_notification *notif = (void *)pkt->data;
276 
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);
286 
287  if (priv->scan_type == IWL_SCAN_ROC &&
288  !priv->hw_roc_start_notified) {
290  priv->hw_roc_start_notified = true;
291  }
292 
293  return 0;
294 }
295 
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)
300 {
301 #ifdef CONFIG_IWLWIFI_DEBUG
302  struct iwl_rx_packet *pkt = rxb_addr(rxb);
303  struct iwl_scanresults_notification *notif = (void *)pkt->data;
304 
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;
319 }
320 
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)
325 {
326  struct iwl_rx_packet *pkt = rxb_addr(rxb);
327  struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data;
328 
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);
333 
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));
337 
338  /*
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.
345  */
348  queue_work(priv->workqueue, &priv->scan_completed);
349 
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 =
358  /*
359  * otherwise, no traffic load information provided
360  * no changes made
361  */
362  } else {
363  /* BT off */
364  priv->bt_traffic_load =
366  }
367  priv->bt_status = scan_notif->bt_status;
368  queue_work(priv->workqueue,
369  &priv->bt_traffic_change_work);
370  }
371  return 0;
372 }
373 
375 {
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;
380  iwl_rx_scan_results_notif;
382  iwl_rx_scan_complete_notif;
383 }
384 
385 static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
386  enum ieee80211_band band, u8 n_probes)
387 {
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);
394 }
395 
396 static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
397 {
398  struct iwl_rxon_context *ctx;
399  int limits[NUM_IWL_RXON_CTX] = {};
400  int n_active = 0;
401  u16 limit;
402 
404 
405  /*
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.
412  */
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:
426  /*
427  * These seem to always have timers for TBTT
428  * active in uCode even when not associated yet.
429  */
430  break;
431  }
432 
433  limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE;
434  }
435 
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;
451  }
452 }
453 
454 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
455  enum ieee80211_band band)
456 {
457  u16 passive = (band == IEEE80211_BAND_2GHZ) ?
460 
461  return iwl_limit_dwell(priv, passive);
462 }
463 
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)
467 {
468  struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band];
469  struct iwl_rxon_context *ctx;
470  int i;
471 
472  for (i = 0; i < sband->n_channels; i++) {
473  bool busy = false;
474 
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;
480  }
481 
482  if (busy)
483  continue;
484 
485  if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED))
486  return sband->channels[i].hw_value;
487  }
488 
489  return 0;
490 }
491 
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)
496 {
497  const struct ieee80211_supported_band *sband;
498  u16 channel;
499 
500  sband = iwl_get_hw_mode(priv, band);
501  if (!sband) {
502  IWL_ERR(priv, "invalid band\n");
503  return 0;
504  }
505 
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 =
512  scan_ch->passive_dwell =
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;
521  }
522 
523  IWL_ERR(priv, "no valid channel found\n");
524  return 0;
525 }
526 
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)
532 {
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;
539 
540  sband = iwl_get_hw_mode(priv, band);
541  if (!sband)
542  return 0;
543 
544  active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
545  passive_dwell = iwl_get_passive_dwell_time(priv, band);
546 
547  if (passive_dwell <= active_dwell)
548  passive_dwell = active_dwell + 1;
549 
550  for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
551  chan = priv->scan_request->channels[i];
552 
553  if (chan->band != band)
554  continue;
555 
556  channel = chan->hw_value;
557  scan_ch->channel = cpu_to_le16(channel);
558 
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;
563 
564  if (n_probes)
565  scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
566 
567  scan_ch->active_dwell = cpu_to_le16(active_dwell);
568  scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
569 
570  /* Set txpower levels to defaults */
571  scan_ch->dsp_atten = 110;
572 
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;
576  */
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));
581 
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);
588 
589  scan_ch++;
590  added++;
591  }
592 
593  IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
594  return added;
595 }
596 
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)
604 {
605  int len = 0;
606  u8 *pos = NULL;
607 
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;
613 
615  eth_broadcast_addr(frame->da);
616  memcpy(frame->sa, ta, ETH_ALEN);
617  eth_broadcast_addr(frame->bssid);
618  frame->seq_ctrl = 0;
619 
620  len += 24;
621 
622  /* ...next IE... */
623  pos = &frame->u.probe_req.variable[0];
624 
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;
634  }
635 
636  len += ssid_len + 2;
637 
638  if (WARN_ON(left < ie_len))
639  return len;
640 
641  if (ies && ie_len) {
642  memcpy(pos, ies, ie_len);
643  len += ie_len;
644  }
645 
646  return (u16)len;
647 }
648 
649 static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
650 {
651  struct iwl_host_cmd cmd = {
652  .id = REPLY_SCAN_CMD,
653  .len = { sizeof(struct iwl_scan_cmd), },
654  .flags = CMD_SYNC,
655  };
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->eeprom_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->eeprom_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;
675 
676  if (WARN_ON_ONCE(priv->scan_request &&
677  priv->scan_request->n_channels > MAX_SCAN_CHANNEL))
678  return -EINVAL;
679 
680  lockdep_assert_held(&priv->mutex);
681 
682  if (vif)
683  ctx = iwl_rxon_ctx_from_vif(vif);
684 
685  if (!priv->scan_cmd) {
686  priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
687  if (!priv->scan_cmd) {
688  IWL_DEBUG_SCAN(priv,
689  "fail to allocate memory for scan\n");
690  return -ENOMEM;
691  }
692  }
693  scan = priv->scan_cmd;
694  memset(scan, 0, scan_cmd_size);
695 
698 
699  if (priv->scan_type != IWL_SCAN_ROC &&
700  iwl_is_any_associated(priv)) {
701  u16 interval = 0;
702  u32 extra;
703  u32 suspend_time = 100;
704  u32 scan_suspend_time = 100;
705 
706  IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
707  switch (priv->scan_type) {
708  case IWL_SCAN_ROC:
709  WARN_ON(1);
710  break;
712  interval = 0;
713  break;
714  case IWL_SCAN_NORMAL:
715  interval = vif->bss_conf.beacon_int;
716  break;
717  }
718 
719  scan->suspend_time = 0;
720  scan->max_out_time = cpu_to_le32(200 * 1024);
721  if (!interval)
722  interval = suspend_time;
723 
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;
735  }
736 
737  switch (priv->scan_type) {
739  IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
740  /*
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.
744  */
746  break;
747  case IWL_SCAN_NORMAL:
748  if (priv->scan_request->n_ssids) {
749  int i, p = 0;
750  IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
751  /*
752  * The highest priority SSID is inserted to the
753  * probe request template.
754  */
755  ssid_len = priv->scan_request->ssids[0].ssid_len;
756  ssid = priv->scan_request->ssids[0].ssid;
757 
758  /*
759  * Invert the order of ssids, the firmware will invert
760  * it back.
761  */
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);
769  n_probes++;
770  p++;
771  }
772  is_active = true;
773  } else
774  IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
775  break;
776  case IWL_SCAN_ROC:
777  IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
778  break;
779  }
780 
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;
784 
785  switch (priv->scan_band) {
786  case IEEE80211_BAND_2GHZ:
788  chan_mod = le32_to_cpu(
789  priv->contexts[IWL_RXON_CTX_BSS].active.flags &
792  if ((priv->scan_request && priv->scan_request->no_cck) ||
793  chan_mod == CHANNEL_MODE_PURE_40) {
794  rate = IWL_RATE_6M_PLCP;
795  } else {
796  rate = IWL_RATE_1M_PLCP;
797  rate_flags = RATE_MCS_CCK_MSK;
798  }
799  /*
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.
802  */
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;
806  break;
807  case IEEE80211_BAND_5GHZ:
808  rate = IWL_RATE_6M_PLCP;
809  break;
810  default:
811  IWL_WARN(priv, "Invalid scan band\n");
812  return -EIO;
813  }
814 
815  /*
816  * If active scanning is requested but a certain channel is
817  * marked passive, we can do active scanning if we detect
818  * transmissions.
819  *
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
823  * send probes.
824  *
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.
831  *
832  * This was fixed in later versions along with some other
833  * scan changes, and the threshold behaves as a flag in those
834  * versions.
835  */
837  scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
839  else
840  scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
842 
843  band = priv->scan_band;
844 
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);
850  }
851 
852  priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
853  priv->scan_tx_ant[band],
854  scan_tx_antennas);
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);
857 
858  /*
859  * In power save mode while associated use one chain,
860  * otherwise use all chains
861  */
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));
867  if (!active_chains)
868  active_chains = rx_ant;
869 
870  IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
871  priv->chain_noise_data.active_chains);
872 
873  rx_ant = first_antenna(active_chains);
874  }
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);
880  }
881 
882  /* MIMO is not used here, but value is required */
883  rx_chain |=
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,
893  vif->addr,
894  priv->scan_request->ie,
895  priv->scan_request->ie_len,
896  ssid, ssid_len,
897  scan_cmd_size - sizeof(*scan));
898  break;
900  case IWL_SCAN_ROC:
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,
905  NULL, 0,
906  scan_cmd_size - sizeof(*scan));
907  break;
908  default:
909  BUG();
910  }
911  scan->tx_cmd.len = cpu_to_le16(cmd_len);
912 
915 
916  switch (priv->scan_type) {
918  scan->channel_count =
919  iwl_get_channel_for_reset_scan(priv, vif, band,
920  (void *)&scan->data[cmd_len]);
921  break;
922  case IWL_SCAN_NORMAL:
923  scan->channel_count =
924  iwl_get_channels_for_scan(priv, vif, band,
925  is_active, n_probes,
926  (void *)&scan->data[cmd_len]);
927  break;
928  case IWL_SCAN_ROC: {
929  struct iwl_scan_channel *scan_ch;
930  int n_chan, i;
931  u16 dwell;
932 
933  dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
934  n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
935 
936  scan->channel_count = n_chan;
937 
938  scan_ch = (void *)&scan->data[cmd_len];
939 
940  for (i = 0; i < n_chan; i++) {
941  scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
942  scan_ch->channel =
943  cpu_to_le16(priv->hw_roc_channel->hw_value);
944 
945  if (i == n_chan - 1)
946  dwell = priv->hw_roc_duration - i * dwell;
947 
948  scan_ch->active_dwell =
949  scan_ch->passive_dwell = cpu_to_le16(dwell);
950 
951  /* Set txpower levels to defaults */
952  scan_ch->dsp_atten = 110;
953 
954  /* NOTE: if we were doing 6Mb OFDM for scans we'd use
955  * power level:
956  * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
957  */
958  if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
959  scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
960  else
961  scan_ch->tx_gain = ((1 << 5) | (5 << 3));
962 
963  scan_ch++;
964  }
965  }
966 
967  break;
968  }
969 
970  if (scan->channel_count == 0) {
971  IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
972  return -EIO;
973  }
974 
975  cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
976  scan->channel_count * sizeof(struct iwl_scan_channel);
977  cmd.data[0] = scan;
979  scan->len = cpu_to_le16(cmd.len[0]);
980 
981  /* set scan bit here for PAN params */
982  set_bit(STATUS_SCAN_HW, &priv->status);
983 
984  ret = iwlagn_set_pan_params(priv);
985  if (ret) {
987  return ret;
988  }
989 
990  ret = iwl_dvm_send_cmd(priv, &cmd);
991  if (ret) {
993  iwlagn_set_pan_params(priv);
994  }
995 
996  return ret;
997 }
998 
999 void iwl_init_scan_params(struct iwl_priv *priv)
1000 {
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;
1006 }
1007 
1009  struct ieee80211_vif *vif,
1010  enum iwl_scan_type scan_type,
1011  enum ieee80211_band band)
1012 {
1013  int ret;
1014 
1015  lockdep_assert_held(&priv->mutex);
1016 
1018 
1019  if (!iwl_is_ready_rf(priv)) {
1020  IWL_WARN(priv, "Request scan called when driver not ready.\n");
1021  return -EIO;
1022  }
1023 
1024  if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1025  IWL_DEBUG_SCAN(priv,
1026  "Multiple concurrent scan requests in parallel.\n");
1027  return -EBUSY;
1028  }
1029 
1030  if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1031  IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
1032  return -EBUSY;
1033  }
1034 
1035  IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
1036  scan_type == IWL_SCAN_NORMAL ? "" :
1037  scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
1038  "internal short ");
1039 
1040  set_bit(STATUS_SCANNING, &priv->status);
1041  priv->scan_type = scan_type;
1042  priv->scan_start = jiffies;
1043  priv->scan_band = band;
1044 
1045  ret = iwlagn_request_scan(priv, vif);
1046  if (ret) {
1047  clear_bit(STATUS_SCANNING, &priv->status);
1048  priv->scan_type = IWL_SCAN_NORMAL;
1049  return ret;
1050  }
1051 
1052  queue_delayed_work(priv->workqueue, &priv->scan_check,
1054 
1055  return 0;
1056 }
1057 
1058 
1059 /*
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
1062  */
1064 {
1065  queue_work(priv->workqueue, &priv->start_internal_scan);
1066 }
1067 
1068 static void iwl_bg_start_internal_scan(struct work_struct *work)
1069 {
1070  struct iwl_priv *priv =
1071  container_of(work, struct iwl_priv, start_internal_scan);
1072 
1073  IWL_DEBUG_SCAN(priv, "Start internal scan\n");
1074 
1075  mutex_lock(&priv->mutex);
1076 
1077  if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
1078  IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
1079  goto unlock;
1080  }
1081 
1082  if (test_bit(STATUS_SCANNING, &priv->status)) {
1083  IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
1084  goto unlock;
1085  }
1086 
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");
1089  unlock:
1090  mutex_unlock(&priv->mutex);
1091 }
1092 
1093 static void iwl_bg_scan_check(struct work_struct *data)
1094 {
1095  struct iwl_priv *priv =
1096  container_of(data, struct iwl_priv, scan_check.work);
1097 
1098  IWL_DEBUG_SCAN(priv, "Scan check work\n");
1099 
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);
1106 }
1107 
1108 static void iwl_bg_abort_scan(struct work_struct *work)
1109 {
1110  struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1111 
1112  IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1113 
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);
1119 }
1120 
1121 static void iwl_bg_scan_completed(struct work_struct *work)
1122 {
1123  struct iwl_priv *priv =
1124  container_of(work, struct iwl_priv, scan_completed);
1125 
1126  mutex_lock(&priv->mutex);
1127  iwl_process_scan_complete(priv);
1128  mutex_unlock(&priv->mutex);
1129 }
1130 
1132 {
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);
1137 }
1138 
1140 {
1142  cancel_work_sync(&priv->abort_scan);
1144 
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);
1149  }
1150 }
1151 
1152 void iwl_scan_roc_expired(struct iwl_priv *priv)
1153 {
1154  /*
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
1158  */
1160 
1161  if (atomic_read(&priv->num_aux_in_flight) == 0) {
1163  priv->hw_roc_channel = NULL;
1165  10 * HZ);
1166 
1168  } else {
1169  IWL_DEBUG_SCAN(priv, "ROC done with %d frames in aux\n",
1170  atomic_read(&priv->num_aux_in_flight));
1171  }
1172 }
1173 
1175 {
1177  atomic_inc(&priv->num_aux_in_flight);
1178 }
1179 
1181 {
1182  if (atomic_dec_return(&priv->num_aux_in_flight) == 0 &&
1184  IWL_DEBUG_SCAN(priv, "0 aux frames. Calling ROC expired\n");
1185  iwl_scan_roc_expired(priv);
1186  }
1187 }