blob: 7886596b62c7c530ce6a4761a21c6b73776a0bba [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, \
61 IWL_RATE_MIMO_##s##M_PLCP, \
62 IWL_RATE_##r##M_IEEE, \
63 IWL_RATE_##ip##M_INDEX, \
64 IWL_RATE_##in##M_INDEX, \
65 IWL_RATE_##rp##M_INDEX, \
66 IWL_RATE_##rn##M_INDEX, \
67 IWL_RATE_##pp##M_INDEX, \
68 IWL_RATE_##np##M_INDEX }
69
70/*
71 * Parameter order:
72 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
73 *
74 * If there isn't a valid next or previous rate then INV is used which
75 * maps to IWL_RATE_INVALID
76 *
77 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080078const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
Zhu Yib481de92007-09-25 17:54:57 -070079 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
80 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
81 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
82 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
83 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
84 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
85 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
86 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
87 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
88 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
89 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
90 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
91 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
92};
93
Ron Rindjunskyfe01b472008-01-28 14:07:24 +020094#ifdef CONFIG_IWL4965_HT
95
96static const u16 default_tid_to_tx_fifo[] = {
97 IWL_TX_FIFO_AC1,
98 IWL_TX_FIFO_AC0,
99 IWL_TX_FIFO_AC0,
100 IWL_TX_FIFO_AC1,
101 IWL_TX_FIFO_AC2,
102 IWL_TX_FIFO_AC2,
103 IWL_TX_FIFO_AC3,
104 IWL_TX_FIFO_AC3,
105 IWL_TX_FIFO_NONE,
106 IWL_TX_FIFO_NONE,
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_AC3
114};
115
116#endif /*CONFIG_IWL4965_HT */
117
Tomas Winkler57aab752008-04-14 21:16:03 -0700118/* check contents of special bootstrap uCode SRAM */
119static int iwl4965_verify_bsm(struct iwl_priv *priv)
120{
121 __le32 *image = priv->ucode_boot.v_addr;
122 u32 len = priv->ucode_boot.len;
123 u32 reg;
124 u32 val;
125
126 IWL_DEBUG_INFO("Begin verify bsm\n");
127
128 /* verify BSM SRAM contents */
129 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
130 for (reg = BSM_SRAM_LOWER_BOUND;
131 reg < BSM_SRAM_LOWER_BOUND + len;
132 reg += sizeof(u32), image++) {
133 val = iwl_read_prph(priv, reg);
134 if (val != le32_to_cpu(*image)) {
135 IWL_ERROR("BSM uCode verification failed at "
136 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
137 BSM_SRAM_LOWER_BOUND,
138 reg - BSM_SRAM_LOWER_BOUND, len,
139 val, le32_to_cpu(*image));
140 return -EIO;
141 }
142 }
143
144 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
145
146 return 0;
147}
148
149/**
150 * iwl4965_load_bsm - Load bootstrap instructions
151 *
152 * BSM operation:
153 *
154 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
155 * in special SRAM that does not power down during RFKILL. When powering back
156 * up after power-saving sleeps (or during initial uCode load), the BSM loads
157 * the bootstrap program into the on-board processor, and starts it.
158 *
159 * The bootstrap program loads (via DMA) instructions and data for a new
160 * program from host DRAM locations indicated by the host driver in the
161 * BSM_DRAM_* registers. Once the new program is loaded, it starts
162 * automatically.
163 *
164 * When initializing the NIC, the host driver points the BSM to the
165 * "initialize" uCode image. This uCode sets up some internal data, then
166 * notifies host via "initialize alive" that it is complete.
167 *
168 * The host then replaces the BSM_DRAM_* pointer values to point to the
169 * normal runtime uCode instructions and a backup uCode data cache buffer
170 * (filled initially with starting data values for the on-board processor),
171 * then triggers the "initialize" uCode to load and launch the runtime uCode,
172 * which begins normal operation.
173 *
174 * When doing a power-save shutdown, runtime uCode saves data SRAM into
175 * the backup data cache in DRAM before SRAM is powered down.
176 *
177 * When powering back up, the BSM loads the bootstrap program. This reloads
178 * the runtime uCode instructions and the backup data cache into SRAM,
179 * and re-launches the runtime uCode from where it left off.
180 */
181static int iwl4965_load_bsm(struct iwl_priv *priv)
182{
183 __le32 *image = priv->ucode_boot.v_addr;
184 u32 len = priv->ucode_boot.len;
185 dma_addr_t pinst;
186 dma_addr_t pdata;
187 u32 inst_len;
188 u32 data_len;
189 int i;
190 u32 done;
191 u32 reg_offset;
192 int ret;
193
194 IWL_DEBUG_INFO("Begin load bsm\n");
195
196 /* make sure bootstrap program is no larger than BSM's SRAM size */
197 if (len > IWL_MAX_BSM_SIZE)
198 return -EINVAL;
199
200 /* Tell bootstrap uCode where to find the "Initialize" uCode
201 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
202 * NOTE: iwl4965_initialize_alive_start() will replace these values,
203 * after the "initialize" uCode has run, to point to
204 * runtime/protocol instructions and backup data cache. */
205 pinst = priv->ucode_init.p_addr >> 4;
206 pdata = priv->ucode_init_data.p_addr >> 4;
207 inst_len = priv->ucode_init.len;
208 data_len = priv->ucode_init_data.len;
209
210 ret = iwl_grab_nic_access(priv);
211 if (ret)
212 return ret;
213
214 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
215 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
216 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
217 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
218
219 /* Fill BSM memory with bootstrap instructions */
220 for (reg_offset = BSM_SRAM_LOWER_BOUND;
221 reg_offset < BSM_SRAM_LOWER_BOUND + len;
222 reg_offset += sizeof(u32), image++)
223 _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
224
225 ret = iwl4965_verify_bsm(priv);
226 if (ret) {
227 iwl_release_nic_access(priv);
228 return ret;
229 }
230
231 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
232 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
233 iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
234 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
235
236 /* Load bootstrap code into instruction SRAM now,
237 * to prepare to load "initialize" uCode */
238 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
239
240 /* Wait for load of bootstrap uCode to finish */
241 for (i = 0; i < 100; i++) {
242 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
243 if (!(done & BSM_WR_CTRL_REG_BIT_START))
244 break;
245 udelay(10);
246 }
247 if (i < 100)
248 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
249 else {
250 IWL_ERROR("BSM write did not complete!\n");
251 return -EIO;
252 }
253
254 /* Enable future boot loads whenever power management unit triggers it
255 * (e.g. when powering back up after power-save shutdown) */
256 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
257
258 iwl_release_nic_access(priv);
259
260 return 0;
261}
262
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700263static int iwl4965_init_drv(struct iwl_priv *priv)
264{
265 int ret;
266 int i;
267
Assaf Krauss1ea87392008-03-18 14:57:50 -0700268 priv->antenna = (enum iwl4965_antenna)priv->cfg->mod_params->antenna;
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700269 priv->retry_rate = 1;
270 priv->ibss_beacon = NULL;
271
272 spin_lock_init(&priv->lock);
273 spin_lock_init(&priv->power_data.lock);
274 spin_lock_init(&priv->sta_lock);
275 spin_lock_init(&priv->hcmd_lock);
276 spin_lock_init(&priv->lq_mngr.lock);
277
Tomas Winkler059ff822008-04-14 21:16:14 -0700278 priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
279 sizeof(struct iwl4965_shared),
280 &priv->shared_phys);
281
282 if (!priv->shared_virt) {
283 ret = -ENOMEM;
284 goto err;
285 }
286
287 memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
288
289
Assaf Kraussbf85ea42008-03-14 10:38:49 -0700290 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
291 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
292
293 INIT_LIST_HEAD(&priv->free_frames);
294
295 mutex_init(&priv->mutex);
296
297 /* Clear the driver's (not device's) station table */
298 iwlcore_clear_stations_table(priv);
299
300 priv->data_retry_limit = -1;
301 priv->ieee_channels = NULL;
302 priv->ieee_rates = NULL;
303 priv->band = IEEE80211_BAND_2GHZ;
304
305 priv->iw_mode = IEEE80211_IF_TYPE_STA;
306
307 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
308 priv->valid_antenna = 0x7; /* assume all 3 connected */
309 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
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700364static u8 is_single_stream(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700365{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800366#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +0200367 if (!priv->current_ht_config.is_ht ||
368 (priv->current_ht_config.supp_mcs_set[1] == 0) ||
Zhu Yib481de92007-09-25 17:54:57 -0700369 (priv->ps_mode == IWL_MIMO_PS_STATIC))
370 return 1;
371#else
372 return 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800373#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700374 return 0;
375}
376
Tomas Winkler17744ff2008-03-02 01:52:00 +0200377int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
378{
379 int idx = 0;
380
381 /* 4965 HT rate format */
382 if (rate_n_flags & RATE_MCS_HT_MSK) {
383 idx = (rate_n_flags & 0xff);
384
385 if (idx >= IWL_RATE_MIMO_6M_PLCP)
386 idx = idx - IWL_RATE_MIMO_6M_PLCP;
387
388 idx += IWL_FIRST_OFDM_RATE;
389 /* skip 9M not supported in ht*/
390 if (idx >= IWL_RATE_9M_INDEX)
391 idx += 1;
392 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
393 return idx;
394
395 /* 4965 legacy rate format, search for match in table */
396 } else {
397 for (idx = 0; idx < ARRAY_SIZE(iwl4965_rates); idx++)
398 if (iwl4965_rates[idx].plcp == (rate_n_flags & 0xFF))
399 return idx;
400 }
401
402 return -1;
403}
404
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800405/**
406 * translate ucode response to mac80211 tx status control values
407 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700408void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800409 struct ieee80211_tx_control *control)
410{
411 int rate_index;
412
413 control->antenna_sel_tx =
Tomas Winklerec35cf22008-04-15 16:01:39 -0700414 ((rate_n_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800415 if (rate_n_flags & RATE_MCS_HT_MSK)
416 control->flags |= IEEE80211_TXCTL_OFDM_HT;
417 if (rate_n_flags & RATE_MCS_GF_MSK)
418 control->flags |= IEEE80211_TXCTL_GREEN_FIELD;
419 if (rate_n_flags & RATE_MCS_FAT_MSK)
420 control->flags |= IEEE80211_TXCTL_40_MHZ_WIDTH;
421 if (rate_n_flags & RATE_MCS_DUP_MSK)
422 control->flags |= IEEE80211_TXCTL_DUP_DATA;
423 if (rate_n_flags & RATE_MCS_SGI_MSK)
424 control->flags |= IEEE80211_TXCTL_SHORT_GI;
425 /* since iwl4965_hwrate_to_plcp_idx is band indifferent, we always use
426 * IEEE80211_BAND_2GHZ band as it contains all the rates */
427 rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
428 if (rate_index == -1)
429 control->tx_rate = NULL;
430 else
431 control->tx_rate =
432 &priv->bands[IEEE80211_BAND_2GHZ].bitrates[rate_index];
433}
Tomas Winkler17744ff2008-03-02 01:52:00 +0200434
Zhu Yib481de92007-09-25 17:54:57 -0700435/*
436 * Determine how many receiver/antenna chains to use.
437 * More provides better reception via diversity. Fewer saves power.
438 * MIMO (dual stream) requires at least 2, but works better with 3.
439 * This does not determine *which* chains to use, just how many.
440 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700441static int iwl4965_get_rx_chain_counter(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700442 u8 *idle_state, u8 *rx_state)
443{
444 u8 is_single = is_single_stream(priv);
445 u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
446
447 /* # of Rx chains to use when expecting MIMO. */
448 if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
449 *rx_state = 2;
450 else
451 *rx_state = 3;
452
453 /* # Rx chains when idling and maybe trying to save power */
454 switch (priv->ps_mode) {
455 case IWL_MIMO_PS_STATIC:
456 case IWL_MIMO_PS_DYNAMIC:
457 *idle_state = (is_cam) ? 2 : 1;
458 break;
459 case IWL_MIMO_PS_NONE:
460 *idle_state = (is_cam) ? *rx_state : 1;
461 break;
462 default:
463 *idle_state = 1;
464 break;
465 }
466
467 return 0;
468}
469
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700470int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700471{
472 int rc;
473 unsigned long flags;
474
475 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700476 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700477 if (rc) {
478 spin_unlock_irqrestore(&priv->lock, flags);
479 return rc;
480 }
481
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800482 /* stop Rx DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700483 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
484 rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
Zhu Yib481de92007-09-25 17:54:57 -0700485 (1 << 24), 1000);
486 if (rc < 0)
487 IWL_ERROR("Can't stop Rx DMA.\n");
488
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700489 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700490 spin_unlock_irqrestore(&priv->lock, flags);
491
492 return 0;
493}
494
Tomas Winkler079a2532008-04-17 16:03:39 -0700495int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
Zhu Yib481de92007-09-25 17:54:57 -0700496{
Tomas Winklerd8609652007-10-25 17:15:35 +0800497 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700498 unsigned long flags;
499
500 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700501 ret = iwl_grab_nic_access(priv);
Tomas Winklerd8609652007-10-25 17:15:35 +0800502 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700503 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winklerd8609652007-10-25 17:15:35 +0800504 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700505 }
506
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700507 if (src == IWL_PWR_SRC_VAUX) {
Zhu Yib481de92007-09-25 17:54:57 -0700508 u32 val;
Tomas Winklerd8609652007-10-25 17:15:35 +0800509 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700510 &val);
Zhu Yib481de92007-09-25 17:54:57 -0700511
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700512 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700513 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700514 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
515 ~APMG_PS_CTRL_MSK_PWR_SRC);
516 }
517 } else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700518 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700519 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
520 ~APMG_PS_CTRL_MSK_PWR_SRC);
521 }
Zhu Yib481de92007-09-25 17:54:57 -0700522
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700523 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700524 spin_unlock_irqrestore(&priv->lock, flags);
525
Tomas Winklerd8609652007-10-25 17:15:35 +0800526 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700527}
528
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700529static int iwl4965_rx_init(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -0700530{
Tomas Winkler059ff822008-04-14 21:16:14 -0700531 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700532 unsigned long flags;
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200533 unsigned int rb_size;
Zhu Yib481de92007-09-25 17:54:57 -0700534
535 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler059ff822008-04-14 21:16:14 -0700536 ret = iwl_grab_nic_access(priv);
537 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -0700538 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler059ff822008-04-14 21:16:14 -0700539 return ret;
Zhu Yib481de92007-09-25 17:54:57 -0700540 }
541
Assaf Krauss1ea87392008-03-18 14:57:50 -0700542 if (priv->cfg->mod_params->amsdu_size_8K)
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +0200543 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
544 else
545 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
546
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800547 /* Stop Rx DMA */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700548 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700549
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800550 /* Reset driver's Rx queue write index */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700551 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800552
553 /* Tell device where to find RBD circular buffer in DRAM */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700554 iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
555 rxq->dma_addr >> 8);
Zhu Yib481de92007-09-25 17:54:57 -0700556
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800557 /* Tell device where in DRAM to update its Rx status */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700558 iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
Tomas Winkler059ff822008-04-14 21:16:14 -0700559 (priv->shared_phys +
560 offsetof(struct iwl4965_shared, rb_closed)) >> 4);
Zhu Yib481de92007-09-25 17:54:57 -0700561
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800562 /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700563 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
564 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
565 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
566 rb_size |
Tomas Winkler059ff822008-04-14 21:16:14 -0700567 /* 0x10 << 4 | */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700568 (RX_QUEUE_SIZE_LOG <<
Zhu Yib481de92007-09-25 17:54:57 -0700569 FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
570
571 /*
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700572 * iwl_write32(priv,CSR_INT_COAL_REG,0);
Zhu Yib481de92007-09-25 17:54:57 -0700573 */
574
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700575 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700576 spin_unlock_irqrestore(&priv->lock, flags);
577
578 return 0;
579}
580
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800581/* Tell 4965 where to find the "keep warm" buffer */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700582static int iwl4965_kw_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700583{
584 unsigned long flags;
585 int rc;
586
587 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700588 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700589 if (rc)
590 goto out;
591
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700592 iwl_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
Zhu Yib481de92007-09-25 17:54:57 -0700593 priv->kw.dma_addr >> 4);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700594 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700595out:
596 spin_unlock_irqrestore(&priv->lock, flags);
597 return rc;
598}
599
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700600static int iwl4965_kw_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700601{
602 struct pci_dev *dev = priv->pci_dev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800603 struct iwl4965_kw *kw = &priv->kw;
Zhu Yib481de92007-09-25 17:54:57 -0700604
605 kw->size = IWL4965_KW_SIZE; /* TBW need set somewhere else */
606 kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
607 if (!kw->v_addr)
608 return -ENOMEM;
609
610 return 0;
611}
612
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800613/**
614 * iwl4965_kw_free - Free the "keep warm" buffer
615 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700616static void iwl4965_kw_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700617{
618 struct pci_dev *dev = priv->pci_dev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800619 struct iwl4965_kw *kw = &priv->kw;
Zhu Yib481de92007-09-25 17:54:57 -0700620
621 if (kw->v_addr) {
622 pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
623 memset(kw, 0, sizeof(*kw));
624 }
625}
626
627/**
628 * iwl4965_txq_ctx_reset - Reset TX queue context
629 * Destroys all DMA structures and initialise them again
630 *
631 * @param priv
632 * @return error code
633 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700634static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700635{
636 int rc = 0;
637 int txq_id, slots_num;
638 unsigned long flags;
639
640 iwl4965_kw_free(priv);
641
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800642 /* Free all tx/cmd queues and keep-warm buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800643 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700644
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800645 /* Alloc keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700646 rc = iwl4965_kw_alloc(priv);
647 if (rc) {
648 IWL_ERROR("Keep Warm allocation failed");
649 goto error_kw;
650 }
651
652 spin_lock_irqsave(&priv->lock, flags);
653
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700654 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700655 if (unlikely(rc)) {
656 IWL_ERROR("TX reset failed");
657 spin_unlock_irqrestore(&priv->lock, flags);
658 goto error_reset;
659 }
660
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800661 /* Turn off all Tx DMA channels */
Tomas Winkler12a81f62008-04-03 16:05:20 -0700662 iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700663 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700664 spin_unlock_irqrestore(&priv->lock, flags);
665
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800666 /* Tell 4965 where to find the keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700667 rc = iwl4965_kw_init(priv);
668 if (rc) {
669 IWL_ERROR("kw_init failed\n");
670 goto error_reset;
671 }
672
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800673 /* Alloc and init all (default 16) Tx queues,
674 * including the command queue (#4) */
Tomas Winkler5425e492008-04-15 16:01:38 -0700675 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
Zhu Yib481de92007-09-25 17:54:57 -0700676 slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
677 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800678 rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
Zhu Yib481de92007-09-25 17:54:57 -0700679 txq_id);
680 if (rc) {
681 IWL_ERROR("Tx %d queue init failed\n", txq_id);
682 goto error;
683 }
684 }
685
686 return rc;
687
688 error:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800689 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700690 error_reset:
691 iwl4965_kw_free(priv);
692 error_kw:
693 return rc;
694}
695
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700696int iwl4965_hw_nic_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700697{
698 int rc;
699 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -0700701 u8 rev_id;
Zhu Yib481de92007-09-25 17:54:57 -0700702 u8 val_link;
Tomas Winkler073d3f52008-04-21 15:41:52 -0700703 u16 sku_cap;
704 u32 val;
Zhu Yib481de92007-09-25 17:54:57 -0700705
Zhu Yib481de92007-09-25 17:54:57 -0700706 /* nic_init */
707 spin_lock_irqsave(&priv->lock, flags);
708
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700709 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Zhu Yib481de92007-09-25 17:54:57 -0700710 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
711
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700712 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
713 rc = iwl_poll_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -0700714 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
715 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
716 if (rc < 0) {
717 spin_unlock_irqrestore(&priv->lock, flags);
718 IWL_DEBUG_INFO("Failed to init the card\n");
719 return rc;
720 }
721
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700722 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700723 if (rc) {
724 spin_unlock_irqrestore(&priv->lock, flags);
725 return rc;
726 }
727
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700728 iwl_read_prph(priv, APMG_CLK_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -0700729
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700730 iwl_write_prph(priv, APMG_CLK_CTRL_REG,
731 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
732 iwl_read_prph(priv, APMG_CLK_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -0700733
734 udelay(20);
735
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700736 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
737 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Zhu Yib481de92007-09-25 17:54:57 -0700738
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700739 iwl_release_nic_access(priv);
740 iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
Zhu Yib481de92007-09-25 17:54:57 -0700741 spin_unlock_irqrestore(&priv->lock, flags);
742
743 /* Determine HW type */
744 rc = pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &rev_id);
745 if (rc)
746 return rc;
747
748 IWL_DEBUG_INFO("HW Revision ID = 0x%X\n", rev_id);
749
Tomas Winkler6f4083a2008-04-16 16:34:49 -0700750 rc = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
751
Zhu Yib481de92007-09-25 17:54:57 -0700752 spin_lock_irqsave(&priv->lock, flags);
753
754 if ((rev_id & 0x80) == 0x80 && (rev_id & 0x7f) < 8) {
755 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
756 /* Enable No Snoop field */
757 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
758 val & ~(1 << 11));
759 }
760
761 spin_unlock_irqrestore(&priv->lock, flags);
762
Tomas Winkler073d3f52008-04-21 15:41:52 -0700763 if (iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET) <
764 EEPROM_4965_TX_POWER_VERSION) {
Zhu Yib481de92007-09-25 17:54:57 -0700765 IWL_ERROR("Older EEPROM detected! Aborting.\n");
766 return -EINVAL;
767 }
768
769 pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
770
771 /* disable L1 entry -- workaround for pre-B1 */
772 pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
773
774 spin_lock_irqsave(&priv->lock, flags);
775
776 /* set CSR_HW_CONFIG_REG for uCode use */
777
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700778 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
779 CSR49_HW_IF_CONFIG_REG_BIT_4965_R |
780 CSR49_HW_IF_CONFIG_REG_BIT_RADIO_SI |
781 CSR49_HW_IF_CONFIG_REG_BIT_MAC_SI);
Zhu Yib481de92007-09-25 17:54:57 -0700782
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700783 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700784 if (rc < 0) {
785 spin_unlock_irqrestore(&priv->lock, flags);
786 IWL_DEBUG_INFO("Failed to init the card\n");
787 return rc;
788 }
789
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700790 iwl_read_prph(priv, APMG_PS_CTRL_REG);
791 iwl_set_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
Zhu Yib481de92007-09-25 17:54:57 -0700792 udelay(5);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700793 iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
Zhu Yib481de92007-09-25 17:54:57 -0700794
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700795 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700796 spin_unlock_irqrestore(&priv->lock, flags);
797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800798 iwl4965_hw_card_show_info(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700799
800 /* end nic_init */
801
802 /* Allocate the RX queue, or reset if it is already allocated */
803 if (!rxq->bd) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800804 rc = iwl4965_rx_queue_alloc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700805 if (rc) {
806 IWL_ERROR("Unable to initialize Rx queue\n");
807 return -ENOMEM;
808 }
809 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800810 iwl4965_rx_queue_reset(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -0700811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800812 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700813
814 iwl4965_rx_init(priv, rxq);
815
816 spin_lock_irqsave(&priv->lock, flags);
817
818 rxq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800819 iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -0700820
Tomas Winkler073d3f52008-04-21 15:41:52 -0700821 /* init the txpower calibration pointer */
822 priv->calib_info = (struct iwl_eeprom_calib_info *)
823 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
824
Zhu Yib481de92007-09-25 17:54:57 -0700825 spin_unlock_irqrestore(&priv->lock, flags);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800826
827 /* Allocate and init all Tx and Command queues */
Zhu Yib481de92007-09-25 17:54:57 -0700828 rc = iwl4965_txq_ctx_reset(priv);
829 if (rc)
830 return rc;
831
Tomas Winkler073d3f52008-04-21 15:41:52 -0700832 sku_cap = iwl_eeprom_query16(priv, EEPROM_SKU_CAP);
833 if (sku_cap & EEPROM_SKU_CAP_SW_RF_KILL_ENABLE)
Zhu Yib481de92007-09-25 17:54:57 -0700834 IWL_DEBUG_RF_KILL("SW RF KILL supported in EEPROM.\n");
835
Tomas Winkler073d3f52008-04-21 15:41:52 -0700836 if (sku_cap & EEPROM_SKU_CAP_HW_RF_KILL_ENABLE)
Zhu Yib481de92007-09-25 17:54:57 -0700837 IWL_DEBUG_RF_KILL("HW RF KILL supported in EEPROM.\n");
838
839 set_bit(STATUS_INIT, &priv->status);
840
841 return 0;
842}
843
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700844int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700845{
846 int rc = 0;
847 u32 reg_val;
848 unsigned long flags;
849
850 spin_lock_irqsave(&priv->lock, flags);
851
852 /* set stop master bit */
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700853 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
Zhu Yib481de92007-09-25 17:54:57 -0700854
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700855 reg_val = iwl_read32(priv, CSR_GP_CNTRL);
Zhu Yib481de92007-09-25 17:54:57 -0700856
857 if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
858 (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
859 IWL_DEBUG_INFO("Card in power save, master is already "
860 "stopped\n");
861 else {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700862 rc = iwl_poll_bit(priv, CSR_RESET,
Zhu Yib481de92007-09-25 17:54:57 -0700863 CSR_RESET_REG_FLAG_MASTER_DISABLED,
864 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
865 if (rc < 0) {
866 spin_unlock_irqrestore(&priv->lock, flags);
867 return rc;
868 }
869 }
870
871 spin_unlock_irqrestore(&priv->lock, flags);
872 IWL_DEBUG_INFO("stop master\n");
873
874 return rc;
875}
876
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800877/**
878 * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
879 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700880void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700881{
882
883 int txq_id;
884 unsigned long flags;
885
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800886 /* Stop each Tx DMA channel, and wait for it to be idle */
Tomas Winkler5425e492008-04-15 16:01:38 -0700887 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
Zhu Yib481de92007-09-25 17:54:57 -0700888 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700889 if (iwl_grab_nic_access(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -0700890 spin_unlock_irqrestore(&priv->lock, flags);
891 continue;
892 }
893
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700894 iwl_write_direct32(priv,
895 IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
896 iwl_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
897 IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
898 (txq_id), 200);
899 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700900 spin_unlock_irqrestore(&priv->lock, flags);
901 }
902
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800903 /* Deallocate memory for all Tx queues */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800904 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700905}
906
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700907int iwl4965_hw_nic_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700908{
909 int rc = 0;
910 unsigned long flags;
911
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800912 iwl4965_hw_nic_stop_master(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700913
914 spin_lock_irqsave(&priv->lock, flags);
915
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700916 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Zhu Yib481de92007-09-25 17:54:57 -0700917
918 udelay(10);
919
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700920 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
921 rc = iwl_poll_bit(priv, CSR_RESET,
Zhu Yib481de92007-09-25 17:54:57 -0700922 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
923 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
924
925 udelay(10);
926
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700927 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700928 if (!rc) {
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700929 iwl_write_prph(priv, APMG_CLK_EN_REG,
930 APMG_CLK_VAL_DMA_CLK_RQT |
931 APMG_CLK_VAL_BSM_CLK_RQT);
Zhu Yib481de92007-09-25 17:54:57 -0700932
933 udelay(10);
934
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700935 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
936 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
Zhu Yib481de92007-09-25 17:54:57 -0700937
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700938 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700939 }
940
941 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
942 wake_up_interruptible(&priv->wait_command_queue);
943
944 spin_unlock_irqrestore(&priv->lock, flags);
945
946 return rc;
947
948}
949
950#define REG_RECALIB_PERIOD (60)
951
952/**
953 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
954 *
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700955 * This callback is provided in order to send a statistics request.
Zhu Yib481de92007-09-25 17:54:57 -0700956 *
957 * This timer function is continually reset to execute within
958 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
959 * was received. We need to ensure we receive the statistics in order
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700960 * to update the temperature used for calibrating the TXPOWER.
Zhu Yib481de92007-09-25 17:54:57 -0700961 */
962static void iwl4965_bg_statistics_periodic(unsigned long data)
963{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700964 struct iwl_priv *priv = (struct iwl_priv *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700965
Zhu Yib481de92007-09-25 17:54:57 -0700966 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
967 return;
968
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -0700969 iwl_send_statistics_request(priv, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -0700970}
971
972#define CT_LIMIT_CONST 259
973#define TM_CT_KILL_THRESHOLD 110
974
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700975void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700976{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800977 struct iwl4965_ct_kill_config cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700978 u32 R1, R2, R3;
979 u32 temp_th;
980 u32 crit_temperature;
981 unsigned long flags;
Tomas Winkler857485c2008-03-21 13:53:44 -0700982 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700983
984 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -0700985 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -0700986 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
987 spin_unlock_irqrestore(&priv->lock, flags);
988
989 if (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK) {
990 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
991 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
992 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
993 } else {
994 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
995 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
996 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
997 }
998
999 temp_th = CELSIUS_TO_KELVIN(TM_CT_KILL_THRESHOLD);
1000
1001 crit_temperature = ((temp_th * (R3-R1))/CT_LIMIT_CONST) + R2;
1002 cmd.critical_temperature_R = cpu_to_le32(crit_temperature);
Tomas Winkler857485c2008-03-21 13:53:44 -07001003 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
1004 sizeof(cmd), &cmd);
1005 if (ret)
Zhu Yib481de92007-09-25 17:54:57 -07001006 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
1007 else
1008 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded\n");
1009}
1010
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001011#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07001012
1013/* Reset differential Rx gains in NIC to prepare for chain noise calibration.
1014 * Called after every association, but this runs only once!
1015 * ... once chain noise is calibrated the first time, it's good forever. */
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001016static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001017{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001018 struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
Zhu Yib481de92007-09-25 17:54:57 -07001019
Tomas Winkler3109ece2008-03-28 16:33:35 -07001020 if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001021 struct iwl4965_calibration_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001022
1023 memset(&cmd, 0, sizeof(cmd));
1024 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1025 cmd.diff_gain_a = 0;
1026 cmd.diff_gain_b = 0;
1027 cmd.diff_gain_c = 0;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001028 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1029 sizeof(cmd), &cmd))
1030 IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
Zhu Yib481de92007-09-25 17:54:57 -07001031 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1032 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
1033 }
Zhu Yib481de92007-09-25 17:54:57 -07001034}
1035
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001036static void iwl4965_gain_computation(struct iwl_priv *priv,
1037 u32 *average_noise,
1038 u16 min_average_noise_antenna_i,
1039 u32 min_average_noise)
Zhu Yib481de92007-09-25 17:54:57 -07001040{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001041 int i, ret;
1042 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
Zhu Yib481de92007-09-25 17:54:57 -07001043
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001044 data->delta_gain_code[min_average_noise_antenna_i] = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001045
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001046 for (i = 0; i < NUM_RX_CHAINS; i++) {
1047 s32 delta_g = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001048
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001049 if (!(data->disconn_array[i]) &&
1050 (data->delta_gain_code[i] ==
Zhu Yib481de92007-09-25 17:54:57 -07001051 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001052 delta_g = average_noise[i] - min_average_noise;
1053 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
1054 data->delta_gain_code[i] =
1055 min(data->delta_gain_code[i],
1056 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
Zhu Yib481de92007-09-25 17:54:57 -07001057
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001058 data->delta_gain_code[i] =
1059 (data->delta_gain_code[i] | (1 << 2));
1060 } else {
1061 data->delta_gain_code[i] = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001062 }
Zhu Yib481de92007-09-25 17:54:57 -07001063 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001064 IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
1065 data->delta_gain_code[0],
1066 data->delta_gain_code[1],
1067 data->delta_gain_code[2]);
Zhu Yib481de92007-09-25 17:54:57 -07001068
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001069 /* Differential gain gets sent to uCode only once */
1070 if (!data->radio_write) {
1071 struct iwl4965_calibration_cmd cmd;
1072 data->radio_write = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001073
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001074 memset(&cmd, 0, sizeof(cmd));
1075 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1076 cmd.diff_gain_a = data->delta_gain_code[0];
1077 cmd.diff_gain_b = data->delta_gain_code[1];
1078 cmd.diff_gain_c = data->delta_gain_code[2];
1079 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1080 sizeof(cmd), &cmd);
1081 if (ret)
1082 IWL_DEBUG_CALIB("fail sending cmd "
1083 "REPLY_PHY_CALIBRATION_CMD \n");
Zhu Yib481de92007-09-25 17:54:57 -07001084
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001085 /* TODO we might want recalculate
1086 * rx_chain in rxon cmd */
1087
1088 /* Mark so we run this algo only once! */
1089 data->state = IWL_CHAIN_NOISE_CALIBRATED;
Zhu Yib481de92007-09-25 17:54:57 -07001090 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001091 data->chain_noise_a = 0;
1092 data->chain_noise_b = 0;
1093 data->chain_noise_c = 0;
1094 data->chain_signal_a = 0;
1095 data->chain_signal_b = 0;
1096 data->chain_signal_c = 0;
1097 data->beacon_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001098}
1099
1100static void iwl4965_bg_sensitivity_work(struct work_struct *work)
1101{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001102 struct iwl_priv *priv = container_of(work, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07001103 sensitivity_work);
1104
1105 mutex_lock(&priv->mutex);
1106
1107 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1108 test_bit(STATUS_SCANNING, &priv->status)) {
1109 mutex_unlock(&priv->mutex);
1110 return;
1111 }
1112
1113 if (priv->start_calib) {
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001114 iwl_chain_noise_calibration(priv, &priv->statistics);
Zhu Yib481de92007-09-25 17:54:57 -07001115
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001116 iwl_sensitivity_calibration(priv, &priv->statistics);
Zhu Yib481de92007-09-25 17:54:57 -07001117 }
1118
1119 mutex_unlock(&priv->mutex);
1120 return;
1121}
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001122#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
Zhu Yib481de92007-09-25 17:54:57 -07001123
1124static void iwl4965_bg_txpower_work(struct work_struct *work)
1125{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001126 struct iwl_priv *priv = container_of(work, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07001127 txpower_work);
1128
1129 /* If a scan happened to start before we got here
1130 * then just return; the statistics notification will
1131 * kick off another scheduled work to compensate for
1132 * any temperature delta we missed here. */
1133 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1134 test_bit(STATUS_SCANNING, &priv->status))
1135 return;
1136
1137 mutex_lock(&priv->mutex);
1138
1139 /* Regardless of if we are assocaited, we must reconfigure the
1140 * TX power since frames can be sent on non-radar channels while
1141 * not associated */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001142 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001143
1144 /* Update last_temperature to keep is_calib_needed from running
1145 * when it isn't needed... */
1146 priv->last_temperature = priv->temperature;
1147
1148 mutex_unlock(&priv->mutex);
1149}
1150
1151/*
1152 * Acquire priv->lock before calling this function !
1153 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001154static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
Zhu Yib481de92007-09-25 17:54:57 -07001155{
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001156 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Zhu Yib481de92007-09-25 17:54:57 -07001157 (index & 0xff) | (txq_id << 8));
Tomas Winkler12a81f62008-04-03 16:05:20 -07001158 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
Zhu Yib481de92007-09-25 17:54:57 -07001159}
1160
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001161/**
1162 * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
1163 * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
1164 * @scd_retry: (1) Indicates queue will be used in aggregation mode
1165 *
1166 * NOTE: Acquire priv->lock before calling this function !
Zhu Yib481de92007-09-25 17:54:57 -07001167 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001168static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001169 struct iwl4965_tx_queue *txq,
Zhu Yib481de92007-09-25 17:54:57 -07001170 int tx_fifo_id, int scd_retry)
1171{
1172 int txq_id = txq->q.id;
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001173
1174 /* Find out whether to activate Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07001175 int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
1176
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001177 /* Set up and activate */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001178 iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
Zhu Yib481de92007-09-25 17:54:57 -07001179 (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1180 (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
1181 (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
1182 (scd_retry << SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
1183 SCD_QUEUE_STTS_REG_MSK);
1184
1185 txq->sched_retry = scd_retry;
1186
1187 IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001188 active ? "Activate" : "Deactivate",
Zhu Yib481de92007-09-25 17:54:57 -07001189 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
1190}
1191
1192static const u16 default_queue_to_tx_fifo[] = {
1193 IWL_TX_FIFO_AC3,
1194 IWL_TX_FIFO_AC2,
1195 IWL_TX_FIFO_AC1,
1196 IWL_TX_FIFO_AC0,
1197 IWL_CMD_FIFO_NUM,
1198 IWL_TX_FIFO_HCCA_1,
1199 IWL_TX_FIFO_HCCA_2
1200};
1201
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001202static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
Zhu Yib481de92007-09-25 17:54:57 -07001203{
1204 set_bit(txq_id, &priv->txq_ctx_active_msk);
1205}
1206
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001207static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
Zhu Yib481de92007-09-25 17:54:57 -07001208{
1209 clear_bit(txq_id, &priv->txq_ctx_active_msk);
1210}
1211
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001212int iwl4965_alive_notify(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001213{
1214 u32 a;
1215 int i = 0;
1216 unsigned long flags;
Tomas Winkler857485c2008-03-21 13:53:44 -07001217 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001218
1219 spin_lock_irqsave(&priv->lock, flags);
1220
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001221#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07001222 memset(&(priv->sensitivity_data), 0,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001223 sizeof(struct iwl_sensitivity_data));
Zhu Yib481de92007-09-25 17:54:57 -07001224 memset(&(priv->chain_noise_data), 0,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001225 sizeof(struct iwl_chain_noise_data));
Zhu Yib481de92007-09-25 17:54:57 -07001226 for (i = 0; i < NUM_RX_CHAINS; i++)
1227 priv->chain_noise_data.delta_gain_code[i] =
1228 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001229#endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001230 ret = iwl_grab_nic_access(priv);
Tomas Winkler857485c2008-03-21 13:53:44 -07001231 if (ret) {
Zhu Yib481de92007-09-25 17:54:57 -07001232 spin_unlock_irqrestore(&priv->lock, flags);
Tomas Winkler857485c2008-03-21 13:53:44 -07001233 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001234 }
1235
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001236 /* Clear 4965's internal Tx Scheduler data base */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001237 priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
Zhu Yib481de92007-09-25 17:54:57 -07001238 a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
1239 for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001240 iwl_write_targ_mem(priv, a, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001241 for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001242 iwl_write_targ_mem(priv, a, 0);
Tomas Winkler5425e492008-04-15 16:01:38 -07001243 for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001244 iwl_write_targ_mem(priv, a, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001245
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001246 /* Tel 4965 where to find Tx byte count tables */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001247 iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
Tomas Winkler059ff822008-04-14 21:16:14 -07001248 (priv->shared_phys +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001249 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001250
1251 /* Disable chain mode for all queues */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001252 iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001253
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001254 /* Initialize each Tx queue (including the command queue) */
Tomas Winkler5425e492008-04-15 16:01:38 -07001255 for (i = 0; i < priv->hw_params.max_txq_num; i++) {
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001256
1257 /* TFD circular buffer read/write indexes */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001258 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001259 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001260
1261 /* Max Tx Window size for Scheduler-ACK mode */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001262 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07001263 SCD_CONTEXT_QUEUE_OFFSET(i),
1264 (SCD_WIN_SIZE <<
1265 SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
1266 SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001267
1268 /* Frame limit */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001269 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07001270 SCD_CONTEXT_QUEUE_OFFSET(i) +
1271 sizeof(u32),
1272 (SCD_FRAME_LIMIT <<
1273 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1274 SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
1275
1276 }
Tomas Winkler12a81f62008-04-03 16:05:20 -07001277 iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
Tomas Winkler5425e492008-04-15 16:01:38 -07001278 (1 << priv->hw_params.max_txq_num) - 1);
Zhu Yib481de92007-09-25 17:54:57 -07001279
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001280 /* Activate all Tx DMA/FIFO channels */
Tomas Winkler12a81f62008-04-03 16:05:20 -07001281 iwl_write_prph(priv, IWL49_SCD_TXFACT,
Zhu Yib481de92007-09-25 17:54:57 -07001282 SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
1283
1284 iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001285
1286 /* Map each Tx/cmd queue to its corresponding fifo */
Zhu Yib481de92007-09-25 17:54:57 -07001287 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
1288 int ac = default_queue_to_tx_fifo[i];
1289 iwl4965_txq_ctx_activate(priv, i);
1290 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
1291 }
1292
Tomas Winkler3395f6e2008-03-25 16:33:37 -07001293 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001294 spin_unlock_irqrestore(&priv->lock, flags);
1295
Emmanuel Grumbach49ea8592008-04-15 16:01:37 -07001296 /* Ask for statistics now, the uCode will send statistics notification
1297 * periodically after association */
1298 iwl_send_statistics_request(priv, CMD_ASYNC);
Tomas Winkler857485c2008-03-21 13:53:44 -07001299 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001300}
1301
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001302#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1303static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
1304 .min_nrg_cck = 97,
1305 .max_nrg_cck = 0,
1306
1307 .auto_corr_min_ofdm = 85,
1308 .auto_corr_min_ofdm_mrc = 170,
1309 .auto_corr_min_ofdm_x1 = 105,
1310 .auto_corr_min_ofdm_mrc_x1 = 220,
1311
1312 .auto_corr_max_ofdm = 120,
1313 .auto_corr_max_ofdm_mrc = 210,
1314 .auto_corr_max_ofdm_x1 = 140,
1315 .auto_corr_max_ofdm_mrc_x1 = 270,
1316
1317 .auto_corr_min_cck = 125,
1318 .auto_corr_max_cck = 200,
1319 .auto_corr_min_cck_mrc = 200,
1320 .auto_corr_max_cck_mrc = 400,
1321
1322 .nrg_th_cck = 100,
1323 .nrg_th_ofdm = 100,
1324};
1325#endif
1326
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001327/**
Tomas Winkler5425e492008-04-15 16:01:38 -07001328 * iwl4965_hw_set_hw_params
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001329 *
1330 * Called when initializing driver
1331 */
Tomas Winkler5425e492008-04-15 16:01:38 -07001332int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001333{
Assaf Krauss316c30d2008-03-14 10:38:46 -07001334
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07001335 if ((priv->cfg->mod_params->num_of_queues > IWL4965_MAX_NUM_QUEUES) ||
Assaf Krauss1ea87392008-03-18 14:57:50 -07001336 (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
Assaf Krauss316c30d2008-03-14 10:38:46 -07001337 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07001338 IWL_MIN_NUM_QUEUES, IWL4965_MAX_NUM_QUEUES);
Tomas Winkler059ff822008-04-14 21:16:14 -07001339 return -EINVAL;
Assaf Krauss316c30d2008-03-14 10:38:46 -07001340 }
1341
Tomas Winkler5425e492008-04-15 16:01:38 -07001342 priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
1343 priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
1344 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1345 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
Assaf Krauss1ea87392008-03-18 14:57:50 -07001346 if (priv->cfg->mod_params->amsdu_size_8K)
Tomas Winkler5425e492008-04-15 16:01:38 -07001347 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02001348 else
Tomas Winkler5425e492008-04-15 16:01:38 -07001349 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
1350 priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
1351 priv->hw_params.max_stations = IWL4965_STATION_COUNT;
1352 priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
Tomas Winkler3e82a822008-02-13 11:32:31 -08001353
Tomas Winklerec35cf22008-04-15 16:01:39 -07001354 priv->hw_params.tx_chains_num = 2;
1355 priv->hw_params.rx_chains_num = 2;
1356 priv->hw_params.valid_tx_ant = (IWL_ANTENNA_MAIN | IWL_ANTENNA_AUX);
1357 priv->hw_params.valid_rx_ant = (IWL_ANTENNA_MAIN | IWL_ANTENNA_AUX);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07001358#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1359 priv->hw_params.sens = &iwl4965_sensitivity;
1360#endif
Tomas Winkler3e82a822008-02-13 11:32:31 -08001361
Tomas Winkler059ff822008-04-14 21:16:14 -07001362 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001363}
1364
1365/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001366 * iwl4965_hw_txq_ctx_free - Free TXQ Context
Zhu Yib481de92007-09-25 17:54:57 -07001367 *
1368 * Destroy all TX DMA queues and structures
1369 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001370void iwl4965_hw_txq_ctx_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001371{
1372 int txq_id;
1373
1374 /* Tx queues */
Tomas Winkler5425e492008-04-15 16:01:38 -07001375 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001376 iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
Zhu Yib481de92007-09-25 17:54:57 -07001377
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001378 /* Keep-warm buffer */
Zhu Yib481de92007-09-25 17:54:57 -07001379 iwl4965_kw_free(priv);
1380}
1381
1382/**
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001383 * iwl4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Zhu Yib481de92007-09-25 17:54:57 -07001384 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001385 * Does NOT advance any TFD circular buffer read/write indexes
1386 * Does NOT free the TFD itself (which is within circular buffer)
Zhu Yib481de92007-09-25 17:54:57 -07001387 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001388int iwl4965_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07001389{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001390 struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
1391 struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07001392 struct pci_dev *dev = priv->pci_dev;
1393 int i;
1394 int counter = 0;
1395 int index, is_odd;
1396
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001397 /* Host command buffers stay mapped in memory, nothing to clean */
Zhu Yib481de92007-09-25 17:54:57 -07001398 if (txq->q.id == IWL_CMD_QUEUE_NUM)
Zhu Yib481de92007-09-25 17:54:57 -07001399 return 0;
1400
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001401 /* Sanity check on number of chunks */
Zhu Yib481de92007-09-25 17:54:57 -07001402 counter = IWL_GET_BITS(*bd, num_tbs);
1403 if (counter > MAX_NUM_OF_TBS) {
1404 IWL_ERROR("Too many chunks: %i\n", counter);
1405 /* @todo issue fatal error, it is quite serious situation */
1406 return 0;
1407 }
1408
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001409 /* Unmap chunks, if any.
1410 * TFD info for odd chunks is different format than for even chunks. */
Zhu Yib481de92007-09-25 17:54:57 -07001411 for (i = 0; i < counter; i++) {
1412 index = i / 2;
1413 is_odd = i & 0x1;
1414
1415 if (is_odd)
1416 pci_unmap_single(
1417 dev,
1418 IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
1419 (IWL_GET_BITS(bd->pa[index],
1420 tb2_addr_hi20) << 16),
1421 IWL_GET_BITS(bd->pa[index], tb2_len),
1422 PCI_DMA_TODEVICE);
1423
1424 else if (i > 0)
1425 pci_unmap_single(dev,
1426 le32_to_cpu(bd->pa[index].tb1_addr),
1427 IWL_GET_BITS(bd->pa[index], tb1_len),
1428 PCI_DMA_TODEVICE);
1429
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001430 /* Free SKB, if any, for this chunk */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001431 if (txq->txb[txq->q.read_ptr].skb[i]) {
1432 struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
Zhu Yib481de92007-09-25 17:54:57 -07001433
1434 dev_kfree_skb(skb);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08001435 txq->txb[txq->q.read_ptr].skb[i] = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001436 }
1437 }
1438 return 0;
1439}
1440
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001441/* set card power command */
1442static int iwl4965_set_power(struct iwl_priv *priv,
1443 void *cmd)
1444{
1445 int ret = 0;
1446
1447 ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1448 sizeof(struct iwl4965_powertable_cmd),
1449 cmd, NULL);
1450 return ret;
1451}
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001452int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
Zhu Yib481de92007-09-25 17:54:57 -07001453{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001454 IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001455 return -EINVAL;
1456}
1457
1458static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1459{
1460 s32 sign = 1;
1461
1462 if (num < 0) {
1463 sign = -sign;
1464 num = -num;
1465 }
1466 if (denom < 0) {
1467 sign = -sign;
1468 denom = -denom;
1469 }
1470 *res = 1;
1471 *res = ((num * 2 + denom) / (denom * 2)) * sign;
1472
1473 return 1;
1474}
1475
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001476/**
1477 * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1478 *
1479 * Determines power supply voltage compensation for txpower calculations.
1480 * Returns number of 1/2-dB steps to subtract from gain table index,
1481 * to compensate for difference between power supply voltage during
1482 * factory measurements, vs. current power supply voltage.
1483 *
1484 * Voltage indication is higher for lower voltage.
1485 * Lower voltage requires more gain (lower gain table index).
1486 */
Zhu Yib481de92007-09-25 17:54:57 -07001487static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1488 s32 current_voltage)
1489{
1490 s32 comp = 0;
1491
1492 if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1493 (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1494 return 0;
1495
1496 iwl4965_math_div_round(current_voltage - eeprom_voltage,
1497 TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1498
1499 if (current_voltage > eeprom_voltage)
1500 comp *= 2;
1501 if ((comp < -2) || (comp > 2))
1502 comp = 0;
1503
1504 return comp;
1505}
1506
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001507static const struct iwl_channel_info *
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001508iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001509 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07001510{
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001511 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001512
Assaf Krauss8622e702008-03-21 13:53:43 -07001513 ch_info = iwl_get_channel_info(priv, band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07001514
1515 if (!is_channel_valid(ch_info))
1516 return NULL;
1517
1518 return ch_info;
1519}
1520
1521static s32 iwl4965_get_tx_atten_grp(u16 channel)
1522{
1523 if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1524 channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1525 return CALIB_CH_GROUP_5;
1526
1527 if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1528 channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1529 return CALIB_CH_GROUP_1;
1530
1531 if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1532 channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1533 return CALIB_CH_GROUP_2;
1534
1535 if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1536 channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1537 return CALIB_CH_GROUP_3;
1538
1539 if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1540 channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1541 return CALIB_CH_GROUP_4;
1542
1543 IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1544 return -1;
1545}
1546
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001547static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
Zhu Yib481de92007-09-25 17:54:57 -07001548{
1549 s32 b = -1;
1550
1551 for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
Tomas Winkler073d3f52008-04-21 15:41:52 -07001552 if (priv->calib_info->band_info[b].ch_from == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001553 continue;
1554
Tomas Winkler073d3f52008-04-21 15:41:52 -07001555 if ((channel >= priv->calib_info->band_info[b].ch_from)
1556 && (channel <= priv->calib_info->band_info[b].ch_to))
Zhu Yib481de92007-09-25 17:54:57 -07001557 break;
1558 }
1559
1560 return b;
1561}
1562
1563static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1564{
1565 s32 val;
1566
1567 if (x2 == x1)
1568 return y1;
1569 else {
1570 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1571 return val + y2;
1572 }
1573}
1574
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08001575/**
1576 * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1577 *
1578 * Interpolates factory measurements from the two sample channels within a
1579 * sub-band, to apply to channel of interest. Interpolation is proportional to
1580 * differences in channel frequencies, which is proportional to differences
1581 * in channel number.
1582 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001583static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
Tomas Winkler073d3f52008-04-21 15:41:52 -07001584 struct iwl_eeprom_calib_ch_info *chan_info)
Zhu Yib481de92007-09-25 17:54:57 -07001585{
1586 s32 s = -1;
1587 u32 c;
1588 u32 m;
Tomas Winkler073d3f52008-04-21 15:41:52 -07001589 const struct iwl_eeprom_calib_measure *m1;
1590 const struct iwl_eeprom_calib_measure *m2;
1591 struct iwl_eeprom_calib_measure *omeas;
Zhu Yib481de92007-09-25 17:54:57 -07001592 u32 ch_i1;
1593 u32 ch_i2;
1594
1595 s = iwl4965_get_sub_band(priv, channel);
1596 if (s >= EEPROM_TX_POWER_BANDS) {
1597 IWL_ERROR("Tx Power can not find channel %d ", channel);
1598 return -1;
1599 }
1600
Tomas Winkler073d3f52008-04-21 15:41:52 -07001601 ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1602 ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
Zhu Yib481de92007-09-25 17:54:57 -07001603 chan_info->ch_num = (u8) channel;
1604
1605 IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1606 channel, s, ch_i1, ch_i2);
1607
1608 for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1609 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
Tomas Winkler073d3f52008-04-21 15:41:52 -07001610 m1 = &(priv->calib_info->band_info[s].ch1.
Zhu Yib481de92007-09-25 17:54:57 -07001611 measurements[c][m]);
Tomas Winkler073d3f52008-04-21 15:41:52 -07001612 m2 = &(priv->calib_info->band_info[s].ch2.
Zhu Yib481de92007-09-25 17:54:57 -07001613 measurements[c][m]);
1614 omeas = &(chan_info->measurements[c][m]);
1615
1616 omeas->actual_pow =
1617 (u8) iwl4965_interpolate_value(channel, ch_i1,
1618 m1->actual_pow,
1619 ch_i2,
1620 m2->actual_pow);
1621 omeas->gain_idx =
1622 (u8) iwl4965_interpolate_value(channel, ch_i1,
1623 m1->gain_idx, ch_i2,
1624 m2->gain_idx);
1625 omeas->temperature =
1626 (u8) iwl4965_interpolate_value(channel, ch_i1,
1627 m1->temperature,
1628 ch_i2,
1629 m2->temperature);
1630 omeas->pa_det =
1631 (s8) iwl4965_interpolate_value(channel, ch_i1,
1632 m1->pa_det, ch_i2,
1633 m2->pa_det);
1634
1635 IWL_DEBUG_TXPOWER
1636 ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1637 m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1638 IWL_DEBUG_TXPOWER
1639 ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1640 m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1641 IWL_DEBUG_TXPOWER
1642 ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1643 m1->pa_det, m2->pa_det, omeas->pa_det);
1644 IWL_DEBUG_TXPOWER
1645 ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
1646 m1->temperature, m2->temperature,
1647 omeas->temperature);
1648 }
1649 }
1650
1651 return 0;
1652}
1653
1654/* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1655 * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1656static s32 back_off_table[] = {
1657 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1658 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1659 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1660 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1661 10 /* CCK */
1662};
1663
1664/* Thermal compensation values for txpower for various frequency ranges ...
1665 * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001666static struct iwl4965_txpower_comp_entry {
Zhu Yib481de92007-09-25 17:54:57 -07001667 s32 degrees_per_05db_a;
1668 s32 degrees_per_05db_a_denom;
1669} tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1670 {9, 2}, /* group 0 5.2, ch 34-43 */
1671 {4, 1}, /* group 1 5.2, ch 44-70 */
1672 {4, 1}, /* group 2 5.2, ch 71-124 */
1673 {4, 1}, /* group 3 5.2, ch 125-200 */
1674 {3, 1} /* group 4 2.4, ch all */
1675};
1676
1677static s32 get_min_power_index(s32 rate_power_index, u32 band)
1678{
1679 if (!band) {
1680 if ((rate_power_index & 7) <= 4)
1681 return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1682 }
1683 return MIN_TX_GAIN_INDEX;
1684}
1685
1686struct gain_entry {
1687 u8 dsp;
1688 u8 radio;
1689};
1690
1691static const struct gain_entry gain_table[2][108] = {
1692 /* 5.2GHz power gain index table */
1693 {
1694 {123, 0x3F}, /* highest txpower */
1695 {117, 0x3F},
1696 {110, 0x3F},
1697 {104, 0x3F},
1698 {98, 0x3F},
1699 {110, 0x3E},
1700 {104, 0x3E},
1701 {98, 0x3E},
1702 {110, 0x3D},
1703 {104, 0x3D},
1704 {98, 0x3D},
1705 {110, 0x3C},
1706 {104, 0x3C},
1707 {98, 0x3C},
1708 {110, 0x3B},
1709 {104, 0x3B},
1710 {98, 0x3B},
1711 {110, 0x3A},
1712 {104, 0x3A},
1713 {98, 0x3A},
1714 {110, 0x39},
1715 {104, 0x39},
1716 {98, 0x39},
1717 {110, 0x38},
1718 {104, 0x38},
1719 {98, 0x38},
1720 {110, 0x37},
1721 {104, 0x37},
1722 {98, 0x37},
1723 {110, 0x36},
1724 {104, 0x36},
1725 {98, 0x36},
1726 {110, 0x35},
1727 {104, 0x35},
1728 {98, 0x35},
1729 {110, 0x34},
1730 {104, 0x34},
1731 {98, 0x34},
1732 {110, 0x33},
1733 {104, 0x33},
1734 {98, 0x33},
1735 {110, 0x32},
1736 {104, 0x32},
1737 {98, 0x32},
1738 {110, 0x31},
1739 {104, 0x31},
1740 {98, 0x31},
1741 {110, 0x30},
1742 {104, 0x30},
1743 {98, 0x30},
1744 {110, 0x25},
1745 {104, 0x25},
1746 {98, 0x25},
1747 {110, 0x24},
1748 {104, 0x24},
1749 {98, 0x24},
1750 {110, 0x23},
1751 {104, 0x23},
1752 {98, 0x23},
1753 {110, 0x22},
1754 {104, 0x18},
1755 {98, 0x18},
1756 {110, 0x17},
1757 {104, 0x17},
1758 {98, 0x17},
1759 {110, 0x16},
1760 {104, 0x16},
1761 {98, 0x16},
1762 {110, 0x15},
1763 {104, 0x15},
1764 {98, 0x15},
1765 {110, 0x14},
1766 {104, 0x14},
1767 {98, 0x14},
1768 {110, 0x13},
1769 {104, 0x13},
1770 {98, 0x13},
1771 {110, 0x12},
1772 {104, 0x08},
1773 {98, 0x08},
1774 {110, 0x07},
1775 {104, 0x07},
1776 {98, 0x07},
1777 {110, 0x06},
1778 {104, 0x06},
1779 {98, 0x06},
1780 {110, 0x05},
1781 {104, 0x05},
1782 {98, 0x05},
1783 {110, 0x04},
1784 {104, 0x04},
1785 {98, 0x04},
1786 {110, 0x03},
1787 {104, 0x03},
1788 {98, 0x03},
1789 {110, 0x02},
1790 {104, 0x02},
1791 {98, 0x02},
1792 {110, 0x01},
1793 {104, 0x01},
1794 {98, 0x01},
1795 {110, 0x00},
1796 {104, 0x00},
1797 {98, 0x00},
1798 {93, 0x00},
1799 {88, 0x00},
1800 {83, 0x00},
1801 {78, 0x00},
1802 },
1803 /* 2.4GHz power gain index table */
1804 {
1805 {110, 0x3f}, /* highest txpower */
1806 {104, 0x3f},
1807 {98, 0x3f},
1808 {110, 0x3e},
1809 {104, 0x3e},
1810 {98, 0x3e},
1811 {110, 0x3d},
1812 {104, 0x3d},
1813 {98, 0x3d},
1814 {110, 0x3c},
1815 {104, 0x3c},
1816 {98, 0x3c},
1817 {110, 0x3b},
1818 {104, 0x3b},
1819 {98, 0x3b},
1820 {110, 0x3a},
1821 {104, 0x3a},
1822 {98, 0x3a},
1823 {110, 0x39},
1824 {104, 0x39},
1825 {98, 0x39},
1826 {110, 0x38},
1827 {104, 0x38},
1828 {98, 0x38},
1829 {110, 0x37},
1830 {104, 0x37},
1831 {98, 0x37},
1832 {110, 0x36},
1833 {104, 0x36},
1834 {98, 0x36},
1835 {110, 0x35},
1836 {104, 0x35},
1837 {98, 0x35},
1838 {110, 0x34},
1839 {104, 0x34},
1840 {98, 0x34},
1841 {110, 0x33},
1842 {104, 0x33},
1843 {98, 0x33},
1844 {110, 0x32},
1845 {104, 0x32},
1846 {98, 0x32},
1847 {110, 0x31},
1848 {104, 0x31},
1849 {98, 0x31},
1850 {110, 0x30},
1851 {104, 0x30},
1852 {98, 0x30},
1853 {110, 0x6},
1854 {104, 0x6},
1855 {98, 0x6},
1856 {110, 0x5},
1857 {104, 0x5},
1858 {98, 0x5},
1859 {110, 0x4},
1860 {104, 0x4},
1861 {98, 0x4},
1862 {110, 0x3},
1863 {104, 0x3},
1864 {98, 0x3},
1865 {110, 0x2},
1866 {104, 0x2},
1867 {98, 0x2},
1868 {110, 0x1},
1869 {104, 0x1},
1870 {98, 0x1},
1871 {110, 0x0},
1872 {104, 0x0},
1873 {98, 0x0},
1874 {97, 0},
1875 {96, 0},
1876 {95, 0},
1877 {94, 0},
1878 {93, 0},
1879 {92, 0},
1880 {91, 0},
1881 {90, 0},
1882 {89, 0},
1883 {88, 0},
1884 {87, 0},
1885 {86, 0},
1886 {85, 0},
1887 {84, 0},
1888 {83, 0},
1889 {82, 0},
1890 {81, 0},
1891 {80, 0},
1892 {79, 0},
1893 {78, 0},
1894 {77, 0},
1895 {76, 0},
1896 {75, 0},
1897 {74, 0},
1898 {73, 0},
1899 {72, 0},
1900 {71, 0},
1901 {70, 0},
1902 {69, 0},
1903 {68, 0},
1904 {67, 0},
1905 {66, 0},
1906 {65, 0},
1907 {64, 0},
1908 {63, 0},
1909 {62, 0},
1910 {61, 0},
1911 {60, 0},
1912 {59, 0},
1913 }
1914};
1915
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001916static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
Zhu Yib481de92007-09-25 17:54:57 -07001917 u8 is_fat, u8 ctrl_chan_high,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001918 struct iwl4965_tx_power_db *tx_power_tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001919{
1920 u8 saturation_power;
1921 s32 target_power;
1922 s32 user_target_power;
1923 s32 power_limit;
1924 s32 current_temp;
1925 s32 reg_limit;
1926 s32 current_regulatory;
1927 s32 txatten_grp = CALIB_CH_GROUP_MAX;
1928 int i;
1929 int c;
Assaf Kraussbf85ea42008-03-14 10:38:49 -07001930 const struct iwl_channel_info *ch_info = NULL;
Tomas Winkler073d3f52008-04-21 15:41:52 -07001931 struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1932 const struct iwl_eeprom_calib_measure *measurement;
Zhu Yib481de92007-09-25 17:54:57 -07001933 s16 voltage;
1934 s32 init_voltage;
1935 s32 voltage_compensation;
1936 s32 degrees_per_05db_num;
1937 s32 degrees_per_05db_denom;
1938 s32 factory_temp;
1939 s32 temperature_comp[2];
1940 s32 factory_gain_index[2];
1941 s32 factory_actual_pwr[2];
1942 s32 power_index;
1943
1944 /* Sanity check requested level (dBm) */
1945 if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1946 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1947 priv->user_txpower_limit);
1948 return -EINVAL;
1949 }
1950 if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1951 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1952 priv->user_txpower_limit);
1953 return -EINVAL;
1954 }
1955
1956 /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1957 * are used for indexing into txpower table) */
1958 user_target_power = 2 * priv->user_txpower_limit;
1959
1960 /* Get current (RXON) channel, band, width */
1961 ch_info =
Johannes Berg8318d782008-01-24 19:38:38 +01001962 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07001963
1964 IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1965 is_fat);
1966
1967 if (!ch_info)
1968 return -EINVAL;
1969
1970 /* get txatten group, used to select 1) thermal txpower adjustment
1971 * and 2) mimo txpower balance between Tx chains. */
1972 txatten_grp = iwl4965_get_tx_atten_grp(channel);
1973 if (txatten_grp < 0)
1974 return -EINVAL;
1975
1976 IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1977 channel, txatten_grp);
1978
1979 if (is_fat) {
1980 if (ctrl_chan_high)
1981 channel -= 2;
1982 else
1983 channel += 2;
1984 }
1985
1986 /* hardware txpower limits ...
1987 * saturation (clipping distortion) txpowers are in half-dBm */
1988 if (band)
Tomas Winkler073d3f52008-04-21 15:41:52 -07001989 saturation_power = priv->calib_info->saturation_power24;
Zhu Yib481de92007-09-25 17:54:57 -07001990 else
Tomas Winkler073d3f52008-04-21 15:41:52 -07001991 saturation_power = priv->calib_info->saturation_power52;
Zhu Yib481de92007-09-25 17:54:57 -07001992
1993 if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1994 saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1995 if (band)
1996 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1997 else
1998 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1999 }
2000
2001 /* regulatory txpower limits ... reg_limit values are in half-dBm,
2002 * max_power_avg values are in dBm, convert * 2 */
2003 if (is_fat)
2004 reg_limit = ch_info->fat_max_power_avg * 2;
2005 else
2006 reg_limit = ch_info->max_power_avg * 2;
2007
2008 if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
2009 (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
2010 if (band)
2011 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
2012 else
2013 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
2014 }
2015
2016 /* Interpolate txpower calibration values for this channel,
2017 * based on factory calibration tests on spaced channels. */
2018 iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
2019
2020 /* calculate tx gain adjustment based on power supply voltage */
Tomas Winkler073d3f52008-04-21 15:41:52 -07002021 voltage = priv->calib_info->voltage;
Zhu Yib481de92007-09-25 17:54:57 -07002022 init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
2023 voltage_compensation =
2024 iwl4965_get_voltage_compensation(voltage, init_voltage);
2025
2026 IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
2027 init_voltage,
2028 voltage, voltage_compensation);
2029
2030 /* get current temperature (Celsius) */
2031 current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
2032 current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
2033 current_temp = KELVIN_TO_CELSIUS(current_temp);
2034
2035 /* select thermal txpower adjustment params, based on channel group
2036 * (same frequency group used for mimo txatten adjustment) */
2037 degrees_per_05db_num =
2038 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
2039 degrees_per_05db_denom =
2040 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
2041
2042 /* get per-chain txpower values from factory measurements */
2043 for (c = 0; c < 2; c++) {
2044 measurement = &ch_eeprom_info.measurements[c][1];
2045
2046 /* txgain adjustment (in half-dB steps) based on difference
2047 * between factory and current temperature */
2048 factory_temp = measurement->temperature;
2049 iwl4965_math_div_round((current_temp - factory_temp) *
2050 degrees_per_05db_denom,
2051 degrees_per_05db_num,
2052 &temperature_comp[c]);
2053
2054 factory_gain_index[c] = measurement->gain_idx;
2055 factory_actual_pwr[c] = measurement->actual_pow;
2056
2057 IWL_DEBUG_TXPOWER("chain = %d\n", c);
2058 IWL_DEBUG_TXPOWER("fctry tmp %d, "
2059 "curr tmp %d, comp %d steps\n",
2060 factory_temp, current_temp,
2061 temperature_comp[c]);
2062
2063 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
2064 factory_gain_index[c],
2065 factory_actual_pwr[c]);
2066 }
2067
2068 /* for each of 33 bit-rates (including 1 for CCK) */
2069 for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
2070 u8 is_mimo_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002071 union iwl4965_tx_power_dual_stream tx_power;
Zhu Yib481de92007-09-25 17:54:57 -07002072
2073 /* for mimo, reduce each chain's txpower by half
2074 * (3dB, 6 steps), so total output power is regulatory
2075 * compliant. */
2076 if (i & 0x8) {
2077 current_regulatory = reg_limit -
2078 IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
2079 is_mimo_rate = 1;
2080 } else {
2081 current_regulatory = reg_limit;
2082 is_mimo_rate = 0;
2083 }
2084
2085 /* find txpower limit, either hardware or regulatory */
2086 power_limit = saturation_power - back_off_table[i];
2087 if (power_limit > current_regulatory)
2088 power_limit = current_regulatory;
2089
2090 /* reduce user's txpower request if necessary
2091 * for this rate on this channel */
2092 target_power = user_target_power;
2093 if (target_power > power_limit)
2094 target_power = power_limit;
2095
2096 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
2097 i, saturation_power - back_off_table[i],
2098 current_regulatory, user_target_power,
2099 target_power);
2100
2101 /* for each of 2 Tx chains (radio transmitters) */
2102 for (c = 0; c < 2; c++) {
2103 s32 atten_value;
2104
2105 if (is_mimo_rate)
2106 atten_value =
2107 (s32)le32_to_cpu(priv->card_alive_init.
2108 tx_atten[txatten_grp][c]);
2109 else
2110 atten_value = 0;
2111
2112 /* calculate index; higher index means lower txpower */
2113 power_index = (u8) (factory_gain_index[c] -
2114 (target_power -
2115 factory_actual_pwr[c]) -
2116 temperature_comp[c] -
2117 voltage_compensation +
2118 atten_value);
2119
2120/* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
2121 power_index); */
2122
2123 if (power_index < get_min_power_index(i, band))
2124 power_index = get_min_power_index(i, band);
2125
2126 /* adjust 5 GHz index to support negative indexes */
2127 if (!band)
2128 power_index += 9;
2129
2130 /* CCK, rate 32, reduce txpower for CCK */
2131 if (i == POWER_TABLE_CCK_ENTRY)
2132 power_index +=
2133 IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
2134
2135 /* stay within the table! */
2136 if (power_index > 107) {
2137 IWL_WARNING("txpower index %d > 107\n",
2138 power_index);
2139 power_index = 107;
2140 }
2141 if (power_index < 0) {
2142 IWL_WARNING("txpower index %d < 0\n",
2143 power_index);
2144 power_index = 0;
2145 }
2146
2147 /* fill txpower command for this rate/chain */
2148 tx_power.s.radio_tx_gain[c] =
2149 gain_table[band][power_index].radio;
2150 tx_power.s.dsp_predis_atten[c] =
2151 gain_table[band][power_index].dsp;
2152
2153 IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
2154 "gain 0x%02x dsp %d\n",
2155 c, atten_value, power_index,
2156 tx_power.s.radio_tx_gain[c],
2157 tx_power.s.dsp_predis_atten[c]);
2158 }/* for each chain */
2159
2160 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
2161
2162 }/* for each rate */
2163
2164 return 0;
2165}
2166
2167/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002168 * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
Zhu Yib481de92007-09-25 17:54:57 -07002169 *
2170 * Uses the active RXON for channel, band, and characteristics (fat, high)
2171 * The power limit is taken from priv->user_txpower_limit.
2172 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002173int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002174{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002175 struct iwl4965_txpowertable_cmd cmd = { 0 };
Tomas Winkler857485c2008-03-21 13:53:44 -07002176 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07002177 u8 band = 0;
2178 u8 is_fat = 0;
2179 u8 ctrl_chan_high = 0;
2180
2181 if (test_bit(STATUS_SCANNING, &priv->status)) {
2182 /* If this gets hit a lot, switch it to a BUG() and catch
2183 * the stack trace to find out who is calling this during
2184 * a scan. */
2185 IWL_WARNING("TX Power requested while scanning!\n");
2186 return -EAGAIN;
2187 }
2188
Johannes Berg8318d782008-01-24 19:38:38 +01002189 band = priv->band == IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002190
2191 is_fat = is_fat_channel(priv->active_rxon.flags);
2192
2193 if (is_fat &&
2194 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2195 ctrl_chan_high = 1;
2196
2197 cmd.band = band;
2198 cmd.channel = priv->active_rxon.channel;
2199
Tomas Winkler857485c2008-03-21 13:53:44 -07002200 ret = iwl4965_fill_txpower_tbl(priv, band,
Zhu Yib481de92007-09-25 17:54:57 -07002201 le16_to_cpu(priv->active_rxon.channel),
2202 is_fat, ctrl_chan_high, &cmd.tx_power);
Tomas Winkler857485c2008-03-21 13:53:44 -07002203 if (ret)
2204 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002205
Tomas Winkler857485c2008-03-21 13:53:44 -07002206 ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
2207
2208out:
2209 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07002210}
2211
Tomas Winkler7e8c5192008-04-15 16:01:43 -07002212static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
2213{
2214 int ret = 0;
2215 struct iwl4965_rxon_assoc_cmd rxon_assoc;
2216 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
2217 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
2218
2219 if ((rxon1->flags == rxon2->flags) &&
2220 (rxon1->filter_flags == rxon2->filter_flags) &&
2221 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
2222 (rxon1->ofdm_ht_single_stream_basic_rates ==
2223 rxon2->ofdm_ht_single_stream_basic_rates) &&
2224 (rxon1->ofdm_ht_dual_stream_basic_rates ==
2225 rxon2->ofdm_ht_dual_stream_basic_rates) &&
2226 (rxon1->rx_chain == rxon2->rx_chain) &&
2227 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
2228 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
2229 return 0;
2230 }
2231
2232 rxon_assoc.flags = priv->staging_rxon.flags;
2233 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
2234 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
2235 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
2236 rxon_assoc.reserved = 0;
2237 rxon_assoc.ofdm_ht_single_stream_basic_rates =
2238 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
2239 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
2240 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
2241 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
2242
2243 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
2244 sizeof(rxon_assoc), &rxon_assoc, NULL);
2245 if (ret)
2246 return ret;
2247
2248 return ret;
2249}
2250
2251
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002252int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07002253{
2254 int rc;
2255 u8 band = 0;
2256 u8 is_fat = 0;
2257 u8 ctrl_chan_high = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002258 struct iwl4965_channel_switch_cmd cmd = { 0 };
Assaf Kraussbf85ea42008-03-14 10:38:49 -07002259 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002260
Johannes Berg8318d782008-01-24 19:38:38 +01002261 band = priv->band == IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002262
Assaf Krauss8622e702008-03-21 13:53:43 -07002263 ch_info = iwl_get_channel_info(priv, priv->band, channel);
Zhu Yib481de92007-09-25 17:54:57 -07002264
2265 is_fat = is_fat_channel(priv->staging_rxon.flags);
2266
2267 if (is_fat &&
2268 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2269 ctrl_chan_high = 1;
2270
2271 cmd.band = band;
2272 cmd.expect_beacon = 0;
2273 cmd.channel = cpu_to_le16(channel);
2274 cmd.rxon_flags = priv->active_rxon.flags;
2275 cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
2276 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
2277 if (ch_info)
2278 cmd.expect_beacon = is_channel_radar(ch_info);
2279 else
2280 cmd.expect_beacon = 1;
2281
2282 rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
2283 ctrl_chan_high, &cmd.tx_power);
2284 if (rc) {
2285 IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
2286 return rc;
2287 }
2288
Tomas Winkler857485c2008-03-21 13:53:44 -07002289 rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002290 return rc;
2291}
2292
2293#define RTS_HCCA_RETRY_LIMIT 3
2294#define RTS_DFAULT_RETRY_LIMIT 60
2295
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002296void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
Tomas Winkler857485c2008-03-21 13:53:44 -07002297 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002298 struct ieee80211_tx_control *ctrl,
2299 struct ieee80211_hdr *hdr, int sta_id,
2300 int is_hcca)
2301{
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002302 struct iwl4965_tx_cmd *tx = &cmd->cmd.tx;
Zhu Yib481de92007-09-25 17:54:57 -07002303 u8 rts_retry_limit = 0;
2304 u8 data_retry_limit = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002305 u16 fc = le16_to_cpu(hdr->frame_control);
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002306 u8 rate_plcp;
2307 u16 rate_flags = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002308 int rate_idx = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
Zhu Yib481de92007-09-25 17:54:57 -07002309
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002310 rate_plcp = iwl4965_rates[rate_idx].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07002311
2312 rts_retry_limit = (is_hcca) ?
2313 RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
2314
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002315 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
2316 rate_flags |= RATE_MCS_CCK_MSK;
2317
2318
Zhu Yib481de92007-09-25 17:54:57 -07002319 if (ieee80211_is_probe_response(fc)) {
2320 data_retry_limit = 3;
2321 if (data_retry_limit < rts_retry_limit)
2322 rts_retry_limit = data_retry_limit;
2323 } else
2324 data_retry_limit = IWL_DEFAULT_TX_RETRY;
2325
2326 if (priv->data_retry_limit != -1)
2327 data_retry_limit = priv->data_retry_limit;
2328
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002329
2330 if (ieee80211_is_data(fc)) {
2331 tx->initial_rate_index = 0;
2332 tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
2333 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002334 switch (fc & IEEE80211_FCTL_STYPE) {
2335 case IEEE80211_STYPE_AUTH:
2336 case IEEE80211_STYPE_DEAUTH:
2337 case IEEE80211_STYPE_ASSOC_REQ:
2338 case IEEE80211_STYPE_REASSOC_REQ:
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002339 if (tx->tx_flags & TX_CMD_FLG_RTS_MSK) {
2340 tx->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2341 tx->tx_flags |= TX_CMD_FLG_CTS_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002342 }
2343 break;
2344 default:
2345 break;
2346 }
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002347
2348 /* Alternate between antenna A and B for successive frames */
2349 if (priv->use_ant_b_for_management_frame) {
2350 priv->use_ant_b_for_management_frame = 0;
2351 rate_flags |= RATE_MCS_ANT_B_MSK;
2352 } else {
2353 priv->use_ant_b_for_management_frame = 1;
2354 rate_flags |= RATE_MCS_ANT_A_MSK;
2355 }
Zhu Yib481de92007-09-25 17:54:57 -07002356 }
2357
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002358 tx->rts_retry_limit = rts_retry_limit;
2359 tx->data_retry_limit = data_retry_limit;
2360 tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002361}
2362
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002363int iwl4965_hw_get_rx_read(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002364{
Tomas Winkler059ff822008-04-14 21:16:14 -07002365 struct iwl4965_shared *s = priv->shared_virt;
2366 return le32_to_cpu(s->rb_closed) & 0xFFF;
Zhu Yib481de92007-09-25 17:54:57 -07002367}
2368
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002369int iwl4965_hw_get_temperature(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002370{
2371 return priv->temperature;
2372}
2373
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002374unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002375 struct iwl4965_frame *frame, u8 rate)
Zhu Yib481de92007-09-25 17:54:57 -07002376{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002377 struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002378 unsigned int frame_size;
2379
2380 tx_beacon_cmd = &frame->u.beacon;
2381 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
2382
Tomas Winkler5425e492008-04-15 16:01:38 -07002383 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002384 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2385
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002386 frame_size = iwl4965_fill_beacon_frame(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002387 tx_beacon_cmd->frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002388 iwl4965_broadcast_addr,
Zhu Yib481de92007-09-25 17:54:57 -07002389 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
2390
2391 BUG_ON(frame_size > MAX_MPDU_SIZE);
2392 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
2393
2394 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
2395 tx_beacon_cmd->tx.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002396 iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
Zhu Yib481de92007-09-25 17:54:57 -07002397 else
2398 tx_beacon_cmd->tx.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002399 iwl4965_hw_set_rate_n_flags(rate, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002400
2401 tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
2402 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
2403 return (sizeof(*tx_beacon_cmd) + frame_size);
2404}
2405
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002406/*
2407 * Tell 4965 where to find circular buffer of Tx Frame Descriptors for
2408 * given Tx queue, and enable the DMA channel used for that queue.
2409 *
2410 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
2411 * channels supported in hardware.
2412 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002413int iwl4965_hw_tx_queue_init(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07002414{
2415 int rc;
2416 unsigned long flags;
2417 int txq_id = txq->q.id;
2418
2419 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002420 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002421 if (rc) {
2422 spin_unlock_irqrestore(&priv->lock, flags);
2423 return rc;
2424 }
2425
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002426 /* Circular buffer (TFD queue in DRAM) physical base address */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002427 iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
Zhu Yib481de92007-09-25 17:54:57 -07002428 txq->q.dma_addr >> 8);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002429
2430 /* Enable DMA channel, using same id as for TFD queue */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002431 iwl_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07002432 priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
2433 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
2434 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07002435 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002436 spin_unlock_irqrestore(&priv->lock, flags);
2437
2438 return 0;
2439}
2440
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002441int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
Zhu Yib481de92007-09-25 17:54:57 -07002442 dma_addr_t addr, u16 len)
2443{
2444 int index, is_odd;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002445 struct iwl4965_tfd_frame *tfd = ptr;
Zhu Yib481de92007-09-25 17:54:57 -07002446 u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
2447
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002448 /* Each TFD can point to a maximum 20 Tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002449 if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
2450 IWL_ERROR("Error can not send more than %d chunks\n",
2451 MAX_NUM_OF_TBS);
2452 return -EINVAL;
2453 }
2454
2455 index = num_tbs / 2;
2456 is_odd = num_tbs & 0x1;
2457
2458 if (!is_odd) {
2459 tfd->pa[index].tb1_addr = cpu_to_le32(addr);
2460 IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
Tomas Winkler6a218f62008-01-14 17:46:15 -08002461 iwl_get_dma_hi_address(addr));
Zhu Yib481de92007-09-25 17:54:57 -07002462 IWL_SET_BITS(tfd->pa[index], tb1_len, len);
2463 } else {
2464 IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
2465 (u32) (addr & 0xffff));
2466 IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
2467 IWL_SET_BITS(tfd->pa[index], tb2_len, len);
2468 }
2469
2470 IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
2471
2472 return 0;
2473}
2474
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002475static void iwl4965_hw_card_show_info(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002476{
Tomas Winkler073d3f52008-04-21 15:41:52 -07002477 u16 hw_version = iwl_eeprom_query16(priv, EEPROM_4965_BOARD_REVISION);
Zhu Yib481de92007-09-25 17:54:57 -07002478
2479 IWL_DEBUG_INFO("4965ABGN HW Version %u.%u.%u\n",
2480 ((hw_version >> 8) & 0x0F),
2481 ((hw_version >> 8) >> 4), (hw_version & 0x00FF));
2482
2483 IWL_DEBUG_INFO("4965ABGN PBA Number %.16s\n",
Tomas Winkler073d3f52008-04-21 15:41:52 -07002484 &priv->eeprom[EEPROM_4965_BOARD_PBA]);
Zhu Yib481de92007-09-25 17:54:57 -07002485}
2486
2487#define IWL_TX_CRC_SIZE 4
2488#define IWL_TX_DELIMITER_SIZE 4
2489
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002490/**
Tomas Winklere2a722e2008-04-14 21:16:10 -07002491 * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002492 */
Tomas Winklere2a722e2008-04-14 21:16:10 -07002493static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
2494 struct iwl4965_tx_queue *txq,
2495 u16 byte_cnt)
Zhu Yib481de92007-09-25 17:54:57 -07002496{
2497 int len;
2498 int txq_id = txq->q.id;
Tomas Winkler059ff822008-04-14 21:16:14 -07002499 struct iwl4965_shared *shared_data = priv->shared_virt;
Zhu Yib481de92007-09-25 17:54:57 -07002500
Zhu Yib481de92007-09-25 17:54:57 -07002501 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2502
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002503 /* Set up byte count within first 256 entries */
Zhu Yib481de92007-09-25 17:54:57 -07002504 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002505 tfd_offset[txq->q.write_ptr], byte_cnt, len);
Zhu Yib481de92007-09-25 17:54:57 -07002506
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002507 /* If within first 64 entries, duplicate at end */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002508 if (txq->q.write_ptr < IWL4965_MAX_WIN_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07002509 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002510 tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
Zhu Yib481de92007-09-25 17:54:57 -07002511 byte_cnt, len);
Zhu Yib481de92007-09-25 17:54:57 -07002512}
2513
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002514/**
2515 * iwl4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
2516 *
2517 * Selects how many and which Rx receivers/antennas/chains to use.
2518 * This should not be used for scan command ... it puts data in wrong place.
2519 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002520void iwl4965_set_rxon_chain(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002521{
2522 u8 is_single = is_single_stream(priv);
2523 u8 idle_state, rx_state;
2524
2525 priv->staging_rxon.rx_chain = 0;
2526 rx_state = idle_state = 3;
2527
2528 /* Tell uCode which antennas are actually connected.
2529 * Before first association, we assume all antennas are connected.
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002530 * Just after first association, iwl_chain_noise_calibration()
Zhu Yib481de92007-09-25 17:54:57 -07002531 * checks which antennas actually *are* connected. */
2532 priv->staging_rxon.rx_chain |=
2533 cpu_to_le16(priv->valid_antenna << RXON_RX_CHAIN_VALID_POS);
2534
2535 /* How many receivers should we use? */
2536 iwl4965_get_rx_chain_counter(priv, &idle_state, &rx_state);
2537 priv->staging_rxon.rx_chain |=
2538 cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
2539 priv->staging_rxon.rx_chain |=
2540 cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
2541
2542 if (!is_single && (rx_state >= 2) &&
2543 !test_bit(STATUS_POWER_PMI, &priv->status))
2544 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
2545 else
2546 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
2547
2548 IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
2549}
2550
Zhu Yib481de92007-09-25 17:54:57 -07002551/**
2552 * sign_extend - Sign extend a value using specified bit as sign-bit
2553 *
2554 * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
2555 * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
2556 *
2557 * @param oper value to sign extend
2558 * @param index 0 based bit index (0<=index<32) to sign bit
2559 */
2560static s32 sign_extend(u32 oper, int index)
2561{
2562 u8 shift = 31 - index;
2563
2564 return (s32)(oper << shift) >> shift;
2565}
2566
2567/**
2568 * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
2569 * @statistics: Provides the temperature reading from the uCode
2570 *
2571 * A return of <0 indicates bogus data in the statistics
2572 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002573int iwl4965_get_temperature(const struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002574{
2575 s32 temperature;
2576 s32 vt;
2577 s32 R1, R2, R3;
2578 u32 R4;
2579
2580 if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
2581 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
2582 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
2583 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
2584 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
2585 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
2586 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
2587 } else {
2588 IWL_DEBUG_TEMP("Running temperature calibration\n");
2589 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
2590 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
2591 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2592 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2593 }
2594
2595 /*
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002596 * Temperature is only 23 bits, so sign extend out to 32.
Zhu Yib481de92007-09-25 17:54:57 -07002597 *
2598 * NOTE If we haven't received a statistics notification yet
2599 * with an updated temperature, use R4 provided to us in the
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08002600 * "initialize" ALIVE response.
2601 */
Zhu Yib481de92007-09-25 17:54:57 -07002602 if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2603 vt = sign_extend(R4, 23);
2604 else
2605 vt = sign_extend(
2606 le32_to_cpu(priv->statistics.general.temperature), 23);
2607
2608 IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2609 R1, R2, R3, vt);
2610
2611 if (R3 == R1) {
2612 IWL_ERROR("Calibration conflict R1 == R3\n");
2613 return -1;
2614 }
2615
2616 /* Calculate temperature in degrees Kelvin, adjust by 97%.
2617 * Add offset to center the adjustment around 0 degrees Centigrade. */
2618 temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2619 temperature /= (R3 - R1);
2620 temperature = (temperature * 97) / 100 +
2621 TEMPERATURE_CALIB_KELVIN_OFFSET;
2622
2623 IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2624 KELVIN_TO_CELSIUS(temperature));
2625
2626 return temperature;
2627}
2628
2629/* Adjust Txpower only if temperature variance is greater than threshold. */
2630#define IWL_TEMPERATURE_THRESHOLD 3
2631
2632/**
2633 * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2634 *
2635 * If the temperature changed has changed sufficiently, then a recalibration
2636 * is needed.
2637 *
2638 * Assumes caller will replace priv->last_temperature once calibration
2639 * executed.
2640 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002641static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002642{
2643 int temp_diff;
2644
2645 if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2646 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2647 return 0;
2648 }
2649
2650 temp_diff = priv->temperature - priv->last_temperature;
2651
2652 /* get absolute value */
2653 if (temp_diff < 0) {
2654 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2655 temp_diff = -temp_diff;
2656 } else if (temp_diff == 0)
2657 IWL_DEBUG_POWER("Same temp, \n");
2658 else
2659 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2660
2661 if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2662 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2663 return 0;
2664 }
2665
2666 IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2667
2668 return 1;
2669}
2670
2671/* Calculate noise level, based on measurements during network silence just
2672 * before arriving beacon. This measurement can be done only if we know
2673 * exactly when to expect beacons, therefore only when we're associated. */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002674static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002675{
2676 struct statistics_rx_non_phy *rx_info
2677 = &(priv->statistics.rx.general);
2678 int num_active_rx = 0;
2679 int total_silence = 0;
2680 int bcn_silence_a =
2681 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2682 int bcn_silence_b =
2683 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2684 int bcn_silence_c =
2685 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2686
2687 if (bcn_silence_a) {
2688 total_silence += bcn_silence_a;
2689 num_active_rx++;
2690 }
2691 if (bcn_silence_b) {
2692 total_silence += bcn_silence_b;
2693 num_active_rx++;
2694 }
2695 if (bcn_silence_c) {
2696 total_silence += bcn_silence_c;
2697 num_active_rx++;
2698 }
2699
2700 /* Average among active antennas */
2701 if (num_active_rx)
2702 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2703 else
2704 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2705
2706 IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2707 bcn_silence_a, bcn_silence_b, bcn_silence_c,
2708 priv->last_rx_noise);
2709}
2710
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002711void iwl4965_hw_rx_statistics(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002712{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002713 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002714 int change;
2715 s32 temp;
2716
2717 IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2718 (int)sizeof(priv->statistics), pkt->len);
2719
2720 change = ((priv->statistics.general.temperature !=
2721 pkt->u.stats.general.temperature) ||
2722 ((priv->statistics.flag &
2723 STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2724 (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2725
2726 memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2727
2728 set_bit(STATUS_STATISTICS, &priv->status);
2729
2730 /* Reschedule the statistics timer to occur in
2731 * REG_RECALIB_PERIOD seconds to ensure we get a
2732 * thermal update even if the uCode doesn't give
2733 * us one */
2734 mod_timer(&priv->statistics_periodic, jiffies +
2735 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2736
2737 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2738 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2739 iwl4965_rx_calc_noise(priv);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07002740#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07002741 queue_work(priv->workqueue, &priv->sensitivity_work);
2742#endif
2743 }
2744
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002745 iwl_leds_background(priv);
2746
Zhu Yib481de92007-09-25 17:54:57 -07002747 /* If the hardware hasn't reported a change in
2748 * temperature then don't bother computing a
2749 * calibrated temperature value */
2750 if (!change)
2751 return;
2752
2753 temp = iwl4965_get_temperature(priv);
2754 if (temp < 0)
2755 return;
2756
2757 if (priv->temperature != temp) {
2758 if (priv->temperature)
2759 IWL_DEBUG_TEMP("Temperature changed "
2760 "from %dC to %dC\n",
2761 KELVIN_TO_CELSIUS(priv->temperature),
2762 KELVIN_TO_CELSIUS(temp));
2763 else
2764 IWL_DEBUG_TEMP("Temperature "
2765 "initialized to %dC\n",
2766 KELVIN_TO_CELSIUS(temp));
2767 }
2768
2769 priv->temperature = temp;
2770 set_bit(STATUS_TEMPERATURE, &priv->status);
2771
2772 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2773 iwl4965_is_temp_calib_needed(priv))
2774 queue_work(priv->workqueue, &priv->txpower_work);
2775}
2776
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002777static void iwl4965_add_radiotap(struct iwl_priv *priv,
Zhu Yi12342c42007-12-20 11:27:32 +08002778 struct sk_buff *skb,
2779 struct iwl4965_rx_phy_res *rx_start,
2780 struct ieee80211_rx_status *stats,
2781 u32 ampdu_status)
2782{
2783 s8 signal = stats->ssi;
2784 s8 noise = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002785 int rate = stats->rate_idx;
Zhu Yi12342c42007-12-20 11:27:32 +08002786 u64 tsf = stats->mactime;
Johannes Berga0b484f2008-04-01 17:51:47 +02002787 __le16 antenna;
Zhu Yi12342c42007-12-20 11:27:32 +08002788 __le16 phy_flags_hw = rx_start->phy_flags;
2789 struct iwl4965_rt_rx_hdr {
2790 struct ieee80211_radiotap_header rt_hdr;
2791 __le64 rt_tsf; /* TSF */
2792 u8 rt_flags; /* radiotap packet flags */
2793 u8 rt_rate; /* rate in 500kb/s */
2794 __le16 rt_channelMHz; /* channel in MHz */
2795 __le16 rt_chbitmask; /* channel bitfield */
2796 s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
2797 s8 rt_dbmnoise;
2798 u8 rt_antenna; /* antenna number */
2799 } __attribute__ ((packed)) *iwl4965_rt;
2800
2801 /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2802 if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2803 if (net_ratelimit())
2804 printk(KERN_ERR "not enough headroom [%d] for "
Miguel Botón01c20982008-01-04 23:34:35 +01002805 "radiotap head [%zd]\n",
Zhu Yi12342c42007-12-20 11:27:32 +08002806 skb_headroom(skb), sizeof(*iwl4965_rt));
2807 return;
2808 }
2809
2810 /* put radiotap header in front of 802.11 header and data */
2811 iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2812
2813 /* initialise radiotap header */
2814 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2815 iwl4965_rt->rt_hdr.it_pad = 0;
2816
2817 /* total header + data */
2818 put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2819 &iwl4965_rt->rt_hdr.it_len);
2820
2821 /* Indicate all the fields we add to the radiotap header */
2822 put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2823 (1 << IEEE80211_RADIOTAP_FLAGS) |
2824 (1 << IEEE80211_RADIOTAP_RATE) |
2825 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2826 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2827 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2828 (1 << IEEE80211_RADIOTAP_ANTENNA)),
2829 &iwl4965_rt->rt_hdr.it_present);
2830
2831 /* Zero the flags, we'll add to them as we go */
2832 iwl4965_rt->rt_flags = 0;
2833
2834 put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2835
2836 iwl4965_rt->rt_dbmsignal = signal;
2837 iwl4965_rt->rt_dbmnoise = noise;
2838
2839 /* Convert the channel frequency and set the flags */
2840 put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2841 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2842 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2843 IEEE80211_CHAN_5GHZ),
2844 &iwl4965_rt->rt_chbitmask);
2845 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2846 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2847 IEEE80211_CHAN_2GHZ),
2848 &iwl4965_rt->rt_chbitmask);
2849 else /* 802.11g */
2850 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2851 IEEE80211_CHAN_2GHZ),
2852 &iwl4965_rt->rt_chbitmask);
2853
Zhu Yi12342c42007-12-20 11:27:32 +08002854 if (rate == -1)
2855 iwl4965_rt->rt_rate = 0;
2856 else
2857 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
2858
2859 /*
2860 * "antenna number"
2861 *
2862 * It seems that the antenna field in the phy flags value
2863 * is actually a bitfield. This is undefined by radiotap,
2864 * it wants an actual antenna number but I always get "7"
2865 * for most legacy frames I receive indicating that the
2866 * same frame was received on all three RX chains.
2867 *
2868 * I think this field should be removed in favour of a
2869 * new 802.11n radiotap field "RX chains" that is defined
2870 * as a bitmask.
2871 */
Johannes Berga0b484f2008-04-01 17:51:47 +02002872 antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2873 iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
Zhu Yi12342c42007-12-20 11:27:32 +08002874
2875 /* set the preamble flag if appropriate */
2876 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2877 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2878
2879 stats->flag |= RX_FLAG_RADIOTAP;
2880}
2881
Tomas Winkler19758be2008-03-12 16:58:51 -07002882static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2883{
2884 /* 0 - mgmt, 1 - cnt, 2 - data */
2885 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2886 priv->rx_stats[idx].cnt++;
2887 priv->rx_stats[idx].bytes += len;
2888}
2889
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07002890/*
2891 * returns non-zero if packet should be dropped
2892 */
2893static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2894 struct ieee80211_hdr *hdr,
2895 u32 decrypt_res,
2896 struct ieee80211_rx_status *stats)
2897{
2898 u16 fc = le16_to_cpu(hdr->frame_control);
2899
2900 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2901 return 0;
2902
2903 if (!(fc & IEEE80211_FCTL_PROTECTED))
2904 return 0;
2905
2906 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2907 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2908 case RX_RES_STATUS_SEC_TYPE_TKIP:
2909 /* The uCode has got a bad phase 1 Key, pushes the packet.
2910 * Decryption will be done in SW. */
2911 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2912 RX_RES_STATUS_BAD_KEY_TTAK)
2913 break;
2914
2915 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2916 RX_RES_STATUS_BAD_ICV_MIC) {
2917 /* bad ICV, the packet is destroyed since the
2918 * decryption is inplace, drop it */
2919 IWL_DEBUG_RX("Packet destroyed\n");
2920 return -1;
2921 }
2922 case RX_RES_STATUS_SEC_TYPE_WEP:
2923 case RX_RES_STATUS_SEC_TYPE_CCMP:
2924 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2925 RX_RES_STATUS_DECRYPT_OK) {
2926 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2927 stats->flag |= RX_FLAG_DECRYPTED;
2928 }
2929 break;
2930
2931 default:
2932 break;
2933 }
2934 return 0;
2935}
2936
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07002937static u32 iwl4965_translate_rx_status(u32 decrypt_in)
2938{
2939 u32 decrypt_out = 0;
2940
2941 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2942 RX_RES_STATUS_STATION_FOUND)
2943 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2944 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2945
2946 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2947
2948 /* packet was not encrypted */
2949 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2950 RX_RES_STATUS_SEC_TYPE_NONE)
2951 return decrypt_out;
2952
2953 /* packet was encrypted with unknown alg */
2954 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2955 RX_RES_STATUS_SEC_TYPE_ERR)
2956 return decrypt_out;
2957
2958 /* decryption was not done in HW */
2959 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2960 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2961 return decrypt_out;
2962
2963 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2964
2965 case RX_RES_STATUS_SEC_TYPE_CCMP:
2966 /* alg is CCM: check MIC only */
2967 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2968 /* Bad MIC */
2969 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2970 else
2971 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2972
2973 break;
2974
2975 case RX_RES_STATUS_SEC_TYPE_TKIP:
2976 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2977 /* Bad TTAK */
2978 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2979 break;
2980 }
2981 /* fall through if TTAK OK */
2982 default:
2983 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
2984 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2985 else
2986 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2987 break;
2988 };
2989
2990 IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
2991 decrypt_in, decrypt_out);
2992
2993 return decrypt_out;
2994}
2995
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002996static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
Zhu Yib481de92007-09-25 17:54:57 -07002997 int include_phy,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002998 struct iwl4965_rx_mem_buffer *rxb,
Zhu Yib481de92007-09-25 17:54:57 -07002999 struct ieee80211_rx_status *stats)
3000{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003001 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003002 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3003 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
3004 struct ieee80211_hdr *hdr;
3005 u16 len;
3006 __le32 *rx_end;
3007 unsigned int skblen;
3008 u32 ampdu_status;
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07003009 u32 ampdu_status_legacy;
Zhu Yib481de92007-09-25 17:54:57 -07003010
3011 if (!include_phy && priv->last_phy_res[0])
3012 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3013
3014 if (!rx_start) {
3015 IWL_ERROR("MPDU frame without a PHY data\n");
3016 return;
3017 }
3018 if (include_phy) {
3019 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
3020 rx_start->cfg_phy_cnt);
3021
3022 len = le16_to_cpu(rx_start->byte_count);
3023
3024 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
3025 sizeof(struct iwl4965_rx_phy_res) +
3026 rx_start->cfg_phy_cnt + len);
3027
3028 } else {
3029 struct iwl4965_rx_mpdu_res_start *amsdu =
3030 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3031
3032 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
3033 sizeof(struct iwl4965_rx_mpdu_res_start));
3034 len = le16_to_cpu(amsdu->byte_count);
3035 rx_start->byte_count = amsdu->byte_count;
3036 rx_end = (__le32 *) (((u8 *) hdr) + len);
3037 }
Tomas Winkler5425e492008-04-15 16:01:38 -07003038 if (len > priv->hw_params.max_pkt_size || len < 16) {
Zhu Yi12342c42007-12-20 11:27:32 +08003039 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07003040 return;
3041 }
3042
3043 ampdu_status = le32_to_cpu(*rx_end);
3044 skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
3045
Emmanuel Grumbach17e476b2008-03-19 16:41:42 -07003046 if (!include_phy) {
3047 /* New status scheme, need to translate */
3048 ampdu_status_legacy = ampdu_status;
3049 ampdu_status = iwl4965_translate_rx_status(ampdu_status);
3050 }
3051
Zhu Yib481de92007-09-25 17:54:57 -07003052 /* start from MAC */
3053 skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
3054 skb_put(rxb->skb, len); /* end where data ends */
3055
3056 /* We only process data packets if the interface is open */
3057 if (unlikely(!priv->is_open)) {
3058 IWL_DEBUG_DROP_LIMIT
3059 ("Dropping packet while interface is not open.\n");
3060 return;
3061 }
3062
Zhu Yib481de92007-09-25 17:54:57 -07003063 stats->flag = 0;
3064 hdr = (struct ieee80211_hdr *)rxb->skb->data;
3065
Emmanuel Grumbach3ec47732008-04-17 16:03:36 -07003066 /* in case of HW accelerated crypto and bad decryption, drop */
3067 if (!priv->cfg->mod_params->sw_crypto &&
3068 iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
3069 return;
Zhu Yib481de92007-09-25 17:54:57 -07003070
Zhu Yi12342c42007-12-20 11:27:32 +08003071 if (priv->add_radiotap)
3072 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
3073
Tomas Winkler19758be2008-03-12 16:58:51 -07003074 iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
Zhu Yib481de92007-09-25 17:54:57 -07003075 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3076 priv->alloc_rxb_skb--;
3077 rxb->skb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003078}
3079
3080/* Calc max signal level (dBm) among 3 possible receivers */
3081static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
3082{
3083 /* data from PHY/DSP regarding signal strength, etc.,
3084 * contents are always there, not configurable by host. */
3085 struct iwl4965_rx_non_cfg_phy *ncphy =
3086 (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
3087 u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
3088 >> IWL_AGC_DB_POS;
3089
3090 u32 valid_antennae =
3091 (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
3092 >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
3093 u8 max_rssi = 0;
3094 u32 i;
3095
3096 /* Find max rssi among 3 possible receivers.
3097 * These values are measured by the digital signal processor (DSP).
3098 * They should stay fairly constant even as the signal strength varies,
3099 * if the radio's automatic gain control (AGC) is working right.
3100 * AGC value (see below) will provide the "interesting" info. */
3101 for (i = 0; i < 3; i++)
3102 if (valid_antennae & (1 << i))
3103 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
3104
3105 IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
3106 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
3107 max_rssi, agc);
3108
3109 /* dBm = max_rssi dB - agc dB - constant.
3110 * Higher AGC (higher radio gain) means lower signal. */
3111 return (max_rssi - agc - IWL_RSSI_OFFSET);
3112}
3113
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003114#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003115
Assaf Krauss1ea87392008-03-18 14:57:50 -07003116void iwl4965_init_ht_hw_capab(struct iwl_priv *priv,
3117 struct ieee80211_ht_info *ht_info,
Tomas Winkler78330fd2008-02-06 02:37:18 +02003118 enum ieee80211_band band)
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003119{
3120 ht_info->cap = 0;
3121 memset(ht_info->supp_mcs_set, 0, 16);
3122
3123 ht_info->ht_supported = 1;
3124
Tomas Winkler78330fd2008-02-06 02:37:18 +02003125 if (band == IEEE80211_BAND_5GHZ) {
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003126 ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
3127 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
3128 ht_info->supp_mcs_set[4] = 0x01;
3129 }
3130 ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
3131 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
3132 ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
3133 (IWL_MIMO_PS_NONE << 2));
Assaf Krauss1ea87392008-03-18 14:57:50 -07003134
3135 if (priv->cfg->mod_params->amsdu_size_8K)
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02003136 ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
Ron Rindjunsky326eeee2007-11-26 16:14:37 +02003137
3138 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
3139 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
3140
3141 ht_info->supp_mcs_set[0] = 0xFF;
3142 ht_info->supp_mcs_set[1] = 0xFF;
3143}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003144#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003145
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003146static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07003147{
3148 unsigned long flags;
3149
3150 spin_lock_irqsave(&priv->sta_lock, flags);
3151 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
3152 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3153 priv->stations[sta_id].sta.sta.modify_mask = 0;
3154 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3155 spin_unlock_irqrestore(&priv->sta_lock, flags);
3156
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003157 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07003158}
3159
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003160static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
Zhu Yib481de92007-09-25 17:54:57 -07003161{
3162 /* FIXME: need locking over ps_status ??? */
Tomas Winkler947b13a2008-04-16 16:34:48 -07003163 u8 sta_id = iwl_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07003164
3165 if (sta_id != IWL_INVALID_STATION) {
3166 u8 sta_awake = priv->stations[sta_id].
3167 ps_status == STA_PS_STATUS_WAKE;
3168
3169 if (sta_awake && ps_bit)
3170 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
3171 else if (!sta_awake && !ps_bit) {
3172 iwl4965_sta_modify_ps_wake(priv, sta_id);
3173 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
3174 }
3175 }
3176}
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003177#ifdef CONFIG_IWLWIFI_DEBUG
Tomas Winkler17744ff2008-03-02 01:52:00 +02003178
3179/**
3180 * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
3181 *
3182 * You may hack this function to show different aspects of received frames,
3183 * including selective frame dumps.
3184 * group100 parameter selects whether to show 1 out of 100 good frames.
3185 *
3186 * TODO: This was originally written for 3945, need to audit for
3187 * proper operation with 4965.
3188 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003189static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003190 struct iwl4965_rx_packet *pkt,
3191 struct ieee80211_hdr *header, int group100)
3192{
3193 u32 to_us;
3194 u32 print_summary = 0;
3195 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
3196 u32 hundred = 0;
3197 u32 dataframe = 0;
3198 u16 fc;
3199 u16 seq_ctl;
3200 u16 channel;
3201 u16 phy_flags;
3202 int rate_sym;
3203 u16 length;
3204 u16 status;
3205 u16 bcn_tmr;
3206 u32 tsf_low;
3207 u64 tsf;
3208 u8 rssi;
3209 u8 agc;
3210 u16 sig_avg;
3211 u16 noise_diff;
3212 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
3213 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
3214 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
3215 u8 *data = IWL_RX_DATA(pkt);
3216
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003217 if (likely(!(iwl_debug_level & IWL_DL_RX)))
Tomas Winkler17744ff2008-03-02 01:52:00 +02003218 return;
3219
3220 /* MAC header */
3221 fc = le16_to_cpu(header->frame_control);
3222 seq_ctl = le16_to_cpu(header->seq_ctrl);
3223
3224 /* metadata */
3225 channel = le16_to_cpu(rx_hdr->channel);
3226 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
3227 rate_sym = rx_hdr->rate;
3228 length = le16_to_cpu(rx_hdr->len);
3229
3230 /* end-of-frame status and timestamp */
3231 status = le32_to_cpu(rx_end->status);
3232 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
3233 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
3234 tsf = le64_to_cpu(rx_end->timestamp);
3235
3236 /* signal statistics */
3237 rssi = rx_stats->rssi;
3238 agc = rx_stats->agc;
3239 sig_avg = le16_to_cpu(rx_stats->sig_avg);
3240 noise_diff = le16_to_cpu(rx_stats->noise_diff);
3241
3242 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
3243
3244 /* if data frame is to us and all is good,
3245 * (optionally) print summary for only 1 out of every 100 */
3246 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
3247 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
3248 dataframe = 1;
3249 if (!group100)
3250 print_summary = 1; /* print each frame */
3251 else if (priv->framecnt_to_us < 100) {
3252 priv->framecnt_to_us++;
3253 print_summary = 0;
3254 } else {
3255 priv->framecnt_to_us = 0;
3256 print_summary = 1;
3257 hundred = 1;
3258 }
3259 } else {
3260 /* print summary for all other frames */
3261 print_summary = 1;
3262 }
3263
3264 if (print_summary) {
3265 char *title;
3266 int rate_idx;
3267 u32 bitrate;
3268
3269 if (hundred)
3270 title = "100Frames";
3271 else if (fc & IEEE80211_FCTL_RETRY)
3272 title = "Retry";
3273 else if (ieee80211_is_assoc_response(fc))
3274 title = "AscRsp";
3275 else if (ieee80211_is_reassoc_response(fc))
3276 title = "RasRsp";
3277 else if (ieee80211_is_probe_response(fc)) {
3278 title = "PrbRsp";
3279 print_dump = 1; /* dump frame contents */
3280 } else if (ieee80211_is_beacon(fc)) {
3281 title = "Beacon";
3282 print_dump = 1; /* dump frame contents */
3283 } else if (ieee80211_is_atim(fc))
3284 title = "ATIM";
3285 else if (ieee80211_is_auth(fc))
3286 title = "Auth";
3287 else if (ieee80211_is_deauth(fc))
3288 title = "DeAuth";
3289 else if (ieee80211_is_disassoc(fc))
3290 title = "DisAssoc";
3291 else
3292 title = "Frame";
3293
3294 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
3295 if (unlikely(rate_idx == -1))
3296 bitrate = 0;
3297 else
3298 bitrate = iwl4965_rates[rate_idx].ieee / 2;
3299
3300 /* print frame summary.
3301 * MAC addresses show just the last byte (for brevity),
3302 * but you can hack it to show more, if you'd like to. */
3303 if (dataframe)
3304 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
3305 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
3306 title, fc, header->addr1[5],
3307 length, rssi, channel, bitrate);
3308 else {
3309 /* src/dst addresses assume managed mode */
3310 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
3311 "src=0x%02x, rssi=%u, tim=%lu usec, "
3312 "phy=0x%02x, chnl=%d\n",
3313 title, fc, header->addr1[5],
3314 header->addr3[5], rssi,
3315 tsf_low - priv->scan_start_tsf,
3316 phy_flags, channel);
3317 }
3318 }
3319 if (print_dump)
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003320 iwl_print_hex_dump(IWL_DL_RX, data, length);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003321}
3322#else
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003323static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003324 struct iwl4965_rx_packet *pkt,
3325 struct ieee80211_hdr *header,
3326 int group100)
3327{
3328}
3329#endif
3330
Zhu Yib481de92007-09-25 17:54:57 -07003331
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003332
Tomas Winkler857485c2008-03-21 13:53:44 -07003333/* Called for REPLY_RX (legacy ABG frames), or
Zhu Yib481de92007-09-25 17:54:57 -07003334 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003335static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003336 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003337{
Tomas Winkler17744ff2008-03-02 01:52:00 +02003338 struct ieee80211_hdr *header;
3339 struct ieee80211_rx_status rx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003340 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003341 /* Use phy data (Rx signal strength, etc.) contained within
3342 * this rx packet for legacy frames,
3343 * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
Tomas Winkler857485c2008-03-21 13:53:44 -07003344 int include_phy = (pkt->hdr.cmd == REPLY_RX);
Zhu Yib481de92007-09-25 17:54:57 -07003345 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3346 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
3347 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3348 __le32 *rx_end;
3349 unsigned int len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003350 u16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07003351 u8 network_packet;
3352
Tomas Winkler17744ff2008-03-02 01:52:00 +02003353 rx_status.mactime = le64_to_cpu(rx_start->timestamp);
Tomas Winklerdc92e492008-04-03 16:05:22 -07003354 rx_status.freq =
3355 ieee80211_frequency_to_channel(le16_to_cpu(rx_start->channel));
Tomas Winkler17744ff2008-03-02 01:52:00 +02003356 rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
3357 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Tomas Winklerdc92e492008-04-03 16:05:22 -07003358 rx_status.rate_idx =
3359 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
Tomas Winkler17744ff2008-03-02 01:52:00 +02003360 if (rx_status.band == IEEE80211_BAND_5GHZ)
3361 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
3362
3363 rx_status.antenna = 0;
3364 rx_status.flag = 0;
3365
Zhu Yib481de92007-09-25 17:54:57 -07003366 if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
Tomas Winklerdc92e492008-04-03 16:05:22 -07003367 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
3368 rx_start->cfg_phy_cnt);
Zhu Yib481de92007-09-25 17:54:57 -07003369 return;
3370 }
Tomas Winkler17744ff2008-03-02 01:52:00 +02003371
Zhu Yib481de92007-09-25 17:54:57 -07003372 if (!include_phy) {
3373 if (priv->last_phy_res[0])
3374 rx_start = (struct iwl4965_rx_phy_res *)
3375 &priv->last_phy_res[1];
3376 else
3377 rx_start = NULL;
3378 }
3379
3380 if (!rx_start) {
3381 IWL_ERROR("MPDU frame without a PHY data\n");
3382 return;
3383 }
3384
3385 if (include_phy) {
3386 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
3387 + rx_start->cfg_phy_cnt);
3388
3389 len = le16_to_cpu(rx_start->byte_count);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003390 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
Zhu Yib481de92007-09-25 17:54:57 -07003391 sizeof(struct iwl4965_rx_phy_res) + len);
3392 } else {
3393 struct iwl4965_rx_mpdu_res_start *amsdu =
3394 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3395
3396 header = (void *)(pkt->u.raw +
3397 sizeof(struct iwl4965_rx_mpdu_res_start));
3398 len = le16_to_cpu(amsdu->byte_count);
3399 rx_end = (__le32 *) (pkt->u.raw +
3400 sizeof(struct iwl4965_rx_mpdu_res_start) + len);
3401 }
3402
3403 if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
3404 !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
3405 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
3406 le32_to_cpu(*rx_end));
3407 return;
3408 }
3409
3410 priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
3411
Zhu Yib481de92007-09-25 17:54:57 -07003412 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Tomas Winkler17744ff2008-03-02 01:52:00 +02003413 rx_status.ssi = iwl4965_calc_rssi(rx_start);
Zhu Yib481de92007-09-25 17:54:57 -07003414
3415 /* Meaningful noise values are available only from beacon statistics,
3416 * which are gathered only when associated, and indicate noise
3417 * only for the associated network channel ...
3418 * Ignore these noise values while scanning (other channels) */
Tomas Winkler3109ece2008-03-28 16:33:35 -07003419 if (iwl_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07003420 !test_bit(STATUS_SCANNING, &priv->status)) {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003421 rx_status.noise = priv->last_rx_noise;
3422 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi,
3423 rx_status.noise);
Zhu Yib481de92007-09-25 17:54:57 -07003424 } else {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003425 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3426 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003427 }
3428
3429 /* Reset beacon noise level if not associated. */
Tomas Winkler3109ece2008-03-28 16:33:35 -07003430 if (!iwl_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003431 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3432
Tomas Winkler17744ff2008-03-02 01:52:00 +02003433 /* Set "1" to report good data frames in groups of 100 */
3434 /* FIXME: need to optimze the call: */
3435 iwl4965_dbg_report_frame(priv, pkt, header, 1);
Zhu Yib481de92007-09-25 17:54:57 -07003436
Tomas Winkler17744ff2008-03-02 01:52:00 +02003437 IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
3438 rx_status.ssi, rx_status.noise, rx_status.signal,
John W. Linville06501d22008-04-01 17:38:47 -04003439 (unsigned long long)rx_status.mactime);
Zhu Yib481de92007-09-25 17:54:57 -07003440
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003441 network_packet = iwl4965_is_network_packet(priv, header);
Zhu Yib481de92007-09-25 17:54:57 -07003442 if (network_packet) {
Tomas Winkler17744ff2008-03-02 01:52:00 +02003443 priv->last_rx_rssi = rx_status.ssi;
Zhu Yib481de92007-09-25 17:54:57 -07003444 priv->last_beacon_time = priv->ucode_beacon_time;
3445 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
3446 }
3447
3448 fc = le16_to_cpu(header->frame_control);
3449 switch (fc & IEEE80211_FCTL_FTYPE) {
3450 case IEEE80211_FTYPE_MGMT:
Zhu Yib481de92007-09-25 17:54:57 -07003451 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3452 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3453 header->addr2);
Tomas Winkler17744ff2008-03-02 01:52:00 +02003454 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003455 break;
3456
3457 case IEEE80211_FTYPE_CTL:
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02003458#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003459 switch (fc & IEEE80211_FCTL_STYPE) {
3460 case IEEE80211_STYPE_BACK_REQ:
3461 IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
3462 iwl4965_handle_data_packet(priv, 0, include_phy,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003463 rxb, &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003464 break;
3465 default:
3466 break;
3467 }
3468#endif
Zhu Yib481de92007-09-25 17:54:57 -07003469 break;
3470
Joe Perches0795af52007-10-03 17:59:30 -07003471 case IEEE80211_FTYPE_DATA: {
3472 DECLARE_MAC_BUF(mac1);
3473 DECLARE_MAC_BUF(mac2);
3474 DECLARE_MAC_BUF(mac3);
3475
Zhu Yib481de92007-09-25 17:54:57 -07003476 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3477 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3478 header->addr2);
3479
3480 if (unlikely(!network_packet))
3481 IWL_DEBUG_DROP("Dropping (non network): "
Joe Perches0795af52007-10-03 17:59:30 -07003482 "%s, %s, %s\n",
3483 print_mac(mac1, header->addr1),
3484 print_mac(mac2, header->addr2),
3485 print_mac(mac3, header->addr3));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003486 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
Joe Perches0795af52007-10-03 17:59:30 -07003487 IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
3488 print_mac(mac1, header->addr1),
3489 print_mac(mac2, header->addr2),
3490 print_mac(mac3, header->addr3));
Zhu Yib481de92007-09-25 17:54:57 -07003491 else
3492 iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
Tomas Winkler17744ff2008-03-02 01:52:00 +02003493 &rx_status);
Zhu Yib481de92007-09-25 17:54:57 -07003494 break;
Joe Perches0795af52007-10-03 17:59:30 -07003495 }
Zhu Yib481de92007-09-25 17:54:57 -07003496 default:
3497 break;
3498
3499 }
3500}
3501
3502/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
3503 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003504static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003505 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003506{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003507 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003508 priv->last_phy_res[0] = 1;
3509 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
3510 sizeof(struct iwl4965_rx_phy_res));
3511}
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003512static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003513 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003514
3515{
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003516#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003517 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3518 struct iwl4965_missed_beacon_notif *missed_beacon;
Zhu Yib481de92007-09-25 17:54:57 -07003519
3520 missed_beacon = &pkt->u.missed_beacon;
3521 if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
3522 IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
3523 le32_to_cpu(missed_beacon->consequtive_missed_beacons),
3524 le32_to_cpu(missed_beacon->total_missed_becons),
3525 le32_to_cpu(missed_beacon->num_recvd_beacons),
3526 le32_to_cpu(missed_beacon->num_expected_beacons));
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003527 if (!test_bit(STATUS_SCANNING, &priv->status))
3528 iwl_init_sensitivity(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003529 }
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07003530#endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
Zhu Yib481de92007-09-25 17:54:57 -07003531}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003532#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003533
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003534/**
3535 * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
3536 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003537static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003538 int sta_id, int tid)
3539{
3540 unsigned long flags;
3541
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003542 /* Remove "disable" flag, to enable Tx for this TID */
Zhu Yib481de92007-09-25 17:54:57 -07003543 spin_lock_irqsave(&priv->sta_lock, flags);
3544 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3545 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3546 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3547 spin_unlock_irqrestore(&priv->sta_lock, flags);
3548
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003549 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07003550}
3551
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003552/**
3553 * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
3554 *
3555 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
3556 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
3557 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003558static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559 struct iwl4965_ht_agg *agg,
3560 struct iwl4965_compressed_ba_resp*
Zhu Yib481de92007-09-25 17:54:57 -07003561 ba_resp)
3562
3563{
3564 int i, sh, ack;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003565 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
3566 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3567 u64 bitmap;
3568 int successes = 0;
3569 struct ieee80211_tx_status *tx_status;
Zhu Yib481de92007-09-25 17:54:57 -07003570
3571 if (unlikely(!agg->wait_for_ba)) {
3572 IWL_ERROR("Received BA when not expected\n");
3573 return -EINVAL;
3574 }
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003575
3576 /* Mark that the expected block-ack response arrived */
Zhu Yib481de92007-09-25 17:54:57 -07003577 agg->wait_for_ba = 0;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003578 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003579
3580 /* Calculate shift to align block-ack bits with our Tx window bits */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003581 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
Ian Schram01ebd062007-10-25 17:15:22 +08003582 if (sh < 0) /* tbw something is wrong with indices */
Zhu Yib481de92007-09-25 17:54:57 -07003583 sh += 0x100;
3584
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003585 /* don't use 64-bit values for now */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003586 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
Zhu Yib481de92007-09-25 17:54:57 -07003587
3588 if (agg->frame_count > (64 - sh)) {
3589 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
3590 return -1;
3591 }
3592
3593 /* check for success or failure according to the
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003594 * transmitted bitmap and block-ack bitmap */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003595 bitmap &= agg->bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003596
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003597 /* For each frame attempted in aggregation,
3598 * update driver's record of tx frame's status. */
Zhu Yib481de92007-09-25 17:54:57 -07003599 for (i = 0; i < agg->frame_count ; i++) {
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003600 ack = bitmap & (1 << i);
3601 successes += !!ack;
Zhu Yib481de92007-09-25 17:54:57 -07003602 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003603 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
3604 agg->start_idx + i);
Zhu Yib481de92007-09-25 17:54:57 -07003605 }
3606
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003607 tx_status = &priv->txq[scd_flow].txb[agg->start_idx].status;
3608 tx_status->flags = IEEE80211_TX_STATUS_ACK;
Ron Rindjunsky99556432008-01-28 14:07:25 +02003609 tx_status->flags |= IEEE80211_TX_STATUS_AMPDU;
3610 tx_status->ampdu_ack_map = successes;
3611 tx_status->ampdu_ack_len = agg->frame_count;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003612 iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags,
3613 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003614
John W. Linvillef868f4e2008-03-07 16:38:43 -05003615 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003616
3617 return 0;
3618}
3619
3620/**
3621 * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
3622 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003623static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003624 u16 txq_id)
3625{
3626 /* Simply stop the queue, but don't change any configuration;
3627 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003628 iwl_write_prph(priv,
Tomas Winkler12a81f62008-04-03 16:05:20 -07003629 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003630 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
3631 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3632}
3633
3634/**
3635 * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003636 * priv->lock must be held by the caller
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003637 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003638static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003639 u16 ssn_idx, u8 tx_fifo)
3640{
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003641 int ret = 0;
3642
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003643 if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3644 IWL_WARNING("queue number too small: %d, must be > %d\n",
3645 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3646 return -EINVAL;
3647 }
3648
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003649 ret = iwl_grab_nic_access(priv);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003650 if (ret)
3651 return ret;
3652
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003653 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3654
Tomas Winkler12a81f62008-04-03 16:05:20 -07003655 iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003656
3657 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3658 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3659 /* supposes that ssn_idx is valid (!= 0xFFF) */
3660 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3661
Tomas Winkler12a81f62008-04-03 16:05:20 -07003662 iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003663 iwl4965_txq_ctx_deactivate(priv, txq_id);
3664 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3665
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003666 iwl_release_nic_access(priv);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08003667
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003668 return 0;
3669}
3670
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003671int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003672 u8 tid, int txq_id)
3673{
3674 struct iwl4965_queue *q = &priv->txq[txq_id].q;
3675 u8 *addr = priv->stations[sta_id].sta.sta.addr;
3676 struct iwl4965_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3677
3678 switch (priv->stations[sta_id].tid[tid].agg.state) {
3679 case IWL_EMPTYING_HW_QUEUE_DELBA:
3680 /* We are reclaiming the last packet of the */
3681 /* aggregated HW queue */
3682 if (txq_id == tid_data->agg.txq_id &&
3683 q->read_ptr == q->write_ptr) {
3684 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3685 int tx_fifo = default_tid_to_tx_fifo[tid];
3686 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3687 iwl4965_tx_queue_agg_disable(priv, txq_id,
3688 ssn, tx_fifo);
3689 tid_data->agg.state = IWL_AGG_OFF;
3690 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3691 }
3692 break;
3693 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3694 /* We are reclaiming the last packet of the queue */
3695 if (tid_data->tfds_in_queue == 0) {
3696 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3697 tid_data->agg.state = IWL_AGG_ON;
3698 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3699 }
3700 break;
3701 }
Zhu Yib481de92007-09-25 17:54:57 -07003702 return 0;
3703}
3704
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003705/**
3706 * iwl4965_queue_dec_wrap - Decrement queue index, wrap back to end if needed
3707 * @index -- current index
3708 * @n_bd -- total number of entries in queue (s/b power of 2)
3709 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003710static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -07003711{
3712 return (index == 0) ? n_bd - 1 : index - 1;
3713}
3714
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003715/**
3716 * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3717 *
3718 * Handles block-acknowledge notification from device, which reports success
3719 * of frames sent via aggregation.
3720 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003721static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003722 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003723{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003724 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3725 struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
Zhu Yib481de92007-09-25 17:54:57 -07003726 int index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003727 struct iwl4965_tx_queue *txq = NULL;
3728 struct iwl4965_ht_agg *agg;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003729 DECLARE_MAC_BUF(mac);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003730
3731 /* "flow" corresponds to Tx queue */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003732 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003733
3734 /* "ssn" is start of block-ack Tx window, corresponds to index
3735 * (in Tx queue's circular buffer) of first TFD/frame in window */
Zhu Yib481de92007-09-25 17:54:57 -07003736 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3737
Ron Rindjunskydfe7d452008-04-15 16:01:45 -07003738 if (scd_flow >= priv->hw_params.max_txq_num) {
Zhu Yib481de92007-09-25 17:54:57 -07003739 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3740 return;
3741 }
3742
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003743 txq = &priv->txq[scd_flow];
Zhu Yib481de92007-09-25 17:54:57 -07003744 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003745
3746 /* Find index just before block-ack window */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003747 index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003748
Ian Schram01ebd062007-10-25 17:15:22 +08003749 /* TODO: Need to get this copy more safely - now good for debug */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003750
Joe Perches0795af52007-10-03 17:59:30 -07003751 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3752 "sta_id = %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003753 agg->wait_for_ba,
Joe Perches0795af52007-10-03 17:59:30 -07003754 print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
Zhu Yib481de92007-09-25 17:54:57 -07003755 ba_resp->sta_id);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003756 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
Zhu Yib481de92007-09-25 17:54:57 -07003757 "%d, scd_ssn = %d\n",
3758 ba_resp->tid,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003759 ba_resp->seq_ctl,
Tomas Winkler0310ae72008-03-11 16:17:19 -07003760 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
Zhu Yib481de92007-09-25 17:54:57 -07003761 ba_resp->scd_flow,
3762 ba_resp->scd_ssn);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003763 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
Zhu Yib481de92007-09-25 17:54:57 -07003764 agg->start_idx,
John W. Linvillef868f4e2008-03-07 16:38:43 -05003765 (unsigned long long)agg->bitmap);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003766
3767 /* Update driver's record of ACK vs. not for each frame in window */
Zhu Yib481de92007-09-25 17:54:57 -07003768 iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003769
3770 /* Release all TFDs before the SSN, i.e. all TFDs in front of
3771 * block-ack window (we assume that they've been successfully
3772 * transmitted ... if not, it's too late anyway). */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003773 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3774 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3775 priv->stations[ba_resp->sta_id].
3776 tid[ba_resp->tid].tfds_in_queue -= freed;
3777 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3778 priv->mac80211_registered &&
3779 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3780 ieee80211_wake_queue(priv->hw, scd_flow);
3781 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3782 ba_resp->tid, scd_flow);
3783 }
Zhu Yib481de92007-09-25 17:54:57 -07003784}
3785
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003786/**
3787 * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3788 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003789static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
Zhu Yib481de92007-09-25 17:54:57 -07003790 u16 txq_id)
3791{
3792 u32 tbl_dw_addr;
3793 u32 tbl_dw;
3794 u16 scd_q2ratid;
3795
3796 scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3797
3798 tbl_dw_addr = priv->scd_base_addr +
3799 SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3800
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003801 tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003802
3803 if (txq_id & 0x1)
3804 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3805 else
3806 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3807
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003808 iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
Zhu Yib481de92007-09-25 17:54:57 -07003809
3810 return 0;
3811}
3812
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003813
Zhu Yib481de92007-09-25 17:54:57 -07003814/**
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003815 * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3816 *
3817 * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3818 * i.e. it must be one of the higher queues used for aggregation
Zhu Yib481de92007-09-25 17:54:57 -07003819 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003820static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
Zhu Yib481de92007-09-25 17:54:57 -07003821 int tx_fifo, int sta_id, int tid,
3822 u16 ssn_idx)
3823{
3824 unsigned long flags;
3825 int rc;
3826 u16 ra_tid;
3827
3828 if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3829 IWL_WARNING("queue number too small: %d, must be > %d\n",
3830 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3831
3832 ra_tid = BUILD_RAxTID(sta_id, tid);
3833
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003834 /* Modify device's station table to Tx this TID */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003835 iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
Zhu Yib481de92007-09-25 17:54:57 -07003836
3837 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003838 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003839 if (rc) {
3840 spin_unlock_irqrestore(&priv->lock, flags);
3841 return rc;
3842 }
3843
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003844 /* Stop this Tx queue before configuring it */
Zhu Yib481de92007-09-25 17:54:57 -07003845 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3846
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003847 /* Map receiver-address / traffic-ID to this queue */
Zhu Yib481de92007-09-25 17:54:57 -07003848 iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3849
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003850 /* Set this queue as a chain-building queue */
Tomas Winkler12a81f62008-04-03 16:05:20 -07003851 iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Zhu Yib481de92007-09-25 17:54:57 -07003852
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003853 /* Place first TFD at index corresponding to start sequence number.
3854 * Assumes that ssn_idx is valid (!= 0xFFF) */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003855 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3856 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
Zhu Yib481de92007-09-25 17:54:57 -07003857 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3858
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003859 /* Set up Tx window size and frame limit for this queue */
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003860 iwl_write_targ_mem(priv,
Zhu Yib481de92007-09-25 17:54:57 -07003861 priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3862 (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3863 SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3864
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003865 iwl_write_targ_mem(priv, priv->scd_base_addr +
Zhu Yib481de92007-09-25 17:54:57 -07003866 SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3867 (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3868 & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3869
Tomas Winkler12a81f62008-04-03 16:05:20 -07003870 iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Zhu Yib481de92007-09-25 17:54:57 -07003871
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003872 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
Zhu Yib481de92007-09-25 17:54:57 -07003873 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3874
Tomas Winkler3395f6e2008-03-25 16:33:37 -07003875 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003876 spin_unlock_irqrestore(&priv->lock, flags);
3877
3878 return 0;
3879}
3880
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003881#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003882
3883/**
3884 * iwl4965_add_station - Initialize a station's hardware rate table
3885 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003886 * The uCode's station table contains a table of fallback rates
Zhu Yib481de92007-09-25 17:54:57 -07003887 * for automatic fallback during transmission.
3888 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003889 * NOTE: This sets up a default set of values. These will be replaced later
3890 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
3891 * rc80211_simple.
Zhu Yib481de92007-09-25 17:54:57 -07003892 *
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003893 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
3894 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
3895 * which requires station table entry to exist).
Zhu Yib481de92007-09-25 17:54:57 -07003896 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003897void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -07003898{
3899 int i, r;
Tomas Winkler66c73db2008-04-15 16:01:40 -07003900 struct iwl_link_quality_cmd link_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003901 .reserved1 = 0,
3902 };
3903 u16 rate_flags;
3904
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003905 /* Set up the rate scaling to start at selected rate, fall back
3906 * all the way down to 1M in IEEE order, and then spin on 1M */
Zhu Yib481de92007-09-25 17:54:57 -07003907 if (is_ap)
3908 r = IWL_RATE_54M_INDEX;
Johannes Berg8318d782008-01-24 19:38:38 +01003909 else if (priv->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07003910 r = IWL_RATE_6M_INDEX;
3911 else
3912 r = IWL_RATE_1M_INDEX;
3913
3914 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3915 rate_flags = 0;
3916 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
3917 rate_flags |= RATE_MCS_CCK_MSK;
3918
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08003919 /* Use Tx antenna B only */
Zhu Yib481de92007-09-25 17:54:57 -07003920 rate_flags |= RATE_MCS_ANT_B_MSK;
3921 rate_flags &= ~RATE_MCS_ANT_A_MSK;
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
4026 IWL_DEBUG_ASSOC("supported HT rate 0x%X %X "
4027 "rxon flags 0x%X operation mode :0x%X "
4028 "extension channel offset 0x%x "
4029 "control chan %d\n",
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02004030 ht_info->supp_mcs_set[0], ht_info->supp_mcs_set[1],
4031 le32_to_cpu(rxon->flags), ht_info->ht_protection,
Zhu Yib481de92007-09-25 17:54:57 -07004032 ht_info->extension_chan_offset,
4033 ht_info->control_channel);
4034 return;
4035}
4036
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004037void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004038 struct ieee80211_ht_info *sta_ht_inf)
Zhu Yib481de92007-09-25 17:54:57 -07004039{
4040 __le32 sta_flags;
Tomas Winklere53cfe02008-01-30 22:05:13 -08004041 u8 mimo_ps_mode;
Zhu Yib481de92007-09-25 17:54:57 -07004042
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004043 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07004044 goto done;
4045
Tomas Winklere53cfe02008-01-30 22:05:13 -08004046 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
4047
Zhu Yib481de92007-09-25 17:54:57 -07004048 sta_flags = priv->stations[index].sta.station_flags;
4049
Tomas Winklere53cfe02008-01-30 22:05:13 -08004050 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
4051
4052 switch (mimo_ps_mode) {
4053 case WLAN_HT_CAP_MIMO_PS_STATIC:
4054 sta_flags |= STA_FLG_MIMO_DIS_MSK;
4055 break;
4056 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
Zhu Yib481de92007-09-25 17:54:57 -07004057 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
Tomas Winklere53cfe02008-01-30 22:05:13 -08004058 break;
4059 case WLAN_HT_CAP_MIMO_PS_DISABLED:
4060 break;
4061 default:
4062 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
4063 break;
4064 }
Zhu Yib481de92007-09-25 17:54:57 -07004065
4066 sta_flags |= cpu_to_le32(
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004067 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
Zhu Yib481de92007-09-25 17:54:57 -07004068
4069 sta_flags |= cpu_to_le32(
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004070 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
Zhu Yib481de92007-09-25 17:54:57 -07004071
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004072 if (iwl4965_is_fat_tx_allowed(priv, sta_ht_inf))
Zhu Yib481de92007-09-25 17:54:57 -07004073 sta_flags |= STA_FLG_FAT_EN_MSK;
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004074 else
Tomas Winklere53cfe02008-01-30 22:05:13 -08004075 sta_flags &= ~STA_FLG_FAT_EN_MSK;
Ron Rindjunsky67d62032007-11-26 16:14:40 +02004076
Zhu Yib481de92007-09-25 17:54:57 -07004077 priv->stations[index].sta.station_flags = sta_flags;
4078 done:
4079 return;
4080}
4081
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004082static int iwl4965_rx_agg_start(struct iwl_priv *priv,
4083 const u8 *addr, int tid, u16 ssn)
Zhu Yib481de92007-09-25 17:54:57 -07004084{
4085 unsigned long flags;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004086 int sta_id;
4087
4088 sta_id = iwl_find_station(priv, addr);
4089 if (sta_id == IWL_INVALID_STATION)
4090 return -ENXIO;
Zhu Yib481de92007-09-25 17:54:57 -07004091
4092 spin_lock_irqsave(&priv->sta_lock, flags);
4093 priv->stations[sta_id].sta.station_flags_msk = 0;
4094 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
4095 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
4096 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
4097 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4098 spin_unlock_irqrestore(&priv->sta_lock, flags);
4099
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004100 return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4101 CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07004102}
4103
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004104static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
4105 const u8 *addr, int tid)
Zhu Yib481de92007-09-25 17:54:57 -07004106{
4107 unsigned long flags;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004108 int sta_id;
4109
4110 sta_id = iwl_find_station(priv, addr);
4111 if (sta_id == IWL_INVALID_STATION)
4112 return -ENXIO;
Zhu Yib481de92007-09-25 17:54:57 -07004113
4114 spin_lock_irqsave(&priv->sta_lock, flags);
4115 priv->stations[sta_id].sta.station_flags_msk = 0;
4116 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
4117 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
4118 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4119 spin_unlock_irqrestore(&priv->sta_lock, flags);
4120
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004121 return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4122 CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07004123}
4124
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +08004125/*
4126 * Find first available (lowest unused) Tx Queue, mark it "active".
4127 * Called only when finding queue for aggregation.
4128 * Should never return anything < 7, because they should already
4129 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
4130 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004131static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004132{
4133 int txq_id;
4134
Tomas Winkler5425e492008-04-15 16:01:38 -07004135 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
Zhu Yib481de92007-09-25 17:54:57 -07004136 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
4137 return txq_id;
4138 return -1;
4139}
4140
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004141static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
4142 u16 tid, u16 *start_seq_num)
Zhu Yib481de92007-09-25 17:54:57 -07004143{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004144 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004145 int sta_id;
4146 int tx_fifo;
4147 int txq_id;
4148 int ssn = -1;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004149 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004150 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004151 struct iwl4965_tid_data *tid_data;
Joe Perches0795af52007-10-03 17:59:30 -07004152 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07004153
4154 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4155 tx_fifo = default_tid_to_tx_fifo[tid];
4156 else
4157 return -EINVAL;
4158
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004159 IWL_WARNING("%s on ra = %s tid = %d\n",
4160 __func__, print_mac(mac, ra), tid);
Zhu Yib481de92007-09-25 17:54:57 -07004161
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004162 sta_id = iwl_find_station(priv, ra);
Zhu Yib481de92007-09-25 17:54:57 -07004163 if (sta_id == IWL_INVALID_STATION)
4164 return -ENXIO;
4165
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004166 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
4167 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
4168 return -ENXIO;
4169 }
4170
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004171 txq_id = iwl4965_txq_ctx_activate_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004172 if (txq_id == -1)
4173 return -ENXIO;
4174
4175 spin_lock_irqsave(&priv->sta_lock, flags);
4176 tid_data = &priv->stations[sta_id].tid[tid];
4177 ssn = SEQ_TO_SN(tid_data->seq_number);
4178 tid_data->agg.txq_id = txq_id;
4179 spin_unlock_irqrestore(&priv->sta_lock, flags);
4180
4181 *start_seq_num = ssn;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004182 ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
4183 sta_id, tid, ssn);
4184 if (ret)
4185 return ret;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004186
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004187 ret = 0;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004188 if (tid_data->tfds_in_queue == 0) {
4189 printk(KERN_ERR "HW queue is empty\n");
4190 tid_data->agg.state = IWL_AGG_ON;
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004191 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004192 } else {
4193 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
4194 tid_data->tfds_in_queue);
4195 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
4196 }
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004197 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004198}
4199
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004200static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
Zhu Yib481de92007-09-25 17:54:57 -07004201{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004202 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07004203 int tx_fifo_id, txq_id, sta_id, ssn = -1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004204 struct iwl4965_tid_data *tid_data;
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004205 int ret, write_ptr, read_ptr;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004206 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07004207 DECLARE_MAC_BUF(mac);
4208
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004209 if (!ra) {
4210 IWL_ERROR("ra = NULL\n");
Zhu Yib481de92007-09-25 17:54:57 -07004211 return -EINVAL;
4212 }
4213
4214 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4215 tx_fifo_id = default_tid_to_tx_fifo[tid];
4216 else
4217 return -EINVAL;
4218
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004219 sta_id = iwl_find_station(priv, ra);
Zhu Yib481de92007-09-25 17:54:57 -07004220
4221 if (sta_id == IWL_INVALID_STATION)
4222 return -ENXIO;
4223
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004224 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
4225 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
4226
Zhu Yib481de92007-09-25 17:54:57 -07004227 tid_data = &priv->stations[sta_id].tid[tid];
4228 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
4229 txq_id = tid_data->agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004230 write_ptr = priv->txq[txq_id].q.write_ptr;
4231 read_ptr = priv->txq[txq_id].q.read_ptr;
Zhu Yib481de92007-09-25 17:54:57 -07004232
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004233 /* The queue is not empty */
4234 if (write_ptr != read_ptr) {
4235 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
4236 priv->stations[sta_id].tid[tid].agg.state =
4237 IWL_EMPTYING_HW_QUEUE_DELBA;
4238 return 0;
4239 }
4240
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004241 IWL_DEBUG_HT("HW queue is empty\n");
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004242 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
4243
4244 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004245 ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02004246 spin_unlock_irqrestore(&priv->lock, flags);
4247
Ron Rindjunskyb095d03a72008-03-06 17:36:56 -08004248 if (ret)
4249 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07004250
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004251 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
Zhu Yib481de92007-09-25 17:54:57 -07004252
4253 return 0;
4254}
4255
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004256int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
4257 enum ieee80211_ampdu_mlme_action action,
4258 const u8 *addr, u16 tid, u16 *ssn)
4259{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004260 struct iwl_priv *priv = hw->priv;
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004261 DECLARE_MAC_BUF(mac);
4262
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004263 IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
4264 print_mac(mac, addr), tid);
4265
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004266 switch (action) {
4267 case IEEE80211_AMPDU_RX_START:
4268 IWL_DEBUG_HT("start Rx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004269 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004270 case IEEE80211_AMPDU_RX_STOP:
4271 IWL_DEBUG_HT("stop Rx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004272 return iwl4965_rx_agg_stop(priv, addr, tid);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004273 case IEEE80211_AMPDU_TX_START:
4274 IWL_DEBUG_HT("start Tx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004275 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004276 case IEEE80211_AMPDU_TX_STOP:
4277 IWL_DEBUG_HT("stop Tx\n");
Ron Rindjunskyfe07aa72008-04-17 16:03:37 -07004278 return iwl4965_tx_agg_stop(hw, addr, tid);
Ron Rindjunsky8114fcf2008-01-28 14:07:23 +02004279 default:
4280 IWL_DEBUG_HT("unknown\n");
4281 return -EINVAL;
4282 break;
4283 }
4284 return 0;
4285}
4286
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004287#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004288
4289/* Set up 4965-specific Rx frame reply handlers */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004290void iwl4965_hw_rx_handler_setup(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004291{
4292 /* Legacy Rx frames */
Tomas Winkler857485c2008-03-21 13:53:44 -07004293 priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
Zhu Yib481de92007-09-25 17:54:57 -07004294
4295 /* High-throughput (HT) Rx frames */
4296 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
4297 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
4298
4299 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
4300 iwl4965_rx_missed_beacon_notif;
4301
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004302#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07004303 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004304#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07004305}
4306
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004307void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004308{
4309 INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004310#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
Zhu Yib481de92007-09-25 17:54:57 -07004311 INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
4312#endif
Zhu Yib481de92007-09-25 17:54:57 -07004313 init_timer(&priv->statistics_periodic);
4314 priv->statistics_periodic.data = (unsigned long)priv;
4315 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
4316}
4317
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004318void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004319{
4320 del_timer_sync(&priv->statistics_periodic);
4321
4322 cancel_delayed_work(&priv->init_alive_start);
4323}
4324
Tomas Winkler3c424c22008-04-15 16:01:42 -07004325
4326static struct iwl_hcmd_ops iwl4965_hcmd = {
Tomas Winkler7e8c5192008-04-15 16:01:43 -07004327 .rxon_assoc = iwl4965_send_rxon_assoc,
Tomas Winkler3c424c22008-04-15 16:01:42 -07004328};
4329
Tomas Winkler857485c2008-03-21 13:53:44 -07004330static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
4331 .enqueue_hcmd = iwl4965_enqueue_hcmd,
Emmanuel Grumbachf0832f12008-04-16 16:34:47 -07004332#ifdef CONFIG_IWL4965_RUN_TIME_CALIB
4333 .chain_noise_reset = iwl4965_chain_noise_reset,
4334 .gain_computation = iwl4965_gain_computation,
4335#endif
Tomas Winkler857485c2008-03-21 13:53:44 -07004336};
4337
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004338static struct iwl_lib_ops iwl4965_lib = {
Assaf Kraussbf85ea42008-03-14 10:38:49 -07004339 .init_drv = iwl4965_init_drv,
Tomas Winkler5425e492008-04-15 16:01:38 -07004340 .set_hw_params = iwl4965_hw_set_hw_params,
Tomas Winklere2a722e2008-04-14 21:16:10 -07004341 .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
Tomas Winkler57aab752008-04-14 21:16:03 -07004342 .hw_nic_init = iwl4965_hw_nic_init,
4343 .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
4344 .alive_notify = iwl4965_alive_notify,
4345 .load_ucode = iwl4965_load_bsm,
Tomas Winkler6f4083a2008-04-16 16:34:49 -07004346 .apm_ops = {
4347 .set_pwr_src = iwl4965_set_pwr_src,
4348 },
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004349 .eeprom_ops = {
Tomas Winkler073d3f52008-04-21 15:41:52 -07004350 .regulatory_bands = {
4351 EEPROM_REGULATORY_BAND_1_CHANNELS,
4352 EEPROM_REGULATORY_BAND_2_CHANNELS,
4353 EEPROM_REGULATORY_BAND_3_CHANNELS,
4354 EEPROM_REGULATORY_BAND_4_CHANNELS,
4355 EEPROM_REGULATORY_BAND_5_CHANNELS,
4356 EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
4357 EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
4358 },
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004359 .verify_signature = iwlcore_eeprom_verify_signature,
4360 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
4361 .release_semaphore = iwlcore_eeprom_release_semaphore,
Tomas Winkler073d3f52008-04-21 15:41:52 -07004362 .query_addr = iwlcore_eeprom_query_addr,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004363 },
Mohamed Abbasad97edd2008-03-28 16:21:06 -07004364 .radio_kill_sw = iwl4965_radio_kill_sw,
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004365 .set_power = iwl4965_set_power,
4366 .update_chain_flags = iwl4965_update_chain_flags,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004367};
4368
4369static struct iwl_ops iwl4965_ops = {
4370 .lib = &iwl4965_lib,
Tomas Winkler3c424c22008-04-15 16:01:42 -07004371 .hcmd = &iwl4965_hcmd,
Tomas Winkler857485c2008-03-21 13:53:44 -07004372 .utils = &iwl4965_hcmd_utils,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004373};
4374
Ron Rindjunskyfed90172008-04-15 16:01:41 -07004375struct iwl_cfg iwl4965_agn_cfg = {
Tomas Winkler82b9a122008-03-04 18:09:30 -08004376 .name = "4965AGN",
Tomas Winkler4bf775c2008-03-04 18:09:31 -08004377 .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
Tomas Winkler82b9a122008-03-04 18:09:30 -08004378 .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
Tomas Winkler073d3f52008-04-21 15:41:52 -07004379 .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
Assaf Krauss6bc913b2008-03-11 16:17:18 -07004380 .ops = &iwl4965_ops,
Assaf Krauss1ea87392008-03-18 14:57:50 -07004381 .mod_params = &iwl4965_mod_params,
Tomas Winkler82b9a122008-03-04 18:09:30 -08004382};
4383
Assaf Krauss1ea87392008-03-18 14:57:50 -07004384module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
4385MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
4386module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
4387MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Emmanuel Grumbachfcc76c62008-04-15 16:01:47 -07004388module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
4389MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
Assaf Krauss1ea87392008-03-18 14:57:50 -07004390module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
4391MODULE_PARM_DESC(debug, "debug output mask");
4392module_param_named(
4393 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
4394MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4395
4396module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
4397MODULE_PARM_DESC(queues_num, "number of hw queues.");
4398
4399/* QoS */
4400module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
4401MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
4402module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
4403MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
4404