]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/iwl-scan.c
iwlwifi: add wrappers for command sending
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / iwl-scan.c
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 <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>
32
33 #include "iwl-eeprom.h"
34 #include "iwl-dev.h"
35 #include "iwl-core.h"
36 #include "iwl-io.h"
37 #include "iwl-agn.h"
38 #include "iwl-trans.h"
39
40 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
41  * sending probe req.  This should be set long enough to hear probe responses
42  * from more than one AP.  */
43 #define IWL_ACTIVE_DWELL_TIME_24    (30)       /* all times in msec */
44 #define IWL_ACTIVE_DWELL_TIME_52    (20)
45
46 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
47 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
48
49 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
50  * Must be set longer than active dwell time.
51  * For the most reliable scan, set > AP beacon interval (typically 100msec). */
52 #define IWL_PASSIVE_DWELL_TIME_24   (20)       /* all times in msec */
53 #define IWL_PASSIVE_DWELL_TIME_52   (10)
54 #define IWL_PASSIVE_DWELL_BASE      (100)
55 #define IWL_CHANNEL_TUNE_TIME       5
56
57 static int iwl_send_scan_abort(struct iwl_priv *priv)
58 {
59         int ret;
60         struct iwl_rx_packet *pkt;
61         struct iwl_host_cmd cmd = {
62                 .id = REPLY_SCAN_ABORT_CMD,
63                 .flags = CMD_SYNC | CMD_WANT_SKB,
64         };
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->shrd->status) ||
70             !test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) ||
71             !test_bit(STATUS_SCAN_HW, &priv->shrd->status) ||
72             test_bit(STATUS_FW_ERROR, &priv->shrd->status))
73                 return -EIO;
74
75         ret = iwl_dvm_send_cmd(priv, &cmd);
76         if (ret)
77                 return ret;
78
79         pkt = cmd.resp_pkt;
80         if (pkt->u.status != CAN_ABORT_STATUS) {
81                 /* The scan abort will return 1 for success or
82                  * 2 for "failure".  A failure condition can be
83                  * due to simply not being in an active scan which
84                  * can occur if we send the scan abort before we
85                  * the microcode has notified us that a scan is
86                  * completed. */
87                 IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n", pkt->u.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                 ieee80211_remain_on_channel_expired(priv->hw);
105                 priv->hw_roc_channel = NULL;
106                 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
107         }
108
109         priv->scan_type = IWL_SCAN_NORMAL;
110         priv->scan_vif = NULL;
111         priv->scan_request = NULL;
112 }
113
114 static void iwl_process_scan_complete(struct iwl_priv *priv)
115 {
116         bool aborted;
117
118         lockdep_assert_held(&priv->shrd->mutex);
119
120         if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->shrd->status))
121                 return;
122
123         IWL_DEBUG_SCAN(priv, "Completed scan.\n");
124
125         cancel_delayed_work(&priv->scan_check);
126
127         aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
128         if (aborted)
129                 IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
130
131         if (!test_and_clear_bit(STATUS_SCANNING, &priv->shrd->status)) {
132                 IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
133                 goto out_settings;
134         }
135
136         if (priv->scan_type == IWL_SCAN_ROC) {
137                 ieee80211_remain_on_channel_expired(priv->hw);
138                 priv->hw_roc_channel = NULL;
139                 schedule_delayed_work(&priv->hw_roc_disable_work, 10 * HZ);
140         }
141
142         if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
143                 int err;
144
145                 /* Check if mac80211 requested scan during our internal scan */
146                 if (priv->scan_request == NULL)
147                         goto out_complete;
148
149                 /* If so request a new scan */
150                 err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
151                                         priv->scan_request->channels[0]->band);
152                 if (err) {
153                         IWL_DEBUG_SCAN(priv,
154                                 "failed to initiate pending scan: %d\n", err);
155                         aborted = true;
156                         goto out_complete;
157                 }
158
159                 return;
160         }
161
162 out_complete:
163         iwl_complete_scan(priv, aborted);
164
165 out_settings:
166         /* Can we still talk to firmware ? */
167         if (!iwl_is_ready_rf(priv->shrd))
168                 return;
169
170         iwlagn_post_scan(priv);
171 }
172
173 void iwl_force_scan_end(struct iwl_priv *priv)
174 {
175         lockdep_assert_held(&priv->shrd->mutex);
176
177         if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
178                 IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
179                 return;
180         }
181
182         IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
183         clear_bit(STATUS_SCANNING, &priv->shrd->status);
184         clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
185         clear_bit(STATUS_SCAN_ABORTING, &priv->shrd->status);
186         clear_bit(STATUS_SCAN_COMPLETE, &priv->shrd->status);
187         iwl_complete_scan(priv, true);
188 }
189
190 static void iwl_do_scan_abort(struct iwl_priv *priv)
191 {
192         int ret;
193
194         lockdep_assert_held(&priv->shrd->mutex);
195
196         if (!test_bit(STATUS_SCANNING, &priv->shrd->status)) {
197                 IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
198                 return;
199         }
200
201         if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
202                 IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
203                 return;
204         }
205
206         ret = iwl_send_scan_abort(priv);
207         if (ret) {
208                 IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
209                 iwl_force_scan_end(priv);
210         } else
211                 IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
212 }
213
214 /**
215  * iwl_scan_cancel - Cancel any currently executing HW scan
216  */
217 int iwl_scan_cancel(struct iwl_priv *priv)
218 {
219         IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
220         queue_work(priv->workqueue, &priv->abort_scan);
221         return 0;
222 }
223
224 /**
225  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
226  * @ms: amount of time to wait (in milliseconds) for scan to abort
227  *
228  */
229 void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
230 {
231         unsigned long timeout = jiffies + msecs_to_jiffies(ms);
232
233         lockdep_assert_held(&priv->shrd->mutex);
234
235         IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
236
237         iwl_do_scan_abort(priv);
238
239         while (time_before_eq(jiffies, timeout)) {
240                 if (!test_bit(STATUS_SCAN_HW, &priv->shrd->status))
241                         goto finished;
242                 msleep(20);
243         }
244
245         return;
246
247  finished:
248         /*
249          * Now STATUS_SCAN_HW is clear. This means that the
250          * device finished, but the background work is going
251          * to execute at best as soon as we release the mutex.
252          * Since we need to be able to issue a new scan right
253          * after this function returns, run the complete here.
254          * The STATUS_SCAN_COMPLETE bit will then be cleared
255          * and prevent the background work from "completing"
256          * a possible new scan.
257          */
258         iwl_process_scan_complete(priv);
259 }
260
261 /* Service response to REPLY_SCAN_CMD (0x80) */
262 static int iwl_rx_reply_scan(struct iwl_priv *priv,
263                               struct iwl_rx_cmd_buffer *rxb,
264                               struct iwl_device_cmd *cmd)
265 {
266 #ifdef CONFIG_IWLWIFI_DEBUG
267         struct iwl_rx_packet *pkt = rxb_addr(rxb);
268         struct iwl_scanreq_notification *notif =
269             (struct iwl_scanreq_notification *)pkt->u.raw;
270
271         IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
272 #endif
273         return 0;
274 }
275
276 /* Service SCAN_START_NOTIFICATION (0x82) */
277 static int iwl_rx_scan_start_notif(struct iwl_priv *priv,
278                                     struct iwl_rx_cmd_buffer *rxb,
279                                     struct iwl_device_cmd *cmd)
280 {
281         struct iwl_rx_packet *pkt = rxb_addr(rxb);
282         struct iwl_scanstart_notification *notif =
283             (struct iwl_scanstart_notification *)pkt->u.raw;
284         priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
285         IWL_DEBUG_SCAN(priv, "Scan start: "
286                        "%d [802.11%s] "
287                        "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
288                        notif->channel,
289                        notif->band ? "bg" : "a",
290                        le32_to_cpu(notif->tsf_high),
291                        le32_to_cpu(notif->tsf_low),
292                        notif->status, notif->beacon_timer);
293
294         if (priv->scan_type == IWL_SCAN_ROC &&
295             !priv->hw_roc_start_notified) {
296                 ieee80211_ready_on_channel(priv->hw);
297                 priv->hw_roc_start_notified = true;
298         }
299
300         return 0;
301 }
302
303 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
304 static int iwl_rx_scan_results_notif(struct iwl_priv *priv,
305                                       struct iwl_rx_cmd_buffer *rxb,
306                                       struct iwl_device_cmd *cmd)
307 {
308 #ifdef CONFIG_IWLWIFI_DEBUG
309         struct iwl_rx_packet *pkt = rxb_addr(rxb);
310         struct iwl_scanresults_notification *notif =
311             (struct iwl_scanresults_notification *)pkt->u.raw;
312
313         IWL_DEBUG_SCAN(priv, "Scan ch.res: "
314                        "%d [802.11%s] "
315                        "probe status: %u:%u "
316                        "(TSF: 0x%08X:%08X) - %d "
317                        "elapsed=%lu usec\n",
318                        notif->channel,
319                        notif->band ? "bg" : "a",
320                        notif->probe_status, notif->num_probe_not_sent,
321                        le32_to_cpu(notif->tsf_high),
322                        le32_to_cpu(notif->tsf_low),
323                        le32_to_cpu(notif->statistics[0]),
324                        le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
325 #endif
326         return 0;
327 }
328
329 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
330 static int iwl_rx_scan_complete_notif(struct iwl_priv *priv,
331                                        struct iwl_rx_cmd_buffer *rxb,
332                                        struct iwl_device_cmd *cmd)
333 {
334         struct iwl_rx_packet *pkt = rxb_addr(rxb);
335         struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
336
337         IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
338                        scan_notif->scanned_channels,
339                        scan_notif->tsf_low,
340                        scan_notif->tsf_high, scan_notif->status);
341
342         IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
343                        (priv->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
344                        jiffies_to_msecs(jiffies - priv->scan_start));
345
346         /*
347          * When aborting, we run the scan completed background work inline
348          * and the background work must then do nothing. The SCAN_COMPLETE
349          * bit helps implement that logic and thus needs to be set before
350          * queueing the work. Also, since the scan abort waits for SCAN_HW
351          * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
352          * to avoid a race there.
353          */
354         set_bit(STATUS_SCAN_COMPLETE, &priv->shrd->status);
355         clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
356         queue_work(priv->workqueue, &priv->scan_completed);
357
358         if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
359             iwl_advanced_bt_coexist(priv) &&
360             priv->bt_status != scan_notif->bt_status) {
361                 if (scan_notif->bt_status) {
362                         /* BT on */
363                         if (!priv->bt_ch_announce)
364                                 priv->bt_traffic_load =
365                                         IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
366                         /*
367                          * otherwise, no traffic load information provided
368                          * no changes made
369                          */
370                 } else {
371                         /* BT off */
372                         priv->bt_traffic_load =
373                                 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
374                 }
375                 priv->bt_status = scan_notif->bt_status;
376                 queue_work(priv->workqueue,
377                            &priv->bt_traffic_change_work);
378         }
379         return 0;
380 }
381
382 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
383 {
384         /* scan handlers */
385         priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
386         priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
387         priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
388                                         iwl_rx_scan_results_notif;
389         priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
390                                         iwl_rx_scan_complete_notif;
391 }
392
393 static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
394                                      enum ieee80211_band band, u8 n_probes)
395 {
396         if (band == IEEE80211_BAND_5GHZ)
397                 return IWL_ACTIVE_DWELL_TIME_52 +
398                         IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
399         else
400                 return IWL_ACTIVE_DWELL_TIME_24 +
401                         IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
402 }
403
404 static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
405 {
406         struct iwl_rxon_context *ctx;
407
408         /*
409          * If we're associated, we clamp the dwell time 98%
410          * of the smallest beacon interval (minus 2 * channel
411          * tune time)
412          */
413         for_each_context(priv, ctx) {
414                 u16 value;
415
416                 switch (ctx->staging.dev_type) {
417                 case RXON_DEV_TYPE_P2P:
418                         /* no timing constraints */
419                         continue;
420                 case RXON_DEV_TYPE_ESS:
421                 default:
422                         /* timing constraints if associated */
423                         if (!iwl_is_associated_ctx(ctx))
424                                 continue;
425                         break;
426                 case RXON_DEV_TYPE_CP:
427                 case RXON_DEV_TYPE_2STA:
428                         /*
429                          * These seem to always have timers for TBTT
430                          * active in uCode even when not associated yet.
431                          */
432                         break;
433                 }
434
435                 value = ctx->beacon_int;
436                 if (!value)
437                         value = IWL_PASSIVE_DWELL_BASE;
438                 value = (value * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
439                 dwell_time = min(value, dwell_time);
440         }
441
442         return dwell_time;
443 }
444
445 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
446                                       enum ieee80211_band band)
447 {
448         u16 passive = (band == IEEE80211_BAND_2GHZ) ?
449             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
450             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
451
452         return iwl_limit_dwell(priv, passive);
453 }
454
455 static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
456                                            struct ieee80211_vif *vif,
457                                            enum ieee80211_band band,
458                                            struct iwl_scan_channel *scan_ch)
459 {
460         const struct ieee80211_supported_band *sband;
461         u16 passive_dwell = 0;
462         u16 active_dwell = 0;
463         int added = 0;
464         u16 channel = 0;
465
466         sband = iwl_get_hw_mode(priv, band);
467         if (!sband) {
468                 IWL_ERR(priv, "invalid band\n");
469                 return added;
470         }
471
472         active_dwell = iwl_get_active_dwell_time(priv, band, 0);
473         passive_dwell = iwl_get_passive_dwell_time(priv, band);
474
475         if (passive_dwell <= active_dwell)
476                 passive_dwell = active_dwell + 1;
477
478         channel = iwl_get_single_channel_number(priv, band);
479         if (channel) {
480                 scan_ch->channel = cpu_to_le16(channel);
481                 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
482                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
483                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
484                 /* Set txpower levels to defaults */
485                 scan_ch->dsp_atten = 110;
486                 if (band == IEEE80211_BAND_5GHZ)
487                         scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
488                 else
489                         scan_ch->tx_gain = ((1 << 5) | (5 << 3));
490                 added++;
491         } else
492                 IWL_ERR(priv, "no valid channel found\n");
493         return added;
494 }
495
496 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
497                                      struct ieee80211_vif *vif,
498                                      enum ieee80211_band band,
499                                      u8 is_active, u8 n_probes,
500                                      struct iwl_scan_channel *scan_ch)
501 {
502         struct ieee80211_channel *chan;
503         const struct ieee80211_supported_band *sband;
504         const struct iwl_channel_info *ch_info;
505         u16 passive_dwell = 0;
506         u16 active_dwell = 0;
507         int added, i;
508         u16 channel;
509
510         sband = iwl_get_hw_mode(priv, band);
511         if (!sband)
512                 return 0;
513
514         active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
515         passive_dwell = iwl_get_passive_dwell_time(priv, band);
516
517         if (passive_dwell <= active_dwell)
518                 passive_dwell = active_dwell + 1;
519
520         for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
521                 chan = priv->scan_request->channels[i];
522
523                 if (chan->band != band)
524                         continue;
525
526                 channel = chan->hw_value;
527                 scan_ch->channel = cpu_to_le16(channel);
528
529                 ch_info = iwl_get_channel_info(priv, band, channel);
530                 if (!is_channel_valid(ch_info)) {
531                         IWL_DEBUG_SCAN(priv,
532                                        "Channel %d is INVALID for this band.\n",
533                                        channel);
534                         continue;
535                 }
536
537                 if (!is_active || is_channel_passive(ch_info) ||
538                     (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
539                         scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
540                 else
541                         scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
542
543                 if (n_probes)
544                         scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
545
546                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
547                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
548
549                 /* Set txpower levels to defaults */
550                 scan_ch->dsp_atten = 110;
551
552                 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
553                  * power level:
554                  * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
555                  */
556                 if (band == IEEE80211_BAND_5GHZ)
557                         scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
558                 else
559                         scan_ch->tx_gain = ((1 << 5) | (5 << 3));
560
561                 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
562                                channel, le32_to_cpu(scan_ch->type),
563                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
564                                 "ACTIVE" : "PASSIVE",
565                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
566                                active_dwell : passive_dwell);
567
568                 scan_ch++;
569                 added++;
570         }
571
572         IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
573         return added;
574 }
575
576 static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
577 {
578         struct iwl_host_cmd cmd = {
579                 .id = REPLY_SCAN_CMD,
580                 .len = { sizeof(struct iwl_scan_cmd), },
581                 .flags = CMD_SYNC,
582         };
583         struct iwl_scan_cmd *scan;
584         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
585         u32 rate_flags = 0;
586         u16 cmd_len = 0;
587         u16 rx_chain = 0;
588         enum ieee80211_band band;
589         u8 n_probes = 0;
590         u8 rx_ant = hw_params(priv).valid_rx_ant;
591         u8 rate;
592         bool is_active = false;
593         int  chan_mod;
594         u8 active_chains;
595         u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
596         int ret;
597
598         lockdep_assert_held(&priv->shrd->mutex);
599
600         if (vif)
601                 ctx = iwl_rxon_ctx_from_vif(vif);
602
603         if (!priv->scan_cmd) {
604                 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
605                                          IWL_MAX_SCAN_SIZE, GFP_KERNEL);
606                 if (!priv->scan_cmd) {
607                         IWL_DEBUG_SCAN(priv,
608                                        "fail to allocate memory for scan\n");
609                         return -ENOMEM;
610                 }
611         }
612         scan = priv->scan_cmd;
613         memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
614
615         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
616         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
617
618         if (priv->scan_type != IWL_SCAN_ROC &&
619             iwl_is_any_associated(priv)) {
620                 u16 interval = 0;
621                 u32 extra;
622                 u32 suspend_time = 100;
623                 u32 scan_suspend_time = 100;
624
625                 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
626                 switch (priv->scan_type) {
627                 case IWL_SCAN_ROC:
628                         WARN_ON(1);
629                         break;
630                 case IWL_SCAN_RADIO_RESET:
631                         interval = 0;
632                         break;
633                 case IWL_SCAN_NORMAL:
634                         interval = vif->bss_conf.beacon_int;
635                         break;
636                 }
637
638                 scan->suspend_time = 0;
639                 scan->max_out_time = cpu_to_le32(200 * 1024);
640                 if (!interval)
641                         interval = suspend_time;
642
643                 extra = (suspend_time / interval) << 22;
644                 scan_suspend_time = (extra |
645                     ((suspend_time % interval) * 1024));
646                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
647                 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
648                                scan_suspend_time, interval);
649         } else if (priv->scan_type == IWL_SCAN_ROC) {
650                 scan->suspend_time = 0;
651                 scan->max_out_time = 0;
652                 scan->quiet_time = 0;
653                 scan->quiet_plcp_th = 0;
654         }
655
656         switch (priv->scan_type) {
657         case IWL_SCAN_RADIO_RESET:
658                 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
659                 break;
660         case IWL_SCAN_NORMAL:
661                 if (priv->scan_request->n_ssids) {
662                         int i, p = 0;
663                         IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
664                         for (i = 0; i < priv->scan_request->n_ssids; i++) {
665                                 /* always does wildcard anyway */
666                                 if (!priv->scan_request->ssids[i].ssid_len)
667                                         continue;
668                                 scan->direct_scan[p].id = WLAN_EID_SSID;
669                                 scan->direct_scan[p].len =
670                                         priv->scan_request->ssids[i].ssid_len;
671                                 memcpy(scan->direct_scan[p].ssid,
672                                        priv->scan_request->ssids[i].ssid,
673                                        priv->scan_request->ssids[i].ssid_len);
674                                 n_probes++;
675                                 p++;
676                         }
677                         is_active = true;
678                 } else
679                         IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
680                 break;
681         case IWL_SCAN_ROC:
682                 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
683                 break;
684         }
685
686         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
687         scan->tx_cmd.sta_id = ctx->bcast_sta_id;
688         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
689
690         switch (priv->scan_band) {
691         case IEEE80211_BAND_2GHZ:
692                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
693                 chan_mod = le32_to_cpu(
694                         priv->contexts[IWL_RXON_CTX_BSS].active.flags &
695                                                 RXON_FLG_CHANNEL_MODE_MSK)
696                                        >> RXON_FLG_CHANNEL_MODE_POS;
697                 if ((priv->scan_request && priv->scan_request->no_cck) ||
698                     chan_mod == CHANNEL_MODE_PURE_40) {
699                         rate = IWL_RATE_6M_PLCP;
700                 } else {
701                         rate = IWL_RATE_1M_PLCP;
702                         rate_flags = RATE_MCS_CCK_MSK;
703                 }
704                 /*
705                  * Internal scans are passive, so we can indiscriminately set
706                  * the BT ignore flag on 2.4 GHz since it applies to TX only.
707                  */
708                 if (cfg(priv)->bt_params &&
709                     cfg(priv)->bt_params->advanced_bt_coexist)
710                         scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
711                 break;
712         case IEEE80211_BAND_5GHZ:
713                 rate = IWL_RATE_6M_PLCP;
714                 break;
715         default:
716                 IWL_WARN(priv, "Invalid scan band\n");
717                 return -EIO;
718         }
719
720         /*
721          * If active scanning is requested but a certain channel is
722          * marked passive, we can do active scanning if we detect
723          * transmissions.
724          *
725          * There is an issue with some firmware versions that triggers
726          * a sysassert on a "good CRC threshold" of zero (== disabled),
727          * on a radar channel even though this means that we should NOT
728          * send probes.
729          *
730          * The "good CRC threshold" is the number of frames that we
731          * need to receive during our dwell time on a channel before
732          * sending out probes -- setting this to a huge value will
733          * mean we never reach it, but at the same time work around
734          * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
735          * here instead of IWL_GOOD_CRC_TH_DISABLED.
736          *
737          * This was fixed in later versions along with some other
738          * scan changes, and the threshold behaves as a flag in those
739          * versions.
740          */
741         if (priv->new_scan_threshold_behaviour)
742                 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
743                                                 IWL_GOOD_CRC_TH_DISABLED;
744         else
745                 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
746                                                 IWL_GOOD_CRC_TH_NEVER;
747
748         band = priv->scan_band;
749
750         if (cfg(priv)->scan_rx_antennas[band])
751                 rx_ant = cfg(priv)->scan_rx_antennas[band];
752
753         if (band == IEEE80211_BAND_2GHZ &&
754             cfg(priv)->bt_params &&
755             cfg(priv)->bt_params->advanced_bt_coexist) {
756                 /* transmit 2.4 GHz probes only on first antenna */
757                 scan_tx_antennas = first_antenna(scan_tx_antennas);
758         }
759
760         priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
761                                                     priv->scan_tx_ant[band],
762                                                     scan_tx_antennas);
763         rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
764         scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
765
766         /* In power save mode use one chain, otherwise use all chains */
767         if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
768                 /* rx_ant has been set to all valid chains previously */
769                 active_chains = rx_ant &
770                                 ((u8)(priv->chain_noise_data.active_chains));
771                 if (!active_chains)
772                         active_chains = rx_ant;
773
774                 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
775                                 priv->chain_noise_data.active_chains);
776
777                 rx_ant = first_antenna(active_chains);
778         }
779         if (cfg(priv)->bt_params &&
780             cfg(priv)->bt_params->advanced_bt_coexist &&
781             priv->bt_full_concurrent) {
782                 /* operated as 1x1 in full concurrency mode */
783                 rx_ant = first_antenna(rx_ant);
784         }
785
786         /* MIMO is not used here, but value is required */
787         rx_chain |=
788                 hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
789         rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
790         rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
791         rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
792         scan->rx_chain = cpu_to_le16(rx_chain);
793         switch (priv->scan_type) {
794         case IWL_SCAN_NORMAL:
795                 cmd_len = iwl_fill_probe_req(priv,
796                                         (struct ieee80211_mgmt *)scan->data,
797                                         vif->addr,
798                                         priv->scan_request->ie,
799                                         priv->scan_request->ie_len,
800                                         IWL_MAX_SCAN_SIZE - sizeof(*scan));
801                 break;
802         case IWL_SCAN_RADIO_RESET:
803         case IWL_SCAN_ROC:
804                 /* use bcast addr, will not be transmitted but must be valid */
805                 cmd_len = iwl_fill_probe_req(priv,
806                                         (struct ieee80211_mgmt *)scan->data,
807                                         iwl_bcast_addr, NULL, 0,
808                                         IWL_MAX_SCAN_SIZE - sizeof(*scan));
809                 break;
810         default:
811                 BUG();
812         }
813         scan->tx_cmd.len = cpu_to_le16(cmd_len);
814
815         scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
816                                RXON_FILTER_BCON_AWARE_MSK);
817
818         switch (priv->scan_type) {
819         case IWL_SCAN_RADIO_RESET:
820                 scan->channel_count =
821                         iwl_get_single_channel_for_scan(priv, vif, band,
822                                 (void *)&scan->data[cmd_len]);
823                 break;
824         case IWL_SCAN_NORMAL:
825                 scan->channel_count =
826                         iwl_get_channels_for_scan(priv, vif, band,
827                                 is_active, n_probes,
828                                 (void *)&scan->data[cmd_len]);
829                 break;
830         case IWL_SCAN_ROC: {
831                 struct iwl_scan_channel *scan_ch;
832                 int n_chan, i;
833                 u16 dwell;
834
835                 dwell = iwl_limit_dwell(priv, priv->hw_roc_duration);
836                 n_chan = DIV_ROUND_UP(priv->hw_roc_duration, dwell);
837
838                 scan->channel_count = n_chan;
839
840                 scan_ch = (void *)&scan->data[cmd_len];
841
842                 for (i = 0; i < n_chan; i++) {
843                         scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
844                         scan_ch->channel =
845                                 cpu_to_le16(priv->hw_roc_channel->hw_value);
846
847                         if (i == n_chan - 1)
848                                 dwell = priv->hw_roc_duration - i * dwell;
849
850                         scan_ch->active_dwell =
851                         scan_ch->passive_dwell = cpu_to_le16(dwell);
852
853                         /* Set txpower levels to defaults */
854                         scan_ch->dsp_atten = 110;
855
856                         /* NOTE: if we were doing 6Mb OFDM for scans we'd use
857                          * power level:
858                          * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
859                          */
860                         if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
861                                 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
862                         else
863                                 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
864
865                         scan_ch++;
866                 }
867                 }
868
869                 break;
870         }
871
872         if (scan->channel_count == 0) {
873                 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
874                 return -EIO;
875         }
876
877         cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
878             scan->channel_count * sizeof(struct iwl_scan_channel);
879         cmd.data[0] = scan;
880         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
881         scan->len = cpu_to_le16(cmd.len[0]);
882
883         /* set scan bit here for PAN params */
884         set_bit(STATUS_SCAN_HW, &priv->shrd->status);
885
886         ret = iwlagn_set_pan_params(priv);
887         if (ret)
888                 return ret;
889
890         ret = iwl_dvm_send_cmd(priv, &cmd);
891         if (ret) {
892                 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
893                 iwlagn_set_pan_params(priv);
894         }
895
896         return ret;
897 }
898
899 void iwl_init_scan_params(struct iwl_priv *priv)
900 {
901         u8 ant_idx = fls(hw_params(priv).valid_tx_ant) - 1;
902         if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
903                 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
904         if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
905                 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
906 }
907
908 int __must_check iwl_scan_initiate(struct iwl_priv *priv,
909                                    struct ieee80211_vif *vif,
910                                    enum iwl_scan_type scan_type,
911                                    enum ieee80211_band band)
912 {
913         int ret;
914
915         lockdep_assert_held(&priv->shrd->mutex);
916
917         cancel_delayed_work(&priv->scan_check);
918
919         if (!iwl_is_ready_rf(priv->shrd)) {
920                 IWL_WARN(priv, "Request scan called when driver not ready.\n");
921                 return -EIO;
922         }
923
924         if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
925                 IWL_DEBUG_SCAN(priv,
926                         "Multiple concurrent scan requests in parallel.\n");
927                 return -EBUSY;
928         }
929
930         if (test_bit(STATUS_SCAN_ABORTING, &priv->shrd->status)) {
931                 IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
932                 return -EBUSY;
933         }
934
935         IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
936                         scan_type == IWL_SCAN_NORMAL ? "" :
937                         scan_type == IWL_SCAN_ROC ? "remain-on-channel " :
938                         "internal short ");
939
940         set_bit(STATUS_SCANNING, &priv->shrd->status);
941         priv->scan_type = scan_type;
942         priv->scan_start = jiffies;
943         priv->scan_band = band;
944
945         ret = iwlagn_request_scan(priv, vif);
946         if (ret) {
947                 clear_bit(STATUS_SCANNING, &priv->shrd->status);
948                 priv->scan_type = IWL_SCAN_NORMAL;
949                 return ret;
950         }
951
952         queue_delayed_work(priv->workqueue, &priv->scan_check,
953                            IWL_SCAN_CHECK_WATCHDOG);
954
955         return 0;
956 }
957
958
959 /*
960  * internal short scan, this function should only been called while associated.
961  * It will reset and tune the radio to prevent possible RF related problem
962  */
963 void iwl_internal_short_hw_scan(struct iwl_priv *priv)
964 {
965         queue_work(priv->workqueue, &priv->start_internal_scan);
966 }
967
968 static void iwl_bg_start_internal_scan(struct work_struct *work)
969 {
970         struct iwl_priv *priv =
971                 container_of(work, struct iwl_priv, start_internal_scan);
972
973         IWL_DEBUG_SCAN(priv, "Start internal scan\n");
974
975         mutex_lock(&priv->shrd->mutex);
976
977         if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
978                 IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
979                 goto unlock;
980         }
981
982         if (test_bit(STATUS_SCANNING, &priv->shrd->status)) {
983                 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
984                 goto unlock;
985         }
986
987         if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
988                 IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
989  unlock:
990         mutex_unlock(&priv->shrd->mutex);
991 }
992
993 static void iwl_bg_scan_check(struct work_struct *data)
994 {
995         struct iwl_priv *priv =
996             container_of(data, struct iwl_priv, scan_check.work);
997
998         IWL_DEBUG_SCAN(priv, "Scan check work\n");
999
1000         /* Since we are here firmware does not finish scan and
1001          * most likely is in bad shape, so we don't bother to
1002          * send abort command, just force scan complete to mac80211 */
1003         mutex_lock(&priv->shrd->mutex);
1004         iwl_force_scan_end(priv);
1005         mutex_unlock(&priv->shrd->mutex);
1006 }
1007
1008 /**
1009  * iwl_fill_probe_req - fill in all required fields and IE for probe request
1010  */
1011
1012 u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
1013                        const u8 *ta, const u8 *ies, int ie_len, int left)
1014 {
1015         int len = 0;
1016         u8 *pos = NULL;
1017
1018         /* Make sure there is enough space for the probe request,
1019          * two mandatory IEs and the data */
1020         left -= 24;
1021         if (left < 0)
1022                 return 0;
1023
1024         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1025         memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
1026         memcpy(frame->sa, ta, ETH_ALEN);
1027         memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
1028         frame->seq_ctrl = 0;
1029
1030         len += 24;
1031
1032         /* ...next IE... */
1033         pos = &frame->u.probe_req.variable[0];
1034
1035         /* fill in our indirect SSID IE */
1036         left -= 2;
1037         if (left < 0)
1038                 return 0;
1039         *pos++ = WLAN_EID_SSID;
1040         *pos++ = 0;
1041
1042         len += 2;
1043
1044         if (WARN_ON(left < ie_len))
1045                 return len;
1046
1047         if (ies && ie_len) {
1048                 memcpy(pos, ies, ie_len);
1049                 len += ie_len;
1050         }
1051
1052         return (u16)len;
1053 }
1054
1055 static void iwl_bg_abort_scan(struct work_struct *work)
1056 {
1057         struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1058
1059         IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1060
1061         /* We keep scan_check work queued in case when firmware will not
1062          * report back scan completed notification */
1063         mutex_lock(&priv->shrd->mutex);
1064         iwl_scan_cancel_timeout(priv, 200);
1065         mutex_unlock(&priv->shrd->mutex);
1066 }
1067
1068 static void iwl_bg_scan_completed(struct work_struct *work)
1069 {
1070         struct iwl_priv *priv =
1071                 container_of(work, struct iwl_priv, scan_completed);
1072
1073         mutex_lock(&priv->shrd->mutex);
1074         iwl_process_scan_complete(priv);
1075         mutex_unlock(&priv->shrd->mutex);
1076 }
1077
1078 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1079 {
1080         INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
1081         INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
1082         INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
1083         INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1084 }
1085
1086 void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1087 {
1088         cancel_work_sync(&priv->start_internal_scan);
1089         cancel_work_sync(&priv->abort_scan);
1090         cancel_work_sync(&priv->scan_completed);
1091
1092         if (cancel_delayed_work_sync(&priv->scan_check)) {
1093                 mutex_lock(&priv->shrd->mutex);
1094                 iwl_force_scan_end(priv);
1095                 mutex_unlock(&priv->shrd->mutex);
1096         }
1097 }