1 /******************************************************************************
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *****************************************************************************/
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/pci.h>
36 #include <linux/pci-aspm.h>
37 #include <linux/slab.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/delay.h>
40 #include <linux/sched.h>
41 #include <linux/skbuff.h>
42 #include <linux/netdevice.h>
43 #include <linux/firmware.h>
44 #include <linux/etherdevice.h>
45 #include <linux/if_arp.h>
47 #include <net/mac80211.h>
49 #include <asm/div64.h>
51 #define DRV_NAME "iwl4965"
56 /******************************************************************************
60 ******************************************************************************/
63 * module name, copyright, version, etc.
65 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
67 #ifdef CONFIG_IWLEGACY_DEBUG
73 #define DRV_VERSION IWLWIFI_VERSION VD
75 MODULE_DESCRIPTION(DRV_DESCRIPTION);
76 MODULE_VERSION(DRV_VERSION);
77 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
78 MODULE_LICENSE("GPL");
79 MODULE_ALIAS("iwl4965");
82 il4965_check_abort_status(struct il_priv *il, u8 frame_count, u32 status)
84 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
85 IL_ERR("Tx flush command to flush out all frames\n");
86 if (!test_bit(S_EXIT_PENDING, &il->status))
87 queue_work(il->workqueue, &il->tx_flush);
94 struct il_mod_params il4965_mod_params = {
97 /* the rest are 0 by default */
101 il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
105 spin_lock_irqsave(&rxq->lock, flags);
106 INIT_LIST_HEAD(&rxq->rx_free);
107 INIT_LIST_HEAD(&rxq->rx_used);
108 /* Fill the rx_used queue with _all_ of the Rx buffers */
109 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
110 /* In the reset function, these buffers may have been allocated
111 * to an SKB, so we need to unmap and free potential storage */
112 if (rxq->pool[i].page != NULL) {
113 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
114 PAGE_SIZE << il->hw_params.rx_page_order,
116 __il_free_pages(il, rxq->pool[i].page);
117 rxq->pool[i].page = NULL;
119 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
122 for (i = 0; i < RX_QUEUE_SIZE; i++)
123 rxq->queue[i] = NULL;
125 /* Set us so that we have processed and used all buffers, but have
126 * not restocked the Rx queue with fresh buffers */
127 rxq->read = rxq->write = 0;
128 rxq->write_actual = 0;
130 spin_unlock_irqrestore(&rxq->lock, flags);
134 il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
137 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
140 if (il->cfg->mod_params->amsdu_size_8K)
141 rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
143 rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
146 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
148 /* Reset driver's Rx queue write idx */
149 il_wr(il, FH49_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
151 /* Tell device where to find RBD circular buffer in DRAM */
152 il_wr(il, FH49_RSCSR_CHNL0_RBDCB_BASE_REG, (u32) (rxq->bd_dma >> 8));
154 /* Tell device where in DRAM to update its Rx status */
155 il_wr(il, FH49_RSCSR_CHNL0_STTS_WPTR_REG, rxq->rb_stts_dma >> 4);
158 * Direct rx interrupts to hosts
159 * Rx buffer size 4 or 8k
163 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG,
164 FH49_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
165 FH49_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
166 FH49_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK | rb_size | (rb_timeout
168 FH49_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)
169 | (rfdnlog << FH49_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
171 /* Set interrupt coalescing timer to default (2048 usecs) */
172 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
178 il4965_set_pwr_vmain(struct il_priv *il)
181 * (for documentation purposes)
182 * to set power to V_AUX, do:
184 if (pci_pme_capable(il->pci_dev, PCI_D3cold))
185 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
186 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
187 ~APMG_PS_CTRL_MSK_PWR_SRC);
190 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
191 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
192 ~APMG_PS_CTRL_MSK_PWR_SRC);
196 il4965_hw_nic_init(struct il_priv *il)
199 struct il_rx_queue *rxq = &il->rxq;
203 spin_lock_irqsave(&il->lock, flags);
204 il->cfg->ops->lib->apm_ops.init(il);
206 /* Set interrupt coalescing calibration timer to default (512 usecs) */
207 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
209 spin_unlock_irqrestore(&il->lock, flags);
211 il4965_set_pwr_vmain(il);
213 il->cfg->ops->lib->apm_ops.config(il);
215 /* Allocate the RX queue, or reset if it is already allocated */
217 ret = il_rx_queue_alloc(il);
219 IL_ERR("Unable to initialize Rx queue\n");
223 il4965_rx_queue_reset(il, rxq);
225 il4965_rx_replenish(il);
227 il4965_rx_init(il, rxq);
229 spin_lock_irqsave(&il->lock, flags);
231 rxq->need_update = 1;
232 il_rx_queue_update_write_ptr(il, rxq);
234 spin_unlock_irqrestore(&il->lock, flags);
236 /* Allocate or reset and init all Tx and Command queues */
238 ret = il4965_txq_ctx_alloc(il);
242 il4965_txq_ctx_reset(il);
244 set_bit(S_INIT, &il->status);
250 * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
253 il4965_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
255 return cpu_to_le32((u32) (dma_addr >> 8));
259 * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
261 * If there are slots in the RX queue that need to be restocked,
262 * and we have free pre-allocated buffers, fill the ranks as much
263 * as we can, pulling from rx_free.
265 * This moves the 'write' idx forward to catch up with 'processed', and
266 * also updates the memory address in the firmware to reference the new
270 il4965_rx_queue_restock(struct il_priv *il)
272 struct il_rx_queue *rxq = &il->rxq;
273 struct list_head *element;
274 struct il_rx_buf *rxb;
277 spin_lock_irqsave(&rxq->lock, flags);
278 while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
279 /* The overwritten rxb must be a used one */
280 rxb = rxq->queue[rxq->write];
281 BUG_ON(rxb && rxb->page);
283 /* Get next free Rx buffer, remove from free list */
284 element = rxq->rx_free.next;
285 rxb = list_entry(element, struct il_rx_buf, list);
288 /* Point to Rx buffer via next RBD in circular buffer */
289 rxq->bd[rxq->write] =
290 il4965_dma_addr2rbd_ptr(il, rxb->page_dma);
291 rxq->queue[rxq->write] = rxb;
292 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
295 spin_unlock_irqrestore(&rxq->lock, flags);
296 /* If the pre-allocated buffer pool is dropping low, schedule to
298 if (rxq->free_count <= RX_LOW_WATERMARK)
299 queue_work(il->workqueue, &il->rx_replenish);
301 /* If we've added more space for the firmware to place data, tell it.
302 * Increment device's write pointer in multiples of 8. */
303 if (rxq->write_actual != (rxq->write & ~0x7)) {
304 spin_lock_irqsave(&rxq->lock, flags);
305 rxq->need_update = 1;
306 spin_unlock_irqrestore(&rxq->lock, flags);
307 il_rx_queue_update_write_ptr(il, rxq);
312 * il4965_rx_replenish - Move all used packet from rx_used to rx_free
314 * When moving to rx_free an SKB is allocated for the slot.
316 * Also restock the Rx queue via il_rx_queue_restock.
317 * This is called as a scheduled work item (except for during initialization)
320 il4965_rx_allocate(struct il_priv *il, gfp_t priority)
322 struct il_rx_queue *rxq = &il->rxq;
323 struct list_head *element;
324 struct il_rx_buf *rxb;
327 gfp_t gfp_mask = priority;
330 spin_lock_irqsave(&rxq->lock, flags);
331 if (list_empty(&rxq->rx_used)) {
332 spin_unlock_irqrestore(&rxq->lock, flags);
335 spin_unlock_irqrestore(&rxq->lock, flags);
337 if (rxq->free_count > RX_LOW_WATERMARK)
338 gfp_mask |= __GFP_NOWARN;
340 if (il->hw_params.rx_page_order > 0)
341 gfp_mask |= __GFP_COMP;
343 /* Alloc a new receive buffer */
344 page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
347 D_INFO("alloc_pages failed, " "order: %d\n",
348 il->hw_params.rx_page_order);
350 if (rxq->free_count <= RX_LOW_WATERMARK &&
352 IL_ERR("Failed to alloc_pages with %s. "
353 "Only %u free buffers remaining.\n",
355 GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
357 /* We don't reschedule replenish work here -- we will
358 * call the restock method and if it still needs
359 * more buffers it will schedule replenish */
363 spin_lock_irqsave(&rxq->lock, flags);
365 if (list_empty(&rxq->rx_used)) {
366 spin_unlock_irqrestore(&rxq->lock, flags);
367 __free_pages(page, il->hw_params.rx_page_order);
370 element = rxq->rx_used.next;
371 rxb = list_entry(element, struct il_rx_buf, list);
374 spin_unlock_irqrestore(&rxq->lock, flags);
378 /* Get physical address of the RB */
380 pci_map_page(il->pci_dev, page, 0,
381 PAGE_SIZE << il->hw_params.rx_page_order,
383 /* dma address must be no more than 36 bits */
384 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
385 /* and also 256 byte aligned! */
386 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
388 spin_lock_irqsave(&rxq->lock, flags);
390 list_add_tail(&rxb->list, &rxq->rx_free);
392 il->alloc_rxb_page++;
394 spin_unlock_irqrestore(&rxq->lock, flags);
399 il4965_rx_replenish(struct il_priv *il)
403 il4965_rx_allocate(il, GFP_KERNEL);
405 spin_lock_irqsave(&il->lock, flags);
406 il4965_rx_queue_restock(il);
407 spin_unlock_irqrestore(&il->lock, flags);
411 il4965_rx_replenish_now(struct il_priv *il)
413 il4965_rx_allocate(il, GFP_ATOMIC);
415 il4965_rx_queue_restock(il);
418 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
419 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
420 * This free routine walks the list of POOL entries and if SKB is set to
421 * non NULL it is unmapped and freed
424 il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
427 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
428 if (rxq->pool[i].page != NULL) {
429 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
430 PAGE_SIZE << il->hw_params.rx_page_order,
432 __il_free_pages(il, rxq->pool[i].page);
433 rxq->pool[i].page = NULL;
437 dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
439 dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
440 rxq->rb_stts, rxq->rb_stts_dma);
446 il4965_rxq_stop(struct il_priv *il)
450 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
451 il_poll_bit(il, FH49_MEM_RSSR_RX_STATUS_REG,
452 FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
458 il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
463 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
464 if (rate_n_flags & RATE_MCS_HT_MSK) {
465 idx = (rate_n_flags & 0xff);
467 /* Legacy rate format, search for match in table */
469 if (band == IEEE80211_BAND_5GHZ)
470 band_offset = IL_FIRST_OFDM_RATE;
471 for (idx = band_offset; idx < RATE_COUNT_LEGACY; idx++)
472 if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
473 return idx - band_offset;
480 il4965_calc_rssi(struct il_priv *il, struct il_rx_phy_res *rx_resp)
482 /* data from PHY/DSP regarding signal strength, etc.,
483 * contents are always there, not configurable by host. */
484 struct il4965_rx_non_cfg_phy *ncphy =
485 (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
487 (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK) >>
491 (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
492 >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
496 /* Find max rssi among 3 possible receivers.
497 * These values are measured by the digital signal processor (DSP).
498 * They should stay fairly constant even as the signal strength varies,
499 * if the radio's automatic gain control (AGC) is working right.
500 * AGC value (see below) will provide the "interesting" info. */
501 for (i = 0; i < 3; i++)
502 if (valid_antennae & (1 << i))
503 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
505 D_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
506 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
509 /* dBm = max_rssi dB - agc dB - constant.
510 * Higher AGC (higher radio gain) means lower signal. */
511 return max_rssi - agc - IL4965_RSSI_OFFSET;
515 il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
519 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
520 RX_RES_STATUS_STATION_FOUND)
522 (RX_RES_STATUS_STATION_FOUND |
523 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
525 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
527 /* packet was not encrypted */
528 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
529 RX_RES_STATUS_SEC_TYPE_NONE)
532 /* packet was encrypted with unknown alg */
533 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
534 RX_RES_STATUS_SEC_TYPE_ERR)
537 /* decryption was not done in HW */
538 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
539 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
542 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
544 case RX_RES_STATUS_SEC_TYPE_CCMP:
545 /* alg is CCM: check MIC only */
546 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
548 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
550 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
554 case RX_RES_STATUS_SEC_TYPE_TKIP:
555 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
557 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
560 /* fall through if TTAK OK */
562 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
563 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
565 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
569 D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n", decrypt_in, decrypt_out);
575 il4965_pass_packet_to_mac80211(struct il_priv *il, struct ieee80211_hdr *hdr,
576 u16 len, u32 ampdu_status, struct il_rx_buf *rxb,
577 struct ieee80211_rx_status *stats)
580 __le16 fc = hdr->frame_control;
582 /* We only process data packets if the interface is open */
583 if (unlikely(!il->is_open)) {
584 D_DROP("Dropping packet while interface is not open.\n");
588 /* In case of HW accelerated crypto and bad decryption, drop */
589 if (!il->cfg->mod_params->sw_crypto &&
590 il_set_decrypted_flag(il, hdr, ampdu_status, stats))
593 skb = dev_alloc_skb(128);
595 IL_ERR("dev_alloc_skb failed\n");
599 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
601 il_update_stats(il, false, fc, len);
602 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
604 ieee80211_rx(il->hw, skb);
605 il->alloc_rxb_page--;
609 /* Called for N_RX (legacy ABG frames), or
610 * N_RX_MPDU (HT high-throughput N frames). */
612 il4965_hdl_rx(struct il_priv *il, struct il_rx_buf *rxb)
614 struct ieee80211_hdr *header;
615 struct ieee80211_rx_status rx_status;
616 struct il_rx_pkt *pkt = rxb_addr(rxb);
617 struct il_rx_phy_res *phy_res;
618 __le32 rx_pkt_status;
619 struct il_rx_mpdu_res_start *amsdu;
625 * N_RX and N_RX_MPDU are handled differently.
626 * N_RX: physical layer info is in this buffer
627 * N_RX_MPDU: physical layer info was sent in separate
628 * command and cached in il->last_phy_res
630 * Here we set up local variables depending on which command is
633 if (pkt->hdr.cmd == N_RX) {
634 phy_res = (struct il_rx_phy_res *)pkt->u.raw;
636 (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res) +
637 phy_res->cfg_phy_cnt);
639 len = le16_to_cpu(phy_res->byte_count);
641 *(__le32 *) (pkt->u.raw + sizeof(*phy_res) +
642 phy_res->cfg_phy_cnt + len);
643 ampdu_status = le32_to_cpu(rx_pkt_status);
645 if (!il->_4965.last_phy_res_valid) {
646 IL_ERR("MPDU frame without cached PHY data\n");
649 phy_res = &il->_4965.last_phy_res;
650 amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
651 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
652 len = le16_to_cpu(amsdu->byte_count);
653 rx_pkt_status = *(__le32 *) (pkt->u.raw + sizeof(*amsdu) + len);
655 il4965_translate_rx_status(il, le32_to_cpu(rx_pkt_status));
658 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
659 D_DROP("dsp size out of range [0,20]: %d/n",
660 phy_res->cfg_phy_cnt);
664 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
665 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
666 D_RX("Bad CRC or FIFO: 0x%08X.\n", le32_to_cpu(rx_pkt_status));
670 /* This will be used in several places later */
671 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
673 /* rx_status carries information about the packet to mac80211 */
674 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
677 phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ? IEEE80211_BAND_2GHZ :
680 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
683 il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
686 /* TSF isn't reliable. In order to allow smooth user experience,
687 * this W/A doesn't propagate it to the mac80211 */
688 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU; */
690 il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
692 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
693 rx_status.signal = il4965_calc_rssi(il, phy_res);
695 il_dbg_log_rx_data_frame(il, len, header);
696 D_STATS("Rssi %d, TSF %llu\n", rx_status.signal,
697 (unsigned long long)rx_status.mactime);
702 * It seems that the antenna field in the phy flags value
703 * is actually a bit field. This is undefined by radiotap,
704 * it wants an actual antenna number but I always get "7"
705 * for most legacy frames I receive indicating that the
706 * same frame was received on all three RX chains.
708 * I think this field should be removed in favor of a
709 * new 802.11n radiotap field "RX chains" that is defined
713 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK) >>
714 RX_RES_PHY_FLAGS_ANTENNA_POS;
716 /* set the preamble flag if appropriate */
717 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
718 rx_status.flag |= RX_FLAG_SHORTPRE;
720 /* Set up the HT phy flags */
721 if (rate_n_flags & RATE_MCS_HT_MSK)
722 rx_status.flag |= RX_FLAG_HT;
723 if (rate_n_flags & RATE_MCS_HT40_MSK)
724 rx_status.flag |= RX_FLAG_40MHZ;
725 if (rate_n_flags & RATE_MCS_SGI_MSK)
726 rx_status.flag |= RX_FLAG_SHORT_GI;
728 il4965_pass_packet_to_mac80211(il, header, len, ampdu_status, rxb,
732 /* Cache phy data (Rx signal strength, etc) for HT frame (N_RX_PHY).
733 * This will be used later in il_hdl_rx() for N_RX_MPDU. */
735 il4965_hdl_rx_phy(struct il_priv *il, struct il_rx_buf *rxb)
737 struct il_rx_pkt *pkt = rxb_addr(rxb);
738 il->_4965.last_phy_res_valid = true;
739 memcpy(&il->_4965.last_phy_res, pkt->u.raw,
740 sizeof(struct il_rx_phy_res));
744 il4965_get_channels_for_scan(struct il_priv *il, struct ieee80211_vif *vif,
745 enum ieee80211_band band, u8 is_active,
746 u8 n_probes, struct il_scan_channel *scan_ch)
748 struct ieee80211_channel *chan;
749 const struct ieee80211_supported_band *sband;
750 const struct il_channel_info *ch_info;
751 u16 passive_dwell = 0;
752 u16 active_dwell = 0;
756 sband = il_get_hw_mode(il, band);
760 active_dwell = il_get_active_dwell_time(il, band, n_probes);
761 passive_dwell = il_get_passive_dwell_time(il, band, vif);
763 if (passive_dwell <= active_dwell)
764 passive_dwell = active_dwell + 1;
766 for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
767 chan = il->scan_request->channels[i];
769 if (chan->band != band)
772 channel = chan->hw_value;
773 scan_ch->channel = cpu_to_le16(channel);
775 ch_info = il_get_channel_info(il, band, channel);
776 if (!il_is_channel_valid(ch_info)) {
777 D_SCAN("Channel %d is INVALID for this band.\n",
782 if (!is_active || il_is_channel_passive(ch_info) ||
783 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
784 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
786 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
789 scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
791 scan_ch->active_dwell = cpu_to_le16(active_dwell);
792 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
794 /* Set txpower levels to defaults */
795 scan_ch->dsp_atten = 110;
797 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
799 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
801 if (band == IEEE80211_BAND_5GHZ)
802 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
804 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
806 D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", channel,
807 le32_to_cpu(scan_ch->type),
809 type & SCAN_CHANNEL_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
811 type & SCAN_CHANNEL_TYPE_ACTIVE) ? active_dwell :
818 D_SCAN("total channels to scan %d\n", added);
823 il4965_ant_idx_to_flags(u8 ant_idx)
825 return BIT(ant_idx) << RATE_MCS_ANT_POS;
829 il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
831 struct il_host_cmd cmd = {
833 .len = sizeof(struct il_scan_cmd),
834 .flags = CMD_SIZE_HUGE,
836 struct il_scan_cmd *scan;
837 struct il_rxon_context *ctx = &il->ctx;
841 enum ieee80211_band band;
843 u8 rx_ant = il->hw_params.valid_rx_ant;
845 bool is_active = false;
848 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
851 lockdep_assert_held(&il->mutex);
854 ctx = il_rxon_ctx_from_vif(vif);
858 kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
861 D_SCAN("fail to allocate memory for scan\n");
866 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
868 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
869 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
871 if (il_is_any_associated(il)) {
874 u32 suspend_time = 100;
875 u32 scan_suspend_time = 100;
877 D_INFO("Scanning while associated...\n");
878 interval = vif->bss_conf.beacon_int;
880 scan->suspend_time = 0;
881 scan->max_out_time = cpu_to_le32(200 * 1024);
883 interval = suspend_time;
885 extra = (suspend_time / interval) << 22;
887 (extra | ((suspend_time % interval) * 1024));
888 scan->suspend_time = cpu_to_le32(scan_suspend_time);
889 D_SCAN("suspend_time 0x%X beacon interval %d\n",
890 scan_suspend_time, interval);
893 if (il->scan_request->n_ssids) {
895 D_SCAN("Kicking off active scan\n");
896 for (i = 0; i < il->scan_request->n_ssids; i++) {
897 /* always does wildcard anyway */
898 if (!il->scan_request->ssids[i].ssid_len)
900 scan->direct_scan[p].id = WLAN_EID_SSID;
901 scan->direct_scan[p].len =
902 il->scan_request->ssids[i].ssid_len;
903 memcpy(scan->direct_scan[p].ssid,
904 il->scan_request->ssids[i].ssid,
905 il->scan_request->ssids[i].ssid_len);
911 D_SCAN("Start passive scan.\n");
913 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
914 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
915 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
917 switch (il->scan_band) {
918 case IEEE80211_BAND_2GHZ:
919 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
921 le32_to_cpu(il->ctx.active.
922 flags & RXON_FLG_CHANNEL_MODE_MSK) >>
923 RXON_FLG_CHANNEL_MODE_POS;
924 if (chan_mod == CHANNEL_MODE_PURE_40) {
928 rate_flags = RATE_MCS_CCK_MSK;
931 case IEEE80211_BAND_5GHZ:
935 IL_WARN("Invalid scan band\n");
940 * If active scanning is requested but a certain channel is
941 * marked passive, we can do active scanning if we detect
944 * There is an issue with some firmware versions that triggers
945 * a sysassert on a "good CRC threshold" of zero (== disabled),
946 * on a radar channel even though this means that we should NOT
949 * The "good CRC threshold" is the number of frames that we
950 * need to receive during our dwell time on a channel before
951 * sending out probes -- setting this to a huge value will
952 * mean we never reach it, but at the same time work around
953 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
954 * here instead of IL_GOOD_CRC_TH_DISABLED.
957 is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
959 band = il->scan_band;
961 if (il->cfg->scan_rx_antennas[band])
962 rx_ant = il->cfg->scan_rx_antennas[band];
964 il->scan_tx_ant[band] =
965 il4965_toggle_tx_ant(il, il->scan_tx_ant[band], scan_tx_antennas);
966 rate_flags |= il4965_ant_idx_to_flags(il->scan_tx_ant[band]);
967 scan->tx_cmd.rate_n_flags =
968 il4965_hw_set_rate_n_flags(rate, rate_flags);
970 /* In power save mode use one chain, otherwise use all chains */
971 if (test_bit(S_POWER_PMI, &il->status)) {
972 /* rx_ant has been set to all valid chains previously */
974 rx_ant & ((u8) (il->chain_noise_data.active_chains));
976 active_chains = rx_ant;
978 D_SCAN("chain_noise_data.active_chains: %u\n",
979 il->chain_noise_data.active_chains);
981 rx_ant = il4965_first_antenna(active_chains);
984 /* MIMO is not used here, but value is required */
985 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
986 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
987 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
988 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
989 scan->rx_chain = cpu_to_le16(rx_chain);
992 il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
993 vif->addr, il->scan_request->ie,
994 il->scan_request->ie_len,
995 IL_MAX_SCAN_SIZE - sizeof(*scan));
996 scan->tx_cmd.len = cpu_to_le16(cmd_len);
998 scan->filter_flags |=
999 (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
1001 scan->channel_count =
1002 il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
1003 (void *)&scan->data[cmd_len]);
1004 if (scan->channel_count == 0) {
1005 D_SCAN("channel count %d\n", scan->channel_count);
1010 le16_to_cpu(scan->tx_cmd.len) +
1011 scan->channel_count * sizeof(struct il_scan_channel);
1013 scan->len = cpu_to_le16(cmd.len);
1015 set_bit(S_SCAN_HW, &il->status);
1017 ret = il_send_cmd_sync(il, &cmd);
1019 clear_bit(S_SCAN_HW, &il->status);
1025 il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
1028 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1031 return il4965_add_bssid_station(il, vif_priv->ctx,
1032 vif->bss_conf.bssid,
1033 &vif_priv->ibss_bssid_sta_id);
1034 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
1035 vif->bss_conf.bssid);
1039 il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
1041 lockdep_assert_held(&il->sta_lock);
1043 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1044 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1046 D_TX("free more than tfds_in_queue (%u:%d)\n",
1047 il->stations[sta_id].tid[tid].tfds_in_queue, freed);
1048 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1052 #define IL_TX_QUEUE_MSK 0xfffff
1055 il4965_is_single_rx_stream(struct il_priv *il)
1057 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
1058 il->current_ht_config.single_chain_sufficient;
1061 #define IL_NUM_RX_CHAINS_MULTIPLE 3
1062 #define IL_NUM_RX_CHAINS_SINGLE 2
1063 #define IL_NUM_IDLE_CHAINS_DUAL 2
1064 #define IL_NUM_IDLE_CHAINS_SINGLE 1
1067 * Determine how many receiver/antenna chains to use.
1069 * More provides better reception via diversity. Fewer saves power
1070 * at the expense of throughput, but only when not in powersave to
1073 * MIMO (dual stream) requires at least 2, but works better with 3.
1074 * This does not determine *which* chains to use, just how many.
1077 il4965_get_active_rx_chain_count(struct il_priv *il)
1079 /* # of Rx chains to use when expecting MIMO. */
1080 if (il4965_is_single_rx_stream(il))
1081 return IL_NUM_RX_CHAINS_SINGLE;
1083 return IL_NUM_RX_CHAINS_MULTIPLE;
1087 * When we are in power saving mode, unless device support spatial
1088 * multiplexing power save, use the active count for rx chain count.
1091 il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1093 /* # Rx chains when idling, depending on SMPS mode */
1094 switch (il->current_ht_config.smps) {
1095 case IEEE80211_SMPS_STATIC:
1096 case IEEE80211_SMPS_DYNAMIC:
1097 return IL_NUM_IDLE_CHAINS_SINGLE;
1098 case IEEE80211_SMPS_OFF:
1101 WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
1106 /* up to 4 chains */
1108 il4965_count_chain_bitmap(u32 chain_bitmap)
1111 res = (chain_bitmap & BIT(0)) >> 0;
1112 res += (chain_bitmap & BIT(1)) >> 1;
1113 res += (chain_bitmap & BIT(2)) >> 2;
1114 res += (chain_bitmap & BIT(3)) >> 3;
1119 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1121 * Selects how many and which Rx receivers/antennas/chains to use.
1122 * This should not be used for scan command ... it puts data in wrong place.
1125 il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
1127 bool is_single = il4965_is_single_rx_stream(il);
1128 bool is_cam = !test_bit(S_POWER_PMI, &il->status);
1129 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1133 /* Tell uCode which antennas are actually connected.
1134 * Before first association, we assume all antennas are connected.
1135 * Just after first association, il4965_chain_noise_calibration()
1136 * checks which antennas actually *are* connected. */
1137 if (il->chain_noise_data.active_chains)
1138 active_chains = il->chain_noise_data.active_chains;
1140 active_chains = il->hw_params.valid_rx_ant;
1142 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1144 /* How many receivers should we use? */
1145 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1146 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1148 /* correct rx chain count according hw settings
1149 * and chain noise calibration
1151 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1152 if (valid_rx_cnt < active_rx_cnt)
1153 active_rx_cnt = valid_rx_cnt;
1155 if (valid_rx_cnt < idle_rx_cnt)
1156 idle_rx_cnt = valid_rx_cnt;
1158 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
1159 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
1161 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1163 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
1164 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1166 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1168 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", ctx->staging.rx_chain,
1169 active_rx_cnt, idle_rx_cnt);
1171 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1172 active_rx_cnt < idle_rx_cnt);
1176 il4965_toggle_tx_ant(struct il_priv *il, u8 ant, u8 valid)
1181 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
1182 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
1183 if (valid & BIT(ind))
1190 il4965_get_fh_string(int cmd)
1193 IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
1194 IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
1195 IL_CMD(FH49_RSCSR_CHNL0_WPTR);
1196 IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
1197 IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
1198 IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
1199 IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
1200 IL_CMD(FH49_TSSR_TX_STATUS_REG);
1201 IL_CMD(FH49_TSSR_TX_ERROR_REG);
1208 il4965_dump_fh(struct il_priv *il, char **buf, bool display)
1211 #ifdef CONFIG_IWLEGACY_DEBUG
1215 static const u32 fh_tbl[] = {
1216 FH49_RSCSR_CHNL0_STTS_WPTR_REG,
1217 FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
1218 FH49_RSCSR_CHNL0_WPTR,
1219 FH49_MEM_RCSR_CHNL0_CONFIG_REG,
1220 FH49_MEM_RSSR_SHARED_CTRL_REG,
1221 FH49_MEM_RSSR_RX_STATUS_REG,
1222 FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
1223 FH49_TSSR_TX_STATUS_REG,
1224 FH49_TSSR_TX_ERROR_REG
1226 #ifdef CONFIG_IWLEGACY_DEBUG
1228 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
1229 *buf = kmalloc(bufsz, GFP_KERNEL);
1233 scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
1234 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1236 scnprintf(*buf + pos, bufsz - pos,
1238 il4965_get_fh_string(fh_tbl[i]), il_rd(il,
1245 IL_ERR("FH register values:\n");
1246 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1247 IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
1248 il_rd(il, fh_tbl[i]));
1254 il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
1256 struct il_rx_pkt *pkt = rxb_addr(rxb);
1257 struct il_missed_beacon_notif *missed_beacon;
1259 missed_beacon = &pkt->u.missed_beacon;
1260 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
1261 il->missed_beacon_threshold) {
1262 D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
1263 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
1264 le32_to_cpu(missed_beacon->total_missed_becons),
1265 le32_to_cpu(missed_beacon->num_recvd_beacons),
1266 le32_to_cpu(missed_beacon->num_expected_beacons));
1267 if (!test_bit(S_SCANNING, &il->status))
1268 il4965_init_sensitivity(il);
1272 /* Calculate noise level, based on measurements during network silence just
1273 * before arriving beacon. This measurement can be done only if we know
1274 * exactly when to expect beacons, therefore only when we're associated. */
1276 il4965_rx_calc_noise(struct il_priv *il)
1278 struct stats_rx_non_phy *rx_info;
1279 int num_active_rx = 0;
1280 int total_silence = 0;
1281 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
1284 rx_info = &(il->_4965.stats.rx.general);
1286 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
1288 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
1290 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
1292 if (bcn_silence_a) {
1293 total_silence += bcn_silence_a;
1296 if (bcn_silence_b) {
1297 total_silence += bcn_silence_b;
1300 if (bcn_silence_c) {
1301 total_silence += bcn_silence_c;
1305 /* Average among active antennas */
1307 last_rx_noise = (total_silence / num_active_rx) - 107;
1309 last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
1311 D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
1312 bcn_silence_b, bcn_silence_c, last_rx_noise);
1315 #ifdef CONFIG_IWLEGACY_DEBUGFS
1317 * based on the assumption of all stats counter are in DWORD
1318 * FIXME: This function is for debugging, do not deal with
1319 * the case of counters roll-over.
1322 il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
1327 u32 *delta, *max_delta;
1328 struct stats_general_common *general, *accum_general;
1329 struct stats_tx *tx, *accum_tx;
1331 prev_stats = (__le32 *) & il->_4965.stats;
1332 accum_stats = (u32 *) & il->_4965.accum_stats;
1333 size = sizeof(struct il_notif_stats);
1334 general = &il->_4965.stats.general.common;
1335 accum_general = &il->_4965.accum_stats.general.common;
1336 tx = &il->_4965.stats.tx;
1337 accum_tx = &il->_4965.accum_stats.tx;
1338 delta = (u32 *) & il->_4965.delta_stats;
1339 max_delta = (u32 *) & il->_4965.max_delta;
1341 for (i = sizeof(__le32); i < size;
1343 sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
1345 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
1347 (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
1348 *accum_stats += *delta;
1349 if (*delta > *max_delta)
1350 *max_delta = *delta;
1354 /* reset accumulative stats for "no-counter" type stats */
1355 accum_general->temperature = general->temperature;
1356 accum_general->ttl_timestamp = general->ttl_timestamp;
1360 #define REG_RECALIB_PERIOD (60)
1363 il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
1366 struct il_rx_pkt *pkt = rxb_addr(rxb);
1368 D_RX("Statistics notification received (%d vs %d).\n",
1369 (int)sizeof(struct il_notif_stats),
1370 le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
1373 ((il->_4965.stats.general.common.temperature !=
1374 pkt->u.stats.general.common.temperature) ||
1375 ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
1376 (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
1377 #ifdef CONFIG_IWLEGACY_DEBUGFS
1378 il4965_accumulative_stats(il, (__le32 *) & pkt->u.stats);
1381 /* TODO: reading some of stats is unneeded */
1382 memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
1384 set_bit(S_STATS, &il->status);
1386 /* Reschedule the stats timer to occur in
1387 * REG_RECALIB_PERIOD seconds to ensure we get a
1388 * thermal update even if the uCode doesn't give
1390 mod_timer(&il->stats_periodic,
1391 jiffies + msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
1393 if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
1394 (pkt->hdr.cmd == N_STATS)) {
1395 il4965_rx_calc_noise(il);
1396 queue_work(il->workqueue, &il->run_time_calib_work);
1398 if (il->cfg->ops->lib->temp_ops.temperature && change)
1399 il->cfg->ops->lib->temp_ops.temperature(il);
1403 il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
1405 struct il_rx_pkt *pkt = rxb_addr(rxb);
1407 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
1408 #ifdef CONFIG_IWLEGACY_DEBUGFS
1409 memset(&il->_4965.accum_stats, 0,
1410 sizeof(struct il_notif_stats));
1411 memset(&il->_4965.delta_stats, 0,
1412 sizeof(struct il_notif_stats));
1413 memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
1415 D_RX("Statistics have been cleared\n");
1417 il4965_hdl_stats(il, rxb);
1422 * mac80211 queues, ACs, hardware queues, FIFOs.
1424 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
1426 * Mac80211 uses the following numbers, which we get as from it
1427 * by way of skb_get_queue_mapping(skb):
1435 * Regular (not A-MPDU) frames are put into hardware queues corresponding
1436 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
1437 * own queue per aggregation session (RA/TID combination), such queues are
1438 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
1439 * order to map frames to the right queue, we also need an AC->hw queue
1440 * mapping. This is implemented here.
1442 * Due to the way hw queues are set up (by the hw specific modules like
1443 * 4965.c), the AC->hw queue mapping is the identity
1447 static const u8 tid_to_ac[] = {
1459 il4965_get_ac_from_tid(u16 tid)
1461 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1462 return tid_to_ac[tid];
1464 /* no support for TIDs 8-15 yet */
1469 il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
1471 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1472 return ctx->ac_to_fifo[tid_to_ac[tid]];
1474 /* no support for TIDs 8-15 yet */
1479 * handle build C_TX command notification.
1482 il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
1483 struct il_tx_cmd *tx_cmd,
1484 struct ieee80211_tx_info *info,
1485 struct ieee80211_hdr *hdr, u8 std_id)
1487 __le16 fc = hdr->frame_control;
1488 __le32 tx_flags = tx_cmd->tx_flags;
1490 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1491 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
1492 tx_flags |= TX_CMD_FLG_ACK_MSK;
1493 if (ieee80211_is_mgmt(fc))
1494 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1495 if (ieee80211_is_probe_resp(fc) &&
1496 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1497 tx_flags |= TX_CMD_FLG_TSF_MSK;
1499 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1500 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1503 if (ieee80211_is_back_req(fc))
1504 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1506 tx_cmd->sta_id = std_id;
1507 if (ieee80211_has_morefrags(fc))
1508 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1510 if (ieee80211_is_data_qos(fc)) {
1511 u8 *qc = ieee80211_get_qos_ctl(hdr);
1512 tx_cmd->tid_tspec = qc[0] & 0xf;
1513 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1515 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1518 il_tx_cmd_protection(il, info, fc, &tx_flags);
1520 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1521 if (ieee80211_is_mgmt(fc)) {
1522 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
1523 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
1525 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
1527 tx_cmd->timeout.pm_frame_timeout = 0;
1530 tx_cmd->driver_txop = 0;
1531 tx_cmd->tx_flags = tx_flags;
1532 tx_cmd->next_frame_len = 0;
1535 #define RTS_DFAULT_RETRY_LIMIT 60
1538 il4965_tx_cmd_build_rate(struct il_priv *il, struct il_tx_cmd *tx_cmd,
1539 struct ieee80211_tx_info *info, __le16 fc)
1544 u8 data_retry_limit;
1547 /* Set retry limit on DATA packets and Probe Responses */
1548 if (ieee80211_is_probe_resp(fc))
1549 data_retry_limit = 3;
1551 data_retry_limit = IL4965_DEFAULT_TX_RETRY;
1552 tx_cmd->data_retry_limit = data_retry_limit;
1554 /* Set retry limit on RTS packets */
1555 rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
1556 if (data_retry_limit < rts_retry_limit)
1557 rts_retry_limit = data_retry_limit;
1558 tx_cmd->rts_retry_limit = rts_retry_limit;
1560 /* DATA packets will use the uCode station table for rate/antenna
1562 if (ieee80211_is_data(fc)) {
1563 tx_cmd->initial_rate_idx = 0;
1564 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1569 * If the current TX rate stored in mac80211 has the MCS bit set, it's
1570 * not really a TX rate. Thus, we use the lowest supported rate for
1571 * this band. Also use the lowest supported rate if the stored rate
1574 rate_idx = info->control.rates[0].idx;
1575 if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
1576 || rate_idx > RATE_COUNT_LEGACY)
1578 rate_lowest_index(&il->bands[info->band],
1580 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
1581 if (info->band == IEEE80211_BAND_5GHZ)
1582 rate_idx += IL_FIRST_OFDM_RATE;
1583 /* Get PLCP rate for tx_cmd->rate_n_flags */
1584 rate_plcp = il_rates[rate_idx].plcp;
1585 /* Zero out flags for this packet */
1588 /* Set CCK flag as needed */
1589 if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
1590 rate_flags |= RATE_MCS_CCK_MSK;
1592 /* Set up antennas */
1594 il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
1595 il->hw_params.valid_tx_ant);
1597 rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
1599 /* Set the rate in the TX cmd */
1600 tx_cmd->rate_n_flags =
1601 il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
1605 il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
1606 struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
1609 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1611 switch (keyconf->cipher) {
1612 case WLAN_CIPHER_SUITE_CCMP:
1613 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1614 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1615 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1616 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1617 D_TX("tx_cmd with AES hwcrypto\n");
1620 case WLAN_CIPHER_SUITE_TKIP:
1621 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1622 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1623 D_TX("tx_cmd with tkip hwcrypto\n");
1626 case WLAN_CIPHER_SUITE_WEP104:
1627 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1629 case WLAN_CIPHER_SUITE_WEP40:
1631 (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
1634 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1636 D_TX("Configuring packet for WEP encryption " "with key %d\n",
1641 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1647 * start C_TX command process
1650 il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
1652 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1653 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1654 struct ieee80211_sta *sta = info->control.sta;
1655 struct il_station_priv *sta_priv = NULL;
1656 struct il_tx_queue *txq;
1658 struct il_device_cmd *out_cmd;
1659 struct il_cmd_meta *out_meta;
1660 struct il_tx_cmd *tx_cmd;
1661 struct il_rxon_context *ctx = &il->ctx;
1663 dma_addr_t phys_addr;
1664 dma_addr_t txcmd_phys;
1665 dma_addr_t scratch_phys;
1666 u16 len, firstlen, secondlen;
1671 u8 wait_write_ptr = 0;
1674 unsigned long flags;
1675 bool is_agg = false;
1677 if (info->control.vif)
1678 ctx = il_rxon_ctx_from_vif(info->control.vif);
1680 spin_lock_irqsave(&il->lock, flags);
1681 if (il_is_rfkill(il)) {
1682 D_DROP("Dropping - RF KILL\n");
1686 fc = hdr->frame_control;
1688 #ifdef CONFIG_IWLEGACY_DEBUG
1689 if (ieee80211_is_auth(fc))
1690 D_TX("Sending AUTH frame\n");
1691 else if (ieee80211_is_assoc_req(fc))
1692 D_TX("Sending ASSOC frame\n");
1693 else if (ieee80211_is_reassoc_req(fc))
1694 D_TX("Sending REASSOC frame\n");
1697 hdr_len = ieee80211_hdrlen(fc);
1699 /* For management frames use broadcast id to do not break aggregation */
1700 if (!ieee80211_is_data(fc))
1701 sta_id = ctx->bcast_sta_id;
1703 /* Find idx into station table for destination station */
1704 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1706 if (sta_id == IL_INVALID_STATION) {
1707 D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
1712 D_TX("station Id %d\n", sta_id);
1715 sta_priv = (void *)sta->drv_priv;
1717 if (sta_priv && sta_priv->asleep &&
1718 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1720 * This sends an asynchronous command to the device,
1721 * but we can rely on it being processed before the
1722 * next frame is processed -- and the next frame to
1723 * this station is the one that will consume this
1725 * For now set the counter to just 1 since we do not
1726 * support uAPSD yet.
1728 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1732 * Send this frame after DTIM -- there's a special queue
1733 * reserved for this for contexts that support AP mode.
1735 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1736 txq_id = ctx->mcast_queue;
1738 * The microcode will clear the more data
1739 * bit in the last frame it transmits.
1741 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1743 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
1745 /* irqs already disabled/saved above when locking il->lock */
1746 spin_lock(&il->sta_lock);
1748 if (ieee80211_is_data_qos(fc)) {
1749 qc = ieee80211_get_qos_ctl(hdr);
1750 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1751 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1752 spin_unlock(&il->sta_lock);
1755 seq_number = il->stations[sta_id].tid[tid].seq_number;
1756 seq_number &= IEEE80211_SCTL_SEQ;
1758 hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
1759 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1761 /* aggregation is on for this <sta,tid> */
1762 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1763 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1764 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1769 txq = &il->txq[txq_id];
1772 if (unlikely(il_queue_space(q) < q->high_mark)) {
1773 spin_unlock(&il->sta_lock);
1777 if (ieee80211_is_data_qos(fc)) {
1778 il->stations[sta_id].tid[tid].tfds_in_queue++;
1779 if (!ieee80211_has_morefrags(fc))
1780 il->stations[sta_id].tid[tid].seq_number = seq_number;
1783 spin_unlock(&il->sta_lock);
1785 /* Set up driver data for this TFD */
1786 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1787 txq->txb[q->write_ptr].skb = skb;
1788 txq->txb[q->write_ptr].ctx = ctx;
1790 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1791 out_cmd = txq->cmd[q->write_ptr];
1792 out_meta = &txq->meta[q->write_ptr];
1793 tx_cmd = &out_cmd->cmd.tx;
1794 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1795 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1798 * Set up the Tx-command (not MAC!) header.
1799 * Store the chosen Tx queue and TFD idx within the sequence field;
1800 * after Tx, uCode's Tx response will return this value so driver can
1801 * locate the frame within the tx queue and do post-tx processing.
1803 out_cmd->hdr.cmd = C_TX;
1804 out_cmd->hdr.sequence =
1806 (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
1808 /* Copy MAC header from skb into command buffer */
1809 memcpy(tx_cmd->hdr, hdr, hdr_len);
1811 /* Total # bytes to be transmitted */
1812 len = (u16) skb->len;
1813 tx_cmd->len = cpu_to_le16(len);
1815 if (info->control.hw_key)
1816 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1818 /* TODO need this for burst mode later on */
1819 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1820 il_dbg_log_tx_data_frame(il, len, hdr);
1822 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1824 il_update_stats(il, true, fc, len);
1826 * Use the first empty entry in this queue's command buffer array
1827 * to contain the Tx command and MAC header concatenated together
1828 * (payload data will be in another buffer).
1829 * Size of this varies, due to varying MAC header length.
1830 * If end is not dword aligned, we'll have 2 extra bytes at the end
1831 * of the MAC header (device reads on dword boundaries).
1832 * We'll tell device about this padding later.
1834 len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
1835 firstlen = (len + 3) & ~3;
1837 /* Tell NIC about any 2-byte padding after MAC header */
1838 if (firstlen != len)
1839 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1841 /* Physical address of this Tx command's header (not MAC header!),
1842 * within command buffer array. */
1844 pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
1845 PCI_DMA_BIDIRECTIONAL);
1846 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1847 dma_unmap_len_set(out_meta, len, firstlen);
1848 /* Add buffer containing Tx command and MAC(!) header to TFD's
1850 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen,
1853 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1854 txq->need_update = 1;
1857 txq->need_update = 0;
1860 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1861 * if any (802.11 null frames have no payload). */
1862 secondlen = skb->len - hdr_len;
1863 if (secondlen > 0) {
1865 pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
1867 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr,
1872 txcmd_phys + sizeof(struct il_cmd_header) +
1873 offsetof(struct il_tx_cmd, scratch);
1875 /* take back ownership of DMA buffer to enable update */
1876 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
1877 PCI_DMA_BIDIRECTIONAL);
1878 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1879 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1881 D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
1882 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
1883 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
1884 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
1886 /* Set up entry for this TFD in Tx byte-count array */
1887 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1888 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
1889 le16_to_cpu(tx_cmd->
1892 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
1893 PCI_DMA_BIDIRECTIONAL);
1895 /* Tell device the write idx *just past* this latest filled TFD */
1896 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1897 il_txq_update_write_ptr(il, txq);
1898 spin_unlock_irqrestore(&il->lock, flags);
1901 * At this point the frame is "transmitted" successfully
1902 * and we will get a TX status notification eventually,
1903 * regardless of the value of ret. "ret" only indicates
1904 * whether or not we should update the write pointer.
1908 * Avoid atomic ops if it isn't an associated client.
1909 * Also, if this is a packet for aggregation, don't
1910 * increase the counter because the ucode will stop
1911 * aggregation queues when their respective station
1914 if (sta_priv && sta_priv->client && !is_agg)
1915 atomic_inc(&sta_priv->pending_frames);
1917 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1918 if (wait_write_ptr) {
1919 spin_lock_irqsave(&il->lock, flags);
1920 txq->need_update = 1;
1921 il_txq_update_write_ptr(il, txq);
1922 spin_unlock_irqrestore(&il->lock, flags);
1924 il_stop_queue(il, txq);
1931 spin_unlock_irqrestore(&il->lock, flags);
1936 il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
1939 dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
1947 il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
1949 if (unlikely(!ptr->addr))
1952 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1953 memset(ptr, 0, sizeof(*ptr));
1957 * il4965_hw_txq_ctx_free - Free TXQ Context
1959 * Destroy all TX DMA queues and structures
1962 il4965_hw_txq_ctx_free(struct il_priv *il)
1968 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1969 if (txq_id == il->cmd_queue)
1970 il_cmd_queue_free(il);
1972 il_tx_queue_free(il, txq_id);
1974 il4965_free_dma_ptr(il, &il->kw);
1976 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1978 /* free tx queue structure */
1983 * il4965_txq_ctx_alloc - allocate TX queue context
1984 * Allocate all Tx DMA structures and initialize them
1987 * @return error code
1990 il4965_txq_ctx_alloc(struct il_priv *il)
1993 int txq_id, slots_num;
1994 unsigned long flags;
1996 /* Free all tx/cmd queues and keep-warm buffer */
1997 il4965_hw_txq_ctx_free(il);
2000 il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
2001 il->hw_params.scd_bc_tbls_size);
2003 IL_ERR("Scheduler BC Table allocation failed\n");
2006 /* Alloc keep-warm buffer */
2007 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
2009 IL_ERR("Keep Warm allocation failed\n");
2013 /* allocate tx queue structure */
2014 ret = il_alloc_txq_mem(il);
2018 spin_lock_irqsave(&il->lock, flags);
2020 /* Turn off all Tx DMA fifos */
2021 il4965_txq_set_sched(il, 0);
2023 /* Tell NIC where to find the "keep warm" buffer */
2024 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
2026 spin_unlock_irqrestore(&il->lock, flags);
2028 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2029 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
2032 il->cmd_queue) ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2033 ret = il_tx_queue_init(il, &il->txq[txq_id], slots_num, txq_id);
2035 IL_ERR("Tx %d queue init failed\n", txq_id);
2043 il4965_hw_txq_ctx_free(il);
2044 il4965_free_dma_ptr(il, &il->kw);
2046 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
2052 il4965_txq_ctx_reset(struct il_priv *il)
2054 int txq_id, slots_num;
2055 unsigned long flags;
2057 spin_lock_irqsave(&il->lock, flags);
2059 /* Turn off all Tx DMA fifos */
2060 il4965_txq_set_sched(il, 0);
2062 /* Tell NIC where to find the "keep warm" buffer */
2063 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
2065 spin_unlock_irqrestore(&il->lock, flags);
2067 /* Alloc and init all Tx queues, including the command queue (#4) */
2068 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
2070 txq_id == il->cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2071 il_tx_queue_reset(il, &il->txq[txq_id], slots_num, txq_id);
2076 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2079 il4965_txq_ctx_stop(struct il_priv *il)
2082 unsigned long flags;
2084 /* Turn off all Tx DMA fifos */
2085 spin_lock_irqsave(&il->lock, flags);
2087 il4965_txq_set_sched(il, 0);
2089 /* Stop each Tx DMA channel, and wait for it to be idle */
2090 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
2091 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2093 (il, FH49_TSSR_TX_STATUS_REG,
2094 FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000))
2095 IL_ERR("Failing on timeout while stopping"
2096 " DMA channel %d [0x%08x]", ch, il_rd(il,
2097 FH49_TSSR_TX_STATUS_REG));
2099 spin_unlock_irqrestore(&il->lock, flags);
2104 /* Unmap DMA from host system and free skb's */
2105 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2106 if (txq_id == il->cmd_queue)
2107 il_cmd_queue_unmap(il);
2109 il_tx_queue_unmap(il, txq_id);
2113 * Find first available (lowest unused) Tx Queue, mark it "active".
2114 * Called only when finding queue for aggregation.
2115 * Should never return anything < 7, because they should already
2116 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2119 il4965_txq_ctx_activate_free(struct il_priv *il)
2123 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2124 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2130 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2133 il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
2135 /* Simply stop the queue, but don't change any configuration;
2136 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
2137 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
2138 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) | (1 <<
2139 IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
2143 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2146 il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
2152 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2155 il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
2157 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2160 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2162 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2164 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2170 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2172 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2173 * i.e. it must be one of the higher queues used for aggregation
2176 il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
2177 int tid, u16 ssn_idx)
2179 unsigned long flags;
2183 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2184 (IL49_FIRST_AMPDU_QUEUE +
2185 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2186 IL_WARN("queue number out of range: %d, must be %d to %d\n",
2187 txq_id, IL49_FIRST_AMPDU_QUEUE,
2188 IL49_FIRST_AMPDU_QUEUE +
2189 il->cfg->base_params->num_of_ampdu_queues - 1);
2193 ra_tid = BUILD_RAxTID(sta_id, tid);
2195 /* Modify device's station table to Tx this TID */
2196 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2200 spin_lock_irqsave(&il->lock, flags);
2202 /* Stop this Tx queue before configuring it */
2203 il4965_tx_queue_stop_scheduler(il, txq_id);
2205 /* Map receiver-address / traffic-ID to this queue */
2206 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2208 /* Set this queue as a chain-building queue */
2209 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2211 /* Place first TFD at idx corresponding to start sequence number.
2212 * Assumes that ssn_idx is valid (!= 0xFFF) */
2213 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2214 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2215 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2217 /* Set up Tx win size and frame limit for this queue */
2218 il_write_targ_mem(il,
2220 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2221 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
2222 & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
2224 il_write_targ_mem(il,
2226 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2228 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
2229 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
2231 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2233 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2234 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2236 spin_unlock_irqrestore(&il->lock, flags);
2242 il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2243 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
2249 unsigned long flags;
2250 struct il_tid_data *tid_data;
2252 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2253 if (unlikely(tx_fifo < 0))
2256 IL_WARN("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
2258 sta_id = il_sta_id(sta);
2259 if (sta_id == IL_INVALID_STATION) {
2260 IL_ERR("Start AGG on invalid station\n");
2263 if (unlikely(tid >= MAX_TID_COUNT))
2266 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2267 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2271 txq_id = il4965_txq_ctx_activate_free(il);
2273 IL_ERR("No free aggregation queue available\n");
2277 spin_lock_irqsave(&il->sta_lock, flags);
2278 tid_data = &il->stations[sta_id].tid[tid];
2279 *ssn = SEQ_TO_SN(tid_data->seq_number);
2280 tid_data->agg.txq_id = txq_id;
2281 il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
2282 spin_unlock_irqrestore(&il->sta_lock, flags);
2284 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
2288 spin_lock_irqsave(&il->sta_lock, flags);
2289 tid_data = &il->stations[sta_id].tid[tid];
2290 if (tid_data->tfds_in_queue == 0) {
2291 D_HT("HW queue is empty\n");
2292 tid_data->agg.state = IL_AGG_ON;
2293 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2295 D_HT("HW queue is NOT empty: %d packets in HW queue\n",
2296 tid_data->tfds_in_queue);
2297 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2299 spin_unlock_irqrestore(&il->sta_lock, flags);
2304 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2305 * il->lock must be held by the caller
2308 il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
2310 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2311 (IL49_FIRST_AMPDU_QUEUE +
2312 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2313 IL_WARN("queue number out of range: %d, must be %d to %d\n",
2314 txq_id, IL49_FIRST_AMPDU_QUEUE,
2315 IL49_FIRST_AMPDU_QUEUE +
2316 il->cfg->base_params->num_of_ampdu_queues - 1);
2320 il4965_tx_queue_stop_scheduler(il, txq_id);
2322 il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2324 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2325 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2326 /* supposes that ssn_idx is valid (!= 0xFFF) */
2327 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2329 il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2330 il_txq_ctx_deactivate(il, txq_id);
2331 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2337 il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2338 struct ieee80211_sta *sta, u16 tid)
2340 int tx_fifo_id, txq_id, sta_id, ssn;
2341 struct il_tid_data *tid_data;
2342 int write_ptr, read_ptr;
2343 unsigned long flags;
2345 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2346 if (unlikely(tx_fifo_id < 0))
2349 sta_id = il_sta_id(sta);
2351 if (sta_id == IL_INVALID_STATION) {
2352 IL_ERR("Invalid station for AGG tid %d\n", tid);
2356 spin_lock_irqsave(&il->sta_lock, flags);
2358 tid_data = &il->stations[sta_id].tid[tid];
2359 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2360 txq_id = tid_data->agg.txq_id;
2362 switch (il->stations[sta_id].tid[tid].agg.state) {
2363 case IL_EMPTYING_HW_QUEUE_ADDBA:
2365 * This can happen if the peer stops aggregation
2366 * again before we've had a chance to drain the
2367 * queue we selected previously, i.e. before the
2368 * session was really started completely.
2370 D_HT("AGG stop before setup done\n");
2375 IL_WARN("Stopping AGG while state not ON or starting\n");
2378 write_ptr = il->txq[txq_id].q.write_ptr;
2379 read_ptr = il->txq[txq_id].q.read_ptr;
2381 /* The queue is not empty */
2382 if (write_ptr != read_ptr) {
2383 D_HT("Stopping a non empty AGG HW QUEUE\n");
2384 il->stations[sta_id].tid[tid].agg.state =
2385 IL_EMPTYING_HW_QUEUE_DELBA;
2386 spin_unlock_irqrestore(&il->sta_lock, flags);
2390 D_HT("HW queue is empty\n");
2392 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2394 /* do not restore/save irqs */
2395 spin_unlock(&il->sta_lock);
2396 spin_lock(&il->lock);
2399 * the only reason this call can fail is queue number out of range,
2400 * which can happen if uCode is reloaded and all the station
2401 * information are lost. if it is outside the range, there is no need
2402 * to deactivate the uCode queue, just return "success" to allow
2403 * mac80211 to clean up it own data.
2405 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2406 spin_unlock_irqrestore(&il->lock, flags);
2408 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2414 il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
2416 struct il_queue *q = &il->txq[txq_id].q;
2417 u8 *addr = il->stations[sta_id].sta.sta.addr;
2418 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2419 struct il_rxon_context *ctx;
2423 lockdep_assert_held(&il->sta_lock);
2425 switch (il->stations[sta_id].tid[tid].agg.state) {
2426 case IL_EMPTYING_HW_QUEUE_DELBA:
2427 /* We are reclaiming the last packet of the */
2428 /* aggregated HW queue */
2429 if (txq_id == tid_data->agg.txq_id &&
2430 q->read_ptr == q->write_ptr) {
2431 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2432 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
2433 D_HT("HW queue empty: continue DELBA flow\n");
2434 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2435 tid_data->agg.state = IL_AGG_OFF;
2436 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2439 case IL_EMPTYING_HW_QUEUE_ADDBA:
2440 /* We are reclaiming the last packet of the queue */
2441 if (tid_data->tfds_in_queue == 0) {
2442 D_HT("HW queue empty: continue ADDBA flow\n");
2443 tid_data->agg.state = IL_AGG_ON;
2444 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2453 il4965_non_agg_tx_status(struct il_priv *il, struct il_rxon_context *ctx,
2456 struct ieee80211_sta *sta;
2457 struct il_station_priv *sta_priv;
2460 sta = ieee80211_find_sta(ctx->vif, addr1);
2462 sta_priv = (void *)sta->drv_priv;
2463 /* avoid atomic ops if this isn't a client */
2464 if (sta_priv->client &&
2465 atomic_dec_return(&sta_priv->pending_frames) == 0)
2466 ieee80211_sta_block_awake(il->hw, sta, false);
2472 il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info, bool is_agg)
2474 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2477 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2479 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2483 il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
2485 struct il_tx_queue *txq = &il->txq[txq_id];
2486 struct il_queue *q = &txq->q;
2487 struct il_tx_info *tx_info;
2489 struct ieee80211_hdr *hdr;
2491 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2492 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
2493 "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
2494 q->write_ptr, q->read_ptr);
2498 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
2499 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2501 tx_info = &txq->txb[txq->q.read_ptr];
2503 if (WARN_ON_ONCE(tx_info->skb == NULL))
2506 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2507 if (ieee80211_is_data_qos(hdr->frame_control))
2510 il4965_tx_status(il, tx_info,
2511 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2512 tx_info->skb = NULL;
2514 il->cfg->ops->lib->txq_free_tfd(il, txq);
2520 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2522 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2523 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2526 il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
2527 struct il_compressed_ba_resp *ba_resp)
2530 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2531 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2533 struct ieee80211_tx_info *info;
2534 u64 bitmap, sent_bitmap;
2536 if (unlikely(!agg->wait_for_ba)) {
2537 if (unlikely(ba_resp->bitmap))
2538 IL_ERR("Received BA when not expected\n");
2542 /* Mark that the expected block-ack response arrived */
2543 agg->wait_for_ba = 0;
2544 D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
2546 /* Calculate shift to align block-ack bits with our Tx win bits */
2547 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
2548 if (sh < 0) /* tbw something is wrong with indices */
2551 if (agg->frame_count > (64 - sh)) {
2552 D_TX_REPLY("more frames than bitmap size");
2556 /* don't use 64-bit values for now */
2557 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2559 /* check for success or failure according to the
2560 * transmitted bitmap and block-ack bitmap */
2561 sent_bitmap = bitmap & agg->bitmap;
2563 /* For each frame attempted in aggregation,
2564 * update driver's record of tx frame's status. */
2566 while (sent_bitmap) {
2567 ack = sent_bitmap & 1ULL;
2569 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
2570 i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
2575 D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
2577 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2578 memset(&info->status, 0, sizeof(info->status));
2579 info->flags |= IEEE80211_TX_STAT_ACK;
2580 info->flags |= IEEE80211_TX_STAT_AMPDU;
2581 info->status.ampdu_ack_len = successes;
2582 info->status.ampdu_len = agg->frame_count;
2583 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2589 * translate ucode response to mac80211 tx status control values
2592 il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2593 struct ieee80211_tx_info *info)
2595 struct ieee80211_tx_rate *r = &info->control.rates[0];
2597 info->antenna_sel_tx =
2598 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
2599 if (rate_n_flags & RATE_MCS_HT_MSK)
2600 r->flags |= IEEE80211_TX_RC_MCS;
2601 if (rate_n_flags & RATE_MCS_GF_MSK)
2602 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2603 if (rate_n_flags & RATE_MCS_HT40_MSK)
2604 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2605 if (rate_n_flags & RATE_MCS_DUP_MSK)
2606 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2607 if (rate_n_flags & RATE_MCS_SGI_MSK)
2608 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2609 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2613 * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
2615 * Handles block-acknowledge notification from device, which reports success
2616 * of frames sent via aggregation.
2619 il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
2621 struct il_rx_pkt *pkt = rxb_addr(rxb);
2622 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2623 struct il_tx_queue *txq = NULL;
2624 struct il_ht_agg *agg;
2628 unsigned long flags;
2630 /* "flow" corresponds to Tx queue */
2631 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2633 /* "ssn" is start of block-ack Tx win, corresponds to idx
2634 * (in Tx queue's circular buffer) of first TFD/frame in win */
2635 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2637 if (scd_flow >= il->hw_params.max_txq_num) {
2638 IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
2642 txq = &il->txq[scd_flow];
2643 sta_id = ba_resp->sta_id;
2645 agg = &il->stations[sta_id].tid[tid].agg;
2646 if (unlikely(agg->txq_id != scd_flow)) {
2648 * FIXME: this is a uCode bug which need to be addressed,
2649 * log the information and return for now!
2650 * since it is possible happen very often and in order
2651 * not to fill the syslog, don't enable the logging by default
2653 D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
2654 scd_flow, agg->txq_id);
2658 /* Find idx just before block-ack win */
2659 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2661 spin_lock_irqsave(&il->sta_lock, flags);
2663 D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
2664 agg->wait_for_ba, (u8 *) & ba_resp->sta_addr_lo32,
2666 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
2667 "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
2668 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2669 ba_resp->scd_flow, ba_resp->scd_ssn);
2670 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
2671 (unsigned long long)agg->bitmap);
2673 /* Update driver's record of ACK vs. not for each frame in win */
2674 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2676 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2677 * block-ack win (we assume that they've been successfully
2678 * transmitted ... if not, it's too late anyway). */
2679 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2680 /* calculate mac80211 ampdu sw queue to wake */
2681 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2682 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2684 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2685 il->mac80211_registered &&
2686 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2687 il_wake_queue(il, txq);
2689 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2692 spin_unlock_irqrestore(&il->sta_lock, flags);
2695 #ifdef CONFIG_IWLEGACY_DEBUG
2697 il4965_get_tx_fail_reason(u32 status)
2699 #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2700 #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2702 switch (status & TX_STATUS_MSK) {
2703 case TX_STATUS_SUCCESS:
2705 TX_STATUS_POSTPONE(DELAY);
2706 TX_STATUS_POSTPONE(FEW_BYTES);
2707 TX_STATUS_POSTPONE(QUIET_PERIOD);
2708 TX_STATUS_POSTPONE(CALC_TTAK);
2709 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2710 TX_STATUS_FAIL(SHORT_LIMIT);
2711 TX_STATUS_FAIL(LONG_LIMIT);
2712 TX_STATUS_FAIL(FIFO_UNDERRUN);
2713 TX_STATUS_FAIL(DRAIN_FLOW);
2714 TX_STATUS_FAIL(RFKILL_FLUSH);
2715 TX_STATUS_FAIL(LIFE_EXPIRE);
2716 TX_STATUS_FAIL(DEST_PS);
2717 TX_STATUS_FAIL(HOST_ABORTED);
2718 TX_STATUS_FAIL(BT_RETRY);
2719 TX_STATUS_FAIL(STA_INVALID);
2720 TX_STATUS_FAIL(FRAG_DROPPED);
2721 TX_STATUS_FAIL(TID_DISABLE);
2722 TX_STATUS_FAIL(FIFO_FLUSHED);
2723 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2724 TX_STATUS_FAIL(PASSIVE_NO_RX);
2725 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
2730 #undef TX_STATUS_FAIL
2731 #undef TX_STATUS_POSTPONE
2733 #endif /* CONFIG_IWLEGACY_DEBUG */
2735 static struct il_link_quality_cmd *
2736 il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2739 struct il_link_quality_cmd *link_cmd;
2741 __le32 rate_n_flags;
2743 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2745 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2748 /* Set up the rate scaling to start at selected rate, fall back
2749 * all the way down to 1M in IEEE order, and then spin on 1M */
2750 if (il->band == IEEE80211_BAND_5GHZ)
2755 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2756 rate_flags |= RATE_MCS_CCK_MSK;
2759 il4965_first_antenna(il->hw_params.
2760 valid_tx_ant) << RATE_MCS_ANT_POS;
2761 rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp, rate_flags);
2762 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2763 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2765 link_cmd->general_params.single_stream_ant_msk =
2766 il4965_first_antenna(il->hw_params.valid_tx_ant);
2768 link_cmd->general_params.dual_stream_ant_msk =
2769 il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
2771 if (!link_cmd->general_params.dual_stream_ant_msk) {
2772 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2773 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2774 link_cmd->general_params.dual_stream_ant_msk =
2775 il->hw_params.valid_tx_ant;
2778 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2779 link_cmd->agg_params.agg_time_limit =
2780 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2782 link_cmd->sta_id = sta_id;
2788 * il4965_add_bssid_station - Add the special IBSS BSSID station
2793 il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
2794 const u8 * addr, u8 * sta_id_r)
2798 struct il_link_quality_cmd *link_cmd;
2799 unsigned long flags;
2802 *sta_id_r = IL_INVALID_STATION;
2804 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2806 IL_ERR("Unable to add station %pM\n", addr);
2813 spin_lock_irqsave(&il->sta_lock, flags);
2814 il->stations[sta_id].used |= IL_STA_LOCAL;
2815 spin_unlock_irqrestore(&il->sta_lock, flags);
2817 /* Set up default rate scaling table in device's station table */
2818 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2820 IL_ERR("Unable to initialize rate scaling for station %pM.\n",
2825 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2827 IL_ERR("Link quality command failed (%d)\n", ret);
2829 spin_lock_irqsave(&il->sta_lock, flags);
2830 il->stations[sta_id].lq = link_cmd;
2831 spin_unlock_irqrestore(&il->sta_lock, flags);
2837 il4965_static_wepkey_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2840 int i, not_empty = 0;
2841 u8 buff[sizeof(struct il_wep_cmd) +
2842 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2843 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
2844 size_t cmd_size = sizeof(struct il_wep_cmd);
2845 struct il_host_cmd cmd = {
2846 .id = ctx->wep_key_cmd,
2854 cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
2856 for (i = 0; i < WEP_KEYS_MAX; i++) {
2857 wep_cmd->key[i].key_idx = i;
2858 if (ctx->wep_keys[i].key_size) {
2859 wep_cmd->key[i].key_offset = i;
2862 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2865 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
2866 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
2867 ctx->wep_keys[i].key_size);
2870 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2871 wep_cmd->num_keys = WEP_KEYS_MAX;
2873 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
2877 if (not_empty || send_if_empty)
2878 return il_send_cmd(il, &cmd);
2884 il4965_restore_default_wep_keys(struct il_priv *il, struct il_rxon_context *ctx)
2886 lockdep_assert_held(&il->mutex);
2888 return il4965_static_wepkey_cmd(il, ctx, false);
2892 il4965_remove_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2893 struct ieee80211_key_conf *keyconf)
2897 lockdep_assert_held(&il->mutex);
2899 D_WEP("Removing default WEP key: idx=%d\n", keyconf->keyidx);
2901 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
2902 if (il_is_rfkill(il)) {
2903 D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
2904 /* but keys in device are clear anyway so return success */
2907 ret = il4965_static_wepkey_cmd(il, ctx, 1);
2908 D_WEP("Remove default WEP key: idx=%d ret=%d\n", keyconf->keyidx, ret);
2914 il4965_set_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2915 struct ieee80211_key_conf *keyconf)
2919 lockdep_assert_held(&il->mutex);
2921 if (keyconf->keylen != WEP_KEY_LEN_128 &&
2922 keyconf->keylen != WEP_KEY_LEN_64) {
2923 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2927 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2928 keyconf->hw_key_idx = HW_KEY_DEFAULT;
2929 il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
2931 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
2932 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
2935 ret = il4965_static_wepkey_cmd(il, ctx, false);
2936 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", keyconf->keylen,
2937 keyconf->keyidx, ret);
2943 il4965_set_wep_dynamic_key_info(struct il_priv *il, struct il_rxon_context *ctx,
2944 struct ieee80211_key_conf *keyconf, u8 sta_id)
2946 unsigned long flags;
2947 __le16 key_flags = 0;
2948 struct il_addsta_cmd sta_cmd;
2950 lockdep_assert_held(&il->mutex);
2952 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2954 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2955 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2956 key_flags &= ~STA_KEY_FLG_INVALID;
2958 if (keyconf->keylen == WEP_KEY_LEN_128)
2959 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2961 if (sta_id == ctx->bcast_sta_id)
2962 key_flags |= STA_KEY_MULTICAST_MSK;
2964 spin_lock_irqsave(&il->sta_lock, flags);
2966 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2967 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2968 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2970 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
2972 memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
2975 if ((il->stations[sta_id].sta.key.
2976 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
2977 il->stations[sta_id].sta.key.key_offset =
2978 il_get_free_ucode_key_idx(il);
2979 /* else, we are overriding an existing key => no need to allocated room
2982 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
2983 "no space for a new key");
2985 il->stations[sta_id].sta.key.key_flags = key_flags;
2986 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
2987 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2989 memcpy(&sta_cmd, &il->stations[sta_id].sta,
2990 sizeof(struct il_addsta_cmd));
2991 spin_unlock_irqrestore(&il->sta_lock, flags);
2993 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2997 il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
2998 struct il_rxon_context *ctx,
2999 struct ieee80211_key_conf *keyconf, u8 sta_id)
3001 unsigned long flags;
3002 __le16 key_flags = 0;
3003 struct il_addsta_cmd sta_cmd;
3005 lockdep_assert_held(&il->mutex);
3007 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
3008 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3009 key_flags &= ~STA_KEY_FLG_INVALID;
3011 if (sta_id == ctx->bcast_sta_id)
3012 key_flags |= STA_KEY_MULTICAST_MSK;
3014 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3016 spin_lock_irqsave(&il->sta_lock, flags);
3017 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3018 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
3020 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
3022 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
3024 if ((il->stations[sta_id].sta.key.
3025 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
3026 il->stations[sta_id].sta.key.key_offset =
3027 il_get_free_ucode_key_idx(il);
3028 /* else, we are overriding an existing key => no need to allocated room
3031 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
3032 "no space for a new key");
3034 il->stations[sta_id].sta.key.key_flags = key_flags;
3035 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3036 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3038 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3039 sizeof(struct il_addsta_cmd));
3040 spin_unlock_irqrestore(&il->sta_lock, flags);
3042 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3046 il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3047 struct il_rxon_context *ctx,
3048 struct ieee80211_key_conf *keyconf, u8 sta_id)
3050 unsigned long flags;
3052 __le16 key_flags = 0;
3054 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3055 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3056 key_flags &= ~STA_KEY_FLG_INVALID;
3058 if (sta_id == ctx->bcast_sta_id)
3059 key_flags |= STA_KEY_MULTICAST_MSK;
3061 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3062 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3064 spin_lock_irqsave(&il->sta_lock, flags);
3066 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3067 il->stations[sta_id].keyinfo.keylen = 16;
3069 if ((il->stations[sta_id].sta.key.
3070 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
3071 il->stations[sta_id].sta.key.key_offset =
3072 il_get_free_ucode_key_idx(il);
3073 /* else, we are overriding an existing key => no need to allocated room
3076 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
3077 "no space for a new key");
3079 il->stations[sta_id].sta.key.key_flags = key_flags;
3081 /* This copy is acutally not needed: we get the key with each TX */
3082 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3084 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3086 spin_unlock_irqrestore(&il->sta_lock, flags);
3092 il4965_update_tkip_key(struct il_priv *il, struct il_rxon_context *ctx,
3093 struct ieee80211_key_conf *keyconf,
3094 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
3097 unsigned long flags;
3100 if (il_scan_cancel(il)) {
3101 /* cancel scan failed, just live w/ bad key and rely
3102 briefly on SW decryption */
3106 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3107 if (sta_id == IL_INVALID_STATION)
3110 spin_lock_irqsave(&il->sta_lock, flags);
3112 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3114 for (i = 0; i < 5; i++)
3115 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3116 cpu_to_le16(phase1key[i]);
3118 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3119 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3121 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3123 spin_unlock_irqrestore(&il->sta_lock, flags);
3128 il4965_remove_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3129 struct ieee80211_key_conf *keyconf, u8 sta_id)
3131 unsigned long flags;
3134 struct il_addsta_cmd sta_cmd;
3136 lockdep_assert_held(&il->mutex);
3138 ctx->key_mapping_keys--;
3140 spin_lock_irqsave(&il->sta_lock, flags);
3141 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3142 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3144 D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
3146 if (keyconf->keyidx != keyidx) {
3147 /* We need to remove a key with idx different that the one
3148 * in the uCode. This means that the key we need to remove has
3149 * been replaced by another one with different idx.
3150 * Don't do anything and return ok
3152 spin_unlock_irqrestore(&il->sta_lock, flags);
3156 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
3157 IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
3159 spin_unlock_irqrestore(&il->sta_lock, flags);
3163 if (!test_and_clear_bit
3164 (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
3165 IL_ERR("idx %d not used in uCode key table.\n",
3166 il->stations[sta_id].sta.key.key_offset);
3167 memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
3168 memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
3169 il->stations[sta_id].sta.key.key_flags =
3170 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
3171 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3172 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3173 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3175 if (il_is_rfkill(il)) {
3177 ("Not sending C_ADD_STA command because RFKILL enabled.\n");
3178 spin_unlock_irqrestore(&il->sta_lock, flags);
3181 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3182 sizeof(struct il_addsta_cmd));
3183 spin_unlock_irqrestore(&il->sta_lock, flags);
3185 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3189 il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3190 struct ieee80211_key_conf *keyconf, u8 sta_id)
3194 lockdep_assert_held(&il->mutex);
3196 ctx->key_mapping_keys++;
3197 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3199 switch (keyconf->cipher) {
3200 case WLAN_CIPHER_SUITE_CCMP:
3202 il4965_set_ccmp_dynamic_key_info(il, ctx, keyconf, sta_id);
3204 case WLAN_CIPHER_SUITE_TKIP:
3206 il4965_set_tkip_dynamic_key_info(il, ctx, keyconf, sta_id);
3208 case WLAN_CIPHER_SUITE_WEP40:
3209 case WLAN_CIPHER_SUITE_WEP104:
3210 ret = il4965_set_wep_dynamic_key_info(il, ctx, keyconf, sta_id);
3213 IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
3218 D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3219 keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
3225 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3227 * This adds the broadcast station into the driver's station table
3228 * and marks it driver active, so that it will be restored to the
3229 * device at the next best time.
3232 il4965_alloc_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
3234 struct il_link_quality_cmd *link_cmd;
3235 unsigned long flags;
3238 spin_lock_irqsave(&il->sta_lock, flags);
3239 sta_id = il_prep_station(il, ctx, il_bcast_addr, false, NULL);
3240 if (sta_id == IL_INVALID_STATION) {
3241 IL_ERR("Unable to prepare broadcast station\n");
3242 spin_unlock_irqrestore(&il->sta_lock, flags);
3247 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3248 il->stations[sta_id].used |= IL_STA_BCAST;
3249 spin_unlock_irqrestore(&il->sta_lock, flags);
3251 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3254 ("Unable to initialize rate scaling for bcast station.\n");
3258 spin_lock_irqsave(&il->sta_lock, flags);
3259 il->stations[sta_id].lq = link_cmd;
3260 spin_unlock_irqrestore(&il->sta_lock, flags);
3266 * il4965_update_bcast_station - update broadcast station's LQ command
3268 * Only used by iwl4965. Placed here to have all bcast station management
3272 il4965_update_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
3274 unsigned long flags;
3275 struct il_link_quality_cmd *link_cmd;
3276 u8 sta_id = ctx->bcast_sta_id;
3278 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3281 ("Unable to initialize rate scaling for bcast station.\n");
3285 spin_lock_irqsave(&il->sta_lock, flags);
3286 if (il->stations[sta_id].lq)
3287 kfree(il->stations[sta_id].lq);
3290 ("Bcast station rate scaling has not been initialized yet.\n");
3291 il->stations[sta_id].lq = link_cmd;
3292 spin_unlock_irqrestore(&il->sta_lock, flags);
3298 il4965_update_bcast_stations(struct il_priv *il)
3300 return il4965_update_bcast_station(il, &il->ctx);
3304 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3307 il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
3309 unsigned long flags;
3310 struct il_addsta_cmd sta_cmd;
3312 lockdep_assert_held(&il->mutex);
3314 /* Remove "disable" flag, to enable Tx for this TID */
3315 spin_lock_irqsave(&il->sta_lock, flags);
3316 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3317 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3318 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3319 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3320 sizeof(struct il_addsta_cmd));
3321 spin_unlock_irqrestore(&il->sta_lock, flags);
3323 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3327 il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
3330 unsigned long flags;
3332 struct il_addsta_cmd sta_cmd;
3334 lockdep_assert_held(&il->mutex);
3336 sta_id = il_sta_id(sta);
3337 if (sta_id == IL_INVALID_STATION)
3340 spin_lock_irqsave(&il->sta_lock, flags);
3341 il->stations[sta_id].sta.station_flags_msk = 0;
3342 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
3343 il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
3344 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3345 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3346 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3347 sizeof(struct il_addsta_cmd));
3348 spin_unlock_irqrestore(&il->sta_lock, flags);
3350 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3354 il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
3356 unsigned long flags;
3358 struct il_addsta_cmd sta_cmd;
3360 lockdep_assert_held(&il->mutex);
3362 sta_id = il_sta_id(sta);
3363 if (sta_id == IL_INVALID_STATION) {
3364 IL_ERR("Invalid station for AGG tid %d\n", tid);
3368 spin_lock_irqsave(&il->sta_lock, flags);
3369 il->stations[sta_id].sta.station_flags_msk = 0;
3370 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
3371 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
3372 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3373 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3374 sizeof(struct il_addsta_cmd));
3375 spin_unlock_irqrestore(&il->sta_lock, flags);
3377 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3381 il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3383 unsigned long flags;
3385 spin_lock_irqsave(&il->sta_lock, flags);
3386 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3387 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3388 il->stations[sta_id].sta.sta.modify_mask =
3389 STA_MODIFY_SLEEP_TX_COUNT_MSK;
3390 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3391 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3392 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3393 spin_unlock_irqrestore(&il->sta_lock, flags);
3398 il4965_update_chain_flags(struct il_priv *il)
3400 if (il->cfg->ops->hcmd->set_rxon_chain) {
3401 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
3402 if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
3403 il_commit_rxon(il, &il->ctx);
3408 il4965_clear_free_frames(struct il_priv *il)
3410 struct list_head *element;
3412 D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
3414 while (!list_empty(&il->free_frames)) {
3415 element = il->free_frames.next;
3417 kfree(list_entry(element, struct il_frame, list));
3421 if (il->frames_count) {
3422 IL_WARN("%d frames still in use. Did we lose one?\n",
3424 il->frames_count = 0;
3428 static struct il_frame *
3429 il4965_get_free_frame(struct il_priv *il)
3431 struct il_frame *frame;
3432 struct list_head *element;
3433 if (list_empty(&il->free_frames)) {
3434 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3436 IL_ERR("Could not allocate frame!\n");
3444 element = il->free_frames.next;
3446 return list_entry(element, struct il_frame, list);
3450 il4965_free_frame(struct il_priv *il, struct il_frame *frame)
3452 memset(frame, 0, sizeof(*frame));
3453 list_add(&frame->list, &il->free_frames);
3457 il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
3460 lockdep_assert_held(&il->mutex);
3462 if (!il->beacon_skb)
3465 if (il->beacon_skb->len > left)
3468 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
3470 return il->beacon_skb->len;
3473 /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
3475 il4965_set_beacon_tim(struct il_priv *il,
3476 struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
3480 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3483 * The idx is relative to frame start but we start looking at the
3484 * variable-length part of the beacon.
3486 tim_idx = mgmt->u.beacon.variable - beacon;
3488 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3489 while ((tim_idx < (frame_size - 2)) &&
3490 (beacon[tim_idx] != WLAN_EID_TIM))
3491 tim_idx += beacon[tim_idx + 1] + 2;
3493 /* If TIM field was found, set variables */
3494 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3495 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
3496 tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
3498 IL_WARN("Unable to find TIM Element in beacon\n");
3502 il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
3504 struct il_tx_beacon_cmd *tx_beacon_cmd;
3509 * We have to set up the TX command, the TX Beacon command, and the
3513 lockdep_assert_held(&il->mutex);
3515 if (!il->beacon_ctx) {
3516 IL_ERR("trying to build beacon w/o beacon context!\n");
3520 /* Initialize memory */
3521 tx_beacon_cmd = &frame->u.beacon;
3522 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3524 /* Set up TX beacon contents */
3526 il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
3527 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
3528 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3533 /* Set up TX command fields */
3534 tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
3535 tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
3536 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3537 tx_beacon_cmd->tx.tx_flags =
3538 TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
3539 TX_CMD_FLG_STA_RATE_MSK;
3541 /* Set up TX beacon command fields */
3542 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
3545 /* Set up packet rate and flags */
3546 rate = il_get_lowest_plcp(il, il->beacon_ctx);
3548 il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
3549 il->hw_params.valid_tx_ant);
3550 rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
3551 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
3552 rate_flags |= RATE_MCS_CCK_MSK;
3553 tx_beacon_cmd->tx.rate_n_flags =
3554 il4965_hw_set_rate_n_flags(rate, rate_flags);
3556 return sizeof(*tx_beacon_cmd) + frame_size;
3560 il4965_send_beacon_cmd(struct il_priv *il)
3562 struct il_frame *frame;
3563 unsigned int frame_size;
3566 frame = il4965_get_free_frame(il);
3568 IL_ERR("Could not obtain free frame buffer for beacon "
3573 frame_size = il4965_hw_get_beacon_cmd(il, frame);
3575 IL_ERR("Error configuring the beacon command\n");
3576 il4965_free_frame(il, frame);
3580 rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
3582 il4965_free_frame(il, frame);
3587 static inline dma_addr_t
3588 il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
3590 struct il_tfd_tb *tb = &tfd->tbs[idx];
3592 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3593 if (sizeof(dma_addr_t) > sizeof(u32))
3595 ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
3602 il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
3604 struct il_tfd_tb *tb = &tfd->tbs[idx];
3606 return le16_to_cpu(tb->hi_n_len) >> 4;
3610 il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
3612 struct il_tfd_tb *tb = &tfd->tbs[idx];
3613 u16 hi_n_len = len << 4;
3615 put_unaligned_le32(addr, &tb->lo);
3616 if (sizeof(dma_addr_t) > sizeof(u32))
3617 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3619 tb->hi_n_len = cpu_to_le16(hi_n_len);
3621 tfd->num_tbs = idx + 1;
3625 il4965_tfd_get_num_tbs(struct il_tfd *tfd)
3627 return tfd->num_tbs & 0x1f;
3631 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
3632 * @il - driver ilate data
3635 * Does NOT advance any TFD circular buffer read/write idxes
3636 * Does NOT free the TFD itself (which is within circular buffer)
3639 il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
3641 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3643 struct pci_dev *dev = il->pci_dev;
3644 int idx = txq->q.read_ptr;
3648 tfd = &tfd_tmp[idx];
3650 /* Sanity check on number of chunks */
3651 num_tbs = il4965_tfd_get_num_tbs(tfd);
3653 if (num_tbs >= IL_NUM_OF_TBS) {
3654 IL_ERR("Too many chunks: %i\n", num_tbs);
3655 /* @todo issue fatal error, it is quite serious situation */
3661 pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
3662 dma_unmap_len(&txq->meta[idx], len),
3663 PCI_DMA_BIDIRECTIONAL);
3665 /* Unmap chunks, if any. */
3666 for (i = 1; i < num_tbs; i++)
3667 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
3668 il4965_tfd_tb_get_len(tfd, i),
3673 struct sk_buff *skb;
3675 skb = txq->txb[txq->q.read_ptr].skb;
3677 /* can be called from irqs-disabled context */
3679 dev_kfree_skb_any(skb);
3680 txq->txb[txq->q.read_ptr].skb = NULL;
3686 il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
3687 dma_addr_t addr, u16 len, u8 reset, u8 pad)
3690 struct il_tfd *tfd, *tfd_tmp;
3694 tfd_tmp = (struct il_tfd *)txq->tfds;
3695 tfd = &tfd_tmp[q->write_ptr];
3698 memset(tfd, 0, sizeof(*tfd));
3700 num_tbs = il4965_tfd_get_num_tbs(tfd);
3702 /* Each TFD can point to a maximum 20 Tx buffers */
3703 if (num_tbs >= IL_NUM_OF_TBS) {
3704 IL_ERR("Error can not send more than %d chunks\n",