blob: 406bf59bd4419139b0c0882ab3b2d66bffaf100a [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"
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070039
Shanyu Zhao18089722010-05-06 10:15:21 -070040int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070041{
42 struct iwl_tx_ant_config_cmd tx_ant_cmd = {
43 .valid = cpu_to_le32(valid_tx_ant),
44 };
45
46 if (IWL_UCODE_API(priv->ucode_ver) > 1) {
47 IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
Emmanuel Grumbache419d622011-07-08 08:46:14 -070048 return priv->trans.ops->send_cmd_pdu(priv,
49 TX_ANT_CONFIGURATION_CMD,
50 CMD_SYNC,
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070051 sizeof(struct iwl_tx_ant_config_cmd),
52 &tx_ant_cmd);
53 } else {
54 IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
55 return -EOPNOTSUPP;
56 }
57}
58
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070059static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
60{
61 u16 size = (u16)sizeof(struct iwl_addsta_cmd);
62 struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
63 memcpy(addsta, cmd, size);
64 /* resrved in 5000 */
65 addsta->rate_n_flags = cpu_to_le16(0);
66 return size;
67}
68
69static void iwlagn_gain_computation(struct iwl_priv *priv,
70 u32 average_noise[NUM_RX_CHAINS],
71 u16 min_average_noise_antenna_i,
72 u32 min_average_noise,
73 u8 default_chain)
74{
75 int i;
76 s32 delta_g;
77 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
78
79 /*
80 * Find Gain Code for the chains based on "default chain"
81 */
82 for (i = default_chain + 1; i < NUM_RX_CHAINS; i++) {
83 if ((data->disconn_array[i])) {
84 data->delta_gain_code[i] = 0;
85 continue;
86 }
87
Wey-Yi Guy7cb1b082010-10-06 08:10:00 -070088 delta_g = (priv->cfg->base_params->chain_noise_scale *
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -070089 ((s32)average_noise[default_chain] -
90 (s32)average_noise[i])) / 1500;
91
92 /* bound gain by 2 bits value max, 3rd bit is sign */
93 data->delta_gain_code[i] =
94 min(abs(delta_g), (long) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
95
96 if (delta_g < 0)
97 /*
98 * set negative sign ...
99 * note to Intel developers: This is uCode API format,
100 * not the format of any internal device registers.
101 * Do not change this format for e.g. 6050 or similar
102 * devices. Change format only if more resolution
103 * (i.e. more than 2 bits magnitude) is needed.
104 */
105 data->delta_gain_code[i] |= (1 << 2);
106 }
107
108 IWL_DEBUG_CALIB(priv, "Delta gains: ANT_B = %d ANT_C = %d\n",
109 data->delta_gain_code[1], data->delta_gain_code[2]);
110
111 if (!data->radio_write) {
112 struct iwl_calib_chain_noise_gain_cmd cmd;
113
114 memset(&cmd, 0, sizeof(cmd));
115
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700116 iwl_set_calib_hdr(&cmd.hdr,
117 priv->_agn.phy_calib_chain_noise_gain_cmd);
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700118 cmd.delta_gain_1 = data->delta_gain_code[1];
119 cmd.delta_gain_2 = data->delta_gain_code[2];
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700120 priv->trans.ops->send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
121 CMD_ASYNC, sizeof(cmd), &cmd);
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700122
123 data->radio_write = 1;
124 data->state = IWL_CHAIN_NOISE_CALIBRATED;
125 }
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700126}
127
128static void iwlagn_chain_noise_reset(struct iwl_priv *priv)
129{
130 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
131 int ret;
132
Shanyu Zhaof4308442010-05-11 15:25:03 -0700133 if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
Johannes Berg246ed352010-08-23 10:46:32 +0200134 iwl_is_any_associated(priv)) {
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700135 struct iwl_calib_chain_noise_reset_cmd cmd;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700136
Shanyu Zhaof4308442010-05-11 15:25:03 -0700137 /* clear data for chain noise calibration algorithm */
138 data->chain_noise_a = 0;
139 data->chain_noise_b = 0;
140 data->chain_noise_c = 0;
141 data->chain_signal_a = 0;
142 data->chain_signal_b = 0;
143 data->chain_signal_c = 0;
144 data->beacon_count = 0;
145
146 memset(&cmd, 0, sizeof(cmd));
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700147 iwl_set_calib_hdr(&cmd.hdr,
148 priv->_agn.phy_calib_chain_noise_reset_cmd);
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700149 ret = priv->trans.ops->send_cmd_pdu(priv,
150 REPLY_PHY_CALIBRATION_CMD,
151 CMD_SYNC, sizeof(cmd), &cmd);
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700152 if (ret)
153 IWL_ERR(priv,
154 "Could not send REPLY_PHY_CALIBRATION_CMD\n");
155 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
156 IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
157 }
158}
159
Johannes Berg94597ab2010-08-09 10:57:02 -0700160static void iwlagn_tx_cmd_protection(struct iwl_priv *priv,
161 struct ieee80211_tx_info *info,
162 __le16 fc, __le32 *tx_flags)
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700163{
Johannes Berg94597ab2010-08-09 10:57:02 -0700164 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS ||
Stanislaw Gruszka42b70a52011-05-26 17:14:22 +0200165 info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT ||
166 info->flags & IEEE80211_TX_CTL_AMPDU)
Johannes Berg94597ab2010-08-09 10:57:02 -0700167 *tx_flags |= TX_CMD_FLG_PROT_REQUIRE_MSK;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700168}
169
170/* Calc max signal level (dBm) among 3 possible receivers */
171static int iwlagn_calc_rssi(struct iwl_priv *priv,
172 struct iwl_rx_phy_res *rx_resp)
173{
174 /* data from PHY/DSP regarding signal strength, etc.,
175 * contents are always there, not configurable by host
176 */
Wey-Yi Guy7ccc8962010-08-04 08:42:17 -0700177 struct iwlagn_non_cfg_phy *ncphy =
178 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700179 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
180 u8 agc;
181
Wey-Yi Guy7ccc8962010-08-04 08:42:17 -0700182 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
183 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700184
185 /* Find max rssi among 3 possible receivers.
186 * These values are measured by the digital signal processor (DSP).
187 * They should stay fairly constant even as the signal strength varies,
188 * if the radio's automatic gain control (AGC) is working right.
189 * AGC value (see below) will provide the "interesting" info.
190 */
Wey-Yi Guy7ccc8962010-08-04 08:42:17 -0700191 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
192 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
193 IWLAGN_OFDM_RSSI_A_BIT_POS;
194 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
195 IWLAGN_OFDM_RSSI_B_BIT_POS;
196 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
197 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
198 IWLAGN_OFDM_RSSI_C_BIT_POS;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700199
200 max_rssi = max_t(u32, rssi_a, rssi_b);
201 max_rssi = max_t(u32, max_rssi, rssi_c);
202
203 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
204 rssi_a, rssi_b, rssi_c, max_rssi, agc);
205
206 /* dBm = max_rssi dB - agc dB - constant.
207 * Higher AGC (higher radio gain) means lower signal. */
Wey-Yi Guyb744cb72010-03-23 11:37:59 -0700208 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700209}
210
Wey-Yi Guye3f10ce2011-07-01 07:59:26 -0700211int iwlagn_set_pan_params(struct iwl_priv *priv)
Johannes Berg52a02d12010-08-27 09:44:50 -0700212{
213 struct iwl_wipan_params_cmd cmd;
214 struct iwl_rxon_context *ctx_bss, *ctx_pan;
215 int slot0 = 300, slot1 = 0;
216 int ret;
217
218 if (priv->valid_contexts == BIT(IWL_RXON_CTX_BSS))
219 return 0;
220
221 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
222
223 lockdep_assert_held(&priv->mutex);
224
225 ctx_bss = &priv->contexts[IWL_RXON_CTX_BSS];
226 ctx_pan = &priv->contexts[IWL_RXON_CTX_PAN];
227
Johannes Berg763cc3b2010-09-03 06:32:21 -0700228 /*
229 * If the PAN context is inactive, then we don't need
230 * to update the PAN parameters, the last thing we'll
231 * have done before it goes inactive is making the PAN
232 * parameters be WLAN-only.
233 */
234 if (!ctx_pan->is_active)
235 return 0;
236
Johannes Berg52a02d12010-08-27 09:44:50 -0700237 memset(&cmd, 0, sizeof(cmd));
238
239 /* only 2 slots are currently allowed */
240 cmd.num_slots = 2;
241
242 cmd.slots[0].type = 0; /* BSS */
243 cmd.slots[1].type = 1; /* PAN */
244
Johannes Berg9b9190d2011-01-06 08:07:10 -0800245 if (priv->_agn.hw_roc_channel) {
246 /* both contexts must be used for this to happen */
247 slot1 = priv->_agn.hw_roc_duration;
Johannes Berg94073912011-01-06 08:07:11 -0800248 slot0 = IWL_MIN_SLOT_TIME;
Johannes Berg9b9190d2011-01-06 08:07:10 -0800249 } else if (ctx_bss->vif && ctx_pan->vif) {
Johannes Berg52a02d12010-08-27 09:44:50 -0700250 int bcnint = ctx_pan->vif->bss_conf.beacon_int;
Johannes Bergefe54db2010-11-10 18:25:49 -0800251 int dtim = ctx_pan->vif->bss_conf.dtim_period ?: 1;
Johannes Berg52a02d12010-08-27 09:44:50 -0700252
253 /* should be set, but seems unused?? */
254 cmd.flags |= cpu_to_le16(IWL_WIPAN_PARAMS_FLG_SLOTTED_MODE);
255
256 if (ctx_pan->vif->type == NL80211_IFTYPE_AP &&
257 bcnint &&
258 bcnint != ctx_bss->vif->bss_conf.beacon_int) {
259 IWL_ERR(priv,
260 "beacon intervals don't match (%d, %d)\n",
261 ctx_bss->vif->bss_conf.beacon_int,
262 ctx_pan->vif->bss_conf.beacon_int);
263 } else
264 bcnint = max_t(int, bcnint,
265 ctx_bss->vif->bss_conf.beacon_int);
266 if (!bcnint)
Johannes Bergea196fd2010-09-03 06:30:55 -0700267 bcnint = DEFAULT_BEACON_INTERVAL;
Johannes Berg52a02d12010-08-27 09:44:50 -0700268 slot0 = bcnint / 2;
269 slot1 = bcnint - slot0;
270
271 if (test_bit(STATUS_SCAN_HW, &priv->status) ||
272 (!ctx_bss->vif->bss_conf.idle &&
273 !ctx_bss->vif->bss_conf.assoc)) {
Johannes Berg94073912011-01-06 08:07:11 -0800274 slot0 = dtim * bcnint * 3 - IWL_MIN_SLOT_TIME;
275 slot1 = IWL_MIN_SLOT_TIME;
Johannes Berg52a02d12010-08-27 09:44:50 -0700276 } else if (!ctx_pan->vif->bss_conf.idle &&
Johannes Bergefe54db2010-11-10 18:25:49 -0800277 !ctx_pan->vif->bss_conf.assoc) {
Johannes Berg94073912011-01-06 08:07:11 -0800278 slot1 = bcnint * 3 - IWL_MIN_SLOT_TIME;
279 slot0 = IWL_MIN_SLOT_TIME;
Johannes Berg52a02d12010-08-27 09:44:50 -0700280 }
281 } else if (ctx_pan->vif) {
282 slot0 = 0;
283 slot1 = max_t(int, 1, ctx_pan->vif->bss_conf.dtim_period) *
284 ctx_pan->vif->bss_conf.beacon_int;
Johannes Bergea196fd2010-09-03 06:30:55 -0700285 slot1 = max_t(int, DEFAULT_BEACON_INTERVAL, slot1);
Johannes Berg27eafdd2010-08-30 06:12:00 -0700286
287 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
Johannes Berg94073912011-01-06 08:07:11 -0800288 slot0 = slot1 * 3 - IWL_MIN_SLOT_TIME;
289 slot1 = IWL_MIN_SLOT_TIME;
Johannes Berg27eafdd2010-08-30 06:12:00 -0700290 }
Johannes Berg52a02d12010-08-27 09:44:50 -0700291 }
292
293 cmd.slots[0].width = cpu_to_le16(slot0);
294 cmd.slots[1].width = cpu_to_le16(slot1);
295
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700296 ret = priv->trans.ops->send_cmd_pdu(priv, REPLY_WIPAN_PARAMS, CMD_SYNC,
297 sizeof(cmd), &cmd);
Johannes Berg52a02d12010-08-27 09:44:50 -0700298 if (ret)
299 IWL_ERR(priv, "Error setting PAN parameters (%d)\n", ret);
300
301 return ret;
302}
303
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700304struct iwl_hcmd_utils_ops iwlagn_hcmd_utils = {
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700305 .build_addsta_hcmd = iwlagn_build_addsta_hcmd,
306 .gain_computation = iwlagn_gain_computation,
307 .chain_noise_reset = iwlagn_chain_noise_reset,
Johannes Berg94597ab2010-08-09 10:57:02 -0700308 .tx_cmd_protection = iwlagn_tx_cmd_protection,
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700309 .calc_rssi = iwlagn_calc_rssi,
Johannes Bergb6e4c552010-04-06 04:12:42 -0700310 .request_scan = iwlagn_request_scan,
Wey-Yi Guy7dc77db2010-03-16 10:23:31 -0700311};