blob: 359bd90605609ead338a6c17d131fbcb75f962f9 [file] [log] [blame]
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -07001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07005 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -07006 *
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 *****************************************************************************/
Wey-Yi Guy8d801082010-03-17 13:34:36 -070029#include <linux/etherdevice.h>
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070030#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"
Johannes Berg1fa61b22010-04-28 08:44:52 -070041#include "iwl-sta.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070042#include "iwl-trans.h"
Emmanuel Grumbach48f20d32011-08-25 23:10:36 -070043#include "iwl-shared.h"
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070044
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070045int 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{
Wey-Yi Guyab63c682010-09-20 09:12:31 -070053 struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070054 u8 tx_ant_cfg_cmd;
55
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -070056 if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->shrd->status),
Stanislaw Gruszka4beeba72010-10-25 10:34:50 +020057 "TX Power requested while scanning!\n"))
58 return -EAGAIN;
59
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070060 /* 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 }
Wey-Yi Guyab63c682010-09-20 09:12:31 -070078 tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
79 tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070080
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
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -070086 return iwl_trans_send_cmd_pdu(trans(priv), tx_ant_cfg_cmd, CMD_SYNC,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070087 sizeof(tx_power_cmd), &tx_power_cmd);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070088}
89
90void iwlagn_temperature(struct iwl_priv *priv)
91{
Fry, Donald Hf8f79a52011-02-25 09:44:48 -080092 /* store temperature from correct statistics (in Celsius) */
Johannes Berg0da0e5b2011-04-08 08:14:56 -070093 priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -070094 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,
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700106 EEPROM_CALIB_ALL);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700107 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:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700123 offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700124 break;
125 case INDIRECT_GENERAL:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700126 offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700127 break;
128 case INDIRECT_REGULATORY:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700129 offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700130 break;
Johannes Berg8d6748c2010-12-09 09:30:14 -0800131 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;
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700137 case INDIRECT_CALIBRATION:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700138 offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700139 break;
140 case INDIRECT_PROCESS_ADJST:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700141 offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700142 break;
143 case INDIRECT_OTHERS:
Wey-Yi Guy7944f8e2010-04-06 21:10:33 -0700144 offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700145 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
Fry, Donald H70e3e8a2011-07-08 08:46:19 -0700156const u8 *iwl_eeprom_query_addr(const struct iwl_priv *priv, size_t offset)
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700157{
158 u32 address = eeprom_indirect_address(priv, offset);
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700159 BUG_ON(address >= priv->cfg->base_params->eeprom_size);
Wey-Yi Guye04ed0a2010-03-16 17:47:58 -0700160 return &priv->eeprom[address];
161}
Wey-Yi Guy348ee7c2010-03-16 12:37:27 -0700162
163struct iwl_mod_params iwlagn_mod_params = {
164 .amsdu_size_8K = 1,
165 .restart_fw = 1,
Stanislaw Gruszkab7977ff2011-02-28 14:33:15 +0100166 .plcp_check = true,
Wey-Yi Guyb60eec92011-06-03 13:52:38 -0700167 .bt_coex_active = true,
Wey-Yi Guy3f1e5f42011-06-03 13:52:40 -0700168 .no_sleep_autoadjust = true,
Wey-Yi Guyf7538162011-06-11 10:00:06 -0700169 .power_level = IWL_POWER_INDEX_1,
Wey-Yi Guyfee84f02011-08-25 23:10:57 -0700170 .bt_ch_announce = true,
Emmanuel Grumbach48f20d32011-08-25 23:10:36 -0700171 .wanted_ucode_alternative = 1,
Wey-Yi Guydd5b6d02011-08-25 23:10:55 -0700172 .auto_agg = true,
Wey-Yi Guy348ee7c2010-03-16 12:37:27 -0700173 /* the rest are 0 by default */
174};
Wey-Yi Guy74bcdb32010-03-17 13:34:34 -0700175
Wey-Yi Guy8d801082010-03-17 13:34:36 -0700176int 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
Johannes Bergb6e4c552010-04-06 04:12:42 -0700197static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
Johannes Berg1dda6d22010-04-29 04:43:06 -0700198 struct ieee80211_vif *vif,
199 enum ieee80211_band band,
200 struct iwl_scan_channel *scan_ch)
Johannes Bergb6e4c552010-04-06 04:12:42 -0700201{
202 const struct ieee80211_supported_band *sband;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700203 u16 passive_dwell = 0;
204 u16 active_dwell = 0;
Abhijeet Kolekar14023642010-06-02 21:15:10 -0700205 int added = 0;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700206 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);
Johannes Berg1dda6d22010-04-29 04:43:06 -0700215 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700216
217 if (passive_dwell <= active_dwell)
218 passive_dwell = active_dwell + 1;
219
Abhijeet Kolekar14023642010-06-02 21:15:10 -0700220 channel = iwl_get_single_channel_number(priv, band);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700221 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,
Johannes Berg1dda6d22010-04-29 04:43:06 -0700239 struct ieee80211_vif *vif,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700240 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);
Johannes Berg1dda6d22010-04-29 04:43:06 -0700257 passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700258
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
Shanyu Zhao81e95432010-07-28 13:40:27 -0700268 channel = chan->hw_value;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700269 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
Johannes Berg3eecce52010-09-13 14:46:33 +0200317int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
Johannes Bergb6e4c552010-04-06 04:12:42 -0700318{
319 struct iwl_host_cmd cmd = {
320 .id = REPLY_SCAN_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700321 .len = { sizeof(struct iwl_scan_cmd), },
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700322 .flags = CMD_SYNC,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700323 };
324 struct iwl_scan_cmd *scan;
Johannes Berga194e322010-08-27 08:53:46 -0700325 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
Johannes Bergb6e4c552010-04-06 04:12:42 -0700326 u32 rate_flags = 0;
327 u16 cmd_len;
328 u16 rx_chain = 0;
329 enum ieee80211_band band;
330 u8 n_probes = 0;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700331 u8 rx_ant = hw_params(priv).valid_rx_ant;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700332 u8 rate;
333 bool is_active = false;
334 int chan_mod;
335 u8 active_chains;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700336 u8 scan_tx_antennas = hw_params(priv).valid_tx_ant;
Johannes Berg3eecce52010-09-13 14:46:33 +0200337 int ret;
338
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700339 lockdep_assert_held(&priv->shrd->mutex);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700340
Johannes Berga194e322010-08-27 08:53:46 -0700341 if (vif)
342 ctx = iwl_rxon_ctx_from_vif(vif);
343
Johannes Bergb6e4c552010-04-06 04:12:42 -0700344 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");
Johannes Berg3eecce52010-09-13 14:46:33 +0200350 return -ENOMEM;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700351 }
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
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700359 if (priv->scan_type != IWL_SCAN_ROC &&
Johannes Berg266af4c72011-03-10 20:13:26 -0800360 iwl_is_any_associated(priv)) {
Johannes Bergb6e4c552010-04-06 04:12:42 -0700361 u16 interval = 0;
362 u32 extra;
363 u32 suspend_time = 100;
364 u32 scan_suspend_time = 100;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700365
366 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
Johannes Berg266af4c72011-03-10 20:13:26 -0800367 switch (priv->scan_type) {
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700368 case IWL_SCAN_ROC:
Johannes Berg266af4c72011-03-10 20:13:26 -0800369 WARN_ON(1);
370 break;
371 case IWL_SCAN_RADIO_RESET:
John W. Linvillea6e492b2010-07-22 15:24:56 -0400372 interval = 0;
Johannes Berg266af4c72011-03-10 20:13:26 -0800373 break;
374 case IWL_SCAN_NORMAL:
John W. Linvillea6e492b2010-07-22 15:24:56 -0400375 interval = vif->bss_conf.beacon_int;
Johannes Berg266af4c72011-03-10 20:13:26 -0800376 break;
377 }
Johannes Bergb6e4c552010-04-06 04:12:42 -0700378
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);
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700390 } else if (priv->scan_type == IWL_SCAN_ROC) {
Johannes Berg266af4c72011-03-10 20:13:26 -0800391 scan->suspend_time = 0;
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700392 scan->max_out_time = 0;
393 scan->quiet_time = 0;
394 scan->quiet_plcp_th = 0;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700395 }
396
Johannes Berg266af4c72011-03-10 20:13:26 -0800397 switch (priv->scan_type) {
398 case IWL_SCAN_RADIO_RESET:
Johannes Bergb6e4c552010-04-06 04:12:42 -0700399 IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
Johannes Berg266af4c72011-03-10 20:13:26 -0800400 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;
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700422 case IWL_SCAN_ROC:
423 IWL_DEBUG_SCAN(priv, "Start ROC scan.\n");
Johannes Berg266af4c72011-03-10 20:13:26 -0800424 break;
425 }
Johannes Bergb6e4c552010-04-06 04:12:42 -0700426
427 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Johannes Berga194e322010-08-27 08:53:46 -0700428 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700429 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;
Johannes Berg246ed352010-08-23 10:46:32 +0200434 chan_mod = le32_to_cpu(
435 priv->contexts[IWL_RXON_CTX_BSS].active.flags &
436 RXON_FLG_CHANNEL_MODE_MSK)
Johannes Bergb6e4c552010-04-06 04:12:42 -0700437 >> 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 }
Johannes Bergd44ae692010-08-23 07:56:56 -0700444 /*
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 */
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700448 if (priv->cfg->bt_params &&
449 priv->cfg->bt_params->advanced_bt_coexist)
Johannes Bergd44ae692010-08-23 07:56:56 -0700450 scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700451 break;
452 case IEEE80211_BAND_5GHZ:
453 rate = IWL_RATE_6M_PLCP;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700454 break;
455 default:
Johannes Berg3eecce52010-09-13 14:46:33 +0200456 IWL_WARN(priv, "Invalid scan band\n");
457 return -EIO;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700458 }
459
Johannes Berg085fbca2010-10-04 05:47:23 -0700460 /*
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.
Johannes Bergd2690c02011-04-20 09:10:39 -0700476 *
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.
Johannes Berg085fbca2010-10-04 05:47:23 -0700480 */
Johannes Bergd2690c02011-04-20 09:10:39 -0700481 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;
Johannes Berg085fbca2010-10-04 05:47:23 -0700487
Johannes Bergb6e4c552010-04-06 04:12:42 -0700488 band = priv->scan_band;
489
Johannes Berg0e1654f2010-05-18 02:48:36 -0700490 if (priv->cfg->scan_rx_antennas[band])
491 rx_ant = priv->cfg->scan_rx_antennas[band];
Johannes Berge7cb4952010-04-13 01:04:35 -0700492
Stanislaw Gruszkacd017f22010-12-23 15:12:30 +0100493 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);
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700498 }
499
Johannes Berg0e1654f2010-05-18 02:48:36 -0700500 priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
501 scan_tx_antennas);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700502 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 */
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700506 if (test_bit(STATUS_POWER_PMI, &priv->shrd->status)) {
Johannes Bergb6e4c552010-04-06 04:12:42 -0700507 /* 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 }
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -0700518 if (priv->cfg->bt_params &&
519 priv->cfg->bt_params->advanced_bt_coexist &&
520 priv->bt_full_concurrent) {
Wey-Yi Guybee008b2010-08-23 07:57:04 -0700521 /* operated as 1x1 in full concurrency mode */
522 rx_ant = first_antenna(rx_ant);
523 }
524
Johannes Bergb6e4c552010-04-06 04:12:42 -0700525 /* MIMO is not used here, but value is required */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700526 rx_chain |=
527 hw_params(priv).valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700528 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);
Johannes Berg266af4c72011-03-10 20:13:26 -0800532 switch (priv->scan_type) {
533 case IWL_SCAN_NORMAL:
Johannes Bergb6e4c552010-04-06 04:12:42 -0700534 cmd_len = iwl_fill_probe_req(priv,
535 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -0700536 vif->addr,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700537 priv->scan_request->ie,
538 priv->scan_request->ie_len,
539 IWL_MAX_SCAN_SIZE - sizeof(*scan));
Johannes Berg266af4c72011-03-10 20:13:26 -0800540 break;
541 case IWL_SCAN_RADIO_RESET:
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700542 case IWL_SCAN_ROC:
Johannes Berg3a0b9aa2010-05-12 03:33:12 -0700543 /* use bcast addr, will not be transmitted but must be valid */
Johannes Bergb6e4c552010-04-06 04:12:42 -0700544 cmd_len = iwl_fill_probe_req(priv,
545 (struct ieee80211_mgmt *)scan->data,
Johannes Berg3a0b9aa2010-05-12 03:33:12 -0700546 iwl_bcast_addr, NULL, 0,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700547 IWL_MAX_SCAN_SIZE - sizeof(*scan));
Johannes Berg266af4c72011-03-10 20:13:26 -0800548 break;
Johannes Berg266af4c72011-03-10 20:13:26 -0800549 default:
550 BUG();
Johannes Bergb6e4c552010-04-06 04:12:42 -0700551 }
552 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700553
554 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
555 RXON_FILTER_BCON_AWARE_MSK);
556
Johannes Berg266af4c72011-03-10 20:13:26 -0800557 switch (priv->scan_type) {
558 case IWL_SCAN_RADIO_RESET:
Johannes Bergb6e4c552010-04-06 04:12:42 -0700559 scan->channel_count =
Johannes Berg1dda6d22010-04-29 04:43:06 -0700560 iwl_get_single_channel_for_scan(priv, vif, band,
Johannes Berg266af4c72011-03-10 20:13:26 -0800561 (void *)&scan->data[cmd_len]);
562 break;
563 case IWL_SCAN_NORMAL:
Johannes Bergb6e4c552010-04-06 04:12:42 -0700564 scan->channel_count =
Johannes Berg1dda6d22010-04-29 04:43:06 -0700565 iwl_get_channels_for_scan(priv, vif, band,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700566 is_active, n_probes,
Johannes Berg266af4c72011-03-10 20:13:26 -0800567 (void *)&scan->data[cmd_len]);
568 break;
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700569 case IWL_SCAN_ROC: {
Johannes Berg266af4c72011-03-10 20:13:26 -0800570 struct iwl_scan_channel *scan_ch;
571
572 scan->channel_count = 1;
573
574 scan_ch = (void *)&scan->data[cmd_len];
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700575 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
Johannes Berg266af4c72011-03-10 20:13:26 -0800576 scan_ch->channel =
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700577 cpu_to_le16(priv->hw_roc_channel->hw_value);
Johannes Berg266af4c72011-03-10 20:13:26 -0800578 scan_ch->active_dwell =
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700579 scan_ch->passive_dwell =
580 cpu_to_le16(priv->hw_roc_duration);
Johannes Berg266af4c72011-03-10 20:13:26 -0800581
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 */
Johannes Bergc6baf7f2011-07-23 10:24:47 -0700589 if (priv->hw_roc_channel->band == IEEE80211_BAND_5GHZ)
Johannes Berg266af4c72011-03-10 20:13:26 -0800590 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
591 else
592 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
593 }
594 break;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700595 }
Johannes Berg266af4c72011-03-10 20:13:26 -0800596
Johannes Bergb6e4c552010-04-06 04:12:42 -0700597 if (scan->channel_count == 0) {
598 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
Johannes Berg3eecce52010-09-13 14:46:33 +0200599 return -EIO;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700600 }
601
Johannes Berg3fa50732011-05-04 07:50:38 -0700602 cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
Johannes Bergb6e4c552010-04-06 04:12:42 -0700603 scan->channel_count * sizeof(struct iwl_scan_channel);
Johannes Berg3fa50732011-05-04 07:50:38 -0700604 cmd.data[0] = scan;
Johannes Berg4ce7cc22011-05-13 11:57:40 -0700605 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
Johannes Berg3fa50732011-05-04 07:50:38 -0700606 scan->len = cpu_to_le16(cmd.len[0]);
Johannes Bergb6e4c552010-04-06 04:12:42 -0700607
Johannes Berg1cf26372010-09-22 07:32:13 -0700608 /* set scan bit here for PAN params */
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700609 set_bit(STATUS_SCAN_HW, &priv->shrd->status);
Johannes Berg1cf26372010-09-22 07:32:13 -0700610
Wey-Yi Guye3f10ce2011-07-01 07:59:26 -0700611 ret = iwlagn_set_pan_params(priv);
612 if (ret)
613 return ret;
Johannes Berg3eecce52010-09-13 14:46:33 +0200614
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700615 ret = iwl_trans_send_cmd(trans(priv), &cmd);
Johannes Berg3eecce52010-09-13 14:46:33 +0200616 if (ret) {
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700617 clear_bit(STATUS_SCAN_HW, &priv->shrd->status);
Wey-Yi Guye3f10ce2011-07-01 07:59:26 -0700618 iwlagn_set_pan_params(priv);
Johannes Berg3eecce52010-09-13 14:46:33 +0200619 }
Johannes Berg52a02d12010-08-27 09:44:50 -0700620
Johannes Berg3eecce52010-09-13 14:46:33 +0200621 return ret;
Johannes Bergb6e4c552010-04-06 04:12:42 -0700622}
Johannes Berg1fa61b22010-04-28 08:44:52 -0700623
624int iwlagn_manage_ibss_station(struct iwl_priv *priv,
625 struct ieee80211_vif *vif, bool add)
626{
Johannes Bergfd1af152010-04-30 11:30:43 -0700627 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
628
Johannes Berg1fa61b22010-04-28 08:44:52 -0700629 if (add)
Johannes Berga30e3112010-09-22 18:02:01 +0200630 return iwlagn_add_bssid_station(priv, vif_priv->ctx,
631 vif->bss_conf.bssid,
632 &vif_priv->ibss_bssid_sta_id);
Johannes Bergfd1af152010-04-30 11:30:43 -0700633 return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
634 vif->bss_conf.bssid);
Johannes Berg1fa61b22010-04-28 08:44:52 -0700635}
Johannes Berg1ff504e2010-05-03 01:22:42 -0700636
Wey-Yi Guy716c74b2010-06-24 13:22:36 -0700637#define IWL_FLUSH_WAIT_MS 2000
638
639int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
640{
641 struct iwl_tx_queue *txq;
642 struct iwl_queue *q;
643 int cnt;
644 unsigned long now = jiffies;
645 int ret = 0;
646
647 /* waiting for all the tx frames complete might take a while */
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700648 for (cnt = 0; cnt < hw_params(priv).max_txq_num; cnt++) {
Emmanuel Grumbachcefeaa52011-08-25 23:10:40 -0700649 if (cnt == priv->shrd->cmd_queue)
Wey-Yi Guy716c74b2010-06-24 13:22:36 -0700650 continue;
651 txq = &priv->txq[cnt];
652 q = &txq->q;
653 while (q->read_ptr != q->write_ptr && !time_after(jiffies,
654 now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
655 msleep(1);
656
657 if (q->read_ptr != q->write_ptr) {
658 IWL_ERR(priv, "fail to flush all tx fifo queues\n");
659 ret = -ETIMEDOUT;
660 break;
661 }
662 }
663 return ret;
664}
665
666#define IWL_TX_QUEUE_MSK 0xfffff
667
668/**
669 * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
670 *
671 * pre-requirements:
672 * 1. acquire mutex before calling
673 * 2. make sure rf is on and not in exit state
674 */
675int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
676{
677 struct iwl_txfifo_flush_cmd flush_cmd;
678 struct iwl_host_cmd cmd = {
679 .id = REPLY_TXFIFO_FLUSH,
Johannes Berg3fa50732011-05-04 07:50:38 -0700680 .len = { sizeof(struct iwl_txfifo_flush_cmd), },
Wey-Yi Guy716c74b2010-06-24 13:22:36 -0700681 .flags = CMD_SYNC,
Johannes Berg3fa50732011-05-04 07:50:38 -0700682 .data = { &flush_cmd, },
Wey-Yi Guy716c74b2010-06-24 13:22:36 -0700683 };
684
685 might_sleep();
686
687 memset(&flush_cmd, 0, sizeof(flush_cmd));
Wey-Yi Guyecdbe862011-06-08 09:57:26 -0700688 if (flush_control & BIT(IWL_RXON_CTX_BSS))
689 flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
Wey-Yi Guyf88e0ec2011-06-08 09:57:25 -0700690 IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
691 IWL_SCD_MGMT_MSK;
Wey-Yi Guyecdbe862011-06-08 09:57:26 -0700692 if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
693 (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
Wey-Yi Guyf88e0ec2011-06-08 09:57:25 -0700694 flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
695 IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
696 IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
697 IWL_PAN_SCD_MULTICAST_MSK;
698
Wey-Yi Guy88950752011-06-06 12:05:46 -0700699 if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
Wey-Yi Guy716c74b2010-06-24 13:22:36 -0700700 flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
701
702 IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
703 flush_cmd.fifo_control);
704 flush_cmd.flush_control = cpu_to_le16(flush_control);
705
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700706 return iwl_trans_send_cmd(trans(priv), &cmd);
Wey-Yi Guy716c74b2010-06-24 13:22:36 -0700707}
Wey-Yi Guy65550632010-06-24 13:18:35 -0700708
709void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
710{
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700711 mutex_lock(&priv->shrd->mutex);
Wey-Yi Guy65550632010-06-24 13:18:35 -0700712 ieee80211_stop_queues(priv->hw);
Wey-Yi Guyc68744f2011-06-18 08:03:18 -0700713 if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
Wey-Yi Guy65550632010-06-24 13:18:35 -0700714 IWL_ERR(priv, "flush request fail\n");
715 goto done;
716 }
717 IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
718 iwlagn_wait_tx_queue_empty(priv);
719done:
720 ieee80211_wake_queues(priv->hw);
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700721 mutex_unlock(&priv->shrd->mutex);
Wey-Yi Guy65550632010-06-24 13:18:35 -0700722}
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700723
724/*
725 * BT coex
726 */
727/*
728 * Macros to access the lookup table.
729 *
730 * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
731* wifi_prio, wifi_txrx and wifi_sh_ant_req.
732 *
733 * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
734 *
735 * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
736 * one after another in 32-bit registers, and "registers" 0 through 7 contain
737 * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
738 *
739 * These macros encode that format.
740 */
741#define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
742 wifi_txrx, wifi_sh_ant_req) \
743 (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
744 (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
745
746#define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
747 lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
748#define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
749 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
750 (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
751 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
752 wifi_sh_ant_req))))
753#define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
754 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
755 LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
756 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
757 wifi_sh_ant_req))
758#define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
759 wifi_req, wifi_prio, wifi_txrx, \
760 wifi_sh_ant_req) \
761 LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
762 bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
763 wifi_sh_ant_req))
764
765#define LUT_WLAN_KILL_OP(lut, op, val) \
766 lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
767#define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
768 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
769 (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
770 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
771#define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
772 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
773 LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
774 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
775#define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
776 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
777 LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
778 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
779
780#define LUT_ANT_SWITCH_OP(lut, op, val) \
781 lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
782#define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
783 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
784 (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
785 wifi_req, wifi_prio, wifi_txrx, \
786 wifi_sh_ant_req))))
787#define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
788 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
789 LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
790 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
791#define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
792 wifi_prio, wifi_txrx, wifi_sh_ant_req) \
793 LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
794 wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
795
796static const __le32 iwlagn_def_3w_lookup[12] = {
797 cpu_to_le32(0xaaaaaaaa),
798 cpu_to_le32(0xaaaaaaaa),
799 cpu_to_le32(0xaeaaaaaa),
800 cpu_to_le32(0xaaaaaaaa),
801 cpu_to_le32(0xcc00ff28),
802 cpu_to_le32(0x0000aaaa),
803 cpu_to_le32(0xcc00aaaa),
804 cpu_to_le32(0x0000aaaa),
805 cpu_to_le32(0xc0004000),
806 cpu_to_le32(0x00004000),
807 cpu_to_le32(0xf0005000),
Wey-Yi Guy9a67d762010-11-18 10:40:03 -0800808 cpu_to_le32(0xf0005000),
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700809};
810
811static const __le32 iwlagn_concurrent_lookup[12] = {
812 cpu_to_le32(0xaaaaaaaa),
813 cpu_to_le32(0xaaaaaaaa),
814 cpu_to_le32(0xaaaaaaaa),
815 cpu_to_le32(0xaaaaaaaa),
816 cpu_to_le32(0xaaaaaaaa),
817 cpu_to_le32(0xaaaaaaaa),
818 cpu_to_le32(0xaaaaaaaa),
819 cpu_to_le32(0xaaaaaaaa),
820 cpu_to_le32(0x00000000),
821 cpu_to_le32(0x00000000),
822 cpu_to_le32(0x00000000),
823 cpu_to_le32(0x00000000),
824};
825
826void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
827{
Wey-Yi Guy60132702011-02-18 17:23:54 -0800828 struct iwl_basic_bt_cmd basic = {
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700829 .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
830 .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
831 .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
832 .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
833 };
Wey-Yi Guy60132702011-02-18 17:23:54 -0800834 struct iwl6000_bt_cmd bt_cmd_6000;
835 struct iwl2000_bt_cmd bt_cmd_2000;
836 int ret;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700837
838 BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
Wey-Yi Guy60132702011-02-18 17:23:54 -0800839 sizeof(basic.bt3_lookup_table));
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700840
Wey-Yi Guy60132702011-02-18 17:23:54 -0800841 if (priv->cfg->bt_params) {
842 if (priv->cfg->bt_params->bt_session_2) {
843 bt_cmd_2000.prio_boost = cpu_to_le32(
844 priv->cfg->bt_params->bt_prio_boost);
845 bt_cmd_2000.tx_prio_boost = 0;
846 bt_cmd_2000.rx_prio_boost = 0;
847 } else {
848 bt_cmd_6000.prio_boost =
849 priv->cfg->bt_params->bt_prio_boost;
850 bt_cmd_6000.tx_prio_boost = 0;
851 bt_cmd_6000.rx_prio_boost = 0;
852 }
853 } else {
854 IWL_ERR(priv, "failed to construct BT Coex Config\n");
855 return;
856 }
Wey-Yi Guy506aa152010-11-24 17:25:03 -0800857
Wey-Yi Guy60132702011-02-18 17:23:54 -0800858 basic.kill_ack_mask = priv->kill_ack_mask;
859 basic.kill_cts_mask = priv->kill_cts_mask;
860 basic.valid = priv->bt_valid;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700861
862 /*
863 * Configure BT coex mode to "no coexistence" when the
864 * user disabled BT coexistence, we have no interface
865 * (might be in monitor mode), or the interface is in
866 * IBSS mode (no proper uCode support for coex then).
867 */
Wey-Yi Guyb60eec92011-06-03 13:52:38 -0700868 if (!iwlagn_mod_params.bt_coex_active ||
869 priv->iw_mode == NL80211_IFTYPE_ADHOC) {
Wey-Yi Guy60132702011-02-18 17:23:54 -0800870 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700871 } else {
Wey-Yi Guy60132702011-02-18 17:23:54 -0800872 basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700873 IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -0700874
875 if (!priv->bt_enable_pspoll)
Wey-Yi Guy60132702011-02-18 17:23:54 -0800876 basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -0700877 else
878 basic.flags &= ~IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
Wey-Yi Guye3661762010-11-23 10:58:54 -0800879
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700880 if (priv->bt_ch_announce)
Wey-Yi Guy60132702011-02-18 17:23:54 -0800881 basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -0700882 IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700883 }
Wey-Yi Guy60132702011-02-18 17:23:54 -0800884 priv->bt_enable_flag = basic.flags;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700885 if (priv->bt_full_concurrent)
Wey-Yi Guy60132702011-02-18 17:23:54 -0800886 memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700887 sizeof(iwlagn_concurrent_lookup));
888 else
Wey-Yi Guy60132702011-02-18 17:23:54 -0800889 memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700890 sizeof(iwlagn_def_3w_lookup));
891
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -0700892 IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
Wey-Yi Guy60132702011-02-18 17:23:54 -0800893 basic.flags ? "active" : "disabled",
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700894 priv->bt_full_concurrent ?
895 "full concurrency" : "3-wire");
896
Wey-Yi Guy60132702011-02-18 17:23:54 -0800897 if (priv->cfg->bt_params->bt_session_2) {
898 memcpy(&bt_cmd_2000.basic, &basic,
899 sizeof(basic));
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700900 ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700901 CMD_SYNC, sizeof(bt_cmd_2000), &bt_cmd_2000);
Wey-Yi Guy60132702011-02-18 17:23:54 -0800902 } else {
903 memcpy(&bt_cmd_6000.basic, &basic,
904 sizeof(basic));
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -0700905 ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700906 CMD_SYNC, sizeof(bt_cmd_6000), &bt_cmd_6000);
Wey-Yi Guy60132702011-02-18 17:23:54 -0800907 }
908 if (ret)
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700909 IWL_ERR(priv, "failed to send BT Coex Config\n");
910
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700911}
912
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -0700913void iwlagn_bt_adjust_rssi_monitor(struct iwl_priv *priv, bool rssi_ena)
914{
915 struct iwl_rxon_context *ctx, *found_ctx = NULL;
916 bool found_ap = false;
917
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700918 lockdep_assert_held(&priv->shrd->mutex);
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -0700919
920 /* Check whether AP or GO mode is active. */
921 if (rssi_ena) {
922 for_each_context(priv, ctx) {
923 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_AP &&
924 iwl_is_associated_ctx(ctx)) {
925 found_ap = true;
926 break;
927 }
928 }
929 }
930
931 /*
932 * If disable was received or If GO/AP mode, disable RSSI
933 * measurements.
934 */
935 if (!rssi_ena || found_ap) {
936 if (priv->cur_rssi_ctx) {
937 ctx = priv->cur_rssi_ctx;
938 ieee80211_disable_rssi_reports(ctx->vif);
939 priv->cur_rssi_ctx = NULL;
940 }
941 return;
942 }
943
944 /*
945 * If rssi measurements need to be enabled, consider all cases now.
946 * Figure out how many contexts are active.
947 */
948 for_each_context(priv, ctx) {
949 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION &&
950 iwl_is_associated_ctx(ctx)) {
951 found_ctx = ctx;
952 break;
953 }
954 }
955
956 /*
957 * rssi monitor already enabled for the correct interface...nothing
958 * to do.
959 */
960 if (found_ctx == priv->cur_rssi_ctx)
961 return;
962
963 /*
964 * Figure out if rssi monitor is currently enabled, and needs
965 * to be changed. If rssi monitor is already enabled, disable
966 * it first else just enable rssi measurements on the
967 * interface found above.
968 */
969 if (priv->cur_rssi_ctx) {
970 ctx = priv->cur_rssi_ctx;
971 if (ctx->vif)
972 ieee80211_disable_rssi_reports(ctx->vif);
973 }
974
975 priv->cur_rssi_ctx = found_ctx;
976
977 if (!found_ctx)
978 return;
979
980 ieee80211_enable_rssi_reports(found_ctx->vif,
981 IWLAGN_BT_PSP_MIN_RSSI_THRESHOLD,
982 IWLAGN_BT_PSP_MAX_RSSI_THRESHOLD);
983}
984
985static bool iwlagn_bt_traffic_is_sco(struct iwl_bt_uart_msg *uart_msg)
986{
987 return BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3 >>
988 BT_UART_MSG_FRAME3SCOESCO_POS;
989}
990
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700991static void iwlagn_bt_traffic_change_work(struct work_struct *work)
992{
993 struct iwl_priv *priv =
994 container_of(work, struct iwl_priv, bt_traffic_change_work);
Johannes Berg8bd413e2010-08-23 10:46:40 +0200995 struct iwl_rxon_context *ctx;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -0700996 int smps_request = -1;
997
Wey-Yi Guyc4197c62011-02-06 08:56:35 -0800998 if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
999 /* bt coex disabled */
1000 return;
1001 }
1002
Stanislaw Gruszka5eda74a2010-10-22 17:04:28 +02001003 /*
1004 * Note: bt_traffic_load can be overridden by scan complete and
1005 * coex profile notifications. Ignore that since only bad consequence
1006 * can be not matching debug print with actual state.
1007 */
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001008 IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001009 priv->bt_traffic_load);
1010
1011 switch (priv->bt_traffic_load) {
1012 case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
Wey-Yi Guyf5682c02010-10-23 09:15:44 -07001013 if (priv->bt_status)
1014 smps_request = IEEE80211_SMPS_DYNAMIC;
1015 else
1016 smps_request = IEEE80211_SMPS_AUTOMATIC;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001017 break;
1018 case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
1019 smps_request = IEEE80211_SMPS_DYNAMIC;
1020 break;
1021 case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
1022 case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
1023 smps_request = IEEE80211_SMPS_STATIC;
1024 break;
1025 default:
1026 IWL_ERR(priv, "Invalid BT traffic load: %d\n",
1027 priv->bt_traffic_load);
1028 break;
1029 }
1030
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001031 mutex_lock(&priv->shrd->mutex);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001032
Stanislaw Gruszka5eda74a2010-10-22 17:04:28 +02001033 /*
1034 * We can not send command to firmware while scanning. When the scan
1035 * complete we will schedule this work again. We do check with mutex
1036 * locked to prevent new scan request to arrive. We do not check
1037 * STATUS_SCANNING to avoid race when queue_work two times from
1038 * different notifications, but quit and not perform any work at all.
1039 */
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -07001040 if (test_bit(STATUS_SCAN_HW, &priv->shrd->status))
Stanislaw Gruszka5eda74a2010-10-22 17:04:28 +02001041 goto out;
1042
Fry, Donald H6b6db912011-07-08 08:46:17 -07001043 iwl_update_chain_flags(priv);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001044
Johannes Berg8bd413e2010-08-23 10:46:40 +02001045 if (smps_request != -1) {
Wey-Yi Guy88e9ba72011-06-03 07:54:12 -07001046 priv->current_ht_config.smps = smps_request;
Johannes Berg8bd413e2010-08-23 10:46:40 +02001047 for_each_context(priv, ctx) {
1048 if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
1049 ieee80211_request_smps(ctx->vif, smps_request);
1050 }
1051 }
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -07001052
1053 /*
1054 * Dynamic PS poll related functionality. Adjust RSSI measurements if
1055 * necessary.
1056 */
1057 iwlagn_bt_coex_rssi_monitor(priv);
Stanislaw Gruszka5eda74a2010-10-22 17:04:28 +02001058out:
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001059 mutex_unlock(&priv->shrd->mutex);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001060}
1061
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -07001062/*
1063 * If BT sco traffic, and RSSI monitor is enabled, move measurements to the
1064 * correct interface or disable it if this is the last interface to be
1065 * removed.
1066 */
1067void iwlagn_bt_coex_rssi_monitor(struct iwl_priv *priv)
1068{
1069 if (priv->bt_is_sco &&
1070 priv->bt_traffic_load == IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS)
1071 iwlagn_bt_adjust_rssi_monitor(priv, true);
1072 else
1073 iwlagn_bt_adjust_rssi_monitor(priv, false);
1074}
1075
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001076static void iwlagn_print_uartmsg(struct iwl_priv *priv,
1077 struct iwl_bt_uart_msg *uart_msg)
1078{
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001079 IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001080 "Update Req = 0x%X",
1081 (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
1082 BT_UART_MSG_FRAME1MSGTYPE_POS,
1083 (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
1084 BT_UART_MSG_FRAME1SSN_POS,
1085 (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
1086 BT_UART_MSG_FRAME1UPDATEREQ_POS);
1087
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001088 IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001089 "Chl_SeqN = 0x%X, In band = 0x%X",
1090 (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
1091 BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
1092 (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
1093 BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
1094 (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
1095 BT_UART_MSG_FRAME2CHLSEQN_POS,
1096 (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
1097 BT_UART_MSG_FRAME2INBAND_POS);
1098
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001099 IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001100 "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
1101 (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
1102 BT_UART_MSG_FRAME3SCOESCO_POS,
1103 (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
1104 BT_UART_MSG_FRAME3SNIFF_POS,
1105 (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
1106 BT_UART_MSG_FRAME3A2DP_POS,
1107 (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
1108 BT_UART_MSG_FRAME3ACL_POS,
1109 (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
1110 BT_UART_MSG_FRAME3MASTER_POS,
1111 (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
1112 BT_UART_MSG_FRAME3OBEX_POS);
1113
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001114 IWL_DEBUG_COEX(priv, "Idle duration = 0x%X",
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001115 (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
1116 BT_UART_MSG_FRAME4IDLEDURATION_POS);
1117
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001118 IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001119 "eSCO Retransmissions = 0x%X",
1120 (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
1121 BT_UART_MSG_FRAME5TXACTIVITY_POS,
1122 (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
1123 BT_UART_MSG_FRAME5RXACTIVITY_POS,
1124 (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
1125 BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
1126
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001127 IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001128 (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
1129 BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
1130 (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
1131 BT_UART_MSG_FRAME6DISCOVERABLE_POS);
1132
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001133 IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
Wey-Yi Guy399f66f2011-02-22 08:24:22 -08001134 "0x%X, Inquiry = 0x%X, Connectable = 0x%X",
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001135 (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
1136 BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
Wey-Yi Guy399f66f2011-02-22 08:24:22 -08001137 (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
1138 BT_UART_MSG_FRAME7PAGE_POS,
1139 (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
1140 BT_UART_MSG_FRAME7INQUIRY_POS,
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001141 (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
1142 BT_UART_MSG_FRAME7CONNECTABLE_POS);
1143}
1144
Wey-Yi Guy506aa152010-11-24 17:25:03 -08001145static void iwlagn_set_kill_msk(struct iwl_priv *priv,
1146 struct iwl_bt_uart_msg *uart_msg)
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001147{
Wey-Yi Guy506aa152010-11-24 17:25:03 -08001148 u8 kill_msk;
Joe Perches20407ed2010-11-20 18:38:57 -08001149 static const __le32 bt_kill_ack_msg[2] = {
Wey-Yi Guy506aa152010-11-24 17:25:03 -08001150 IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
1151 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
1152 static const __le32 bt_kill_cts_msg[2] = {
1153 IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
1154 IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001155
Wey-Yi Guy506aa152010-11-24 17:25:03 -08001156 kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
1157 ? 1 : 0;
1158 if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
1159 priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001160 priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
Wey-Yi Guy506aa152010-11-24 17:25:03 -08001161 priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
1162 priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
1163 priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
1164
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001165 /* schedule to send runtime bt_config */
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -07001166 queue_work(priv->shrd->workqueue, &priv->bt_runtime_config);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001167 }
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001168}
1169
1170void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
1171 struct iwl_rx_mem_buffer *rxb)
1172{
1173 unsigned long flags;
1174 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1175 struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001176 struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001177
Wey-Yi Guyc4197c62011-02-06 08:56:35 -08001178 if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
1179 /* bt coex disabled */
1180 return;
1181 }
1182
Wey-Yi Guyfa7f1412011-06-06 12:49:25 -07001183 IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
1184 IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
1185 IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
1186 IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001187 coex->bt_ci_compliance);
1188 iwlagn_print_uartmsg(priv, uart_msg);
1189
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08001190 priv->last_bt_traffic_load = priv->bt_traffic_load;
Meenakshi Venkataraman207ecc52011-07-08 08:46:23 -07001191 priv->bt_is_sco = iwlagn_bt_traffic_is_sco(uart_msg);
1192
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001193 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
1194 if (priv->bt_status != coex->bt_status ||
Wey-Yi Guy66e863a52010-11-08 14:54:37 -08001195 priv->last_bt_traffic_load != coex->bt_traffic_load) {
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001196 if (coex->bt_status) {
1197 /* BT on */
1198 if (!priv->bt_ch_announce)
1199 priv->bt_traffic_load =
1200 IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1201 else
1202 priv->bt_traffic_load =
1203 coex->bt_traffic_load;
1204 } else {
1205 /* BT off */
1206 priv->bt_traffic_load =
1207 IWL_BT_COEX_TRAFFIC_LOAD_NONE;
1208 }
1209 priv->bt_status = coex->bt_status;
Emmanuel Grumbach74e28e42011-08-25 23:10:41 -07001210 queue_work(priv->shrd->workqueue,
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001211 &priv->bt_traffic_change_work);
1212 }
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001213 }
1214
Wey-Yi Guy506aa152010-11-24 17:25:03 -08001215 iwlagn_set_kill_msk(priv, uart_msg);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001216
1217 /* FIXME: based on notification, adjust the prio_boost */
1218
Emmanuel Grumbach10b15e62011-08-25 23:10:43 -07001219 spin_lock_irqsave(&priv->shrd->lock, flags);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001220 priv->bt_ci_compliance = coex->bt_ci_compliance;
Emmanuel Grumbach10b15e62011-08-25 23:10:43 -07001221 spin_unlock_irqrestore(&priv->shrd->lock, flags);
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001222}
1223
1224void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
1225{
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001226 priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
1227 iwlagn_bt_coex_profile_notif;
1228}
1229
1230void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
1231{
Wey-Yi Guyb6e116e2010-08-23 07:57:14 -07001232 INIT_WORK(&priv->bt_traffic_change_work,
1233 iwlagn_bt_traffic_change_work);
1234}
1235
1236void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
1237{
1238 cancel_work_sync(&priv->bt_traffic_change_work);
1239}
Johannes Berg5de33062010-09-22 18:01:58 +02001240
1241static bool is_single_rx_stream(struct iwl_priv *priv)
1242{
1243 return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
1244 priv->current_ht_config.single_chain_sufficient;
1245}
1246
1247#define IWL_NUM_RX_CHAINS_MULTIPLE 3
1248#define IWL_NUM_RX_CHAINS_SINGLE 2
1249#define IWL_NUM_IDLE_CHAINS_DUAL 2
1250#define IWL_NUM_IDLE_CHAINS_SINGLE 1
1251
1252/*
1253 * Determine how many receiver/antenna chains to use.
1254 *
1255 * More provides better reception via diversity. Fewer saves power
1256 * at the expense of throughput, but only when not in powersave to
1257 * start with.
1258 *
1259 * MIMO (dual stream) requires at least 2, but works better with 3.
1260 * This does not determine *which* chains to use, just how many.
1261 */
1262static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
1263{
1264 if (priv->cfg->bt_params &&
1265 priv->cfg->bt_params->advanced_bt_coexist &&
1266 (priv->bt_full_concurrent ||
1267 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
1268 /*
1269 * only use chain 'A' in bt high traffic load or
1270 * full concurrency mode
1271 */
1272 return IWL_NUM_RX_CHAINS_SINGLE;
1273 }
1274 /* # of Rx chains to use when expecting MIMO. */
1275 if (is_single_rx_stream(priv))
1276 return IWL_NUM_RX_CHAINS_SINGLE;
1277 else
1278 return IWL_NUM_RX_CHAINS_MULTIPLE;
1279}
1280
1281/*
1282 * When we are in power saving mode, unless device support spatial
1283 * multiplexing power save, use the active count for rx chain count.
1284 */
1285static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
1286{
1287 /* # Rx chains when idling, depending on SMPS mode */
1288 switch (priv->current_ht_config.smps) {
1289 case IEEE80211_SMPS_STATIC:
1290 case IEEE80211_SMPS_DYNAMIC:
1291 return IWL_NUM_IDLE_CHAINS_SINGLE;
1292 case IEEE80211_SMPS_OFF:
1293 return active_cnt;
1294 default:
1295 WARN(1, "invalid SMPS mode %d",
1296 priv->current_ht_config.smps);
1297 return active_cnt;
1298 }
1299}
1300
1301/* up to 4 chains */
1302static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
1303{
1304 u8 res;
1305 res = (chain_bitmap & BIT(0)) >> 0;
1306 res += (chain_bitmap & BIT(1)) >> 1;
1307 res += (chain_bitmap & BIT(2)) >> 2;
1308 res += (chain_bitmap & BIT(3)) >> 3;
1309 return res;
1310}
1311
1312/**
1313 * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1314 *
1315 * Selects how many and which Rx receivers/antennas/chains to use.
1316 * This should not be used for scan command ... it puts data in wrong place.
1317 */
1318void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1319{
1320 bool is_single = is_single_rx_stream(priv);
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -07001321 bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->shrd->status);
Johannes Berg5de33062010-09-22 18:01:58 +02001322 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1323 u32 active_chains;
1324 u16 rx_chain;
1325
1326 /* Tell uCode which antennas are actually connected.
1327 * Before first association, we assume all antennas are connected.
1328 * Just after first association, iwl_chain_noise_calibration()
1329 * checks which antennas actually *are* connected. */
1330 if (priv->chain_noise_data.active_chains)
1331 active_chains = priv->chain_noise_data.active_chains;
1332 else
Emmanuel Grumbachd6189122011-08-25 23:10:39 -07001333 active_chains = hw_params(priv).valid_rx_ant;
Johannes Berg5de33062010-09-22 18:01:58 +02001334
1335 if (priv->cfg->bt_params &&
1336 priv->cfg->bt_params->advanced_bt_coexist &&
1337 (priv->bt_full_concurrent ||
1338 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
1339 /*
1340 * only use chain 'A' in bt high traffic load or
1341 * full concurrency mode
1342 */
1343 active_chains = first_antenna(active_chains);
1344 }
1345
1346 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1347
1348 /* How many receivers should we use? */
1349 active_rx_cnt = iwl_get_active_rx_chain_count(priv);
1350 idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
1351
1352
1353 /* correct rx chain count according hw settings
1354 * and chain noise calibration
1355 */
1356 valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
1357 if (valid_rx_cnt < active_rx_cnt)
1358 active_rx_cnt = valid_rx_cnt;
1359
1360 if (valid_rx_cnt < idle_rx_cnt)
1361 idle_rx_cnt = valid_rx_cnt;
1362
1363 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
1364 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
1365
1366 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1367
1368 if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
1369 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1370 else
1371 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1372
1373 IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
1374 ctx->staging.rx_chain,
1375 active_rx_cnt, idle_rx_cnt);
1376
1377 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1378 active_rx_cnt < idle_rx_cnt);
1379}
Johannes Bergfacd9822010-09-22 18:02:05 +02001380
1381u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
1382{
1383 int i;
1384 u8 ind = ant;
1385
1386 if (priv->band == IEEE80211_BAND_2GHZ &&
1387 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
1388 return 0;
1389
1390 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
1391 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
1392 if (valid & BIT(ind))
1393 return ind;
1394 }
1395 return ant;
1396}
Johannes Bergfed73292010-09-22 18:02:06 +02001397
Johannes Berg7194207c2011-01-04 16:22:00 -08001398/* notification wait support */
1399void iwlagn_init_notification_wait(struct iwl_priv *priv,
1400 struct iwl_notification_wait *wait_entry,
Johannes Berg09f18af2011-04-13 03:14:47 -07001401 u8 cmd,
Johannes Berg7194207c2011-01-04 16:22:00 -08001402 void (*fn)(struct iwl_priv *priv,
Johannes Berg09f18af2011-04-13 03:14:47 -07001403 struct iwl_rx_packet *pkt,
1404 void *data),
1405 void *fn_data)
Johannes Berg7194207c2011-01-04 16:22:00 -08001406{
1407 wait_entry->fn = fn;
Johannes Berg09f18af2011-04-13 03:14:47 -07001408 wait_entry->fn_data = fn_data;
Johannes Berg7194207c2011-01-04 16:22:00 -08001409 wait_entry->cmd = cmd;
1410 wait_entry->triggered = false;
Johannes Berge74fe232011-04-13 03:14:49 -07001411 wait_entry->aborted = false;
Johannes Berg7194207c2011-01-04 16:22:00 -08001412
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001413 spin_lock_bh(&priv->notif_wait_lock);
1414 list_add(&wait_entry->list, &priv->notif_waits);
1415 spin_unlock_bh(&priv->notif_wait_lock);
Johannes Berg7194207c2011-01-04 16:22:00 -08001416}
1417
Johannes Berga8674a12011-04-13 03:14:48 -07001418int iwlagn_wait_notification(struct iwl_priv *priv,
1419 struct iwl_notification_wait *wait_entry,
1420 unsigned long timeout)
Johannes Berg7194207c2011-01-04 16:22:00 -08001421{
1422 int ret;
1423
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001424 ret = wait_event_timeout(priv->notif_waitq,
Johannes Berge74fe232011-04-13 03:14:49 -07001425 wait_entry->triggered || wait_entry->aborted,
Johannes Berg7194207c2011-01-04 16:22:00 -08001426 timeout);
1427
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001428 spin_lock_bh(&priv->notif_wait_lock);
Johannes Berg7194207c2011-01-04 16:22:00 -08001429 list_del(&wait_entry->list);
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001430 spin_unlock_bh(&priv->notif_wait_lock);
Johannes Berg7194207c2011-01-04 16:22:00 -08001431
Johannes Berge74fe232011-04-13 03:14:49 -07001432 if (wait_entry->aborted)
1433 return -EIO;
1434
Johannes Berga8674a12011-04-13 03:14:48 -07001435 /* return value is always >= 0 */
1436 if (ret <= 0)
1437 return -ETIMEDOUT;
1438 return 0;
Johannes Berg7194207c2011-01-04 16:22:00 -08001439}
1440
1441void iwlagn_remove_notification(struct iwl_priv *priv,
1442 struct iwl_notification_wait *wait_entry)
1443{
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001444 spin_lock_bh(&priv->notif_wait_lock);
Johannes Berg7194207c2011-01-04 16:22:00 -08001445 list_del(&wait_entry->list);
Wey-Yi Guy898ed672011-07-13 08:38:57 -07001446 spin_unlock_bh(&priv->notif_wait_lock);
Johannes Berg7194207c2011-01-04 16:22:00 -08001447}