iwlagn: move traffic_log back to upper layer
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-agn-lib.c
CommitLineData
e04ed0a5
WYG
1/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
901069c7 5 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
e04ed0a5
WYG
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
8d801080 29#include <linux/etherdevice.h>
e04ed0a5
WYG
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/sched.h>
34
35#include "iwl-dev.h"
36#include "iwl-core.h"
37#include "iwl-io.h"
38#include "iwl-helpers.h"
39#include "iwl-agn-hw.h"
40#include "iwl-agn.h"
1fa61b2e 41#include "iwl-sta.h"
bdfbf092 42#include "iwl-trans.h"
48f20d35 43#include "iwl-shared.h"
e04ed0a5 44
e04ed0a5
WYG
45int iwlagn_hw_valid_rtc_data_addr(u32 addr)
46{
47 return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
48 (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
49}
50
51int iwlagn_send_tx_power(struct iwl_priv *priv)
52{
ab63c68a 53 struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
e04ed0a5
WYG
54 u8 tx_ant_cfg_cmd;
55
63013ae3 56 if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->shrd->status),
4beeba7d
SG
57 "TX Power requested while scanning!\n"))
58 return -EAGAIN;
59
e04ed0a5
WYG
60 /* half dBm need to multiply */
61 tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
62
63 if (priv->tx_power_lmt_in_half_dbm &&
64 priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
65 /*
66 * For the newer devices which using enhanced/extend tx power
67 * table in EEPROM, the format is in half dBm. driver need to
68 * convert to dBm format before report to mac80211.
69 * By doing so, there is a possibility of 1/2 dBm resolution
70 * lost. driver will perform "round-up" operation before
71 * reporting, but it will cause 1/2 dBm tx power over the
72 * regulatory limit. Perform the checking here, if the
73 * "tx_power_user_lmt" is higher than EEPROM value (in
74 * half-dBm format), lower the tx power based on EEPROM
75 */
76 tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
77 }
ab63c68a
WYG
78 tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
79 tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
e04ed0a5
WYG
80
81 if (IWL_UCODE_API(priv->ucode_ver) == 1)
82 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
83 else
84 tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
85
e6bb4c9c 86 return iwl_trans_send_cmd_pdu(trans(priv), tx_ant_cfg_cmd, CMD_SYNC,
e419d62d 87 sizeof(tx_power_cmd), &tx_power_cmd);
e04ed0a5
WYG
88}
89
90void iwlagn_temperature(struct iwl_priv *priv)
91{
f8f79a5d 92 /* store temperature from correct statistics (in Celsius) */
0da0e5bf 93 priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
e04ed0a5
WYG
94 iwl_tt_handler(priv);
95}
96
97u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
98{
99 struct iwl_eeprom_calib_hdr {
100 u8 version;
101 u8 pa_type;
102 u16 voltage;
103 } *hdr;
104
105 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
7944f8e4 106 EEPROM_CALIB_ALL);
e04ed0a5
WYG
107 return hdr->version;
108
109}
110
111/*
112 * EEPROM
113 */
114static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
115{
116 u16 offset = 0;
117
118 if ((address & INDIRECT_ADDRESS) == 0)
119 return address;
120
121 switch (address & INDIRECT_TYPE_MSK) {
122 case INDIRECT_HOST:
7944f8e4 123 offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
e04ed0a5
WYG
124 break;
125 case INDIRECT_GENERAL:
7944f8e4 126 offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
e04ed0a5
WYG
127 break;
128 case INDIRECT_REGULATORY:
7944f8e4 129 offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
e04ed0a5 130 break;
8d6748ca
JB
131 case INDIRECT_TXP_LIMIT:
132 offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
133 break;
134 case INDIRECT_TXP_LIMIT_SIZE:
135 offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
136 break;
e04ed0a5 137 case INDIRECT_CALIBRATION:
7944f8e4 138 offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
e04ed0a5
WYG
139 break;
140 case INDIRECT_PROCESS_ADJST:
7944f8e4 141 offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
e04ed0a5
WYG
142 break;
143 case INDIRECT_OTHERS:
7944f8e4 144 offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
e04ed0a5
WYG
145 break;
146 default:
147 IWL_ERR(priv, "illegal indirect type: 0x%X\n",
148 address & INDIRECT_TYPE_MSK);
149 break;
150 }
151
152 /* translate the offset from words to byte */
153 return (address & ADDRESS_MSK) + (offset << 1);
154}
155
70e3e8a6 156const u8 *iwl_eeprom_query_addr(const struct iwl_priv *priv, size_t offset)
e04ed0a5
WYG
157{
158 u32 address = eeprom_indirect_address(priv, offset);
7cb1b088 159 BUG_ON(address >= priv->cfg->base_params->eeprom_size);
e04ed0a5
WYG
160 return &priv->eeprom[address];
161}
348ee7cd
WYG
162
163struct iwl_mod_params iwlagn_mod_params = {
164 .amsdu_size_8K = 1,
165 .restart_fw = 1,
b7977ffa 166 .plcp_check = true,
b60eec9b 167 .bt_coex_active = true,
3f1e5f4a 168 .no_sleep_autoadjust = true,
f7538168 169 .power_level = IWL_POWER_INDEX_1,
fee84f0d 170 .bt_ch_announce = true,
48f20d35 171 .wanted_ucode_alternative = 1,
dd5b6d0a 172 .auto_agg = true,
348ee7cd
WYG
173 /* the rest are 0 by default */
174};
74bcdb33 175
8d801080
WYG
176int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
177{
178 int idx = 0;
179 int band_offset = 0;
180
181 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
182 if (rate_n_flags & RATE_MCS_HT_MSK) {
183 idx = (rate_n_flags & 0xff);
184 return idx;
185 /* Legacy rate format, search for match in table */
186 } else {
187 if (band == IEEE80211_BAND_5GHZ)
188 band_offset = IWL_FIRST_OFDM_RATE;
189 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
190 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
191 return idx - band_offset;
192 }
193
194 return -1;
195}
196
b6e4c55a 197static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
1dda6d28
JB
198 struct ieee80211_vif *vif,
199 enum ieee80211_band band,
200 struct iwl_scan_channel *scan_ch)
b6e4c55a
JB
201{
202 const struct ieee80211_supported_band *sband;
b6e4c55a
JB
203 u16 passive_dwell = 0;
204 u16 active_dwell = 0;
14023641 205 int added = 0;
b6e4c55a
JB
206 u16 channel = 0;
207
208 sband = iwl_get_hw_mode(priv, band);
209 if (!sband) {
210 IWL_ERR(priv, "invalid band\n");
211 return added;
212 }
213
214 active_dwell = iwl_get_active_dwell_time(priv, band, 0);
1dda6d28 215 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
b6e4c55a
JB
216
217 if (passive_dwell <= active_dwell)
218 passive_dwell = active_dwell + 1;
219
14023641 220 channel = iwl_get_single_channel_number(priv, band);
b6e4c55a
JB
221 if (channel) {
222 scan_ch->channel = cpu_to_le16(channel);
223 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
224 scan_ch->active_dwell = cpu_to_le16(active_dwell);
225 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
226 /* Set txpower levels to defaults */
227 scan_ch->dsp_atten = 110;
228 if (band == IEEE80211_BAND_5GHZ)
229 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
230 else
231 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
232 added++;
233 } else
234 IWL_ERR(priv, "no valid channel found\n");
235 return added;
236}
237
238static int iwl_get_channels_for_scan(struct iwl_priv *priv,
1dda6d28 239 struct ieee80211_vif *vif,
b6e4c55a
JB
240 enum ieee80211_band band,
241 u8 is_active, u8 n_probes,
242 struct iwl_scan_channel *scan_ch)
243{
244 struct ieee80211_channel *chan;
245 const struct ieee80211_supported_band *sband;
246 const struct iwl_channel_info *ch_info;
247 u16 passive_dwell = 0;
248 u16 active_dwell = 0;
249 int added, i;
250 u16 channel;
251
252 sband = iwl_get_hw_mode(priv, band);
253 if (!sband)
254 return 0;
255
256 active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
1dda6d28 257 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
b6e4c55a
JB
258
259 if (passive_dwell <= active_dwell)
260 passive_dwell = active_dwell + 1;
261
262 for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
263 chan = priv->scan_request->channels[i];
264
265 if (chan->band != band)
266 continue;
267
81e95430 268 channel = chan->hw_value;
b6e4c55a
JB
269 scan_ch->channel = cpu_to_le16(channel);
270
271 ch_info = iwl_get_channel_info(priv, band, channel);
272 if (!is_channel_valid(ch_info)) {
273 IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
274 channel);
275 continue;
276 }
277
278 if (!is_active || is_channel_passive(ch_info) ||
279 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
280 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
281 else
282 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
283
284 if (n_probes)
285 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
286
287 scan_ch->active_dwell = cpu_to_le16(active_dwell);
288 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
289
290 /* Set txpower levels to defaults */
291 scan_ch->dsp_atten = 110;
292
293 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
294 * power level:
295 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
296 */
297 if (band == IEEE80211_BAND_5GHZ)
298 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
299 else
300 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
301
302 IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
303 channel, le32_to_cpu(scan_ch->type),
304 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
305 "ACTIVE" : "PASSIVE",
306 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
307 active_dwell : passive_dwell);
308
309 scan_ch++;
310 added++;
311 }
312
313 IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
314 return added;
315}
316
3eecce52 317int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
b6e4c55a
JB
318{
319 struct iwl_host_cmd cmd = {
320 .id = REPLY_SCAN_CMD,
3fa50738 321 .len = { sizeof(struct iwl_scan_cmd), },
e419d62d 322 .flags = CMD_SYNC,
b6e4c55a
JB
323 };
324 struct iwl_scan_cmd *scan;
a194e324 325 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
b6e4c55a
JB
326 u32 rate_flags = 0;
327 u16 cmd_len;
328 u16 rx_chain = 0;
329 enum ieee80211_band band;
330 u8 n_probes = 0;
d6189124 331 u8 rx_ant = hw_params(priv).valid_rx_ant;
b6e4c55a
JB
332 u8 rate;
333 bool is_active = false;
334 int chan_mod;
335 u8 active_chains;
d6189124 336 u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
3eecce52
JB
337 int ret;
338
6ac2f839 339 lockdep_assert_held(&priv->shrd->mutex);
b6e4c55a 340
a194e324
JB
341 if (vif)
342 ctx = iwl_rxon_ctx_from_vif(vif);
343
b6e4c55a
JB
344 if (!priv->scan_cmd) {
345 priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
346 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
347 if (!priv->scan_cmd) {
348 IWL_DEBUG_SCAN(priv,
349 "fail to allocate memory for scan\n");
3eecce52 350 return -ENOMEM;
b6e4c55a
JB
351 }
352 }
353 scan = priv->scan_cmd;
354 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
355
356 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
357 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
358
c6baf7fb 359 if (priv->scan_type != IWL_SCAN_ROC &&
266af4c7 360 iwl_is_any_associated(priv)) {
b6e4c55a
JB
361 u16 interval = 0;
362 u32 extra;
363 u32 suspend_time = 100;
364 u32 scan_suspend_time = 100;
b6e4c55a
JB
365
366 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
266af4c7 367 switch (priv->scan_type) {
c6baf7fb 368 case IWL_SCAN_ROC:
266af4c7
JB
369 WARN_ON(1);
370 break;
371 case IWL_SCAN_RADIO_RESET:
a6e492b9 372 interval = 0;
266af4c7
JB
373 break;
374 case IWL_SCAN_NORMAL:
a6e492b9 375 interval = vif->bss_conf.beacon_int;
266af4c7
JB
376 break;
377 }
b6e4c55a
JB
378
379 scan->suspend_time = 0;
380 scan->max_out_time = cpu_to_le32(200 * 1024);
381 if (!interval)
382 interval = suspend_time;
383
384 extra = (suspend_time / interval) << 22;
385 scan_suspend_time = (extra |
386 ((suspend_time % interval) * 1024));
387 scan->suspend_time = cpu_to_le32(scan_suspend_time);
388 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
389 scan_suspend_time, interval);
c6baf7fb 390 } else if (priv->scan_type == IWL_SCAN_ROC) {
266af4c7 391 scan->suspend_time = 0;
c6baf7fb
JB
392 scan->max_out_time = 0;
393 scan->quiet_time = 0;
394 scan->quiet_plcp_th = 0;
b6e4c55a
JB
395 }
396
266af4c7
JB
397 switch (priv->scan_type) {
398 case IWL_SCAN_RADIO_RESET:
b6e4c55a 399 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
266af4c7
JB
400 break;
401 case IWL_SCAN_NORMAL:
402 if (priv->scan_request->n_ssids) {
403 int i, p = 0;
404 IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
405 for (i = 0; i < priv->scan_request->n_ssids; i++) {
406 /* always does wildcard anyway */
407 if (!priv->scan_request->ssids[i].ssid_len)
408 continue;
409 scan->direct_scan[p].id = WLAN_EID_SSID;
410 scan->direct_scan[p].len =
411 priv->scan_request->ssids[i].ssid_len;
412 memcpy(scan->direct_scan[p].ssid,
413 priv->scan_request->ssids[i].ssid,
414 priv->scan_request->ssids[i].ssid_len);
415 n_probes++;
416 p++;
417 }
418 is_active = true;
419 } else
420 IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
421 break;
c6baf7fb
JB
422 case IWL_SCAN_ROC:
423 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
266af4c7
JB
424 break;
425 }
b6e4c55a
JB
426
427 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
a194e324 428 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
b6e4c55a
JB
429 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
430
431 switch (priv->scan_band) {
432 case IEEE80211_BAND_2GHZ:
433 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
246ed355
JB
434 chan_mod = le32_to_cpu(
435 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
436 RXON_FLG_CHANNEL_MODE_MSK)
b6e4c55a
JB
437 >> RXON_FLG_CHANNEL_MODE_POS;
438 if (chan_mod == CHANNEL_MODE_PURE_40) {
439 rate = IWL_RATE_6M_PLCP;
440 } else {
441 rate = IWL_RATE_1M_PLCP;
442 rate_flags = RATE_MCS_CCK_MSK;
443 }
d44ae69e
JB
444 /*
445 * Internal scans are passive, so we can indiscriminately set
446 * the BT ignore flag on 2.4 GHz since it applies to TX only.
447 */
7cb1b088
WYG
448 if (priv->cfg->bt_params &&
449 priv->cfg->bt_params->advanced_bt_coexist)
d44ae69e 450 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
b6e4c55a
JB
451 break;
452 case IEEE80211_BAND_5GHZ:
453 rate = IWL_RATE_6M_PLCP;
b6e4c55a
JB
454 break;
455 default:
3eecce52
JB
456 IWL_WARN(priv, "Invalid scan band\n");
457 return -EIO;
b6e4c55a
JB
458 }
459
085fbca2
JB
460 /*
461 * If active scanning is requested but a certain channel is
462 * marked passive, we can do active scanning if we detect
463 * transmissions.
464 *
465 * There is an issue with some firmware versions that triggers
466 * a sysassert on a "good CRC threshold" of zero (== disabled),
467 * on a radar channel even though this means that we should NOT
468 * send probes.
469 *
470 * The "good CRC threshold" is the number of frames that we
471 * need to receive during our dwell time on a channel before
472 * sending out probes -- setting this to a huge value will
473 * mean we never reach it, but at the same time work around
474 * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
475 * here instead of IWL_GOOD_CRC_TH_DISABLED.
d2690c0d
JB
476 *
477 * This was fixed in later versions along with some other
478 * scan changes, and the threshold behaves as a flag in those
479 * versions.
085fbca2 480 */
d2690c0d
JB
481 if (priv->new_scan_threshold_behaviour)
482 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
483 IWL_GOOD_CRC_TH_DISABLED;
484 else
485 scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
486 IWL_GOOD_CRC_TH_NEVER;
085fbca2 487
b6e4c55a
JB
488 band = priv->scan_band;
489
0e1654fa
JB
490 if (priv->cfg->scan_rx_antennas[band])
491 rx_ant = priv->cfg->scan_rx_antennas[band];
e7cb4955 492
cd017f25
SG
493 if (band == IEEE80211_BAND_2GHZ &&
494 priv->cfg->bt_params &&
495 priv->cfg->bt_params->advanced_bt_coexist) {
496 /* transmit 2.4 GHz probes only on first antenna */
497 scan_tx_antennas = first_antenna(scan_tx_antennas);
bee008b7
WYG
498 }
499
0e1654fa
JB
500 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
501 scan_tx_antennas);
b6e4c55a
JB
502 rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
503 scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
504
505 /* In power save mode use one chain, otherwise use all chains */
63013ae3 506 if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
b6e4c55a
JB
507 /* rx_ant has been set to all valid chains previously */
508 active_chains = rx_ant &
509 ((u8)(priv->chain_noise_data.active_chains));
510 if (!active_chains)
511 active_chains = rx_ant;
512
513 IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
514 priv->chain_noise_data.active_chains);
515
516 rx_ant = first_antenna(active_chains);
517 }
7cb1b088
WYG
518 if (priv->cfg->bt_params &&
519 priv->cfg->bt_params->advanced_bt_coexist &&
520 priv->bt_full_concurrent) {
bee008b7
WYG
521 /* operated as 1x1 in full concurrency mode */
522 rx_ant = first_antenna(rx_ant);
523 }
524
b6e4c55a 525 /* MIMO is not used here, but value is required */
d6189124
EG
526 rx_chain |=
527 hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
b6e4c55a
JB
528 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
529 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
530 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
531 scan->rx_chain = cpu_to_le16(rx_chain);
266af4c7
JB
532 switch (priv->scan_type) {
533 case IWL_SCAN_NORMAL:
b6e4c55a
JB
534 cmd_len = iwl_fill_probe_req(priv,
535 (struct ieee80211_mgmt *)scan->data,
3a0b9aad 536 vif->addr,
b6e4c55a
JB
537 priv->scan_request->ie,
538 priv->scan_request->ie_len,
539 IWL_MAX_SCAN_SIZE - sizeof(*scan));
266af4c7
JB
540 break;
541 case IWL_SCAN_RADIO_RESET:
c6baf7fb 542 case IWL_SCAN_ROC:
3a0b9aad 543 /* use bcast addr, will not be transmitted but must be valid */
b6e4c55a
JB
544 cmd_len = iwl_fill_probe_req(priv,
545 (struct ieee80211_mgmt *)scan->data,
3a0b9aad 546 iwl_bcast_addr, NULL, 0,
b6e4c55a 547 IWL_MAX_SCAN_SIZE - sizeof(*scan));
266af4c7 548 break;
266af4c7
JB
549 default:
550 BUG();
b6e4c55a
JB
551 }
552 scan->tx_cmd.len = cpu_to_le16(cmd_len);
b6e4c55a
JB
553
554 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
555 RXON_FILTER_BCON_AWARE_MSK);
556
266af4c7
JB
557 switch (priv->scan_type) {
558 case IWL_SCAN_RADIO_RESET:
b6e4c55a 559 scan->channel_count =
1dda6d28 560 iwl_get_single_channel_for_scan(priv, vif, band,
266af4c7
JB
561 (void *)&scan->data[cmd_len]);
562 break;
563 case IWL_SCAN_NORMAL:
b6e4c55a 564 scan->channel_count =
1dda6d28 565 iwl_get_channels_for_scan(priv, vif, band,
b6e4c55a 566 is_active, n_probes,
266af4c7
JB
567 (void *)&scan->data[cmd_len]);
568 break;
c6baf7fb 569 case IWL_SCAN_ROC: {
266af4c7
JB
570 struct iwl_scan_channel *scan_ch;
571
572 scan->channel_count = 1;
573
574 scan_ch = (void *)&scan->data[cmd_len];
c6baf7fb 575 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
266af4c7 576 scan_ch->channel =
c6baf7fb 577 cpu_to_le16(priv->hw_roc_channel->hw_value);
266af4c7 578 scan_ch->active_dwell =
c6baf7fb
JB
579 scan_ch->passive_dwell =
580 cpu_to_le16(priv->hw_roc_duration);
266af4c7
JB
581
582 /* Set txpower levels to defaults */
583 scan_ch->dsp_atten = 110;
584
585 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
586 * power level:
587 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
588 */
c6baf7fb 589 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
266af4c7
JB
590 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
591 else
592 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
593 }
594 break;
b6e4c55a 595 }
266af4c7 596
b6e4c55a
JB
597 if (scan->channel_count == 0) {
598 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
3eecce52 599 return -EIO;
b6e4c55a
JB
600 }
601
3fa50738 602 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
b6e4c55a 603 scan->channel_count * sizeof(struct iwl_scan_channel);
3fa50738 604 cmd.data[0] = scan;
4ce7cc2b 605 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
3fa50738 606 scan->len = cpu_to_le16(cmd.len[0]);
b6e4c55a 607
1cf26373 608 /* set scan bit here for PAN params */
63013ae3 609 set_bit(STATUS_SCAN_HW, &priv->shrd->status);
1cf26373 610
e3f10cea
WYG
611 ret = iwlagn_set_pan_params(priv);
612 if (ret)
613 return ret;
b6e4c55a 614
e6bb4c9c 615 ret = iwl_trans_send_cmd(trans(priv), &cmd);
3eecce52 616 if (ret) {
63013ae3 617 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
e3f10cea 618 iwlagn_set_pan_params(priv);
3eecce52 619 }
b6e4c55a 620
3eecce52 621 return ret;
b6e4c55a 622}
1fa61b2e
JB
623
624int iwlagn_manage_ibss_station(struct iwl_priv *priv,
625 struct ieee80211_vif *vif, bool add)
626{
fd1af15d
JB
627 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
628
1fa61b2e 629 if (add)
a30e3112
JB
630 return iwlagn_add_bssid_station(priv, vif_priv->ctx,
631 vif->bss_conf.bssid,
632 &vif_priv->ibss_bssid_sta_id);
fd1af15d
JB
633 return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
634 vif->bss_conf.bssid);
1fa61b2e 635}
1ff504e0 636
716c74b0
WYG
637/**
638 * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
639 *
640 * pre-requirements:
641 * 1. acquire mutex before calling
642 * 2. make sure rf is on and not in exit state
643 */
644int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
645{
646 struct iwl_txfifo_flush_cmd flush_cmd;
647 struct iwl_host_cmd cmd = {
648 .id = REPLY_TXFIFO_FLUSH,
3fa50738 649 .len = { sizeof(struct iwl_txfifo_flush_cmd), },
716c74b0 650 .flags = CMD_SYNC,
3fa50738 651 .data = { &flush_cmd, },
716c74b0
WYG
652 };
653
654 might_sleep();
655
656 memset(&flush_cmd, 0, sizeof(flush_cmd));
ecdbe86e
WYG
657 if (flush_control & BIT(IWL_RXON_CTX_BSS))
658 flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
f88e0ecc
WYG
659 IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
660 IWL_SCD_MGMT_MSK;
ecdbe86e
WYG
661 if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
662 (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
f88e0ecc
WYG
663 flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
664 IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
665 IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
666 IWL_PAN_SCD_MULTICAST_MSK;
667
88950758 668 if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
716c74b0
WYG
669 flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
670
671 IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
672 flush_cmd.fifo_control);
673 flush_cmd.flush_control = cpu_to_le16(flush_control);
674
e6bb4c9c 675 return iwl_trans_send_cmd(trans(priv), &cmd);
716c74b0 676}
65550636
WYG
677
678void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
679{
6ac2f839 680 mutex_lock(&priv->shrd->mutex);
65550636 681 ieee80211_stop_queues(priv->hw);
c68744fb 682 if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
65550636
WYG
683 IWL_ERR(priv, "flush request fail\n");
684 goto done;
685 }
686 IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
5f178cd2 687 iwl_trans_wait_tx_queue_empty(trans(priv));
65550636
WYG
688done:
689 ieee80211_wake_queues(priv->hw);
6ac2f839 690 mutex_unlock(&priv->shrd->mutex);
65550636 691}
b6e116e8
WYG
692
693/*
694 * BT coex
695 */
696/*
697 * Macros to access the lookup table.
698 *
699 * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
700* wifi_prio, wifi_txrx and wifi_sh_ant_req.
701 *
702 * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
703 *
704 * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
705 * one after another in 32-bit registers, and "registers" 0 through 7 contain
706 * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
707 *
708 * These macros encode that format.
709 */
710#define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
711 wifi_txrx, wifi_sh_ant_req) \
712 (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
713 (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
714
715#define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
716 lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
717#define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
718 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
719 (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
720 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
721 wifi_sh_ant_req))))
722#define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
723 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
724 LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
725 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
726 wifi_sh_ant_req))
727#define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
728 wifi_req, wifi_prio, wifi_txrx, \
729 wifi_sh_ant_req) \
730 LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
731 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
732 wifi_sh_ant_req))
733
734#define LUT_WLAN_KILL_OP(lut, op, val) \
735 lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
736#define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
737 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
738 (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
739 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
740#define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
741 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
742 LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
743 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
744#define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
745 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
746 LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
747 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
748
749#define LUT_ANT_SWITCH_OP(lut, op, val) \
750 lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
751#define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
752 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
753 (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
754 wifi_req, wifi_prio, wifi_txrx, \
755 wifi_sh_ant_req))))
756#define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
757 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
758 LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
759 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
760#define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
761 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
762 LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
763 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
764
765static const __le32 iwlagn_def_3w_lookup[12] = {
766 cpu_to_le32(0xaaaaaaaa),
767 cpu_to_le32(0xaaaaaaaa),
768 cpu_to_le32(0xaeaaaaaa),
769 cpu_to_le32(0xaaaaaaaa),
770 cpu_to_le32(0xcc00ff28),
771 cpu_to_le32(0x0000aaaa),
772 cpu_to_le32(0xcc00aaaa),
773 cpu_to_le32(0x0000aaaa),
774 cpu_to_le32(0xc0004000),
775 cpu_to_le32(0x00004000),
776 cpu_to_le32(0xf0005000),
9a67d761 777 cpu_to_le32(0xf0005000),
b6e116e8
WYG
778};
779
780static const __le32 iwlagn_concurrent_lookup[12] = {
781 cpu_to_le32(0xaaaaaaaa),
782 cpu_to_le32(0xaaaaaaaa),
783 cpu_to_le32(0xaaaaaaaa),
784 cpu_to_le32(0xaaaaaaaa),
785 cpu_to_le32(0xaaaaaaaa),
786 cpu_to_le32(0xaaaaaaaa),
787 cpu_to_le32(0xaaaaaaaa),
788 cpu_to_le32(0xaaaaaaaa),
789 cpu_to_le32(0x00000000),
790 cpu_to_le32(0x00000000),
791 cpu_to_le32(0x00000000),
792 cpu_to_le32(0x00000000),
793};
794
795void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
796{
6013270a 797 struct iwl_basic_bt_cmd basic = {
b6e116e8
WYG
798 .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
799 .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
800 .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
801 .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
802 };
6013270a
WYG
803 struct iwl6000_bt_cmd bt_cmd_6000;
804 struct iwl2000_bt_cmd bt_cmd_2000;
805 int ret;
b6e116e8
WYG
806
807 BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
6013270a
WYG
808 sizeof(basic.bt3_lookup_table));
809
810 if (priv->cfg->bt_params) {
811 if (priv->cfg->bt_params->bt_session_2) {
812 bt_cmd_2000.prio_boost = cpu_to_le32(
813 priv->cfg->bt_params->bt_prio_boost);
814 bt_cmd_2000.tx_prio_boost = 0;
815 bt_cmd_2000.rx_prio_boost = 0;
816 } else {
817 bt_cmd_6000.prio_boost =
818 priv->cfg->bt_params->bt_prio_boost;
819 bt_cmd_6000.tx_prio_boost = 0;
820 bt_cmd_6000.rx_prio_boost = 0;
821 }
822 } else {
823 IWL_ERR(priv, "failed to construct BT Coex Config\n");
824 return;
825 }
506aa156 826
6013270a
WYG
827 basic.kill_ack_mask = priv->kill_ack_mask;
828 basic.kill_cts_mask = priv->kill_cts_mask;
829 basic.valid = priv->bt_valid;
b6e116e8
WYG
830
831 /*
832 * Configure BT coex mode to "no coexistence" when the
833 * user disabled BT coexistence, we have no interface
834 * (might be in monitor mode), or the interface is in
835 * IBSS mode (no proper uCode support for coex then).
836 */
b60eec9b
WYG
837 if (!iwlagn_mod_params.bt_coex_active ||
838 priv->iw_mode == NL80211_IFTYPE_ADHOC) {
6013270a 839 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
b6e116e8 840 } else {
6013270a 841 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
b6e116e8 842 IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
207ecc5e
MV
843
844 if (!priv->bt_enable_pspoll)
6013270a 845 basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
207ecc5e
MV
846 else
847 basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
e366176e 848
b6e116e8 849 if (priv->bt_ch_announce)
6013270a 850 basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
fa7f1413 851 IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
b6e116e8 852 }
6013270a 853 priv->bt_enable_flag = basic.flags;
b6e116e8 854 if (priv->bt_full_concurrent)
6013270a 855 memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
b6e116e8
WYG
856 sizeof(iwlagn_concurrent_lookup));
857 else
6013270a 858 memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
b6e116e8
WYG
859 sizeof(iwlagn_def_3w_lookup));
860
fa7f1413 861 IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
6013270a 862 basic.flags ? "active" : "disabled",
b6e116e8
WYG
863 priv->bt_full_concurrent ?
864 "full concurrency" : "3-wire");
865
6013270a
WYG
866 if (priv->cfg->bt_params->bt_session_2) {
867 memcpy(&bt_cmd_2000.basic, &basic,
868 sizeof(basic));
e6bb4c9c 869 ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
e419d62d 870 CMD_SYNC, sizeof(bt_cmd_2000), &bt_cmd_2000);
6013270a
WYG
871 } else {
872 memcpy(&bt_cmd_6000.basic, &basic,
873 sizeof(basic));
e6bb4c9c 874 ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
e419d62d 875 CMD_SYNC, sizeof(bt_cmd_6000), &bt_cmd_6000);
6013270a
WYG
876 }
877 if (ret)
b6e116e8
WYG
878 IWL_ERR(priv, "failed to send BT Coex Config\n");
879
b6e116e8
WYG
880}
881
207ecc5e
MV
882void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
883{
884 struct iwl_rxon_context *ctx, *found_ctx = NULL;
885 bool found_ap = false;
886
6ac2f839 887 lockdep_assert_held(&priv->shrd->mutex);
207ecc5e
MV
888
889 /* Check whether AP or GO mode is active. */
890 if (rssi_ena) {
891 for_each_context(priv, ctx) {
892 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
893 iwl_is_associated_ctx(ctx)) {
894 found_ap = true;
895 break;
896 }
897 }
898 }
899
900 /*
901 * If disable was received or If GO/AP mode, disable RSSI
902 * measurements.
903 */
904 if (!rssi_ena || found_ap) {
905 if (priv->cur_rssi_ctx) {
906 ctx = priv->cur_rssi_ctx;
907 ieee80211_disable_rssi_reports(ctx->vif);
908 priv->cur_rssi_ctx = NULL;
909 }
910 return;
911 }
912
913 /*
914 * If rssi measurements need to be enabled, consider all cases now.
915 * Figure out how many contexts are active.
916 */
917 for_each_context(priv, ctx) {
918 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
919 iwl_is_associated_ctx(ctx)) {
920 found_ctx = ctx;
921 break;
922 }
923 }
924
925 /*
926 * rssi monitor already enabled for the correct interface...nothing
927 * to do.
928 */
929 if (found_ctx == priv->cur_rssi_ctx)
930 return;
931
932 /*
933 * Figure out if rssi monitor is currently enabled, and needs
934 * to be changed. If rssi monitor is already enabled, disable
935 * it first else just enable rssi measurements on the
936 * interface found above.
937 */
938 if (priv->cur_rssi_ctx) {
939 ctx = priv->cur_rssi_ctx;
940 if (ctx->vif)
941 ieee80211_disable_rssi_reports(ctx->vif);
942 }
943
944 priv->cur_rssi_ctx = found_ctx;
945
946 if (!found_ctx)
947 return;
948
949 ieee80211_enable_rssi_reports(found_ctx->vif,
950 IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
951 IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
952}
953
954static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
955{
956 return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
957 BT_UART_MSG_FRAME3SCOESCO_POS;
958}
959
b6e116e8
WYG
960static void iwlagn_bt_traffic_change_work(struct work_struct *work)
961{
962 struct iwl_priv *priv =
963 container_of(work, struct iwl_priv, bt_traffic_change_work);
8bd413e6 964 struct iwl_rxon_context *ctx;
b6e116e8
WYG
965 int smps_request = -1;
966
c4197c62
WYG
967 if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
968 /* bt coex disabled */
969 return;
970 }
971
5eda74a4
SG
972 /*
973 * Note: bt_traffic_load can be overridden by scan complete and
974 * coex profile notifications. Ignore that since only bad consequence
975 * can be not matching debug print with actual state.
976 */
fa7f1413 977 IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
b6e116e8
WYG
978 priv->bt_traffic_load);
979
980 switch (priv->bt_traffic_load) {
981 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
f5682c01
WYG
982 if (priv->bt_status)
983 smps_request = IEEE80211_SMPS_DYNAMIC;
984 else
985 smps_request = IEEE80211_SMPS_AUTOMATIC;
b6e116e8
WYG
986 break;
987 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
988 smps_request = IEEE80211_SMPS_DYNAMIC;
989 break;
990 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
991 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
992 smps_request = IEEE80211_SMPS_STATIC;
993 break;
994 default:
995 IWL_ERR(priv, "Invalid BT traffic load: %d\n",
996 priv->bt_traffic_load);
997 break;
998 }
999
6ac2f839 1000 mutex_lock(&priv->shrd->mutex);
b6e116e8 1001
5eda74a4
SG
1002 /*
1003 * We can not send command to firmware while scanning. When the scan
1004 * complete we will schedule this work again. We do check with mutex
1005 * locked to prevent new scan request to arrive. We do not check
1006 * STATUS_SCANNING to avoid race when queue_work two times from
1007 * different notifications, but quit and not perform any work at all.
1008 */
63013ae3 1009 if (test_bit(STATUS_SCAN_HW, &priv->shrd->status))
5eda74a4
SG
1010 goto out;
1011
6b6db91c 1012 iwl_update_chain_flags(priv);
b6e116e8 1013
8bd413e6 1014 if (smps_request != -1) {
88e9ba76 1015 priv->current_ht_config.smps = smps_request;
8bd413e6
JB
1016 for_each_context(priv, ctx) {
1017 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
1018 ieee80211_request_smps(ctx->vif, smps_request);
1019 }
1020 }
207ecc5e
MV
1021
1022 /*
1023 * Dynamic PS poll related functionality. Adjust RSSI measurements if
1024 * necessary.
1025 */
1026 iwlagn_bt_coex_rssi_monitor(priv);
5eda74a4 1027out:
6ac2f839 1028 mutex_unlock(&priv->shrd->mutex);
b6e116e8
WYG
1029}
1030
207ecc5e
MV
1031/*
1032 * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
1033 * correct interface or disable it if this is the last interface to be
1034 * removed.
1035 */
1036void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
1037{
1038 if (priv->bt_is_sco &&
1039 priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
1040 iwlagn_bt_adjust_rssi_monitor(priv, true);
1041 else
1042 iwlagn_bt_adjust_rssi_monitor(priv, false);
1043}
1044
b6e116e8
WYG
1045static void iwlagn_print_uartmsg(struct iwl_priv *priv,
1046 struct iwl_bt_uart_msg *uart_msg)
1047{
fa7f1413 1048 IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
b6e116e8
WYG
1049 "Update Req = 0x%X",
1050 (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
1051 BT_UART_MSG_FRAME1MSGTYPE_POS,
1052 (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
1053 BT_UART_MSG_FRAME1SSN_POS,
1054 (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
1055 BT_UART_MSG_FRAME1UPDATEREQ_POS);
1056
fa7f1413 1057 IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
b6e116e8
WYG
1058 "Chl_SeqN = 0x%X, In band = 0x%X",
1059 (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
1060 BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
1061 (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
1062 BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
1063 (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
1064 BT_UART_MSG_FRAME2CHLSEQN_POS,
1065 (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
1066 BT_UART_MSG_FRAME2INBAND_POS);
1067
fa7f1413 1068 IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
b6e116e8
WYG
1069 "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
1070 (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
1071 BT_UART_MSG_FRAME3SCOESCO_POS,
1072 (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
1073 BT_UART_MSG_FRAME3SNIFF_POS,
1074 (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
1075 BT_UART_MSG_FRAME3A2DP_POS,
1076 (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
1077 BT_UART_MSG_FRAME3ACL_POS,
1078 (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
1079 BT_UART_MSG_FRAME3MASTER_POS,
1080 (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
1081 BT_UART_MSG_FRAME3OBEX_POS);
1082
fa7f1413 1083 IWL_DEBUG_COEX(priv, "Idle duration = 0x%X",
b6e116e8
WYG
1084 (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
1085 BT_UART_MSG_FRAME4IDLEDURATION_POS);
1086
fa7f1413 1087 IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
b6e116e8
WYG
1088 "eSCO Retransmissions = 0x%X",
1089 (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
1090 BT_UART_MSG_FRAME5TXACTIVITY_POS,
1091 (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
1092 BT_UART_MSG_FRAME5RXACTIVITY_POS,
1093 (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
1094 BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
1095
fa7f1413 1096 IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
b6e116e8
WYG
1097 (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
1098 BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
1099 (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
1100 BT_UART_MSG_FRAME6DISCOVERABLE_POS);
1101
fa7f1413 1102 IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
399f66fd 1103 "0x%X, Inquiry = 0x%X, Connectable = 0x%X",
b6e116e8
WYG
1104 (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
1105 BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
399f66fd
WYG
1106 (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
1107 BT_UART_MSG_FRAME7PAGE_POS,
1108 (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
1109 BT_UART_MSG_FRAME7INQUIRY_POS,
b6e116e8
WYG
1110 (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
1111 BT_UART_MSG_FRAME7CONNECTABLE_POS);
1112}
1113
506aa156
WYG
1114static void iwlagn_set_kill_msk(struct iwl_priv *priv,
1115 struct iwl_bt_uart_msg *uart_msg)
b6e116e8 1116{
506aa156 1117 u8 kill_msk;
20407ed8 1118 static const __le32 bt_kill_ack_msg[2] = {
506aa156
WYG
1119 IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
1120 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
1121 static const __le32 bt_kill_cts_msg[2] = {
1122 IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
1123 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
1124
1125 kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
1126 ? 1 : 0;
1127 if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
1128 priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
b6e116e8 1129 priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
506aa156
WYG
1130 priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
1131 priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
1132 priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
1133
b6e116e8 1134 /* schedule to send runtime bt_config */
74e28e44 1135 queue_work(priv->shrd->workqueue, &priv->bt_runtime_config);
b6e116e8 1136 }
b6e116e8
WYG
1137}
1138
1139void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
1140 struct iwl_rx_mem_buffer *rxb)
1141{
1142 unsigned long flags;
1143 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1144 struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
b6e116e8 1145 struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
b6e116e8 1146
c4197c62
WYG
1147 if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
1148 /* bt coex disabled */
1149 return;
1150 }
1151
fa7f1413
WYG
1152 IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
1153 IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
1154 IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
1155 IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
b6e116e8
WYG
1156 coex->bt_ci_compliance);
1157 iwlagn_print_uartmsg(priv, uart_msg);
1158
66e863a5 1159 priv->last_bt_traffic_load = priv->bt_traffic_load;
207ecc5e
MV
1160 priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
1161
b6e116e8
WYG
1162 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
1163 if (priv->bt_status != coex->bt_status ||
66e863a5 1164 priv->last_bt_traffic_load != coex->bt_traffic_load) {
b6e116e8
WYG
1165 if (coex->bt_status) {
1166 /* BT on */
1167 if (!priv->bt_ch_announce)
1168 priv->bt_traffic_load =
1169 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1170 else
1171 priv->bt_traffic_load =
1172 coex->bt_traffic_load;
1173 } else {
1174 /* BT off */
1175 priv->bt_traffic_load =
1176 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
1177 }
1178 priv->bt_status = coex->bt_status;
74e28e44 1179 queue_work(priv->shrd->workqueue,
b6e116e8
WYG
1180 &priv->bt_traffic_change_work);
1181 }
b6e116e8
WYG
1182 }
1183
506aa156 1184 iwlagn_set_kill_msk(priv, uart_msg);
b6e116e8
WYG
1185
1186 /* FIXME: based on notification, adjust the prio_boost */
1187
10b15e6f 1188 spin_lock_irqsave(&priv->shrd->lock, flags);
b6e116e8 1189 priv->bt_ci_compliance = coex->bt_ci_compliance;
10b15e6f 1190 spin_unlock_irqrestore(&priv->shrd->lock, flags);
b6e116e8
WYG
1191}
1192
1193void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
1194{
b6e116e8
WYG
1195 priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
1196 iwlagn_bt_coex_profile_notif;
1197}
1198
1199void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
1200{
b6e116e8
WYG
1201 INIT_WORK(&priv->bt_traffic_change_work,
1202 iwlagn_bt_traffic_change_work);
1203}
1204
1205void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
1206{
1207 cancel_work_sync(&priv->bt_traffic_change_work);
1208}
5de33068
JB
1209
1210static bool is_single_rx_stream(struct iwl_priv *priv)
1211{
1212 return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
1213 priv->current_ht_config.single_chain_sufficient;
1214}
1215
1216#define IWL_NUM_RX_CHAINS_MULTIPLE 3
1217#define IWL_NUM_RX_CHAINS_SINGLE 2
1218#define IWL_NUM_IDLE_CHAINS_DUAL 2
1219#define IWL_NUM_IDLE_CHAINS_SINGLE 1
1220
1221/*
1222 * Determine how many receiver/antenna chains to use.
1223 *
1224 * More provides better reception via diversity. Fewer saves power
1225 * at the expense of throughput, but only when not in powersave to
1226 * start with.
1227 *
1228 * MIMO (dual stream) requires at least 2, but works better with 3.
1229 * This does not determine *which* chains to use, just how many.
1230 */
1231static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
1232{
1233 if (priv->cfg->bt_params &&
1234 priv->cfg->bt_params->advanced_bt_coexist &&
1235 (priv->bt_full_concurrent ||
1236 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
1237 /*
1238 * only use chain 'A' in bt high traffic load or
1239 * full concurrency mode
1240 */
1241 return IWL_NUM_RX_CHAINS_SINGLE;
1242 }
1243 /* # of Rx chains to use when expecting MIMO. */
1244 if (is_single_rx_stream(priv))
1245 return IWL_NUM_RX_CHAINS_SINGLE;
1246 else
1247 return IWL_NUM_RX_CHAINS_MULTIPLE;
1248}
1249
1250/*
1251 * When we are in power saving mode, unless device support spatial
1252 * multiplexing power save, use the active count for rx chain count.
1253 */
1254static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
1255{
1256 /* # Rx chains when idling, depending on SMPS mode */
1257 switch (priv->current_ht_config.smps) {
1258 case IEEE80211_SMPS_STATIC:
1259 case IEEE80211_SMPS_DYNAMIC:
1260 return IWL_NUM_IDLE_CHAINS_SINGLE;
1261 case IEEE80211_SMPS_OFF:
1262 return active_cnt;
1263 default:
1264 WARN(1, "invalid SMPS mode %d",
1265 priv->current_ht_config.smps);
1266 return active_cnt;
1267 }
1268}
1269
1270/* up to 4 chains */
1271static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
1272{
1273 u8 res;
1274 res = (chain_bitmap & BIT(0)) >> 0;
1275 res += (chain_bitmap & BIT(1)) >> 1;
1276 res += (chain_bitmap & BIT(2)) >> 2;
1277 res += (chain_bitmap & BIT(3)) >> 3;
1278 return res;
1279}
1280
1281/**
1282 * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1283 *
1284 * Selects how many and which Rx receivers/antennas/chains to use.
1285 * This should not be used for scan command ... it puts data in wrong place.
1286 */
1287void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1288{
1289 bool is_single = is_single_rx_stream(priv);
63013ae3 1290 bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->shrd->status);
5de33068
JB
1291 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1292 u32 active_chains;
1293 u16 rx_chain;
1294
1295 /* Tell uCode which antennas are actually connected.
1296 * Before first association, we assume all antennas are connected.
1297 * Just after first association, iwl_chain_noise_calibration()
1298 * checks which antennas actually *are* connected. */
1299 if (priv->chain_noise_data.active_chains)
1300 active_chains = priv->chain_noise_data.active_chains;
1301 else
d6189124 1302 active_chains = hw_params(priv).valid_rx_ant;
5de33068
JB
1303
1304 if (priv->cfg->bt_params &&
1305 priv->cfg->bt_params->advanced_bt_coexist &&
1306 (priv->bt_full_concurrent ||
1307 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
1308 /*
1309 * only use chain 'A' in bt high traffic load or
1310 * full concurrency mode
1311 */
1312 active_chains = first_antenna(active_chains);
1313 }
1314
1315 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1316
1317 /* How many receivers should we use? */
1318 active_rx_cnt = iwl_get_active_rx_chain_count(priv);
1319 idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
1320
1321
1322 /* correct rx chain count according hw settings
1323 * and chain noise calibration
1324 */
1325 valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
1326 if (valid_rx_cnt < active_rx_cnt)
1327 active_rx_cnt = valid_rx_cnt;
1328
1329 if (valid_rx_cnt < idle_rx_cnt)
1330 idle_rx_cnt = valid_rx_cnt;
1331
1332 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
1333 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
1334
1335 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1336
1337 if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
1338 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1339 else
1340 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1341
1342 IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
1343 ctx->staging.rx_chain,
1344 active_rx_cnt, idle_rx_cnt);
1345
1346 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1347 active_rx_cnt < idle_rx_cnt);
1348}
facd982e
JB
1349
1350u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
1351{
1352 int i;
1353 u8 ind = ant;
1354
1355 if (priv->band == IEEE80211_BAND_2GHZ &&
1356 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
1357 return 0;
1358
1359 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
1360 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
1361 if (valid & BIT(ind))
1362 return ind;
1363 }
1364 return ant;
1365}
fed73292 1366
7194207c
JB
1367/* notification wait support */
1368void iwlagn_init_notification_wait(struct iwl_priv *priv,
1369 struct iwl_notification_wait *wait_entry,
09f18afe 1370 u8 cmd,
7194207c 1371 void (*fn)(struct iwl_priv *priv,
09f18afe
JB
1372 struct iwl_rx_packet *pkt,
1373 void *data),
1374 void *fn_data)
7194207c
JB
1375{
1376 wait_entry->fn = fn;
09f18afe 1377 wait_entry->fn_data = fn_data;
7194207c
JB
1378 wait_entry->cmd = cmd;
1379 wait_entry->triggered = false;
e74fe233 1380 wait_entry->aborted = false;
7194207c 1381
898ed67b
WYG
1382 spin_lock_bh(&priv->notif_wait_lock);
1383 list_add(&wait_entry->list, &priv->notif_waits);
1384 spin_unlock_bh(&priv->notif_wait_lock);
7194207c
JB
1385}
1386
a8674a1e
JB
1387int iwlagn_wait_notification(struct iwl_priv *priv,
1388 struct iwl_notification_wait *wait_entry,
1389 unsigned long timeout)
7194207c
JB
1390{
1391 int ret;
1392
898ed67b 1393 ret = wait_event_timeout(priv->notif_waitq,
e74fe233 1394 wait_entry->triggered || wait_entry->aborted,
7194207c
JB
1395 timeout);
1396
898ed67b 1397 spin_lock_bh(&priv->notif_wait_lock);
7194207c 1398 list_del(&wait_entry->list);
898ed67b 1399 spin_unlock_bh(&priv->notif_wait_lock);
7194207c 1400
e74fe233
JB
1401 if (wait_entry->aborted)
1402 return -EIO;
1403
a8674a1e
JB
1404 /* return value is always >= 0 */
1405 if (ret <= 0)
1406 return -ETIMEDOUT;
1407 return 0;
7194207c
JB
1408}
1409
1410void iwlagn_remove_notification(struct iwl_priv *priv,
1411 struct iwl_notification_wait *wait_entry)
1412{
898ed67b 1413 spin_lock_bh(&priv->notif_wait_lock);
7194207c 1414 list_del(&wait_entry->list);
898ed67b 1415 spin_unlock_bh(&priv->notif_wait_lock);
7194207c 1416}
This page took 0.317897 seconds and 5 git commands to generate.