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