blob: 02288779a71f480f4dc0122fd8cf55af2fef415f [file] [log] [blame]
Johannes Berg2295c662010-10-23 09:15:41 -07001/******************************************************************************
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
33static 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
51static 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
Johannes Bergbd50a8ab2010-10-23 09:15:42 -070075static 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
Johannes Berg2295c662010-10-23 09:15:41 -070087/**
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 */
95int 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) {
Johannes Bergbd50a8ab2010-10-23 09:15:42 -0700216 /*
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 }
Johannes Berg2295c662010-10-23 09:15:41 -0700229 }
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));
Johannes Bergbd50a8ab2010-10-23 09:15:42 -0700245
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");
Johannes Berg2295c662010-10-23 09:15:41 -0700250 }
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
Wey-Yi Guy35a6eb32010-11-10 09:56:43 -0800272static void iwlagn_update_qos(struct iwl_priv *priv,
273 struct iwl_rxon_context *ctx)
274{
275 int ret;
276
277 if (!ctx->is_active)
278 return;
279
280 ctx->qos_data.def_qos_parm.qos_flags = 0;
281
282 if (ctx->qos_data.qos_active)
283 ctx->qos_data.def_qos_parm.qos_flags |=
284 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
285
286 if (ctx->ht.enabled)
287 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
288
289 IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
290 ctx->qos_data.qos_active,
291 ctx->qos_data.def_qos_parm.qos_flags);
292
293 ret = iwl_send_cmd_pdu(priv, ctx->qos_cmd,
294 sizeof(struct iwl_qosparam_cmd),
295 &ctx->qos_data.def_qos_parm);
296 if (ret)
297 IWL_ERR(priv, "Failed to update QoS\n");
298}
299
Johannes Berg2295c662010-10-23 09:15:41 -0700300int iwlagn_mac_config(struct ieee80211_hw *hw, u32 changed)
301{
302 struct iwl_priv *priv = hw->priv;
303 struct iwl_rxon_context *ctx;
304 struct ieee80211_conf *conf = &hw->conf;
305 struct ieee80211_channel *channel = conf->channel;
306 const struct iwl_channel_info *ch_info;
307 int ret = 0;
Wey-Yi Guy35a6eb32010-11-10 09:56:43 -0800308 bool ht_changed[NUM_IWL_RXON_CTX] = {};
Johannes Berg2295c662010-10-23 09:15:41 -0700309
310 IWL_DEBUG_MAC80211(priv, "changed %#x", changed);
311
312 mutex_lock(&priv->mutex);
313
314 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
315 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
316 goto out;
317 }
318
319 if (!iwl_is_ready(priv)) {
320 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
321 goto out;
322 }
323
324 if (changed & (IEEE80211_CONF_CHANGE_SMPS |
325 IEEE80211_CONF_CHANGE_CHANNEL)) {
326 /* mac80211 uses static for non-HT which is what we want */
327 priv->current_ht_config.smps = conf->smps_mode;
328
329 /*
330 * Recalculate chain counts.
331 *
332 * If monitor mode is enabled then mac80211 will
333 * set up the SM PS mode to OFF if an HT channel is
334 * configured.
335 */
336 if (priv->cfg->ops->hcmd->set_rxon_chain)
337 for_each_context(priv, ctx)
338 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
339 }
340
341 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
342 unsigned long flags;
343
344 ch_info = iwl_get_channel_info(priv, channel->band,
345 channel->hw_value);
346 if (!is_channel_valid(ch_info)) {
347 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
348 ret = -EINVAL;
349 goto out;
350 }
351
352 spin_lock_irqsave(&priv->lock, flags);
353
354 for_each_context(priv, ctx) {
355 /* Configure HT40 channels */
Wey-Yi Guy35a6eb32010-11-10 09:56:43 -0800356 if (ctx->ht.enabled != conf_is_ht(conf)) {
357 ctx->ht.enabled = conf_is_ht(conf);
358 ht_changed[ctx->ctxid] = true;
359 }
360
Johannes Berg2295c662010-10-23 09:15:41 -0700361 if (ctx->ht.enabled) {
362 if (conf_is_ht40_minus(conf)) {
363 ctx->ht.extension_chan_offset =
364 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
365 ctx->ht.is_40mhz = true;
366 } else if (conf_is_ht40_plus(conf)) {
367 ctx->ht.extension_chan_offset =
368 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
369 ctx->ht.is_40mhz = true;
370 } else {
371 ctx->ht.extension_chan_offset =
372 IEEE80211_HT_PARAM_CHA_SEC_NONE;
373 ctx->ht.is_40mhz = false;
374 }
375 } else
376 ctx->ht.is_40mhz = false;
377
378 /*
379 * Default to no protection. Protection mode will
380 * later be set from BSS config in iwl_ht_conf
381 */
382 ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
383
384 /* if we are switching from ht to 2.4 clear flags
385 * from any ht related info since 2.4 does not
386 * support ht */
387 if (le16_to_cpu(ctx->staging.channel) !=
388 channel->hw_value)
389 ctx->staging.flags = 0;
390
391 iwl_set_rxon_channel(priv, channel, ctx);
392 iwl_set_rxon_ht(priv, &priv->current_ht_config);
393
394 iwl_set_flags_for_band(priv, ctx, channel->band,
395 ctx->vif);
396 }
397
398 spin_unlock_irqrestore(&priv->lock, flags);
399
400 iwl_update_bcast_stations(priv);
401
402 /*
403 * The list of supported rates and rate mask can be different
404 * for each band; since the band may have changed, reset
405 * the rate mask to what mac80211 lists.
406 */
407 iwl_set_rate(priv);
408 }
409
410 if (changed & (IEEE80211_CONF_CHANGE_PS |
411 IEEE80211_CONF_CHANGE_IDLE)) {
412 ret = iwl_power_update_mode(priv, false);
413 if (ret)
414 IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
415 }
416
417 if (changed & IEEE80211_CONF_CHANGE_POWER) {
418 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
419 priv->tx_power_user_lmt, conf->power_level);
420
421 iwl_set_tx_power(priv, conf->power_level, false);
422 }
423
424 for_each_context(priv, ctx) {
425 if (!memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
426 continue;
427 iwlagn_commit_rxon(priv, ctx);
Wey-Yi Guy35a6eb32010-11-10 09:56:43 -0800428 if (ht_changed[ctx->ctxid])
429 iwlagn_update_qos(priv, ctx);
Johannes Berg2295c662010-10-23 09:15:41 -0700430 }
431 out:
432 mutex_unlock(&priv->mutex);
433 return ret;
434}
435
Johannes Berg2295c662010-10-23 09:15:41 -0700436static void iwlagn_check_needed_chains(struct iwl_priv *priv,
437 struct iwl_rxon_context *ctx,
438 struct ieee80211_bss_conf *bss_conf)
439{
440 struct ieee80211_vif *vif = ctx->vif;
441 struct iwl_rxon_context *tmp;
442 struct ieee80211_sta *sta;
443 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
444 bool need_multiple;
445
446 lockdep_assert_held(&priv->mutex);
447
448 switch (vif->type) {
449 case NL80211_IFTYPE_STATION:
450 rcu_read_lock();
451 sta = ieee80211_find_sta(vif, bss_conf->bssid);
452 if (sta) {
453 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
454 int maxstreams;
455
456 maxstreams = (ht_cap->mcs.tx_params &
457 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
458 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
459 maxstreams += 1;
460
461 if ((ht_cap->mcs.rx_mask[1] == 0) &&
462 (ht_cap->mcs.rx_mask[2] == 0))
463 need_multiple = false;
464 if (maxstreams <= 1)
465 need_multiple = true;
466 } else {
467 /*
468 * If at all, this can only happen through a race
469 * when the AP disconnects us while we're still
470 * setting up the connection, in that case mac80211
471 * will soon tell us about that.
472 */
473 need_multiple = false;
474 }
475 rcu_read_unlock();
476 break;
477 case NL80211_IFTYPE_ADHOC:
478 /* currently */
479 need_multiple = false;
480 break;
481 default:
482 /* only AP really */
483 need_multiple = true;
484 break;
485 }
486
487 ctx->ht_need_multiple_chains = need_multiple;
488
489 if (!need_multiple) {
490 /* check all contexts */
491 for_each_context(priv, tmp) {
492 if (!tmp->vif)
493 continue;
494 if (tmp->ht_need_multiple_chains) {
495 need_multiple = true;
496 break;
497 }
498 }
499 }
500
501 ht_conf->single_chain_sufficient = !need_multiple;
502}
503
504void iwlagn_bss_info_changed(struct ieee80211_hw *hw,
505 struct ieee80211_vif *vif,
506 struct ieee80211_bss_conf *bss_conf,
507 u32 changes)
508{
509 struct iwl_priv *priv = hw->priv;
510 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
511 int ret;
512 bool force = false;
513
514 mutex_lock(&priv->mutex);
515
516 if (changes & BSS_CHANGED_BEACON_INT)
517 force = true;
518
519 if (changes & BSS_CHANGED_QOS) {
520 ctx->qos_data.qos_active = bss_conf->qos;
521 iwlagn_update_qos(priv, ctx);
522 }
523
524 ctx->staging.assoc_id = cpu_to_le16(vif->bss_conf.aid);
525 if (vif->bss_conf.use_short_preamble)
526 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
527 else
528 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
529
530 if (changes & BSS_CHANGED_ASSOC) {
531 if (bss_conf->assoc) {
532 iwl_led_associate(priv);
533 priv->timestamp = bss_conf->timestamp;
534 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
535 } else {
536 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
537 iwl_led_disassociate(priv);
538 }
539 }
540
541 if (ctx->ht.enabled) {
542 ctx->ht.protection = bss_conf->ht_operation_mode &
543 IEEE80211_HT_OP_MODE_PROTECTION;
544 ctx->ht.non_gf_sta_present = !!(bss_conf->ht_operation_mode &
545 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
546 iwlagn_check_needed_chains(priv, ctx, bss_conf);
547 }
548
549 if (priv->cfg->ops->hcmd->set_rxon_chain)
550 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
551
552 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
553 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
554 else
555 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
556
557 if (bss_conf->use_cts_prot)
558 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
559 else
560 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
561
562 memcpy(ctx->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
563
564 if (vif->type == NL80211_IFTYPE_AP ||
565 vif->type == NL80211_IFTYPE_ADHOC) {
566 if (vif->bss_conf.enable_beacon) {
567 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
568 priv->beacon_ctx = ctx;
569 } else {
570 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
571 priv->beacon_ctx = NULL;
572 }
573 }
574
575 if (force || memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
576 iwlagn_commit_rxon(priv, ctx);
577
578 if (changes & BSS_CHANGED_IBSS) {
579 ret = iwlagn_manage_ibss_station(priv, vif,
580 bss_conf->ibss_joined);
581 if (ret)
582 IWL_ERR(priv, "failed to %s IBSS station %pM\n",
583 bss_conf->ibss_joined ? "add" : "remove",
584 bss_conf->bssid);
585 }
586
Johannes Bergbd50a8ab2010-10-23 09:15:42 -0700587 if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_ADHOC &&
588 priv->beacon_ctx) {
589 if (iwlagn_update_beacon(priv, vif))
590 IWL_ERR(priv, "Error sending IBSS beacon\n");
591 }
592
Johannes Berg2295c662010-10-23 09:15:41 -0700593 mutex_unlock(&priv->mutex);
594}
Johannes Bergae79d232010-11-10 09:56:38 -0800595
596void iwlagn_post_scan(struct iwl_priv *priv)
597{
598 struct iwl_rxon_context *ctx;
599
600 /*
601 * Since setting the RXON may have been deferred while
602 * performing the scan, fire one off if needed
603 */
604 for_each_context(priv, ctx)
605 if (memcmp(&ctx->staging, &ctx->active, sizeof(ctx->staging)))
606 iwlagn_commit_rxon(priv, ctx);
607
608 if (priv->cfg->ops->hcmd->set_pan_params)
609 priv->cfg->ops->hcmd->set_pan_params(priv);
610}