iwlagn: fix non-5000+ build
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / net / wireless / iwlwifi / iwl-agn-rxon.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  * Intel Linux Wireless <ilw@linux.intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26
27 #include "iwl-dev.h"
28 #include "iwl-agn.h"
29 #include "iwl-sta.h"
30 #include "iwl-core.h"
31 #include "iwl-agn-calib.h"
32
33 static int iwlagn_disable_bss(struct iwl_priv *priv,
34                               struct iwl_rxon_context *ctx,
35                               struct iwl_rxon_cmd *send)
36 {
37         __le32 old_filter = send->filter_flags;
38         int ret;
39
40         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
41         ret = iwl_send_cmd_pdu(priv, ctx->rxon_cmd, sizeof(*send), send);
42
43         send->filter_flags = old_filter;
44
45         if (ret)
46                 IWL_ERR(priv, "Error clearing ASSOC_MSK on BSS (%d)\n", ret);
47
48         return ret;
49 }
50
51 static int iwlagn_disable_pan(struct iwl_priv *priv,
52                               struct iwl_rxon_context *ctx,
53                               struct iwl_rxon_cmd *send)
54 {
55         __le32 old_filter = send->filter_flags;
56         u8 old_dev_type = send->dev_type;
57         int ret;
58
59         send->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
60         send->dev_type = RXON_DEV_TYPE_P2P;
61         ret = iwl_send_cmd_pdu(priv, ctx->rxon_cmd, sizeof(*send), send);
62
63         send->filter_flags = old_filter;
64         send->dev_type = old_dev_type;
65
66         if (ret)
67                 IWL_ERR(priv, "Error disabling PAN (%d)\n", ret);
68
69         /* FIXME: WAIT FOR PAN DISABLE */
70         msleep(300);
71
72         return ret;
73 }
74
75 static int iwlagn_update_beacon(struct iwl_priv *priv,
76                                 struct ieee80211_vif *vif)
77 {
78         lockdep_assert_held(&priv->mutex);
79
80         dev_kfree_skb(priv->beacon_skb);
81         priv->beacon_skb = ieee80211_beacon_get(priv->hw, vif);
82         if (!priv->beacon_skb)
83                 return -ENOMEM;
84         return iwlagn_send_beacon_cmd(priv);
85 }
86
87 /**
88  * iwlagn_commit_rxon - commit staging_rxon to hardware
89  *
90  * The RXON command in staging_rxon is committed to the hardware and
91  * the active_rxon structure is updated with the new data.  This
92  * function correctly transitions out of the RXON_ASSOC_MSK state if
93  * a HW tune is required based on the RXON structure changes.
94  */
95 int iwlagn_commit_rxon(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
96 {
97         /* cast away the const for active_rxon in this function */
98         struct iwl_rxon_cmd *active = (void *)&ctx->active;
99         bool old_assoc = !!(ctx->active.filter_flags & RXON_FILTER_ASSOC_MSK);
100         bool new_assoc = !!(ctx->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
101         int ret;
102
103         lockdep_assert_held(&priv->mutex);
104
105         if (!iwl_is_alive(priv))
106                 return -EBUSY;
107
108         /* This function hardcodes a bunch of dual-mode assumptions */
109         BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
110
111         if (!ctx->is_active)
112                 return 0;
113
114         /* always get timestamp with Rx frame */
115         ctx->staging.flags |= RXON_FLG_TSF2HOST_MSK;
116
117         if ((ctx->vif && ctx->vif->bss_conf.use_short_slot) ||
118             !(ctx->staging.flags & RXON_FLG_BAND_24G_MSK))
119                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
120         else
121                 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
122
123         ret = iwl_check_rxon_cmd(priv, ctx);
124         if (ret) {
125                 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
126                 return -EINVAL;
127         }
128
129         /*
130          * receive commit_rxon request
131          * abort any previous channel switch if still in process
132          */
133         if (priv->switch_rxon.switch_in_progress &&
134             (priv->switch_rxon.channel != ctx->staging.channel)) {
135                 IWL_DEBUG_11H(priv, "abort channel switch on %d\n",
136                       le16_to_cpu(priv->switch_rxon.channel));
137                 iwl_chswitch_done(priv, false);
138         }
139
140         /*
141          * If we don't need to send a full RXON, we can use
142          * iwl_rxon_assoc_cmd which is used to reconfigure filter
143          * and other flags for the current radio configuration.
144          */
145         if (!iwl_full_rxon_required(priv, ctx)) {
146                 ret = iwl_send_rxon_assoc(priv, ctx);
147                 if (ret) {
148                         IWL_ERR(priv, "Error setting RXON_ASSOC (%d)\n", ret);
149                         return ret;
150                 }
151
152                 memcpy(active, &ctx->staging, sizeof(*active));
153                 iwl_print_rx_config_cmd(priv, ctx);
154                 return 0;
155         }
156
157         if (priv->cfg->ops->hcmd->set_pan_params) {
158                 ret = priv->cfg->ops->hcmd->set_pan_params(priv);
159                 if (ret)
160                         return ret;
161         }
162
163         iwl_set_rxon_hwcrypto(priv, ctx, !priv->cfg->mod_params->sw_crypto);
164
165         IWL_DEBUG_INFO(priv,
166                        "Going to commit RXON\n"
167                        "  * with%s RXON_FILTER_ASSOC_MSK\n"
168                        "  * channel = %d\n"
169                        "  * bssid = %pM\n",
170                        (new_assoc ? "" : "out"),
171                        le16_to_cpu(ctx->staging.channel),
172                        ctx->staging.bssid_addr);
173
174         /*
175          * If we are currently associated and the new config is also
176          * going to be associated, OR if the new config is simply not
177          * associated, clear associated.
178          */
179         if ((old_assoc && new_assoc) || !new_assoc) {
180                 struct iwl_rxon_cmd *send = active;
181
182                 if (!new_assoc)
183                         send = &ctx->staging;
184
185                 if (ctx->ctxid == IWL_RXON_CTX_BSS)
186                         ret = iwlagn_disable_bss(priv, ctx, send);
187                 else
188                         ret = iwlagn_disable_pan(priv, ctx, send);
189                 if (ret)
190                         return ret;
191
192                 if (send != active)
193                         memcpy(active, send, sizeof(*active));
194
195                 /*
196                  * Un-assoc RXON clears the station table and WEP
197                  * keys, so we have to restore those afterwards.
198                  */
199                 iwl_clear_ucode_stations(priv, ctx);
200                 iwl_restore_stations(priv, ctx);
201                 ret = iwl_restore_default_wep_keys(priv, ctx);
202                 if (ret) {
203                         IWL_ERR(priv, "Failed to restore WEP keys (%d)\n", ret);
204                         return ret;
205                 }
206         }
207
208         /* RXON timing must be before associated RXON */
209         ret = iwl_send_rxon_timing(priv, ctx);
210         if (ret) {
211                 IWL_ERR(priv, "Failed to send timing (%d)!\n", ret);
212                 return ret;
213         }
214
215         if (new_assoc) {
216                 /*
217                  * We'll run into this code path when beaconing is
218                  * enabled, but then we also need to send the beacon
219                  * to the device.
220                  */
221                 if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_AP)) {
222                         ret = iwlagn_update_beacon(priv, ctx->vif);
223                         if (ret) {
224                                 IWL_ERR(priv,
225                                         "Error sending required beacon (%d)!\n",
226                                         ret);
227                                 return ret;
228                         }
229                 }
230
231                 priv->start_calib = 0;
232                 /*
233                  * Apply the new configuration.
234                  *
235                  * Associated RXON doesn't clear the station table in uCode,
236                  * so we don't need to restore stations etc. after this.
237                  */
238                 ret = iwl_send_cmd_pdu(priv, ctx->rxon_cmd,
239                               sizeof(struct iwl_rxon_cmd), &ctx->staging);
240                 if (ret) {
241                         IWL_ERR(priv, "Error setting new RXON (%d)\n", ret);
242                         return ret;
243                 }
244                 memcpy(active, &ctx->staging, sizeof(*active));
245
246                 /* IBSS beacon needs to be sent after setting assoc */
247                 if (ctx->vif && (ctx->vif->type == NL80211_IFTYPE_ADHOC))
248                         if (iwlagn_update_beacon(priv, ctx->vif))
249                                 IWL_ERR(priv, "Error sending IBSS beacon\n");
250         }
251
252         iwl_print_rx_config_cmd(priv, ctx);
253
254         iwl_init_sensitivity(priv);
255
256         /*
257          * If we issue a new RXON command which required a tune then we must
258          * send a new TXPOWER command or we won't be able to Tx any frames.
259          *
260          * FIXME: which RXON requires a tune? Can we optimise this out in
261          *        some cases?
262          */
263         ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
264         if (ret) {
265                 IWL_ERR(priv, "Error sending TX power (%d)\n", ret);
266                 return ret;
267         }
268
269         return 0;
270 }
271
272 int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
273 {
274         struct iwl_priv *priv = hw->priv;
275         struct iwl_rxon_context *ctx;
276         struct ieee80211_conf *conf = &hw->conf;
277         struct ieee80211_channel *channel = conf->channel;
278         const struct iwl_channel_info *ch_info;
279         int ret = 0;
280
281         IWL_DEBUG_MAC80211(priv, "changed %#x", changed);
282
283         mutex_lock(&priv->mutex);
284
285         if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
286                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
287                 goto out;
288         }
289
290         if (!iwl_is_ready(priv)) {
291                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
292                 goto out;
293         }
294
295         if (changed & (IEEE80211_CONF_CHANGE_SMPS |
296                        IEEE80211_CONF_CHANGE_CHANNEL)) {
297                 /* mac80211 uses static for non-HT which is what we want */
298                 priv->current_ht_config.smps = conf->smps_mode;
299
300                 /*
301                  * Recalculate chain counts.
302                  *
303                  * If monitor mode is enabled then mac80211 will
304                  * set up the SM PS mode to OFF if an HT channel is
305                  * configured.
306                  */
307                 if (priv->cfg->ops->hcmd->set_rxon_chain)
308                         for_each_context(priv, ctx)
309                                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
310         }
311
312         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
313                 unsigned long flags;
314
315                 ch_info = iwl_get_channel_info(priv, channel->band,
316                                                channel->hw_value);
317                 if (!is_channel_valid(ch_info)) {
318                         IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
319                         ret = -EINVAL;
320                         goto out;
321                 }
322
323                 spin_lock_irqsave(&priv->lock, flags);
324
325                 for_each_context(priv, ctx) {
326                         /* Configure HT40 channels */
327                         ctx->ht.enabled = conf_is_ht(conf);
328                         if (ctx->ht.enabled) {
329                                 if (conf_is_ht40_minus(conf)) {
330                                         ctx->ht.extension_chan_offset =
331                                                 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
332                                         ctx->ht.is_40mhz = true;
333                                 } else if (conf_is_ht40_plus(conf)) {
334                                         ctx->ht.extension_chan_offset =
335                                                 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
336                                         ctx->ht.is_40mhz = true;
337                                 } else {
338                                         ctx->ht.extension_chan_offset =
339                                                 IEEE80211_HT_PARAM_CHA_SEC_NONE;
340                                         ctx->ht.is_40mhz = false;
341                                 }
342                         } else
343                                 ctx->ht.is_40mhz = false;
344
345                         /*
346                          * Default to no protection. Protection mode will
347                          * later be set from BSS config in iwl_ht_conf
348                          */
349                         ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
350
351                         /* if we are switching from ht to 2.4 clear flags
352                          * from any ht related info since 2.4 does not
353                          * support ht */
354                         if (le16_to_cpu(ctx->staging.channel) !=
355                             channel->hw_value)
356                                 ctx->staging.flags = 0;
357
358                         iwl_set_rxon_channel(priv, channel, ctx);
359                         iwl_set_rxon_ht(priv, &priv->current_ht_config);
360
361                         iwl_set_flags_for_band(priv, ctx, channel->band,
362                                                ctx->vif);
363                 }
364
365                 spin_unlock_irqrestore(&priv->lock, flags);
366
367                 iwl_update_bcast_stations(priv);
368
369                 /*
370                  * The list of supported rates and rate mask can be different
371                  * for each band; since the band may have changed, reset
372                  * the rate mask to what mac80211 lists.
373                  */
374                 iwl_set_rate(priv);
375         }
376
377         if (changed & (IEEE80211_CONF_CHANGE_PS |
378                         IEEE80211_CONF_CHANGE_IDLE)) {
379                 ret = iwl_power_update_mode(priv, false);
380                 if (ret)
381                         IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
382         }
383
384         if (changed & IEEE80211_CONF_CHANGE_POWER) {
385                 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
386                         priv->tx_power_user_lmt, conf->power_level);
387
388                 iwl_set_tx_power(priv, conf->power_level, false);
389         }
390
391         for_each_context(priv, ctx) {
392                 if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
393                         continue;
394                 iwlagn_commit_rxon(priv, ctx);
395         }
396  out:
397         mutex_unlock(&priv->mutex);
398         return ret;
399 }
400
401 static void iwlagn_update_qos(struct iwl_priv *priv,
402                               struct iwl_rxon_context *ctx)
403 {
404         int ret;
405
406         if (!ctx->is_active)
407                 return;
408
409         ctx->qos_data.def_qos_parm.qos_flags = 0;
410
411         if (ctx->qos_data.qos_active)
412                 ctx->qos_data.def_qos_parm.qos_flags |=
413                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
414
415         if (ctx->ht.enabled)
416                 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
417
418         IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
419                       ctx->qos_data.qos_active,
420                       ctx->qos_data.def_qos_parm.qos_flags);
421
422         ret = iwl_send_cmd_pdu(priv, ctx->qos_cmd,
423                                sizeof(struct iwl_qosparam_cmd),
424                                &ctx->qos_data.def_qos_parm);
425         if (ret)
426                 IWL_ERR(priv, "Failed to update QoS\n");
427 }
428
429 static void iwlagn_check_needed_chains(struct iwl_priv *priv,
430                                        struct iwl_rxon_context *ctx,
431                                        struct ieee80211_bss_conf *bss_conf)
432 {
433         struct ieee80211_vif *vif = ctx->vif;
434         struct iwl_rxon_context *tmp;
435         struct ieee80211_sta *sta;
436         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
437         bool need_multiple;
438
439         lockdep_assert_held(&priv->mutex);
440
441         switch (vif->type) {
442         case NL80211_IFTYPE_STATION:
443                 rcu_read_lock();
444                 sta = ieee80211_find_sta(vif, bss_conf->bssid);
445                 if (sta) {
446                         struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
447                         int maxstreams;
448
449                         maxstreams = (ht_cap->mcs.tx_params &
450                                       IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
451                                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
452                         maxstreams += 1;
453
454                         if ((ht_cap->mcs.rx_mask[1] == 0) &&
455                             (ht_cap->mcs.rx_mask[2] == 0))
456                                 need_multiple = false;
457                         if (maxstreams <= 1)
458                                 need_multiple = true;
459                 } else {
460                         /*
461                          * If at all, this can only happen through a race
462                          * when the AP disconnects us while we're still
463                          * setting up the connection, in that case mac80211
464                          * will soon tell us about that.
465                          */
466                         need_multiple = false;
467                 }
468                 rcu_read_unlock();
469                 break;
470         case NL80211_IFTYPE_ADHOC:
471                 /* currently */
472                 need_multiple = false;
473                 break;
474         default:
475                 /* only AP really */
476                 need_multiple = true;
477                 break;
478         }
479
480         ctx->ht_need_multiple_chains = need_multiple;
481
482         if (!need_multiple) {
483                 /* check all contexts */
484                 for_each_context(priv, tmp) {
485                         if (!tmp->vif)
486                                 continue;
487                         if (tmp->ht_need_multiple_chains) {
488                                 need_multiple = true;
489                                 break;
490                         }
491                 }
492         }
493
494         ht_conf->single_chain_sufficient = !need_multiple;
495 }
496
497 void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
498                              struct ieee80211_vif *vif,
499                              struct ieee80211_bss_conf *bss_conf,
500                              u32 changes)
501 {
502         struct iwl_priv *priv = hw->priv;
503         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
504         int ret;
505         bool force = false;
506
507         mutex_lock(&priv->mutex);
508
509         if (changes & BSS_CHANGED_BEACON_INT)
510                 force = true;
511
512         if (changes & BSS_CHANGED_QOS) {
513                 ctx->qos_data.qos_active = bss_conf->qos;
514                 iwlagn_update_qos(priv, ctx);
515         }
516
517         ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
518         if (vif->bss_conf.use_short_preamble)
519                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
520         else
521                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
522
523         if (changes & BSS_CHANGED_ASSOC) {
524                 if (bss_conf->assoc) {
525                         iwl_led_associate(priv);
526                         priv->timestamp = bss_conf->timestamp;
527                         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
528                 } else {
529                         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
530                         iwl_led_disassociate(priv);
531                 }
532         }
533
534         if (ctx->ht.enabled) {
535                 ctx->ht.protection = bss_conf->ht_operation_mode &
536                                         IEEE80211_HT_OP_MODE_PROTECTION;
537                 ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
538                                         IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
539                 iwlagn_check_needed_chains(priv, ctx, bss_conf);
540         }
541
542         if (priv->cfg->ops->hcmd->set_rxon_chain)
543                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
544
545         if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
546                 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
547         else
548                 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
549
550         if (bss_conf->use_cts_prot)
551                 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
552         else
553                 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
554
555         memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
556
557         if (vif->type == NL80211_IFTYPE_AP ||
558             vif->type == NL80211_IFTYPE_ADHOC) {
559                 if (vif->bss_conf.enable_beacon) {
560                         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
561                         priv->beacon_ctx = ctx;
562                 } else {
563                         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
564                         priv->beacon_ctx = NULL;
565                 }
566         }
567
568         if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
569                 iwlagn_commit_rxon(priv, ctx);
570
571         if (changes & BSS_CHANGED_IBSS) {
572                 ret = iwlagn_manage_ibss_station(priv, vif,
573                                                  bss_conf->ibss_joined);
574                 if (ret)
575                         IWL_ERR(priv, "failed to %s IBSS station %pM\n",
576                                 bss_conf->ibss_joined ? "add" : "remove",
577                                 bss_conf->bssid);
578         }
579
580         if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_ADHOC &&
581             priv->beacon_ctx) {
582                 if (iwlagn_update_beacon(priv, vif))
583                         IWL_ERR(priv, "Error sending IBSS beacon\n");
584         }
585
586         mutex_unlock(&priv->mutex);
587 }
588
589 void iwlagn_post_scan(struct iwl_priv *priv)
590 {
591         struct iwl_rxon_context *ctx;
592
593         /*
594          * Since setting the RXON may have been deferred while
595          * performing the scan, fire one off if needed
596          */
597         for_each_context(priv, ctx)
598                 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
599                         iwlagn_commit_rxon(priv, ctx);
600
601         if (priv->cfg->ops->hcmd->set_pan_params)
602                 priv->cfg->ops->hcmd->set_pan_params(priv);
603 }