71487487d603060c9b5281be6edbbbe0ed5b4855
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-agn-rxon.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * Intel Linux Wireless <ilw@linux.intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26
27 #include "iwl-dev.h"
28 #include "iwl-agn.h"
29 #include "iwl-sta.h"
30 #include "iwl-core.h"
31 #include "iwl-agn-calib.h"
32 #include "iwl-helpers.h"
33 #include "iwl-trans.h"
34
35 static int iwlagn_disable_bss(struct iwl_priv *priv,
36                               struct iwl_rxon_context *ctx,
37                               struct iwl_rxon_cmd *send)
38 {
39         __le32 old_filter = send->filter_flags;
40         int ret;
41
42         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
43         ret = trans_send_cmd_pdu(&priv->trans, ctx->rxon_cmd,
44                                 CMD_SYNC, sizeof(*send), send);
45
46         send->filter_flags = old_filter;
47
48         if (ret)
49                 IWL_ERR(priv, "Error clearing ASSOC_MSK on BSS (%d)\n", ret);
50
51         return ret;
52 }
53
54 static int iwlagn_disable_pan(struct iwl_priv *priv,
55                               struct iwl_rxon_context *ctx,
56                               struct iwl_rxon_cmd *send)
57 {
58         struct iwl_notification_wait disable_wait;
59         __le32 old_filter = send->filter_flags;
60         u8 old_dev_type = send->dev_type;
61         int ret;
62
63         iwlagn_init_notification_wait(priv, &disable_wait,
64                                       REPLY_WIPAN_DEACTIVATION_COMPLETE,
65                                       NULL, NULL);
66
67         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
68         send->dev_type = RXON_DEV_TYPE_P2P;
69         ret = trans_send_cmd_pdu(&priv->trans, ctx->rxon_cmd,
70                                 CMD_SYNC, sizeof(*send), send);
71
72         send->filter_flags = old_filter;
73         send->dev_type = old_dev_type;
74
75         if (ret) {
76                 IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
77                 iwlagn_remove_notification(priv, &disable_wait);
78         } else {
79                 ret = iwlagn_wait_notification(priv, &disable_wait, HZ);
80                 if (ret)
81                         IWL_ERR(priv, "Timed out waiting for PAN disable\n");
82         }
83
84         return ret;
85 }
86
87 static int iwlagn_disconn_pan(struct iwl_priv *priv,
88                               struct iwl_rxon_context *ctx,
89                               struct iwl_rxon_cmd *send)
90 {
91         __le32 old_filter = send->filter_flags;
92         int ret;
93
94         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
95         ret = trans_send_cmd_pdu(&priv->trans, ctx->rxon_cmd, CMD_SYNC,
96                                 sizeof(*send), send);
97
98         send->filter_flags = old_filter;
99
100         return ret;
101 }
102
103 static void iwlagn_update_qos(struct iwl_priv *priv,
104                               struct iwl_rxon_context *ctx)
105 {
106         int ret;
107
108         if (!ctx->is_active)
109                 return;
110
111         ctx->qos_data.def_qos_parm.qos_flags = 0;
112
113         if (ctx->qos_data.qos_active)
114                 ctx->qos_data.def_qos_parm.qos_flags |=
115                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
116
117         if (ctx->ht.enabled)
118                 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
119
120         IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
121                       ctx->qos_data.qos_active,
122                       ctx->qos_data.def_qos_parm.qos_flags);
123
124         ret = trans_send_cmd_pdu(&priv->trans, ctx->qos_cmd, CMD_SYNC,
125                                sizeof(struct iwl_qosparam_cmd),
126                                &ctx->qos_data.def_qos_parm);
127         if (ret)
128                 IWL_ERR(priv, "Failed to update QoS\n");
129 }
130
131 static int iwlagn_update_beacon(struct iwl_priv *priv,
132                                 struct ieee80211_vif *vif)
133 {
134         lockdep_assert_held(&priv->mutex);
135
136         dev_kfree_skb(priv->beacon_skb);
137         priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
138         if (!priv->beacon_skb)
139                 return -ENOMEM;
140         return iwlagn_send_beacon_cmd(priv);
141 }
142
143 static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
144                            struct iwl_rxon_context *ctx)
145 {
146         int ret = 0;
147         struct iwl_rxon_assoc_cmd rxon_assoc;
148         const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
149         const struct iwl_rxon_cmd *rxon2 = &ctx->active;
150
151         if ((rxon1->flags == rxon2->flags) &&
152             (rxon1->filter_flags == rxon2->filter_flags) &&
153             (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
154             (rxon1->ofdm_ht_single_stream_basic_rates ==
155              rxon2->ofdm_ht_single_stream_basic_rates) &&
156             (rxon1->ofdm_ht_dual_stream_basic_rates ==
157              rxon2->ofdm_ht_dual_stream_basic_rates) &&
158             (rxon1->ofdm_ht_triple_stream_basic_rates ==
159              rxon2->ofdm_ht_triple_stream_basic_rates) &&
160             (rxon1->acquisition_data == rxon2->acquisition_data) &&
161             (rxon1->rx_chain == rxon2->rx_chain) &&
162             (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
163                 IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC.  Not resending.\n");
164                 return 0;
165         }
166
167         rxon_assoc.flags = ctx->staging.flags;
168         rxon_assoc.filter_flags = ctx->staging.filter_flags;
169         rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
170         rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
171         rxon_assoc.reserved1 = 0;
172         rxon_assoc.reserved2 = 0;
173         rxon_assoc.reserved3 = 0;
174         rxon_assoc.ofdm_ht_single_stream_basic_rates =
175             ctx->staging.ofdm_ht_single_stream_basic_rates;
176         rxon_assoc.ofdm_ht_dual_stream_basic_rates =
177             ctx->staging.ofdm_ht_dual_stream_basic_rates;
178         rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
179         rxon_assoc.ofdm_ht_triple_stream_basic_rates =
180                  ctx->staging.ofdm_ht_triple_stream_basic_rates;
181         rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
182
183         ret = trans_send_cmd_pdu(&priv->trans, ctx->rxon_assoc_cmd,
184                                 CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
185         return ret;
186 }
187
188 static int iwlagn_rxon_disconn(struct iwl_priv *priv,
189                                struct iwl_rxon_context *ctx)
190 {
191         int ret;
192         struct iwl_rxon_cmd *active = (void *)&ctx->active;
193
194         if (ctx->ctxid == IWL_RXON_CTX_BSS) {
195                 ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
196         } else {
197                 ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
198                 if (ret)
199                         return ret;
200                 if (ctx->vif) {
201                         ret = iwl_send_rxon_timing(priv, ctx);
202                         if (ret) {
203                                 IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
204                                 return ret;
205                         }
206                         ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
207                 }
208         }
209         if (ret)
210                 return ret;
211
212         /*
213          * Un-assoc RXON clears the station table and WEP
214          * keys, so we have to restore those afterwards.
215          */
216         iwl_clear_ucode_stations(priv, ctx);
217         /* update -- might need P2P now */
218         iwl_update_bcast_station(priv, ctx);
219         iwl_restore_stations(priv, ctx);
220         ret = iwl_restore_default_wep_keys(priv, ctx);
221         if (ret) {
222                 IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
223                 return ret;
224         }
225
226         memcpy(active, &ctx->staging, sizeof(*active));
227         return 0;
228 }
229
230 static int iwlagn_rxon_connect(struct iwl_priv *priv,
231                                struct iwl_rxon_context *ctx)
232 {
233         int ret;
234         struct iwl_rxon_cmd *active = (void *)&ctx->active;
235
236         /* RXON timing must be before associated RXON */
237         if (ctx->ctxid == IWL_RXON_CTX_BSS) {
238                 ret = iwl_send_rxon_timing(priv, ctx);
239                 if (ret) {
240                         IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
241                         return ret;
242                 }
243         }
244         /* QoS info may be cleared by previous un-assoc RXON */
245         iwlagn_update_qos(priv, ctx);
246
247         /*
248          * We'll run into this code path when beaconing is
249          * enabled, but then we also need to send the beacon
250          * to the device.
251          */
252         if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
253                 ret = iwlagn_update_beacon(priv, ctx->vif);
254                 if (ret) {
255                         IWL_ERR(priv,
256                                 "Error sending required beacon (%d)!\n",
257                                 ret);
258                         return ret;
259                 }
260         }
261
262         priv->start_calib = 0;
263         /*
264          * Apply the new configuration.
265          *
266          * Associated RXON doesn't clear the station table in uCode,
267          * so we don't need to restore stations etc. after this.
268          */
269         ret = trans_send_cmd_pdu(&priv->trans, ctx->rxon_cmd, CMD_SYNC,
270                       sizeof(struct iwl_rxon_cmd), &ctx->staging);
271         if (ret) {
272                 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
273                 return ret;
274         }
275         memcpy(active, &ctx->staging, sizeof(*active));
276
277         iwl_reprogram_ap_sta(priv, ctx);
278
279         /* IBSS beacon needs to be sent after setting assoc */
280         if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
281                 if (iwlagn_update_beacon(priv, ctx->vif))
282                         IWL_ERR(priv, "Error sending IBSS beacon\n");
283         iwl_init_sensitivity(priv);
284
285         /*
286          * If we issue a new RXON command which required a tune then
287          * we must send a new TXPOWER command or we won't be able to
288          * Tx any frames.
289          *
290          * It's expected we set power here if channel is changing.
291          */
292         ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
293         if (ret) {
294                 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
295                 return ret;
296         }
297
298         if ((ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION) &&
299             priv->cfg->ht_params->smps_mode)
300                 ieee80211_request_smps(ctx->vif,
301                                        priv->cfg->ht_params->smps_mode);
302
303         return 0;
304 }
305
306 int iwlagn_set_pan_params(struct iwl_priv *priv)
307 {
308         struct iwl_wipan_params_cmd cmd;
309         struct iwl_rxon_context *ctx_bss, *ctx_pan;
310         int slot0 = 300, slot1 = 0;
311         int ret;
312
313         if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
314                 return 0;
315
316         BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
317
318         lockdep_assert_held(&priv->mutex);
319
320         ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
321         ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
322
323         /*
324          * If the PAN context is inactive, then we don't need
325          * to update the PAN parameters, the last thing we'll
326          * have done before it goes inactive is making the PAN
327          * parameters be WLAN-only.
328          */
329         if (!ctx_pan->is_active)
330                 return 0;
331
332         memset(&cmd, 0, sizeof(cmd));
333
334         /* only 2 slots are currently allowed */
335         cmd.num_slots = 2;
336
337         cmd.slots[0].type = 0; /* BSS */
338         cmd.slots[1].type = 1; /* PAN */
339
340         if (priv->hw_roc_channel) {
341                 /* both contexts must be used for this to happen */
342                 slot1 = priv->hw_roc_duration;
343                 slot0 = IWL_MIN_SLOT_TIME;
344         } else if (ctx_bss->vif && ctx_pan->vif) {
345                 int bcnint = ctx_pan->vif->bss_conf.beacon_int;
346                 int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
347
348                 /* should be set, but seems unused?? */
349                 cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
350
351                 if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
352                     bcnint &&
353                     bcnint != ctx_bss->vif->bss_conf.beacon_int) {
354                         IWL_ERR(priv,
355                                 "beacon intervals don't match (%d, %d)\n",
356                                 ctx_bss->vif->bss_conf.beacon_int,
357                                 ctx_pan->vif->bss_conf.beacon_int);
358                 } else
359                         bcnint = max_t(int, bcnint,
360                                        ctx_bss->vif->bss_conf.beacon_int);
361                 if (!bcnint)
362                         bcnint = DEFAULT_BEACON_INTERVAL;
363                 slot0 = bcnint / 2;
364                 slot1 = bcnint - slot0;
365
366                 if (test_bit(STATUS_SCAN_HW, &priv->status) ||
367                     (!ctx_bss->vif->bss_conf.idle &&
368                      !ctx_bss->vif->bss_conf.assoc)) {
369                         slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
370                         slot1 = IWL_MIN_SLOT_TIME;
371                 } else if (!ctx_pan->vif->bss_conf.idle &&
372                            !ctx_pan->vif->bss_conf.assoc) {
373                         slot1 = bcnint * 3 - IWL_MIN_SLOT_TIME;
374                         slot0 = IWL_MIN_SLOT_TIME;
375                 }
376         } else if (ctx_pan->vif) {
377                 slot0 = 0;
378                 slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
379                                         ctx_pan->vif->bss_conf.beacon_int;
380                 slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
381
382                 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
383                         slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
384                         slot1 = IWL_MIN_SLOT_TIME;
385                 }
386         }
387
388         cmd.slots[0].width = cpu_to_le16(slot0);
389         cmd.slots[1].width = cpu_to_le16(slot1);
390
391         ret = trans_send_cmd_pdu(&priv->trans, REPLY_WIPAN_PARAMS, CMD_SYNC,
392                         sizeof(cmd), &cmd);
393         if (ret)
394                 IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
395
396         return ret;
397 }
398
399 /**
400  * iwlagn_commit_rxon - commit staging_rxon to hardware
401  *
402  * The RXON command in staging_rxon is committed to the hardware and
403  * the active_rxon structure is updated with the new data.  This
404  * function correctly transitions out of the RXON_ASSOC_MSK state if
405  * a HW tune is required based on the RXON structure changes.
406  *
407  * The connect/disconnect flow should be as the following:
408  *
409  * 1. make sure send RXON command with association bit unset if not connect
410  *      this should include the channel and the band for the candidate
411  *      to be connected to
412  * 2. Add Station before RXON association with the AP
413  * 3. RXON_timing has to send before RXON for connection
414  * 4. full RXON command - associated bit set
415  * 5. use RXON_ASSOC command to update any flags changes
416  */
417 int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
418 {
419         /* cast away the const for active_rxon in this function */
420         struct iwl_rxon_cmd *active = (void *)&ctx->active;
421         bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
422         int ret;
423
424         lockdep_assert_held(&priv->mutex);
425
426         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
427                 return -EINVAL;
428
429         if (!iwl_is_alive(priv))
430                 return -EBUSY;
431
432         /* This function hardcodes a bunch of dual-mode assumptions */
433         BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
434
435         if (!ctx->is_active)
436                 return 0;
437
438         /* always get timestamp with Rx frame */
439         ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
440
441         if (ctx->ctxid == IWL_RXON_CTX_PAN && priv->hw_roc_channel) {
442                 struct ieee80211_channel *chan = priv->hw_roc_channel;
443
444                 iwl_set_rxon_channel(priv, chan, ctx);
445                 iwl_set_flags_for_band(priv, ctx, chan->band, NULL);
446                 ctx->staging.filter_flags |=
447                         RXON_FILTER_ASSOC_MSK |
448                         RXON_FILTER_PROMISC_MSK |
449                         RXON_FILTER_CTL2HOST_MSK;
450                 ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
451                 new_assoc = true;
452
453                 if (memcmp(&ctx->staging, &ctx->active,
454                            sizeof(ctx->staging)) == 0)
455                         return 0;
456         }
457
458         /*
459          * force CTS-to-self frames protection if RTS-CTS is not preferred
460          * one aggregation protection method
461          */
462         if (!(priv->cfg->ht_params &&
463               priv->cfg->ht_params->use_rts_for_aggregation))
464                 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
465
466         if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
467             !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
468                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
469         else
470                 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
471
472         iwl_print_rx_config_cmd(priv, ctx);
473         ret = iwl_check_rxon_cmd(priv, ctx);
474         if (ret) {
475                 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
476                 return -EINVAL;
477         }
478
479         /*
480          * receive commit_rxon request
481          * abort any previous channel switch if still in process
482          */
483         if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
484             (priv->switch_channel != ctx->staging.channel)) {
485                 IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
486                               le16_to_cpu(priv->switch_channel));
487                 iwl_chswitch_done(priv, false);
488         }
489
490         /*
491          * If we don't need to send a full RXON, we can use
492          * iwl_rxon_assoc_cmd which is used to reconfigure filter
493          * and other flags for the current radio configuration.
494          */
495         if (!iwl_full_rxon_required(priv, ctx)) {
496                 ret = iwlagn_send_rxon_assoc(priv, ctx);
497                 if (ret) {
498                         IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
499                         return ret;
500                 }
501
502                 memcpy(active, &ctx->staging, sizeof(*active));
503                 /*
504                  * We do not commit tx power settings while channel changing,
505                  * do it now if after settings changed.
506                  */
507                 iwl_set_tx_power(priv, priv->tx_power_next, false);
508
509                 /* make sure we are in the right PS state */
510                 iwl_power_update_mode(priv, true);
511
512                 return 0;
513         }
514
515         iwl_set_rxon_hwcrypto(priv, ctx, !iwlagn_mod_params.sw_crypto);
516
517         IWL_DEBUG_INFO(priv,
518                        "Going to commit RXON\n"
519                        "  * with%s RXON_FILTER_ASSOC_MSK\n"
520                        "  * channel = %d\n"
521                        "  * bssid = %pM\n",
522                        (new_assoc ? "" : "out"),
523                        le16_to_cpu(ctx->staging.channel),
524                        ctx->staging.bssid_addr);
525
526         /*
527          * Always clear associated first, but with the correct config.
528          * This is required as for example station addition for the
529          * AP station must be done after the BSSID is set to correctly
530          * set up filters in the device.
531          */
532         ret = iwlagn_rxon_disconn(priv, ctx);
533         if (ret)
534                 return ret;
535
536         ret = iwlagn_set_pan_params(priv);
537         if (ret)
538                 return ret;
539
540         if (new_assoc)
541                 return iwlagn_rxon_connect(priv, ctx);
542
543         return 0;
544 }
545
546 int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
547 {
548         struct iwl_priv *priv = hw->priv;
549         struct iwl_rxon_context *ctx;
550         struct ieee80211_conf *conf = &hw->conf;
551         struct ieee80211_channel *channel = conf->channel;
552         const struct iwl_channel_info *ch_info;
553         int ret = 0;
554
555         IWL_DEBUG_MAC80211(priv, "changed %#x", changed);
556
557         mutex_lock(&priv->mutex);
558
559         if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
560                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
561                 goto out;
562         }
563
564         if (!iwl_is_ready(priv)) {
565                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
566                 goto out;
567         }
568
569         if (changed & (IEEE80211_CONF_CHANGE_SMPS |
570                        IEEE80211_CONF_CHANGE_CHANNEL)) {
571                 /* mac80211 uses static for non-HT which is what we want */
572                 priv->current_ht_config.smps = conf->smps_mode;
573
574                 /*
575                  * Recalculate chain counts.
576                  *
577                  * If monitor mode is enabled then mac80211 will
578                  * set up the SM PS mode to OFF if an HT channel is
579                  * configured.
580                  */
581                 for_each_context(priv, ctx)
582                         iwlagn_set_rxon_chain(priv, ctx);
583         }
584
585         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
586                 unsigned long flags;
587
588                 ch_info = iwl_get_channel_info(priv, channel->band,
589                                                channel->hw_value);
590                 if (!is_channel_valid(ch_info)) {
591                         IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
592                         ret = -EINVAL;
593                         goto out;
594                 }
595
596                 spin_lock_irqsave(&priv->lock, flags);
597
598                 for_each_context(priv, ctx) {
599                         /* Configure HT40 channels */
600                         if (ctx->ht.enabled != conf_is_ht(conf))
601                                 ctx->ht.enabled = conf_is_ht(conf);
602
603                         if (ctx->ht.enabled) {
604                                 if (conf_is_ht40_minus(conf)) {
605                                         ctx->ht.extension_chan_offset =
606                                                 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
607                                         ctx->ht.is_40mhz = true;
608                                 } else if (conf_is_ht40_plus(conf)) {
609                                         ctx->ht.extension_chan_offset =
610                                                 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
611                                         ctx->ht.is_40mhz = true;
612                                 } else {
613                                         ctx->ht.extension_chan_offset =
614                                                 IEEE80211_HT_PARAM_CHA_SEC_NONE;
615                                         ctx->ht.is_40mhz = false;
616                                 }
617                         } else
618                                 ctx->ht.is_40mhz = false;
619
620                         /*
621                          * Default to no protection. Protection mode will
622                          * later be set from BSS config in iwl_ht_conf
623                          */
624                         ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
625
626                         /* if we are switching from ht to 2.4 clear flags
627                          * from any ht related info since 2.4 does not
628                          * support ht */
629                         if (le16_to_cpu(ctx->staging.channel) !=
630                             channel->hw_value)
631                                 ctx->staging.flags = 0;
632
633                         iwl_set_rxon_channel(priv, channel, ctx);
634                         iwl_set_rxon_ht(priv, &priv->current_ht_config);
635
636                         iwl_set_flags_for_band(priv, ctx, channel->band,
637                                                ctx->vif);
638                 }
639
640                 spin_unlock_irqrestore(&priv->lock, flags);
641
642                 iwl_update_bcast_stations(priv);
643
644                 /*
645                  * The list of supported rates and rate mask can be different
646                  * for each band; since the band may have changed, reset
647                  * the rate mask to what mac80211 lists.
648                  */
649                 iwl_set_rate(priv);
650         }
651
652         if (changed & (IEEE80211_CONF_CHANGE_PS |
653                         IEEE80211_CONF_CHANGE_IDLE)) {
654                 ret = iwl_power_update_mode(priv, false);
655                 if (ret)
656                         IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
657         }
658
659         if (changed & IEEE80211_CONF_CHANGE_POWER) {
660                 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
661                         priv->tx_power_user_lmt, conf->power_level);
662
663                 iwl_set_tx_power(priv, conf->power_level, false);
664         }
665
666         for_each_context(priv, ctx) {
667                 if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
668                         continue;
669                 iwlagn_commit_rxon(priv, ctx);
670         }
671  out:
672         mutex_unlock(&priv->mutex);
673         return ret;
674 }
675
676 static void iwlagn_check_needed_chains(struct iwl_priv *priv,
677                                        struct iwl_rxon_context *ctx,
678                                        struct ieee80211_bss_conf *bss_conf)
679 {
680         struct ieee80211_vif *vif = ctx->vif;
681         struct iwl_rxon_context *tmp;
682         struct ieee80211_sta *sta;
683         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
684         struct ieee80211_sta_ht_cap *ht_cap;
685         bool need_multiple;
686
687         lockdep_assert_held(&priv->mutex);
688
689         switch (vif->type) {
690         case NL80211_IFTYPE_STATION:
691                 rcu_read_lock();
692                 sta = ieee80211_find_sta(vif, bss_conf->bssid);
693                 if (!sta) {
694                         /*
695                          * If at all, this can only happen through a race
696                          * when the AP disconnects us while we're still
697                          * setting up the connection, in that case mac80211
698                          * will soon tell us about that.
699                          */
700                         need_multiple = false;
701                         rcu_read_unlock();
702                         break;
703                 }
704
705                 ht_cap = &sta->ht_cap;
706
707                 need_multiple = true;
708
709                 /*
710                  * If the peer advertises no support for receiving 2 and 3
711                  * stream MCS rates, it can't be transmitting them either.
712                  */
713                 if (ht_cap->mcs.rx_mask[1] == 0 &&
714                     ht_cap->mcs.rx_mask[2] == 0) {
715                         need_multiple = false;
716                 } else if (!(ht_cap->mcs.tx_params &
717                                                 IEEE80211_HT_MCS_TX_DEFINED)) {
718                         /* If it can't TX MCS at all ... */
719                         need_multiple = false;
720                 } else if (ht_cap->mcs.tx_params &
721                                                 IEEE80211_HT_MCS_TX_RX_DIFF) {
722                         int maxstreams;
723
724                         /*
725                          * But if it can receive them, it might still not
726                          * be able to transmit them, which is what we need
727                          * to check here -- so check the number of streams
728                          * it advertises for TX (if different from RX).
729                          */
730
731                         maxstreams = (ht_cap->mcs.tx_params &
732                                  IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
733                         maxstreams >>=
734                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
735                         maxstreams += 1;
736
737                         if (maxstreams <= 1)
738                                 need_multiple = false;
739                 }
740
741                 rcu_read_unlock();
742                 break;
743         case NL80211_IFTYPE_ADHOC:
744                 /* currently */
745                 need_multiple = false;
746                 break;
747         default:
748                 /* only AP really */
749                 need_multiple = true;
750                 break;
751         }
752
753         ctx->ht_need_multiple_chains = need_multiple;
754
755         if (!need_multiple) {
756                 /* check all contexts */
757                 for_each_context(priv, tmp) {
758                         if (!tmp->vif)
759                                 continue;
760                         if (tmp->ht_need_multiple_chains) {
761                                 need_multiple = true;
762                                 break;
763                         }
764                 }
765         }
766
767         ht_conf->single_chain_sufficient = !need_multiple;
768 }
769
770 static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
771 {
772         struct iwl_chain_noise_data *data = &priv->chain_noise_data;
773         int ret;
774
775         if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
776             iwl_is_any_associated(priv)) {
777                 struct iwl_calib_chain_noise_reset_cmd cmd;
778
779                 /* clear data for chain noise calibration algorithm */
780                 data->chain_noise_a = 0;
781                 data->chain_noise_b = 0;
782                 data->chain_noise_c = 0;
783                 data->chain_signal_a = 0;
784                 data->chain_signal_b = 0;
785                 data->chain_signal_c = 0;
786                 data->beacon_count = 0;
787
788                 memset(&cmd, 0, sizeof(cmd));
789                 iwl_set_calib_hdr(&cmd.hdr,
790                         priv->phy_calib_chain_noise_reset_cmd);
791                 ret = trans_send_cmd_pdu(&priv->trans,
792                                         REPLY_PHY_CALIBRATION_CMD,
793                                         CMD_SYNC, sizeof(cmd), &cmd);
794                 if (ret)
795                         IWL_ERR(priv,
796                                 "Could not send REPLY_PHY_CALIBRATION_CMD\n");
797                 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
798                 IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
799         }
800 }
801
802 void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
803                              struct ieee80211_vif *vif,
804                              struct ieee80211_bss_conf *bss_conf,
805                              u32 changes)
806 {
807         struct iwl_priv *priv = hw->priv;
808         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
809         int ret;
810         bool force = false;
811
812         mutex_lock(&priv->mutex);
813
814         if (unlikely(!iwl_is_ready(priv))) {
815                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
816                 mutex_unlock(&priv->mutex);
817                 return;
818         }
819
820         if (unlikely(!ctx->vif)) {
821                 IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
822                 mutex_unlock(&priv->mutex);
823                 return;
824         }
825
826         if (changes & BSS_CHANGED_BEACON_INT)
827                 force = true;
828
829         if (changes & BSS_CHANGED_QOS) {
830                 ctx->qos_data.qos_active = bss_conf->qos;
831                 iwlagn_update_qos(priv, ctx);
832         }
833
834         ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
835         if (vif->bss_conf.use_short_preamble)
836                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
837         else
838                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
839
840         if (changes & BSS_CHANGED_ASSOC) {
841                 if (bss_conf->assoc) {
842                         priv->timestamp = bss_conf->timestamp;
843                         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
844                 } else {
845                         /*
846                          * If we disassociate while there are pending
847                          * frames, just wake up the queues and let the
848                          * frames "escape" ... This shouldn't really
849                          * be happening to start with, but we should
850                          * not get stuck in this case either since it
851                          * can happen if userspace gets confused.
852                          */
853                         if (ctx->last_tx_rejected) {
854                                 ctx->last_tx_rejected = false;
855                                 iwl_wake_any_queue(priv, ctx);
856                         }
857                         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
858                 }
859
860                 iwlagn_bt_coex_rssi_monitor(priv);
861         }
862
863         if (ctx->ht.enabled) {
864                 ctx->ht.protection = bss_conf->ht_operation_mode &
865                                         IEEE80211_HT_OP_MODE_PROTECTION;
866                 ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
867                                         IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
868                 iwlagn_check_needed_chains(priv, ctx, bss_conf);
869                 iwl_set_rxon_ht(priv, &priv->current_ht_config);
870         }
871
872         iwlagn_set_rxon_chain(priv, ctx);
873
874         if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
875                 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
876         else
877                 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
878
879         if (bss_conf->use_cts_prot)
880                 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
881         else
882                 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
883
884         memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
885
886         if (vif->type == NL80211_IFTYPE_AP ||
887             vif->type == NL80211_IFTYPE_ADHOC) {
888                 if (vif->bss_conf.enable_beacon) {
889                         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
890                         priv->beacon_ctx = ctx;
891                 } else {
892                         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
893                         priv->beacon_ctx = NULL;
894                 }
895         }
896
897         if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
898                 iwlagn_commit_rxon(priv, ctx);
899
900         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
901                 /*
902                  * The chain noise calibration will enable PM upon
903                  * completion. If calibration has already been run
904                  * then we need to enable power management here.
905                  */
906                 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
907                         iwl_power_update_mode(priv, false);
908
909                 /* Enable RX differential gain and sensitivity calibrations */
910                 if (!priv->disable_chain_noise_cal)
911                         iwlagn_chain_noise_reset(priv);
912                 priv->start_calib = 1;
913         }
914
915         if (changes & BSS_CHANGED_IBSS) {
916                 ret = iwlagn_manage_ibss_station(priv, vif,
917                                                  bss_conf->ibss_joined);
918                 if (ret)
919                         IWL_ERR(priv, "failed to %s IBSS station %pM\n",
920                                 bss_conf->ibss_joined ? "add" : "remove",
921                                 bss_conf->bssid);
922         }
923
924         if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_ADHOC &&
925             priv->beacon_ctx) {
926                 if (iwlagn_update_beacon(priv, vif))
927                         IWL_ERR(priv, "Error sending IBSS beacon\n");
928         }
929
930         mutex_unlock(&priv->mutex);
931 }
932
933 void iwlagn_post_scan(struct iwl_priv *priv)
934 {
935         struct iwl_rxon_context *ctx;
936
937         /*
938          * We do not commit power settings while scan is pending,
939          * do it now if the settings changed.
940          */
941         iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
942         iwl_set_tx_power(priv, priv->tx_power_next, false);
943
944         /*
945          * Since setting the RXON may have been deferred while
946          * performing the scan, fire one off if needed
947          */
948         for_each_context(priv, ctx)
949                 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
950                         iwlagn_commit_rxon(priv, ctx);
951
952         iwlagn_set_pan_params(priv);
953 }