1 /******************************************************************************
3 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
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.
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
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
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
25 *****************************************************************************/
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>
41 #include "iwl-eeprom.h"
45 #include "iwl-helpers.h"
46 #include "iwl-calib.h"
48 /* module parameters */
49 static struct iwl_mod_params iwl4965_mod_params = {
50 .num_of_queues = IWL49_NUM_QUEUES,
54 /* the rest are 0 by default */
57 #ifdef CONFIG_IWL4965_HT
59 static const u16 default_tid_to_tx_fifo[] = {
79 #endif /*CONFIG_IWL4965_HT */
81 /* check contents of special bootstrap uCode SRAM */
82 static int iwl4965_verify_bsm(struct iwl_priv *priv)
84 __le32 *image = priv->ucode_boot.v_addr;
85 u32 len = priv->ucode_boot.len;
89 IWL_DEBUG_INFO("Begin verify bsm\n");
91 /* verify BSM SRAM contents */
92 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
93 for (reg = BSM_SRAM_LOWER_BOUND;
94 reg < BSM_SRAM_LOWER_BOUND + len;
95 reg += sizeof(u32), image++) {
96 val = iwl_read_prph(priv, reg);
97 if (val != le32_to_cpu(*image)) {
98 IWL_ERROR("BSM uCode verification failed at "
99 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
100 BSM_SRAM_LOWER_BOUND,
101 reg - BSM_SRAM_LOWER_BOUND, len,
102 val, le32_to_cpu(*image));
107 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
113 * iwl4965_load_bsm - Load bootstrap instructions
117 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
118 * in special SRAM that does not power down during RFKILL. When powering back
119 * up after power-saving sleeps (or during initial uCode load), the BSM loads
120 * the bootstrap program into the on-board processor, and starts it.
122 * The bootstrap program loads (via DMA) instructions and data for a new
123 * program from host DRAM locations indicated by the host driver in the
124 * BSM_DRAM_* registers. Once the new program is loaded, it starts
127 * When initializing the NIC, the host driver points the BSM to the
128 * "initialize" uCode image. This uCode sets up some internal data, then
129 * notifies host via "initialize alive" that it is complete.
131 * The host then replaces the BSM_DRAM_* pointer values to point to the
132 * normal runtime uCode instructions and a backup uCode data cache buffer
133 * (filled initially with starting data values for the on-board processor),
134 * then triggers the "initialize" uCode to load and launch the runtime uCode,
135 * which begins normal operation.
137 * When doing a power-save shutdown, runtime uCode saves data SRAM into
138 * the backup data cache in DRAM before SRAM is powered down.
140 * When powering back up, the BSM loads the bootstrap program. This reloads
141 * the runtime uCode instructions and the backup data cache into SRAM,
142 * and re-launches the runtime uCode from where it left off.
144 static int iwl4965_load_bsm(struct iwl_priv *priv)
146 __le32 *image = priv->ucode_boot.v_addr;
147 u32 len = priv->ucode_boot.len;
157 IWL_DEBUG_INFO("Begin load bsm\n");
159 priv->ucode_type = UCODE_RT;
161 /* make sure bootstrap program is no larger than BSM's SRAM size */
162 if (len > IWL_MAX_BSM_SIZE)
165 /* Tell bootstrap uCode where to find the "Initialize" uCode
166 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
167 * NOTE: iwl_init_alive_start() will replace these values,
168 * after the "initialize" uCode has run, to point to
169 * runtime/protocol instructions and backup data cache.
171 pinst = priv->ucode_init.p_addr >> 4;
172 pdata = priv->ucode_init_data.p_addr >> 4;
173 inst_len = priv->ucode_init.len;
174 data_len = priv->ucode_init_data.len;
176 ret = iwl_grab_nic_access(priv);
180 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
181 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
182 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
183 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
185 /* Fill BSM memory with bootstrap instructions */
186 for (reg_offset = BSM_SRAM_LOWER_BOUND;
187 reg_offset < BSM_SRAM_LOWER_BOUND + len;
188 reg_offset += sizeof(u32), image++)
189 _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
191 ret = iwl4965_verify_bsm(priv);
193 iwl_release_nic_access(priv);
197 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
198 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
199 iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
200 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
202 /* Load bootstrap code into instruction SRAM now,
203 * to prepare to load "initialize" uCode */
204 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
206 /* Wait for load of bootstrap uCode to finish */
207 for (i = 0; i < 100; i++) {
208 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
209 if (!(done & BSM_WR_CTRL_REG_BIT_START))
214 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
216 IWL_ERROR("BSM write did not complete!\n");
220 /* Enable future boot loads whenever power management unit triggers it
221 * (e.g. when powering back up after power-save shutdown) */
222 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
224 iwl_release_nic_access(priv);
230 * iwl4965_set_ucode_ptrs - Set uCode address location
232 * Tell initialization uCode where to find runtime uCode.
234 * BSM registers initially contain pointers to initialization uCode.
235 * We need to replace them to load runtime uCode inst and data,
236 * and to save runtime data when powering down.
238 static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
245 /* bits 35:4 for 4965 */
246 pinst = priv->ucode_code.p_addr >> 4;
247 pdata = priv->ucode_data_backup.p_addr >> 4;
249 spin_lock_irqsave(&priv->lock, flags);
250 ret = iwl_grab_nic_access(priv);
252 spin_unlock_irqrestore(&priv->lock, flags);
256 /* Tell bootstrap uCode where to find image to load */
257 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
258 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
259 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
260 priv->ucode_data.len);
262 /* Inst bytecount must be last to set up, bit 31 signals uCode
263 * that all new ptr/size info is in place */
264 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
265 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
266 iwl_release_nic_access(priv);
268 spin_unlock_irqrestore(&priv->lock, flags);
270 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
276 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
278 * Called after REPLY_ALIVE notification received from "initialize" uCode.
280 * The 4965 "initialize" ALIVE reply contains calibration data for:
281 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
282 * (3945 does not contain this data).
284 * Tell "initialize" uCode to go ahead and load the runtime uCode.
286 static void iwl4965_init_alive_start(struct iwl_priv *priv)
288 /* Check alive response for "valid" sign from uCode */
289 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
290 /* We had an error bringing up the hardware, so take it
291 * all the way back down so we can try again */
292 IWL_DEBUG_INFO("Initialize Alive failed.\n");
296 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
297 * This is a paranoid check, because we would not have gotten the
298 * "initialize" alive if code weren't properly loaded. */
299 if (iwl_verify_ucode(priv)) {
300 /* Runtime instruction load was bad;
301 * take it all the way back down so we can try again */
302 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
306 /* Calculate temperature */
307 priv->temperature = iwl4965_get_temperature(priv);
309 /* Send pointers to protocol/runtime uCode image ... init code will
310 * load and launch runtime uCode, which will send us another "Alive"
312 IWL_DEBUG_INFO("Initialization Alive received.\n");
313 if (iwl4965_set_ucode_ptrs(priv)) {
314 /* Runtime instruction load won't happen;
315 * take it all the way back down so we can try again */
316 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
322 queue_work(priv->workqueue, &priv->restart);
325 static int is_fat_channel(__le32 rxon_flags)
327 return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
328 (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
331 int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
335 /* 4965 HT rate format */
336 if (rate_n_flags & RATE_MCS_HT_MSK) {
337 idx = (rate_n_flags & 0xff);
339 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
340 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
342 idx += IWL_FIRST_OFDM_RATE;
343 /* skip 9M not supported in ht*/
344 if (idx >= IWL_RATE_9M_INDEX)
346 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
349 /* 4965 legacy rate format, search for match in table */
351 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
352 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
360 * translate ucode response to mac80211 tx status control values
362 void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
363 struct ieee80211_tx_info *control)
367 control->antenna_sel_tx =
368 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
369 if (rate_n_flags & RATE_MCS_HT_MSK)
370 control->flags |= IEEE80211_TX_CTL_OFDM_HT;
371 if (rate_n_flags & RATE_MCS_GF_MSK)
372 control->flags |= IEEE80211_TX_CTL_GREEN_FIELD;
373 if (rate_n_flags & RATE_MCS_FAT_MSK)
374 control->flags |= IEEE80211_TX_CTL_40_MHZ_WIDTH;
375 if (rate_n_flags & RATE_MCS_DUP_MSK)
376 control->flags |= IEEE80211_TX_CTL_DUP_DATA;
377 if (rate_n_flags & RATE_MCS_SGI_MSK)
378 control->flags |= IEEE80211_TX_CTL_SHORT_GI;
379 rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
380 if (control->band == IEEE80211_BAND_5GHZ)
381 rate_index -= IWL_FIRST_OFDM_RATE;
382 control->tx_rate_idx = rate_index;
389 static int iwl4965_eeprom_check_version(struct iwl_priv *priv)
394 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
396 calib_ver = iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
398 if (eeprom_ver < EEPROM_4965_EEPROM_VERSION ||
399 calib_ver < EEPROM_4965_TX_POWER_VERSION)
404 IWL_ERROR("Unsuported EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
405 eeprom_ver, EEPROM_4965_EEPROM_VERSION,
406 calib_ver, EEPROM_4965_TX_POWER_VERSION);
410 int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
415 spin_lock_irqsave(&priv->lock, flags);
416 ret = iwl_grab_nic_access(priv);
418 spin_unlock_irqrestore(&priv->lock, flags);
422 if (src == IWL_PWR_SRC_VAUX) {
424 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
427 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
428 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
429 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
430 ~APMG_PS_CTRL_MSK_PWR_SRC);
433 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
434 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
435 ~APMG_PS_CTRL_MSK_PWR_SRC);
438 iwl_release_nic_access(priv);
439 spin_unlock_irqrestore(&priv->lock, flags);
445 * Activate/Deactivat Tx DMA/FIFO channels according tx fifos mask
446 * must be called under priv->lock and mac access
448 static void iwl4965_txq_set_sched(struct iwl_priv *priv, u32 mask)
450 iwl_write_prph(priv, IWL49_SCD_TXFACT, mask);
453 static int iwl4965_apm_init(struct iwl_priv *priv)
457 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
458 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
460 /* disable L0s without affecting L1 :don't wait for ICH L0s bug W/A) */
461 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
462 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
464 /* set "initialization complete" bit to move adapter
465 * D0U* --> D0A* state */
466 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
468 /* wait for clock stabilization */
469 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
470 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
471 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
473 IWL_DEBUG_INFO("Failed to init the card\n");
477 ret = iwl_grab_nic_access(priv);
482 iwl_write_prph(priv, APMG_CLK_CTRL_REG, APMG_CLK_VAL_DMA_CLK_RQT |
483 APMG_CLK_VAL_BSM_CLK_RQT);
487 /* disable L1-Active */
488 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
489 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
491 iwl_release_nic_access(priv);
497 static void iwl4965_nic_config(struct iwl_priv *priv)
504 spin_lock_irqsave(&priv->lock, flags);
506 if ((priv->rev_id & 0x80) == 0x80 && (priv->rev_id & 0x7f) < 8) {
507 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
508 /* Enable No Snoop field */
509 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
513 pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
515 /* L1 is enabled by BIOS */
516 if ((val_link & PCI_LINK_VAL_L1_EN) == PCI_LINK_VAL_L1_EN)
517 /* diable L0S disabled L1A enabled */
518 iwl_set_bit(priv, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
520 /* L0S enabled L1A disabled */
521 iwl_clear_bit(priv, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
523 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
525 /* write radio config values to register */
526 if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) == EEPROM_4965_RF_CFG_TYPE_MAX)
527 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
528 EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
529 EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
530 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
532 /* set CSR_HW_CONFIG_REG for uCode use */
533 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
534 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
535 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
537 priv->calib_info = (struct iwl_eeprom_calib_info *)
538 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
540 spin_unlock_irqrestore(&priv->lock, flags);
543 static int iwl4965_apm_stop_master(struct iwl_priv *priv)
548 spin_lock_irqsave(&priv->lock, flags);
550 /* set stop master bit */
551 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
553 ret = iwl_poll_bit(priv, CSR_RESET,
554 CSR_RESET_REG_FLAG_MASTER_DISABLED,
555 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
560 spin_unlock_irqrestore(&priv->lock, flags);
561 IWL_DEBUG_INFO("stop master\n");
566 static void iwl4965_apm_stop(struct iwl_priv *priv)
570 iwl4965_apm_stop_master(priv);
572 spin_lock_irqsave(&priv->lock, flags);
574 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
578 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
579 spin_unlock_irqrestore(&priv->lock, flags);
582 static int iwl4965_apm_reset(struct iwl_priv *priv)
587 iwl4965_apm_stop_master(priv);
589 spin_lock_irqsave(&priv->lock, flags);
591 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
595 /* FIXME: put here L1A -L0S w/a */
597 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
599 ret = iwl_poll_bit(priv, CSR_RESET,
600 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
601 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
608 ret = iwl_grab_nic_access(priv);
611 /* Enable DMA and BSM Clock */
612 iwl_write_prph(priv, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT |
613 APMG_CLK_VAL_BSM_CLK_RQT);
618 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
619 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
621 iwl_release_nic_access(priv);
623 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
624 wake_up_interruptible(&priv->wait_command_queue);
627 spin_unlock_irqrestore(&priv->lock, flags);
632 #define REG_RECALIB_PERIOD (60)
635 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
637 * This callback is provided in order to send a statistics request.
639 * This timer function is continually reset to execute within
640 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
641 * was received. We need to ensure we receive the statistics in order
642 * to update the temperature used for calibrating the TXPOWER.
644 static void iwl4965_bg_statistics_periodic(unsigned long data)
646 struct iwl_priv *priv = (struct iwl_priv *)data;
648 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
651 iwl_send_statistics_request(priv, CMD_ASYNC);
654 void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
656 struct iwl4965_ct_kill_config cmd;
660 spin_lock_irqsave(&priv->lock, flags);
661 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
662 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
663 spin_unlock_irqrestore(&priv->lock, flags);
665 cmd.critical_temperature_R =
666 cpu_to_le32(priv->hw_params.ct_kill_threshold);
668 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
671 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
673 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
674 "critical temperature is %d\n",
675 cmd.critical_temperature_R);
678 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
680 /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
681 * Called after every association, but this runs only once!
682 * ... once chain noise is calibrated the first time, it's good forever. */
683 static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
685 struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
687 if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
688 struct iwl4965_calibration_cmd cmd;
690 memset(&cmd, 0, sizeof(cmd));
691 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
695 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
697 IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
698 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
699 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
703 static void iwl4965_gain_computation(struct iwl_priv *priv,
705 u16 min_average_noise_antenna_i,
706 u32 min_average_noise)
709 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
711 data->delta_gain_code[min_average_noise_antenna_i] = 0;
713 for (i = 0; i < NUM_RX_CHAINS; i++) {
716 if (!(data->disconn_array[i]) &&
717 (data->delta_gain_code[i] ==
718 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
719 delta_g = average_noise[i] - min_average_noise;
720 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
721 data->delta_gain_code[i] =
722 min(data->delta_gain_code[i],
723 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
725 data->delta_gain_code[i] =
726 (data->delta_gain_code[i] | (1 << 2));
728 data->delta_gain_code[i] = 0;
731 IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
732 data->delta_gain_code[0],
733 data->delta_gain_code[1],
734 data->delta_gain_code[2]);
736 /* Differential gain gets sent to uCode only once */
737 if (!data->radio_write) {
738 struct iwl4965_calibration_cmd cmd;
739 data->radio_write = 1;
741 memset(&cmd, 0, sizeof(cmd));
742 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
743 cmd.diff_gain_a = data->delta_gain_code[0];
744 cmd.diff_gain_b = data->delta_gain_code[1];
745 cmd.diff_gain_c = data->delta_gain_code[2];
746 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
749 IWL_DEBUG_CALIB("fail sending cmd "
750 "REPLY_PHY_CALIBRATION_CMD \n");
752 /* TODO we might want recalculate
753 * rx_chain in rxon cmd */
755 /* Mark so we run this algo only once! */
756 data->state = IWL_CHAIN_NOISE_CALIBRATED;
758 data->chain_noise_a = 0;
759 data->chain_noise_b = 0;
760 data->chain_noise_c = 0;
761 data->chain_signal_a = 0;
762 data->chain_signal_b = 0;
763 data->chain_signal_c = 0;
764 data->beacon_count = 0;
767 static void iwl4965_bg_sensitivity_work(struct work_struct *work)
769 struct iwl_priv *priv = container_of(work, struct iwl_priv,
772 mutex_lock(&priv->mutex);
774 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
775 test_bit(STATUS_SCANNING, &priv->status)) {
776 mutex_unlock(&priv->mutex);
780 if (priv->start_calib) {
781 iwl_chain_noise_calibration(priv, &priv->statistics);
783 iwl_sensitivity_calibration(priv, &priv->statistics);
786 mutex_unlock(&priv->mutex);
789 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
791 static void iwl4965_bg_txpower_work(struct work_struct *work)
793 struct iwl_priv *priv = container_of(work, struct iwl_priv,
796 /* If a scan happened to start before we got here
797 * then just return; the statistics notification will
798 * kick off another scheduled work to compensate for
799 * any temperature delta we missed here. */
800 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
801 test_bit(STATUS_SCANNING, &priv->status))
804 mutex_lock(&priv->mutex);
806 /* Regardless of if we are assocaited, we must reconfigure the
807 * TX power since frames can be sent on non-radar channels while
809 iwl4965_hw_reg_send_txpower(priv);
811 /* Update last_temperature to keep is_calib_needed from running
812 * when it isn't needed... */
813 priv->last_temperature = priv->temperature;
815 mutex_unlock(&priv->mutex);
819 * Acquire priv->lock before calling this function !
821 static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
823 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
824 (index & 0xff) | (txq_id << 8));
825 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
829 * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
830 * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
831 * @scd_retry: (1) Indicates queue will be used in aggregation mode
833 * NOTE: Acquire priv->lock before calling this function !
835 static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
836 struct iwl_tx_queue *txq,
837 int tx_fifo_id, int scd_retry)
839 int txq_id = txq->q.id;
841 /* Find out whether to activate Tx queue */
842 int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
844 /* Set up and activate */
845 iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
846 (active << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
847 (tx_fifo_id << IWL49_SCD_QUEUE_STTS_REG_POS_TXF) |
848 (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_WSL) |
849 (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
850 IWL49_SCD_QUEUE_STTS_REG_MSK);
852 txq->sched_retry = scd_retry;
854 IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
855 active ? "Activate" : "Deactivate",
856 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
859 static const u16 default_queue_to_tx_fifo[] = {
869 int iwl4965_alive_notify(struct iwl_priv *priv)
876 spin_lock_irqsave(&priv->lock, flags);
878 ret = iwl_grab_nic_access(priv);
880 spin_unlock_irqrestore(&priv->lock, flags);
884 /* Clear 4965's internal Tx Scheduler data base */
885 priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
886 a = priv->scd_base_addr + IWL49_SCD_CONTEXT_DATA_OFFSET;
887 for (; a < priv->scd_base_addr + IWL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
888 iwl_write_targ_mem(priv, a, 0);
889 for (; a < priv->scd_base_addr + IWL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
890 iwl_write_targ_mem(priv, a, 0);
891 for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
892 iwl_write_targ_mem(priv, a, 0);
894 /* Tel 4965 where to find Tx byte count tables */
895 iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
897 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
899 /* Disable chain mode for all queues */
900 iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
902 /* Initialize each Tx queue (including the command queue) */
903 for (i = 0; i < priv->hw_params.max_txq_num; i++) {
905 /* TFD circular buffer read/write indexes */
906 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
907 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
909 /* Max Tx Window size for Scheduler-ACK mode */
910 iwl_write_targ_mem(priv, priv->scd_base_addr +
911 IWL49_SCD_CONTEXT_QUEUE_OFFSET(i),
913 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
914 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
917 iwl_write_targ_mem(priv, priv->scd_base_addr +
918 IWL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
921 IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
922 IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
925 iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
926 (1 << priv->hw_params.max_txq_num) - 1);
928 /* Activate all Tx DMA/FIFO channels */
929 priv->cfg->ops->lib->txq_set_sched(priv, IWL_MASK(0, 7));
931 iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
933 /* Map each Tx/cmd queue to its corresponding fifo */
934 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
935 int ac = default_queue_to_tx_fifo[i];
936 iwl_txq_ctx_activate(priv, i);
937 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
940 iwl_release_nic_access(priv);
941 spin_unlock_irqrestore(&priv->lock, flags);
946 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
947 static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
951 .auto_corr_min_ofdm = 85,
952 .auto_corr_min_ofdm_mrc = 170,
953 .auto_corr_min_ofdm_x1 = 105,
954 .auto_corr_min_ofdm_mrc_x1 = 220,
956 .auto_corr_max_ofdm = 120,
957 .auto_corr_max_ofdm_mrc = 210,
958 .auto_corr_max_ofdm_x1 = 140,
959 .auto_corr_max_ofdm_mrc_x1 = 270,
961 .auto_corr_min_cck = 125,
962 .auto_corr_max_cck = 200,
963 .auto_corr_min_cck_mrc = 200,
964 .auto_corr_max_cck_mrc = 400,
972 * iwl4965_hw_set_hw_params
974 * Called when initializing driver
976 int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
979 if ((priv->cfg->mod_params->num_of_queues > IWL49_NUM_QUEUES) ||
980 (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
981 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
982 IWL_MIN_NUM_QUEUES, IWL49_NUM_QUEUES);
986 priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
987 priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
988 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
989 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
990 if (priv->cfg->mod_params->amsdu_size_8K)
991 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
993 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
994 priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
995 priv->hw_params.max_stations = IWL4965_STATION_COUNT;
996 priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
998 priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
999 priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
1000 priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
1001 priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
1003 priv->hw_params.tx_chains_num = 2;
1004 priv->hw_params.rx_chains_num = 2;
1005 priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
1006 priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
1007 priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
1009 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1010 priv->hw_params.sens = &iwl4965_sensitivity;
1016 /* set card power command */
1017 static int iwl4965_set_power(struct iwl_priv *priv,
1022 ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1023 sizeof(struct iwl4965_powertable_cmd),
1027 int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
1029 IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
1033 static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1046 *res = ((num * 2 + denom) / (denom * 2)) * sign;
1052 * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1054 * Determines power supply voltage compensation for txpower calculations.
1055 * Returns number of 1/2-dB steps to subtract from gain table index,
1056 * to compensate for difference between power supply voltage during
1057 * factory measurements, vs. current power supply voltage.
1059 * Voltage indication is higher for lower voltage.
1060 * Lower voltage requires more gain (lower gain table index).
1062 static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1063 s32 current_voltage)
1067 if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1068 (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1071 iwl4965_math_div_round(current_voltage - eeprom_voltage,
1072 TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1074 if (current_voltage > eeprom_voltage)
1076 if ((comp < -2) || (comp > 2))
1082 static const struct iwl_channel_info *
1083 iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
1084 enum ieee80211_band band, u16 channel)
1086 const struct iwl_channel_info *ch_info;
1088 ch_info = iwl_get_channel_info(priv, band, channel);
1090 if (!is_channel_valid(ch_info))
1096 static s32 iwl4965_get_tx_atten_grp(u16 channel)
1098 if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1099 channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1100 return CALIB_CH_GROUP_5;
1102 if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1103 channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1104 return CALIB_CH_GROUP_1;
1106 if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1107 channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1108 return CALIB_CH_GROUP_2;
1110 if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1111 channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1112 return CALIB_CH_GROUP_3;
1114 if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1115 channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1116 return CALIB_CH_GROUP_4;
1118 IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1122 static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
1126 for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
1127 if (priv->calib_info->band_info[b].ch_from == 0)
1130 if ((channel >= priv->calib_info->band_info[b].ch_from)
1131 && (channel <= priv->calib_info->band_info[b].ch_to))
1138 static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1145 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1151 * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1153 * Interpolates factory measurements from the two sample channels within a
1154 * sub-band, to apply to channel of interest. Interpolation is proportional to
1155 * differences in channel frequencies, which is proportional to differences
1156 * in channel number.
1158 static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
1159 struct iwl_eeprom_calib_ch_info *chan_info)
1164 const struct iwl_eeprom_calib_measure *m1;
1165 const struct iwl_eeprom_calib_measure *m2;
1166 struct iwl_eeprom_calib_measure *omeas;
1170 s = iwl4965_get_sub_band(priv, channel);
1171 if (s >= EEPROM_TX_POWER_BANDS) {
1172 IWL_ERROR("Tx Power can not find channel %d ", channel);
1176 ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1177 ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
1178 chan_info->ch_num = (u8) channel;
1180 IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1181 channel, s, ch_i1, ch_i2);
1183 for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1184 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
1185 m1 = &(priv->calib_info->band_info[s].ch1.
1186 measurements[c][m]);
1187 m2 = &(priv->calib_info->band_info[s].ch2.
1188 measurements[c][m]);
1189 omeas = &(chan_info->measurements[c][m]);
1192 (u8) iwl4965_interpolate_value(channel, ch_i1,
1197 (u8) iwl4965_interpolate_value(channel, ch_i1,
1198 m1->gain_idx, ch_i2,
1200 omeas->temperature =
1201 (u8) iwl4965_interpolate_value(channel, ch_i1,
1206 (s8) iwl4965_interpolate_value(channel, ch_i1,
1211 ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1212 m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1214 ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1215 m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1217 ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1218 m1->pa_det, m2->pa_det, omeas->pa_det);
1220 ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
1221 m1->temperature, m2->temperature,
1222 omeas->temperature);
1229 /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1230 * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1231 static s32 back_off_table[] = {
1232 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1233 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1234 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1235 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1239 /* Thermal compensation values for txpower for various frequency ranges ...
1240 * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
1241 static struct iwl4965_txpower_comp_entry {
1242 s32 degrees_per_05db_a;
1243 s32 degrees_per_05db_a_denom;
1244 } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1245 {9, 2}, /* group 0 5.2, ch 34-43 */
1246 {4, 1}, /* group 1 5.2, ch 44-70 */
1247 {4, 1}, /* group 2 5.2, ch 71-124 */
1248 {4, 1}, /* group 3 5.2, ch 125-200 */
1249 {3, 1} /* group 4 2.4, ch all */
1252 static s32 get_min_power_index(s32 rate_power_index, u32 band)
1255 if ((rate_power_index & 7) <= 4)
1256 return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1258 return MIN_TX_GAIN_INDEX;
1266 static const struct gain_entry gain_table[2][108] = {
1267 /* 5.2GHz power gain index table */
1269 {123, 0x3F}, /* highest txpower */
1378 /* 2.4GHz power gain index table */
1380 {110, 0x3f}, /* highest txpower */
1491 static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
1492 u8 is_fat, u8 ctrl_chan_high,
1493 struct iwl4965_tx_power_db *tx_power_tbl)
1495 u8 saturation_power;
1497 s32 user_target_power;
1501 s32 current_regulatory;
1502 s32 txatten_grp = CALIB_CH_GROUP_MAX;
1505 const struct iwl_channel_info *ch_info = NULL;
1506 struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1507 const struct iwl_eeprom_calib_measure *measurement;
1510 s32 voltage_compensation;
1511 s32 degrees_per_05db_num;
1512 s32 degrees_per_05db_denom;
1514 s32 temperature_comp[2];
1515 s32 factory_gain_index[2];
1516 s32 factory_actual_pwr[2];
1519 /* Sanity check requested level (dBm) */
1520 if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1521 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1522 priv->user_txpower_limit);
1525 if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1526 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1527 priv->user_txpower_limit);
1531 /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1532 * are used for indexing into txpower table) */
1533 user_target_power = 2 * priv->user_txpower_limit;
1535 /* Get current (RXON) channel, band, width */
1537 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
1539 IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1545 /* get txatten group, used to select 1) thermal txpower adjustment
1546 * and 2) mimo txpower balance between Tx chains. */
1547 txatten_grp = iwl4965_get_tx_atten_grp(channel);
1548 if (txatten_grp < 0)
1551 IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1552 channel, txatten_grp);
1561 /* hardware txpower limits ...
1562 * saturation (clipping distortion) txpowers are in half-dBm */
1564 saturation_power = priv->calib_info->saturation_power24;
1566 saturation_power = priv->calib_info->saturation_power52;
1568 if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1569 saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1571 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1573 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1576 /* regulatory txpower limits ... reg_limit values are in half-dBm,
1577 * max_power_avg values are in dBm, convert * 2 */
1579 reg_limit = ch_info->fat_max_power_avg * 2;
1581 reg_limit = ch_info->max_power_avg * 2;
1583 if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
1584 (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
1586 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
1588 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
1591 /* Interpolate txpower calibration values for this channel,
1592 * based on factory calibration tests on spaced channels. */
1593 iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
1595 /* calculate tx gain adjustment based on power supply voltage */
1596 voltage = priv->calib_info->voltage;
1597 init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
1598 voltage_compensation =
1599 iwl4965_get_voltage_compensation(voltage, init_voltage);
1601 IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
1603 voltage, voltage_compensation);
1605 /* get current temperature (Celsius) */
1606 current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
1607 current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
1608 current_temp = KELVIN_TO_CELSIUS(current_temp);
1610 /* select thermal txpower adjustment params, based on channel group
1611 * (same frequency group used for mimo txatten adjustment) */
1612 degrees_per_05db_num =
1613 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
1614 degrees_per_05db_denom =
1615 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
1617 /* get per-chain txpower values from factory measurements */
1618 for (c = 0; c < 2; c++) {
1619 measurement = &ch_eeprom_info.measurements[c][1];
1621 /* txgain adjustment (in half-dB steps) based on difference
1622 * between factory and current temperature */
1623 factory_temp = measurement->temperature;
1624 iwl4965_math_div_round((current_temp - factory_temp) *
1625 degrees_per_05db_denom,
1626 degrees_per_05db_num,
1627 &temperature_comp[c]);
1629 factory_gain_index[c] = measurement->gain_idx;
1630 factory_actual_pwr[c] = measurement->actual_pow;
1632 IWL_DEBUG_TXPOWER("chain = %d\n", c);
1633 IWL_DEBUG_TXPOWER("fctry tmp %d, "
1634 "curr tmp %d, comp %d steps\n",
1635 factory_temp, current_temp,
1636 temperature_comp[c]);
1638 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
1639 factory_gain_index[c],
1640 factory_actual_pwr[c]);
1643 /* for each of 33 bit-rates (including 1 for CCK) */
1644 for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
1646 union iwl4965_tx_power_dual_stream tx_power;
1648 /* for mimo, reduce each chain's txpower by half
1649 * (3dB, 6 steps), so total output power is regulatory
1652 current_regulatory = reg_limit -
1653 IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
1656 current_regulatory = reg_limit;
1660 /* find txpower limit, either hardware or regulatory */
1661 power_limit = saturation_power - back_off_table[i];
1662 if (power_limit > current_regulatory)
1663 power_limit = current_regulatory;
1665 /* reduce user's txpower request if necessary
1666 * for this rate on this channel */
1667 target_power = user_target_power;
1668 if (target_power > power_limit)
1669 target_power = power_limit;
1671 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
1672 i, saturation_power - back_off_table[i],
1673 current_regulatory, user_target_power,
1676 /* for each of 2 Tx chains (radio transmitters) */
1677 for (c = 0; c < 2; c++) {
1682 (s32)le32_to_cpu(priv->card_alive_init.
1683 tx_atten[txatten_grp][c]);
1687 /* calculate index; higher index means lower txpower */
1688 power_index = (u8) (factory_gain_index[c] -
1690 factory_actual_pwr[c]) -
1691 temperature_comp[c] -
1692 voltage_compensation +
1695 /* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
1698 if (power_index < get_min_power_index(i, band))
1699 power_index = get_min_power_index(i, band);
1701 /* adjust 5 GHz index to support negative indexes */
1705 /* CCK, rate 32, reduce txpower for CCK */
1706 if (i == POWER_TABLE_CCK_ENTRY)
1708 IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
1710 /* stay within the table! */
1711 if (power_index > 107) {
1712 IWL_WARNING("txpower index %d > 107\n",
1716 if (power_index < 0) {
1717 IWL_WARNING("txpower index %d < 0\n",
1722 /* fill txpower command for this rate/chain */
1723 tx_power.s.radio_tx_gain[c] =
1724 gain_table[band][power_index].radio;
1725 tx_power.s.dsp_predis_atten[c] =
1726 gain_table[band][power_index].dsp;
1728 IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
1729 "gain 0x%02x dsp %d\n",
1730 c, atten_value, power_index,
1731 tx_power.s.radio_tx_gain[c],
1732 tx_power.s.dsp_predis_atten[c]);
1733 }/* for each chain */
1735 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
1737 }/* for each rate */
1743 * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
1745 * Uses the active RXON for channel, band, and characteristics (fat, high)
1746 * The power limit is taken from priv->user_txpower_limit.
1748 int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
1750 struct iwl4965_txpowertable_cmd cmd = { 0 };
1754 u8 ctrl_chan_high = 0;
1756 if (test_bit(STATUS_SCANNING, &priv->status)) {
1757 /* If this gets hit a lot, switch it to a BUG() and catch
1758 * the stack trace to find out who is calling this during
1760 IWL_WARNING("TX Power requested while scanning!\n");
1764 band = priv->band == IEEE80211_BAND_2GHZ;
1766 is_fat = is_fat_channel(priv->active_rxon.flags);
1769 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
1773 cmd.channel = priv->active_rxon.channel;
1775 ret = iwl4965_fill_txpower_tbl(priv, band,
1776 le16_to_cpu(priv->active_rxon.channel),
1777 is_fat, ctrl_chan_high, &cmd.tx_power);
1781 ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
1787 static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
1790 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1791 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1792 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1794 if ((rxon1->flags == rxon2->flags) &&
1795 (rxon1->filter_flags == rxon2->filter_flags) &&
1796 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1797 (rxon1->ofdm_ht_single_stream_basic_rates ==
1798 rxon2->ofdm_ht_single_stream_basic_rates) &&
1799 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1800 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1801 (rxon1->rx_chain == rxon2->rx_chain) &&
1802 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1803 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1807 rxon_assoc.flags = priv->staging_rxon.flags;
1808 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1809 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1810 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1811 rxon_assoc.reserved = 0;
1812 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1813 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1814 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1815 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1816 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1818 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
1819 sizeof(rxon_assoc), &rxon_assoc, NULL);
1827 int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
1832 u8 ctrl_chan_high = 0;
1833 struct iwl4965_channel_switch_cmd cmd = { 0 };
1834 const struct iwl_channel_info *ch_info;
1836 band = priv->band == IEEE80211_BAND_2GHZ;
1838 ch_info = iwl_get_channel_info(priv, priv->band, channel);
1840 is_fat = is_fat_channel(priv->staging_rxon.flags);
1843 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
1847 cmd.expect_beacon = 0;
1848 cmd.channel = cpu_to_le16(channel);
1849 cmd.rxon_flags = priv->active_rxon.flags;
1850 cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
1851 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
1853 cmd.expect_beacon = is_channel_radar(ch_info);
1855 cmd.expect_beacon = 1;
1857 rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
1858 ctrl_chan_high, &cmd.tx_power);
1860 IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
1864 rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
1868 static int iwl4965_shared_mem_rx_idx(struct iwl_priv *priv)
1870 struct iwl4965_shared *s = priv->shared_virt;
1871 return le32_to_cpu(s->rb_closed) & 0xFFF;
1874 int iwl4965_hw_get_temperature(struct iwl_priv *priv)
1876 return priv->temperature;
1879 unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
1880 struct iwl_frame *frame, u8 rate)
1882 struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
1883 unsigned int frame_size;
1885 tx_beacon_cmd = &frame->u.beacon;
1886 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
1888 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
1889 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1891 frame_size = iwl4965_fill_beacon_frame(priv,
1892 tx_beacon_cmd->frame,
1894 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
1896 BUG_ON(frame_size > MAX_MPDU_SIZE);
1897 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
1899 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
1900 tx_beacon_cmd->tx.rate_n_flags =
1901 iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
1903 tx_beacon_cmd->tx.rate_n_flags =
1904 iwl4965_hw_set_rate_n_flags(rate, 0);
1906 tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
1907 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
1908 return (sizeof(*tx_beacon_cmd) + frame_size);
1911 static int iwl4965_alloc_shared_mem(struct iwl_priv *priv)
1913 priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
1914 sizeof(struct iwl4965_shared),
1915 &priv->shared_phys);
1916 if (!priv->shared_virt)
1919 memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
1921 priv->rb_closed_offset = offsetof(struct iwl4965_shared, rb_closed);
1926 static void iwl4965_free_shared_mem(struct iwl_priv *priv)
1928 if (priv->shared_virt)
1929 pci_free_consistent(priv->pci_dev,
1930 sizeof(struct iwl4965_shared),
1936 * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
1938 static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
1939 struct iwl_tx_queue *txq,
1943 int txq_id = txq->q.id;
1944 struct iwl4965_shared *shared_data = priv->shared_virt;
1946 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
1948 /* Set up byte count within first 256 entries */
1949 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
1950 tfd_offset[txq->q.write_ptr], byte_cnt, len);
1952 /* If within first 64 entries, duplicate at end */
1953 if (txq->q.write_ptr < IWL49_MAX_WIN_SIZE)
1954 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
1955 tfd_offset[IWL49_QUEUE_SIZE + txq->q.write_ptr],
1960 * sign_extend - Sign extend a value using specified bit as sign-bit
1962 * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
1963 * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
1965 * @param oper value to sign extend
1966 * @param index 0 based bit index (0<=index<32) to sign bit
1968 static s32 sign_extend(u32 oper, int index)
1970 u8 shift = 31 - index;
1972 return (s32)(oper << shift) >> shift;
1976 * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
1977 * @statistics: Provides the temperature reading from the uCode
1979 * A return of <0 indicates bogus data in the statistics
1981 int iwl4965_get_temperature(const struct iwl_priv *priv)
1988 if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
1989 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
1990 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
1991 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
1992 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
1993 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
1994 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
1996 IWL_DEBUG_TEMP("Running temperature calibration\n");
1997 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
1998 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
1999 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2000 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2004 * Temperature is only 23 bits, so sign extend out to 32.
2006 * NOTE If we haven't received a statistics notification yet
2007 * with an updated temperature, use R4 provided to us in the
2008 * "initialize" ALIVE response.
2010 if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2011 vt = sign_extend(R4, 23);
2014 le32_to_cpu(priv->statistics.general.temperature), 23);
2016 IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2020 IWL_ERROR("Calibration conflict R1 == R3\n");
2024 /* Calculate temperature in degrees Kelvin, adjust by 97%.
2025 * Add offset to center the adjustment around 0 degrees Centigrade. */
2026 temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2027 temperature /= (R3 - R1);
2028 temperature = (temperature * 97) / 100 +
2029 TEMPERATURE_CALIB_KELVIN_OFFSET;
2031 IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2032 KELVIN_TO_CELSIUS(temperature));
2037 /* Adjust Txpower only if temperature variance is greater than threshold. */
2038 #define IWL_TEMPERATURE_THRESHOLD 3
2041 * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2043 * If the temperature changed has changed sufficiently, then a recalibration
2046 * Assumes caller will replace priv->last_temperature once calibration
2049 static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
2053 if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2054 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2058 temp_diff = priv->temperature - priv->last_temperature;
2060 /* get absolute value */
2061 if (temp_diff < 0) {
2062 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2063 temp_diff = -temp_diff;
2064 } else if (temp_diff == 0)
2065 IWL_DEBUG_POWER("Same temp, \n");
2067 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2069 if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2070 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2074 IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2079 /* Calculate noise level, based on measurements during network silence just
2080 * before arriving beacon. This measurement can be done only if we know
2081 * exactly when to expect beacons, therefore only when we're associated. */
2082 static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
2084 struct statistics_rx_non_phy *rx_info
2085 = &(priv->statistics.rx.general);
2086 int num_active_rx = 0;
2087 int total_silence = 0;
2089 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2091 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2093 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2095 if (bcn_silence_a) {
2096 total_silence += bcn_silence_a;
2099 if (bcn_silence_b) {
2100 total_silence += bcn_silence_b;
2103 if (bcn_silence_c) {
2104 total_silence += bcn_silence_c;
2108 /* Average among active antennas */
2110 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2112 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2114 IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2115 bcn_silence_a, bcn_silence_b, bcn_silence_c,
2116 priv->last_rx_noise);
2119 void iwl4965_hw_rx_statistics(struct iwl_priv *priv,
2120 struct iwl_rx_mem_buffer *rxb)
2122 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2126 IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2127 (int)sizeof(priv->statistics), pkt->len);
2129 change = ((priv->statistics.general.temperature !=
2130 pkt->u.stats.general.temperature) ||
2131 ((priv->statistics.flag &
2132 STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2133 (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2135 memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2137 set_bit(STATUS_STATISTICS, &priv->status);
2139 /* Reschedule the statistics timer to occur in
2140 * REG_RECALIB_PERIOD seconds to ensure we get a
2141 * thermal update even if the uCode doesn't give
2143 mod_timer(&priv->statistics_periodic, jiffies +
2144 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2146 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2147 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2148 iwl4965_rx_calc_noise(priv);
2149 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
2150 queue_work(priv->workqueue, &priv->sensitivity_work);
2154 iwl_leds_background(priv);
2156 /* If the hardware hasn't reported a change in
2157 * temperature then don't bother computing a
2158 * calibrated temperature value */
2162 temp = iwl4965_get_temperature(priv);
2166 if (priv->temperature != temp) {
2167 if (priv->temperature)
2168 IWL_DEBUG_TEMP("Temperature changed "
2169 "from %dC to %dC\n",
2170 KELVIN_TO_CELSIUS(priv->temperature),
2171 KELVIN_TO_CELSIUS(temp));
2173 IWL_DEBUG_TEMP("Temperature "
2174 "initialized to %dC\n",
2175 KELVIN_TO_CELSIUS(temp));
2178 priv->temperature = temp;
2179 set_bit(STATUS_TEMPERATURE, &priv->status);
2181 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2182 iwl4965_is_temp_calib_needed(priv))
2183 queue_work(priv->workqueue, &priv->txpower_work);
2186 static void iwl4965_add_radiotap(struct iwl_priv *priv,
2187 struct sk_buff *skb,
2188 struct iwl4965_rx_phy_res *rx_start,
2189 struct ieee80211_rx_status *stats,
2192 s8 signal = stats->signal;
2194 int rate = stats->rate_idx;
2195 u64 tsf = stats->mactime;
2197 __le16 phy_flags_hw = rx_start->phy_flags;
2198 struct iwl4965_rt_rx_hdr {
2199 struct ieee80211_radiotap_header rt_hdr;
2200 __le64 rt_tsf; /* TSF */
2201 u8 rt_flags; /* radiotap packet flags */
2202 u8 rt_rate; /* rate in 500kb/s */
2203 __le16 rt_channelMHz; /* channel in MHz */
2204 __le16 rt_chbitmask; /* channel bitfield */
2205 s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
2207 u8 rt_antenna; /* antenna number */
2208 } __attribute__ ((packed)) *iwl4965_rt;
2210 /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2211 if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2212 if (net_ratelimit())
2213 printk(KERN_ERR "not enough headroom [%d] for "
2214 "radiotap head [%zd]\n",
2215 skb_headroom(skb), sizeof(*iwl4965_rt));
2219 /* put radiotap header in front of 802.11 header and data */
2220 iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2222 /* initialise radiotap header */
2223 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2224 iwl4965_rt->rt_hdr.it_pad = 0;
2226 /* total header + data */
2227 put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2228 &iwl4965_rt->rt_hdr.it_len);
2230 /* Indicate all the fields we add to the radiotap header */
2231 put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2232 (1 << IEEE80211_RADIOTAP_FLAGS) |
2233 (1 << IEEE80211_RADIOTAP_RATE) |
2234 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2235 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2236 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2237 (1 << IEEE80211_RADIOTAP_ANTENNA)),
2238 &iwl4965_rt->rt_hdr.it_present);
2240 /* Zero the flags, we'll add to them as we go */
2241 iwl4965_rt->rt_flags = 0;
2243 put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2245 iwl4965_rt->rt_dbmsignal = signal;
2246 iwl4965_rt->rt_dbmnoise = noise;
2248 /* Convert the channel frequency and set the flags */
2249 put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2250 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2251 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2252 IEEE80211_CHAN_5GHZ),
2253 &iwl4965_rt->rt_chbitmask);
2254 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2255 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2256 IEEE80211_CHAN_2GHZ),
2257 &iwl4965_rt->rt_chbitmask);
2259 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2260 IEEE80211_CHAN_2GHZ),
2261 &iwl4965_rt->rt_chbitmask);
2264 iwl4965_rt->rt_rate = 0;
2266 iwl4965_rt->rt_rate = iwl_rates[rate].ieee;
2271 * It seems that the antenna field in the phy flags value
2272 * is actually a bitfield. This is undefined by radiotap,
2273 * it wants an actual antenna number but I always get "7"
2274 * for most legacy frames I receive indicating that the
2275 * same frame was received on all three RX chains.
2277 * I think this field should be removed in favour of a
2278 * new 802.11n radiotap field "RX chains" that is defined
2281 antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2282 iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
2284 /* set the preamble flag if appropriate */
2285 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2286 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2288 stats->flag |= RX_FLAG_RADIOTAP;
2291 static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2293 /* 0 - mgmt, 1 - cnt, 2 - data */
2294 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2295 priv->rx_stats[idx].cnt++;
2296 priv->rx_stats[idx].bytes += len;
2300 * returns non-zero if packet should be dropped
2302 static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2303 struct ieee80211_hdr *hdr,
2305 struct ieee80211_rx_status *stats)
2307 u16 fc = le16_to_cpu(hdr->frame_control);
2309 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2312 if (!(fc & IEEE80211_FCTL_PROTECTED))
2315 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2316 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2317 case RX_RES_STATUS_SEC_TYPE_TKIP:
2318 /* The uCode has got a bad phase 1 Key, pushes the packet.
2319 * Decryption will be done in SW. */
2320 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2321 RX_RES_STATUS_BAD_KEY_TTAK)
2324 case RX_RES_STATUS_SEC_TYPE_WEP:
2325 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2326 RX_RES_STATUS_BAD_ICV_MIC) {
2327 /* bad ICV, the packet is destroyed since the
2328 * decryption is inplace, drop it */
2329 IWL_DEBUG_RX("Packet destroyed\n");
2332 case RX_RES_STATUS_SEC_TYPE_CCMP:
2333 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2334 RX_RES_STATUS_DECRYPT_OK) {
2335 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2336 stats->flag |= RX_FLAG_DECRYPTED;
2346 static u32 iwl4965_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
2348 u32 decrypt_out = 0;
2350 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2351 RX_RES_STATUS_STATION_FOUND)
2352 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2353 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2355 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2357 /* packet was not encrypted */
2358 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2359 RX_RES_STATUS_SEC_TYPE_NONE)
2362 /* packet was encrypted with unknown alg */
2363 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2364 RX_RES_STATUS_SEC_TYPE_ERR)
2367 /* decryption was not done in HW */
2368 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2369 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2372 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2374 case RX_RES_STATUS_SEC_TYPE_CCMP:
2375 /* alg is CCM: check MIC only */
2376 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2378 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2380 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2384 case RX_RES_STATUS_SEC_TYPE_TKIP:
2385 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2387 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2390 /* fall through if TTAK OK */
2392 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
2393 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2395 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2399 IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
2400 decrypt_in, decrypt_out);
2405 static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
2407 struct iwl_rx_mem_buffer *rxb,
2408 struct ieee80211_rx_status *stats)
2410 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2411 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
2412 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
2413 struct ieee80211_hdr *hdr;
2416 unsigned int skblen;
2418 u32 ampdu_status_legacy;
2420 if (!include_phy && priv->last_phy_res[0])
2421 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
2424 IWL_ERROR("MPDU frame without a PHY data\n");
2428 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
2429 rx_start->cfg_phy_cnt);
2431 len = le16_to_cpu(rx_start->byte_count);
2433 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
2434 sizeof(struct iwl4965_rx_phy_res) +
2435 rx_start->cfg_phy_cnt + len);
2438 struct iwl4965_rx_mpdu_res_start *amsdu =
2439 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
2441 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
2442 sizeof(struct iwl4965_rx_mpdu_res_start));
2443 len = le16_to_cpu(amsdu->byte_count);
2444 rx_start->byte_count = amsdu->byte_count;
2445 rx_end = (__le32 *) (((u8 *) hdr) + len);
2447 /* In monitor mode allow 802.11 ACk frames (10 bytes) */
2448 if (len > priv->hw_params.max_pkt_size ||
2449 len < ((priv->iw_mode == IEEE80211_IF_TYPE_MNTR) ? 10 : 16)) {
2450 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
2454 ampdu_status = le32_to_cpu(*rx_end);
2455 skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
2458 /* New status scheme, need to translate */
2459 ampdu_status_legacy = ampdu_status;
2460 ampdu_status = iwl4965_translate_rx_status(priv, ampdu_status);
2463 /* start from MAC */
2464 skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
2465 skb_put(rxb->skb, len); /* end where data ends */
2467 /* We only process data packets if the interface is open */
2468 if (unlikely(!priv->is_open)) {
2469 IWL_DEBUG_DROP_LIMIT
2470 ("Dropping packet while interface is not open.\n");
2475 hdr = (struct ieee80211_hdr *)rxb->skb->data;
2477 /* in case of HW accelerated crypto and bad decryption, drop */
2478 if (!priv->hw_params.sw_crypto &&
2479 iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
2482 if (priv->add_radiotap)
2483 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
2485 iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
2486 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
2487 priv->alloc_rxb_skb--;
2491 /* Calc max signal level (dBm) among 3 possible receivers */
2492 static int iwl4965_calc_rssi(struct iwl_priv *priv,
2493 struct iwl4965_rx_phy_res *rx_resp)
2495 /* data from PHY/DSP regarding signal strength, etc.,
2496 * contents are always there, not configurable by host. */
2497 struct iwl4965_rx_non_cfg_phy *ncphy =
2498 (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
2499 u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
2502 u32 valid_antennae =
2503 (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
2504 >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
2508 /* Find max rssi among 3 possible receivers.
2509 * These values are measured by the digital signal processor (DSP).
2510 * They should stay fairly constant even as the signal strength varies,
2511 * if the radio's automatic gain control (AGC) is working right.
2512 * AGC value (see below) will provide the "interesting" info. */
2513 for (i = 0; i < 3; i++)
2514 if (valid_antennae & (1 << i))
2515 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
2517 IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
2518 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
2521 /* dBm = max_rssi dB - agc dB - constant.
2522 * Higher AGC (higher radio gain) means lower signal. */
2523 return (max_rssi - agc - IWL_RSSI_OFFSET);
2526 static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
2528 unsigned long flags;
2530 spin_lock_irqsave(&priv->sta_lock, flags);
2531 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
2532 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
2533 priv->stations[sta_id].sta.sta.modify_mask = 0;
2534 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2535 spin_unlock_irqrestore(&priv->sta_lock, flags);
2537 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
2540 static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
2542 /* FIXME: need locking over ps_status ??? */
2543 u8 sta_id = iwl_find_station(priv, addr);
2545 if (sta_id != IWL_INVALID_STATION) {
2546 u8 sta_awake = priv->stations[sta_id].
2547 ps_status == STA_PS_STATUS_WAKE;
2549 if (sta_awake && ps_bit)
2550 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
2551 else if (!sta_awake && !ps_bit) {
2552 iwl4965_sta_modify_ps_wake(priv, sta_id);
2553 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
2557 #ifdef CONFIG_IWLWIFI_DEBUG
2560 * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
2562 * You may hack this function to show different aspects of received frames,
2563 * including selective frame dumps.
2564 * group100 parameter selects whether to show 1 out of 100 good frames.
2566 * TODO: This was originally written for 3945, need to audit for
2567 * proper operation with 4965.
2569 static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
2570 struct iwl_rx_packet *pkt,
2571 struct ieee80211_hdr *header, int group100)
2574 u32 print_summary = 0;
2575 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
2592 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
2593 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
2594 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
2595 u8 *data = IWL_RX_DATA(pkt);
2597 if (likely(!(priv->debug_level & IWL_DL_RX)))
2601 fc = le16_to_cpu(header->frame_control);
2602 seq_ctl = le16_to_cpu(header->seq_ctrl);
2605 channel = le16_to_cpu(rx_hdr->channel);
2606 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
2607 rate_sym = rx_hdr->rate;
2608 length = le16_to_cpu(rx_hdr->len);
2610 /* end-of-frame status and timestamp */
2611 status = le32_to_cpu(rx_end->status);
2612 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
2613 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
2614 tsf = le64_to_cpu(rx_end->timestamp);
2616 /* signal statistics */
2617 rssi = rx_stats->rssi;
2618 agc = rx_stats->agc;
2619 sig_avg = le16_to_cpu(rx_stats->sig_avg);
2620 noise_diff = le16_to_cpu(rx_stats->noise_diff);
2622 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
2624 /* if data frame is to us and all is good,
2625 * (optionally) print summary for only 1 out of every 100 */
2626 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
2627 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
2630 print_summary = 1; /* print each frame */
2631 else if (priv->framecnt_to_us < 100) {
2632 priv->framecnt_to_us++;
2635 priv->framecnt_to_us = 0;
2640 /* print summary for all other frames */
2644 if (print_summary) {
2650 title = "100Frames";
2651 else if (fc & IEEE80211_FCTL_RETRY)
2653 else if (ieee80211_is_assoc_response(fc))
2655 else if (ieee80211_is_reassoc_response(fc))
2657 else if (ieee80211_is_probe_response(fc)) {
2659 print_dump = 1; /* dump frame contents */
2660 } else if (ieee80211_is_beacon(fc)) {
2662 print_dump = 1; /* dump frame contents */
2663 } else if (ieee80211_is_atim(fc))
2665 else if (ieee80211_is_auth(fc))
2667 else if (ieee80211_is_deauth(fc))
2669 else if (ieee80211_is_disassoc(fc))
2674 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
2675 if (unlikely(rate_idx == -1))
2678 bitrate = iwl_rates[rate_idx].ieee / 2;
2680 /* print frame summary.
2681 * MAC addresses show just the last byte (for brevity),
2682 * but you can hack it to show more, if you'd like to. */
2684 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
2685 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
2686 title, fc, header->addr1[5],
2687 length, rssi, channel, bitrate);
2689 /* src/dst addresses assume managed mode */
2690 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
2691 "src=0x%02x, rssi=%u, tim=%lu usec, "
2692 "phy=0x%02x, chnl=%d\n",
2693 title, fc, header->addr1[5],
2694 header->addr3[5], rssi,
2695 tsf_low - priv->scan_start_tsf,
2696 phy_flags, channel);
2700 iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
2703 static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
2704 struct iwl_rx_packet *pkt,
2705 struct ieee80211_hdr *header,
2713 /* Called for REPLY_RX (legacy ABG frames), or
2714 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
2715 static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
2716 struct iwl_rx_mem_buffer *rxb)
2718 struct ieee80211_hdr *header;
2719 struct ieee80211_rx_status rx_status;
2720 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2721 /* Use phy data (Rx signal strength, etc.) contained within
2722 * this rx packet for legacy frames,
2723 * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
2724 int include_phy = (pkt->hdr.cmd == REPLY_RX);
2725 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
2726 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
2727 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
2729 unsigned int len = 0;
2733 rx_status.mactime = le64_to_cpu(rx_start->timestamp);
2735 ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
2736 rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
2737 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
2738 rx_status.rate_idx =
2739 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
2740 if (rx_status.band == IEEE80211_BAND_5GHZ)
2741 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
2743 rx_status.antenna = 0;
2746 if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
2747 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
2748 rx_start->cfg_phy_cnt);
2753 if (priv->last_phy_res[0])
2754 rx_start = (struct iwl4965_rx_phy_res *)
2755 &priv->last_phy_res[1];
2761 IWL_ERROR("MPDU frame without a PHY data\n");
2766 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
2767 + rx_start->cfg_phy_cnt);
2769 len = le16_to_cpu(rx_start->byte_count);
2770 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
2771 sizeof(struct iwl4965_rx_phy_res) + len);
2773 struct iwl4965_rx_mpdu_res_start *amsdu =
2774 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
2776 header = (void *)(pkt->u.raw +
2777 sizeof(struct iwl4965_rx_mpdu_res_start));
2778 len = le16_to_cpu(amsdu->byte_count);
2779 rx_end = (__le32 *) (pkt->u.raw +
2780 sizeof(struct iwl4965_rx_mpdu_res_start) + len);
2783 if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
2784 !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
2785 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
2786 le32_to_cpu(*rx_end));
2790 priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
2792 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
2793 rx_status.signal = iwl4965_calc_rssi(priv, rx_start);
2795 /* Meaningful noise values are available only from beacon statistics,
2796 * which are gathered only when associated, and indicate noise
2797 * only for the associated network channel ...
2798 * Ignore these noise values while scanning (other channels) */
2799 if (iwl_is_associated(priv) &&
2800 !test_bit(STATUS_SCANNING, &priv->status)) {
2801 rx_status.noise = priv->last_rx_noise;
2802 rx_status.qual = iwl4965_calc_sig_qual(rx_status.signal,
2805 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2806 rx_status.qual = iwl4965_calc_sig_qual(rx_status.signal, 0);
2809 /* Reset beacon noise level if not associated. */
2810 if (!iwl_is_associated(priv))
2811 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2813 /* Set "1" to report good data frames in groups of 100 */
2814 /* FIXME: need to optimze the call: */
2815 iwl4965_dbg_report_frame(priv, pkt, header, 1);
2817 IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
2818 rx_status.signal, rx_status.noise, rx_status.signal,
2819 (unsigned long long)rx_status.mactime);
2822 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2823 iwl4965_handle_data_packet(priv, 1, include_phy,
2828 network_packet = iwl4965_is_network_packet(priv, header);
2829 if (network_packet) {
2830 priv->last_rx_rssi = rx_status.signal;
2831 priv->last_beacon_time = priv->ucode_beacon_time;
2832 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
2835 fc = le16_to_cpu(header->frame_control);
2836 switch (fc & IEEE80211_FCTL_FTYPE) {
2837 case IEEE80211_FTYPE_MGMT:
2838 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
2839 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
2841 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
2844 case IEEE80211_FTYPE_CTL:
2845 #ifdef CONFIG_IWL4965_HT
2846 switch (fc & IEEE80211_FCTL_STYPE) {
2847 case IEEE80211_STYPE_BACK_REQ:
2848 IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
2849 iwl4965_handle_data_packet(priv, 0, include_phy,
2858 case IEEE80211_FTYPE_DATA: {
2859 DECLARE_MAC_BUF(mac1);
2860 DECLARE_MAC_BUF(mac2);
2861 DECLARE_MAC_BUF(mac3);
2863 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
2864 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
2867 if (unlikely(!network_packet))
2868 IWL_DEBUG_DROP("Dropping (non network): "
2870 print_mac(mac1, header->addr1),
2871 print_mac(mac2, header->addr2),
2872 print_mac(mac3, header->addr3));
2873 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
2874 IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
2875 print_mac(mac1, header->addr1),
2876 print_mac(mac2, header->addr2),
2877 print_mac(mac3, header->addr3));
2879 iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
2889 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
2890 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
2891 static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
2892 struct iwl_rx_mem_buffer *rxb)
2894 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2895 priv->last_phy_res[0] = 1;
2896 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
2897 sizeof(struct iwl4965_rx_phy_res));
2900 #ifdef CONFIG_IWL4965_HT
2903 * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
2905 static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
2906 int sta_id, int tid)
2908 unsigned long flags;
2910 /* Remove "disable" flag, to enable Tx for this TID */
2911 spin_lock_irqsave(&priv->sta_lock, flags);
2912 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
2913 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
2914 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2915 spin_unlock_irqrestore(&priv->sta_lock, flags);
2917 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
2921 * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2923 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2924 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2926 static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
2927 struct iwl_ht_agg *agg,
2928 struct iwl4965_compressed_ba_resp*
2933 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2934 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2937 struct ieee80211_tx_info *info;
2939 if (unlikely(!agg->wait_for_ba)) {
2940 IWL_ERROR("Received BA when not expected\n");
2944 /* Mark that the expected block-ack response arrived */
2945 agg->wait_for_ba = 0;
2946 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
2948 /* Calculate shift to align block-ack bits with our Tx window bits */
2949 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
2950 if (sh < 0) /* tbw something is wrong with indices */
2953 /* don't use 64-bit values for now */
2954 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2956 if (agg->frame_count > (64 - sh)) {
2957 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
2961 /* check for success or failure according to the
2962 * transmitted bitmap and block-ack bitmap */
2963 bitmap &= agg->bitmap;
2965 /* For each frame attempted in aggregation,
2966 * update driver's record of tx frame's status. */
2967 for (i = 0; i < agg->frame_count ; i++) {
2968 ack = bitmap & (1 << i);
2970 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
2971 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
2972 agg->start_idx + i);
2975 info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]);
2976 memset(&info->status, 0, sizeof(info->status));
2977 info->flags = IEEE80211_TX_STAT_ACK;
2978 info->flags |= IEEE80211_TX_STAT_AMPDU;
2979 info->status.ampdu_ack_map = successes;
2980 info->status.ampdu_ack_len = agg->frame_count;
2981 iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
2983 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
2989 * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2991 static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
2994 /* Simply stop the queue, but don't change any configuration;
2995 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
2996 iwl_write_prph(priv,
2997 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
2998 (0 << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
2999 (1 << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3003 * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
3004 * priv->lock must be held by the caller
3006 static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
3007 u16 ssn_idx, u8 tx_fifo)
3011 if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3012 IWL_WARNING("queue number too small: %d, must be > %d\n",
3013 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3017 ret = iwl_grab_nic_access(priv);
3021 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3023 iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3025 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3026 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3027 /* supposes that ssn_idx is valid (!= 0xFFF) */
3028 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3030 iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3031 iwl_txq_ctx_deactivate(priv, txq_id);
3032 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3034 iwl_release_nic_access(priv);
3039 int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
3042 struct iwl_queue *q = &priv->txq[txq_id].q;
3043 u8 *addr = priv->stations[sta_id].sta.sta.addr;
3044 struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3046 switch (priv->stations[sta_id].tid[tid].agg.state) {
3047 case IWL_EMPTYING_HW_QUEUE_DELBA:
3048 /* We are reclaiming the last packet of the */
3049 /* aggregated HW queue */
3050 if (txq_id == tid_data->agg.txq_id &&
3051 q->read_ptr == q->write_ptr) {
3052 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3053 int tx_fifo = default_tid_to_tx_fifo[tid];
3054 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3055 iwl4965_tx_queue_agg_disable(priv, txq_id,
3057 tid_data->agg.state = IWL_AGG_OFF;
3058 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3061 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3062 /* We are reclaiming the last packet of the queue */
3063 if (tid_data->tfds_in_queue == 0) {
3064 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3065 tid_data->agg.state = IWL_AGG_ON;
3066 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3074 * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3076 * Handles block-acknowledge notification from device, which reports success
3077 * of frames sent via aggregation.
3079 static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
3080 struct iwl_rx_mem_buffer *rxb)
3082 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3083 struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
3085 struct iwl_tx_queue *txq = NULL;
3086 struct iwl_ht_agg *agg;
3087 DECLARE_MAC_BUF(mac);
3089 /* "flow" corresponds to Tx queue */
3090 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3092 /* "ssn" is start of block-ack Tx window, corresponds to index
3093 * (in Tx queue's circular buffer) of first TFD/frame in window */
3094 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3096 if (scd_flow >= priv->hw_params.max_txq_num) {
3097 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3101 txq = &priv->txq[scd_flow];
3102 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
3104 /* Find index just before block-ack window */
3105 index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
3107 /* TODO: Need to get this copy more safely - now good for debug */
3109 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3112 print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
3114 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
3115 "%d, scd_ssn = %d\n",
3118 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
3121 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
3123 (unsigned long long)agg->bitmap);
3125 /* Update driver's record of ACK vs. not for each frame in window */
3126 iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
3128 /* Release all TFDs before the SSN, i.e. all TFDs in front of
3129 * block-ack window (we assume that they've been successfully
3130 * transmitted ... if not, it's too late anyway). */
3131 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3132 /* calculate mac80211 ampdu sw queue to wake */
3134 scd_flow - IWL_BACK_QUEUE_FIRST_ID + priv->hw->queues;
3135 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3136 priv->stations[ba_resp->sta_id].
3137 tid[ba_resp->tid].tfds_in_queue -= freed;
3138 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
3139 priv->mac80211_registered &&
3140 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3141 ieee80211_wake_queue(priv->hw, ampdu_q);
3142 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3143 ba_resp->tid, scd_flow);
3148 * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3150 static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
3157 scd_q2ratid = ra_tid & IWL49_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3159 tbl_dw_addr = priv->scd_base_addr +
3160 IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3162 tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
3165 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3167 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3169 iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
3176 * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3178 * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3179 * i.e. it must be one of the higher queues used for aggregation
3181 static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
3182 int tx_fifo, int sta_id, int tid,
3185 unsigned long flags;
3189 if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3190 IWL_WARNING("queue number too small: %d, must be > %d\n",
3191 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3193 ra_tid = BUILD_RAxTID(sta_id, tid);
3195 /* Modify device's station table to Tx this TID */
3196 iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
3198 spin_lock_irqsave(&priv->lock, flags);
3199 rc = iwl_grab_nic_access(priv);
3201 spin_unlock_irqrestore(&priv->lock, flags);
3205 /* Stop this Tx queue before configuring it */
3206 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3208 /* Map receiver-address / traffic-ID to this queue */
3209 iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3211 /* Set this queue as a chain-building queue */
3212 iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3214 /* Place first TFD at index corresponding to start sequence number.
3215 * Assumes that ssn_idx is valid (!= 0xFFF) */
3216 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3217 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3218 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3220 /* Set up Tx window size and frame limit for this queue */
3221 iwl_write_targ_mem(priv,
3222 priv->scd_base_addr + IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3223 (SCD_WIN_SIZE << IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3224 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3226 iwl_write_targ_mem(priv, priv->scd_base_addr +
3227 IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3228 (SCD_FRAME_LIMIT << IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3229 & IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3231 iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3233 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
3234 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3236 iwl_release_nic_access(priv);
3237 spin_unlock_irqrestore(&priv->lock, flags);
3242 #endif /* CONFIG_IWL4965_HT */
3245 #ifdef CONFIG_IWL4965_HT
3246 static int iwl4965_rx_agg_start(struct iwl_priv *priv,
3247 const u8 *addr, int tid, u16 ssn)
3249 unsigned long flags;
3252 sta_id = iwl_find_station(priv, addr);
3253 if (sta_id == IWL_INVALID_STATION)
3256 spin_lock_irqsave(&priv->sta_lock, flags);
3257 priv->stations[sta_id].sta.station_flags_msk = 0;
3258 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
3259 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
3260 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3261 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3262 spin_unlock_irqrestore(&priv->sta_lock, flags);
3264 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
3268 static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
3269 const u8 *addr, int tid)
3271 unsigned long flags;
3274 sta_id = iwl_find_station(priv, addr);
3275 if (sta_id == IWL_INVALID_STATION)
3278 spin_lock_irqsave(&priv->sta_lock, flags);
3279 priv->stations[sta_id].sta.station_flags_msk = 0;
3280 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
3281 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
3282 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3283 spin_unlock_irqrestore(&priv->sta_lock, flags);
3285 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
3290 * Find first available (lowest unused) Tx Queue, mark it "active".
3291 * Called only when finding queue for aggregation.
3292 * Should never return anything < 7, because they should already
3293 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
3295 static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
3299 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
3300 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
3305 static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
3306 u16 tid, u16 *start_seq_num)
3308 struct iwl_priv *priv = hw->priv;
3314 unsigned long flags;
3315 struct iwl_tid_data *tid_data;
3316 DECLARE_MAC_BUF(mac);
3318 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
3319 tx_fifo = default_tid_to_tx_fifo[tid];
3323 IWL_WARNING("%s on ra = %s tid = %d\n",
3324 __func__, print_mac(mac, ra), tid);
3326 sta_id = iwl_find_station(priv, ra);
3327 if (sta_id == IWL_INVALID_STATION)
3330 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
3331 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
3335 txq_id = iwl4965_txq_ctx_activate_free(priv);
3339 spin_lock_irqsave(&priv->sta_lock, flags);
3340 tid_data = &priv->stations[sta_id].tid[tid];
3341 ssn = SEQ_TO_SN(tid_data->seq_number);
3342 tid_data->agg.txq_id = txq_id;
3343 spin_unlock_irqrestore(&priv->sta_lock, flags);
3345 *start_seq_num = ssn;
3346 ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
3352 if (tid_data->tfds_in_queue == 0) {
3353 printk(KERN_ERR "HW queue is empty\n");
3354 tid_data->agg.state = IWL_AGG_ON;
3355 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
3357 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
3358 tid_data->tfds_in_queue);
3359 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3364 static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
3366 struct iwl_priv *priv = hw->priv;
3367 int tx_fifo_id, txq_id, sta_id, ssn = -1;
3368 struct iwl_tid_data *tid_data;
3369 int ret, write_ptr, read_ptr;
3370 unsigned long flags;
3371 DECLARE_MAC_BUF(mac);
3374 IWL_ERROR("ra = NULL\n");
3378 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
3379 tx_fifo_id = default_tid_to_tx_fifo[tid];
3383 sta_id = iwl_find_station(priv, ra);
3385 if (sta_id == IWL_INVALID_STATION)
3388 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
3389 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
3391 tid_data = &priv->stations[sta_id].tid[tid];
3392 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
3393 txq_id = tid_data->agg.txq_id;
3394 write_ptr = priv->txq[txq_id].q.write_ptr;
3395 read_ptr = priv->txq[txq_id].q.read_ptr;
3397 /* The queue is not empty */
3398 if (write_ptr != read_ptr) {
3399 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
3400 priv->stations[sta_id].tid[tid].agg.state =
3401 IWL_EMPTYING_HW_QUEUE_DELBA;
3405 IWL_DEBUG_HT("HW queue is empty\n");
3406 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
3408 spin_lock_irqsave(&priv->lock, flags);
3409 ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
3410 spin_unlock_irqrestore(&priv->lock, flags);
3415 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
3420 int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
3421 enum ieee80211_ampdu_mlme_action action,
3422 const u8 *addr, u16 tid, u16 *ssn)
3424 struct iwl_priv *priv = hw->priv;
3425 DECLARE_MAC_BUF(mac);
3427 IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
3428 print_mac(mac, addr), tid);
3431 case IEEE80211_AMPDU_RX_START:
3432 IWL_DEBUG_HT("start Rx\n");
3433 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
3434 case IEEE80211_AMPDU_RX_STOP:
3435 IWL_DEBUG_HT("stop Rx\n");
3436 return iwl4965_rx_agg_stop(priv, addr, tid);
3437 case IEEE80211_AMPDU_TX_START:
3438 IWL_DEBUG_HT("start Tx\n");
3439 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
3440 case IEEE80211_AMPDU_TX_STOP:
3441 IWL_DEBUG_HT("stop Tx\n");
3442 return iwl4965_tx_agg_stop(hw, addr, tid);
3444 IWL_DEBUG_HT("unknown\n");
3450 #endif /* CONFIG_IWL4965_HT */
3453 static u16 iwl4965_get_hcmd_size(u8 cmd_id, u16 len)
3457 return (u16) sizeof(struct iwl4965_rxon_cmd);
3463 static u16 iwl4965_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
3465 struct iwl4965_addsta_cmd *addsta = (struct iwl4965_addsta_cmd *)data;
3466 addsta->mode = cmd->mode;
3467 memcpy(&addsta->sta, &cmd->sta, sizeof(struct sta_id_modify));
3468 memcpy(&addsta->key, &cmd->key, sizeof(struct iwl4965_keyinfo));
3469 addsta->station_flags = cmd->station_flags;
3470 addsta->station_flags_msk = cmd->station_flags_msk;
3471 addsta->tid_disable_tx = cmd->tid_disable_tx;
3472 addsta->add_immediate_ba_tid = cmd->add_immediate_ba_tid;
3473 addsta->remove_immediate_ba_tid = cmd->remove_immediate_ba_tid;
3474 addsta->add_immediate_ba_ssn = cmd->add_immediate_ba_ssn;
3475 addsta->reserved1 = __constant_cpu_to_le16(0);
3476 addsta->reserved2 = __constant_cpu_to_le32(0);
3478 return (u16)sizeof(struct iwl4965_addsta_cmd);
3481 #ifdef CONFIG_IWL4965_HT
3482 static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
3484 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3485 tx_resp->frame_count);
3486 return le32_to_cpu(*scd_ssn) & MAX_SN;
3491 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3493 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
3494 struct iwl_ht_agg *agg,
3495 struct iwl4965_tx_resp_agg *tx_resp,
3499 struct agg_tx_status *frame_status = &tx_resp->status;
3500 struct ieee80211_tx_info *info = NULL;
3501 struct ieee80211_hdr *hdr = NULL;
3506 if (agg->wait_for_ba)
3507 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
3509 agg->frame_count = tx_resp->frame_count;
3510 agg->start_idx = start_idx;
3511 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3514 /* # frames attempted by Tx command */
3515 if (agg->frame_count == 1) {
3516 /* Only one frame was attempted; no block-ack will arrive */
3517 status = le16_to_cpu(frame_status[0].status);
3518 seq = le16_to_cpu(frame_status[0].sequence);
3519 idx = SEQ_TO_INDEX(seq);
3520 txq_id = SEQ_TO_QUEUE(seq);
3522 /* FIXME: code repetition */
3523 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3524 agg->frame_count, agg->start_idx, idx);
3526 info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
3527 info->status.retry_count = tx_resp->failure_frame;
3528 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3529 info->flags |= iwl_is_tx_success(status)?
3530 IEEE80211_TX_STAT_ACK : 0;
3531 iwl4965_hwrate_to_tx_control(priv,
3532 le32_to_cpu(tx_resp->rate_n_flags),
3534 /* FIXME: code repetition end */
3536 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3537 status & 0xff, tx_resp->failure_frame);
3538 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
3539 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
3541 agg->wait_for_ba = 0;
3543 /* Two or more frames were attempted; expect block-ack */
3545 int start = agg->start_idx;
3547 /* Construct bit-map of pending frames within Tx window */
3548 for (i = 0; i < agg->frame_count; i++) {
3550 status = le16_to_cpu(frame_status[i].status);
3551 seq = le16_to_cpu(frame_status[i].sequence);
3552 idx = SEQ_TO_INDEX(seq);
3553 txq_id = SEQ_TO_QUEUE(seq);
3555 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3556 AGG_TX_STATE_ABORT_MSK))
3559 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3560 agg->frame_count, txq_id, idx);
3562 hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
3564 sc = le16_to_cpu(hdr->seq_ctrl);
3565 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3566 IWL_ERROR("BUG_ON idx doesn't match seq control"
3567 " idx=%d, seq_idx=%d, seq=%d\n",
3573 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3574 i, idx, SEQ_TO_SN(sc));
3578 sh = (start - idx) + 0xff;
3579 bitmap = bitmap << sh;
3582 } else if (sh < -64)
3583 sh = 0xff - (start - idx);
3587 bitmap = bitmap << sh;
3590 bitmap |= (1 << sh);
3591 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3592 start, (u32)(bitmap & 0xFFFFFFFF));
3595 agg->bitmap = bitmap;
3596 agg->start_idx = start;
3597 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3598 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
3599 agg->frame_count, agg->start_idx,
3600 (unsigned long long)agg->bitmap);
3603 agg->wait_for_ba = 1;
3610 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3612 static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
3613 struct iwl_rx_mem_buffer *rxb)
3615 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3616 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3617 int txq_id = SEQ_TO_QUEUE(sequence);
3618 int index = SEQ_TO_INDEX(sequence);
3619 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3620 struct ieee80211_tx_info *info;
3621 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3622 u32 status = le32_to_cpu(tx_resp->status);
3623 #ifdef CONFIG_IWL4965_HT
3624 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3626 struct ieee80211_hdr *hdr;
3630 if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
3631 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3632 "is out of range [0-%d] %d %d\n", txq_id,
3633 index, txq->q.n_bd, txq->q.write_ptr,
3638 info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
3639 memset(&info->status, 0, sizeof(info->status));
3641 #ifdef CONFIG_IWL4965_HT
3642 hdr = iwl_tx_queue_get_hdr(priv, txq_id, index);
3643 fc = le16_to_cpu(hdr->frame_control);
3644 if (ieee80211_is_qos_data(fc)) {
3645 qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
3649 sta_id = iwl_get_ra_sta_id(priv, hdr);
3650 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3651 IWL_ERROR("Station not known\n");
3655 if (txq->sched_retry) {
3656 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3657 struct iwl_ht_agg *agg = NULL;
3662 agg = &priv->stations[sta_id].tid[tid].agg;
3664 iwl4965_tx_status_reply_tx(priv, agg,
3665 (struct iwl4965_tx_resp_agg *)tx_resp, index);
3667 if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status)) {
3668 /* TODO: send BAR */
3671 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3673 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
3674 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3675 "%d index %d\n", scd_ssn , index);
3676 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3677 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3679 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
3680 txq_id >= 0 && priv->mac80211_registered &&
3681 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
3682 /* calculate mac80211 ampdu sw queue to wake */
3683 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
3685 if (agg->state == IWL_AGG_OFF)
3686 ieee80211_wake_queue(priv->hw, txq_id);
3688 ieee80211_wake_queue(priv->hw, ampdu_q);
3690 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3693 #endif /* CONFIG_IWL4965_HT */
3695 info->status.retry_count = tx_resp->failure_frame;
3696 info->flags |= iwl_is_tx_success(status) ? IEEE80211_TX_STAT_ACK : 0;
3697 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3700 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
3701 "retries %d\n", txq_id, iwl_get_tx_fail_reason(status),
3702 status, le32_to_cpu(tx_resp->rate_n_flags),
3703 tx_resp->failure_frame);
3705 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3706 #ifdef CONFIG_IWL4965_HT
3708 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3709 if (tid != MAX_TID_COUNT)
3710 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3711 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
3712 (txq_id >= 0) && priv->mac80211_registered)
3713 ieee80211_wake_queue(priv->hw, txq_id);
3714 if (tid != MAX_TID_COUNT)
3715 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3718 #endif /* CONFIG_IWL4965_HT */
3720 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3721 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3725 /* Set up 4965-specific Rx frame reply handlers */
3726 static void iwl4965_rx_handler_setup(struct iwl_priv *priv)
3728 /* Legacy Rx frames */
3729 priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
3731 /* High-throughput (HT) Rx frames */
3732 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
3733 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
3735 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
3737 #ifdef CONFIG_IWL4965_HT
3738 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
3739 #endif /* CONFIG_IWL4965_HT */
3742 void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
3744 INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
3745 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3746 INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
3748 init_timer(&priv->statistics_periodic);
3749 priv->statistics_periodic.data = (unsigned long)priv;
3750 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
3753 void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
3755 del_timer_sync(&priv->statistics_periodic);
3757 cancel_delayed_work(&priv->init_alive_start);
3761 static struct iwl_hcmd_ops iwl4965_hcmd = {
3762 .rxon_assoc = iwl4965_send_rxon_assoc,
3765 static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
3766 .get_hcmd_size = iwl4965_get_hcmd_size,
3767 .build_addsta_hcmd = iwl4965_build_addsta_hcmd,
3768 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3769 .chain_noise_reset = iwl4965_chain_noise_reset,
3770 .gain_computation = iwl4965_gain_computation,
3774 static struct iwl_lib_ops iwl4965_lib = {
3775 .set_hw_params = iwl4965_hw_set_hw_params,
3776 .alloc_shared_mem = iwl4965_alloc_shared_mem,
3777 .free_shared_mem = iwl4965_free_shared_mem,
3778 .shared_mem_rx_idx = iwl4965_shared_mem_rx_idx,
3779 .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
3780 .txq_set_sched = iwl4965_txq_set_sched,
3781 .rx_handler_setup = iwl4965_rx_handler_setup,
3782 .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
3783 .alive_notify = iwl4965_alive_notify,
3784 .init_alive_start = iwl4965_init_alive_start,
3785 .load_ucode = iwl4965_load_bsm,
3787 .init = iwl4965_apm_init,
3788 .reset = iwl4965_apm_reset,
3789 .stop = iwl4965_apm_stop,
3790 .config = iwl4965_nic_config,
3791 .set_pwr_src = iwl4965_set_pwr_src,
3794 .regulatory_bands = {
3795 EEPROM_REGULATORY_BAND_1_CHANNELS,
3796 EEPROM_REGULATORY_BAND_2_CHANNELS,
3797 EEPROM_REGULATORY_BAND_3_CHANNELS,
3798 EEPROM_REGULATORY_BAND_4_CHANNELS,
3799 EEPROM_REGULATORY_BAND_5_CHANNELS,
3800 EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
3801 EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
3803 .verify_signature = iwlcore_eeprom_verify_signature,
3804 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
3805 .release_semaphore = iwlcore_eeprom_release_semaphore,
3806 .check_version = iwl4965_eeprom_check_version,
3807 .query_addr = iwlcore_eeprom_query_addr,
3809 .radio_kill_sw = iwl4965_radio_kill_sw,
3810 .set_power = iwl4965_set_power,
3811 .update_chain_flags = iwl4965_update_chain_flags,
3814 static struct iwl_ops iwl4965_ops = {
3815 .lib = &iwl4965_lib,
3816 .hcmd = &iwl4965_hcmd,
3817 .utils = &iwl4965_hcmd_utils,
3820 struct iwl_cfg iwl4965_agn_cfg = {
3822 .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
3823 .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
3824 .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
3825 .ops = &iwl4965_ops,
3826 .mod_params = &iwl4965_mod_params,
3829 module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
3830 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
3831 module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
3832 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
3833 module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
3834 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
3835 module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
3836 MODULE_PARM_DESC(debug, "debug output mask");
3838 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
3839 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
3841 module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
3842 MODULE_PARM_DESC(queues_num, "number of hw queues.");
3845 module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
3846 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
3847 module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
3848 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
3849 module_param_named(fw_restart4965, iwl4965_mod_params.restart_fw, int, 0444);
3850 MODULE_PARM_DESC(fw_restart4965, "restart firmware in case of error");