Merge branch 'core/xen' into x86/xen
[safe/jmp/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         priv->alloc_rxb_skb--;
206         dev_kfree_skb_any(cmd.meta.u.skb);
207
208         return ret;
209 }
210
211
212 /* Service response to REPLY_SCAN_CMD (0x80) */
213 static void iwl_rx_reply_scan(struct iwl_priv *priv,
214                               struct iwl_rx_mem_buffer *rxb)
215 {
216 #ifdef CONFIG_IWLWIFI_DEBUG
217         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
218         struct iwl_scanreq_notification *notif =
219             (struct iwl_scanreq_notification *)pkt->u.raw;
220
221         IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
222 #endif
223 }
224
225 /* Service SCAN_START_NOTIFICATION (0x82) */
226 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
227                                     struct iwl_rx_mem_buffer *rxb)
228 {
229         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
230         struct iwl_scanstart_notification *notif =
231             (struct iwl_scanstart_notification *)pkt->u.raw;
232         priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
233         IWL_DEBUG_SCAN("Scan start: "
234                        "%d [802.11%s] "
235                        "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
236                        notif->channel,
237                        notif->band ? "bg" : "a",
238                        le32_to_cpu(notif->tsf_high),
239                        le32_to_cpu(notif->tsf_low),
240                        notif->status, notif->beacon_timer);
241 }
242
243 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
244 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
245                                       struct iwl_rx_mem_buffer *rxb)
246 {
247 #ifdef CONFIG_IWLWIFI_DEBUG
248         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
249         struct iwl_scanresults_notification *notif =
250             (struct iwl_scanresults_notification *)pkt->u.raw;
251
252         IWL_DEBUG_SCAN("Scan ch.res: "
253                        "%d [802.11%s] "
254                        "(TSF: 0x%08X:%08X) - %d "
255                        "elapsed=%lu usec (%dms since last)\n",
256                        notif->channel,
257                        notif->band ? "bg" : "a",
258                        le32_to_cpu(notif->tsf_high),
259                        le32_to_cpu(notif->tsf_low),
260                        le32_to_cpu(notif->statistics[0]),
261                        le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
262                        jiffies_to_msecs(elapsed_jiffies
263                                         (priv->last_scan_jiffies, jiffies)));
264 #endif
265
266         priv->last_scan_jiffies = jiffies;
267         priv->next_scan_jiffies = 0;
268 }
269
270 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
271 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
272                                        struct iwl_rx_mem_buffer *rxb)
273 {
274 #ifdef CONFIG_IWLWIFI_DEBUG
275         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
276         struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
277
278         IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
279                        scan_notif->scanned_channels,
280                        scan_notif->tsf_low,
281                        scan_notif->tsf_high, scan_notif->status);
282 #endif
283
284         /* The HW is no longer scanning */
285         clear_bit(STATUS_SCAN_HW, &priv->status);
286
287         /* The scan completion notification came in, so kill that timer... */
288         cancel_delayed_work(&priv->scan_check);
289
290         IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
291                        (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
292                                                 "2.4" : "5.2",
293                        jiffies_to_msecs(elapsed_jiffies
294                                         (priv->scan_pass_start, jiffies)));
295
296         /* Remove this scanned band from the list of pending
297          * bands to scan, band G precedes A in order of scanning
298          * as seen in iwl_bg_request_scan */
299         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
300                 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
301         else if (priv->scan_bands &  BIT(IEEE80211_BAND_5GHZ))
302                 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
303
304         /* If a request to abort was given, or the scan did not succeed
305          * then we reset the scan state machine and terminate,
306          * re-queuing another scan if one has been requested */
307         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
308                 IWL_DEBUG_INFO("Aborted scan completed.\n");
309                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
310         } else {
311                 /* If there are more bands on this scan pass reschedule */
312                 if (priv->scan_bands)
313                         goto reschedule;
314         }
315
316         priv->last_scan_jiffies = jiffies;
317         priv->next_scan_jiffies = 0;
318         IWL_DEBUG_INFO("Setting scan to off\n");
319
320         clear_bit(STATUS_SCANNING, &priv->status);
321
322         IWL_DEBUG_INFO("Scan took %dms\n",
323                 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
324
325         queue_work(priv->workqueue, &priv->scan_completed);
326
327         return;
328
329 reschedule:
330         priv->scan_pass_start = jiffies;
331         queue_work(priv->workqueue, &priv->request_scan);
332 }
333
334 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
335 {
336         /* scan handlers */
337         priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
338         priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
339         priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
340                                         iwl_rx_scan_results_notif;
341         priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
342                                         iwl_rx_scan_complete_notif;
343 }
344 EXPORT_SYMBOL(iwl_setup_rx_scan_handlers);
345
346 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
347                                             enum ieee80211_band band,
348                                             u8 n_probes)
349 {
350         if (band == IEEE80211_BAND_5GHZ)
351                 return IWL_ACTIVE_DWELL_TIME_52 +
352                         IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
353         else
354                 return IWL_ACTIVE_DWELL_TIME_24 +
355                         IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
356 }
357
358 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
359                                       enum ieee80211_band band)
360 {
361         u16 passive = (band == IEEE80211_BAND_2GHZ) ?
362             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
363             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
364
365         if (iwl_is_associated(priv)) {
366                 /* If we're associated, we clamp the maximum passive
367                  * dwell time to be 98% of the beacon interval (minus
368                  * 2 * channel tune time) */
369                 passive = priv->beacon_int;
370                 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
371                         passive = IWL_PASSIVE_DWELL_BASE;
372                 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
373         }
374
375         return passive;
376 }
377
378 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
379                                      enum ieee80211_band band,
380                                      u8 is_active, u8 n_probes,
381                                      struct iwl_scan_channel *scan_ch)
382 {
383         const struct ieee80211_channel *channels = NULL;
384         const struct ieee80211_supported_band *sband;
385         const struct iwl_channel_info *ch_info;
386         u16 passive_dwell = 0;
387         u16 active_dwell = 0;
388         int added, i;
389         u16 channel;
390
391         sband = iwl_get_hw_mode(priv, band);
392         if (!sband)
393                 return 0;
394
395         channels = sband->channels;
396
397         active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
398         passive_dwell = iwl_get_passive_dwell_time(priv, band);
399
400         if (passive_dwell <= active_dwell)
401                 passive_dwell = active_dwell + 1;
402
403         for (i = 0, added = 0; i < sband->n_channels; i++) {
404                 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
405                         continue;
406
407                 channel =
408                         ieee80211_frequency_to_channel(channels[i].center_freq);
409                 scan_ch->channel = cpu_to_le16(channel);
410
411                 ch_info = iwl_get_channel_info(priv, band, channel);
412                 if (!is_channel_valid(ch_info)) {
413                         IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
414                                         channel);
415                         continue;
416                 }
417
418                 if (!is_active || is_channel_passive(ch_info) ||
419                     (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
420                         scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
421                 else
422                         scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
423
424                 if (n_probes)
425                         scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
426
427                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
428                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
429
430                 /* Set txpower levels to defaults */
431                 scan_ch->dsp_atten = 110;
432
433                 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
434                  * power level:
435                  * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
436                  */
437                 if (band == IEEE80211_BAND_5GHZ)
438                         scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
439                 else
440                         scan_ch->tx_gain = ((1 << 5) | (5 << 3));
441
442                 IWL_DEBUG_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n",
443                                channel, le32_to_cpu(scan_ch->type),
444                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
445                                 "ACTIVE" : "PASSIVE",
446                                (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
447                                active_dwell : passive_dwell);
448
449                 scan_ch++;
450                 added++;
451         }
452
453         IWL_DEBUG_SCAN("total channels to scan %d \n", added);
454         return added;
455 }
456
457 void iwl_init_scan_params(struct iwl_priv *priv)
458 {
459         if (!priv->scan_tx_ant[IEEE80211_BAND_5GHZ])
460                 priv->scan_tx_ant[IEEE80211_BAND_5GHZ] = RATE_MCS_ANT_INIT_IND;
461         if (!priv->scan_tx_ant[IEEE80211_BAND_2GHZ])
462                 priv->scan_tx_ant[IEEE80211_BAND_2GHZ] = RATE_MCS_ANT_INIT_IND;
463 }
464
465 int iwl_scan_initiate(struct iwl_priv *priv)
466 {
467         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
468                 IWL_ERROR("APs don't scan.\n");
469                 return 0;
470         }
471
472         if (!iwl_is_ready_rf(priv)) {
473                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
474                 return -EIO;
475         }
476
477         if (test_bit(STATUS_SCANNING, &priv->status)) {
478                 IWL_DEBUG_SCAN("Scan already in progress.\n");
479                 return -EAGAIN;
480         }
481
482         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
483                 IWL_DEBUG_SCAN("Scan request while abort pending.  "
484                                "Queuing.\n");
485                 return -EAGAIN;
486         }
487
488         IWL_DEBUG_INFO("Starting scan...\n");
489         if (priv->cfg->sku & IWL_SKU_G)
490                 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
491         if (priv->cfg->sku & IWL_SKU_A)
492                 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
493         set_bit(STATUS_SCANNING, &priv->status);
494         priv->scan_start = jiffies;
495         priv->scan_pass_start = priv->scan_start;
496
497         queue_work(priv->workqueue, &priv->request_scan);
498
499         return 0;
500 }
501 EXPORT_SYMBOL(iwl_scan_initiate);
502
503 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
504
505 static void iwl_bg_scan_check(struct work_struct *data)
506 {
507         struct iwl_priv *priv =
508             container_of(data, struct iwl_priv, scan_check.work);
509
510         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
511                 return;
512
513         mutex_lock(&priv->mutex);
514         if (test_bit(STATUS_SCANNING, &priv->status) ||
515             test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
516                 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
517                         "adapter (%dms)\n",
518                         jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
519
520                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
521                         iwl_send_scan_abort(priv);
522         }
523         mutex_unlock(&priv->mutex);
524 }
525 /**
526  * iwl_supported_rate_to_ie - fill in the supported rate in IE field
527  *
528  * return : set the bit for each supported rate insert in ie
529  */
530 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
531                                     u16 basic_rate, int *left)
532 {
533         u16 ret_rates = 0, bit;
534         int i;
535         u8 *cnt = ie;
536         u8 *rates = ie + 1;
537
538         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
539                 if (bit & supported_rate) {
540                         ret_rates |= bit;
541                         rates[*cnt] = iwl_rates[i].ieee |
542                                 ((bit & basic_rate) ? 0x80 : 0x00);
543                         (*cnt)++;
544                         (*left)--;
545                         if ((*left <= 0) ||
546                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
547                                 break;
548                 }
549         }
550
551         return ret_rates;
552 }
553
554
555 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
556                              u8 *pos, int *left)
557 {
558         struct ieee80211_ht_cap *ht_cap;
559
560         if (!sband || !sband->ht_info.ht_supported)
561                 return;
562
563         if (*left < sizeof(struct ieee80211_ht_cap))
564                 return;
565
566         *pos++ = sizeof(struct ieee80211_ht_cap);
567         ht_cap = (struct ieee80211_ht_cap *) pos;
568
569         ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
570         memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
571         ht_cap->ampdu_params_info =
572                 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
573                 ((sband->ht_info.ampdu_density << 2) &
574                         IEEE80211_HT_CAP_AMPDU_DENSITY);
575         *left -= sizeof(struct ieee80211_ht_cap);
576 }
577
578 /**
579  * iwl_fill_probe_req - fill in all required fields and IE for probe request
580  */
581
582 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
583                                   enum ieee80211_band band,
584                                   struct ieee80211_mgmt *frame,
585                                   int left)
586 {
587         int len = 0;
588         u8 *pos = NULL;
589         u16 active_rates, ret_rates, cck_rates, active_rate_basic;
590         const struct ieee80211_supported_band *sband =
591                                                 iwl_get_hw_mode(priv, band);
592
593
594         /* Make sure there is enough space for the probe request,
595          * two mandatory IEs and the data */
596         left -= 24;
597         if (left < 0)
598                 return 0;
599
600         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
601         memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
602         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
603         memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
604         frame->seq_ctrl = 0;
605
606         len += 24;
607
608         /* ...next IE... */
609         pos = &frame->u.probe_req.variable[0];
610
611         /* fill in our indirect SSID IE */
612         left -= 2;
613         if (left < 0)
614                 return 0;
615         *pos++ = WLAN_EID_SSID;
616         *pos++ = 0;
617
618         len += 2;
619
620         /* fill in supported rate */
621         left -= 2;
622         if (left < 0)
623                 return 0;
624
625         *pos++ = WLAN_EID_SUPP_RATES;
626         *pos = 0;
627
628         /* exclude 60M rate */
629         active_rates = priv->rates_mask;
630         active_rates &= ~IWL_RATE_60M_MASK;
631
632         active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
633
634         cck_rates = IWL_CCK_RATES_MASK & active_rates;
635         ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
636                                              active_rate_basic, &left);
637         active_rates &= ~ret_rates;
638
639         ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
640                                              active_rate_basic, &left);
641         active_rates &= ~ret_rates;
642
643         len += 2 + *pos;
644         pos += (*pos) + 1;
645
646         if (active_rates == 0)
647                 goto fill_end;
648
649         /* fill in supported extended rate */
650         /* ...next IE... */
651         left -= 2;
652         if (left < 0)
653                 return 0;
654         /* ... fill it in... */
655         *pos++ = WLAN_EID_EXT_SUPP_RATES;
656         *pos = 0;
657         iwl_supported_rate_to_ie(pos, active_rates, active_rate_basic, &left);
658         if (*pos > 0) {
659                 len += 2 + *pos;
660                 pos += (*pos) + 1;
661         } else {
662                 pos--;
663         }
664
665  fill_end:
666
667         left -= 2;
668         if (left < 0)
669                 return 0;
670
671         *pos++ = WLAN_EID_HT_CAPABILITY;
672         *pos = 0;
673         iwl_ht_cap_to_ie(sband, pos, &left);
674         if (*pos > 0)
675                 len += 2 + *pos;
676
677         return (u16)len;
678 }
679
680 static u32 iwl_scan_tx_ant(struct iwl_priv *priv, enum ieee80211_band band)
681 {
682         int i, ind;
683
684         ind = priv->scan_tx_ant[band];
685         for (i = 0; i < priv->hw_params.tx_chains_num; i++) {
686                 ind = (ind+1) >= priv->hw_params.tx_chains_num ? 0 : ind+1;
687                 if (priv->hw_params.valid_tx_ant & (1 << ind)) {
688                         priv->scan_tx_ant[band] = ind;
689                         break;
690                 }
691         }
692         IWL_DEBUG_SCAN("select TX ANT = %c\n", 'A' + ind);
693         return scan_tx_ant[ind];
694 }
695
696
697 static void iwl_bg_request_scan(struct work_struct *data)
698 {
699         struct iwl_priv *priv =
700             container_of(data, struct iwl_priv, request_scan);
701         struct iwl_host_cmd cmd = {
702                 .id = REPLY_SCAN_CMD,
703                 .len = sizeof(struct iwl_scan_cmd),
704                 .meta.flags = CMD_SIZE_HUGE,
705         };
706         struct iwl_scan_cmd *scan;
707         struct ieee80211_conf *conf = NULL;
708         int ret = 0;
709         u32 tx_ant;
710         u16 cmd_len;
711         enum ieee80211_band band;
712         u8 n_probes = 2;
713         u8 rx_chain = 0x7; /* bitmap: ABC chains */
714
715         conf = ieee80211_get_hw_conf(priv->hw);
716
717         mutex_lock(&priv->mutex);
718
719         if (!iwl_is_ready(priv)) {
720                 IWL_WARNING("request scan called when driver not ready.\n");
721                 goto done;
722         }
723
724         /* Make sure the scan wasn't cancelled before this queued work
725          * was given the chance to run... */
726         if (!test_bit(STATUS_SCANNING, &priv->status))
727                 goto done;
728
729         /* This should never be called or scheduled if there is currently
730          * a scan active in the hardware. */
731         if (test_bit(STATUS_SCAN_HW, &priv->status)) {
732                 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
733                                "Ignoring second request.\n");
734                 ret = -EIO;
735                 goto done;
736         }
737
738         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
739                 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
740                 goto done;
741         }
742
743         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
744                 IWL_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
745                 goto done;
746         }
747
748         if (iwl_is_rfkill(priv)) {
749                 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
750                 goto done;
751         }
752
753         if (!test_bit(STATUS_READY, &priv->status)) {
754                 IWL_DEBUG_HC("Scan request while uninitialized.  Queuing.\n");
755                 goto done;
756         }
757
758         if (!priv->scan_bands) {
759                 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
760                 goto done;
761         }
762
763         if (!priv->scan) {
764                 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
765                                      IWL_MAX_SCAN_SIZE, GFP_KERNEL);
766                 if (!priv->scan) {
767                         ret = -ENOMEM;
768                         goto done;
769                 }
770         }
771         scan = priv->scan;
772         memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
773
774         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
775         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
776
777         if (iwl_is_associated(priv)) {
778                 u16 interval = 0;
779                 u32 extra;
780                 u32 suspend_time = 100;
781                 u32 scan_suspend_time = 100;
782                 unsigned long flags;
783
784                 IWL_DEBUG_INFO("Scanning while associated...\n");
785
786                 spin_lock_irqsave(&priv->lock, flags);
787                 interval = priv->beacon_int;
788                 spin_unlock_irqrestore(&priv->lock, flags);
789
790                 scan->suspend_time = 0;
791                 scan->max_out_time = cpu_to_le32(200 * 1024);
792                 if (!interval)
793                         interval = suspend_time;
794
795                 extra = (suspend_time / interval) << 22;
796                 scan_suspend_time = (extra |
797                     ((suspend_time % interval) * 1024));
798                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
799                 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
800                                scan_suspend_time, interval);
801         }
802
803         /* We should add the ability for user to lock to PASSIVE ONLY */
804         if (priv->one_direct_scan) {
805                 IWL_DEBUG_SCAN("Start direct scan for '%s'\n",
806                                 iwl_escape_essid(priv->direct_ssid,
807                                 priv->direct_ssid_len));
808                 scan->direct_scan[0].id = WLAN_EID_SSID;
809                 scan->direct_scan[0].len = priv->direct_ssid_len;
810                 memcpy(scan->direct_scan[0].ssid,
811                        priv->direct_ssid, priv->direct_ssid_len);
812                 n_probes++;
813         } else if (!iwl_is_associated(priv) && priv->essid_len) {
814                 IWL_DEBUG_SCAN("Start direct scan for '%s' (not associated)\n",
815                                 iwl_escape_essid(priv->essid, priv->essid_len));
816                 scan->direct_scan[0].id = WLAN_EID_SSID;
817                 scan->direct_scan[0].len = priv->essid_len;
818                 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
819                 n_probes++;
820         } else {
821                 IWL_DEBUG_SCAN("Start indirect scan.\n");
822         }
823
824         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
825         scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
826         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
827
828
829         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
830                 band = IEEE80211_BAND_2GHZ;
831                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
832                 tx_ant = iwl_scan_tx_ant(priv, band);
833                 if (priv->active_rxon.flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK)
834                         scan->tx_cmd.rate_n_flags =
835                                 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
836                                                         tx_ant);
837                 else
838                         scan->tx_cmd.rate_n_flags =
839                                 iwl_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
840                                                         tx_ant |
841                                                         RATE_MCS_CCK_MSK);
842                 scan->good_CRC_th = 0;
843         } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
844                 band = IEEE80211_BAND_5GHZ;
845                 tx_ant = iwl_scan_tx_ant(priv, band);
846                 scan->tx_cmd.rate_n_flags =
847                                 iwl_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
848                                                         tx_ant);
849                 scan->good_CRC_th = IWL_GOOD_CRC_TH;
850
851                 /* Force use of chains B and C (0x6) for scan Rx for 4965
852                  * Avoid A (0x1) because of its off-channel reception on A-band.
853                  * MIMO is not used here, but value is required */
854                 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
855                         rx_chain = 0x6;
856         } else {
857                 IWL_WARNING("Invalid scan band count\n");
858                 goto done;
859         }
860
861         scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
862                                 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
863                                 (rx_chain << RXON_RX_CHAIN_FORCE_SEL_POS) |
864                                 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
865
866         cmd_len = iwl_fill_probe_req(priv, band,
867                                      (struct ieee80211_mgmt *)scan->data,
868                                      IWL_MAX_SCAN_SIZE - sizeof(*scan));
869
870         scan->tx_cmd.len = cpu_to_le16(cmd_len);
871
872         if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
873                 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
874
875         scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
876                                RXON_FILTER_BCON_AWARE_MSK);
877
878         scan->channel_count =
879                 iwl_get_channels_for_scan(priv, band, 1, /* active */
880                         n_probes,
881                         (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
882
883         if (scan->channel_count == 0) {
884                 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
885                 goto done;
886         }
887
888         cmd.len += le16_to_cpu(scan->tx_cmd.len) +
889             scan->channel_count * sizeof(struct iwl_scan_channel);
890         cmd.data = scan;
891         scan->len = cpu_to_le16(cmd.len);
892
893         set_bit(STATUS_SCAN_HW, &priv->status);
894         ret = iwl_send_cmd_sync(priv, &cmd);
895         if (ret)
896                 goto done;
897
898         queue_delayed_work(priv->workqueue, &priv->scan_check,
899                            IWL_SCAN_CHECK_WATCHDOG);
900
901         mutex_unlock(&priv->mutex);
902         return;
903
904  done:
905         /* inform mac80211 scan aborted */
906         queue_work(priv->workqueue, &priv->scan_completed);
907         mutex_unlock(&priv->mutex);
908 }
909
910 static void iwl_bg_abort_scan(struct work_struct *work)
911 {
912         struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
913
914         if (!iwl_is_ready(priv))
915                 return;
916
917         mutex_lock(&priv->mutex);
918
919         set_bit(STATUS_SCAN_ABORTING, &priv->status);
920         iwl_send_scan_abort(priv);
921
922         mutex_unlock(&priv->mutex);
923 }
924
925 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
926 {
927         /*  FIXME: move here when resolved PENDING
928          *  INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed); */
929         INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
930         INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
931         INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
932 }
933 EXPORT_SYMBOL(iwl_setup_scan_deferred_work);
934