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