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 /* make sure bootstrap program is no larger than BSM's SRAM size */
160 if (len > IWL_MAX_BSM_SIZE)
163 /* Tell bootstrap uCode where to find the "Initialize" uCode
164 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
165 * NOTE: iwl4965_initialize_alive_start() will replace these values,
166 * after the "initialize" uCode has run, to point to
167 * runtime/protocol instructions and backup data cache. */
168 pinst = priv->ucode_init.p_addr >> 4;
169 pdata = priv->ucode_init_data.p_addr >> 4;
170 inst_len = priv->ucode_init.len;
171 data_len = priv->ucode_init_data.len;
173 ret = iwl_grab_nic_access(priv);
177 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
178 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
179 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
180 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
182 /* Fill BSM memory with bootstrap instructions */
183 for (reg_offset = BSM_SRAM_LOWER_BOUND;
184 reg_offset < BSM_SRAM_LOWER_BOUND + len;
185 reg_offset += sizeof(u32), image++)
186 _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
188 ret = iwl4965_verify_bsm(priv);
190 iwl_release_nic_access(priv);
194 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
195 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
196 iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
197 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
199 /* Load bootstrap code into instruction SRAM now,
200 * to prepare to load "initialize" uCode */
201 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
203 /* Wait for load of bootstrap uCode to finish */
204 for (i = 0; i < 100; i++) {
205 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
206 if (!(done & BSM_WR_CTRL_REG_BIT_START))
211 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
213 IWL_ERROR("BSM write did not complete!\n");
217 /* Enable future boot loads whenever power management unit triggers it
218 * (e.g. when powering back up after power-save shutdown) */
219 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
221 iwl_release_nic_access(priv);
227 * iwl4965_set_ucode_ptrs - Set uCode address location
229 * Tell initialization uCode where to find runtime uCode.
231 * BSM registers initially contain pointers to initialization uCode.
232 * We need to replace them to load runtime uCode inst and data,
233 * and to save runtime data when powering down.
235 static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
242 /* bits 35:4 for 4965 */
243 pinst = priv->ucode_code.p_addr >> 4;
244 pdata = priv->ucode_data_backup.p_addr >> 4;
246 spin_lock_irqsave(&priv->lock, flags);
247 ret = iwl_grab_nic_access(priv);
249 spin_unlock_irqrestore(&priv->lock, flags);
253 /* Tell bootstrap uCode where to find image to load */
254 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
255 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
256 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
257 priv->ucode_data.len);
259 /* Inst bytecount must be last to set up, bit 31 signals uCode
260 * that all new ptr/size info is in place */
261 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
262 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
263 iwl_release_nic_access(priv);
265 spin_unlock_irqrestore(&priv->lock, flags);
267 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
273 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
275 * Called after REPLY_ALIVE notification received from "initialize" uCode.
277 * The 4965 "initialize" ALIVE reply contains calibration data for:
278 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
279 * (3945 does not contain this data).
281 * Tell "initialize" uCode to go ahead and load the runtime uCode.
283 static void iwl4965_init_alive_start(struct iwl_priv *priv)
285 /* Check alive response for "valid" sign from uCode */
286 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
287 /* We had an error bringing up the hardware, so take it
288 * all the way back down so we can try again */
289 IWL_DEBUG_INFO("Initialize Alive failed.\n");
293 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
294 * This is a paranoid check, because we would not have gotten the
295 * "initialize" alive if code weren't properly loaded. */
296 if (iwl_verify_ucode(priv)) {
297 /* Runtime instruction load was bad;
298 * take it all the way back down so we can try again */
299 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
303 /* Calculate temperature */
304 priv->temperature = iwl4965_get_temperature(priv);
306 /* Send pointers to protocol/runtime uCode image ... init code will
307 * load and launch runtime uCode, which will send us another "Alive"
309 IWL_DEBUG_INFO("Initialization Alive received.\n");
310 if (iwl4965_set_ucode_ptrs(priv)) {
311 /* Runtime instruction load won't happen;
312 * take it all the way back down so we can try again */
313 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
319 queue_work(priv->workqueue, &priv->restart);
322 static int is_fat_channel(__le32 rxon_flags)
324 return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
325 (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
328 int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
332 /* 4965 HT rate format */
333 if (rate_n_flags & RATE_MCS_HT_MSK) {
334 idx = (rate_n_flags & 0xff);
336 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
337 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
339 idx += IWL_FIRST_OFDM_RATE;
340 /* skip 9M not supported in ht*/
341 if (idx >= IWL_RATE_9M_INDEX)
343 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
346 /* 4965 legacy rate format, search for match in table */
348 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
349 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
357 * translate ucode response to mac80211 tx status control values
359 void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
360 struct ieee80211_tx_control *control)
364 control->antenna_sel_tx =
365 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
366 if (rate_n_flags & RATE_MCS_HT_MSK)
367 control->flags |= IEEE80211_TXCTL_OFDM_HT;
368 if (rate_n_flags & RATE_MCS_GF_MSK)
369 control->flags |= IEEE80211_TXCTL_GREEN_FIELD;
370 if (rate_n_flags & RATE_MCS_FAT_MSK)
371 control->flags |= IEEE80211_TXCTL_40_MHZ_WIDTH;
372 if (rate_n_flags & RATE_MCS_DUP_MSK)
373 control->flags |= IEEE80211_TXCTL_DUP_DATA;
374 if (rate_n_flags & RATE_MCS_SGI_MSK)
375 control->flags |= IEEE80211_TXCTL_SHORT_GI;
376 /* since iwl4965_hwrate_to_plcp_idx is band indifferent, we always use
377 * IEEE80211_BAND_2GHZ band as it contains all the rates */
378 rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
379 if (rate_index == -1)
380 control->tx_rate = NULL;
383 &priv->bands[IEEE80211_BAND_2GHZ].bitrates[rate_index];
386 int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
391 spin_lock_irqsave(&priv->lock, flags);
392 rc = iwl_grab_nic_access(priv);
394 spin_unlock_irqrestore(&priv->lock, flags);
399 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
400 rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
403 IWL_ERROR("Can't stop Rx DMA.\n");
405 iwl_release_nic_access(priv);
406 spin_unlock_irqrestore(&priv->lock, flags);
415 static int iwl4965_eeprom_check_version(struct iwl_priv *priv)
420 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
422 calib_ver = iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
424 if (eeprom_ver < EEPROM_4965_EEPROM_VERSION ||
425 calib_ver < EEPROM_4965_TX_POWER_VERSION)
430 IWL_ERROR("Unsuported EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
431 eeprom_ver, EEPROM_4965_EEPROM_VERSION,
432 calib_ver, EEPROM_4965_TX_POWER_VERSION);
436 int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
441 spin_lock_irqsave(&priv->lock, flags);
442 ret = iwl_grab_nic_access(priv);
444 spin_unlock_irqrestore(&priv->lock, flags);
448 if (src == IWL_PWR_SRC_VAUX) {
450 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
453 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
454 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
455 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
456 ~APMG_PS_CTRL_MSK_PWR_SRC);
459 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
460 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
461 ~APMG_PS_CTRL_MSK_PWR_SRC);
464 iwl_release_nic_access(priv);
465 spin_unlock_irqrestore(&priv->lock, flags);
470 static int iwl4965_disable_tx_fifo(struct iwl_priv *priv)
475 spin_lock_irqsave(&priv->lock, flags);
477 ret = iwl_grab_nic_access(priv);
479 IWL_ERROR("Tx fifo reset failed");
480 spin_unlock_irqrestore(&priv->lock, flags);
484 iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
485 iwl_release_nic_access(priv);
486 spin_unlock_irqrestore(&priv->lock, flags);
491 static int iwl4965_apm_init(struct iwl_priv *priv)
495 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
496 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
498 /* set "initialization complete" bit to move adapter
499 * D0U* --> D0A* state */
500 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
502 /* wait for clock stabilization */
503 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
504 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
505 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
507 IWL_DEBUG_INFO("Failed to init the card\n");
511 ret = iwl_grab_nic_access(priv);
516 iwl_write_prph(priv, APMG_CLK_CTRL_REG,
517 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
521 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
522 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
524 iwl_release_nic_access(priv);
530 static void iwl4965_nic_config(struct iwl_priv *priv)
537 spin_lock_irqsave(&priv->lock, flags);
539 if ((priv->rev_id & 0x80) == 0x80 && (priv->rev_id & 0x7f) < 8) {
540 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
541 /* Enable No Snoop field */
542 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
546 pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
548 /* disable L1 entry -- workaround for pre-B1 */
549 pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
551 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
553 /* write radio config values to register */
554 if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) == EEPROM_4965_RF_CFG_TYPE_MAX)
555 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
556 EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
557 EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
558 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
560 /* set CSR_HW_CONFIG_REG for uCode use */
561 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
562 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
563 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
565 priv->calib_info = (struct iwl_eeprom_calib_info *)
566 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
568 spin_unlock_irqrestore(&priv->lock, flags);
571 int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
577 spin_lock_irqsave(&priv->lock, flags);
579 /* set stop master bit */
580 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
582 reg_val = iwl_read32(priv, CSR_GP_CNTRL);
584 if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
585 (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
586 IWL_DEBUG_INFO("Card in power save, master is already "
589 rc = iwl_poll_bit(priv, CSR_RESET,
590 CSR_RESET_REG_FLAG_MASTER_DISABLED,
591 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
593 spin_unlock_irqrestore(&priv->lock, flags);
598 spin_unlock_irqrestore(&priv->lock, flags);
599 IWL_DEBUG_INFO("stop master\n");
605 * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
607 void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
613 /* Stop each Tx DMA channel, and wait for it to be idle */
614 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
615 spin_lock_irqsave(&priv->lock, flags);
616 if (iwl_grab_nic_access(priv)) {
617 spin_unlock_irqrestore(&priv->lock, flags);
621 iwl_write_direct32(priv,
622 FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
623 iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
624 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
626 iwl_release_nic_access(priv);
627 spin_unlock_irqrestore(&priv->lock, flags);
630 /* Deallocate memory for all Tx queues */
631 iwl_hw_txq_ctx_free(priv);
634 int iwl4965_hw_nic_reset(struct iwl_priv *priv)
639 iwl4965_hw_nic_stop_master(priv);
641 spin_lock_irqsave(&priv->lock, flags);
643 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
647 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
648 rc = iwl_poll_bit(priv, CSR_RESET,
649 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
650 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
654 rc = iwl_grab_nic_access(priv);
656 iwl_write_prph(priv, APMG_CLK_EN_REG,
657 APMG_CLK_VAL_DMA_CLK_RQT |
658 APMG_CLK_VAL_BSM_CLK_RQT);
662 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
663 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
665 iwl_release_nic_access(priv);
668 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
669 wake_up_interruptible(&priv->wait_command_queue);
671 spin_unlock_irqrestore(&priv->lock, flags);
677 #define REG_RECALIB_PERIOD (60)
680 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
682 * This callback is provided in order to send a statistics request.
684 * This timer function is continually reset to execute within
685 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
686 * was received. We need to ensure we receive the statistics in order
687 * to update the temperature used for calibrating the TXPOWER.
689 static void iwl4965_bg_statistics_periodic(unsigned long data)
691 struct iwl_priv *priv = (struct iwl_priv *)data;
693 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
696 iwl_send_statistics_request(priv, CMD_ASYNC);
699 void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
701 struct iwl4965_ct_kill_config cmd;
705 spin_lock_irqsave(&priv->lock, flags);
706 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
707 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
708 spin_unlock_irqrestore(&priv->lock, flags);
710 cmd.critical_temperature_R =
711 cpu_to_le32(priv->hw_params.ct_kill_threshold);
713 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
716 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
718 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
719 "critical temperature is %d\n",
720 cmd.critical_temperature_R);
723 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
725 /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
726 * Called after every association, but this runs only once!
727 * ... once chain noise is calibrated the first time, it's good forever. */
728 static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
730 struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
732 if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
733 struct iwl4965_calibration_cmd cmd;
735 memset(&cmd, 0, sizeof(cmd));
736 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
740 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
742 IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
743 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
744 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
748 static void iwl4965_gain_computation(struct iwl_priv *priv,
750 u16 min_average_noise_antenna_i,
751 u32 min_average_noise)
754 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
756 data->delta_gain_code[min_average_noise_antenna_i] = 0;
758 for (i = 0; i < NUM_RX_CHAINS; i++) {
761 if (!(data->disconn_array[i]) &&
762 (data->delta_gain_code[i] ==
763 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
764 delta_g = average_noise[i] - min_average_noise;
765 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
766 data->delta_gain_code[i] =
767 min(data->delta_gain_code[i],
768 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
770 data->delta_gain_code[i] =
771 (data->delta_gain_code[i] | (1 << 2));
773 data->delta_gain_code[i] = 0;
776 IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
777 data->delta_gain_code[0],
778 data->delta_gain_code[1],
779 data->delta_gain_code[2]);
781 /* Differential gain gets sent to uCode only once */
782 if (!data->radio_write) {
783 struct iwl4965_calibration_cmd cmd;
784 data->radio_write = 1;
786 memset(&cmd, 0, sizeof(cmd));
787 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
788 cmd.diff_gain_a = data->delta_gain_code[0];
789 cmd.diff_gain_b = data->delta_gain_code[1];
790 cmd.diff_gain_c = data->delta_gain_code[2];
791 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
794 IWL_DEBUG_CALIB("fail sending cmd "
795 "REPLY_PHY_CALIBRATION_CMD \n");
797 /* TODO we might want recalculate
798 * rx_chain in rxon cmd */
800 /* Mark so we run this algo only once! */
801 data->state = IWL_CHAIN_NOISE_CALIBRATED;
803 data->chain_noise_a = 0;
804 data->chain_noise_b = 0;
805 data->chain_noise_c = 0;
806 data->chain_signal_a = 0;
807 data->chain_signal_b = 0;
808 data->chain_signal_c = 0;
809 data->beacon_count = 0;
812 static void iwl4965_bg_sensitivity_work(struct work_struct *work)
814 struct iwl_priv *priv = container_of(work, struct iwl_priv,
817 mutex_lock(&priv->mutex);
819 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
820 test_bit(STATUS_SCANNING, &priv->status)) {
821 mutex_unlock(&priv->mutex);
825 if (priv->start_calib) {
826 iwl_chain_noise_calibration(priv, &priv->statistics);
828 iwl_sensitivity_calibration(priv, &priv->statistics);
831 mutex_unlock(&priv->mutex);
834 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
836 static void iwl4965_bg_txpower_work(struct work_struct *work)
838 struct iwl_priv *priv = container_of(work, struct iwl_priv,
841 /* If a scan happened to start before we got here
842 * then just return; the statistics notification will
843 * kick off another scheduled work to compensate for
844 * any temperature delta we missed here. */
845 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
846 test_bit(STATUS_SCANNING, &priv->status))
849 mutex_lock(&priv->mutex);
851 /* Regardless of if we are assocaited, we must reconfigure the
852 * TX power since frames can be sent on non-radar channels while
854 iwl4965_hw_reg_send_txpower(priv);
856 /* Update last_temperature to keep is_calib_needed from running
857 * when it isn't needed... */
858 priv->last_temperature = priv->temperature;
860 mutex_unlock(&priv->mutex);
864 * Acquire priv->lock before calling this function !
866 static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
868 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
869 (index & 0xff) | (txq_id << 8));
870 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
874 * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
875 * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
876 * @scd_retry: (1) Indicates queue will be used in aggregation mode
878 * NOTE: Acquire priv->lock before calling this function !
880 static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
881 struct iwl_tx_queue *txq,
882 int tx_fifo_id, int scd_retry)
884 int txq_id = txq->q.id;
886 /* Find out whether to activate Tx queue */
887 int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
889 /* Set up and activate */
890 iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
891 (active << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
892 (tx_fifo_id << IWL49_SCD_QUEUE_STTS_REG_POS_TXF) |
893 (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_WSL) |
894 (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
895 IWL49_SCD_QUEUE_STTS_REG_MSK);
897 txq->sched_retry = scd_retry;
899 IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
900 active ? "Activate" : "Deactivate",
901 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
904 static const u16 default_queue_to_tx_fifo[] = {
914 static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
916 set_bit(txq_id, &priv->txq_ctx_active_msk);
919 static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
921 clear_bit(txq_id, &priv->txq_ctx_active_msk);
924 int iwl4965_alive_notify(struct iwl_priv *priv)
931 spin_lock_irqsave(&priv->lock, flags);
933 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
934 memset(&(priv->sensitivity_data), 0,
935 sizeof(struct iwl_sensitivity_data));
936 memset(&(priv->chain_noise_data), 0,
937 sizeof(struct iwl_chain_noise_data));
938 for (i = 0; i < NUM_RX_CHAINS; i++)
939 priv->chain_noise_data.delta_gain_code[i] =
940 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
941 #endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
942 ret = iwl_grab_nic_access(priv);
944 spin_unlock_irqrestore(&priv->lock, flags);
948 /* Clear 4965's internal Tx Scheduler data base */
949 priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
950 a = priv->scd_base_addr + IWL49_SCD_CONTEXT_DATA_OFFSET;
951 for (; a < priv->scd_base_addr + IWL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
952 iwl_write_targ_mem(priv, a, 0);
953 for (; a < priv->scd_base_addr + IWL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
954 iwl_write_targ_mem(priv, a, 0);
955 for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
956 iwl_write_targ_mem(priv, a, 0);
958 /* Tel 4965 where to find Tx byte count tables */
959 iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
961 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
963 /* Disable chain mode for all queues */
964 iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
966 /* Initialize each Tx queue (including the command queue) */
967 for (i = 0; i < priv->hw_params.max_txq_num; i++) {
969 /* TFD circular buffer read/write indexes */
970 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
971 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
973 /* Max Tx Window size for Scheduler-ACK mode */
974 iwl_write_targ_mem(priv, priv->scd_base_addr +
975 IWL49_SCD_CONTEXT_QUEUE_OFFSET(i),
977 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
978 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
981 iwl_write_targ_mem(priv, priv->scd_base_addr +
982 IWL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
985 IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
986 IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
989 iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
990 (1 << priv->hw_params.max_txq_num) - 1);
992 /* Activate all Tx DMA/FIFO channels */
993 iwl_write_prph(priv, IWL49_SCD_TXFACT,
994 SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
996 iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
998 /* Map each Tx/cmd queue to its corresponding fifo */
999 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
1000 int ac = default_queue_to_tx_fifo[i];
1001 iwl4965_txq_ctx_activate(priv, i);
1002 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
1005 iwl_release_nic_access(priv);
1006 spin_unlock_irqrestore(&priv->lock, flags);
1008 /* Ask for statistics now, the uCode will send statistics notification
1009 * periodically after association */
1010 iwl_send_statistics_request(priv, CMD_ASYNC);
1014 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1015 static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
1019 .auto_corr_min_ofdm = 85,
1020 .auto_corr_min_ofdm_mrc = 170,
1021 .auto_corr_min_ofdm_x1 = 105,
1022 .auto_corr_min_ofdm_mrc_x1 = 220,
1024 .auto_corr_max_ofdm = 120,
1025 .auto_corr_max_ofdm_mrc = 210,
1026 .auto_corr_max_ofdm_x1 = 140,
1027 .auto_corr_max_ofdm_mrc_x1 = 270,
1029 .auto_corr_min_cck = 125,
1030 .auto_corr_max_cck = 200,
1031 .auto_corr_min_cck_mrc = 200,
1032 .auto_corr_max_cck_mrc = 400,
1040 * iwl4965_hw_set_hw_params
1042 * Called when initializing driver
1044 int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
1047 if ((priv->cfg->mod_params->num_of_queues > IWL49_NUM_QUEUES) ||
1048 (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
1049 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
1050 IWL_MIN_NUM_QUEUES, IWL49_NUM_QUEUES);
1054 priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
1055 priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
1056 priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
1057 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1058 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
1059 if (priv->cfg->mod_params->amsdu_size_8K)
1060 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
1062 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
1063 priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
1064 priv->hw_params.max_stations = IWL4965_STATION_COUNT;
1065 priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
1067 priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
1068 priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
1069 priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
1070 priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
1072 priv->hw_params.tx_chains_num = 2;
1073 priv->hw_params.rx_chains_num = 2;
1074 priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
1075 priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
1076 priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
1078 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1079 priv->hw_params.sens = &iwl4965_sensitivity;
1085 /* set card power command */
1086 static int iwl4965_set_power(struct iwl_priv *priv,
1091 ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1092 sizeof(struct iwl4965_powertable_cmd),
1096 int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
1098 IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
1102 static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1115 *res = ((num * 2 + denom) / (denom * 2)) * sign;
1121 * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1123 * Determines power supply voltage compensation for txpower calculations.
1124 * Returns number of 1/2-dB steps to subtract from gain table index,
1125 * to compensate for difference between power supply voltage during
1126 * factory measurements, vs. current power supply voltage.
1128 * Voltage indication is higher for lower voltage.
1129 * Lower voltage requires more gain (lower gain table index).
1131 static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1132 s32 current_voltage)
1136 if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1137 (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1140 iwl4965_math_div_round(current_voltage - eeprom_voltage,
1141 TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1143 if (current_voltage > eeprom_voltage)
1145 if ((comp < -2) || (comp > 2))
1151 static const struct iwl_channel_info *
1152 iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
1153 enum ieee80211_band band, u16 channel)
1155 const struct iwl_channel_info *ch_info;
1157 ch_info = iwl_get_channel_info(priv, band, channel);
1159 if (!is_channel_valid(ch_info))
1165 static s32 iwl4965_get_tx_atten_grp(u16 channel)
1167 if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1168 channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1169 return CALIB_CH_GROUP_5;
1171 if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1172 channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1173 return CALIB_CH_GROUP_1;
1175 if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1176 channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1177 return CALIB_CH_GROUP_2;
1179 if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1180 channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1181 return CALIB_CH_GROUP_3;
1183 if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1184 channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1185 return CALIB_CH_GROUP_4;
1187 IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1191 static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
1195 for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
1196 if (priv->calib_info->band_info[b].ch_from == 0)
1199 if ((channel >= priv->calib_info->band_info[b].ch_from)
1200 && (channel <= priv->calib_info->band_info[b].ch_to))
1207 static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1214 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1220 * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1222 * Interpolates factory measurements from the two sample channels within a
1223 * sub-band, to apply to channel of interest. Interpolation is proportional to
1224 * differences in channel frequencies, which is proportional to differences
1225 * in channel number.
1227 static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
1228 struct iwl_eeprom_calib_ch_info *chan_info)
1233 const struct iwl_eeprom_calib_measure *m1;
1234 const struct iwl_eeprom_calib_measure *m2;
1235 struct iwl_eeprom_calib_measure *omeas;
1239 s = iwl4965_get_sub_band(priv, channel);
1240 if (s >= EEPROM_TX_POWER_BANDS) {
1241 IWL_ERROR("Tx Power can not find channel %d ", channel);
1245 ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1246 ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
1247 chan_info->ch_num = (u8) channel;
1249 IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1250 channel, s, ch_i1, ch_i2);
1252 for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1253 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
1254 m1 = &(priv->calib_info->band_info[s].ch1.
1255 measurements[c][m]);
1256 m2 = &(priv->calib_info->band_info[s].ch2.
1257 measurements[c][m]);
1258 omeas = &(chan_info->measurements[c][m]);
1261 (u8) iwl4965_interpolate_value(channel, ch_i1,
1266 (u8) iwl4965_interpolate_value(channel, ch_i1,
1267 m1->gain_idx, ch_i2,
1269 omeas->temperature =
1270 (u8) iwl4965_interpolate_value(channel, ch_i1,
1275 (s8) iwl4965_interpolate_value(channel, ch_i1,
1280 ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1281 m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1283 ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1284 m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1286 ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1287 m1->pa_det, m2->pa_det, omeas->pa_det);
1289 ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
1290 m1->temperature, m2->temperature,
1291 omeas->temperature);
1298 /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1299 * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1300 static s32 back_off_table[] = {
1301 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1302 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1303 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1304 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1308 /* Thermal compensation values for txpower for various frequency ranges ...
1309 * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
1310 static struct iwl4965_txpower_comp_entry {
1311 s32 degrees_per_05db_a;
1312 s32 degrees_per_05db_a_denom;
1313 } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1314 {9, 2}, /* group 0 5.2, ch 34-43 */
1315 {4, 1}, /* group 1 5.2, ch 44-70 */
1316 {4, 1}, /* group 2 5.2, ch 71-124 */
1317 {4, 1}, /* group 3 5.2, ch 125-200 */
1318 {3, 1} /* group 4 2.4, ch all */
1321 static s32 get_min_power_index(s32 rate_power_index, u32 band)
1324 if ((rate_power_index & 7) <= 4)
1325 return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1327 return MIN_TX_GAIN_INDEX;
1335 static const struct gain_entry gain_table[2][108] = {
1336 /* 5.2GHz power gain index table */
1338 {123, 0x3F}, /* highest txpower */
1447 /* 2.4GHz power gain index table */
1449 {110, 0x3f}, /* highest txpower */
1560 static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
1561 u8 is_fat, u8 ctrl_chan_high,
1562 struct iwl4965_tx_power_db *tx_power_tbl)
1564 u8 saturation_power;
1566 s32 user_target_power;
1570 s32 current_regulatory;
1571 s32 txatten_grp = CALIB_CH_GROUP_MAX;
1574 const struct iwl_channel_info *ch_info = NULL;
1575 struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1576 const struct iwl_eeprom_calib_measure *measurement;
1579 s32 voltage_compensation;
1580 s32 degrees_per_05db_num;
1581 s32 degrees_per_05db_denom;
1583 s32 temperature_comp[2];
1584 s32 factory_gain_index[2];
1585 s32 factory_actual_pwr[2];
1588 /* Sanity check requested level (dBm) */
1589 if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1590 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1591 priv->user_txpower_limit);
1594 if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1595 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1596 priv->user_txpower_limit);
1600 /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1601 * are used for indexing into txpower table) */
1602 user_target_power = 2 * priv->user_txpower_limit;
1604 /* Get current (RXON) channel, band, width */
1606 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
1608 IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1614 /* get txatten group, used to select 1) thermal txpower adjustment
1615 * and 2) mimo txpower balance between Tx chains. */
1616 txatten_grp = iwl4965_get_tx_atten_grp(channel);
1617 if (txatten_grp < 0)
1620 IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1621 channel, txatten_grp);
1630 /* hardware txpower limits ...
1631 * saturation (clipping distortion) txpowers are in half-dBm */
1633 saturation_power = priv->calib_info->saturation_power24;
1635 saturation_power = priv->calib_info->saturation_power52;
1637 if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1638 saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1640 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1642 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1645 /* regulatory txpower limits ... reg_limit values are in half-dBm,
1646 * max_power_avg values are in dBm, convert * 2 */
1648 reg_limit = ch_info->fat_max_power_avg * 2;
1650 reg_limit = ch_info->max_power_avg * 2;
1652 if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
1653 (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
1655 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
1657 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
1660 /* Interpolate txpower calibration values for this channel,
1661 * based on factory calibration tests on spaced channels. */
1662 iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
1664 /* calculate tx gain adjustment based on power supply voltage */
1665 voltage = priv->calib_info->voltage;
1666 init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
1667 voltage_compensation =
1668 iwl4965_get_voltage_compensation(voltage, init_voltage);
1670 IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
1672 voltage, voltage_compensation);
1674 /* get current temperature (Celsius) */
1675 current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
1676 current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
1677 current_temp = KELVIN_TO_CELSIUS(current_temp);
1679 /* select thermal txpower adjustment params, based on channel group
1680 * (same frequency group used for mimo txatten adjustment) */
1681 degrees_per_05db_num =
1682 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
1683 degrees_per_05db_denom =
1684 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
1686 /* get per-chain txpower values from factory measurements */
1687 for (c = 0; c < 2; c++) {
1688 measurement = &ch_eeprom_info.measurements[c][1];
1690 /* txgain adjustment (in half-dB steps) based on difference
1691 * between factory and current temperature */
1692 factory_temp = measurement->temperature;
1693 iwl4965_math_div_round((current_temp - factory_temp) *
1694 degrees_per_05db_denom,
1695 degrees_per_05db_num,
1696 &temperature_comp[c]);
1698 factory_gain_index[c] = measurement->gain_idx;
1699 factory_actual_pwr[c] = measurement->actual_pow;
1701 IWL_DEBUG_TXPOWER("chain = %d\n", c);
1702 IWL_DEBUG_TXPOWER("fctry tmp %d, "
1703 "curr tmp %d, comp %d steps\n",
1704 factory_temp, current_temp,
1705 temperature_comp[c]);
1707 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
1708 factory_gain_index[c],
1709 factory_actual_pwr[c]);
1712 /* for each of 33 bit-rates (including 1 for CCK) */
1713 for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
1715 union iwl4965_tx_power_dual_stream tx_power;
1717 /* for mimo, reduce each chain's txpower by half
1718 * (3dB, 6 steps), so total output power is regulatory
1721 current_regulatory = reg_limit -
1722 IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
1725 current_regulatory = reg_limit;
1729 /* find txpower limit, either hardware or regulatory */
1730 power_limit = saturation_power - back_off_table[i];
1731 if (power_limit > current_regulatory)
1732 power_limit = current_regulatory;
1734 /* reduce user's txpower request if necessary
1735 * for this rate on this channel */
1736 target_power = user_target_power;
1737 if (target_power > power_limit)
1738 target_power = power_limit;
1740 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
1741 i, saturation_power - back_off_table[i],
1742 current_regulatory, user_target_power,
1745 /* for each of 2 Tx chains (radio transmitters) */
1746 for (c = 0; c < 2; c++) {
1751 (s32)le32_to_cpu(priv->card_alive_init.
1752 tx_atten[txatten_grp][c]);
1756 /* calculate index; higher index means lower txpower */
1757 power_index = (u8) (factory_gain_index[c] -
1759 factory_actual_pwr[c]) -
1760 temperature_comp[c] -
1761 voltage_compensation +
1764 /* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
1767 if (power_index < get_min_power_index(i, band))
1768 power_index = get_min_power_index(i, band);
1770 /* adjust 5 GHz index to support negative indexes */
1774 /* CCK, rate 32, reduce txpower for CCK */
1775 if (i == POWER_TABLE_CCK_ENTRY)
1777 IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
1779 /* stay within the table! */
1780 if (power_index > 107) {
1781 IWL_WARNING("txpower index %d > 107\n",
1785 if (power_index < 0) {
1786 IWL_WARNING("txpower index %d < 0\n",
1791 /* fill txpower command for this rate/chain */
1792 tx_power.s.radio_tx_gain[c] =
1793 gain_table[band][power_index].radio;
1794 tx_power.s.dsp_predis_atten[c] =
1795 gain_table[band][power_index].dsp;
1797 IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
1798 "gain 0x%02x dsp %d\n",
1799 c, atten_value, power_index,
1800 tx_power.s.radio_tx_gain[c],
1801 tx_power.s.dsp_predis_atten[c]);
1802 }/* for each chain */
1804 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
1806 }/* for each rate */
1812 * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
1814 * Uses the active RXON for channel, band, and characteristics (fat, high)
1815 * The power limit is taken from priv->user_txpower_limit.
1817 int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
1819 struct iwl4965_txpowertable_cmd cmd = { 0 };
1823 u8 ctrl_chan_high = 0;
1825 if (test_bit(STATUS_SCANNING, &priv->status)) {
1826 /* If this gets hit a lot, switch it to a BUG() and catch
1827 * the stack trace to find out who is calling this during
1829 IWL_WARNING("TX Power requested while scanning!\n");
1833 band = priv->band == IEEE80211_BAND_2GHZ;
1835 is_fat = is_fat_channel(priv->active_rxon.flags);
1838 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
1842 cmd.channel = priv->active_rxon.channel;
1844 ret = iwl4965_fill_txpower_tbl(priv, band,
1845 le16_to_cpu(priv->active_rxon.channel),
1846 is_fat, ctrl_chan_high, &cmd.tx_power);
1850 ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
1856 static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
1859 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1860 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1861 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1863 if ((rxon1->flags == rxon2->flags) &&
1864 (rxon1->filter_flags == rxon2->filter_flags) &&
1865 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1866 (rxon1->ofdm_ht_single_stream_basic_rates ==
1867 rxon2->ofdm_ht_single_stream_basic_rates) &&
1868 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1869 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1870 (rxon1->rx_chain == rxon2->rx_chain) &&
1871 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1872 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1876 rxon_assoc.flags = priv->staging_rxon.flags;
1877 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1878 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1879 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1880 rxon_assoc.reserved = 0;
1881 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1882 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1883 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1884 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1885 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1887 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
1888 sizeof(rxon_assoc), &rxon_assoc, NULL);
1896 int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
1901 u8 ctrl_chan_high = 0;
1902 struct iwl4965_channel_switch_cmd cmd = { 0 };
1903 const struct iwl_channel_info *ch_info;
1905 band = priv->band == IEEE80211_BAND_2GHZ;
1907 ch_info = iwl_get_channel_info(priv, priv->band, channel);
1909 is_fat = is_fat_channel(priv->staging_rxon.flags);
1912 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
1916 cmd.expect_beacon = 0;
1917 cmd.channel = cpu_to_le16(channel);
1918 cmd.rxon_flags = priv->active_rxon.flags;
1919 cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
1920 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
1922 cmd.expect_beacon = is_channel_radar(ch_info);
1924 cmd.expect_beacon = 1;
1926 rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
1927 ctrl_chan_high, &cmd.tx_power);
1929 IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
1933 rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
1937 #define RTS_HCCA_RETRY_LIMIT 3
1938 #define RTS_DFAULT_RETRY_LIMIT 60
1940 void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
1941 struct iwl_cmd *cmd,
1942 struct ieee80211_tx_control *ctrl,
1943 struct ieee80211_hdr *hdr, int sta_id,
1946 struct iwl4965_tx_cmd *tx = &cmd->cmd.tx;
1947 u8 rts_retry_limit = 0;
1948 u8 data_retry_limit = 0;
1949 u16 fc = le16_to_cpu(hdr->frame_control);
1952 int rate_idx = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
1954 rate_plcp = iwl_rates[rate_idx].plcp;
1956 rts_retry_limit = (is_hcca) ?
1957 RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
1959 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
1960 rate_flags |= RATE_MCS_CCK_MSK;
1963 if (ieee80211_is_probe_response(fc)) {
1964 data_retry_limit = 3;
1965 if (data_retry_limit < rts_retry_limit)
1966 rts_retry_limit = data_retry_limit;
1968 data_retry_limit = IWL_DEFAULT_TX_RETRY;
1970 if (priv->data_retry_limit != -1)
1971 data_retry_limit = priv->data_retry_limit;
1974 if (ieee80211_is_data(fc)) {
1975 tx->initial_rate_index = 0;
1976 tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1978 switch (fc & IEEE80211_FCTL_STYPE) {
1979 case IEEE80211_STYPE_AUTH:
1980 case IEEE80211_STYPE_DEAUTH:
1981 case IEEE80211_STYPE_ASSOC_REQ:
1982 case IEEE80211_STYPE_REASSOC_REQ:
1983 if (tx->tx_flags & TX_CMD_FLG_RTS_MSK) {
1984 tx->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1985 tx->tx_flags |= TX_CMD_FLG_CTS_MSK;
1992 /* Alternate between antenna A and B for successive frames */
1993 if (priv->use_ant_b_for_management_frame) {
1994 priv->use_ant_b_for_management_frame = 0;
1995 rate_flags |= RATE_MCS_ANT_B_MSK;
1997 priv->use_ant_b_for_management_frame = 1;
1998 rate_flags |= RATE_MCS_ANT_A_MSK;
2002 tx->rts_retry_limit = rts_retry_limit;
2003 tx->data_retry_limit = data_retry_limit;
2004 tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
2007 static int iwl4965_shared_mem_rx_idx(struct iwl_priv *priv)
2009 struct iwl4965_shared *s = priv->shared_virt;
2010 return le32_to_cpu(s->rb_closed) & 0xFFF;
2013 int iwl4965_hw_get_temperature(struct iwl_priv *priv)
2015 return priv->temperature;
2018 unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
2019 struct iwl_frame *frame, u8 rate)
2021 struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
2022 unsigned int frame_size;
2024 tx_beacon_cmd = &frame->u.beacon;
2025 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
2027 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
2028 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2030 frame_size = iwl4965_fill_beacon_frame(priv,
2031 tx_beacon_cmd->frame,
2033 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
2035 BUG_ON(frame_size > MAX_MPDU_SIZE);
2036 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
2038 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
2039 tx_beacon_cmd->tx.rate_n_flags =
2040 iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
2042 tx_beacon_cmd->tx.rate_n_flags =
2043 iwl4965_hw_set_rate_n_flags(rate, 0);
2045 tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
2046 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
2047 return (sizeof(*tx_beacon_cmd) + frame_size);
2050 int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
2051 dma_addr_t addr, u16 len)
2054 struct iwl_tfd_frame *tfd = ptr;
2055 u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
2057 /* Each TFD can point to a maximum 20 Tx buffers */
2058 if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
2059 IWL_ERROR("Error can not send more than %d chunks\n",
2064 index = num_tbs / 2;
2065 is_odd = num_tbs & 0x1;
2068 tfd->pa[index].tb1_addr = cpu_to_le32(addr);
2069 IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
2070 iwl_get_dma_hi_address(addr));
2071 IWL_SET_BITS(tfd->pa[index], tb1_len, len);
2073 IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
2074 (u32) (addr & 0xffff));
2075 IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
2076 IWL_SET_BITS(tfd->pa[index], tb2_len, len);
2079 IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
2084 static int iwl4965_alloc_shared_mem(struct iwl_priv *priv)
2086 priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
2087 sizeof(struct iwl4965_shared),
2088 &priv->shared_phys);
2089 if (!priv->shared_virt)
2092 memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
2094 priv->rb_closed_offset = offsetof(struct iwl4965_shared, rb_closed);
2099 static void iwl4965_free_shared_mem(struct iwl_priv *priv)
2101 if (priv->shared_virt)
2102 pci_free_consistent(priv->pci_dev,
2103 sizeof(struct iwl4965_shared),
2109 * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
2111 static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
2112 struct iwl_tx_queue *txq,
2116 int txq_id = txq->q.id;
2117 struct iwl4965_shared *shared_data = priv->shared_virt;
2119 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2121 /* Set up byte count within first 256 entries */
2122 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
2123 tfd_offset[txq->q.write_ptr], byte_cnt, len);
2125 /* If within first 64 entries, duplicate at end */
2126 if (txq->q.write_ptr < IWL49_MAX_WIN_SIZE)
2127 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
2128 tfd_offset[IWL49_QUEUE_SIZE + txq->q.write_ptr],
2133 * sign_extend - Sign extend a value using specified bit as sign-bit
2135 * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
2136 * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
2138 * @param oper value to sign extend
2139 * @param index 0 based bit index (0<=index<32) to sign bit
2141 static s32 sign_extend(u32 oper, int index)
2143 u8 shift = 31 - index;
2145 return (s32)(oper << shift) >> shift;
2149 * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
2150 * @statistics: Provides the temperature reading from the uCode
2152 * A return of <0 indicates bogus data in the statistics
2154 int iwl4965_get_temperature(const struct iwl_priv *priv)
2161 if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
2162 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
2163 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
2164 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
2165 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
2166 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
2167 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
2169 IWL_DEBUG_TEMP("Running temperature calibration\n");
2170 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
2171 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
2172 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2173 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2177 * Temperature is only 23 bits, so sign extend out to 32.
2179 * NOTE If we haven't received a statistics notification yet
2180 * with an updated temperature, use R4 provided to us in the
2181 * "initialize" ALIVE response.
2183 if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2184 vt = sign_extend(R4, 23);
2187 le32_to_cpu(priv->statistics.general.temperature), 23);
2189 IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2193 IWL_ERROR("Calibration conflict R1 == R3\n");
2197 /* Calculate temperature in degrees Kelvin, adjust by 97%.
2198 * Add offset to center the adjustment around 0 degrees Centigrade. */
2199 temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2200 temperature /= (R3 - R1);
2201 temperature = (temperature * 97) / 100 +
2202 TEMPERATURE_CALIB_KELVIN_OFFSET;
2204 IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2205 KELVIN_TO_CELSIUS(temperature));
2210 /* Adjust Txpower only if temperature variance is greater than threshold. */
2211 #define IWL_TEMPERATURE_THRESHOLD 3
2214 * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2216 * If the temperature changed has changed sufficiently, then a recalibration
2219 * Assumes caller will replace priv->last_temperature once calibration
2222 static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
2226 if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2227 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2231 temp_diff = priv->temperature - priv->last_temperature;
2233 /* get absolute value */
2234 if (temp_diff < 0) {
2235 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2236 temp_diff = -temp_diff;
2237 } else if (temp_diff == 0)
2238 IWL_DEBUG_POWER("Same temp, \n");
2240 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2242 if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2243 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2247 IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2252 /* Calculate noise level, based on measurements during network silence just
2253 * before arriving beacon. This measurement can be done only if we know
2254 * exactly when to expect beacons, therefore only when we're associated. */
2255 static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
2257 struct statistics_rx_non_phy *rx_info
2258 = &(priv->statistics.rx.general);
2259 int num_active_rx = 0;
2260 int total_silence = 0;
2262 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2264 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2266 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2268 if (bcn_silence_a) {
2269 total_silence += bcn_silence_a;
2272 if (bcn_silence_b) {
2273 total_silence += bcn_silence_b;
2276 if (bcn_silence_c) {
2277 total_silence += bcn_silence_c;
2281 /* Average among active antennas */
2283 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2285 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2287 IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2288 bcn_silence_a, bcn_silence_b, bcn_silence_c,
2289 priv->last_rx_noise);
2292 void iwl4965_hw_rx_statistics(struct iwl_priv *priv,
2293 struct iwl_rx_mem_buffer *rxb)
2295 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2299 IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2300 (int)sizeof(priv->statistics), pkt->len);
2302 change = ((priv->statistics.general.temperature !=
2303 pkt->u.stats.general.temperature) ||
2304 ((priv->statistics.flag &
2305 STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2306 (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2308 memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2310 set_bit(STATUS_STATISTICS, &priv->status);
2312 /* Reschedule the statistics timer to occur in
2313 * REG_RECALIB_PERIOD seconds to ensure we get a
2314 * thermal update even if the uCode doesn't give
2316 mod_timer(&priv->statistics_periodic, jiffies +
2317 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2319 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2320 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2321 iwl4965_rx_calc_noise(priv);
2322 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
2323 queue_work(priv->workqueue, &priv->sensitivity_work);
2327 iwl_leds_background(priv);
2329 /* If the hardware hasn't reported a change in
2330 * temperature then don't bother computing a
2331 * calibrated temperature value */
2335 temp = iwl4965_get_temperature(priv);
2339 if (priv->temperature != temp) {
2340 if (priv->temperature)
2341 IWL_DEBUG_TEMP("Temperature changed "
2342 "from %dC to %dC\n",
2343 KELVIN_TO_CELSIUS(priv->temperature),
2344 KELVIN_TO_CELSIUS(temp));
2346 IWL_DEBUG_TEMP("Temperature "
2347 "initialized to %dC\n",
2348 KELVIN_TO_CELSIUS(temp));
2351 priv->temperature = temp;
2352 set_bit(STATUS_TEMPERATURE, &priv->status);
2354 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2355 iwl4965_is_temp_calib_needed(priv))
2356 queue_work(priv->workqueue, &priv->txpower_work);
2359 static void iwl4965_add_radiotap(struct iwl_priv *priv,
2360 struct sk_buff *skb,
2361 struct iwl4965_rx_phy_res *rx_start,
2362 struct ieee80211_rx_status *stats,
2365 s8 signal = stats->signal;
2367 int rate = stats->rate_idx;
2368 u64 tsf = stats->mactime;
2370 __le16 phy_flags_hw = rx_start->phy_flags;
2371 struct iwl4965_rt_rx_hdr {
2372 struct ieee80211_radiotap_header rt_hdr;
2373 __le64 rt_tsf; /* TSF */
2374 u8 rt_flags; /* radiotap packet flags */
2375 u8 rt_rate; /* rate in 500kb/s */
2376 __le16 rt_channelMHz; /* channel in MHz */
2377 __le16 rt_chbitmask; /* channel bitfield */
2378 s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
2380 u8 rt_antenna; /* antenna number */
2381 } __attribute__ ((packed)) *iwl4965_rt;
2383 /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2384 if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2385 if (net_ratelimit())
2386 printk(KERN_ERR "not enough headroom [%d] for "
2387 "radiotap head [%zd]\n",
2388 skb_headroom(skb), sizeof(*iwl4965_rt));
2392 /* put radiotap header in front of 802.11 header and data */
2393 iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2395 /* initialise radiotap header */
2396 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2397 iwl4965_rt->rt_hdr.it_pad = 0;
2399 /* total header + data */
2400 put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2401 &iwl4965_rt->rt_hdr.it_len);
2403 /* Indicate all the fields we add to the radiotap header */
2404 put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2405 (1 << IEEE80211_RADIOTAP_FLAGS) |
2406 (1 << IEEE80211_RADIOTAP_RATE) |
2407 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2408 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2409 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2410 (1 << IEEE80211_RADIOTAP_ANTENNA)),
2411 &iwl4965_rt->rt_hdr.it_present);
2413 /* Zero the flags, we'll add to them as we go */
2414 iwl4965_rt->rt_flags = 0;
2416 put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2418 iwl4965_rt->rt_dbmsignal = signal;
2419 iwl4965_rt->rt_dbmnoise = noise;
2421 /* Convert the channel frequency and set the flags */
2422 put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2423 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2424 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2425 IEEE80211_CHAN_5GHZ),
2426 &iwl4965_rt->rt_chbitmask);
2427 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2428 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2429 IEEE80211_CHAN_2GHZ),
2430 &iwl4965_rt->rt_chbitmask);
2432 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2433 IEEE80211_CHAN_2GHZ),
2434 &iwl4965_rt->rt_chbitmask);
2437 iwl4965_rt->rt_rate = 0;
2439 iwl4965_rt->rt_rate = iwl_rates[rate].ieee;
2444 * It seems that the antenna field in the phy flags value
2445 * is actually a bitfield. This is undefined by radiotap,
2446 * it wants an actual antenna number but I always get "7"
2447 * for most legacy frames I receive indicating that the
2448 * same frame was received on all three RX chains.
2450 * I think this field should be removed in favour of a
2451 * new 802.11n radiotap field "RX chains" that is defined
2454 antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2455 iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
2457 /* set the preamble flag if appropriate */
2458 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2459 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2461 stats->flag |= RX_FLAG_RADIOTAP;
2464 static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2466 /* 0 - mgmt, 1 - cnt, 2 - data */
2467 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2468 priv->rx_stats[idx].cnt++;
2469 priv->rx_stats[idx].bytes += len;
2473 * returns non-zero if packet should be dropped
2475 static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2476 struct ieee80211_hdr *hdr,
2478 struct ieee80211_rx_status *stats)
2480 u16 fc = le16_to_cpu(hdr->frame_control);
2482 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2485 if (!(fc & IEEE80211_FCTL_PROTECTED))
2488 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2489 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2490 case RX_RES_STATUS_SEC_TYPE_TKIP:
2491 /* The uCode has got a bad phase 1 Key, pushes the packet.
2492 * Decryption will be done in SW. */
2493 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2494 RX_RES_STATUS_BAD_KEY_TTAK)
2497 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2498 RX_RES_STATUS_BAD_ICV_MIC) {
2499 /* bad ICV, the packet is destroyed since the
2500 * decryption is inplace, drop it */
2501 IWL_DEBUG_RX("Packet destroyed\n");
2504 case RX_RES_STATUS_SEC_TYPE_WEP:
2505 case RX_RES_STATUS_SEC_TYPE_CCMP:
2506 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2507 RX_RES_STATUS_DECRYPT_OK) {
2508 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2509 stats->flag |= RX_FLAG_DECRYPTED;
2519 static u32 iwl4965_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
2521 u32 decrypt_out = 0;
2523 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2524 RX_RES_STATUS_STATION_FOUND)
2525 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2526 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2528 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2530 /* packet was not encrypted */
2531 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2532 RX_RES_STATUS_SEC_TYPE_NONE)
2535 /* packet was encrypted with unknown alg */
2536 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2537 RX_RES_STATUS_SEC_TYPE_ERR)
2540 /* decryption was not done in HW */
2541 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2542 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2545 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2547 case RX_RES_STATUS_SEC_TYPE_CCMP:
2548 /* alg is CCM: check MIC only */
2549 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2551 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2553 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2557 case RX_RES_STATUS_SEC_TYPE_TKIP:
2558 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2560 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2563 /* fall through if TTAK OK */
2565 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
2566 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2568 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2572 IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
2573 decrypt_in, decrypt_out);
2578 static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
2580 struct iwl_rx_mem_buffer *rxb,
2581 struct ieee80211_rx_status *stats)
2583 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2584 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
2585 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
2586 struct ieee80211_hdr *hdr;
2589 unsigned int skblen;
2591 u32 ampdu_status_legacy;
2593 if (!include_phy && priv->last_phy_res[0])
2594 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
2597 IWL_ERROR("MPDU frame without a PHY data\n");
2601 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
2602 rx_start->cfg_phy_cnt);
2604 len = le16_to_cpu(rx_start->byte_count);
2606 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
2607 sizeof(struct iwl4965_rx_phy_res) +
2608 rx_start->cfg_phy_cnt + len);
2611 struct iwl4965_rx_mpdu_res_start *amsdu =
2612 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
2614 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
2615 sizeof(struct iwl4965_rx_mpdu_res_start));
2616 len = le16_to_cpu(amsdu->byte_count);
2617 rx_start->byte_count = amsdu->byte_count;
2618 rx_end = (__le32 *) (((u8 *) hdr) + len);
2620 /* In monitor mode allow 802.11 ACk frames (10 bytes) */
2621 if (len > priv->hw_params.max_pkt_size ||
2622 len < ((priv->iw_mode == IEEE80211_IF_TYPE_MNTR) ? 10 : 16)) {
2623 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
2627 ampdu_status = le32_to_cpu(*rx_end);
2628 skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
2631 /* New status scheme, need to translate */
2632 ampdu_status_legacy = ampdu_status;
2633 ampdu_status = iwl4965_translate_rx_status(priv, ampdu_status);
2636 /* start from MAC */
2637 skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
2638 skb_put(rxb->skb, len); /* end where data ends */
2640 /* We only process data packets if the interface is open */
2641 if (unlikely(!priv->is_open)) {
2642 IWL_DEBUG_DROP_LIMIT
2643 ("Dropping packet while interface is not open.\n");
2648 hdr = (struct ieee80211_hdr *)rxb->skb->data;
2650 /* in case of HW accelerated crypto and bad decryption, drop */
2651 if (!priv->hw_params.sw_crypto &&
2652 iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
2655 if (priv->add_radiotap)
2656 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
2658 iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
2659 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
2660 priv->alloc_rxb_skb--;
2664 /* Calc max signal level (dBm) among 3 possible receivers */
2665 static int iwl4965_calc_rssi(struct iwl_priv *priv,
2666 struct iwl4965_rx_phy_res *rx_resp)
2668 /* data from PHY/DSP regarding signal strength, etc.,
2669 * contents are always there, not configurable by host. */
2670 struct iwl4965_rx_non_cfg_phy *ncphy =
2671 (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
2672 u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
2675 u32 valid_antennae =
2676 (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
2677 >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
2681 /* Find max rssi among 3 possible receivers.
2682 * These values are measured by the digital signal processor (DSP).
2683 * They should stay fairly constant even as the signal strength varies,
2684 * if the radio's automatic gain control (AGC) is working right.
2685 * AGC value (see below) will provide the "interesting" info. */
2686 for (i = 0; i < 3; i++)
2687 if (valid_antennae & (1 << i))
2688 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
2690 IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
2691 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
2694 /* dBm = max_rssi dB - agc dB - constant.
2695 * Higher AGC (higher radio gain) means lower signal. */
2696 return (max_rssi - agc - IWL_RSSI_OFFSET);
2699 static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
2701 unsigned long flags;
2703 spin_lock_irqsave(&priv->sta_lock, flags);
2704 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
2705 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
2706 priv->stations[sta_id].sta.sta.modify_mask = 0;
2707 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2708 spin_unlock_irqrestore(&priv->sta_lock, flags);
2710 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
2713 static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
2715 /* FIXME: need locking over ps_status ??? */
2716 u8 sta_id = iwl_find_station(priv, addr);
2718 if (sta_id != IWL_INVALID_STATION) {
2719 u8 sta_awake = priv->stations[sta_id].
2720 ps_status == STA_PS_STATUS_WAKE;
2722 if (sta_awake && ps_bit)
2723 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
2724 else if (!sta_awake && !ps_bit) {
2725 iwl4965_sta_modify_ps_wake(priv, sta_id);
2726 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
2730 #ifdef CONFIG_IWLWIFI_DEBUG
2733 * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
2735 * You may hack this function to show different aspects of received frames,
2736 * including selective frame dumps.
2737 * group100 parameter selects whether to show 1 out of 100 good frames.
2739 * TODO: This was originally written for 3945, need to audit for
2740 * proper operation with 4965.
2742 static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
2743 struct iwl_rx_packet *pkt,
2744 struct ieee80211_hdr *header, int group100)
2747 u32 print_summary = 0;
2748 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
2765 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
2766 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
2767 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
2768 u8 *data = IWL_RX_DATA(pkt);
2770 if (likely(!(priv->debug_level & IWL_DL_RX)))
2774 fc = le16_to_cpu(header->frame_control);
2775 seq_ctl = le16_to_cpu(header->seq_ctrl);
2778 channel = le16_to_cpu(rx_hdr->channel);
2779 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
2780 rate_sym = rx_hdr->rate;
2781 length = le16_to_cpu(rx_hdr->len);
2783 /* end-of-frame status and timestamp */
2784 status = le32_to_cpu(rx_end->status);
2785 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
2786 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
2787 tsf = le64_to_cpu(rx_end->timestamp);
2789 /* signal statistics */
2790 rssi = rx_stats->rssi;
2791 agc = rx_stats->agc;
2792 sig_avg = le16_to_cpu(rx_stats->sig_avg);
2793 noise_diff = le16_to_cpu(rx_stats->noise_diff);
2795 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
2797 /* if data frame is to us and all is good,
2798 * (optionally) print summary for only 1 out of every 100 */
2799 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
2800 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
2803 print_summary = 1; /* print each frame */
2804 else if (priv->framecnt_to_us < 100) {
2805 priv->framecnt_to_us++;
2808 priv->framecnt_to_us = 0;
2813 /* print summary for all other frames */
2817 if (print_summary) {
2823 title = "100Frames";
2824 else if (fc & IEEE80211_FCTL_RETRY)
2826 else if (ieee80211_is_assoc_response(fc))
2828 else if (ieee80211_is_reassoc_response(fc))
2830 else if (ieee80211_is_probe_response(fc)) {
2832 print_dump = 1; /* dump frame contents */
2833 } else if (ieee80211_is_beacon(fc)) {
2835 print_dump = 1; /* dump frame contents */
2836 } else if (ieee80211_is_atim(fc))
2838 else if (ieee80211_is_auth(fc))
2840 else if (ieee80211_is_deauth(fc))
2842 else if (ieee80211_is_disassoc(fc))
2847 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
2848 if (unlikely(rate_idx == -1))
2851 bitrate = iwl_rates[rate_idx].ieee / 2;
2853 /* print frame summary.
2854 * MAC addresses show just the last byte (for brevity),
2855 * but you can hack it to show more, if you'd like to. */
2857 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
2858 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
2859 title, fc, header->addr1[5],
2860 length, rssi, channel, bitrate);
2862 /* src/dst addresses assume managed mode */
2863 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
2864 "src=0x%02x, rssi=%u, tim=%lu usec, "
2865 "phy=0x%02x, chnl=%d\n",
2866 title, fc, header->addr1[5],
2867 header->addr3[5], rssi,
2868 tsf_low - priv->scan_start_tsf,
2869 phy_flags, channel);
2873 iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
2876 static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
2877 struct iwl_rx_packet *pkt,
2878 struct ieee80211_hdr *header,
2886 /* Called for REPLY_RX (legacy ABG frames), or
2887 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
2888 static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
2889 struct iwl_rx_mem_buffer *rxb)
2891 struct ieee80211_hdr *header;
2892 struct ieee80211_rx_status rx_status;
2893 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2894 /* Use phy data (Rx signal strength, etc.) contained within
2895 * this rx packet for legacy frames,
2896 * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
2897 int include_phy = (pkt->hdr.cmd == REPLY_RX);
2898 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
2899 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
2900 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
2902 unsigned int len = 0;
2906 rx_status.mactime = le64_to_cpu(rx_start->timestamp);
2908 ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
2909 rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
2910 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
2911 rx_status.rate_idx =
2912 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
2913 if (rx_status.band == IEEE80211_BAND_5GHZ)
2914 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
2916 rx_status.antenna = 0;
2919 if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
2920 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
2921 rx_start->cfg_phy_cnt);
2926 if (priv->last_phy_res[0])
2927 rx_start = (struct iwl4965_rx_phy_res *)
2928 &priv->last_phy_res[1];
2934 IWL_ERROR("MPDU frame without a PHY data\n");
2939 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
2940 + rx_start->cfg_phy_cnt);
2942 len = le16_to_cpu(rx_start->byte_count);
2943 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
2944 sizeof(struct iwl4965_rx_phy_res) + len);
2946 struct iwl4965_rx_mpdu_res_start *amsdu =
2947 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
2949 header = (void *)(pkt->u.raw +
2950 sizeof(struct iwl4965_rx_mpdu_res_start));
2951 len = le16_to_cpu(amsdu->byte_count);
2952 rx_end = (__le32 *) (pkt->u.raw +
2953 sizeof(struct iwl4965_rx_mpdu_res_start) + len);
2956 if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
2957 !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
2958 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
2959 le32_to_cpu(*rx_end));
2963 priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
2965 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
2966 rx_status.signal = iwl4965_calc_rssi(priv, rx_start);
2968 /* Meaningful noise values are available only from beacon statistics,
2969 * which are gathered only when associated, and indicate noise
2970 * only for the associated network channel ...
2971 * Ignore these noise values while scanning (other channels) */
2972 if (iwl_is_associated(priv) &&
2973 !test_bit(STATUS_SCANNING, &priv->status)) {
2974 rx_status.noise = priv->last_rx_noise;
2975 rx_status.qual = iwl4965_calc_sig_qual(rx_status.signal,
2978 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2979 rx_status.qual = iwl4965_calc_sig_qual(rx_status.signal, 0);
2982 /* Reset beacon noise level if not associated. */
2983 if (!iwl_is_associated(priv))
2984 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2986 /* Set "1" to report good data frames in groups of 100 */
2987 /* FIXME: need to optimze the call: */
2988 iwl4965_dbg_report_frame(priv, pkt, header, 1);
2990 IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
2991 rx_status.signal, rx_status.noise, rx_status.signal,
2992 (unsigned long long)rx_status.mactime);
2995 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2996 iwl4965_handle_data_packet(priv, 1, include_phy,
3001 network_packet = iwl4965_is_network_packet(priv, header);
3002 if (network_packet) {
3003 priv->last_rx_rssi = rx_status.signal;
3004 priv->last_beacon_time = priv->ucode_beacon_time;
3005 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
3008 fc = le16_to_cpu(header->frame_control);
3009 switch (fc & IEEE80211_FCTL_FTYPE) {
3010 case IEEE80211_FTYPE_MGMT:
3011 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3012 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3014 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
3017 case IEEE80211_FTYPE_CTL:
3018 #ifdef CONFIG_IWL4965_HT
3019 switch (fc & IEEE80211_FCTL_STYPE) {
3020 case IEEE80211_STYPE_BACK_REQ:
3021 IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
3022 iwl4965_handle_data_packet(priv, 0, include_phy,
3031 case IEEE80211_FTYPE_DATA: {
3032 DECLARE_MAC_BUF(mac1);
3033 DECLARE_MAC_BUF(mac2);
3034 DECLARE_MAC_BUF(mac3);
3036 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3037 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3040 if (unlikely(!network_packet))
3041 IWL_DEBUG_DROP("Dropping (non network): "
3043 print_mac(mac1, header->addr1),
3044 print_mac(mac2, header->addr2),
3045 print_mac(mac3, header->addr3));
3046 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
3047 IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
3048 print_mac(mac1, header->addr1),
3049 print_mac(mac2, header->addr2),
3050 print_mac(mac3, header->addr3));
3052 iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
3062 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
3063 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
3064 static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
3065 struct iwl_rx_mem_buffer *rxb)
3067 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3068 priv->last_phy_res[0] = 1;
3069 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
3070 sizeof(struct iwl4965_rx_phy_res));
3072 static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
3073 struct iwl_rx_mem_buffer *rxb)
3076 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3077 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3078 struct iwl4965_missed_beacon_notif *missed_beacon;
3080 missed_beacon = &pkt->u.missed_beacon;
3081 if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
3082 IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
3083 le32_to_cpu(missed_beacon->consequtive_missed_beacons),
3084 le32_to_cpu(missed_beacon->total_missed_becons),
3085 le32_to_cpu(missed_beacon->num_recvd_beacons),
3086 le32_to_cpu(missed_beacon->num_expected_beacons));
3087 if (!test_bit(STATUS_SCANNING, &priv->status))
3088 iwl_init_sensitivity(priv);
3090 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
3092 #ifdef CONFIG_IWL4965_HT
3095 * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
3097 static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
3098 int sta_id, int tid)
3100 unsigned long flags;
3102 /* Remove "disable" flag, to enable Tx for this TID */
3103 spin_lock_irqsave(&priv->sta_lock, flags);
3104 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3105 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3106 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3107 spin_unlock_irqrestore(&priv->sta_lock, flags);
3109 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3113 * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
3115 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
3116 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
3118 static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
3119 struct iwl_ht_agg *agg,
3120 struct iwl4965_compressed_ba_resp*
3125 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
3126 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3129 struct ieee80211_tx_status *tx_status;
3131 if (unlikely(!agg->wait_for_ba)) {
3132 IWL_ERROR("Received BA when not expected\n");
3136 /* Mark that the expected block-ack response arrived */
3137 agg->wait_for_ba = 0;
3138 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
3140 /* Calculate shift to align block-ack bits with our Tx window bits */
3141 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
3142 if (sh < 0) /* tbw something is wrong with indices */
3145 /* don't use 64-bit values for now */
3146 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
3148 if (agg->frame_count > (64 - sh)) {
3149 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
3153 /* check for success or failure according to the
3154 * transmitted bitmap and block-ack bitmap */
3155 bitmap &= agg->bitmap;
3157 /* For each frame attempted in aggregation,
3158 * update driver's record of tx frame's status. */
3159 for (i = 0; i < agg->frame_count ; i++) {
3160 ack = bitmap & (1 << i);
3162 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
3163 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
3164 agg->start_idx + i);
3167 tx_status = &priv->txq[scd_flow].txb[agg->start_idx].status;
3168 tx_status->flags = IEEE80211_TX_STATUS_ACK;
3169 tx_status->flags |= IEEE80211_TX_STATUS_AMPDU;
3170 tx_status->ampdu_ack_map = successes;
3171 tx_status->ampdu_ack_len = agg->frame_count;
3172 iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags,
3173 &tx_status->control);
3175 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
3181 * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
3183 static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
3186 /* Simply stop the queue, but don't change any configuration;
3187 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
3188 iwl_write_prph(priv,
3189 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
3190 (0 << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
3191 (1 << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3195 * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
3196 * priv->lock must be held by the caller
3198 static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
3199 u16 ssn_idx, u8 tx_fifo)
3203 if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3204 IWL_WARNING("queue number too small: %d, must be > %d\n",
3205 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3209 ret = iwl_grab_nic_access(priv);
3213 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3215 iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3217 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3218 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3219 /* supposes that ssn_idx is valid (!= 0xFFF) */
3220 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3222 iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3223 iwl4965_txq_ctx_deactivate(priv, txq_id);
3224 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3226 iwl_release_nic_access(priv);
3231 int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
3234 struct iwl_queue *q = &priv->txq[txq_id].q;
3235 u8 *addr = priv->stations[sta_id].sta.sta.addr;
3236 struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3238 switch (priv->stations[sta_id].tid[tid].agg.state) {
3239 case IWL_EMPTYING_HW_QUEUE_DELBA:
3240 /* We are reclaiming the last packet of the */
3241 /* aggregated HW queue */
3242 if (txq_id == tid_data->agg.txq_id &&
3243 q->read_ptr == q->write_ptr) {
3244 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3245 int tx_fifo = default_tid_to_tx_fifo[tid];
3246 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3247 iwl4965_tx_queue_agg_disable(priv, txq_id,
3249 tid_data->agg.state = IWL_AGG_OFF;
3250 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3253 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3254 /* We are reclaiming the last packet of the queue */
3255 if (tid_data->tfds_in_queue == 0) {
3256 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3257 tid_data->agg.state = IWL_AGG_ON;
3258 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3266 * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3268 * Handles block-acknowledge notification from device, which reports success
3269 * of frames sent via aggregation.
3271 static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
3272 struct iwl_rx_mem_buffer *rxb)
3274 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3275 struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
3277 struct iwl_tx_queue *txq = NULL;
3278 struct iwl_ht_agg *agg;
3279 DECLARE_MAC_BUF(mac);
3281 /* "flow" corresponds to Tx queue */
3282 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3284 /* "ssn" is start of block-ack Tx window, corresponds to index
3285 * (in Tx queue's circular buffer) of first TFD/frame in window */
3286 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3288 if (scd_flow >= priv->hw_params.max_txq_num) {
3289 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3293 txq = &priv->txq[scd_flow];
3294 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
3296 /* Find index just before block-ack window */
3297 index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
3299 /* TODO: Need to get this copy more safely - now good for debug */
3301 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3304 print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
3306 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
3307 "%d, scd_ssn = %d\n",
3310 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
3313 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
3315 (unsigned long long)agg->bitmap);
3317 /* Update driver's record of ACK vs. not for each frame in window */
3318 iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
3320 /* Release all TFDs before the SSN, i.e. all TFDs in front of
3321 * block-ack window (we assume that they've been successfully
3322 * transmitted ... if not, it's too late anyway). */
3323 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3324 /* calculate mac80211 ampdu sw queue to wake */
3326 scd_flow - IWL_BACK_QUEUE_FIRST_ID + priv->hw->queues;
3327 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3328 priv->stations[ba_resp->sta_id].
3329 tid[ba_resp->tid].tfds_in_queue -= freed;
3330 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
3331 priv->mac80211_registered &&
3332 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3333 ieee80211_wake_queue(priv->hw, ampdu_q);
3334 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3335 ba_resp->tid, scd_flow);
3340 * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3342 static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
3349 scd_q2ratid = ra_tid & IWL49_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3351 tbl_dw_addr = priv->scd_base_addr +
3352 IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3354 tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
3357 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3359 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3361 iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
3368 * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3370 * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3371 * i.e. it must be one of the higher queues used for aggregation
3373 static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
3374 int tx_fifo, int sta_id, int tid,
3377 unsigned long flags;
3381 if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3382 IWL_WARNING("queue number too small: %d, must be > %d\n",
3383 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3385 ra_tid = BUILD_RAxTID(sta_id, tid);
3387 /* Modify device's station table to Tx this TID */
3388 iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
3390 spin_lock_irqsave(&priv->lock, flags);
3391 rc = iwl_grab_nic_access(priv);
3393 spin_unlock_irqrestore(&priv->lock, flags);
3397 /* Stop this Tx queue before configuring it */
3398 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3400 /* Map receiver-address / traffic-ID to this queue */
3401 iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3403 /* Set this queue as a chain-building queue */
3404 iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3406 /* Place first TFD at index corresponding to start sequence number.
3407 * Assumes that ssn_idx is valid (!= 0xFFF) */
3408 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3409 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3410 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3412 /* Set up Tx window size and frame limit for this queue */
3413 iwl_write_targ_mem(priv,
3414 priv->scd_base_addr + IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3415 (SCD_WIN_SIZE << IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3416 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3418 iwl_write_targ_mem(priv, priv->scd_base_addr +
3419 IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3420 (SCD_FRAME_LIMIT << IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3421 & IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3423 iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3425 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
3426 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3428 iwl_release_nic_access(priv);
3429 spin_unlock_irqrestore(&priv->lock, flags);
3434 #endif /* CONFIG_IWL4965_HT */
3437 * iwl4965_add_station - Initialize a station's hardware rate table
3439 * The uCode's station table contains a table of fallback rates
3440 * for automatic fallback during transmission.
3442 * NOTE: This sets up a default set of values. These will be replaced later
3443 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
3446 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
3447 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
3448 * which requires station table entry to exist).
3450 void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
3453 struct iwl_link_quality_cmd link_cmd = {
3458 /* Set up the rate scaling to start at selected rate, fall back
3459 * all the way down to 1M in IEEE order, and then spin on 1M */
3461 r = IWL_RATE_54M_INDEX;
3462 else if (priv->band == IEEE80211_BAND_5GHZ)
3463 r = IWL_RATE_6M_INDEX;
3465 r = IWL_RATE_1M_INDEX;
3467 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3469 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
3470 rate_flags |= RATE_MCS_CCK_MSK;
3472 /* Use Tx antenna B only */
3473 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
3475 link_cmd.rs_table[i].rate_n_flags =
3476 iwl4965_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
3477 r = iwl4965_get_prev_ieee_rate(r);
3480 link_cmd.general_params.single_stream_ant_msk = 2;
3481 link_cmd.general_params.dual_stream_ant_msk = 3;
3482 link_cmd.agg_params.agg_dis_start_th = 3;
3483 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
3485 /* Update the rate scaling for control frame Tx to AP */
3486 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
3488 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
3489 sizeof(link_cmd), &link_cmd, NULL);
3492 #ifdef CONFIG_IWL4965_HT
3494 void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
3495 struct ieee80211_ht_info *sta_ht_inf)
3500 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
3503 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
3505 sta_flags = priv->stations[index].sta.station_flags;
3507 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
3509 switch (mimo_ps_mode) {
3510 case WLAN_HT_CAP_MIMO_PS_STATIC:
3511 sta_flags |= STA_FLG_MIMO_DIS_MSK;
3513 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
3514 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
3516 case WLAN_HT_CAP_MIMO_PS_DISABLED:
3519 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
3523 sta_flags |= cpu_to_le32(
3524 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
3526 sta_flags |= cpu_to_le32(
3527 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
3529 if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
3530 sta_flags |= STA_FLG_FAT_EN_MSK;
3532 sta_flags &= ~STA_FLG_FAT_EN_MSK;
3534 priv->stations[index].sta.station_flags = sta_flags;
3539 static int iwl4965_rx_agg_start(struct iwl_priv *priv,
3540 const u8 *addr, int tid, u16 ssn)
3542 unsigned long flags;
3545 sta_id = iwl_find_station(priv, addr);
3546 if (sta_id == IWL_INVALID_STATION)
3549 spin_lock_irqsave(&priv->sta_lock, flags);
3550 priv->stations[sta_id].sta.station_flags_msk = 0;
3551 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
3552 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
3553 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3554 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3555 spin_unlock_irqrestore(&priv->sta_lock, flags);
3557 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
3561 static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
3562 const u8 *addr, int tid)
3564 unsigned long flags;
3567 sta_id = iwl_find_station(priv, addr);
3568 if (sta_id == IWL_INVALID_STATION)
3571 spin_lock_irqsave(&priv->sta_lock, flags);
3572 priv->stations[sta_id].sta.station_flags_msk = 0;
3573 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
3574 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
3575 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3576 spin_unlock_irqrestore(&priv->sta_lock, flags);
3578 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
3583 * Find first available (lowest unused) Tx Queue, mark it "active".
3584 * Called only when finding queue for aggregation.
3585 * Should never return anything < 7, because they should already
3586 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
3588 static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
3592 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
3593 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
3598 static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
3599 u16 tid, u16 *start_seq_num)
3601 struct iwl_priv *priv = hw->priv;
3607 unsigned long flags;
3608 struct iwl_tid_data *tid_data;
3609 DECLARE_MAC_BUF(mac);
3611 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
3612 tx_fifo = default_tid_to_tx_fifo[tid];
3616 IWL_WARNING("%s on ra = %s tid = %d\n",
3617 __func__, print_mac(mac, ra), tid);
3619 sta_id = iwl_find_station(priv, ra);
3620 if (sta_id == IWL_INVALID_STATION)
3623 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
3624 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
3628 txq_id = iwl4965_txq_ctx_activate_free(priv);
3632 spin_lock_irqsave(&priv->sta_lock, flags);
3633 tid_data = &priv->stations[sta_id].tid[tid];
3634 ssn = SEQ_TO_SN(tid_data->seq_number);
3635 tid_data->agg.txq_id = txq_id;
3636 spin_unlock_irqrestore(&priv->sta_lock, flags);
3638 *start_seq_num = ssn;
3639 ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
3645 if (tid_data->tfds_in_queue == 0) {
3646 printk(KERN_ERR "HW queue is empty\n");
3647 tid_data->agg.state = IWL_AGG_ON;
3648 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
3650 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
3651 tid_data->tfds_in_queue);
3652 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3657 static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
3659 struct iwl_priv *priv = hw->priv;
3660 int tx_fifo_id, txq_id, sta_id, ssn = -1;
3661 struct iwl_tid_data *tid_data;
3662 int ret, write_ptr, read_ptr;
3663 unsigned long flags;
3664 DECLARE_MAC_BUF(mac);
3667 IWL_ERROR("ra = NULL\n");
3671 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
3672 tx_fifo_id = default_tid_to_tx_fifo[tid];
3676 sta_id = iwl_find_station(priv, ra);
3678 if (sta_id == IWL_INVALID_STATION)
3681 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
3682 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
3684 tid_data = &priv->stations[sta_id].tid[tid];
3685 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
3686 txq_id = tid_data->agg.txq_id;
3687 write_ptr = priv->txq[txq_id].q.write_ptr;
3688 read_ptr = priv->txq[txq_id].q.read_ptr;
3690 /* The queue is not empty */
3691 if (write_ptr != read_ptr) {
3692 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
3693 priv->stations[sta_id].tid[tid].agg.state =
3694 IWL_EMPTYING_HW_QUEUE_DELBA;
3698 IWL_DEBUG_HT("HW queue is empty\n");
3699 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
3701 spin_lock_irqsave(&priv->lock, flags);
3702 ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
3703 spin_unlock_irqrestore(&priv->lock, flags);
3708 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
3713 int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
3714 enum ieee80211_ampdu_mlme_action action,
3715 const u8 *addr, u16 tid, u16 *ssn)
3717 struct iwl_priv *priv = hw->priv;
3718 DECLARE_MAC_BUF(mac);
3720 IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
3721 print_mac(mac, addr), tid);
3724 case IEEE80211_AMPDU_RX_START:
3725 IWL_DEBUG_HT("start Rx\n");
3726 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
3727 case IEEE80211_AMPDU_RX_STOP:
3728 IWL_DEBUG_HT("stop Rx\n");
3729 return iwl4965_rx_agg_stop(priv, addr, tid);
3730 case IEEE80211_AMPDU_TX_START:
3731 IWL_DEBUG_HT("start Tx\n");
3732 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
3733 case IEEE80211_AMPDU_TX_STOP:
3734 IWL_DEBUG_HT("stop Tx\n");
3735 return iwl4965_tx_agg_stop(hw, addr, tid);
3737 IWL_DEBUG_HT("unknown\n");
3743 #endif /* CONFIG_IWL4965_HT */
3746 static u16 iwl4965_get_hcmd_size(u8 cmd_id, u16 len)
3750 return (u16) sizeof(struct iwl4965_rxon_cmd);
3756 static u16 iwl4965_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
3758 struct iwl4965_addsta_cmd *addsta = (struct iwl4965_addsta_cmd *)data;
3759 addsta->mode = cmd->mode;
3760 memcpy(&addsta->sta, &cmd->sta, sizeof(struct sta_id_modify));
3761 memcpy(&addsta->key, &cmd->key, sizeof(struct iwl4965_keyinfo));
3762 addsta->station_flags = cmd->station_flags;
3763 addsta->station_flags_msk = cmd->station_flags_msk;
3764 addsta->tid_disable_tx = cmd->tid_disable_tx;
3765 addsta->add_immediate_ba_tid = cmd->add_immediate_ba_tid;
3766 addsta->remove_immediate_ba_tid = cmd->remove_immediate_ba_tid;
3767 addsta->add_immediate_ba_ssn = cmd->add_immediate_ba_ssn;
3768 addsta->reserved1 = __constant_cpu_to_le16(0);
3769 addsta->reserved2 = __constant_cpu_to_le32(0);
3771 return (u16)sizeof(struct iwl4965_addsta_cmd);
3773 /* Set up 4965-specific Rx frame reply handlers */
3774 static void iwl4965_rx_handler_setup(struct iwl_priv *priv)
3776 /* Legacy Rx frames */
3777 priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
3779 /* High-throughput (HT) Rx frames */
3780 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
3781 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
3783 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
3784 iwl4965_rx_missed_beacon_notif;
3786 #ifdef CONFIG_IWL4965_HT
3787 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
3788 #endif /* CONFIG_IWL4965_HT */
3791 void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
3793 INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
3794 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3795 INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
3797 init_timer(&priv->statistics_periodic);
3798 priv->statistics_periodic.data = (unsigned long)priv;
3799 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
3802 void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
3804 del_timer_sync(&priv->statistics_periodic);
3806 cancel_delayed_work(&priv->init_alive_start);
3810 static struct iwl_hcmd_ops iwl4965_hcmd = {
3811 .rxon_assoc = iwl4965_send_rxon_assoc,
3814 static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
3815 .get_hcmd_size = iwl4965_get_hcmd_size,
3816 .enqueue_hcmd = iwl4965_enqueue_hcmd,
3817 .build_addsta_hcmd = iwl4965_build_addsta_hcmd,
3818 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3819 .chain_noise_reset = iwl4965_chain_noise_reset,
3820 .gain_computation = iwl4965_gain_computation,
3824 static struct iwl_lib_ops iwl4965_lib = {
3825 .set_hw_params = iwl4965_hw_set_hw_params,
3826 .alloc_shared_mem = iwl4965_alloc_shared_mem,
3827 .free_shared_mem = iwl4965_free_shared_mem,
3828 .shared_mem_rx_idx = iwl4965_shared_mem_rx_idx,
3829 .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
3830 .disable_tx_fifo = iwl4965_disable_tx_fifo,
3831 .rx_handler_setup = iwl4965_rx_handler_setup,
3832 .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
3833 .alive_notify = iwl4965_alive_notify,
3834 .init_alive_start = iwl4965_init_alive_start,
3835 .load_ucode = iwl4965_load_bsm,
3837 .init = iwl4965_apm_init,
3838 .config = iwl4965_nic_config,
3839 .set_pwr_src = iwl4965_set_pwr_src,
3842 .regulatory_bands = {
3843 EEPROM_REGULATORY_BAND_1_CHANNELS,
3844 EEPROM_REGULATORY_BAND_2_CHANNELS,
3845 EEPROM_REGULATORY_BAND_3_CHANNELS,
3846 EEPROM_REGULATORY_BAND_4_CHANNELS,
3847 EEPROM_REGULATORY_BAND_5_CHANNELS,
3848 EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
3849 EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
3851 .verify_signature = iwlcore_eeprom_verify_signature,
3852 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
3853 .release_semaphore = iwlcore_eeprom_release_semaphore,
3854 .check_version = iwl4965_eeprom_check_version,
3855 .query_addr = iwlcore_eeprom_query_addr,
3857 .radio_kill_sw = iwl4965_radio_kill_sw,
3858 .set_power = iwl4965_set_power,
3859 .update_chain_flags = iwl4965_update_chain_flags,
3862 static struct iwl_ops iwl4965_ops = {
3863 .lib = &iwl4965_lib,
3864 .hcmd = &iwl4965_hcmd,
3865 .utils = &iwl4965_hcmd_utils,
3868 struct iwl_cfg iwl4965_agn_cfg = {
3870 .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
3871 .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
3872 .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
3873 .ops = &iwl4965_ops,
3874 .mod_params = &iwl4965_mod_params,
3877 module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
3878 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
3879 module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
3880 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
3881 module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
3882 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
3883 module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
3884 MODULE_PARM_DESC(debug, "debug output mask");
3886 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
3887 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
3889 module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
3890 MODULE_PARM_DESC(queues_num, "number of hw queues.");
3893 module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
3894 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
3895 module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
3896 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
3897 module_param_named(fw_restart4965, iwl4965_mod_params.restart_fw, int, 0444);
3898 MODULE_PARM_DESC(fw_restart4965, "restart firmware in case of error");