blob: e80f6022cf43a4e4f8ed9a14d2e51b940632e93e [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/version.h>
30#include <linux/init.h>
31#include <linux/pci.h>
32#include <linux/dma-mapping.h>
33#include <linux/delay.h>
34#include <linux/skbuff.h>
35#include <linux/netdevice.h>
36#include <linux/wireless.h>
37#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070038#include <linux/etherdevice.h>
Zhu Yi12342c42007-12-20 11:27:32 +080039#include <asm/unaligned.h>
Zhu Yib481de92007-09-25 17:54:57 -070040
Assaf Krauss6bc913b2008-03-11 16:17:18 -070041#include "iwl-eeprom.h"
Zhu Yib481de92007-09-25 17:54:57 -070042#include "iwl-4965.h"
Tomas Winklerfee12472008-04-03 16:05:21 -070043#include "iwl-core.h"
Tomas Winkler3395f6e2008-03-25 16:33:37 -070044#include "iwl-io.h"
Zhu Yib481de92007-09-25 17:54:57 -070045#include "iwl-helpers.h"
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -070046#include "iwl-calib.h"
Zhu Yib481de92007-09-25 17:54:57 -070047
Assaf Krauss1ea87392008-03-18 14:57:50 -070048/* module parameters */
49static struct iwl_mod_params iwl4965_mod_params = {
Ron Rindjunskydfe7d452008-04-15 16:01:45 -070050 .num_of_queues = IWL4965_MAX_NUM_QUEUES,
Assaf Krauss1ea87392008-03-18 14:57:50 -070051 .enable_qos = 1,
52 .amsdu_size_8K = 1,
53 /* the rest are 0 by default */
54};
55
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070056static void iwl4965_hw_card_show_info(struct iwl_priv *priv);
Christoph Hellwig416e1432007-10-25 17:15:49 +080057
Zhu Yib481de92007-09-25 17:54:57 -070058#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
59 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
60 IWL_RATE_SISO_##s##M_PLCP, \
Guy Cohenfde0db32008-04-21 15:42:01 -070061 IWL_RATE_MIMO2_##s##M_PLCP,\
62 IWL_RATE_MIMO3_##s##M_PLCP,\
Zhu Yib481de92007-09-25 17:54:57 -070063 IWL_RATE_##r##M_IEEE, \
64 IWL_RATE_##ip##M_INDEX, \
65 IWL_RATE_##in##M_INDEX, \
66 IWL_RATE_##rp##M_INDEX, \
67 IWL_RATE_##rn##M_INDEX, \
68 IWL_RATE_##pp##M_INDEX, \
69 IWL_RATE_##np##M_INDEX }
70
71/*
72 * Parameter order:
73 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
74 *
75 * If there isn't a valid next or previous rate then INV is used which
76 * maps to IWL_RATE_INVALID
77 *
78 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080079const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -070080 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
81 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
82 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
83 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
84 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
85 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
86 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
87 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
88 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
89 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
90 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
91 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
92 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
Guy Cohenfde0db32008-04-21 15:42:01 -070093 /* FIXME:RS: ^^ should be INV (legacy) */
Zhu Yib481de92007-09-25 17:54:57 -070094};
95
Ron Rindjunskyfe01b472008-01-28 14:07:24 +020096#ifdef CONFIG_IWL4965_HT
97
98static const u16 default_tid_to_tx_fifo[] = {
99 IWL_TX_FIFO_AC1,
100 IWL_TX_FIFO_AC0,
101 IWL_TX_FIFO_AC0,
102 IWL_TX_FIFO_AC1,
103 IWL_TX_FIFO_AC2,
104 IWL_TX_FIFO_AC2,
105 IWL_TX_FIFO_AC3,
106 IWL_TX_FIFO_AC3,
107 IWL_TX_FIFO_NONE,
108 IWL_TX_FIFO_NONE,
109 IWL_TX_FIFO_NONE,
110 IWL_TX_FIFO_NONE,
111 IWL_TX_FIFO_NONE,
112 IWL_TX_FIFO_NONE,
113 IWL_TX_FIFO_NONE,
114 IWL_TX_FIFO_NONE,
115 IWL_TX_FIFO_AC3
116};
117
118#endif /*CONFIG_IWL4965_HT */
119
Tomas Winkler57aab752008-04-14 21:16:03 -0700120/* check contents of special bootstrap uCode SRAM */
121static int iwl4965_verify_bsm(struct iwl_priv *priv)
122{
123 __le32 *image = priv->ucode_boot.v_addr;
124 u32 len = priv->ucode_boot.len;
125 u32 reg;
126 u32 val;
127
128 IWL_DEBUG_INFO("Begin verify bsm\n");
129
130 /* verify BSM SRAM contents */
131 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
132 for (reg = BSM_SRAM_LOWER_BOUND;
133 reg < BSM_SRAM_LOWER_BOUND + len;
134 reg += sizeof(u32), image++) {
135 val = iwl_read_prph(priv, reg);
136 if (val != le32_to_cpu(*image)) {
137 IWL_ERROR("BSM uCode verification failed at "
138 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
139 BSM_SRAM_LOWER_BOUND,
140 reg - BSM_SRAM_LOWER_BOUND, len,
141 val, le32_to_cpu(*image));
142 return -EIO;
143 }
144 }
145
146 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
147
148 return 0;
149}
150
151/**
152 * iwl4965_load_bsm - Load bootstrap instructions
153 *
154 * BSM operation:
155 *
156 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
157 * in special SRAM that does not power down during RFKILL. When powering back
158 * up after power-saving sleeps (or during initial uCode load), the BSM loads
159 * the bootstrap program into the on-board processor, and starts it.
160 *
161 * The bootstrap program loads (via DMA) instructions and data for a new
162 * program from host DRAM locations indicated by the host driver in the
163 * BSM_DRAM_* registers. Once the new program is loaded, it starts
164 * automatically.
165 *
166 * When initializing the NIC, the host driver points the BSM to the
167 * "initialize" uCode image. This uCode sets up some internal data, then
168 * notifies host via "initialize alive" that it is complete.
169 *
170 * The host then replaces the BSM_DRAM_* pointer values to point to the
171 * normal runtime uCode instructions and a backup uCode data cache buffer
172 * (filled initially with starting data values for the on-board processor),
173 * then triggers the "initialize" uCode to load and launch the runtime uCode,
174 * which begins normal operation.
175 *
176 * When doing a power-save shutdown, runtime uCode saves data SRAM into
177 * the backup data cache in DRAM before SRAM is powered down.
178 *
179 * When powering back up, the BSM loads the bootstrap program. This reloads
180 * the runtime uCode instructions and the backup data cache into SRAM,
181 * and re-launches the runtime uCode from where it left off.
182 */
183static int iwl4965_load_bsm(struct iwl_priv *priv)
184{
185 __le32 *image = priv->ucode_boot.v_addr;
186 u32 len = priv->ucode_boot.len;
187 dma_addr_t pinst;
188 dma_addr_t pdata;
189 u32 inst_len;
190 u32 data_len;
191 int i;
192 u32 done;
193 u32 reg_offset;
194 int ret;
195
196 IWL_DEBUG_INFO("Begin load bsm\n");
197
198 /* make sure bootstrap program is no larger than BSM's SRAM size */
199 if (len > IWL_MAX_BSM_SIZE)
200 return -EINVAL;
201
202 /* Tell bootstrap uCode where to find the "Initialize" uCode
203 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
204 * NOTE: iwl4965_initialize_alive_start() will replace these values,
205 * after the "initialize" uCode has run, to point to
206 * runtime/protocol instructions and backup data cache. */
207 pinst = priv->ucode_init.p_addr >> 4;
208 pdata = priv->ucode_init_data.p_addr >> 4;
209 inst_len = priv->ucode_init.len;
210 data_len = priv->ucode_init_data.len;
211
212 ret = iwl_grab_nic_access(priv);
213 if (ret)
214 return ret;
215
216 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
217 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
218 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
219 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
220
221 /* Fill BSM memory with bootstrap instructions */
222 for (reg_offset = BSM_SRAM_LOWER_BOUND;
223 reg_offset < BSM_SRAM_LOWER_BOUND + len;
224 reg_offset += sizeof(u32), image++)
225 _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
226
227 ret = iwl4965_verify_bsm(priv);
228 if (ret) {
229 iwl_release_nic_access(priv);
230 return ret;
231 }
232
233 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
234 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
235 iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
236 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
237
238 /* Load bootstrap code into instruction SRAM now,
239 * to prepare to load "initialize" uCode */
240 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
241
242 /* Wait for load of bootstrap uCode to finish */
243 for (i = 0; i < 100; i++) {
244 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
245 if (!(done & BSM_WR_CTRL_REG_BIT_START))
246 break;
247 udelay(10);
248 }
249 if (i < 100)
250 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
251 else {
252 IWL_ERROR("BSM write did not complete!\n");
253 return -EIO;
254 }
255
256 /* Enable future boot loads whenever power management unit triggers it
257 * (e.g. when powering back up after power-save shutdown) */
258 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
259
260 iwl_release_nic_access(priv);
261
262 return 0;
263}
264
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700265static int iwl4965_init_drv(struct iwl_priv *priv)
266{
267 int ret;
268 int i;
269
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700270 priv->retry_rate = 1;
271 priv->ibss_beacon = NULL;
272
273 spin_lock_init(&priv->lock);
274 spin_lock_init(&priv->power_data.lock);
275 spin_lock_init(&priv->sta_lock);
276 spin_lock_init(&priv->hcmd_lock);
277 spin_lock_init(&priv->lq_mngr.lock);
278
279 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
280 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
281
282 INIT_LIST_HEAD(&priv->free_frames);
283
284 mutex_init(&priv->mutex);
285
286 /* Clear the driver's (not device's) station table */
287 iwlcore_clear_stations_table(priv);
288
289 priv->data_retry_limit = -1;
290 priv->ieee_channels = NULL;
291 priv->ieee_rates = NULL;
292 priv->band = IEEE80211_BAND_2GHZ;
293
294 priv->iw_mode = IEEE80211_IF_TYPE_STA;
295
296 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700297 priv->ps_mode = IWL_MIMO_PS_NONE;
298
299 /* Choose which receivers/antennas to use */
300 iwl4965_set_rxon_chain(priv);
301
302 iwlcore_reset_qos(priv);
303
304 priv->qos_data.qos_active = 0;
305 priv->qos_data.qos_cap.val = 0;
306
307 iwlcore_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
308
309 priv->rates_mask = IWL_RATES_MASK;
310 /* If power management is turned on, default to AC mode */
311 priv->power_mode = IWL_POWER_AC;
312 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
313
314 ret = iwl_init_channel_map(priv);
315 if (ret) {
316 IWL_ERROR("initializing regulatory failed: %d\n", ret);
317 goto err;
318 }
319
320 ret = iwl4965_init_geos(priv);
321 if (ret) {
322 IWL_ERROR("initializing geos failed: %d\n", ret);
323 goto err_free_channel_map;
324 }
325
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700326 ret = ieee80211_register_hw(priv->hw);
327 if (ret) {
328 IWL_ERROR("Failed to register network device (error %d)\n",
329 ret);
330 goto err_free_geos;
331 }
332
333 priv->hw->conf.beacon_int = 100;
334 priv->mac80211_registered = 1;
335
336 return 0;
337
338err_free_geos:
339 iwl4965_free_geos(priv);
340err_free_channel_map:
341 iwl_free_channel_map(priv);
342err:
343 return ret;
344}
345
Zhu Yib481de92007-09-25 17:54:57 -0700346static int is_fat_channel(__le32 rxon_flags)
347{
348 return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
349 (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
350}
351
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800352#ifdef CONFIG_IWL4965_HT
Guy Cohenfde0db32008-04-21 15:42:01 -0700353static u8 is_single_rx_stream(struct iwl_priv *priv)
354{
355 return !priv->current_ht_config.is_ht ||
356 ((priv->current_ht_config.supp_mcs_set[1] == 0) &&
357 (priv->current_ht_config.supp_mcs_set[2] == 0)) ||
358 priv->ps_mode == IWL_MIMO_PS_STATIC;
Zhu Yib481de92007-09-25 17:54:57 -0700359}
Guy Cohenfde0db32008-04-21 15:42:01 -0700360#else
361static inline u8 is_single_rx_stream(struct iwl_priv *priv)
362{
363 return 1;
364}
365#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700366
Tomas Winkler17744ff2008-03-02 01:52:00 +0200367int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
368{
369 int idx = 0;
370
371 /* 4965 HT rate format */
372 if (rate_n_flags & RATE_MCS_HT_MSK) {
373 idx = (rate_n_flags & 0xff);
374
Guy Cohenfde0db32008-04-21 15:42:01 -0700375 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
376 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
Tomas Winkler17744ff2008-03-02 01:52:00 +0200377
378 idx += IWL_FIRST_OFDM_RATE;
379 /* skip 9M not supported in ht*/
380 if (idx >= IWL_RATE_9M_INDEX)
381 idx += 1;
382 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
383 return idx;
384
385 /* 4965 legacy rate format, search for match in table */
386 } else {
387 for (idx = 0; idx < ARRAY_SIZE(iwl4965_rates); idx++)
388 if (iwl4965_rates[idx].plcp == (rate_n_flags & 0xFF))
389 return idx;
390 }
391
392 return -1;
393}
394
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800395/**
396 * translate ucode response to mac80211 tx status control values
397 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700398void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800399 struct ieee80211_tx_control *control)
400{
401 int rate_index;
402
403 control->antenna_sel_tx =
Guy Cohenfde0db32008-04-21 15:42:01 -0700404 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800405 if (rate_n_flags & RATE_MCS_HT_MSK)
406 control->flags |= IEEE80211_TXCTL_OFDM_HT;
407 if (rate_n_flags & RATE_MCS_GF_MSK)
408 control->flags |= IEEE80211_TXCTL_GREEN_FIELD;
409 if (rate_n_flags & RATE_MCS_FAT_MSK)
410 control->flags |= IEEE80211_TXCTL_40_MHZ_WIDTH;
411 if (rate_n_flags & RATE_MCS_DUP_MSK)
412 control->flags |= IEEE80211_TXCTL_DUP_DATA;
413 if (rate_n_flags & RATE_MCS_SGI_MSK)
414 control->flags |= IEEE80211_TXCTL_SHORT_GI;
415 /* since iwl4965_hwrate_to_plcp_idx is band indifferent, we always use
416 * IEEE80211_BAND_2GHZ band as it contains all the rates */
417 rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
418 if (rate_index == -1)
419 control->tx_rate = NULL;
420 else
421 control->tx_rate =
422 &priv->bands[IEEE80211_BAND_2GHZ].bitrates[rate_index];
423}
Tomas Winkler17744ff2008-03-02 01:52:00 +0200424
Zhu Yib481de92007-09-25 17:54:57 -0700425/*
426 * Determine how many receiver/antenna chains to use.
427 * More provides better reception via diversity. Fewer saves power.
428 * MIMO (dual stream) requires at least 2, but works better with 3.
429 * This does not determine *which* chains to use, just how many.
430 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700431static int iwl4965_get_rx_chain_counter(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700432 u8 *idle_state, u8 *rx_state)
433{
Guy Cohenfde0db32008-04-21 15:42:01 -0700434 u8 is_single = is_single_rx_stream(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700435 u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
436
437 /* # of Rx chains to use when expecting MIMO. */
438 if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
439 *rx_state = 2;
440 else
441 *rx_state = 3;
442
443 /* # Rx chains when idling and maybe trying to save power */
444 switch (priv->ps_mode) {
445 case IWL_MIMO_PS_STATIC:
446 case IWL_MIMO_PS_DYNAMIC:
447 *idle_state = (is_cam) ? 2 : 1;
448 break;
449 case IWL_MIMO_PS_NONE:
450 *idle_state = (is_cam) ? *rx_state : 1;
451 break;
452 default:
453 *idle_state = 1;
454 break;
455 }
456
457 return 0;
458}
459
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700460int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700461{
462 int rc;
463 unsigned long flags;
464
465 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700466 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700467 if (rc) {
468 spin_unlock_irqrestore(&priv->lock, flags);
469 return rc;
470 }
471
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800472 /* stop Rx DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700473 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
474 rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
Zhu Yib481de92007-09-25 17:54:57 -0700475 (1 << 24), 1000);
476 if (rc < 0)
477 IWL_ERROR("Can't stop Rx DMA.\n");
478
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700479 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700480 spin_unlock_irqrestore(&priv->lock, flags);
481
482 return 0;
483}
484
Tomas Winkler8614f362008-04-23 17:14:55 -0700485/*
486 * EEPROM handlers
487 */
488
489static int iwl4965_eeprom_check_version(struct iwl_priv *priv)
490{
491 u16 eeprom_ver;
492 u16 calib_ver;
493
494 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
495
496 calib_ver = iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
497
498 if (eeprom_ver < EEPROM_4965_EEPROM_VERSION ||
499 calib_ver < EEPROM_4965_TX_POWER_VERSION)
500 goto err;
501
502 return 0;
503err:
504 IWL_ERROR("Unsuported EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
505 eeprom_ver, EEPROM_4965_EEPROM_VERSION,
506 calib_ver, EEPROM_4965_TX_POWER_VERSION);
507 return -EINVAL;
508
509}
Tomas Winkler079a2532008-04-17 16:03:39 -0700510int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
Zhu Yib481de92007-09-25 17:54:57 -0700511{
Tomas Winklerd8609652007-10-25 17:15:35 +0800512 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700513 unsigned long flags;
514
515 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700516 ret = iwl_grab_nic_access(priv);
Tomas Winklerd8609652007-10-25 17:15:35 +0800517 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700518 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winklerd8609652007-10-25 17:15:35 +0800519 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700520 }
521
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700522 if (src == IWL_PWR_SRC_VAUX) {
Zhu Yib481de92007-09-25 17:54:57 -0700523 u32 val;
Tomas Winklerd8609652007-10-25 17:15:35 +0800524 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700525 &val);
Zhu Yib481de92007-09-25 17:54:57 -0700526
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700527 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700528 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700529 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
530 ~APMG_PS_CTRL_MSK_PWR_SRC);
531 }
532 } else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700533 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700534 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
535 ~APMG_PS_CTRL_MSK_PWR_SRC);
536 }
Zhu Yib481de92007-09-25 17:54:57 -0700537
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700538 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700539 spin_unlock_irqrestore(&priv->lock, flags);
540
Tomas Winklerd8609652007-10-25 17:15:35 +0800541 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700542}
543
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700544static int iwl4965_rx_init(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -0700545{
Tomas Winkler059ff822008-04-14 21:16:14 -0700546 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700547 unsigned long flags;
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200548 unsigned int rb_size;
Zhu Yib481de92007-09-25 17:54:57 -0700549
550 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler059ff822008-04-14 21:16:14 -0700551 ret = iwl_grab_nic_access(priv);
552 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700553 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler059ff822008-04-14 21:16:14 -0700554 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700555 }
556
Assaf Krauss1ea87392008-03-18 14:57:50 -0700557 if (priv->cfg->mod_params->amsdu_size_8K)
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200558 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
559 else
560 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
561
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800562 /* Stop Rx DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700563 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700564
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800565 /* Reset driver's Rx queue write index */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700566 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800567
568 /* Tell device where to find RBD circular buffer in DRAM */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700569 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
570 rxq->dma_addr >> 8);
Zhu Yib481de92007-09-25 17:54:57 -0700571
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800572 /* Tell device where in DRAM to update its Rx status */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700573 iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
Tomas Winkler059ff822008-04-14 21:16:14 -0700574 (priv->shared_phys +
575 offsetof(struct iwl4965_shared, rb_closed)) >> 4);
Zhu Yib481de92007-09-25 17:54:57 -0700576
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800577 /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700578 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
579 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
580 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
581 rb_size |
Tomas Winkler059ff822008-04-14 21:16:14 -0700582 /* 0x10 << 4 | */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700583 (RX_QUEUE_SIZE_LOG <<
Zhu Yib481de92007-09-25 17:54:57 -0700584 FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
585
586 /*
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700587 * iwl_write32(priv,CSR_INT_COAL_REG,0);
Zhu Yib481de92007-09-25 17:54:57 -0700588 */
589
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700590 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700591 spin_unlock_irqrestore(&priv->lock, flags);
592
593 return 0;
594}
595
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800596/* Tell 4965 where to find the "keep warm" buffer */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700597static int iwl4965_kw_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700598{
599 unsigned long flags;
600 int rc;
601
602 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700603 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700604 if (rc)
605 goto out;
606
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700607 iwl_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
Zhu Yib481de92007-09-25 17:54:57 -0700608 priv->kw.dma_addr >> 4);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700609 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700610out:
611 spin_unlock_irqrestore(&priv->lock, flags);
612 return rc;
613}
614
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700615static int iwl4965_kw_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700616{
617 struct pci_dev *dev = priv->pci_dev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800618 struct iwl4965_kw *kw = &priv->kw;
Zhu Yib481de92007-09-25 17:54:57 -0700619
620 kw->size = IWL4965_KW_SIZE; /* TBW need set somewhere else */
621 kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
622 if (!kw->v_addr)
623 return -ENOMEM;
624
625 return 0;
626}
627
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800628/**
629 * iwl4965_kw_free - Free the "keep warm" buffer
630 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700631static void iwl4965_kw_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700632{
633 struct pci_dev *dev = priv->pci_dev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800634 struct iwl4965_kw *kw = &priv->kw;
Zhu Yib481de92007-09-25 17:54:57 -0700635
636 if (kw->v_addr) {
637 pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
638 memset(kw, 0, sizeof(*kw));
639 }
640}
641
642/**
643 * iwl4965_txq_ctx_reset - Reset TX queue context
644 * Destroys all DMA structures and initialise them again
645 *
646 * @param priv
647 * @return error code
648 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700649static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700650{
651 int rc = 0;
652 int txq_id, slots_num;
653 unsigned long flags;
654
655 iwl4965_kw_free(priv);
656
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800657 /* Free all tx/cmd queues and keep-warm buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800658 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700659
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800660 /* Alloc keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700661 rc = iwl4965_kw_alloc(priv);
662 if (rc) {
663 IWL_ERROR("Keep Warm allocation failed");
664 goto error_kw;
665 }
666
667 spin_lock_irqsave(&priv->lock, flags);
668
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700669 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700670 if (unlikely(rc)) {
671 IWL_ERROR("TX reset failed");
672 spin_unlock_irqrestore(&priv->lock, flags);
673 goto error_reset;
674 }
675
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800676 /* Turn off all Tx DMA channels */
Tomas Winkler12a81f62008-04-03 16:05:20 -0700677 iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700678 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700679 spin_unlock_irqrestore(&priv->lock, flags);
680
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800681 /* Tell 4965 where to find the keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700682 rc = iwl4965_kw_init(priv);
683 if (rc) {
684 IWL_ERROR("kw_init failed\n");
685 goto error_reset;
686 }
687
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800688 /* Alloc and init all (default 16) Tx queues,
689 * including the command queue (#4) */
Tomas Winkler5425e492008-04-15 16:01:38 -0700690 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
Zhu Yib481de92007-09-25 17:54:57 -0700691 slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
692 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800693 rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
Zhu Yib481de92007-09-25 17:54:57 -0700694 txq_id);
695 if (rc) {
696 IWL_ERROR("Tx %d queue init failed\n", txq_id);
697 goto error;
698 }
699 }
700
701 return rc;
702
703 error:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800704 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700705 error_reset:
706 iwl4965_kw_free(priv);
707 error_kw:
708 return rc;
709}
Tomas Winkler91238712008-04-23 17:14:53 -0700710static int iwl4965_apm_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700711{
Zhu Yib481de92007-09-25 17:54:57 -0700712 unsigned long flags;
Tomas Winkler91238712008-04-23 17:14:53 -0700713 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700714
Zhu Yib481de92007-09-25 17:54:57 -0700715 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700716 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler91238712008-04-23 17:14:53 -0700717 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yib481de92007-09-25 17:54:57 -0700718
Tomas Winkler91238712008-04-23 17:14:53 -0700719 /* set "initialization complete" bit to move adapter
720 * D0U* --> D0A* state */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700721 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
Tomas Winkler91238712008-04-23 17:14:53 -0700722
723 /* wait for clock stabilization */
724 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
725 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
726 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
727 if (ret < 0) {
Zhu Yib481de92007-09-25 17:54:57 -0700728 IWL_DEBUG_INFO("Failed to init the card\n");
Tomas Winkler91238712008-04-23 17:14:53 -0700729 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700730 }
731
Tomas Winkler91238712008-04-23 17:14:53 -0700732 ret = iwl_grab_nic_access(priv);
733 if (ret)
734 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700735
Tomas Winkler91238712008-04-23 17:14:53 -0700736 /* enable DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700737 iwl_write_prph(priv, APMG_CLK_CTRL_REG,
738 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -0700739
740 udelay(20);
741
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700742 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
Tomas Winkler91238712008-04-23 17:14:53 -0700743 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Zhu Yib481de92007-09-25 17:54:57 -0700744
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700745 iwl_release_nic_access(priv);
Tomas Winkler91238712008-04-23 17:14:53 -0700746out:
747 spin_unlock_irqrestore(&priv->lock, flags);
748 return ret;
749}
750
751int iwl4965_hw_nic_init(struct iwl_priv *priv)
752{
753 unsigned long flags;
754 struct iwl4965_rx_queue *rxq = &priv->rxq;
Tomas Winkler91238712008-04-23 17:14:53 -0700755 u8 val_link;
756 u32 val;
757 int ret;
758
759 /* nic_init */
760 priv->cfg->ops->lib->apm_ops.init(priv);
761
762 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700763 iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
Zhu Yib481de92007-09-25 17:54:57 -0700764 spin_unlock_irqrestore(&priv->lock, flags);
765
Tomas Winkler91238712008-04-23 17:14:53 -0700766 ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700767
Zhu Yib481de92007-09-25 17:54:57 -0700768 spin_lock_irqsave(&priv->lock, flags);
769
Tomas Winklerb661c812008-04-23 17:14:54 -0700770 if ((priv->rev_id & 0x80) == 0x80 && (priv->rev_id & 0x7f) < 8) {
Zhu Yib481de92007-09-25 17:54:57 -0700771 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
772 /* Enable No Snoop field */
773 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
774 val & ~(1 << 11));
775 }
776
777 spin_unlock_irqrestore(&priv->lock, flags);
778
Zhu Yib481de92007-09-25 17:54:57 -0700779 pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
780
781 /* disable L1 entry -- workaround for pre-B1 */
782 pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
783
784 spin_lock_irqsave(&priv->lock, flags);
785
786 /* set CSR_HW_CONFIG_REG for uCode use */
787
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700788 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
789 CSR49_HW_IF_CONFIG_REG_BIT_4965_R |
790 CSR49_HW_IF_CONFIG_REG_BIT_RADIO_SI |
791 CSR49_HW_IF_CONFIG_REG_BIT_MAC_SI);
Zhu Yib481de92007-09-25 17:54:57 -0700792
Tomas Winkler91238712008-04-23 17:14:53 -0700793 ret = iwl_grab_nic_access(priv);
794 if (ret < 0) {
Zhu Yib481de92007-09-25 17:54:57 -0700795 spin_unlock_irqrestore(&priv->lock, flags);
796 IWL_DEBUG_INFO("Failed to init the card\n");
Tomas Winkler91238712008-04-23 17:14:53 -0700797 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700798 }
799
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700800 iwl_read_prph(priv, APMG_PS_CTRL_REG);
801 iwl_set_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
Zhu Yib481de92007-09-25 17:54:57 -0700802 udelay(5);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700803 iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
Zhu Yib481de92007-09-25 17:54:57 -0700804
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700805 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700806 spin_unlock_irqrestore(&priv->lock, flags);
807
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800808 iwl4965_hw_card_show_info(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700809
810 /* end nic_init */
811
812 /* Allocate the RX queue, or reset if it is already allocated */
813 if (!rxq->bd) {
Tomas Winkler91238712008-04-23 17:14:53 -0700814 ret = iwl4965_rx_queue_alloc(priv);
815 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700816 IWL_ERROR("Unable to initialize Rx queue\n");
817 return -ENOMEM;
818 }
819 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800820 iwl4965_rx_queue_reset(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -0700821
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800822 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700823
824 iwl4965_rx_init(priv, rxq);
825
826 spin_lock_irqsave(&priv->lock, flags);
827
828 rxq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800829 iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -0700830
Tomas Winkler073d3f52008-04-21 15:41:52 -0700831 /* init the txpower calibration pointer */
832 priv->calib_info = (struct iwl_eeprom_calib_info *)
833 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
834
Zhu Yib481de92007-09-25 17:54:57 -0700835 spin_unlock_irqrestore(&priv->lock, flags);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800836
837 /* Allocate and init all Tx and Command queues */
Tomas Winkler91238712008-04-23 17:14:53 -0700838 ret = iwl4965_txq_ctx_reset(priv);
839 if (ret)
840 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700841
842 set_bit(STATUS_INIT, &priv->status);
843
844 return 0;
845}
846
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700847int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700848{
849 int rc = 0;
850 u32 reg_val;
851 unsigned long flags;
852
853 spin_lock_irqsave(&priv->lock, flags);
854
855 /* set stop master bit */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700856 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
Zhu Yib481de92007-09-25 17:54:57 -0700857
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700858 reg_val = iwl_read32(priv, CSR_GP_CNTRL);
Zhu Yib481de92007-09-25 17:54:57 -0700859
860 if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
861 (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
862 IWL_DEBUG_INFO("Card in power save, master is already "
863 "stopped\n");
864 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700865 rc = iwl_poll_bit(priv, CSR_RESET,
Zhu Yib481de92007-09-25 17:54:57 -0700866 CSR_RESET_REG_FLAG_MASTER_DISABLED,
867 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
868 if (rc < 0) {
869 spin_unlock_irqrestore(&priv->lock, flags);
870 return rc;
871 }
872 }
873
874 spin_unlock_irqrestore(&priv->lock, flags);
875 IWL_DEBUG_INFO("stop master\n");
876
877 return rc;
878}
879
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800880/**
881 * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
882 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700883void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700884{
885
886 int txq_id;
887 unsigned long flags;
888
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800889 /* Stop each Tx DMA channel, and wait for it to be idle */
Tomas Winkler5425e492008-04-15 16:01:38 -0700890 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
Zhu Yib481de92007-09-25 17:54:57 -0700891 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700892 if (iwl_grab_nic_access(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -0700893 spin_unlock_irqrestore(&priv->lock, flags);
894 continue;
895 }
896
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700897 iwl_write_direct32(priv,
898 IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
899 iwl_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
900 IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
901 (txq_id), 200);
902 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700903 spin_unlock_irqrestore(&priv->lock, flags);
904 }
905
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800906 /* Deallocate memory for all Tx queues */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800907 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700908}
909
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700910int iwl4965_hw_nic_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700911{
912 int rc = 0;
913 unsigned long flags;
914
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800915 iwl4965_hw_nic_stop_master(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700916
917 spin_lock_irqsave(&priv->lock, flags);
918
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700919 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Zhu Yib481de92007-09-25 17:54:57 -0700920
921 udelay(10);
922
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700923 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
924 rc = iwl_poll_bit(priv, CSR_RESET,
Zhu Yib481de92007-09-25 17:54:57 -0700925 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
926 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
927
928 udelay(10);
929
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700930 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700931 if (!rc) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700932 iwl_write_prph(priv, APMG_CLK_EN_REG,
933 APMG_CLK_VAL_DMA_CLK_RQT |
934 APMG_CLK_VAL_BSM_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -0700935
936 udelay(10);
937
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700938 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
939 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Zhu Yib481de92007-09-25 17:54:57 -0700940
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700941 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700942 }
943
944 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
945 wake_up_interruptible(&priv->wait_command_queue);
946
947 spin_unlock_irqrestore(&priv->lock, flags);
948
949 return rc;
950
951}
952
953#define REG_RECALIB_PERIOD (60)
954
955/**
956 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
957 *
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700958 * This callback is provided in order to send a statistics request.
Zhu Yib481de92007-09-25 17:54:57 -0700959 *
960 * This timer function is continually reset to execute within
961 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
962 * was received. We need to ensure we receive the statistics in order
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700963 * to update the temperature used for calibrating the TXPOWER.
Zhu Yib481de92007-09-25 17:54:57 -0700964 */
965static void iwl4965_bg_statistics_periodic(unsigned long data)
966{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700967 struct iwl_priv *priv = (struct iwl_priv *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700968
Zhu Yib481de92007-09-25 17:54:57 -0700969 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
970 return;
971
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700972 iwl_send_statistics_request(priv, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700973}
974
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700975void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700976{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800977 struct iwl4965_ct_kill_config cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700978 unsigned long flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700979 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700980
981 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700982 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -0700983 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
984 spin_unlock_irqrestore(&priv->lock, flags);
985
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700986 cmd.critical_temperature_R =
Emmanuel Grumbachb73cdf22008-04-21 15:41:58 -0700987 cpu_to_le32(priv->hw_params.ct_kill_threshold);
988
Tomas Winkler857485c2008-03-21 13:53:44 -0700989 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
990 sizeof(cmd), &cmd);
991 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -0700992 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
993 else
Emmanuel Grumbachb73cdf22008-04-21 15:41:58 -0700994 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
995 "critical temperature is %d\n",
996 cmd.critical_temperature_R);
Zhu Yib481de92007-09-25 17:54:57 -0700997}
998
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -0700999#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07001000
1001/* Reset differential Rx gains in NIC to prepare for chain noise calibration.
1002 * Called after every association, but this runs only once!
1003 * ... once chain noise is calibrated the first time, it's good forever. */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001004static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001005{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001006 struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
Zhu Yib481de92007-09-25 17:54:57 -07001007
Tomas Winkler3109ece2008-03-28 16:33:35 -07001008 if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001009 struct iwl4965_calibration_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001010
1011 memset(&cmd, 0, sizeof(cmd));
1012 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1013 cmd.diff_gain_a = 0;
1014 cmd.diff_gain_b = 0;
1015 cmd.diff_gain_c = 0;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001016 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1017 sizeof(cmd), &cmd))
1018 IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
Zhu Yib481de92007-09-25 17:54:57 -07001019 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1020 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
1021 }
Zhu Yib481de92007-09-25 17:54:57 -07001022}
1023
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001024static void iwl4965_gain_computation(struct iwl_priv *priv,
1025 u32 *average_noise,
1026 u16 min_average_noise_antenna_i,
1027 u32 min_average_noise)
Zhu Yib481de92007-09-25 17:54:57 -07001028{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001029 int i, ret;
1030 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
Zhu Yib481de92007-09-25 17:54:57 -07001031
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001032 data->delta_gain_code[min_average_noise_antenna_i] = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001033
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001034 for (i = 0; i < NUM_RX_CHAINS; i++) {
1035 s32 delta_g = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001036
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001037 if (!(data->disconn_array[i]) &&
1038 (data->delta_gain_code[i] ==
Zhu Yib481de92007-09-25 17:54:57 -07001039 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001040 delta_g = average_noise[i] - min_average_noise;
1041 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
1042 data->delta_gain_code[i] =
1043 min(data->delta_gain_code[i],
1044 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
Zhu Yib481de92007-09-25 17:54:57 -07001045
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001046 data->delta_gain_code[i] =
1047 (data->delta_gain_code[i] | (1 << 2));
1048 } else {
1049 data->delta_gain_code[i] = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001050 }
Zhu Yib481de92007-09-25 17:54:57 -07001051 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001052 IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
1053 data->delta_gain_code[0],
1054 data->delta_gain_code[1],
1055 data->delta_gain_code[2]);
Zhu Yib481de92007-09-25 17:54:57 -07001056
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001057 /* Differential gain gets sent to uCode only once */
1058 if (!data->radio_write) {
1059 struct iwl4965_calibration_cmd cmd;
1060 data->radio_write = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001061
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001062 memset(&cmd, 0, sizeof(cmd));
1063 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1064 cmd.diff_gain_a = data->delta_gain_code[0];
1065 cmd.diff_gain_b = data->delta_gain_code[1];
1066 cmd.diff_gain_c = data->delta_gain_code[2];
1067 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1068 sizeof(cmd), &cmd);
1069 if (ret)
1070 IWL_DEBUG_CALIB("fail sending cmd "
1071 "REPLY_PHY_CALIBRATION_CMD \n");
Zhu Yib481de92007-09-25 17:54:57 -07001072
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001073 /* TODO we might want recalculate
1074 * rx_chain in rxon cmd */
1075
1076 /* Mark so we run this algo only once! */
1077 data->state = IWL_CHAIN_NOISE_CALIBRATED;
Zhu Yib481de92007-09-25 17:54:57 -07001078 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001079 data->chain_noise_a = 0;
1080 data->chain_noise_b = 0;
1081 data->chain_noise_c = 0;
1082 data->chain_signal_a = 0;
1083 data->chain_signal_b = 0;
1084 data->chain_signal_c = 0;
1085 data->beacon_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001086}
1087
1088static void iwl4965_bg_sensitivity_work(struct work_struct *work)
1089{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001090 struct iwl_priv *priv = container_of(work, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07001091 sensitivity_work);
1092
1093 mutex_lock(&priv->mutex);
1094
1095 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1096 test_bit(STATUS_SCANNING, &priv->status)) {
1097 mutex_unlock(&priv->mutex);
1098 return;
1099 }
1100
1101 if (priv->start_calib) {
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001102 iwl_chain_noise_calibration(priv, &priv->statistics);
Zhu Yib481de92007-09-25 17:54:57 -07001103
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001104 iwl_sensitivity_calibration(priv, &priv->statistics);
Zhu Yib481de92007-09-25 17:54:57 -07001105 }
1106
1107 mutex_unlock(&priv->mutex);
1108 return;
1109}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001110#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
Zhu Yib481de92007-09-25 17:54:57 -07001111
1112static void iwl4965_bg_txpower_work(struct work_struct *work)
1113{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001114 struct iwl_priv *priv = container_of(work, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07001115 txpower_work);
1116
1117 /* If a scan happened to start before we got here
1118 * then just return; the statistics notification will
1119 * kick off another scheduled work to compensate for
1120 * any temperature delta we missed here. */
1121 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1122 test_bit(STATUS_SCANNING, &priv->status))
1123 return;
1124
1125 mutex_lock(&priv->mutex);
1126
1127 /* Regardless of if we are assocaited, we must reconfigure the
1128 * TX power since frames can be sent on non-radar channels while
1129 * not associated */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001130 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001131
1132 /* Update last_temperature to keep is_calib_needed from running
1133 * when it isn't needed... */
1134 priv->last_temperature = priv->temperature;
1135
1136 mutex_unlock(&priv->mutex);
1137}
1138
1139/*
1140 * Acquire priv->lock before calling this function !
1141 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001142static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
Zhu Yib481de92007-09-25 17:54:57 -07001143{
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001144 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Zhu Yib481de92007-09-25 17:54:57 -07001145 (index & 0xff) | (txq_id << 8));
Tomas Winkler12a81f62008-04-03 16:05:20 -07001146 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
Zhu Yib481de92007-09-25 17:54:57 -07001147}
1148
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001149/**
1150 * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
1151 * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
1152 * @scd_retry: (1) Indicates queue will be used in aggregation mode
1153 *
1154 * NOTE: Acquire priv->lock before calling this function !
Zhu Yib481de92007-09-25 17:54:57 -07001155 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001156static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001157 struct iwl4965_tx_queue *txq,
Zhu Yib481de92007-09-25 17:54:57 -07001158 int tx_fifo_id, int scd_retry)
1159{
1160 int txq_id = txq->q.id;
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001161
1162 /* Find out whether to activate Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001163 int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
1164
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001165 /* Set up and activate */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001166 iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
Zhu Yib481de92007-09-25 17:54:57 -07001167 (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1168 (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
1169 (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
1170 (scd_retry << SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
1171 SCD_QUEUE_STTS_REG_MSK);
1172
1173 txq->sched_retry = scd_retry;
1174
1175 IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001176 active ? "Activate" : "Deactivate",
Zhu Yib481de92007-09-25 17:54:57 -07001177 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
1178}
1179
1180static const u16 default_queue_to_tx_fifo[] = {
1181 IWL_TX_FIFO_AC3,
1182 IWL_TX_FIFO_AC2,
1183 IWL_TX_FIFO_AC1,
1184 IWL_TX_FIFO_AC0,
1185 IWL_CMD_FIFO_NUM,
1186 IWL_TX_FIFO_HCCA_1,
1187 IWL_TX_FIFO_HCCA_2
1188};
1189
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001190static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
Zhu Yib481de92007-09-25 17:54:57 -07001191{
1192 set_bit(txq_id, &priv->txq_ctx_active_msk);
1193}
1194
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001195static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
Zhu Yib481de92007-09-25 17:54:57 -07001196{
1197 clear_bit(txq_id, &priv->txq_ctx_active_msk);
1198}
1199
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001200int iwl4965_alive_notify(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001201{
1202 u32 a;
1203 int i = 0;
1204 unsigned long flags;
Tomas Winkler857485c2008-03-21 13:53:44 -07001205 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001206
1207 spin_lock_irqsave(&priv->lock, flags);
1208
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001209#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07001210 memset(&(priv->sensitivity_data), 0,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001211 sizeof(struct iwl_sensitivity_data));
Zhu Yib481de92007-09-25 17:54:57 -07001212 memset(&(priv->chain_noise_data), 0,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001213 sizeof(struct iwl_chain_noise_data));
Zhu Yib481de92007-09-25 17:54:57 -07001214 for (i = 0; i < NUM_RX_CHAINS; i++)
1215 priv->chain_noise_data.delta_gain_code[i] =
1216 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001217#endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001218 ret = iwl_grab_nic_access(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07001219 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001220 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler857485c2008-03-21 13:53:44 -07001221 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001222 }
1223
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001224 /* Clear 4965's internal Tx Scheduler data base */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001225 priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
Zhu Yib481de92007-09-25 17:54:57 -07001226 a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
1227 for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001228 iwl_write_targ_mem(priv, a, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001229 for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001230 iwl_write_targ_mem(priv, a, 0);
Tomas Winkler5425e492008-04-15 16:01:38 -07001231 for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001232 iwl_write_targ_mem(priv, a, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001233
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001234 /* Tel 4965 where to find Tx byte count tables */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001235 iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
Tomas Winkler059ff822008-04-14 21:16:14 -07001236 (priv->shared_phys +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001237 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001238
1239 /* Disable chain mode for all queues */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001240 iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001241
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001242 /* Initialize each Tx queue (including the command queue) */
Tomas Winkler5425e492008-04-15 16:01:38 -07001243 for (i = 0; i < priv->hw_params.max_txq_num; i++) {
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001244
1245 /* TFD circular buffer read/write indexes */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001246 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001247 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001248
1249 /* Max Tx Window size for Scheduler-ACK mode */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001250 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07001251 SCD_CONTEXT_QUEUE_OFFSET(i),
1252 (SCD_WIN_SIZE <<
1253 SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
1254 SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001255
1256 /* Frame limit */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001257 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07001258 SCD_CONTEXT_QUEUE_OFFSET(i) +
1259 sizeof(u32),
1260 (SCD_FRAME_LIMIT <<
1261 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1262 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
1263
1264 }
Tomas Winkler12a81f62008-04-03 16:05:20 -07001265 iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
Tomas Winkler5425e492008-04-15 16:01:38 -07001266 (1 << priv->hw_params.max_txq_num) - 1);
Zhu Yib481de92007-09-25 17:54:57 -07001267
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001268 /* Activate all Tx DMA/FIFO channels */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001269 iwl_write_prph(priv, IWL49_SCD_TXFACT,
Zhu Yib481de92007-09-25 17:54:57 -07001270 SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
1271
1272 iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001273
1274 /* Map each Tx/cmd queue to its corresponding fifo */
Zhu Yib481de92007-09-25 17:54:57 -07001275 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
1276 int ac = default_queue_to_tx_fifo[i];
1277 iwl4965_txq_ctx_activate(priv, i);
1278 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
1279 }
1280
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001281 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001282 spin_unlock_irqrestore(&priv->lock, flags);
1283
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07001284 /* Ask for statistics now, the uCode will send statistics notification
1285 * periodically after association */
1286 iwl_send_statistics_request(priv, CMD_ASYNC);
Tomas Winkler857485c2008-03-21 13:53:44 -07001287 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001288}
1289
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001290#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1291static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
1292 .min_nrg_cck = 97,
1293 .max_nrg_cck = 0,
1294
1295 .auto_corr_min_ofdm = 85,
1296 .auto_corr_min_ofdm_mrc = 170,
1297 .auto_corr_min_ofdm_x1 = 105,
1298 .auto_corr_min_ofdm_mrc_x1 = 220,
1299
1300 .auto_corr_max_ofdm = 120,
1301 .auto_corr_max_ofdm_mrc = 210,
1302 .auto_corr_max_ofdm_x1 = 140,
1303 .auto_corr_max_ofdm_mrc_x1 = 270,
1304
1305 .auto_corr_min_cck = 125,
1306 .auto_corr_max_cck = 200,
1307 .auto_corr_min_cck_mrc = 200,
1308 .auto_corr_max_cck_mrc = 400,
1309
1310 .nrg_th_cck = 100,
1311 .nrg_th_ofdm = 100,
1312};
1313#endif
1314
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001315/**
Tomas Winkler5425e492008-04-15 16:01:38 -07001316 * iwl4965_hw_set_hw_params
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001317 *
1318 * Called when initializing driver
1319 */
Tomas Winkler5425e492008-04-15 16:01:38 -07001320int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001321{
Assaf Krauss316c30d2008-03-14 10:38:46 -07001322
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07001323 if ((priv->cfg->mod_params->num_of_queues > IWL4965_MAX_NUM_QUEUES) ||
Assaf Krauss1ea87392008-03-18 14:57:50 -07001324 (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07001325 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07001326 IWL_MIN_NUM_QUEUES, IWL4965_MAX_NUM_QUEUES);
Tomas Winkler059ff822008-04-14 21:16:14 -07001327 return -EINVAL;
Assaf Krauss316c30d2008-03-14 10:38:46 -07001328 }
1329
Tomas Winkler5425e492008-04-15 16:01:38 -07001330 priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001331 priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
Tomas Winkler5425e492008-04-15 16:01:38 -07001332 priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
1333 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1334 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
Assaf Krauss1ea87392008-03-18 14:57:50 -07001335 if (priv->cfg->mod_params->amsdu_size_8K)
Tomas Winkler5425e492008-04-15 16:01:38 -07001336 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02001337 else
Tomas Winkler5425e492008-04-15 16:01:38 -07001338 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
1339 priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
1340 priv->hw_params.max_stations = IWL4965_STATION_COUNT;
1341 priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
Tomas Winkler3e82a822008-02-13 11:32:31 -08001342
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001343 priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
1344 priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
1345 priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
1346 priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
1347
Tomas Winklerec35cf22008-04-15 16:01:39 -07001348 priv->hw_params.tx_chains_num = 2;
1349 priv->hw_params.rx_chains_num = 2;
Guy Cohenfde0db32008-04-21 15:42:01 -07001350 priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
1351 priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001352 priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
1353
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001354#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1355 priv->hw_params.sens = &iwl4965_sensitivity;
1356#endif
Tomas Winkler3e82a822008-02-13 11:32:31 -08001357
Tomas Winkler059ff822008-04-14 21:16:14 -07001358 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001359}
1360
1361/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001362 * iwl4965_hw_txq_ctx_free - Free TXQ Context
Zhu Yib481de92007-09-25 17:54:57 -07001363 *
1364 * Destroy all TX DMA queues and structures
1365 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001366void iwl4965_hw_txq_ctx_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001367{
1368 int txq_id;
1369
1370 /* Tx queues */
Tomas Winkler5425e492008-04-15 16:01:38 -07001371 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001372 iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
Zhu Yib481de92007-09-25 17:54:57 -07001373
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001374 /* Keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001375 iwl4965_kw_free(priv);
1376}
1377
1378/**
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001379 * iwl4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Zhu Yib481de92007-09-25 17:54:57 -07001380 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001381 * Does NOT advance any TFD circular buffer read/write indexes
1382 * Does NOT free the TFD itself (which is within circular buffer)
Zhu Yib481de92007-09-25 17:54:57 -07001383 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001384int iwl4965_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07001385{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001386 struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
1387 struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001388 struct pci_dev *dev = priv->pci_dev;
1389 int i;
1390 int counter = 0;
1391 int index, is_odd;
1392
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001393 /* Host command buffers stay mapped in memory, nothing to clean */
Zhu Yib481de92007-09-25 17:54:57 -07001394 if (txq->q.id == IWL_CMD_QUEUE_NUM)
Zhu Yib481de92007-09-25 17:54:57 -07001395 return 0;
1396
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001397 /* Sanity check on number of chunks */
Zhu Yib481de92007-09-25 17:54:57 -07001398 counter = IWL_GET_BITS(*bd, num_tbs);
1399 if (counter > MAX_NUM_OF_TBS) {
1400 IWL_ERROR("Too many chunks: %i\n", counter);
1401 /* @todo issue fatal error, it is quite serious situation */
1402 return 0;
1403 }
1404
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001405 /* Unmap chunks, if any.
1406 * TFD info for odd chunks is different format than for even chunks. */
Zhu Yib481de92007-09-25 17:54:57 -07001407 for (i = 0; i < counter; i++) {
1408 index = i / 2;
1409 is_odd = i & 0x1;
1410
1411 if (is_odd)
1412 pci_unmap_single(
1413 dev,
1414 IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
1415 (IWL_GET_BITS(bd->pa[index],
1416 tb2_addr_hi20) << 16),
1417 IWL_GET_BITS(bd->pa[index], tb2_len),
1418 PCI_DMA_TODEVICE);
1419
1420 else if (i > 0)
1421 pci_unmap_single(dev,
1422 le32_to_cpu(bd->pa[index].tb1_addr),
1423 IWL_GET_BITS(bd->pa[index], tb1_len),
1424 PCI_DMA_TODEVICE);
1425
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001426 /* Free SKB, if any, for this chunk */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001427 if (txq->txb[txq->q.read_ptr].skb[i]) {
1428 struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
Zhu Yib481de92007-09-25 17:54:57 -07001429
1430 dev_kfree_skb(skb);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001431 txq->txb[txq->q.read_ptr].skb[i] = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001432 }
1433 }
1434 return 0;
1435}
1436
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001437/* set card power command */
1438static int iwl4965_set_power(struct iwl_priv *priv,
1439 void *cmd)
1440{
1441 int ret = 0;
1442
1443 ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1444 sizeof(struct iwl4965_powertable_cmd),
1445 cmd, NULL);
1446 return ret;
1447}
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001448int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
Zhu Yib481de92007-09-25 17:54:57 -07001449{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001450 IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001451 return -EINVAL;
1452}
1453
1454static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1455{
1456 s32 sign = 1;
1457
1458 if (num < 0) {
1459 sign = -sign;
1460 num = -num;
1461 }
1462 if (denom < 0) {
1463 sign = -sign;
1464 denom = -denom;
1465 }
1466 *res = 1;
1467 *res = ((num * 2 + denom) / (denom * 2)) * sign;
1468
1469 return 1;
1470}
1471
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001472/**
1473 * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1474 *
1475 * Determines power supply voltage compensation for txpower calculations.
1476 * Returns number of 1/2-dB steps to subtract from gain table index,
1477 * to compensate for difference between power supply voltage during
1478 * factory measurements, vs. current power supply voltage.
1479 *
1480 * Voltage indication is higher for lower voltage.
1481 * Lower voltage requires more gain (lower gain table index).
1482 */
Zhu Yib481de92007-09-25 17:54:57 -07001483static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1484 s32 current_voltage)
1485{
1486 s32 comp = 0;
1487
1488 if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1489 (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1490 return 0;
1491
1492 iwl4965_math_div_round(current_voltage - eeprom_voltage,
1493 TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1494
1495 if (current_voltage > eeprom_voltage)
1496 comp *= 2;
1497 if ((comp < -2) || (comp > 2))
1498 comp = 0;
1499
1500 return comp;
1501}
1502
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001503static const struct iwl_channel_info *
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001504iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001505 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07001506{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001507 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001508
Assaf Krauss8622e702008-03-21 13:53:43 -07001509 ch_info = iwl_get_channel_info(priv, band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07001510
1511 if (!is_channel_valid(ch_info))
1512 return NULL;
1513
1514 return ch_info;
1515}
1516
1517static s32 iwl4965_get_tx_atten_grp(u16 channel)
1518{
1519 if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1520 channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1521 return CALIB_CH_GROUP_5;
1522
1523 if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1524 channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1525 return CALIB_CH_GROUP_1;
1526
1527 if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1528 channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1529 return CALIB_CH_GROUP_2;
1530
1531 if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1532 channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1533 return CALIB_CH_GROUP_3;
1534
1535 if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1536 channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1537 return CALIB_CH_GROUP_4;
1538
1539 IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1540 return -1;
1541}
1542
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001543static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
Zhu Yib481de92007-09-25 17:54:57 -07001544{
1545 s32 b = -1;
1546
1547 for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
Tomas Winkler073d3f52008-04-21 15:41:52 -07001548 if (priv->calib_info->band_info[b].ch_from == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001549 continue;
1550
Tomas Winkler073d3f52008-04-21 15:41:52 -07001551 if ((channel >= priv->calib_info->band_info[b].ch_from)
1552 && (channel <= priv->calib_info->band_info[b].ch_to))
Zhu Yib481de92007-09-25 17:54:57 -07001553 break;
1554 }
1555
1556 return b;
1557}
1558
1559static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1560{
1561 s32 val;
1562
1563 if (x2 == x1)
1564 return y1;
1565 else {
1566 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1567 return val + y2;
1568 }
1569}
1570
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001571/**
1572 * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1573 *
1574 * Interpolates factory measurements from the two sample channels within a
1575 * sub-band, to apply to channel of interest. Interpolation is proportional to
1576 * differences in channel frequencies, which is proportional to differences
1577 * in channel number.
1578 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001579static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
Tomas Winkler073d3f52008-04-21 15:41:52 -07001580 struct iwl_eeprom_calib_ch_info *chan_info)
Zhu Yib481de92007-09-25 17:54:57 -07001581{
1582 s32 s = -1;
1583 u32 c;
1584 u32 m;
Tomas Winkler073d3f52008-04-21 15:41:52 -07001585 const struct iwl_eeprom_calib_measure *m1;
1586 const struct iwl_eeprom_calib_measure *m2;
1587 struct iwl_eeprom_calib_measure *omeas;
Zhu Yib481de92007-09-25 17:54:57 -07001588 u32 ch_i1;
1589 u32 ch_i2;
1590
1591 s = iwl4965_get_sub_band(priv, channel);
1592 if (s >= EEPROM_TX_POWER_BANDS) {
1593 IWL_ERROR("Tx Power can not find channel %d ", channel);
1594 return -1;
1595 }
1596
Tomas Winkler073d3f52008-04-21 15:41:52 -07001597 ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1598 ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
Zhu Yib481de92007-09-25 17:54:57 -07001599 chan_info->ch_num = (u8) channel;
1600
1601 IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1602 channel, s, ch_i1, ch_i2);
1603
1604 for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1605 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
Tomas Winkler073d3f52008-04-21 15:41:52 -07001606 m1 = &(priv->calib_info->band_info[s].ch1.
Zhu Yib481de92007-09-25 17:54:57 -07001607 measurements[c][m]);
Tomas Winkler073d3f52008-04-21 15:41:52 -07001608 m2 = &(priv->calib_info->band_info[s].ch2.
Zhu Yib481de92007-09-25 17:54:57 -07001609 measurements[c][m]);
1610 omeas = &(chan_info->measurements[c][m]);
1611
1612 omeas->actual_pow =
1613 (u8) iwl4965_interpolate_value(channel, ch_i1,
1614 m1->actual_pow,
1615 ch_i2,
1616 m2->actual_pow);
1617 omeas->gain_idx =
1618 (u8) iwl4965_interpolate_value(channel, ch_i1,
1619 m1->gain_idx, ch_i2,
1620 m2->gain_idx);
1621 omeas->temperature =
1622 (u8) iwl4965_interpolate_value(channel, ch_i1,
1623 m1->temperature,
1624 ch_i2,
1625 m2->temperature);
1626 omeas->pa_det =
1627 (s8) iwl4965_interpolate_value(channel, ch_i1,
1628 m1->pa_det, ch_i2,
1629 m2->pa_det);
1630
1631 IWL_DEBUG_TXPOWER
1632 ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1633 m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1634 IWL_DEBUG_TXPOWER
1635 ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1636 m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1637 IWL_DEBUG_TXPOWER
1638 ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1639 m1->pa_det, m2->pa_det, omeas->pa_det);
1640 IWL_DEBUG_TXPOWER
1641 ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
1642 m1->temperature, m2->temperature,
1643 omeas->temperature);
1644 }
1645 }
1646
1647 return 0;
1648}
1649
1650/* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1651 * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1652static s32 back_off_table[] = {
1653 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1654 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1655 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1656 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1657 10 /* CCK */
1658};
1659
1660/* Thermal compensation values for txpower for various frequency ranges ...
1661 * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001662static struct iwl4965_txpower_comp_entry {
Zhu Yib481de92007-09-25 17:54:57 -07001663 s32 degrees_per_05db_a;
1664 s32 degrees_per_05db_a_denom;
1665} tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1666 {9, 2}, /* group 0 5.2, ch 34-43 */
1667 {4, 1}, /* group 1 5.2, ch 44-70 */
1668 {4, 1}, /* group 2 5.2, ch 71-124 */
1669 {4, 1}, /* group 3 5.2, ch 125-200 */
1670 {3, 1} /* group 4 2.4, ch all */
1671};
1672
1673static s32 get_min_power_index(s32 rate_power_index, u32 band)
1674{
1675 if (!band) {
1676 if ((rate_power_index & 7) <= 4)
1677 return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1678 }
1679 return MIN_TX_GAIN_INDEX;
1680}
1681
1682struct gain_entry {
1683 u8 dsp;
1684 u8 radio;
1685};
1686
1687static const struct gain_entry gain_table[2][108] = {
1688 /* 5.2GHz power gain index table */
1689 {
1690 {123, 0x3F}, /* highest txpower */
1691 {117, 0x3F},
1692 {110, 0x3F},
1693 {104, 0x3F},
1694 {98, 0x3F},
1695 {110, 0x3E},
1696 {104, 0x3E},
1697 {98, 0x3E},
1698 {110, 0x3D},
1699 {104, 0x3D},
1700 {98, 0x3D},
1701 {110, 0x3C},
1702 {104, 0x3C},
1703 {98, 0x3C},
1704 {110, 0x3B},
1705 {104, 0x3B},
1706 {98, 0x3B},
1707 {110, 0x3A},
1708 {104, 0x3A},
1709 {98, 0x3A},
1710 {110, 0x39},
1711 {104, 0x39},
1712 {98, 0x39},
1713 {110, 0x38},
1714 {104, 0x38},
1715 {98, 0x38},
1716 {110, 0x37},
1717 {104, 0x37},
1718 {98, 0x37},
1719 {110, 0x36},
1720 {104, 0x36},
1721 {98, 0x36},
1722 {110, 0x35},
1723 {104, 0x35},
1724 {98, 0x35},
1725 {110, 0x34},
1726 {104, 0x34},
1727 {98, 0x34},
1728 {110, 0x33},
1729 {104, 0x33},
1730 {98, 0x33},
1731 {110, 0x32},
1732 {104, 0x32},
1733 {98, 0x32},
1734 {110, 0x31},
1735 {104, 0x31},
1736 {98, 0x31},
1737 {110, 0x30},
1738 {104, 0x30},
1739 {98, 0x30},
1740 {110, 0x25},
1741 {104, 0x25},
1742 {98, 0x25},
1743 {110, 0x24},
1744 {104, 0x24},
1745 {98, 0x24},
1746 {110, 0x23},
1747 {104, 0x23},
1748 {98, 0x23},
1749 {110, 0x22},
1750 {104, 0x18},
1751 {98, 0x18},
1752 {110, 0x17},
1753 {104, 0x17},
1754 {98, 0x17},
1755 {110, 0x16},
1756 {104, 0x16},
1757 {98, 0x16},
1758 {110, 0x15},
1759 {104, 0x15},
1760 {98, 0x15},
1761 {110, 0x14},
1762 {104, 0x14},
1763 {98, 0x14},
1764 {110, 0x13},
1765 {104, 0x13},
1766 {98, 0x13},
1767 {110, 0x12},
1768 {104, 0x08},
1769 {98, 0x08},
1770 {110, 0x07},
1771 {104, 0x07},
1772 {98, 0x07},
1773 {110, 0x06},
1774 {104, 0x06},
1775 {98, 0x06},
1776 {110, 0x05},
1777 {104, 0x05},
1778 {98, 0x05},
1779 {110, 0x04},
1780 {104, 0x04},
1781 {98, 0x04},
1782 {110, 0x03},
1783 {104, 0x03},
1784 {98, 0x03},
1785 {110, 0x02},
1786 {104, 0x02},
1787 {98, 0x02},
1788 {110, 0x01},
1789 {104, 0x01},
1790 {98, 0x01},
1791 {110, 0x00},
1792 {104, 0x00},
1793 {98, 0x00},
1794 {93, 0x00},
1795 {88, 0x00},
1796 {83, 0x00},
1797 {78, 0x00},
1798 },
1799 /* 2.4GHz power gain index table */
1800 {
1801 {110, 0x3f}, /* highest txpower */
1802 {104, 0x3f},
1803 {98, 0x3f},
1804 {110, 0x3e},
1805 {104, 0x3e},
1806 {98, 0x3e},
1807 {110, 0x3d},
1808 {104, 0x3d},
1809 {98, 0x3d},
1810 {110, 0x3c},
1811 {104, 0x3c},
1812 {98, 0x3c},
1813 {110, 0x3b},
1814 {104, 0x3b},
1815 {98, 0x3b},
1816 {110, 0x3a},
1817 {104, 0x3a},
1818 {98, 0x3a},
1819 {110, 0x39},
1820 {104, 0x39},
1821 {98, 0x39},
1822 {110, 0x38},
1823 {104, 0x38},
1824 {98, 0x38},
1825 {110, 0x37},
1826 {104, 0x37},
1827 {98, 0x37},
1828 {110, 0x36},
1829 {104, 0x36},
1830 {98, 0x36},
1831 {110, 0x35},
1832 {104, 0x35},
1833 {98, 0x35},
1834 {110, 0x34},
1835 {104, 0x34},
1836 {98, 0x34},
1837 {110, 0x33},
1838 {104, 0x33},
1839 {98, 0x33},
1840 {110, 0x32},
1841 {104, 0x32},
1842 {98, 0x32},
1843 {110, 0x31},
1844 {104, 0x31},
1845 {98, 0x31},
1846 {110, 0x30},
1847 {104, 0x30},
1848 {98, 0x30},
1849 {110, 0x6},
1850 {104, 0x6},
1851 {98, 0x6},
1852 {110, 0x5},
1853 {104, 0x5},
1854 {98, 0x5},
1855 {110, 0x4},
1856 {104, 0x4},
1857 {98, 0x4},
1858 {110, 0x3},
1859 {104, 0x3},
1860 {98, 0x3},
1861 {110, 0x2},
1862 {104, 0x2},
1863 {98, 0x2},
1864 {110, 0x1},
1865 {104, 0x1},
1866 {98, 0x1},
1867 {110, 0x0},
1868 {104, 0x0},
1869 {98, 0x0},
1870 {97, 0},
1871 {96, 0},
1872 {95, 0},
1873 {94, 0},
1874 {93, 0},
1875 {92, 0},
1876 {91, 0},
1877 {90, 0},
1878 {89, 0},
1879 {88, 0},
1880 {87, 0},
1881 {86, 0},
1882 {85, 0},
1883 {84, 0},
1884 {83, 0},
1885 {82, 0},
1886 {81, 0},
1887 {80, 0},
1888 {79, 0},
1889 {78, 0},
1890 {77, 0},
1891 {76, 0},
1892 {75, 0},
1893 {74, 0},
1894 {73, 0},
1895 {72, 0},
1896 {71, 0},
1897 {70, 0},
1898 {69, 0},
1899 {68, 0},
1900 {67, 0},
1901 {66, 0},
1902 {65, 0},
1903 {64, 0},
1904 {63, 0},
1905 {62, 0},
1906 {61, 0},
1907 {60, 0},
1908 {59, 0},
1909 }
1910};
1911
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001912static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
Zhu Yib481de92007-09-25 17:54:57 -07001913 u8 is_fat, u8 ctrl_chan_high,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001914 struct iwl4965_tx_power_db *tx_power_tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001915{
1916 u8 saturation_power;
1917 s32 target_power;
1918 s32 user_target_power;
1919 s32 power_limit;
1920 s32 current_temp;
1921 s32 reg_limit;
1922 s32 current_regulatory;
1923 s32 txatten_grp = CALIB_CH_GROUP_MAX;
1924 int i;
1925 int c;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001926 const struct iwl_channel_info *ch_info = NULL;
Tomas Winkler073d3f52008-04-21 15:41:52 -07001927 struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1928 const struct iwl_eeprom_calib_measure *measurement;
Zhu Yib481de92007-09-25 17:54:57 -07001929 s16 voltage;
1930 s32 init_voltage;
1931 s32 voltage_compensation;
1932 s32 degrees_per_05db_num;
1933 s32 degrees_per_05db_denom;
1934 s32 factory_temp;
1935 s32 temperature_comp[2];
1936 s32 factory_gain_index[2];
1937 s32 factory_actual_pwr[2];
1938 s32 power_index;
1939
1940 /* Sanity check requested level (dBm) */
1941 if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1942 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1943 priv->user_txpower_limit);
1944 return -EINVAL;
1945 }
1946 if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1947 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1948 priv->user_txpower_limit);
1949 return -EINVAL;
1950 }
1951
1952 /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1953 * are used for indexing into txpower table) */
1954 user_target_power = 2 * priv->user_txpower_limit;
1955
1956 /* Get current (RXON) channel, band, width */
1957 ch_info =
Johannes Berg8318d782008-01-24 19:38:38 +01001958 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07001959
1960 IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1961 is_fat);
1962
1963 if (!ch_info)
1964 return -EINVAL;
1965
1966 /* get txatten group, used to select 1) thermal txpower adjustment
1967 * and 2) mimo txpower balance between Tx chains. */
1968 txatten_grp = iwl4965_get_tx_atten_grp(channel);
1969 if (txatten_grp < 0)
1970 return -EINVAL;
1971
1972 IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1973 channel, txatten_grp);
1974
1975 if (is_fat) {
1976 if (ctrl_chan_high)
1977 channel -= 2;
1978 else
1979 channel += 2;
1980 }
1981
1982 /* hardware txpower limits ...
1983 * saturation (clipping distortion) txpowers are in half-dBm */
1984 if (band)
Tomas Winkler073d3f52008-04-21 15:41:52 -07001985 saturation_power = priv->calib_info->saturation_power24;
Zhu Yib481de92007-09-25 17:54:57 -07001986 else
Tomas Winkler073d3f52008-04-21 15:41:52 -07001987 saturation_power = priv->calib_info->saturation_power52;
Zhu Yib481de92007-09-25 17:54:57 -07001988
1989 if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1990 saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1991 if (band)
1992 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1993 else
1994 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1995 }
1996
1997 /* regulatory txpower limits ... reg_limit values are in half-dBm,
1998 * max_power_avg values are in dBm, convert * 2 */
1999 if (is_fat)
2000 reg_limit = ch_info->fat_max_power_avg * 2;
2001 else
2002 reg_limit = ch_info->max_power_avg * 2;
2003
2004 if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
2005 (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
2006 if (band)
2007 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
2008 else
2009 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
2010 }
2011
2012 /* Interpolate txpower calibration values for this channel,
2013 * based on factory calibration tests on spaced channels. */
2014 iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
2015
2016 /* calculate tx gain adjustment based on power supply voltage */
Tomas Winkler073d3f52008-04-21 15:41:52 -07002017 voltage = priv->calib_info->voltage;
Zhu Yib481de92007-09-25 17:54:57 -07002018 init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
2019 voltage_compensation =
2020 iwl4965_get_voltage_compensation(voltage, init_voltage);
2021
2022 IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
2023 init_voltage,
2024 voltage, voltage_compensation);
2025
2026 /* get current temperature (Celsius) */
2027 current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
2028 current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
2029 current_temp = KELVIN_TO_CELSIUS(current_temp);
2030
2031 /* select thermal txpower adjustment params, based on channel group
2032 * (same frequency group used for mimo txatten adjustment) */
2033 degrees_per_05db_num =
2034 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
2035 degrees_per_05db_denom =
2036 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
2037
2038 /* get per-chain txpower values from factory measurements */
2039 for (c = 0; c < 2; c++) {
2040 measurement = &ch_eeprom_info.measurements[c][1];
2041
2042 /* txgain adjustment (in half-dB steps) based on difference
2043 * between factory and current temperature */
2044 factory_temp = measurement->temperature;
2045 iwl4965_math_div_round((current_temp - factory_temp) *
2046 degrees_per_05db_denom,
2047 degrees_per_05db_num,
2048 &temperature_comp[c]);
2049
2050 factory_gain_index[c] = measurement->gain_idx;
2051 factory_actual_pwr[c] = measurement->actual_pow;
2052
2053 IWL_DEBUG_TXPOWER("chain = %d\n", c);
2054 IWL_DEBUG_TXPOWER("fctry tmp %d, "
2055 "curr tmp %d, comp %d steps\n",
2056 factory_temp, current_temp,
2057 temperature_comp[c]);
2058
2059 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
2060 factory_gain_index[c],
2061 factory_actual_pwr[c]);
2062 }
2063
2064 /* for each of 33 bit-rates (including 1 for CCK) */
2065 for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
2066 u8 is_mimo_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002067 union iwl4965_tx_power_dual_stream tx_power;
Zhu Yib481de92007-09-25 17:54:57 -07002068
2069 /* for mimo, reduce each chain's txpower by half
2070 * (3dB, 6 steps), so total output power is regulatory
2071 * compliant. */
2072 if (i & 0x8) {
2073 current_regulatory = reg_limit -
2074 IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
2075 is_mimo_rate = 1;
2076 } else {
2077 current_regulatory = reg_limit;
2078 is_mimo_rate = 0;
2079 }
2080
2081 /* find txpower limit, either hardware or regulatory */
2082 power_limit = saturation_power - back_off_table[i];
2083 if (power_limit > current_regulatory)
2084 power_limit = current_regulatory;
2085
2086 /* reduce user's txpower request if necessary
2087 * for this rate on this channel */
2088 target_power = user_target_power;
2089 if (target_power > power_limit)
2090 target_power = power_limit;
2091
2092 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
2093 i, saturation_power - back_off_table[i],
2094 current_regulatory, user_target_power,
2095 target_power);
2096
2097 /* for each of 2 Tx chains (radio transmitters) */
2098 for (c = 0; c < 2; c++) {
2099 s32 atten_value;
2100
2101 if (is_mimo_rate)
2102 atten_value =
2103 (s32)le32_to_cpu(priv->card_alive_init.
2104 tx_atten[txatten_grp][c]);
2105 else
2106 atten_value = 0;
2107
2108 /* calculate index; higher index means lower txpower */
2109 power_index = (u8) (factory_gain_index[c] -
2110 (target_power -
2111 factory_actual_pwr[c]) -
2112 temperature_comp[c] -
2113 voltage_compensation +
2114 atten_value);
2115
2116/* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
2117 power_index); */
2118
2119 if (power_index < get_min_power_index(i, band))
2120 power_index = get_min_power_index(i, band);
2121
2122 /* adjust 5 GHz index to support negative indexes */
2123 if (!band)
2124 power_index += 9;
2125
2126 /* CCK, rate 32, reduce txpower for CCK */
2127 if (i == POWER_TABLE_CCK_ENTRY)
2128 power_index +=
2129 IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
2130
2131 /* stay within the table! */
2132 if (power_index > 107) {
2133 IWL_WARNING("txpower index %d > 107\n",
2134 power_index);
2135 power_index = 107;
2136 }
2137 if (power_index < 0) {
2138 IWL_WARNING("txpower index %d < 0\n",
2139 power_index);
2140 power_index = 0;
2141 }
2142
2143 /* fill txpower command for this rate/chain */
2144 tx_power.s.radio_tx_gain[c] =
2145 gain_table[band][power_index].radio;
2146 tx_power.s.dsp_predis_atten[c] =
2147 gain_table[band][power_index].dsp;
2148
2149 IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
2150 "gain 0x%02x dsp %d\n",
2151 c, atten_value, power_index,
2152 tx_power.s.radio_tx_gain[c],
2153 tx_power.s.dsp_predis_atten[c]);
2154 }/* for each chain */
2155
2156 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
2157
2158 }/* for each rate */
2159
2160 return 0;
2161}
2162
2163/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002164 * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
Zhu Yib481de92007-09-25 17:54:57 -07002165 *
2166 * Uses the active RXON for channel, band, and characteristics (fat, high)
2167 * The power limit is taken from priv->user_txpower_limit.
2168 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002169int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002170{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002171 struct iwl4965_txpowertable_cmd cmd = { 0 };
Tomas Winkler857485c2008-03-21 13:53:44 -07002172 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002173 u8 band = 0;
2174 u8 is_fat = 0;
2175 u8 ctrl_chan_high = 0;
2176
2177 if (test_bit(STATUS_SCANNING, &priv->status)) {
2178 /* If this gets hit a lot, switch it to a BUG() and catch
2179 * the stack trace to find out who is calling this during
2180 * a scan. */
2181 IWL_WARNING("TX Power requested while scanning!\n");
2182 return -EAGAIN;
2183 }
2184
Johannes Berg8318d782008-01-24 19:38:38 +01002185 band = priv->band == IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002186
2187 is_fat = is_fat_channel(priv->active_rxon.flags);
2188
2189 if (is_fat &&
2190 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2191 ctrl_chan_high = 1;
2192
2193 cmd.band = band;
2194 cmd.channel = priv->active_rxon.channel;
2195
Tomas Winkler857485c2008-03-21 13:53:44 -07002196 ret = iwl4965_fill_txpower_tbl(priv, band,
Zhu Yib481de92007-09-25 17:54:57 -07002197 le16_to_cpu(priv->active_rxon.channel),
2198 is_fat, ctrl_chan_high, &cmd.tx_power);
Tomas Winkler857485c2008-03-21 13:53:44 -07002199 if (ret)
2200 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002201
Tomas Winkler857485c2008-03-21 13:53:44 -07002202 ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
2203
2204out:
2205 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002206}
2207
Tomas Winkler7e8c5192008-04-15 16:01:43 -07002208static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
2209{
2210 int ret = 0;
2211 struct iwl4965_rxon_assoc_cmd rxon_assoc;
2212 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
2213 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
2214
2215 if ((rxon1->flags == rxon2->flags) &&
2216 (rxon1->filter_flags == rxon2->filter_flags) &&
2217 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
2218 (rxon1->ofdm_ht_single_stream_basic_rates ==
2219 rxon2->ofdm_ht_single_stream_basic_rates) &&
2220 (rxon1->ofdm_ht_dual_stream_basic_rates ==
2221 rxon2->ofdm_ht_dual_stream_basic_rates) &&
2222 (rxon1->rx_chain == rxon2->rx_chain) &&
2223 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
2224 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
2225 return 0;
2226 }
2227
2228 rxon_assoc.flags = priv->staging_rxon.flags;
2229 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
2230 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
2231 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
2232 rxon_assoc.reserved = 0;
2233 rxon_assoc.ofdm_ht_single_stream_basic_rates =
2234 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
2235 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
2236 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
2237 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
2238
2239 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
2240 sizeof(rxon_assoc), &rxon_assoc, NULL);
2241 if (ret)
2242 return ret;
2243
2244 return ret;
2245}
2246
2247
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002248int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07002249{
2250 int rc;
2251 u8 band = 0;
2252 u8 is_fat = 0;
2253 u8 ctrl_chan_high = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002254 struct iwl4965_channel_switch_cmd cmd = { 0 };
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002255 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002256
Johannes Berg8318d782008-01-24 19:38:38 +01002257 band = priv->band == IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002258
Assaf Krauss8622e702008-03-21 13:53:43 -07002259 ch_info = iwl_get_channel_info(priv, priv->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002260
2261 is_fat = is_fat_channel(priv->staging_rxon.flags);
2262
2263 if (is_fat &&
2264 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2265 ctrl_chan_high = 1;
2266
2267 cmd.band = band;
2268 cmd.expect_beacon = 0;
2269 cmd.channel = cpu_to_le16(channel);
2270 cmd.rxon_flags = priv->active_rxon.flags;
2271 cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
2272 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
2273 if (ch_info)
2274 cmd.expect_beacon = is_channel_radar(ch_info);
2275 else
2276 cmd.expect_beacon = 1;
2277
2278 rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
2279 ctrl_chan_high, &cmd.tx_power);
2280 if (rc) {
2281 IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
2282 return rc;
2283 }
2284
Tomas Winkler857485c2008-03-21 13:53:44 -07002285 rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002286 return rc;
2287}
2288
2289#define RTS_HCCA_RETRY_LIMIT 3
2290#define RTS_DFAULT_RETRY_LIMIT 60
2291
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002292void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002293 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002294 struct ieee80211_tx_control *ctrl,
2295 struct ieee80211_hdr *hdr, int sta_id,
2296 int is_hcca)
2297{
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002298 struct iwl4965_tx_cmd *tx = &cmd->cmd.tx;
Zhu Yib481de92007-09-25 17:54:57 -07002299 u8 rts_retry_limit = 0;
2300 u8 data_retry_limit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002301 u16 fc = le16_to_cpu(hdr->frame_control);
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002302 u8 rate_plcp;
2303 u16 rate_flags = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002304 int rate_idx = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
Zhu Yib481de92007-09-25 17:54:57 -07002305
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002306 rate_plcp = iwl4965_rates[rate_idx].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07002307
2308 rts_retry_limit = (is_hcca) ?
2309 RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
2310
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002311 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
2312 rate_flags |= RATE_MCS_CCK_MSK;
2313
2314
Zhu Yib481de92007-09-25 17:54:57 -07002315 if (ieee80211_is_probe_response(fc)) {
2316 data_retry_limit = 3;
2317 if (data_retry_limit < rts_retry_limit)
2318 rts_retry_limit = data_retry_limit;
2319 } else
2320 data_retry_limit = IWL_DEFAULT_TX_RETRY;
2321
2322 if (priv->data_retry_limit != -1)
2323 data_retry_limit = priv->data_retry_limit;
2324
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002325
2326 if (ieee80211_is_data(fc)) {
2327 tx->initial_rate_index = 0;
2328 tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
2329 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002330 switch (fc & IEEE80211_FCTL_STYPE) {
2331 case IEEE80211_STYPE_AUTH:
2332 case IEEE80211_STYPE_DEAUTH:
2333 case IEEE80211_STYPE_ASSOC_REQ:
2334 case IEEE80211_STYPE_REASSOC_REQ:
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002335 if (tx->tx_flags & TX_CMD_FLG_RTS_MSK) {
2336 tx->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2337 tx->tx_flags |= TX_CMD_FLG_CTS_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002338 }
2339 break;
2340 default:
2341 break;
2342 }
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002343
2344 /* Alternate between antenna A and B for successive frames */
2345 if (priv->use_ant_b_for_management_frame) {
2346 priv->use_ant_b_for_management_frame = 0;
2347 rate_flags |= RATE_MCS_ANT_B_MSK;
2348 } else {
2349 priv->use_ant_b_for_management_frame = 1;
2350 rate_flags |= RATE_MCS_ANT_A_MSK;
2351 }
Zhu Yib481de92007-09-25 17:54:57 -07002352 }
2353
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002354 tx->rts_retry_limit = rts_retry_limit;
2355 tx->data_retry_limit = data_retry_limit;
2356 tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002357}
2358
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002359int iwl4965_hw_get_rx_read(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002360{
Tomas Winkler059ff822008-04-14 21:16:14 -07002361 struct iwl4965_shared *s = priv->shared_virt;
2362 return le32_to_cpu(s->rb_closed) & 0xFFF;
Zhu Yib481de92007-09-25 17:54:57 -07002363}
2364
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002365int iwl4965_hw_get_temperature(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002366{
2367 return priv->temperature;
2368}
2369
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002370unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002371 struct iwl4965_frame *frame, u8 rate)
Zhu Yib481de92007-09-25 17:54:57 -07002372{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002373 struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002374 unsigned int frame_size;
2375
2376 tx_beacon_cmd = &frame->u.beacon;
2377 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
2378
Tomas Winkler5425e492008-04-15 16:01:38 -07002379 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002380 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2381
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 frame_size = iwl4965_fill_beacon_frame(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002383 tx_beacon_cmd->frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384 iwl4965_broadcast_addr,
Zhu Yib481de92007-09-25 17:54:57 -07002385 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
2386
2387 BUG_ON(frame_size > MAX_MPDU_SIZE);
2388 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
2389
2390 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
2391 tx_beacon_cmd->tx.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002392 iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07002393 else
2394 tx_beacon_cmd->tx.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002395 iwl4965_hw_set_rate_n_flags(rate, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002396
2397 tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
2398 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
2399 return (sizeof(*tx_beacon_cmd) + frame_size);
2400}
2401
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002402/*
2403 * Tell 4965 where to find circular buffer of Tx Frame Descriptors for
2404 * given Tx queue, and enable the DMA channel used for that queue.
2405 *
2406 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
2407 * channels supported in hardware.
2408 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002409int iwl4965_hw_tx_queue_init(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07002410{
2411 int rc;
2412 unsigned long flags;
2413 int txq_id = txq->q.id;
2414
2415 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002416 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002417 if (rc) {
2418 spin_unlock_irqrestore(&priv->lock, flags);
2419 return rc;
2420 }
2421
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002422 /* Circular buffer (TFD queue in DRAM) physical base address */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002423 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
Zhu Yib481de92007-09-25 17:54:57 -07002424 txq->q.dma_addr >> 8);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002425
2426 /* Enable DMA channel, using same id as for TFD queue */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002427 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002428 priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
2429 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
2430 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002431 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002432 spin_unlock_irqrestore(&priv->lock, flags);
2433
2434 return 0;
2435}
2436
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002437int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
Zhu Yib481de92007-09-25 17:54:57 -07002438 dma_addr_t addr, u16 len)
2439{
2440 int index, is_odd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002441 struct iwl4965_tfd_frame *tfd = ptr;
Zhu Yib481de92007-09-25 17:54:57 -07002442 u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
2443
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002444 /* Each TFD can point to a maximum 20 Tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002445 if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
2446 IWL_ERROR("Error can not send more than %d chunks\n",
2447 MAX_NUM_OF_TBS);
2448 return -EINVAL;
2449 }
2450
2451 index = num_tbs / 2;
2452 is_odd = num_tbs & 0x1;
2453
2454 if (!is_odd) {
2455 tfd->pa[index].tb1_addr = cpu_to_le32(addr);
2456 IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
Tomas Winkler6a218f62008-01-14 17:46:15 -08002457 iwl_get_dma_hi_address(addr));
Zhu Yib481de92007-09-25 17:54:57 -07002458 IWL_SET_BITS(tfd->pa[index], tb1_len, len);
2459 } else {
2460 IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
2461 (u32) (addr & 0xffff));
2462 IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
2463 IWL_SET_BITS(tfd->pa[index], tb2_len, len);
2464 }
2465
2466 IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
2467
2468 return 0;
2469}
2470
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002471static void iwl4965_hw_card_show_info(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002472{
Tomas Winkler073d3f52008-04-21 15:41:52 -07002473 u16 hw_version = iwl_eeprom_query16(priv, EEPROM_4965_BOARD_REVISION);
Zhu Yib481de92007-09-25 17:54:57 -07002474
2475 IWL_DEBUG_INFO("4965ABGN HW Version %u.%u.%u\n",
2476 ((hw_version >> 8) & 0x0F),
2477 ((hw_version >> 8) >> 4), (hw_version & 0x00FF));
2478
2479 IWL_DEBUG_INFO("4965ABGN PBA Number %.16s\n",
Tomas Winkler073d3f52008-04-21 15:41:52 -07002480 &priv->eeprom[EEPROM_4965_BOARD_PBA]);
Zhu Yib481de92007-09-25 17:54:57 -07002481}
2482
Ron Rindjunsky399f4902008-04-23 17:14:56 -07002483static int iwl4965_alloc_shared_mem(struct iwl_priv *priv)
2484{
2485 priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
2486 sizeof(struct iwl4965_shared),
2487 &priv->shared_phys);
2488 if (!priv->shared_virt)
2489 return -ENOMEM;
2490
2491 memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
2492
2493 return 0;
2494}
2495
2496static void iwl4965_free_shared_mem(struct iwl_priv *priv)
2497{
2498 if (priv->shared_virt)
2499 pci_free_consistent(priv->pci_dev,
2500 sizeof(struct iwl4965_shared),
2501 priv->shared_virt,
2502 priv->shared_phys);
2503}
2504
Zhu Yib481de92007-09-25 17:54:57 -07002505#define IWL_TX_CRC_SIZE 4
2506#define IWL_TX_DELIMITER_SIZE 4
2507
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002508/**
Tomas Winklere2a722e2008-04-14 21:16:10 -07002509 * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002510 */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002511static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
2512 struct iwl4965_tx_queue *txq,
2513 u16 byte_cnt)
Zhu Yib481de92007-09-25 17:54:57 -07002514{
2515 int len;
2516 int txq_id = txq->q.id;
Tomas Winkler059ff822008-04-14 21:16:14 -07002517 struct iwl4965_shared *shared_data = priv->shared_virt;
Zhu Yib481de92007-09-25 17:54:57 -07002518
Zhu Yib481de92007-09-25 17:54:57 -07002519 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2520
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002521 /* Set up byte count within first 256 entries */
Zhu Yib481de92007-09-25 17:54:57 -07002522 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002523 tfd_offset[txq->q.write_ptr], byte_cnt, len);
Zhu Yib481de92007-09-25 17:54:57 -07002524
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002525 /* If within first 64 entries, duplicate at end */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002526 if (txq->q.write_ptr < IWL4965_MAX_WIN_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07002527 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002528 tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
Zhu Yib481de92007-09-25 17:54:57 -07002529 byte_cnt, len);
Zhu Yib481de92007-09-25 17:54:57 -07002530}
2531
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002532/**
2533 * iwl4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
2534 *
2535 * Selects how many and which Rx receivers/antennas/chains to use.
2536 * This should not be used for scan command ... it puts data in wrong place.
2537 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002538void iwl4965_set_rxon_chain(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002539{
Guy Cohenfde0db32008-04-21 15:42:01 -07002540 u8 is_single = is_single_rx_stream(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002541 u8 idle_state, rx_state;
2542
2543 priv->staging_rxon.rx_chain = 0;
2544 rx_state = idle_state = 3;
2545
2546 /* Tell uCode which antennas are actually connected.
2547 * Before first association, we assume all antennas are connected.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002548 * Just after first association, iwl_chain_noise_calibration()
Zhu Yib481de92007-09-25 17:54:57 -07002549 * checks which antennas actually *are* connected. */
2550 priv->staging_rxon.rx_chain |=
Guy Cohenfde0db32008-04-21 15:42:01 -07002551 cpu_to_le16(priv->hw_params.valid_rx_ant <<
2552 RXON_RX_CHAIN_VALID_POS);
Zhu Yib481de92007-09-25 17:54:57 -07002553
2554 /* How many receivers should we use? */
2555 iwl4965_get_rx_chain_counter(priv, &idle_state, &rx_state);
2556 priv->staging_rxon.rx_chain |=
2557 cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
2558 priv->staging_rxon.rx_chain |=
2559 cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
2560
2561 if (!is_single && (rx_state >= 2) &&
2562 !test_bit(STATUS_POWER_PMI, &priv->status))
2563 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
2564 else
2565 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
2566
2567 IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
2568}
2569
Zhu Yib481de92007-09-25 17:54:57 -07002570/**
2571 * sign_extend - Sign extend a value using specified bit as sign-bit
2572 *
2573 * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
2574 * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
2575 *
2576 * @param oper value to sign extend
2577 * @param index 0 based bit index (0<=index<32) to sign bit
2578 */
2579static s32 sign_extend(u32 oper, int index)
2580{
2581 u8 shift = 31 - index;
2582
2583 return (s32)(oper << shift) >> shift;
2584}
2585
2586/**
2587 * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
2588 * @statistics: Provides the temperature reading from the uCode
2589 *
2590 * A return of <0 indicates bogus data in the statistics
2591 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002592int iwl4965_get_temperature(const struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002593{
2594 s32 temperature;
2595 s32 vt;
2596 s32 R1, R2, R3;
2597 u32 R4;
2598
2599 if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
2600 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
2601 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
2602 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
2603 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
2604 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
2605 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
2606 } else {
2607 IWL_DEBUG_TEMP("Running temperature calibration\n");
2608 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
2609 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
2610 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2611 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2612 }
2613
2614 /*
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002615 * Temperature is only 23 bits, so sign extend out to 32.
Zhu Yib481de92007-09-25 17:54:57 -07002616 *
2617 * NOTE If we haven't received a statistics notification yet
2618 * with an updated temperature, use R4 provided to us in the
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002619 * "initialize" ALIVE response.
2620 */
Zhu Yib481de92007-09-25 17:54:57 -07002621 if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2622 vt = sign_extend(R4, 23);
2623 else
2624 vt = sign_extend(
2625 le32_to_cpu(priv->statistics.general.temperature), 23);
2626
2627 IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2628 R1, R2, R3, vt);
2629
2630 if (R3 == R1) {
2631 IWL_ERROR("Calibration conflict R1 == R3\n");
2632 return -1;
2633 }
2634
2635 /* Calculate temperature in degrees Kelvin, adjust by 97%.
2636 * Add offset to center the adjustment around 0 degrees Centigrade. */
2637 temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2638 temperature /= (R3 - R1);
2639 temperature = (temperature * 97) / 100 +
2640 TEMPERATURE_CALIB_KELVIN_OFFSET;
2641
2642 IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2643 KELVIN_TO_CELSIUS(temperature));
2644
2645 return temperature;
2646}
2647
2648/* Adjust Txpower only if temperature variance is greater than threshold. */
2649#define IWL_TEMPERATURE_THRESHOLD 3
2650
2651/**
2652 * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2653 *
2654 * If the temperature changed has changed sufficiently, then a recalibration
2655 * is needed.
2656 *
2657 * Assumes caller will replace priv->last_temperature once calibration
2658 * executed.
2659 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002660static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002661{
2662 int temp_diff;
2663
2664 if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2665 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2666 return 0;
2667 }
2668
2669 temp_diff = priv->temperature - priv->last_temperature;
2670
2671 /* get absolute value */
2672 if (temp_diff < 0) {
2673 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2674 temp_diff = -temp_diff;
2675 } else if (temp_diff == 0)
2676 IWL_DEBUG_POWER("Same temp, \n");
2677 else
2678 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2679
2680 if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2681 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2682 return 0;
2683 }
2684
2685 IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2686
2687 return 1;
2688}
2689
2690/* Calculate noise level, based on measurements during network silence just
2691 * before arriving beacon. This measurement can be done only if we know
2692 * exactly when to expect beacons, therefore only when we're associated. */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002693static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002694{
2695 struct statistics_rx_non_phy *rx_info
2696 = &(priv->statistics.rx.general);
2697 int num_active_rx = 0;
2698 int total_silence = 0;
2699 int bcn_silence_a =
2700 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2701 int bcn_silence_b =
2702 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2703 int bcn_silence_c =
2704 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2705
2706 if (bcn_silence_a) {
2707 total_silence += bcn_silence_a;
2708 num_active_rx++;
2709 }
2710 if (bcn_silence_b) {
2711 total_silence += bcn_silence_b;
2712 num_active_rx++;
2713 }
2714 if (bcn_silence_c) {
2715 total_silence += bcn_silence_c;
2716 num_active_rx++;
2717 }
2718
2719 /* Average among active antennas */
2720 if (num_active_rx)
2721 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2722 else
2723 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2724
2725 IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2726 bcn_silence_a, bcn_silence_b, bcn_silence_c,
2727 priv->last_rx_noise);
2728}
2729
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002730void iwl4965_hw_rx_statistics(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002731{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002732 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002733 int change;
2734 s32 temp;
2735
2736 IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2737 (int)sizeof(priv->statistics), pkt->len);
2738
2739 change = ((priv->statistics.general.temperature !=
2740 pkt->u.stats.general.temperature) ||
2741 ((priv->statistics.flag &
2742 STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2743 (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2744
2745 memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2746
2747 set_bit(STATUS_STATISTICS, &priv->status);
2748
2749 /* Reschedule the statistics timer to occur in
2750 * REG_RECALIB_PERIOD seconds to ensure we get a
2751 * thermal update even if the uCode doesn't give
2752 * us one */
2753 mod_timer(&priv->statistics_periodic, jiffies +
2754 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2755
2756 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2757 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2758 iwl4965_rx_calc_noise(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002759#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07002760 queue_work(priv->workqueue, &priv->sensitivity_work);
2761#endif
2762 }
2763
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002764 iwl_leds_background(priv);
2765
Zhu Yib481de92007-09-25 17:54:57 -07002766 /* If the hardware hasn't reported a change in
2767 * temperature then don't bother computing a
2768 * calibrated temperature value */
2769 if (!change)
2770 return;
2771
2772 temp = iwl4965_get_temperature(priv);
2773 if (temp < 0)
2774 return;
2775
2776 if (priv->temperature != temp) {
2777 if (priv->temperature)
2778 IWL_DEBUG_TEMP("Temperature changed "
2779 "from %dC to %dC\n",
2780 KELVIN_TO_CELSIUS(priv->temperature),
2781 KELVIN_TO_CELSIUS(temp));
2782 else
2783 IWL_DEBUG_TEMP("Temperature "
2784 "initialized to %dC\n",
2785 KELVIN_TO_CELSIUS(temp));
2786 }
2787
2788 priv->temperature = temp;
2789 set_bit(STATUS_TEMPERATURE, &priv->status);
2790
2791 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2792 iwl4965_is_temp_calib_needed(priv))
2793 queue_work(priv->workqueue, &priv->txpower_work);
2794}
2795
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002796static void iwl4965_add_radiotap(struct iwl_priv *priv,
Zhu Yi12342c42007-12-20 11:27:32 +08002797 struct sk_buff *skb,
2798 struct iwl4965_rx_phy_res *rx_start,
2799 struct ieee80211_rx_status *stats,
2800 u32 ampdu_status)
2801{
2802 s8 signal = stats->ssi;
2803 s8 noise = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002804 int rate = stats->rate_idx;
Zhu Yi12342c42007-12-20 11:27:32 +08002805 u64 tsf = stats->mactime;
Johannes Berga0b484f2008-04-01 17:51:47 +02002806 __le16 antenna;
Zhu Yi12342c42007-12-20 11:27:32 +08002807 __le16 phy_flags_hw = rx_start->phy_flags;
2808 struct iwl4965_rt_rx_hdr {
2809 struct ieee80211_radiotap_header rt_hdr;
2810 __le64 rt_tsf; /* TSF */
2811 u8 rt_flags; /* radiotap packet flags */
2812 u8 rt_rate; /* rate in 500kb/s */
2813 __le16 rt_channelMHz; /* channel in MHz */
2814 __le16 rt_chbitmask; /* channel bitfield */
2815 s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
2816 s8 rt_dbmnoise;
2817 u8 rt_antenna; /* antenna number */
2818 } __attribute__ ((packed)) *iwl4965_rt;
2819
2820 /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2821 if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2822 if (net_ratelimit())
2823 printk(KERN_ERR "not enough headroom [%d] for "
Miguel Botón01c20982008-01-04 23:34:35 +01002824 "radiotap head [%zd]\n",
Zhu Yi12342c42007-12-20 11:27:32 +08002825 skb_headroom(skb), sizeof(*iwl4965_rt));
2826 return;
2827 }
2828
2829 /* put radiotap header in front of 802.11 header and data */
2830 iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2831
2832 /* initialise radiotap header */
2833 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2834 iwl4965_rt->rt_hdr.it_pad = 0;
2835
2836 /* total header + data */
2837 put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2838 &iwl4965_rt->rt_hdr.it_len);
2839
2840 /* Indicate all the fields we add to the radiotap header */
2841 put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2842 (1 << IEEE80211_RADIOTAP_FLAGS) |
2843 (1 << IEEE80211_RADIOTAP_RATE) |
2844 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2845 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2846 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2847 (1 << IEEE80211_RADIOTAP_ANTENNA)),
2848 &iwl4965_rt->rt_hdr.it_present);
2849
2850 /* Zero the flags, we'll add to them as we go */
2851 iwl4965_rt->rt_flags = 0;
2852
2853 put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2854
2855 iwl4965_rt->rt_dbmsignal = signal;
2856 iwl4965_rt->rt_dbmnoise = noise;
2857
2858 /* Convert the channel frequency and set the flags */
2859 put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2860 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2861 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2862 IEEE80211_CHAN_5GHZ),
2863 &iwl4965_rt->rt_chbitmask);
2864 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2865 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2866 IEEE80211_CHAN_2GHZ),
2867 &iwl4965_rt->rt_chbitmask);
2868 else /* 802.11g */
2869 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2870 IEEE80211_CHAN_2GHZ),
2871 &iwl4965_rt->rt_chbitmask);
2872
Zhu Yi12342c42007-12-20 11:27:32 +08002873 if (rate == -1)
2874 iwl4965_rt->rt_rate = 0;
2875 else
2876 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
2877
2878 /*
2879 * "antenna number"
2880 *
2881 * It seems that the antenna field in the phy flags value
2882 * is actually a bitfield. This is undefined by radiotap,
2883 * it wants an actual antenna number but I always get "7"
2884 * for most legacy frames I receive indicating that the
2885 * same frame was received on all three RX chains.
2886 *
2887 * I think this field should be removed in favour of a
2888 * new 802.11n radiotap field "RX chains" that is defined
2889 * as a bitmask.
2890 */
Johannes Berga0b484f2008-04-01 17:51:47 +02002891 antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2892 iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
Zhu Yi12342c42007-12-20 11:27:32 +08002893
2894 /* set the preamble flag if appropriate */
2895 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2896 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2897
2898 stats->flag |= RX_FLAG_RADIOTAP;
2899}
2900
Tomas Winkler19758be2008-03-12 16:58:51 -07002901static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2902{
2903 /* 0 - mgmt, 1 - cnt, 2 - data */
2904 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2905 priv->rx_stats[idx].cnt++;
2906 priv->rx_stats[idx].bytes += len;
2907}
2908
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07002909/*
2910 * returns non-zero if packet should be dropped
2911 */
2912static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2913 struct ieee80211_hdr *hdr,
2914 u32 decrypt_res,
2915 struct ieee80211_rx_status *stats)
2916{
2917 u16 fc = le16_to_cpu(hdr->frame_control);
2918
2919 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2920 return 0;
2921
2922 if (!(fc & IEEE80211_FCTL_PROTECTED))
2923 return 0;
2924
2925 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2926 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2927 case RX_RES_STATUS_SEC_TYPE_TKIP:
2928 /* The uCode has got a bad phase 1 Key, pushes the packet.
2929 * Decryption will be done in SW. */
2930 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2931 RX_RES_STATUS_BAD_KEY_TTAK)
2932 break;
2933
2934 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2935 RX_RES_STATUS_BAD_ICV_MIC) {
2936 /* bad ICV, the packet is destroyed since the
2937 * decryption is inplace, drop it */
2938 IWL_DEBUG_RX("Packet destroyed\n");
2939 return -1;
2940 }
2941 case RX_RES_STATUS_SEC_TYPE_WEP:
2942 case RX_RES_STATUS_SEC_TYPE_CCMP:
2943 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2944 RX_RES_STATUS_DECRYPT_OK) {
2945 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2946 stats->flag |= RX_FLAG_DECRYPTED;
2947 }
2948 break;
2949
2950 default:
2951 break;
2952 }
2953 return 0;
2954}
2955
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002956static u32 iwl4965_translate_rx_status(u32 decrypt_in)
2957{
2958 u32 decrypt_out = 0;
2959
2960 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2961 RX_RES_STATUS_STATION_FOUND)
2962 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2963 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2964
2965 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2966
2967 /* packet was not encrypted */
2968 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2969 RX_RES_STATUS_SEC_TYPE_NONE)
2970 return decrypt_out;
2971
2972 /* packet was encrypted with unknown alg */
2973 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2974 RX_RES_STATUS_SEC_TYPE_ERR)
2975 return decrypt_out;
2976
2977 /* decryption was not done in HW */
2978 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2979 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2980 return decrypt_out;
2981
2982 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2983
2984 case RX_RES_STATUS_SEC_TYPE_CCMP:
2985 /* alg is CCM: check MIC only */
2986 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2987 /* Bad MIC */
2988 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2989 else
2990 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2991
2992 break;
2993
2994 case RX_RES_STATUS_SEC_TYPE_TKIP:
2995 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2996 /* Bad TTAK */
2997 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2998 break;
2999 }
3000 /* fall through if TTAK OK */
3001 default:
3002 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
3003 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
3004 else
3005 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
3006 break;
3007 };
3008
3009 IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
3010 decrypt_in, decrypt_out);
3011
3012 return decrypt_out;
3013}
3014
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003015static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
Zhu Yib481de92007-09-25 17:54:57 -07003016 int include_phy,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 struct iwl4965_rx_mem_buffer *rxb,
Zhu Yib481de92007-09-25 17:54:57 -07003018 struct ieee80211_rx_status *stats)
3019{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003020 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003021 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3022 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
3023 struct ieee80211_hdr *hdr;
3024 u16 len;
3025 __le32 *rx_end;
3026 unsigned int skblen;
3027 u32 ampdu_status;
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07003028 u32 ampdu_status_legacy;
Zhu Yib481de92007-09-25 17:54:57 -07003029
3030 if (!include_phy && priv->last_phy_res[0])
3031 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3032
3033 if (!rx_start) {
3034 IWL_ERROR("MPDU frame without a PHY data\n");
3035 return;
3036 }
3037 if (include_phy) {
3038 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
3039 rx_start->cfg_phy_cnt);
3040
3041 len = le16_to_cpu(rx_start->byte_count);
3042
3043 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
3044 sizeof(struct iwl4965_rx_phy_res) +
3045 rx_start->cfg_phy_cnt + len);
3046
3047 } else {
3048 struct iwl4965_rx_mpdu_res_start *amsdu =
3049 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3050
3051 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
3052 sizeof(struct iwl4965_rx_mpdu_res_start));
3053 len = le16_to_cpu(amsdu->byte_count);
3054 rx_start->byte_count = amsdu->byte_count;
3055 rx_end = (__le32 *) (((u8 *) hdr) + len);
3056 }
Tomas Winkler5425e492008-04-15 16:01:38 -07003057 if (len > priv->hw_params.max_pkt_size || len < 16) {
Zhu Yi12342c42007-12-20 11:27:32 +08003058 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003059 return;
3060 }
3061
3062 ampdu_status = le32_to_cpu(*rx_end);
3063 skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
3064
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07003065 if (!include_phy) {
3066 /* New status scheme, need to translate */
3067 ampdu_status_legacy = ampdu_status;
3068 ampdu_status = iwl4965_translate_rx_status(ampdu_status);
3069 }
3070
Zhu Yib481de92007-09-25 17:54:57 -07003071 /* start from MAC */
3072 skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
3073 skb_put(rxb->skb, len); /* end where data ends */
3074
3075 /* We only process data packets if the interface is open */
3076 if (unlikely(!priv->is_open)) {
3077 IWL_DEBUG_DROP_LIMIT
3078 ("Dropping packet while interface is not open.\n");
3079 return;
3080 }
3081
Zhu Yib481de92007-09-25 17:54:57 -07003082 stats->flag = 0;
3083 hdr = (struct ieee80211_hdr *)rxb->skb->data;
3084
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003085 /* in case of HW accelerated crypto and bad decryption, drop */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003086 if (!priv->hw_params.sw_crypto &&
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003087 iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
3088 return;
Zhu Yib481de92007-09-25 17:54:57 -07003089
Zhu Yi12342c42007-12-20 11:27:32 +08003090 if (priv->add_radiotap)
3091 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
3092
Tomas Winkler19758be2008-03-12 16:58:51 -07003093 iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
Zhu Yib481de92007-09-25 17:54:57 -07003094 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3095 priv->alloc_rxb_skb--;
3096 rxb->skb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003097}
3098
3099/* Calc max signal level (dBm) among 3 possible receivers */
3100static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
3101{
3102 /* data from PHY/DSP regarding signal strength, etc.,
3103 * contents are always there, not configurable by host. */
3104 struct iwl4965_rx_non_cfg_phy *ncphy =
3105 (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
3106 u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
3107 >> IWL_AGC_DB_POS;
3108
3109 u32 valid_antennae =
3110 (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
3111 >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
3112 u8 max_rssi = 0;
3113 u32 i;
3114
3115 /* Find max rssi among 3 possible receivers.
3116 * These values are measured by the digital signal processor (DSP).
3117 * They should stay fairly constant even as the signal strength varies,
3118 * if the radio's automatic gain control (AGC) is working right.
3119 * AGC value (see below) will provide the "interesting" info. */
3120 for (i = 0; i < 3; i++)
3121 if (valid_antennae & (1 << i))
3122 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
3123
3124 IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
3125 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
3126 max_rssi, agc);
3127
3128 /* dBm = max_rssi dB - agc dB - constant.
3129 * Higher AGC (higher radio gain) means lower signal. */
3130 return (max_rssi - agc - IWL_RSSI_OFFSET);
3131}
3132
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003133#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003134
Guy Cohenfde0db32008-04-21 15:42:01 -07003135void iwl4965_init_ht_hw_capab(const struct iwl_priv *priv,
Assaf Krauss1ea87392008-03-18 14:57:50 -07003136 struct ieee80211_ht_info *ht_info,
Tomas Winkler78330fd2008-02-06 02:37:18 +02003137 enum ieee80211_band band)
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003138{
3139 ht_info->cap = 0;
3140 memset(ht_info->supp_mcs_set, 0, 16);
3141
3142 ht_info->ht_supported = 1;
3143
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003144 if (priv->hw_params.fat_channel & BIT(band)) {
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003145 ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
3146 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
3147 ht_info->supp_mcs_set[4] = 0x01;
3148 }
3149 ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
3150 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
3151 ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
3152 (IWL_MIMO_PS_NONE << 2));
Assaf Krauss1ea87392008-03-18 14:57:50 -07003153
3154 if (priv->cfg->mod_params->amsdu_size_8K)
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003155 ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003156
3157 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
3158 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
3159
3160 ht_info->supp_mcs_set[0] = 0xFF;
Guy Cohenfde0db32008-04-21 15:42:01 -07003161 if (priv->hw_params.tx_chains_num >= 2)
3162 ht_info->supp_mcs_set[1] = 0xFF;
3163 if (priv->hw_params.tx_chains_num >= 3)
3164 ht_info->supp_mcs_set[2] = 0xFF;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003165}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003166#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003167
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003168static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07003169{
3170 unsigned long flags;
3171
3172 spin_lock_irqsave(&priv->sta_lock, flags);
3173 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
3174 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3175 priv->stations[sta_id].sta.sta.modify_mask = 0;
3176 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3177 spin_unlock_irqrestore(&priv->sta_lock, flags);
3178
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003179 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07003180}
3181
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003182static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
Zhu Yib481de92007-09-25 17:54:57 -07003183{
3184 /* FIXME: need locking over ps_status ??? */
Tomas Winkler947b13a2008-04-16 16:34:48 -07003185 u8 sta_id = iwl_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07003186
3187 if (sta_id != IWL_INVALID_STATION) {
3188 u8 sta_awake = priv->stations[sta_id].
3189 ps_status == STA_PS_STATUS_WAKE;
3190
3191 if (sta_awake && ps_bit)
3192 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
3193 else if (!sta_awake && !ps_bit) {
3194 iwl4965_sta_modify_ps_wake(priv, sta_id);
3195 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
3196 }
3197 }
3198}
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003199#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler17744ff2008-03-02 01:52:00 +02003200
3201/**
3202 * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
3203 *
3204 * You may hack this function to show different aspects of received frames,
3205 * including selective frame dumps.
3206 * group100 parameter selects whether to show 1 out of 100 good frames.
3207 *
3208 * TODO: This was originally written for 3945, need to audit for
3209 * proper operation with 4965.
3210 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003211static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003212 struct iwl4965_rx_packet *pkt,
3213 struct ieee80211_hdr *header, int group100)
3214{
3215 u32 to_us;
3216 u32 print_summary = 0;
3217 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
3218 u32 hundred = 0;
3219 u32 dataframe = 0;
3220 u16 fc;
3221 u16 seq_ctl;
3222 u16 channel;
3223 u16 phy_flags;
3224 int rate_sym;
3225 u16 length;
3226 u16 status;
3227 u16 bcn_tmr;
3228 u32 tsf_low;
3229 u64 tsf;
3230 u8 rssi;
3231 u8 agc;
3232 u16 sig_avg;
3233 u16 noise_diff;
3234 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
3235 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
3236 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
3237 u8 *data = IWL_RX_DATA(pkt);
3238
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003239 if (likely(!(iwl_debug_level & IWL_DL_RX)))
Tomas Winkler17744ff2008-03-02 01:52:00 +02003240 return;
3241
3242 /* MAC header */
3243 fc = le16_to_cpu(header->frame_control);
3244 seq_ctl = le16_to_cpu(header->seq_ctrl);
3245
3246 /* metadata */
3247 channel = le16_to_cpu(rx_hdr->channel);
3248 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
3249 rate_sym = rx_hdr->rate;
3250 length = le16_to_cpu(rx_hdr->len);
3251
3252 /* end-of-frame status and timestamp */
3253 status = le32_to_cpu(rx_end->status);
3254 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
3255 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
3256 tsf = le64_to_cpu(rx_end->timestamp);
3257
3258 /* signal statistics */
3259 rssi = rx_stats->rssi;
3260 agc = rx_stats->agc;
3261 sig_avg = le16_to_cpu(rx_stats->sig_avg);
3262 noise_diff = le16_to_cpu(rx_stats->noise_diff);
3263
3264 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
3265
3266 /* if data frame is to us and all is good,
3267 * (optionally) print summary for only 1 out of every 100 */
3268 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
3269 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
3270 dataframe = 1;
3271 if (!group100)
3272 print_summary = 1; /* print each frame */
3273 else if (priv->framecnt_to_us < 100) {
3274 priv->framecnt_to_us++;
3275 print_summary = 0;
3276 } else {
3277 priv->framecnt_to_us = 0;
3278 print_summary = 1;
3279 hundred = 1;
3280 }
3281 } else {
3282 /* print summary for all other frames */
3283 print_summary = 1;
3284 }
3285
3286 if (print_summary) {
3287 char *title;
3288 int rate_idx;
3289 u32 bitrate;
3290
3291 if (hundred)
3292 title = "100Frames";
3293 else if (fc & IEEE80211_FCTL_RETRY)
3294 title = "Retry";
3295 else if (ieee80211_is_assoc_response(fc))
3296 title = "AscRsp";
3297 else if (ieee80211_is_reassoc_response(fc))
3298 title = "RasRsp";
3299 else if (ieee80211_is_probe_response(fc)) {
3300 title = "PrbRsp";
3301 print_dump = 1; /* dump frame contents */
3302 } else if (ieee80211_is_beacon(fc)) {
3303 title = "Beacon";
3304 print_dump = 1; /* dump frame contents */
3305 } else if (ieee80211_is_atim(fc))
3306 title = "ATIM";
3307 else if (ieee80211_is_auth(fc))
3308 title = "Auth";
3309 else if (ieee80211_is_deauth(fc))
3310 title = "DeAuth";
3311 else if (ieee80211_is_disassoc(fc))
3312 title = "DisAssoc";
3313 else
3314 title = "Frame";
3315
3316 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
3317 if (unlikely(rate_idx == -1))
3318 bitrate = 0;
3319 else
3320 bitrate = iwl4965_rates[rate_idx].ieee / 2;
3321
3322 /* print frame summary.
3323 * MAC addresses show just the last byte (for brevity),
3324 * but you can hack it to show more, if you'd like to. */
3325 if (dataframe)
3326 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
3327 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
3328 title, fc, header->addr1[5],
3329 length, rssi, channel, bitrate);
3330 else {
3331 /* src/dst addresses assume managed mode */
3332 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
3333 "src=0x%02x, rssi=%u, tim=%lu usec, "
3334 "phy=0x%02x, chnl=%d\n",
3335 title, fc, header->addr1[5],
3336 header->addr3[5], rssi,
3337 tsf_low - priv->scan_start_tsf,
3338 phy_flags, channel);
3339 }
3340 }
3341 if (print_dump)
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003342 iwl_print_hex_dump(IWL_DL_RX, data, length);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003343}
3344#else
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003345static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003346 struct iwl4965_rx_packet *pkt,
3347 struct ieee80211_hdr *header,
3348 int group100)
3349{
3350}
3351#endif
3352
Zhu Yib481de92007-09-25 17:54:57 -07003353
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003354
Tomas Winkler857485c2008-03-21 13:53:44 -07003355/* Called for REPLY_RX (legacy ABG frames), or
Zhu Yib481de92007-09-25 17:54:57 -07003356 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003357static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003358 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003359{
Tomas Winkler17744ff2008-03-02 01:52:00 +02003360 struct ieee80211_hdr *header;
3361 struct ieee80211_rx_status rx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003362 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003363 /* Use phy data (Rx signal strength, etc.) contained within
3364 * this rx packet for legacy frames,
3365 * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
Tomas Winkler857485c2008-03-21 13:53:44 -07003366 int include_phy = (pkt->hdr.cmd == REPLY_RX);
Zhu Yib481de92007-09-25 17:54:57 -07003367 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3368 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
3369 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3370 __le32 *rx_end;
3371 unsigned int len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003372 u16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07003373 u8 network_packet;
3374
Tomas Winkler17744ff2008-03-02 01:52:00 +02003375 rx_status.mactime = le64_to_cpu(rx_start->timestamp);
Tomas Winklerdc92e492008-04-03 16:05:22 -07003376 rx_status.freq =
3377 ieee80211_frequency_to_channel(le16_to_cpu(rx_start->channel));
Tomas Winkler17744ff2008-03-02 01:52:00 +02003378 rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
3379 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Tomas Winklerdc92e492008-04-03 16:05:22 -07003380 rx_status.rate_idx =
3381 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
Tomas Winkler17744ff2008-03-02 01:52:00 +02003382 if (rx_status.band == IEEE80211_BAND_5GHZ)
3383 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
3384
3385 rx_status.antenna = 0;
3386 rx_status.flag = 0;
3387
Zhu Yib481de92007-09-25 17:54:57 -07003388 if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
Tomas Winklerdc92e492008-04-03 16:05:22 -07003389 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
3390 rx_start->cfg_phy_cnt);
Zhu Yib481de92007-09-25 17:54:57 -07003391 return;
3392 }
Tomas Winkler17744ff2008-03-02 01:52:00 +02003393
Zhu Yib481de92007-09-25 17:54:57 -07003394 if (!include_phy) {
3395 if (priv->last_phy_res[0])
3396 rx_start = (struct iwl4965_rx_phy_res *)
3397 &priv->last_phy_res[1];
3398 else
3399 rx_start = NULL;
3400 }
3401
3402 if (!rx_start) {
3403 IWL_ERROR("MPDU frame without a PHY data\n");
3404 return;
3405 }
3406
3407 if (include_phy) {
3408 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
3409 + rx_start->cfg_phy_cnt);
3410
3411 len = le16_to_cpu(rx_start->byte_count);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003412 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
Zhu Yib481de92007-09-25 17:54:57 -07003413 sizeof(struct iwl4965_rx_phy_res) + len);
3414 } else {
3415 struct iwl4965_rx_mpdu_res_start *amsdu =
3416 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3417
3418 header = (void *)(pkt->u.raw +
3419 sizeof(struct iwl4965_rx_mpdu_res_start));
3420 len = le16_to_cpu(amsdu->byte_count);
3421 rx_end = (__le32 *) (pkt->u.raw +
3422 sizeof(struct iwl4965_rx_mpdu_res_start) + len);
3423 }
3424
3425 if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
3426 !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
3427 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
3428 le32_to_cpu(*rx_end));
3429 return;
3430 }
3431
3432 priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
3433
Zhu Yib481de92007-09-25 17:54:57 -07003434 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Tomas Winkler17744ff2008-03-02 01:52:00 +02003435 rx_status.ssi = iwl4965_calc_rssi(rx_start);
Zhu Yib481de92007-09-25 17:54:57 -07003436
3437 /* Meaningful noise values are available only from beacon statistics,
3438 * which are gathered only when associated, and indicate noise
3439 * only for the associated network channel ...
3440 * Ignore these noise values while scanning (other channels) */
Tomas Winkler3109ece2008-03-28 16:33:35 -07003441 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07003442 !test_bit(STATUS_SCANNING, &priv->status)) {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003443 rx_status.noise = priv->last_rx_noise;
3444 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi,
3445 rx_status.noise);
Zhu Yib481de92007-09-25 17:54:57 -07003446 } else {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003447 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3448 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003449 }
3450
3451 /* Reset beacon noise level if not associated. */
Tomas Winkler3109ece2008-03-28 16:33:35 -07003452 if (!iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003453 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3454
Tomas Winkler17744ff2008-03-02 01:52:00 +02003455 /* Set "1" to report good data frames in groups of 100 */
3456 /* FIXME: need to optimze the call: */
3457 iwl4965_dbg_report_frame(priv, pkt, header, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003458
Tomas Winkler17744ff2008-03-02 01:52:00 +02003459 IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
3460 rx_status.ssi, rx_status.noise, rx_status.signal,
John W. Linville06501d22008-04-01 17:38:47 -04003461 (unsigned long long)rx_status.mactime);
Zhu Yib481de92007-09-25 17:54:57 -07003462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003463 network_packet = iwl4965_is_network_packet(priv, header);
Zhu Yib481de92007-09-25 17:54:57 -07003464 if (network_packet) {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003465 priv->last_rx_rssi = rx_status.ssi;
Zhu Yib481de92007-09-25 17:54:57 -07003466 priv->last_beacon_time = priv->ucode_beacon_time;
3467 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
3468 }
3469
3470 fc = le16_to_cpu(header->frame_control);
3471 switch (fc & IEEE80211_FCTL_FTYPE) {
3472 case IEEE80211_FTYPE_MGMT:
Zhu Yib481de92007-09-25 17:54:57 -07003473 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3474 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3475 header->addr2);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003476 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003477 break;
3478
3479 case IEEE80211_FTYPE_CTL:
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02003480#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003481 switch (fc & IEEE80211_FCTL_STYPE) {
3482 case IEEE80211_STYPE_BACK_REQ:
3483 IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
3484 iwl4965_handle_data_packet(priv, 0, include_phy,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003485 rxb, &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003486 break;
3487 default:
3488 break;
3489 }
3490#endif
Zhu Yib481de92007-09-25 17:54:57 -07003491 break;
3492
Joe Perches0795af52007-10-03 17:59:30 -07003493 case IEEE80211_FTYPE_DATA: {
3494 DECLARE_MAC_BUF(mac1);
3495 DECLARE_MAC_BUF(mac2);
3496 DECLARE_MAC_BUF(mac3);
3497
Zhu Yib481de92007-09-25 17:54:57 -07003498 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3499 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3500 header->addr2);
3501
3502 if (unlikely(!network_packet))
3503 IWL_DEBUG_DROP("Dropping (non network): "
Joe Perches0795af52007-10-03 17:59:30 -07003504 "%s, %s, %s\n",
3505 print_mac(mac1, header->addr1),
3506 print_mac(mac2, header->addr2),
3507 print_mac(mac3, header->addr3));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003508 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
Joe Perches0795af52007-10-03 17:59:30 -07003509 IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
3510 print_mac(mac1, header->addr1),
3511 print_mac(mac2, header->addr2),
3512 print_mac(mac3, header->addr3));
Zhu Yib481de92007-09-25 17:54:57 -07003513 else
3514 iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003515 &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003516 break;
Joe Perches0795af52007-10-03 17:59:30 -07003517 }
Zhu Yib481de92007-09-25 17:54:57 -07003518 default:
3519 break;
3520
3521 }
3522}
3523
3524/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
3525 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003526static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003527 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003528{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003529 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003530 priv->last_phy_res[0] = 1;
3531 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
3532 sizeof(struct iwl4965_rx_phy_res));
3533}
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003534static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003535 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003536
3537{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003538#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003539 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3540 struct iwl4965_missed_beacon_notif *missed_beacon;
Zhu Yib481de92007-09-25 17:54:57 -07003541
3542 missed_beacon = &pkt->u.missed_beacon;
3543 if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
3544 IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
3545 le32_to_cpu(missed_beacon->consequtive_missed_beacons),
3546 le32_to_cpu(missed_beacon->total_missed_becons),
3547 le32_to_cpu(missed_beacon->num_recvd_beacons),
3548 le32_to_cpu(missed_beacon->num_expected_beacons));
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003549 if (!test_bit(STATUS_SCANNING, &priv->status))
3550 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003551 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003552#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
Zhu Yib481de92007-09-25 17:54:57 -07003553}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003554#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003555
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003556/**
3557 * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
3558 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003559static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003560 int sta_id, int tid)
3561{
3562 unsigned long flags;
3563
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003564 /* Remove "disable" flag, to enable Tx for this TID */
Zhu Yib481de92007-09-25 17:54:57 -07003565 spin_lock_irqsave(&priv->sta_lock, flags);
3566 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3567 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3568 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3569 spin_unlock_irqrestore(&priv->sta_lock, flags);
3570
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003571 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07003572}
3573
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003574/**
3575 * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
3576 *
3577 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
3578 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
3579 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003580static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003581 struct iwl4965_ht_agg *agg,
3582 struct iwl4965_compressed_ba_resp*
Zhu Yib481de92007-09-25 17:54:57 -07003583 ba_resp)
3584
3585{
3586 int i, sh, ack;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003587 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
3588 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3589 u64 bitmap;
3590 int successes = 0;
3591 struct ieee80211_tx_status *tx_status;
Zhu Yib481de92007-09-25 17:54:57 -07003592
3593 if (unlikely(!agg->wait_for_ba)) {
3594 IWL_ERROR("Received BA when not expected\n");
3595 return -EINVAL;
3596 }
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003597
3598 /* Mark that the expected block-ack response arrived */
Zhu Yib481de92007-09-25 17:54:57 -07003599 agg->wait_for_ba = 0;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003600 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003601
3602 /* Calculate shift to align block-ack bits with our Tx window bits */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003603 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
Ian Schram01ebd062007-10-25 17:15:22 +08003604 if (sh < 0) /* tbw something is wrong with indices */
Zhu Yib481de92007-09-25 17:54:57 -07003605 sh += 0x100;
3606
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003607 /* don't use 64-bit values for now */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003608 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
Zhu Yib481de92007-09-25 17:54:57 -07003609
3610 if (agg->frame_count > (64 - sh)) {
3611 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
3612 return -1;
3613 }
3614
3615 /* check for success or failure according to the
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003616 * transmitted bitmap and block-ack bitmap */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003617 bitmap &= agg->bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003618
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003619 /* For each frame attempted in aggregation,
3620 * update driver's record of tx frame's status. */
Zhu Yib481de92007-09-25 17:54:57 -07003621 for (i = 0; i < agg->frame_count ; i++) {
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003622 ack = bitmap & (1 << i);
3623 successes += !!ack;
Zhu Yib481de92007-09-25 17:54:57 -07003624 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003625 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
3626 agg->start_idx + i);
Zhu Yib481de92007-09-25 17:54:57 -07003627 }
3628
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003629 tx_status = &priv->txq[scd_flow].txb[agg->start_idx].status;
3630 tx_status->flags = IEEE80211_TX_STATUS_ACK;
Ron Rindjunsky99556432008-01-28 14:07:25 +02003631 tx_status->flags |= IEEE80211_TX_STATUS_AMPDU;
3632 tx_status->ampdu_ack_map = successes;
3633 tx_status->ampdu_ack_len = agg->frame_count;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003634 iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags,
3635 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003636
John W. Linvillef868f4e2008-03-07 16:38:43 -05003637 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003638
3639 return 0;
3640}
3641
3642/**
3643 * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
3644 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003645static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003646 u16 txq_id)
3647{
3648 /* Simply stop the queue, but don't change any configuration;
3649 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003650 iwl_write_prph(priv,
Tomas Winkler12a81f62008-04-03 16:05:20 -07003651 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003652 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
3653 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3654}
3655
3656/**
3657 * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003658 * priv->lock must be held by the caller
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003659 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003660static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003661 u16 ssn_idx, u8 tx_fifo)
3662{
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003663 int ret = 0;
3664
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003665 if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3666 IWL_WARNING("queue number too small: %d, must be > %d\n",
3667 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3668 return -EINVAL;
3669 }
3670
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003671 ret = iwl_grab_nic_access(priv);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003672 if (ret)
3673 return ret;
3674
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003675 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3676
Tomas Winkler12a81f62008-04-03 16:05:20 -07003677 iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003678
3679 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3680 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3681 /* supposes that ssn_idx is valid (!= 0xFFF) */
3682 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3683
Tomas Winkler12a81f62008-04-03 16:05:20 -07003684 iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003685 iwl4965_txq_ctx_deactivate(priv, txq_id);
3686 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3687
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003688 iwl_release_nic_access(priv);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003689
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003690 return 0;
3691}
3692
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003693int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003694 u8 tid, int txq_id)
3695{
3696 struct iwl4965_queue *q = &priv->txq[txq_id].q;
3697 u8 *addr = priv->stations[sta_id].sta.sta.addr;
3698 struct iwl4965_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3699
3700 switch (priv->stations[sta_id].tid[tid].agg.state) {
3701 case IWL_EMPTYING_HW_QUEUE_DELBA:
3702 /* We are reclaiming the last packet of the */
3703 /* aggregated HW queue */
3704 if (txq_id == tid_data->agg.txq_id &&
3705 q->read_ptr == q->write_ptr) {
3706 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3707 int tx_fifo = default_tid_to_tx_fifo[tid];
3708 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3709 iwl4965_tx_queue_agg_disable(priv, txq_id,
3710 ssn, tx_fifo);
3711 tid_data->agg.state = IWL_AGG_OFF;
3712 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3713 }
3714 break;
3715 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3716 /* We are reclaiming the last packet of the queue */
3717 if (tid_data->tfds_in_queue == 0) {
3718 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3719 tid_data->agg.state = IWL_AGG_ON;
3720 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3721 }
3722 break;
3723 }
Zhu Yib481de92007-09-25 17:54:57 -07003724 return 0;
3725}
3726
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003727/**
3728 * iwl4965_queue_dec_wrap - Decrement queue index, wrap back to end if needed
3729 * @index -- current index
3730 * @n_bd -- total number of entries in queue (s/b power of 2)
3731 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003732static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -07003733{
3734 return (index == 0) ? n_bd - 1 : index - 1;
3735}
3736
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003737/**
3738 * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3739 *
3740 * Handles block-acknowledge notification from device, which reports success
3741 * of frames sent via aggregation.
3742 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003743static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003744 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003745{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003746 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3747 struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
Zhu Yib481de92007-09-25 17:54:57 -07003748 int index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003749 struct iwl4965_tx_queue *txq = NULL;
3750 struct iwl4965_ht_agg *agg;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003751 DECLARE_MAC_BUF(mac);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003752
3753 /* "flow" corresponds to Tx queue */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003754 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003755
3756 /* "ssn" is start of block-ack Tx window, corresponds to index
3757 * (in Tx queue's circular buffer) of first TFD/frame in window */
Zhu Yib481de92007-09-25 17:54:57 -07003758 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3759
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07003760 if (scd_flow >= priv->hw_params.max_txq_num) {
Zhu Yib481de92007-09-25 17:54:57 -07003761 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3762 return;
3763 }
3764
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003765 txq = &priv->txq[scd_flow];
Zhu Yib481de92007-09-25 17:54:57 -07003766 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003767
3768 /* Find index just before block-ack window */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003769 index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003770
Ian Schram01ebd062007-10-25 17:15:22 +08003771 /* TODO: Need to get this copy more safely - now good for debug */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003772
Joe Perches0795af52007-10-03 17:59:30 -07003773 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3774 "sta_id = %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003775 agg->wait_for_ba,
Joe Perches0795af52007-10-03 17:59:30 -07003776 print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
Zhu Yib481de92007-09-25 17:54:57 -07003777 ba_resp->sta_id);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003778 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
Zhu Yib481de92007-09-25 17:54:57 -07003779 "%d, scd_ssn = %d\n",
3780 ba_resp->tid,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003781 ba_resp->seq_ctl,
Tomas Winkler0310ae72008-03-11 16:17:19 -07003782 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
Zhu Yib481de92007-09-25 17:54:57 -07003783 ba_resp->scd_flow,
3784 ba_resp->scd_ssn);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003785 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
Zhu Yib481de92007-09-25 17:54:57 -07003786 agg->start_idx,
John W. Linvillef868f4e2008-03-07 16:38:43 -05003787 (unsigned long long)agg->bitmap);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003788
3789 /* Update driver's record of ACK vs. not for each frame in window */
Zhu Yib481de92007-09-25 17:54:57 -07003790 iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003791
3792 /* Release all TFDs before the SSN, i.e. all TFDs in front of
3793 * block-ack window (we assume that they've been successfully
3794 * transmitted ... if not, it's too late anyway). */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003795 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3796 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3797 priv->stations[ba_resp->sta_id].
3798 tid[ba_resp->tid].tfds_in_queue -= freed;
3799 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3800 priv->mac80211_registered &&
3801 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3802 ieee80211_wake_queue(priv->hw, scd_flow);
3803 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3804 ba_resp->tid, scd_flow);
3805 }
Zhu Yib481de92007-09-25 17:54:57 -07003806}
3807
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003808/**
3809 * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3810 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003811static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
Zhu Yib481de92007-09-25 17:54:57 -07003812 u16 txq_id)
3813{
3814 u32 tbl_dw_addr;
3815 u32 tbl_dw;
3816 u16 scd_q2ratid;
3817
3818 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3819
3820 tbl_dw_addr = priv->scd_base_addr +
3821 SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3822
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003823 tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003824
3825 if (txq_id & 0x1)
3826 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3827 else
3828 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3829
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003830 iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
Zhu Yib481de92007-09-25 17:54:57 -07003831
3832 return 0;
3833}
3834
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003835
Zhu Yib481de92007-09-25 17:54:57 -07003836/**
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003837 * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3838 *
3839 * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3840 * i.e. it must be one of the higher queues used for aggregation
Zhu Yib481de92007-09-25 17:54:57 -07003841 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003842static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
Zhu Yib481de92007-09-25 17:54:57 -07003843 int tx_fifo, int sta_id, int tid,
3844 u16 ssn_idx)
3845{
3846 unsigned long flags;
3847 int rc;
3848 u16 ra_tid;
3849
3850 if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3851 IWL_WARNING("queue number too small: %d, must be > %d\n",
3852 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3853
3854 ra_tid = BUILD_RAxTID(sta_id, tid);
3855
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003856 /* Modify device's station table to Tx this TID */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003857 iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
Zhu Yib481de92007-09-25 17:54:57 -07003858
3859 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003860 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003861 if (rc) {
3862 spin_unlock_irqrestore(&priv->lock, flags);
3863 return rc;
3864 }
3865
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003866 /* Stop this Tx queue before configuring it */
Zhu Yib481de92007-09-25 17:54:57 -07003867 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3868
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003869 /* Map receiver-address / traffic-ID to this queue */
Zhu Yib481de92007-09-25 17:54:57 -07003870 iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3871
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003872 /* Set this queue as a chain-building queue */
Tomas Winkler12a81f62008-04-03 16:05:20 -07003873 iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Zhu Yib481de92007-09-25 17:54:57 -07003874
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003875 /* Place first TFD at index corresponding to start sequence number.
3876 * Assumes that ssn_idx is valid (!= 0xFFF) */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003877 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3878 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
Zhu Yib481de92007-09-25 17:54:57 -07003879 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3880
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003881 /* Set up Tx window size and frame limit for this queue */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003882 iwl_write_targ_mem(priv,
Zhu Yib481de92007-09-25 17:54:57 -07003883 priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3884 (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3885 SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3886
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003887 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07003888 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3889 (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3890 & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3891
Tomas Winkler12a81f62008-04-03 16:05:20 -07003892 iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Zhu Yib481de92007-09-25 17:54:57 -07003893
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003894 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
Zhu Yib481de92007-09-25 17:54:57 -07003895 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3896
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003897 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003898 spin_unlock_irqrestore(&priv->lock, flags);
3899
3900 return 0;
3901}
3902
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003903#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003904
3905/**
3906 * iwl4965_add_station - Initialize a station's hardware rate table
3907 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003908 * The uCode's station table contains a table of fallback rates
Zhu Yib481de92007-09-25 17:54:57 -07003909 * for automatic fallback during transmission.
3910 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003911 * NOTE: This sets up a default set of values. These will be replaced later
3912 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
3913 * rc80211_simple.
Zhu Yib481de92007-09-25 17:54:57 -07003914 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003915 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
3916 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
3917 * which requires station table entry to exist).
Zhu Yib481de92007-09-25 17:54:57 -07003918 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003919void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -07003920{
3921 int i, r;
Tomas Winkler66c73db2008-04-15 16:01:40 -07003922 struct iwl_link_quality_cmd link_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003923 .reserved1 = 0,
3924 };
3925 u16 rate_flags;
3926
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003927 /* Set up the rate scaling to start at selected rate, fall back
3928 * all the way down to 1M in IEEE order, and then spin on 1M */
Zhu Yib481de92007-09-25 17:54:57 -07003929 if (is_ap)
3930 r = IWL_RATE_54M_INDEX;
Johannes Berg8318d782008-01-24 19:38:38 +01003931 else if (priv->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003932 r = IWL_RATE_6M_INDEX;
3933 else
3934 r = IWL_RATE_1M_INDEX;
3935
3936 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3937 rate_flags = 0;
3938 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
3939 rate_flags |= RATE_MCS_CCK_MSK;
3940
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003941 /* Use Tx antenna B only */
Guy Cohenfde0db32008-04-21 15:42:01 -07003942 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003943
Zhu Yib481de92007-09-25 17:54:57 -07003944 link_cmd.rs_table[i].rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003945 iwl4965_hw_set_rate_n_flags(iwl4965_rates[r].plcp, rate_flags);
3946 r = iwl4965_get_prev_ieee_rate(r);
Zhu Yib481de92007-09-25 17:54:57 -07003947 }
3948
3949 link_cmd.general_params.single_stream_ant_msk = 2;
3950 link_cmd.general_params.dual_stream_ant_msk = 3;
3951 link_cmd.agg_params.agg_dis_start_th = 3;
3952 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
3953
3954 /* Update the rate scaling for control frame Tx to AP */
Tomas Winkler5425e492008-04-15 16:01:38 -07003955 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003956
Tomas Winklere5472972008-03-28 16:21:12 -07003957 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
3958 sizeof(link_cmd), &link_cmd, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003959}
3960
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003961#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003962
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003963static u8 iwl4965_is_channel_extension(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003964 enum ieee80211_band band,
Tomas Winkler78330fd2008-02-06 02:37:18 +02003965 u16 channel, u8 extension_chan_offset)
Zhu Yib481de92007-09-25 17:54:57 -07003966{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003967 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003968
Assaf Krauss8622e702008-03-21 13:53:43 -07003969 ch_info = iwl_get_channel_info(priv, band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003970 if (!is_channel_valid(ch_info))
3971 return 0;
3972
Guy Cohen134eb5d2008-03-04 18:09:25 -08003973 if (extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07003974 return 0;
3975
3976 if ((ch_info->fat_extension_channel == extension_chan_offset) ||
3977 (ch_info->fat_extension_channel == HT_IE_EXT_CHANNEL_MAX))
3978 return 1;
3979
3980 return 0;
3981}
3982
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003983static u8 iwl4965_is_fat_tx_allowed(struct iwl_priv *priv,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003984 struct ieee80211_ht_info *sta_ht_inf)
Zhu Yib481de92007-09-25 17:54:57 -07003985{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003986 struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
Zhu Yib481de92007-09-25 17:54:57 -07003987
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003988 if ((!iwl_ht_conf->is_ht) ||
3989 (iwl_ht_conf->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ) ||
Guy Cohen134eb5d2008-03-04 18:09:25 -08003990 (iwl_ht_conf->extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE))
Zhu Yib481de92007-09-25 17:54:57 -07003991 return 0;
3992
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003993 if (sta_ht_inf) {
3994 if ((!sta_ht_inf->ht_supported) ||
Roel Kluin194c7ca2008-02-02 20:48:48 +01003995 (!(sta_ht_inf->cap & IEEE80211_HT_CAP_SUP_WIDTH)))
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003996 return 0;
3997 }
Zhu Yib481de92007-09-25 17:54:57 -07003998
Tomas Winkler78330fd2008-02-06 02:37:18 +02003999 return (iwl4965_is_channel_extension(priv, priv->band,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004000 iwl_ht_conf->control_channel,
4001 iwl_ht_conf->extension_chan_offset));
Zhu Yib481de92007-09-25 17:54:57 -07004002}
4003
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004004void iwl4965_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_info *ht_info)
Zhu Yib481de92007-09-25 17:54:57 -07004005{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004006 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07004007 u32 val;
4008
4009 if (!ht_info->is_ht)
4010 return;
4011
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08004012 /* Set up channel bandwidth: 20 MHz only, or 20/40 mixed if fat ok */
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004013 if (iwl4965_is_fat_tx_allowed(priv, NULL))
Zhu Yib481de92007-09-25 17:54:57 -07004014 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
4015 else
4016 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
4017 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
4018
4019 if (le16_to_cpu(rxon->channel) != ht_info->control_channel) {
4020 IWL_DEBUG_ASSOC("control diff than current %d %d\n",
4021 le16_to_cpu(rxon->channel),
4022 ht_info->control_channel);
4023 rxon->channel = cpu_to_le16(ht_info->control_channel);
4024 return;
4025 }
4026
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08004027 /* Note: control channel is opposite of extension channel */
Zhu Yib481de92007-09-25 17:54:57 -07004028 switch (ht_info->extension_chan_offset) {
4029 case IWL_EXT_CHANNEL_OFFSET_ABOVE:
4030 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
4031 break;
4032 case IWL_EXT_CHANNEL_OFFSET_BELOW:
4033 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
4034 break;
Guy Cohen134eb5d2008-03-04 18:09:25 -08004035 case IWL_EXT_CHANNEL_OFFSET_NONE:
Zhu Yib481de92007-09-25 17:54:57 -07004036 default:
4037 rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
4038 break;
4039 }
4040
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004041 val = ht_info->ht_protection;
Zhu Yib481de92007-09-25 17:54:57 -07004042
4043 rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
4044
Zhu Yib481de92007-09-25 17:54:57 -07004045 iwl4965_set_rxon_chain(priv);
4046
Guy Cohenfde0db32008-04-21 15:42:01 -07004047 IWL_DEBUG_ASSOC("supported HT rate 0x%X 0x%X 0x%X "
Zhu Yib481de92007-09-25 17:54:57 -07004048 "rxon flags 0x%X operation mode :0x%X "
4049 "extension channel offset 0x%x "
4050 "control chan %d\n",
Guy Cohenfde0db32008-04-21 15:42:01 -07004051 ht_info->supp_mcs_set[0],
4052 ht_info->supp_mcs_set[1],
4053 ht_info->supp_mcs_set[2],
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004054 le32_to_cpu(rxon->flags), ht_info->ht_protection,
Zhu Yib481de92007-09-25 17:54:57 -07004055 ht_info->extension_chan_offset,
4056 ht_info->control_channel);
4057 return;
4058}
4059
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004060void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004061 struct ieee80211_ht_info *sta_ht_inf)
Zhu Yib481de92007-09-25 17:54:57 -07004062{
4063 __le32 sta_flags;
Tomas Winklere53cfe02008-01-30 22:05:13 -08004064 u8 mimo_ps_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004065
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004066 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07004067 goto done;
4068
Tomas Winklere53cfe02008-01-30 22:05:13 -08004069 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
4070
Zhu Yib481de92007-09-25 17:54:57 -07004071 sta_flags = priv->stations[index].sta.station_flags;
4072
Tomas Winklere53cfe02008-01-30 22:05:13 -08004073 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
4074
4075 switch (mimo_ps_mode) {
4076 case WLAN_HT_CAP_MIMO_PS_STATIC:
4077 sta_flags |= STA_FLG_MIMO_DIS_MSK;
4078 break;
4079 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
Zhu Yib481de92007-09-25 17:54:57 -07004080 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Tomas Winklere53cfe02008-01-30 22:05:13 -08004081 break;
4082 case WLAN_HT_CAP_MIMO_PS_DISABLED:
4083 break;
4084 default:
4085 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
4086 break;
4087 }
Zhu Yib481de92007-09-25 17:54:57 -07004088
4089 sta_flags |= cpu_to_le32(
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004090 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Zhu Yib481de92007-09-25 17:54:57 -07004091
4092 sta_flags |= cpu_to_le32(
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004093 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Zhu Yib481de92007-09-25 17:54:57 -07004094
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004095 if (iwl4965_is_fat_tx_allowed(priv, sta_ht_inf))
Zhu Yib481de92007-09-25 17:54:57 -07004096 sta_flags |= STA_FLG_FAT_EN_MSK;
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004097 else
Tomas Winklere53cfe02008-01-30 22:05:13 -08004098 sta_flags &= ~STA_FLG_FAT_EN_MSK;
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004099
Zhu Yib481de92007-09-25 17:54:57 -07004100 priv->stations[index].sta.station_flags = sta_flags;
4101 done:
4102 return;
4103}
4104
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004105static int iwl4965_rx_agg_start(struct iwl_priv *priv,
4106 const u8 *addr, int tid, u16 ssn)
Zhu Yib481de92007-09-25 17:54:57 -07004107{
4108 unsigned long flags;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004109 int sta_id;
4110
4111 sta_id = iwl_find_station(priv, addr);
4112 if (sta_id == IWL_INVALID_STATION)
4113 return -ENXIO;
Zhu Yib481de92007-09-25 17:54:57 -07004114
4115 spin_lock_irqsave(&priv->sta_lock, flags);
4116 priv->stations[sta_id].sta.station_flags_msk = 0;
4117 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
4118 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
4119 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
4120 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4121 spin_unlock_irqrestore(&priv->sta_lock, flags);
4122
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004123 return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4124 CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07004125}
4126
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004127static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
4128 const u8 *addr, int tid)
Zhu Yib481de92007-09-25 17:54:57 -07004129{
4130 unsigned long flags;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004131 int sta_id;
4132
4133 sta_id = iwl_find_station(priv, addr);
4134 if (sta_id == IWL_INVALID_STATION)
4135 return -ENXIO;
Zhu Yib481de92007-09-25 17:54:57 -07004136
4137 spin_lock_irqsave(&priv->sta_lock, flags);
4138 priv->stations[sta_id].sta.station_flags_msk = 0;
4139 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
4140 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
4141 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4142 spin_unlock_irqrestore(&priv->sta_lock, flags);
4143
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004144 return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4145 CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07004146}
4147
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08004148/*
4149 * Find first available (lowest unused) Tx Queue, mark it "active".
4150 * Called only when finding queue for aggregation.
4151 * Should never return anything < 7, because they should already
4152 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
4153 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004154static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004155{
4156 int txq_id;
4157
Tomas Winkler5425e492008-04-15 16:01:38 -07004158 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Zhu Yib481de92007-09-25 17:54:57 -07004159 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
4160 return txq_id;
4161 return -1;
4162}
4163
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004164static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
4165 u16 tid, u16 *start_seq_num)
Zhu Yib481de92007-09-25 17:54:57 -07004166{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004167 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004168 int sta_id;
4169 int tx_fifo;
4170 int txq_id;
4171 int ssn = -1;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004172 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004173 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004174 struct iwl4965_tid_data *tid_data;
Joe Perches0795af52007-10-03 17:59:30 -07004175 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004176
4177 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4178 tx_fifo = default_tid_to_tx_fifo[tid];
4179 else
4180 return -EINVAL;
4181
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004182 IWL_WARNING("%s on ra = %s tid = %d\n",
4183 __func__, print_mac(mac, ra), tid);
Zhu Yib481de92007-09-25 17:54:57 -07004184
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004185 sta_id = iwl_find_station(priv, ra);
Zhu Yib481de92007-09-25 17:54:57 -07004186 if (sta_id == IWL_INVALID_STATION)
4187 return -ENXIO;
4188
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004189 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
4190 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
4191 return -ENXIO;
4192 }
4193
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004194 txq_id = iwl4965_txq_ctx_activate_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004195 if (txq_id == -1)
4196 return -ENXIO;
4197
4198 spin_lock_irqsave(&priv->sta_lock, flags);
4199 tid_data = &priv->stations[sta_id].tid[tid];
4200 ssn = SEQ_TO_SN(tid_data->seq_number);
4201 tid_data->agg.txq_id = txq_id;
4202 spin_unlock_irqrestore(&priv->sta_lock, flags);
4203
4204 *start_seq_num = ssn;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004205 ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
4206 sta_id, tid, ssn);
4207 if (ret)
4208 return ret;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004209
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004210 ret = 0;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004211 if (tid_data->tfds_in_queue == 0) {
4212 printk(KERN_ERR "HW queue is empty\n");
4213 tid_data->agg.state = IWL_AGG_ON;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004214 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004215 } else {
4216 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
4217 tid_data->tfds_in_queue);
4218 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
4219 }
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004220 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004221}
4222
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004223static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
Zhu Yib481de92007-09-25 17:54:57 -07004224{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004225 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004226 int tx_fifo_id, txq_id, sta_id, ssn = -1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227 struct iwl4965_tid_data *tid_data;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004228 int ret, write_ptr, read_ptr;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004229 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07004230 DECLARE_MAC_BUF(mac);
4231
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004232 if (!ra) {
4233 IWL_ERROR("ra = NULL\n");
Zhu Yib481de92007-09-25 17:54:57 -07004234 return -EINVAL;
4235 }
4236
4237 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4238 tx_fifo_id = default_tid_to_tx_fifo[tid];
4239 else
4240 return -EINVAL;
4241
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004242 sta_id = iwl_find_station(priv, ra);
Zhu Yib481de92007-09-25 17:54:57 -07004243
4244 if (sta_id == IWL_INVALID_STATION)
4245 return -ENXIO;
4246
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004247 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
4248 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
4249
Zhu Yib481de92007-09-25 17:54:57 -07004250 tid_data = &priv->stations[sta_id].tid[tid];
4251 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
4252 txq_id = tid_data->agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004253 write_ptr = priv->txq[txq_id].q.write_ptr;
4254 read_ptr = priv->txq[txq_id].q.read_ptr;
Zhu Yib481de92007-09-25 17:54:57 -07004255
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004256 /* The queue is not empty */
4257 if (write_ptr != read_ptr) {
4258 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
4259 priv->stations[sta_id].tid[tid].agg.state =
4260 IWL_EMPTYING_HW_QUEUE_DELBA;
4261 return 0;
4262 }
4263
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004264 IWL_DEBUG_HT("HW queue is empty\n");
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004265 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
4266
4267 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004268 ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004269 spin_unlock_irqrestore(&priv->lock, flags);
4270
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004271 if (ret)
4272 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004273
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004274 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
Zhu Yib481de92007-09-25 17:54:57 -07004275
4276 return 0;
4277}
4278
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004279int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
4280 enum ieee80211_ampdu_mlme_action action,
4281 const u8 *addr, u16 tid, u16 *ssn)
4282{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004283 struct iwl_priv *priv = hw->priv;
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004284 DECLARE_MAC_BUF(mac);
4285
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004286 IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
4287 print_mac(mac, addr), tid);
4288
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004289 switch (action) {
4290 case IEEE80211_AMPDU_RX_START:
4291 IWL_DEBUG_HT("start Rx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004292 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004293 case IEEE80211_AMPDU_RX_STOP:
4294 IWL_DEBUG_HT("stop Rx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004295 return iwl4965_rx_agg_stop(priv, addr, tid);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004296 case IEEE80211_AMPDU_TX_START:
4297 IWL_DEBUG_HT("start Tx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004298 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004299 case IEEE80211_AMPDU_TX_STOP:
4300 IWL_DEBUG_HT("stop Tx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004301 return iwl4965_tx_agg_stop(hw, addr, tid);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004302 default:
4303 IWL_DEBUG_HT("unknown\n");
4304 return -EINVAL;
4305 break;
4306 }
4307 return 0;
4308}
4309
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004310#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004311
4312/* Set up 4965-specific Rx frame reply handlers */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004313void iwl4965_hw_rx_handler_setup(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004314{
4315 /* Legacy Rx frames */
Tomas Winkler857485c2008-03-21 13:53:44 -07004316 priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
Zhu Yib481de92007-09-25 17:54:57 -07004317
4318 /* High-throughput (HT) Rx frames */
4319 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
4320 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
4321
4322 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
4323 iwl4965_rx_missed_beacon_notif;
4324
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004325#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07004326 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004327#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004328}
4329
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004330void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004331{
4332 INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004333#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07004334 INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
4335#endif
Zhu Yib481de92007-09-25 17:54:57 -07004336 init_timer(&priv->statistics_periodic);
4337 priv->statistics_periodic.data = (unsigned long)priv;
4338 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
4339}
4340
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004341void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004342{
4343 del_timer_sync(&priv->statistics_periodic);
4344
4345 cancel_delayed_work(&priv->init_alive_start);
4346}
4347
Tomas Winkler3c424c22008-04-15 16:01:42 -07004348
4349static struct iwl_hcmd_ops iwl4965_hcmd = {
Tomas Winkler7e8c5192008-04-15 16:01:43 -07004350 .rxon_assoc = iwl4965_send_rxon_assoc,
Tomas Winkler3c424c22008-04-15 16:01:42 -07004351};
4352
Tomas Winkler857485c2008-03-21 13:53:44 -07004353static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
4354 .enqueue_hcmd = iwl4965_enqueue_hcmd,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004355#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
4356 .chain_noise_reset = iwl4965_chain_noise_reset,
4357 .gain_computation = iwl4965_gain_computation,
4358#endif
Tomas Winkler857485c2008-03-21 13:53:44 -07004359};
4360
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004361static struct iwl_lib_ops iwl4965_lib = {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004362 .init_drv = iwl4965_init_drv,
Tomas Winkler5425e492008-04-15 16:01:38 -07004363 .set_hw_params = iwl4965_hw_set_hw_params,
Ron Rindjunsky399f4902008-04-23 17:14:56 -07004364 .alloc_shared_mem = iwl4965_alloc_shared_mem,
4365 .free_shared_mem = iwl4965_free_shared_mem,
Tomas Winklere2a722e2008-04-14 21:16:10 -07004366 .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
Tomas Winkler57aab752008-04-14 21:16:03 -07004367 .hw_nic_init = iwl4965_hw_nic_init,
4368 .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
4369 .alive_notify = iwl4965_alive_notify,
4370 .load_ucode = iwl4965_load_bsm,
Tomas Winkler6f4083a2008-04-16 16:34:49 -07004371 .apm_ops = {
Tomas Winkler91238712008-04-23 17:14:53 -07004372 .init = iwl4965_apm_init,
Tomas Winkler6f4083a2008-04-16 16:34:49 -07004373 .set_pwr_src = iwl4965_set_pwr_src,
4374 },
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004375 .eeprom_ops = {
Tomas Winkler073d3f52008-04-21 15:41:52 -07004376 .regulatory_bands = {
4377 EEPROM_REGULATORY_BAND_1_CHANNELS,
4378 EEPROM_REGULATORY_BAND_2_CHANNELS,
4379 EEPROM_REGULATORY_BAND_3_CHANNELS,
4380 EEPROM_REGULATORY_BAND_4_CHANNELS,
4381 EEPROM_REGULATORY_BAND_5_CHANNELS,
4382 EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
4383 EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
4384 },
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004385 .verify_signature = iwlcore_eeprom_verify_signature,
4386 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
4387 .release_semaphore = iwlcore_eeprom_release_semaphore,
Tomas Winkler8614f362008-04-23 17:14:55 -07004388 .check_version = iwl4965_eeprom_check_version,
Tomas Winkler073d3f52008-04-21 15:41:52 -07004389 .query_addr = iwlcore_eeprom_query_addr,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004390 },
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004391 .radio_kill_sw = iwl4965_radio_kill_sw,
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004392 .set_power = iwl4965_set_power,
4393 .update_chain_flags = iwl4965_update_chain_flags,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004394};
4395
4396static struct iwl_ops iwl4965_ops = {
4397 .lib = &iwl4965_lib,
Tomas Winkler3c424c22008-04-15 16:01:42 -07004398 .hcmd = &iwl4965_hcmd,
Tomas Winkler857485c2008-03-21 13:53:44 -07004399 .utils = &iwl4965_hcmd_utils,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004400};
4401
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004402struct iwl_cfg iwl4965_agn_cfg = {
Tomas Winkler82b9a122008-03-04 18:09:30 -08004403 .name = "4965AGN",
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004404 .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
Tomas Winkler82b9a122008-03-04 18:09:30 -08004405 .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
Tomas Winkler073d3f52008-04-21 15:41:52 -07004406 .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004407 .ops = &iwl4965_ops,
Assaf Krauss1ea87392008-03-18 14:57:50 -07004408 .mod_params = &iwl4965_mod_params,
Tomas Winkler82b9a122008-03-04 18:09:30 -08004409};
4410
Assaf Krauss1ea87392008-03-18 14:57:50 -07004411module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
4412MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
4413module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
4414MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -07004415module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
4416MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
Assaf Krauss1ea87392008-03-18 14:57:50 -07004417module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
4418MODULE_PARM_DESC(debug, "debug output mask");
4419module_param_named(
4420 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
4421MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4422
4423module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
4424MODULE_PARM_DESC(queues_num, "number of hw queues.");
4425
4426/* QoS */
4427module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
4428MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
4429module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
4430MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
4431