rtl8187: Fix for TX sequence number problem
[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 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 EXPORT_SYMBOL(iwl_escape_essid);
115
116 /**
117  * iwl_scan_cancel - Cancel any currently executing HW scan
118  *
119  * NOTE: priv->mutex is not required before calling this function
120  */
121 int iwl_scan_cancel(struct iwl_priv *priv)
122 {
123         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
124                 clear_bit(STATUS_SCANNING, &priv->status);
125                 return 0;
126         }
127
128         if (test_bit(STATUS_SCANNING, &priv->status)) {
129                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
130                         IWL_DEBUG_SCAN("Queuing scan abort.\n");
131                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
132                         queue_work(priv->workqueue, &priv->abort_scan);
133
134                 } else
135                         IWL_DEBUG_SCAN("Scan abort already in progress.\n");
136
137                 return test_bit(STATUS_SCANNING, &priv->status);
138         }
139
140         return 0;
141 }
142 EXPORT_SYMBOL(iwl_scan_cancel);
143 /**
144  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
145  * @ms: amount of time to wait (in milliseconds) for scan to abort
146  *
147  * NOTE: priv->mutex must be held before calling this function
148  */
149 int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
150 {
151         unsigned long now = jiffies;
152         int ret;
153
154         ret = iwl_scan_cancel(priv);
155         if (ret && ms) {
156                 mutex_unlock(&priv->mutex);
157                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
158                                 test_bit(STATUS_SCANNING, &priv->status))
159                         msleep(1);
160                 mutex_lock(&priv->mutex);
161
162                 return test_bit(STATUS_SCANNING, &priv->status);
163         }
164
165         return ret;
166 }
167 EXPORT_SYMBOL(iwl_scan_cancel_timeout);
168
169 static int iwl_send_scan_abort(struct iwl_priv *priv)
170 {
171         int ret = 0;
172         struct iwl_rx_packet *res;
173         struct iwl_host_cmd cmd = {
174                 .id = REPLY_SCAN_ABORT_CMD,
175                 .meta.flags = CMD_WANT_SKB,
176         };
177
178         /* If there isn't a scan actively going on in the hardware
179          * then we are in between scan bands and not actually
180          * actively scanning, so don't send the abort command */
181         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
182                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
183                 return 0;
184         }
185
186         ret = iwl_send_cmd_sync(priv, &cmd);
187         if (ret) {
188                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
189                 return ret;
190         }
191
192         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
193         if (res->u.status != CAN_ABORT_STATUS) {
194                 /* The scan abort will return 1 for success or
195                  * 2 for "failure".  A failure condition can be
196                  * due to simply not being in an active scan which
197                  * can occur if we send the scan abort before we
198                  * the microcode has notified us that a scan is
199                  * completed. */
200                 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
201                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
202                 clear_bit(STATUS_SCAN_HW, &priv->status);
203         }
204
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 ((scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) && 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 (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
467                 IWL_ERROR("APs don't scan.\n");
468                 return 0;
469         }
470
471         if (!iwl_is_ready_rf(priv)) {
472                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
473                 return -EIO;
474         }
475
476         if (test_bit(STATUS_SCANNING, &priv->status)) {
477                 IWL_DEBUG_SCAN("Scan already in progress.\n");
478                 return -EAGAIN;
479         }
480
481         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
482                 IWL_DEBUG_SCAN("Scan request while abort pending.  "
483                                "Queuing.\n");
484                 return -EAGAIN;
485         }
486
487         IWL_DEBUG_INFO("Starting scan...\n");
488         if (priv->cfg->sku & IWL_SKU_G)
489                 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
490         if (priv->cfg->sku & IWL_SKU_A)
491                 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
492         set_bit(STATUS_SCANNING, &priv->status);
493         priv->scan_start = jiffies;
494         priv->scan_pass_start = priv->scan_start;
495
496         queue_work(priv->workqueue, &priv->request_scan);
497
498         return 0;
499 }
500 EXPORT_SYMBOL(iwl_scan_initiate);
501
502 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
503
504 static void iwl_bg_scan_check(struct work_struct *data)
505 {
506         struct iwl_priv *priv =
507             container_of(data, struct iwl_priv, scan_check.work);
508
509         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
510                 return;
511
512         mutex_lock(&priv->mutex);
513         if (test_bit(STATUS_SCANNING, &priv->status) ||
514             test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
515                 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
516                         "adapter (%dms)\n",
517                         jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
518
519                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
520                         iwl_send_scan_abort(priv);
521         }
522         mutex_unlock(&priv->mutex);
523 }
524 /**
525  * iwl_supported_rate_to_ie - fill in the supported rate in IE field
526  *
527  * return : set the bit for each supported rate insert in ie
528  */
529 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
530                                     u16 basic_rate, int *left)
531 {
532         u16 ret_rates = 0, bit;
533         int i;
534         u8 *cnt = ie;
535         u8 *rates = ie + 1;
536
537         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
538                 if (bit & supported_rate) {
539                         ret_rates |= bit;
540                         rates[*cnt] = iwl_rates[i].ieee |
541                                 ((bit & basic_rate) ? 0x80 : 0x00);
542                         (*cnt)++;
543                         (*left)--;
544                         if ((*left <= 0) ||
545                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
546                                 break;
547                 }
548         }
549
550         return ret_rates;
551 }
552
553
554 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
555                              u8 *pos, int *left)
556 {
557         struct ieee80211_ht_cap *ht_cap;
558
559         if (!sband || !sband->ht_info.ht_supported)
560                 return;
561
562         if (*left < sizeof(struct ieee80211_ht_cap))
563                 return;
564
565         *pos++ = sizeof(struct ieee80211_ht_cap);
566         ht_cap = (struct ieee80211_ht_cap *) pos;
567
568         ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
569         memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
570         ht_cap->ampdu_params_info =
571                 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
572                 ((sband->ht_info.ampdu_density << 2) &
573                         IEEE80211_HT_CAP_AMPDU_DENSITY);
574         *left -= sizeof(struct ieee80211_ht_cap);
575 }
576
577 /**
578  * iwl_fill_probe_req - fill in all required fields and IE for probe request
579  */
580
581 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
582                                   enum ieee80211_band band,
583                                   struct ieee80211_mgmt *frame,
584                                   int left)
585 {
586         int len = 0;
587         u8 *pos = NULL;
588         u16 active_rates, ret_rates, cck_rates, active_rate_basic;
589         const struct ieee80211_supported_band *sband =
590                                                 iwl_get_hw_mode(priv, band);
591
592
593         /* Make sure there is enough space for the probe request,
594          * two mandatory IEs and the data */
595         left -= 24;
596         if (left < 0)
597                 return 0;
598
599         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
600         memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
601         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
602         memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
603         frame->seq_ctrl = 0;
604
605         len += 24;
606
607         /* ...next IE... */
608         pos = &frame->u.probe_req.variable[0];
609
610         /* fill in our indirect SSID IE */
611         left -= 2;
612         if (left < 0)
613                 return 0;
614         *pos++ = WLAN_EID_SSID;
615         *pos++ = 0;
616
617         len += 2;
618
619         /* fill in supported rate */
620         left -= 2;
621         if (left < 0)
622                 return 0;
623
624         *pos++ = WLAN_EID_SUPP_RATES;
625         *pos = 0;
626
627         /* exclude 60M rate */
628         active_rates = priv->rates_mask;
629         active_rates &= ~IWL_RATE_60M_MASK;
630
631         active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
632
633         cck_rates = IWL_CCK_RATES_MASK & active_rates;
634         ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
635                                              active_rate_basic, &left);
636         active_rates &= ~ret_rates;
637
638         ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
639                                              active_rate_basic, &left);
640         active_rates &= ~ret_rates;
641
642         len += 2 + *pos;
643         pos += (*pos) + 1;
644
645         if (active_rates == 0)
646                 goto fill_end;
647
648         /* fill in supported extended rate */
649         /* ...next IE... */
650         left -= 2;
651         if (left < 0)
652                 return 0;
653         /* ... fill it in... */
654         *pos++ = WLAN_EID_EXT_SUPP_RATES;
655         *pos = 0;
656         iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left);
657         if (*pos > 0) {
658                 len += 2 + *pos;
659                 pos += (*pos) + 1;
660         } else {
661                 pos--;
662         }
663
664  fill_end:
665
666         left -= 2;
667         if (left < 0)
668                 return 0;
669
670         *pos++ = WLAN_EID_HT_CAPABILITY;
671         *pos = 0;
672         iwl_ht_cap_to_ie(sband, pos, &left);
673         if (*pos > 0)
674                 len += 2 + *pos;
675
676         return (u16)len;
677 }
678
679 static u32 iwl_scan_tx_ant(struct iwl_priv *priv, enum ieee80211_band band)
680 {
681         int i, ind;
682
683         ind = priv->scan_tx_ant[band];
684         for (i = 0; i < priv->hw_params.tx_chains_num; i++) {
685                 ind = (ind+1) >= priv->hw_params.tx_chains_num ? 0 : ind+1;
686                 if (priv->hw_params.valid_tx_ant & (1 << ind)) {
687                         priv->scan_tx_ant[band] = ind;
688                         break;
689                 }
690         }
691         IWL_DEBUG_SCAN("select TX ANT = %c\n", 'A' + ind);
692         return scan_tx_ant[ind];
693 }
694
695
696 static void iwl_bg_request_scan(struct work_struct *data)
697 {
698         struct iwl_priv *priv =
699             container_of(data, struct iwl_priv, request_scan);
700         struct iwl_host_cmd cmd = {
701                 .id = REPLY_SCAN_CMD,
702                 .len = sizeof(struct iwl_scan_cmd),
703                 .meta.flags = CMD_SIZE_HUGE,
704         };
705         struct iwl_scan_cmd *scan;
706         struct ieee80211_conf *conf = NULL;
707         int ret = 0;
708         u32 tx_ant;
709         u16 cmd_len;
710         enum ieee80211_band band;
711         u8 n_probes = 2;
712         u8 rx_chain = 0x7; /* bitmap: ABC chains */
713
714         conf = ieee80211_get_hw_conf(priv->hw);
715
716         mutex_lock(&priv->mutex);
717
718         if (!iwl_is_ready(priv)) {
719                 IWL_WARNING("request scan called when driver not ready.\n");
720                 goto done;
721         }
722
723         /* Make sure the scan wasn't cancelled before this queued work
724          * was given the chance to run... */
725         if (!test_bit(STATUS_SCANNING, &priv->status))
726                 goto done;
727
728         /* This should never be called or scheduled if there is currently
729          * a scan active in the hardware. */
730         if (test_bit(STATUS_SCAN_HW, &priv->status)) {
731                 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
732                                "Ignoring second request.\n");
733                 ret = -EIO;
734                 goto done;
735         }
736
737         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
738                 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
739                 goto done;
740         }
741
742         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
743                 IWL_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
744                 goto done;
745         }
746
747         if (iwl_is_rfkill(priv)) {
748                 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
749                 goto done;
750         }
751
752         if (!test_bit(STATUS_READY, &priv->status)) {
753                 IWL_DEBUG_HC("Scan request while uninitialized.  Queuing.\n");
754                 goto done;
755         }
756
757         if (!priv->scan_bands) {
758                 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
759                 goto done;
760         }
761
762         if (!priv->scan) {
763                 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
764                                      IWL_MAX_SCAN_SIZE, GFP_KERNEL);
765                 if (!priv->scan) {
766                         ret = -ENOMEM;
767                         goto done;
768                 }
769         }
770         scan = priv->scan;
771         memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
772
773         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
774         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
775
776         if (iwl_is_associated(priv)) {
777                 u16 interval = 0;
778                 u32 extra;
779                 u32 suspend_time = 100;
780                 u32 scan_suspend_time = 100;
781                 unsigned long flags;
782
783                 IWL_DEBUG_INFO("Scanning while associated...\n");
784
785                 spin_lock_irqsave(&priv->lock, flags);
786                 interval = priv->beacon_int;
787                 spin_unlock_irqrestore(&priv->lock, flags);
788
789                 scan->suspend_time = 0;
790                 scan->max_out_time = cpu_to_le32(200 * 1024);
791                 if (!interval)
792                         interval = suspend_time;
793
794                 extra = (suspend_time / interval) << 22;
795                 scan_suspend_time = (extra |
796                     ((suspend_time % interval) * 1024));
797                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
798                 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
799                                scan_suspend_time, interval);
800         }
801
802         /* We should add the ability for user to lock to PASSIVE ONLY */
803         if (priv->one_direct_scan) {
804                 IWL_DEBUG_SCAN("Start direct scan for '%s'\n",
805                                 iwl_escape_essid(priv->direct_ssid,
806                                 priv->direct_ssid_len));
807                 scan->direct_scan[0].id = WLAN_EID_SSID;
808                 scan->direct_scan[0].len = priv->direct_ssid_len;
809                 memcpy(scan->direct_scan[0].ssid,
810                        priv->direct_ssid, priv->direct_ssid_len);
811                 n_probes++;
812         } else if (!iwl_is_associated(priv) && priv->essid_len) {
813                 IWL_DEBUG_SCAN("Start direct scan for '%s' (not associated)\n",
814                                 iwl_escape_essid(priv->essid, priv->essid_len));
815                 scan->direct_scan[0].id = WLAN_EID_SSID;
816                 scan->direct_scan[0].len = priv->essid_len;
817                 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
818                 n_probes++;
819         } else {
820                 IWL_DEBUG_SCAN("Start indirect scan.\n");
821         }
822
823         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
824         scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
825         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
826
827
828         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
829                 band = IEEE80211_BAND_2GHZ;
830                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
831                 tx_ant = iwl_scan_tx_ant(priv, band);
832                 if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK)
833                         scan->tx_cmd.rate_n_flags =
834                                 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
835                                                         tx_ant);
836                 else
837                         scan->tx_cmd.rate_n_flags =
838                                 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
839                                                         tx_ant |
840                                                         RATE_MCS_CCK_MSK);
841                 scan->good_CRC_th = 0;
842         } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
843                 band = IEEE80211_BAND_5GHZ;
844                 tx_ant = iwl_scan_tx_ant(priv, band);
845                 scan->tx_cmd.rate_n_flags =
846                                 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
847                                                         tx_ant);
848                 scan->good_CRC_th = IWL_GOOD_CRC_TH;
849
850                 /* Force use of chains B and C (0x6) for scan Rx for 4965
851                  * Avoid A (0x1) because of its off-channel reception on A-band.
852                  * MIMO is not used here, but value is required */
853                 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
854                         rx_chain = 0x6;
855         } else {
856                 IWL_WARNING("Invalid scan band count\n");
857                 goto done;
858         }
859
860         scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
861                                 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
862                                 (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) |
863                                 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
864
865         cmd_len = iwl_fill_probe_req(priv, band,
866                                      (struct ieee80211_mgmt *)scan->data,
867                                      IWL_MAX_SCAN_SIZE - sizeof(*scan));
868
869         scan->tx_cmd.len = cpu_to_le16(cmd_len);
870
871         if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
872                 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
873
874         scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
875                                RXON_FILTER_BCON_AWARE_MSK);
876
877         scan->channel_count =
878                 iwl_get_channels_for_scan(priv, band, 1, /* active */
879                         n_probes,
880                         (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
881
882         if (scan->channel_count == 0) {
883                 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
884                 goto done;
885         }
886
887         cmd.len += le16_to_cpu(scan->tx_cmd.len) +
888             scan->channel_count * sizeof(struct iwl_scan_channel);
889         cmd.data = scan;
890         scan->len = cpu_to_le16(cmd.len);
891
892         set_bit(STATUS_SCAN_HW, &priv->status);
893         ret = iwl_send_cmd_sync(priv, &cmd);
894         if (ret)
895                 goto done;
896
897         queue_delayed_work(priv->workqueue, &priv->scan_check,
898                            IWL_SCAN_CHECK_WATCHDOG);
899
900         mutex_unlock(&priv->mutex);
901         return;
902
903  done:
904         /* inform mac80211 scan aborted */
905         queue_work(priv->workqueue, &priv->scan_completed);
906         mutex_unlock(&priv->mutex);
907 }
908
909 static void iwl_bg_abort_scan(struct work_struct *work)
910 {
911         struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
912
913         if (!iwl_is_ready(priv))
914                 return;
915
916         mutex_lock(&priv->mutex);
917
918         set_bit(STATUS_SCAN_ABORTING, &priv->status);
919         iwl_send_scan_abort(priv);
920
921         mutex_unlock(&priv->mutex);
922 }
923
924 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
925 {
926         /*  FIXME: move here when resolved PENDING
927          *  INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed); */
928         INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
929         INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
930         INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
931 }
932 EXPORT_SYMBOL(iwl_setup_scan_deferred_work);
933