blob: 4d7674c0c72170530af96dcc1ad27dd4b6b2c5cf [file] [log] [blame]
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -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 Guy7dc77db2010-03-16 10:23:31 -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 *****************************************************************************/
29
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"
Wey-Yi Guy510cb792010-03-16 12:37:28 -070038#include "iwl-agn.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070039#include "iwl-trans.h"
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070040
Shanyu Zhao18089722010-05-06 10:15:21 -070041int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070042{
43 struct iwl_tx_ant_config_cmd tx_ant_cmd = {
44 .valid = cpu_to_le32(valid_tx_ant),
45 };
46
47 if (IWL_UCODE_API(priv->ucode_ver) > 1) {
48 IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070049 return trans_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -070050 TX_ANT_CONFIGURATION_CMD,
51 CMD_SYNC,
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070052 sizeof(struct iwl_tx_ant_config_cmd),
53 &tx_ant_cmd);
54 } else {
55 IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
56 return -EOPNOTSUPP;
57 }
58}
59
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070060static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
61{
62 u16 size = (u16)sizeof(struct iwl_addsta_cmd);
63 struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
64 memcpy(addsta, cmd, size);
65 /* resrved in 5000 */
66 addsta->rate_n_flags = cpu_to_le16(0);
67 return size;
68}
69
70static void iwlagn_gain_computation(struct iwl_priv *priv,
71 u32 average_noise[NUM_RX_CHAINS],
72 u16 min_average_noise_antenna_i,
73 u32 min_average_noise,
74 u8 default_chain)
75{
76 int i;
77 s32 delta_g;
78 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
79
80 /*
81 * Find Gain Code for the chains based on "default chain"
82 */
83 for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
84 if ((data->disconn_array[i])) {
85 data->delta_gain_code[i] = 0;
86 continue;
87 }
88
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -070089 delta_g = (priv->cfg->base_params->chain_noise_scale *
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070090 ((s32)average_noise[default_chain] -
91 (s32)average_noise[i])) / 1500;
92
93 /* bound gain by 2 bits value max, 3rd bit is sign */
94 data->delta_gain_code[i] =
95 min(abs(delta_g), (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
96
97 if (delta_g < 0)
98 /*
99 * set negative sign ...
100 * note to Intel developers: This is uCode API format,
101 * not the format of any internal device registers.
102 * Do not change this format for e.g. 6050 or similar
103 * devices. Change format only if more resolution
104 * (i.e. more than 2 bits magnitude) is needed.
105 */
106 data->delta_gain_code[i] |= (1 << 2);
107 }
108
109 IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
110 data->delta_gain_code[1], data->delta_gain_code[2]);
111
112 if (!data->radio_write) {
113 struct iwl_calib_chain_noise_gain_cmd cmd;
114
115 memset(&cmd, 0, sizeof(cmd));
116
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700117 iwl_set_calib_hdr(&cmd.hdr,
118 priv->_agn.phy_calib_chain_noise_gain_cmd);
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700119 cmd.delta_gain_1 = data->delta_gain_code[1];
120 cmd.delta_gain_2 = data->delta_gain_code[2];
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700121 trans_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700122 CMD_ASYNC, sizeof(cmd), &cmd);
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700123
124 data->radio_write = 1;
125 data->state = IWL_CHAIN_NOISE_CALIBRATED;
126 }
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700127}
128
129static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
130{
131 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
132 int ret;
133
Shanyu Zhaof4308442010-05-11 15:25:03 -0700134 if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
Johannes Berg246ed352010-08-23 10:46:32 +0200135 iwl_is_any_associated(priv)) {
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700136 struct iwl_calib_chain_noise_reset_cmd cmd;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700137
Shanyu Zhaof4308442010-05-11 15:25:03 -0700138 /* clear data for chain noise calibration algorithm */
139 data->chain_noise_a = 0;
140 data->chain_noise_b = 0;
141 data->chain_noise_c = 0;
142 data->chain_signal_a = 0;
143 data->chain_signal_b = 0;
144 data->chain_signal_c = 0;
145 data->beacon_count = 0;
146
147 memset(&cmd, 0, sizeof(cmd));
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700148 iwl_set_calib_hdr(&cmd.hdr,
149 priv->_agn.phy_calib_chain_noise_reset_cmd);
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700150 ret = trans_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700151 REPLY_PHY_CALIBRATION_CMD,
152 CMD_SYNC, sizeof(cmd), &cmd);
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700153 if (ret)
154 IWL_ERR(priv,
155 "Could not send REPLY_PHY_CALIBRATION_CMD\n");
156 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
157 IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
158 }
159}
160
Johannes Berg94597ab2010-08-09 10:57:02 -0700161static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
162 struct ieee80211_tx_info *info,
163 __le16 fc, __le32 *tx_flags)
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700164{
Johannes Berg94597ab2010-08-09 10:57:02 -0700165 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
Stanislaw Gruszka42b70a52011-05-26 17:14:22 +0200166 info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
167 info->flags & IEEE80211_TX_CTL_AMPDU)
Johannes Berg94597ab2010-08-09 10:57:02 -0700168 *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700169}
170
171/* Calc max signal level (dBm) among 3 possible receivers */
172static int iwlagn_calc_rssi(struct iwl_priv *priv,
173 struct iwl_rx_phy_res *rx_resp)
174{
175 /* data from PHY/DSP regarding signal strength, etc.,
176 * contents are always there, not configurable by host
177 */
Wey-Yi Guy7ccc8962010-08-04 08:42:17 -0700178 struct iwlagn_non_cfg_phy *ncphy =
179 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700180 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
181 u8 agc;
182
Wey-Yi Guy7ccc8962010-08-04 08:42:17 -0700183 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
184 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700185
186 /* Find max rssi among 3 possible receivers.
187 * These values are measured by the digital signal processor (DSP).
188 * They should stay fairly constant even as the signal strength varies,
189 * if the radio's automatic gain control (AGC) is working right.
190 * AGC value (see below) will provide the "interesting" info.
191 */
Wey-Yi Guy7ccc8962010-08-04 08:42:17 -0700192 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
193 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
194 IWLAGN_OFDM_RSSI_A_BIT_POS;
195 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
196 IWLAGN_OFDM_RSSI_B_BIT_POS;
197 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
198 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
199 IWLAGN_OFDM_RSSI_C_BIT_POS;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700200
201 max_rssi = max_t(u32, rssi_a, rssi_b);
202 max_rssi = max_t(u32, max_rssi, rssi_c);
203
204 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
205 rssi_a, rssi_b, rssi_c, max_rssi, agc);
206
207 /* dBm = max_rssi dB - agc dB - constant.
208 * Higher AGC (higher radio gain) means lower signal. */
Wey-Yi Guyb744cb72010-03-23 11:37:59 -0700209 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700210}
211
Wey-Yi Guye3f10ce2011-07-01 07:59:26 -0700212int iwlagn_set_pan_params(struct iwl_priv *priv)
Johannes Berg52a02d12010-08-27 09:44:50 -0700213{
214 struct iwl_wipan_params_cmd cmd;
215 struct iwl_rxon_context *ctx_bss, *ctx_pan;
216 int slot0 = 300, slot1 = 0;
217 int ret;
218
219 if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
220 return 0;
221
222 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
223
224 lockdep_assert_held(&priv->mutex);
225
226 ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
227 ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
228
Johannes Berg763cc3b2010-09-03 06:32:21 -0700229 /*
230 * If the PAN context is inactive, then we don't need
231 * to update the PAN parameters, the last thing we'll
232 * have done before it goes inactive is making the PAN
233 * parameters be WLAN-only.
234 */
235 if (!ctx_pan->is_active)
236 return 0;
237
Johannes Berg52a02d12010-08-27 09:44:50 -0700238 memset(&cmd, 0, sizeof(cmd));
239
240 /* only 2 slots are currently allowed */
241 cmd.num_slots = 2;
242
243 cmd.slots[0].type = 0; /* BSS */
244 cmd.slots[1].type = 1; /* PAN */
245
Johannes Berg9b9190d2011-01-06 08:07:10 -0800246 if (priv->_agn.hw_roc_channel) {
247 /* both contexts must be used for this to happen */
248 slot1 = priv->_agn.hw_roc_duration;
Johannes Berg94073912011-01-06 08:07:11 -0800249 slot0 = IWL_MIN_SLOT_TIME;
Johannes Berg9b9190d2011-01-06 08:07:10 -0800250 } else if (ctx_bss->vif && ctx_pan->vif) {
Johannes Berg52a02d12010-08-27 09:44:50 -0700251 int bcnint = ctx_pan->vif->bss_conf.beacon_int;
Johannes Bergefe54db2010-11-10 18:25:49 -0800252 int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
Johannes Berg52a02d12010-08-27 09:44:50 -0700253
254 /* should be set, but seems unused?? */
255 cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
256
257 if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
258 bcnint &&
259 bcnint != ctx_bss->vif->bss_conf.beacon_int) {
260 IWL_ERR(priv,
261 "beacon intervals don't match (%d, %d)\n",
262 ctx_bss->vif->bss_conf.beacon_int,
263 ctx_pan->vif->bss_conf.beacon_int);
264 } else
265 bcnint = max_t(int, bcnint,
266 ctx_bss->vif->bss_conf.beacon_int);
267 if (!bcnint)
Johannes Bergea196fd2010-09-03 06:30:55 -0700268 bcnint = DEFAULT_BEACON_INTERVAL;
Johannes Berg52a02d12010-08-27 09:44:50 -0700269 slot0 = bcnint / 2;
270 slot1 = bcnint - slot0;
271
272 if (test_bit(STATUS_SCAN_HW, &priv->status) ||
273 (!ctx_bss->vif->bss_conf.idle &&
274 !ctx_bss->vif->bss_conf.assoc)) {
Johannes Berg94073912011-01-06 08:07:11 -0800275 slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
276 slot1 = IWL_MIN_SLOT_TIME;
Johannes Berg52a02d12010-08-27 09:44:50 -0700277 } else if (!ctx_pan->vif->bss_conf.idle &&
Johannes Bergefe54db2010-11-10 18:25:49 -0800278 !ctx_pan->vif->bss_conf.assoc) {
Johannes Berg94073912011-01-06 08:07:11 -0800279 slot1 = bcnint * 3 - IWL_MIN_SLOT_TIME;
280 slot0 = IWL_MIN_SLOT_TIME;
Johannes Berg52a02d12010-08-27 09:44:50 -0700281 }
282 } else if (ctx_pan->vif) {
283 slot0 = 0;
284 slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
285 ctx_pan->vif->bss_conf.beacon_int;
Johannes Bergea196fd2010-09-03 06:30:55 -0700286 slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
Johannes Berg27eafdd2010-08-30 06:12:00 -0700287
288 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Johannes Berg94073912011-01-06 08:07:11 -0800289 slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
290 slot1 = IWL_MIN_SLOT_TIME;
Johannes Berg27eafdd2010-08-30 06:12:00 -0700291 }
Johannes Berg52a02d12010-08-27 09:44:50 -0700292 }
293
294 cmd.slots[0].width = cpu_to_le16(slot0);
295 cmd.slots[1].width = cpu_to_le16(slot1);
296
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700297 ret = trans_send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700298 sizeof(cmd), &cmd);
Johannes Berg52a02d12010-08-27 09:44:50 -0700299 if (ret)
300 IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
301
302 return ret;
303}
304
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700305struct iwl_hcmd_utils_ops iwlagn_hcmd_utils = {
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700306 .build_addsta_hcmd = iwlagn_build_addsta_hcmd,
307 .gain_computation = iwlagn_gain_computation,
308 .chain_noise_reset = iwlagn_chain_noise_reset,
Johannes Berg94597ab2010-08-09 10:57:02 -0700309 .tx_cmd_protection = iwlagn_tx_cmd_protection,
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700310 .calc_rssi = iwlagn_calc_rssi,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700311 .request_scan = iwlagn_request_scan,
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700312};