Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003  *
0004  * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
0005  * Copyright(c) 2018        Intel Corporation
0006  *****************************************************************************/
0007 #include <linux/slab.h>
0008 #include <linux/types.h>
0009 #include <linux/etherdevice.h>
0010 #include <net/mac80211.h>
0011 
0012 #include "dev.h"
0013 #include "agn.h"
0014 
0015 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
0016  * sending probe req.  This should be set long enough to hear probe responses
0017  * from more than one AP.  */
0018 #define IWL_ACTIVE_DWELL_TIME_24    (30)       /* all times in msec */
0019 #define IWL_ACTIVE_DWELL_TIME_52    (20)
0020 
0021 #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
0022 #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
0023 
0024 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
0025  * Must be set longer than active dwell time.
0026  * For the most reliable scan, set > AP beacon interval (typically 100msec). */
0027 #define IWL_PASSIVE_DWELL_TIME_24   (20)       /* all times in msec */
0028 #define IWL_PASSIVE_DWELL_TIME_52   (10)
0029 #define IWL_PASSIVE_DWELL_BASE      (100)
0030 #define IWL_CHANNEL_TUNE_TIME       5
0031 #define MAX_SCAN_CHANNEL        50
0032 
0033 /* For reset radio, need minimal dwell time only */
0034 #define IWL_RADIO_RESET_DWELL_TIME  5
0035 
0036 static int iwl_send_scan_abort(struct iwl_priv *priv)
0037 {
0038     int ret;
0039     struct iwl_host_cmd cmd = {
0040         .id = REPLY_SCAN_ABORT_CMD,
0041         .flags = CMD_WANT_SKB,
0042     };
0043     __le32 *status;
0044 
0045     /* Exit instantly with error when device is not ready
0046      * to receive scan abort command or it does not perform
0047      * hardware scan currently */
0048     if (!test_bit(STATUS_READY, &priv->status) ||
0049         !test_bit(STATUS_SCAN_HW, &priv->status) ||
0050         test_bit(STATUS_FW_ERROR, &priv->status))
0051         return -EIO;
0052 
0053     ret = iwl_dvm_send_cmd(priv, &cmd);
0054     if (ret)
0055         return ret;
0056 
0057     status = (void *)cmd.resp_pkt->data;
0058     if (*status != CAN_ABORT_STATUS) {
0059         /* The scan abort will return 1 for success or
0060          * 2 for "failure".  A failure condition can be
0061          * due to simply not being in an active scan which
0062          * can occur if we send the scan abort before we
0063          * the microcode has notified us that a scan is
0064          * completed. */
0065         IWL_DEBUG_SCAN(priv, "SCAN_ABORT ret %d.\n",
0066                    le32_to_cpu(*status));
0067         ret = -EIO;
0068     }
0069 
0070     iwl_free_resp(&cmd);
0071     return ret;
0072 }
0073 
0074 static void iwl_complete_scan(struct iwl_priv *priv, bool aborted)
0075 {
0076     struct cfg80211_scan_info info = {
0077         .aborted = aborted,
0078     };
0079 
0080     /* check if scan was requested from mac80211 */
0081     if (priv->scan_request) {
0082         IWL_DEBUG_SCAN(priv, "Complete scan in mac80211\n");
0083         ieee80211_scan_completed(priv->hw, &info);
0084     }
0085 
0086     priv->scan_type = IWL_SCAN_NORMAL;
0087     priv->scan_vif = NULL;
0088     priv->scan_request = NULL;
0089 }
0090 
0091 static void iwl_process_scan_complete(struct iwl_priv *priv)
0092 {
0093     bool aborted;
0094 
0095     lockdep_assert_held(&priv->mutex);
0096 
0097     if (!test_and_clear_bit(STATUS_SCAN_COMPLETE, &priv->status))
0098         return;
0099 
0100     IWL_DEBUG_SCAN(priv, "Completed scan.\n");
0101 
0102     cancel_delayed_work(&priv->scan_check);
0103 
0104     aborted = test_and_clear_bit(STATUS_SCAN_ABORTING, &priv->status);
0105     if (aborted)
0106         IWL_DEBUG_SCAN(priv, "Aborted scan completed.\n");
0107 
0108     if (!test_and_clear_bit(STATUS_SCANNING, &priv->status)) {
0109         IWL_DEBUG_SCAN(priv, "Scan already completed.\n");
0110         goto out_settings;
0111     }
0112 
0113     if (priv->scan_type != IWL_SCAN_NORMAL && !aborted) {
0114         int err;
0115 
0116         /* Check if mac80211 requested scan during our internal scan */
0117         if (priv->scan_request == NULL)
0118             goto out_complete;
0119 
0120         /* If so request a new scan */
0121         err = iwl_scan_initiate(priv, priv->scan_vif, IWL_SCAN_NORMAL,
0122                     priv->scan_request->channels[0]->band);
0123         if (err) {
0124             IWL_DEBUG_SCAN(priv,
0125                 "failed to initiate pending scan: %d\n", err);
0126             aborted = true;
0127             goto out_complete;
0128         }
0129 
0130         return;
0131     }
0132 
0133 out_complete:
0134     iwl_complete_scan(priv, aborted);
0135 
0136 out_settings:
0137     /* Can we still talk to firmware ? */
0138     if (!iwl_is_ready_rf(priv))
0139         return;
0140 
0141     iwlagn_post_scan(priv);
0142 }
0143 
0144 void iwl_force_scan_end(struct iwl_priv *priv)
0145 {
0146     lockdep_assert_held(&priv->mutex);
0147 
0148     if (!test_bit(STATUS_SCANNING, &priv->status)) {
0149         IWL_DEBUG_SCAN(priv, "Forcing scan end while not scanning\n");
0150         return;
0151     }
0152 
0153     IWL_DEBUG_SCAN(priv, "Forcing scan end\n");
0154     clear_bit(STATUS_SCANNING, &priv->status);
0155     clear_bit(STATUS_SCAN_HW, &priv->status);
0156     clear_bit(STATUS_SCAN_ABORTING, &priv->status);
0157     clear_bit(STATUS_SCAN_COMPLETE, &priv->status);
0158     iwl_complete_scan(priv, true);
0159 }
0160 
0161 static void iwl_do_scan_abort(struct iwl_priv *priv)
0162 {
0163     int ret;
0164 
0165     lockdep_assert_held(&priv->mutex);
0166 
0167     if (!test_bit(STATUS_SCANNING, &priv->status)) {
0168         IWL_DEBUG_SCAN(priv, "Not performing scan to abort\n");
0169         return;
0170     }
0171 
0172     if (test_and_set_bit(STATUS_SCAN_ABORTING, &priv->status)) {
0173         IWL_DEBUG_SCAN(priv, "Scan abort in progress\n");
0174         return;
0175     }
0176 
0177     ret = iwl_send_scan_abort(priv);
0178     if (ret) {
0179         IWL_DEBUG_SCAN(priv, "Send scan abort failed %d\n", ret);
0180         iwl_force_scan_end(priv);
0181     } else
0182         IWL_DEBUG_SCAN(priv, "Successfully send scan abort\n");
0183 }
0184 
0185 /*
0186  * iwl_scan_cancel - Cancel any currently executing HW scan
0187  */
0188 int iwl_scan_cancel(struct iwl_priv *priv)
0189 {
0190     IWL_DEBUG_SCAN(priv, "Queuing abort scan\n");
0191     queue_work(priv->workqueue, &priv->abort_scan);
0192     return 0;
0193 }
0194 
0195 /*
0196  * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
0197  * @ms: amount of time to wait (in milliseconds) for scan to abort
0198  */
0199 void iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
0200 {
0201     unsigned long timeout = jiffies + msecs_to_jiffies(ms);
0202 
0203     lockdep_assert_held(&priv->mutex);
0204 
0205     IWL_DEBUG_SCAN(priv, "Scan cancel timeout\n");
0206 
0207     iwl_do_scan_abort(priv);
0208 
0209     while (time_before_eq(jiffies, timeout)) {
0210         if (!test_bit(STATUS_SCAN_HW, &priv->status))
0211             goto finished;
0212         msleep(20);
0213     }
0214 
0215     return;
0216 
0217  finished:
0218     /*
0219      * Now STATUS_SCAN_HW is clear. This means that the
0220      * device finished, but the background work is going
0221      * to execute at best as soon as we release the mutex.
0222      * Since we need to be able to issue a new scan right
0223      * after this function returns, run the complete here.
0224      * The STATUS_SCAN_COMPLETE bit will then be cleared
0225      * and prevent the background work from "completing"
0226      * a possible new scan.
0227      */
0228     iwl_process_scan_complete(priv);
0229 }
0230 
0231 /* Service response to REPLY_SCAN_CMD (0x80) */
0232 static void iwl_rx_reply_scan(struct iwl_priv *priv,
0233                   struct iwl_rx_cmd_buffer *rxb)
0234 {
0235 #ifdef CONFIG_IWLWIFI_DEBUG
0236     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0237     struct iwl_scanreq_notification *notif = (void *)pkt->data;
0238 
0239     IWL_DEBUG_SCAN(priv, "Scan request status = 0x%x\n", notif->status);
0240 #endif
0241 }
0242 
0243 /* Service SCAN_START_NOTIFICATION (0x82) */
0244 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
0245                     struct iwl_rx_cmd_buffer *rxb)
0246 {
0247     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0248     struct iwl_scanstart_notification *notif = (void *)pkt->data;
0249 
0250     priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
0251     IWL_DEBUG_SCAN(priv, "Scan start: "
0252                "%d [802.11%s] "
0253                "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
0254                notif->channel,
0255                notif->band ? "bg" : "a",
0256                le32_to_cpu(notif->tsf_high),
0257                le32_to_cpu(notif->tsf_low),
0258                notif->status, notif->beacon_timer);
0259 }
0260 
0261 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
0262 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
0263                       struct iwl_rx_cmd_buffer *rxb)
0264 {
0265 #ifdef CONFIG_IWLWIFI_DEBUG
0266     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0267     struct iwl_scanresults_notification *notif = (void *)pkt->data;
0268 
0269     IWL_DEBUG_SCAN(priv, "Scan ch.res: "
0270                "%d [802.11%s] "
0271                "probe status: %u:%u "
0272                "(TSF: 0x%08X:%08X) - %d "
0273                "elapsed=%lu usec\n",
0274                notif->channel,
0275                notif->band ? "bg" : "a",
0276                notif->probe_status, notif->num_probe_not_sent,
0277                le32_to_cpu(notif->tsf_high),
0278                le32_to_cpu(notif->tsf_low),
0279                le32_to_cpu(notif->statistics[0]),
0280                le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf);
0281 #endif
0282 }
0283 
0284 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
0285 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
0286                        struct iwl_rx_cmd_buffer *rxb)
0287 {
0288     struct iwl_rx_packet *pkt = rxb_addr(rxb);
0289     struct iwl_scancomplete_notification *scan_notif = (void *)pkt->data;
0290 
0291     IWL_DEBUG_SCAN(priv, "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
0292                scan_notif->scanned_channels,
0293                scan_notif->tsf_low,
0294                scan_notif->tsf_high, scan_notif->status);
0295 
0296     IWL_DEBUG_SCAN(priv, "Scan on %sGHz took %dms\n",
0297                (priv->scan_band == NL80211_BAND_2GHZ) ? "2.4" : "5.2",
0298                jiffies_to_msecs(jiffies - priv->scan_start));
0299 
0300     /*
0301      * When aborting, we run the scan completed background work inline
0302      * and the background work must then do nothing. The SCAN_COMPLETE
0303      * bit helps implement that logic and thus needs to be set before
0304      * queueing the work. Also, since the scan abort waits for SCAN_HW
0305      * to clear, we need to set SCAN_COMPLETE before clearing SCAN_HW
0306      * to avoid a race there.
0307      */
0308     set_bit(STATUS_SCAN_COMPLETE, &priv->status);
0309     clear_bit(STATUS_SCAN_HW, &priv->status);
0310     queue_work(priv->workqueue, &priv->scan_completed);
0311 
0312     if (priv->iw_mode != NL80211_IFTYPE_ADHOC &&
0313         iwl_advanced_bt_coexist(priv) &&
0314         priv->bt_status != scan_notif->bt_status) {
0315         if (scan_notif->bt_status) {
0316             /* BT on */
0317             if (!priv->bt_ch_announce)
0318                 priv->bt_traffic_load =
0319                     IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
0320             /*
0321              * otherwise, no traffic load information provided
0322              * no changes made
0323              */
0324         } else {
0325             /* BT off */
0326             priv->bt_traffic_load =
0327                 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
0328         }
0329         priv->bt_status = scan_notif->bt_status;
0330         queue_work(priv->workqueue,
0331                &priv->bt_traffic_change_work);
0332     }
0333 }
0334 
0335 void iwl_setup_rx_scan_handlers(struct iwl_priv *priv)
0336 {
0337     /* scan handlers */
0338     priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
0339     priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
0340     priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
0341                     iwl_rx_scan_results_notif;
0342     priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
0343                     iwl_rx_scan_complete_notif;
0344 }
0345 
0346 static u16 iwl_get_active_dwell_time(struct iwl_priv *priv,
0347                      enum nl80211_band band, u8 n_probes)
0348 {
0349     if (band == NL80211_BAND_5GHZ)
0350         return IWL_ACTIVE_DWELL_TIME_52 +
0351             IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
0352     else
0353         return IWL_ACTIVE_DWELL_TIME_24 +
0354             IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
0355 }
0356 
0357 static u16 iwl_limit_dwell(struct iwl_priv *priv, u16 dwell_time)
0358 {
0359     struct iwl_rxon_context *ctx;
0360     int limits[NUM_IWL_RXON_CTX] = {};
0361     int n_active = 0;
0362     u16 limit;
0363 
0364     BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
0365 
0366     /*
0367      * If we're associated, we clamp the dwell time 98%
0368      * of the beacon interval (minus 2 * channel tune time)
0369      * If both contexts are active, we have to restrict to
0370      * 1/2 of the minimum of them, because they might be in
0371      * lock-step with the time inbetween only half of what
0372      * time we'd have in each of them.
0373      */
0374     for_each_context(priv, ctx) {
0375         switch (ctx->staging.dev_type) {
0376         case RXON_DEV_TYPE_P2P:
0377             /* no timing constraints */
0378             continue;
0379         case RXON_DEV_TYPE_ESS:
0380         default:
0381             /* timing constraints if associated */
0382             if (!iwl_is_associated_ctx(ctx))
0383                 continue;
0384             break;
0385         case RXON_DEV_TYPE_CP:
0386         case RXON_DEV_TYPE_2STA:
0387             /*
0388              * These seem to always have timers for TBTT
0389              * active in uCode even when not associated yet.
0390              */
0391             break;
0392         }
0393 
0394         limits[n_active++] = ctx->beacon_int ?: IWL_PASSIVE_DWELL_BASE;
0395     }
0396 
0397     switch (n_active) {
0398     case 0:
0399         return dwell_time;
0400     case 2:
0401         limit = (limits[1] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
0402         limit /= 2;
0403         dwell_time = min(limit, dwell_time);
0404         fallthrough;
0405     case 1:
0406         limit = (limits[0] * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
0407         limit /= n_active;
0408         return min(limit, dwell_time);
0409     default:
0410         WARN_ON_ONCE(1);
0411         return dwell_time;
0412     }
0413 }
0414 
0415 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv,
0416                       enum nl80211_band band)
0417 {
0418     u16 passive = (band == NL80211_BAND_2GHZ) ?
0419         IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
0420         IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
0421 
0422     return iwl_limit_dwell(priv, passive);
0423 }
0424 
0425 /* Return valid, unused, channel for a passive scan to reset the RF */
0426 static u8 iwl_get_single_channel_number(struct iwl_priv *priv,
0427                     enum nl80211_band band)
0428 {
0429     struct ieee80211_supported_band *sband = priv->hw->wiphy->bands[band];
0430     struct iwl_rxon_context *ctx;
0431     int i;
0432 
0433     for (i = 0; i < sband->n_channels; i++) {
0434         bool busy = false;
0435 
0436         for_each_context(priv, ctx) {
0437             busy = sband->channels[i].hw_value ==
0438                 le16_to_cpu(ctx->staging.channel);
0439             if (busy)
0440                 break;
0441         }
0442 
0443         if (busy)
0444             continue;
0445 
0446         if (!(sband->channels[i].flags & IEEE80211_CHAN_DISABLED))
0447             return sband->channels[i].hw_value;
0448     }
0449 
0450     return 0;
0451 }
0452 
0453 static int iwl_get_channel_for_reset_scan(struct iwl_priv *priv,
0454                       struct ieee80211_vif *vif,
0455                       enum nl80211_band band,
0456                       struct iwl_scan_channel *scan_ch)
0457 {
0458     const struct ieee80211_supported_band *sband;
0459     u16 channel;
0460 
0461     sband = iwl_get_hw_mode(priv, band);
0462     if (!sband) {
0463         IWL_ERR(priv, "invalid band\n");
0464         return 0;
0465     }
0466 
0467     channel = iwl_get_single_channel_number(priv, band);
0468     if (channel) {
0469         scan_ch->channel = cpu_to_le16(channel);
0470         scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
0471         scan_ch->active_dwell =
0472             cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
0473         scan_ch->passive_dwell =
0474             cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
0475         /* Set txpower levels to defaults */
0476         scan_ch->dsp_atten = 110;
0477         if (band == NL80211_BAND_5GHZ)
0478             scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
0479         else
0480             scan_ch->tx_gain = ((1 << 5) | (5 << 3));
0481         return 1;
0482     }
0483 
0484     IWL_ERR(priv, "no valid channel found\n");
0485     return 0;
0486 }
0487 
0488 static int iwl_get_channels_for_scan(struct iwl_priv *priv,
0489                      struct ieee80211_vif *vif,
0490                      enum nl80211_band band,
0491                      u8 is_active, u8 n_probes,
0492                      struct iwl_scan_channel *scan_ch)
0493 {
0494     struct ieee80211_channel *chan;
0495     const struct ieee80211_supported_band *sband;
0496     u16 passive_dwell = 0;
0497     u16 active_dwell = 0;
0498     int added, i;
0499     u16 channel;
0500 
0501     sband = iwl_get_hw_mode(priv, band);
0502     if (!sband)
0503         return 0;
0504 
0505     active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
0506     passive_dwell = iwl_get_passive_dwell_time(priv, band);
0507 
0508     if (passive_dwell <= active_dwell)
0509         passive_dwell = active_dwell + 1;
0510 
0511     for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
0512         chan = priv->scan_request->channels[i];
0513 
0514         if (chan->band != band)
0515             continue;
0516 
0517         channel = chan->hw_value;
0518         scan_ch->channel = cpu_to_le16(channel);
0519 
0520         if (!is_active || (chan->flags & IEEE80211_CHAN_NO_IR))
0521             scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
0522         else
0523             scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
0524 
0525         if (n_probes)
0526             scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
0527 
0528         scan_ch->active_dwell = cpu_to_le16(active_dwell);
0529         scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
0530 
0531         /* Set txpower levels to defaults */
0532         scan_ch->dsp_atten = 110;
0533 
0534         /* NOTE: if we were doing 6Mb OFDM for scans we'd use
0535          * power level:
0536          * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
0537          */
0538         if (band == NL80211_BAND_5GHZ)
0539             scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
0540         else
0541             scan_ch->tx_gain = ((1 << 5) | (5 << 3));
0542 
0543         IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
0544                    channel, le32_to_cpu(scan_ch->type),
0545                    (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
0546                 "ACTIVE" : "PASSIVE",
0547                    (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
0548                    active_dwell : passive_dwell);
0549 
0550         scan_ch++;
0551         added++;
0552     }
0553 
0554     IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
0555     return added;
0556 }
0557 
0558 /*
0559  * iwl_fill_probe_req - fill in all required fields and IE for probe request
0560  */
0561 static u16 iwl_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
0562                   const u8 *ies, int ie_len, const u8 *ssid,
0563                   u8 ssid_len, int left)
0564 {
0565     int len = 0;
0566     u8 *pos = NULL;
0567 
0568     /* Make sure there is enough space for the probe request,
0569      * two mandatory IEs and the data */
0570     left -= 24;
0571     if (left < 0)
0572         return 0;
0573 
0574     frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
0575     eth_broadcast_addr(frame->da);
0576     memcpy(frame->sa, ta, ETH_ALEN);
0577     eth_broadcast_addr(frame->bssid);
0578     frame->seq_ctrl = 0;
0579 
0580     len += 24;
0581 
0582     /* ...next IE... */
0583     pos = &frame->u.probe_req.variable[0];
0584 
0585     /* fill in our SSID IE */
0586     left -= ssid_len + 2;
0587     if (left < 0)
0588         return 0;
0589     *pos++ = WLAN_EID_SSID;
0590     *pos++ = ssid_len;
0591     if (ssid && ssid_len) {
0592         memcpy(pos, ssid, ssid_len);
0593         pos += ssid_len;
0594     }
0595 
0596     len += ssid_len + 2;
0597 
0598     if (WARN_ON(left < ie_len))
0599         return len;
0600 
0601     if (ies && ie_len) {
0602         memcpy(pos, ies, ie_len);
0603         len += ie_len;
0604     }
0605 
0606     return (u16)len;
0607 }
0608 
0609 static int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
0610 {
0611     struct iwl_host_cmd cmd = {
0612         .id = REPLY_SCAN_CMD,
0613         .len = { sizeof(struct iwl_scan_cmd), },
0614     };
0615     struct iwl_scan_cmd *scan;
0616     struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
0617     u32 rate_flags = 0;
0618     u16 cmd_len = 0;
0619     u16 rx_chain = 0;
0620     enum nl80211_band band;
0621     u8 n_probes = 0;
0622     u8 rx_ant = priv->nvm_data->valid_rx_ant;
0623     u8 rate;
0624     bool is_active = false;
0625     int  chan_mod;
0626     u8 active_chains;
0627     u8 scan_tx_antennas = priv->nvm_data->valid_tx_ant;
0628     int ret;
0629     int scan_cmd_size = sizeof(struct iwl_scan_cmd) +
0630                 MAX_SCAN_CHANNEL * sizeof(struct iwl_scan_channel) +
0631                 priv->fw->ucode_capa.max_probe_length;
0632     const u8 *ssid = NULL;
0633     u8 ssid_len = 0;
0634 
0635     if (WARN_ON(priv->scan_type == IWL_SCAN_NORMAL &&
0636             (!priv->scan_request ||
0637              priv->scan_request->n_channels > MAX_SCAN_CHANNEL)))
0638         return -EINVAL;
0639 
0640     lockdep_assert_held(&priv->mutex);
0641 
0642     if (vif)
0643         ctx = iwl_rxon_ctx_from_vif(vif);
0644 
0645     if (!priv->scan_cmd) {
0646         priv->scan_cmd = kmalloc(scan_cmd_size, GFP_KERNEL);
0647         if (!priv->scan_cmd) {
0648             IWL_DEBUG_SCAN(priv,
0649                        "fail to allocate memory for scan\n");
0650             return -ENOMEM;
0651         }
0652     }
0653     scan = priv->scan_cmd;
0654     memset(scan, 0, scan_cmd_size);
0655 
0656     scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
0657     scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
0658 
0659     if (iwl_is_any_associated(priv)) {
0660         u16 interval = 0;
0661         u32 extra;
0662         u32 suspend_time = 100;
0663         u32 scan_suspend_time = 100;
0664 
0665         IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
0666         switch (priv->scan_type) {
0667         case IWL_SCAN_RADIO_RESET:
0668             interval = 0;
0669             break;
0670         case IWL_SCAN_NORMAL:
0671             interval = vif->bss_conf.beacon_int;
0672             break;
0673         }
0674 
0675         scan->suspend_time = 0;
0676         scan->max_out_time = cpu_to_le32(200 * 1024);
0677         if (!interval)
0678             interval = suspend_time;
0679 
0680         extra = (suspend_time / interval) << 22;
0681         scan_suspend_time = (extra |
0682             ((suspend_time % interval) * 1024));
0683         scan->suspend_time = cpu_to_le32(scan_suspend_time);
0684         IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
0685                    scan_suspend_time, interval);
0686     }
0687 
0688     switch (priv->scan_type) {
0689     case IWL_SCAN_RADIO_RESET:
0690         IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
0691         /*
0692          * Override quiet time as firmware checks that active
0693          * dwell is >= quiet; since we use passive scan it'll
0694          * not actually be used.
0695          */
0696         scan->quiet_time = cpu_to_le16(IWL_RADIO_RESET_DWELL_TIME);
0697         break;
0698     case IWL_SCAN_NORMAL:
0699         if (priv->scan_request->n_ssids) {
0700             int i, p = 0;
0701             IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
0702             /*
0703              * The highest priority SSID is inserted to the
0704              * probe request template.
0705              */
0706             ssid_len = priv->scan_request->ssids[0].ssid_len;
0707             ssid = priv->scan_request->ssids[0].ssid;
0708 
0709             /*
0710              * Invert the order of ssids, the firmware will invert
0711              * it back.
0712              */
0713             for (i = priv->scan_request->n_ssids - 1; i >= 1; i--) {
0714                 scan->direct_scan[p].id = WLAN_EID_SSID;
0715                 scan->direct_scan[p].len =
0716                     priv->scan_request->ssids[i].ssid_len;
0717                 memcpy(scan->direct_scan[p].ssid,
0718                        priv->scan_request->ssids[i].ssid,
0719                        priv->scan_request->ssids[i].ssid_len);
0720                 n_probes++;
0721                 p++;
0722             }
0723             is_active = true;
0724         } else
0725             IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
0726         break;
0727     }
0728 
0729     scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
0730     scan->tx_cmd.sta_id = ctx->bcast_sta_id;
0731     scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
0732 
0733     switch (priv->scan_band) {
0734     case NL80211_BAND_2GHZ:
0735         scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
0736         chan_mod = le32_to_cpu(
0737             priv->contexts[IWL_RXON_CTX_BSS].active.flags &
0738                         RXON_FLG_CHANNEL_MODE_MSK)
0739                        >> RXON_FLG_CHANNEL_MODE_POS;
0740         if ((priv->scan_request && priv->scan_request->no_cck) ||
0741             chan_mod == CHANNEL_MODE_PURE_40) {
0742             rate = IWL_RATE_6M_PLCP;
0743         } else {
0744             rate = IWL_RATE_1M_PLCP;
0745             rate_flags = RATE_MCS_CCK_MSK;
0746         }
0747         /*
0748          * Internal scans are passive, so we can indiscriminately set
0749          * the BT ignore flag on 2.4 GHz since it applies to TX only.
0750          */
0751         if (priv->lib->bt_params &&
0752             priv->lib->bt_params->advanced_bt_coexist)
0753             scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
0754         break;
0755     case NL80211_BAND_5GHZ:
0756         rate = IWL_RATE_6M_PLCP;
0757         break;
0758     default:
0759         IWL_WARN(priv, "Invalid scan band\n");
0760         return -EIO;
0761     }
0762 
0763     /*
0764      * If active scanning is requested but a certain channel is
0765      * marked passive, we can do active scanning if we detect
0766      * transmissions.
0767      *
0768      * There is an issue with some firmware versions that triggers
0769      * a sysassert on a "good CRC threshold" of zero (== disabled),
0770      * on a radar channel even though this means that we should NOT
0771      * send probes.
0772      *
0773      * The "good CRC threshold" is the number of frames that we
0774      * need to receive during our dwell time on a channel before
0775      * sending out probes -- setting this to a huge value will
0776      * mean we never reach it, but at the same time work around
0777      * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
0778      * here instead of IWL_GOOD_CRC_TH_DISABLED.
0779      *
0780      * This was fixed in later versions along with some other
0781      * scan changes, and the threshold behaves as a flag in those
0782      * versions.
0783      */
0784     if (priv->new_scan_threshold_behaviour)
0785         scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
0786                         IWL_GOOD_CRC_TH_DISABLED;
0787     else
0788         scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
0789                         IWL_GOOD_CRC_TH_NEVER;
0790 
0791     band = priv->scan_band;
0792 
0793     if (band == NL80211_BAND_2GHZ &&
0794         priv->lib->bt_params &&
0795         priv->lib->bt_params->advanced_bt_coexist) {
0796         /* transmit 2.4 GHz probes only on first antenna */
0797         scan_tx_antennas = first_antenna(scan_tx_antennas);
0798     }
0799 
0800     priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv,
0801                             priv->scan_tx_ant[band],
0802                             scan_tx_antennas);
0803     rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
0804     scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
0805 
0806     /*
0807      * In power save mode while associated use one chain,
0808      * otherwise use all chains
0809      */
0810     if (test_bit(STATUS_POWER_PMI, &priv->status) &&
0811         !(priv->hw->conf.flags & IEEE80211_CONF_IDLE)) {
0812         /* rx_ant has been set to all valid chains previously */
0813         active_chains = rx_ant &
0814                 ((u8)(priv->chain_noise_data.active_chains));
0815         if (!active_chains)
0816             active_chains = rx_ant;
0817 
0818         IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
0819                 priv->chain_noise_data.active_chains);
0820 
0821         rx_ant = first_antenna(active_chains);
0822     }
0823     if (priv->lib->bt_params &&
0824         priv->lib->bt_params->advanced_bt_coexist &&
0825         priv->bt_full_concurrent) {
0826         /* operated as 1x1 in full concurrency mode */
0827         rx_ant = first_antenna(rx_ant);
0828     }
0829 
0830     /* MIMO is not used here, but value is required */
0831     rx_chain |=
0832         priv->nvm_data->valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
0833     rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
0834     rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
0835     rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
0836     scan->rx_chain = cpu_to_le16(rx_chain);
0837     switch (priv->scan_type) {
0838     case IWL_SCAN_NORMAL:
0839         cmd_len = iwl_fill_probe_req(
0840                     (struct ieee80211_mgmt *)scan->data,
0841                     vif->addr,
0842                     priv->scan_request->ie,
0843                     priv->scan_request->ie_len,
0844                     ssid, ssid_len,
0845                     scan_cmd_size - sizeof(*scan));
0846         break;
0847     case IWL_SCAN_RADIO_RESET:
0848         /* use bcast addr, will not be transmitted but must be valid */
0849         cmd_len = iwl_fill_probe_req(
0850                     (struct ieee80211_mgmt *)scan->data,
0851                     iwl_bcast_addr, NULL, 0,
0852                     NULL, 0,
0853                     scan_cmd_size - sizeof(*scan));
0854         break;
0855     default:
0856         BUG();
0857     }
0858     scan->tx_cmd.len = cpu_to_le16(cmd_len);
0859 
0860     scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
0861                    RXON_FILTER_BCON_AWARE_MSK);
0862 
0863     switch (priv->scan_type) {
0864     case IWL_SCAN_RADIO_RESET:
0865         scan->channel_count =
0866             iwl_get_channel_for_reset_scan(priv, vif, band,
0867                 (void *)&scan->data[cmd_len]);
0868         break;
0869     case IWL_SCAN_NORMAL:
0870         scan->channel_count =
0871             iwl_get_channels_for_scan(priv, vif, band,
0872                 is_active, n_probes,
0873                 (void *)&scan->data[cmd_len]);
0874         break;
0875     }
0876 
0877     if (scan->channel_count == 0) {
0878         IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
0879         return -EIO;
0880     }
0881 
0882     cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
0883         scan->channel_count * sizeof(struct iwl_scan_channel);
0884     cmd.data[0] = scan;
0885     cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
0886     scan->len = cpu_to_le16(cmd.len[0]);
0887 
0888     /* set scan bit here for PAN params */
0889     set_bit(STATUS_SCAN_HW, &priv->status);
0890 
0891     ret = iwlagn_set_pan_params(priv);
0892     if (ret) {
0893         clear_bit(STATUS_SCAN_HW, &priv->status);
0894         return ret;
0895     }
0896 
0897     ret = iwl_dvm_send_cmd(priv, &cmd);
0898     if (ret) {
0899         clear_bit(STATUS_SCAN_HW, &priv->status);
0900         iwlagn_set_pan_params(priv);
0901     }
0902 
0903     return ret;
0904 }
0905 
0906 void iwl_init_scan_params(struct iwl_priv *priv)
0907 {
0908     u8 ant_idx = fls(priv->nvm_data->valid_tx_ant) - 1;
0909     if (!priv->scan_tx_ant[NL80211_BAND_5GHZ])
0910         priv->scan_tx_ant[NL80211_BAND_5GHZ] = ant_idx;
0911     if (!priv->scan_tx_ant[NL80211_BAND_2GHZ])
0912         priv->scan_tx_ant[NL80211_BAND_2GHZ] = ant_idx;
0913 }
0914 
0915 int __must_check iwl_scan_initiate(struct iwl_priv *priv,
0916                    struct ieee80211_vif *vif,
0917                    enum iwl_scan_type scan_type,
0918                    enum nl80211_band band)
0919 {
0920     int ret;
0921 
0922     lockdep_assert_held(&priv->mutex);
0923 
0924     cancel_delayed_work(&priv->scan_check);
0925 
0926     if (!iwl_is_ready_rf(priv)) {
0927         IWL_WARN(priv, "Request scan called when driver not ready.\n");
0928         return -EIO;
0929     }
0930 
0931     if (test_bit(STATUS_SCAN_HW, &priv->status)) {
0932         IWL_DEBUG_SCAN(priv,
0933             "Multiple concurrent scan requests in parallel.\n");
0934         return -EBUSY;
0935     }
0936 
0937     if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
0938         IWL_DEBUG_SCAN(priv, "Scan request while abort pending.\n");
0939         return -EBUSY;
0940     }
0941 
0942     IWL_DEBUG_SCAN(priv, "Starting %sscan...\n",
0943             scan_type == IWL_SCAN_NORMAL ? "" :
0944             "internal short ");
0945 
0946     set_bit(STATUS_SCANNING, &priv->status);
0947     priv->scan_type = scan_type;
0948     priv->scan_start = jiffies;
0949     priv->scan_band = band;
0950 
0951     ret = iwlagn_request_scan(priv, vif);
0952     if (ret) {
0953         clear_bit(STATUS_SCANNING, &priv->status);
0954         priv->scan_type = IWL_SCAN_NORMAL;
0955         return ret;
0956     }
0957 
0958     queue_delayed_work(priv->workqueue, &priv->scan_check,
0959                IWL_SCAN_CHECK_WATCHDOG);
0960 
0961     return 0;
0962 }
0963 
0964 
0965 /*
0966  * internal short scan, this function should only been called while associated.
0967  * It will reset and tune the radio to prevent possible RF related problem
0968  */
0969 void iwl_internal_short_hw_scan(struct iwl_priv *priv)
0970 {
0971     queue_work(priv->workqueue, &priv->start_internal_scan);
0972 }
0973 
0974 static void iwl_bg_start_internal_scan(struct work_struct *work)
0975 {
0976     struct iwl_priv *priv =
0977         container_of(work, struct iwl_priv, start_internal_scan);
0978 
0979     IWL_DEBUG_SCAN(priv, "Start internal scan\n");
0980 
0981     mutex_lock(&priv->mutex);
0982 
0983     if (priv->scan_type == IWL_SCAN_RADIO_RESET) {
0984         IWL_DEBUG_SCAN(priv, "Internal scan already in progress\n");
0985         goto unlock;
0986     }
0987 
0988     if (test_bit(STATUS_SCANNING, &priv->status)) {
0989         IWL_DEBUG_SCAN(priv, "Scan already in progress.\n");
0990         goto unlock;
0991     }
0992 
0993     if (iwl_scan_initiate(priv, NULL, IWL_SCAN_RADIO_RESET, priv->band))
0994         IWL_DEBUG_SCAN(priv, "failed to start internal short scan\n");
0995  unlock:
0996     mutex_unlock(&priv->mutex);
0997 }
0998 
0999 static void iwl_bg_scan_check(struct work_struct *data)
1000 {
1001     struct iwl_priv *priv =
1002         container_of(data, struct iwl_priv, scan_check.work);
1003 
1004     IWL_DEBUG_SCAN(priv, "Scan check work\n");
1005 
1006     /* Since we are here firmware does not finish scan and
1007      * most likely is in bad shape, so we don't bother to
1008      * send abort command, just force scan complete to mac80211 */
1009     mutex_lock(&priv->mutex);
1010     iwl_force_scan_end(priv);
1011     mutex_unlock(&priv->mutex);
1012 }
1013 
1014 static void iwl_bg_abort_scan(struct work_struct *work)
1015 {
1016     struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
1017 
1018     IWL_DEBUG_SCAN(priv, "Abort scan work\n");
1019 
1020     /* We keep scan_check work queued in case when firmware will not
1021      * report back scan completed notification */
1022     mutex_lock(&priv->mutex);
1023     iwl_scan_cancel_timeout(priv, 200);
1024     mutex_unlock(&priv->mutex);
1025 }
1026 
1027 static void iwl_bg_scan_completed(struct work_struct *work)
1028 {
1029     struct iwl_priv *priv =
1030         container_of(work, struct iwl_priv, scan_completed);
1031 
1032     mutex_lock(&priv->mutex);
1033     iwl_process_scan_complete(priv);
1034     mutex_unlock(&priv->mutex);
1035 }
1036 
1037 void iwl_setup_scan_deferred_work(struct iwl_priv *priv)
1038 {
1039     INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
1040     INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
1041     INIT_WORK(&priv->start_internal_scan, iwl_bg_start_internal_scan);
1042     INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
1043 }
1044 
1045 void iwl_cancel_scan_deferred_work(struct iwl_priv *priv)
1046 {
1047     cancel_work_sync(&priv->start_internal_scan);
1048     cancel_work_sync(&priv->abort_scan);
1049     cancel_work_sync(&priv->scan_completed);
1050 
1051     if (cancel_delayed_work_sync(&priv->scan_check)) {
1052         mutex_lock(&priv->mutex);
1053         iwl_force_scan_end(priv);
1054         mutex_unlock(&priv->mutex);
1055     }
1056 }