Merge branch 'linus' into x86/signal
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-scan.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 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  * Tomas Winkler <tomas.winkler@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *****************************************************************************/
28 #include <net/mac80211.h>
29 #include <linux/etherdevice.h>
30
31 #include "iwl-eeprom.h"
32 #include "iwl-dev.h"
33 #include "iwl-core.h"
34 #include "iwl-sta.h"
35 #include "iwl-io.h"
36 #include "iwl-helpers.h"
37
38 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
39  * sending probe req.  This should be set long enough to hear probe responses
40  * from more than one AP.  */
41 #define IWL_ACTIVE_DWELL_TIME_24    (30)       /* all times in msec */
42 #define IWL_ACTIVE_DWELL_TIME_52    (20)
43
44 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
45 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
46
47 /* For faster active scanning, scan will move to the next channel if fewer than
48  * PLCP_QUIET_THRESH packets are heard on this channel within
49  * ACTIVE_QUIET_TIME after sending probe request.  This shortens the dwell
50  * time if it's a quiet channel (nothing responded to our probe, and there's
51  * no other traffic).
52  * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
53 #define IWL_PLCP_QUIET_THRESH       __constant_cpu_to_le16(1)  /* packets */
54 #define IWL_ACTIVE_QUIET_TIME       __constant_cpu_to_le16(10)  /* msec */
55
56 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
57  * Must be set longer than active dwell time.
58  * For the most reliable scan, set > AP beacon interval (typically 100msec). */
59 #define IWL_PASSIVE_DWELL_TIME_24   (20)       /* all times in msec */
60 #define IWL_PASSIVE_DWELL_TIME_52   (10)
61 #define IWL_PASSIVE_DWELL_BASE      (100)
62 #define IWL_CHANNEL_TUNE_TIME       5
63
64 #define IWL_SCAN_PROBE_MASK(n)  cpu_to_le32((BIT(n) | (BIT(n) - BIT(1))))
65
66
67 static int scan_tx_ant[3] = {
68         RATE_MCS_ANT_A_MSK, RATE_MCS_ANT_B_MSK, RATE_MCS_ANT_C_MSK
69 };
70
71
72
73 static int iwl_is_empty_essid(const char *essid, int essid_len)
74 {
75         /* Single white space is for Linksys APs */
76         if (essid_len == 1 && essid[0] == ' ')
77                 return 1;
78
79         /* Otherwise, if the entire essid is 0, we assume it is hidden */
80         while (essid_len) {
81                 essid_len--;
82                 if (essid[essid_len] != '\0')
83                         return 0;
84         }
85
86         return 1;
87 }
88
89
90
91 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
92 {
93         static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
94         const char *s = essid;
95         char *d = escaped;
96
97         if (iwl_is_empty_essid(essid, essid_len)) {
98                 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
99                 return escaped;
100         }
101
102         essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
103         while (essid_len--) {
104                 if (*s == '\0') {
105                         *d++ = '\\';
106                         *d++ = '0';
107                         s++;
108                 } else
109                         *d++ = *s++;
110         }
111         *d = '\0';
112         return escaped;
113 }
114
115 /**
116  * iwl_scan_cancel - Cancel any currently executing HW scan
117  *
118  * NOTE: priv->mutex is not required before calling this function
119  */
120 int iwl_scan_cancel(struct iwl_priv *priv)
121 {
122         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
123                 clear_bit(STATUS_SCANNING, &priv->status);
124                 return 0;
125         }
126
127         if (test_bit(STATUS_SCANNING, &priv->status)) {
128                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
129                         IWL_DEBUG_SCAN("Queuing scan abort.\n");
130                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
131                         queue_work(priv->workqueue, &priv->abort_scan);
132
133                 } else
134                         IWL_DEBUG_SCAN("Scan abort already in progress.\n");
135
136                 return test_bit(STATUS_SCANNING, &priv->status);
137         }
138
139         return 0;
140 }
141 EXPORT_SYMBOL(iwl_scan_cancel);
142 /**
143  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
144  * @ms: amount of time to wait (in milliseconds) for scan to abort
145  *
146  * NOTE: priv->mutex must be held before calling this function
147  */
148 int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
149 {
150         unsigned long now = jiffies;
151         int ret;
152
153         ret = iwl_scan_cancel(priv);
154         if (ret && ms) {
155                 mutex_unlock(&priv->mutex);
156                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
157                                 test_bit(STATUS_SCANNING, &priv->status))
158                         msleep(1);
159                 mutex_lock(&priv->mutex);
160
161                 return test_bit(STATUS_SCANNING, &priv->status);
162         }
163
164         return ret;
165 }
166 EXPORT_SYMBOL(iwl_scan_cancel_timeout);
167
168 static int iwl_send_scan_abort(struct iwl_priv *priv)
169 {
170         int ret = 0;
171         struct iwl_rx_packet *res;
172         struct iwl_host_cmd cmd = {
173                 .id = REPLY_SCAN_ABORT_CMD,
174                 .meta.flags = CMD_WANT_SKB,
175         };
176
177         /* If there isn't a scan actively going on in the hardware
178          * then we are in between scan bands and not actually
179          * actively scanning, so don't send the abort command */
180         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
181                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
182                 return 0;
183         }
184
185         ret = iwl_send_cmd_sync(priv, &cmd);
186         if (ret) {
187                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
188                 return ret;
189         }
190
191         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
192         if (res->u.status != CAN_ABORT_STATUS) {
193                 /* The scan abort will return 1 for success or
194                  * 2 for "failure".  A failure condition can be
195                  * due to simply not being in an active scan which
196                  * can occur if we send the scan abort before we
197                  * the microcode has notified us that a scan is
198                  * completed. */
199                 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
200                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
201                 clear_bit(STATUS_SCAN_HW, &priv->status);
202         }
203
204         priv->alloc_rxb_skb--;
205         dev_kfree_skb_any(cmd.meta.u.skb);
206
207         return ret;
208 }
209
210
211 /* Service response to REPLY_SCAN_CMD (0x80) */
212 static void iwl_rx_reply_scan(struct iwl_priv *priv,
213                               struct iwl_rx_mem_buffer *rxb)
214 {
215 #ifdef CONFIG_IWLWIFI_DEBUG
216         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
217         struct iwl_scanreq_notification *notif =
218             (struct iwl_scanreq_notification *)pkt->u.raw;
219
220         IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
221 #endif
222 }
223
224 /* Service SCAN_START_NOTIFICATION (0x82) */
225 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
226                                     struct iwl_rx_mem_buffer *rxb)
227 {
228         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
229         struct iwl_scanstart_notification *notif =
230             (struct iwl_scanstart_notification *)pkt->u.raw;
231         priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
232         IWL_DEBUG_SCAN("Scan start: "
233                        "%d [802.11%s] "
234                        "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
235                        notif->channel,
236                        notif->band ? "bg" : "a",
237                        le32_to_cpu(notif->tsf_high),
238                        le32_to_cpu(notif->tsf_low),
239                        notif->status, notif->beacon_timer);
240 }
241
242 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
243 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
244                                       struct iwl_rx_mem_buffer *rxb)
245 {
246 #ifdef CONFIG_IWLWIFI_DEBUG
247         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
248         struct iwl_scanresults_notification *notif =
249             (struct iwl_scanresults_notification *)pkt->u.raw;
250
251         IWL_DEBUG_SCAN("Scan ch.res: "
252                        "%d [802.11%s] "
253                        "(TSF: 0x%08X:%08X) - %d "
254                        "elapsed=%lu usec (%dms since last)\n",
255                        notif->channel,
256                        notif->band ? "bg" : "a",
257                        le32_to_cpu(notif->tsf_high),
258                        le32_to_cpu(notif->tsf_low),
259                        le32_to_cpu(notif->statistics[0]),
260                        le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
261                        jiffies_to_msecs(elapsed_jiffies
262                                         (priv->last_scan_jiffies, jiffies)));
263 #endif
264
265         priv->last_scan_jiffies = jiffies;
266         priv->next_scan_jiffies = 0;
267 }
268
269 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
270 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
271                                        struct iwl_rx_mem_buffer *rxb)
272 {
273 #ifdef CONFIG_IWLWIFI_DEBUG
274         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
275         struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
276
277         IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
278                        scan_notif->scanned_channels,
279                        scan_notif->tsf_low,
280                        scan_notif->tsf_high, scan_notif->status);
281 #endif
282
283         /* The HW is no longer scanning */
284         clear_bit(STATUS_SCAN_HW, &priv->status);
285
286         /* The scan completion notification came in, so kill that timer... */
287         cancel_delayed_work(&priv->scan_check);
288
289         IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
290                        (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
291                                                 "2.4" : "5.2",
292                        jiffies_to_msecs(elapsed_jiffies
293                                         (priv->scan_pass_start, jiffies)));
294
295         /* Remove this scanned band from the list of pending
296          * bands to scan, band G precedes A in order of scanning
297          * as seen in iwl_bg_request_scan */
298         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
299                 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
300         else if (priv->scan_bands &  BIT(IEEE80211_BAND_5GHZ))
301                 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
302
303         /* If a request to abort was given, or the scan did not succeed
304          * then we reset the scan state machine and terminate,
305          * re-queuing another scan if one has been requested */
306         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
307                 IWL_DEBUG_INFO("Aborted scan completed.\n");
308                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
309         } else {
310                 /* If there are more bands on this scan pass reschedule */
311                 if (priv->scan_bands)
312                         goto reschedule;
313         }
314
315         priv->last_scan_jiffies = jiffies;
316         priv->next_scan_jiffies = 0;
317         IWL_DEBUG_INFO("Setting scan to off\n");
318
319         clear_bit(STATUS_SCANNING, &priv->status);
320
321         IWL_DEBUG_INFO("Scan took %dms\n",
322                 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
323
324         queue_work(priv->workqueue, &priv->scan_completed);
325
326         return;
327
328 reschedule:
329         priv->scan_pass_start = jiffies;
330         queue_work(priv->workqueue, &priv->request_scan);
331 }
332
333 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
334 {
335         /* scan handlers */
336         priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
337         priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
338         priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
339                                         iwl_rx_scan_results_notif;
340         priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
341                                         iwl_rx_scan_complete_notif;
342 }
343 EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
344
345 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
346                                             enum ieee80211_band band,
347                                             u8 n_probes)
348 {
349         if (band == IEEE80211_BAND_5GHZ)
350                 return IWL_ACTIVE_DWELL_TIME_52 +
351                         IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
352         else
353                 return IWL_ACTIVE_DWELL_TIME_24 +
354                         IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
355 }
356
357 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
358                                       enum ieee80211_band band)
359 {
360         u16 passive = (band == IEEE80211_BAND_2GHZ) ?
361             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
362             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
363
364         if (iwl_is_associated(priv)) {
365                 /* If we're associated, we clamp the maximum passive
366                  * dwell time to be 98% of the beacon interval (minus
367                  * 2 * channel tune time) */
368                 passive = priv->beacon_int;
369                 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
370                         passive = IWL_PASSIVE_DWELL_BASE;
371                 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
372         }
373
374         return passive;
375 }
376
377 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
378                                      enum ieee80211_band band,
379                                      u8 is_active, u8 n_probes,
380                                      struct iwl_scan_channel *scan_ch)
381 {
382         const struct ieee80211_channel *channels = NULL;
383         const struct ieee80211_supported_band *sband;
384         const struct iwl_channel_info *ch_info;
385         u16 passive_dwell = 0;
386         u16 active_dwell = 0;
387         int added, i;
388         u16 channel;
389
390         sband = iwl_get_hw_mode(priv, band);
391         if (!sband)
392                 return 0;
393
394         channels = sband->channels;
395
396         active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
397         passive_dwell = iwl_get_passive_dwell_time(priv, band);
398
399         if (passive_dwell <= active_dwell)
400                 passive_dwell = active_dwell + 1;
401
402         for (i = 0, added = 0; i < sband->n_channels; i++) {
403                 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
404                         continue;
405
406                 channel =
407                         ieee80211_frequency_to_channel(channels[i].center_freq);
408                 scan_ch->channel = cpu_to_le16(channel);
409
410                 ch_info = iwl_get_channel_info(priv, band, channel);
411                 if (!is_channel_valid(ch_info)) {
412                         IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
413                                         channel);
414                         continue;
415                 }
416
417                 if (!is_active || is_channel_passive(ch_info) ||
418                     (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
419                         scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
420                 else
421                         scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
422
423                 if (n_probes)
424                         scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
425
426                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
427                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
428
429                 /* Set txpower levels to defaults */
430                 scan_ch->dsp_atten = 110;
431
432                 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
433                  * power level:
434                  * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
435                  */
436                 if (band == IEEE80211_BAND_5GHZ)
437                         scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
438                 else
439                         scan_ch->tx_gain = ((1 << 5) | (5 << 3));
440
441                 IWL_DEBUG_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n",
442                                channel, le32_to_cpu(scan_ch->type),
443                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
444                                 "ACTIVE" : "PASSIVE",
445                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
446                                active_dwell : passive_dwell);
447
448                 scan_ch++;
449                 added++;
450         }
451
452         IWL_DEBUG_SCAN("total channels to scan %d \n", added);
453         return added;
454 }
455
456 void iwl_init_scan_params(struct iwl_priv *priv)
457 {
458         if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
459                 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = RATE_MCS_ANT_INIT_IND;
460         if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
461                 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = RATE_MCS_ANT_INIT_IND;
462 }
463
464 int iwl_scan_initiate(struct iwl_priv *priv)
465 {
466         if (!iwl_is_ready_rf(priv)) {
467                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
468                 return -EIO;
469         }
470
471         if (test_bit(STATUS_SCANNING, &priv->status)) {
472                 IWL_DEBUG_SCAN("Scan already in progress.\n");
473                 return -EAGAIN;
474         }
475
476         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
477                 IWL_DEBUG_SCAN("Scan request while abort pending\n");
478                 return -EAGAIN;
479         }
480
481         IWL_DEBUG_INFO("Starting scan...\n");
482         if (priv->cfg->sku & IWL_SKU_G)
483                 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
484         if (priv->cfg->sku & IWL_SKU_A)
485                 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
486         set_bit(STATUS_SCANNING, &priv->status);
487         priv->scan_start = jiffies;
488         priv->scan_pass_start = priv->scan_start;
489
490         queue_work(priv->workqueue, &priv->request_scan);
491
492         return 0;
493 }
494 EXPORT_SYMBOL(iwl_scan_initiate);
495
496 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
497
498 static void iwl_bg_scan_check(struct work_struct *data)
499 {
500         struct iwl_priv *priv =
501             container_of(data, struct iwl_priv, scan_check.work);
502
503         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
504                 return;
505
506         mutex_lock(&priv->mutex);
507         if (test_bit(STATUS_SCANNING, &priv->status) ||
508             test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
509                 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
510                         "adapter (%dms)\n",
511                         jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
512
513                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
514                         iwl_send_scan_abort(priv);
515         }
516         mutex_unlock(&priv->mutex);
517 }
518 /**
519  * iwl_supported_rate_to_ie - fill in the supported rate in IE field
520  *
521  * return : set the bit for each supported rate insert in ie
522  */
523 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
524                                     u16 basic_rate, int *left)
525 {
526         u16 ret_rates = 0, bit;
527         int i;
528         u8 *cnt = ie;
529         u8 *rates = ie + 1;
530
531         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
532                 if (bit & supported_rate) {
533                         ret_rates |= bit;
534                         rates[*cnt] = iwl_rates[i].ieee |
535                                 ((bit & basic_rate) ? 0x80 : 0x00);
536                         (*cnt)++;
537                         (*left)--;
538                         if ((*left <= 0) ||
539                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
540                                 break;
541                 }
542         }
543
544         return ret_rates;
545 }
546
547
548 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
549                              u8 *pos, int *left)
550 {
551         struct ieee80211_ht_cap *ht_cap;
552
553         if (!sband || !sband->ht_info.ht_supported)
554                 return;
555
556         if (*left < sizeof(struct ieee80211_ht_cap))
557                 return;
558
559         *pos++ = sizeof(struct ieee80211_ht_cap);
560         ht_cap = (struct ieee80211_ht_cap *) pos;
561
562         ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
563         memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
564         ht_cap->ampdu_params_info =
565                 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
566                 ((sband->ht_info.ampdu_density << 2) &
567                         IEEE80211_HT_CAP_AMPDU_DENSITY);
568         *left -= sizeof(struct ieee80211_ht_cap);
569 }
570
571 /**
572  * iwl_fill_probe_req - fill in all required fields and IE for probe request
573  */
574
575 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
576                                   enum ieee80211_band band,
577                                   struct ieee80211_mgmt *frame,
578                                   int left)
579 {
580         int len = 0;
581         u8 *pos = NULL;
582         u16 active_rates, ret_rates, cck_rates, active_rate_basic;
583         const struct ieee80211_supported_band *sband =
584                                                 iwl_get_hw_mode(priv, band);
585
586
587         /* Make sure there is enough space for the probe request,
588          * two mandatory IEs and the data */
589         left -= 24;
590         if (left < 0)
591                 return 0;
592
593         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
594         memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
595         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
596         memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
597         frame->seq_ctrl = 0;
598
599         len += 24;
600
601         /* ...next IE... */
602         pos = &frame->u.probe_req.variable[0];
603
604         /* fill in our indirect SSID IE */
605         left -= 2;
606         if (left < 0)
607                 return 0;
608         *pos++ = WLAN_EID_SSID;
609         *pos++ = 0;
610
611         len += 2;
612
613         /* fill in supported rate */
614         left -= 2;
615         if (left < 0)
616                 return 0;
617
618         *pos++ = WLAN_EID_SUPP_RATES;
619         *pos = 0;
620
621         /* exclude 60M rate */
622         active_rates = priv->rates_mask;
623         active_rates &= ~IWL_RATE_60M_MASK;
624
625         active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
626
627         cck_rates = IWL_CCK_RATES_MASK & active_rates;
628         ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
629                                              active_rate_basic, &left);
630         active_rates &= ~ret_rates;
631
632         ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
633                                              active_rate_basic, &left);
634         active_rates &= ~ret_rates;
635
636         len += 2 + *pos;
637         pos += (*pos) + 1;
638
639         if (active_rates == 0)
640                 goto fill_end;
641
642         /* fill in supported extended rate */
643         /* ...next IE... */
644         left -= 2;
645         if (left < 0)
646                 return 0;
647         /* ... fill it in... */
648         *pos++ = WLAN_EID_EXT_SUPP_RATES;
649         *pos = 0;
650         iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left);
651         if (*pos > 0) {
652                 len += 2 + *pos;
653                 pos += (*pos) + 1;
654         } else {
655                 pos--;
656         }
657
658  fill_end:
659
660         left -= 2;
661         if (left < 0)
662                 return 0;
663
664         *pos++ = WLAN_EID_HT_CAPABILITY;
665         *pos = 0;
666         iwl_ht_cap_to_ie(sband, pos, &left);
667         if (*pos > 0)
668                 len += 2 + *pos;
669
670         return (u16)len;
671 }
672
673 static u32 iwl_scan_tx_ant(struct iwl_priv *priv, enum ieee80211_band band)
674 {
675         int i, ind;
676
677         ind = priv->scan_tx_ant[band];
678         for (i = 0; i < priv->hw_params.tx_chains_num; i++) {
679                 ind = (ind+1) >= priv->hw_params.tx_chains_num ? 0 : ind+1;
680                 if (priv->hw_params.valid_tx_ant & (1 << ind)) {
681                         priv->scan_tx_ant[band] = ind;
682                         break;
683                 }
684         }
685         IWL_DEBUG_SCAN("select TX ANT = %c\n", 'A' + ind);
686         return scan_tx_ant[ind];
687 }
688
689
690 static void iwl_bg_request_scan(struct work_struct *data)
691 {
692         struct iwl_priv *priv =
693             container_of(data, struct iwl_priv, request_scan);
694         struct iwl_host_cmd cmd = {
695                 .id = REPLY_SCAN_CMD,
696                 .len = sizeof(struct iwl_scan_cmd),
697                 .meta.flags = CMD_SIZE_HUGE,
698         };
699         struct iwl_scan_cmd *scan;
700         struct ieee80211_conf *conf = NULL;
701         int ret = 0;
702         u32 tx_ant;
703         u16 cmd_len;
704         enum ieee80211_band band;
705         u8 n_probes = 2;
706         u8 rx_chain = priv->hw_params.valid_rx_ant;
707
708         conf = ieee80211_get_hw_conf(priv->hw);
709
710         mutex_lock(&priv->mutex);
711
712         if (!iwl_is_ready(priv)) {
713                 IWL_WARNING("request scan called when driver not ready.\n");
714                 goto done;
715         }
716
717         /* Make sure the scan wasn't cancelled before this queued work
718          * was given the chance to run... */
719         if (!test_bit(STATUS_SCANNING, &priv->status))
720                 goto done;
721
722         /* This should never be called or scheduled if there is currently
723          * a scan active in the hardware. */
724         if (test_bit(STATUS_SCAN_HW, &priv->status)) {
725                 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
726                                "Ignoring second request.\n");
727                 ret = -EIO;
728                 goto done;
729         }
730
731         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
732                 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
733                 goto done;
734         }
735
736         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
737                 IWL_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
738                 goto done;
739         }
740
741         if (iwl_is_rfkill(priv)) {
742                 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
743                 goto done;
744         }
745
746         if (!test_bit(STATUS_READY, &priv->status)) {
747                 IWL_DEBUG_HC("Scan request while uninitialized.  Queuing.\n");
748                 goto done;
749         }
750
751         if (!priv->scan_bands) {
752                 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
753                 goto done;
754         }
755
756         if (!priv->scan) {
757                 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
758                                      IWL_MAX_SCAN_SIZE, GFP_KERNEL);
759                 if (!priv->scan) {
760                         ret = -ENOMEM;
761                         goto done;
762                 }
763         }
764         scan = priv->scan;
765         memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
766
767         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
768         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
769
770         if (iwl_is_associated(priv)) {
771                 u16 interval = 0;
772                 u32 extra;
773                 u32 suspend_time = 100;
774                 u32 scan_suspend_time = 100;
775                 unsigned long flags;
776
777                 IWL_DEBUG_INFO("Scanning while associated...\n");
778
779                 spin_lock_irqsave(&priv->lock, flags);
780                 interval = priv->beacon_int;
781                 spin_unlock_irqrestore(&priv->lock, flags);
782
783                 scan->suspend_time = 0;
784                 scan->max_out_time = cpu_to_le32(200 * 1024);
785                 if (!interval)
786                         interval = suspend_time;
787
788                 extra = (suspend_time / interval) << 22;
789                 scan_suspend_time = (extra |
790                     ((suspend_time % interval) * 1024));
791                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
792                 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
793                                scan_suspend_time, interval);
794         }
795
796         /* We should add the ability for user to lock to PASSIVE ONLY */
797         if (priv->one_direct_scan) {
798                 IWL_DEBUG_SCAN("Start direct scan for '%s'\n",
799                                 iwl_escape_essid(priv->direct_ssid,
800                                 priv->direct_ssid_len));
801                 scan->direct_scan[0].id = WLAN_EID_SSID;
802                 scan->direct_scan[0].len = priv->direct_ssid_len;
803                 memcpy(scan->direct_scan[0].ssid,
804                        priv->direct_ssid, priv->direct_ssid_len);
805                 n_probes++;
806         } else if (!iwl_is_associated(priv) && priv->essid_len) {
807                 IWL_DEBUG_SCAN("Start direct scan for '%s' (not associated)\n",
808                                 iwl_escape_essid(priv->essid, priv->essid_len));
809                 scan->direct_scan[0].id = WLAN_EID_SSID;
810                 scan->direct_scan[0].len = priv->essid_len;
811                 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
812                 n_probes++;
813         } else {
814                 IWL_DEBUG_SCAN("Start indirect scan.\n");
815         }
816
817         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
818         scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
819         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
820
821
822         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
823                 band = IEEE80211_BAND_2GHZ;
824                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
825                 tx_ant = iwl_scan_tx_ant(priv, band);
826                 if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK)
827                         scan->tx_cmd.rate_n_flags =
828                                 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
829                                                         tx_ant);
830                 else
831                         scan->tx_cmd.rate_n_flags =
832                                 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
833                                                         tx_ant |
834                                                         RATE_MCS_CCK_MSK);
835                 scan->good_CRC_th = 0;
836         } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
837                 band = IEEE80211_BAND_5GHZ;
838                 tx_ant = iwl_scan_tx_ant(priv, band);
839                 scan->tx_cmd.rate_n_flags =
840                                 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
841                                                         tx_ant);
842                 scan->good_CRC_th = IWL_GOOD_CRC_TH;
843
844                 /* Force use of chains B and C (0x6) for scan Rx for 4965
845                  * Avoid A (0x1) because of its off-channel reception on A-band.
846                  */
847                 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
848                         rx_chain = 0x6;
849         } else {
850                 IWL_WARNING("Invalid scan band count\n");
851                 goto done;
852         }
853
854         /* MIMO is not used here, but value is required */
855         scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
856                                 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
857                                 (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) |
858                                 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
859
860         cmd_len = iwl_fill_probe_req(priv, band,
861                                      (struct ieee80211_mgmt *)scan->data,
862                                      IWL_MAX_SCAN_SIZE - sizeof(*scan));
863
864         scan->tx_cmd.len = cpu_to_le16(cmd_len);
865
866         if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
867                 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
868
869         scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
870                                RXON_FILTER_BCON_AWARE_MSK);
871
872         scan->channel_count =
873                 iwl_get_channels_for_scan(priv, band, 1, /* active */
874                         n_probes,
875                         (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
876
877         if (scan->channel_count == 0) {
878                 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
879                 goto done;
880         }
881
882         cmd.len += le16_to_cpu(scan->tx_cmd.len) +
883             scan->channel_count * sizeof(struct iwl_scan_channel);
884         cmd.data = scan;
885         scan->len = cpu_to_le16(cmd.len);
886
887         set_bit(STATUS_SCAN_HW, &priv->status);
888         ret = iwl_send_cmd_sync(priv, &cmd);
889         if (ret)
890                 goto done;
891
892         queue_delayed_work(priv->workqueue, &priv->scan_check,
893                            IWL_SCAN_CHECK_WATCHDOG);
894
895         mutex_unlock(&priv->mutex);
896         return;
897
898  done:
899         /* inform mac80211 scan aborted */
900         queue_work(priv->workqueue, &priv->scan_completed);
901         mutex_unlock(&priv->mutex);
902 }
903
904 static void iwl_bg_abort_scan(struct work_struct *work)
905 {
906         struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
907
908         if (!iwl_is_ready(priv))
909                 return;
910
911         mutex_lock(&priv->mutex);
912
913         set_bit(STATUS_SCAN_ABORTING, &priv->status);
914         iwl_send_scan_abort(priv);
915
916         mutex_unlock(&priv->mutex);
917 }
918
919 static void iwl_bg_scan_completed(struct work_struct *work)
920 {
921         struct iwl_priv *priv =
922             container_of(work, struct iwl_priv, scan_completed);
923
924         IWL_DEBUG_SCAN("SCAN complete scan\n");
925
926         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
927                 return;
928
929         ieee80211_scan_completed(priv->hw);
930
931         /* Since setting the TXPOWER may have been deferred while
932          * performing the scan, fire one off */
933         mutex_lock(&priv->mutex);
934         iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
935         mutex_unlock(&priv->mutex);
936 }
937
938
939 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
940 {
941         INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
942         INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
943         INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
944         INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
945 }
946 EXPORT_SYMBOL(iwl_setup_scan_deferred_work);
947