]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/dvm/rxon.c
openvswitch: Use generic struct pcpu_tstats.
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / dvm / rxon.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2013 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 <linux/etherdevice.h>
28 #include "iwl-trans.h"
29 #include "iwl-modparams.h"
30 #include "dev.h"
31 #include "agn.h"
32 #include "calib.h"
33
34 /*
35  * initialize rxon structure with default values from eeprom
36  */
37 void iwl_connection_init_rx_config(struct iwl_priv *priv,
38                                    struct iwl_rxon_context *ctx)
39 {
40         memset(&ctx->staging, 0, sizeof(ctx->staging));
41
42         if (!ctx->vif) {
43                 ctx->staging.dev_type = ctx->unused_devtype;
44         } else
45         switch (ctx->vif->type) {
46         case NL80211_IFTYPE_AP:
47                 ctx->staging.dev_type = ctx->ap_devtype;
48                 break;
49
50         case NL80211_IFTYPE_STATION:
51                 ctx->staging.dev_type = ctx->station_devtype;
52                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
53                 break;
54
55         case NL80211_IFTYPE_ADHOC:
56                 ctx->staging.dev_type = ctx->ibss_devtype;
57                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
58                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
59                                                   RXON_FILTER_ACCEPT_GRP_MSK;
60                 break;
61
62         case NL80211_IFTYPE_MONITOR:
63                 ctx->staging.dev_type = RXON_DEV_TYPE_SNIFFER;
64                 break;
65
66         default:
67                 IWL_ERR(priv, "Unsupported interface type %d\n",
68                         ctx->vif->type);
69                 break;
70         }
71
72 #if 0
73         /* TODO:  Figure out when short_preamble would be set and cache from
74          * that */
75         if (!hw_to_local(priv->hw)->short_preamble)
76                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
77         else
78                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
79 #endif
80
81         ctx->staging.channel = cpu_to_le16(priv->hw->conf.channel->hw_value);
82         priv->band = priv->hw->conf.channel->band;
83
84         iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
85
86         /* clear both MIX and PURE40 mode flag */
87         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
88                                         RXON_FLG_CHANNEL_MODE_PURE_40);
89         if (ctx->vif)
90                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
91
92         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
93         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
94         ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
95 }
96
97 static int iwlagn_disable_bss(struct iwl_priv *priv,
98                               struct iwl_rxon_context *ctx,
99                               struct iwl_rxon_cmd *send)
100 {
101         __le32 old_filter = send->filter_flags;
102         int ret;
103
104         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
105         ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
106                                 CMD_SYNC, sizeof(*send), send);
107
108         send->filter_flags = old_filter;
109
110         if (ret)
111                 IWL_DEBUG_QUIET_RFKILL(priv,
112                         "Error clearing ASSOC_MSK on BSS (%d)\n", ret);
113
114         return ret;
115 }
116
117 static int iwlagn_disable_pan(struct iwl_priv *priv,
118                               struct iwl_rxon_context *ctx,
119                               struct iwl_rxon_cmd *send)
120 {
121         struct iwl_notification_wait disable_wait;
122         __le32 old_filter = send->filter_flags;
123         u8 old_dev_type = send->dev_type;
124         int ret;
125         static const u8 deactivate_cmd[] = {
126                 REPLY_WIPAN_DEACTIVATION_COMPLETE
127         };
128
129         iwl_init_notification_wait(&priv->notif_wait, &disable_wait,
130                                    deactivate_cmd, ARRAY_SIZE(deactivate_cmd),
131                                    NULL, NULL);
132
133         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
134         send->dev_type = RXON_DEV_TYPE_P2P;
135         ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd,
136                                 CMD_SYNC, sizeof(*send), send);
137
138         send->filter_flags = old_filter;
139         send->dev_type = old_dev_type;
140
141         if (ret) {
142                 IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
143                 iwl_remove_notification(&priv->notif_wait, &disable_wait);
144         } else {
145                 ret = iwl_wait_notification(&priv->notif_wait,
146                                             &disable_wait, HZ);
147                 if (ret)
148                         IWL_ERR(priv, "Timed out waiting for PAN disable\n");
149         }
150
151         return ret;
152 }
153
154 static int iwlagn_disconn_pan(struct iwl_priv *priv,
155                               struct iwl_rxon_context *ctx,
156                               struct iwl_rxon_cmd *send)
157 {
158         __le32 old_filter = send->filter_flags;
159         int ret;
160
161         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
162         ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, CMD_SYNC,
163                                 sizeof(*send), send);
164
165         send->filter_flags = old_filter;
166
167         return ret;
168 }
169
170 static void iwlagn_update_qos(struct iwl_priv *priv,
171                               struct iwl_rxon_context *ctx)
172 {
173         int ret;
174
175         if (!ctx->is_active)
176                 return;
177
178         ctx->qos_data.def_qos_parm.qos_flags = 0;
179
180         if (ctx->qos_data.qos_active)
181                 ctx->qos_data.def_qos_parm.qos_flags |=
182                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
183
184         if (ctx->ht.enabled)
185                 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
186
187         IWL_DEBUG_INFO(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
188                       ctx->qos_data.qos_active,
189                       ctx->qos_data.def_qos_parm.qos_flags);
190
191         ret = iwl_dvm_send_cmd_pdu(priv, ctx->qos_cmd, CMD_SYNC,
192                                sizeof(struct iwl_qosparam_cmd),
193                                &ctx->qos_data.def_qos_parm);
194         if (ret)
195                 IWL_DEBUG_QUIET_RFKILL(priv, "Failed to update QoS\n");
196 }
197
198 static int iwlagn_update_beacon(struct iwl_priv *priv,
199                                 struct ieee80211_vif *vif)
200 {
201         lockdep_assert_held(&priv->mutex);
202
203         dev_kfree_skb(priv->beacon_skb);
204         priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
205         if (!priv->beacon_skb)
206                 return -ENOMEM;
207         return iwlagn_send_beacon_cmd(priv);
208 }
209
210 static int iwlagn_send_rxon_assoc(struct iwl_priv *priv,
211                                   struct iwl_rxon_context *ctx)
212 {
213         int ret = 0;
214         struct iwl_rxon_assoc_cmd rxon_assoc;
215         const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
216         const struct iwl_rxon_cmd *rxon2 = &ctx->active;
217
218         if ((rxon1->flags == rxon2->flags) &&
219             (rxon1->filter_flags == rxon2->filter_flags) &&
220             (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
221             (rxon1->ofdm_ht_single_stream_basic_rates ==
222              rxon2->ofdm_ht_single_stream_basic_rates) &&
223             (rxon1->ofdm_ht_dual_stream_basic_rates ==
224              rxon2->ofdm_ht_dual_stream_basic_rates) &&
225             (rxon1->ofdm_ht_triple_stream_basic_rates ==
226              rxon2->ofdm_ht_triple_stream_basic_rates) &&
227             (rxon1->acquisition_data == rxon2->acquisition_data) &&
228             (rxon1->rx_chain == rxon2->rx_chain) &&
229             (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
230                 IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC.  Not resending.\n");
231                 return 0;
232         }
233
234         rxon_assoc.flags = ctx->staging.flags;
235         rxon_assoc.filter_flags = ctx->staging.filter_flags;
236         rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
237         rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
238         rxon_assoc.reserved1 = 0;
239         rxon_assoc.reserved2 = 0;
240         rxon_assoc.reserved3 = 0;
241         rxon_assoc.ofdm_ht_single_stream_basic_rates =
242             ctx->staging.ofdm_ht_single_stream_basic_rates;
243         rxon_assoc.ofdm_ht_dual_stream_basic_rates =
244             ctx->staging.ofdm_ht_dual_stream_basic_rates;
245         rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
246         rxon_assoc.ofdm_ht_triple_stream_basic_rates =
247                  ctx->staging.ofdm_ht_triple_stream_basic_rates;
248         rxon_assoc.acquisition_data = ctx->staging.acquisition_data;
249
250         ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_assoc_cmd,
251                                 CMD_ASYNC, sizeof(rxon_assoc), &rxon_assoc);
252         return ret;
253 }
254
255 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
256 {
257         u16 new_val;
258         u16 beacon_factor;
259
260         /*
261          * If mac80211 hasn't given us a beacon interval, program
262          * the default into the device (not checking this here
263          * would cause the adjustment below to return the maximum
264          * value, which may break PAN.)
265          */
266         if (!beacon_val)
267                 return DEFAULT_BEACON_INTERVAL;
268
269         /*
270          * If the beacon interval we obtained from the peer
271          * is too large, we'll have to wake up more often
272          * (and in IBSS case, we'll beacon too much)
273          *
274          * For example, if max_beacon_val is 4096, and the
275          * requested beacon interval is 7000, we'll have to
276          * use 3500 to be able to wake up on the beacons.
277          *
278          * This could badly influence beacon detection stats.
279          */
280
281         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
282         new_val = beacon_val / beacon_factor;
283
284         if (!new_val)
285                 new_val = max_beacon_val;
286
287         return new_val;
288 }
289
290 static int iwl_send_rxon_timing(struct iwl_priv *priv,
291                                 struct iwl_rxon_context *ctx)
292 {
293         u64 tsf;
294         s32 interval_tm, rem;
295         struct ieee80211_conf *conf = NULL;
296         u16 beacon_int;
297         struct ieee80211_vif *vif = ctx->vif;
298
299         conf = &priv->hw->conf;
300
301         lockdep_assert_held(&priv->mutex);
302
303         memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
304
305         ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
306         ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
307
308         beacon_int = vif ? vif->bss_conf.beacon_int : 0;
309
310         /*
311          * TODO: For IBSS we need to get atim_window from mac80211,
312          *       for now just always use 0
313          */
314         ctx->timing.atim_window = 0;
315
316         if (ctx->ctxid == IWL_RXON_CTX_PAN &&
317             (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
318             iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
319             priv->contexts[IWL_RXON_CTX_BSS].vif &&
320             priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
321                 ctx->timing.beacon_interval =
322                         priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
323                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
324         } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
325                    iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
326                    priv->contexts[IWL_RXON_CTX_PAN].vif &&
327                    priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
328                    (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
329                     !ctx->vif->bss_conf.beacon_int)) {
330                 ctx->timing.beacon_interval =
331                         priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
332                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
333         } else {
334                 beacon_int = iwl_adjust_beacon_interval(beacon_int,
335                         IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
336                 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
337         }
338
339         ctx->beacon_int = beacon_int;
340
341         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
342         interval_tm = beacon_int * TIME_UNIT;
343         rem = do_div(tsf, interval_tm);
344         ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
345
346         ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
347
348         IWL_DEBUG_ASSOC(priv,
349                         "beacon interval %d beacon timer %d beacon tim %d\n",
350                         le16_to_cpu(ctx->timing.beacon_interval),
351                         le32_to_cpu(ctx->timing.beacon_init_val),
352                         le16_to_cpu(ctx->timing.atim_window));
353
354         return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
355                                 CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
356 }
357
358 static int iwlagn_rxon_disconn(struct iwl_priv *priv,
359                                struct iwl_rxon_context *ctx)
360 {
361         int ret;
362         struct iwl_rxon_cmd *active = (void *)&ctx->active;
363
364         if (ctx->ctxid == IWL_RXON_CTX_BSS) {
365                 ret = iwlagn_disable_bss(priv, ctx, &ctx->staging);
366         } else {
367                 ret = iwlagn_disable_pan(priv, ctx, &ctx->staging);
368                 if (ret)
369                         return ret;
370                 if (ctx->vif) {
371                         ret = iwl_send_rxon_timing(priv, ctx);
372                         if (ret) {
373                                 IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
374                                 return ret;
375                         }
376                         ret = iwlagn_disconn_pan(priv, ctx, &ctx->staging);
377                 }
378         }
379         if (ret)
380                 return ret;
381
382         /*
383          * Un-assoc RXON clears the station table and WEP
384          * keys, so we have to restore those afterwards.
385          */
386         iwl_clear_ucode_stations(priv, ctx);
387         /* update -- might need P2P now */
388         iwl_update_bcast_station(priv, ctx);
389         iwl_restore_stations(priv, ctx);
390         ret = iwl_restore_default_wep_keys(priv, ctx);
391         if (ret) {
392                 IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
393                 return ret;
394         }
395
396         memcpy(active, &ctx->staging, sizeof(*active));
397         return 0;
398 }
399
400 static int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
401 {
402         int ret;
403         s8 prev_tx_power;
404         bool defer;
405         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
406
407         if (priv->calib_disabled & IWL_TX_POWER_CALIB_DISABLED)
408                 return 0;
409
410         lockdep_assert_held(&priv->mutex);
411
412         if (priv->tx_power_user_lmt == tx_power && !force)
413                 return 0;
414
415         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
416                 IWL_WARN(priv,
417                          "Requested user TXPOWER %d below lower limit %d.\n",
418                          tx_power,
419                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
420                 return -EINVAL;
421         }
422
423         if (tx_power > DIV_ROUND_UP(priv->nvm_data->max_tx_pwr_half_dbm, 2)) {
424                 IWL_WARN(priv,
425                         "Requested user TXPOWER %d above upper limit %d.\n",
426                          tx_power, priv->nvm_data->max_tx_pwr_half_dbm);
427                 return -EINVAL;
428         }
429
430         if (!iwl_is_ready_rf(priv))
431                 return -EIO;
432
433         /* scan complete and commit_rxon use tx_power_next value,
434          * it always need to be updated for newest request */
435         priv->tx_power_next = tx_power;
436
437         /* do not set tx power when scanning or channel changing */
438         defer = test_bit(STATUS_SCANNING, &priv->status) ||
439                 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
440         if (defer && !force) {
441                 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
442                 return 0;
443         }
444
445         prev_tx_power = priv->tx_power_user_lmt;
446         priv->tx_power_user_lmt = tx_power;
447
448         ret = iwlagn_send_tx_power(priv);
449
450         /* if fail to set tx_power, restore the orig. tx power */
451         if (ret) {
452                 priv->tx_power_user_lmt = prev_tx_power;
453                 priv->tx_power_next = prev_tx_power;
454         }
455         return ret;
456 }
457
458 static int iwlagn_rxon_connect(struct iwl_priv *priv,
459                                struct iwl_rxon_context *ctx)
460 {
461         int ret;
462         struct iwl_rxon_cmd *active = (void *)&ctx->active;
463
464         /* RXON timing must be before associated RXON */
465         if (ctx->ctxid == IWL_RXON_CTX_BSS) {
466                 ret = iwl_send_rxon_timing(priv, ctx);
467                 if (ret) {
468                         IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
469                         return ret;
470                 }
471         }
472         /* QoS info may be cleared by previous un-assoc RXON */
473         iwlagn_update_qos(priv, ctx);
474
475         /*
476          * We'll run into this code path when beaconing is
477          * enabled, but then we also need to send the beacon
478          * to the device.
479          */
480         if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
481                 ret = iwlagn_update_beacon(priv, ctx->vif);
482                 if (ret) {
483                         IWL_ERR(priv,
484                                 "Error sending required beacon (%d)!\n",
485                                 ret);
486                         return ret;
487                 }
488         }
489
490         priv->start_calib = 0;
491         /*
492          * Apply the new configuration.
493          *
494          * Associated RXON doesn't clear the station table in uCode,
495          * so we don't need to restore stations etc. after this.
496          */
497         ret = iwl_dvm_send_cmd_pdu(priv, ctx->rxon_cmd, CMD_SYNC,
498                       sizeof(struct iwl_rxon_cmd), &ctx->staging);
499         if (ret) {
500                 IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
501                 return ret;
502         }
503         memcpy(active, &ctx->staging, sizeof(*active));
504
505         /* IBSS beacon needs to be sent after setting assoc */
506         if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
507                 if (iwlagn_update_beacon(priv, ctx->vif))
508                         IWL_ERR(priv, "Error sending IBSS beacon\n");
509         iwl_init_sensitivity(priv);
510
511         /*
512          * If we issue a new RXON command which required a tune then
513          * we must send a new TXPOWER command or we won't be able to
514          * Tx any frames.
515          *
516          * It's expected we set power here if channel is changing.
517          */
518         ret = iwl_set_tx_power(priv, priv->tx_power_next, true);
519         if (ret) {
520                 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
521                 return ret;
522         }
523
524         if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
525             priv->cfg->ht_params && priv->cfg->ht_params->smps_mode)
526                 ieee80211_request_smps(ctx->vif,
527                                        priv->cfg->ht_params->smps_mode);
528
529         return 0;
530 }
531
532 int iwlagn_set_pan_params(struct iwl_priv *priv)
533 {
534         struct iwl_wipan_params_cmd cmd;
535         struct iwl_rxon_context *ctx_bss, *ctx_pan;
536         int slot0 = 300, slot1 = 0;
537         int ret;
538
539         if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
540                 return 0;
541
542         BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
543
544         lockdep_assert_held(&priv->mutex);
545
546         ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
547         ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
548
549         /*
550          * If the PAN context is inactive, then we don't need
551          * to update the PAN parameters, the last thing we'll
552          * have done before it goes inactive is making the PAN
553          * parameters be WLAN-only.
554          */
555         if (!ctx_pan->is_active)
556                 return 0;
557
558         memset(&cmd, 0, sizeof(cmd));
559
560         /* only 2 slots are currently allowed */
561         cmd.num_slots = 2;
562
563         cmd.slots[0].type = 0; /* BSS */
564         cmd.slots[1].type = 1; /* PAN */
565
566         if (priv->hw_roc_setup) {
567                 /* both contexts must be used for this to happen */
568                 slot1 = IWL_MIN_SLOT_TIME;
569                 slot0 = 3000;
570         } else if (ctx_bss->vif && ctx_pan->vif) {
571                 int bcnint = ctx_pan->beacon_int;
572                 int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
573
574                 /* should be set, but seems unused?? */
575                 cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
576
577                 if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
578                     bcnint &&
579                     bcnint != ctx_bss->beacon_int) {
580                         IWL_ERR(priv,
581                                 "beacon intervals don't match (%d, %d)\n",
582                                 ctx_bss->beacon_int, ctx_pan->beacon_int);
583                 } else
584                         bcnint = max_t(int, bcnint,
585                                        ctx_bss->beacon_int);
586                 if (!bcnint)
587                         bcnint = DEFAULT_BEACON_INTERVAL;
588                 slot0 = bcnint / 2;
589                 slot1 = bcnint - slot0;
590
591                 if (test_bit(STATUS_SCAN_HW, &priv->status) ||
592                     (!ctx_bss->vif->bss_conf.idle &&
593                      !ctx_bss->vif->bss_conf.assoc)) {
594                         slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
595                         slot1 = IWL_MIN_SLOT_TIME;
596                 } else if (!ctx_pan->vif->bss_conf.idle &&
597                            !ctx_pan->vif->bss_conf.assoc) {
598                         slot1 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
599                         slot0 = IWL_MIN_SLOT_TIME;
600                 }
601         } else if (ctx_pan->vif) {
602                 slot0 = 0;
603                 slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
604                                         ctx_pan->beacon_int;
605                 slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
606
607                 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
608                         slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
609                         slot1 = IWL_MIN_SLOT_TIME;
610                 }
611         }
612
613         cmd.slots[0].width = cpu_to_le16(slot0);
614         cmd.slots[1].width = cpu_to_le16(slot1);
615
616         ret = iwl_dvm_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
617                         sizeof(cmd), &cmd);
618         if (ret)
619                 IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
620
621         return ret;
622 }
623
624 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
625                              struct iwl_ht_config *ht_conf,
626                              struct iwl_rxon_context *ctx)
627 {
628         struct iwl_rxon_cmd *rxon = &ctx->staging;
629
630         if (!ctx->ht.enabled) {
631                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
632                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
633                         RXON_FLG_HT40_PROT_MSK |
634                         RXON_FLG_HT_PROT_MSK);
635                 return;
636         }
637
638         /* FIXME: if the definition of ht.protection changed, the "translation"
639          * will be needed for rxon->flags
640          */
641         rxon->flags |= cpu_to_le32(ctx->ht.protection <<
642                                    RXON_FLG_HT_OPERATING_MODE_POS);
643
644         /* Set up channel bandwidth:
645          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
646         /* clear the HT channel mode before set the mode */
647         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
648                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
649         if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
650                 /* pure ht40 */
651                 if (ctx->ht.protection ==
652                     IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
653                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
654                         /*
655                          * Note: control channel is opposite of extension
656                          * channel
657                          */
658                         switch (ctx->ht.extension_chan_offset) {
659                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
660                                 rxon->flags &=
661                                         ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
662                                 break;
663                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
664                                 rxon->flags |=
665                                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
666                                 break;
667                         }
668                 } else {
669                         /*
670                          * Note: control channel is opposite of extension
671                          * channel
672                          */
673                         switch (ctx->ht.extension_chan_offset) {
674                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
675                                 rxon->flags &=
676                                         ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
677                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
678                                 break;
679                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
680                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
681                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
682                                 break;
683                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
684                         default:
685                                 /*
686                                  * channel location only valid if in Mixed
687                                  * mode
688                                  */
689                                 IWL_ERR(priv,
690                                         "invalid extension channel offset\n");
691                                 break;
692                         }
693                 }
694         } else {
695                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
696         }
697
698         iwlagn_set_rxon_chain(priv, ctx);
699
700         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
701                         "extension channel offset 0x%x\n",
702                         le32_to_cpu(rxon->flags), ctx->ht.protection,
703                         ctx->ht.extension_chan_offset);
704 }
705
706 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
707 {
708         struct iwl_rxon_context *ctx;
709
710         for_each_context(priv, ctx)
711                 _iwl_set_rxon_ht(priv, ht_conf, ctx);
712 }
713
714 /**
715  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
716  * @ch: requested channel as a pointer to struct ieee80211_channel
717
718  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
719  * in the staging RXON flag structure based on the ch->band
720  */
721 void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
722                          struct iwl_rxon_context *ctx)
723 {
724         enum ieee80211_band band = ch->band;
725         u16 channel = ch->hw_value;
726
727         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
728             (priv->band == band))
729                 return;
730
731         ctx->staging.channel = cpu_to_le16(channel);
732         if (band == IEEE80211_BAND_5GHZ)
733                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
734         else
735                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
736
737         priv->band = band;
738
739         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
740
741 }
742
743 void iwl_set_flags_for_band(struct iwl_priv *priv,
744                             struct iwl_rxon_context *ctx,
745                             enum ieee80211_band band,
746                             struct ieee80211_vif *vif)
747 {
748         if (band == IEEE80211_BAND_5GHZ) {
749                 ctx->staging.flags &=
750                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
751                       | RXON_FLG_CCK_MSK);
752                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
753         } else {
754                 /* Copied from iwl_post_associate() */
755                 if (vif && vif->bss_conf.use_short_slot)
756                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
757                 else
758                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
759
760                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
761                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
762                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
763         }
764 }
765
766 static void iwl_set_rxon_hwcrypto(struct iwl_priv *priv,
767                                   struct iwl_rxon_context *ctx, int hw_decrypt)
768 {
769         struct iwl_rxon_cmd *rxon = &ctx->staging;
770
771         if (hw_decrypt)
772                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
773         else
774                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
775
776 }
777
778 /* validate RXON structure is valid */
779 static int iwl_check_rxon_cmd(struct iwl_priv *priv,
780                               struct iwl_rxon_context *ctx)
781 {
782         struct iwl_rxon_cmd *rxon = &ctx->staging;
783         u32 errors = 0;
784
785         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
786                 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
787                         IWL_WARN(priv, "check 2.4G: wrong narrow\n");
788                         errors |= BIT(0);
789                 }
790                 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
791                         IWL_WARN(priv, "check 2.4G: wrong radar\n");
792                         errors |= BIT(1);
793                 }
794         } else {
795                 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
796                         IWL_WARN(priv, "check 5.2G: not short slot!\n");
797                         errors |= BIT(2);
798                 }
799                 if (rxon->flags & RXON_FLG_CCK_MSK) {
800                         IWL_WARN(priv, "check 5.2G: CCK!\n");
801                         errors |= BIT(3);
802                 }
803         }
804         if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
805                 IWL_WARN(priv, "mac/bssid mcast!\n");
806                 errors |= BIT(4);
807         }
808
809         /* make sure basic rates 6Mbps and 1Mbps are supported */
810         if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
811             (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
812                 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
813                 errors |= BIT(5);
814         }
815
816         if (le16_to_cpu(rxon->assoc_id) > 2007) {
817                 IWL_WARN(priv, "aid > 2007\n");
818                 errors |= BIT(6);
819         }
820
821         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
822                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
823                 IWL_WARN(priv, "CCK and short slot\n");
824                 errors |= BIT(7);
825         }
826
827         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
828                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
829                 IWL_WARN(priv, "CCK and auto detect");
830                 errors |= BIT(8);
831         }
832
833         if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
834                             RXON_FLG_TGG_PROTECT_MSK)) ==
835                             RXON_FLG_TGG_PROTECT_MSK) {
836                 IWL_WARN(priv, "TGg but no auto-detect\n");
837                 errors |= BIT(9);
838         }
839
840         if (rxon->channel == 0) {
841                 IWL_WARN(priv, "zero channel is invalid\n");
842                 errors |= BIT(10);
843         }
844
845         WARN(errors, "Invalid RXON (%#x), channel %d",
846              errors, le16_to_cpu(rxon->channel));
847
848         return errors ? -EINVAL : 0;
849 }
850
851 /**
852  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
853  * @priv: staging_rxon is compared to active_rxon
854  *
855  * If the RXON structure is changing enough to require a new tune,
856  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
857  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
858  */
859 static int iwl_full_rxon_required(struct iwl_priv *priv,
860                                   struct iwl_rxon_context *ctx)
861 {
862         const struct iwl_rxon_cmd *staging = &ctx->staging;
863         const struct iwl_rxon_cmd *active = &ctx->active;
864
865 #define CHK(cond)                                                       \
866         if ((cond)) {                                                   \
867                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
868                 return 1;                                               \
869         }
870
871 #define CHK_NEQ(c1, c2)                                         \
872         if ((c1) != (c2)) {                                     \
873                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
874                                #c1 " != " #c2 " - %d != %d\n",  \
875                                (c1), (c2));                     \
876                 return 1;                                       \
877         }
878
879         /* These items are only settable from the full RXON command */
880         CHK(!iwl_is_associated_ctx(ctx));
881         CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
882         CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
883         CHK(!ether_addr_equal(staging->wlap_bssid_addr,
884                               active->wlap_bssid_addr));
885         CHK_NEQ(staging->dev_type, active->dev_type);
886         CHK_NEQ(staging->channel, active->channel);
887         CHK_NEQ(staging->air_propagation, active->air_propagation);
888         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
889                 active->ofdm_ht_single_stream_basic_rates);
890         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
891                 active->ofdm_ht_dual_stream_basic_rates);
892         CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
893                 active->ofdm_ht_triple_stream_basic_rates);
894         CHK_NEQ(staging->assoc_id, active->assoc_id);
895
896         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
897          * be updated with the RXON_ASSOC command -- however only some
898          * flag transitions are allowed using RXON_ASSOC */
899
900         /* Check if we are not switching bands */
901         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
902                 active->flags & RXON_FLG_BAND_24G_MSK);
903
904         /* Check if we are switching association toggle */
905         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
906                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
907
908 #undef CHK
909 #undef CHK_NEQ
910
911         return 0;
912 }
913
914 #ifdef CONFIG_IWLWIFI_DEBUG
915 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
916                              enum iwl_rxon_context_id ctxid)
917 {
918         struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
919         struct iwl_rxon_cmd *rxon = &ctx->staging;
920
921         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
922         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
923         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
924                         le16_to_cpu(rxon->channel));
925         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n",
926                         le32_to_cpu(rxon->flags));
927         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
928                         le32_to_cpu(rxon->filter_flags));
929         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
930         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
931                         rxon->ofdm_basic_rates);
932         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
933                         rxon->cck_basic_rates);
934         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
935         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
936         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
937                         le16_to_cpu(rxon->assoc_id));
938 }
939 #endif
940
941 static void iwl_calc_basic_rates(struct iwl_priv *priv,
942                                  struct iwl_rxon_context *ctx)
943 {
944         int lowest_present_ofdm = 100;
945         int lowest_present_cck = 100;
946         u8 cck = 0;
947         u8 ofdm = 0;
948
949         if (ctx->vif) {
950                 struct ieee80211_supported_band *sband;
951                 unsigned long basic = ctx->vif->bss_conf.basic_rates;
952                 int i;
953
954                 sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
955
956                 for_each_set_bit(i, &basic, BITS_PER_LONG) {
957                         int hw = sband->bitrates[i].hw_value;
958                         if (hw >= IWL_FIRST_OFDM_RATE) {
959                                 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
960                                 if (lowest_present_ofdm > hw)
961                                         lowest_present_ofdm = hw;
962                         } else {
963                                 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
964
965                                 cck |= BIT(hw);
966                                 if (lowest_present_cck > hw)
967                                         lowest_present_cck = hw;
968                         }
969                 }
970         }
971
972         /*
973          * Now we've got the basic rates as bitmaps in the ofdm and cck
974          * variables. This isn't sufficient though, as there might not
975          * be all the right rates in the bitmap. E.g. if the only basic
976          * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
977          * and 6 Mbps because the 802.11-2007 standard says in 9.6:
978          *
979          *    [...] a STA responding to a received frame shall transmit
980          *    its Control Response frame [...] at the highest rate in the
981          *    BSSBasicRateSet parameter that is less than or equal to the
982          *    rate of the immediately previous frame in the frame exchange
983          *    sequence ([...]) and that is of the same modulation class
984          *    ([...]) as the received frame. If no rate contained in the
985          *    BSSBasicRateSet parameter meets these conditions, then the
986          *    control frame sent in response to a received frame shall be
987          *    transmitted at the highest mandatory rate of the PHY that is
988          *    less than or equal to the rate of the received frame, and
989          *    that is of the same modulation class as the received frame.
990          *
991          * As a consequence, we need to add all mandatory rates that are
992          * lower than all of the basic rates to these bitmaps.
993          */
994
995         if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
996                 ofdm |= IWL_RATE_24M_MASK >> IWL_FIRST_OFDM_RATE;
997         if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
998                 ofdm |= IWL_RATE_12M_MASK >> IWL_FIRST_OFDM_RATE;
999         /* 6M already there or needed so always add */
1000         ofdm |= IWL_RATE_6M_MASK >> IWL_FIRST_OFDM_RATE;
1001
1002         /*
1003          * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
1004          * Note, however:
1005          *  - if no CCK rates are basic, it must be ERP since there must
1006          *    be some basic rates at all, so they're OFDM => ERP PHY
1007          *    (or we're in 5 GHz, and the cck bitmap will never be used)
1008          *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
1009          *  - if 5.5M is basic, 1M and 2M are mandatory
1010          *  - if 2M is basic, 1M is mandatory
1011          *  - if 1M is basic, that's the only valid ACK rate.
1012          * As a consequence, it's not as complicated as it sounds, just add
1013          * any lower rates to the ACK rate bitmap.
1014          */
1015         if (IWL_RATE_11M_INDEX < lowest_present_cck)
1016                 cck |= IWL_RATE_11M_MASK >> IWL_FIRST_CCK_RATE;
1017         if (IWL_RATE_5M_INDEX < lowest_present_cck)
1018                 cck |= IWL_RATE_5M_MASK >> IWL_FIRST_CCK_RATE;
1019         if (IWL_RATE_2M_INDEX < lowest_present_cck)
1020                 cck |= IWL_RATE_2M_MASK >> IWL_FIRST_CCK_RATE;
1021         /* 1M already there or needed so always add */
1022         cck |= IWL_RATE_1M_MASK >> IWL_FIRST_CCK_RATE;
1023
1024         IWL_DEBUG_RATE(priv, "Set basic rates cck:0x%.2x ofdm:0x%.2x\n",
1025                        cck, ofdm);
1026
1027         /* "basic_rates" is a misnomer here -- should be called ACK rates */
1028         ctx->staging.cck_basic_rates = cck;
1029         ctx->staging.ofdm_basic_rates = ofdm;
1030 }
1031
1032 /**
1033  * iwlagn_commit_rxon - commit staging_rxon to hardware
1034  *
1035  * The RXON command in staging_rxon is committed to the hardware and
1036  * the active_rxon structure is updated with the new data.  This
1037  * function correctly transitions out of the RXON_ASSOC_MSK state if
1038  * a HW tune is required based on the RXON structure changes.
1039  *
1040  * The connect/disconnect flow should be as the following:
1041  *
1042  * 1. make sure send RXON command with association bit unset if not connect
1043  *      this should include the channel and the band for the candidate
1044  *      to be connected to
1045  * 2. Add Station before RXON association with the AP
1046  * 3. RXON_timing has to send before RXON for connection
1047  * 4. full RXON command - associated bit set
1048  * 5. use RXON_ASSOC command to update any flags changes
1049  */
1050 int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1051 {
1052         /* cast away the const for active_rxon in this function */
1053         struct iwl_rxon_cmd *active = (void *)&ctx->active;
1054         bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
1055         int ret;
1056
1057         lockdep_assert_held(&priv->mutex);
1058
1059         if (!iwl_is_alive(priv))
1060                 return -EBUSY;
1061
1062         /* This function hardcodes a bunch of dual-mode assumptions */
1063         BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1064
1065         if (!ctx->is_active)
1066                 return 0;
1067
1068         /* always get timestamp with Rx frame */
1069         ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
1070
1071         /* recalculate basic rates */
1072         iwl_calc_basic_rates(priv, ctx);
1073
1074         /*
1075          * force CTS-to-self frames protection if RTS-CTS is not preferred
1076          * one aggregation protection method
1077          */
1078         if (!priv->hw_params.use_rts_for_aggregation)
1079                 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1080
1081         if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
1082             !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
1083                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
1084         else
1085                 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1086
1087         iwl_print_rx_config_cmd(priv, ctx->ctxid);
1088         ret = iwl_check_rxon_cmd(priv, ctx);
1089         if (ret) {
1090                 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
1091                 return -EINVAL;
1092         }
1093
1094         /*
1095          * receive commit_rxon request
1096          * abort any previous channel switch if still in process
1097          */
1098         if (test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status) &&
1099             (priv->switch_channel != ctx->staging.channel)) {
1100                 IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
1101                               le16_to_cpu(priv->switch_channel));
1102                 iwl_chswitch_done(priv, false);
1103         }
1104
1105         /*
1106          * If we don't need to send a full RXON, we can use
1107          * iwl_rxon_assoc_cmd which is used to reconfigure filter
1108          * and other flags for the current radio configuration.
1109          */
1110         if (!iwl_full_rxon_required(priv, ctx)) {
1111                 ret = iwlagn_send_rxon_assoc(priv, ctx);
1112                 if (ret) {
1113                         IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
1114                         return ret;
1115                 }
1116
1117                 memcpy(active, &ctx->staging, sizeof(*active));
1118                 /*
1119                  * We do not commit tx power settings while channel changing,
1120                  * do it now if after settings changed.
1121                  */
1122                 iwl_set_tx_power(priv, priv->tx_power_next, false);
1123
1124                 /* make sure we are in the right PS state */
1125                 iwl_power_update_mode(priv, true);
1126
1127                 return 0;
1128         }
1129
1130         iwl_set_rxon_hwcrypto(priv, ctx, !iwlwifi_mod_params.sw_crypto);
1131
1132         IWL_DEBUG_INFO(priv,
1133                        "Going to commit RXON\n"
1134                        "  * with%s RXON_FILTER_ASSOC_MSK\n"
1135                        "  * channel = %d\n"
1136                        "  * bssid = %pM\n",
1137                        (new_assoc ? "" : "out"),
1138                        le16_to_cpu(ctx->staging.channel),
1139                        ctx->staging.bssid_addr);
1140
1141         /*
1142          * Always clear associated first, but with the correct config.
1143          * This is required as for example station addition for the
1144          * AP station must be done after the BSSID is set to correctly
1145          * set up filters in the device.
1146          */
1147         ret = iwlagn_rxon_disconn(priv, ctx);
1148         if (ret)
1149                 return ret;
1150
1151         ret = iwlagn_set_pan_params(priv);
1152         if (ret)
1153                 return ret;
1154
1155         if (new_assoc)
1156                 return iwlagn_rxon_connect(priv, ctx);
1157
1158         return 0;
1159 }
1160
1161 void iwlagn_config_ht40(struct ieee80211_conf *conf,
1162         struct iwl_rxon_context *ctx)
1163 {
1164         if (conf_is_ht40_minus(conf)) {
1165                 ctx->ht.extension_chan_offset =
1166                         IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1167                 ctx->ht.is_40mhz = true;
1168         } else if (conf_is_ht40_plus(conf)) {
1169                 ctx->ht.extension_chan_offset =
1170                         IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1171                 ctx->ht.is_40mhz = true;
1172         } else {
1173                 ctx->ht.extension_chan_offset =
1174                         IEEE80211_HT_PARAM_CHA_SEC_NONE;
1175                 ctx->ht.is_40mhz = false;
1176         }
1177 }
1178
1179 int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
1180 {
1181         struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1182         struct iwl_rxon_context *ctx;
1183         struct ieee80211_conf *conf = &hw->conf;
1184         struct ieee80211_channel *channel = conf->channel;
1185         int ret = 0;
1186
1187         IWL_DEBUG_MAC80211(priv, "enter: changed %#x\n", changed);
1188
1189         mutex_lock(&priv->mutex);
1190
1191         if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
1192                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
1193                 goto out;
1194         }
1195
1196         if (!iwl_is_ready(priv)) {
1197                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1198                 goto out;
1199         }
1200
1201         if (changed & (IEEE80211_CONF_CHANGE_SMPS |
1202                        IEEE80211_CONF_CHANGE_CHANNEL)) {
1203                 /* mac80211 uses static for non-HT which is what we want */
1204                 priv->current_ht_config.smps = conf->smps_mode;
1205
1206                 /*
1207                  * Recalculate chain counts.
1208                  *
1209                  * If monitor mode is enabled then mac80211 will
1210                  * set up the SM PS mode to OFF if an HT channel is
1211                  * configured.
1212                  */
1213                 for_each_context(priv, ctx)
1214                         iwlagn_set_rxon_chain(priv, ctx);
1215         }
1216
1217         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1218                 for_each_context(priv, ctx) {
1219                         /* Configure HT40 channels */
1220                         if (ctx->ht.enabled != conf_is_ht(conf))
1221                                 ctx->ht.enabled = conf_is_ht(conf);
1222
1223                         if (ctx->ht.enabled) {
1224                                 /* if HT40 is used, it should not change
1225                                  * after associated except channel switch */
1226                                 if (!ctx->ht.is_40mhz ||
1227                                                 !iwl_is_associated_ctx(ctx))
1228                                         iwlagn_config_ht40(conf, ctx);
1229                         } else
1230                                 ctx->ht.is_40mhz = false;
1231
1232                         /*
1233                          * Default to no protection. Protection mode will
1234                          * later be set from BSS config in iwl_ht_conf
1235                          */
1236                         ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1237
1238                         /* if we are switching from ht to 2.4 clear flags
1239                          * from any ht related info since 2.4 does not
1240                          * support ht */
1241                         if (le16_to_cpu(ctx->staging.channel) !=
1242                             channel->hw_value)
1243                                 ctx->staging.flags = 0;
1244
1245                         iwl_set_rxon_channel(priv, channel, ctx);
1246                         iwl_set_rxon_ht(priv, &priv->current_ht_config);
1247
1248                         iwl_set_flags_for_band(priv, ctx, channel->band,
1249                                                ctx->vif);
1250                 }
1251
1252                 iwl_update_bcast_stations(priv);
1253         }
1254
1255         if (changed & (IEEE80211_CONF_CHANGE_PS |
1256                         IEEE80211_CONF_CHANGE_IDLE)) {
1257                 ret = iwl_power_update_mode(priv, false);
1258                 if (ret)
1259                         IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
1260         }
1261
1262         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1263                 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
1264                         priv->tx_power_user_lmt, conf->power_level);
1265
1266                 iwl_set_tx_power(priv, conf->power_level, false);
1267         }
1268
1269         for_each_context(priv, ctx) {
1270                 if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1271                         continue;
1272                 iwlagn_commit_rxon(priv, ctx);
1273         }
1274  out:
1275         mutex_unlock(&priv->mutex);
1276         IWL_DEBUG_MAC80211(priv, "leave\n");
1277
1278         return ret;
1279 }
1280
1281 static void iwlagn_check_needed_chains(struct iwl_priv *priv,
1282                                        struct iwl_rxon_context *ctx,
1283                                        struct ieee80211_bss_conf *bss_conf)
1284 {
1285         struct ieee80211_vif *vif = ctx->vif;
1286         struct iwl_rxon_context *tmp;
1287         struct ieee80211_sta *sta;
1288         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1289         struct ieee80211_sta_ht_cap *ht_cap;
1290         bool need_multiple;
1291
1292         lockdep_assert_held(&priv->mutex);
1293
1294         switch (vif->type) {
1295         case NL80211_IFTYPE_STATION:
1296                 rcu_read_lock();
1297                 sta = ieee80211_find_sta(vif, bss_conf->bssid);
1298                 if (!sta) {
1299                         /*
1300                          * If at all, this can only happen through a race
1301                          * when the AP disconnects us while we're still
1302                          * setting up the connection, in that case mac80211
1303                          * will soon tell us about that.
1304                          */
1305                         need_multiple = false;
1306                         rcu_read_unlock();
1307                         break;
1308                 }
1309
1310                 ht_cap = &sta->ht_cap;
1311
1312                 need_multiple = true;
1313
1314                 /*
1315                  * If the peer advertises no support for receiving 2 and 3
1316                  * stream MCS rates, it can't be transmitting them either.
1317                  */
1318                 if (ht_cap->mcs.rx_mask[1] == 0 &&
1319                     ht_cap->mcs.rx_mask[2] == 0) {
1320                         need_multiple = false;
1321                 } else if (!(ht_cap->mcs.tx_params &
1322                                                 IEEE80211_HT_MCS_TX_DEFINED)) {
1323                         /* If it can't TX MCS at all ... */
1324                         need_multiple = false;
1325                 } else if (ht_cap->mcs.tx_params &
1326                                                 IEEE80211_HT_MCS_TX_RX_DIFF) {
1327                         int maxstreams;
1328
1329                         /*
1330                          * But if it can receive them, it might still not
1331                          * be able to transmit them, which is what we need
1332                          * to check here -- so check the number of streams
1333                          * it advertises for TX (if different from RX).
1334                          */
1335
1336                         maxstreams = (ht_cap->mcs.tx_params &
1337                                  IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK);
1338                         maxstreams >>=
1339                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1340                         maxstreams += 1;
1341
1342                         if (maxstreams <= 1)
1343                                 need_multiple = false;
1344                 }
1345
1346                 rcu_read_unlock();
1347                 break;
1348         case NL80211_IFTYPE_ADHOC:
1349                 /* currently */
1350                 need_multiple = false;
1351                 break;
1352         default:
1353                 /* only AP really */
1354                 need_multiple = true;
1355                 break;
1356         }
1357
1358         ctx->ht_need_multiple_chains = need_multiple;
1359
1360         if (!need_multiple) {
1361                 /* check all contexts */
1362                 for_each_context(priv, tmp) {
1363                         if (!tmp->vif)
1364                                 continue;
1365                         if (tmp->ht_need_multiple_chains) {
1366                                 need_multiple = true;
1367                                 break;
1368                         }
1369                 }
1370         }
1371
1372         ht_conf->single_chain_sufficient = !need_multiple;
1373 }
1374
1375 static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
1376 {
1377         struct iwl_chain_noise_data *data = &priv->chain_noise_data;
1378         int ret;
1379
1380         if (!(priv->calib_disabled & IWL_CHAIN_NOISE_CALIB_DISABLED))
1381                 return;
1382
1383         if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
1384             iwl_is_any_associated(priv)) {
1385                 struct iwl_calib_chain_noise_reset_cmd cmd;
1386
1387                 /* clear data for chain noise calibration algorithm */
1388                 data->chain_noise_a = 0;
1389                 data->chain_noise_b = 0;
1390                 data->chain_noise_c = 0;
1391                 data->chain_signal_a = 0;
1392                 data->chain_signal_b = 0;
1393                 data->chain_signal_c = 0;
1394                 data->beacon_count = 0;
1395
1396                 memset(&cmd, 0, sizeof(cmd));
1397                 iwl_set_calib_hdr(&cmd.hdr,
1398                         priv->phy_calib_chain_noise_reset_cmd);
1399                 ret = iwl_dvm_send_cmd_pdu(priv,
1400                                         REPLY_PHY_CALIBRATION_CMD,
1401                                         CMD_SYNC, sizeof(cmd), &cmd);
1402                 if (ret)
1403                         IWL_ERR(priv,
1404                                 "Could not send REPLY_PHY_CALIBRATION_CMD\n");
1405                 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1406                 IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
1407         }
1408 }
1409
1410 void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
1411                              struct ieee80211_vif *vif,
1412                              struct ieee80211_bss_conf *bss_conf,
1413                              u32 changes)
1414 {
1415         struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1416         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1417         int ret;
1418         bool force = false;
1419
1420         mutex_lock(&priv->mutex);
1421
1422         if (unlikely(!iwl_is_ready(priv))) {
1423                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
1424                 mutex_unlock(&priv->mutex);
1425                 return;
1426         }
1427
1428         if (unlikely(!ctx->vif)) {
1429                 IWL_DEBUG_MAC80211(priv, "leave - vif is NULL\n");
1430                 mutex_unlock(&priv->mutex);
1431                 return;
1432         }
1433
1434         if (changes & BSS_CHANGED_BEACON_INT)
1435                 force = true;
1436
1437         if (changes & BSS_CHANGED_QOS) {
1438                 ctx->qos_data.qos_active = bss_conf->qos;
1439                 iwlagn_update_qos(priv, ctx);
1440         }
1441
1442         ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
1443         if (vif->bss_conf.use_short_preamble)
1444                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1445         else
1446                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1447
1448         if (changes & BSS_CHANGED_ASSOC) {
1449                 if (bss_conf->assoc) {
1450                         priv->timestamp = bss_conf->sync_tsf;
1451                         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1452                 } else {
1453                         /*
1454                          * If we disassociate while there are pending
1455                          * frames, just wake up the queues and let the
1456                          * frames "escape" ... This shouldn't really
1457                          * be happening to start with, but we should
1458                          * not get stuck in this case either since it
1459                          * can happen if userspace gets confused.
1460                          */
1461                         iwlagn_lift_passive_no_rx(priv);
1462
1463                         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1464
1465                         if (ctx->ctxid == IWL_RXON_CTX_BSS)
1466                                 priv->have_rekey_data = false;
1467                 }
1468
1469                 iwlagn_bt_coex_rssi_monitor(priv);
1470         }
1471
1472         if (ctx->ht.enabled) {
1473                 ctx->ht.protection = bss_conf->ht_operation_mode &
1474                                         IEEE80211_HT_OP_MODE_PROTECTION;
1475                 ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
1476                                         IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1477                 iwlagn_check_needed_chains(priv, ctx, bss_conf);
1478                 iwl_set_rxon_ht(priv, &priv->current_ht_config);
1479         }
1480
1481         iwlagn_set_rxon_chain(priv, ctx);
1482
1483         if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
1484                 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
1485         else
1486                 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
1487
1488         if (bss_conf->use_cts_prot)
1489                 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1490         else
1491                 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
1492
1493         memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
1494
1495         if (vif->type == NL80211_IFTYPE_AP ||
1496             vif->type == NL80211_IFTYPE_ADHOC) {
1497                 if (vif->bss_conf.enable_beacon) {
1498                         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
1499                         priv->beacon_ctx = ctx;
1500                 } else {
1501                         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1502                         priv->beacon_ctx = NULL;
1503                 }
1504         }
1505
1506         /*
1507          * If the ucode decides to do beacon filtering before
1508          * association, it will lose beacons that are needed
1509          * before sending frames out on passive channels. This
1510          * causes association failures on those channels. Enable
1511          * receiving beacons in such cases.
1512          */
1513
1514         if (vif->type == NL80211_IFTYPE_STATION) {
1515                 if (!bss_conf->assoc)
1516                         ctx->staging.filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
1517                 else
1518                         ctx->staging.filter_flags &=
1519                                                     ~RXON_FILTER_BCON_AWARE_MSK;
1520         }
1521
1522         if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1523                 iwlagn_commit_rxon(priv, ctx);
1524
1525         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
1526                 /*
1527                  * The chain noise calibration will enable PM upon
1528                  * completion. If calibration has already been run
1529                  * then we need to enable power management here.
1530                  */
1531                 if (priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE)
1532                         iwl_power_update_mode(priv, false);
1533
1534                 /* Enable RX differential gain and sensitivity calibrations */
1535                 iwlagn_chain_noise_reset(priv);
1536                 priv->start_calib = 1;
1537         }
1538
1539         if (changes & BSS_CHANGED_IBSS) {
1540                 ret = iwlagn_manage_ibss_station(priv, vif,
1541                                                  bss_conf->ibss_joined);
1542                 if (ret)
1543                         IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1544                                 bss_conf->ibss_joined ? "add" : "remove",
1545                                 bss_conf->bssid);
1546         }
1547
1548         if (changes & BSS_CHANGED_BEACON && priv->beacon_ctx == ctx) {
1549                 if (iwlagn_update_beacon(priv, vif))
1550                         IWL_ERR(priv, "Error updating beacon\n");
1551         }
1552
1553         mutex_unlock(&priv->mutex);
1554 }
1555
1556 void iwlagn_post_scan(struct iwl_priv *priv)
1557 {
1558         struct iwl_rxon_context *ctx;
1559
1560         /*
1561          * We do not commit power settings while scan is pending,
1562          * do it now if the settings changed.
1563          */
1564         iwl_power_set_mode(priv, &priv->power_data.sleep_cmd_next, false);
1565         iwl_set_tx_power(priv, priv->tx_power_next, false);
1566
1567         /*
1568          * Since setting the RXON may have been deferred while
1569          * performing the scan, fire one off if needed
1570          */
1571         for_each_context(priv, ctx)
1572                 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
1573                         iwlagn_commit_rxon(priv, ctx);
1574
1575         iwlagn_set_pan_params(priv);
1576 }