OSDN Git Service

iwlwifi: handle shared memory
[uclinux-h8/linux.git] / drivers / net / wireless / iwlwifi / iwl-4965.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4  *
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>
38 #include <linux/etherdevice.h>
39 #include <asm/unaligned.h>
40
41 #include "iwl-eeprom.h"
42 #include "iwl-4965.h"
43 #include "iwl-core.h"
44 #include "iwl-io.h"
45 #include "iwl-helpers.h"
46 #include "iwl-calib.h"
47
48 /* module parameters */
49 static struct iwl_mod_params iwl4965_mod_params = {
50         .num_of_queues = IWL4965_MAX_NUM_QUEUES,
51         .enable_qos = 1,
52         .amsdu_size_8K = 1,
53         /* the rest are 0 by default */
54 };
55
56 static void iwl4965_hw_card_show_info(struct iwl_priv *priv);
57
58 #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_MIMO2_##s##M_PLCP,\
62                                     IWL_RATE_MIMO3_##s##M_PLCP,\
63                                     IWL_RATE_##r##M_IEEE,      \
64                                     IWL_RATE_##ip##M_INDEX,    \
65                                     IWL_RATE_##in##M_INDEX,    \
66                                     IWL_RATE_##rp##M_INDEX,    \
67                                     IWL_RATE_##rn##M_INDEX,    \
68                                     IWL_RATE_##pp##M_INDEX,    \
69                                     IWL_RATE_##np##M_INDEX }
70
71 /*
72  * Parameter order:
73  *   rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
74  *
75  * If there isn't a valid next or previous rate then INV is used which
76  * maps to IWL_RATE_INVALID
77  *
78  */
79 const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
80         IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2),    /*  1mbps */
81         IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5),          /*  2mbps */
82         IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11),        /*5.5mbps */
83         IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18),      /* 11mbps */
84         IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11),        /*  6mbps */
85         IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11),       /*  9mbps */
86         IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18),   /* 12mbps */
87         IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24),   /* 18mbps */
88         IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36),   /* 24mbps */
89         IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48),   /* 36mbps */
90         IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54),   /* 48mbps */
91         IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
92         IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
93         /* FIXME:RS:          ^^    should be INV (legacy) */
94 };
95
96 #ifdef CONFIG_IWL4965_HT
97
98 static const u16 default_tid_to_tx_fifo[] = {
99         IWL_TX_FIFO_AC1,
100         IWL_TX_FIFO_AC0,
101         IWL_TX_FIFO_AC0,
102         IWL_TX_FIFO_AC1,
103         IWL_TX_FIFO_AC2,
104         IWL_TX_FIFO_AC2,
105         IWL_TX_FIFO_AC3,
106         IWL_TX_FIFO_AC3,
107         IWL_TX_FIFO_NONE,
108         IWL_TX_FIFO_NONE,
109         IWL_TX_FIFO_NONE,
110         IWL_TX_FIFO_NONE,
111         IWL_TX_FIFO_NONE,
112         IWL_TX_FIFO_NONE,
113         IWL_TX_FIFO_NONE,
114         IWL_TX_FIFO_NONE,
115         IWL_TX_FIFO_AC3
116 };
117
118 #endif  /*CONFIG_IWL4965_HT */
119
120 /* check contents of special bootstrap uCode SRAM */
121 static int iwl4965_verify_bsm(struct iwl_priv *priv)
122 {
123         __le32 *image = priv->ucode_boot.v_addr;
124         u32 len = priv->ucode_boot.len;
125         u32 reg;
126         u32 val;
127
128         IWL_DEBUG_INFO("Begin verify bsm\n");
129
130         /* verify BSM SRAM contents */
131         val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
132         for (reg = BSM_SRAM_LOWER_BOUND;
133              reg < BSM_SRAM_LOWER_BOUND + len;
134              reg += sizeof(u32), image++) {
135                 val = iwl_read_prph(priv, reg);
136                 if (val != le32_to_cpu(*image)) {
137                         IWL_ERROR("BSM uCode verification failed at "
138                                   "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
139                                   BSM_SRAM_LOWER_BOUND,
140                                   reg - BSM_SRAM_LOWER_BOUND, len,
141                                   val, le32_to_cpu(*image));
142                         return -EIO;
143                 }
144         }
145
146         IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
147
148         return 0;
149 }
150
151 /**
152  * iwl4965_load_bsm - Load bootstrap instructions
153  *
154  * BSM operation:
155  *
156  * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
157  * in special SRAM that does not power down during RFKILL.  When powering back
158  * up after power-saving sleeps (or during initial uCode load), the BSM loads
159  * the bootstrap program into the on-board processor, and starts it.
160  *
161  * The bootstrap program loads (via DMA) instructions and data for a new
162  * program from host DRAM locations indicated by the host driver in the
163  * BSM_DRAM_* registers.  Once the new program is loaded, it starts
164  * automatically.
165  *
166  * When initializing the NIC, the host driver points the BSM to the
167  * "initialize" uCode image.  This uCode sets up some internal data, then
168  * notifies host via "initialize alive" that it is complete.
169  *
170  * The host then replaces the BSM_DRAM_* pointer values to point to the
171  * normal runtime uCode instructions and a backup uCode data cache buffer
172  * (filled initially with starting data values for the on-board processor),
173  * then triggers the "initialize" uCode to load and launch the runtime uCode,
174  * which begins normal operation.
175  *
176  * When doing a power-save shutdown, runtime uCode saves data SRAM into
177  * the backup data cache in DRAM before SRAM is powered down.
178  *
179  * When powering back up, the BSM loads the bootstrap program.  This reloads
180  * the runtime uCode instructions and the backup data cache into SRAM,
181  * and re-launches the runtime uCode from where it left off.
182  */
183 static int iwl4965_load_bsm(struct iwl_priv *priv)
184 {
185         __le32 *image = priv->ucode_boot.v_addr;
186         u32 len = priv->ucode_boot.len;
187         dma_addr_t pinst;
188         dma_addr_t pdata;
189         u32 inst_len;
190         u32 data_len;
191         int i;
192         u32 done;
193         u32 reg_offset;
194         int ret;
195
196         IWL_DEBUG_INFO("Begin load bsm\n");
197
198         /* make sure bootstrap program is no larger than BSM's SRAM size */
199         if (len > IWL_MAX_BSM_SIZE)
200                 return -EINVAL;
201
202         /* Tell bootstrap uCode where to find the "Initialize" uCode
203          *   in host DRAM ... host DRAM physical address bits 35:4 for 4965.
204          * NOTE:  iwl4965_initialize_alive_start() will replace these values,
205          *        after the "initialize" uCode has run, to point to
206          *        runtime/protocol instructions and backup data cache. */
207         pinst = priv->ucode_init.p_addr >> 4;
208         pdata = priv->ucode_init_data.p_addr >> 4;
209         inst_len = priv->ucode_init.len;
210         data_len = priv->ucode_init_data.len;
211
212         ret = iwl_grab_nic_access(priv);
213         if (ret)
214                 return ret;
215
216         iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
217         iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
218         iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
219         iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
220
221         /* Fill BSM memory with bootstrap instructions */
222         for (reg_offset = BSM_SRAM_LOWER_BOUND;
223              reg_offset < BSM_SRAM_LOWER_BOUND + len;
224              reg_offset += sizeof(u32), image++)
225                 _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
226
227         ret = iwl4965_verify_bsm(priv);
228         if (ret) {
229                 iwl_release_nic_access(priv);
230                 return ret;
231         }
232
233         /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
234         iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
235         iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
236         iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
237
238         /* Load bootstrap code into instruction SRAM now,
239          *   to prepare to load "initialize" uCode */
240         iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
241
242         /* Wait for load of bootstrap uCode to finish */
243         for (i = 0; i < 100; i++) {
244                 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
245                 if (!(done & BSM_WR_CTRL_REG_BIT_START))
246                         break;
247                 udelay(10);
248         }
249         if (i < 100)
250                 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
251         else {
252                 IWL_ERROR("BSM write did not complete!\n");
253                 return -EIO;
254         }
255
256         /* Enable future boot loads whenever power management unit triggers it
257          *   (e.g. when powering back up after power-save shutdown) */
258         iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
259
260         iwl_release_nic_access(priv);
261
262         return 0;
263 }
264
265 static int iwl4965_init_drv(struct iwl_priv *priv)
266 {
267         int ret;
268         int i;
269
270         priv->retry_rate = 1;
271         priv->ibss_beacon = NULL;
272
273         spin_lock_init(&priv->lock);
274         spin_lock_init(&priv->power_data.lock);
275         spin_lock_init(&priv->sta_lock);
276         spin_lock_init(&priv->hcmd_lock);
277         spin_lock_init(&priv->lq_mngr.lock);
278
279         for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
280                 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
281
282         INIT_LIST_HEAD(&priv->free_frames);
283
284         mutex_init(&priv->mutex);
285
286         /* Clear the driver's (not device's) station table */
287         iwlcore_clear_stations_table(priv);
288
289         priv->data_retry_limit = -1;
290         priv->ieee_channels = NULL;
291         priv->ieee_rates = NULL;
292         priv->band = IEEE80211_BAND_2GHZ;
293
294         priv->iw_mode = IEEE80211_IF_TYPE_STA;
295
296         priv->use_ant_b_for_management_frame = 1; /* start with ant B */
297         priv->ps_mode = IWL_MIMO_PS_NONE;
298
299         /* Choose which receivers/antennas to use */
300         iwl4965_set_rxon_chain(priv);
301
302         iwlcore_reset_qos(priv);
303
304         priv->qos_data.qos_active = 0;
305         priv->qos_data.qos_cap.val = 0;
306
307         iwlcore_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
308
309         priv->rates_mask = IWL_RATES_MASK;
310         /* If power management is turned on, default to AC mode */
311         priv->power_mode = IWL_POWER_AC;
312         priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
313
314         ret = iwl_init_channel_map(priv);
315         if (ret) {
316                 IWL_ERROR("initializing regulatory failed: %d\n", ret);
317                 goto err;
318         }
319
320         ret = iwl4965_init_geos(priv);
321         if (ret) {
322                 IWL_ERROR("initializing geos failed: %d\n", ret);
323                 goto err_free_channel_map;
324         }
325
326         ret = ieee80211_register_hw(priv->hw);
327         if (ret) {
328                 IWL_ERROR("Failed to register network device (error %d)\n",
329                                 ret);
330                 goto err_free_geos;
331         }
332
333         priv->hw->conf.beacon_int = 100;
334         priv->mac80211_registered = 1;
335
336         return 0;
337
338 err_free_geos:
339         iwl4965_free_geos(priv);
340 err_free_channel_map:
341         iwl_free_channel_map(priv);
342 err:
343         return ret;
344 }
345
346 static int is_fat_channel(__le32 rxon_flags)
347 {
348         return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
349                 (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
350 }
351
352 #ifdef CONFIG_IWL4965_HT
353 static u8 is_single_rx_stream(struct iwl_priv *priv)
354 {
355         return !priv->current_ht_config.is_ht ||
356                ((priv->current_ht_config.supp_mcs_set[1] == 0) &&
357                 (priv->current_ht_config.supp_mcs_set[2] == 0)) ||
358                priv->ps_mode == IWL_MIMO_PS_STATIC;
359 }
360 #else
361 static inline u8 is_single_rx_stream(struct iwl_priv *priv)
362 {
363         return 1;
364 }
365 #endif  /*CONFIG_IWL4965_HT */
366
367 int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
368 {
369         int idx = 0;
370
371         /* 4965 HT rate format */
372         if (rate_n_flags & RATE_MCS_HT_MSK) {
373                 idx = (rate_n_flags & 0xff);
374
375                 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
376                         idx = idx - IWL_RATE_MIMO2_6M_PLCP;
377
378                 idx += IWL_FIRST_OFDM_RATE;
379                 /* skip 9M not supported in ht*/
380                 if (idx >= IWL_RATE_9M_INDEX)
381                         idx += 1;
382                 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
383                         return idx;
384
385         /* 4965 legacy rate format, search for match in table */
386         } else {
387                 for (idx = 0; idx < ARRAY_SIZE(iwl4965_rates); idx++)
388                         if (iwl4965_rates[idx].plcp == (rate_n_flags & 0xFF))
389                                 return idx;
390         }
391
392         return -1;
393 }
394
395 /**
396  * translate ucode response to mac80211 tx status control values
397  */
398 void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
399                                   struct ieee80211_tx_control *control)
400 {
401         int rate_index;
402
403         control->antenna_sel_tx =
404                 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
405         if (rate_n_flags & RATE_MCS_HT_MSK)
406                 control->flags |= IEEE80211_TXCTL_OFDM_HT;
407         if (rate_n_flags & RATE_MCS_GF_MSK)
408                 control->flags |= IEEE80211_TXCTL_GREEN_FIELD;
409         if (rate_n_flags & RATE_MCS_FAT_MSK)
410                 control->flags |= IEEE80211_TXCTL_40_MHZ_WIDTH;
411         if (rate_n_flags & RATE_MCS_DUP_MSK)
412                 control->flags |= IEEE80211_TXCTL_DUP_DATA;
413         if (rate_n_flags & RATE_MCS_SGI_MSK)
414                 control->flags |= IEEE80211_TXCTL_SHORT_GI;
415         /* since iwl4965_hwrate_to_plcp_idx is band indifferent, we always use
416          * IEEE80211_BAND_2GHZ band as it contains all the rates */
417         rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
418         if (rate_index == -1)
419                 control->tx_rate = NULL;
420         else
421                 control->tx_rate =
422                         &priv->bands[IEEE80211_BAND_2GHZ].bitrates[rate_index];
423 }
424
425 /*
426  * Determine how many receiver/antenna chains to use.
427  * More provides better reception via diversity.  Fewer saves power.
428  * MIMO (dual stream) requires at least 2, but works better with 3.
429  * This does not determine *which* chains to use, just how many.
430  */
431 static int iwl4965_get_rx_chain_counter(struct iwl_priv *priv,
432                                         u8 *idle_state, u8 *rx_state)
433 {
434         u8 is_single = is_single_rx_stream(priv);
435         u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
436
437         /* # of Rx chains to use when expecting MIMO. */
438         if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
439                 *rx_state = 2;
440         else
441                 *rx_state = 3;
442
443         /* # Rx chains when idling and maybe trying to save power */
444         switch (priv->ps_mode) {
445         case IWL_MIMO_PS_STATIC:
446         case IWL_MIMO_PS_DYNAMIC:
447                 *idle_state = (is_cam) ? 2 : 1;
448                 break;
449         case IWL_MIMO_PS_NONE:
450                 *idle_state = (is_cam) ? *rx_state : 1;
451                 break;
452         default:
453                 *idle_state = 1;
454                 break;
455         }
456
457         return 0;
458 }
459
460 int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
461 {
462         int rc;
463         unsigned long flags;
464
465         spin_lock_irqsave(&priv->lock, flags);
466         rc = iwl_grab_nic_access(priv);
467         if (rc) {
468                 spin_unlock_irqrestore(&priv->lock, flags);
469                 return rc;
470         }
471
472         /* stop Rx DMA */
473         iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
474         rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
475                                      (1 << 24), 1000);
476         if (rc < 0)
477                 IWL_ERROR("Can't stop Rx DMA.\n");
478
479         iwl_release_nic_access(priv);
480         spin_unlock_irqrestore(&priv->lock, flags);
481
482         return 0;
483 }
484
485 /*
486  * EEPROM handlers
487  */
488
489 static int iwl4965_eeprom_check_version(struct iwl_priv *priv)
490 {
491         u16 eeprom_ver;
492         u16 calib_ver;
493
494         eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
495
496         calib_ver = iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
497
498         if (eeprom_ver < EEPROM_4965_EEPROM_VERSION ||
499             calib_ver < EEPROM_4965_TX_POWER_VERSION)
500                 goto err;
501
502         return 0;
503 err:
504         IWL_ERROR("Unsuported EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
505                   eeprom_ver, EEPROM_4965_EEPROM_VERSION,
506                   calib_ver, EEPROM_4965_TX_POWER_VERSION);
507         return -EINVAL;
508
509 }
510 int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
511 {
512         int ret;
513         unsigned long flags;
514
515         spin_lock_irqsave(&priv->lock, flags);
516         ret = iwl_grab_nic_access(priv);
517         if (ret) {
518                 spin_unlock_irqrestore(&priv->lock, flags);
519                 return ret;
520         }
521
522         if (src == IWL_PWR_SRC_VAUX) {
523                 u32 val;
524                 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
525                                             &val);
526
527                 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
528                         iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
529                                                APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
530                                                ~APMG_PS_CTRL_MSK_PWR_SRC);
531                 }
532         } else {
533                 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
534                                        APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
535                                        ~APMG_PS_CTRL_MSK_PWR_SRC);
536         }
537
538         iwl_release_nic_access(priv);
539         spin_unlock_irqrestore(&priv->lock, flags);
540
541         return ret;
542 }
543
544 static int iwl4965_rx_init(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
545 {
546         int ret;
547         unsigned long flags;
548         unsigned int rb_size;
549
550         spin_lock_irqsave(&priv->lock, flags);
551         ret = iwl_grab_nic_access(priv);
552         if (ret) {
553                 spin_unlock_irqrestore(&priv->lock, flags);
554                 return ret;
555         }
556
557         if (priv->cfg->mod_params->amsdu_size_8K)
558                 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
559         else
560                 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
561
562         /* Stop Rx DMA */
563         iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
564
565         /* Reset driver's Rx queue write index */
566         iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
567
568         /* Tell device where to find RBD circular buffer in DRAM */
569         iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
570                            rxq->dma_addr >> 8);
571
572         /* Tell device where in DRAM to update its Rx status */
573         iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
574                            (priv->shared_phys +
575                             offsetof(struct iwl4965_shared, rb_closed)) >> 4);
576
577         /* Enable Rx DMA, enable host interrupt, Rx buffer size 4k, 256 RBDs */
578         iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
579                            FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
580                            FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
581                            rb_size |
582                              /* 0x10 << 4 | */
583                            (RX_QUEUE_SIZE_LOG <<
584                               FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
585
586         /*
587          * iwl_write32(priv,CSR_INT_COAL_REG,0);
588          */
589
590         iwl_release_nic_access(priv);
591         spin_unlock_irqrestore(&priv->lock, flags);
592
593         return 0;
594 }
595
596 /* Tell 4965 where to find the "keep warm" buffer */
597 static int iwl4965_kw_init(struct iwl_priv *priv)
598 {
599         unsigned long flags;
600         int rc;
601
602         spin_lock_irqsave(&priv->lock, flags);
603         rc = iwl_grab_nic_access(priv);
604         if (rc)
605                 goto out;
606
607         iwl_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
608                              priv->kw.dma_addr >> 4);
609         iwl_release_nic_access(priv);
610 out:
611         spin_unlock_irqrestore(&priv->lock, flags);
612         return rc;
613 }
614
615 static int iwl4965_kw_alloc(struct iwl_priv *priv)
616 {
617         struct pci_dev *dev = priv->pci_dev;
618         struct iwl4965_kw *kw = &priv->kw;
619
620         kw->size = IWL4965_KW_SIZE;     /* TBW need set somewhere else */
621         kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
622         if (!kw->v_addr)
623                 return -ENOMEM;
624
625         return 0;
626 }
627
628 /**
629  * iwl4965_kw_free - Free the "keep warm" buffer
630  */
631 static void iwl4965_kw_free(struct iwl_priv *priv)
632 {
633         struct pci_dev *dev = priv->pci_dev;
634         struct iwl4965_kw *kw = &priv->kw;
635
636         if (kw->v_addr) {
637                 pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
638                 memset(kw, 0, sizeof(*kw));
639         }
640 }
641
642 /**
643  * iwl4965_txq_ctx_reset - Reset TX queue context
644  * Destroys all DMA structures and initialise them again
645  *
646  * @param priv
647  * @return error code
648  */
649 static int iwl4965_txq_ctx_reset(struct iwl_priv *priv)
650 {
651         int rc = 0;
652         int txq_id, slots_num;
653         unsigned long flags;
654
655         iwl4965_kw_free(priv);
656
657         /* Free all tx/cmd queues and keep-warm buffer */
658         iwl4965_hw_txq_ctx_free(priv);
659
660         /* Alloc keep-warm buffer */
661         rc = iwl4965_kw_alloc(priv);
662         if (rc) {
663                 IWL_ERROR("Keep Warm allocation failed");
664                 goto error_kw;
665         }
666
667         spin_lock_irqsave(&priv->lock, flags);
668
669         rc = iwl_grab_nic_access(priv);
670         if (unlikely(rc)) {
671                 IWL_ERROR("TX reset failed");
672                 spin_unlock_irqrestore(&priv->lock, flags);
673                 goto error_reset;
674         }
675
676         /* Turn off all Tx DMA channels */
677         iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
678         iwl_release_nic_access(priv);
679         spin_unlock_irqrestore(&priv->lock, flags);
680
681         /* Tell 4965 where to find the keep-warm buffer */
682         rc = iwl4965_kw_init(priv);
683         if (rc) {
684                 IWL_ERROR("kw_init failed\n");
685                 goto error_reset;
686         }
687
688         /* Alloc and init all (default 16) Tx queues,
689          * including the command queue (#4) */
690         for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
691                 slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
692                                         TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
693                 rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
694                                        txq_id);
695                 if (rc) {
696                         IWL_ERROR("Tx %d queue init failed\n", txq_id);
697                         goto error;
698                 }
699         }
700
701         return rc;
702
703  error:
704         iwl4965_hw_txq_ctx_free(priv);
705  error_reset:
706         iwl4965_kw_free(priv);
707  error_kw:
708         return rc;
709 }
710 static int iwl4965_apm_init(struct iwl_priv *priv)
711 {
712         unsigned long flags;
713         int ret = 0;
714
715         spin_lock_irqsave(&priv->lock, flags);
716         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
717                           CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
718
719         /* set "initialization complete" bit to move adapter
720          * D0U* --> D0A* state */
721         iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
722
723         /* wait for clock stabilization */
724         ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
725                            CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
726                            CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
727         if (ret < 0) {
728                 IWL_DEBUG_INFO("Failed to init the card\n");
729                 goto out;
730         }
731
732         ret = iwl_grab_nic_access(priv);
733         if (ret)
734                 goto out;
735
736         /* enable DMA */
737         iwl_write_prph(priv, APMG_CLK_CTRL_REG,
738                         APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
739
740         udelay(20);
741
742         iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
743                           APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
744
745         iwl_release_nic_access(priv);
746 out:
747         spin_unlock_irqrestore(&priv->lock, flags);
748         return ret;
749 }
750
751 int iwl4965_hw_nic_init(struct iwl_priv *priv)
752 {
753         unsigned long flags;
754         struct iwl4965_rx_queue *rxq = &priv->rxq;
755         u8 val_link;
756         u32 val;
757         int ret;
758
759         /* nic_init */
760         priv->cfg->ops->lib->apm_ops.init(priv);
761
762         spin_lock_irqsave(&priv->lock, flags);
763         iwl_write32(priv, CSR_INT_COALESCING, 512 / 32);
764         spin_unlock_irqrestore(&priv->lock, flags);
765
766         ret = priv->cfg->ops->lib->apm_ops.set_pwr_src(priv, IWL_PWR_SRC_VMAIN);
767
768         spin_lock_irqsave(&priv->lock, flags);
769
770         if ((priv->rev_id & 0x80) == 0x80 && (priv->rev_id & 0x7f) < 8) {
771                 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
772                 /* Enable No Snoop field */
773                 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
774                                        val & ~(1 << 11));
775         }
776
777         spin_unlock_irqrestore(&priv->lock, flags);
778
779         pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
780
781         /* disable L1 entry -- workaround for pre-B1 */
782         pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
783
784         spin_lock_irqsave(&priv->lock, flags);
785
786         /* set CSR_HW_CONFIG_REG for uCode use */
787
788         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
789                     CSR49_HW_IF_CONFIG_REG_BIT_4965_R |
790                     CSR49_HW_IF_CONFIG_REG_BIT_RADIO_SI |
791                     CSR49_HW_IF_CONFIG_REG_BIT_MAC_SI);
792
793         ret = iwl_grab_nic_access(priv);
794         if (ret < 0) {
795                 spin_unlock_irqrestore(&priv->lock, flags);
796                 IWL_DEBUG_INFO("Failed to init the card\n");
797                 return ret;
798         }
799
800         iwl_read_prph(priv, APMG_PS_CTRL_REG);
801         iwl_set_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
802         udelay(5);
803         iwl_clear_bits_prph(priv, APMG_PS_CTRL_REG, APMG_PS_CTRL_VAL_RESET_REQ);
804
805         iwl_release_nic_access(priv);
806         spin_unlock_irqrestore(&priv->lock, flags);
807
808         iwl4965_hw_card_show_info(priv);
809
810         /* end nic_init */
811
812         /* Allocate the RX queue, or reset if it is already allocated */
813         if (!rxq->bd) {
814                 ret = iwl4965_rx_queue_alloc(priv);
815                 if (ret) {
816                         IWL_ERROR("Unable to initialize Rx queue\n");
817                         return -ENOMEM;
818                 }
819         } else
820                 iwl4965_rx_queue_reset(priv, rxq);
821
822         iwl4965_rx_replenish(priv);
823
824         iwl4965_rx_init(priv, rxq);
825
826         spin_lock_irqsave(&priv->lock, flags);
827
828         rxq->need_update = 1;
829         iwl4965_rx_queue_update_write_ptr(priv, rxq);
830
831         /* init the txpower calibration pointer */
832         priv->calib_info = (struct iwl_eeprom_calib_info *)
833                 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
834
835         spin_unlock_irqrestore(&priv->lock, flags);
836
837         /* Allocate and init all Tx and Command queues */
838         ret = iwl4965_txq_ctx_reset(priv);
839         if (ret)
840                 return ret;
841
842         set_bit(STATUS_INIT, &priv->status);
843
844         return 0;
845 }
846
847 int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
848 {
849         int rc = 0;
850         u32 reg_val;
851         unsigned long flags;
852
853         spin_lock_irqsave(&priv->lock, flags);
854
855         /* set stop master bit */
856         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
857
858         reg_val = iwl_read32(priv, CSR_GP_CNTRL);
859
860         if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
861             (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
862                 IWL_DEBUG_INFO("Card in power save, master is already "
863                                "stopped\n");
864         else {
865                 rc = iwl_poll_bit(priv, CSR_RESET,
866                                   CSR_RESET_REG_FLAG_MASTER_DISABLED,
867                                   CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
868                 if (rc < 0) {
869                         spin_unlock_irqrestore(&priv->lock, flags);
870                         return rc;
871                 }
872         }
873
874         spin_unlock_irqrestore(&priv->lock, flags);
875         IWL_DEBUG_INFO("stop master\n");
876
877         return rc;
878 }
879
880 /**
881  * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
882  */
883 void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
884 {
885
886         int txq_id;
887         unsigned long flags;
888
889         /* Stop each Tx DMA channel, and wait for it to be idle */
890         for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
891                 spin_lock_irqsave(&priv->lock, flags);
892                 if (iwl_grab_nic_access(priv)) {
893                         spin_unlock_irqrestore(&priv->lock, flags);
894                         continue;
895                 }
896
897                 iwl_write_direct32(priv,
898                                    IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
899                 iwl_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
900                                     IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
901                                     (txq_id), 200);
902                 iwl_release_nic_access(priv);
903                 spin_unlock_irqrestore(&priv->lock, flags);
904         }
905
906         /* Deallocate memory for all Tx queues */
907         iwl4965_hw_txq_ctx_free(priv);
908 }
909
910 int iwl4965_hw_nic_reset(struct iwl_priv *priv)
911 {
912         int rc = 0;
913         unsigned long flags;
914
915         iwl4965_hw_nic_stop_master(priv);
916
917         spin_lock_irqsave(&priv->lock, flags);
918
919         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
920
921         udelay(10);
922
923         iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
924         rc = iwl_poll_bit(priv, CSR_RESET,
925                           CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
926                           CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
927
928         udelay(10);
929
930         rc = iwl_grab_nic_access(priv);
931         if (!rc) {
932                 iwl_write_prph(priv, APMG_CLK_EN_REG,
933                                 APMG_CLK_VAL_DMA_CLK_RQT |
934                                 APMG_CLK_VAL_BSM_CLK_RQT);
935
936                 udelay(10);
937
938                 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
939                                         APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
940
941                 iwl_release_nic_access(priv);
942         }
943
944         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
945         wake_up_interruptible(&priv->wait_command_queue);
946
947         spin_unlock_irqrestore(&priv->lock, flags);
948
949         return rc;
950
951 }
952
953 #define REG_RECALIB_PERIOD (60)
954
955 /**
956  * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
957  *
958  * This callback is provided in order to send a statistics request.
959  *
960  * This timer function is continually reset to execute within
961  * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
962  * was received.  We need to ensure we receive the statistics in order
963  * to update the temperature used for calibrating the TXPOWER.
964  */
965 static void iwl4965_bg_statistics_periodic(unsigned long data)
966 {
967         struct iwl_priv *priv = (struct iwl_priv *)data;
968
969         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
970                 return;
971
972         iwl_send_statistics_request(priv, CMD_ASYNC);
973 }
974
975 void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
976 {
977         struct iwl4965_ct_kill_config cmd;
978         unsigned long flags;
979         int ret = 0;
980
981         spin_lock_irqsave(&priv->lock, flags);
982         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
983                     CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
984         spin_unlock_irqrestore(&priv->lock, flags);
985
986         cmd.critical_temperature_R =
987                 cpu_to_le32(priv->hw_params.ct_kill_threshold);
988
989         ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
990                                sizeof(cmd), &cmd);
991         if (ret)
992                 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
993         else
994                 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
995                         "critical temperature is %d\n",
996                         cmd.critical_temperature_R);
997 }
998
999 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1000
1001 /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
1002  * Called after every association, but this runs only once!
1003  *  ... once chain noise is calibrated the first time, it's good forever.  */
1004 static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
1005 {
1006         struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
1007
1008         if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
1009                 struct iwl4965_calibration_cmd cmd;
1010
1011                 memset(&cmd, 0, sizeof(cmd));
1012                 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1013                 cmd.diff_gain_a = 0;
1014                 cmd.diff_gain_b = 0;
1015                 cmd.diff_gain_c = 0;
1016                 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1017                                  sizeof(cmd), &cmd))
1018                         IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
1019                 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
1020                 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
1021         }
1022 }
1023
1024 static void iwl4965_gain_computation(struct iwl_priv *priv,
1025                 u32 *average_noise,
1026                 u16 min_average_noise_antenna_i,
1027                 u32 min_average_noise)
1028 {
1029         int i, ret;
1030         struct iwl_chain_noise_data *data = &priv->chain_noise_data;
1031
1032         data->delta_gain_code[min_average_noise_antenna_i] = 0;
1033
1034         for (i = 0; i < NUM_RX_CHAINS; i++) {
1035                 s32 delta_g = 0;
1036
1037                 if (!(data->disconn_array[i]) &&
1038                     (data->delta_gain_code[i] ==
1039                              CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
1040                         delta_g = average_noise[i] - min_average_noise;
1041                         data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
1042                         data->delta_gain_code[i] =
1043                                 min(data->delta_gain_code[i],
1044                                 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
1045
1046                         data->delta_gain_code[i] =
1047                                 (data->delta_gain_code[i] | (1 << 2));
1048                 } else {
1049                         data->delta_gain_code[i] = 0;
1050                 }
1051         }
1052         IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
1053                      data->delta_gain_code[0],
1054                      data->delta_gain_code[1],
1055                      data->delta_gain_code[2]);
1056
1057         /* Differential gain gets sent to uCode only once */
1058         if (!data->radio_write) {
1059                 struct iwl4965_calibration_cmd cmd;
1060                 data->radio_write = 1;
1061
1062                 memset(&cmd, 0, sizeof(cmd));
1063                 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
1064                 cmd.diff_gain_a = data->delta_gain_code[0];
1065                 cmd.diff_gain_b = data->delta_gain_code[1];
1066                 cmd.diff_gain_c = data->delta_gain_code[2];
1067                 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
1068                                       sizeof(cmd), &cmd);
1069                 if (ret)
1070                         IWL_DEBUG_CALIB("fail sending cmd "
1071                                      "REPLY_PHY_CALIBRATION_CMD \n");
1072
1073                 /* TODO we might want recalculate
1074                  * rx_chain in rxon cmd */
1075
1076                 /* Mark so we run this algo only once! */
1077                 data->state = IWL_CHAIN_NOISE_CALIBRATED;
1078         }
1079         data->chain_noise_a = 0;
1080         data->chain_noise_b = 0;
1081         data->chain_noise_c = 0;
1082         data->chain_signal_a = 0;
1083         data->chain_signal_b = 0;
1084         data->chain_signal_c = 0;
1085         data->beacon_count = 0;
1086 }
1087
1088 static void iwl4965_bg_sensitivity_work(struct work_struct *work)
1089 {
1090         struct iwl_priv *priv = container_of(work, struct iwl_priv,
1091                         sensitivity_work);
1092
1093         mutex_lock(&priv->mutex);
1094
1095         if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1096             test_bit(STATUS_SCANNING, &priv->status)) {
1097                 mutex_unlock(&priv->mutex);
1098                 return;
1099         }
1100
1101         if (priv->start_calib) {
1102                 iwl_chain_noise_calibration(priv, &priv->statistics);
1103
1104                 iwl_sensitivity_calibration(priv, &priv->statistics);
1105         }
1106
1107         mutex_unlock(&priv->mutex);
1108         return;
1109 }
1110 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
1111
1112 static void iwl4965_bg_txpower_work(struct work_struct *work)
1113 {
1114         struct iwl_priv *priv = container_of(work, struct iwl_priv,
1115                         txpower_work);
1116
1117         /* If a scan happened to start before we got here
1118          * then just return; the statistics notification will
1119          * kick off another scheduled work to compensate for
1120          * any temperature delta we missed here. */
1121         if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1122             test_bit(STATUS_SCANNING, &priv->status))
1123                 return;
1124
1125         mutex_lock(&priv->mutex);
1126
1127         /* Regardless of if we are assocaited, we must reconfigure the
1128          * TX power since frames can be sent on non-radar channels while
1129          * not associated */
1130         iwl4965_hw_reg_send_txpower(priv);
1131
1132         /* Update last_temperature to keep is_calib_needed from running
1133          * when it isn't needed... */
1134         priv->last_temperature = priv->temperature;
1135
1136         mutex_unlock(&priv->mutex);
1137 }
1138
1139 /*
1140  * Acquire priv->lock before calling this function !
1141  */
1142 static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
1143 {
1144         iwl_write_direct32(priv, HBUS_TARG_WRPTR,
1145                              (index & 0xff) | (txq_id << 8));
1146         iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
1147 }
1148
1149 /**
1150  * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
1151  * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
1152  * @scd_retry: (1) Indicates queue will be used in aggregation mode
1153  *
1154  * NOTE:  Acquire priv->lock before calling this function !
1155  */
1156 static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
1157                                         struct iwl4965_tx_queue *txq,
1158                                         int tx_fifo_id, int scd_retry)
1159 {
1160         int txq_id = txq->q.id;
1161
1162         /* Find out whether to activate Tx queue */
1163         int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
1164
1165         /* Set up and activate */
1166         iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
1167                                  (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
1168                                  (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
1169                                  (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
1170                                  (scd_retry << SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
1171                                  SCD_QUEUE_STTS_REG_MSK);
1172
1173         txq->sched_retry = scd_retry;
1174
1175         IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
1176                        active ? "Activate" : "Deactivate",
1177                        scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
1178 }
1179
1180 static const u16 default_queue_to_tx_fifo[] = {
1181         IWL_TX_FIFO_AC3,
1182         IWL_TX_FIFO_AC2,
1183         IWL_TX_FIFO_AC1,
1184         IWL_TX_FIFO_AC0,
1185         IWL_CMD_FIFO_NUM,
1186         IWL_TX_FIFO_HCCA_1,
1187         IWL_TX_FIFO_HCCA_2
1188 };
1189
1190 static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
1191 {
1192         set_bit(txq_id, &priv->txq_ctx_active_msk);
1193 }
1194
1195 static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
1196 {
1197         clear_bit(txq_id, &priv->txq_ctx_active_msk);
1198 }
1199
1200 int iwl4965_alive_notify(struct iwl_priv *priv)
1201 {
1202         u32 a;
1203         int i = 0;
1204         unsigned long flags;
1205         int ret;
1206
1207         spin_lock_irqsave(&priv->lock, flags);
1208
1209 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1210         memset(&(priv->sensitivity_data), 0,
1211                sizeof(struct iwl_sensitivity_data));
1212         memset(&(priv->chain_noise_data), 0,
1213                sizeof(struct iwl_chain_noise_data));
1214         for (i = 0; i < NUM_RX_CHAINS; i++)
1215                 priv->chain_noise_data.delta_gain_code[i] =
1216                                 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
1217 #endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
1218         ret = iwl_grab_nic_access(priv);
1219         if (ret) {
1220                 spin_unlock_irqrestore(&priv->lock, flags);
1221                 return ret;
1222         }
1223
1224         /* Clear 4965's internal Tx Scheduler data base */
1225         priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
1226         a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
1227         for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
1228                 iwl_write_targ_mem(priv, a, 0);
1229         for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
1230                 iwl_write_targ_mem(priv, a, 0);
1231         for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
1232                 iwl_write_targ_mem(priv, a, 0);
1233
1234         /* Tel 4965 where to find Tx byte count tables */
1235         iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
1236                 (priv->shared_phys +
1237                  offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
1238
1239         /* Disable chain mode for all queues */
1240         iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
1241
1242         /* Initialize each Tx queue (including the command queue) */
1243         for (i = 0; i < priv->hw_params.max_txq_num; i++) {
1244
1245                 /* TFD circular buffer read/write indexes */
1246                 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
1247                 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
1248
1249                 /* Max Tx Window size for Scheduler-ACK mode */
1250                 iwl_write_targ_mem(priv, priv->scd_base_addr +
1251                                         SCD_CONTEXT_QUEUE_OFFSET(i),
1252                                         (SCD_WIN_SIZE <<
1253                                         SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
1254                                         SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
1255
1256                 /* Frame limit */
1257                 iwl_write_targ_mem(priv, priv->scd_base_addr +
1258                                         SCD_CONTEXT_QUEUE_OFFSET(i) +
1259                                         sizeof(u32),
1260                                         (SCD_FRAME_LIMIT <<
1261                                         SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
1262                                         SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
1263
1264         }
1265         iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
1266                                  (1 << priv->hw_params.max_txq_num) - 1);
1267
1268         /* Activate all Tx DMA/FIFO channels */
1269         iwl_write_prph(priv, IWL49_SCD_TXFACT,
1270                                  SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
1271
1272         iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
1273
1274         /* Map each Tx/cmd queue to its corresponding fifo */
1275         for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
1276                 int ac = default_queue_to_tx_fifo[i];
1277                 iwl4965_txq_ctx_activate(priv, i);
1278                 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
1279         }
1280
1281         iwl_release_nic_access(priv);
1282         spin_unlock_irqrestore(&priv->lock, flags);
1283
1284         /* Ask for statistics now, the uCode will send statistics notification
1285          * periodically after association */
1286         iwl_send_statistics_request(priv, CMD_ASYNC);
1287         return ret;
1288 }
1289
1290 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1291 static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
1292         .min_nrg_cck = 97,
1293         .max_nrg_cck = 0,
1294
1295         .auto_corr_min_ofdm = 85,
1296         .auto_corr_min_ofdm_mrc = 170,
1297         .auto_corr_min_ofdm_x1 = 105,
1298         .auto_corr_min_ofdm_mrc_x1 = 220,
1299
1300         .auto_corr_max_ofdm = 120,
1301         .auto_corr_max_ofdm_mrc = 210,
1302         .auto_corr_max_ofdm_x1 = 140,
1303         .auto_corr_max_ofdm_mrc_x1 = 270,
1304
1305         .auto_corr_min_cck = 125,
1306         .auto_corr_max_cck = 200,
1307         .auto_corr_min_cck_mrc = 200,
1308         .auto_corr_max_cck_mrc = 400,
1309
1310         .nrg_th_cck = 100,
1311         .nrg_th_ofdm = 100,
1312 };
1313 #endif
1314
1315 /**
1316  * iwl4965_hw_set_hw_params
1317  *
1318  * Called when initializing driver
1319  */
1320 int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
1321 {
1322
1323         if ((priv->cfg->mod_params->num_of_queues > IWL4965_MAX_NUM_QUEUES) ||
1324             (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
1325                 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
1326                           IWL_MIN_NUM_QUEUES, IWL4965_MAX_NUM_QUEUES);
1327                 return -EINVAL;
1328         }
1329
1330         priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
1331         priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
1332         priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
1333         priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1334         priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
1335         if (priv->cfg->mod_params->amsdu_size_8K)
1336                 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
1337         else
1338                 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
1339         priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
1340         priv->hw_params.max_stations = IWL4965_STATION_COUNT;
1341         priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
1342
1343         priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
1344         priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
1345         priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
1346         priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
1347
1348         priv->hw_params.tx_chains_num = 2;
1349         priv->hw_params.rx_chains_num = 2;
1350         priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
1351         priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
1352         priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
1353
1354 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1355         priv->hw_params.sens = &iwl4965_sensitivity;
1356 #endif
1357
1358         return 0;
1359 }
1360
1361 /**
1362  * iwl4965_hw_txq_ctx_free - Free TXQ Context
1363  *
1364  * Destroy all TX DMA queues and structures
1365  */
1366 void iwl4965_hw_txq_ctx_free(struct iwl_priv *priv)
1367 {
1368         int txq_id;
1369
1370         /* Tx queues */
1371         for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
1372                 iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
1373
1374         /* Keep-warm buffer */
1375         iwl4965_kw_free(priv);
1376 }
1377
1378 /**
1379  * iwl4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
1380  *
1381  * Does NOT advance any TFD circular buffer read/write indexes
1382  * Does NOT free the TFD itself (which is within circular buffer)
1383  */
1384 int iwl4965_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
1385 {
1386         struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
1387         struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
1388         struct pci_dev *dev = priv->pci_dev;
1389         int i;
1390         int counter = 0;
1391         int index, is_odd;
1392
1393         /* Host command buffers stay mapped in memory, nothing to clean */
1394         if (txq->q.id == IWL_CMD_QUEUE_NUM)
1395                 return 0;
1396
1397         /* Sanity check on number of chunks */
1398         counter = IWL_GET_BITS(*bd, num_tbs);
1399         if (counter > MAX_NUM_OF_TBS) {
1400                 IWL_ERROR("Too many chunks: %i\n", counter);
1401                 /* @todo issue fatal error, it is quite serious situation */
1402                 return 0;
1403         }
1404
1405         /* Unmap chunks, if any.
1406          * TFD info for odd chunks is different format than for even chunks. */
1407         for (i = 0; i < counter; i++) {
1408                 index = i / 2;
1409                 is_odd = i & 0x1;
1410
1411                 if (is_odd)
1412                         pci_unmap_single(
1413                                 dev,
1414                                 IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
1415                                 (IWL_GET_BITS(bd->pa[index],
1416                                               tb2_addr_hi20) << 16),
1417                                 IWL_GET_BITS(bd->pa[index], tb2_len),
1418                                 PCI_DMA_TODEVICE);
1419
1420                 else if (i > 0)
1421                         pci_unmap_single(dev,
1422                                          le32_to_cpu(bd->pa[index].tb1_addr),
1423                                          IWL_GET_BITS(bd->pa[index], tb1_len),
1424                                          PCI_DMA_TODEVICE);
1425
1426                 /* Free SKB, if any, for this chunk */
1427                 if (txq->txb[txq->q.read_ptr].skb[i]) {
1428                         struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
1429
1430                         dev_kfree_skb(skb);
1431                         txq->txb[txq->q.read_ptr].skb[i] = NULL;
1432                 }
1433         }
1434         return 0;
1435 }
1436
1437 /* set card power command */
1438 static int iwl4965_set_power(struct iwl_priv *priv,
1439                       void *cmd)
1440 {
1441         int ret = 0;
1442
1443         ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1444                                     sizeof(struct iwl4965_powertable_cmd),
1445                                     cmd, NULL);
1446         return ret;
1447 }
1448 int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
1449 {
1450         IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
1451         return -EINVAL;
1452 }
1453
1454 static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1455 {
1456         s32 sign = 1;
1457
1458         if (num < 0) {
1459                 sign = -sign;
1460                 num = -num;
1461         }
1462         if (denom < 0) {
1463                 sign = -sign;
1464                 denom = -denom;
1465         }
1466         *res = 1;
1467         *res = ((num * 2 + denom) / (denom * 2)) * sign;
1468
1469         return 1;
1470 }
1471
1472 /**
1473  * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1474  *
1475  * Determines power supply voltage compensation for txpower calculations.
1476  * Returns number of 1/2-dB steps to subtract from gain table index,
1477  * to compensate for difference between power supply voltage during
1478  * factory measurements, vs. current power supply voltage.
1479  *
1480  * Voltage indication is higher for lower voltage.
1481  * Lower voltage requires more gain (lower gain table index).
1482  */
1483 static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1484                                             s32 current_voltage)
1485 {
1486         s32 comp = 0;
1487
1488         if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1489             (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1490                 return 0;
1491
1492         iwl4965_math_div_round(current_voltage - eeprom_voltage,
1493                                TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1494
1495         if (current_voltage > eeprom_voltage)
1496                 comp *= 2;
1497         if ((comp < -2) || (comp > 2))
1498                 comp = 0;
1499
1500         return comp;
1501 }
1502
1503 static const struct iwl_channel_info *
1504 iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
1505                                  enum ieee80211_band band, u16 channel)
1506 {
1507         const struct iwl_channel_info *ch_info;
1508
1509         ch_info = iwl_get_channel_info(priv, band, channel);
1510
1511         if (!is_channel_valid(ch_info))
1512                 return NULL;
1513
1514         return ch_info;
1515 }
1516
1517 static s32 iwl4965_get_tx_atten_grp(u16 channel)
1518 {
1519         if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1520             channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1521                 return CALIB_CH_GROUP_5;
1522
1523         if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1524             channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1525                 return CALIB_CH_GROUP_1;
1526
1527         if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1528             channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1529                 return CALIB_CH_GROUP_2;
1530
1531         if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1532             channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1533                 return CALIB_CH_GROUP_3;
1534
1535         if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1536             channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1537                 return CALIB_CH_GROUP_4;
1538
1539         IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1540         return -1;
1541 }
1542
1543 static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
1544 {
1545         s32 b = -1;
1546
1547         for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
1548                 if (priv->calib_info->band_info[b].ch_from == 0)
1549                         continue;
1550
1551                 if ((channel >= priv->calib_info->band_info[b].ch_from)
1552                     && (channel <= priv->calib_info->band_info[b].ch_to))
1553                         break;
1554         }
1555
1556         return b;
1557 }
1558
1559 static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1560 {
1561         s32 val;
1562
1563         if (x2 == x1)
1564                 return y1;
1565         else {
1566                 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1567                 return val + y2;
1568         }
1569 }
1570
1571 /**
1572  * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1573  *
1574  * Interpolates factory measurements from the two sample channels within a
1575  * sub-band, to apply to channel of interest.  Interpolation is proportional to
1576  * differences in channel frequencies, which is proportional to differences
1577  * in channel number.
1578  */
1579 static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
1580                                     struct iwl_eeprom_calib_ch_info *chan_info)
1581 {
1582         s32 s = -1;
1583         u32 c;
1584         u32 m;
1585         const struct iwl_eeprom_calib_measure *m1;
1586         const struct iwl_eeprom_calib_measure *m2;
1587         struct iwl_eeprom_calib_measure *omeas;
1588         u32 ch_i1;
1589         u32 ch_i2;
1590
1591         s = iwl4965_get_sub_band(priv, channel);
1592         if (s >= EEPROM_TX_POWER_BANDS) {
1593                 IWL_ERROR("Tx Power can not find channel %d ", channel);
1594                 return -1;
1595         }
1596
1597         ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1598         ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
1599         chan_info->ch_num = (u8) channel;
1600
1601         IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1602                           channel, s, ch_i1, ch_i2);
1603
1604         for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1605                 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
1606                         m1 = &(priv->calib_info->band_info[s].ch1.
1607                                measurements[c][m]);
1608                         m2 = &(priv->calib_info->band_info[s].ch2.
1609                                measurements[c][m]);
1610                         omeas = &(chan_info->measurements[c][m]);
1611
1612                         omeas->actual_pow =
1613                             (u8) iwl4965_interpolate_value(channel, ch_i1,
1614                                                            m1->actual_pow,
1615                                                            ch_i2,
1616                                                            m2->actual_pow);
1617                         omeas->gain_idx =
1618                             (u8) iwl4965_interpolate_value(channel, ch_i1,
1619                                                            m1->gain_idx, ch_i2,
1620                                                            m2->gain_idx);
1621                         omeas->temperature =
1622                             (u8) iwl4965_interpolate_value(channel, ch_i1,
1623                                                            m1->temperature,
1624                                                            ch_i2,
1625                                                            m2->temperature);
1626                         omeas->pa_det =
1627                             (s8) iwl4965_interpolate_value(channel, ch_i1,
1628                                                            m1->pa_det, ch_i2,
1629                                                            m2->pa_det);
1630
1631                         IWL_DEBUG_TXPOWER
1632                             ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1633                              m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1634                         IWL_DEBUG_TXPOWER
1635                             ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1636                              m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1637                         IWL_DEBUG_TXPOWER
1638                             ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1639                              m1->pa_det, m2->pa_det, omeas->pa_det);
1640                         IWL_DEBUG_TXPOWER
1641                             ("chain %d meas %d  T1=%d  T2=%d  T=%d\n", c, m,
1642                              m1->temperature, m2->temperature,
1643                              omeas->temperature);
1644                 }
1645         }
1646
1647         return 0;
1648 }
1649
1650 /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1651  * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1652 static s32 back_off_table[] = {
1653         10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1654         10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1655         10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1656         10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1657         10                      /* CCK */
1658 };
1659
1660 /* Thermal compensation values for txpower for various frequency ranges ...
1661  *   ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
1662 static struct iwl4965_txpower_comp_entry {
1663         s32 degrees_per_05db_a;
1664         s32 degrees_per_05db_a_denom;
1665 } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1666         {9, 2},                 /* group 0 5.2, ch  34-43 */
1667         {4, 1},                 /* group 1 5.2, ch  44-70 */
1668         {4, 1},                 /* group 2 5.2, ch  71-124 */
1669         {4, 1},                 /* group 3 5.2, ch 125-200 */
1670         {3, 1}                  /* group 4 2.4, ch   all */
1671 };
1672
1673 static s32 get_min_power_index(s32 rate_power_index, u32 band)
1674 {
1675         if (!band) {
1676                 if ((rate_power_index & 7) <= 4)
1677                         return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1678         }
1679         return MIN_TX_GAIN_INDEX;
1680 }
1681
1682 struct gain_entry {
1683         u8 dsp;
1684         u8 radio;
1685 };
1686
1687 static const struct gain_entry gain_table[2][108] = {
1688         /* 5.2GHz power gain index table */
1689         {
1690          {123, 0x3F},           /* highest txpower */
1691          {117, 0x3F},
1692          {110, 0x3F},
1693          {104, 0x3F},
1694          {98, 0x3F},
1695          {110, 0x3E},
1696          {104, 0x3E},
1697          {98, 0x3E},
1698          {110, 0x3D},
1699          {104, 0x3D},
1700          {98, 0x3D},
1701          {110, 0x3C},
1702          {104, 0x3C},
1703          {98, 0x3C},
1704          {110, 0x3B},
1705          {104, 0x3B},
1706          {98, 0x3B},
1707          {110, 0x3A},
1708          {104, 0x3A},
1709          {98, 0x3A},
1710          {110, 0x39},
1711          {104, 0x39},
1712          {98, 0x39},
1713          {110, 0x38},
1714          {104, 0x38},
1715          {98, 0x38},
1716          {110, 0x37},
1717          {104, 0x37},
1718          {98, 0x37},
1719          {110, 0x36},
1720          {104, 0x36},
1721          {98, 0x36},
1722          {110, 0x35},
1723          {104, 0x35},
1724          {98, 0x35},
1725          {110, 0x34},
1726          {104, 0x34},
1727          {98, 0x34},
1728          {110, 0x33},
1729          {104, 0x33},
1730          {98, 0x33},
1731          {110, 0x32},
1732          {104, 0x32},
1733          {98, 0x32},
1734          {110, 0x31},
1735          {104, 0x31},
1736          {98, 0x31},
1737          {110, 0x30},
1738          {104, 0x30},
1739          {98, 0x30},
1740          {110, 0x25},
1741          {104, 0x25},
1742          {98, 0x25},
1743          {110, 0x24},
1744          {104, 0x24},
1745          {98, 0x24},
1746          {110, 0x23},
1747          {104, 0x23},
1748          {98, 0x23},
1749          {110, 0x22},
1750          {104, 0x18},
1751          {98, 0x18},
1752          {110, 0x17},
1753          {104, 0x17},
1754          {98, 0x17},
1755          {110, 0x16},
1756          {104, 0x16},
1757          {98, 0x16},
1758          {110, 0x15},
1759          {104, 0x15},
1760          {98, 0x15},
1761          {110, 0x14},
1762          {104, 0x14},
1763          {98, 0x14},
1764          {110, 0x13},
1765          {104, 0x13},
1766          {98, 0x13},
1767          {110, 0x12},
1768          {104, 0x08},
1769          {98, 0x08},
1770          {110, 0x07},
1771          {104, 0x07},
1772          {98, 0x07},
1773          {110, 0x06},
1774          {104, 0x06},
1775          {98, 0x06},
1776          {110, 0x05},
1777          {104, 0x05},
1778          {98, 0x05},
1779          {110, 0x04},
1780          {104, 0x04},
1781          {98, 0x04},
1782          {110, 0x03},
1783          {104, 0x03},
1784          {98, 0x03},
1785          {110, 0x02},
1786          {104, 0x02},
1787          {98, 0x02},
1788          {110, 0x01},
1789          {104, 0x01},
1790          {98, 0x01},
1791          {110, 0x00},
1792          {104, 0x00},
1793          {98, 0x00},
1794          {93, 0x00},
1795          {88, 0x00},
1796          {83, 0x00},
1797          {78, 0x00},
1798          },
1799         /* 2.4GHz power gain index table */
1800         {
1801          {110, 0x3f},           /* highest txpower */
1802          {104, 0x3f},
1803          {98, 0x3f},
1804          {110, 0x3e},
1805          {104, 0x3e},
1806          {98, 0x3e},
1807          {110, 0x3d},
1808          {104, 0x3d},
1809          {98, 0x3d},
1810          {110, 0x3c},
1811          {104, 0x3c},
1812          {98, 0x3c},
1813          {110, 0x3b},
1814          {104, 0x3b},
1815          {98, 0x3b},
1816          {110, 0x3a},
1817          {104, 0x3a},
1818          {98, 0x3a},
1819          {110, 0x39},
1820          {104, 0x39},
1821          {98, 0x39},
1822          {110, 0x38},
1823          {104, 0x38},
1824          {98, 0x38},
1825          {110, 0x37},
1826          {104, 0x37},
1827          {98, 0x37},
1828          {110, 0x36},
1829          {104, 0x36},
1830          {98, 0x36},
1831          {110, 0x35},
1832          {104, 0x35},
1833          {98, 0x35},
1834          {110, 0x34},
1835          {104, 0x34},
1836          {98, 0x34},
1837          {110, 0x33},
1838          {104, 0x33},
1839          {98, 0x33},
1840          {110, 0x32},
1841          {104, 0x32},
1842          {98, 0x32},
1843          {110, 0x31},
1844          {104, 0x31},
1845          {98, 0x31},
1846          {110, 0x30},
1847          {104, 0x30},
1848          {98, 0x30},
1849          {110, 0x6},
1850          {104, 0x6},
1851          {98, 0x6},
1852          {110, 0x5},
1853          {104, 0x5},
1854          {98, 0x5},
1855          {110, 0x4},
1856          {104, 0x4},
1857          {98, 0x4},
1858          {110, 0x3},
1859          {104, 0x3},
1860          {98, 0x3},
1861          {110, 0x2},
1862          {104, 0x2},
1863          {98, 0x2},
1864          {110, 0x1},
1865          {104, 0x1},
1866          {98, 0x1},
1867          {110, 0x0},
1868          {104, 0x0},
1869          {98, 0x0},
1870          {97, 0},
1871          {96, 0},
1872          {95, 0},
1873          {94, 0},
1874          {93, 0},
1875          {92, 0},
1876          {91, 0},
1877          {90, 0},
1878          {89, 0},
1879          {88, 0},
1880          {87, 0},
1881          {86, 0},
1882          {85, 0},
1883          {84, 0},
1884          {83, 0},
1885          {82, 0},
1886          {81, 0},
1887          {80, 0},
1888          {79, 0},
1889          {78, 0},
1890          {77, 0},
1891          {76, 0},
1892          {75, 0},
1893          {74, 0},
1894          {73, 0},
1895          {72, 0},
1896          {71, 0},
1897          {70, 0},
1898          {69, 0},
1899          {68, 0},
1900          {67, 0},
1901          {66, 0},
1902          {65, 0},
1903          {64, 0},
1904          {63, 0},
1905          {62, 0},
1906          {61, 0},
1907          {60, 0},
1908          {59, 0},
1909          }
1910 };
1911
1912 static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
1913                                     u8 is_fat, u8 ctrl_chan_high,
1914                                     struct iwl4965_tx_power_db *tx_power_tbl)
1915 {
1916         u8 saturation_power;
1917         s32 target_power;
1918         s32 user_target_power;
1919         s32 power_limit;
1920         s32 current_temp;
1921         s32 reg_limit;
1922         s32 current_regulatory;
1923         s32 txatten_grp = CALIB_CH_GROUP_MAX;
1924         int i;
1925         int c;
1926         const struct iwl_channel_info *ch_info = NULL;
1927         struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1928         const struct iwl_eeprom_calib_measure *measurement;
1929         s16 voltage;
1930         s32 init_voltage;
1931         s32 voltage_compensation;
1932         s32 degrees_per_05db_num;
1933         s32 degrees_per_05db_denom;
1934         s32 factory_temp;
1935         s32 temperature_comp[2];
1936         s32 factory_gain_index[2];
1937         s32 factory_actual_pwr[2];
1938         s32 power_index;
1939
1940         /* Sanity check requested level (dBm) */
1941         if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1942                 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1943                             priv->user_txpower_limit);
1944                 return -EINVAL;
1945         }
1946         if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1947                 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1948                             priv->user_txpower_limit);
1949                 return -EINVAL;
1950         }
1951
1952         /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1953          *   are used for indexing into txpower table) */
1954         user_target_power = 2 * priv->user_txpower_limit;
1955
1956         /* Get current (RXON) channel, band, width */
1957         ch_info =
1958                 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
1959
1960         IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1961                           is_fat);
1962
1963         if (!ch_info)
1964                 return -EINVAL;
1965
1966         /* get txatten group, used to select 1) thermal txpower adjustment
1967          *   and 2) mimo txpower balance between Tx chains. */
1968         txatten_grp = iwl4965_get_tx_atten_grp(channel);
1969         if (txatten_grp < 0)
1970                 return -EINVAL;
1971
1972         IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1973                           channel, txatten_grp);
1974
1975         if (is_fat) {
1976                 if (ctrl_chan_high)
1977                         channel -= 2;
1978                 else
1979                         channel += 2;
1980         }
1981
1982         /* hardware txpower limits ...
1983          * saturation (clipping distortion) txpowers are in half-dBm */
1984         if (band)
1985                 saturation_power = priv->calib_info->saturation_power24;
1986         else
1987                 saturation_power = priv->calib_info->saturation_power52;
1988
1989         if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1990             saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1991                 if (band)
1992                         saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1993                 else
1994                         saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1995         }
1996
1997         /* regulatory txpower limits ... reg_limit values are in half-dBm,
1998          *   max_power_avg values are in dBm, convert * 2 */
1999         if (is_fat)
2000                 reg_limit = ch_info->fat_max_power_avg * 2;
2001         else
2002                 reg_limit = ch_info->max_power_avg * 2;
2003
2004         if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
2005             (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
2006                 if (band)
2007                         reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
2008                 else
2009                         reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
2010         }
2011
2012         /* Interpolate txpower calibration values for this channel,
2013          *   based on factory calibration tests on spaced channels. */
2014         iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
2015
2016         /* calculate tx gain adjustment based on power supply voltage */
2017         voltage = priv->calib_info->voltage;
2018         init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
2019         voltage_compensation =
2020             iwl4965_get_voltage_compensation(voltage, init_voltage);
2021
2022         IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
2023                           init_voltage,
2024                           voltage, voltage_compensation);
2025
2026         /* get current temperature (Celsius) */
2027         current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
2028         current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
2029         current_temp = KELVIN_TO_CELSIUS(current_temp);
2030
2031         /* select thermal txpower adjustment params, based on channel group
2032          *   (same frequency group used for mimo txatten adjustment) */
2033         degrees_per_05db_num =
2034             tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
2035         degrees_per_05db_denom =
2036             tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
2037
2038         /* get per-chain txpower values from factory measurements */
2039         for (c = 0; c < 2; c++) {
2040                 measurement = &ch_eeprom_info.measurements[c][1];
2041
2042                 /* txgain adjustment (in half-dB steps) based on difference
2043                  *   between factory and current temperature */
2044                 factory_temp = measurement->temperature;
2045                 iwl4965_math_div_round((current_temp - factory_temp) *
2046                                        degrees_per_05db_denom,
2047                                        degrees_per_05db_num,
2048                                        &temperature_comp[c]);
2049
2050                 factory_gain_index[c] = measurement->gain_idx;
2051                 factory_actual_pwr[c] = measurement->actual_pow;
2052
2053                 IWL_DEBUG_TXPOWER("chain = %d\n", c);
2054                 IWL_DEBUG_TXPOWER("fctry tmp %d, "
2055                                   "curr tmp %d, comp %d steps\n",
2056                                   factory_temp, current_temp,
2057                                   temperature_comp[c]);
2058
2059                 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
2060                                   factory_gain_index[c],
2061                                   factory_actual_pwr[c]);
2062         }
2063
2064         /* for each of 33 bit-rates (including 1 for CCK) */
2065         for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
2066                 u8 is_mimo_rate;
2067                 union iwl4965_tx_power_dual_stream tx_power;
2068
2069                 /* for mimo, reduce each chain's txpower by half
2070                  * (3dB, 6 steps), so total output power is regulatory
2071                  * compliant. */
2072                 if (i & 0x8) {
2073                         current_regulatory = reg_limit -
2074                             IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
2075                         is_mimo_rate = 1;
2076                 } else {
2077                         current_regulatory = reg_limit;
2078                         is_mimo_rate = 0;
2079                 }
2080
2081                 /* find txpower limit, either hardware or regulatory */
2082                 power_limit = saturation_power - back_off_table[i];
2083                 if (power_limit > current_regulatory)
2084                         power_limit = current_regulatory;
2085
2086                 /* reduce user's txpower request if necessary
2087                  * for this rate on this channel */
2088                 target_power = user_target_power;
2089                 if (target_power > power_limit)
2090                         target_power = power_limit;
2091
2092                 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
2093                                   i, saturation_power - back_off_table[i],
2094                                   current_regulatory, user_target_power,
2095                                   target_power);
2096
2097                 /* for each of 2 Tx chains (radio transmitters) */
2098                 for (c = 0; c < 2; c++) {
2099                         s32 atten_value;
2100
2101                         if (is_mimo_rate)
2102                                 atten_value =
2103                                     (s32)le32_to_cpu(priv->card_alive_init.
2104                                     tx_atten[txatten_grp][c]);
2105                         else
2106                                 atten_value = 0;
2107
2108                         /* calculate index; higher index means lower txpower */
2109                         power_index = (u8) (factory_gain_index[c] -
2110                                             (target_power -
2111                                              factory_actual_pwr[c]) -
2112                                             temperature_comp[c] -
2113                                             voltage_compensation +
2114                                             atten_value);
2115
2116 /*                      IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
2117                                                 power_index); */
2118
2119                         if (power_index < get_min_power_index(i, band))
2120                                 power_index = get_min_power_index(i, band);
2121
2122                         /* adjust 5 GHz index to support negative indexes */
2123                         if (!band)
2124                                 power_index += 9;
2125
2126                         /* CCK, rate 32, reduce txpower for CCK */
2127                         if (i == POWER_TABLE_CCK_ENTRY)
2128                                 power_index +=
2129                                     IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
2130
2131                         /* stay within the table! */
2132                         if (power_index > 107) {
2133                                 IWL_WARNING("txpower index %d > 107\n",
2134                                             power_index);
2135                                 power_index = 107;
2136                         }
2137                         if (power_index < 0) {
2138                                 IWL_WARNING("txpower index %d < 0\n",
2139                                             power_index);
2140                                 power_index = 0;
2141                         }
2142
2143                         /* fill txpower command for this rate/chain */
2144                         tx_power.s.radio_tx_gain[c] =
2145                                 gain_table[band][power_index].radio;
2146                         tx_power.s.dsp_predis_atten[c] =
2147                                 gain_table[band][power_index].dsp;
2148
2149                         IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
2150                                           "gain 0x%02x dsp %d\n",
2151                                           c, atten_value, power_index,
2152                                         tx_power.s.radio_tx_gain[c],
2153                                         tx_power.s.dsp_predis_atten[c]);
2154                 }/* for each chain */
2155
2156                 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
2157
2158         }/* for each rate */
2159
2160         return 0;
2161 }
2162
2163 /**
2164  * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
2165  *
2166  * Uses the active RXON for channel, band, and characteristics (fat, high)
2167  * The power limit is taken from priv->user_txpower_limit.
2168  */
2169 int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
2170 {
2171         struct iwl4965_txpowertable_cmd cmd = { 0 };
2172         int ret;
2173         u8 band = 0;
2174         u8 is_fat = 0;
2175         u8 ctrl_chan_high = 0;
2176
2177         if (test_bit(STATUS_SCANNING, &priv->status)) {
2178                 /* If this gets hit a lot, switch it to a BUG() and catch
2179                  * the stack trace to find out who is calling this during
2180                  * a scan. */
2181                 IWL_WARNING("TX Power requested while scanning!\n");
2182                 return -EAGAIN;
2183         }
2184
2185         band = priv->band == IEEE80211_BAND_2GHZ;
2186
2187         is_fat =  is_fat_channel(priv->active_rxon.flags);
2188
2189         if (is_fat &&
2190             (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2191                 ctrl_chan_high = 1;
2192
2193         cmd.band = band;
2194         cmd.channel = priv->active_rxon.channel;
2195
2196         ret = iwl4965_fill_txpower_tbl(priv, band,
2197                                 le16_to_cpu(priv->active_rxon.channel),
2198                                 is_fat, ctrl_chan_high, &cmd.tx_power);
2199         if (ret)
2200                 goto out;
2201
2202         ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
2203
2204 out:
2205         return ret;
2206 }
2207
2208 static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
2209 {
2210         int ret = 0;
2211         struct iwl4965_rxon_assoc_cmd rxon_assoc;
2212         const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
2213         const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
2214
2215         if ((rxon1->flags == rxon2->flags) &&
2216             (rxon1->filter_flags == rxon2->filter_flags) &&
2217             (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
2218             (rxon1->ofdm_ht_single_stream_basic_rates ==
2219              rxon2->ofdm_ht_single_stream_basic_rates) &&
2220             (rxon1->ofdm_ht_dual_stream_basic_rates ==
2221              rxon2->ofdm_ht_dual_stream_basic_rates) &&
2222             (rxon1->rx_chain == rxon2->rx_chain) &&
2223             (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
2224                 IWL_DEBUG_INFO("Using current RXON_ASSOC.  Not resending.\n");
2225                 return 0;
2226         }
2227
2228         rxon_assoc.flags = priv->staging_rxon.flags;
2229         rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
2230         rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
2231         rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
2232         rxon_assoc.reserved = 0;
2233         rxon_assoc.ofdm_ht_single_stream_basic_rates =
2234             priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
2235         rxon_assoc.ofdm_ht_dual_stream_basic_rates =
2236             priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
2237         rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
2238
2239         ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
2240                                      sizeof(rxon_assoc), &rxon_assoc, NULL);
2241         if (ret)
2242                 return ret;
2243
2244         return ret;
2245 }
2246
2247
2248 int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
2249 {
2250         int rc;
2251         u8 band = 0;
2252         u8 is_fat = 0;
2253         u8 ctrl_chan_high = 0;
2254         struct iwl4965_channel_switch_cmd cmd = { 0 };
2255         const struct iwl_channel_info *ch_info;
2256
2257         band = priv->band == IEEE80211_BAND_2GHZ;
2258
2259         ch_info = iwl_get_channel_info(priv, priv->band, channel);
2260
2261         is_fat = is_fat_channel(priv->staging_rxon.flags);
2262
2263         if (is_fat &&
2264             (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
2265                 ctrl_chan_high = 1;
2266
2267         cmd.band = band;
2268         cmd.expect_beacon = 0;
2269         cmd.channel = cpu_to_le16(channel);
2270         cmd.rxon_flags = priv->active_rxon.flags;
2271         cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
2272         cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
2273         if (ch_info)
2274                 cmd.expect_beacon = is_channel_radar(ch_info);
2275         else
2276                 cmd.expect_beacon = 1;
2277
2278         rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
2279                                       ctrl_chan_high, &cmd.tx_power);
2280         if (rc) {
2281                 IWL_DEBUG_11H("error:%d  fill txpower_tbl\n", rc);
2282                 return rc;
2283         }
2284
2285         rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
2286         return rc;
2287 }
2288
2289 #define RTS_HCCA_RETRY_LIMIT            3
2290 #define RTS_DFAULT_RETRY_LIMIT          60
2291
2292 void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
2293                               struct iwl_cmd *cmd,
2294                               struct ieee80211_tx_control *ctrl,
2295                               struct ieee80211_hdr *hdr, int sta_id,
2296                               int is_hcca)
2297 {
2298         struct iwl4965_tx_cmd *tx = &cmd->cmd.tx;
2299         u8 rts_retry_limit = 0;
2300         u8 data_retry_limit = 0;
2301         u16 fc = le16_to_cpu(hdr->frame_control);
2302         u8 rate_plcp;
2303         u16 rate_flags = 0;
2304         int rate_idx = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
2305
2306         rate_plcp = iwl4965_rates[rate_idx].plcp;
2307
2308         rts_retry_limit = (is_hcca) ?
2309             RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
2310
2311         if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
2312                 rate_flags |= RATE_MCS_CCK_MSK;
2313
2314
2315         if (ieee80211_is_probe_response(fc)) {
2316                 data_retry_limit = 3;
2317                 if (data_retry_limit < rts_retry_limit)
2318                         rts_retry_limit = data_retry_limit;
2319         } else
2320                 data_retry_limit = IWL_DEFAULT_TX_RETRY;
2321
2322         if (priv->data_retry_limit != -1)
2323                 data_retry_limit = priv->data_retry_limit;
2324
2325
2326         if (ieee80211_is_data(fc)) {
2327                 tx->initial_rate_index = 0;
2328                 tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
2329         } else {
2330                 switch (fc & IEEE80211_FCTL_STYPE) {
2331                 case IEEE80211_STYPE_AUTH:
2332                 case IEEE80211_STYPE_DEAUTH:
2333                 case IEEE80211_STYPE_ASSOC_REQ:
2334                 case IEEE80211_STYPE_REASSOC_REQ:
2335                         if (tx->tx_flags & TX_CMD_FLG_RTS_MSK) {
2336                                 tx->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2337                                 tx->tx_flags |= TX_CMD_FLG_CTS_MSK;
2338                         }
2339                         break;
2340                 default:
2341                         break;
2342                 }
2343
2344                 /* Alternate between antenna A and B for successive frames */
2345                 if (priv->use_ant_b_for_management_frame) {
2346                         priv->use_ant_b_for_management_frame = 0;
2347                         rate_flags |= RATE_MCS_ANT_B_MSK;
2348                 } else {
2349                         priv->use_ant_b_for_management_frame = 1;
2350                         rate_flags |= RATE_MCS_ANT_A_MSK;
2351                 }
2352         }
2353
2354         tx->rts_retry_limit = rts_retry_limit;
2355         tx->data_retry_limit = data_retry_limit;
2356         tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
2357 }
2358
2359 int iwl4965_hw_get_rx_read(struct iwl_priv *priv)
2360 {
2361         struct iwl4965_shared *s = priv->shared_virt;
2362         return le32_to_cpu(s->rb_closed) & 0xFFF;
2363 }
2364
2365 int iwl4965_hw_get_temperature(struct iwl_priv *priv)
2366 {
2367         return priv->temperature;
2368 }
2369
2370 unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
2371                           struct iwl4965_frame *frame, u8 rate)
2372 {
2373         struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
2374         unsigned int frame_size;
2375
2376         tx_beacon_cmd = &frame->u.beacon;
2377         memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
2378
2379         tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
2380         tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2381
2382         frame_size = iwl4965_fill_beacon_frame(priv,
2383                                 tx_beacon_cmd->frame,
2384                                 iwl4965_broadcast_addr,
2385                                 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
2386
2387         BUG_ON(frame_size > MAX_MPDU_SIZE);
2388         tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
2389
2390         if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
2391                 tx_beacon_cmd->tx.rate_n_flags =
2392                         iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
2393         else
2394                 tx_beacon_cmd->tx.rate_n_flags =
2395                         iwl4965_hw_set_rate_n_flags(rate, 0);
2396
2397         tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
2398                                 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
2399         return (sizeof(*tx_beacon_cmd) + frame_size);
2400 }
2401
2402 /*
2403  * Tell 4965 where to find circular buffer of Tx Frame Descriptors for
2404  * given Tx queue, and enable the DMA channel used for that queue.
2405  *
2406  * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
2407  * channels supported in hardware.
2408  */
2409 int iwl4965_hw_tx_queue_init(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
2410 {
2411         int rc;
2412         unsigned long flags;
2413         int txq_id = txq->q.id;
2414
2415         spin_lock_irqsave(&priv->lock, flags);
2416         rc = iwl_grab_nic_access(priv);
2417         if (rc) {
2418                 spin_unlock_irqrestore(&priv->lock, flags);
2419                 return rc;
2420         }
2421
2422         /* Circular buffer (TFD queue in DRAM) physical base address */
2423         iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
2424                              txq->q.dma_addr >> 8);
2425
2426         /* Enable DMA channel, using same id as for TFD queue */
2427         iwl_write_direct32(
2428                 priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
2429                 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
2430                 IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
2431         iwl_release_nic_access(priv);
2432         spin_unlock_irqrestore(&priv->lock, flags);
2433
2434         return 0;
2435 }
2436
2437 int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
2438                                  dma_addr_t addr, u16 len)
2439 {
2440         int index, is_odd;
2441         struct iwl4965_tfd_frame *tfd = ptr;
2442         u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
2443
2444         /* Each TFD can point to a maximum 20 Tx buffers */
2445         if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
2446                 IWL_ERROR("Error can not send more than %d chunks\n",
2447                           MAX_NUM_OF_TBS);
2448                 return -EINVAL;
2449         }
2450
2451         index = num_tbs / 2;
2452         is_odd = num_tbs & 0x1;
2453
2454         if (!is_odd) {
2455                 tfd->pa[index].tb1_addr = cpu_to_le32(addr);
2456                 IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
2457                              iwl_get_dma_hi_address(addr));
2458                 IWL_SET_BITS(tfd->pa[index], tb1_len, len);
2459         } else {
2460                 IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
2461                              (u32) (addr & 0xffff));
2462                 IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
2463                 IWL_SET_BITS(tfd->pa[index], tb2_len, len);
2464         }
2465
2466         IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
2467
2468         return 0;
2469 }
2470
2471 static void iwl4965_hw_card_show_info(struct iwl_priv *priv)
2472 {
2473         u16 hw_version = iwl_eeprom_query16(priv, EEPROM_4965_BOARD_REVISION);
2474
2475         IWL_DEBUG_INFO("4965ABGN HW Version %u.%u.%u\n",
2476                        ((hw_version >> 8) & 0x0F),
2477                        ((hw_version >> 8) >> 4), (hw_version & 0x00FF));
2478
2479         IWL_DEBUG_INFO("4965ABGN PBA Number %.16s\n",
2480                        &priv->eeprom[EEPROM_4965_BOARD_PBA]);
2481 }
2482
2483 static int iwl4965_alloc_shared_mem(struct iwl_priv *priv)
2484 {
2485         priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
2486                                         sizeof(struct iwl4965_shared),
2487                                         &priv->shared_phys);
2488         if (!priv->shared_virt)
2489                 return -ENOMEM;
2490
2491         memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
2492
2493         return 0;
2494 }
2495
2496 static void iwl4965_free_shared_mem(struct iwl_priv *priv)
2497 {
2498         if (priv->shared_virt)
2499                 pci_free_consistent(priv->pci_dev,
2500                                     sizeof(struct iwl4965_shared),
2501                                     priv->shared_virt,
2502                                     priv->shared_phys);
2503 }
2504
2505 #define IWL_TX_CRC_SIZE         4
2506 #define IWL_TX_DELIMITER_SIZE   4
2507
2508 /**
2509  * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
2510  */
2511 static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
2512                                             struct iwl4965_tx_queue *txq,
2513                                             u16 byte_cnt)
2514 {
2515         int len;
2516         int txq_id = txq->q.id;
2517         struct iwl4965_shared *shared_data = priv->shared_virt;
2518
2519         len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2520
2521         /* Set up byte count within first 256 entries */
2522         IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
2523                        tfd_offset[txq->q.write_ptr], byte_cnt, len);
2524
2525         /* If within first 64 entries, duplicate at end */
2526         if (txq->q.write_ptr < IWL4965_MAX_WIN_SIZE)
2527                 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
2528                         tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
2529                         byte_cnt, len);
2530 }
2531
2532 /**
2533  * iwl4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
2534  *
2535  * Selects how many and which Rx receivers/antennas/chains to use.
2536  * This should not be used for scan command ... it puts data in wrong place.
2537  */
2538 void iwl4965_set_rxon_chain(struct iwl_priv *priv)
2539 {
2540         u8 is_single = is_single_rx_stream(priv);
2541         u8 idle_state, rx_state;
2542
2543         priv->staging_rxon.rx_chain = 0;
2544         rx_state = idle_state = 3;
2545
2546         /* Tell uCode which antennas are actually connected.
2547          * Before first association, we assume all antennas are connected.
2548          * Just after first association, iwl_chain_noise_calibration()
2549          *    checks which antennas actually *are* connected. */
2550         priv->staging_rxon.rx_chain |=
2551                     cpu_to_le16(priv->hw_params.valid_rx_ant <<
2552                                                  RXON_RX_CHAIN_VALID_POS);
2553
2554         /* How many receivers should we use? */
2555         iwl4965_get_rx_chain_counter(priv, &idle_state, &rx_state);
2556         priv->staging_rxon.rx_chain |=
2557                 cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
2558         priv->staging_rxon.rx_chain |=
2559                 cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
2560
2561         if (!is_single && (rx_state >= 2) &&
2562             !test_bit(STATUS_POWER_PMI, &priv->status))
2563                 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
2564         else
2565                 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
2566
2567         IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
2568 }
2569
2570 /**
2571  * sign_extend - Sign extend a value using specified bit as sign-bit
2572  *
2573  * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
2574  * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
2575  *
2576  * @param oper value to sign extend
2577  * @param index 0 based bit index (0<=index<32) to sign bit
2578  */
2579 static s32 sign_extend(u32 oper, int index)
2580 {
2581         u8 shift = 31 - index;
2582
2583         return (s32)(oper << shift) >> shift;
2584 }
2585
2586 /**
2587  * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
2588  * @statistics: Provides the temperature reading from the uCode
2589  *
2590  * A return of <0 indicates bogus data in the statistics
2591  */
2592 int iwl4965_get_temperature(const struct iwl_priv *priv)
2593 {
2594         s32 temperature;
2595         s32 vt;
2596         s32 R1, R2, R3;
2597         u32 R4;
2598
2599         if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
2600                 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
2601                 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
2602                 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
2603                 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
2604                 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
2605                 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
2606         } else {
2607                 IWL_DEBUG_TEMP("Running temperature calibration\n");
2608                 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
2609                 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
2610                 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2611                 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2612         }
2613
2614         /*
2615          * Temperature is only 23 bits, so sign extend out to 32.
2616          *
2617          * NOTE If we haven't received a statistics notification yet
2618          * with an updated temperature, use R4 provided to us in the
2619          * "initialize" ALIVE response.
2620          */
2621         if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2622                 vt = sign_extend(R4, 23);
2623         else
2624                 vt = sign_extend(
2625                         le32_to_cpu(priv->statistics.general.temperature), 23);
2626
2627         IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2628                        R1, R2, R3, vt);
2629
2630         if (R3 == R1) {
2631                 IWL_ERROR("Calibration conflict R1 == R3\n");
2632                 return -1;
2633         }
2634
2635         /* Calculate temperature in degrees Kelvin, adjust by 97%.
2636          * Add offset to center the adjustment around 0 degrees Centigrade. */
2637         temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2638         temperature /= (R3 - R1);
2639         temperature = (temperature * 97) / 100 +
2640             TEMPERATURE_CALIB_KELVIN_OFFSET;
2641
2642         IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2643             KELVIN_TO_CELSIUS(temperature));
2644
2645         return temperature;
2646 }
2647
2648 /* Adjust Txpower only if temperature variance is greater than threshold. */
2649 #define IWL_TEMPERATURE_THRESHOLD   3
2650
2651 /**
2652  * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2653  *
2654  * If the temperature changed has changed sufficiently, then a recalibration
2655  * is needed.
2656  *
2657  * Assumes caller will replace priv->last_temperature once calibration
2658  * executed.
2659  */
2660 static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
2661 {
2662         int temp_diff;
2663
2664         if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2665                 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2666                 return 0;
2667         }
2668
2669         temp_diff = priv->temperature - priv->last_temperature;
2670
2671         /* get absolute value */
2672         if (temp_diff < 0) {
2673                 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2674                 temp_diff = -temp_diff;
2675         } else if (temp_diff == 0)
2676                 IWL_DEBUG_POWER("Same temp, \n");
2677         else
2678                 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2679
2680         if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2681                 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2682                 return 0;
2683         }
2684
2685         IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2686
2687         return 1;
2688 }
2689
2690 /* Calculate noise level, based on measurements during network silence just
2691  *   before arriving beacon.  This measurement can be done only if we know
2692  *   exactly when to expect beacons, therefore only when we're associated. */
2693 static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
2694 {
2695         struct statistics_rx_non_phy *rx_info
2696                                 = &(priv->statistics.rx.general);
2697         int num_active_rx = 0;
2698         int total_silence = 0;
2699         int bcn_silence_a =
2700                 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2701         int bcn_silence_b =
2702                 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2703         int bcn_silence_c =
2704                 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2705
2706         if (bcn_silence_a) {
2707                 total_silence += bcn_silence_a;
2708                 num_active_rx++;
2709         }
2710         if (bcn_silence_b) {
2711                 total_silence += bcn_silence_b;
2712                 num_active_rx++;
2713         }
2714         if (bcn_silence_c) {
2715                 total_silence += bcn_silence_c;
2716                 num_active_rx++;
2717         }
2718
2719         /* Average among active antennas */
2720         if (num_active_rx)
2721                 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2722         else
2723                 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2724
2725         IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2726                         bcn_silence_a, bcn_silence_b, bcn_silence_c,
2727                         priv->last_rx_noise);
2728 }
2729
2730 void iwl4965_hw_rx_statistics(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
2731 {
2732         struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
2733         int change;
2734         s32 temp;
2735
2736         IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2737                      (int)sizeof(priv->statistics), pkt->len);
2738
2739         change = ((priv->statistics.general.temperature !=
2740                    pkt->u.stats.general.temperature) ||
2741                   ((priv->statistics.flag &
2742                     STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2743                    (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2744
2745         memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2746
2747         set_bit(STATUS_STATISTICS, &priv->status);
2748
2749         /* Reschedule the statistics timer to occur in
2750          * REG_RECALIB_PERIOD seconds to ensure we get a
2751          * thermal update even if the uCode doesn't give
2752          * us one */
2753         mod_timer(&priv->statistics_periodic, jiffies +
2754                   msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2755
2756         if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2757             (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2758                 iwl4965_rx_calc_noise(priv);
2759 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
2760                 queue_work(priv->workqueue, &priv->sensitivity_work);
2761 #endif
2762         }
2763
2764         iwl_leds_background(priv);
2765
2766         /* If the hardware hasn't reported a change in
2767          * temperature then don't bother computing a
2768          * calibrated temperature value */
2769         if (!change)
2770                 return;
2771
2772         temp = iwl4965_get_temperature(priv);
2773         if (temp < 0)
2774                 return;
2775
2776         if (priv->temperature != temp) {
2777                 if (priv->temperature)
2778                         IWL_DEBUG_TEMP("Temperature changed "
2779                                        "from %dC to %dC\n",
2780                                        KELVIN_TO_CELSIUS(priv->temperature),
2781                                        KELVIN_TO_CELSIUS(temp));
2782                 else
2783                         IWL_DEBUG_TEMP("Temperature "
2784                                        "initialized to %dC\n",
2785                                        KELVIN_TO_CELSIUS(temp));
2786         }
2787
2788         priv->temperature = temp;
2789         set_bit(STATUS_TEMPERATURE, &priv->status);
2790
2791         if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2792                      iwl4965_is_temp_calib_needed(priv))
2793                 queue_work(priv->workqueue, &priv->txpower_work);
2794 }
2795
2796 static void iwl4965_add_radiotap(struct iwl_priv *priv,
2797                                  struct sk_buff *skb,
2798                                  struct iwl4965_rx_phy_res *rx_start,
2799                                  struct ieee80211_rx_status *stats,
2800                                  u32 ampdu_status)
2801 {
2802         s8 signal = stats->ssi;
2803         s8 noise = 0;
2804         int rate = stats->rate_idx;
2805         u64 tsf = stats->mactime;
2806         __le16 antenna;
2807         __le16 phy_flags_hw = rx_start->phy_flags;
2808         struct iwl4965_rt_rx_hdr {
2809                 struct ieee80211_radiotap_header rt_hdr;
2810                 __le64 rt_tsf;          /* TSF */
2811                 u8 rt_flags;            /* radiotap packet flags */
2812                 u8 rt_rate;             /* rate in 500kb/s */
2813                 __le16 rt_channelMHz;   /* channel in MHz */
2814                 __le16 rt_chbitmask;    /* channel bitfield */
2815                 s8 rt_dbmsignal;        /* signal in dBm, kluged to signed */
2816                 s8 rt_dbmnoise;
2817                 u8 rt_antenna;          /* antenna number */
2818         } __attribute__ ((packed)) *iwl4965_rt;
2819
2820         /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2821         if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2822                 if (net_ratelimit())
2823                         printk(KERN_ERR "not enough headroom [%d] for "
2824                                "radiotap head [%zd]\n",
2825                                skb_headroom(skb), sizeof(*iwl4965_rt));
2826                 return;
2827         }
2828
2829         /* put radiotap header in front of 802.11 header and data */
2830         iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2831
2832         /* initialise radiotap header */
2833         iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2834         iwl4965_rt->rt_hdr.it_pad = 0;
2835
2836         /* total header + data */
2837         put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2838                       &iwl4965_rt->rt_hdr.it_len);
2839
2840         /* Indicate all the fields we add to the radiotap header */
2841         put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2842                                   (1 << IEEE80211_RADIOTAP_FLAGS) |
2843                                   (1 << IEEE80211_RADIOTAP_RATE) |
2844                                   (1 << IEEE80211_RADIOTAP_CHANNEL) |
2845                                   (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2846                                   (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2847                                   (1 << IEEE80211_RADIOTAP_ANTENNA)),
2848                       &iwl4965_rt->rt_hdr.it_present);
2849
2850         /* Zero the flags, we'll add to them as we go */
2851         iwl4965_rt->rt_flags = 0;
2852
2853         put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2854
2855         iwl4965_rt->rt_dbmsignal = signal;
2856         iwl4965_rt->rt_dbmnoise = noise;
2857
2858         /* Convert the channel frequency and set the flags */
2859         put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2860         if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2861                 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2862                                           IEEE80211_CHAN_5GHZ),
2863                               &iwl4965_rt->rt_chbitmask);
2864         else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2865                 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2866                                           IEEE80211_CHAN_2GHZ),
2867                               &iwl4965_rt->rt_chbitmask);
2868         else    /* 802.11g */
2869                 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2870                                           IEEE80211_CHAN_2GHZ),
2871                               &iwl4965_rt->rt_chbitmask);
2872
2873         if (rate == -1)
2874                 iwl4965_rt->rt_rate = 0;
2875         else
2876                 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
2877
2878         /*
2879          * "antenna number"
2880          *
2881          * It seems that the antenna field in the phy flags value
2882          * is actually a bitfield. This is undefined by radiotap,
2883          * it wants an actual antenna number but I always get "7"
2884          * for most legacy frames I receive indicating that the
2885          * same frame was received on all three RX chains.
2886          *
2887          * I think this field should be removed in favour of a
2888          * new 802.11n radiotap field "RX chains" that is defined
2889          * as a bitmask.
2890          */
2891         antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2892         iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
2893
2894         /* set the preamble flag if appropriate */
2895         if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2896                 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2897
2898         stats->flag |= RX_FLAG_RADIOTAP;
2899 }
2900
2901 static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2902 {
2903         /* 0 - mgmt, 1 - cnt, 2 - data */
2904         int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2905         priv->rx_stats[idx].cnt++;
2906         priv->rx_stats[idx].bytes += len;
2907 }
2908
2909 /*
2910  * returns non-zero if packet should be dropped
2911  */
2912 static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2913                                       struct ieee80211_hdr *hdr,
2914                                       u32 decrypt_res,
2915                                       struct ieee80211_rx_status *stats)
2916 {
2917         u16 fc = le16_to_cpu(hdr->frame_control);
2918
2919         if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2920                 return 0;
2921
2922         if (!(fc & IEEE80211_FCTL_PROTECTED))
2923                 return 0;
2924
2925         IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2926         switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2927         case RX_RES_STATUS_SEC_TYPE_TKIP:
2928                 /* The uCode has got a bad phase 1 Key, pushes the packet.
2929                  * Decryption will be done in SW. */
2930                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2931                     RX_RES_STATUS_BAD_KEY_TTAK)
2932                         break;
2933
2934                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2935                     RX_RES_STATUS_BAD_ICV_MIC) {
2936                         /* bad ICV, the packet is destroyed since the
2937                          * decryption is inplace, drop it */
2938                         IWL_DEBUG_RX("Packet destroyed\n");
2939                         return -1;
2940                 }
2941         case RX_RES_STATUS_SEC_TYPE_WEP:
2942         case RX_RES_STATUS_SEC_TYPE_CCMP:
2943                 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2944                     RX_RES_STATUS_DECRYPT_OK) {
2945                         IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2946                         stats->flag |= RX_FLAG_DECRYPTED;
2947                 }
2948                 break;
2949
2950         default:
2951                 break;
2952         }
2953         return 0;
2954 }
2955
2956 static u32 iwl4965_translate_rx_status(u32 decrypt_in)
2957 {
2958         u32 decrypt_out = 0;
2959
2960         if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2961                                         RX_RES_STATUS_STATION_FOUND)
2962                 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2963                                 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2964
2965         decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2966
2967         /* packet was not encrypted */
2968         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2969                                         RX_RES_STATUS_SEC_TYPE_NONE)
2970                 return decrypt_out;
2971
2972         /* packet was encrypted with unknown alg */
2973         if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2974                                         RX_RES_STATUS_SEC_TYPE_ERR)
2975                 return decrypt_out;
2976
2977         /* decryption was not done in HW */
2978         if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2979                                         RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2980                 return decrypt_out;
2981
2982         switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2983
2984         case RX_RES_STATUS_SEC_TYPE_CCMP:
2985                 /* alg is CCM: check MIC only */
2986                 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2987                         /* Bad MIC */
2988                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2989                 else
2990                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2991
2992                 break;
2993
2994         case RX_RES_STATUS_SEC_TYPE_TKIP:
2995                 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2996                         /* Bad TTAK */
2997                         decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2998                         break;
2999                 }
3000                 /* fall through if TTAK OK */
3001         default:
3002                 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
3003                         decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
3004                 else
3005                         decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
3006                 break;
3007         };
3008
3009         IWL_DEBUG_RX("decrypt_in:0x%x  decrypt_out = 0x%x\n",
3010                                         decrypt_in, decrypt_out);
3011
3012         return decrypt_out;
3013 }
3014
3015 static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
3016                                        int include_phy,
3017                                        struct iwl4965_rx_mem_buffer *rxb,
3018                                        struct ieee80211_rx_status *stats)
3019 {
3020         struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
3021         struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3022             (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
3023         struct ieee80211_hdr *hdr;
3024         u16 len;
3025         __le32 *rx_end;
3026         unsigned int skblen;
3027         u32 ampdu_status;
3028         u32 ampdu_status_legacy;
3029
3030         if (!include_phy && priv->last_phy_res[0])
3031                 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3032
3033         if (!rx_start) {
3034                 IWL_ERROR("MPDU frame without a PHY data\n");
3035                 return;
3036         }
3037         if (include_phy) {
3038                 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
3039                                                rx_start->cfg_phy_cnt);
3040
3041                 len = le16_to_cpu(rx_start->byte_count);
3042
3043                 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
3044                                   sizeof(struct iwl4965_rx_phy_res) +
3045                                   rx_start->cfg_phy_cnt + len);
3046
3047         } else {
3048                 struct iwl4965_rx_mpdu_res_start *amsdu =
3049                     (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3050
3051                 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
3052                                sizeof(struct iwl4965_rx_mpdu_res_start));
3053                 len =  le16_to_cpu(amsdu->byte_count);
3054                 rx_start->byte_count = amsdu->byte_count;
3055                 rx_end = (__le32 *) (((u8 *) hdr) + len);
3056         }
3057         if (len > priv->hw_params.max_pkt_size || len < 16) {
3058                 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
3059                 return;
3060         }
3061
3062         ampdu_status = le32_to_cpu(*rx_end);
3063         skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
3064
3065         if (!include_phy) {
3066                 /* New status scheme, need to translate */
3067                 ampdu_status_legacy = ampdu_status;
3068                 ampdu_status = iwl4965_translate_rx_status(ampdu_status);
3069         }
3070
3071         /* start from MAC */
3072         skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
3073         skb_put(rxb->skb, len); /* end where data ends */
3074
3075         /* We only process data packets if the interface is open */
3076         if (unlikely(!priv->is_open)) {
3077                 IWL_DEBUG_DROP_LIMIT
3078                     ("Dropping packet while interface is not open.\n");
3079                 return;
3080         }
3081
3082         stats->flag = 0;
3083         hdr = (struct ieee80211_hdr *)rxb->skb->data;
3084
3085         /*  in case of HW accelerated crypto and bad decryption, drop */
3086         if (!priv->hw_params.sw_crypto &&
3087             iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
3088                 return;
3089
3090         if (priv->add_radiotap)
3091                 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
3092
3093         iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
3094         ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3095         priv->alloc_rxb_skb--;
3096         rxb->skb = NULL;
3097 }
3098
3099 /* Calc max signal level (dBm) among 3 possible receivers */
3100 static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
3101 {
3102         /* data from PHY/DSP regarding signal strength, etc.,
3103          *   contents are always there, not configurable by host.  */
3104         struct iwl4965_rx_non_cfg_phy *ncphy =
3105             (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
3106         u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
3107                         >> IWL_AGC_DB_POS;
3108
3109         u32 valid_antennae =
3110             (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
3111                         >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
3112         u8 max_rssi = 0;
3113         u32 i;
3114
3115         /* Find max rssi among 3 possible receivers.
3116          * These values are measured by the digital signal processor (DSP).
3117          * They should stay fairly constant even as the signal strength varies,
3118          *   if the radio's automatic gain control (AGC) is working right.
3119          * AGC value (see below) will provide the "interesting" info. */
3120         for (i = 0; i < 3; i++)
3121                 if (valid_antennae & (1 << i))
3122                         max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
3123
3124         IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
3125                 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
3126                 max_rssi, agc);
3127
3128         /* dBm = max_rssi dB - agc dB - constant.
3129          * Higher AGC (higher radio gain) means lower signal. */
3130         return (max_rssi - agc - IWL_RSSI_OFFSET);
3131 }
3132
3133 #ifdef CONFIG_IWL4965_HT
3134
3135 void iwl4965_init_ht_hw_capab(const struct iwl_priv *priv,
3136                               struct ieee80211_ht_info *ht_info,
3137                               enum ieee80211_band band)
3138 {
3139         ht_info->cap = 0;
3140         memset(ht_info->supp_mcs_set, 0, 16);
3141
3142         ht_info->ht_supported = 1;
3143
3144         if (priv->hw_params.fat_channel & BIT(band)) {
3145                 ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
3146                 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
3147                 ht_info->supp_mcs_set[4] = 0x01;
3148         }
3149         ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
3150         ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
3151         ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
3152                              (IWL_MIMO_PS_NONE << 2));
3153
3154         if (priv->cfg->mod_params->amsdu_size_8K)
3155                 ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
3156
3157         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
3158         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
3159
3160         ht_info->supp_mcs_set[0] = 0xFF;
3161         if (priv->hw_params.tx_chains_num >= 2)
3162                 ht_info->supp_mcs_set[1] = 0xFF;
3163         if (priv->hw_params.tx_chains_num >= 3)
3164                 ht_info->supp_mcs_set[2] = 0xFF;
3165 }
3166 #endif /* CONFIG_IWL4965_HT */
3167
3168 static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
3169 {
3170         unsigned long flags;
3171
3172         spin_lock_irqsave(&priv->sta_lock, flags);
3173         priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
3174         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3175         priv->stations[sta_id].sta.sta.modify_mask = 0;
3176         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3177         spin_unlock_irqrestore(&priv->sta_lock, flags);
3178
3179         iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3180 }
3181
3182 static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
3183 {
3184         /* FIXME: need locking over ps_status ??? */
3185         u8 sta_id = iwl_find_station(priv, addr);
3186
3187         if (sta_id != IWL_INVALID_STATION) {
3188                 u8 sta_awake = priv->stations[sta_id].
3189                                 ps_status == STA_PS_STATUS_WAKE;
3190
3191                 if (sta_awake && ps_bit)
3192                         priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
3193                 else if (!sta_awake && !ps_bit) {
3194                         iwl4965_sta_modify_ps_wake(priv, sta_id);
3195                         priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
3196                 }
3197         }
3198 }
3199 #ifdef CONFIG_IWLWIFI_DEBUG
3200
3201 /**
3202  * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
3203  *
3204  * You may hack this function to show different aspects of received frames,
3205  * including selective frame dumps.
3206  * group100 parameter selects whether to show 1 out of 100 good frames.
3207  *
3208  * TODO:  This was originally written for 3945, need to audit for
3209  *        proper operation with 4965.
3210  */
3211 static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
3212                       struct iwl4965_rx_packet *pkt,
3213                       struct ieee80211_hdr *header, int group100)
3214 {
3215         u32 to_us;
3216         u32 print_summary = 0;
3217         u32 print_dump = 0;     /* set to 1 to dump all frames' contents */
3218         u32 hundred = 0;
3219         u32 dataframe = 0;
3220         u16 fc;
3221         u16 seq_ctl;
3222         u16 channel;
3223         u16 phy_flags;
3224         int rate_sym;
3225         u16 length;
3226         u16 status;
3227         u16 bcn_tmr;
3228         u32 tsf_low;
3229         u64 tsf;
3230         u8 rssi;
3231         u8 agc;
3232         u16 sig_avg;
3233         u16 noise_diff;
3234         struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
3235         struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
3236         struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
3237         u8 *data = IWL_RX_DATA(pkt);
3238
3239         if (likely(!(iwl_debug_level & IWL_DL_RX)))
3240                 return;
3241
3242         /* MAC header */
3243         fc = le16_to_cpu(header->frame_control);
3244         seq_ctl = le16_to_cpu(header->seq_ctrl);
3245
3246         /* metadata */
3247         channel = le16_to_cpu(rx_hdr->channel);
3248         phy_flags = le16_to_cpu(rx_hdr->phy_flags);
3249         rate_sym = rx_hdr->rate;
3250         length = le16_to_cpu(rx_hdr->len);
3251
3252         /* end-of-frame status and timestamp */
3253         status = le32_to_cpu(rx_end->status);
3254         bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
3255         tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
3256         tsf = le64_to_cpu(rx_end->timestamp);
3257
3258         /* signal statistics */
3259         rssi = rx_stats->rssi;
3260         agc = rx_stats->agc;
3261         sig_avg = le16_to_cpu(rx_stats->sig_avg);
3262         noise_diff = le16_to_cpu(rx_stats->noise_diff);
3263
3264         to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
3265
3266         /* if data frame is to us and all is good,
3267          *   (optionally) print summary for only 1 out of every 100 */
3268         if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
3269             (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
3270                 dataframe = 1;
3271                 if (!group100)
3272                         print_summary = 1;      /* print each frame */
3273                 else if (priv->framecnt_to_us < 100) {
3274                         priv->framecnt_to_us++;
3275                         print_summary = 0;
3276                 } else {
3277                         priv->framecnt_to_us = 0;
3278                         print_summary = 1;
3279                         hundred = 1;
3280                 }
3281         } else {
3282                 /* print summary for all other frames */
3283                 print_summary = 1;
3284         }
3285
3286         if (print_summary) {
3287                 char *title;
3288                 int rate_idx;
3289                 u32 bitrate;
3290
3291                 if (hundred)
3292                         title = "100Frames";
3293                 else if (fc & IEEE80211_FCTL_RETRY)
3294                         title = "Retry";
3295                 else if (ieee80211_is_assoc_response(fc))
3296                         title = "AscRsp";
3297                 else if (ieee80211_is_reassoc_response(fc))
3298                         title = "RasRsp";
3299                 else if (ieee80211_is_probe_response(fc)) {
3300                         title = "PrbRsp";
3301                         print_dump = 1; /* dump frame contents */
3302                 } else if (ieee80211_is_beacon(fc)) {
3303                         title = "Beacon";
3304                         print_dump = 1; /* dump frame contents */
3305                 } else if (ieee80211_is_atim(fc))
3306                         title = "ATIM";
3307                 else if (ieee80211_is_auth(fc))
3308                         title = "Auth";
3309                 else if (ieee80211_is_deauth(fc))
3310                         title = "DeAuth";
3311                 else if (ieee80211_is_disassoc(fc))
3312                         title = "DisAssoc";
3313                 else
3314                         title = "Frame";
3315
3316                 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
3317                 if (unlikely(rate_idx == -1))
3318                         bitrate = 0;
3319                 else
3320                         bitrate = iwl4965_rates[rate_idx].ieee / 2;
3321
3322                 /* print frame summary.
3323                  * MAC addresses show just the last byte (for brevity),
3324                  *    but you can hack it to show more, if you'd like to. */
3325                 if (dataframe)
3326                         IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
3327                                      "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
3328                                      title, fc, header->addr1[5],
3329                                      length, rssi, channel, bitrate);
3330                 else {
3331                         /* src/dst addresses assume managed mode */
3332                         IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
3333                                      "src=0x%02x, rssi=%u, tim=%lu usec, "
3334                                      "phy=0x%02x, chnl=%d\n",
3335                                      title, fc, header->addr1[5],
3336                                      header->addr3[5], rssi,
3337                                      tsf_low - priv->scan_start_tsf,
3338                                      phy_flags, channel);
3339                 }
3340         }
3341         if (print_dump)
3342                 iwl_print_hex_dump(IWL_DL_RX, data, length);
3343 }
3344 #else
3345 static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
3346                                             struct iwl4965_rx_packet *pkt,
3347                                             struct ieee80211_hdr *header,
3348                                             int group100)
3349 {
3350 }
3351 #endif
3352
3353
3354
3355 /* Called for REPLY_RX (legacy ABG frames), or
3356  * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
3357 static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
3358                                 struct iwl4965_rx_mem_buffer *rxb)
3359 {
3360         struct ieee80211_hdr *header;
3361         struct ieee80211_rx_status rx_status;
3362         struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3363         /* Use phy data (Rx signal strength, etc.) contained within
3364          *   this rx packet for legacy frames,
3365          *   or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
3366         int include_phy = (pkt->hdr.cmd == REPLY_RX);
3367         struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
3368                 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
3369                 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
3370         __le32 *rx_end;
3371         unsigned int len = 0;
3372         u16 fc;
3373         u8 network_packet;
3374
3375         rx_status.mactime = le64_to_cpu(rx_start->timestamp);
3376         rx_status.freq =
3377                 ieee80211_frequency_to_channel(le16_to_cpu(rx_start->channel));
3378         rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
3379                                 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
3380         rx_status.rate_idx =
3381                 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
3382         if (rx_status.band == IEEE80211_BAND_5GHZ)
3383                 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
3384
3385         rx_status.antenna = 0;
3386         rx_status.flag = 0;
3387
3388         if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
3389                 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
3390                                 rx_start->cfg_phy_cnt);
3391                 return;
3392         }
3393
3394         if (!include_phy) {
3395                 if (priv->last_phy_res[0])
3396                         rx_start = (struct iwl4965_rx_phy_res *)
3397                                 &priv->last_phy_res[1];
3398                 else
3399                         rx_start = NULL;
3400         }
3401
3402         if (!rx_start) {
3403                 IWL_ERROR("MPDU frame without a PHY data\n");
3404                 return;
3405         }
3406
3407         if (include_phy) {
3408                 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
3409                                                   + rx_start->cfg_phy_cnt);
3410
3411                 len = le16_to_cpu(rx_start->byte_count);
3412                 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
3413                                   sizeof(struct iwl4965_rx_phy_res) + len);
3414         } else {
3415                 struct iwl4965_rx_mpdu_res_start *amsdu =
3416                         (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
3417
3418                 header = (void *)(pkt->u.raw +
3419                         sizeof(struct iwl4965_rx_mpdu_res_start));
3420                 len = le16_to_cpu(amsdu->byte_count);
3421                 rx_end = (__le32 *) (pkt->u.raw +
3422                         sizeof(struct iwl4965_rx_mpdu_res_start) + len);
3423         }
3424
3425         if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
3426             !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
3427                 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
3428                                 le32_to_cpu(*rx_end));
3429                 return;
3430         }
3431
3432         priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
3433
3434         /* Find max signal strength (dBm) among 3 antenna/receiver chains */
3435         rx_status.ssi = iwl4965_calc_rssi(rx_start);
3436
3437         /* Meaningful noise values are available only from beacon statistics,
3438          *   which are gathered only when associated, and indicate noise
3439          *   only for the associated network channel ...
3440          * Ignore these noise values while scanning (other channels) */
3441         if (iwl_is_associated(priv) &&
3442             !test_bit(STATUS_SCANNING, &priv->status)) {
3443                 rx_status.noise = priv->last_rx_noise;
3444                 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi,
3445                                                          rx_status.noise);
3446         } else {
3447                 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3448                 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi, 0);
3449         }
3450
3451         /* Reset beacon noise level if not associated. */
3452         if (!iwl_is_associated(priv))
3453                 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
3454
3455         /* Set "1" to report good data frames in groups of 100 */
3456         /* FIXME: need to optimze the call: */
3457         iwl4965_dbg_report_frame(priv, pkt, header, 1);
3458
3459         IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
3460                               rx_status.ssi, rx_status.noise, rx_status.signal,
3461                               (unsigned long long)rx_status.mactime);
3462
3463         network_packet = iwl4965_is_network_packet(priv, header);
3464         if (network_packet) {
3465                 priv->last_rx_rssi = rx_status.ssi;
3466                 priv->last_beacon_time =  priv->ucode_beacon_time;
3467                 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
3468         }
3469
3470         fc = le16_to_cpu(header->frame_control);
3471         switch (fc & IEEE80211_FCTL_FTYPE) {
3472         case IEEE80211_FTYPE_MGMT:
3473                 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3474                         iwl4965_update_ps_mode(priv, fc  & IEEE80211_FCTL_PM,
3475                                                 header->addr2);
3476                 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
3477                 break;
3478
3479         case IEEE80211_FTYPE_CTL:
3480 #ifdef CONFIG_IWL4965_HT
3481                 switch (fc & IEEE80211_FCTL_STYPE) {
3482                 case IEEE80211_STYPE_BACK_REQ:
3483                         IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
3484                         iwl4965_handle_data_packet(priv, 0, include_phy,
3485                                                 rxb, &rx_status);
3486                         break;
3487                 default:
3488                         break;
3489                 }
3490 #endif
3491                 break;
3492
3493         case IEEE80211_FTYPE_DATA: {
3494                 DECLARE_MAC_BUF(mac1);
3495                 DECLARE_MAC_BUF(mac2);
3496                 DECLARE_MAC_BUF(mac3);
3497
3498                 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3499                         iwl4965_update_ps_mode(priv, fc  & IEEE80211_FCTL_PM,
3500                                                 header->addr2);
3501
3502                 if (unlikely(!network_packet))
3503                         IWL_DEBUG_DROP("Dropping (non network): "
3504                                        "%s, %s, %s\n",
3505                                        print_mac(mac1, header->addr1),
3506                                        print_mac(mac2, header->addr2),
3507                                        print_mac(mac3, header->addr3));
3508                 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
3509                         IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
3510                                        print_mac(mac1, header->addr1),
3511                                        print_mac(mac2, header->addr2),
3512                                        print_mac(mac3, header->addr3));
3513                 else
3514                         iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
3515                                                    &rx_status);
3516                 break;
3517         }
3518         default:
3519                 break;
3520
3521         }
3522 }
3523
3524 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
3525  * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
3526 static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
3527                                     struct iwl4965_rx_mem_buffer *rxb)
3528 {
3529         struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3530         priv->last_phy_res[0] = 1;
3531         memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
3532                sizeof(struct iwl4965_rx_phy_res));
3533 }
3534 static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
3535                                            struct iwl4965_rx_mem_buffer *rxb)
3536
3537 {
3538 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3539         struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3540         struct iwl4965_missed_beacon_notif *missed_beacon;
3541
3542         missed_beacon = &pkt->u.missed_beacon;
3543         if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
3544                 IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
3545                     le32_to_cpu(missed_beacon->consequtive_missed_beacons),
3546                     le32_to_cpu(missed_beacon->total_missed_becons),
3547                     le32_to_cpu(missed_beacon->num_recvd_beacons),
3548                     le32_to_cpu(missed_beacon->num_expected_beacons));
3549                 if (!test_bit(STATUS_SCANNING, &priv->status))
3550                         iwl_init_sensitivity(priv);
3551         }
3552 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
3553 }
3554 #ifdef CONFIG_IWL4965_HT
3555
3556 /**
3557  * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
3558  */
3559 static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
3560                                          int sta_id, int tid)
3561 {
3562         unsigned long flags;
3563
3564         /* Remove "disable" flag, to enable Tx for this TID */
3565         spin_lock_irqsave(&priv->sta_lock, flags);
3566         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3567         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3568         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3569         spin_unlock_irqrestore(&priv->sta_lock, flags);
3570
3571         iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3572 }
3573
3574 /**
3575  * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
3576  *
3577  * Go through block-ack's bitmap of ACK'd frames, update driver's record of
3578  * ACK vs. not.  This gets sent to mac80211, then to rate scaling algo.
3579  */
3580 static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
3581                                                  struct iwl4965_ht_agg *agg,
3582                                                  struct iwl4965_compressed_ba_resp*
3583                                                  ba_resp)
3584
3585 {
3586         int i, sh, ack;
3587         u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
3588         u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3589         u64 bitmap;
3590         int successes = 0;
3591         struct ieee80211_tx_status *tx_status;
3592
3593         if (unlikely(!agg->wait_for_ba))  {
3594                 IWL_ERROR("Received BA when not expected\n");
3595                 return -EINVAL;
3596         }
3597
3598         /* Mark that the expected block-ack response arrived */
3599         agg->wait_for_ba = 0;
3600         IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
3601
3602         /* Calculate shift to align block-ack bits with our Tx window bits */
3603         sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
3604         if (sh < 0) /* tbw something is wrong with indices */
3605                 sh += 0x100;
3606
3607         /* don't use 64-bit values for now */
3608         bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
3609
3610         if (agg->frame_count > (64 - sh)) {
3611                 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
3612                 return -1;
3613         }
3614
3615         /* check for success or failure according to the
3616          * transmitted bitmap and block-ack bitmap */
3617         bitmap &= agg->bitmap;
3618
3619         /* For each frame attempted in aggregation,
3620          * update driver's record of tx frame's status. */
3621         for (i = 0; i < agg->frame_count ; i++) {
3622                 ack = bitmap & (1 << i);
3623                 successes += !!ack;
3624                 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
3625                         ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
3626                         agg->start_idx + i);
3627         }
3628
3629         tx_status = &priv->txq[scd_flow].txb[agg->start_idx].status;
3630         tx_status->flags = IEEE80211_TX_STATUS_ACK;
3631         tx_status->flags |= IEEE80211_TX_STATUS_AMPDU;
3632         tx_status->ampdu_ack_map = successes;
3633         tx_status->ampdu_ack_len = agg->frame_count;
3634         iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags,
3635                                      &tx_status->control);
3636
3637         IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
3638
3639         return 0;
3640 }
3641
3642 /**
3643  * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
3644  */
3645 static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
3646                                             u16 txq_id)
3647 {
3648         /* Simply stop the queue, but don't change any configuration;
3649          * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
3650         iwl_write_prph(priv,
3651                 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
3652                 (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
3653                 (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3654 }
3655
3656 /**
3657  * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
3658  * priv->lock must be held by the caller
3659  */
3660 static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
3661                                         u16 ssn_idx, u8 tx_fifo)
3662 {
3663         int ret = 0;
3664
3665         if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3666                 IWL_WARNING("queue number too small: %d, must be > %d\n",
3667                                 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3668                 return -EINVAL;
3669         }
3670
3671         ret = iwl_grab_nic_access(priv);
3672         if (ret)
3673                 return ret;
3674
3675         iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3676
3677         iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3678
3679         priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3680         priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3681         /* supposes that ssn_idx is valid (!= 0xFFF) */
3682         iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3683
3684         iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3685         iwl4965_txq_ctx_deactivate(priv, txq_id);
3686         iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3687
3688         iwl_release_nic_access(priv);
3689
3690         return 0;
3691 }
3692
3693 int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
3694                                          u8 tid, int txq_id)
3695 {
3696         struct iwl4965_queue *q = &priv->txq[txq_id].q;
3697         u8 *addr = priv->stations[sta_id].sta.sta.addr;
3698         struct iwl4965_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3699
3700         switch (priv->stations[sta_id].tid[tid].agg.state) {
3701         case IWL_EMPTYING_HW_QUEUE_DELBA:
3702                 /* We are reclaiming the last packet of the */
3703                 /* aggregated HW queue */
3704                 if (txq_id  == tid_data->agg.txq_id &&
3705                     q->read_ptr == q->write_ptr) {
3706                         u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3707                         int tx_fifo = default_tid_to_tx_fifo[tid];
3708                         IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3709                         iwl4965_tx_queue_agg_disable(priv, txq_id,
3710                                                      ssn, tx_fifo);
3711                         tid_data->agg.state = IWL_AGG_OFF;
3712                         ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3713                 }
3714                 break;
3715         case IWL_EMPTYING_HW_QUEUE_ADDBA:
3716                 /* We are reclaiming the last packet of the queue */
3717                 if (tid_data->tfds_in_queue == 0) {
3718                         IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3719                         tid_data->agg.state = IWL_AGG_ON;
3720                         ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3721                 }
3722                 break;
3723         }
3724         return 0;
3725 }
3726
3727 /**
3728  * iwl4965_queue_dec_wrap - Decrement queue index, wrap back to end if needed
3729  * @index -- current index
3730  * @n_bd -- total number of entries in queue (s/b power of 2)
3731  */
3732 static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
3733 {
3734         return (index == 0) ? n_bd - 1 : index - 1;
3735 }
3736
3737 /**
3738  * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3739  *
3740  * Handles block-acknowledge notification from device, which reports success
3741  * of frames sent via aggregation.
3742  */
3743 static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
3744                                            struct iwl4965_rx_mem_buffer *rxb)
3745 {
3746         struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3747         struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
3748         int index;
3749         struct iwl4965_tx_queue *txq = NULL;
3750         struct iwl4965_ht_agg *agg;
3751         DECLARE_MAC_BUF(mac);
3752
3753         /* "flow" corresponds to Tx queue */
3754         u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3755
3756         /* "ssn" is start of block-ack Tx window, corresponds to index
3757          * (in Tx queue's circular buffer) of first TFD/frame in window */
3758         u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3759
3760         if (scd_flow >= priv->hw_params.max_txq_num) {
3761                 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3762                 return;
3763         }
3764
3765         txq = &priv->txq[scd_flow];
3766         agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
3767
3768         /* Find index just before block-ack window */
3769         index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
3770
3771         /* TODO: Need to get this copy more safely - now good for debug */
3772
3773         IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3774                            "sta_id = %d\n",
3775                            agg->wait_for_ba,
3776                            print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
3777                            ba_resp->sta_id);
3778         IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
3779                            "%d, scd_ssn = %d\n",
3780                            ba_resp->tid,
3781                            ba_resp->seq_ctl,
3782                            (unsigned long long)le64_to_cpu(ba_resp->bitmap),
3783                            ba_resp->scd_flow,
3784                            ba_resp->scd_ssn);
3785         IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
3786                            agg->start_idx,
3787                            (unsigned long long)agg->bitmap);
3788
3789         /* Update driver's record of ACK vs. not for each frame in window */
3790         iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
3791
3792         /* Release all TFDs before the SSN, i.e. all TFDs in front of
3793          * block-ack window (we assume that they've been successfully
3794          * transmitted ... if not, it's too late anyway). */
3795         if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3796                 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3797                 priv->stations[ba_resp->sta_id].
3798                         tid[ba_resp->tid].tfds_in_queue -= freed;
3799                 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3800                         priv->mac80211_registered &&
3801                         agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3802                         ieee80211_wake_queue(priv->hw, scd_flow);
3803                 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3804                         ba_resp->tid, scd_flow);
3805         }
3806 }
3807
3808 /**
3809  * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3810  */
3811 static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
3812                                         u16 txq_id)
3813 {
3814         u32 tbl_dw_addr;
3815         u32 tbl_dw;
3816         u16 scd_q2ratid;
3817
3818         scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3819
3820         tbl_dw_addr = priv->scd_base_addr +
3821                         SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3822
3823         tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
3824
3825         if (txq_id & 0x1)
3826                 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3827         else
3828                 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3829
3830         iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
3831
3832         return 0;
3833 }
3834
3835
3836 /**
3837  * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3838  *
3839  * NOTE:  txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3840  *        i.e. it must be one of the higher queues used for aggregation
3841  */
3842 static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
3843                                        int tx_fifo, int sta_id, int tid,
3844                                        u16 ssn_idx)
3845 {
3846         unsigned long flags;
3847         int rc;
3848         u16 ra_tid;
3849
3850         if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3851                 IWL_WARNING("queue number too small: %d, must be > %d\n",
3852                         txq_id, IWL_BACK_QUEUE_FIRST_ID);
3853
3854         ra_tid = BUILD_RAxTID(sta_id, tid);
3855
3856         /* Modify device's station table to Tx this TID */
3857         iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
3858
3859         spin_lock_irqsave(&priv->lock, flags);
3860         rc = iwl_grab_nic_access(priv);
3861         if (rc) {
3862                 spin_unlock_irqrestore(&priv->lock, flags);
3863                 return rc;
3864         }
3865
3866         /* Stop this Tx queue before configuring it */
3867         iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3868
3869         /* Map receiver-address / traffic-ID to this queue */
3870         iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3871
3872         /* Set this queue as a chain-building queue */
3873         iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3874
3875         /* Place first TFD at index corresponding to start sequence number.
3876          * Assumes that ssn_idx is valid (!= 0xFFF) */
3877         priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3878         priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3879         iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3880
3881         /* Set up Tx window size and frame limit for this queue */
3882         iwl_write_targ_mem(priv,
3883                         priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3884                         (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3885                         SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3886
3887         iwl_write_targ_mem(priv, priv->scd_base_addr +
3888                         SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3889                         (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3890                         & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3891
3892         iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3893
3894         /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
3895         iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3896
3897         iwl_release_nic_access(priv);
3898         spin_unlock_irqrestore(&priv->lock, flags);
3899
3900         return 0;
3901 }
3902
3903 #endif /* CONFIG_IWL4965_HT */
3904
3905 /**
3906  * iwl4965_add_station - Initialize a station's hardware rate table
3907  *
3908  * The uCode's station table contains a table of fallback rates
3909  * for automatic fallback during transmission.
3910  *
3911  * NOTE: This sets up a default set of values.  These will be replaced later
3912  *       if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
3913  *       rc80211_simple.
3914  *
3915  * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
3916  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
3917  *       which requires station table entry to exist).
3918  */
3919 void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
3920 {
3921         int i, r;
3922         struct iwl_link_quality_cmd link_cmd = {
3923                 .reserved1 = 0,
3924         };
3925         u16 rate_flags;
3926
3927         /* Set up the rate scaling to start at selected rate, fall back
3928          * all the way down to 1M in IEEE order, and then spin on 1M */
3929         if (is_ap)
3930                 r = IWL_RATE_54M_INDEX;
3931         else if (priv->band == IEEE80211_BAND_5GHZ)
3932                 r = IWL_RATE_6M_INDEX;
3933         else
3934                 r = IWL_RATE_1M_INDEX;
3935
3936         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3937                 rate_flags = 0;
3938                 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
3939                         rate_flags |= RATE_MCS_CCK_MSK;
3940
3941                 /* Use Tx antenna B only */
3942                 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
3943
3944                 link_cmd.rs_table[i].rate_n_flags =
3945                         iwl4965_hw_set_rate_n_flags(iwl4965_rates[r].plcp, rate_flags);
3946                 r = iwl4965_get_prev_ieee_rate(r);
3947         }
3948
3949         link_cmd.general_params.single_stream_ant_msk = 2;
3950         link_cmd.general_params.dual_stream_ant_msk = 3;
3951         link_cmd.agg_params.agg_dis_start_th = 3;
3952         link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
3953
3954         /* Update the rate scaling for control frame Tx to AP */
3955         link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
3956
3957         iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
3958                                sizeof(link_cmd), &link_cmd, NULL);
3959 }
3960
3961 #ifdef CONFIG_IWL4965_HT
3962
3963 static u8 iwl4965_is_channel_extension(struct iwl_priv *priv,
3964                                        enum ieee80211_band band,
3965                                        u16 channel, u8 extension_chan_offset)
3966 {
3967         const struct iwl_channel_info *ch_info;
3968
3969         ch_info = iwl_get_channel_info(priv, band, channel);
3970         if (!is_channel_valid(ch_info))
3971                 return 0;
3972
3973         if (extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE)
3974                 return 0;
3975
3976         if ((ch_info->fat_extension_channel == extension_chan_offset) ||
3977             (ch_info->fat_extension_channel == HT_IE_EXT_CHANNEL_MAX))
3978                 return 1;
3979
3980         return 0;
3981 }
3982
3983 static u8 iwl4965_is_fat_tx_allowed(struct iwl_priv *priv,
3984                                 struct ieee80211_ht_info *sta_ht_inf)
3985 {
3986         struct iwl_ht_info *iwl_ht_conf = &priv->current_ht_config;
3987
3988         if ((!iwl_ht_conf->is_ht) ||
3989            (iwl_ht_conf->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ) ||
3990            (iwl_ht_conf->extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_NONE))
3991                 return 0;
3992
3993         if (sta_ht_inf) {
3994                 if ((!sta_ht_inf->ht_supported) ||
3995                    (!(sta_ht_inf->cap & IEEE80211_HT_CAP_SUP_WIDTH)))
3996                         return 0;
3997         }
3998
3999         return (iwl4965_is_channel_extension(priv, priv->band,
4000                                          iwl_ht_conf->control_channel,
4001                                          iwl_ht_conf->extension_chan_offset));
4002 }
4003
4004 void iwl4965_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_info *ht_info)
4005 {
4006         struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
4007         u32 val;
4008
4009         if (!ht_info->is_ht)
4010                 return;
4011
4012         /* Set up channel bandwidth:  20 MHz only, or 20/40 mixed if fat ok */
4013         if (iwl4965_is_fat_tx_allowed(priv, NULL))
4014                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
4015         else
4016                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
4017                                  RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
4018
4019         if (le16_to_cpu(rxon->channel) != ht_info->control_channel) {
4020                 IWL_DEBUG_ASSOC("control diff than current %d %d\n",
4021                                 le16_to_cpu(rxon->channel),
4022                                 ht_info->control_channel);
4023                 rxon->channel = cpu_to_le16(ht_info->control_channel);
4024                 return;
4025         }
4026
4027         /* Note: control channel is opposite of extension channel */
4028         switch (ht_info->extension_chan_offset) {
4029         case IWL_EXT_CHANNEL_OFFSET_ABOVE:
4030                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
4031                 break;
4032         case IWL_EXT_CHANNEL_OFFSET_BELOW:
4033                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
4034                 break;
4035         case IWL_EXT_CHANNEL_OFFSET_NONE:
4036         default:
4037                 rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
4038                 break;
4039         }
4040
4041         val = ht_info->ht_protection;
4042
4043         rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
4044
4045         iwl4965_set_rxon_chain(priv);
4046
4047         IWL_DEBUG_ASSOC("supported HT rate 0x%X 0x%X 0x%X "
4048                         "rxon flags 0x%X operation mode :0x%X "
4049                         "extension channel offset 0x%x "
4050                         "control chan %d\n",
4051                         ht_info->supp_mcs_set[0],
4052                         ht_info->supp_mcs_set[1],
4053                         ht_info->supp_mcs_set[2],
4054                         le32_to_cpu(rxon->flags), ht_info->ht_protection,
4055                         ht_info->extension_chan_offset,
4056                         ht_info->control_channel);
4057         return;
4058 }
4059
4060 void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
4061                                 struct ieee80211_ht_info *sta_ht_inf)
4062 {
4063         __le32 sta_flags;
4064         u8 mimo_ps_mode;
4065
4066         if (!sta_ht_inf || !sta_ht_inf->ht_supported)
4067                 goto done;
4068
4069         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
4070
4071         sta_flags = priv->stations[index].sta.station_flags;
4072
4073         sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
4074
4075         switch (mimo_ps_mode) {
4076         case WLAN_HT_CAP_MIMO_PS_STATIC:
4077                 sta_flags |= STA_FLG_MIMO_DIS_MSK;
4078                 break;
4079         case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
4080                 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
4081                 break;
4082         case WLAN_HT_CAP_MIMO_PS_DISABLED:
4083                 break;
4084         default:
4085                 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
4086                 break;
4087         }
4088
4089         sta_flags |= cpu_to_le32(
4090               (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
4091
4092         sta_flags |= cpu_to_le32(
4093               (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
4094
4095         if (iwl4965_is_fat_tx_allowed(priv, sta_ht_inf))
4096                 sta_flags |= STA_FLG_FAT_EN_MSK;
4097         else
4098                 sta_flags &= ~STA_FLG_FAT_EN_MSK;
4099
4100         priv->stations[index].sta.station_flags = sta_flags;
4101  done:
4102         return;
4103 }
4104
4105 static int iwl4965_rx_agg_start(struct iwl_priv *priv,
4106                                 const u8 *addr, int tid, u16 ssn)
4107 {
4108         unsigned long flags;
4109         int sta_id;
4110
4111         sta_id = iwl_find_station(priv, addr);
4112         if (sta_id == IWL_INVALID_STATION)
4113                 return -ENXIO;
4114
4115         spin_lock_irqsave(&priv->sta_lock, flags);
4116         priv->stations[sta_id].sta.station_flags_msk = 0;
4117         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
4118         priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
4119         priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
4120         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4121         spin_unlock_irqrestore(&priv->sta_lock, flags);
4122
4123         return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4124                                         CMD_ASYNC);
4125 }
4126
4127 static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
4128                                const u8 *addr, int tid)
4129 {
4130         unsigned long flags;
4131         int sta_id;
4132
4133         sta_id = iwl_find_station(priv, addr);
4134         if (sta_id == IWL_INVALID_STATION)
4135                 return -ENXIO;
4136
4137         spin_lock_irqsave(&priv->sta_lock, flags);
4138         priv->stations[sta_id].sta.station_flags_msk = 0;
4139         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
4140         priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
4141         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4142         spin_unlock_irqrestore(&priv->sta_lock, flags);
4143
4144         return iwl4965_send_add_station(priv, &priv->stations[sta_id].sta,
4145                                         CMD_ASYNC);
4146 }
4147
4148 /*
4149  * Find first available (lowest unused) Tx Queue, mark it "active".
4150  * Called only when finding queue for aggregation.
4151  * Should never return anything < 7, because they should already
4152  * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
4153  */
4154 static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
4155 {
4156         int txq_id;
4157
4158         for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
4159                 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
4160                         return txq_id;
4161         return -1;
4162 }
4163
4164 static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
4165                                 u16 tid, u16 *start_seq_num)
4166 {
4167         struct iwl_priv *priv = hw->priv;
4168         int sta_id;
4169         int tx_fifo;
4170         int txq_id;
4171         int ssn = -1;
4172         int ret = 0;
4173         unsigned long flags;
4174         struct iwl4965_tid_data *tid_data;
4175         DECLARE_MAC_BUF(mac);
4176
4177         if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4178                 tx_fifo = default_tid_to_tx_fifo[tid];
4179         else
4180                 return -EINVAL;
4181
4182         IWL_WARNING("%s on ra = %s tid = %d\n",
4183                         __func__, print_mac(mac, ra), tid);
4184
4185         sta_id = iwl_find_station(priv, ra);
4186         if (sta_id == IWL_INVALID_STATION)
4187                 return -ENXIO;
4188
4189         if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
4190                 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
4191                 return -ENXIO;
4192         }
4193
4194         txq_id = iwl4965_txq_ctx_activate_free(priv);
4195         if (txq_id == -1)
4196                 return -ENXIO;
4197
4198         spin_lock_irqsave(&priv->sta_lock, flags);
4199         tid_data = &priv->stations[sta_id].tid[tid];
4200         ssn = SEQ_TO_SN(tid_data->seq_number);
4201         tid_data->agg.txq_id = txq_id;
4202         spin_unlock_irqrestore(&priv->sta_lock, flags);
4203
4204         *start_seq_num = ssn;
4205         ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
4206                                           sta_id, tid, ssn);
4207         if (ret)
4208                 return ret;
4209
4210         ret = 0;
4211         if (tid_data->tfds_in_queue == 0) {
4212                 printk(KERN_ERR "HW queue is empty\n");
4213                 tid_data->agg.state = IWL_AGG_ON;
4214                 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
4215         } else {
4216                 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
4217                                 tid_data->tfds_in_queue);
4218                 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
4219         }
4220         return ret;
4221 }
4222
4223 static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
4224 {
4225         struct iwl_priv *priv = hw->priv;
4226         int tx_fifo_id, txq_id, sta_id, ssn = -1;
4227         struct iwl4965_tid_data *tid_data;
4228         int ret, write_ptr, read_ptr;
4229         unsigned long flags;
4230         DECLARE_MAC_BUF(mac);
4231
4232         if (!ra) {
4233                 IWL_ERROR("ra = NULL\n");
4234                 return -EINVAL;
4235         }
4236
4237         if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
4238                 tx_fifo_id = default_tid_to_tx_fifo[tid];
4239         else
4240                 return -EINVAL;
4241
4242         sta_id = iwl_find_station(priv, ra);
4243
4244         if (sta_id == IWL_INVALID_STATION)
4245                 return -ENXIO;
4246
4247         if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
4248                 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
4249
4250         tid_data = &priv->stations[sta_id].tid[tid];
4251         ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
4252         txq_id = tid_data->agg.txq_id;
4253         write_ptr = priv->txq[txq_id].q.write_ptr;
4254         read_ptr = priv->txq[txq_id].q.read_ptr;
4255
4256         /* The queue is not empty */
4257         if (write_ptr != read_ptr) {
4258                 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
4259                 priv->stations[sta_id].tid[tid].agg.state =
4260                                 IWL_EMPTYING_HW_QUEUE_DELBA;
4261                 return 0;
4262         }
4263
4264         IWL_DEBUG_HT("HW queue is empty\n");
4265         priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
4266
4267         spin_lock_irqsave(&priv->lock, flags);
4268         ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
4269         spin_unlock_irqrestore(&priv->lock, flags);
4270
4271         if (ret)
4272                 return ret;
4273
4274         ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
4275
4276         return 0;
4277 }
4278
4279 int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
4280                              enum ieee80211_ampdu_mlme_action action,
4281                              const u8 *addr, u16 tid, u16 *ssn)
4282 {
4283         struct iwl_priv *priv = hw->priv;
4284         DECLARE_MAC_BUF(mac);
4285
4286         IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
4287                      print_mac(mac, addr), tid);
4288
4289         switch (action) {
4290         case IEEE80211_AMPDU_RX_START:
4291                 IWL_DEBUG_HT("start Rx\n");
4292                 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
4293         case IEEE80211_AMPDU_RX_STOP:
4294                 IWL_DEBUG_HT("stop Rx\n");
4295                 return iwl4965_rx_agg_stop(priv, addr, tid);
4296         case IEEE80211_AMPDU_TX_START:
4297                 IWL_DEBUG_HT("start Tx\n");
4298                 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
4299         case IEEE80211_AMPDU_TX_STOP:
4300                 IWL_DEBUG_HT("stop Tx\n");
4301                 return iwl4965_tx_agg_stop(hw, addr, tid);
4302         default:
4303                 IWL_DEBUG_HT("unknown\n");
4304                 return -EINVAL;
4305                 break;
4306         }
4307         return 0;
4308 }
4309
4310 #endif /* CONFIG_IWL4965_HT */
4311
4312 /* Set up 4965-specific Rx frame reply handlers */
4313 void iwl4965_hw_rx_handler_setup(struct iwl_priv *priv)
4314 {
4315         /* Legacy Rx frames */
4316         priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
4317
4318         /* High-throughput (HT) Rx frames */
4319         priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
4320         priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
4321
4322         priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
4323             iwl4965_rx_missed_beacon_notif;
4324
4325 #ifdef CONFIG_IWL4965_HT
4326         priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
4327 #endif /* CONFIG_IWL4965_HT */
4328 }
4329
4330 void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
4331 {
4332         INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
4333 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
4334         INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
4335 #endif
4336         init_timer(&priv->statistics_periodic);
4337         priv->statistics_periodic.data = (unsigned long)priv;
4338         priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
4339 }
4340
4341 void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
4342 {
4343         del_timer_sync(&priv->statistics_periodic);
4344
4345         cancel_delayed_work(&priv->init_alive_start);
4346 }
4347
4348
4349 static struct iwl_hcmd_ops iwl4965_hcmd = {
4350         .rxon_assoc = iwl4965_send_rxon_assoc,
4351 };
4352
4353 static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
4354         .enqueue_hcmd = iwl4965_enqueue_hcmd,
4355 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
4356         .chain_noise_reset = iwl4965_chain_noise_reset,
4357         .gain_computation = iwl4965_gain_computation,
4358 #endif
4359 };
4360
4361 static struct iwl_lib_ops iwl4965_lib = {
4362         .init_drv = iwl4965_init_drv,
4363         .set_hw_params = iwl4965_hw_set_hw_params,
4364         .alloc_shared_mem = iwl4965_alloc_shared_mem,
4365         .free_shared_mem = iwl4965_free_shared_mem,
4366         .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
4367         .hw_nic_init = iwl4965_hw_nic_init,
4368         .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
4369         .alive_notify = iwl4965_alive_notify,
4370         .load_ucode = iwl4965_load_bsm,
4371         .apm_ops = {
4372                 .init = iwl4965_apm_init,
4373                 .set_pwr_src = iwl4965_set_pwr_src,
4374         },
4375         .eeprom_ops = {
4376                 .regulatory_bands = {
4377                         EEPROM_REGULATORY_BAND_1_CHANNELS,
4378                         EEPROM_REGULATORY_BAND_2_CHANNELS,
4379                         EEPROM_REGULATORY_BAND_3_CHANNELS,
4380                         EEPROM_REGULATORY_BAND_4_CHANNELS,
4381                         EEPROM_REGULATORY_BAND_5_CHANNELS,
4382                         EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
4383                         EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
4384                 },
4385                 .verify_signature  = iwlcore_eeprom_verify_signature,
4386                 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
4387                 .release_semaphore = iwlcore_eeprom_release_semaphore,
4388                 .check_version = iwl4965_eeprom_check_version,
4389                 .query_addr = iwlcore_eeprom_query_addr,
4390         },
4391         .radio_kill_sw = iwl4965_radio_kill_sw,
4392         .set_power = iwl4965_set_power,
4393         .update_chain_flags = iwl4965_update_chain_flags,
4394 };
4395
4396 static struct iwl_ops iwl4965_ops = {
4397         .lib = &iwl4965_lib,
4398         .hcmd = &iwl4965_hcmd,
4399         .utils = &iwl4965_hcmd_utils,
4400 };
4401
4402 struct iwl_cfg iwl4965_agn_cfg = {
4403         .name = "4965AGN",
4404         .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
4405         .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
4406         .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
4407         .ops = &iwl4965_ops,
4408         .mod_params = &iwl4965_mod_params,
4409 };
4410
4411 module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
4412 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
4413 module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
4414 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
4415 module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
4416 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
4417 module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
4418 MODULE_PARM_DESC(debug, "debug output mask");
4419 module_param_named(
4420         disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
4421 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
4422
4423 module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
4424 MODULE_PARM_DESC(queues_num, "number of hw queues.");
4425
4426 /* QoS */
4427 module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
4428 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
4429 module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
4430 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
4431