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,
53 /* the rest are 0 by default */
56 #ifdef CONFIG_IWL4965_HT
58 static const u16 default_tid_to_tx_fifo[] = {
78 #endif /*CONFIG_IWL4965_HT */
80 /* check contents of special bootstrap uCode SRAM */
81 static int iwl4965_verify_bsm(struct iwl_priv *priv)
83 __le32 *image = priv->ucode_boot.v_addr;
84 u32 len = priv->ucode_boot.len;
88 IWL_DEBUG_INFO("Begin verify bsm\n");
90 /* verify BSM SRAM contents */
91 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
92 for (reg = BSM_SRAM_LOWER_BOUND;
93 reg < BSM_SRAM_LOWER_BOUND + len;
94 reg += sizeof(u32), image++) {
95 val = iwl_read_prph(priv, reg);
96 if (val != le32_to_cpu(*image)) {
97 IWL_ERROR("BSM uCode verification failed at "
98 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
100 reg - BSM_SRAM_LOWER_BOUND, len,
101 val, le32_to_cpu(*image));
106 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
112 * iwl4965_load_bsm - Load bootstrap instructions
116 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
117 * in special SRAM that does not power down during RFKILL. When powering back
118 * up after power-saving sleeps (or during initial uCode load), the BSM loads
119 * the bootstrap program into the on-board processor, and starts it.
121 * The bootstrap program loads (via DMA) instructions and data for a new
122 * program from host DRAM locations indicated by the host driver in the
123 * BSM_DRAM_* registers. Once the new program is loaded, it starts
126 * When initializing the NIC, the host driver points the BSM to the
127 * "initialize" uCode image. This uCode sets up some internal data, then
128 * notifies host via "initialize alive" that it is complete.
130 * The host then replaces the BSM_DRAM_* pointer values to point to the
131 * normal runtime uCode instructions and a backup uCode data cache buffer
132 * (filled initially with starting data values for the on-board processor),
133 * then triggers the "initialize" uCode to load and launch the runtime uCode,
134 * which begins normal operation.
136 * When doing a power-save shutdown, runtime uCode saves data SRAM into
137 * the backup data cache in DRAM before SRAM is powered down.
139 * When powering back up, the BSM loads the bootstrap program. This reloads
140 * the runtime uCode instructions and the backup data cache into SRAM,
141 * and re-launches the runtime uCode from where it left off.
143 static int iwl4965_load_bsm(struct iwl_priv *priv)
145 __le32 *image = priv->ucode_boot.v_addr;
146 u32 len = priv->ucode_boot.len;
156 IWL_DEBUG_INFO("Begin load bsm\n");
158 /* make sure bootstrap program is no larger than BSM's SRAM size */
159 if (len > IWL_MAX_BSM_SIZE)
162 /* Tell bootstrap uCode where to find the "Initialize" uCode
163 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
164 * NOTE: iwl4965_initialize_alive_start() will replace these values,
165 * after the "initialize" uCode has run, to point to
166 * runtime/protocol instructions and backup data cache. */
167 pinst = priv->ucode_init.p_addr >> 4;
168 pdata = priv->ucode_init_data.p_addr >> 4;
169 inst_len = priv->ucode_init.len;
170 data_len = priv->ucode_init_data.len;
172 ret = iwl_grab_nic_access(priv);
176 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
177 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
178 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
179 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
181 /* Fill BSM memory with bootstrap instructions */
182 for (reg_offset = BSM_SRAM_LOWER_BOUND;
183 reg_offset < BSM_SRAM_LOWER_BOUND + len;
184 reg_offset += sizeof(u32), image++)
185 _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
187 ret = iwl4965_verify_bsm(priv);
189 iwl_release_nic_access(priv);
193 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
194 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
195 iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
196 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
198 /* Load bootstrap code into instruction SRAM now,
199 * to prepare to load "initialize" uCode */
200 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
202 /* Wait for load of bootstrap uCode to finish */
203 for (i = 0; i < 100; i++) {
204 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
205 if (!(done & BSM_WR_CTRL_REG_BIT_START))
210 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
212 IWL_ERROR("BSM write did not complete!\n");
216 /* Enable future boot loads whenever power management unit triggers it
217 * (e.g. when powering back up after power-save shutdown) */
218 iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
220 iwl_release_nic_access(priv);
226 * iwl4965_set_ucode_ptrs - Set uCode address location
228 * Tell initialization uCode where to find runtime uCode.
230 * BSM registers initially contain pointers to initialization uCode.
231 * We need to replace them to load runtime uCode inst and data,
232 * and to save runtime data when powering down.
234 static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
241 /* bits 35:4 for 4965 */
242 pinst = priv->ucode_code.p_addr >> 4;
243 pdata = priv->ucode_data_backup.p_addr >> 4;
245 spin_lock_irqsave(&priv->lock, flags);
246 ret = iwl_grab_nic_access(priv);
248 spin_unlock_irqrestore(&priv->lock, flags);
252 /* Tell bootstrap uCode where to find image to load */
253 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
254 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
255 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
256 priv->ucode_data.len);
258 /* Inst bytecount must be last to set up, bit 31 signals uCode
259 * that all new ptr/size info is in place */
260 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
261 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
262 iwl_release_nic_access(priv);
264 spin_unlock_irqrestore(&priv->lock, flags);
266 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
272 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
274 * Called after REPLY_ALIVE notification received from "initialize" uCode.
276 * The 4965 "initialize" ALIVE reply contains calibration data for:
277 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
278 * (3945 does not contain this data).
280 * Tell "initialize" uCode to go ahead and load the runtime uCode.
282 static void iwl4965_init_alive_start(struct iwl_priv *priv)
284 /* Check alive response for "valid" sign from uCode */
285 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
286 /* We had an error bringing up the hardware, so take it
287 * all the way back down so we can try again */
288 IWL_DEBUG_INFO("Initialize Alive failed.\n");
292 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
293 * This is a paranoid check, because we would not have gotten the
294 * "initialize" alive if code weren't properly loaded. */
295 if (iwl_verify_ucode(priv)) {
296 /* Runtime instruction load was bad;
297 * take it all the way back down so we can try again */
298 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
302 /* Calculate temperature */
303 priv->temperature = iwl4965_get_temperature(priv);
305 /* Send pointers to protocol/runtime uCode image ... init code will
306 * load and launch runtime uCode, which will send us another "Alive"
308 IWL_DEBUG_INFO("Initialization Alive received.\n");
309 if (iwl4965_set_ucode_ptrs(priv)) {
310 /* Runtime instruction load won't happen;
311 * take it all the way back down so we can try again */
312 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
318 queue_work(priv->workqueue, &priv->restart);
321 static int is_fat_channel(__le32 rxon_flags)
323 return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
324 (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
327 int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
331 /* 4965 HT rate format */
332 if (rate_n_flags & RATE_MCS_HT_MSK) {
333 idx = (rate_n_flags & 0xff);
335 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
336 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
338 idx += IWL_FIRST_OFDM_RATE;
339 /* skip 9M not supported in ht*/
340 if (idx >= IWL_RATE_9M_INDEX)
342 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
345 /* 4965 legacy rate format, search for match in table */
347 for (idx = 0; idx < ARRAY_SIZE(iwl4965_rates); idx++)
348 if (iwl4965_rates[idx].plcp == (rate_n_flags & 0xFF))
356 * translate ucode response to mac80211 tx status control values
358 void iwl4965_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
359 struct ieee80211_tx_control *control)
363 control->antenna_sel_tx =
364 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
365 if (rate_n_flags & RATE_MCS_HT_MSK)
366 control->flags |= IEEE80211_TXCTL_OFDM_HT;
367 if (rate_n_flags & RATE_MCS_GF_MSK)
368 control->flags |= IEEE80211_TXCTL_GREEN_FIELD;
369 if (rate_n_flags & RATE_MCS_FAT_MSK)
370 control->flags |= IEEE80211_TXCTL_40_MHZ_WIDTH;
371 if (rate_n_flags & RATE_MCS_DUP_MSK)
372 control->flags |= IEEE80211_TXCTL_DUP_DATA;
373 if (rate_n_flags & RATE_MCS_SGI_MSK)
374 control->flags |= IEEE80211_TXCTL_SHORT_GI;
375 /* since iwl4965_hwrate_to_plcp_idx is band indifferent, we always use
376 * IEEE80211_BAND_2GHZ band as it contains all the rates */
377 rate_index = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
378 if (rate_index == -1)
379 control->tx_rate = NULL;
382 &priv->bands[IEEE80211_BAND_2GHZ].bitrates[rate_index];
385 int iwl4965_hw_rxq_stop(struct iwl_priv *priv)
390 spin_lock_irqsave(&priv->lock, flags);
391 rc = iwl_grab_nic_access(priv);
393 spin_unlock_irqrestore(&priv->lock, flags);
398 iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
399 rc = iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
402 IWL_ERROR("Can't stop Rx DMA.\n");
404 iwl_release_nic_access(priv);
405 spin_unlock_irqrestore(&priv->lock, flags);
414 static int iwl4965_eeprom_check_version(struct iwl_priv *priv)
419 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
421 calib_ver = iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
423 if (eeprom_ver < EEPROM_4965_EEPROM_VERSION ||
424 calib_ver < EEPROM_4965_TX_POWER_VERSION)
429 IWL_ERROR("Unsuported EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
430 eeprom_ver, EEPROM_4965_EEPROM_VERSION,
431 calib_ver, EEPROM_4965_TX_POWER_VERSION);
435 int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
440 spin_lock_irqsave(&priv->lock, flags);
441 ret = iwl_grab_nic_access(priv);
443 spin_unlock_irqrestore(&priv->lock, flags);
447 if (src == IWL_PWR_SRC_VAUX) {
449 ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
452 if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
453 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
454 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
455 ~APMG_PS_CTRL_MSK_PWR_SRC);
458 iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
459 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
460 ~APMG_PS_CTRL_MSK_PWR_SRC);
463 iwl_release_nic_access(priv);
464 spin_unlock_irqrestore(&priv->lock, flags);
469 static int iwl4965_disable_tx_fifo(struct iwl_priv *priv)
474 spin_lock_irqsave(&priv->lock, flags);
476 ret = iwl_grab_nic_access(priv);
478 IWL_ERROR("Tx fifo reset failed");
479 spin_unlock_irqrestore(&priv->lock, flags);
483 iwl_write_prph(priv, IWL49_SCD_TXFACT, 0);
484 iwl_release_nic_access(priv);
485 spin_unlock_irqrestore(&priv->lock, flags);
490 static int iwl4965_apm_init(struct iwl_priv *priv)
495 spin_lock_irqsave(&priv->lock, flags);
496 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
497 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
499 /* set "initialization complete" bit to move adapter
500 * D0U* --> D0A* state */
501 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
503 /* wait for clock stabilization */
504 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
505 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
506 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
508 IWL_DEBUG_INFO("Failed to init the card\n");
512 ret = iwl_grab_nic_access(priv);
517 iwl_write_prph(priv, APMG_CLK_CTRL_REG,
518 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
522 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
523 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
525 iwl_release_nic_access(priv);
527 spin_unlock_irqrestore(&priv->lock, flags);
532 static void iwl4965_nic_config(struct iwl_priv *priv)
539 spin_lock_irqsave(&priv->lock, flags);
541 if ((priv->rev_id & 0x80) == 0x80 && (priv->rev_id & 0x7f) < 8) {
542 pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
543 /* Enable No Snoop field */
544 pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
548 pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
550 /* disable L1 entry -- workaround for pre-B1 */
551 pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
553 radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
555 /* write radio config values to register */
556 if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) == EEPROM_4965_RF_CFG_TYPE_MAX)
557 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
558 EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
559 EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
560 EEPROM_RF_CFG_DASH_MSK(radio_cfg));
562 /* set CSR_HW_CONFIG_REG for uCode use */
563 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
564 CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
565 CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
567 priv->calib_info = (struct iwl_eeprom_calib_info *)
568 iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
570 spin_unlock_irqrestore(&priv->lock, flags);
573 int iwl4965_hw_nic_stop_master(struct iwl_priv *priv)
579 spin_lock_irqsave(&priv->lock, flags);
581 /* set stop master bit */
582 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
584 reg_val = iwl_read32(priv, CSR_GP_CNTRL);
586 if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
587 (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
588 IWL_DEBUG_INFO("Card in power save, master is already "
591 rc = iwl_poll_bit(priv, CSR_RESET,
592 CSR_RESET_REG_FLAG_MASTER_DISABLED,
593 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
595 spin_unlock_irqrestore(&priv->lock, flags);
600 spin_unlock_irqrestore(&priv->lock, flags);
601 IWL_DEBUG_INFO("stop master\n");
607 * iwl4965_hw_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
609 void iwl4965_hw_txq_ctx_stop(struct iwl_priv *priv)
615 /* Stop each Tx DMA channel, and wait for it to be idle */
616 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
617 spin_lock_irqsave(&priv->lock, flags);
618 if (iwl_grab_nic_access(priv)) {
619 spin_unlock_irqrestore(&priv->lock, flags);
623 iwl_write_direct32(priv,
624 FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
625 iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
626 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
628 iwl_release_nic_access(priv);
629 spin_unlock_irqrestore(&priv->lock, flags);
632 /* Deallocate memory for all Tx queues */
633 iwl_hw_txq_ctx_free(priv);
636 int iwl4965_hw_nic_reset(struct iwl_priv *priv)
641 iwl4965_hw_nic_stop_master(priv);
643 spin_lock_irqsave(&priv->lock, flags);
645 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
649 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
650 rc = iwl_poll_bit(priv, CSR_RESET,
651 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
652 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
656 rc = iwl_grab_nic_access(priv);
658 iwl_write_prph(priv, APMG_CLK_EN_REG,
659 APMG_CLK_VAL_DMA_CLK_RQT |
660 APMG_CLK_VAL_BSM_CLK_RQT);
664 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
665 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
667 iwl_release_nic_access(priv);
670 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
671 wake_up_interruptible(&priv->wait_command_queue);
673 spin_unlock_irqrestore(&priv->lock, flags);
679 #define REG_RECALIB_PERIOD (60)
682 * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
684 * This callback is provided in order to send a statistics request.
686 * This timer function is continually reset to execute within
687 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
688 * was received. We need to ensure we receive the statistics in order
689 * to update the temperature used for calibrating the TXPOWER.
691 static void iwl4965_bg_statistics_periodic(unsigned long data)
693 struct iwl_priv *priv = (struct iwl_priv *)data;
695 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
698 iwl_send_statistics_request(priv, CMD_ASYNC);
701 void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
703 struct iwl4965_ct_kill_config cmd;
707 spin_lock_irqsave(&priv->lock, flags);
708 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
709 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
710 spin_unlock_irqrestore(&priv->lock, flags);
712 cmd.critical_temperature_R =
713 cpu_to_le32(priv->hw_params.ct_kill_threshold);
715 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
718 IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
720 IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded, "
721 "critical temperature is %d\n",
722 cmd.critical_temperature_R);
725 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
727 /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
728 * Called after every association, but this runs only once!
729 * ... once chain noise is calibrated the first time, it's good forever. */
730 static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
732 struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
734 if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl_is_associated(priv)) {
735 struct iwl4965_calibration_cmd cmd;
737 memset(&cmd, 0, sizeof(cmd));
738 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
742 if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
744 IWL_ERROR("Could not send REPLY_PHY_CALIBRATION_CMD\n");
745 data->state = IWL_CHAIN_NOISE_ACCUMULATE;
746 IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
750 static void iwl4965_gain_computation(struct iwl_priv *priv,
752 u16 min_average_noise_antenna_i,
753 u32 min_average_noise)
756 struct iwl_chain_noise_data *data = &priv->chain_noise_data;
758 data->delta_gain_code[min_average_noise_antenna_i] = 0;
760 for (i = 0; i < NUM_RX_CHAINS; i++) {
763 if (!(data->disconn_array[i]) &&
764 (data->delta_gain_code[i] ==
765 CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
766 delta_g = average_noise[i] - min_average_noise;
767 data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
768 data->delta_gain_code[i] =
769 min(data->delta_gain_code[i],
770 (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
772 data->delta_gain_code[i] =
773 (data->delta_gain_code[i] | (1 << 2));
775 data->delta_gain_code[i] = 0;
778 IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
779 data->delta_gain_code[0],
780 data->delta_gain_code[1],
781 data->delta_gain_code[2]);
783 /* Differential gain gets sent to uCode only once */
784 if (!data->radio_write) {
785 struct iwl4965_calibration_cmd cmd;
786 data->radio_write = 1;
788 memset(&cmd, 0, sizeof(cmd));
789 cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
790 cmd.diff_gain_a = data->delta_gain_code[0];
791 cmd.diff_gain_b = data->delta_gain_code[1];
792 cmd.diff_gain_c = data->delta_gain_code[2];
793 ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
796 IWL_DEBUG_CALIB("fail sending cmd "
797 "REPLY_PHY_CALIBRATION_CMD \n");
799 /* TODO we might want recalculate
800 * rx_chain in rxon cmd */
802 /* Mark so we run this algo only once! */
803 data->state = IWL_CHAIN_NOISE_CALIBRATED;
805 data->chain_noise_a = 0;
806 data->chain_noise_b = 0;
807 data->chain_noise_c = 0;
808 data->chain_signal_a = 0;
809 data->chain_signal_b = 0;
810 data->chain_signal_c = 0;
811 data->beacon_count = 0;
814 static void iwl4965_bg_sensitivity_work(struct work_struct *work)
816 struct iwl_priv *priv = container_of(work, struct iwl_priv,
819 mutex_lock(&priv->mutex);
821 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
822 test_bit(STATUS_SCANNING, &priv->status)) {
823 mutex_unlock(&priv->mutex);
827 if (priv->start_calib) {
828 iwl_chain_noise_calibration(priv, &priv->statistics);
830 iwl_sensitivity_calibration(priv, &priv->statistics);
833 mutex_unlock(&priv->mutex);
836 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
838 static void iwl4965_bg_txpower_work(struct work_struct *work)
840 struct iwl_priv *priv = container_of(work, struct iwl_priv,
843 /* If a scan happened to start before we got here
844 * then just return; the statistics notification will
845 * kick off another scheduled work to compensate for
846 * any temperature delta we missed here. */
847 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
848 test_bit(STATUS_SCANNING, &priv->status))
851 mutex_lock(&priv->mutex);
853 /* Regardless of if we are assocaited, we must reconfigure the
854 * TX power since frames can be sent on non-radar channels while
856 iwl4965_hw_reg_send_txpower(priv);
858 /* Update last_temperature to keep is_calib_needed from running
859 * when it isn't needed... */
860 priv->last_temperature = priv->temperature;
862 mutex_unlock(&priv->mutex);
866 * Acquire priv->lock before calling this function !
868 static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
870 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
871 (index & 0xff) | (txq_id << 8));
872 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
876 * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
877 * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
878 * @scd_retry: (1) Indicates queue will be used in aggregation mode
880 * NOTE: Acquire priv->lock before calling this function !
882 static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
883 struct iwl4965_tx_queue *txq,
884 int tx_fifo_id, int scd_retry)
886 int txq_id = txq->q.id;
888 /* Find out whether to activate Tx queue */
889 int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
891 /* Set up and activate */
892 iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
893 (active << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
894 (tx_fifo_id << IWL49_SCD_QUEUE_STTS_REG_POS_TXF) |
895 (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_WSL) |
896 (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
897 IWL49_SCD_QUEUE_STTS_REG_MSK);
899 txq->sched_retry = scd_retry;
901 IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
902 active ? "Activate" : "Deactivate",
903 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
906 static const u16 default_queue_to_tx_fifo[] = {
916 static inline void iwl4965_txq_ctx_activate(struct iwl_priv *priv, int txq_id)
918 set_bit(txq_id, &priv->txq_ctx_active_msk);
921 static inline void iwl4965_txq_ctx_deactivate(struct iwl_priv *priv, int txq_id)
923 clear_bit(txq_id, &priv->txq_ctx_active_msk);
926 int iwl4965_alive_notify(struct iwl_priv *priv)
933 spin_lock_irqsave(&priv->lock, flags);
935 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
936 memset(&(priv->sensitivity_data), 0,
937 sizeof(struct iwl_sensitivity_data));
938 memset(&(priv->chain_noise_data), 0,
939 sizeof(struct iwl_chain_noise_data));
940 for (i = 0; i < NUM_RX_CHAINS; i++)
941 priv->chain_noise_data.delta_gain_code[i] =
942 CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
943 #endif /* CONFIG_IWL4965_RUN_TIME_CALIB*/
944 ret = iwl_grab_nic_access(priv);
946 spin_unlock_irqrestore(&priv->lock, flags);
950 /* Clear 4965's internal Tx Scheduler data base */
951 priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
952 a = priv->scd_base_addr + IWL49_SCD_CONTEXT_DATA_OFFSET;
953 for (; a < priv->scd_base_addr + IWL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
954 iwl_write_targ_mem(priv, a, 0);
955 for (; a < priv->scd_base_addr + IWL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
956 iwl_write_targ_mem(priv, a, 0);
957 for (; a < sizeof(u16) * priv->hw_params.max_txq_num; a += 4)
958 iwl_write_targ_mem(priv, a, 0);
960 /* Tel 4965 where to find Tx byte count tables */
961 iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
963 offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
965 /* Disable chain mode for all queues */
966 iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
968 /* Initialize each Tx queue (including the command queue) */
969 for (i = 0; i < priv->hw_params.max_txq_num; i++) {
971 /* TFD circular buffer read/write indexes */
972 iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
973 iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
975 /* Max Tx Window size for Scheduler-ACK mode */
976 iwl_write_targ_mem(priv, priv->scd_base_addr +
977 IWL49_SCD_CONTEXT_QUEUE_OFFSET(i),
979 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
980 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
983 iwl_write_targ_mem(priv, priv->scd_base_addr +
984 IWL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
987 IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
988 IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
991 iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
992 (1 << priv->hw_params.max_txq_num) - 1);
994 /* Activate all Tx DMA/FIFO channels */
995 iwl_write_prph(priv, IWL49_SCD_TXFACT,
996 SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
998 iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
1000 /* Map each Tx/cmd queue to its corresponding fifo */
1001 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
1002 int ac = default_queue_to_tx_fifo[i];
1003 iwl4965_txq_ctx_activate(priv, i);
1004 iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
1007 iwl_release_nic_access(priv);
1008 spin_unlock_irqrestore(&priv->lock, flags);
1010 /* Ask for statistics now, the uCode will send statistics notification
1011 * periodically after association */
1012 iwl_send_statistics_request(priv, CMD_ASYNC);
1016 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1017 static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
1021 .auto_corr_min_ofdm = 85,
1022 .auto_corr_min_ofdm_mrc = 170,
1023 .auto_corr_min_ofdm_x1 = 105,
1024 .auto_corr_min_ofdm_mrc_x1 = 220,
1026 .auto_corr_max_ofdm = 120,
1027 .auto_corr_max_ofdm_mrc = 210,
1028 .auto_corr_max_ofdm_x1 = 140,
1029 .auto_corr_max_ofdm_mrc_x1 = 270,
1031 .auto_corr_min_cck = 125,
1032 .auto_corr_max_cck = 200,
1033 .auto_corr_min_cck_mrc = 200,
1034 .auto_corr_max_cck_mrc = 400,
1042 * iwl4965_hw_set_hw_params
1044 * Called when initializing driver
1046 int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
1049 if ((priv->cfg->mod_params->num_of_queues > IWL49_NUM_QUEUES) ||
1050 (priv->cfg->mod_params->num_of_queues < IWL_MIN_NUM_QUEUES)) {
1051 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
1052 IWL_MIN_NUM_QUEUES, IWL49_NUM_QUEUES);
1056 priv->hw_params.max_txq_num = priv->cfg->mod_params->num_of_queues;
1057 priv->hw_params.sw_crypto = priv->cfg->mod_params->sw_crypto;
1058 priv->hw_params.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
1059 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1060 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
1061 if (priv->cfg->mod_params->amsdu_size_8K)
1062 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
1064 priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
1065 priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
1066 priv->hw_params.max_stations = IWL4965_STATION_COUNT;
1067 priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
1069 priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
1070 priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
1071 priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
1072 priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
1074 priv->hw_params.tx_chains_num = 2;
1075 priv->hw_params.rx_chains_num = 2;
1076 priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
1077 priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
1078 priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
1080 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
1081 priv->hw_params.sens = &iwl4965_sensitivity;
1087 /* set card power command */
1088 static int iwl4965_set_power(struct iwl_priv *priv,
1093 ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
1094 sizeof(struct iwl4965_powertable_cmd),
1098 int iwl4965_hw_reg_set_txpower(struct iwl_priv *priv, s8 power)
1100 IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
1104 static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
1117 *res = ((num * 2 + denom) / (denom * 2)) * sign;
1123 * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
1125 * Determines power supply voltage compensation for txpower calculations.
1126 * Returns number of 1/2-dB steps to subtract from gain table index,
1127 * to compensate for difference between power supply voltage during
1128 * factory measurements, vs. current power supply voltage.
1130 * Voltage indication is higher for lower voltage.
1131 * Lower voltage requires more gain (lower gain table index).
1133 static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
1134 s32 current_voltage)
1138 if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
1139 (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
1142 iwl4965_math_div_round(current_voltage - eeprom_voltage,
1143 TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
1145 if (current_voltage > eeprom_voltage)
1147 if ((comp < -2) || (comp > 2))
1153 static const struct iwl_channel_info *
1154 iwl4965_get_channel_txpower_info(struct iwl_priv *priv,
1155 enum ieee80211_band band, u16 channel)
1157 const struct iwl_channel_info *ch_info;
1159 ch_info = iwl_get_channel_info(priv, band, channel);
1161 if (!is_channel_valid(ch_info))
1167 static s32 iwl4965_get_tx_atten_grp(u16 channel)
1169 if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
1170 channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
1171 return CALIB_CH_GROUP_5;
1173 if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
1174 channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
1175 return CALIB_CH_GROUP_1;
1177 if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
1178 channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
1179 return CALIB_CH_GROUP_2;
1181 if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
1182 channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
1183 return CALIB_CH_GROUP_3;
1185 if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
1186 channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
1187 return CALIB_CH_GROUP_4;
1189 IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
1193 static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
1197 for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
1198 if (priv->calib_info->band_info[b].ch_from == 0)
1201 if ((channel >= priv->calib_info->band_info[b].ch_from)
1202 && (channel <= priv->calib_info->band_info[b].ch_to))
1209 static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
1216 iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
1222 * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
1224 * Interpolates factory measurements from the two sample channels within a
1225 * sub-band, to apply to channel of interest. Interpolation is proportional to
1226 * differences in channel frequencies, which is proportional to differences
1227 * in channel number.
1229 static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
1230 struct iwl_eeprom_calib_ch_info *chan_info)
1235 const struct iwl_eeprom_calib_measure *m1;
1236 const struct iwl_eeprom_calib_measure *m2;
1237 struct iwl_eeprom_calib_measure *omeas;
1241 s = iwl4965_get_sub_band(priv, channel);
1242 if (s >= EEPROM_TX_POWER_BANDS) {
1243 IWL_ERROR("Tx Power can not find channel %d ", channel);
1247 ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
1248 ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
1249 chan_info->ch_num = (u8) channel;
1251 IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
1252 channel, s, ch_i1, ch_i2);
1254 for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
1255 for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
1256 m1 = &(priv->calib_info->band_info[s].ch1.
1257 measurements[c][m]);
1258 m2 = &(priv->calib_info->band_info[s].ch2.
1259 measurements[c][m]);
1260 omeas = &(chan_info->measurements[c][m]);
1263 (u8) iwl4965_interpolate_value(channel, ch_i1,
1268 (u8) iwl4965_interpolate_value(channel, ch_i1,
1269 m1->gain_idx, ch_i2,
1271 omeas->temperature =
1272 (u8) iwl4965_interpolate_value(channel, ch_i1,
1277 (s8) iwl4965_interpolate_value(channel, ch_i1,
1282 ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
1283 m1->actual_pow, m2->actual_pow, omeas->actual_pow);
1285 ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
1286 m1->gain_idx, m2->gain_idx, omeas->gain_idx);
1288 ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
1289 m1->pa_det, m2->pa_det, omeas->pa_det);
1291 ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
1292 m1->temperature, m2->temperature,
1293 omeas->temperature);
1300 /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
1301 * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
1302 static s32 back_off_table[] = {
1303 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
1304 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
1305 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
1306 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
1310 /* Thermal compensation values for txpower for various frequency ranges ...
1311 * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
1312 static struct iwl4965_txpower_comp_entry {
1313 s32 degrees_per_05db_a;
1314 s32 degrees_per_05db_a_denom;
1315 } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
1316 {9, 2}, /* group 0 5.2, ch 34-43 */
1317 {4, 1}, /* group 1 5.2, ch 44-70 */
1318 {4, 1}, /* group 2 5.2, ch 71-124 */
1319 {4, 1}, /* group 3 5.2, ch 125-200 */
1320 {3, 1} /* group 4 2.4, ch all */
1323 static s32 get_min_power_index(s32 rate_power_index, u32 band)
1326 if ((rate_power_index & 7) <= 4)
1327 return MIN_TX_GAIN_INDEX_52GHZ_EXT;
1329 return MIN_TX_GAIN_INDEX;
1337 static const struct gain_entry gain_table[2][108] = {
1338 /* 5.2GHz power gain index table */
1340 {123, 0x3F}, /* highest txpower */
1449 /* 2.4GHz power gain index table */
1451 {110, 0x3f}, /* highest txpower */
1562 static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
1563 u8 is_fat, u8 ctrl_chan_high,
1564 struct iwl4965_tx_power_db *tx_power_tbl)
1566 u8 saturation_power;
1568 s32 user_target_power;
1572 s32 current_regulatory;
1573 s32 txatten_grp = CALIB_CH_GROUP_MAX;
1576 const struct iwl_channel_info *ch_info = NULL;
1577 struct iwl_eeprom_calib_ch_info ch_eeprom_info;
1578 const struct iwl_eeprom_calib_measure *measurement;
1581 s32 voltage_compensation;
1582 s32 degrees_per_05db_num;
1583 s32 degrees_per_05db_denom;
1585 s32 temperature_comp[2];
1586 s32 factory_gain_index[2];
1587 s32 factory_actual_pwr[2];
1590 /* Sanity check requested level (dBm) */
1591 if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
1592 IWL_WARNING("Requested user TXPOWER %d below limit.\n",
1593 priv->user_txpower_limit);
1596 if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
1597 IWL_WARNING("Requested user TXPOWER %d above limit.\n",
1598 priv->user_txpower_limit);
1602 /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
1603 * are used for indexing into txpower table) */
1604 user_target_power = 2 * priv->user_txpower_limit;
1606 /* Get current (RXON) channel, band, width */
1608 iwl4965_get_channel_txpower_info(priv, priv->band, channel);
1610 IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
1616 /* get txatten group, used to select 1) thermal txpower adjustment
1617 * and 2) mimo txpower balance between Tx chains. */
1618 txatten_grp = iwl4965_get_tx_atten_grp(channel);
1619 if (txatten_grp < 0)
1622 IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
1623 channel, txatten_grp);
1632 /* hardware txpower limits ...
1633 * saturation (clipping distortion) txpowers are in half-dBm */
1635 saturation_power = priv->calib_info->saturation_power24;
1637 saturation_power = priv->calib_info->saturation_power52;
1639 if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
1640 saturation_power > IWL_TX_POWER_SATURATION_MAX) {
1642 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
1644 saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
1647 /* regulatory txpower limits ... reg_limit values are in half-dBm,
1648 * max_power_avg values are in dBm, convert * 2 */
1650 reg_limit = ch_info->fat_max_power_avg * 2;
1652 reg_limit = ch_info->max_power_avg * 2;
1654 if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
1655 (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
1657 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
1659 reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
1662 /* Interpolate txpower calibration values for this channel,
1663 * based on factory calibration tests on spaced channels. */
1664 iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
1666 /* calculate tx gain adjustment based on power supply voltage */
1667 voltage = priv->calib_info->voltage;
1668 init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
1669 voltage_compensation =
1670 iwl4965_get_voltage_compensation(voltage, init_voltage);
1672 IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
1674 voltage, voltage_compensation);
1676 /* get current temperature (Celsius) */
1677 current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
1678 current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
1679 current_temp = KELVIN_TO_CELSIUS(current_temp);
1681 /* select thermal txpower adjustment params, based on channel group
1682 * (same frequency group used for mimo txatten adjustment) */
1683 degrees_per_05db_num =
1684 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
1685 degrees_per_05db_denom =
1686 tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
1688 /* get per-chain txpower values from factory measurements */
1689 for (c = 0; c < 2; c++) {
1690 measurement = &ch_eeprom_info.measurements[c][1];
1692 /* txgain adjustment (in half-dB steps) based on difference
1693 * between factory and current temperature */
1694 factory_temp = measurement->temperature;
1695 iwl4965_math_div_round((current_temp - factory_temp) *
1696 degrees_per_05db_denom,
1697 degrees_per_05db_num,
1698 &temperature_comp[c]);
1700 factory_gain_index[c] = measurement->gain_idx;
1701 factory_actual_pwr[c] = measurement->actual_pow;
1703 IWL_DEBUG_TXPOWER("chain = %d\n", c);
1704 IWL_DEBUG_TXPOWER("fctry tmp %d, "
1705 "curr tmp %d, comp %d steps\n",
1706 factory_temp, current_temp,
1707 temperature_comp[c]);
1709 IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
1710 factory_gain_index[c],
1711 factory_actual_pwr[c]);
1714 /* for each of 33 bit-rates (including 1 for CCK) */
1715 for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
1717 union iwl4965_tx_power_dual_stream tx_power;
1719 /* for mimo, reduce each chain's txpower by half
1720 * (3dB, 6 steps), so total output power is regulatory
1723 current_regulatory = reg_limit -
1724 IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
1727 current_regulatory = reg_limit;
1731 /* find txpower limit, either hardware or regulatory */
1732 power_limit = saturation_power - back_off_table[i];
1733 if (power_limit > current_regulatory)
1734 power_limit = current_regulatory;
1736 /* reduce user's txpower request if necessary
1737 * for this rate on this channel */
1738 target_power = user_target_power;
1739 if (target_power > power_limit)
1740 target_power = power_limit;
1742 IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
1743 i, saturation_power - back_off_table[i],
1744 current_regulatory, user_target_power,
1747 /* for each of 2 Tx chains (radio transmitters) */
1748 for (c = 0; c < 2; c++) {
1753 (s32)le32_to_cpu(priv->card_alive_init.
1754 tx_atten[txatten_grp][c]);
1758 /* calculate index; higher index means lower txpower */
1759 power_index = (u8) (factory_gain_index[c] -
1761 factory_actual_pwr[c]) -
1762 temperature_comp[c] -
1763 voltage_compensation +
1766 /* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
1769 if (power_index < get_min_power_index(i, band))
1770 power_index = get_min_power_index(i, band);
1772 /* adjust 5 GHz index to support negative indexes */
1776 /* CCK, rate 32, reduce txpower for CCK */
1777 if (i == POWER_TABLE_CCK_ENTRY)
1779 IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
1781 /* stay within the table! */
1782 if (power_index > 107) {
1783 IWL_WARNING("txpower index %d > 107\n",
1787 if (power_index < 0) {
1788 IWL_WARNING("txpower index %d < 0\n",
1793 /* fill txpower command for this rate/chain */
1794 tx_power.s.radio_tx_gain[c] =
1795 gain_table[band][power_index].radio;
1796 tx_power.s.dsp_predis_atten[c] =
1797 gain_table[band][power_index].dsp;
1799 IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
1800 "gain 0x%02x dsp %d\n",
1801 c, atten_value, power_index,
1802 tx_power.s.radio_tx_gain[c],
1803 tx_power.s.dsp_predis_atten[c]);
1804 }/* for each chain */
1806 tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
1808 }/* for each rate */
1814 * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
1816 * Uses the active RXON for channel, band, and characteristics (fat, high)
1817 * The power limit is taken from priv->user_txpower_limit.
1819 int iwl4965_hw_reg_send_txpower(struct iwl_priv *priv)
1821 struct iwl4965_txpowertable_cmd cmd = { 0 };
1825 u8 ctrl_chan_high = 0;
1827 if (test_bit(STATUS_SCANNING, &priv->status)) {
1828 /* If this gets hit a lot, switch it to a BUG() and catch
1829 * the stack trace to find out who is calling this during
1831 IWL_WARNING("TX Power requested while scanning!\n");
1835 band = priv->band == IEEE80211_BAND_2GHZ;
1837 is_fat = is_fat_channel(priv->active_rxon.flags);
1840 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
1844 cmd.channel = priv->active_rxon.channel;
1846 ret = iwl4965_fill_txpower_tbl(priv, band,
1847 le16_to_cpu(priv->active_rxon.channel),
1848 is_fat, ctrl_chan_high, &cmd.tx_power);
1852 ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
1858 static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
1861 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1862 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1863 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
1865 if ((rxon1->flags == rxon2->flags) &&
1866 (rxon1->filter_flags == rxon2->filter_flags) &&
1867 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1868 (rxon1->ofdm_ht_single_stream_basic_rates ==
1869 rxon2->ofdm_ht_single_stream_basic_rates) &&
1870 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1871 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1872 (rxon1->rx_chain == rxon2->rx_chain) &&
1873 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1874 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1878 rxon_assoc.flags = priv->staging_rxon.flags;
1879 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1880 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1881 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1882 rxon_assoc.reserved = 0;
1883 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1884 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1885 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1886 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1887 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1889 ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
1890 sizeof(rxon_assoc), &rxon_assoc, NULL);
1898 int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
1903 u8 ctrl_chan_high = 0;
1904 struct iwl4965_channel_switch_cmd cmd = { 0 };
1905 const struct iwl_channel_info *ch_info;
1907 band = priv->band == IEEE80211_BAND_2GHZ;
1909 ch_info = iwl_get_channel_info(priv, priv->band, channel);
1911 is_fat = is_fat_channel(priv->staging_rxon.flags);
1914 (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
1918 cmd.expect_beacon = 0;
1919 cmd.channel = cpu_to_le16(channel);
1920 cmd.rxon_flags = priv->active_rxon.flags;
1921 cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
1922 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
1924 cmd.expect_beacon = is_channel_radar(ch_info);
1926 cmd.expect_beacon = 1;
1928 rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
1929 ctrl_chan_high, &cmd.tx_power);
1931 IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
1935 rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
1939 #define RTS_HCCA_RETRY_LIMIT 3
1940 #define RTS_DFAULT_RETRY_LIMIT 60
1942 void iwl4965_hw_build_tx_cmd_rate(struct iwl_priv *priv,
1943 struct iwl_cmd *cmd,
1944 struct ieee80211_tx_control *ctrl,
1945 struct ieee80211_hdr *hdr, int sta_id,
1948 struct iwl4965_tx_cmd *tx = &cmd->cmd.tx;
1949 u8 rts_retry_limit = 0;
1950 u8 data_retry_limit = 0;
1951 u16 fc = le16_to_cpu(hdr->frame_control);
1954 int rate_idx = min(ctrl->tx_rate->hw_value & 0xffff, IWL_RATE_COUNT - 1);
1956 rate_plcp = iwl4965_rates[rate_idx].plcp;
1958 rts_retry_limit = (is_hcca) ?
1959 RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
1961 if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
1962 rate_flags |= RATE_MCS_CCK_MSK;
1965 if (ieee80211_is_probe_response(fc)) {
1966 data_retry_limit = 3;
1967 if (data_retry_limit < rts_retry_limit)
1968 rts_retry_limit = data_retry_limit;
1970 data_retry_limit = IWL_DEFAULT_TX_RETRY;
1972 if (priv->data_retry_limit != -1)
1973 data_retry_limit = priv->data_retry_limit;
1976 if (ieee80211_is_data(fc)) {
1977 tx->initial_rate_index = 0;
1978 tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1980 switch (fc & IEEE80211_FCTL_STYPE) {
1981 case IEEE80211_STYPE_AUTH:
1982 case IEEE80211_STYPE_DEAUTH:
1983 case IEEE80211_STYPE_ASSOC_REQ:
1984 case IEEE80211_STYPE_REASSOC_REQ:
1985 if (tx->tx_flags & TX_CMD_FLG_RTS_MSK) {
1986 tx->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1987 tx->tx_flags |= TX_CMD_FLG_CTS_MSK;
1994 /* Alternate between antenna A and B for successive frames */
1995 if (priv->use_ant_b_for_management_frame) {
1996 priv->use_ant_b_for_management_frame = 0;
1997 rate_flags |= RATE_MCS_ANT_B_MSK;
1999 priv->use_ant_b_for_management_frame = 1;
2000 rate_flags |= RATE_MCS_ANT_A_MSK;
2004 tx->rts_retry_limit = rts_retry_limit;
2005 tx->data_retry_limit = data_retry_limit;
2006 tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
2009 static int iwl4965_shared_mem_rx_idx(struct iwl_priv *priv)
2011 struct iwl4965_shared *s = priv->shared_virt;
2012 return le32_to_cpu(s->rb_closed) & 0xFFF;
2015 int iwl4965_hw_get_temperature(struct iwl_priv *priv)
2017 return priv->temperature;
2020 unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
2021 struct iwl4965_frame *frame, u8 rate)
2023 struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
2024 unsigned int frame_size;
2026 tx_beacon_cmd = &frame->u.beacon;
2027 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
2029 tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
2030 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2032 frame_size = iwl4965_fill_beacon_frame(priv,
2033 tx_beacon_cmd->frame,
2034 iwl4965_broadcast_addr,
2035 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
2037 BUG_ON(frame_size > MAX_MPDU_SIZE);
2038 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
2040 if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
2041 tx_beacon_cmd->tx.rate_n_flags =
2042 iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
2044 tx_beacon_cmd->tx.rate_n_flags =
2045 iwl4965_hw_set_rate_n_flags(rate, 0);
2047 tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
2048 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
2049 return (sizeof(*tx_beacon_cmd) + frame_size);
2052 int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
2053 dma_addr_t addr, u16 len)
2056 struct iwl_tfd_frame *tfd = ptr;
2057 u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
2059 /* Each TFD can point to a maximum 20 Tx buffers */
2060 if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
2061 IWL_ERROR("Error can not send more than %d chunks\n",
2066 index = num_tbs / 2;
2067 is_odd = num_tbs & 0x1;
2070 tfd->pa[index].tb1_addr = cpu_to_le32(addr);
2071 IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
2072 iwl_get_dma_hi_address(addr));
2073 IWL_SET_BITS(tfd->pa[index], tb1_len, len);
2075 IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
2076 (u32) (addr & 0xffff));
2077 IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
2078 IWL_SET_BITS(tfd->pa[index], tb2_len, len);
2081 IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
2086 static int iwl4965_alloc_shared_mem(struct iwl_priv *priv)
2088 priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
2089 sizeof(struct iwl4965_shared),
2090 &priv->shared_phys);
2091 if (!priv->shared_virt)
2094 memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
2096 priv->rb_closed_offset = offsetof(struct iwl4965_shared, rb_closed);
2101 static void iwl4965_free_shared_mem(struct iwl_priv *priv)
2103 if (priv->shared_virt)
2104 pci_free_consistent(priv->pci_dev,
2105 sizeof(struct iwl4965_shared),
2111 * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
2113 static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
2114 struct iwl4965_tx_queue *txq,
2118 int txq_id = txq->q.id;
2119 struct iwl4965_shared *shared_data = priv->shared_virt;
2121 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
2123 /* Set up byte count within first 256 entries */
2124 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
2125 tfd_offset[txq->q.write_ptr], byte_cnt, len);
2127 /* If within first 64 entries, duplicate at end */
2128 if (txq->q.write_ptr < IWL49_MAX_WIN_SIZE)
2129 IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
2130 tfd_offset[IWL49_QUEUE_SIZE + txq->q.write_ptr],
2135 * sign_extend - Sign extend a value using specified bit as sign-bit
2137 * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
2138 * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
2140 * @param oper value to sign extend
2141 * @param index 0 based bit index (0<=index<32) to sign bit
2143 static s32 sign_extend(u32 oper, int index)
2145 u8 shift = 31 - index;
2147 return (s32)(oper << shift) >> shift;
2151 * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
2152 * @statistics: Provides the temperature reading from the uCode
2154 * A return of <0 indicates bogus data in the statistics
2156 int iwl4965_get_temperature(const struct iwl_priv *priv)
2163 if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
2164 (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
2165 IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
2166 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
2167 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
2168 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
2169 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
2171 IWL_DEBUG_TEMP("Running temperature calibration\n");
2172 R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
2173 R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
2174 R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
2175 R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
2179 * Temperature is only 23 bits, so sign extend out to 32.
2181 * NOTE If we haven't received a statistics notification yet
2182 * with an updated temperature, use R4 provided to us in the
2183 * "initialize" ALIVE response.
2185 if (!test_bit(STATUS_TEMPERATURE, &priv->status))
2186 vt = sign_extend(R4, 23);
2189 le32_to_cpu(priv->statistics.general.temperature), 23);
2191 IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
2195 IWL_ERROR("Calibration conflict R1 == R3\n");
2199 /* Calculate temperature in degrees Kelvin, adjust by 97%.
2200 * Add offset to center the adjustment around 0 degrees Centigrade. */
2201 temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
2202 temperature /= (R3 - R1);
2203 temperature = (temperature * 97) / 100 +
2204 TEMPERATURE_CALIB_KELVIN_OFFSET;
2206 IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
2207 KELVIN_TO_CELSIUS(temperature));
2212 /* Adjust Txpower only if temperature variance is greater than threshold. */
2213 #define IWL_TEMPERATURE_THRESHOLD 3
2216 * iwl4965_is_temp_calib_needed - determines if new calibration is needed
2218 * If the temperature changed has changed sufficiently, then a recalibration
2221 * Assumes caller will replace priv->last_temperature once calibration
2224 static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
2228 if (!test_bit(STATUS_STATISTICS, &priv->status)) {
2229 IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
2233 temp_diff = priv->temperature - priv->last_temperature;
2235 /* get absolute value */
2236 if (temp_diff < 0) {
2237 IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
2238 temp_diff = -temp_diff;
2239 } else if (temp_diff == 0)
2240 IWL_DEBUG_POWER("Same temp, \n");
2242 IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
2244 if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
2245 IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
2249 IWL_DEBUG_POWER("Thermal txpower calib needed\n");
2254 /* Calculate noise level, based on measurements during network silence just
2255 * before arriving beacon. This measurement can be done only if we know
2256 * exactly when to expect beacons, therefore only when we're associated. */
2257 static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
2259 struct statistics_rx_non_phy *rx_info
2260 = &(priv->statistics.rx.general);
2261 int num_active_rx = 0;
2262 int total_silence = 0;
2264 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
2266 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
2268 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
2270 if (bcn_silence_a) {
2271 total_silence += bcn_silence_a;
2274 if (bcn_silence_b) {
2275 total_silence += bcn_silence_b;
2278 if (bcn_silence_c) {
2279 total_silence += bcn_silence_c;
2283 /* Average among active antennas */
2285 priv->last_rx_noise = (total_silence / num_active_rx) - 107;
2287 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2289 IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
2290 bcn_silence_a, bcn_silence_b, bcn_silence_c,
2291 priv->last_rx_noise);
2294 void iwl4965_hw_rx_statistics(struct iwl_priv *priv,
2295 struct iwl_rx_mem_buffer *rxb)
2297 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2301 IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
2302 (int)sizeof(priv->statistics), pkt->len);
2304 change = ((priv->statistics.general.temperature !=
2305 pkt->u.stats.general.temperature) ||
2306 ((priv->statistics.flag &
2307 STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
2308 (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
2310 memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
2312 set_bit(STATUS_STATISTICS, &priv->status);
2314 /* Reschedule the statistics timer to occur in
2315 * REG_RECALIB_PERIOD seconds to ensure we get a
2316 * thermal update even if the uCode doesn't give
2318 mod_timer(&priv->statistics_periodic, jiffies +
2319 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
2321 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2322 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
2323 iwl4965_rx_calc_noise(priv);
2324 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
2325 queue_work(priv->workqueue, &priv->sensitivity_work);
2329 iwl_leds_background(priv);
2331 /* If the hardware hasn't reported a change in
2332 * temperature then don't bother computing a
2333 * calibrated temperature value */
2337 temp = iwl4965_get_temperature(priv);
2341 if (priv->temperature != temp) {
2342 if (priv->temperature)
2343 IWL_DEBUG_TEMP("Temperature changed "
2344 "from %dC to %dC\n",
2345 KELVIN_TO_CELSIUS(priv->temperature),
2346 KELVIN_TO_CELSIUS(temp));
2348 IWL_DEBUG_TEMP("Temperature "
2349 "initialized to %dC\n",
2350 KELVIN_TO_CELSIUS(temp));
2353 priv->temperature = temp;
2354 set_bit(STATUS_TEMPERATURE, &priv->status);
2356 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
2357 iwl4965_is_temp_calib_needed(priv))
2358 queue_work(priv->workqueue, &priv->txpower_work);
2361 static void iwl4965_add_radiotap(struct iwl_priv *priv,
2362 struct sk_buff *skb,
2363 struct iwl4965_rx_phy_res *rx_start,
2364 struct ieee80211_rx_status *stats,
2367 s8 signal = stats->ssi;
2369 int rate = stats->rate_idx;
2370 u64 tsf = stats->mactime;
2372 __le16 phy_flags_hw = rx_start->phy_flags;
2373 struct iwl4965_rt_rx_hdr {
2374 struct ieee80211_radiotap_header rt_hdr;
2375 __le64 rt_tsf; /* TSF */
2376 u8 rt_flags; /* radiotap packet flags */
2377 u8 rt_rate; /* rate in 500kb/s */
2378 __le16 rt_channelMHz; /* channel in MHz */
2379 __le16 rt_chbitmask; /* channel bitfield */
2380 s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
2382 u8 rt_antenna; /* antenna number */
2383 } __attribute__ ((packed)) *iwl4965_rt;
2385 /* TODO: We won't have enough headroom for HT frames. Fix it later. */
2386 if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
2387 if (net_ratelimit())
2388 printk(KERN_ERR "not enough headroom [%d] for "
2389 "radiotap head [%zd]\n",
2390 skb_headroom(skb), sizeof(*iwl4965_rt));
2394 /* put radiotap header in front of 802.11 header and data */
2395 iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
2397 /* initialise radiotap header */
2398 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
2399 iwl4965_rt->rt_hdr.it_pad = 0;
2401 /* total header + data */
2402 put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
2403 &iwl4965_rt->rt_hdr.it_len);
2405 /* Indicate all the fields we add to the radiotap header */
2406 put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
2407 (1 << IEEE80211_RADIOTAP_FLAGS) |
2408 (1 << IEEE80211_RADIOTAP_RATE) |
2409 (1 << IEEE80211_RADIOTAP_CHANNEL) |
2410 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
2411 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
2412 (1 << IEEE80211_RADIOTAP_ANTENNA)),
2413 &iwl4965_rt->rt_hdr.it_present);
2415 /* Zero the flags, we'll add to them as we go */
2416 iwl4965_rt->rt_flags = 0;
2418 put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
2420 iwl4965_rt->rt_dbmsignal = signal;
2421 iwl4965_rt->rt_dbmnoise = noise;
2423 /* Convert the channel frequency and set the flags */
2424 put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
2425 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
2426 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2427 IEEE80211_CHAN_5GHZ),
2428 &iwl4965_rt->rt_chbitmask);
2429 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
2430 put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
2431 IEEE80211_CHAN_2GHZ),
2432 &iwl4965_rt->rt_chbitmask);
2434 put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
2435 IEEE80211_CHAN_2GHZ),
2436 &iwl4965_rt->rt_chbitmask);
2439 iwl4965_rt->rt_rate = 0;
2441 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
2446 * It seems that the antenna field in the phy flags value
2447 * is actually a bitfield. This is undefined by radiotap,
2448 * it wants an actual antenna number but I always get "7"
2449 * for most legacy frames I receive indicating that the
2450 * same frame was received on all three RX chains.
2452 * I think this field should be removed in favour of a
2453 * new 802.11n radiotap field "RX chains" that is defined
2456 antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
2457 iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
2459 /* set the preamble flag if appropriate */
2460 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
2461 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2463 stats->flag |= RX_FLAG_RADIOTAP;
2466 static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2468 /* 0 - mgmt, 1 - cnt, 2 - data */
2469 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2470 priv->rx_stats[idx].cnt++;
2471 priv->rx_stats[idx].bytes += len;
2475 * returns non-zero if packet should be dropped
2477 static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
2478 struct ieee80211_hdr *hdr,
2480 struct ieee80211_rx_status *stats)
2482 u16 fc = le16_to_cpu(hdr->frame_control);
2484 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2487 if (!(fc & IEEE80211_FCTL_PROTECTED))
2490 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2491 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2492 case RX_RES_STATUS_SEC_TYPE_TKIP:
2493 /* The uCode has got a bad phase 1 Key, pushes the packet.
2494 * Decryption will be done in SW. */
2495 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2496 RX_RES_STATUS_BAD_KEY_TTAK)
2499 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2500 RX_RES_STATUS_BAD_ICV_MIC) {
2501 /* bad ICV, the packet is destroyed since the
2502 * decryption is inplace, drop it */
2503 IWL_DEBUG_RX("Packet destroyed\n");
2506 case RX_RES_STATUS_SEC_TYPE_WEP:
2507 case RX_RES_STATUS_SEC_TYPE_CCMP:
2508 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2509 RX_RES_STATUS_DECRYPT_OK) {
2510 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2511 stats->flag |= RX_FLAG_DECRYPTED;
2521 static u32 iwl4965_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
2523 u32 decrypt_out = 0;
2525 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
2526 RX_RES_STATUS_STATION_FOUND)
2527 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
2528 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
2530 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
2532 /* packet was not encrypted */
2533 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2534 RX_RES_STATUS_SEC_TYPE_NONE)
2537 /* packet was encrypted with unknown alg */
2538 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
2539 RX_RES_STATUS_SEC_TYPE_ERR)
2542 /* decryption was not done in HW */
2543 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
2544 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
2547 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
2549 case RX_RES_STATUS_SEC_TYPE_CCMP:
2550 /* alg is CCM: check MIC only */
2551 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
2553 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2555 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2559 case RX_RES_STATUS_SEC_TYPE_TKIP:
2560 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
2562 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
2565 /* fall through if TTAK OK */
2567 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
2568 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
2570 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
2574 IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
2575 decrypt_in, decrypt_out);
2580 static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
2582 struct iwl_rx_mem_buffer *rxb,
2583 struct ieee80211_rx_status *stats)
2585 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2586 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
2587 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
2588 struct ieee80211_hdr *hdr;
2591 unsigned int skblen;
2593 u32 ampdu_status_legacy;
2595 if (!include_phy && priv->last_phy_res[0])
2596 rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
2599 IWL_ERROR("MPDU frame without a PHY data\n");
2603 hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
2604 rx_start->cfg_phy_cnt);
2606 len = le16_to_cpu(rx_start->byte_count);
2608 rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
2609 sizeof(struct iwl4965_rx_phy_res) +
2610 rx_start->cfg_phy_cnt + len);
2613 struct iwl4965_rx_mpdu_res_start *amsdu =
2614 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
2616 hdr = (struct ieee80211_hdr *)(pkt->u.raw +
2617 sizeof(struct iwl4965_rx_mpdu_res_start));
2618 len = le16_to_cpu(amsdu->byte_count);
2619 rx_start->byte_count = amsdu->byte_count;
2620 rx_end = (__le32 *) (((u8 *) hdr) + len);
2622 /* In monitor mode allow 802.11 ACk frames (10 bytes) */
2623 if (len > priv->hw_params.max_pkt_size ||
2624 len < ((priv->iw_mode == IEEE80211_IF_TYPE_MNTR) ? 10 : 16)) {
2625 IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
2629 ampdu_status = le32_to_cpu(*rx_end);
2630 skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
2633 /* New status scheme, need to translate */
2634 ampdu_status_legacy = ampdu_status;
2635 ampdu_status = iwl4965_translate_rx_status(priv, ampdu_status);
2638 /* start from MAC */
2639 skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
2640 skb_put(rxb->skb, len); /* end where data ends */
2642 /* We only process data packets if the interface is open */
2643 if (unlikely(!priv->is_open)) {
2644 IWL_DEBUG_DROP_LIMIT
2645 ("Dropping packet while interface is not open.\n");
2650 hdr = (struct ieee80211_hdr *)rxb->skb->data;
2652 /* in case of HW accelerated crypto and bad decryption, drop */
2653 if (!priv->hw_params.sw_crypto &&
2654 iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
2657 if (priv->add_radiotap)
2658 iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
2660 iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
2661 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
2662 priv->alloc_rxb_skb--;
2666 /* Calc max signal level (dBm) among 3 possible receivers */
2667 static int iwl4965_calc_rssi(struct iwl_priv *priv,
2668 struct iwl4965_rx_phy_res *rx_resp)
2670 /* data from PHY/DSP regarding signal strength, etc.,
2671 * contents are always there, not configurable by host. */
2672 struct iwl4965_rx_non_cfg_phy *ncphy =
2673 (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
2674 u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
2677 u32 valid_antennae =
2678 (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
2679 >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
2683 /* Find max rssi among 3 possible receivers.
2684 * These values are measured by the digital signal processor (DSP).
2685 * They should stay fairly constant even as the signal strength varies,
2686 * if the radio's automatic gain control (AGC) is working right.
2687 * AGC value (see below) will provide the "interesting" info. */
2688 for (i = 0; i < 3; i++)
2689 if (valid_antennae & (1 << i))
2690 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
2692 IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
2693 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
2696 /* dBm = max_rssi dB - agc dB - constant.
2697 * Higher AGC (higher radio gain) means lower signal. */
2698 return (max_rssi - agc - IWL_RSSI_OFFSET);
2701 static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
2703 unsigned long flags;
2705 spin_lock_irqsave(&priv->sta_lock, flags);
2706 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
2707 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
2708 priv->stations[sta_id].sta.sta.modify_mask = 0;
2709 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2710 spin_unlock_irqrestore(&priv->sta_lock, flags);
2712 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
2715 static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
2717 /* FIXME: need locking over ps_status ??? */
2718 u8 sta_id = iwl_find_station(priv, addr);
2720 if (sta_id != IWL_INVALID_STATION) {
2721 u8 sta_awake = priv->stations[sta_id].
2722 ps_status == STA_PS_STATUS_WAKE;
2724 if (sta_awake && ps_bit)
2725 priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
2726 else if (!sta_awake && !ps_bit) {
2727 iwl4965_sta_modify_ps_wake(priv, sta_id);
2728 priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
2732 #ifdef CONFIG_IWLWIFI_DEBUG
2735 * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
2737 * You may hack this function to show different aspects of received frames,
2738 * including selective frame dumps.
2739 * group100 parameter selects whether to show 1 out of 100 good frames.
2741 * TODO: This was originally written for 3945, need to audit for
2742 * proper operation with 4965.
2744 static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
2745 struct iwl_rx_packet *pkt,
2746 struct ieee80211_hdr *header, int group100)
2749 u32 print_summary = 0;
2750 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
2767 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
2768 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
2769 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
2770 u8 *data = IWL_RX_DATA(pkt);
2772 if (likely(!(priv->debug_level & IWL_DL_RX)))
2776 fc = le16_to_cpu(header->frame_control);
2777 seq_ctl = le16_to_cpu(header->seq_ctrl);
2780 channel = le16_to_cpu(rx_hdr->channel);
2781 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
2782 rate_sym = rx_hdr->rate;
2783 length = le16_to_cpu(rx_hdr->len);
2785 /* end-of-frame status and timestamp */
2786 status = le32_to_cpu(rx_end->status);
2787 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
2788 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
2789 tsf = le64_to_cpu(rx_end->timestamp);
2791 /* signal statistics */
2792 rssi = rx_stats->rssi;
2793 agc = rx_stats->agc;
2794 sig_avg = le16_to_cpu(rx_stats->sig_avg);
2795 noise_diff = le16_to_cpu(rx_stats->noise_diff);
2797 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
2799 /* if data frame is to us and all is good,
2800 * (optionally) print summary for only 1 out of every 100 */
2801 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
2802 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
2805 print_summary = 1; /* print each frame */
2806 else if (priv->framecnt_to_us < 100) {
2807 priv->framecnt_to_us++;
2810 priv->framecnt_to_us = 0;
2815 /* print summary for all other frames */
2819 if (print_summary) {
2825 title = "100Frames";
2826 else if (fc & IEEE80211_FCTL_RETRY)
2828 else if (ieee80211_is_assoc_response(fc))
2830 else if (ieee80211_is_reassoc_response(fc))
2832 else if (ieee80211_is_probe_response(fc)) {
2834 print_dump = 1; /* dump frame contents */
2835 } else if (ieee80211_is_beacon(fc)) {
2837 print_dump = 1; /* dump frame contents */
2838 } else if (ieee80211_is_atim(fc))
2840 else if (ieee80211_is_auth(fc))
2842 else if (ieee80211_is_deauth(fc))
2844 else if (ieee80211_is_disassoc(fc))
2849 rate_idx = iwl4965_hwrate_to_plcp_idx(rate_sym);
2850 if (unlikely(rate_idx == -1))
2853 bitrate = iwl4965_rates[rate_idx].ieee / 2;
2855 /* print frame summary.
2856 * MAC addresses show just the last byte (for brevity),
2857 * but you can hack it to show more, if you'd like to. */
2859 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
2860 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
2861 title, fc, header->addr1[5],
2862 length, rssi, channel, bitrate);
2864 /* src/dst addresses assume managed mode */
2865 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
2866 "src=0x%02x, rssi=%u, tim=%lu usec, "
2867 "phy=0x%02x, chnl=%d\n",
2868 title, fc, header->addr1[5],
2869 header->addr3[5], rssi,
2870 tsf_low - priv->scan_start_tsf,
2871 phy_flags, channel);
2875 iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
2878 static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
2879 struct iwl_rx_packet *pkt,
2880 struct ieee80211_hdr *header,
2888 /* Called for REPLY_RX (legacy ABG frames), or
2889 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
2890 static void iwl4965_rx_reply_rx(struct iwl_priv *priv,
2891 struct iwl_rx_mem_buffer *rxb)
2893 struct ieee80211_hdr *header;
2894 struct ieee80211_rx_status rx_status;
2895 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2896 /* Use phy data (Rx signal strength, etc.) contained within
2897 * this rx packet for legacy frames,
2898 * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
2899 int include_phy = (pkt->hdr.cmd == REPLY_RX);
2900 struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
2901 (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
2902 (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
2904 unsigned int len = 0;
2908 rx_status.mactime = le64_to_cpu(rx_start->timestamp);
2910 ieee80211_frequency_to_channel(le16_to_cpu(rx_start->channel));
2911 rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
2912 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
2913 rx_status.rate_idx =
2914 iwl4965_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
2915 if (rx_status.band == IEEE80211_BAND_5GHZ)
2916 rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
2918 rx_status.antenna = 0;
2921 if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
2922 IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
2923 rx_start->cfg_phy_cnt);
2928 if (priv->last_phy_res[0])
2929 rx_start = (struct iwl4965_rx_phy_res *)
2930 &priv->last_phy_res[1];
2936 IWL_ERROR("MPDU frame without a PHY data\n");
2941 header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
2942 + rx_start->cfg_phy_cnt);
2944 len = le16_to_cpu(rx_start->byte_count);
2945 rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
2946 sizeof(struct iwl4965_rx_phy_res) + len);
2948 struct iwl4965_rx_mpdu_res_start *amsdu =
2949 (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
2951 header = (void *)(pkt->u.raw +
2952 sizeof(struct iwl4965_rx_mpdu_res_start));
2953 len = le16_to_cpu(amsdu->byte_count);
2954 rx_end = (__le32 *) (pkt->u.raw +
2955 sizeof(struct iwl4965_rx_mpdu_res_start) + len);
2958 if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
2959 !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
2960 IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
2961 le32_to_cpu(*rx_end));
2965 priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
2967 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
2968 rx_status.ssi = iwl4965_calc_rssi(priv, rx_start);
2970 /* Meaningful noise values are available only from beacon statistics,
2971 * which are gathered only when associated, and indicate noise
2972 * only for the associated network channel ...
2973 * Ignore these noise values while scanning (other channels) */
2974 if (iwl_is_associated(priv) &&
2975 !test_bit(STATUS_SCANNING, &priv->status)) {
2976 rx_status.noise = priv->last_rx_noise;
2977 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi,
2980 rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2981 rx_status.signal = iwl4965_calc_sig_qual(rx_status.ssi, 0);
2984 /* Reset beacon noise level if not associated. */
2985 if (!iwl_is_associated(priv))
2986 priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
2988 /* Set "1" to report good data frames in groups of 100 */
2989 /* FIXME: need to optimze the call: */
2990 iwl4965_dbg_report_frame(priv, pkt, header, 1);
2992 IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
2993 rx_status.ssi, rx_status.noise, rx_status.signal,
2994 (unsigned long long)rx_status.mactime);
2997 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2998 iwl4965_handle_data_packet(priv, 1, include_phy,
3003 network_packet = iwl4965_is_network_packet(priv, header);
3004 if (network_packet) {
3005 priv->last_rx_rssi = rx_status.ssi;
3006 priv->last_beacon_time = priv->ucode_beacon_time;
3007 priv->last_tsf = le64_to_cpu(rx_start->timestamp);
3010 fc = le16_to_cpu(header->frame_control);
3011 switch (fc & IEEE80211_FCTL_FTYPE) {
3012 case IEEE80211_FTYPE_MGMT:
3013 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3014 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3016 iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
3019 case IEEE80211_FTYPE_CTL:
3020 #ifdef CONFIG_IWL4965_HT
3021 switch (fc & IEEE80211_FCTL_STYPE) {
3022 case IEEE80211_STYPE_BACK_REQ:
3023 IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
3024 iwl4965_handle_data_packet(priv, 0, include_phy,
3033 case IEEE80211_FTYPE_DATA: {
3034 DECLARE_MAC_BUF(mac1);
3035 DECLARE_MAC_BUF(mac2);
3036 DECLARE_MAC_BUF(mac3);
3038 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3039 iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
3042 if (unlikely(!network_packet))
3043 IWL_DEBUG_DROP("Dropping (non network): "
3045 print_mac(mac1, header->addr1),
3046 print_mac(mac2, header->addr2),
3047 print_mac(mac3, header->addr3));
3048 else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
3049 IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
3050 print_mac(mac1, header->addr1),
3051 print_mac(mac2, header->addr2),
3052 print_mac(mac3, header->addr3));
3054 iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
3064 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
3065 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
3066 static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
3067 struct iwl_rx_mem_buffer *rxb)
3069 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3070 priv->last_phy_res[0] = 1;
3071 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
3072 sizeof(struct iwl4965_rx_phy_res));
3074 static void iwl4965_rx_missed_beacon_notif(struct iwl_priv *priv,
3075 struct iwl_rx_mem_buffer *rxb)
3078 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3079 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3080 struct iwl4965_missed_beacon_notif *missed_beacon;
3082 missed_beacon = &pkt->u.missed_beacon;
3083 if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
3084 IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
3085 le32_to_cpu(missed_beacon->consequtive_missed_beacons),
3086 le32_to_cpu(missed_beacon->total_missed_becons),
3087 le32_to_cpu(missed_beacon->num_recvd_beacons),
3088 le32_to_cpu(missed_beacon->num_expected_beacons));
3089 if (!test_bit(STATUS_SCANNING, &priv->status))
3090 iwl_init_sensitivity(priv);
3092 #endif /*CONFIG_IWL4965_RUN_TIME_CALIB*/
3094 #ifdef CONFIG_IWL4965_HT
3097 * iwl4965_sta_modify_enable_tid_tx - Enable Tx for this TID in station table
3099 static void iwl4965_sta_modify_enable_tid_tx(struct iwl_priv *priv,
3100 int sta_id, int tid)
3102 unsigned long flags;
3104 /* Remove "disable" flag, to enable Tx for this TID */
3105 spin_lock_irqsave(&priv->sta_lock, flags);
3106 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3107 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3108 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3109 spin_unlock_irqrestore(&priv->sta_lock, flags);
3111 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3115 * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
3117 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
3118 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
3120 static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
3121 struct iwl_ht_agg *agg,
3122 struct iwl4965_compressed_ba_resp*
3127 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
3128 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3131 struct ieee80211_tx_status *tx_status;
3133 if (unlikely(!agg->wait_for_ba)) {
3134 IWL_ERROR("Received BA when not expected\n");
3138 /* Mark that the expected block-ack response arrived */
3139 agg->wait_for_ba = 0;
3140 IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
3142 /* Calculate shift to align block-ack bits with our Tx window bits */
3143 sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
3144 if (sh < 0) /* tbw something is wrong with indices */
3147 /* don't use 64-bit values for now */
3148 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
3150 if (agg->frame_count > (64 - sh)) {
3151 IWL_DEBUG_TX_REPLY("more frames than bitmap size");
3155 /* check for success or failure according to the
3156 * transmitted bitmap and block-ack bitmap */
3157 bitmap &= agg->bitmap;
3159 /* For each frame attempted in aggregation,
3160 * update driver's record of tx frame's status. */
3161 for (i = 0; i < agg->frame_count ; i++) {
3162 ack = bitmap & (1 << i);
3164 IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
3165 ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
3166 agg->start_idx + i);
3169 tx_status = &priv->txq[scd_flow].txb[agg->start_idx].status;
3170 tx_status->flags = IEEE80211_TX_STATUS_ACK;
3171 tx_status->flags |= IEEE80211_TX_STATUS_AMPDU;
3172 tx_status->ampdu_ack_map = successes;
3173 tx_status->ampdu_ack_len = agg->frame_count;
3174 iwl4965_hwrate_to_tx_control(priv, agg->rate_n_flags,
3175 &tx_status->control);
3177 IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
3183 * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
3185 static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
3188 /* Simply stop the queue, but don't change any configuration;
3189 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
3190 iwl_write_prph(priv,
3191 IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
3192 (0 << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
3193 (1 << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
3197 * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
3198 * priv->lock must be held by the caller
3200 static int iwl4965_tx_queue_agg_disable(struct iwl_priv *priv, u16 txq_id,
3201 u16 ssn_idx, u8 tx_fifo)
3205 if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
3206 IWL_WARNING("queue number too small: %d, must be > %d\n",
3207 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3211 ret = iwl_grab_nic_access(priv);
3215 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3217 iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3219 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3220 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3221 /* supposes that ssn_idx is valid (!= 0xFFF) */
3222 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3224 iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3225 iwl4965_txq_ctx_deactivate(priv, txq_id);
3226 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
3228 iwl_release_nic_access(priv);
3233 int iwl4965_check_empty_hw_queue(struct iwl_priv *priv, int sta_id,
3236 struct iwl4965_queue *q = &priv->txq[txq_id].q;
3237 u8 *addr = priv->stations[sta_id].sta.sta.addr;
3238 struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
3240 switch (priv->stations[sta_id].tid[tid].agg.state) {
3241 case IWL_EMPTYING_HW_QUEUE_DELBA:
3242 /* We are reclaiming the last packet of the */
3243 /* aggregated HW queue */
3244 if (txq_id == tid_data->agg.txq_id &&
3245 q->read_ptr == q->write_ptr) {
3246 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
3247 int tx_fifo = default_tid_to_tx_fifo[tid];
3248 IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
3249 iwl4965_tx_queue_agg_disable(priv, txq_id,
3251 tid_data->agg.state = IWL_AGG_OFF;
3252 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3255 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3256 /* We are reclaiming the last packet of the queue */
3257 if (tid_data->tfds_in_queue == 0) {
3258 IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
3259 tid_data->agg.state = IWL_AGG_ON;
3260 ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
3268 * iwl4965_queue_dec_wrap - Decrement queue index, wrap back to end if needed
3269 * @index -- current index
3270 * @n_bd -- total number of entries in queue (s/b power of 2)
3272 static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
3274 return (index == 0) ? n_bd - 1 : index - 1;
3278 * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
3280 * Handles block-acknowledge notification from device, which reports success
3281 * of frames sent via aggregation.
3283 static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
3284 struct iwl_rx_mem_buffer *rxb)
3286 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3287 struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
3289 struct iwl4965_tx_queue *txq = NULL;
3290 struct iwl_ht_agg *agg;
3291 DECLARE_MAC_BUF(mac);
3293 /* "flow" corresponds to Tx queue */
3294 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
3296 /* "ssn" is start of block-ack Tx window, corresponds to index
3297 * (in Tx queue's circular buffer) of first TFD/frame in window */
3298 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
3300 if (scd_flow >= priv->hw_params.max_txq_num) {
3301 IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
3305 txq = &priv->txq[scd_flow];
3306 agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
3308 /* Find index just before block-ack window */
3309 index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
3311 /* TODO: Need to get this copy more safely - now good for debug */
3313 IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
3316 print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
3318 IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
3319 "%d, scd_ssn = %d\n",
3322 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
3325 IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
3327 (unsigned long long)agg->bitmap);
3329 /* Update driver's record of ACK vs. not for each frame in window */
3330 iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
3332 /* Release all TFDs before the SSN, i.e. all TFDs in front of
3333 * block-ack window (we assume that they've been successfully
3334 * transmitted ... if not, it's too late anyway). */
3335 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
3336 /* calculate mac80211 ampdu sw queue to wake */
3338 scd_flow - IWL_BACK_QUEUE_FIRST_ID + priv->hw->queues;
3339 int freed = iwl4965_tx_queue_reclaim(priv, scd_flow, index);
3340 priv->stations[ba_resp->sta_id].
3341 tid[ba_resp->tid].tfds_in_queue -= freed;
3342 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3343 priv->mac80211_registered &&
3344 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3345 ieee80211_wake_queue(priv->hw, ampdu_q);
3346 iwl4965_check_empty_hw_queue(priv, ba_resp->sta_id,
3347 ba_resp->tid, scd_flow);
3352 * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
3354 static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
3361 scd_q2ratid = ra_tid & IWL49_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
3363 tbl_dw_addr = priv->scd_base_addr +
3364 IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
3366 tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
3369 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
3371 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
3373 iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
3380 * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
3382 * NOTE: txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID,
3383 * i.e. it must be one of the higher queues used for aggregation
3385 static int iwl4965_tx_queue_agg_enable(struct iwl_priv *priv, int txq_id,
3386 int tx_fifo, int sta_id, int tid,
3389 unsigned long flags;
3393 if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
3394 IWL_WARNING("queue number too small: %d, must be > %d\n",
3395 txq_id, IWL_BACK_QUEUE_FIRST_ID);
3397 ra_tid = BUILD_RAxTID(sta_id, tid);
3399 /* Modify device's station table to Tx this TID */
3400 iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
3402 spin_lock_irqsave(&priv->lock, flags);
3403 rc = iwl_grab_nic_access(priv);
3405 spin_unlock_irqrestore(&priv->lock, flags);
3409 /* Stop this Tx queue before configuring it */
3410 iwl4965_tx_queue_stop_scheduler(priv, txq_id);
3412 /* Map receiver-address / traffic-ID to this queue */
3413 iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
3415 /* Set this queue as a chain-building queue */
3416 iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
3418 /* Place first TFD at index corresponding to start sequence number.
3419 * Assumes that ssn_idx is valid (!= 0xFFF) */
3420 priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
3421 priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
3422 iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
3424 /* Set up Tx window size and frame limit for this queue */
3425 iwl_write_targ_mem(priv,
3426 priv->scd_base_addr + IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
3427 (SCD_WIN_SIZE << IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
3428 IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
3430 iwl_write_targ_mem(priv, priv->scd_base_addr +
3431 IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
3432 (SCD_FRAME_LIMIT << IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
3433 & IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
3435 iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
3437 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
3438 iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
3440 iwl_release_nic_access(priv);
3441 spin_unlock_irqrestore(&priv->lock, flags);
3446 #endif /* CONFIG_IWL4965_HT */
3449 * iwl4965_add_station - Initialize a station's hardware rate table
3451 * The uCode's station table contains a table of fallback rates
3452 * for automatic fallback during transmission.
3454 * NOTE: This sets up a default set of values. These will be replaced later
3455 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
3458 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
3459 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
3460 * which requires station table entry to exist).
3462 void iwl4965_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
3465 struct iwl_link_quality_cmd link_cmd = {
3470 /* Set up the rate scaling to start at selected rate, fall back
3471 * all the way down to 1M in IEEE order, and then spin on 1M */
3473 r = IWL_RATE_54M_INDEX;
3474 else if (priv->band == IEEE80211_BAND_5GHZ)
3475 r = IWL_RATE_6M_INDEX;
3477 r = IWL_RATE_1M_INDEX;
3479 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
3481 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
3482 rate_flags |= RATE_MCS_CCK_MSK;
3484 /* Use Tx antenna B only */
3485 rate_flags |= RATE_MCS_ANT_B_MSK; /*FIXME:RS*/
3487 link_cmd.rs_table[i].rate_n_flags =
3488 iwl4965_hw_set_rate_n_flags(iwl4965_rates[r].plcp, rate_flags);
3489 r = iwl4965_get_prev_ieee_rate(r);
3492 link_cmd.general_params.single_stream_ant_msk = 2;
3493 link_cmd.general_params.dual_stream_ant_msk = 3;
3494 link_cmd.agg_params.agg_dis_start_th = 3;
3495 link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
3497 /* Update the rate scaling for control frame Tx to AP */
3498 link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
3500 iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
3501 sizeof(link_cmd), &link_cmd, NULL);
3504 #ifdef CONFIG_IWL4965_HT
3506 void iwl4965_set_ht_add_station(struct iwl_priv *priv, u8 index,
3507 struct ieee80211_ht_info *sta_ht_inf)
3512 if (!sta_ht_inf || !sta_ht_inf->ht_supported)
3515 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2;
3517 sta_flags = priv->stations[index].sta.station_flags;
3519 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
3521 switch (mimo_ps_mode) {
3522 case WLAN_HT_CAP_MIMO_PS_STATIC:
3523 sta_flags |= STA_FLG_MIMO_DIS_MSK;
3525 case WLAN_HT_CAP_MIMO_PS_DYNAMIC:
3526 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
3528 case WLAN_HT_CAP_MIMO_PS_DISABLED:
3531 IWL_WARNING("Invalid MIMO PS mode %d", mimo_ps_mode);
3535 sta_flags |= cpu_to_le32(
3536 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
3538 sta_flags |= cpu_to_le32(
3539 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
3541 if (iwl_is_fat_tx_allowed(priv, sta_ht_inf))
3542 sta_flags |= STA_FLG_FAT_EN_MSK;
3544 sta_flags &= ~STA_FLG_FAT_EN_MSK;
3546 priv->stations[index].sta.station_flags = sta_flags;
3551 static int iwl4965_rx_agg_start(struct iwl_priv *priv,
3552 const u8 *addr, int tid, u16 ssn)
3554 unsigned long flags;
3557 sta_id = iwl_find_station(priv, addr);
3558 if (sta_id == IWL_INVALID_STATION)
3561 spin_lock_irqsave(&priv->sta_lock, flags);
3562 priv->stations[sta_id].sta.station_flags_msk = 0;
3563 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
3564 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
3565 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3566 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3567 spin_unlock_irqrestore(&priv->sta_lock, flags);
3569 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
3573 static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
3574 const u8 *addr, int tid)
3576 unsigned long flags;
3579 sta_id = iwl_find_station(priv, addr);
3580 if (sta_id == IWL_INVALID_STATION)
3583 spin_lock_irqsave(&priv->sta_lock, flags);
3584 priv->stations[sta_id].sta.station_flags_msk = 0;
3585 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
3586 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
3587 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3588 spin_unlock_irqrestore(&priv->sta_lock, flags);
3590 return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
3595 * Find first available (lowest unused) Tx Queue, mark it "active".
3596 * Called only when finding queue for aggregation.
3597 * Should never return anything < 7, because they should already
3598 * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
3600 static int iwl4965_txq_ctx_activate_free(struct iwl_priv *priv)
3604 for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
3605 if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
3610 static int iwl4965_tx_agg_start(struct ieee80211_hw *hw, const u8 *ra,
3611 u16 tid, u16 *start_seq_num)
3613 struct iwl_priv *priv = hw->priv;
3619 unsigned long flags;
3620 struct iwl_tid_data *tid_data;
3621 DECLARE_MAC_BUF(mac);
3623 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
3624 tx_fifo = default_tid_to_tx_fifo[tid];
3628 IWL_WARNING("%s on ra = %s tid = %d\n",
3629 __func__, print_mac(mac, ra), tid);
3631 sta_id = iwl_find_station(priv, ra);
3632 if (sta_id == IWL_INVALID_STATION)
3635 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
3636 IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
3640 txq_id = iwl4965_txq_ctx_activate_free(priv);
3644 spin_lock_irqsave(&priv->sta_lock, flags);
3645 tid_data = &priv->stations[sta_id].tid[tid];
3646 ssn = SEQ_TO_SN(tid_data->seq_number);
3647 tid_data->agg.txq_id = txq_id;
3648 spin_unlock_irqrestore(&priv->sta_lock, flags);
3650 *start_seq_num = ssn;
3651 ret = iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
3657 if (tid_data->tfds_in_queue == 0) {
3658 printk(KERN_ERR "HW queue is empty\n");
3659 tid_data->agg.state = IWL_AGG_ON;
3660 ieee80211_start_tx_ba_cb_irqsafe(hw, ra, tid);
3662 IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
3663 tid_data->tfds_in_queue);
3664 tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3669 static int iwl4965_tx_agg_stop(struct ieee80211_hw *hw, const u8 *ra, u16 tid)
3671 struct iwl_priv *priv = hw->priv;
3672 int tx_fifo_id, txq_id, sta_id, ssn = -1;
3673 struct iwl_tid_data *tid_data;
3674 int ret, write_ptr, read_ptr;
3675 unsigned long flags;
3676 DECLARE_MAC_BUF(mac);
3679 IWL_ERROR("ra = NULL\n");
3683 if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
3684 tx_fifo_id = default_tid_to_tx_fifo[tid];
3688 sta_id = iwl_find_station(priv, ra);
3690 if (sta_id == IWL_INVALID_STATION)
3693 if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
3694 IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
3696 tid_data = &priv->stations[sta_id].tid[tid];
3697 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
3698 txq_id = tid_data->agg.txq_id;
3699 write_ptr = priv->txq[txq_id].q.write_ptr;
3700 read_ptr = priv->txq[txq_id].q.read_ptr;
3702 /* The queue is not empty */
3703 if (write_ptr != read_ptr) {
3704 IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
3705 priv->stations[sta_id].tid[tid].agg.state =
3706 IWL_EMPTYING_HW_QUEUE_DELBA;
3710 IWL_DEBUG_HT("HW queue is empty\n");
3711 priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
3713 spin_lock_irqsave(&priv->lock, flags);
3714 ret = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
3715 spin_unlock_irqrestore(&priv->lock, flags);
3720 ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
3725 int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
3726 enum ieee80211_ampdu_mlme_action action,
3727 const u8 *addr, u16 tid, u16 *ssn)
3729 struct iwl_priv *priv = hw->priv;
3730 DECLARE_MAC_BUF(mac);
3732 IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
3733 print_mac(mac, addr), tid);
3736 case IEEE80211_AMPDU_RX_START:
3737 IWL_DEBUG_HT("start Rx\n");
3738 return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
3739 case IEEE80211_AMPDU_RX_STOP:
3740 IWL_DEBUG_HT("stop Rx\n");
3741 return iwl4965_rx_agg_stop(priv, addr, tid);
3742 case IEEE80211_AMPDU_TX_START:
3743 IWL_DEBUG_HT("start Tx\n");
3744 return iwl4965_tx_agg_start(hw, addr, tid, ssn);
3745 case IEEE80211_AMPDU_TX_STOP:
3746 IWL_DEBUG_HT("stop Tx\n");
3747 return iwl4965_tx_agg_stop(hw, addr, tid);
3749 IWL_DEBUG_HT("unknown\n");
3755 #endif /* CONFIG_IWL4965_HT */
3758 static u16 iwl4965_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
3760 struct iwl4965_addsta_cmd *addsta = (struct iwl4965_addsta_cmd *)data;
3761 addsta->mode = cmd->mode;
3762 memcpy(&addsta->sta, &cmd->sta, sizeof(struct sta_id_modify));
3763 memcpy(&addsta->key, &cmd->key, sizeof(struct iwl4965_keyinfo));
3764 addsta->station_flags = cmd->station_flags;
3765 addsta->station_flags_msk = cmd->station_flags_msk;
3766 addsta->tid_disable_tx = cmd->tid_disable_tx;
3767 addsta->add_immediate_ba_tid = cmd->add_immediate_ba_tid;
3768 addsta->remove_immediate_ba_tid = cmd->remove_immediate_ba_tid;
3769 addsta->add_immediate_ba_ssn = cmd->add_immediate_ba_ssn;
3770 addsta->reserved1 = __constant_cpu_to_le16(0);
3771 addsta->reserved2 = __constant_cpu_to_le32(0);
3773 return (u16)sizeof(struct iwl4965_addsta_cmd);
3775 /* Set up 4965-specific Rx frame reply handlers */
3776 static void iwl4965_rx_handler_setup(struct iwl_priv *priv)
3778 /* Legacy Rx frames */
3779 priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
3781 /* High-throughput (HT) Rx frames */
3782 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
3783 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
3785 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
3786 iwl4965_rx_missed_beacon_notif;
3788 #ifdef CONFIG_IWL4965_HT
3789 priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
3790 #endif /* CONFIG_IWL4965_HT */
3793 void iwl4965_hw_setup_deferred_work(struct iwl_priv *priv)
3795 INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
3796 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3797 INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
3799 init_timer(&priv->statistics_periodic);
3800 priv->statistics_periodic.data = (unsigned long)priv;
3801 priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
3804 void iwl4965_hw_cancel_deferred_work(struct iwl_priv *priv)
3806 del_timer_sync(&priv->statistics_periodic);
3808 cancel_delayed_work(&priv->init_alive_start);
3812 static struct iwl_hcmd_ops iwl4965_hcmd = {
3813 .rxon_assoc = iwl4965_send_rxon_assoc,
3816 static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
3817 .enqueue_hcmd = iwl4965_enqueue_hcmd,
3818 .build_addsta_hcmd = iwl4965_build_addsta_hcmd,
3819 #ifdef CONFIG_IWL4965_RUN_TIME_CALIB
3820 .chain_noise_reset = iwl4965_chain_noise_reset,
3821 .gain_computation = iwl4965_gain_computation,
3825 static struct iwl_lib_ops iwl4965_lib = {
3826 .set_hw_params = iwl4965_hw_set_hw_params,
3827 .alloc_shared_mem = iwl4965_alloc_shared_mem,
3828 .free_shared_mem = iwl4965_free_shared_mem,
3829 .shared_mem_rx_idx = iwl4965_shared_mem_rx_idx,
3830 .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
3831 .disable_tx_fifo = iwl4965_disable_tx_fifo,
3832 .rx_handler_setup = iwl4965_rx_handler_setup,
3833 .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
3834 .alive_notify = iwl4965_alive_notify,
3835 .init_alive_start = iwl4965_init_alive_start,
3836 .load_ucode = iwl4965_load_bsm,
3838 .init = iwl4965_apm_init,
3839 .config = iwl4965_nic_config,
3840 .set_pwr_src = iwl4965_set_pwr_src,
3843 .regulatory_bands = {
3844 EEPROM_REGULATORY_BAND_1_CHANNELS,
3845 EEPROM_REGULATORY_BAND_2_CHANNELS,
3846 EEPROM_REGULATORY_BAND_3_CHANNELS,
3847 EEPROM_REGULATORY_BAND_4_CHANNELS,
3848 EEPROM_REGULATORY_BAND_5_CHANNELS,
3849 EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
3850 EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
3852 .verify_signature = iwlcore_eeprom_verify_signature,
3853 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
3854 .release_semaphore = iwlcore_eeprom_release_semaphore,
3855 .check_version = iwl4965_eeprom_check_version,
3856 .query_addr = iwlcore_eeprom_query_addr,
3858 .radio_kill_sw = iwl4965_radio_kill_sw,
3859 .set_power = iwl4965_set_power,
3860 .update_chain_flags = iwl4965_update_chain_flags,
3863 static struct iwl_ops iwl4965_ops = {
3864 .lib = &iwl4965_lib,
3865 .hcmd = &iwl4965_hcmd,
3866 .utils = &iwl4965_hcmd_utils,
3869 struct iwl_cfg iwl4965_agn_cfg = {
3871 .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
3872 .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
3873 .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
3874 .ops = &iwl4965_ops,
3875 .mod_params = &iwl4965_mod_params,
3878 module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
3879 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
3880 module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
3881 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
3882 module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
3883 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
3884 module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
3885 MODULE_PARM_DESC(debug, "debug output mask");
3887 disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
3888 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
3890 module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
3891 MODULE_PARM_DESC(queues_num, "number of hw queues.");
3894 module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
3895 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
3896 module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
3897 MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");