ar9170: 40mhz fixes
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-scan.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2009 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/types.h>
29 #include <linux/etherdevice.h>
30 #include <net/lib80211.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-sta.h"
37 #include "iwl-io.h"
38 #include "iwl-helpers.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
58
59 /**
60  * iwl_scan_cancel - Cancel any currently executing HW scan
61  *
62  * NOTE: priv->mutex is not required before calling this function
63  */
64 int iwl_scan_cancel(struct iwl_priv *priv)
65 {
66         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
67                 clear_bit(STATUS_SCANNING, &priv->status);
68                 return 0;
69         }
70
71         if (test_bit(STATUS_SCANNING, &priv->status)) {
72                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
73                         IWL_DEBUG_SCAN(priv, "Queuing scan abort.\n");
74                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
75                         queue_work(priv->workqueue, &priv->abort_scan);
76
77                 } else
78                         IWL_DEBUG_SCAN(priv, "Scan abort already in progress.\n");
79
80                 return test_bit(STATUS_SCANNING, &priv->status);
81         }
82
83         return 0;
84 }
85 EXPORT_SYMBOL(iwl_scan_cancel);
86 /**
87  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
88  * @ms: amount of time to wait (in milliseconds) for scan to abort
89  *
90  * NOTE: priv->mutex must be held before calling this function
91  */
92 int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
93 {
94         unsigned long now = jiffies;
95         int ret;
96
97         ret = iwl_scan_cancel(priv);
98         if (ret && ms) {
99                 mutex_unlock(&priv->mutex);
100                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
101                                 test_bit(STATUS_SCANNING, &priv->status))
102                         msleep(1);
103                 mutex_lock(&priv->mutex);
104
105                 return test_bit(STATUS_SCANNING, &priv->status);
106         }
107
108         return ret;
109 }
110 EXPORT_SYMBOL(iwl_scan_cancel_timeout);
111
112 int iwl_send_scan_abort(struct iwl_priv *priv)
113 {
114         int ret = 0;
115         struct iwl_rx_packet *res;
116         struct iwl_host_cmd cmd = {
117                 .id = REPLY_SCAN_ABORT_CMD,
118                 .meta.flags = CMD_WANT_SKB,
119         };
120
121         /* If there isn't a scan actively going on in the hardware
122          * then we are in between scan bands and not actually
123          * actively scanning, so don't send the abort command */
124         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
125                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
126                 return 0;
127         }
128
129         ret = iwl_send_cmd_sync(priv, &cmd);
130         if (ret) {
131                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
132                 return ret;
133         }
134
135         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
136         if (res->u.status != CAN_ABORT_STATUS) {
137                 /* The scan abort will return 1 for success or
138                  * 2 for "failure".  A failure condition can be
139                  * due to simply not being in an active scan which
140                  * can occur if we send the scan abort before we
141                  * the microcode has notified us that a scan is
142                  * completed. */
143                 IWL_DEBUG_INFO(priv, "SCAN_ABORT returned %d.\n", res->u.status);
144                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
145                 clear_bit(STATUS_SCAN_HW, &priv->status);
146         }
147
148         priv->alloc_rxb_skb--;
149         dev_kfree_skb_any(cmd.meta.u.skb);
150
151         return ret;
152 }
153 EXPORT_SYMBOL(iwl_send_scan_abort);
154
155 /* Service response to REPLY_SCAN_CMD (0x80) */
156 static void iwl_rx_reply_scan(struct iwl_priv *priv,
157                               struct iwl_rx_mem_buffer *rxb)
158 {
159 #ifdef CONFIG_IWLWIFI_DEBUG
160         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
161         struct iwl_scanreq_notification *notif =
162             (struct iwl_scanreq_notification *)pkt->u.raw;
163
164         IWL_DEBUG_RX(priv, "Scan request status = 0x%x\n", notif->status);
165 #endif
166 }
167
168 /* Service SCAN_START_NOTIFICATION (0x82) */
169 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
170                                     struct iwl_rx_mem_buffer *rxb)
171 {
172         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
173         struct iwl_scanstart_notification *notif =
174             (struct iwl_scanstart_notification *)pkt->u.raw;
175         priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
176         IWL_DEBUG_SCAN(priv, "Scan start: "
177                        "%d [802.11%s] "
178                        "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
179                        notif->channel,
180                        notif->band ? "bg" : "a",
181                        le32_to_cpu(notif->tsf_high),
182                        le32_to_cpu(notif->tsf_low),
183                        notif->status, notif->beacon_timer);
184 }
185
186 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
187 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
188                                       struct iwl_rx_mem_buffer *rxb)
189 {
190 #ifdef CONFIG_IWLWIFI_DEBUG
191         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
192         struct iwl_scanresults_notification *notif =
193             (struct iwl_scanresults_notification *)pkt->u.raw;
194
195         IWL_DEBUG_SCAN(priv, "Scan ch.res: "
196                        "%d [802.11%s] "
197                        "(TSF: 0x%08X:%08X) - %d "
198                        "elapsed=%lu usec (%dms since last)\n",
199                        notif->channel,
200                        notif->band ? "bg" : "a",
201                        le32_to_cpu(notif->tsf_high),
202                        le32_to_cpu(notif->tsf_low),
203                        le32_to_cpu(notif->statistics[0]),
204                        le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
205                        jiffies_to_msecs(elapsed_jiffies
206                                         (priv->last_scan_jiffies, jiffies)));
207 #endif
208
209         priv->last_scan_jiffies = jiffies;
210         priv->next_scan_jiffies = 0;
211 }
212
213 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
214 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
215                                        struct iwl_rx_mem_buffer *rxb)
216 {
217 #ifdef CONFIG_IWLWIFI_DEBUG
218         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
219         struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
220
221         IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
222                        scan_notif->scanned_channels,
223                        scan_notif->tsf_low,
224                        scan_notif->tsf_high, scan_notif->status);
225 #endif
226
227         /* The HW is no longer scanning */
228         clear_bit(STATUS_SCAN_HW, &priv->status);
229
230         IWL_DEBUG_INFO(priv, "Scan pass on %sGHz took %dms\n",
231                        (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
232                                                 "2.4" : "5.2",
233                        jiffies_to_msecs(elapsed_jiffies
234                                         (priv->scan_pass_start, jiffies)));
235
236         /* Remove this scanned band from the list of pending
237          * bands to scan, band G precedes A in order of scanning
238          * as seen in iwl_bg_request_scan */
239         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
240                 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
241         else if (priv->scan_bands &  BIT(IEEE80211_BAND_5GHZ))
242                 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
243
244         /* If a request to abort was given, or the scan did not succeed
245          * then we reset the scan state machine and terminate,
246          * re-queuing another scan if one has been requested */
247         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
248                 IWL_DEBUG_INFO(priv, "Aborted scan completed.\n");
249                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
250         } else {
251                 /* If there are more bands on this scan pass reschedule */
252                 if (priv->scan_bands)
253                         goto reschedule;
254         }
255
256         priv->last_scan_jiffies = jiffies;
257         priv->next_scan_jiffies = 0;
258         IWL_DEBUG_INFO(priv, "Setting scan to off\n");
259
260         clear_bit(STATUS_SCANNING, &priv->status);
261
262         IWL_DEBUG_INFO(priv, "Scan took %dms\n",
263                 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
264
265         queue_work(priv->workqueue, &priv->scan_completed);
266
267         return;
268
269 reschedule:
270         priv->scan_pass_start = jiffies;
271         queue_work(priv->workqueue, &priv->request_scan);
272 }
273
274 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
275 {
276         /* scan handlers */
277         priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
278         priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
279         priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
280                                         iwl_rx_scan_results_notif;
281         priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
282                                         iwl_rx_scan_complete_notif;
283 }
284 EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
285
286 inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
287                                      enum ieee80211_band band,
288                                      u8 n_probes)
289 {
290         if (band == IEEE80211_BAND_5GHZ)
291                 return IWL_ACTIVE_DWELL_TIME_52 +
292                         IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
293         else
294                 return IWL_ACTIVE_DWELL_TIME_24 +
295                         IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
296 }
297 EXPORT_SYMBOL(iwl_get_active_dwell_time);
298
299 u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
300                                enum ieee80211_band band)
301 {
302         u16 passive = (band == IEEE80211_BAND_2GHZ) ?
303             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
304             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
305
306         if (iwl_is_associated(priv)) {
307                 /* If we're associated, we clamp the maximum passive
308                  * dwell time to be 98% of the beacon interval (minus
309                  * 2 * channel tune time) */
310                 passive = priv->beacon_int;
311                 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
312                         passive = IWL_PASSIVE_DWELL_BASE;
313                 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
314         }
315
316         return passive;
317 }
318 EXPORT_SYMBOL(iwl_get_passive_dwell_time);
319
320 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
321                                      enum ieee80211_band band,
322                                      u8 is_active, u8 n_probes,
323                                      struct iwl_scan_channel *scan_ch)
324 {
325         const struct ieee80211_channel *channels = NULL;
326         const struct ieee80211_supported_band *sband;
327         const struct iwl_channel_info *ch_info;
328         u16 passive_dwell = 0;
329         u16 active_dwell = 0;
330         int added, i;
331         u16 channel;
332
333         sband = iwl_get_hw_mode(priv, band);
334         if (!sband)
335                 return 0;
336
337         channels = sband->channels;
338
339         active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
340         passive_dwell = iwl_get_passive_dwell_time(priv, band);
341
342         if (passive_dwell <= active_dwell)
343                 passive_dwell = active_dwell + 1;
344
345         for (i = 0, added = 0; i < sband->n_channels; i++) {
346                 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
347                         continue;
348
349                 channel =
350                         ieee80211_frequency_to_channel(channels[i].center_freq);
351                 scan_ch->channel = cpu_to_le16(channel);
352
353                 ch_info = iwl_get_channel_info(priv, band, channel);
354                 if (!is_channel_valid(ch_info)) {
355                         IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
356                                         channel);
357                         continue;
358                 }
359
360                 if (!is_active || is_channel_passive(ch_info) ||
361                     (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
362                         scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
363                 else
364                         scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
365
366                 if (n_probes)
367                         scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
368
369                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
370                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
371
372                 /* Set txpower levels to defaults */
373                 scan_ch->dsp_atten = 110;
374
375                 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
376                  * power level:
377                  * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
378                  */
379                 if (band == IEEE80211_BAND_5GHZ)
380                         scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
381                 else
382                         scan_ch->tx_gain = ((1 << 5) | (5 << 3));
383
384                 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
385                                channel, le32_to_cpu(scan_ch->type),
386                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
387                                 "ACTIVE" : "PASSIVE",
388                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
389                                active_dwell : passive_dwell);
390
391                 scan_ch++;
392                 added++;
393         }
394
395         IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
396         return added;
397 }
398
399 void iwl_init_scan_params(struct iwl_priv *priv)
400 {
401         u8 ant_idx = fls(priv->hw_params.valid_tx_ant) - 1;
402         if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
403                 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
404         if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
405                 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
406 }
407
408 int iwl_scan_initiate(struct iwl_priv *priv)
409 {
410         if (!iwl_is_ready_rf(priv)) {
411                 IWL_DEBUG_SCAN(priv, "Aborting scan due to not ready.\n");
412                 return -EIO;
413         }
414
415         if (test_bit(STATUS_SCANNING, &priv->status)) {
416                 IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
417                 return -EAGAIN;
418         }
419
420         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
421                 IWL_DEBUG_SCAN(priv, "Scan request while abort pending\n");
422                 return -EAGAIN;
423         }
424
425         IWL_DEBUG_INFO(priv, "Starting scan...\n");
426         if (priv->cfg->sku & IWL_SKU_G)
427                 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
428         if (priv->cfg->sku & IWL_SKU_A)
429                 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
430         set_bit(STATUS_SCANNING, &priv->status);
431         priv->scan_start = jiffies;
432         priv->scan_pass_start = priv->scan_start;
433
434         queue_work(priv->workqueue, &priv->request_scan);
435
436         return 0;
437 }
438 EXPORT_SYMBOL(iwl_scan_initiate);
439
440 #define IWL_DELAY_NEXT_SCAN (HZ*2)
441
442 int iwl_mac_hw_scan(struct ieee80211_hw *hw,
443                      struct cfg80211_scan_request *req)
444 {
445         unsigned long flags;
446         struct iwl_priv *priv = hw->priv;
447         int ret;
448
449         IWL_DEBUG_MAC80211(priv, "enter\n");
450
451         mutex_lock(&priv->mutex);
452         spin_lock_irqsave(&priv->lock, flags);
453
454         if (!iwl_is_ready_rf(priv)) {
455                 ret = -EIO;
456                 IWL_DEBUG_MAC80211(priv, "leave - not ready or exit pending\n");
457                 goto out_unlock;
458         }
459
460         /* We don't schedule scan within next_scan_jiffies period.
461          * Avoid scanning during possible EAPOL exchange, return
462          * success immediately.
463          */
464         if (priv->next_scan_jiffies &&
465             time_after(priv->next_scan_jiffies, jiffies)) {
466                 IWL_DEBUG_SCAN(priv, "scan rejected: within next scan period\n");
467                 queue_work(priv->workqueue, &priv->scan_completed);
468                 ret = 0;
469                 goto out_unlock;
470         }
471
472         /* if we just finished scan ask for delay */
473         if (iwl_is_associated(priv) && priv->last_scan_jiffies &&
474             time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN, jiffies)) {
475                 IWL_DEBUG_SCAN(priv, "scan rejected: within previous scan period\n");
476                 queue_work(priv->workqueue, &priv->scan_completed);
477                 ret = 0;
478                 goto out_unlock;
479         }
480
481         priv->scan_request = req;
482
483         ret = iwl_scan_initiate(priv);
484
485         IWL_DEBUG_MAC80211(priv, "leave\n");
486
487 out_unlock:
488         spin_unlock_irqrestore(&priv->lock, flags);
489         mutex_unlock(&priv->mutex);
490
491         return ret;
492 }
493 EXPORT_SYMBOL(iwl_mac_hw_scan);
494
495 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
496
497 void iwl_bg_scan_check(struct work_struct *data)
498 {
499         struct iwl_priv *priv =
500             container_of(data, struct iwl_priv, scan_check.work);
501
502         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
503                 return;
504
505         mutex_lock(&priv->mutex);
506         if (test_bit(STATUS_SCANNING, &priv->status) ||
507             test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
508                 IWL_DEBUG_SCAN(priv, "Scan completion watchdog resetting "
509                         "adapter (%dms)\n",
510                         jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
511
512                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
513                         iwl_send_scan_abort(priv);
514         }
515         mutex_unlock(&priv->mutex);
516 }
517 EXPORT_SYMBOL(iwl_bg_scan_check);
518
519 /**
520  * iwl_fill_probe_req - fill in all required fields and IE for probe request
521  */
522
523 u16 iwl_fill_probe_req(struct iwl_priv *priv, struct ieee80211_mgmt *frame,
524                        const u8 *ies, int ie_len, int left)
525 {
526         int len = 0;
527         u8 *pos = NULL;
528
529         /* Make sure there is enough space for the probe request,
530          * two mandatory IEs and the data */
531         left -= 24;
532         if (left < 0)
533                 return 0;
534
535         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
536         memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
537         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
538         memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
539         frame->seq_ctrl = 0;
540
541         len += 24;
542
543         /* ...next IE... */
544         pos = &frame->u.probe_req.variable[0];
545
546         /* fill in our indirect SSID IE */
547         left -= 2;
548         if (left < 0)
549                 return 0;
550         *pos++ = WLAN_EID_SSID;
551         *pos++ = 0;
552
553         len += 2;
554
555         if (WARN_ON(left < ie_len))
556                 return len;
557
558         memcpy(pos, ies, ie_len);
559         len += ie_len;
560         left -= ie_len;
561
562         return (u16)len;
563 }
564 EXPORT_SYMBOL(iwl_fill_probe_req);
565
566 static void iwl_bg_request_scan(struct work_struct *data)
567 {
568         struct iwl_priv *priv =
569             container_of(data, struct iwl_priv, request_scan);
570         struct iwl_host_cmd cmd = {
571                 .id = REPLY_SCAN_CMD,
572                 .len = sizeof(struct iwl_scan_cmd),
573                 .meta.flags = CMD_SIZE_HUGE,
574         };
575         struct iwl_scan_cmd *scan;
576         struct ieee80211_conf *conf = NULL;
577         int ret = 0;
578         u32 rate_flags = 0;
579         u16 cmd_len;
580         u16 rx_chain = 0;
581         enum ieee80211_band band;
582         u8 n_probes = 0;
583         u8 rx_ant = priv->hw_params.valid_rx_ant;
584         u8 rate;
585         bool is_active = false;
586         int  chan_mod;
587
588         conf = ieee80211_get_hw_conf(priv->hw);
589
590         mutex_lock(&priv->mutex);
591
592         cancel_delayed_work(&priv->scan_check);
593
594         if (!iwl_is_ready(priv)) {
595                 IWL_WARN(priv, "request scan called when driver not ready.\n");
596                 goto done;
597         }
598
599         /* Make sure the scan wasn't canceled before this queued work
600          * was given the chance to run... */
601         if (!test_bit(STATUS_SCANNING, &priv->status))
602                 goto done;
603
604         /* This should never be called or scheduled if there is currently
605          * a scan active in the hardware. */
606         if (test_bit(STATUS_SCAN_HW, &priv->status)) {
607                 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests in parallel. "
608                                "Ignoring second request.\n");
609                 ret = -EIO;
610                 goto done;
611         }
612
613         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
614                 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
615                 goto done;
616         }
617
618         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
619                 IWL_DEBUG_HC(priv, "Scan request while abort pending.  Queuing.\n");
620                 goto done;
621         }
622
623         if (iwl_is_rfkill(priv)) {
624                 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
625                 goto done;
626         }
627
628         if (!test_bit(STATUS_READY, &priv->status)) {
629                 IWL_DEBUG_HC(priv, "Scan request while uninitialized.  Queuing.\n");
630                 goto done;
631         }
632
633         if (!priv->scan_bands) {
634                 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
635                 goto done;
636         }
637
638         if (!priv->scan) {
639                 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
640                                      IWL_MAX_SCAN_SIZE, GFP_KERNEL);
641                 if (!priv->scan) {
642                         ret = -ENOMEM;
643                         goto done;
644                 }
645         }
646         scan = priv->scan;
647         memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
648
649         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
650         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
651
652         if (iwl_is_associated(priv)) {
653                 u16 interval = 0;
654                 u32 extra;
655                 u32 suspend_time = 100;
656                 u32 scan_suspend_time = 100;
657                 unsigned long flags;
658
659                 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
660
661                 spin_lock_irqsave(&priv->lock, flags);
662                 interval = priv->beacon_int;
663                 spin_unlock_irqrestore(&priv->lock, flags);
664
665                 scan->suspend_time = 0;
666                 scan->max_out_time = cpu_to_le32(200 * 1024);
667                 if (!interval)
668                         interval = suspend_time;
669
670                 extra = (suspend_time / interval) << 22;
671                 scan_suspend_time = (extra |
672                     ((suspend_time % interval) * 1024));
673                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
674                 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
675                                scan_suspend_time, interval);
676         }
677
678         if (priv->scan_request->n_ssids) {
679                 int i, p = 0;
680                 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
681                 for (i = 0; i < priv->scan_request->n_ssids; i++) {
682                         /* always does wildcard anyway */
683                         if (!priv->scan_request->ssids[i].ssid_len)
684                                 continue;
685                         scan->direct_scan[p].id = WLAN_EID_SSID;
686                         scan->direct_scan[p].len =
687                                 priv->scan_request->ssids[i].ssid_len;
688                         memcpy(scan->direct_scan[p].ssid,
689                                priv->scan_request->ssids[i].ssid,
690                                priv->scan_request->ssids[i].ssid_len);
691                         n_probes++;
692                         p++;
693                 }
694                 is_active = true;
695         } else
696                 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
697
698         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
699         scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
700         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
701
702
703         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
704                 band = IEEE80211_BAND_2GHZ;
705                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
706                 chan_mod = le32_to_cpu(priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_MSK)
707                                        >> RXON_FLG_CHANNEL_MODE_POS;
708                 if (chan_mod == CHANNEL_MODE_PURE_40) {
709                         rate = IWL_RATE_6M_PLCP;
710                 } else {
711                         rate = IWL_RATE_1M_PLCP;
712                         rate_flags = RATE_MCS_CCK_MSK;
713                 }
714                 scan->good_CRC_th = 0;
715         } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
716                 band = IEEE80211_BAND_5GHZ;
717                 rate = IWL_RATE_6M_PLCP;
718                 /*
719                  * If active scaning is requested but a certain channel
720                  * is marked passive, we can do active scanning if we
721                  * detect transmissions.
722                  */
723                 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
724
725                 /* Force use of chains B and C (0x6) for scan Rx for 4965
726                  * Avoid A (0x1) because of its off-channel reception on A-band.
727                  */
728                 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
729                         rx_ant = ANT_BC;
730         } else {
731                 IWL_WARN(priv, "Invalid scan band count\n");
732                 goto done;
733         }
734
735         priv->scan_tx_ant[band] =
736                          iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band]);
737         rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
738         scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
739
740         /* MIMO is not used here, but value is required */
741         rx_chain |= ANT_ABC << RXON_RX_CHAIN_VALID_POS;
742         rx_chain |= ANT_ABC << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
743         rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
744         rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
745         scan->rx_chain = cpu_to_le16(rx_chain);
746         cmd_len = iwl_fill_probe_req(priv,
747                                 (struct ieee80211_mgmt *)scan->data,
748                                 priv->scan_request->ie,
749                                 priv->scan_request->ie_len,
750                                 IWL_MAX_SCAN_SIZE - sizeof(*scan));
751
752         scan->tx_cmd.len = cpu_to_le16(cmd_len);
753
754         if (iwl_is_monitor_mode(priv))
755                 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
756
757         scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
758                                RXON_FILTER_BCON_AWARE_MSK);
759
760         scan->channel_count =
761                 iwl_get_channels_for_scan(priv, band, is_active, n_probes,
762                         (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
763
764         if (scan->channel_count == 0) {
765                 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
766                 goto done;
767         }
768
769         cmd.len += le16_to_cpu(scan->tx_cmd.len) +
770             scan->channel_count * sizeof(struct iwl_scan_channel);
771         cmd.data = scan;
772         scan->len = cpu_to_le16(cmd.len);
773
774         set_bit(STATUS_SCAN_HW, &priv->status);
775         ret = iwl_send_cmd_sync(priv, &cmd);
776         if (ret)
777                 goto done;
778
779         queue_delayed_work(priv->workqueue, &priv->scan_check,
780                            IWL_SCAN_CHECK_WATCHDOG);
781
782         mutex_unlock(&priv->mutex);
783         return;
784
785  done:
786         /* Cannot perform scan. Make sure we clear scanning
787         * bits from status so next scan request can be performed.
788         * If we don't clear scanning status bit here all next scan
789         * will fail
790         */
791         clear_bit(STATUS_SCAN_HW, &priv->status);
792         clear_bit(STATUS_SCANNING, &priv->status);
793         /* inform mac80211 scan aborted */
794         queue_work(priv->workqueue, &priv->scan_completed);
795         mutex_unlock(&priv->mutex);
796 }
797
798 void iwl_bg_abort_scan(struct work_struct *work)
799 {
800         struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
801
802         if (!iwl_is_ready(priv))
803                 return;
804
805         mutex_lock(&priv->mutex);
806
807         set_bit(STATUS_SCAN_ABORTING, &priv->status);
808         iwl_send_scan_abort(priv);
809
810         mutex_unlock(&priv->mutex);
811 }
812 EXPORT_SYMBOL(iwl_bg_abort_scan);
813
814 void iwl_bg_scan_completed(struct work_struct *work)
815 {
816         struct iwl_priv *priv =
817             container_of(work, struct iwl_priv, scan_completed);
818
819         IWL_DEBUG_SCAN(priv, "SCAN complete scan\n");
820
821         cancel_delayed_work(&priv->scan_check);
822
823         ieee80211_scan_completed(priv->hw, false);
824
825         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
826                 return;
827
828         /* Since setting the TXPOWER may have been deferred while
829          * performing the scan, fire one off */
830         mutex_lock(&priv->mutex);
831         iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
832         mutex_unlock(&priv->mutex);
833 }
834 EXPORT_SYMBOL(iwl_bg_scan_completed);
835
836 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
837 {
838         INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
839         INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
840         INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
841         INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
842 }
843 EXPORT_SYMBOL(iwl_setup_scan_deferred_work);
844