blob: b8c72159b595a31d85cea370f9225260eb86a553 [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
Tomas Winkler059ff822008-04-14 21:16:14 -0700279 priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
280 sizeof(struct iwl4965_shared),
281 &priv->shared_phys);
282
283 if (!priv->shared_virt) {
284 ret = -ENOMEM;
285 goto err;
286 }
287
288 memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
289
290
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700291 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
292 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
293
294 INIT_LIST_HEAD(&priv->free_frames);
295
296 mutex_init(&priv->mutex);
297
298 /* Clear the driver's (not device's) station table */
299 iwlcore_clear_stations_table(priv);
300
301 priv->data_retry_limit = -1;
302 priv->ieee_channels = NULL;
303 priv->ieee_rates = NULL;
304 priv->band = IEEE80211_BAND_2GHZ;
305
306 priv->iw_mode = IEEE80211_IF_TYPE_STA;
307
308 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700309 priv->ps_mode = IWL_MIMO_PS_NONE;
310
311 /* Choose which receivers/antennas to use */
312 iwl4965_set_rxon_chain(priv);
313
314 iwlcore_reset_qos(priv);
315
316 priv->qos_data.qos_active = 0;
317 priv->qos_data.qos_cap.val = 0;
318
319 iwlcore_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
320
321 priv->rates_mask = IWL_RATES_MASK;
322 /* If power management is turned on, default to AC mode */
323 priv->power_mode = IWL_POWER_AC;
324 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
325
326 ret = iwl_init_channel_map(priv);
327 if (ret) {
328 IWL_ERROR("initializing regulatory failed: %d\n", ret);
329 goto err;
330 }
331
332 ret = iwl4965_init_geos(priv);
333 if (ret) {
334 IWL_ERROR("initializing geos failed: %d\n", ret);
335 goto err_free_channel_map;
336 }
337
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700338 ret = ieee80211_register_hw(priv->hw);
339 if (ret) {
340 IWL_ERROR("Failed to register network device (error %d)\n",
341 ret);
342 goto err_free_geos;
343 }
344
345 priv->hw->conf.beacon_int = 100;
346 priv->mac80211_registered = 1;
347
348 return 0;
349
350err_free_geos:
351 iwl4965_free_geos(priv);
352err_free_channel_map:
353 iwl_free_channel_map(priv);
354err:
355 return ret;
356}
357
Zhu Yib481de92007-09-25 17:54:57 -0700358static int is_fat_channel(__le32 rxon_flags)
359{
360 return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
361 (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
362}
363
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800364#ifdef CONFIG_IWL4965_HT
Guy Cohenfde0db32008-04-21 15:42:01 -0700365static u8 is_single_rx_stream(struct iwl_priv *priv)
366{
367 return !priv->current_ht_config.is_ht ||
368 ((priv->current_ht_config.supp_mcs_set[1] == 0) &&
369 (priv->current_ht_config.supp_mcs_set[2] == 0)) ||
370 priv->ps_mode == IWL_MIMO_PS_STATIC;
Zhu Yib481de92007-09-25 17:54:57 -0700371}
Guy Cohenfde0db32008-04-21 15:42:01 -0700372#else
373static inline u8 is_single_rx_stream(struct iwl_priv *priv)
374{
375 return 1;
376}
377#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700378
Tomas Winkler17744ff2008-03-02 01:52:00 +0200379int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
380{
381 int idx = 0;
382
383 /* 4965 HT rate format */
384 if (rate_n_flags & RATE_MCS_HT_MSK) {
385 idx = (rate_n_flags & 0xff);
386
Guy Cohenfde0db32008-04-21 15:42:01 -0700387 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
388 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
Tomas Winkler17744ff2008-03-02 01:52:00 +0200389
390 idx += IWL_FIRST_OFDM_RATE;
391 /* skip 9M not supported in ht*/
392 if (idx >= IWL_RATE_9M_INDEX)
393 idx += 1;
394 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
395 return idx;
396
397 /* 4965 legacy rate format, search for match in table */
398 } else {
399 for (idx = 0; idx < ARRAY_SIZE(iwl4965_rates); idx++)
400 if (iwl4965_rates[idx].plcp == (rate_n_flags & 0xFF))
401 return idx;
402 }
403
404 return -1;
405}
406
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800407/**
408 * translate ucode response to mac80211 tx status control values
409 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700410void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800411 struct ieee80211_tx_control *control)
412{
413 int rate_index;
414
415 control->antenna_sel_tx =
Guy Cohenfde0db32008-04-21 15:42:01 -0700416 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800417 if (rate_n_flags & RATE_MCS_HT_MSK)
418 control->flags |= IEEE80211_TXCTL_OFDM_HT;
419 if (rate_n_flags & RATE_MCS_GF_MSK)
420 control->flags |= IEEE80211_TXCTL_GREEN_FIELD;
421 if (rate_n_flags & RATE_MCS_FAT_MSK)
422 control->flags |= IEEE80211_TXCTL_40_MHZ_WIDTH;
423 if (rate_n_flags & RATE_MCS_DUP_MSK)
424 control->flags |= IEEE80211_TXCTL_DUP_DATA;
425 if (rate_n_flags & RATE_MCS_SGI_MSK)
426 control->flags |= IEEE80211_TXCTL_SHORT_GI;
427 /* since iwl4965_hwrate_to_plcp_idx is band indifferent, we always use
428 * IEEE80211_BAND_2GHZ band as it contains all the rates */
429 rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
430 if (rate_index == -1)
431 control->tx_rate = NULL;
432 else
433 control->tx_rate =
434 &priv->bands[IEEE80211_BAND_2GHZ].bitrates[rate_index];
435}
Tomas Winkler17744ff2008-03-02 01:52:00 +0200436
Zhu Yib481de92007-09-25 17:54:57 -0700437/*
438 * Determine how many receiver/antenna chains to use.
439 * More provides better reception via diversity. Fewer saves power.
440 * MIMO (dual stream) requires at least 2, but works better with 3.
441 * This does not determine *which* chains to use, just how many.
442 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700443static int iwl4965_get_rx_chain_counter(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700444 u8 *idle_state, u8 *rx_state)
445{
Guy Cohenfde0db32008-04-21 15:42:01 -0700446 u8 is_single = is_single_rx_stream(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700447 u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
448
449 /* # of Rx chains to use when expecting MIMO. */
450 if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
451 *rx_state = 2;
452 else
453 *rx_state = 3;
454
455 /* # Rx chains when idling and maybe trying to save power */
456 switch (priv->ps_mode) {
457 case IWL_MIMO_PS_STATIC:
458 case IWL_MIMO_PS_DYNAMIC:
459 *idle_state = (is_cam) ? 2 : 1;
460 break;
461 case IWL_MIMO_PS_NONE:
462 *idle_state = (is_cam) ? *rx_state : 1;
463 break;
464 default:
465 *idle_state = 1;
466 break;
467 }
468
469 return 0;
470}
471
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700472int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700473{
474 int rc;
475 unsigned long flags;
476
477 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700478 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700479 if (rc) {
480 spin_unlock_irqrestore(&priv->lock, flags);
481 return rc;
482 }
483
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800484 /* stop Rx DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700485 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
486 rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
Zhu Yib481de92007-09-25 17:54:57 -0700487 (1 << 24), 1000);
488 if (rc < 0)
489 IWL_ERROR("Can't stop Rx DMA.\n");
490
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700491 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700492 spin_unlock_irqrestore(&priv->lock, flags);
493
494 return 0;
495}
496
Tomas Winkler079a2532008-04-17 16:03:39 -0700497int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
Zhu Yib481de92007-09-25 17:54:57 -0700498{
Tomas Winklerd8609652007-10-25 17:15:35 +0800499 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700500 unsigned long flags;
501
502 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700503 ret = iwl_grab_nic_access(priv);
Tomas Winklerd8609652007-10-25 17:15:35 +0800504 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700505 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winklerd8609652007-10-25 17:15:35 +0800506 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700507 }
508
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700509 if (src == IWL_PWR_SRC_VAUX) {
Zhu Yib481de92007-09-25 17:54:57 -0700510 u32 val;
Tomas Winklerd8609652007-10-25 17:15:35 +0800511 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700512 &val);
Zhu Yib481de92007-09-25 17:54:57 -0700513
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700514 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700515 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700516 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
517 ~APMG_PS_CTRL_MSK_PWR_SRC);
518 }
519 } else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700520 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700521 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
522 ~APMG_PS_CTRL_MSK_PWR_SRC);
523 }
Zhu Yib481de92007-09-25 17:54:57 -0700524
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700525 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700526 spin_unlock_irqrestore(&priv->lock, flags);
527
Tomas Winklerd8609652007-10-25 17:15:35 +0800528 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700529}
530
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700531static int iwl4965_rx_init(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -0700532{
Tomas Winkler059ff822008-04-14 21:16:14 -0700533 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700534 unsigned long flags;
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200535 unsigned int rb_size;
Zhu Yib481de92007-09-25 17:54:57 -0700536
537 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler059ff822008-04-14 21:16:14 -0700538 ret = iwl_grab_nic_access(priv);
539 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700540 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler059ff822008-04-14 21:16:14 -0700541 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700542 }
543
Assaf Krauss1ea87392008-03-18 14:57:50 -0700544 if (priv->cfg->mod_params->amsdu_size_8K)
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200545 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
546 else
547 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
548
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800549 /* Stop Rx DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700550 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700551
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800552 /* Reset driver's Rx queue write index */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700553 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800554
555 /* Tell device where to find RBD circular buffer in DRAM */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700556 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
557 rxq->dma_addr >> 8);
Zhu Yib481de92007-09-25 17:54:57 -0700558
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800559 /* Tell device where in DRAM to update its Rx status */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700560 iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
Tomas Winkler059ff822008-04-14 21:16:14 -0700561 (priv->shared_phys +
562 offsetof(struct iwl4965_shared, rb_closed)) >> 4);
Zhu Yib481de92007-09-25 17:54:57 -0700563
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800564 /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700565 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
566 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
567 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
568 rb_size |
Tomas Winkler059ff822008-04-14 21:16:14 -0700569 /* 0x10 << 4 | */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700570 (RX_QUEUE_SIZE_LOG <<
Zhu Yib481de92007-09-25 17:54:57 -0700571 FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
572
573 /*
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700574 * iwl_write32(priv,CSR_INT_COAL_REG,0);
Zhu Yib481de92007-09-25 17:54:57 -0700575 */
576
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700577 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700578 spin_unlock_irqrestore(&priv->lock, flags);
579
580 return 0;
581}
582
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800583/* Tell 4965 where to find the "keep warm" buffer */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700584static int iwl4965_kw_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700585{
586 unsigned long flags;
587 int rc;
588
589 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700590 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700591 if (rc)
592 goto out;
593
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700594 iwl_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
Zhu Yib481de92007-09-25 17:54:57 -0700595 priv->kw.dma_addr >> 4);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700596 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700597out:
598 spin_unlock_irqrestore(&priv->lock, flags);
599 return rc;
600}
601
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700602static int iwl4965_kw_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700603{
604 struct pci_dev *dev = priv->pci_dev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800605 struct iwl4965_kw *kw = &priv->kw;
Zhu Yib481de92007-09-25 17:54:57 -0700606
607 kw->size = IWL4965_KW_SIZE; /* TBW need set somewhere else */
608 kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
609 if (!kw->v_addr)
610 return -ENOMEM;
611
612 return 0;
613}
614
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800615/**
616 * iwl4965_kw_free - Free the "keep warm" buffer
617 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700618static void iwl4965_kw_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700619{
620 struct pci_dev *dev = priv->pci_dev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800621 struct iwl4965_kw *kw = &priv->kw;
Zhu Yib481de92007-09-25 17:54:57 -0700622
623 if (kw->v_addr) {
624 pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
625 memset(kw, 0, sizeof(*kw));
626 }
627}
628
629/**
630 * iwl4965_txq_ctx_reset - Reset TX queue context
631 * Destroys all DMA structures and initialise them again
632 *
633 * @param priv
634 * @return error code
635 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700636static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700637{
638 int rc = 0;
639 int txq_id, slots_num;
640 unsigned long flags;
641
642 iwl4965_kw_free(priv);
643
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800644 /* Free all tx/cmd queues and keep-warm buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800645 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700646
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800647 /* Alloc keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700648 rc = iwl4965_kw_alloc(priv);
649 if (rc) {
650 IWL_ERROR("Keep Warm allocation failed");
651 goto error_kw;
652 }
653
654 spin_lock_irqsave(&priv->lock, flags);
655
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700656 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700657 if (unlikely(rc)) {
658 IWL_ERROR("TX reset failed");
659 spin_unlock_irqrestore(&priv->lock, flags);
660 goto error_reset;
661 }
662
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800663 /* Turn off all Tx DMA channels */
Tomas Winkler12a81f62008-04-03 16:05:20 -0700664 iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700665 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700666 spin_unlock_irqrestore(&priv->lock, flags);
667
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800668 /* Tell 4965 where to find the keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700669 rc = iwl4965_kw_init(priv);
670 if (rc) {
671 IWL_ERROR("kw_init failed\n");
672 goto error_reset;
673 }
674
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800675 /* Alloc and init all (default 16) Tx queues,
676 * including the command queue (#4) */
Tomas Winkler5425e492008-04-15 16:01:38 -0700677 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
Zhu Yib481de92007-09-25 17:54:57 -0700678 slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
679 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800680 rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
Zhu Yib481de92007-09-25 17:54:57 -0700681 txq_id);
682 if (rc) {
683 IWL_ERROR("Tx %d queue init failed\n", txq_id);
684 goto error;
685 }
686 }
687
688 return rc;
689
690 error:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800691 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700692 error_reset:
693 iwl4965_kw_free(priv);
694 error_kw:
695 return rc;
696}
Tomas Winkler91238712008-04-23 17:14:53 -0700697static int iwl4965_apm_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700698{
Zhu Yib481de92007-09-25 17:54:57 -0700699 unsigned long flags;
Tomas Winkler91238712008-04-23 17:14:53 -0700700 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700701
Zhu Yib481de92007-09-25 17:54:57 -0700702 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700703 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler91238712008-04-23 17:14:53 -0700704 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yib481de92007-09-25 17:54:57 -0700705
Tomas Winkler91238712008-04-23 17:14:53 -0700706 /* set "initialization complete" bit to move adapter
707 * D0U* --> D0A* state */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700708 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
Tomas Winkler91238712008-04-23 17:14:53 -0700709
710 /* wait for clock stabilization */
711 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
712 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
713 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
714 if (ret < 0) {
Zhu Yib481de92007-09-25 17:54:57 -0700715 IWL_DEBUG_INFO("Failed to init the card\n");
Tomas Winkler91238712008-04-23 17:14:53 -0700716 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700717 }
718
Tomas Winkler91238712008-04-23 17:14:53 -0700719 ret = iwl_grab_nic_access(priv);
720 if (ret)
721 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700722
Tomas Winkler91238712008-04-23 17:14:53 -0700723 /* enable DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700724 iwl_write_prph(priv, APMG_CLK_CTRL_REG,
725 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -0700726
727 udelay(20);
728
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700729 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
Tomas Winkler91238712008-04-23 17:14:53 -0700730 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Zhu Yib481de92007-09-25 17:54:57 -0700731
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700732 iwl_release_nic_access(priv);
Tomas Winkler91238712008-04-23 17:14:53 -0700733out:
734 spin_unlock_irqrestore(&priv->lock, flags);
735 return ret;
736}
737
738int iwl4965_hw_nic_init(struct iwl_priv *priv)
739{
740 unsigned long flags;
741 struct iwl4965_rx_queue *rxq = &priv->rxq;
742 u8 rev_id;
743 u8 val_link;
744 u32 val;
745 int ret;
746
747 /* nic_init */
748 priv->cfg->ops->lib->apm_ops.init(priv);
749
750 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700751 iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
Zhu Yib481de92007-09-25 17:54:57 -0700752 spin_unlock_irqrestore(&priv->lock, flags);
753
754 /* Determine HW type */
Tomas Winkler91238712008-04-23 17:14:53 -0700755 ret = pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &rev_id);
756 if (ret)
757 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700758
759 IWL_DEBUG_INFO("HW Revision ID = 0x%X\n", rev_id);
760
Tomas Winkler91238712008-04-23 17:14:53 -0700761 ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700762
Zhu Yib481de92007-09-25 17:54:57 -0700763 spin_lock_irqsave(&priv->lock, flags);
764
765 if ((rev_id & 0x80) == 0x80 && (rev_id & 0x7f) < 8) {
766 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
767 /* Enable No Snoop field */
768 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
769 val & ~(1 << 11));
770 }
771
772 spin_unlock_irqrestore(&priv->lock, flags);
773
Tomas Winkler073d3f52008-04-21 15:41:52 -0700774 if (iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET) <
775 EEPROM_4965_TX_POWER_VERSION) {
Zhu Yib481de92007-09-25 17:54:57 -0700776 IWL_ERROR("Older EEPROM detected! Aborting.\n");
777 return -EINVAL;
778 }
779
780 pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
781
782 /* disable L1 entry -- workaround for pre-B1 */
783 pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
784
785 spin_lock_irqsave(&priv->lock, flags);
786
787 /* set CSR_HW_CONFIG_REG for uCode use */
788
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700789 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
790 CSR49_HW_IF_CONFIG_REG_BIT_4965_R |
791 CSR49_HW_IF_CONFIG_REG_BIT_RADIO_SI |
792 CSR49_HW_IF_CONFIG_REG_BIT_MAC_SI);
Zhu Yib481de92007-09-25 17:54:57 -0700793
Tomas Winkler91238712008-04-23 17:14:53 -0700794 ret = iwl_grab_nic_access(priv);
795 if (ret < 0) {
Zhu Yib481de92007-09-25 17:54:57 -0700796 spin_unlock_irqrestore(&priv->lock, flags);
797 IWL_DEBUG_INFO("Failed to init the card\n");
Tomas Winkler91238712008-04-23 17:14:53 -0700798 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700799 }
800
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700801 iwl_read_prph(priv, APMG_PS_CTRL_REG);
802 iwl_set_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
Zhu Yib481de92007-09-25 17:54:57 -0700803 udelay(5);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700804 iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
Zhu Yib481de92007-09-25 17:54:57 -0700805
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700806 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700807 spin_unlock_irqrestore(&priv->lock, flags);
808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800809 iwl4965_hw_card_show_info(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700810
811 /* end nic_init */
812
813 /* Allocate the RX queue, or reset if it is already allocated */
814 if (!rxq->bd) {
Tomas Winkler91238712008-04-23 17:14:53 -0700815 ret = iwl4965_rx_queue_alloc(priv);
816 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700817 IWL_ERROR("Unable to initialize Rx queue\n");
818 return -ENOMEM;
819 }
820 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800821 iwl4965_rx_queue_reset(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -0700822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700824
825 iwl4965_rx_init(priv, rxq);
826
827 spin_lock_irqsave(&priv->lock, flags);
828
829 rxq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800830 iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -0700831
Tomas Winkler073d3f52008-04-21 15:41:52 -0700832 /* init the txpower calibration pointer */
833 priv->calib_info = (struct iwl_eeprom_calib_info *)
834 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
835
Zhu Yib481de92007-09-25 17:54:57 -0700836 spin_unlock_irqrestore(&priv->lock, flags);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800837
838 /* Allocate and init all Tx and Command queues */
Tomas Winkler91238712008-04-23 17:14:53 -0700839 ret = iwl4965_txq_ctx_reset(priv);
840 if (ret)
841 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700842
843 set_bit(STATUS_INIT, &priv->status);
844
845 return 0;
846}
847
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700848int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700849{
850 int rc = 0;
851 u32 reg_val;
852 unsigned long flags;
853
854 spin_lock_irqsave(&priv->lock, flags);
855
856 /* set stop master bit */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700857 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
Zhu Yib481de92007-09-25 17:54:57 -0700858
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700859 reg_val = iwl_read32(priv, CSR_GP_CNTRL);
Zhu Yib481de92007-09-25 17:54:57 -0700860
861 if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
862 (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
863 IWL_DEBUG_INFO("Card in power save, master is already "
864 "stopped\n");
865 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700866 rc = iwl_poll_bit(priv, CSR_RESET,
Zhu Yib481de92007-09-25 17:54:57 -0700867 CSR_RESET_REG_FLAG_MASTER_DISABLED,
868 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
869 if (rc < 0) {
870 spin_unlock_irqrestore(&priv->lock, flags);
871 return rc;
872 }
873 }
874
875 spin_unlock_irqrestore(&priv->lock, flags);
876 IWL_DEBUG_INFO("stop master\n");
877
878 return rc;
879}
880
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800881/**
882 * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
883 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700884void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700885{
886
887 int txq_id;
888 unsigned long flags;
889
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800890 /* Stop each Tx DMA channel, and wait for it to be idle */
Tomas Winkler5425e492008-04-15 16:01:38 -0700891 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
Zhu Yib481de92007-09-25 17:54:57 -0700892 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700893 if (iwl_grab_nic_access(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -0700894 spin_unlock_irqrestore(&priv->lock, flags);
895 continue;
896 }
897
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700898 iwl_write_direct32(priv,
899 IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
900 iwl_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
901 IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
902 (txq_id), 200);
903 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700904 spin_unlock_irqrestore(&priv->lock, flags);
905 }
906
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800907 /* Deallocate memory for all Tx queues */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800908 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700909}
910
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700911int iwl4965_hw_nic_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700912{
913 int rc = 0;
914 unsigned long flags;
915
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800916 iwl4965_hw_nic_stop_master(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700917
918 spin_lock_irqsave(&priv->lock, flags);
919
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700920 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Zhu Yib481de92007-09-25 17:54:57 -0700921
922 udelay(10);
923
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700924 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
925 rc = iwl_poll_bit(priv, CSR_RESET,
Zhu Yib481de92007-09-25 17:54:57 -0700926 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
927 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
928
929 udelay(10);
930
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700931 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700932 if (!rc) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700933 iwl_write_prph(priv, APMG_CLK_EN_REG,
934 APMG_CLK_VAL_DMA_CLK_RQT |
935 APMG_CLK_VAL_BSM_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -0700936
937 udelay(10);
938
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700939 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
940 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Zhu Yib481de92007-09-25 17:54:57 -0700941
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700942 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700943 }
944
945 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
946 wake_up_interruptible(&priv->wait_command_queue);
947
948 spin_unlock_irqrestore(&priv->lock, flags);
949
950 return rc;
951
952}
953
954#define REG_RECALIB_PERIOD (60)
955
956/**
957 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
958 *
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700959 * This callback is provided in order to send a statistics request.
Zhu Yib481de92007-09-25 17:54:57 -0700960 *
961 * This timer function is continually reset to execute within
962 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
963 * was received. We need to ensure we receive the statistics in order
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700964 * to update the temperature used for calibrating the TXPOWER.
Zhu Yib481de92007-09-25 17:54:57 -0700965 */
966static void iwl4965_bg_statistics_periodic(unsigned long data)
967{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700968 struct iwl_priv *priv = (struct iwl_priv *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700969
Zhu Yib481de92007-09-25 17:54:57 -0700970 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
971 return;
972
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700973 iwl_send_statistics_request(priv, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700974}
975
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700976void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700977{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800978 struct iwl4965_ct_kill_config cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700979 unsigned long flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700980 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700981
982 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700983 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -0700984 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
985 spin_unlock_irqrestore(&priv->lock, flags);
986
Ron Rindjunsky099b40b2008-04-21 15:41:53 -0700987 cmd.critical_temperature_R =
Emmanuel Grumbachb73cdf22008-04-21 15:41:58 -0700988 cpu_to_le32(priv->hw_params.ct_kill_threshold);
989
Tomas Winkler857485c2008-03-21 13:53:44 -0700990 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
991 sizeof(cmd), &cmd);
992 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -0700993 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
994 else
Emmanuel Grumbachb73cdf22008-04-21 15:41:58 -0700995 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
996 "critical temperature is %d\n",
997 cmd.critical_temperature_R);
Zhu Yib481de92007-09-25 17:54:57 -0700998}
999
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001000#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07001001
1002/* Reset differential Rx gains in NIC to prepare for chain noise calibration.
1003 * Called after every association, but this runs only once!
1004 * ... once chain noise is calibrated the first time, it's good forever. */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001005static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001006{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001007 struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
Zhu Yib481de92007-09-25 17:54:57 -07001008
Tomas Winkler3109ece2008-03-28 16:33:35 -07001009 if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001010 struct iwl4965_calibration_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001011
1012 memset(&cmd, 0, sizeof(cmd));
1013 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1014 cmd.diff_gain_a = 0;
1015 cmd.diff_gain_b = 0;
1016 cmd.diff_gain_c = 0;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001017 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1018 sizeof(cmd), &cmd))
1019 IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
Zhu Yib481de92007-09-25 17:54:57 -07001020 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1021 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
1022 }
Zhu Yib481de92007-09-25 17:54:57 -07001023}
1024
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001025static void iwl4965_gain_computation(struct iwl_priv *priv,
1026 u32 *average_noise,
1027 u16 min_average_noise_antenna_i,
1028 u32 min_average_noise)
Zhu Yib481de92007-09-25 17:54:57 -07001029{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001030 int i, ret;
1031 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
Zhu Yib481de92007-09-25 17:54:57 -07001032
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001033 data->delta_gain_code[min_average_noise_antenna_i] = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001034
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001035 for (i = 0; i < NUM_RX_CHAINS; i++) {
1036 s32 delta_g = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001037
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001038 if (!(data->disconn_array[i]) &&
1039 (data->delta_gain_code[i] ==
Zhu Yib481de92007-09-25 17:54:57 -07001040 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001041 delta_g = average_noise[i] - min_average_noise;
1042 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
1043 data->delta_gain_code[i] =
1044 min(data->delta_gain_code[i],
1045 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
Zhu Yib481de92007-09-25 17:54:57 -07001046
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001047 data->delta_gain_code[i] =
1048 (data->delta_gain_code[i] | (1 << 2));
1049 } else {
1050 data->delta_gain_code[i] = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001051 }
Zhu Yib481de92007-09-25 17:54:57 -07001052 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001053 IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
1054 data->delta_gain_code[0],
1055 data->delta_gain_code[1],
1056 data->delta_gain_code[2]);
Zhu Yib481de92007-09-25 17:54:57 -07001057
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001058 /* Differential gain gets sent to uCode only once */
1059 if (!data->radio_write) {
1060 struct iwl4965_calibration_cmd cmd;
1061 data->radio_write = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001062
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001063 memset(&cmd, 0, sizeof(cmd));
1064 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1065 cmd.diff_gain_a = data->delta_gain_code[0];
1066 cmd.diff_gain_b = data->delta_gain_code[1];
1067 cmd.diff_gain_c = data->delta_gain_code[2];
1068 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1069 sizeof(cmd), &cmd);
1070 if (ret)
1071 IWL_DEBUG_CALIB("fail sending cmd "
1072 "REPLY_PHY_CALIBRATION_CMD \n");
Zhu Yib481de92007-09-25 17:54:57 -07001073
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001074 /* TODO we might want recalculate
1075 * rx_chain in rxon cmd */
1076
1077 /* Mark so we run this algo only once! */
1078 data->state = IWL_CHAIN_NOISE_CALIBRATED;
Zhu Yib481de92007-09-25 17:54:57 -07001079 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001080 data->chain_noise_a = 0;
1081 data->chain_noise_b = 0;
1082 data->chain_noise_c = 0;
1083 data->chain_signal_a = 0;
1084 data->chain_signal_b = 0;
1085 data->chain_signal_c = 0;
1086 data->beacon_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001087}
1088
1089static void iwl4965_bg_sensitivity_work(struct work_struct *work)
1090{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001091 struct iwl_priv *priv = container_of(work, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07001092 sensitivity_work);
1093
1094 mutex_lock(&priv->mutex);
1095
1096 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1097 test_bit(STATUS_SCANNING, &priv->status)) {
1098 mutex_unlock(&priv->mutex);
1099 return;
1100 }
1101
1102 if (priv->start_calib) {
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001103 iwl_chain_noise_calibration(priv, &priv->statistics);
Zhu Yib481de92007-09-25 17:54:57 -07001104
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001105 iwl_sensitivity_calibration(priv, &priv->statistics);
Zhu Yib481de92007-09-25 17:54:57 -07001106 }
1107
1108 mutex_unlock(&priv->mutex);
1109 return;
1110}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001111#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
Zhu Yib481de92007-09-25 17:54:57 -07001112
1113static void iwl4965_bg_txpower_work(struct work_struct *work)
1114{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001115 struct iwl_priv *priv = container_of(work, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07001116 txpower_work);
1117
1118 /* If a scan happened to start before we got here
1119 * then just return; the statistics notification will
1120 * kick off another scheduled work to compensate for
1121 * any temperature delta we missed here. */
1122 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1123 test_bit(STATUS_SCANNING, &priv->status))
1124 return;
1125
1126 mutex_lock(&priv->mutex);
1127
1128 /* Regardless of if we are assocaited, we must reconfigure the
1129 * TX power since frames can be sent on non-radar channels while
1130 * not associated */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001131 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001132
1133 /* Update last_temperature to keep is_calib_needed from running
1134 * when it isn't needed... */
1135 priv->last_temperature = priv->temperature;
1136
1137 mutex_unlock(&priv->mutex);
1138}
1139
1140/*
1141 * Acquire priv->lock before calling this function !
1142 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001143static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
Zhu Yib481de92007-09-25 17:54:57 -07001144{
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001145 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Zhu Yib481de92007-09-25 17:54:57 -07001146 (index & 0xff) | (txq_id << 8));
Tomas Winkler12a81f62008-04-03 16:05:20 -07001147 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
Zhu Yib481de92007-09-25 17:54:57 -07001148}
1149
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001150/**
1151 * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
1152 * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
1153 * @scd_retry: (1) Indicates queue will be used in aggregation mode
1154 *
1155 * NOTE: Acquire priv->lock before calling this function !
Zhu Yib481de92007-09-25 17:54:57 -07001156 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001157static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001158 struct iwl4965_tx_queue *txq,
Zhu Yib481de92007-09-25 17:54:57 -07001159 int tx_fifo_id, int scd_retry)
1160{
1161 int txq_id = txq->q.id;
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001162
1163 /* Find out whether to activate Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001164 int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
1165
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001166 /* Set up and activate */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001167 iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
Zhu Yib481de92007-09-25 17:54:57 -07001168 (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1169 (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
1170 (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
1171 (scd_retry << SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
1172 SCD_QUEUE_STTS_REG_MSK);
1173
1174 txq->sched_retry = scd_retry;
1175
1176 IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001177 active ? "Activate" : "Deactivate",
Zhu Yib481de92007-09-25 17:54:57 -07001178 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
1179}
1180
1181static const u16 default_queue_to_tx_fifo[] = {
1182 IWL_TX_FIFO_AC3,
1183 IWL_TX_FIFO_AC2,
1184 IWL_TX_FIFO_AC1,
1185 IWL_TX_FIFO_AC0,
1186 IWL_CMD_FIFO_NUM,
1187 IWL_TX_FIFO_HCCA_1,
1188 IWL_TX_FIFO_HCCA_2
1189};
1190
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001191static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
Zhu Yib481de92007-09-25 17:54:57 -07001192{
1193 set_bit(txq_id, &priv->txq_ctx_active_msk);
1194}
1195
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001196static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
Zhu Yib481de92007-09-25 17:54:57 -07001197{
1198 clear_bit(txq_id, &priv->txq_ctx_active_msk);
1199}
1200
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001201int iwl4965_alive_notify(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001202{
1203 u32 a;
1204 int i = 0;
1205 unsigned long flags;
Tomas Winkler857485c2008-03-21 13:53:44 -07001206 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001207
1208 spin_lock_irqsave(&priv->lock, flags);
1209
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001210#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07001211 memset(&(priv->sensitivity_data), 0,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001212 sizeof(struct iwl_sensitivity_data));
Zhu Yib481de92007-09-25 17:54:57 -07001213 memset(&(priv->chain_noise_data), 0,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001214 sizeof(struct iwl_chain_noise_data));
Zhu Yib481de92007-09-25 17:54:57 -07001215 for (i = 0; i < NUM_RX_CHAINS; i++)
1216 priv->chain_noise_data.delta_gain_code[i] =
1217 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001218#endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001219 ret = iwl_grab_nic_access(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07001220 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001221 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler857485c2008-03-21 13:53:44 -07001222 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001223 }
1224
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001225 /* Clear 4965's internal Tx Scheduler data base */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001226 priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
Zhu Yib481de92007-09-25 17:54:57 -07001227 a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
1228 for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001229 iwl_write_targ_mem(priv, a, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001230 for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001231 iwl_write_targ_mem(priv, a, 0);
Tomas Winkler5425e492008-04-15 16:01:38 -07001232 for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001233 iwl_write_targ_mem(priv, a, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001234
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001235 /* Tel 4965 where to find Tx byte count tables */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001236 iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
Tomas Winkler059ff822008-04-14 21:16:14 -07001237 (priv->shared_phys +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001238 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001239
1240 /* Disable chain mode for all queues */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001241 iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001242
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001243 /* Initialize each Tx queue (including the command queue) */
Tomas Winkler5425e492008-04-15 16:01:38 -07001244 for (i = 0; i < priv->hw_params.max_txq_num; i++) {
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001245
1246 /* TFD circular buffer read/write indexes */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001247 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001248 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001249
1250 /* Max Tx Window size for Scheduler-ACK mode */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001251 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07001252 SCD_CONTEXT_QUEUE_OFFSET(i),
1253 (SCD_WIN_SIZE <<
1254 SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
1255 SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001256
1257 /* Frame limit */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001258 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07001259 SCD_CONTEXT_QUEUE_OFFSET(i) +
1260 sizeof(u32),
1261 (SCD_FRAME_LIMIT <<
1262 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1263 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
1264
1265 }
Tomas Winkler12a81f62008-04-03 16:05:20 -07001266 iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
Tomas Winkler5425e492008-04-15 16:01:38 -07001267 (1 << priv->hw_params.max_txq_num) - 1);
Zhu Yib481de92007-09-25 17:54:57 -07001268
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001269 /* Activate all Tx DMA/FIFO channels */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001270 iwl_write_prph(priv, IWL49_SCD_TXFACT,
Zhu Yib481de92007-09-25 17:54:57 -07001271 SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
1272
1273 iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001274
1275 /* Map each Tx/cmd queue to its corresponding fifo */
Zhu Yib481de92007-09-25 17:54:57 -07001276 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
1277 int ac = default_queue_to_tx_fifo[i];
1278 iwl4965_txq_ctx_activate(priv, i);
1279 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
1280 }
1281
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001282 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001283 spin_unlock_irqrestore(&priv->lock, flags);
1284
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07001285 /* Ask for statistics now, the uCode will send statistics notification
1286 * periodically after association */
1287 iwl_send_statistics_request(priv, CMD_ASYNC);
Tomas Winkler857485c2008-03-21 13:53:44 -07001288 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001289}
1290
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001291#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1292static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
1293 .min_nrg_cck = 97,
1294 .max_nrg_cck = 0,
1295
1296 .auto_corr_min_ofdm = 85,
1297 .auto_corr_min_ofdm_mrc = 170,
1298 .auto_corr_min_ofdm_x1 = 105,
1299 .auto_corr_min_ofdm_mrc_x1 = 220,
1300
1301 .auto_corr_max_ofdm = 120,
1302 .auto_corr_max_ofdm_mrc = 210,
1303 .auto_corr_max_ofdm_x1 = 140,
1304 .auto_corr_max_ofdm_mrc_x1 = 270,
1305
1306 .auto_corr_min_cck = 125,
1307 .auto_corr_max_cck = 200,
1308 .auto_corr_min_cck_mrc = 200,
1309 .auto_corr_max_cck_mrc = 400,
1310
1311 .nrg_th_cck = 100,
1312 .nrg_th_ofdm = 100,
1313};
1314#endif
1315
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001316/**
Tomas Winkler5425e492008-04-15 16:01:38 -07001317 * iwl4965_hw_set_hw_params
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001318 *
1319 * Called when initializing driver
1320 */
Tomas Winkler5425e492008-04-15 16:01:38 -07001321int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001322{
Assaf Krauss316c30d2008-03-14 10:38:46 -07001323
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07001324 if ((priv->cfg->mod_params->num_of_queues > IWL4965_MAX_NUM_QUEUES) ||
Assaf Krauss1ea87392008-03-18 14:57:50 -07001325 (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07001326 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07001327 IWL_MIN_NUM_QUEUES, IWL4965_MAX_NUM_QUEUES);
Tomas Winkler059ff822008-04-14 21:16:14 -07001328 return -EINVAL;
Assaf Krauss316c30d2008-03-14 10:38:46 -07001329 }
1330
Tomas Winkler5425e492008-04-15 16:01:38 -07001331 priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001332 priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
Tomas Winkler5425e492008-04-15 16:01:38 -07001333 priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
1334 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1335 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
Assaf Krauss1ea87392008-03-18 14:57:50 -07001336 if (priv->cfg->mod_params->amsdu_size_8K)
Tomas Winkler5425e492008-04-15 16:01:38 -07001337 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02001338 else
Tomas Winkler5425e492008-04-15 16:01:38 -07001339 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
1340 priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
1341 priv->hw_params.max_stations = IWL4965_STATION_COUNT;
1342 priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
Tomas Winkler3e82a822008-02-13 11:32:31 -08001343
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001344 priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
1345 priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
1346 priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
1347 priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
1348
Tomas Winklerec35cf22008-04-15 16:01:39 -07001349 priv->hw_params.tx_chains_num = 2;
1350 priv->hw_params.rx_chains_num = 2;
Guy Cohenfde0db32008-04-21 15:42:01 -07001351 priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
1352 priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07001353 priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
1354
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001355#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1356 priv->hw_params.sens = &iwl4965_sensitivity;
1357#endif
Tomas Winkler3e82a822008-02-13 11:32:31 -08001358
Tomas Winkler059ff822008-04-14 21:16:14 -07001359 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001360}
1361
1362/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001363 * iwl4965_hw_txq_ctx_free - Free TXQ Context
Zhu Yib481de92007-09-25 17:54:57 -07001364 *
1365 * Destroy all TX DMA queues and structures
1366 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001367void iwl4965_hw_txq_ctx_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001368{
1369 int txq_id;
1370
1371 /* Tx queues */
Tomas Winkler5425e492008-04-15 16:01:38 -07001372 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001373 iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
Zhu Yib481de92007-09-25 17:54:57 -07001374
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001375 /* Keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001376 iwl4965_kw_free(priv);
1377}
1378
1379/**
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001380 * iwl4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Zhu Yib481de92007-09-25 17:54:57 -07001381 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001382 * Does NOT advance any TFD circular buffer read/write indexes
1383 * Does NOT free the TFD itself (which is within circular buffer)
Zhu Yib481de92007-09-25 17:54:57 -07001384 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001385int iwl4965_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07001386{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001387 struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
1388 struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001389 struct pci_dev *dev = priv->pci_dev;
1390 int i;
1391 int counter = 0;
1392 int index, is_odd;
1393
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001394 /* Host command buffers stay mapped in memory, nothing to clean */
Zhu Yib481de92007-09-25 17:54:57 -07001395 if (txq->q.id == IWL_CMD_QUEUE_NUM)
Zhu Yib481de92007-09-25 17:54:57 -07001396 return 0;
1397
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001398 /* Sanity check on number of chunks */
Zhu Yib481de92007-09-25 17:54:57 -07001399 counter = IWL_GET_BITS(*bd, num_tbs);
1400 if (counter > MAX_NUM_OF_TBS) {
1401 IWL_ERROR("Too many chunks: %i\n", counter);
1402 /* @todo issue fatal error, it is quite serious situation */
1403 return 0;
1404 }
1405
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001406 /* Unmap chunks, if any.
1407 * TFD info for odd chunks is different format than for even chunks. */
Zhu Yib481de92007-09-25 17:54:57 -07001408 for (i = 0; i < counter; i++) {
1409 index = i / 2;
1410 is_odd = i & 0x1;
1411
1412 if (is_odd)
1413 pci_unmap_single(
1414 dev,
1415 IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
1416 (IWL_GET_BITS(bd->pa[index],
1417 tb2_addr_hi20) << 16),
1418 IWL_GET_BITS(bd->pa[index], tb2_len),
1419 PCI_DMA_TODEVICE);
1420
1421 else if (i > 0)
1422 pci_unmap_single(dev,
1423 le32_to_cpu(bd->pa[index].tb1_addr),
1424 IWL_GET_BITS(bd->pa[index], tb1_len),
1425 PCI_DMA_TODEVICE);
1426
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001427 /* Free SKB, if any, for this chunk */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001428 if (txq->txb[txq->q.read_ptr].skb[i]) {
1429 struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
Zhu Yib481de92007-09-25 17:54:57 -07001430
1431 dev_kfree_skb(skb);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001432 txq->txb[txq->q.read_ptr].skb[i] = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001433 }
1434 }
1435 return 0;
1436}
1437
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001438/* set card power command */
1439static int iwl4965_set_power(struct iwl_priv *priv,
1440 void *cmd)
1441{
1442 int ret = 0;
1443
1444 ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1445 sizeof(struct iwl4965_powertable_cmd),
1446 cmd, NULL);
1447 return ret;
1448}
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001449int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
Zhu Yib481de92007-09-25 17:54:57 -07001450{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001451 IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001452 return -EINVAL;
1453}
1454
1455static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1456{
1457 s32 sign = 1;
1458
1459 if (num < 0) {
1460 sign = -sign;
1461 num = -num;
1462 }
1463 if (denom < 0) {
1464 sign = -sign;
1465 denom = -denom;
1466 }
1467 *res = 1;
1468 *res = ((num * 2 + denom) / (denom * 2)) * sign;
1469
1470 return 1;
1471}
1472
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001473/**
1474 * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1475 *
1476 * Determines power supply voltage compensation for txpower calculations.
1477 * Returns number of 1/2-dB steps to subtract from gain table index,
1478 * to compensate for difference between power supply voltage during
1479 * factory measurements, vs. current power supply voltage.
1480 *
1481 * Voltage indication is higher for lower voltage.
1482 * Lower voltage requires more gain (lower gain table index).
1483 */
Zhu Yib481de92007-09-25 17:54:57 -07001484static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1485 s32 current_voltage)
1486{
1487 s32 comp = 0;
1488
1489 if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1490 (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1491 return 0;
1492
1493 iwl4965_math_div_round(current_voltage - eeprom_voltage,
1494 TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1495
1496 if (current_voltage > eeprom_voltage)
1497 comp *= 2;
1498 if ((comp < -2) || (comp > 2))
1499 comp = 0;
1500
1501 return comp;
1502}
1503
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001504static const struct iwl_channel_info *
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001505iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001506 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07001507{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001508 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001509
Assaf Krauss8622e702008-03-21 13:53:43 -07001510 ch_info = iwl_get_channel_info(priv, band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07001511
1512 if (!is_channel_valid(ch_info))
1513 return NULL;
1514
1515 return ch_info;
1516}
1517
1518static s32 iwl4965_get_tx_atten_grp(u16 channel)
1519{
1520 if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1521 channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1522 return CALIB_CH_GROUP_5;
1523
1524 if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1525 channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1526 return CALIB_CH_GROUP_1;
1527
1528 if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1529 channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1530 return CALIB_CH_GROUP_2;
1531
1532 if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1533 channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1534 return CALIB_CH_GROUP_3;
1535
1536 if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1537 channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1538 return CALIB_CH_GROUP_4;
1539
1540 IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1541 return -1;
1542}
1543
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001544static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
Zhu Yib481de92007-09-25 17:54:57 -07001545{
1546 s32 b = -1;
1547
1548 for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
Tomas Winkler073d3f52008-04-21 15:41:52 -07001549 if (priv->calib_info->band_info[b].ch_from == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001550 continue;
1551
Tomas Winkler073d3f52008-04-21 15:41:52 -07001552 if ((channel >= priv->calib_info->band_info[b].ch_from)
1553 && (channel <= priv->calib_info->band_info[b].ch_to))
Zhu Yib481de92007-09-25 17:54:57 -07001554 break;
1555 }
1556
1557 return b;
1558}
1559
1560static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1561{
1562 s32 val;
1563
1564 if (x2 == x1)
1565 return y1;
1566 else {
1567 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1568 return val + y2;
1569 }
1570}
1571
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001572/**
1573 * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1574 *
1575 * Interpolates factory measurements from the two sample channels within a
1576 * sub-band, to apply to channel of interest. Interpolation is proportional to
1577 * differences in channel frequencies, which is proportional to differences
1578 * in channel number.
1579 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001580static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
Tomas Winkler073d3f52008-04-21 15:41:52 -07001581 struct iwl_eeprom_calib_ch_info *chan_info)
Zhu Yib481de92007-09-25 17:54:57 -07001582{
1583 s32 s = -1;
1584 u32 c;
1585 u32 m;
Tomas Winkler073d3f52008-04-21 15:41:52 -07001586 const struct iwl_eeprom_calib_measure *m1;
1587 const struct iwl_eeprom_calib_measure *m2;
1588 struct iwl_eeprom_calib_measure *omeas;
Zhu Yib481de92007-09-25 17:54:57 -07001589 u32 ch_i1;
1590 u32 ch_i2;
1591
1592 s = iwl4965_get_sub_band(priv, channel);
1593 if (s >= EEPROM_TX_POWER_BANDS) {
1594 IWL_ERROR("Tx Power can not find channel %d ", channel);
1595 return -1;
1596 }
1597
Tomas Winkler073d3f52008-04-21 15:41:52 -07001598 ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1599 ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
Zhu Yib481de92007-09-25 17:54:57 -07001600 chan_info->ch_num = (u8) channel;
1601
1602 IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1603 channel, s, ch_i1, ch_i2);
1604
1605 for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1606 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
Tomas Winkler073d3f52008-04-21 15:41:52 -07001607 m1 = &(priv->calib_info->band_info[s].ch1.
Zhu Yib481de92007-09-25 17:54:57 -07001608 measurements[c][m]);
Tomas Winkler073d3f52008-04-21 15:41:52 -07001609 m2 = &(priv->calib_info->band_info[s].ch2.
Zhu Yib481de92007-09-25 17:54:57 -07001610 measurements[c][m]);
1611 omeas = &(chan_info->measurements[c][m]);
1612
1613 omeas->actual_pow =
1614 (u8) iwl4965_interpolate_value(channel, ch_i1,
1615 m1->actual_pow,
1616 ch_i2,
1617 m2->actual_pow);
1618 omeas->gain_idx =
1619 (u8) iwl4965_interpolate_value(channel, ch_i1,
1620 m1->gain_idx, ch_i2,
1621 m2->gain_idx);
1622 omeas->temperature =
1623 (u8) iwl4965_interpolate_value(channel, ch_i1,
1624 m1->temperature,
1625 ch_i2,
1626 m2->temperature);
1627 omeas->pa_det =
1628 (s8) iwl4965_interpolate_value(channel, ch_i1,
1629 m1->pa_det, ch_i2,
1630 m2->pa_det);
1631
1632 IWL_DEBUG_TXPOWER
1633 ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1634 m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1635 IWL_DEBUG_TXPOWER
1636 ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1637 m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1638 IWL_DEBUG_TXPOWER
1639 ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1640 m1->pa_det, m2->pa_det, omeas->pa_det);
1641 IWL_DEBUG_TXPOWER
1642 ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
1643 m1->temperature, m2->temperature,
1644 omeas->temperature);
1645 }
1646 }
1647
1648 return 0;
1649}
1650
1651/* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1652 * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1653static s32 back_off_table[] = {
1654 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1655 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1656 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1657 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1658 10 /* CCK */
1659};
1660
1661/* Thermal compensation values for txpower for various frequency ranges ...
1662 * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001663static struct iwl4965_txpower_comp_entry {
Zhu Yib481de92007-09-25 17:54:57 -07001664 s32 degrees_per_05db_a;
1665 s32 degrees_per_05db_a_denom;
1666} tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1667 {9, 2}, /* group 0 5.2, ch 34-43 */
1668 {4, 1}, /* group 1 5.2, ch 44-70 */
1669 {4, 1}, /* group 2 5.2, ch 71-124 */
1670 {4, 1}, /* group 3 5.2, ch 125-200 */
1671 {3, 1} /* group 4 2.4, ch all */
1672};
1673
1674static s32 get_min_power_index(s32 rate_power_index, u32 band)
1675{
1676 if (!band) {
1677 if ((rate_power_index & 7) <= 4)
1678 return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1679 }
1680 return MIN_TX_GAIN_INDEX;
1681}
1682
1683struct gain_entry {
1684 u8 dsp;
1685 u8 radio;
1686};
1687
1688static const struct gain_entry gain_table[2][108] = {
1689 /* 5.2GHz power gain index table */
1690 {
1691 {123, 0x3F}, /* highest txpower */
1692 {117, 0x3F},
1693 {110, 0x3F},
1694 {104, 0x3F},
1695 {98, 0x3F},
1696 {110, 0x3E},
1697 {104, 0x3E},
1698 {98, 0x3E},
1699 {110, 0x3D},
1700 {104, 0x3D},
1701 {98, 0x3D},
1702 {110, 0x3C},
1703 {104, 0x3C},
1704 {98, 0x3C},
1705 {110, 0x3B},
1706 {104, 0x3B},
1707 {98, 0x3B},
1708 {110, 0x3A},
1709 {104, 0x3A},
1710 {98, 0x3A},
1711 {110, 0x39},
1712 {104, 0x39},
1713 {98, 0x39},
1714 {110, 0x38},
1715 {104, 0x38},
1716 {98, 0x38},
1717 {110, 0x37},
1718 {104, 0x37},
1719 {98, 0x37},
1720 {110, 0x36},
1721 {104, 0x36},
1722 {98, 0x36},
1723 {110, 0x35},
1724 {104, 0x35},
1725 {98, 0x35},
1726 {110, 0x34},
1727 {104, 0x34},
1728 {98, 0x34},
1729 {110, 0x33},
1730 {104, 0x33},
1731 {98, 0x33},
1732 {110, 0x32},
1733 {104, 0x32},
1734 {98, 0x32},
1735 {110, 0x31},
1736 {104, 0x31},
1737 {98, 0x31},
1738 {110, 0x30},
1739 {104, 0x30},
1740 {98, 0x30},
1741 {110, 0x25},
1742 {104, 0x25},
1743 {98, 0x25},
1744 {110, 0x24},
1745 {104, 0x24},
1746 {98, 0x24},
1747 {110, 0x23},
1748 {104, 0x23},
1749 {98, 0x23},
1750 {110, 0x22},
1751 {104, 0x18},
1752 {98, 0x18},
1753 {110, 0x17},
1754 {104, 0x17},
1755 {98, 0x17},
1756 {110, 0x16},
1757 {104, 0x16},
1758 {98, 0x16},
1759 {110, 0x15},
1760 {104, 0x15},
1761 {98, 0x15},
1762 {110, 0x14},
1763 {104, 0x14},
1764 {98, 0x14},
1765 {110, 0x13},
1766 {104, 0x13},
1767 {98, 0x13},
1768 {110, 0x12},
1769 {104, 0x08},
1770 {98, 0x08},
1771 {110, 0x07},
1772 {104, 0x07},
1773 {98, 0x07},
1774 {110, 0x06},
1775 {104, 0x06},
1776 {98, 0x06},
1777 {110, 0x05},
1778 {104, 0x05},
1779 {98, 0x05},
1780 {110, 0x04},
1781 {104, 0x04},
1782 {98, 0x04},
1783 {110, 0x03},
1784 {104, 0x03},
1785 {98, 0x03},
1786 {110, 0x02},
1787 {104, 0x02},
1788 {98, 0x02},
1789 {110, 0x01},
1790 {104, 0x01},
1791 {98, 0x01},
1792 {110, 0x00},
1793 {104, 0x00},
1794 {98, 0x00},
1795 {93, 0x00},
1796 {88, 0x00},
1797 {83, 0x00},
1798 {78, 0x00},
1799 },
1800 /* 2.4GHz power gain index table */
1801 {
1802 {110, 0x3f}, /* highest txpower */
1803 {104, 0x3f},
1804 {98, 0x3f},
1805 {110, 0x3e},
1806 {104, 0x3e},
1807 {98, 0x3e},
1808 {110, 0x3d},
1809 {104, 0x3d},
1810 {98, 0x3d},
1811 {110, 0x3c},
1812 {104, 0x3c},
1813 {98, 0x3c},
1814 {110, 0x3b},
1815 {104, 0x3b},
1816 {98, 0x3b},
1817 {110, 0x3a},
1818 {104, 0x3a},
1819 {98, 0x3a},
1820 {110, 0x39},
1821 {104, 0x39},
1822 {98, 0x39},
1823 {110, 0x38},
1824 {104, 0x38},
1825 {98, 0x38},
1826 {110, 0x37},
1827 {104, 0x37},
1828 {98, 0x37},
1829 {110, 0x36},
1830 {104, 0x36},
1831 {98, 0x36},
1832 {110, 0x35},
1833 {104, 0x35},
1834 {98, 0x35},
1835 {110, 0x34},
1836 {104, 0x34},
1837 {98, 0x34},
1838 {110, 0x33},
1839 {104, 0x33},
1840 {98, 0x33},
1841 {110, 0x32},
1842 {104, 0x32},
1843 {98, 0x32},
1844 {110, 0x31},
1845 {104, 0x31},
1846 {98, 0x31},
1847 {110, 0x30},
1848 {104, 0x30},
1849 {98, 0x30},
1850 {110, 0x6},
1851 {104, 0x6},
1852 {98, 0x6},
1853 {110, 0x5},
1854 {104, 0x5},
1855 {98, 0x5},
1856 {110, 0x4},
1857 {104, 0x4},
1858 {98, 0x4},
1859 {110, 0x3},
1860 {104, 0x3},
1861 {98, 0x3},
1862 {110, 0x2},
1863 {104, 0x2},
1864 {98, 0x2},
1865 {110, 0x1},
1866 {104, 0x1},
1867 {98, 0x1},
1868 {110, 0x0},
1869 {104, 0x0},
1870 {98, 0x0},
1871 {97, 0},
1872 {96, 0},
1873 {95, 0},
1874 {94, 0},
1875 {93, 0},
1876 {92, 0},
1877 {91, 0},
1878 {90, 0},
1879 {89, 0},
1880 {88, 0},
1881 {87, 0},
1882 {86, 0},
1883 {85, 0},
1884 {84, 0},
1885 {83, 0},
1886 {82, 0},
1887 {81, 0},
1888 {80, 0},
1889 {79, 0},
1890 {78, 0},
1891 {77, 0},
1892 {76, 0},
1893 {75, 0},
1894 {74, 0},
1895 {73, 0},
1896 {72, 0},
1897 {71, 0},
1898 {70, 0},
1899 {69, 0},
1900 {68, 0},
1901 {67, 0},
1902 {66, 0},
1903 {65, 0},
1904 {64, 0},
1905 {63, 0},
1906 {62, 0},
1907 {61, 0},
1908 {60, 0},
1909 {59, 0},
1910 }
1911};
1912
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001913static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
Zhu Yib481de92007-09-25 17:54:57 -07001914 u8 is_fat, u8 ctrl_chan_high,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001915 struct iwl4965_tx_power_db *tx_power_tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001916{
1917 u8 saturation_power;
1918 s32 target_power;
1919 s32 user_target_power;
1920 s32 power_limit;
1921 s32 current_temp;
1922 s32 reg_limit;
1923 s32 current_regulatory;
1924 s32 txatten_grp = CALIB_CH_GROUP_MAX;
1925 int i;
1926 int c;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001927 const struct iwl_channel_info *ch_info = NULL;
Tomas Winkler073d3f52008-04-21 15:41:52 -07001928 struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1929 const struct iwl_eeprom_calib_measure *measurement;
Zhu Yib481de92007-09-25 17:54:57 -07001930 s16 voltage;
1931 s32 init_voltage;
1932 s32 voltage_compensation;
1933 s32 degrees_per_05db_num;
1934 s32 degrees_per_05db_denom;
1935 s32 factory_temp;
1936 s32 temperature_comp[2];
1937 s32 factory_gain_index[2];
1938 s32 factory_actual_pwr[2];
1939 s32 power_index;
1940
1941 /* Sanity check requested level (dBm) */
1942 if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1943 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1944 priv->user_txpower_limit);
1945 return -EINVAL;
1946 }
1947 if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1948 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1949 priv->user_txpower_limit);
1950 return -EINVAL;
1951 }
1952
1953 /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1954 * are used for indexing into txpower table) */
1955 user_target_power = 2 * priv->user_txpower_limit;
1956
1957 /* Get current (RXON) channel, band, width */
1958 ch_info =
Johannes Berg8318d782008-01-24 19:38:38 +01001959 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07001960
1961 IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1962 is_fat);
1963
1964 if (!ch_info)
1965 return -EINVAL;
1966
1967 /* get txatten group, used to select 1) thermal txpower adjustment
1968 * and 2) mimo txpower balance between Tx chains. */
1969 txatten_grp = iwl4965_get_tx_atten_grp(channel);
1970 if (txatten_grp < 0)
1971 return -EINVAL;
1972
1973 IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1974 channel, txatten_grp);
1975
1976 if (is_fat) {
1977 if (ctrl_chan_high)
1978 channel -= 2;
1979 else
1980 channel += 2;
1981 }
1982
1983 /* hardware txpower limits ...
1984 * saturation (clipping distortion) txpowers are in half-dBm */
1985 if (band)
Tomas Winkler073d3f52008-04-21 15:41:52 -07001986 saturation_power = priv->calib_info->saturation_power24;
Zhu Yib481de92007-09-25 17:54:57 -07001987 else
Tomas Winkler073d3f52008-04-21 15:41:52 -07001988 saturation_power = priv->calib_info->saturation_power52;
Zhu Yib481de92007-09-25 17:54:57 -07001989
1990 if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1991 saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1992 if (band)
1993 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1994 else
1995 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1996 }
1997
1998 /* regulatory txpower limits ... reg_limit values are in half-dBm,
1999 * max_power_avg values are in dBm, convert * 2 */
2000 if (is_fat)
2001 reg_limit = ch_info->fat_max_power_avg * 2;
2002 else
2003 reg_limit = ch_info->max_power_avg * 2;
2004
2005 if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
2006 (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
2007 if (band)
2008 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
2009 else
2010 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
2011 }
2012
2013 /* Interpolate txpower calibration values for this channel,
2014 * based on factory calibration tests on spaced channels. */
2015 iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
2016
2017 /* calculate tx gain adjustment based on power supply voltage */
Tomas Winkler073d3f52008-04-21 15:41:52 -07002018 voltage = priv->calib_info->voltage;
Zhu Yib481de92007-09-25 17:54:57 -07002019 init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
2020 voltage_compensation =
2021 iwl4965_get_voltage_compensation(voltage, init_voltage);
2022
2023 IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
2024 init_voltage,
2025 voltage, voltage_compensation);
2026
2027 /* get current temperature (Celsius) */
2028 current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
2029 current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
2030 current_temp = KELVIN_TO_CELSIUS(current_temp);
2031
2032 /* select thermal txpower adjustment params, based on channel group
2033 * (same frequency group used for mimo txatten adjustment) */
2034 degrees_per_05db_num =
2035 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
2036 degrees_per_05db_denom =
2037 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
2038
2039 /* get per-chain txpower values from factory measurements */
2040 for (c = 0; c < 2; c++) {
2041 measurement = &ch_eeprom_info.measurements[c][1];
2042
2043 /* txgain adjustment (in half-dB steps) based on difference
2044 * between factory and current temperature */
2045 factory_temp = measurement->temperature;
2046 iwl4965_math_div_round((current_temp - factory_temp) *
2047 degrees_per_05db_denom,
2048 degrees_per_05db_num,
2049 &temperature_comp[c]);
2050
2051 factory_gain_index[c] = measurement->gain_idx;
2052 factory_actual_pwr[c] = measurement->actual_pow;
2053
2054 IWL_DEBUG_TXPOWER("chain = %d\n", c);
2055 IWL_DEBUG_TXPOWER("fctry tmp %d, "
2056 "curr tmp %d, comp %d steps\n",
2057 factory_temp, current_temp,
2058 temperature_comp[c]);
2059
2060 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
2061 factory_gain_index[c],
2062 factory_actual_pwr[c]);
2063 }
2064
2065 /* for each of 33 bit-rates (including 1 for CCK) */
2066 for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
2067 u8 is_mimo_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002068 union iwl4965_tx_power_dual_stream tx_power;
Zhu Yib481de92007-09-25 17:54:57 -07002069
2070 /* for mimo, reduce each chain's txpower by half
2071 * (3dB, 6 steps), so total output power is regulatory
2072 * compliant. */
2073 if (i & 0x8) {
2074 current_regulatory = reg_limit -
2075 IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
2076 is_mimo_rate = 1;
2077 } else {
2078 current_regulatory = reg_limit;
2079 is_mimo_rate = 0;
2080 }
2081
2082 /* find txpower limit, either hardware or regulatory */
2083 power_limit = saturation_power - back_off_table[i];
2084 if (power_limit > current_regulatory)
2085 power_limit = current_regulatory;
2086
2087 /* reduce user's txpower request if necessary
2088 * for this rate on this channel */
2089 target_power = user_target_power;
2090 if (target_power > power_limit)
2091 target_power = power_limit;
2092
2093 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
2094 i, saturation_power - back_off_table[i],
2095 current_regulatory, user_target_power,
2096 target_power);
2097
2098 /* for each of 2 Tx chains (radio transmitters) */
2099 for (c = 0; c < 2; c++) {
2100 s32 atten_value;
2101
2102 if (is_mimo_rate)
2103 atten_value =
2104 (s32)le32_to_cpu(priv->card_alive_init.
2105 tx_atten[txatten_grp][c]);
2106 else
2107 atten_value = 0;
2108
2109 /* calculate index; higher index means lower txpower */
2110 power_index = (u8) (factory_gain_index[c] -
2111 (target_power -
2112 factory_actual_pwr[c]) -
2113 temperature_comp[c] -
2114 voltage_compensation +
2115 atten_value);
2116
2117/* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
2118 power_index); */
2119
2120 if (power_index < get_min_power_index(i, band))
2121 power_index = get_min_power_index(i, band);
2122
2123 /* adjust 5 GHz index to support negative indexes */
2124 if (!band)
2125 power_index += 9;
2126
2127 /* CCK, rate 32, reduce txpower for CCK */
2128 if (i == POWER_TABLE_CCK_ENTRY)
2129 power_index +=
2130 IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
2131
2132 /* stay within the table! */
2133 if (power_index > 107) {
2134 IWL_WARNING("txpower index %d > 107\n",
2135 power_index);
2136 power_index = 107;
2137 }
2138 if (power_index < 0) {
2139 IWL_WARNING("txpower index %d < 0\n",
2140 power_index);
2141 power_index = 0;
2142 }
2143
2144 /* fill txpower command for this rate/chain */
2145 tx_power.s.radio_tx_gain[c] =
2146 gain_table[band][power_index].radio;
2147 tx_power.s.dsp_predis_atten[c] =
2148 gain_table[band][power_index].dsp;
2149
2150 IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
2151 "gain 0x%02x dsp %d\n",
2152 c, atten_value, power_index,
2153 tx_power.s.radio_tx_gain[c],
2154 tx_power.s.dsp_predis_atten[c]);
2155 }/* for each chain */
2156
2157 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
2158
2159 }/* for each rate */
2160
2161 return 0;
2162}
2163
2164/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002165 * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
Zhu Yib481de92007-09-25 17:54:57 -07002166 *
2167 * Uses the active RXON for channel, band, and characteristics (fat, high)
2168 * The power limit is taken from priv->user_txpower_limit.
2169 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002170int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002171{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002172 struct iwl4965_txpowertable_cmd cmd = { 0 };
Tomas Winkler857485c2008-03-21 13:53:44 -07002173 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002174 u8 band = 0;
2175 u8 is_fat = 0;
2176 u8 ctrl_chan_high = 0;
2177
2178 if (test_bit(STATUS_SCANNING, &priv->status)) {
2179 /* If this gets hit a lot, switch it to a BUG() and catch
2180 * the stack trace to find out who is calling this during
2181 * a scan. */
2182 IWL_WARNING("TX Power requested while scanning!\n");
2183 return -EAGAIN;
2184 }
2185
Johannes Berg8318d782008-01-24 19:38:38 +01002186 band = priv->band == IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002187
2188 is_fat = is_fat_channel(priv->active_rxon.flags);
2189
2190 if (is_fat &&
2191 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2192 ctrl_chan_high = 1;
2193
2194 cmd.band = band;
2195 cmd.channel = priv->active_rxon.channel;
2196
Tomas Winkler857485c2008-03-21 13:53:44 -07002197 ret = iwl4965_fill_txpower_tbl(priv, band,
Zhu Yib481de92007-09-25 17:54:57 -07002198 le16_to_cpu(priv->active_rxon.channel),
2199 is_fat, ctrl_chan_high, &cmd.tx_power);
Tomas Winkler857485c2008-03-21 13:53:44 -07002200 if (ret)
2201 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002202
Tomas Winkler857485c2008-03-21 13:53:44 -07002203 ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
2204
2205out:
2206 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002207}
2208
Tomas Winkler7e8c5192008-04-15 16:01:43 -07002209static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
2210{
2211 int ret = 0;
2212 struct iwl4965_rxon_assoc_cmd rxon_assoc;
2213 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
2214 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
2215
2216 if ((rxon1->flags == rxon2->flags) &&
2217 (rxon1->filter_flags == rxon2->filter_flags) &&
2218 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
2219 (rxon1->ofdm_ht_single_stream_basic_rates ==
2220 rxon2->ofdm_ht_single_stream_basic_rates) &&
2221 (rxon1->ofdm_ht_dual_stream_basic_rates ==
2222 rxon2->ofdm_ht_dual_stream_basic_rates) &&
2223 (rxon1->rx_chain == rxon2->rx_chain) &&
2224 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
2225 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
2226 return 0;
2227 }
2228
2229 rxon_assoc.flags = priv->staging_rxon.flags;
2230 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
2231 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
2232 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
2233 rxon_assoc.reserved = 0;
2234 rxon_assoc.ofdm_ht_single_stream_basic_rates =
2235 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
2236 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
2237 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
2238 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
2239
2240 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
2241 sizeof(rxon_assoc), &rxon_assoc, NULL);
2242 if (ret)
2243 return ret;
2244
2245 return ret;
2246}
2247
2248
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002249int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07002250{
2251 int rc;
2252 u8 band = 0;
2253 u8 is_fat = 0;
2254 u8 ctrl_chan_high = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002255 struct iwl4965_channel_switch_cmd cmd = { 0 };
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002256 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002257
Johannes Berg8318d782008-01-24 19:38:38 +01002258 band = priv->band == IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002259
Assaf Krauss8622e702008-03-21 13:53:43 -07002260 ch_info = iwl_get_channel_info(priv, priv->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002261
2262 is_fat = is_fat_channel(priv->staging_rxon.flags);
2263
2264 if (is_fat &&
2265 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2266 ctrl_chan_high = 1;
2267
2268 cmd.band = band;
2269 cmd.expect_beacon = 0;
2270 cmd.channel = cpu_to_le16(channel);
2271 cmd.rxon_flags = priv->active_rxon.flags;
2272 cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
2273 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
2274 if (ch_info)
2275 cmd.expect_beacon = is_channel_radar(ch_info);
2276 else
2277 cmd.expect_beacon = 1;
2278
2279 rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
2280 ctrl_chan_high, &cmd.tx_power);
2281 if (rc) {
2282 IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
2283 return rc;
2284 }
2285
Tomas Winkler857485c2008-03-21 13:53:44 -07002286 rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002287 return rc;
2288}
2289
2290#define RTS_HCCA_RETRY_LIMIT 3
2291#define RTS_DFAULT_RETRY_LIMIT 60
2292
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002293void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002294 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002295 struct ieee80211_tx_control *ctrl,
2296 struct ieee80211_hdr *hdr, int sta_id,
2297 int is_hcca)
2298{
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002299 struct iwl4965_tx_cmd *tx = &cmd->cmd.tx;
Zhu Yib481de92007-09-25 17:54:57 -07002300 u8 rts_retry_limit = 0;
2301 u8 data_retry_limit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002302 u16 fc = le16_to_cpu(hdr->frame_control);
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002303 u8 rate_plcp;
2304 u16 rate_flags = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002305 int rate_idx = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
Zhu Yib481de92007-09-25 17:54:57 -07002306
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002307 rate_plcp = iwl4965_rates[rate_idx].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07002308
2309 rts_retry_limit = (is_hcca) ?
2310 RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
2311
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002312 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
2313 rate_flags |= RATE_MCS_CCK_MSK;
2314
2315
Zhu Yib481de92007-09-25 17:54:57 -07002316 if (ieee80211_is_probe_response(fc)) {
2317 data_retry_limit = 3;
2318 if (data_retry_limit < rts_retry_limit)
2319 rts_retry_limit = data_retry_limit;
2320 } else
2321 data_retry_limit = IWL_DEFAULT_TX_RETRY;
2322
2323 if (priv->data_retry_limit != -1)
2324 data_retry_limit = priv->data_retry_limit;
2325
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002326
2327 if (ieee80211_is_data(fc)) {
2328 tx->initial_rate_index = 0;
2329 tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
2330 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002331 switch (fc & IEEE80211_FCTL_STYPE) {
2332 case IEEE80211_STYPE_AUTH:
2333 case IEEE80211_STYPE_DEAUTH:
2334 case IEEE80211_STYPE_ASSOC_REQ:
2335 case IEEE80211_STYPE_REASSOC_REQ:
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002336 if (tx->tx_flags & TX_CMD_FLG_RTS_MSK) {
2337 tx->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2338 tx->tx_flags |= TX_CMD_FLG_CTS_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002339 }
2340 break;
2341 default:
2342 break;
2343 }
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002344
2345 /* Alternate between antenna A and B for successive frames */
2346 if (priv->use_ant_b_for_management_frame) {
2347 priv->use_ant_b_for_management_frame = 0;
2348 rate_flags |= RATE_MCS_ANT_B_MSK;
2349 } else {
2350 priv->use_ant_b_for_management_frame = 1;
2351 rate_flags |= RATE_MCS_ANT_A_MSK;
2352 }
Zhu Yib481de92007-09-25 17:54:57 -07002353 }
2354
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002355 tx->rts_retry_limit = rts_retry_limit;
2356 tx->data_retry_limit = data_retry_limit;
2357 tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002358}
2359
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002360int iwl4965_hw_get_rx_read(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002361{
Tomas Winkler059ff822008-04-14 21:16:14 -07002362 struct iwl4965_shared *s = priv->shared_virt;
2363 return le32_to_cpu(s->rb_closed) & 0xFFF;
Zhu Yib481de92007-09-25 17:54:57 -07002364}
2365
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002366int iwl4965_hw_get_temperature(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002367{
2368 return priv->temperature;
2369}
2370
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002371unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002372 struct iwl4965_frame *frame, u8 rate)
Zhu Yib481de92007-09-25 17:54:57 -07002373{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002374 struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002375 unsigned int frame_size;
2376
2377 tx_beacon_cmd = &frame->u.beacon;
2378 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
2379
Tomas Winkler5425e492008-04-15 16:01:38 -07002380 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002381 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002383 frame_size = iwl4965_fill_beacon_frame(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002384 tx_beacon_cmd->frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002385 iwl4965_broadcast_addr,
Zhu Yib481de92007-09-25 17:54:57 -07002386 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
2387
2388 BUG_ON(frame_size > MAX_MPDU_SIZE);
2389 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
2390
2391 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
2392 tx_beacon_cmd->tx.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002393 iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07002394 else
2395 tx_beacon_cmd->tx.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002396 iwl4965_hw_set_rate_n_flags(rate, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002397
2398 tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
2399 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
2400 return (sizeof(*tx_beacon_cmd) + frame_size);
2401}
2402
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002403/*
2404 * Tell 4965 where to find circular buffer of Tx Frame Descriptors for
2405 * given Tx queue, and enable the DMA channel used for that queue.
2406 *
2407 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
2408 * channels supported in hardware.
2409 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002410int iwl4965_hw_tx_queue_init(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07002411{
2412 int rc;
2413 unsigned long flags;
2414 int txq_id = txq->q.id;
2415
2416 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002417 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002418 if (rc) {
2419 spin_unlock_irqrestore(&priv->lock, flags);
2420 return rc;
2421 }
2422
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002423 /* Circular buffer (TFD queue in DRAM) physical base address */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002424 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
Zhu Yib481de92007-09-25 17:54:57 -07002425 txq->q.dma_addr >> 8);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002426
2427 /* Enable DMA channel, using same id as for TFD queue */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002428 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002429 priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
2430 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
2431 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002432 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002433 spin_unlock_irqrestore(&priv->lock, flags);
2434
2435 return 0;
2436}
2437
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002438int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
Zhu Yib481de92007-09-25 17:54:57 -07002439 dma_addr_t addr, u16 len)
2440{
2441 int index, is_odd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002442 struct iwl4965_tfd_frame *tfd = ptr;
Zhu Yib481de92007-09-25 17:54:57 -07002443 u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
2444
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002445 /* Each TFD can point to a maximum 20 Tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002446 if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
2447 IWL_ERROR("Error can not send more than %d chunks\n",
2448 MAX_NUM_OF_TBS);
2449 return -EINVAL;
2450 }
2451
2452 index = num_tbs / 2;
2453 is_odd = num_tbs & 0x1;
2454
2455 if (!is_odd) {
2456 tfd->pa[index].tb1_addr = cpu_to_le32(addr);
2457 IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
Tomas Winkler6a218f62008-01-14 17:46:15 -08002458 iwl_get_dma_hi_address(addr));
Zhu Yib481de92007-09-25 17:54:57 -07002459 IWL_SET_BITS(tfd->pa[index], tb1_len, len);
2460 } else {
2461 IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
2462 (u32) (addr & 0xffff));
2463 IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
2464 IWL_SET_BITS(tfd->pa[index], tb2_len, len);
2465 }
2466
2467 IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
2468
2469 return 0;
2470}
2471
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002472static void iwl4965_hw_card_show_info(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002473{
Tomas Winkler073d3f52008-04-21 15:41:52 -07002474 u16 hw_version = iwl_eeprom_query16(priv, EEPROM_4965_BOARD_REVISION);
Zhu Yib481de92007-09-25 17:54:57 -07002475
2476 IWL_DEBUG_INFO("4965ABGN HW Version %u.%u.%u\n",
2477 ((hw_version >> 8) & 0x0F),
2478 ((hw_version >> 8) >> 4), (hw_version & 0x00FF));
2479
2480 IWL_DEBUG_INFO("4965ABGN PBA Number %.16s\n",
Tomas Winkler073d3f52008-04-21 15:41:52 -07002481 &priv->eeprom[EEPROM_4965_BOARD_PBA]);
Zhu Yib481de92007-09-25 17:54:57 -07002482}
2483
2484#define IWL_TX_CRC_SIZE 4
2485#define IWL_TX_DELIMITER_SIZE 4
2486
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002487/**
Tomas Winklere2a722e2008-04-14 21:16:10 -07002488 * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002489 */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002490static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
2491 struct iwl4965_tx_queue *txq,
2492 u16 byte_cnt)
Zhu Yib481de92007-09-25 17:54:57 -07002493{
2494 int len;
2495 int txq_id = txq->q.id;
Tomas Winkler059ff822008-04-14 21:16:14 -07002496 struct iwl4965_shared *shared_data = priv->shared_virt;
Zhu Yib481de92007-09-25 17:54:57 -07002497
Zhu Yib481de92007-09-25 17:54:57 -07002498 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2499
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002500 /* Set up byte count within first 256 entries */
Zhu Yib481de92007-09-25 17:54:57 -07002501 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002502 tfd_offset[txq->q.write_ptr], byte_cnt, len);
Zhu Yib481de92007-09-25 17:54:57 -07002503
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002504 /* If within first 64 entries, duplicate at end */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002505 if (txq->q.write_ptr < IWL4965_MAX_WIN_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07002506 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002507 tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
Zhu Yib481de92007-09-25 17:54:57 -07002508 byte_cnt, len);
Zhu Yib481de92007-09-25 17:54:57 -07002509}
2510
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002511/**
2512 * iwl4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
2513 *
2514 * Selects how many and which Rx receivers/antennas/chains to use.
2515 * This should not be used for scan command ... it puts data in wrong place.
2516 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002517void iwl4965_set_rxon_chain(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002518{
Guy Cohenfde0db32008-04-21 15:42:01 -07002519 u8 is_single = is_single_rx_stream(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002520 u8 idle_state, rx_state;
2521
2522 priv->staging_rxon.rx_chain = 0;
2523 rx_state = idle_state = 3;
2524
2525 /* Tell uCode which antennas are actually connected.
2526 * Before first association, we assume all antennas are connected.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002527 * Just after first association, iwl_chain_noise_calibration()
Zhu Yib481de92007-09-25 17:54:57 -07002528 * checks which antennas actually *are* connected. */
2529 priv->staging_rxon.rx_chain |=
Guy Cohenfde0db32008-04-21 15:42:01 -07002530 cpu_to_le16(priv->hw_params.valid_rx_ant <<
2531 RXON_RX_CHAIN_VALID_POS);
Zhu Yib481de92007-09-25 17:54:57 -07002532
2533 /* How many receivers should we use? */
2534 iwl4965_get_rx_chain_counter(priv, &idle_state, &rx_state);
2535 priv->staging_rxon.rx_chain |=
2536 cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
2537 priv->staging_rxon.rx_chain |=
2538 cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
2539
2540 if (!is_single && (rx_state >= 2) &&
2541 !test_bit(STATUS_POWER_PMI, &priv->status))
2542 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
2543 else
2544 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
2545
2546 IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
2547}
2548
Zhu Yib481de92007-09-25 17:54:57 -07002549/**
2550 * sign_extend - Sign extend a value using specified bit as sign-bit
2551 *
2552 * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
2553 * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
2554 *
2555 * @param oper value to sign extend
2556 * @param index 0 based bit index (0<=index<32) to sign bit
2557 */
2558static s32 sign_extend(u32 oper, int index)
2559{
2560 u8 shift = 31 - index;
2561
2562 return (s32)(oper << shift) >> shift;
2563}
2564
2565/**
2566 * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
2567 * @statistics: Provides the temperature reading from the uCode
2568 *
2569 * A return of <0 indicates bogus data in the statistics
2570 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002571int iwl4965_get_temperature(const struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002572{
2573 s32 temperature;
2574 s32 vt;
2575 s32 R1, R2, R3;
2576 u32 R4;
2577
2578 if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
2579 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
2580 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
2581 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
2582 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
2583 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
2584 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
2585 } else {
2586 IWL_DEBUG_TEMP("Running temperature calibration\n");
2587 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
2588 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
2589 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2590 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2591 }
2592
2593 /*
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002594 * Temperature is only 23 bits, so sign extend out to 32.
Zhu Yib481de92007-09-25 17:54:57 -07002595 *
2596 * NOTE If we haven't received a statistics notification yet
2597 * with an updated temperature, use R4 provided to us in the
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002598 * "initialize" ALIVE response.
2599 */
Zhu Yib481de92007-09-25 17:54:57 -07002600 if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2601 vt = sign_extend(R4, 23);
2602 else
2603 vt = sign_extend(
2604 le32_to_cpu(priv->statistics.general.temperature), 23);
2605
2606 IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2607 R1, R2, R3, vt);
2608
2609 if (R3 == R1) {
2610 IWL_ERROR("Calibration conflict R1 == R3\n");
2611 return -1;
2612 }
2613
2614 /* Calculate temperature in degrees Kelvin, adjust by 97%.
2615 * Add offset to center the adjustment around 0 degrees Centigrade. */
2616 temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2617 temperature /= (R3 - R1);
2618 temperature = (temperature * 97) / 100 +
2619 TEMPERATURE_CALIB_KELVIN_OFFSET;
2620
2621 IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2622 KELVIN_TO_CELSIUS(temperature));
2623
2624 return temperature;
2625}
2626
2627/* Adjust Txpower only if temperature variance is greater than threshold. */
2628#define IWL_TEMPERATURE_THRESHOLD 3
2629
2630/**
2631 * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2632 *
2633 * If the temperature changed has changed sufficiently, then a recalibration
2634 * is needed.
2635 *
2636 * Assumes caller will replace priv->last_temperature once calibration
2637 * executed.
2638 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002639static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002640{
2641 int temp_diff;
2642
2643 if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2644 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2645 return 0;
2646 }
2647
2648 temp_diff = priv->temperature - priv->last_temperature;
2649
2650 /* get absolute value */
2651 if (temp_diff < 0) {
2652 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2653 temp_diff = -temp_diff;
2654 } else if (temp_diff == 0)
2655 IWL_DEBUG_POWER("Same temp, \n");
2656 else
2657 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2658
2659 if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2660 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2661 return 0;
2662 }
2663
2664 IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2665
2666 return 1;
2667}
2668
2669/* Calculate noise level, based on measurements during network silence just
2670 * before arriving beacon. This measurement can be done only if we know
2671 * exactly when to expect beacons, therefore only when we're associated. */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002672static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002673{
2674 struct statistics_rx_non_phy *rx_info
2675 = &(priv->statistics.rx.general);
2676 int num_active_rx = 0;
2677 int total_silence = 0;
2678 int bcn_silence_a =
2679 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2680 int bcn_silence_b =
2681 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2682 int bcn_silence_c =
2683 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2684
2685 if (bcn_silence_a) {
2686 total_silence += bcn_silence_a;
2687 num_active_rx++;
2688 }
2689 if (bcn_silence_b) {
2690 total_silence += bcn_silence_b;
2691 num_active_rx++;
2692 }
2693 if (bcn_silence_c) {
2694 total_silence += bcn_silence_c;
2695 num_active_rx++;
2696 }
2697
2698 /* Average among active antennas */
2699 if (num_active_rx)
2700 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2701 else
2702 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2703
2704 IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2705 bcn_silence_a, bcn_silence_b, bcn_silence_c,
2706 priv->last_rx_noise);
2707}
2708
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002709void iwl4965_hw_rx_statistics(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002710{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002711 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002712 int change;
2713 s32 temp;
2714
2715 IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2716 (int)sizeof(priv->statistics), pkt->len);
2717
2718 change = ((priv->statistics.general.temperature !=
2719 pkt->u.stats.general.temperature) ||
2720 ((priv->statistics.flag &
2721 STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2722 (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2723
2724 memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2725
2726 set_bit(STATUS_STATISTICS, &priv->status);
2727
2728 /* Reschedule the statistics timer to occur in
2729 * REG_RECALIB_PERIOD seconds to ensure we get a
2730 * thermal update even if the uCode doesn't give
2731 * us one */
2732 mod_timer(&priv->statistics_periodic, jiffies +
2733 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2734
2735 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2736 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2737 iwl4965_rx_calc_noise(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002738#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07002739 queue_work(priv->workqueue, &priv->sensitivity_work);
2740#endif
2741 }
2742
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002743 iwl_leds_background(priv);
2744
Zhu Yib481de92007-09-25 17:54:57 -07002745 /* If the hardware hasn't reported a change in
2746 * temperature then don't bother computing a
2747 * calibrated temperature value */
2748 if (!change)
2749 return;
2750
2751 temp = iwl4965_get_temperature(priv);
2752 if (temp < 0)
2753 return;
2754
2755 if (priv->temperature != temp) {
2756 if (priv->temperature)
2757 IWL_DEBUG_TEMP("Temperature changed "
2758 "from %dC to %dC\n",
2759 KELVIN_TO_CELSIUS(priv->temperature),
2760 KELVIN_TO_CELSIUS(temp));
2761 else
2762 IWL_DEBUG_TEMP("Temperature "
2763 "initialized to %dC\n",
2764 KELVIN_TO_CELSIUS(temp));
2765 }
2766
2767 priv->temperature = temp;
2768 set_bit(STATUS_TEMPERATURE, &priv->status);
2769
2770 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2771 iwl4965_is_temp_calib_needed(priv))
2772 queue_work(priv->workqueue, &priv->txpower_work);
2773}
2774
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002775static void iwl4965_add_radiotap(struct iwl_priv *priv,
Zhu Yi12342c42007-12-20 11:27:32 +08002776 struct sk_buff *skb,
2777 struct iwl4965_rx_phy_res *rx_start,
2778 struct ieee80211_rx_status *stats,
2779 u32 ampdu_status)
2780{
2781 s8 signal = stats->ssi;
2782 s8 noise = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002783 int rate = stats->rate_idx;
Zhu Yi12342c42007-12-20 11:27:32 +08002784 u64 tsf = stats->mactime;
Johannes Berga0b484f2008-04-01 17:51:47 +02002785 __le16 antenna;
Zhu Yi12342c42007-12-20 11:27:32 +08002786 __le16 phy_flags_hw = rx_start->phy_flags;
2787 struct iwl4965_rt_rx_hdr {
2788 struct ieee80211_radiotap_header rt_hdr;
2789 __le64 rt_tsf; /* TSF */
2790 u8 rt_flags; /* radiotap packet flags */
2791 u8 rt_rate; /* rate in 500kb/s */
2792 __le16 rt_channelMHz; /* channel in MHz */
2793 __le16 rt_chbitmask; /* channel bitfield */
2794 s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
2795 s8 rt_dbmnoise;
2796 u8 rt_antenna; /* antenna number */
2797 } __attribute__ ((packed)) *iwl4965_rt;
2798
2799 /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2800 if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2801 if (net_ratelimit())
2802 printk(KERN_ERR "not enough headroom [%d] for "
Miguel Botón01c20982008-01-04 23:34:35 +01002803 "radiotap head [%zd]\n",
Zhu Yi12342c42007-12-20 11:27:32 +08002804 skb_headroom(skb), sizeof(*iwl4965_rt));
2805 return;
2806 }
2807
2808 /* put radiotap header in front of 802.11 header and data */
2809 iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2810
2811 /* initialise radiotap header */
2812 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2813 iwl4965_rt->rt_hdr.it_pad = 0;
2814
2815 /* total header + data */
2816 put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2817 &iwl4965_rt->rt_hdr.it_len);
2818
2819 /* Indicate all the fields we add to the radiotap header */
2820 put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2821 (1 << IEEE80211_RADIOTAP_FLAGS) |
2822 (1 << IEEE80211_RADIOTAP_RATE) |
2823 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2824 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2825 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2826 (1 << IEEE80211_RADIOTAP_ANTENNA)),
2827 &iwl4965_rt->rt_hdr.it_present);
2828
2829 /* Zero the flags, we'll add to them as we go */
2830 iwl4965_rt->rt_flags = 0;
2831
2832 put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2833
2834 iwl4965_rt->rt_dbmsignal = signal;
2835 iwl4965_rt->rt_dbmnoise = noise;
2836
2837 /* Convert the channel frequency and set the flags */
2838 put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2839 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2840 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2841 IEEE80211_CHAN_5GHZ),
2842 &iwl4965_rt->rt_chbitmask);
2843 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2844 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2845 IEEE80211_CHAN_2GHZ),
2846 &iwl4965_rt->rt_chbitmask);
2847 else /* 802.11g */
2848 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2849 IEEE80211_CHAN_2GHZ),
2850 &iwl4965_rt->rt_chbitmask);
2851
Zhu Yi12342c42007-12-20 11:27:32 +08002852 if (rate == -1)
2853 iwl4965_rt->rt_rate = 0;
2854 else
2855 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
2856
2857 /*
2858 * "antenna number"
2859 *
2860 * It seems that the antenna field in the phy flags value
2861 * is actually a bitfield. This is undefined by radiotap,
2862 * it wants an actual antenna number but I always get "7"
2863 * for most legacy frames I receive indicating that the
2864 * same frame was received on all three RX chains.
2865 *
2866 * I think this field should be removed in favour of a
2867 * new 802.11n radiotap field "RX chains" that is defined
2868 * as a bitmask.
2869 */
Johannes Berga0b484f2008-04-01 17:51:47 +02002870 antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2871 iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
Zhu Yi12342c42007-12-20 11:27:32 +08002872
2873 /* set the preamble flag if appropriate */
2874 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2875 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2876
2877 stats->flag |= RX_FLAG_RADIOTAP;
2878}
2879
Tomas Winkler19758be2008-03-12 16:58:51 -07002880static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2881{
2882 /* 0 - mgmt, 1 - cnt, 2 - data */
2883 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2884 priv->rx_stats[idx].cnt++;
2885 priv->rx_stats[idx].bytes += len;
2886}
2887
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07002888/*
2889 * returns non-zero if packet should be dropped
2890 */
2891static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2892 struct ieee80211_hdr *hdr,
2893 u32 decrypt_res,
2894 struct ieee80211_rx_status *stats)
2895{
2896 u16 fc = le16_to_cpu(hdr->frame_control);
2897
2898 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2899 return 0;
2900
2901 if (!(fc & IEEE80211_FCTL_PROTECTED))
2902 return 0;
2903
2904 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2905 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2906 case RX_RES_STATUS_SEC_TYPE_TKIP:
2907 /* The uCode has got a bad phase 1 Key, pushes the packet.
2908 * Decryption will be done in SW. */
2909 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2910 RX_RES_STATUS_BAD_KEY_TTAK)
2911 break;
2912
2913 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2914 RX_RES_STATUS_BAD_ICV_MIC) {
2915 /* bad ICV, the packet is destroyed since the
2916 * decryption is inplace, drop it */
2917 IWL_DEBUG_RX("Packet destroyed\n");
2918 return -1;
2919 }
2920 case RX_RES_STATUS_SEC_TYPE_WEP:
2921 case RX_RES_STATUS_SEC_TYPE_CCMP:
2922 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2923 RX_RES_STATUS_DECRYPT_OK) {
2924 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2925 stats->flag |= RX_FLAG_DECRYPTED;
2926 }
2927 break;
2928
2929 default:
2930 break;
2931 }
2932 return 0;
2933}
2934
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002935static u32 iwl4965_translate_rx_status(u32 decrypt_in)
2936{
2937 u32 decrypt_out = 0;
2938
2939 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2940 RX_RES_STATUS_STATION_FOUND)
2941 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2942 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2943
2944 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2945
2946 /* packet was not encrypted */
2947 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2948 RX_RES_STATUS_SEC_TYPE_NONE)
2949 return decrypt_out;
2950
2951 /* packet was encrypted with unknown alg */
2952 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2953 RX_RES_STATUS_SEC_TYPE_ERR)
2954 return decrypt_out;
2955
2956 /* decryption was not done in HW */
2957 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2958 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2959 return decrypt_out;
2960
2961 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2962
2963 case RX_RES_STATUS_SEC_TYPE_CCMP:
2964 /* alg is CCM: check MIC only */
2965 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2966 /* Bad MIC */
2967 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2968 else
2969 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2970
2971 break;
2972
2973 case RX_RES_STATUS_SEC_TYPE_TKIP:
2974 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2975 /* Bad TTAK */
2976 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2977 break;
2978 }
2979 /* fall through if TTAK OK */
2980 default:
2981 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
2982 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2983 else
2984 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2985 break;
2986 };
2987
2988 IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
2989 decrypt_in, decrypt_out);
2990
2991 return decrypt_out;
2992}
2993
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002994static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
Zhu Yib481de92007-09-25 17:54:57 -07002995 int include_phy,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002996 struct iwl4965_rx_mem_buffer *rxb,
Zhu Yib481de92007-09-25 17:54:57 -07002997 struct ieee80211_rx_status *stats)
2998{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002999 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003000 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3001 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
3002 struct ieee80211_hdr *hdr;
3003 u16 len;
3004 __le32 *rx_end;
3005 unsigned int skblen;
3006 u32 ampdu_status;
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07003007 u32 ampdu_status_legacy;
Zhu Yib481de92007-09-25 17:54:57 -07003008
3009 if (!include_phy && priv->last_phy_res[0])
3010 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3011
3012 if (!rx_start) {
3013 IWL_ERROR("MPDU frame without a PHY data\n");
3014 return;
3015 }
3016 if (include_phy) {
3017 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
3018 rx_start->cfg_phy_cnt);
3019
3020 len = le16_to_cpu(rx_start->byte_count);
3021
3022 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
3023 sizeof(struct iwl4965_rx_phy_res) +
3024 rx_start->cfg_phy_cnt + len);
3025
3026 } else {
3027 struct iwl4965_rx_mpdu_res_start *amsdu =
3028 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3029
3030 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
3031 sizeof(struct iwl4965_rx_mpdu_res_start));
3032 len = le16_to_cpu(amsdu->byte_count);
3033 rx_start->byte_count = amsdu->byte_count;
3034 rx_end = (__le32 *) (((u8 *) hdr) + len);
3035 }
Tomas Winkler5425e492008-04-15 16:01:38 -07003036 if (len > priv->hw_params.max_pkt_size || len < 16) {
Zhu Yi12342c42007-12-20 11:27:32 +08003037 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003038 return;
3039 }
3040
3041 ampdu_status = le32_to_cpu(*rx_end);
3042 skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
3043
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07003044 if (!include_phy) {
3045 /* New status scheme, need to translate */
3046 ampdu_status_legacy = ampdu_status;
3047 ampdu_status = iwl4965_translate_rx_status(ampdu_status);
3048 }
3049
Zhu Yib481de92007-09-25 17:54:57 -07003050 /* start from MAC */
3051 skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
3052 skb_put(rxb->skb, len); /* end where data ends */
3053
3054 /* We only process data packets if the interface is open */
3055 if (unlikely(!priv->is_open)) {
3056 IWL_DEBUG_DROP_LIMIT
3057 ("Dropping packet while interface is not open.\n");
3058 return;
3059 }
3060
Zhu Yib481de92007-09-25 17:54:57 -07003061 stats->flag = 0;
3062 hdr = (struct ieee80211_hdr *)rxb->skb->data;
3063
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003064 /* in case of HW accelerated crypto and bad decryption, drop */
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003065 if (!priv->hw_params.sw_crypto &&
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003066 iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
3067 return;
Zhu Yib481de92007-09-25 17:54:57 -07003068
Zhu Yi12342c42007-12-20 11:27:32 +08003069 if (priv->add_radiotap)
3070 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
3071
Tomas Winkler19758be2008-03-12 16:58:51 -07003072 iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
Zhu Yib481de92007-09-25 17:54:57 -07003073 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3074 priv->alloc_rxb_skb--;
3075 rxb->skb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003076}
3077
3078/* Calc max signal level (dBm) among 3 possible receivers */
3079static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
3080{
3081 /* data from PHY/DSP regarding signal strength, etc.,
3082 * contents are always there, not configurable by host. */
3083 struct iwl4965_rx_non_cfg_phy *ncphy =
3084 (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
3085 u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
3086 >> IWL_AGC_DB_POS;
3087
3088 u32 valid_antennae =
3089 (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
3090 >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
3091 u8 max_rssi = 0;
3092 u32 i;
3093
3094 /* Find max rssi among 3 possible receivers.
3095 * These values are measured by the digital signal processor (DSP).
3096 * They should stay fairly constant even as the signal strength varies,
3097 * if the radio's automatic gain control (AGC) is working right.
3098 * AGC value (see below) will provide the "interesting" info. */
3099 for (i = 0; i < 3; i++)
3100 if (valid_antennae & (1 << i))
3101 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
3102
3103 IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
3104 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
3105 max_rssi, agc);
3106
3107 /* dBm = max_rssi dB - agc dB - constant.
3108 * Higher AGC (higher radio gain) means lower signal. */
3109 return (max_rssi - agc - IWL_RSSI_OFFSET);
3110}
3111
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003112#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003113
Guy Cohenfde0db32008-04-21 15:42:01 -07003114void iwl4965_init_ht_hw_capab(const struct iwl_priv *priv,
Assaf Krauss1ea87392008-03-18 14:57:50 -07003115 struct ieee80211_ht_info *ht_info,
Tomas Winkler78330fd2008-02-06 02:37:18 +02003116 enum ieee80211_band band)
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003117{
3118 ht_info->cap = 0;
3119 memset(ht_info->supp_mcs_set, 0, 16);
3120
3121 ht_info->ht_supported = 1;
3122
Ron Rindjunsky099b40b2008-04-21 15:41:53 -07003123 if (priv->hw_params.fat_channel & BIT(band)) {
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003124 ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
3125 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
3126 ht_info->supp_mcs_set[4] = 0x01;
3127 }
3128 ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
3129 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
3130 ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
3131 (IWL_MIMO_PS_NONE << 2));
Assaf Krauss1ea87392008-03-18 14:57:50 -07003132
3133 if (priv->cfg->mod_params->amsdu_size_8K)
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003134 ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003135
3136 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
3137 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
3138
3139 ht_info->supp_mcs_set[0] = 0xFF;
Guy Cohenfde0db32008-04-21 15:42:01 -07003140 if (priv->hw_params.tx_chains_num >= 2)
3141 ht_info->supp_mcs_set[1] = 0xFF;
3142 if (priv->hw_params.tx_chains_num >= 3)
3143 ht_info->supp_mcs_set[2] = 0xFF;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003144}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003145#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003146
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003147static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07003148{
3149 unsigned long flags;
3150
3151 spin_lock_irqsave(&priv->sta_lock, flags);
3152 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
3153 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3154 priv->stations[sta_id].sta.sta.modify_mask = 0;
3155 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3156 spin_unlock_irqrestore(&priv->sta_lock, flags);
3157
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003158 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07003159}
3160
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003161static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
Zhu Yib481de92007-09-25 17:54:57 -07003162{
3163 /* FIXME: need locking over ps_status ??? */
Tomas Winkler947b13a2008-04-16 16:34:48 -07003164 u8 sta_id = iwl_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07003165
3166 if (sta_id != IWL_INVALID_STATION) {
3167 u8 sta_awake = priv->stations[sta_id].
3168 ps_status == STA_PS_STATUS_WAKE;
3169
3170 if (sta_awake && ps_bit)
3171 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
3172 else if (!sta_awake && !ps_bit) {
3173 iwl4965_sta_modify_ps_wake(priv, sta_id);
3174 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
3175 }
3176 }
3177}
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003178#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler17744ff2008-03-02 01:52:00 +02003179
3180/**
3181 * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
3182 *
3183 * You may hack this function to show different aspects of received frames,
3184 * including selective frame dumps.
3185 * group100 parameter selects whether to show 1 out of 100 good frames.
3186 *
3187 * TODO: This was originally written for 3945, need to audit for
3188 * proper operation with 4965.
3189 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003190static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003191 struct iwl4965_rx_packet *pkt,
3192 struct ieee80211_hdr *header, int group100)
3193{
3194 u32 to_us;
3195 u32 print_summary = 0;
3196 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
3197 u32 hundred = 0;
3198 u32 dataframe = 0;
3199 u16 fc;
3200 u16 seq_ctl;
3201 u16 channel;
3202 u16 phy_flags;
3203 int rate_sym;
3204 u16 length;
3205 u16 status;
3206 u16 bcn_tmr;
3207 u32 tsf_low;
3208 u64 tsf;
3209 u8 rssi;
3210 u8 agc;
3211 u16 sig_avg;
3212 u16 noise_diff;
3213 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
3214 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
3215 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
3216 u8 *data = IWL_RX_DATA(pkt);
3217
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003218 if (likely(!(iwl_debug_level & IWL_DL_RX)))
Tomas Winkler17744ff2008-03-02 01:52:00 +02003219 return;
3220
3221 /* MAC header */
3222 fc = le16_to_cpu(header->frame_control);
3223 seq_ctl = le16_to_cpu(header->seq_ctrl);
3224
3225 /* metadata */
3226 channel = le16_to_cpu(rx_hdr->channel);
3227 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
3228 rate_sym = rx_hdr->rate;
3229 length = le16_to_cpu(rx_hdr->len);
3230
3231 /* end-of-frame status and timestamp */
3232 status = le32_to_cpu(rx_end->status);
3233 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
3234 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
3235 tsf = le64_to_cpu(rx_end->timestamp);
3236
3237 /* signal statistics */
3238 rssi = rx_stats->rssi;
3239 agc = rx_stats->agc;
3240 sig_avg = le16_to_cpu(rx_stats->sig_avg);
3241 noise_diff = le16_to_cpu(rx_stats->noise_diff);
3242
3243 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
3244
3245 /* if data frame is to us and all is good,
3246 * (optionally) print summary for only 1 out of every 100 */
3247 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
3248 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
3249 dataframe = 1;
3250 if (!group100)
3251 print_summary = 1; /* print each frame */
3252 else if (priv->framecnt_to_us < 100) {
3253 priv->framecnt_to_us++;
3254 print_summary = 0;
3255 } else {
3256 priv->framecnt_to_us = 0;
3257 print_summary = 1;
3258 hundred = 1;
3259 }
3260 } else {
3261 /* print summary for all other frames */
3262 print_summary = 1;
3263 }
3264
3265 if (print_summary) {
3266 char *title;
3267 int rate_idx;
3268 u32 bitrate;
3269
3270 if (hundred)
3271 title = "100Frames";
3272 else if (fc & IEEE80211_FCTL_RETRY)
3273 title = "Retry";
3274 else if (ieee80211_is_assoc_response(fc))
3275 title = "AscRsp";
3276 else if (ieee80211_is_reassoc_response(fc))
3277 title = "RasRsp";
3278 else if (ieee80211_is_probe_response(fc)) {
3279 title = "PrbRsp";
3280 print_dump = 1; /* dump frame contents */
3281 } else if (ieee80211_is_beacon(fc)) {
3282 title = "Beacon";
3283 print_dump = 1; /* dump frame contents */
3284 } else if (ieee80211_is_atim(fc))
3285 title = "ATIM";
3286 else if (ieee80211_is_auth(fc))
3287 title = "Auth";
3288 else if (ieee80211_is_deauth(fc))
3289 title = "DeAuth";
3290 else if (ieee80211_is_disassoc(fc))
3291 title = "DisAssoc";
3292 else
3293 title = "Frame";
3294
3295 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
3296 if (unlikely(rate_idx == -1))
3297 bitrate = 0;
3298 else
3299 bitrate = iwl4965_rates[rate_idx].ieee / 2;
3300
3301 /* print frame summary.
3302 * MAC addresses show just the last byte (for brevity),
3303 * but you can hack it to show more, if you'd like to. */
3304 if (dataframe)
3305 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
3306 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
3307 title, fc, header->addr1[5],
3308 length, rssi, channel, bitrate);
3309 else {
3310 /* src/dst addresses assume managed mode */
3311 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
3312 "src=0x%02x, rssi=%u, tim=%lu usec, "
3313 "phy=0x%02x, chnl=%d\n",
3314 title, fc, header->addr1[5],
3315 header->addr3[5], rssi,
3316 tsf_low - priv->scan_start_tsf,
3317 phy_flags, channel);
3318 }
3319 }
3320 if (print_dump)
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003321 iwl_print_hex_dump(IWL_DL_RX, data, length);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003322}
3323#else
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003324static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003325 struct iwl4965_rx_packet *pkt,
3326 struct ieee80211_hdr *header,
3327 int group100)
3328{
3329}
3330#endif
3331
Zhu Yib481de92007-09-25 17:54:57 -07003332
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003333
Tomas Winkler857485c2008-03-21 13:53:44 -07003334/* Called for REPLY_RX (legacy ABG frames), or
Zhu Yib481de92007-09-25 17:54:57 -07003335 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003336static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003337 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003338{
Tomas Winkler17744ff2008-03-02 01:52:00 +02003339 struct ieee80211_hdr *header;
3340 struct ieee80211_rx_status rx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003341 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003342 /* Use phy data (Rx signal strength, etc.) contained within
3343 * this rx packet for legacy frames,
3344 * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
Tomas Winkler857485c2008-03-21 13:53:44 -07003345 int include_phy = (pkt->hdr.cmd == REPLY_RX);
Zhu Yib481de92007-09-25 17:54:57 -07003346 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3347 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
3348 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3349 __le32 *rx_end;
3350 unsigned int len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003351 u16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07003352 u8 network_packet;
3353
Tomas Winkler17744ff2008-03-02 01:52:00 +02003354 rx_status.mactime = le64_to_cpu(rx_start->timestamp);
Tomas Winklerdc92e492008-04-03 16:05:22 -07003355 rx_status.freq =
3356 ieee80211_frequency_to_channel(le16_to_cpu(rx_start->channel));
Tomas Winkler17744ff2008-03-02 01:52:00 +02003357 rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
3358 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Tomas Winklerdc92e492008-04-03 16:05:22 -07003359 rx_status.rate_idx =
3360 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
Tomas Winkler17744ff2008-03-02 01:52:00 +02003361 if (rx_status.band == IEEE80211_BAND_5GHZ)
3362 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
3363
3364 rx_status.antenna = 0;
3365 rx_status.flag = 0;
3366
Zhu Yib481de92007-09-25 17:54:57 -07003367 if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
Tomas Winklerdc92e492008-04-03 16:05:22 -07003368 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
3369 rx_start->cfg_phy_cnt);
Zhu Yib481de92007-09-25 17:54:57 -07003370 return;
3371 }
Tomas Winkler17744ff2008-03-02 01:52:00 +02003372
Zhu Yib481de92007-09-25 17:54:57 -07003373 if (!include_phy) {
3374 if (priv->last_phy_res[0])
3375 rx_start = (struct iwl4965_rx_phy_res *)
3376 &priv->last_phy_res[1];
3377 else
3378 rx_start = NULL;
3379 }
3380
3381 if (!rx_start) {
3382 IWL_ERROR("MPDU frame without a PHY data\n");
3383 return;
3384 }
3385
3386 if (include_phy) {
3387 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
3388 + rx_start->cfg_phy_cnt);
3389
3390 len = le16_to_cpu(rx_start->byte_count);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003391 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
Zhu Yib481de92007-09-25 17:54:57 -07003392 sizeof(struct iwl4965_rx_phy_res) + len);
3393 } else {
3394 struct iwl4965_rx_mpdu_res_start *amsdu =
3395 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3396
3397 header = (void *)(pkt->u.raw +
3398 sizeof(struct iwl4965_rx_mpdu_res_start));
3399 len = le16_to_cpu(amsdu->byte_count);
3400 rx_end = (__le32 *) (pkt->u.raw +
3401 sizeof(struct iwl4965_rx_mpdu_res_start) + len);
3402 }
3403
3404 if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
3405 !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
3406 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
3407 le32_to_cpu(*rx_end));
3408 return;
3409 }
3410
3411 priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
3412
Zhu Yib481de92007-09-25 17:54:57 -07003413 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Tomas Winkler17744ff2008-03-02 01:52:00 +02003414 rx_status.ssi = iwl4965_calc_rssi(rx_start);
Zhu Yib481de92007-09-25 17:54:57 -07003415
3416 /* Meaningful noise values are available only from beacon statistics,
3417 * which are gathered only when associated, and indicate noise
3418 * only for the associated network channel ...
3419 * Ignore these noise values while scanning (other channels) */
Tomas Winkler3109ece2008-03-28 16:33:35 -07003420 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07003421 !test_bit(STATUS_SCANNING, &priv->status)) {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003422 rx_status.noise = priv->last_rx_noise;
3423 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi,
3424 rx_status.noise);
Zhu Yib481de92007-09-25 17:54:57 -07003425 } else {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003426 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3427 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003428 }
3429
3430 /* Reset beacon noise level if not associated. */
Tomas Winkler3109ece2008-03-28 16:33:35 -07003431 if (!iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003432 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3433
Tomas Winkler17744ff2008-03-02 01:52:00 +02003434 /* Set "1" to report good data frames in groups of 100 */
3435 /* FIXME: need to optimze the call: */
3436 iwl4965_dbg_report_frame(priv, pkt, header, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003437
Tomas Winkler17744ff2008-03-02 01:52:00 +02003438 IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
3439 rx_status.ssi, rx_status.noise, rx_status.signal,
John W. Linville06501d22008-04-01 17:38:47 -04003440 (unsigned long long)rx_status.mactime);
Zhu Yib481de92007-09-25 17:54:57 -07003441
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003442 network_packet = iwl4965_is_network_packet(priv, header);
Zhu Yib481de92007-09-25 17:54:57 -07003443 if (network_packet) {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003444 priv->last_rx_rssi = rx_status.ssi;
Zhu Yib481de92007-09-25 17:54:57 -07003445 priv->last_beacon_time = priv->ucode_beacon_time;
3446 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
3447 }
3448
3449 fc = le16_to_cpu(header->frame_control);
3450 switch (fc & IEEE80211_FCTL_FTYPE) {
3451 case IEEE80211_FTYPE_MGMT:
Zhu Yib481de92007-09-25 17:54:57 -07003452 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3453 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3454 header->addr2);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003455 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003456 break;
3457
3458 case IEEE80211_FTYPE_CTL:
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02003459#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003460 switch (fc & IEEE80211_FCTL_STYPE) {
3461 case IEEE80211_STYPE_BACK_REQ:
3462 IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
3463 iwl4965_handle_data_packet(priv, 0, include_phy,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003464 rxb, &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003465 break;
3466 default:
3467 break;
3468 }
3469#endif
Zhu Yib481de92007-09-25 17:54:57 -07003470 break;
3471
Joe Perches0795af52007-10-03 17:59:30 -07003472 case IEEE80211_FTYPE_DATA: {
3473 DECLARE_MAC_BUF(mac1);
3474 DECLARE_MAC_BUF(mac2);
3475 DECLARE_MAC_BUF(mac3);
3476
Zhu Yib481de92007-09-25 17:54:57 -07003477 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3478 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3479 header->addr2);
3480
3481 if (unlikely(!network_packet))
3482 IWL_DEBUG_DROP("Dropping (non network): "
Joe Perches0795af52007-10-03 17:59:30 -07003483 "%s, %s, %s\n",
3484 print_mac(mac1, header->addr1),
3485 print_mac(mac2, header->addr2),
3486 print_mac(mac3, header->addr3));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003487 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
Joe Perches0795af52007-10-03 17:59:30 -07003488 IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
3489 print_mac(mac1, header->addr1),
3490 print_mac(mac2, header->addr2),
3491 print_mac(mac3, header->addr3));
Zhu Yib481de92007-09-25 17:54:57 -07003492 else
3493 iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003494 &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003495 break;
Joe Perches0795af52007-10-03 17:59:30 -07003496 }
Zhu Yib481de92007-09-25 17:54:57 -07003497 default:
3498 break;
3499
3500 }
3501}
3502
3503/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
3504 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003505static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003506 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003507{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003508 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003509 priv->last_phy_res[0] = 1;
3510 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
3511 sizeof(struct iwl4965_rx_phy_res));
3512}
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003513static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003514 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003515
3516{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003517#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003518 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3519 struct iwl4965_missed_beacon_notif *missed_beacon;
Zhu Yib481de92007-09-25 17:54:57 -07003520
3521 missed_beacon = &pkt->u.missed_beacon;
3522 if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
3523 IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
3524 le32_to_cpu(missed_beacon->consequtive_missed_beacons),
3525 le32_to_cpu(missed_beacon->total_missed_becons),
3526 le32_to_cpu(missed_beacon->num_recvd_beacons),
3527 le32_to_cpu(missed_beacon->num_expected_beacons));
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003528 if (!test_bit(STATUS_SCANNING, &priv->status))
3529 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003530 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003531#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
Zhu Yib481de92007-09-25 17:54:57 -07003532}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003533#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003534
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003535/**
3536 * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
3537 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003538static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003539 int sta_id, int tid)
3540{
3541 unsigned long flags;
3542
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003543 /* Remove "disable" flag, to enable Tx for this TID */
Zhu Yib481de92007-09-25 17:54:57 -07003544 spin_lock_irqsave(&priv->sta_lock, flags);
3545 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3546 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3547 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3548 spin_unlock_irqrestore(&priv->sta_lock, flags);
3549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003550 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07003551}
3552
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003553/**
3554 * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
3555 *
3556 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
3557 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
3558 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003559static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003560 struct iwl4965_ht_agg *agg,
3561 struct iwl4965_compressed_ba_resp*
Zhu Yib481de92007-09-25 17:54:57 -07003562 ba_resp)
3563
3564{
3565 int i, sh, ack;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003566 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
3567 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3568 u64 bitmap;
3569 int successes = 0;
3570 struct ieee80211_tx_status *tx_status;
Zhu Yib481de92007-09-25 17:54:57 -07003571
3572 if (unlikely(!agg->wait_for_ba)) {
3573 IWL_ERROR("Received BA when not expected\n");
3574 return -EINVAL;
3575 }
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003576
3577 /* Mark that the expected block-ack response arrived */
Zhu Yib481de92007-09-25 17:54:57 -07003578 agg->wait_for_ba = 0;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003579 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003580
3581 /* Calculate shift to align block-ack bits with our Tx window bits */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003582 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
Ian Schram01ebd062007-10-25 17:15:22 +08003583 if (sh < 0) /* tbw something is wrong with indices */
Zhu Yib481de92007-09-25 17:54:57 -07003584 sh += 0x100;
3585
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003586 /* don't use 64-bit values for now */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003587 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
Zhu Yib481de92007-09-25 17:54:57 -07003588
3589 if (agg->frame_count > (64 - sh)) {
3590 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
3591 return -1;
3592 }
3593
3594 /* check for success or failure according to the
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003595 * transmitted bitmap and block-ack bitmap */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003596 bitmap &= agg->bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003597
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003598 /* For each frame attempted in aggregation,
3599 * update driver's record of tx frame's status. */
Zhu Yib481de92007-09-25 17:54:57 -07003600 for (i = 0; i < agg->frame_count ; i++) {
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003601 ack = bitmap & (1 << i);
3602 successes += !!ack;
Zhu Yib481de92007-09-25 17:54:57 -07003603 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003604 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
3605 agg->start_idx + i);
Zhu Yib481de92007-09-25 17:54:57 -07003606 }
3607
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003608 tx_status = &priv->txq[scd_flow].txb[agg->start_idx].status;
3609 tx_status->flags = IEEE80211_TX_STATUS_ACK;
Ron Rindjunsky99556432008-01-28 14:07:25 +02003610 tx_status->flags |= IEEE80211_TX_STATUS_AMPDU;
3611 tx_status->ampdu_ack_map = successes;
3612 tx_status->ampdu_ack_len = agg->frame_count;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003613 iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags,
3614 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003615
John W. Linvillef868f4e2008-03-07 16:38:43 -05003616 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003617
3618 return 0;
3619}
3620
3621/**
3622 * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
3623 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003624static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003625 u16 txq_id)
3626{
3627 /* Simply stop the queue, but don't change any configuration;
3628 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003629 iwl_write_prph(priv,
Tomas Winkler12a81f62008-04-03 16:05:20 -07003630 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003631 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
3632 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3633}
3634
3635/**
3636 * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003637 * priv->lock must be held by the caller
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003638 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003639static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003640 u16 ssn_idx, u8 tx_fifo)
3641{
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003642 int ret = 0;
3643
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003644 if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3645 IWL_WARNING("queue number too small: %d, must be > %d\n",
3646 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3647 return -EINVAL;
3648 }
3649
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003650 ret = iwl_grab_nic_access(priv);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003651 if (ret)
3652 return ret;
3653
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003654 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3655
Tomas Winkler12a81f62008-04-03 16:05:20 -07003656 iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003657
3658 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3659 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3660 /* supposes that ssn_idx is valid (!= 0xFFF) */
3661 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3662
Tomas Winkler12a81f62008-04-03 16:05:20 -07003663 iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003664 iwl4965_txq_ctx_deactivate(priv, txq_id);
3665 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3666
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003667 iwl_release_nic_access(priv);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003668
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003669 return 0;
3670}
3671
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003672int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003673 u8 tid, int txq_id)
3674{
3675 struct iwl4965_queue *q = &priv->txq[txq_id].q;
3676 u8 *addr = priv->stations[sta_id].sta.sta.addr;
3677 struct iwl4965_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3678
3679 switch (priv->stations[sta_id].tid[tid].agg.state) {
3680 case IWL_EMPTYING_HW_QUEUE_DELBA:
3681 /* We are reclaiming the last packet of the */
3682 /* aggregated HW queue */
3683 if (txq_id == tid_data->agg.txq_id &&
3684 q->read_ptr == q->write_ptr) {
3685 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3686 int tx_fifo = default_tid_to_tx_fifo[tid];
3687 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3688 iwl4965_tx_queue_agg_disable(priv, txq_id,
3689 ssn, tx_fifo);
3690 tid_data->agg.state = IWL_AGG_OFF;
3691 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3692 }
3693 break;
3694 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3695 /* We are reclaiming the last packet of the queue */
3696 if (tid_data->tfds_in_queue == 0) {
3697 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3698 tid_data->agg.state = IWL_AGG_ON;
3699 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3700 }
3701 break;
3702 }
Zhu Yib481de92007-09-25 17:54:57 -07003703 return 0;
3704}
3705
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003706/**
3707 * iwl4965_queue_dec_wrap - Decrement queue index, wrap back to end if needed
3708 * @index -- current index
3709 * @n_bd -- total number of entries in queue (s/b power of 2)
3710 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -07003712{
3713 return (index == 0) ? n_bd - 1 : index - 1;
3714}
3715
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003716/**
3717 * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3718 *
3719 * Handles block-acknowledge notification from device, which reports success
3720 * of frames sent via aggregation.
3721 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003722static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003724{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003725 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3726 struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
Zhu Yib481de92007-09-25 17:54:57 -07003727 int index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003728 struct iwl4965_tx_queue *txq = NULL;
3729 struct iwl4965_ht_agg *agg;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003730 DECLARE_MAC_BUF(mac);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003731
3732 /* "flow" corresponds to Tx queue */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003733 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003734
3735 /* "ssn" is start of block-ack Tx window, corresponds to index
3736 * (in Tx queue's circular buffer) of first TFD/frame in window */
Zhu Yib481de92007-09-25 17:54:57 -07003737 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3738
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07003739 if (scd_flow >= priv->hw_params.max_txq_num) {
Zhu Yib481de92007-09-25 17:54:57 -07003740 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3741 return;
3742 }
3743
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003744 txq = &priv->txq[scd_flow];
Zhu Yib481de92007-09-25 17:54:57 -07003745 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003746
3747 /* Find index just before block-ack window */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003748 index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003749
Ian Schram01ebd062007-10-25 17:15:22 +08003750 /* TODO: Need to get this copy more safely - now good for debug */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003751
Joe Perches0795af52007-10-03 17:59:30 -07003752 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3753 "sta_id = %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003754 agg->wait_for_ba,
Joe Perches0795af52007-10-03 17:59:30 -07003755 print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
Zhu Yib481de92007-09-25 17:54:57 -07003756 ba_resp->sta_id);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003757 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
Zhu Yib481de92007-09-25 17:54:57 -07003758 "%d, scd_ssn = %d\n",
3759 ba_resp->tid,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003760 ba_resp->seq_ctl,
Tomas Winkler0310ae72008-03-11 16:17:19 -07003761 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
Zhu Yib481de92007-09-25 17:54:57 -07003762 ba_resp->scd_flow,
3763 ba_resp->scd_ssn);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003764 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
Zhu Yib481de92007-09-25 17:54:57 -07003765 agg->start_idx,
John W. Linvillef868f4e2008-03-07 16:38:43 -05003766 (unsigned long long)agg->bitmap);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003767
3768 /* Update driver's record of ACK vs. not for each frame in window */
Zhu Yib481de92007-09-25 17:54:57 -07003769 iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003770
3771 /* Release all TFDs before the SSN, i.e. all TFDs in front of
3772 * block-ack window (we assume that they've been successfully
3773 * transmitted ... if not, it's too late anyway). */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003774 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3775 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3776 priv->stations[ba_resp->sta_id].
3777 tid[ba_resp->tid].tfds_in_queue -= freed;
3778 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3779 priv->mac80211_registered &&
3780 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3781 ieee80211_wake_queue(priv->hw, scd_flow);
3782 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3783 ba_resp->tid, scd_flow);
3784 }
Zhu Yib481de92007-09-25 17:54:57 -07003785}
3786
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003787/**
3788 * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3789 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003790static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
Zhu Yib481de92007-09-25 17:54:57 -07003791 u16 txq_id)
3792{
3793 u32 tbl_dw_addr;
3794 u32 tbl_dw;
3795 u16 scd_q2ratid;
3796
3797 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3798
3799 tbl_dw_addr = priv->scd_base_addr +
3800 SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3801
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003802 tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003803
3804 if (txq_id & 0x1)
3805 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3806 else
3807 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3808
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003809 iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
Zhu Yib481de92007-09-25 17:54:57 -07003810
3811 return 0;
3812}
3813
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003814
Zhu Yib481de92007-09-25 17:54:57 -07003815/**
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003816 * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3817 *
3818 * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3819 * i.e. it must be one of the higher queues used for aggregation
Zhu Yib481de92007-09-25 17:54:57 -07003820 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003821static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
Zhu Yib481de92007-09-25 17:54:57 -07003822 int tx_fifo, int sta_id, int tid,
3823 u16 ssn_idx)
3824{
3825 unsigned long flags;
3826 int rc;
3827 u16 ra_tid;
3828
3829 if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3830 IWL_WARNING("queue number too small: %d, must be > %d\n",
3831 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3832
3833 ra_tid = BUILD_RAxTID(sta_id, tid);
3834
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003835 /* Modify device's station table to Tx this TID */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836 iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
Zhu Yib481de92007-09-25 17:54:57 -07003837
3838 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003839 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003840 if (rc) {
3841 spin_unlock_irqrestore(&priv->lock, flags);
3842 return rc;
3843 }
3844
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003845 /* Stop this Tx queue before configuring it */
Zhu Yib481de92007-09-25 17:54:57 -07003846 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3847
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003848 /* Map receiver-address / traffic-ID to this queue */
Zhu Yib481de92007-09-25 17:54:57 -07003849 iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3850
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003851 /* Set this queue as a chain-building queue */
Tomas Winkler12a81f62008-04-03 16:05:20 -07003852 iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Zhu Yib481de92007-09-25 17:54:57 -07003853
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003854 /* Place first TFD at index corresponding to start sequence number.
3855 * Assumes that ssn_idx is valid (!= 0xFFF) */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003856 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3857 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
Zhu Yib481de92007-09-25 17:54:57 -07003858 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3859
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003860 /* Set up Tx window size and frame limit for this queue */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003861 iwl_write_targ_mem(priv,
Zhu Yib481de92007-09-25 17:54:57 -07003862 priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3863 (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3864 SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3865
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003866 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07003867 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3868 (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3869 & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3870
Tomas Winkler12a81f62008-04-03 16:05:20 -07003871 iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Zhu Yib481de92007-09-25 17:54:57 -07003872
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003873 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
Zhu Yib481de92007-09-25 17:54:57 -07003874 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3875
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003876 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003877 spin_unlock_irqrestore(&priv->lock, flags);
3878
3879 return 0;
3880}
3881
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003882#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003883
3884/**
3885 * iwl4965_add_station - Initialize a station's hardware rate table
3886 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003887 * The uCode's station table contains a table of fallback rates
Zhu Yib481de92007-09-25 17:54:57 -07003888 * for automatic fallback during transmission.
3889 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003890 * NOTE: This sets up a default set of values. These will be replaced later
3891 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
3892 * rc80211_simple.
Zhu Yib481de92007-09-25 17:54:57 -07003893 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003894 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
3895 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
3896 * which requires station table entry to exist).
Zhu Yib481de92007-09-25 17:54:57 -07003897 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003898void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -07003899{
3900 int i, r;
Tomas Winkler66c73db2008-04-15 16:01:40 -07003901 struct iwl_link_quality_cmd link_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003902 .reserved1 = 0,
3903 };
3904 u16 rate_flags;
3905
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003906 /* Set up the rate scaling to start at selected rate, fall back
3907 * all the way down to 1M in IEEE order, and then spin on 1M */
Zhu Yib481de92007-09-25 17:54:57 -07003908 if (is_ap)
3909 r = IWL_RATE_54M_INDEX;
Johannes Berg8318d782008-01-24 19:38:38 +01003910 else if (priv->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003911 r = IWL_RATE_6M_INDEX;
3912 else
3913 r = IWL_RATE_1M_INDEX;
3914
3915 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3916 rate_flags = 0;
3917 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
3918 rate_flags |= RATE_MCS_CCK_MSK;
3919
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003920 /* Use Tx antenna B only */
Guy Cohenfde0db32008-04-21 15:42:01 -07003921 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003922
Zhu Yib481de92007-09-25 17:54:57 -07003923 link_cmd.rs_table[i].rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003924 iwl4965_hw_set_rate_n_flags(iwl4965_rates[r].plcp, rate_flags);
3925 r = iwl4965_get_prev_ieee_rate(r);
Zhu Yib481de92007-09-25 17:54:57 -07003926 }
3927
3928 link_cmd.general_params.single_stream_ant_msk = 2;
3929 link_cmd.general_params.dual_stream_ant_msk = 3;
3930 link_cmd.agg_params.agg_dis_start_th = 3;
3931 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
3932
3933 /* Update the rate scaling for control frame Tx to AP */
Tomas Winkler5425e492008-04-15 16:01:38 -07003934 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07003935
Tomas Winklere5472972008-03-28 16:21:12 -07003936 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
3937 sizeof(link_cmd), &link_cmd, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003938}
3939
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003940#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003941
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003942static u8 iwl4965_is_channel_extension(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01003943 enum ieee80211_band band,
Tomas Winkler78330fd2008-02-06 02:37:18 +02003944 u16 channel, u8 extension_chan_offset)
Zhu Yib481de92007-09-25 17:54:57 -07003945{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07003946 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07003947
Assaf Krauss8622e702008-03-21 13:53:43 -07003948 ch_info = iwl_get_channel_info(priv, band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07003949 if (!is_channel_valid(ch_info))
3950 return 0;
3951
Guy Cohen134eb5d2008-03-04 18:09:25 -08003952 if (extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE)
Zhu Yib481de92007-09-25 17:54:57 -07003953 return 0;
3954
3955 if ((ch_info->fat_extension_channel == extension_chan_offset) ||
3956 (ch_info->fat_extension_channel == HT_IE_EXT_CHANNEL_MAX))
3957 return 1;
3958
3959 return 0;
3960}
3961
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003962static u8 iwl4965_is_fat_tx_allowed(struct iwl_priv *priv,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003963 struct ieee80211_ht_info *sta_ht_inf)
Zhu Yib481de92007-09-25 17:54:57 -07003964{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003965 struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
Zhu Yib481de92007-09-25 17:54:57 -07003966
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003967 if ((!iwl_ht_conf->is_ht) ||
3968 (iwl_ht_conf->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ) ||
Guy Cohen134eb5d2008-03-04 18:09:25 -08003969 (iwl_ht_conf->extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE))
Zhu Yib481de92007-09-25 17:54:57 -07003970 return 0;
3971
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003972 if (sta_ht_inf) {
3973 if ((!sta_ht_inf->ht_supported) ||
Roel Kluin194c7ca2008-02-02 20:48:48 +01003974 (!(sta_ht_inf->cap & IEEE80211_HT_CAP_SUP_WIDTH)))
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003975 return 0;
3976 }
Zhu Yib481de92007-09-25 17:54:57 -07003977
Tomas Winkler78330fd2008-02-06 02:37:18 +02003978 return (iwl4965_is_channel_extension(priv, priv->band,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003979 iwl_ht_conf->control_channel,
3980 iwl_ht_conf->extension_chan_offset));
Zhu Yib481de92007-09-25 17:54:57 -07003981}
3982
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003983void iwl4965_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_info *ht_info)
Zhu Yib481de92007-09-25 17:54:57 -07003984{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003985 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07003986 u32 val;
3987
3988 if (!ht_info->is_ht)
3989 return;
3990
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003991 /* Set up channel bandwidth: 20 MHz only, or 20/40 mixed if fat ok */
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02003992 if (iwl4965_is_fat_tx_allowed(priv, NULL))
Zhu Yib481de92007-09-25 17:54:57 -07003993 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
3994 else
3995 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
3996 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
3997
3998 if (le16_to_cpu(rxon->channel) != ht_info->control_channel) {
3999 IWL_DEBUG_ASSOC("control diff than current %d %d\n",
4000 le16_to_cpu(rxon->channel),
4001 ht_info->control_channel);
4002 rxon->channel = cpu_to_le16(ht_info->control_channel);
4003 return;
4004 }
4005
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08004006 /* Note: control channel is opposite of extension channel */
Zhu Yib481de92007-09-25 17:54:57 -07004007 switch (ht_info->extension_chan_offset) {
4008 case IWL_EXT_CHANNEL_OFFSET_ABOVE:
4009 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
4010 break;
4011 case IWL_EXT_CHANNEL_OFFSET_BELOW:
4012 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
4013 break;
Guy Cohen134eb5d2008-03-04 18:09:25 -08004014 case IWL_EXT_CHANNEL_OFFSET_NONE:
Zhu Yib481de92007-09-25 17:54:57 -07004015 default:
4016 rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
4017 break;
4018 }
4019
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004020 val = ht_info->ht_protection;
Zhu Yib481de92007-09-25 17:54:57 -07004021
4022 rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
4023
Zhu Yib481de92007-09-25 17:54:57 -07004024 iwl4965_set_rxon_chain(priv);
4025
Guy Cohenfde0db32008-04-21 15:42:01 -07004026 IWL_DEBUG_ASSOC("supported HT rate 0x%X 0x%X 0x%X "
Zhu Yib481de92007-09-25 17:54:57 -07004027 "rxon flags 0x%X operation mode :0x%X "
4028 "extension channel offset 0x%x "
4029 "control chan %d\n",
Guy Cohenfde0db32008-04-21 15:42:01 -07004030 ht_info->supp_mcs_set[0],
4031 ht_info->supp_mcs_set[1],
4032 ht_info->supp_mcs_set[2],
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004033 le32_to_cpu(rxon->flags), ht_info->ht_protection,
Zhu Yib481de92007-09-25 17:54:57 -07004034 ht_info->extension_chan_offset,
4035 ht_info->control_channel);
4036 return;
4037}
4038
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004039void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004040 struct ieee80211_ht_info *sta_ht_inf)
Zhu Yib481de92007-09-25 17:54:57 -07004041{
4042 __le32 sta_flags;
Tomas Winklere53cfe02008-01-30 22:05:13 -08004043 u8 mimo_ps_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004044
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004045 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07004046 goto done;
4047
Tomas Winklere53cfe02008-01-30 22:05:13 -08004048 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
4049
Zhu Yib481de92007-09-25 17:54:57 -07004050 sta_flags = priv->stations[index].sta.station_flags;
4051
Tomas Winklere53cfe02008-01-30 22:05:13 -08004052 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
4053
4054 switch (mimo_ps_mode) {
4055 case WLAN_HT_CAP_MIMO_PS_STATIC:
4056 sta_flags |= STA_FLG_MIMO_DIS_MSK;
4057 break;
4058 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
Zhu Yib481de92007-09-25 17:54:57 -07004059 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Tomas Winklere53cfe02008-01-30 22:05:13 -08004060 break;
4061 case WLAN_HT_CAP_MIMO_PS_DISABLED:
4062 break;
4063 default:
4064 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
4065 break;
4066 }
Zhu Yib481de92007-09-25 17:54:57 -07004067
4068 sta_flags |= cpu_to_le32(
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004069 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Zhu Yib481de92007-09-25 17:54:57 -07004070
4071 sta_flags |= cpu_to_le32(
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004072 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Zhu Yib481de92007-09-25 17:54:57 -07004073
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004074 if (iwl4965_is_fat_tx_allowed(priv, sta_ht_inf))
Zhu Yib481de92007-09-25 17:54:57 -07004075 sta_flags |= STA_FLG_FAT_EN_MSK;
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004076 else
Tomas Winklere53cfe02008-01-30 22:05:13 -08004077 sta_flags &= ~STA_FLG_FAT_EN_MSK;
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004078
Zhu Yib481de92007-09-25 17:54:57 -07004079 priv->stations[index].sta.station_flags = sta_flags;
4080 done:
4081 return;
4082}
4083
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004084static int iwl4965_rx_agg_start(struct iwl_priv *priv,
4085 const u8 *addr, int tid, u16 ssn)
Zhu Yib481de92007-09-25 17:54:57 -07004086{
4087 unsigned long flags;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004088 int sta_id;
4089
4090 sta_id = iwl_find_station(priv, addr);
4091 if (sta_id == IWL_INVALID_STATION)
4092 return -ENXIO;
Zhu Yib481de92007-09-25 17:54:57 -07004093
4094 spin_lock_irqsave(&priv->sta_lock, flags);
4095 priv->stations[sta_id].sta.station_flags_msk = 0;
4096 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
4097 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
4098 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
4099 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4100 spin_unlock_irqrestore(&priv->sta_lock, flags);
4101
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004102 return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4103 CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07004104}
4105
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004106static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
4107 const u8 *addr, int tid)
Zhu Yib481de92007-09-25 17:54:57 -07004108{
4109 unsigned long flags;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004110 int sta_id;
4111
4112 sta_id = iwl_find_station(priv, addr);
4113 if (sta_id == IWL_INVALID_STATION)
4114 return -ENXIO;
Zhu Yib481de92007-09-25 17:54:57 -07004115
4116 spin_lock_irqsave(&priv->sta_lock, flags);
4117 priv->stations[sta_id].sta.station_flags_msk = 0;
4118 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
4119 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
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
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08004127/*
4128 * Find first available (lowest unused) Tx Queue, mark it "active".
4129 * Called only when finding queue for aggregation.
4130 * Should never return anything < 7, because they should already
4131 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
4132 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004133static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004134{
4135 int txq_id;
4136
Tomas Winkler5425e492008-04-15 16:01:38 -07004137 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Zhu Yib481de92007-09-25 17:54:57 -07004138 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
4139 return txq_id;
4140 return -1;
4141}
4142
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004143static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
4144 u16 tid, u16 *start_seq_num)
Zhu Yib481de92007-09-25 17:54:57 -07004145{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004146 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004147 int sta_id;
4148 int tx_fifo;
4149 int txq_id;
4150 int ssn = -1;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004151 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004152 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004153 struct iwl4965_tid_data *tid_data;
Joe Perches0795af52007-10-03 17:59:30 -07004154 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004155
4156 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4157 tx_fifo = default_tid_to_tx_fifo[tid];
4158 else
4159 return -EINVAL;
4160
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004161 IWL_WARNING("%s on ra = %s tid = %d\n",
4162 __func__, print_mac(mac, ra), tid);
Zhu Yib481de92007-09-25 17:54:57 -07004163
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004164 sta_id = iwl_find_station(priv, ra);
Zhu Yib481de92007-09-25 17:54:57 -07004165 if (sta_id == IWL_INVALID_STATION)
4166 return -ENXIO;
4167
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004168 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
4169 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
4170 return -ENXIO;
4171 }
4172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004173 txq_id = iwl4965_txq_ctx_activate_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004174 if (txq_id == -1)
4175 return -ENXIO;
4176
4177 spin_lock_irqsave(&priv->sta_lock, flags);
4178 tid_data = &priv->stations[sta_id].tid[tid];
4179 ssn = SEQ_TO_SN(tid_data->seq_number);
4180 tid_data->agg.txq_id = txq_id;
4181 spin_unlock_irqrestore(&priv->sta_lock, flags);
4182
4183 *start_seq_num = ssn;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004184 ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
4185 sta_id, tid, ssn);
4186 if (ret)
4187 return ret;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004188
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004189 ret = 0;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004190 if (tid_data->tfds_in_queue == 0) {
4191 printk(KERN_ERR "HW queue is empty\n");
4192 tid_data->agg.state = IWL_AGG_ON;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004193 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004194 } else {
4195 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
4196 tid_data->tfds_in_queue);
4197 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
4198 }
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004199 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004200}
4201
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004202static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
Zhu Yib481de92007-09-25 17:54:57 -07004203{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004204 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004205 int tx_fifo_id, txq_id, sta_id, ssn = -1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004206 struct iwl4965_tid_data *tid_data;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004207 int ret, write_ptr, read_ptr;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004208 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07004209 DECLARE_MAC_BUF(mac);
4210
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004211 if (!ra) {
4212 IWL_ERROR("ra = NULL\n");
Zhu Yib481de92007-09-25 17:54:57 -07004213 return -EINVAL;
4214 }
4215
4216 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4217 tx_fifo_id = default_tid_to_tx_fifo[tid];
4218 else
4219 return -EINVAL;
4220
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004221 sta_id = iwl_find_station(priv, ra);
Zhu Yib481de92007-09-25 17:54:57 -07004222
4223 if (sta_id == IWL_INVALID_STATION)
4224 return -ENXIO;
4225
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004226 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
4227 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
4228
Zhu Yib481de92007-09-25 17:54:57 -07004229 tid_data = &priv->stations[sta_id].tid[tid];
4230 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
4231 txq_id = tid_data->agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004232 write_ptr = priv->txq[txq_id].q.write_ptr;
4233 read_ptr = priv->txq[txq_id].q.read_ptr;
Zhu Yib481de92007-09-25 17:54:57 -07004234
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004235 /* The queue is not empty */
4236 if (write_ptr != read_ptr) {
4237 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
4238 priv->stations[sta_id].tid[tid].agg.state =
4239 IWL_EMPTYING_HW_QUEUE_DELBA;
4240 return 0;
4241 }
4242
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004243 IWL_DEBUG_HT("HW queue is empty\n");
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004244 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
4245
4246 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004247 ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004248 spin_unlock_irqrestore(&priv->lock, flags);
4249
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004250 if (ret)
4251 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004252
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004253 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
Zhu Yib481de92007-09-25 17:54:57 -07004254
4255 return 0;
4256}
4257
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004258int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
4259 enum ieee80211_ampdu_mlme_action action,
4260 const u8 *addr, u16 tid, u16 *ssn)
4261{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004262 struct iwl_priv *priv = hw->priv;
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004263 DECLARE_MAC_BUF(mac);
4264
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004265 IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
4266 print_mac(mac, addr), tid);
4267
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004268 switch (action) {
4269 case IEEE80211_AMPDU_RX_START:
4270 IWL_DEBUG_HT("start Rx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004271 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004272 case IEEE80211_AMPDU_RX_STOP:
4273 IWL_DEBUG_HT("stop Rx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004274 return iwl4965_rx_agg_stop(priv, addr, tid);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004275 case IEEE80211_AMPDU_TX_START:
4276 IWL_DEBUG_HT("start Tx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004277 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004278 case IEEE80211_AMPDU_TX_STOP:
4279 IWL_DEBUG_HT("stop Tx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004280 return iwl4965_tx_agg_stop(hw, addr, tid);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004281 default:
4282 IWL_DEBUG_HT("unknown\n");
4283 return -EINVAL;
4284 break;
4285 }
4286 return 0;
4287}
4288
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004289#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004290
4291/* Set up 4965-specific Rx frame reply handlers */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004292void iwl4965_hw_rx_handler_setup(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004293{
4294 /* Legacy Rx frames */
Tomas Winkler857485c2008-03-21 13:53:44 -07004295 priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
Zhu Yib481de92007-09-25 17:54:57 -07004296
4297 /* High-throughput (HT) Rx frames */
4298 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
4299 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
4300
4301 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
4302 iwl4965_rx_missed_beacon_notif;
4303
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004304#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07004305 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004306#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004307}
4308
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004309void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004310{
4311 INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004312#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07004313 INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
4314#endif
Zhu Yib481de92007-09-25 17:54:57 -07004315 init_timer(&priv->statistics_periodic);
4316 priv->statistics_periodic.data = (unsigned long)priv;
4317 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
4318}
4319
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004320void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004321{
4322 del_timer_sync(&priv->statistics_periodic);
4323
4324 cancel_delayed_work(&priv->init_alive_start);
4325}
4326
Tomas Winkler3c424c22008-04-15 16:01:42 -07004327
4328static struct iwl_hcmd_ops iwl4965_hcmd = {
Tomas Winkler7e8c5192008-04-15 16:01:43 -07004329 .rxon_assoc = iwl4965_send_rxon_assoc,
Tomas Winkler3c424c22008-04-15 16:01:42 -07004330};
4331
Tomas Winkler857485c2008-03-21 13:53:44 -07004332static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
4333 .enqueue_hcmd = iwl4965_enqueue_hcmd,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004334#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
4335 .chain_noise_reset = iwl4965_chain_noise_reset,
4336 .gain_computation = iwl4965_gain_computation,
4337#endif
Tomas Winkler857485c2008-03-21 13:53:44 -07004338};
4339
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004340static struct iwl_lib_ops iwl4965_lib = {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004341 .init_drv = iwl4965_init_drv,
Tomas Winkler5425e492008-04-15 16:01:38 -07004342 .set_hw_params = iwl4965_hw_set_hw_params,
Tomas Winklere2a722e2008-04-14 21:16:10 -07004343 .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
Tomas Winkler57aab752008-04-14 21:16:03 -07004344 .hw_nic_init = iwl4965_hw_nic_init,
4345 .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
4346 .alive_notify = iwl4965_alive_notify,
4347 .load_ucode = iwl4965_load_bsm,
Tomas Winkler6f4083a2008-04-16 16:34:49 -07004348 .apm_ops = {
Tomas Winkler91238712008-04-23 17:14:53 -07004349 .init = iwl4965_apm_init,
Tomas Winkler6f4083a2008-04-16 16:34:49 -07004350 .set_pwr_src = iwl4965_set_pwr_src,
4351 },
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004352 .eeprom_ops = {
Tomas Winkler073d3f52008-04-21 15:41:52 -07004353 .regulatory_bands = {
4354 EEPROM_REGULATORY_BAND_1_CHANNELS,
4355 EEPROM_REGULATORY_BAND_2_CHANNELS,
4356 EEPROM_REGULATORY_BAND_3_CHANNELS,
4357 EEPROM_REGULATORY_BAND_4_CHANNELS,
4358 EEPROM_REGULATORY_BAND_5_CHANNELS,
4359 EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
4360 EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
4361 },
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004362 .verify_signature = iwlcore_eeprom_verify_signature,
4363 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
4364 .release_semaphore = iwlcore_eeprom_release_semaphore,
Tomas Winkler073d3f52008-04-21 15:41:52 -07004365 .query_addr = iwlcore_eeprom_query_addr,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004366 },
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004367 .radio_kill_sw = iwl4965_radio_kill_sw,
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004368 .set_power = iwl4965_set_power,
4369 .update_chain_flags = iwl4965_update_chain_flags,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004370};
4371
4372static struct iwl_ops iwl4965_ops = {
4373 .lib = &iwl4965_lib,
Tomas Winkler3c424c22008-04-15 16:01:42 -07004374 .hcmd = &iwl4965_hcmd,
Tomas Winkler857485c2008-03-21 13:53:44 -07004375 .utils = &iwl4965_hcmd_utils,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004376};
4377
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004378struct iwl_cfg iwl4965_agn_cfg = {
Tomas Winkler82b9a122008-03-04 18:09:30 -08004379 .name = "4965AGN",
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004380 .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
Tomas Winkler82b9a122008-03-04 18:09:30 -08004381 .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
Tomas Winkler073d3f52008-04-21 15:41:52 -07004382 .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004383 .ops = &iwl4965_ops,
Assaf Krauss1ea87392008-03-18 14:57:50 -07004384 .mod_params = &iwl4965_mod_params,
Tomas Winkler82b9a122008-03-04 18:09:30 -08004385};
4386
Assaf Krauss1ea87392008-03-18 14:57:50 -07004387module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
4388MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
4389module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
4390MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -07004391module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
4392MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
Assaf Krauss1ea87392008-03-18 14:57:50 -07004393module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
4394MODULE_PARM_DESC(debug, "debug output mask");
4395module_param_named(
4396 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
4397MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4398
4399module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
4400MODULE_PARM_DESC(queues_num, "number of hw queues.");
4401
4402/* QoS */
4403module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
4404MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
4405module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
4406MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
4407