blob: 1c3c85ec11176bbcd4afa03cca03ff5e30021dee [file] [log] [blame]
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
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.
11 *
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
15 * more details.
16 *
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
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
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>
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080043#include <linux/firmware.h>
44#include <linux/etherdevice.h>
45#include <linux/if_arp.h>
46
47#include <net/mac80211.h>
48
49#include <asm/div64.h>
50
51#define DRV_NAME "iwl4965"
52
Stanislaw Gruszka98613be2011-11-15 14:19:34 +010053#include "common.h"
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +020054#include "4965.h"
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080055
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080056/******************************************************************************
57 *
58 * module boiler plate
59 *
60 ******************************************************************************/
61
62/*
63 * module name, copyright, version, etc.
64 */
65#define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
66
Stanislaw Gruszkad3175162011-11-15 11:25:42 +010067#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080068#define VD "d"
69#else
70#define VD
71#endif
72
73#define DRV_VERSION IWLWIFI_VERSION VD
74
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080075MODULE_DESCRIPTION(DRV_DESCRIPTION);
76MODULE_VERSION(DRV_VERSION);
77MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
78MODULE_LICENSE("GPL");
79MODULE_ALIAS("iwl4965");
80
Stanislaw Gruszkae7392362011-11-15 14:45:59 +010081void
82il4965_check_abort_status(struct il_priv *il, u8 frame_count, u32 status)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +020083{
84 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
85 IL_ERR("Tx flush command to flush out all frames\n");
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +010086 if (!test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +020087 queue_work(il->workqueue, &il->tx_flush);
88 }
89}
90
91/*
92 * EEPROM
93 */
94struct il_mod_params il4965_mod_params = {
95 .amsdu_size_8K = 1,
96 .restart_fw = 1,
97 /* the rest are 0 by default */
98};
99
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100100void
101il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200102{
103 unsigned long flags;
104 int i;
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,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100114 PAGE_SIZE << il->hw_params.rx_page_order,
115 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200116 __il_free_pages(il, rxq->pool[i].page);
117 rxq->pool[i].page = NULL;
118 }
119 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
120 }
121
122 for (i = 0; i < RX_QUEUE_SIZE; i++)
123 rxq->queue[i] = NULL;
124
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;
129 rxq->free_count = 0;
130 spin_unlock_irqrestore(&rxq->lock, flags);
131}
132
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100133int
134il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200135{
136 u32 rb_size;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100137 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200138 u32 rb_timeout = 0;
139
140 if (il->cfg->mod_params->amsdu_size_8K)
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200141 rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200142 else
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200143 rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200144
145 /* Stop Rx DMA */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200146 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200147
148 /* Reset driver's Rx queue write idx */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200149 il_wr(il, FH49_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200150
151 /* Tell device where to find RBD circular buffer in DRAM */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100152 il_wr(il, FH49_RSCSR_CHNL0_RBDCB_BASE_REG, (u32) (rxq->bd_dma >> 8));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200153
154 /* Tell device where in DRAM to update its Rx status */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100155 il_wr(il, FH49_RSCSR_CHNL0_STTS_WPTR_REG, rxq->rb_stts_dma >> 4);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200156
157 /* Enable Rx DMA
158 * Direct rx interrupts to hosts
159 * Rx buffer size 4 or 8k
160 * RB timeout 0x10
161 * 256 RBDs
162 */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200163 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100164 FH49_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
165 FH49_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +0100166 FH49_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
167 rb_size |
168 (rb_timeout << FH49_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
169 (rfdnlog << FH49_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200170
171 /* Set interrupt coalescing timer to default (2048 usecs) */
172 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
173
174 return 0;
175}
176
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100177static void
178il4965_set_pwr_vmain(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200179{
180/*
181 * (for documentation purposes)
182 * to set power to V_AUX, do:
183
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);
188 */
189
190 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100191 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
192 ~APMG_PS_CTRL_MSK_PWR_SRC);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200193}
194
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100195int
196il4965_hw_nic_init(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200197{
198 unsigned long flags;
199 struct il_rx_queue *rxq = &il->rxq;
200 int ret;
201
202 /* nic_init */
203 spin_lock_irqsave(&il->lock, flags);
204 il->cfg->ops->lib->apm_ops.init(il);
205
206 /* Set interrupt coalescing calibration timer to default (512 usecs) */
207 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
208
209 spin_unlock_irqrestore(&il->lock, flags);
210
211 il4965_set_pwr_vmain(il);
212
213 il->cfg->ops->lib->apm_ops.config(il);
214
215 /* Allocate the RX queue, or reset if it is already allocated */
216 if (!rxq->bd) {
217 ret = il_rx_queue_alloc(il);
218 if (ret) {
219 IL_ERR("Unable to initialize Rx queue\n");
220 return -ENOMEM;
221 }
222 } else
223 il4965_rx_queue_reset(il, rxq);
224
225 il4965_rx_replenish(il);
226
227 il4965_rx_init(il, rxq);
228
229 spin_lock_irqsave(&il->lock, flags);
230
231 rxq->need_update = 1;
232 il_rx_queue_update_write_ptr(il, rxq);
233
234 spin_unlock_irqrestore(&il->lock, flags);
235
236 /* Allocate or reset and init all Tx and Command queues */
237 if (!il->txq) {
238 ret = il4965_txq_ctx_alloc(il);
239 if (ret)
240 return ret;
241 } else
242 il4965_txq_ctx_reset(il);
243
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100244 set_bit(S_INIT, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200245
246 return 0;
247}
248
249/**
250 * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
251 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100252static inline __le32
253il4965_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200254{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100255 return cpu_to_le32((u32) (dma_addr >> 8));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200256}
257
258/**
259 * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
260 *
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.
264 *
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
267 * target buffer.
268 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100269void
270il4965_rx_queue_restock(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200271{
272 struct il_rx_queue *rxq = &il->rxq;
273 struct list_head *element;
274 struct il_rx_buf *rxb;
275 unsigned long flags;
276
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);
282
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);
286 list_del(element);
287
288 /* Point to Rx buffer via next RBD in circular buffer */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100289 rxq->bd[rxq->write] =
290 il4965_dma_addr2rbd_ptr(il, rxb->page_dma);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200291 rxq->queue[rxq->write] = rxb;
292 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
293 rxq->free_count--;
294 }
295 spin_unlock_irqrestore(&rxq->lock, flags);
296 /* If the pre-allocated buffer pool is dropping low, schedule to
297 * refill it */
298 if (rxq->free_count <= RX_LOW_WATERMARK)
299 queue_work(il->workqueue, &il->rx_replenish);
300
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200301 /* 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);
308 }
309}
310
311/**
312 * il4965_rx_replenish - Move all used packet from rx_used to rx_free
313 *
314 * When moving to rx_free an SKB is allocated for the slot.
315 *
316 * Also restock the Rx queue via il_rx_queue_restock.
317 * This is called as a scheduled work item (except for during initialization)
318 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100319static void
320il4965_rx_allocate(struct il_priv *il, gfp_t priority)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200321{
322 struct il_rx_queue *rxq = &il->rxq;
323 struct list_head *element;
324 struct il_rx_buf *rxb;
325 struct page *page;
326 unsigned long flags;
327 gfp_t gfp_mask = priority;
328
329 while (1) {
330 spin_lock_irqsave(&rxq->lock, flags);
331 if (list_empty(&rxq->rx_used)) {
332 spin_unlock_irqrestore(&rxq->lock, flags);
333 return;
334 }
335 spin_unlock_irqrestore(&rxq->lock, flags);
336
337 if (rxq->free_count > RX_LOW_WATERMARK)
338 gfp_mask |= __GFP_NOWARN;
339
340 if (il->hw_params.rx_page_order > 0)
341 gfp_mask |= __GFP_COMP;
342
343 /* Alloc a new receive buffer */
344 page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
345 if (!page) {
346 if (net_ratelimit())
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100347 D_INFO("alloc_pages failed, " "order: %d\n",
348 il->hw_params.rx_page_order);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200349
350 if (rxq->free_count <= RX_LOW_WATERMARK &&
351 net_ratelimit())
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100352 IL_ERR("Failed to alloc_pages with %s. "
353 "Only %u free buffers remaining.\n",
354 priority ==
355 GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
356 rxq->free_count);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200357 /* 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 */
360 return;
361 }
362
363 spin_lock_irqsave(&rxq->lock, flags);
364
365 if (list_empty(&rxq->rx_used)) {
366 spin_unlock_irqrestore(&rxq->lock, flags);
367 __free_pages(page, il->hw_params.rx_page_order);
368 return;
369 }
370 element = rxq->rx_used.next;
371 rxb = list_entry(element, struct il_rx_buf, list);
372 list_del(element);
373
374 spin_unlock_irqrestore(&rxq->lock, flags);
375
376 BUG_ON(rxb->page);
377 rxb->page = page;
378 /* Get physical address of the RB */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100379 rxb->page_dma =
380 pci_map_page(il->pci_dev, page, 0,
381 PAGE_SIZE << il->hw_params.rx_page_order,
382 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200383 /* 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));
387
388 spin_lock_irqsave(&rxq->lock, flags);
389
390 list_add_tail(&rxb->list, &rxq->rx_free);
391 rxq->free_count++;
392 il->alloc_rxb_page++;
393
394 spin_unlock_irqrestore(&rxq->lock, flags);
395 }
396}
397
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100398void
399il4965_rx_replenish(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200400{
401 unsigned long flags;
402
403 il4965_rx_allocate(il, GFP_KERNEL);
404
405 spin_lock_irqsave(&il->lock, flags);
406 il4965_rx_queue_restock(il);
407 spin_unlock_irqrestore(&il->lock, flags);
408}
409
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100410void
411il4965_rx_replenish_now(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200412{
413 il4965_rx_allocate(il, GFP_ATOMIC);
414
415 il4965_rx_queue_restock(il);
416}
417
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
422 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100423void
424il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200425{
426 int i;
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,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100430 PAGE_SIZE << il->hw_params.rx_page_order,
431 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200432 __il_free_pages(il, rxq->pool[i].page);
433 rxq->pool[i].page = NULL;
434 }
435 }
436
437 dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
438 rxq->bd_dma);
439 dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
440 rxq->rb_stts, rxq->rb_stts_dma);
441 rxq->bd = NULL;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100442 rxq->rb_stts = NULL;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200443}
444
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100445int
446il4965_rxq_stop(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200447{
448
449 /* stop Rx DMA */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200450 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
451 il_poll_bit(il, FH49_MEM_RSSR_RX_STATUS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100452 FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200453
454 return 0;
455}
456
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100457int
458il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200459{
460 int idx = 0;
461 int band_offset = 0;
462
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);
466 return idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100467 /* Legacy rate format, search for match in table */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200468 } else {
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;
474 }
475
476 return -1;
477}
478
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100479static int
480il4965_calc_rssi(struct il_priv *il, struct il_rx_phy_res *rx_resp)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200481{
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;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100486 u32 agc =
487 (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK) >>
488 IL49_AGC_DB_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200489
490 u32 valid_antennae =
491 (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100492 >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200493 u8 max_rssi = 0;
494 u32 i;
495
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);
504
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],
507 max_rssi, agc);
508
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;
512}
513
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100514static u32
515il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200516{
517 u32 decrypt_out = 0;
518
519 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100520 RX_RES_STATUS_STATION_FOUND)
521 decrypt_out |=
522 (RX_RES_STATUS_STATION_FOUND |
523 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200524
525 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
526
527 /* packet was not encrypted */
528 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100529 RX_RES_STATUS_SEC_TYPE_NONE)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200530 return decrypt_out;
531
532 /* packet was encrypted with unknown alg */
533 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100534 RX_RES_STATUS_SEC_TYPE_ERR)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200535 return decrypt_out;
536
537 /* decryption was not done in HW */
538 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100539 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200540 return decrypt_out;
541
542 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
543
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))
547 /* Bad MIC */
548 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
549 else
550 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
551
552 break;
553
554 case RX_RES_STATUS_SEC_TYPE_TKIP:
555 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
556 /* Bad TTAK */
557 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
558 break;
559 }
560 /* fall through if TTAK OK */
561 default:
562 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
563 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
564 else
565 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
566 break;
567 }
568
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100569 D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n", decrypt_in, decrypt_out);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200570
571 return decrypt_out;
572}
573
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100574static void
575il4965_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)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200578{
579 struct sk_buff *skb;
580 __le16 fc = hdr->frame_control;
581
582 /* We only process data packets if the interface is open */
583 if (unlikely(!il->is_open)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100584 D_DROP("Dropping packet while interface is not open.\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200585 return;
586 }
587
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))
591 return;
592
593 skb = dev_alloc_skb(128);
594 if (!skb) {
595 IL_ERR("dev_alloc_skb failed\n");
596 return;
597 }
598
599 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
600
601 il_update_stats(il, false, fc, len);
602 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
603
604 ieee80211_rx(il->hw, skb);
605 il->alloc_rxb_page--;
606 rxb->page = NULL;
607}
608
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200609/* Called for N_RX (legacy ABG frames), or
610 * N_RX_MPDU (HT high-throughput N frames). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100611void
612il4965_hdl_rx(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200613{
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;
620 u32 len;
621 u32 ampdu_status;
622 u32 rate_n_flags;
623
624 /**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200625 * 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
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200628 * command and cached in il->last_phy_res
629 *
630 * Here we set up local variables depending on which command is
631 * received.
632 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200633 if (pkt->hdr.cmd == N_RX) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200634 phy_res = (struct il_rx_phy_res *)pkt->u.raw;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100635 header =
636 (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res) +
637 phy_res->cfg_phy_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200638
639 len = le16_to_cpu(phy_res->byte_count);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100640 rx_pkt_status =
641 *(__le32 *) (pkt->u.raw + sizeof(*phy_res) +
642 phy_res->cfg_phy_cnt + len);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200643 ampdu_status = le32_to_cpu(rx_pkt_status);
644 } else {
645 if (!il->_4965.last_phy_res_valid) {
646 IL_ERR("MPDU frame without cached PHY data\n");
647 return;
648 }
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);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100653 rx_pkt_status = *(__le32 *) (pkt->u.raw + sizeof(*amsdu) + len);
654 ampdu_status =
655 il4965_translate_rx_status(il, le32_to_cpu(rx_pkt_status));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200656 }
657
658 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
659 D_DROP("dsp size out of range [0,20]: %d/n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100660 phy_res->cfg_phy_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200661 return;
662 }
663
664 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
665 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100666 D_RX("Bad CRC or FIFO: 0x%08X.\n", le32_to_cpu(rx_pkt_status));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200667 return;
668 }
669
670 /* This will be used in several places later */
671 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
672
673 /* rx_status carries information about the packet to mac80211 */
674 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100675 rx_status.band =
676 (phy_res->
677 phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ? IEEE80211_BAND_2GHZ :
678 IEEE80211_BAND_5GHZ;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200679 rx_status.freq =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100680 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
681 rx_status.band);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200682 rx_status.rate_idx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100683 il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200684 rx_status.flag = 0;
685
686 /* TSF isn't reliable. In order to allow smooth user experience,
687 * this W/A doesn't propagate it to the mac80211 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100688 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU; */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200689
690 il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
691
692 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
693 rx_status.signal = il4965_calc_rssi(il, phy_res);
694
695 il_dbg_log_rx_data_frame(il, len, header);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100696 D_STATS("Rssi %d, TSF %llu\n", rx_status.signal,
697 (unsigned long long)rx_status.mactime);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200698
699 /*
700 * "antenna number"
701 *
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.
707 *
708 * I think this field should be removed in favor of a
709 * new 802.11n radiotap field "RX chains" that is defined
710 * as a bitmask.
711 */
712 rx_status.antenna =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100713 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK) >>
714 RX_RES_PHY_FLAGS_ANTENNA_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200715
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;
719
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;
727
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100728 il4965_pass_packet_to_mac80211(il, header, len, ampdu_status, rxb,
729 &rx_status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200730}
731
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200732/* Cache phy data (Rx signal strength, etc) for HT frame (N_RX_PHY).
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +0200733 * This will be used later in il_hdl_rx() for N_RX_MPDU. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100734void
735il4965_hdl_rx_phy(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200736{
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));
741}
742
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100743static int
744il4965_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)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200747{
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;
753 int added, i;
754 u16 channel;
755
756 sband = il_get_hw_mode(il, band);
757 if (!sband)
758 return 0;
759
760 active_dwell = il_get_active_dwell_time(il, band, n_probes);
761 passive_dwell = il_get_passive_dwell_time(il, band, vif);
762
763 if (passive_dwell <= active_dwell)
764 passive_dwell = active_dwell + 1;
765
766 for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
767 chan = il->scan_request->channels[i];
768
769 if (chan->band != band)
770 continue;
771
772 channel = chan->hw_value;
773 scan_ch->channel = cpu_to_le16(channel);
774
775 ch_info = il_get_channel_info(il, band, channel);
776 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100777 D_SCAN("Channel %d is INVALID for this band.\n",
778 channel);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200779 continue;
780 }
781
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;
785 else
786 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
787
788 if (n_probes)
789 scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
790
791 scan_ch->active_dwell = cpu_to_le16(active_dwell);
792 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
793
794 /* Set txpower levels to defaults */
795 scan_ch->dsp_atten = 110;
796
797 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
798 * power level:
799 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
800 */
801 if (band == IEEE80211_BAND_5GHZ)
802 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
803 else
804 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
805
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100806 D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", channel,
807 le32_to_cpu(scan_ch->type),
808 (scan_ch->
809 type & SCAN_CHANNEL_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
810 (scan_ch->
811 type & SCAN_CHANNEL_TYPE_ACTIVE) ? active_dwell :
812 passive_dwell);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200813
814 scan_ch++;
815 added++;
816 }
817
818 D_SCAN("total channels to scan %d\n", added);
819 return added;
820}
821
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +0100822static void
823il4965_toggle_tx_ant(struct il_priv *il, u8 *ant, u8 valid)
824{
825 int i;
826 u8 ind = *ant;
827
828 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
829 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
830 if (valid & BIT(ind)) {
831 *ant = ind;
832 return;
833 }
834 }
835}
836
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100837int
838il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200839{
840 struct il_host_cmd cmd = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200841 .id = C_SCAN,
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200842 .len = sizeof(struct il_scan_cmd),
843 .flags = CMD_SIZE_HUGE,
844 };
845 struct il_scan_cmd *scan;
846 struct il_rxon_context *ctx = &il->ctx;
847 u32 rate_flags = 0;
848 u16 cmd_len;
849 u16 rx_chain = 0;
850 enum ieee80211_band band;
851 u8 n_probes = 0;
852 u8 rx_ant = il->hw_params.valid_rx_ant;
853 u8 rate;
854 bool is_active = false;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100855 int chan_mod;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200856 u8 active_chains;
857 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
858 int ret;
859
860 lockdep_assert_held(&il->mutex);
861
Greg Dietsche730b4c22011-09-06 17:33:51 -0500862 ctx = il_rxon_ctx_from_vif(vif);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200863
864 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100865 il->scan_cmd =
866 kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
867 GFP_KERNEL);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200868 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100869 D_SCAN("fail to allocate memory for scan\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200870 return -ENOMEM;
871 }
872 }
873 scan = il->scan_cmd;
874 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
875
876 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
877 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
878
879 if (il_is_any_associated(il)) {
880 u16 interval;
881 u32 extra;
882 u32 suspend_time = 100;
883 u32 scan_suspend_time = 100;
884
885 D_INFO("Scanning while associated...\n");
886 interval = vif->bss_conf.beacon_int;
887
888 scan->suspend_time = 0;
889 scan->max_out_time = cpu_to_le32(200 * 1024);
890 if (!interval)
891 interval = suspend_time;
892
893 extra = (suspend_time / interval) << 22;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100894 scan_suspend_time =
895 (extra | ((suspend_time % interval) * 1024));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200896 scan->suspend_time = cpu_to_le32(scan_suspend_time);
897 D_SCAN("suspend_time 0x%X beacon interval %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100898 scan_suspend_time, interval);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200899 }
900
901 if (il->scan_request->n_ssids) {
902 int i, p = 0;
903 D_SCAN("Kicking off active scan\n");
904 for (i = 0; i < il->scan_request->n_ssids; i++) {
905 /* always does wildcard anyway */
906 if (!il->scan_request->ssids[i].ssid_len)
907 continue;
908 scan->direct_scan[p].id = WLAN_EID_SSID;
909 scan->direct_scan[p].len =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100910 il->scan_request->ssids[i].ssid_len;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200911 memcpy(scan->direct_scan[p].ssid,
912 il->scan_request->ssids[i].ssid,
913 il->scan_request->ssids[i].ssid_len);
914 n_probes++;
915 p++;
916 }
917 is_active = true;
918 } else
919 D_SCAN("Start passive scan.\n");
920
921 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Stanislaw Gruszkab16db502012-02-03 17:31:44 +0100922 scan->tx_cmd.sta_id = il->hw_params.bcast_id;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200923 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
924
925 switch (il->scan_band) {
926 case IEEE80211_BAND_2GHZ:
927 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100928 chan_mod =
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +0100929 le32_to_cpu(il->active.flags & RXON_FLG_CHANNEL_MODE_MSK) >>
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100930 RXON_FLG_CHANNEL_MODE_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200931 if (chan_mod == CHANNEL_MODE_PURE_40) {
932 rate = RATE_6M_PLCP;
933 } else {
934 rate = RATE_1M_PLCP;
935 rate_flags = RATE_MCS_CCK_MSK;
936 }
937 break;
938 case IEEE80211_BAND_5GHZ:
939 rate = RATE_6M_PLCP;
940 break;
941 default:
942 IL_WARN("Invalid scan band\n");
943 return -EIO;
944 }
945
946 /*
947 * If active scanning is requested but a certain channel is
948 * marked passive, we can do active scanning if we detect
949 * transmissions.
950 *
951 * There is an issue with some firmware versions that triggers
952 * a sysassert on a "good CRC threshold" of zero (== disabled),
953 * on a radar channel even though this means that we should NOT
954 * send probes.
955 *
956 * The "good CRC threshold" is the number of frames that we
957 * need to receive during our dwell time on a channel before
958 * sending out probes -- setting this to a huge value will
959 * mean we never reach it, but at the same time work around
960 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
961 * here instead of IL_GOOD_CRC_TH_DISABLED.
962 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100963 scan->good_CRC_th =
964 is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200965
966 band = il->scan_band;
967
968 if (il->cfg->scan_rx_antennas[band])
969 rx_ant = il->cfg->scan_rx_antennas[band];
970
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +0100971 il4965_toggle_tx_ant(il, &il->scan_tx_ant[band], scan_tx_antennas);
Stanislaw Gruszka616107e2011-12-23 08:13:45 +0100972 rate_flags |= BIT(il->scan_tx_ant[band]) << RATE_MCS_ANT_POS;
973 scan->tx_cmd.rate_n_flags = cpu_to_le32(rate | rate_flags);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200974
975 /* In power save mode use one chain, otherwise use all chains */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100976 if (test_bit(S_POWER_PMI, &il->status)) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200977 /* rx_ant has been set to all valid chains previously */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100978 active_chains =
979 rx_ant & ((u8) (il->chain_noise_data.active_chains));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200980 if (!active_chains)
981 active_chains = rx_ant;
982
983 D_SCAN("chain_noise_data.active_chains: %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100984 il->chain_noise_data.active_chains);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200985
986 rx_ant = il4965_first_antenna(active_chains);
987 }
988
989 /* MIMO is not used here, but value is required */
990 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
991 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
992 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
993 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
994 scan->rx_chain = cpu_to_le16(rx_chain);
995
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100996 cmd_len =
997 il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
998 vif->addr, il->scan_request->ie,
999 il->scan_request->ie_len,
1000 IL_MAX_SCAN_SIZE - sizeof(*scan));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001001 scan->tx_cmd.len = cpu_to_le16(cmd_len);
1002
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001003 scan->filter_flags |=
1004 (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001005
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001006 scan->channel_count =
1007 il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
1008 (void *)&scan->data[cmd_len]);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001009 if (scan->channel_count == 0) {
1010 D_SCAN("channel count %d\n", scan->channel_count);
1011 return -EIO;
1012 }
1013
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001014 cmd.len +=
1015 le16_to_cpu(scan->tx_cmd.len) +
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001016 scan->channel_count * sizeof(struct il_scan_channel);
1017 cmd.data = scan;
1018 scan->len = cpu_to_le16(cmd.len);
1019
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001020 set_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001021
1022 ret = il_send_cmd_sync(il, &cmd);
1023 if (ret)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001024 clear_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001025
1026 return ret;
1027}
1028
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001029int
1030il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
1031 bool add)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001032{
1033 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1034
1035 if (add)
1036 return il4965_add_bssid_station(il, vif_priv->ctx,
1037 vif->bss_conf.bssid,
1038 &vif_priv->ibss_bssid_sta_id);
1039 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001040 vif->bss_conf.bssid);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001041}
1042
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001043void
1044il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001045{
1046 lockdep_assert_held(&il->sta_lock);
1047
1048 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1049 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1050 else {
1051 D_TX("free more than tfds_in_queue (%u:%d)\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001052 il->stations[sta_id].tid[tid].tfds_in_queue, freed);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001053 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1054 }
1055}
1056
1057#define IL_TX_QUEUE_MSK 0xfffff
1058
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001059static bool
1060il4965_is_single_rx_stream(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001061{
1062 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001063 il->current_ht_config.single_chain_sufficient;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001064}
1065
1066#define IL_NUM_RX_CHAINS_MULTIPLE 3
1067#define IL_NUM_RX_CHAINS_SINGLE 2
1068#define IL_NUM_IDLE_CHAINS_DUAL 2
1069#define IL_NUM_IDLE_CHAINS_SINGLE 1
1070
1071/*
1072 * Determine how many receiver/antenna chains to use.
1073 *
1074 * More provides better reception via diversity. Fewer saves power
1075 * at the expense of throughput, but only when not in powersave to
1076 * start with.
1077 *
1078 * MIMO (dual stream) requires at least 2, but works better with 3.
1079 * This does not determine *which* chains to use, just how many.
1080 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001081static int
1082il4965_get_active_rx_chain_count(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001083{
1084 /* # of Rx chains to use when expecting MIMO. */
1085 if (il4965_is_single_rx_stream(il))
1086 return IL_NUM_RX_CHAINS_SINGLE;
1087 else
1088 return IL_NUM_RX_CHAINS_MULTIPLE;
1089}
1090
1091/*
1092 * When we are in power saving mode, unless device support spatial
1093 * multiplexing power save, use the active count for rx chain count.
1094 */
1095static int
1096il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1097{
1098 /* # Rx chains when idling, depending on SMPS mode */
1099 switch (il->current_ht_config.smps) {
1100 case IEEE80211_SMPS_STATIC:
1101 case IEEE80211_SMPS_DYNAMIC:
1102 return IL_NUM_IDLE_CHAINS_SINGLE;
1103 case IEEE80211_SMPS_OFF:
1104 return active_cnt;
1105 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001106 WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001107 return active_cnt;
1108 }
1109}
1110
1111/* up to 4 chains */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001112static u8
1113il4965_count_chain_bitmap(u32 chain_bitmap)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001114{
1115 u8 res;
1116 res = (chain_bitmap & BIT(0)) >> 0;
1117 res += (chain_bitmap & BIT(1)) >> 1;
1118 res += (chain_bitmap & BIT(2)) >> 2;
1119 res += (chain_bitmap & BIT(3)) >> 3;
1120 return res;
1121}
1122
1123/**
1124 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1125 *
1126 * Selects how many and which Rx receivers/antennas/chains to use.
1127 * This should not be used for scan command ... it puts data in wrong place.
1128 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001129void
1130il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001131{
1132 bool is_single = il4965_is_single_rx_stream(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001133 bool is_cam = !test_bit(S_POWER_PMI, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001134 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1135 u32 active_chains;
1136 u16 rx_chain;
1137
1138 /* Tell uCode which antennas are actually connected.
1139 * Before first association, we assume all antennas are connected.
1140 * Just after first association, il4965_chain_noise_calibration()
1141 * checks which antennas actually *are* connected. */
1142 if (il->chain_noise_data.active_chains)
1143 active_chains = il->chain_noise_data.active_chains;
1144 else
1145 active_chains = il->hw_params.valid_rx_ant;
1146
1147 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1148
1149 /* How many receivers should we use? */
1150 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1151 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1152
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001153 /* correct rx chain count according hw settings
1154 * and chain noise calibration
1155 */
1156 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1157 if (valid_rx_cnt < active_rx_cnt)
1158 active_rx_cnt = valid_rx_cnt;
1159
1160 if (valid_rx_cnt < idle_rx_cnt)
1161 idle_rx_cnt = valid_rx_cnt;
1162
1163 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001164 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001165
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001166 il->staging.rx_chain = cpu_to_le16(rx_chain);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001167
1168 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001169 il->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001170 else
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001171 il->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001172
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001173 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", il->staging.rx_chain,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001174 active_rx_cnt, idle_rx_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001175
1176 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1177 active_rx_cnt < idle_rx_cnt);
1178}
1179
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001180static const char *
1181il4965_get_fh_string(int cmd)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001182{
1183 switch (cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001184 IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
1185 IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
1186 IL_CMD(FH49_RSCSR_CHNL0_WPTR);
1187 IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
1188 IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
1189 IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
1190 IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
1191 IL_CMD(FH49_TSSR_TX_STATUS_REG);
1192 IL_CMD(FH49_TSSR_TX_ERROR_REG);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001193 default:
1194 return "UNKNOWN";
1195 }
1196}
1197
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001198int
1199il4965_dump_fh(struct il_priv *il, char **buf, bool display)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001200{
1201 int i;
1202#ifdef CONFIG_IWLEGACY_DEBUG
1203 int pos = 0;
1204 size_t bufsz = 0;
1205#endif
1206 static const u32 fh_tbl[] = {
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02001207 FH49_RSCSR_CHNL0_STTS_WPTR_REG,
1208 FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
1209 FH49_RSCSR_CHNL0_WPTR,
1210 FH49_MEM_RCSR_CHNL0_CONFIG_REG,
1211 FH49_MEM_RSSR_SHARED_CTRL_REG,
1212 FH49_MEM_RSSR_RX_STATUS_REG,
1213 FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
1214 FH49_TSSR_TX_STATUS_REG,
1215 FH49_TSSR_TX_ERROR_REG
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001216 };
1217#ifdef CONFIG_IWLEGACY_DEBUG
1218 if (display) {
1219 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
1220 *buf = kmalloc(bufsz, GFP_KERNEL);
1221 if (!*buf)
1222 return -ENOMEM;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001223 pos +=
1224 scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001225 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001226 pos +=
1227 scnprintf(*buf + pos, bufsz - pos,
1228 " %34s: 0X%08x\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001229 il4965_get_fh_string(fh_tbl[i]),
1230 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001231 }
1232 return pos;
1233 }
1234#endif
1235 IL_ERR("FH register values:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001236 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1237 IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
1238 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001239 }
1240 return 0;
1241}
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001242
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001243void
1244il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001245{
1246 struct il_rx_pkt *pkt = rxb_addr(rxb);
1247 struct il_missed_beacon_notif *missed_beacon;
1248
1249 missed_beacon = &pkt->u.missed_beacon;
1250 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
1251 il->missed_beacon_threshold) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001252 D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
1253 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
1254 le32_to_cpu(missed_beacon->total_missed_becons),
1255 le32_to_cpu(missed_beacon->num_recvd_beacons),
1256 le32_to_cpu(missed_beacon->num_expected_beacons));
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001257 if (!test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001258 il4965_init_sensitivity(il);
1259 }
1260}
1261
1262/* Calculate noise level, based on measurements during network silence just
1263 * before arriving beacon. This measurement can be done only if we know
1264 * exactly when to expect beacons, therefore only when we're associated. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001265static void
1266il4965_rx_calc_noise(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001267{
1268 struct stats_rx_non_phy *rx_info;
1269 int num_active_rx = 0;
1270 int total_silence = 0;
1271 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
1272 int last_rx_noise;
1273
1274 rx_info = &(il->_4965.stats.rx.general);
1275 bcn_silence_a =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001276 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001277 bcn_silence_b =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001278 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001279 bcn_silence_c =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001280 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001281
1282 if (bcn_silence_a) {
1283 total_silence += bcn_silence_a;
1284 num_active_rx++;
1285 }
1286 if (bcn_silence_b) {
1287 total_silence += bcn_silence_b;
1288 num_active_rx++;
1289 }
1290 if (bcn_silence_c) {
1291 total_silence += bcn_silence_c;
1292 num_active_rx++;
1293 }
1294
1295 /* Average among active antennas */
1296 if (num_active_rx)
1297 last_rx_noise = (total_silence / num_active_rx) - 107;
1298 else
1299 last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
1300
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001301 D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
1302 bcn_silence_b, bcn_silence_c, last_rx_noise);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001303}
1304
1305#ifdef CONFIG_IWLEGACY_DEBUGFS
1306/*
1307 * based on the assumption of all stats counter are in DWORD
1308 * FIXME: This function is for debugging, do not deal with
1309 * the case of counters roll-over.
1310 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001311static void
1312il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001313{
1314 int i, size;
1315 __le32 *prev_stats;
1316 u32 *accum_stats;
1317 u32 *delta, *max_delta;
1318 struct stats_general_common *general, *accum_general;
1319 struct stats_tx *tx, *accum_tx;
1320
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001321 prev_stats = (__le32 *) &il->_4965.stats;
1322 accum_stats = (u32 *) &il->_4965.accum_stats;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001323 size = sizeof(struct il_notif_stats);
1324 general = &il->_4965.stats.general.common;
1325 accum_general = &il->_4965.accum_stats.general.common;
1326 tx = &il->_4965.stats.tx;
1327 accum_tx = &il->_4965.accum_stats.tx;
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001328 delta = (u32 *) &il->_4965.delta_stats;
1329 max_delta = (u32 *) &il->_4965.max_delta;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001330
1331 for (i = sizeof(__le32); i < size;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001332 i +=
1333 sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
1334 accum_stats++) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001335 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001336 *delta =
1337 (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001338 *accum_stats += *delta;
1339 if (*delta > *max_delta)
1340 *max_delta = *delta;
1341 }
1342 }
1343
1344 /* reset accumulative stats for "no-counter" type stats */
1345 accum_general->temperature = general->temperature;
1346 accum_general->ttl_timestamp = general->ttl_timestamp;
1347}
1348#endif
1349
1350#define REG_RECALIB_PERIOD (60)
1351
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001352void
1353il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001354{
1355 int change;
1356 struct il_rx_pkt *pkt = rxb_addr(rxb);
1357
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001358 D_RX("Statistics notification received (%d vs %d).\n",
1359 (int)sizeof(struct il_notif_stats),
1360 le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001361
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001362 change =
1363 ((il->_4965.stats.general.common.temperature !=
1364 pkt->u.stats.general.common.temperature) ||
1365 ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
1366 (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001367#ifdef CONFIG_IWLEGACY_DEBUGFS
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001368 il4965_accumulative_stats(il, (__le32 *) &pkt->u.stats);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001369#endif
1370
1371 /* TODO: reading some of stats is unneeded */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001372 memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001373
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001374 set_bit(S_STATS, &il->status);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001375
1376 /* Reschedule the stats timer to occur in
1377 * REG_RECALIB_PERIOD seconds to ensure we get a
1378 * thermal update even if the uCode doesn't give
1379 * us one */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001380 mod_timer(&il->stats_periodic,
1381 jiffies + msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001382
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001383 if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001384 (pkt->hdr.cmd == N_STATS)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001385 il4965_rx_calc_noise(il);
1386 queue_work(il->workqueue, &il->run_time_calib_work);
1387 }
1388 if (il->cfg->ops->lib->temp_ops.temperature && change)
1389 il->cfg->ops->lib->temp_ops.temperature(il);
1390}
1391
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001392void
1393il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001394{
1395 struct il_rx_pkt *pkt = rxb_addr(rxb);
1396
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001397 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001398#ifdef CONFIG_IWLEGACY_DEBUGFS
1399 memset(&il->_4965.accum_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001400 sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001401 memset(&il->_4965.delta_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001402 sizeof(struct il_notif_stats));
1403 memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001404#endif
1405 D_RX("Statistics have been cleared\n");
1406 }
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01001407 il4965_hdl_stats(il, rxb);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001408}
1409
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001410
1411/*
1412 * mac80211 queues, ACs, hardware queues, FIFOs.
1413 *
1414 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
1415 *
1416 * Mac80211 uses the following numbers, which we get as from it
1417 * by way of skb_get_queue_mapping(skb):
1418 *
1419 * VO 0
1420 * VI 1
1421 * BE 2
1422 * BK 3
1423 *
1424 *
1425 * Regular (not A-MPDU) frames are put into hardware queues corresponding
1426 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
1427 * own queue per aggregation session (RA/TID combination), such queues are
1428 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
1429 * order to map frames to the right queue, we also need an AC->hw queue
1430 * mapping. This is implemented here.
1431 *
1432 * Due to the way hw queues are set up (by the hw specific modules like
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +02001433 * 4965.c), the AC->hw queue mapping is the identity
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001434 * mapping.
1435 */
1436
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001437static const u8 tid_to_ac[] = {
1438 IEEE80211_AC_BE,
1439 IEEE80211_AC_BK,
1440 IEEE80211_AC_BK,
1441 IEEE80211_AC_BE,
1442 IEEE80211_AC_VI,
1443 IEEE80211_AC_VI,
1444 IEEE80211_AC_VO,
1445 IEEE80211_AC_VO
1446};
1447
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001448static inline int
1449il4965_get_ac_from_tid(u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001450{
1451 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1452 return tid_to_ac[tid];
1453
1454 /* no support for TIDs 8-15 yet */
1455 return -EINVAL;
1456}
1457
1458static inline int
1459il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
1460{
Stanislaw Gruszkab75b3a72012-02-03 17:31:53 +01001461 const u8 ac_to_fifo[] = {
1462 IL_TX_FIFO_VO,
1463 IL_TX_FIFO_VI,
1464 IL_TX_FIFO_BE,
1465 IL_TX_FIFO_BK,
1466 };
1467
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001468 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
Stanislaw Gruszkab75b3a72012-02-03 17:31:53 +01001469 return ac_to_fifo[tid_to_ac[tid]];
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001470
1471 /* no support for TIDs 8-15 yet */
1472 return -EINVAL;
1473}
1474
1475/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001476 * handle build C_TX command notification.
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001477 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001478static void
1479il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
1480 struct il_tx_cmd *tx_cmd,
1481 struct ieee80211_tx_info *info,
1482 struct ieee80211_hdr *hdr, u8 std_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001483{
1484 __le16 fc = hdr->frame_control;
1485 __le32 tx_flags = tx_cmd->tx_flags;
1486
1487 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1488 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
1489 tx_flags |= TX_CMD_FLG_ACK_MSK;
1490 if (ieee80211_is_mgmt(fc))
1491 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1492 if (ieee80211_is_probe_resp(fc) &&
1493 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1494 tx_flags |= TX_CMD_FLG_TSF_MSK;
1495 } else {
1496 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1497 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1498 }
1499
1500 if (ieee80211_is_back_req(fc))
1501 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1502
1503 tx_cmd->sta_id = std_id;
1504 if (ieee80211_has_morefrags(fc))
1505 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1506
1507 if (ieee80211_is_data_qos(fc)) {
1508 u8 *qc = ieee80211_get_qos_ctl(hdr);
1509 tx_cmd->tid_tspec = qc[0] & 0xf;
1510 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1511 } else {
1512 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1513 }
1514
1515 il_tx_cmd_protection(il, info, fc, &tx_flags);
1516
1517 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1518 if (ieee80211_is_mgmt(fc)) {
1519 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
1520 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
1521 else
1522 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
1523 } else {
1524 tx_cmd->timeout.pm_frame_timeout = 0;
1525 }
1526
1527 tx_cmd->driver_txop = 0;
1528 tx_cmd->tx_flags = tx_flags;
1529 tx_cmd->next_frame_len = 0;
1530}
1531
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001532static void
1533il4965_tx_cmd_build_rate(struct il_priv *il, struct il_tx_cmd *tx_cmd,
1534 struct ieee80211_tx_info *info, __le16 fc)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001535{
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001536 const u8 rts_retry_limit = 60;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001537 u32 rate_flags;
1538 int rate_idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001539 u8 data_retry_limit;
1540 u8 rate_plcp;
1541
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001542 /* Set retry limit on DATA packets and Probe Responses */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001543 if (ieee80211_is_probe_resp(fc))
1544 data_retry_limit = 3;
1545 else
1546 data_retry_limit = IL4965_DEFAULT_TX_RETRY;
1547 tx_cmd->data_retry_limit = data_retry_limit;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001548 /* Set retry limit on RTS packets */
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001549 tx_cmd->rts_retry_limit = min(data_retry_limit, rts_retry_limit);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001550
1551 /* DATA packets will use the uCode station table for rate/antenna
1552 * selection */
1553 if (ieee80211_is_data(fc)) {
1554 tx_cmd->initial_rate_idx = 0;
1555 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1556 return;
1557 }
1558
1559 /**
1560 * If the current TX rate stored in mac80211 has the MCS bit set, it's
1561 * not really a TX rate. Thus, we use the lowest supported rate for
1562 * this band. Also use the lowest supported rate if the stored rate
1563 * idx is invalid.
1564 */
1565 rate_idx = info->control.rates[0].idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001566 if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
1567 || rate_idx > RATE_COUNT_LEGACY)
1568 rate_idx =
1569 rate_lowest_index(&il->bands[info->band],
1570 info->control.sta);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001571 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
1572 if (info->band == IEEE80211_BAND_5GHZ)
1573 rate_idx += IL_FIRST_OFDM_RATE;
1574 /* Get PLCP rate for tx_cmd->rate_n_flags */
1575 rate_plcp = il_rates[rate_idx].plcp;
1576 /* Zero out flags for this packet */
1577 rate_flags = 0;
1578
1579 /* Set CCK flag as needed */
1580 if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
1581 rate_flags |= RATE_MCS_CCK_MSK;
1582
1583 /* Set up antennas */
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +01001584 il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001585 rate_flags |= BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001586
1587 /* Set the rate in the TX cmd */
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001588 tx_cmd->rate_n_flags = cpu_to_le32(rate_plcp | rate_flags);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001589}
1590
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001591static void
1592il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
1593 struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
1594 int sta_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001595{
1596 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1597
1598 switch (keyconf->cipher) {
1599 case WLAN_CIPHER_SUITE_CCMP:
1600 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1601 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1602 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1603 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1604 D_TX("tx_cmd with AES hwcrypto\n");
1605 break;
1606
1607 case WLAN_CIPHER_SUITE_TKIP:
1608 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1609 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1610 D_TX("tx_cmd with tkip hwcrypto\n");
1611 break;
1612
1613 case WLAN_CIPHER_SUITE_WEP104:
1614 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1615 /* fall through */
1616 case WLAN_CIPHER_SUITE_WEP40:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001617 tx_cmd->sec_ctl |=
1618 (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
1619 TX_CMD_SEC_SHIFT);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001620
1621 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1622
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001623 D_TX("Configuring packet for WEP encryption " "with key %d\n",
1624 keyconf->keyidx);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001625 break;
1626
1627 default:
1628 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1629 break;
1630 }
1631}
1632
1633/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001634 * start C_TX command process
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001635 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001636int
1637il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001638{
1639 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1640 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1641 struct ieee80211_sta *sta = info->control.sta;
1642 struct il_station_priv *sta_priv = NULL;
1643 struct il_tx_queue *txq;
1644 struct il_queue *q;
1645 struct il_device_cmd *out_cmd;
1646 struct il_cmd_meta *out_meta;
1647 struct il_tx_cmd *tx_cmd;
1648 struct il_rxon_context *ctx = &il->ctx;
1649 int txq_id;
1650 dma_addr_t phys_addr;
1651 dma_addr_t txcmd_phys;
1652 dma_addr_t scratch_phys;
1653 u16 len, firstlen, secondlen;
1654 u16 seq_number = 0;
1655 __le16 fc;
1656 u8 hdr_len;
1657 u8 sta_id;
1658 u8 wait_write_ptr = 0;
1659 u8 tid = 0;
1660 u8 *qc = NULL;
1661 unsigned long flags;
1662 bool is_agg = false;
1663
1664 if (info->control.vif)
1665 ctx = il_rxon_ctx_from_vif(info->control.vif);
1666
1667 spin_lock_irqsave(&il->lock, flags);
1668 if (il_is_rfkill(il)) {
1669 D_DROP("Dropping - RF KILL\n");
1670 goto drop_unlock;
1671 }
1672
1673 fc = hdr->frame_control;
1674
1675#ifdef CONFIG_IWLEGACY_DEBUG
1676 if (ieee80211_is_auth(fc))
1677 D_TX("Sending AUTH frame\n");
1678 else if (ieee80211_is_assoc_req(fc))
1679 D_TX("Sending ASSOC frame\n");
1680 else if (ieee80211_is_reassoc_req(fc))
1681 D_TX("Sending REASSOC frame\n");
1682#endif
1683
1684 hdr_len = ieee80211_hdrlen(fc);
1685
1686 /* For management frames use broadcast id to do not break aggregation */
1687 if (!ieee80211_is_data(fc))
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01001688 sta_id = il->hw_params.bcast_id;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001689 else {
1690 /* Find idx into station table for destination station */
1691 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1692
1693 if (sta_id == IL_INVALID_STATION) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001694 D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001695 goto drop_unlock;
1696 }
1697 }
1698
1699 D_TX("station Id %d\n", sta_id);
1700
1701 if (sta)
1702 sta_priv = (void *)sta->drv_priv;
1703
1704 if (sta_priv && sta_priv->asleep &&
1705 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1706 /*
1707 * This sends an asynchronous command to the device,
1708 * but we can rely on it being processed before the
1709 * next frame is processed -- and the next frame to
1710 * this station is the one that will consume this
1711 * counter.
1712 * For now set the counter to just 1 since we do not
1713 * support uAPSD yet.
1714 */
1715 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1716 }
1717
Stanislaw Gruszkad1e14e92012-02-03 17:31:47 +01001718 /* FIXME: remove me ? */
1719 WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
1720
Stanislaw Gruszkaeb123af2012-02-03 17:31:54 +01001721 /* Access category (AC) is also the queue number */
1722 txq_id = skb_get_queue_mapping(skb);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001723
1724 /* irqs already disabled/saved above when locking il->lock */
1725 spin_lock(&il->sta_lock);
1726
1727 if (ieee80211_is_data_qos(fc)) {
1728 qc = ieee80211_get_qos_ctl(hdr);
1729 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1730 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1731 spin_unlock(&il->sta_lock);
1732 goto drop_unlock;
1733 }
1734 seq_number = il->stations[sta_id].tid[tid].seq_number;
1735 seq_number &= IEEE80211_SCTL_SEQ;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001736 hdr->seq_ctrl =
1737 hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001738 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1739 seq_number += 0x10;
1740 /* aggregation is on for this <sta,tid> */
1741 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1742 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1743 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1744 is_agg = true;
1745 }
1746 }
1747
1748 txq = &il->txq[txq_id];
1749 q = &txq->q;
1750
1751 if (unlikely(il_queue_space(q) < q->high_mark)) {
1752 spin_unlock(&il->sta_lock);
1753 goto drop_unlock;
1754 }
1755
1756 if (ieee80211_is_data_qos(fc)) {
1757 il->stations[sta_id].tid[tid].tfds_in_queue++;
1758 if (!ieee80211_has_morefrags(fc))
1759 il->stations[sta_id].tid[tid].seq_number = seq_number;
1760 }
1761
1762 spin_unlock(&il->sta_lock);
1763
1764 /* Set up driver data for this TFD */
1765 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1766 txq->txb[q->write_ptr].skb = skb;
1767 txq->txb[q->write_ptr].ctx = ctx;
1768
1769 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1770 out_cmd = txq->cmd[q->write_ptr];
1771 out_meta = &txq->meta[q->write_ptr];
1772 tx_cmd = &out_cmd->cmd.tx;
1773 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1774 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1775
1776 /*
1777 * Set up the Tx-command (not MAC!) header.
1778 * Store the chosen Tx queue and TFD idx within the sequence field;
1779 * after Tx, uCode's Tx response will return this value so driver can
1780 * locate the frame within the tx queue and do post-tx processing.
1781 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001782 out_cmd->hdr.cmd = C_TX;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001783 out_cmd->hdr.sequence =
1784 cpu_to_le16((u16)
1785 (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001786
1787 /* Copy MAC header from skb into command buffer */
1788 memcpy(tx_cmd->hdr, hdr, hdr_len);
1789
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001790 /* Total # bytes to be transmitted */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001791 len = (u16) skb->len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001792 tx_cmd->len = cpu_to_le16(len);
1793
1794 if (info->control.hw_key)
1795 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1796
1797 /* TODO need this for burst mode later on */
1798 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1799 il_dbg_log_tx_data_frame(il, len, hdr);
1800
1801 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1802
1803 il_update_stats(il, true, fc, len);
1804 /*
1805 * Use the first empty entry in this queue's command buffer array
1806 * to contain the Tx command and MAC header concatenated together
1807 * (payload data will be in another buffer).
1808 * Size of this varies, due to varying MAC header length.
1809 * If end is not dword aligned, we'll have 2 extra bytes at the end
1810 * of the MAC header (device reads on dword boundaries).
1811 * We'll tell device about this padding later.
1812 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001813 len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001814 firstlen = (len + 3) & ~3;
1815
1816 /* Tell NIC about any 2-byte padding after MAC header */
1817 if (firstlen != len)
1818 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1819
1820 /* Physical address of this Tx command's header (not MAC header!),
1821 * within command buffer array. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001822 txcmd_phys =
1823 pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
1824 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001825 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1826 dma_unmap_len_set(out_meta, len, firstlen);
1827 /* Add buffer containing Tx command and MAC(!) header to TFD's
1828 * first entry */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001829 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen,
1830 1, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001831
1832 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1833 txq->need_update = 1;
1834 } else {
1835 wait_write_ptr = 1;
1836 txq->need_update = 0;
1837 }
1838
1839 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1840 * if any (802.11 null frames have no payload). */
1841 secondlen = skb->len - hdr_len;
1842 if (secondlen > 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001843 phys_addr =
1844 pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
1845 PCI_DMA_TODEVICE);
1846 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr,
1847 secondlen, 0, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001848 }
1849
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001850 scratch_phys =
1851 txcmd_phys + sizeof(struct il_cmd_header) +
1852 offsetof(struct il_tx_cmd, scratch);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001853
1854 /* take back ownership of DMA buffer to enable update */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001855 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
1856 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001857 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1858 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1859
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001860 D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001861 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001862 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
1863 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001864
1865 /* Set up entry for this TFD in Tx byte-count array */
1866 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1867 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001868 le16_to_cpu(tx_cmd->
1869 len));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001870
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001871 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
1872 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001873
1874 /* Tell device the write idx *just past* this latest filled TFD */
1875 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1876 il_txq_update_write_ptr(il, txq);
1877 spin_unlock_irqrestore(&il->lock, flags);
1878
1879 /*
1880 * At this point the frame is "transmitted" successfully
1881 * and we will get a TX status notification eventually,
1882 * regardless of the value of ret. "ret" only indicates
1883 * whether or not we should update the write pointer.
1884 */
1885
1886 /*
1887 * Avoid atomic ops if it isn't an associated client.
1888 * Also, if this is a packet for aggregation, don't
1889 * increase the counter because the ucode will stop
1890 * aggregation queues when their respective station
1891 * goes to sleep.
1892 */
1893 if (sta_priv && sta_priv->client && !is_agg)
1894 atomic_inc(&sta_priv->pending_frames);
1895
1896 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1897 if (wait_write_ptr) {
1898 spin_lock_irqsave(&il->lock, flags);
1899 txq->need_update = 1;
1900 il_txq_update_write_ptr(il, txq);
1901 spin_unlock_irqrestore(&il->lock, flags);
1902 } else {
1903 il_stop_queue(il, txq);
1904 }
1905 }
1906
1907 return 0;
1908
1909drop_unlock:
1910 spin_unlock_irqrestore(&il->lock, flags);
1911 return -1;
1912}
1913
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001914static inline int
1915il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001916{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001917 ptr->addr =
1918 dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001919 if (!ptr->addr)
1920 return -ENOMEM;
1921 ptr->size = size;
1922 return 0;
1923}
1924
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001925static inline void
1926il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001927{
1928 if (unlikely(!ptr->addr))
1929 return;
1930
1931 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1932 memset(ptr, 0, sizeof(*ptr));
1933}
1934
1935/**
1936 * il4965_hw_txq_ctx_free - Free TXQ Context
1937 *
1938 * Destroy all TX DMA queues and structures
1939 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001940void
1941il4965_hw_txq_ctx_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001942{
1943 int txq_id;
1944
1945 /* Tx queues */
1946 if (il->txq) {
1947 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1948 if (txq_id == il->cmd_queue)
1949 il_cmd_queue_free(il);
1950 else
1951 il_tx_queue_free(il, txq_id);
1952 }
1953 il4965_free_dma_ptr(il, &il->kw);
1954
1955 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1956
1957 /* free tx queue structure */
1958 il_txq_mem(il);
1959}
1960
1961/**
1962 * il4965_txq_ctx_alloc - allocate TX queue context
1963 * Allocate all Tx DMA structures and initialize them
1964 *
1965 * @param il
1966 * @return error code
1967 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001968int
1969il4965_txq_ctx_alloc(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001970{
1971 int ret;
1972 int txq_id, slots_num;
1973 unsigned long flags;
1974
1975 /* Free all tx/cmd queues and keep-warm buffer */
1976 il4965_hw_txq_ctx_free(il);
1977
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001978 ret =
1979 il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
1980 il->hw_params.scd_bc_tbls_size);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001981 if (ret) {
1982 IL_ERR("Scheduler BC Table allocation failed\n");
1983 goto error_bc_tbls;
1984 }
1985 /* Alloc keep-warm buffer */
1986 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
1987 if (ret) {
1988 IL_ERR("Keep Warm allocation failed\n");
1989 goto error_kw;
1990 }
1991
1992 /* allocate tx queue structure */
1993 ret = il_alloc_txq_mem(il);
1994 if (ret)
1995 goto error;
1996
1997 spin_lock_irqsave(&il->lock, flags);
1998
1999 /* Turn off all Tx DMA fifos */
2000 il4965_txq_set_sched(il, 0);
2001
2002 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002003 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002004
2005 spin_unlock_irqrestore(&il->lock, flags);
2006
2007 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2008 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002009 slots_num =
2010 (txq_id ==
2011 il->cmd_queue) ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2012 ret = il_tx_queue_init(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002013 if (ret) {
2014 IL_ERR("Tx %d queue init failed\n", txq_id);
2015 goto error;
2016 }
2017 }
2018
2019 return ret;
2020
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002021error:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002022 il4965_hw_txq_ctx_free(il);
2023 il4965_free_dma_ptr(il, &il->kw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002024error_kw:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002025 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002026error_bc_tbls:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002027 return ret;
2028}
2029
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002030void
2031il4965_txq_ctx_reset(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002032{
2033 int txq_id, slots_num;
2034 unsigned long flags;
2035
2036 spin_lock_irqsave(&il->lock, flags);
2037
2038 /* Turn off all Tx DMA fifos */
2039 il4965_txq_set_sched(il, 0);
2040
2041 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002042 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002043
2044 spin_unlock_irqrestore(&il->lock, flags);
2045
2046 /* Alloc and init all Tx queues, including the command queue (#4) */
2047 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002048 slots_num =
2049 txq_id == il->cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2050 il_tx_queue_reset(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002051 }
2052}
2053
2054/**
2055 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2056 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002057void
2058il4965_txq_ctx_stop(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002059{
2060 int ch, txq_id;
2061 unsigned long flags;
2062
2063 /* Turn off all Tx DMA fifos */
2064 spin_lock_irqsave(&il->lock, flags);
2065
2066 il4965_txq_set_sched(il, 0);
2067
2068 /* Stop each Tx DMA channel, and wait for it to be idle */
2069 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002070 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2071 if (il_poll_bit
2072 (il, FH49_TSSR_TX_STATUS_REG,
2073 FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002074 IL_ERR("Failing on timeout while stopping"
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002075 " DMA channel %d [0x%08x]", ch,
2076 il_rd(il, FH49_TSSR_TX_STATUS_REG));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002077 }
2078 spin_unlock_irqrestore(&il->lock, flags);
2079
2080 if (!il->txq)
2081 return;
2082
2083 /* Unmap DMA from host system and free skb's */
2084 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2085 if (txq_id == il->cmd_queue)
2086 il_cmd_queue_unmap(il);
2087 else
2088 il_tx_queue_unmap(il, txq_id);
2089}
2090
2091/*
2092 * Find first available (lowest unused) Tx Queue, mark it "active".
2093 * Called only when finding queue for aggregation.
2094 * Should never return anything < 7, because they should already
2095 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2096 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002097static int
2098il4965_txq_ctx_activate_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002099{
2100 int txq_id;
2101
2102 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2103 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2104 return txq_id;
2105 return -1;
2106}
2107
2108/**
2109 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2110 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002111static void
2112il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002113{
2114 /* Simply stop the queue, but don't change any configuration;
2115 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002116 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002117 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
2118 (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002119}
2120
2121/**
2122 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2123 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002124static int
2125il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002126{
2127 u32 tbl_dw_addr;
2128 u32 tbl_dw;
2129 u16 scd_q2ratid;
2130
2131 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2132
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002133 tbl_dw_addr =
2134 il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002135
2136 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2137
2138 if (txq_id & 0x1)
2139 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2140 else
2141 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2142
2143 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2144
2145 return 0;
2146}
2147
2148/**
2149 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2150 *
2151 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2152 * i.e. it must be one of the higher queues used for aggregation
2153 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002154static int
2155il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
2156 int tid, u16 ssn_idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002157{
2158 unsigned long flags;
2159 u16 ra_tid;
2160 int ret;
2161
2162 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2163 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002164 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2165 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002166 txq_id, IL49_FIRST_AMPDU_QUEUE,
2167 IL49_FIRST_AMPDU_QUEUE +
2168 il->cfg->base_params->num_of_ampdu_queues - 1);
2169 return -EINVAL;
2170 }
2171
2172 ra_tid = BUILD_RAxTID(sta_id, tid);
2173
2174 /* Modify device's station table to Tx this TID */
2175 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2176 if (ret)
2177 return ret;
2178
2179 spin_lock_irqsave(&il->lock, flags);
2180
2181 /* Stop this Tx queue before configuring it */
2182 il4965_tx_queue_stop_scheduler(il, txq_id);
2183
2184 /* Map receiver-address / traffic-ID to this queue */
2185 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2186
2187 /* Set this queue as a chain-building queue */
2188 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2189
2190 /* Place first TFD at idx corresponding to start sequence number.
2191 * Assumes that ssn_idx is valid (!= 0xFFF) */
2192 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2193 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2194 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2195
2196 /* Set up Tx win size and frame limit for this queue */
2197 il_write_targ_mem(il,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002198 il->scd_base_addr +
2199 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2200 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
2201 & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002202
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002203 il_write_targ_mem(il,
2204 il->scd_base_addr +
2205 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2206 (SCD_FRAME_LIMIT <<
2207 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
2208 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002209
2210 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2211
2212 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2213 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2214
2215 spin_unlock_irqrestore(&il->lock, flags);
2216
2217 return 0;
2218}
2219
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002220int
2221il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2222 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002223{
2224 int sta_id;
2225 int tx_fifo;
2226 int txq_id;
2227 int ret;
2228 unsigned long flags;
2229 struct il_tid_data *tid_data;
2230
2231 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2232 if (unlikely(tx_fifo < 0))
2233 return tx_fifo;
2234
Greg Dietsche53611e02011-08-28 08:26:16 -05002235 D_HT("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002236
2237 sta_id = il_sta_id(sta);
2238 if (sta_id == IL_INVALID_STATION) {
2239 IL_ERR("Start AGG on invalid station\n");
2240 return -ENXIO;
2241 }
2242 if (unlikely(tid >= MAX_TID_COUNT))
2243 return -EINVAL;
2244
2245 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2246 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2247 return -ENXIO;
2248 }
2249
2250 txq_id = il4965_txq_ctx_activate_free(il);
2251 if (txq_id == -1) {
2252 IL_ERR("No free aggregation queue available\n");
2253 return -ENXIO;
2254 }
2255
2256 spin_lock_irqsave(&il->sta_lock, flags);
2257 tid_data = &il->stations[sta_id].tid[tid];
2258 *ssn = SEQ_TO_SN(tid_data->seq_number);
2259 tid_data->agg.txq_id = txq_id;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002260 il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002261 spin_unlock_irqrestore(&il->sta_lock, flags);
2262
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002263 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002264 if (ret)
2265 return ret;
2266
2267 spin_lock_irqsave(&il->sta_lock, flags);
2268 tid_data = &il->stations[sta_id].tid[tid];
2269 if (tid_data->tfds_in_queue == 0) {
2270 D_HT("HW queue is empty\n");
2271 tid_data->agg.state = IL_AGG_ON;
2272 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2273 } else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002274 D_HT("HW queue is NOT empty: %d packets in HW queue\n",
2275 tid_data->tfds_in_queue);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002276 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2277 }
2278 spin_unlock_irqrestore(&il->sta_lock, flags);
2279 return ret;
2280}
2281
2282/**
2283 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2284 * il->lock must be held by the caller
2285 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002286static int
2287il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002288{
2289 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2290 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002291 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2292 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002293 txq_id, IL49_FIRST_AMPDU_QUEUE,
2294 IL49_FIRST_AMPDU_QUEUE +
2295 il->cfg->base_params->num_of_ampdu_queues - 1);
2296 return -EINVAL;
2297 }
2298
2299 il4965_tx_queue_stop_scheduler(il, txq_id);
2300
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002301 il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002302
2303 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2304 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2305 /* supposes that ssn_idx is valid (!= 0xFFF) */
2306 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2307
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002308 il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002309 il_txq_ctx_deactivate(il, txq_id);
2310 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2311
2312 return 0;
2313}
2314
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002315int
2316il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2317 struct ieee80211_sta *sta, u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002318{
2319 int tx_fifo_id, txq_id, sta_id, ssn;
2320 struct il_tid_data *tid_data;
2321 int write_ptr, read_ptr;
2322 unsigned long flags;
2323
2324 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2325 if (unlikely(tx_fifo_id < 0))
2326 return tx_fifo_id;
2327
2328 sta_id = il_sta_id(sta);
2329
2330 if (sta_id == IL_INVALID_STATION) {
2331 IL_ERR("Invalid station for AGG tid %d\n", tid);
2332 return -ENXIO;
2333 }
2334
2335 spin_lock_irqsave(&il->sta_lock, flags);
2336
2337 tid_data = &il->stations[sta_id].tid[tid];
2338 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2339 txq_id = tid_data->agg.txq_id;
2340
2341 switch (il->stations[sta_id].tid[tid].agg.state) {
2342 case IL_EMPTYING_HW_QUEUE_ADDBA:
2343 /*
2344 * This can happen if the peer stops aggregation
2345 * again before we've had a chance to drain the
2346 * queue we selected previously, i.e. before the
2347 * session was really started completely.
2348 */
2349 D_HT("AGG stop before setup done\n");
2350 goto turn_off;
2351 case IL_AGG_ON:
2352 break;
2353 default:
2354 IL_WARN("Stopping AGG while state not ON or starting\n");
2355 }
2356
2357 write_ptr = il->txq[txq_id].q.write_ptr;
2358 read_ptr = il->txq[txq_id].q.read_ptr;
2359
2360 /* The queue is not empty */
2361 if (write_ptr != read_ptr) {
2362 D_HT("Stopping a non empty AGG HW QUEUE\n");
2363 il->stations[sta_id].tid[tid].agg.state =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002364 IL_EMPTYING_HW_QUEUE_DELBA;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002365 spin_unlock_irqrestore(&il->sta_lock, flags);
2366 return 0;
2367 }
2368
2369 D_HT("HW queue is empty\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002370turn_off:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002371 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2372
2373 /* do not restore/save irqs */
2374 spin_unlock(&il->sta_lock);
2375 spin_lock(&il->lock);
2376
2377 /*
2378 * the only reason this call can fail is queue number out of range,
2379 * which can happen if uCode is reloaded and all the station
2380 * information are lost. if it is outside the range, there is no need
2381 * to deactivate the uCode queue, just return "success" to allow
2382 * mac80211 to clean up it own data.
2383 */
2384 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2385 spin_unlock_irqrestore(&il->lock, flags);
2386
2387 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2388
2389 return 0;
2390}
2391
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002392int
2393il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002394{
2395 struct il_queue *q = &il->txq[txq_id].q;
2396 u8 *addr = il->stations[sta_id].sta.sta.addr;
2397 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2398 struct il_rxon_context *ctx;
2399
2400 ctx = &il->ctx;
2401
2402 lockdep_assert_held(&il->sta_lock);
2403
2404 switch (il->stations[sta_id].tid[tid].agg.state) {
2405 case IL_EMPTYING_HW_QUEUE_DELBA:
2406 /* We are reclaiming the last packet of the */
2407 /* aggregated HW queue */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002408 if (txq_id == tid_data->agg.txq_id &&
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002409 q->read_ptr == q->write_ptr) {
2410 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2411 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002412 D_HT("HW queue empty: continue DELBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002413 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2414 tid_data->agg.state = IL_AGG_OFF;
2415 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2416 }
2417 break;
2418 case IL_EMPTYING_HW_QUEUE_ADDBA:
2419 /* We are reclaiming the last packet of the queue */
2420 if (tid_data->tfds_in_queue == 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002421 D_HT("HW queue empty: continue ADDBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002422 tid_data->agg.state = IL_AGG_ON;
2423 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2424 }
2425 break;
2426 }
2427
2428 return 0;
2429}
2430
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002431static void
2432il4965_non_agg_tx_status(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002433 const u8 *addr1)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002434{
2435 struct ieee80211_sta *sta;
2436 struct il_station_priv *sta_priv;
2437
2438 rcu_read_lock();
2439 sta = ieee80211_find_sta(ctx->vif, addr1);
2440 if (sta) {
2441 sta_priv = (void *)sta->drv_priv;
2442 /* avoid atomic ops if this isn't a client */
2443 if (sta_priv->client &&
2444 atomic_dec_return(&sta_priv->pending_frames) == 0)
2445 ieee80211_sta_block_awake(il->hw, sta, false);
2446 }
2447 rcu_read_unlock();
2448}
2449
2450static void
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002451il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info, bool is_agg)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002452{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002453 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002454
2455 if (!is_agg)
2456 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2457
2458 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2459}
2460
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002461int
2462il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002463{
2464 struct il_tx_queue *txq = &il->txq[txq_id];
2465 struct il_queue *q = &txq->q;
2466 struct il_tx_info *tx_info;
2467 int nfreed = 0;
2468 struct ieee80211_hdr *hdr;
2469
2470 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2471 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002472 "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
2473 q->write_ptr, q->read_ptr);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002474 return 0;
2475 }
2476
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002477 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002478 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2479
2480 tx_info = &txq->txb[txq->q.read_ptr];
2481
2482 if (WARN_ON_ONCE(tx_info->skb == NULL))
2483 continue;
2484
2485 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2486 if (ieee80211_is_data_qos(hdr->frame_control))
2487 nfreed++;
2488
2489 il4965_tx_status(il, tx_info,
2490 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2491 tx_info->skb = NULL;
2492
2493 il->cfg->ops->lib->txq_free_tfd(il, txq);
2494 }
2495 return nfreed;
2496}
2497
2498/**
2499 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2500 *
2501 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2502 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2503 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002504static int
2505il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
2506 struct il_compressed_ba_resp *ba_resp)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002507{
2508 int i, sh, ack;
2509 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2510 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2511 int successes = 0;
2512 struct ieee80211_tx_info *info;
2513 u64 bitmap, sent_bitmap;
2514
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002515 if (unlikely(!agg->wait_for_ba)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002516 if (unlikely(ba_resp->bitmap))
2517 IL_ERR("Received BA when not expected\n");
2518 return -EINVAL;
2519 }
2520
2521 /* Mark that the expected block-ack response arrived */
2522 agg->wait_for_ba = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002523 D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002524
2525 /* Calculate shift to align block-ack bits with our Tx win bits */
2526 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002527 if (sh < 0) /* tbw something is wrong with indices */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002528 sh += 0x100;
2529
2530 if (agg->frame_count > (64 - sh)) {
2531 D_TX_REPLY("more frames than bitmap size");
2532 return -1;
2533 }
2534
2535 /* don't use 64-bit values for now */
2536 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2537
2538 /* check for success or failure according to the
2539 * transmitted bitmap and block-ack bitmap */
2540 sent_bitmap = bitmap & agg->bitmap;
2541
2542 /* For each frame attempted in aggregation,
2543 * update driver's record of tx frame's status. */
2544 i = 0;
2545 while (sent_bitmap) {
2546 ack = sent_bitmap & 1ULL;
2547 successes += ack;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002548 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
2549 i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002550 sent_bitmap >>= 1;
2551 ++i;
2552 }
2553
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002554 D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002555
2556 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2557 memset(&info->status, 0, sizeof(info->status));
2558 info->flags |= IEEE80211_TX_STAT_ACK;
2559 info->flags |= IEEE80211_TX_STAT_AMPDU;
2560 info->status.ampdu_ack_len = successes;
2561 info->status.ampdu_len = agg->frame_count;
2562 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2563
2564 return 0;
2565}
2566
2567/**
2568 * translate ucode response to mac80211 tx status control values
2569 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002570void
2571il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2572 struct ieee80211_tx_info *info)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002573{
2574 struct ieee80211_tx_rate *r = &info->control.rates[0];
2575
2576 info->antenna_sel_tx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002577 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002578 if (rate_n_flags & RATE_MCS_HT_MSK)
2579 r->flags |= IEEE80211_TX_RC_MCS;
2580 if (rate_n_flags & RATE_MCS_GF_MSK)
2581 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2582 if (rate_n_flags & RATE_MCS_HT40_MSK)
2583 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2584 if (rate_n_flags & RATE_MCS_DUP_MSK)
2585 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2586 if (rate_n_flags & RATE_MCS_SGI_MSK)
2587 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2588 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2589}
2590
2591/**
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02002592 * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002593 *
2594 * Handles block-acknowledge notification from device, which reports success
2595 * of frames sent via aggregation.
2596 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002597void
2598il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002599{
2600 struct il_rx_pkt *pkt = rxb_addr(rxb);
2601 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2602 struct il_tx_queue *txq = NULL;
2603 struct il_ht_agg *agg;
2604 int idx;
2605 int sta_id;
2606 int tid;
2607 unsigned long flags;
2608
2609 /* "flow" corresponds to Tx queue */
2610 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2611
2612 /* "ssn" is start of block-ack Tx win, corresponds to idx
2613 * (in Tx queue's circular buffer) of first TFD/frame in win */
2614 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2615
2616 if (scd_flow >= il->hw_params.max_txq_num) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002617 IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002618 return;
2619 }
2620
2621 txq = &il->txq[scd_flow];
2622 sta_id = ba_resp->sta_id;
2623 tid = ba_resp->tid;
2624 agg = &il->stations[sta_id].tid[tid].agg;
2625 if (unlikely(agg->txq_id != scd_flow)) {
2626 /*
2627 * FIXME: this is a uCode bug which need to be addressed,
2628 * log the information and return for now!
2629 * since it is possible happen very often and in order
2630 * not to fill the syslog, don't enable the logging by default
2631 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002632 D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
2633 scd_flow, agg->txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002634 return;
2635 }
2636
2637 /* Find idx just before block-ack win */
2638 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2639
2640 spin_lock_irqsave(&il->sta_lock, flags);
2641
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002642 D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002643 agg->wait_for_ba, (u8 *) &ba_resp->sta_addr_lo32,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002644 ba_resp->sta_id);
2645 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
2646 "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
2647 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2648 ba_resp->scd_flow, ba_resp->scd_ssn);
2649 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
2650 (unsigned long long)agg->bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002651
2652 /* Update driver's record of ACK vs. not for each frame in win */
2653 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2654
2655 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2656 * block-ack win (we assume that they've been successfully
2657 * transmitted ... if not, it's too late anyway). */
2658 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2659 /* calculate mac80211 ampdu sw queue to wake */
2660 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2661 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2662
2663 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2664 il->mac80211_registered &&
2665 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2666 il_wake_queue(il, txq);
2667
2668 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2669 }
2670
2671 spin_unlock_irqrestore(&il->sta_lock, flags);
2672}
2673
2674#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002675const char *
2676il4965_get_tx_fail_reason(u32 status)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002677{
2678#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2679#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2680
2681 switch (status & TX_STATUS_MSK) {
2682 case TX_STATUS_SUCCESS:
2683 return "SUCCESS";
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002684 TX_STATUS_POSTPONE(DELAY);
2685 TX_STATUS_POSTPONE(FEW_BYTES);
2686 TX_STATUS_POSTPONE(QUIET_PERIOD);
2687 TX_STATUS_POSTPONE(CALC_TTAK);
2688 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2689 TX_STATUS_FAIL(SHORT_LIMIT);
2690 TX_STATUS_FAIL(LONG_LIMIT);
2691 TX_STATUS_FAIL(FIFO_UNDERRUN);
2692 TX_STATUS_FAIL(DRAIN_FLOW);
2693 TX_STATUS_FAIL(RFKILL_FLUSH);
2694 TX_STATUS_FAIL(LIFE_EXPIRE);
2695 TX_STATUS_FAIL(DEST_PS);
2696 TX_STATUS_FAIL(HOST_ABORTED);
2697 TX_STATUS_FAIL(BT_RETRY);
2698 TX_STATUS_FAIL(STA_INVALID);
2699 TX_STATUS_FAIL(FRAG_DROPPED);
2700 TX_STATUS_FAIL(TID_DISABLE);
2701 TX_STATUS_FAIL(FIFO_FLUSHED);
2702 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2703 TX_STATUS_FAIL(PASSIVE_NO_RX);
2704 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002705 }
2706
2707 return "UNKNOWN";
2708
2709#undef TX_STATUS_FAIL
2710#undef TX_STATUS_POSTPONE
2711}
2712#endif /* CONFIG_IWLEGACY_DEBUG */
2713
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002714static struct il_link_quality_cmd *
2715il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2716{
2717 int i, r;
2718 struct il_link_quality_cmd *link_cmd;
2719 u32 rate_flags = 0;
2720 __le32 rate_n_flags;
2721
2722 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2723 if (!link_cmd) {
2724 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2725 return NULL;
2726 }
2727 /* Set up the rate scaling to start at selected rate, fall back
2728 * all the way down to 1M in IEEE order, and then spin on 1M */
2729 if (il->band == IEEE80211_BAND_5GHZ)
2730 r = RATE_6M_IDX;
2731 else
2732 r = RATE_1M_IDX;
2733
2734 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2735 rate_flags |= RATE_MCS_CCK_MSK;
2736
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002737 rate_flags |=
2738 il4965_first_antenna(il->hw_params.
2739 valid_tx_ant) << RATE_MCS_ANT_POS;
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01002740 rate_n_flags = cpu_to_le32(il_rates[r].plcp | rate_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002741 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2742 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2743
2744 link_cmd->general_params.single_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002745 il4965_first_antenna(il->hw_params.valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002746
2747 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002748 il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
2749 valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002750 if (!link_cmd->general_params.dual_stream_ant_msk) {
2751 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2752 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2753 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002754 il->hw_params.valid_tx_ant;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002755 }
2756
2757 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2758 link_cmd->agg_params.agg_time_limit =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002759 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002760
2761 link_cmd->sta_id = sta_id;
2762
2763 return link_cmd;
2764}
2765
2766/*
2767 * il4965_add_bssid_station - Add the special IBSS BSSID station
2768 *
2769 * Function sleeps.
2770 */
2771int
2772il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002773 const u8 *addr, u8 *sta_id_r)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002774{
2775 int ret;
2776 u8 sta_id;
2777 struct il_link_quality_cmd *link_cmd;
2778 unsigned long flags;
2779
2780 if (sta_id_r)
2781 *sta_id_r = IL_INVALID_STATION;
2782
2783 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2784 if (ret) {
2785 IL_ERR("Unable to add station %pM\n", addr);
2786 return ret;
2787 }
2788
2789 if (sta_id_r)
2790 *sta_id_r = sta_id;
2791
2792 spin_lock_irqsave(&il->sta_lock, flags);
2793 il->stations[sta_id].used |= IL_STA_LOCAL;
2794 spin_unlock_irqrestore(&il->sta_lock, flags);
2795
2796 /* Set up default rate scaling table in device's station table */
2797 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2798 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002799 IL_ERR("Unable to initialize rate scaling for station %pM.\n",
2800 addr);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002801 return -ENOMEM;
2802 }
2803
2804 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2805 if (ret)
2806 IL_ERR("Link quality command failed (%d)\n", ret);
2807
2808 spin_lock_irqsave(&il->sta_lock, flags);
2809 il->stations[sta_id].lq = link_cmd;
2810 spin_unlock_irqrestore(&il->sta_lock, flags);
2811
2812 return 0;
2813}
2814
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002815static int
2816il4965_static_wepkey_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2817 bool send_if_empty)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002818{
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002819 int i;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002820 u8 buff[sizeof(struct il_wep_cmd) +
2821 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2822 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002823 size_t cmd_size = sizeof(struct il_wep_cmd);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002824 struct il_host_cmd cmd = {
Stanislaw Gruszkad98e2942012-02-03 17:31:42 +01002825 .id = C_WEPKEY,
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002826 .data = wep_cmd,
2827 .flags = CMD_SYNC,
2828 };
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002829 bool not_empty = false;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002830
2831 might_sleep();
2832
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002833 memset(wep_cmd, 0,
2834 cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002835
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002836 for (i = 0; i < WEP_KEYS_MAX; i++) {
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002837 u8 key_size = il->_4965.wep_keys[i].key_size;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002838
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002839 wep_cmd->key[i].key_idx = i;
2840 if (key_size) {
2841 wep_cmd->key[i].key_offset = i;
2842 not_empty = true;
2843 } else
2844 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2845
2846 wep_cmd->key[i].key_size = key_size;
2847 memcpy(&wep_cmd->key[i].key[3], il->_4965.wep_keys[i].key, key_size);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002848 }
2849
2850 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2851 wep_cmd->num_keys = WEP_KEYS_MAX;
2852
2853 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002854 cmd.len = cmd_size;
2855
2856 if (not_empty || send_if_empty)
2857 return il_send_cmd(il, &cmd);
2858 else
2859 return 0;
2860}
2861
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002862int
2863il4965_restore_default_wep_keys(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002864{
2865 lockdep_assert_held(&il->mutex);
2866
2867 return il4965_static_wepkey_cmd(il, ctx, false);
2868}
2869
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002870int
2871il4965_remove_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2872 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002873{
2874 int ret;
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002875 int idx = keyconf->keyidx;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002876
2877 lockdep_assert_held(&il->mutex);
2878
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002879 D_WEP("Removing default WEP key: idx=%d\n", idx);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002880
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002881 memset(&il->_4965.wep_keys[idx], 0, sizeof(struct il_wep_key));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002882 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002883 D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002884 /* but keys in device are clear anyway so return success */
2885 return 0;
2886 }
2887 ret = il4965_static_wepkey_cmd(il, ctx, 1);
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002888 D_WEP("Remove default WEP key: idx=%d ret=%d\n", idx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002889
2890 return ret;
2891}
2892
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002893int
2894il4965_set_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2895 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002896{
2897 int ret;
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002898 int len = keyconf->keylen;
2899 int idx = keyconf->keyidx;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002900
2901 lockdep_assert_held(&il->mutex);
2902
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002903 if (len != WEP_KEY_LEN_128 && len != WEP_KEY_LEN_64) {
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002904 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2905 return -EINVAL;
2906 }
2907
2908 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2909 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Stanislaw Gruszka8f9e5642012-02-03 17:31:43 +01002910 il->stations[IL_AP_ID].keyinfo.cipher = keyconf->cipher;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002911
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002912 il->_4965.wep_keys[idx].key_size = len;
2913 memcpy(&il->_4965.wep_keys[idx].key, &keyconf->key, len);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002914
2915 ret = il4965_static_wepkey_cmd(il, ctx, false);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002916
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002917 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", len, idx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002918 return ret;
2919}
2920
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002921static int
2922il4965_set_wep_dynamic_key_info(struct il_priv *il, struct il_rxon_context *ctx,
2923 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002924{
2925 unsigned long flags;
2926 __le16 key_flags = 0;
2927 struct il_addsta_cmd sta_cmd;
2928
2929 lockdep_assert_held(&il->mutex);
2930
2931 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2932
2933 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2934 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2935 key_flags &= ~STA_KEY_FLG_INVALID;
2936
2937 if (keyconf->keylen == WEP_KEY_LEN_128)
2938 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2939
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01002940 if (sta_id == il->hw_params.bcast_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002941 key_flags |= STA_KEY_MULTICAST_MSK;
2942
2943 spin_lock_irqsave(&il->sta_lock, flags);
2944
2945 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2946 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2947 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2948
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002949 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002950
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002951 memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
2952 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002953
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002954 if ((il->stations[sta_id].sta.key.
2955 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002956 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002957 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002958 /* else, we are overriding an existing key => no need to allocated room
2959 * in uCode. */
2960
2961 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002962 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002963
2964 il->stations[sta_id].sta.key.key_flags = key_flags;
2965 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
2966 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2967
2968 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002969 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002970 spin_unlock_irqrestore(&il->sta_lock, flags);
2971
2972 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2973}
2974
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002975static int
2976il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
2977 struct il_rxon_context *ctx,
2978 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002979{
2980 unsigned long flags;
2981 __le16 key_flags = 0;
2982 struct il_addsta_cmd sta_cmd;
2983
2984 lockdep_assert_held(&il->mutex);
2985
2986 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
2987 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2988 key_flags &= ~STA_KEY_FLG_INVALID;
2989
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01002990 if (sta_id == il->hw_params.bcast_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002991 key_flags |= STA_KEY_MULTICAST_MSK;
2992
2993 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2994
2995 spin_lock_irqsave(&il->sta_lock, flags);
2996 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2997 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2998
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002999 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003000
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003001 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003002
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003003 if ((il->stations[sta_id].sta.key.
3004 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003005 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003006 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003007 /* else, we are overriding an existing key => no need to allocated room
3008 * in uCode. */
3009
3010 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003011 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003012
3013 il->stations[sta_id].sta.key.key_flags = key_flags;
3014 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3015 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3016
3017 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003018 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003019 spin_unlock_irqrestore(&il->sta_lock, flags);
3020
3021 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3022}
3023
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003024static int
3025il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3026 struct il_rxon_context *ctx,
3027 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003028{
3029 unsigned long flags;
3030 int ret = 0;
3031 __le16 key_flags = 0;
3032
3033 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3034 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3035 key_flags &= ~STA_KEY_FLG_INVALID;
3036
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01003037 if (sta_id == il->hw_params.bcast_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003038 key_flags |= STA_KEY_MULTICAST_MSK;
3039
3040 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3041 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3042
3043 spin_lock_irqsave(&il->sta_lock, flags);
3044
3045 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3046 il->stations[sta_id].keyinfo.keylen = 16;
3047
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003048 if ((il->stations[sta_id].sta.key.
3049 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003050 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003051 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003052 /* else, we are overriding an existing key => no need to allocated room
3053 * in uCode. */
3054
3055 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003056 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003057
3058 il->stations[sta_id].sta.key.key_flags = key_flags;
3059
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003060 /* This copy is acutally not needed: we get the key with each TX */
3061 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3062
3063 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3064
3065 spin_unlock_irqrestore(&il->sta_lock, flags);
3066
3067 return ret;
3068}
3069
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003070void
3071il4965_update_tkip_key(struct il_priv *il, struct il_rxon_context *ctx,
3072 struct ieee80211_key_conf *keyconf,
3073 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003074{
3075 u8 sta_id;
3076 unsigned long flags;
3077 int i;
3078
3079 if (il_scan_cancel(il)) {
3080 /* cancel scan failed, just live w/ bad key and rely
3081 briefly on SW decryption */
3082 return;
3083 }
3084
3085 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3086 if (sta_id == IL_INVALID_STATION)
3087 return;
3088
3089 spin_lock_irqsave(&il->sta_lock, flags);
3090
3091 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3092
3093 for (i = 0; i < 5; i++)
3094 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003095 cpu_to_le16(phase1key[i]);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003096
3097 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3098 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3099
3100 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3101
3102 spin_unlock_irqrestore(&il->sta_lock, flags);
3103
3104}
3105
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003106int
3107il4965_remove_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3108 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003109{
3110 unsigned long flags;
3111 u16 key_flags;
3112 u8 keyidx;
3113 struct il_addsta_cmd sta_cmd;
3114
3115 lockdep_assert_held(&il->mutex);
3116
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01003117 il->_4965.key_mapping_keys--;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003118
3119 spin_lock_irqsave(&il->sta_lock, flags);
3120 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3121 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3122
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003123 D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003124
3125 if (keyconf->keyidx != keyidx) {
3126 /* We need to remove a key with idx different that the one
3127 * in the uCode. This means that the key we need to remove has
3128 * been replaced by another one with different idx.
3129 * Don't do anything and return ok
3130 */
3131 spin_unlock_irqrestore(&il->sta_lock, flags);
3132 return 0;
3133 }
3134
3135 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003136 IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
3137 key_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003138 spin_unlock_irqrestore(&il->sta_lock, flags);
3139 return 0;
3140 }
3141
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003142 if (!test_and_clear_bit
3143 (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003144 IL_ERR("idx %d not used in uCode key table.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003145 il->stations[sta_id].sta.key.key_offset);
3146 memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
3147 memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003148 il->stations[sta_id].sta.key.key_flags =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003149 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003150 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3151 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3152 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3153
3154 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003155 D_WEP
3156 ("Not sending C_ADD_STA command because RFKILL enabled.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003157 spin_unlock_irqrestore(&il->sta_lock, flags);
3158 return 0;
3159 }
3160 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003161 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003162 spin_unlock_irqrestore(&il->sta_lock, flags);
3163
3164 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3165}
3166
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003167int
3168il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3169 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003170{
3171 int ret;
3172
3173 lockdep_assert_held(&il->mutex);
3174
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01003175 il->_4965.key_mapping_keys++;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003176 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3177
3178 switch (keyconf->cipher) {
3179 case WLAN_CIPHER_SUITE_CCMP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003180 ret =
3181 il4965_set_ccmp_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003182 break;
3183 case WLAN_CIPHER_SUITE_TKIP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003184 ret =
3185 il4965_set_tkip_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003186 break;
3187 case WLAN_CIPHER_SUITE_WEP40:
3188 case WLAN_CIPHER_SUITE_WEP104:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003189 ret = il4965_set_wep_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003190 break;
3191 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003192 IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
3193 keyconf->cipher);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003194 ret = -EINVAL;
3195 }
3196
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003197 D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3198 keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003199
3200 return ret;
3201}
3202
3203/**
3204 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3205 *
3206 * This adds the broadcast station into the driver's station table
3207 * and marks it driver active, so that it will be restored to the
3208 * device at the next best time.
3209 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003210int
3211il4965_alloc_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003212{
3213 struct il_link_quality_cmd *link_cmd;
3214 unsigned long flags;
3215 u8 sta_id;
3216
3217 spin_lock_irqsave(&il->sta_lock, flags);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003218 sta_id = il_prep_station(il, ctx, il_bcast_addr, false, NULL);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003219 if (sta_id == IL_INVALID_STATION) {
3220 IL_ERR("Unable to prepare broadcast station\n");
3221 spin_unlock_irqrestore(&il->sta_lock, flags);
3222
3223 return -EINVAL;
3224 }
3225
3226 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3227 il->stations[sta_id].used |= IL_STA_BCAST;
3228 spin_unlock_irqrestore(&il->sta_lock, flags);
3229
3230 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3231 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003232 IL_ERR
3233 ("Unable to initialize rate scaling for bcast station.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003234 return -ENOMEM;
3235 }
3236
3237 spin_lock_irqsave(&il->sta_lock, flags);
3238 il->stations[sta_id].lq = link_cmd;
3239 spin_unlock_irqrestore(&il->sta_lock, flags);
3240
3241 return 0;
3242}
3243
3244/**
3245 * il4965_update_bcast_station - update broadcast station's LQ command
3246 *
3247 * Only used by iwl4965. Placed here to have all bcast station management
3248 * code together.
3249 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003250static int
3251il4965_update_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003252{
3253 unsigned long flags;
3254 struct il_link_quality_cmd *link_cmd;
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01003255 u8 sta_id = il->hw_params.bcast_id;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003256
3257 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3258 if (!link_cmd) {
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003259 IL_ERR("Unable to initialize rate scaling for bcast sta.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003260 return -ENOMEM;
3261 }
3262
3263 spin_lock_irqsave(&il->sta_lock, flags);
3264 if (il->stations[sta_id].lq)
3265 kfree(il->stations[sta_id].lq);
3266 else
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003267 D_INFO("Bcast sta rate scaling has not been initialized.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003268 il->stations[sta_id].lq = link_cmd;
3269 spin_unlock_irqrestore(&il->sta_lock, flags);
3270
3271 return 0;
3272}
3273
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003274int
3275il4965_update_bcast_stations(struct il_priv *il)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003276{
3277 return il4965_update_bcast_station(il, &il->ctx);
3278}
3279
3280/**
3281 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3282 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003283int
3284il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003285{
3286 unsigned long flags;
3287 struct il_addsta_cmd sta_cmd;
3288
3289 lockdep_assert_held(&il->mutex);
3290
3291 /* Remove "disable" flag, to enable Tx for this TID */
3292 spin_lock_irqsave(&il->sta_lock, flags);
3293 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3294 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3295 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3296 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003297 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003298 spin_unlock_irqrestore(&il->sta_lock, flags);
3299
3300 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3301}
3302
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003303int
3304il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
3305 u16 ssn)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003306{
3307 unsigned long flags;
3308 int sta_id;
3309 struct il_addsta_cmd sta_cmd;
3310
3311 lockdep_assert_held(&il->mutex);
3312
3313 sta_id = il_sta_id(sta);
3314 if (sta_id == IL_INVALID_STATION)
3315 return -ENXIO;
3316
3317 spin_lock_irqsave(&il->sta_lock, flags);
3318 il->stations[sta_id].sta.station_flags_msk = 0;
3319 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003320 il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003321 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3322 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3323 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003324 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003325 spin_unlock_irqrestore(&il->sta_lock, flags);
3326
3327 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3328}
3329
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003330int
3331il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003332{
3333 unsigned long flags;
3334 int sta_id;
3335 struct il_addsta_cmd sta_cmd;
3336
3337 lockdep_assert_held(&il->mutex);
3338
3339 sta_id = il_sta_id(sta);
3340 if (sta_id == IL_INVALID_STATION) {
3341 IL_ERR("Invalid station for AGG tid %d\n", tid);
3342 return -ENXIO;
3343 }
3344
3345 spin_lock_irqsave(&il->sta_lock, flags);
3346 il->stations[sta_id].sta.station_flags_msk = 0;
3347 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003348 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003349 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3350 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003351 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003352 spin_unlock_irqrestore(&il->sta_lock, flags);
3353
3354 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3355}
3356
3357void
3358il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3359{
3360 unsigned long flags;
3361
3362 spin_lock_irqsave(&il->sta_lock, flags);
3363 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3364 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3365 il->stations[sta_id].sta.sta.modify_mask =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003366 STA_MODIFY_SLEEP_TX_COUNT_MSK;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003367 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3368 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003369 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003370 spin_unlock_irqrestore(&il->sta_lock, flags);
3371
3372}
3373
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003374void
3375il4965_update_chain_flags(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003376{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003377 if (il->cfg->ops->hcmd->set_rxon_chain) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003378 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01003379 if (il->active.rx_chain != il->staging.rx_chain)
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003380 il_commit_rxon(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003381 }
3382}
3383
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003384static void
3385il4965_clear_free_frames(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003386{
3387 struct list_head *element;
3388
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003389 D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003390
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003391 while (!list_empty(&il->free_frames)) {
3392 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003393 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003394 kfree(list_entry(element, struct il_frame, list));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003395 il->frames_count--;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003396 }
3397
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003398 if (il->frames_count) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003399 IL_WARN("%d frames still in use. Did we lose one?\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003400 il->frames_count);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003401 il->frames_count = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003402 }
3403}
3404
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003405static struct il_frame *
3406il4965_get_free_frame(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003407{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003408 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003409 struct list_head *element;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003410 if (list_empty(&il->free_frames)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003411 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3412 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003413 IL_ERR("Could not allocate frame!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003414 return NULL;
3415 }
3416
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003417 il->frames_count++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003418 return frame;
3419 }
3420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003421 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003422 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003423 return list_entry(element, struct il_frame, list);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003424}
3425
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003426static void
3427il4965_free_frame(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003428{
3429 memset(frame, 0, sizeof(*frame));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003430 list_add(&frame->list, &il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003431}
3432
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003433static u32
3434il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
3435 int left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003436{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003437 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003438
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003439 if (!il->beacon_skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003440 return 0;
3441
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003442 if (il->beacon_skb->len > left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003443 return 0;
3444
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003445 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003446
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003447 return il->beacon_skb->len;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003448}
3449
3450/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003451static void
3452il4965_set_beacon_tim(struct il_priv *il,
3453 struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
3454 u32 frame_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003455{
3456 u16 tim_idx;
3457 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3458
3459 /*
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003460 * The idx is relative to frame start but we start looking at the
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003461 * variable-length part of the beacon.
3462 */
3463 tim_idx = mgmt->u.beacon.variable - beacon;
3464
3465 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3466 while ((tim_idx < (frame_size - 2)) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003467 (beacon[tim_idx] != WLAN_EID_TIM))
3468 tim_idx += beacon[tim_idx + 1] + 2;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003469
3470 /* If TIM field was found, set variables */
3471 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3472 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003473 tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003474 } else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003475 IL_WARN("Unable to find TIM Element in beacon\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003476}
3477
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003478static unsigned int
3479il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003480{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003481 struct il_tx_beacon_cmd *tx_beacon_cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003482 u32 frame_size;
3483 u32 rate_flags;
3484 u32 rate;
3485 /*
3486 * We have to set up the TX command, the TX Beacon command, and the
3487 * beacon contents.
3488 */
3489
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003490 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003491
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003492 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003493 IL_ERR("trying to build beacon w/o beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003494 return 0;
3495 }
3496
3497 /* Initialize memory */
3498 tx_beacon_cmd = &frame->u.beacon;
3499 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3500
3501 /* Set up TX beacon contents */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003502 frame_size =
3503 il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
3504 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003505 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3506 return 0;
3507 if (!frame_size)
3508 return 0;
3509
3510 /* Set up TX command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003511 tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01003512 tx_beacon_cmd->tx.sta_id = il->hw_params.bcast_id;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003513 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003514 tx_beacon_cmd->tx.tx_flags =
3515 TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
3516 TX_CMD_FLG_STA_RATE_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003517
3518 /* Set up TX beacon command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003519 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
3520 frame_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003521
3522 /* Set up packet rate and flags */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003523 rate = il_get_lowest_plcp(il, il->beacon_ctx);
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +01003524 il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01003525 rate_flags = BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003526 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003527 rate_flags |= RATE_MCS_CCK_MSK;
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01003528 tx_beacon_cmd->tx.rate_n_flags = cpu_to_le32(rate | rate_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003529
3530 return sizeof(*tx_beacon_cmd) + frame_size;
3531}
3532
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003533int
3534il4965_send_beacon_cmd(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003535{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003536 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003537 unsigned int frame_size;
3538 int rc;
3539
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003540 frame = il4965_get_free_frame(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003541 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003542 IL_ERR("Could not obtain free frame buffer for beacon "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003543 "command.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003544 return -ENOMEM;
3545 }
3546
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003547 frame_size = il4965_hw_get_beacon_cmd(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003548 if (!frame_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003549 IL_ERR("Error configuring the beacon command\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003550 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003551 return -EINVAL;
3552 }
3553
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003554 rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003555
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003556 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003557
3558 return rc;
3559}
3560
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003561static inline dma_addr_t
3562il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003563{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003564 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003565
3566 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3567 if (sizeof(dma_addr_t) > sizeof(u32))
3568 addr |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003569 ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
3570 16;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003571
3572 return addr;
3573}
3574
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003575static inline u16
3576il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003577{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003578 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003579
3580 return le16_to_cpu(tb->hi_n_len) >> 4;
3581}
3582
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003583static inline void
3584il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003585{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003586 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003587 u16 hi_n_len = len << 4;
3588
3589 put_unaligned_le32(addr, &tb->lo);
3590 if (sizeof(dma_addr_t) > sizeof(u32))
3591 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3592
3593 tb->hi_n_len = cpu_to_le16(hi_n_len);
3594
3595 tfd->num_tbs = idx + 1;
3596}
3597
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003598static inline u8
3599il4965_tfd_get_num_tbs(struct il_tfd *tfd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003600{
3601 return tfd->num_tbs & 0x1f;
3602}
3603
3604/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003605 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003606 * @il - driver ilate data
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003607 * @txq - tx queue
3608 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003609 * Does NOT advance any TFD circular buffer read/write idxes
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003610 * Does NOT free the TFD itself (which is within circular buffer)
3611 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003612void
3613il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003614{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003615 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3616 struct il_tfd *tfd;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003617 struct pci_dev *dev = il->pci_dev;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003618 int idx = txq->q.read_ptr;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003619 int i;
3620 int num_tbs;
3621
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003622 tfd = &tfd_tmp[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003623
3624 /* Sanity check on number of chunks */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003625 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003626
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003627 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003628 IL_ERR("Too many chunks: %i\n", num_tbs);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003629 /* @todo issue fatal error, it is quite serious situation */
3630 return;
3631 }
3632
3633 /* Unmap tx_cmd */
3634 if (num_tbs)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003635 pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
3636 dma_unmap_len(&txq->meta[idx], len),
3637 PCI_DMA_BIDIRECTIONAL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003638
3639 /* Unmap chunks, if any. */
3640 for (i = 1; i < num_tbs; i++)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003641 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003642 il4965_tfd_tb_get_len(tfd, i),
3643 PCI_DMA_TODEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003644
3645 /* free SKB */
3646 if (txq->txb) {
3647 struct sk_buff *skb;
3648
3649 skb = txq->txb[txq->q.read_ptr].skb;
3650
3651 /* can be called from irqs-disabled context */
3652 if (skb) {
3653 dev_kfree_skb_any(skb);
3654 txq->txb[txq->q.read_ptr].skb = NULL;
3655 }
3656 }
3657}
3658
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003659int
3660il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
3661 dma_addr_t addr, u16 len, u8 reset, u8 pad)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003662{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003663 struct il_queue *q;
3664 struct il_tfd *tfd, *tfd_tmp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003665 u32 num_tbs;
3666
3667 q = &txq->q;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003668 tfd_tmp = (struct il_tfd *)txq->tfds;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003669 tfd = &tfd_tmp[q->write_ptr];
3670
3671 if (reset)
3672 memset(tfd, 0, sizeof(*tfd));
3673
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003674 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003675
3676 /* Each TFD can point to a maximum 20 Tx buffers */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003677 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003678 IL_ERR("Error can not send more than %d chunks\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003679 IL_NUM_OF_TBS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003680 return -EINVAL;
3681 }
3682
3683 BUG_ON(addr & ~DMA_BIT_MASK(36));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003684 if (unlikely(addr & ~IL_TX_DMA_MASK))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003685 IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003686
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003687 il4965_tfd_set_tb(tfd, num_tbs, addr, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003688
3689 return 0;
3690}
3691
3692/*
3693 * Tell nic where to find circular buffer of Tx Frame Descriptors for
3694 * given Tx queue, and enable the DMA channel used for that queue.
3695 *
3696 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
3697 * channels supported in hardware.
3698 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003699int
3700il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003701{
3702 int txq_id = txq->q.id;
3703
3704 /* Circular buffer (TFD queue in DRAM) physical base address */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003705 il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003706
3707 return 0;
3708}
3709
3710/******************************************************************************
3711 *
3712 * Generic RX handler implementations
3713 *
3714 ******************************************************************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003715static void
3716il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003717{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003718 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003719 struct il_alive_resp *palive;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003720 struct delayed_work *pwork;
3721
3722 palive = &pkt->u.alive_frame;
3723
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003724 D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
3725 palive->is_valid, palive->ver_type, palive->ver_subtype);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003726
3727 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003728 D_INFO("Initialization Alive received.\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003729 memcpy(&il->card_alive_init, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003730 sizeof(struct il_init_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003731 pwork = &il->init_alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003732 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003733 D_INFO("Runtime Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003734 memcpy(&il->card_alive, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003735 sizeof(struct il_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003736 pwork = &il->alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003737 }
3738
3739 /* We delay the ALIVE response by 5ms to
3740 * give the HW RF Kill time to activate... */
3741 if (palive->is_valid == UCODE_VALID_OK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003742 queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003743 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003744 IL_WARN("uCode did not respond OK.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003745}
3746
3747/**
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003748 * il4965_bg_stats_periodic - Timer callback to queue stats
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003749 *
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003750 * This callback is provided in order to send a stats request.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003751 *
3752 * This timer function is continually reset to execute within
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02003753 * REG_RECALIB_PERIOD seconds since the last N_STATS
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003754 * was received. We need to ensure we receive the stats in order
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003755 * to update the temperature used for calibrating the TXPOWER.
3756 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003757static void
3758il4965_bg_stats_periodic(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003759{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003760 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003761
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003762 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003763 return;
3764
3765 /* dont send host command if rf-kill is on */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003766 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003767 return;
3768
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003769 il_send_stats_request(il, CMD_ASYNC, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003770}
3771
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003772static void
3773il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003774{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003775 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003776 struct il4965_beacon_notif *beacon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003777 (struct il4965_beacon_notif *)pkt->u.raw;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003778#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003779 u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003780
Stanislaw Gruszka5bf0dac2011-12-23 08:13:47 +01003781 D_RX("beacon status %x retries %d iss %d tsf:0x%.8x%.8x rate %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003782 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
3783 beacon->beacon_notify_hdr.failure_frame,
3784 le32_to_cpu(beacon->ibss_mgr_status),
3785 le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003786#endif
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003787 il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003788}
3789
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003790static void
3791il4965_perform_ct_kill_task(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003792{
3793 unsigned long flags;
3794
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003795 D_POWER("Stop all queues\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003796
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003797 if (il->mac80211_registered)
3798 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003799
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003800 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003801 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003802 _il_rd(il, CSR_UCODE_DRV_GP1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003803
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003804 spin_lock_irqsave(&il->reg_lock, flags);
Stanislaw Gruszka13882262011-08-24 15:39:23 +02003805 if (!_il_grab_nic_access(il))
3806 _il_release_nic_access(il);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003807 spin_unlock_irqrestore(&il->reg_lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003808}
3809
3810/* Handle notification from uCode that card's power state is changing
3811 * due to software, hardware, or critical temperature RFKILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003812static void
3813il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003814{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003815 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003816 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003817 unsigned long status = il->status;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003818
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003819 D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003820 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3821 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
3822 (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003823
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003824 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003825
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003826 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003827 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003828
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003829 il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003830
3831 if (!(flags & RXON_CARD_DISABLED)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003832 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003833 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003834 il_wr(il, HBUS_TARG_MBX_C,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003835 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003836 }
3837 }
3838
3839 if (flags & CT_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003840 il4965_perform_ct_kill_task(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003841
3842 if (flags & HW_CARD_DISABLED)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003843 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003844 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003845 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003846
3847 if (!(flags & RXON_CARD_DISABLED))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003848 il_scan_cancel(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003849
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003850 if ((test_bit(S_RF_KILL_HW, &status) !=
3851 test_bit(S_RF_KILL_HW, &il->status)))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003852 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003853 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003854 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003855 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003856}
3857
3858/**
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003859 * il4965_setup_handlers - Initialize Rx handler callbacks
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003860 *
3861 * Setup the RX handlers for each of the reply types sent from the uCode
3862 * to the host.
3863 *
3864 * This function chains into the hardware specific files for them to setup
3865 * any hardware specific handlers as well.
3866 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003867static void
3868il4965_setup_handlers(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003869{
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003870 il->handlers[N_ALIVE] = il4965_hdl_alive;
3871 il->handlers[N_ERROR] = il_hdl_error;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003872 il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003873 il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003874 il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003875 il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003876 il->handlers[N_BEACON] = il4965_hdl_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003877
3878 /*
3879 * The same handler is used for both the REPLY to a discrete
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003880 * stats request from the host as well as for the periodic
3881 * stats notifications (after received beacons) from the uCode.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003882 */
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003883 il->handlers[C_STATS] = il4965_hdl_c_stats;
3884 il->handlers[N_STATS] = il4965_hdl_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003885
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003886 il_setup_rx_scan_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003887
3888 /* status change handler */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003889 il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003890
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003891 il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003892 /* Rx handlers */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003893 il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
3894 il->handlers[N_RX_MPDU] = il4965_hdl_rx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003895 /* block ack */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003896 il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003897 /* Set up hardware specific Rx handlers */
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003898 il->cfg->ops->lib->handler_setup(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003899}
3900
3901/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003902 * il4965_rx_handle - Main entry function for receiving responses from uCode
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003903 *
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003904 * Uses the il->handlers callback function array to invoke
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003905 * the appropriate handlers, including command responses,
3906 * frame-received notifications, and other notifications.
3907 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003908void
3909il4965_rx_handle(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003910{
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003911 struct il_rx_buf *rxb;
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003912 struct il_rx_pkt *pkt;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003913 struct il_rx_queue *rxq = &il->rxq;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003914 u32 r, i;
3915 int reclaim;
3916 unsigned long flags;
3917 u8 fill_rx = 0;
3918 u32 count = 8;
3919 int total_empty;
3920
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003921 /* uCode's read idx (stored in shared DRAM) indicates the last Rx
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003922 * buffer that the driver may process (last buffer filled by ucode). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003923 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003924 i = rxq->read;
3925
3926 /* Rx interrupt, but nothing sent from uCode */
3927 if (i == r)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003928 D_RX("r = %d, i = %d\n", r, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003929
3930 /* calculate total frames need to be restock after handling RX */
3931 total_empty = r - rxq->write_actual;
3932 if (total_empty < 0)
3933 total_empty += RX_QUEUE_SIZE;
3934
3935 if (total_empty > (RX_QUEUE_SIZE / 2))
3936 fill_rx = 1;
3937
3938 while (i != r) {
3939 int len;
3940
3941 rxb = rxq->queue[i];
3942
3943 /* If an RXB doesn't have a Rx queue slot associated with it,
3944 * then a bug has been introduced in the queue refilling
3945 * routines -- catch it here */
3946 BUG_ON(rxb == NULL);
3947
3948 rxq->queue[i] = NULL;
3949
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003950 pci_unmap_page(il->pci_dev, rxb->page_dma,
3951 PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003952 PCI_DMA_FROMDEVICE);
3953 pkt = rxb_addr(rxb);
3954
Stanislaw Gruszkae94a4092011-08-31 13:23:20 +02003955 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003956 len += sizeof(u32); /* account for status word */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003957
3958 /* Reclaim a command buffer only if this packet is a response
3959 * to a (driver-originated) command.
3960 * If the packet (e.g. Rx frame) originated from uCode,
3961 * there is no command buffer to reclaim.
3962 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3963 * but apparently a few don't get set; catch them here. */
3964 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003965 (pkt->hdr.cmd != N_RX_PHY) && (pkt->hdr.cmd != N_RX) &&
3966 (pkt->hdr.cmd != N_RX_MPDU) &&
3967 (pkt->hdr.cmd != N_COMPRESSED_BA) &&
3968 (pkt->hdr.cmd != N_STATS) && (pkt->hdr.cmd != C_TX);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003969
3970 /* Based on type of command response or notification,
3971 * handle those that need handling via function in
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003972 * handlers table. See il4965_setup_handlers() */
3973 if (il->handlers[pkt->hdr.cmd]) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003974 D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
3975 il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003976 il->isr_stats.handlers[pkt->hdr.cmd]++;
3977 il->handlers[pkt->hdr.cmd] (il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003978 } else {
3979 /* No handling needed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003980 D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
3981 i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003982 }
3983
3984 /*
3985 * XXX: After here, we should always check rxb->page
3986 * against NULL before touching it or its virtual
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003987 * memory (pkt). Because some handler might have
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003988 * already taken or freed the pages.
3989 */
3990
3991 if (reclaim) {
3992 /* Invoke any callbacks, transfer the buffer to caller,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003993 * and fire off the (possibly) blocking il_send_cmd()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003994 * as we reclaim the driver command queue */
3995 if (rxb->page)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003996 il_tx_cmd_complete(il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003997 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003998 IL_WARN("Claim null rxb?\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003999 }
4000
4001 /* Reuse the page if possible. For notification packets and
4002 * SKBs that fail to Rx correctly, add them back into the
4003 * rx_free list for reuse later. */
4004 spin_lock_irqsave(&rxq->lock, flags);
4005 if (rxb->page != NULL) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004006 rxb->page_dma =
4007 pci_map_page(il->pci_dev, rxb->page, 0,
4008 PAGE_SIZE << il->hw_params.
4009 rx_page_order, PCI_DMA_FROMDEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004010 list_add_tail(&rxb->list, &rxq->rx_free);
4011 rxq->free_count++;
4012 } else
4013 list_add_tail(&rxb->list, &rxq->rx_used);
4014
4015 spin_unlock_irqrestore(&rxq->lock, flags);
4016
4017 i = (i + 1) & RX_QUEUE_MASK;
4018 /* If there are a lot of unused frames,
4019 * restock the Rx queue so ucode wont assert. */
4020 if (fill_rx) {
4021 count++;
4022 if (count >= 8) {
4023 rxq->read = i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004024 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004025 count = 0;
4026 }
4027 }
4028 }
4029
4030 /* Backtrack one entry */
4031 rxq->read = i;
4032 if (fill_rx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004033 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004034 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004035 il4965_rx_queue_restock(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004036}
4037
4038/* call this function to flush any scheduled tasklet */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004039static inline void
4040il4965_synchronize_irq(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004041{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004042 /* wait to make sure we flush pending tasklet */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004043 synchronize_irq(il->pci_dev->irq);
4044 tasklet_kill(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004045}
4046
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004047static void
4048il4965_irq_tasklet(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004049{
4050 u32 inta, handled = 0;
4051 u32 inta_fh;
4052 unsigned long flags;
4053 u32 i;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004054#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004055 u32 inta_mask;
4056#endif
4057
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004058 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004059
4060 /* Ack/clear/reset pending uCode interrupts.
4061 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4062 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004063 inta = _il_rd(il, CSR_INT);
4064 _il_wr(il, CSR_INT, inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004065
4066 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4067 * Any new interrupts that happen after this, either while we're
4068 * in this tasklet, or later, will show up in next ISR/tasklet. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004069 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
4070 _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004071
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004072#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004073 if (il_get_debug_level(il) & IL_DL_ISR) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004074 /* just for debug */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004075 inta_mask = _il_rd(il, CSR_INT_MASK);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004076 D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
4077 inta_mask, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004078 }
4079#endif
4080
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004081 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004082
4083 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4084 * atomic, make sure that inta covers all the interrupts that
4085 * we've discovered, even if FH interrupt came in just after
4086 * reading CSR_INT. */
4087 if (inta_fh & CSR49_FH_INT_RX_MASK)
4088 inta |= CSR_INT_BIT_FH_RX;
4089 if (inta_fh & CSR49_FH_INT_TX_MASK)
4090 inta |= CSR_INT_BIT_FH_TX;
4091
4092 /* Now service all interrupt bits discovered above. */
4093 if (inta & CSR_INT_BIT_HW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004094 IL_ERR("Hardware error detected. Restarting.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004095
4096 /* Tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004097 il_disable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004098
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004099 il->isr_stats.hw++;
4100 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004101
4102 handled |= CSR_INT_BIT_HW_ERR;
4103
4104 return;
4105 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004106#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004107 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004108 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4109 if (inta & CSR_INT_BIT_SCD) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004110 D_ISR("Scheduler finished to transmit "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004111 "the frame/frames.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004112 il->isr_stats.sch++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004113 }
4114
4115 /* Alive notification via Rx interrupt will do the real work */
4116 if (inta & CSR_INT_BIT_ALIVE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004117 D_ISR("Alive interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004118 il->isr_stats.alive++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004119 }
4120 }
4121#endif
4122 /* Safely ignore these bits for debug checks below */
4123 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
4124
4125 /* HW RF KILL switch toggled */
4126 if (inta & CSR_INT_BIT_RF_KILL) {
4127 int hw_rf_kill = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004128 if (!
4129 (_il_rd(il, CSR_GP_CNTRL) &
4130 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004131 hw_rf_kill = 1;
4132
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004133 IL_WARN("RF_KILL bit toggled to %s.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004134 hw_rf_kill ? "disable radio" : "enable radio");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004135
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004136 il->isr_stats.rfkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004137
4138 /* driver only loads ucode once setting the interface up.
4139 * the driver allows loading the ucode even if the radio
4140 * is killed. Hence update the killswitch state here. The
4141 * rfkill handler will care about restarting if needed.
4142 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004143 if (!test_bit(S_ALIVE, &il->status)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004144 if (hw_rf_kill)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004145 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004146 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004147 clear_bit(S_RF_KILL_HW, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004148 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004149 }
4150
4151 handled |= CSR_INT_BIT_RF_KILL;
4152 }
4153
4154 /* Chip got too hot and stopped itself */
4155 if (inta & CSR_INT_BIT_CT_KILL) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004156 IL_ERR("Microcode CT kill error detected.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004157 il->isr_stats.ctkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004158 handled |= CSR_INT_BIT_CT_KILL;
4159 }
4160
4161 /* Error detected by uCode */
4162 if (inta & CSR_INT_BIT_SW_ERR) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004163 IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
4164 inta);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004165 il->isr_stats.sw++;
4166 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004167 handled |= CSR_INT_BIT_SW_ERR;
4168 }
4169
4170 /*
4171 * uCode wakes up after power-down sleep.
4172 * Tell device about any new tx or host commands enqueued,
4173 * and about any Rx buffers made available while asleep.
4174 */
4175 if (inta & CSR_INT_BIT_WAKEUP) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004176 D_ISR("Wakeup interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004177 il_rx_queue_update_write_ptr(il, &il->rxq);
4178 for (i = 0; i < il->hw_params.max_txq_num; i++)
4179 il_txq_update_write_ptr(il, &il->txq[i]);
4180 il->isr_stats.wakeup++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004181 handled |= CSR_INT_BIT_WAKEUP;
4182 }
4183
4184 /* All uCode command responses, including Tx command responses,
4185 * Rx "responses" (frame-received notification), and other
4186 * notifications from uCode come through here*/
4187 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004188 il4965_rx_handle(il);
4189 il->isr_stats.rx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004190 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4191 }
4192
4193 /* This "Tx" DMA channel is used only for loading uCode */
4194 if (inta & CSR_INT_BIT_FH_TX) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004195 D_ISR("uCode load interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004196 il->isr_stats.tx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004197 handled |= CSR_INT_BIT_FH_TX;
4198 /* Wake up uCode load routine, now that load is complete */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004199 il->ucode_write_complete = 1;
4200 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004201 }
4202
4203 if (inta & ~handled) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004204 IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004205 il->isr_stats.unhandled++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004206 }
4207
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004208 if (inta & ~(il->inta_mask)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004209 IL_WARN("Disabled INTA bits 0x%08x were pending\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004210 inta & ~il->inta_mask);
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004211 IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004212 }
4213
4214 /* Re-enable all interrupts */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02004215 /* only Re-enable if disabled by irq */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004216 if (test_bit(S_INT_ENABLED, &il->status))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004217 il_enable_interrupts(il);
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02004218 /* Re-enable RF_KILL if it occurred */
4219 else if (handled & CSR_INT_BIT_RF_KILL)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004220 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004221
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004222#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004223 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004224 inta = _il_rd(il, CSR_INT);
4225 inta_mask = _il_rd(il, CSR_INT_MASK);
4226 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004227 D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4228 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004229 }
4230#endif
4231}
4232
4233/*****************************************************************************
4234 *
4235 * sysfs attributes
4236 *
4237 *****************************************************************************/
4238
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004239#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004240
4241/*
4242 * The following adds a new attribute to the sysfs representation
4243 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
4244 * used for controlling the debug level.
4245 *
4246 * See the level definitions in iwl for details.
4247 *
4248 * The debug_level being managed using sysfs below is a per device debug
4249 * level that is used instead of the global debug level if it (the per
4250 * device debug level) is set.
4251 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004252static ssize_t
4253il4965_show_debug_level(struct device *d, struct device_attribute *attr,
4254 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004255{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004256 struct il_priv *il = dev_get_drvdata(d);
4257 return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004258}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004259
4260static ssize_t
4261il4965_store_debug_level(struct device *d, struct device_attribute *attr,
4262 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004263{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004264 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004265 unsigned long val;
4266 int ret;
4267
4268 ret = strict_strtoul(buf, 0, &val);
4269 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004270 IL_ERR("%s is not in hex or decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004271 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004272 il->debug_level = val;
4273 if (il_alloc_traffic_mem(il))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004274 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004275 }
4276 return strnlen(buf, count);
4277}
4278
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004279static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il4965_show_debug_level,
4280 il4965_store_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004281
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004282#endif /* CONFIG_IWLEGACY_DEBUG */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004283
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004284static ssize_t
4285il4965_show_temperature(struct device *d, struct device_attribute *attr,
4286 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004287{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004288 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004289
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004290 if (!il_is_alive(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004291 return -EAGAIN;
4292
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004293 return sprintf(buf, "%d\n", il->temperature);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004294}
4295
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004296static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004297
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004298static ssize_t
4299il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004300{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004301 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004302
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004303 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004304 return sprintf(buf, "off\n");
4305 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004306 return sprintf(buf, "%d\n", il->tx_power_user_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004307}
4308
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004309static ssize_t
4310il4965_store_tx_power(struct device *d, struct device_attribute *attr,
4311 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004312{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004313 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004314 unsigned long val;
4315 int ret;
4316
4317 ret = strict_strtoul(buf, 10, &val);
4318 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004319 IL_INFO("%s is not in decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004320 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004321 ret = il_set_tx_power(il, val, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004322 if (ret)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004323 IL_ERR("failed setting tx power (0x%d).\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004324 else
4325 ret = count;
4326 }
4327 return ret;
4328}
4329
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004330static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il4965_show_tx_power,
4331 il4965_store_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004332
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004333static struct attribute *il_sysfs_entries[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004334 &dev_attr_temperature.attr,
4335 &dev_attr_tx_power.attr,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004336#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004337 &dev_attr_debug_level.attr,
4338#endif
4339 NULL
4340};
4341
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004342static struct attribute_group il_attribute_group = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004343 .name = NULL, /* put in device directory */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004344 .attrs = il_sysfs_entries,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004345};
4346
4347/******************************************************************************
4348 *
4349 * uCode download functions
4350 *
4351 ******************************************************************************/
4352
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004353static void
4354il4965_dealloc_ucode_pci(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004355{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004356 il_free_fw_desc(il->pci_dev, &il->ucode_code);
4357 il_free_fw_desc(il->pci_dev, &il->ucode_data);
4358 il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
4359 il_free_fw_desc(il->pci_dev, &il->ucode_init);
4360 il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
4361 il_free_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004362}
4363
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004364static void
4365il4965_nic_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004366{
4367 /* Remove all resets to allow NIC to operate */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004368 _il_wr(il, CSR_RESET, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004369}
4370
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004371static void il4965_ucode_callback(const struct firmware *ucode_raw,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004372 void *context);
4373static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004374
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004375static int __must_check
4376il4965_request_firmware(struct il_priv *il, bool first)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004377{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004378 const char *name_pre = il->cfg->fw_name_pre;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004379 char tag[8];
4380
4381 if (first) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004382 il->fw_idx = il->cfg->ucode_api_max;
4383 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004384 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004385 il->fw_idx--;
4386 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004387 }
4388
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004389 if (il->fw_idx < il->cfg->ucode_api_min) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004390 IL_ERR("no suitable firmware found!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004391 return -ENOENT;
4392 }
4393
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004394 sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004395
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004396 D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004397
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004398 return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
4399 &il->pci_dev->dev, GFP_KERNEL, il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004400 il4965_ucode_callback);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004401}
4402
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004403struct il4965_firmware_pieces {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004404 const void *inst, *data, *init, *init_data, *boot;
4405 size_t inst_size, data_size, init_size, init_data_size, boot_size;
4406};
4407
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004408static int
4409il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
4410 struct il4965_firmware_pieces *pieces)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004411{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004412 struct il_ucode_header *ucode = (void *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004413 u32 api_ver, hdr_size;
4414 const u8 *src;
4415
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004416 il->ucode_ver = le32_to_cpu(ucode->ver);
4417 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004418
4419 switch (api_ver) {
4420 default:
4421 case 0:
4422 case 1:
4423 case 2:
4424 hdr_size = 24;
4425 if (ucode_raw->size < hdr_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004426 IL_ERR("File size too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004427 return -EINVAL;
4428 }
4429 pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
4430 pieces->data_size = le32_to_cpu(ucode->v1.data_size);
4431 pieces->init_size = le32_to_cpu(ucode->v1.init_size);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004432 pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004433 pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
4434 src = ucode->v1.data;
4435 break;
4436 }
4437
4438 /* Verify size of file vs. image size info in file's header */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004439 if (ucode_raw->size !=
4440 hdr_size + pieces->inst_size + pieces->data_size +
4441 pieces->init_size + pieces->init_data_size + pieces->boot_size) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004442
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004443 IL_ERR("uCode file size %d does not match expected size\n",
4444 (int)ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004445 return -EINVAL;
4446 }
4447
4448 pieces->inst = src;
4449 src += pieces->inst_size;
4450 pieces->data = src;
4451 src += pieces->data_size;
4452 pieces->init = src;
4453 src += pieces->init_size;
4454 pieces->init_data = src;
4455 src += pieces->init_data_size;
4456 pieces->boot = src;
4457 src += pieces->boot_size;
4458
4459 return 0;
4460}
4461
4462/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004463 * il4965_ucode_callback - callback when firmware was loaded
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004464 *
4465 * If loaded successfully, copies the firmware into buffers
4466 * for the card to fetch (via DMA).
4467 */
4468static void
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004469il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004470{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004471 struct il_priv *il = context;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004472 struct il_ucode_header *ucode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004473 int err;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004474 struct il4965_firmware_pieces pieces;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004475 const unsigned int api_max = il->cfg->ucode_api_max;
4476 const unsigned int api_min = il->cfg->ucode_api_min;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004477 u32 api_ver;
4478
4479 u32 max_probe_length = 200;
4480 u32 standard_phy_calibration_size =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004481 IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004482
4483 memset(&pieces, 0, sizeof(pieces));
4484
4485 if (!ucode_raw) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004486 if (il->fw_idx <= il->cfg->ucode_api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004487 IL_ERR("request for firmware file '%s' failed.\n",
4488 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004489 goto try_again;
4490 }
4491
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004492 D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
4493 ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004494
4495 /* Make sure that we got at least the API version number */
4496 if (ucode_raw->size < 4) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004497 IL_ERR("File size way too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004498 goto try_again;
4499 }
4500
4501 /* Data from ucode file: header followed by uCode images */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004502 ucode = (struct il_ucode_header *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004503
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004504 err = il4965_load_firmware(il, ucode_raw, &pieces);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004505
4506 if (err)
4507 goto try_again;
4508
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004509 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004510
4511 /*
4512 * api_ver should match the api version forming part of the
4513 * firmware filename ... but we don't check for that and only rely
4514 * on the API version read from firmware header from here on forward
4515 */
4516 if (api_ver < api_min || api_ver > api_max) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004517 IL_ERR("Driver unable to support your firmware API. "
4518 "Driver supports v%u, firmware is v%u.\n", api_max,
4519 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004520 goto try_again;
4521 }
4522
4523 if (api_ver != api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004524 IL_ERR("Firmware has old API version. Expected v%u, "
4525 "got v%u. New firmware can be obtained "
4526 "from http://www.intellinuxwireless.org.\n", api_max,
4527 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004528
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004529 IL_INFO("loaded firmware version %u.%u.%u.%u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004530 IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
4531 IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004532
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004533 snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
4534 "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
4535 IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004536 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004537
4538 /*
4539 * For any of the failures below (before allocating pci memory)
4540 * we will try to load a version with a smaller API -- maybe the
4541 * user just got a corrupted version of the latest API.
4542 */
4543
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004544 D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
4545 D_INFO("f/w package hdr runtime inst size = %Zd\n", pieces.inst_size);
4546 D_INFO("f/w package hdr runtime data size = %Zd\n", pieces.data_size);
4547 D_INFO("f/w package hdr init inst size = %Zd\n", pieces.init_size);
4548 D_INFO("f/w package hdr init data size = %Zd\n", pieces.init_data_size);
4549 D_INFO("f/w package hdr boot inst size = %Zd\n", pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004550
4551 /* Verify that uCode images will fit in card's SRAM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004552 if (pieces.inst_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004553 IL_ERR("uCode instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004554 pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004555 goto try_again;
4556 }
4557
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004558 if (pieces.data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004559 IL_ERR("uCode data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004560 pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004561 goto try_again;
4562 }
4563
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004564 if (pieces.init_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004565 IL_ERR("uCode init instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004566 pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004567 goto try_again;
4568 }
4569
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004570 if (pieces.init_data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004571 IL_ERR("uCode init data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004572 pieces.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004573 goto try_again;
4574 }
4575
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004576 if (pieces.boot_size > il->hw_params.max_bsm_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004577 IL_ERR("uCode boot instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004578 pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004579 goto try_again;
4580 }
4581
4582 /* Allocate ucode buffers for card's bus-master loading ... */
4583
4584 /* Runtime instructions and 2 copies of data:
4585 * 1) unmodified from disk
4586 * 2) backup cache for save/restore during power-downs */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004587 il->ucode_code.len = pieces.inst_size;
4588 il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004589
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004590 il->ucode_data.len = pieces.data_size;
4591 il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004592
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004593 il->ucode_data_backup.len = pieces.data_size;
4594 il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004595
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004596 if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
4597 !il->ucode_data_backup.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004598 goto err_pci_alloc;
4599
4600 /* Initialization instructions and data */
4601 if (pieces.init_size && pieces.init_data_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004602 il->ucode_init.len = pieces.init_size;
4603 il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004604
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004605 il->ucode_init_data.len = pieces.init_data_size;
4606 il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004607
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004608 if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004609 goto err_pci_alloc;
4610 }
4611
4612 /* Bootstrap (instructions only, no data) */
4613 if (pieces.boot_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004614 il->ucode_boot.len = pieces.boot_size;
4615 il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004616
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004617 if (!il->ucode_boot.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004618 goto err_pci_alloc;
4619 }
4620
4621 /* Now that we can no longer fail, copy information */
4622
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004623 il->sta_key_max_num = STA_KEY_MAX_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004624
4625 /* Copy images into buffers for card's bus-master reads ... */
4626
4627 /* Runtime instructions (first block of data in file) */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004628 D_INFO("Copying (but not loading) uCode instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004629 pieces.inst_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004630 memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004631
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004632 D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004633 il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004634
4635 /*
4636 * Runtime data
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004637 * NOTE: Copy into backup buffer will be done in il_up()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004638 */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004639 D_INFO("Copying (but not loading) uCode data len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004640 pieces.data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004641 memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
4642 memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004643
4644 /* Initialization instructions */
4645 if (pieces.init_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004646 D_INFO("Copying (but not loading) init instr len %Zd\n",
4647 pieces.init_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004648 memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004649 }
4650
4651 /* Initialization data */
4652 if (pieces.init_data_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004653 D_INFO("Copying (but not loading) init data len %Zd\n",
4654 pieces.init_data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004655 memcpy(il->ucode_init_data.v_addr, pieces.init_data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004656 pieces.init_data_size);
4657 }
4658
4659 /* Bootstrap instructions */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004660 D_INFO("Copying (but not loading) boot instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004661 pieces.boot_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004662 memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004663
4664 /*
4665 * figure out the offset of chain noise reset and gain commands
4666 * base on the size of standard phy calibration commands table size
4667 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004668 il->_4965.phy_calib_chain_noise_reset_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004669 standard_phy_calibration_size;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004670 il->_4965.phy_calib_chain_noise_gain_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004671 standard_phy_calibration_size + 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004672
4673 /**************************************************
4674 * This is still part of probe() in a sense...
4675 *
4676 * 9. Setup and register with mac80211 and debugfs
4677 **************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004678 err = il4965_mac_setup_register(il, max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004679 if (err)
4680 goto out_unbind;
4681
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004682 err = il_dbgfs_register(il, DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004683 if (err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004684 IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
4685 err);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004686
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004687 err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004688 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004689 IL_ERR("failed to create sysfs device attributes\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004690 goto out_unbind;
4691 }
4692
4693 /* We have our copies now, allow OS release its copies */
4694 release_firmware(ucode_raw);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004695 complete(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004696 return;
4697
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004698try_again:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004699 /* try next, if any */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004700 if (il4965_request_firmware(il, false))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004701 goto out_unbind;
4702 release_firmware(ucode_raw);
4703 return;
4704
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004705err_pci_alloc:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004706 IL_ERR("failed to allocate pci memory\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004707 il4965_dealloc_ucode_pci(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004708out_unbind:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004709 complete(&il->_4965.firmware_loading_complete);
4710 device_release_driver(&il->pci_dev->dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004711 release_firmware(ucode_raw);
4712}
4713
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004714static const char *const desc_lookup_text[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004715 "OK",
4716 "FAIL",
4717 "BAD_PARAM",
4718 "BAD_CHECKSUM",
4719 "NMI_INTERRUPT_WDG",
4720 "SYSASSERT",
4721 "FATAL_ERROR",
4722 "BAD_COMMAND",
4723 "HW_ERROR_TUNE_LOCK",
4724 "HW_ERROR_TEMPERATURE",
4725 "ILLEGAL_CHAN_FREQ",
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +02004726 "VCC_NOT_STBL",
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004727 "FH49_ERROR",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004728 "NMI_INTERRUPT_HOST",
4729 "NMI_INTERRUPT_ACTION_PT",
4730 "NMI_INTERRUPT_UNKNOWN",
4731 "UCODE_VERSION_MISMATCH",
4732 "HW_ERROR_ABS_LOCK",
4733 "HW_ERROR_CAL_LOCK_FAIL",
4734 "NMI_INTERRUPT_INST_ACTION_PT",
4735 "NMI_INTERRUPT_DATA_ACTION_PT",
4736 "NMI_TRM_HW_ER",
4737 "NMI_INTERRUPT_TRM",
Joe Perches861d9c32011-07-08 23:20:24 -07004738 "NMI_INTERRUPT_BREAK_POINT",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004739 "DEBUG_0",
4740 "DEBUG_1",
4741 "DEBUG_2",
4742 "DEBUG_3",
4743};
4744
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004745static struct {
4746 char *name;
4747 u8 num;
4748} advanced_lookup[] = {
4749 {
4750 "NMI_INTERRUPT_WDG", 0x34}, {
4751 "SYSASSERT", 0x35}, {
4752 "UCODE_VERSION_MISMATCH", 0x37}, {
4753 "BAD_COMMAND", 0x38}, {
4754 "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
4755 "FATAL_ERROR", 0x3D}, {
4756 "NMI_TRM_HW_ERR", 0x46}, {
4757 "NMI_INTERRUPT_TRM", 0x4C}, {
4758 "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
4759 "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
4760 "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
4761 "NMI_INTERRUPT_HOST", 0x66}, {
4762 "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
4763 "NMI_INTERRUPT_UNKNOWN", 0x84}, {
4764 "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
4765"ADVANCED_SYSASSERT", 0},};
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004766
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004767static const char *
4768il4965_desc_lookup(u32 num)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004769{
4770 int i;
4771 int max = ARRAY_SIZE(desc_lookup_text);
4772
4773 if (num < max)
4774 return desc_lookup_text[num];
4775
4776 max = ARRAY_SIZE(advanced_lookup) - 1;
4777 for (i = 0; i < max; i++) {
4778 if (advanced_lookup[i].num == num)
4779 break;
4780 }
4781 return advanced_lookup[i].name;
4782}
4783
4784#define ERROR_START_OFFSET (1 * sizeof(u32))
4785#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4786
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004787void
4788il4965_dump_nic_error_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004789{
4790 u32 data2, line;
4791 u32 desc, time, count, base, data1;
4792 u32 blink1, blink2, ilink1, ilink2;
4793 u32 pc, hcmd;
4794
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004795 if (il->ucode_type == UCODE_INIT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004796 base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004797 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004798 base = le32_to_cpu(il->card_alive.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004799
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004800 if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004801 IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
4802 base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004803 return;
4804 }
4805
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004806 count = il_read_targ_mem(il, base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004807
4808 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004809 IL_ERR("Start IWL Error Log Dump:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004810 IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004811 }
4812
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004813 desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
4814 il->isr_stats.err_code = desc;
4815 pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
4816 blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
4817 blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
4818 ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
4819 ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
4820 data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
4821 data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
4822 line = il_read_targ_mem(il, base + 9 * sizeof(u32));
4823 time = il_read_targ_mem(il, base + 11 * sizeof(u32));
4824 hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004825
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004826 IL_ERR("Desc Time "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004827 "data1 data2 line\n");
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004828 IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004829 il4965_desc_lookup(desc), desc, time, data1, data2, line);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004830 IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004831 IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
4832 blink2, ilink1, ilink2, hcmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004833}
4834
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004835static void
4836il4965_rf_kill_ct_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004837{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004838 struct il_ct_kill_config cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004839 unsigned long flags;
4840 int ret = 0;
4841
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004842 spin_lock_irqsave(&il->lock, flags);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004843 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004844 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004845 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004846
4847 cmd.critical_temperature_R =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004848 cpu_to_le32(il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004849
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004850 ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004851 if (ret)
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004852 IL_ERR("C_CT_KILL_CONFIG failed\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004853 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004854 D_INFO("C_CT_KILL_CONFIG " "succeeded, "
4855 "critical temperature is %d\n",
4856 il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004857}
4858
4859static const s8 default_queue_to_tx_fifo[] = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004860 IL_TX_FIFO_VO,
4861 IL_TX_FIFO_VI,
4862 IL_TX_FIFO_BE,
4863 IL_TX_FIFO_BK,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004864 IL49_CMD_FIFO_NUM,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004865 IL_TX_FIFO_UNUSED,
4866 IL_TX_FIFO_UNUSED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004867};
4868
Stanislaw Gruszkae53aac42011-08-31 11:18:16 +02004869#define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
4870
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004871static int
4872il4965_alive_notify(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004873{
4874 u32 a;
4875 unsigned long flags;
4876 int i, chan;
4877 u32 reg_val;
4878
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004879 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004880
4881 /* Clear 4965's internal Tx Scheduler data base */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004882 il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004883 a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
4884 for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004885 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004886 for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004887 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004888 for (;
4889 a <
4890 il->scd_base_addr +
4891 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
4892 a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004893 il_write_targ_mem(il, a, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004894
4895 /* Tel 4965 where to find Tx byte count tables */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004896 il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004897
4898 /* Enable DMA channel */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004899 for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
4900 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
4901 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
4902 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004903
4904 /* Update FH chicken bits */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004905 reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
4906 il_wr(il, FH49_TX_CHICKEN_BITS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004907 reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004908
4909 /* Disable chain mode for all queues */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004910 il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004911
4912 /* Initialize each Tx queue (including the command queue) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004913 for (i = 0; i < il->hw_params.max_txq_num; i++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004914
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004915 /* TFD circular buffer read/write idxes */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004916 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004917 il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004918
4919 /* Max Tx Window size for Scheduler-ACK mode */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004920 il_write_targ_mem(il,
4921 il->scd_base_addr +
4922 IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
4923 (SCD_WIN_SIZE <<
4924 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
4925 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004926
4927 /* Frame limit */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004928 il_write_targ_mem(il,
4929 il->scd_base_addr +
4930 IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
4931 sizeof(u32),
4932 (SCD_FRAME_LIMIT <<
4933 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
4934 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004935
4936 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004937 il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004938 (1 << il->hw_params.max_txq_num) - 1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004939
4940 /* Activate all Tx DMA/FIFO channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004941 il4965_txq_set_sched(il, IL_MASK(0, 6));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004942
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004943 il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004944
4945 /* make sure all queue are not stopped */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004946 memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004947 for (i = 0; i < 4; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004948 atomic_set(&il->queue_stop_count[i], 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004949
4950 /* reset to 0 to enable all the queue first */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004951 il->txq_ctx_active_msk = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004952 /* Map each Tx/cmd queue to its corresponding fifo */
4953 BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
4954
4955 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
4956 int ac = default_queue_to_tx_fifo[i];
4957
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004958 il_txq_ctx_activate(il, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004959
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004960 if (ac == IL_TX_FIFO_UNUSED)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004961 continue;
4962
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004963 il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004964 }
4965
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004966 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004967
4968 return 0;
4969}
4970
4971/**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004972 * il4965_alive_start - called after N_ALIVE notification received
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004973 * from protocol/runtime uCode (initialization uCode's
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004974 * Alive gets handled by il_init_alive_start()).
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004975 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004976static void
4977il4965_alive_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004978{
4979 int ret = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01004980 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004981
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004982 D_INFO("Runtime Alive received.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004983
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004984 if (il->card_alive.is_valid != UCODE_VALID_OK) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004985 /* We had an error bringing up the hardware, so take it
4986 * all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004987 D_INFO("Alive failed.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004988 goto restart;
4989 }
4990
4991 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4992 * This is a paranoid check, because we would not have gotten the
4993 * "runtime" alive if code weren't properly loaded. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004994 if (il4965_verify_ucode(il)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004995 /* Runtime instruction load was bad;
4996 * take it all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004997 D_INFO("Bad runtime uCode load.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004998 goto restart;
4999 }
5000
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005001 ret = il4965_alive_notify(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005002 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005003 IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005004 goto restart;
5005 }
5006
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005007 /* After the ALIVE response, we can send host commands to the uCode */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005008 set_bit(S_ALIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005009
5010 /* Enable watchdog to monitor the driver tx queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005011 il_setup_watchdog(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005012
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005013 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005014 return;
5015
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005016 ieee80211_wake_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005017
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005018 il->active_rate = RATES_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005019
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005020 if (il_is_associated(il)) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005021 struct il_rxon_cmd *active_rxon =
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005022 (struct il_rxon_cmd *)&il->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005023 /* apply any changes in staging */
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005024 il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005025 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5026 } else {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005027 /* Initialize our rx_config data */
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005028 il_connection_init_rx_config(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005029
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005030 if (il->cfg->ops->hcmd->set_rxon_chain)
5031 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005032 }
5033
5034 /* Configure bluetooth coexistence if enabled */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005035 il_send_bt_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005036
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005037 il4965_reset_run_time_calib(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005038
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005039 set_bit(S_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005040
5041 /* Configure the adapter for unassociated operation */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005042 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005043
5044 /* At this point, the NIC is initialized and operational */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005045 il4965_rf_kill_ct_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005046
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005047 D_INFO("ALIVE processing complete.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005048 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005049
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005050 il_power_update_mode(il, true);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005051 D_INFO("Updated power mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005052
5053 return;
5054
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005055restart:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005056 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005057}
5058
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005059static void il4965_cancel_deferred_work(struct il_priv *il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005060
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005061static void
5062__il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005063{
5064 unsigned long flags;
Stanislaw Gruszkaab42b402011-04-28 11:51:24 +02005065 int exit_pending;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005066
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005067 D_INFO(DRV_NAME " is going down\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005068
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005069 il_scan_cancel_timeout(il, 200);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005070
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005071 exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005072
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005073 /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005074 * to prevent rearm timer */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005075 del_timer_sync(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005076
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005077 il_clear_ucode_stations(il, NULL);
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01005078
5079 /* FIXME: race conditions ? */
5080 spin_lock_irq(&il->sta_lock);
5081 /*
5082 * Remove all key information that is not stored as part
5083 * of station information since mac80211 may not have had
5084 * a chance to remove all the keys. When device is
5085 * reconfigured by mac80211 after an error all keys will
5086 * be reconfigured.
5087 */
5088 memset(il->_4965.wep_keys, 0, sizeof(il->_4965.wep_keys));
5089 il->_4965.key_mapping_keys = 0;
5090 spin_unlock_irq(&il->sta_lock);
5091
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005092 il_dealloc_bcast_stations(il);
5093 il_clear_driver_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005094
5095 /* Unblock any waiting calls */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005096 wake_up_all(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005097
5098 /* Wipe out the EXIT_PENDING status bit if we are not actually
5099 * exiting the module */
5100 if (!exit_pending)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005101 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005102
5103 /* stop and reset the on-board processor */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005104 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005105
5106 /* tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005107 spin_lock_irqsave(&il->lock, flags);
5108 il_disable_interrupts(il);
5109 spin_unlock_irqrestore(&il->lock, flags);
5110 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005111
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005112 if (il->mac80211_registered)
5113 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005114
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005115 /* If we have not previously called il_init() then
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005116 * clear all bits but the RF Kill bit and return */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005117 if (!il_is_init(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005118 il->status =
5119 test_bit(S_RF_KILL_HW,
5120 &il->
5121 status) << S_RF_KILL_HW |
5122 test_bit(S_GEO_CONFIGURED,
5123 &il->
5124 status) << S_GEO_CONFIGURED |
5125 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005126 goto exit;
5127 }
5128
5129 /* ...otherwise clear out all the status bits but the RF Kill
5130 * bit and continue taking the NIC down. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005131 il->status &=
5132 test_bit(S_RF_KILL_HW,
5133 &il->status) << S_RF_KILL_HW | test_bit(S_GEO_CONFIGURED,
5134 &il->
5135 status) <<
5136 S_GEO_CONFIGURED | test_bit(S_FW_ERROR,
5137 &il->
5138 status) << S_FW_ERROR |
5139 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005140
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005141 il4965_txq_ctx_stop(il);
5142 il4965_rxq_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005143
5144 /* Power-down device's busmaster DMA clocks */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02005145 il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005146 udelay(5);
5147
5148 /* Make sure (redundant) we've released our request to stay awake */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005149 il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005150
5151 /* Stop the device, and put it in low power state */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005152 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005153
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005154exit:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005155 memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005156
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005157 dev_kfree_skb(il->beacon_skb);
5158 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005159
5160 /* clear out any free frames */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005161 il4965_clear_free_frames(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005162}
5163
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005164static void
5165il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005166{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005167 mutex_lock(&il->mutex);
5168 __il4965_down(il);
5169 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005170
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005171 il4965_cancel_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005172}
5173
5174#define HW_READY_TIMEOUT (50)
5175
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005176static int
5177il4965_set_hw_ready(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005178{
5179 int ret = 0;
5180
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005181 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005182 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005183
5184 /* See if we got it */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005185 ret =
5186 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5187 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5188 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, HW_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005189 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005190 il->hw_ready = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005191 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005192 il->hw_ready = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005193
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005194 D_INFO("hardware %s\n", (il->hw_ready == 1) ? "ready" : "not ready");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005195 return ret;
5196}
5197
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005198static int
5199il4965_prepare_card_hw(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005200{
5201 int ret = 0;
5202
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005203 D_INFO("il4965_prepare_card_hw enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005204
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005205 ret = il4965_set_hw_ready(il);
5206 if (il->hw_ready)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005207 return ret;
5208
5209 /* If HW is not ready, prepare the conditions to check again */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005210 il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005211
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005212 ret =
5213 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5214 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
5215 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005216
5217 /* HW should be ready by now, check again. */
5218 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005219 il4965_set_hw_ready(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005220
5221 return ret;
5222}
5223
5224#define MAX_HW_RESTARTS 5
5225
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005226static int
5227__il4965_up(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005228{
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005229 int i;
5230 int ret;
5231
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005232 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005233 IL_WARN("Exit pending; will not bring the NIC up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005234 return -EIO;
5235 }
5236
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005237 if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005238 IL_ERR("ucode not available for device bringup\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005239 return -EIO;
5240 }
5241
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005242 ret = il4965_alloc_bcast_station(il, &il->ctx);
5243 if (ret) {
5244 il_dealloc_bcast_stations(il);
5245 return ret;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005246 }
5247
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005248 il4965_prepare_card_hw(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005249
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005250 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005251 IL_WARN("Exit HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005252 return -EIO;
5253 }
5254
5255 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005256 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005257 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005258 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005259 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005260
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005261 if (il_is_rfkill(il)) {
5262 wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005263
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005264 il_enable_interrupts(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005265 IL_WARN("Radio disabled by HW RF Kill switch\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005266 return 0;
5267 }
5268
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005269 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005270
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005271 /* must be initialised before il_hw_nic_init */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005272 il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005273
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005274 ret = il4965_hw_nic_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005275 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005276 IL_ERR("Unable to init nic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005277 return ret;
5278 }
5279
5280 /* make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005281 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005282 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005283
5284 /* clear (again), then enable host interrupts */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005285 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005286 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005287
5288 /* really make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005289 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5290 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005291
5292 /* Copy original ucode data image from disk into backup cache.
5293 * This will be used to initialize the on-board processor's
5294 * data SRAM for a clean start when the runtime program first loads. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005295 memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
5296 il->ucode_data.len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005297
5298 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5299
5300 /* load bootstrap state machine,
5301 * load bootstrap program into processor's memory,
5302 * prepare to load the "initialize" uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005303 ret = il->cfg->ops->lib->load_ucode(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005304
5305 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005306 IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005307 continue;
5308 }
5309
5310 /* start card; "initialize" will load runtime ucode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005311 il4965_nic_start(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005312
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005313 D_INFO(DRV_NAME " is coming up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005314
5315 return 0;
5316 }
5317
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005318 set_bit(S_EXIT_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005319 __il4965_down(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005320 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005321
5322 /* tried to restart and config the device for as long as our
5323 * patience could withstand */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005324 IL_ERR("Unable to initialize device after %d attempts.\n", i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005325 return -EIO;
5326}
5327
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005328/*****************************************************************************
5329 *
5330 * Workqueue callbacks
5331 *
5332 *****************************************************************************/
5333
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005334static void
5335il4965_bg_init_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005336{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005337 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005338 container_of(data, struct il_priv, init_alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005339
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005340 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005341 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005342 goto out;
5343
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005344 il->cfg->ops->lib->init_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005345out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005346 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005347}
5348
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005349static void
5350il4965_bg_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005351{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005352 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005353 container_of(data, struct il_priv, alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005354
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005355 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005356 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005357 goto out;
5358
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005359 il4965_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005360out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005361 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005362}
5363
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005364static void
5365il4965_bg_run_time_calib_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005366{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005367 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005368 run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005369
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005370 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005371
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005372 if (test_bit(S_EXIT_PENDING, &il->status) ||
5373 test_bit(S_SCANNING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005374 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005375 return;
5376 }
5377
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005378 if (il->start_calib) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005379 il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
5380 il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005381 }
5382
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005383 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005384}
5385
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005386static void
5387il4965_bg_restart(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005388{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005389 struct il_priv *il = container_of(data, struct il_priv, restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005390
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005391 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005392 return;
5393
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005394 if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005395 mutex_lock(&il->mutex);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005396 il->ctx.vif = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005397 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005398
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005399 __il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005400
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005401 mutex_unlock(&il->mutex);
5402 il4965_cancel_deferred_work(il);
5403 ieee80211_restart_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005404 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005405 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005406
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005407 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005408 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005409 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005410 return;
5411 }
5412
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005413 __il4965_up(il);
5414 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005415 }
5416}
5417
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005418static void
5419il4965_bg_rx_replenish(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005420{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005421 struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005422
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005423 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005424 return;
5425
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005426 mutex_lock(&il->mutex);
5427 il4965_rx_replenish(il);
5428 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005429}
5430
5431/*****************************************************************************
5432 *
5433 * mac80211 entry point functions
5434 *
5435 *****************************************************************************/
5436
5437#define UCODE_READY_TIMEOUT (4 * HZ)
5438
5439/*
5440 * Not a mac80211 entry point function, but it fits in with all the
5441 * other mac80211 functions grouped here.
5442 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005443static int
5444il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005445{
5446 int ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005447 struct ieee80211_hw *hw = il->hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005448
5449 hw->rate_control_algorithm = "iwl-4965-rs";
5450
5451 /* Tell mac80211 our characteristics */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005452 hw->flags =
5453 IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_AMPDU_AGGREGATION |
5454 IEEE80211_HW_NEED_DTIM_PERIOD | IEEE80211_HW_SPECTRUM_MGMT |
5455 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005456
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005457 if (il->cfg->sku & IL_SKU_N)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005458 hw->flags |=
5459 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
5460 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005461
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005462 hw->sta_data_size = sizeof(struct il_station_priv);
5463 hw->vif_data_size = sizeof(struct il_vif_priv);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005464
Stanislaw Gruszka8c9c48d2012-02-03 17:31:50 +01005465 hw->wiphy->interface_modes =
5466 BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005467
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005468 hw->wiphy->flags |=
5469 WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005470
5471 /*
5472 * For now, disable PS by default because it affects
5473 * RX performance significantly.
5474 */
5475 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5476
5477 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
5478 /* we create the 802.11 header and a zero-length SSID element */
5479 hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
5480
5481 /* Default value; 4 EDCA QOS priorities */
5482 hw->queues = 4;
5483
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005484 hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005485
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005486 if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
5487 il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005488 &il->bands[IEEE80211_BAND_2GHZ];
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005489 if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
5490 il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005491 &il->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005492
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005493 il_leds_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005494
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005495 ret = ieee80211_register_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005496 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005497 IL_ERR("Failed to register hw (error %d)\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005498 return ret;
5499 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005500 il->mac80211_registered = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005501
5502 return 0;
5503}
5504
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005505int
5506il4965_mac_start(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005507{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005508 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005509 int ret;
5510
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005511 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005512
5513 /* we should be verifying the device is ready to be opened */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005514 mutex_lock(&il->mutex);
5515 ret = __il4965_up(il);
5516 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005517
5518 if (ret)
5519 return ret;
5520
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005521 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005522 goto out;
5523
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005524 D_INFO("Start UP work done.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005525
5526 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5527 * mac80211 will not be run successfully. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005528 ret = wait_event_timeout(il->wait_command_queue,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005529 test_bit(S_READY, &il->status),
5530 UCODE_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005531 if (!ret) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005532 if (!test_bit(S_READY, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005533 IL_ERR("START_ALIVE timeout after %dms.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005534 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5535 return -ETIMEDOUT;
5536 }
5537 }
5538
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005539 il4965_led_enable(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005540
5541out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005542 il->is_open = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005543 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005544 return 0;
5545}
5546
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005547void
5548il4965_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005549{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005550 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005551
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005552 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005553
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005554 if (!il->is_open)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005555 return;
5556
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005557 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005558
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005559 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005560
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005561 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005562
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02005563 /* User space software may expect getting rfkill changes
5564 * even if interface is down */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005565 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005566 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005567
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005568 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005569}
5570
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005571void
5572il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005573{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005574 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005575
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005576 D_MACDUMP("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005577
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005578 D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005579 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005580
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005581 if (il4965_tx_skb(il, skb))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005582 dev_kfree_skb_any(skb);
5583
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005584 D_MACDUMP("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005585}
5586
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005587void
5588il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5589 struct ieee80211_key_conf *keyconf,
5590 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005591{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005592 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005593 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005594
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005595 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005596
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005597 il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta, iv32,
5598 phase1key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005599
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005600 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005601}
5602
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005603int
5604il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5605 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5606 struct ieee80211_key_conf *key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005607{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005608 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005609 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
5610 struct il_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005611 int ret;
5612 u8 sta_id;
5613 bool is_default_wep_key = false;
5614
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005615 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005616
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005617 if (il->cfg->mod_params->sw_crypto) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005618 D_MAC80211("leave - hwcrypto disabled\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005619 return -EOPNOTSUPP;
5620 }
5621
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005622 sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005623 if (sta_id == IL_INVALID_STATION)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005624 return -EINVAL;
5625
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005626 mutex_lock(&il->mutex);
5627 il_scan_cancel_timeout(il, 100);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005628
5629 /*
5630 * If we are getting WEP group key and we didn't receive any key mapping
5631 * so far, we are in legacy wep mode (group key only), otherwise we are
5632 * in 1X mode.
5633 * In legacy wep mode, we use another host command to the uCode.
5634 */
5635 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005636 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005637 if (cmd == SET_KEY)
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01005638 is_default_wep_key = !il->_4965.key_mapping_keys;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005639 else
5640 is_default_wep_key =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005641 (key->hw_key_idx == HW_KEY_DEFAULT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005642 }
5643
5644 switch (cmd) {
5645 case SET_KEY:
5646 if (is_default_wep_key)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005647 ret =
5648 il4965_set_default_wep_key(il, vif_priv->ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005649 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005650 ret =
5651 il4965_set_dynamic_key(il, vif_priv->ctx, key,
5652 sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005653
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005654 D_MAC80211("enable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005655 break;
5656 case DISABLE_KEY:
5657 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005658 ret = il4965_remove_default_wep_key(il, ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005659 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005660 ret = il4965_remove_dynamic_key(il, ctx, key, sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005661
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005662 D_MAC80211("disable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005663 break;
5664 default:
5665 ret = -EINVAL;
5666 }
5667
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005668 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005669 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005670
5671 return ret;
5672}
5673
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005674int
5675il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5676 enum ieee80211_ampdu_mlme_action action,
5677 struct ieee80211_sta *sta, u16 tid, u16 * ssn,
5678 u8 buf_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005679{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005680 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005681 int ret = -EINVAL;
5682
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005683 D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005684
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005685 if (!(il->cfg->sku & IL_SKU_N))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005686 return -EACCES;
5687
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005688 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005689
5690 switch (action) {
5691 case IEEE80211_AMPDU_RX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005692 D_HT("start Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005693 ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005694 break;
5695 case IEEE80211_AMPDU_RX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005696 D_HT("stop Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005697 ret = il4965_sta_rx_agg_stop(il, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005698 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005699 ret = 0;
5700 break;
5701 case IEEE80211_AMPDU_TX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005702 D_HT("start Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005703 ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005704 break;
5705 case IEEE80211_AMPDU_TX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005706 D_HT("stop Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005707 ret = il4965_tx_agg_stop(il, vif, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005708 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005709 ret = 0;
5710 break;
5711 case IEEE80211_AMPDU_TX_OPERATIONAL:
5712 ret = 0;
5713 break;
5714 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005715 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005716
5717 return ret;
5718}
5719
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005720int
5721il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5722 struct ieee80211_sta *sta)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005723{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005724 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005725 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
5726 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005727 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
5728 int ret;
5729 u8 sta_id;
5730
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005731 D_INFO("received request to add station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005732 mutex_lock(&il->mutex);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005733 D_INFO("proceeding to add station %pM\n", sta->addr);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005734 sta_priv->common.sta_id = IL_INVALID_STATION;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005735
5736 atomic_set(&sta_priv->pending_frames, 0);
5737
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005738 ret =
5739 il_add_station_common(il, vif_priv->ctx, sta->addr, is_ap, sta,
5740 &sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005741 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005742 IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005743 /* Should we return success if return code is EEXIST ? */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005744 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005745 return ret;
5746 }
5747
5748 sta_priv->common.sta_id = sta_id;
5749
5750 /* Initialize rate scaling */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005751 D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005752 il4965_rs_rate_init(il, sta, sta_id);
5753 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005754
5755 return 0;
5756}
5757
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005758void
5759il4965_mac_channel_switch(struct ieee80211_hw *hw,
5760 struct ieee80211_channel_switch *ch_switch)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005761{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005762 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005763 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005764 struct ieee80211_conf *conf = &hw->conf;
5765 struct ieee80211_channel *channel = ch_switch->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005766 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005767
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005768 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005769 u16 ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005770
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005771 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005772
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005773 mutex_lock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005774
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005775 if (il_is_rfkill(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005776 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005777
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005778 if (test_bit(S_EXIT_PENDING, &il->status) ||
5779 test_bit(S_SCANNING, &il->status) ||
5780 test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005781 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005782
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005783 if (!il_is_associated(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005784 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005785
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005786 if (!il->cfg->ops->lib->set_channel_switch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005787 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005788
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005789 ch = channel->hw_value;
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005790 if (le16_to_cpu(il->active.channel) == ch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005791 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005792
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005793 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005794 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005795 D_MAC80211("invalid channel\n");
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005796 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005797 }
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005798
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005799 spin_lock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005800
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005801 il->current_ht_config.smps = conf->smps_mode;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005802
5803 /* Configure HT40 channels */
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005804 il->ht.enabled = conf_is_ht(conf);
5805 if (il->ht.enabled) {
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005806 if (conf_is_ht40_minus(conf)) {
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005807 il->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005808 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005809 il->ht.is_40mhz = true;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005810 } else if (conf_is_ht40_plus(conf)) {
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005811 il->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005812 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005813 il->ht.is_40mhz = true;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005814 } else {
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005815 il->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005816 IEEE80211_HT_PARAM_CHA_SEC_NONE;
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005817 il->ht.is_40mhz = false;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005818 }
5819 } else
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005820 il->ht.is_40mhz = false;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005821
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005822 if ((le16_to_cpu(il->staging.channel) != ch))
5823 il->staging.flags = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005824
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005825 il_set_rxon_channel(il, channel, ctx);
5826 il_set_rxon_ht(il, ht_conf);
5827 il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005828
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005829 spin_unlock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005830
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005831 il_set_rate(il);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005832 /*
5833 * at this point, staging_rxon has the
5834 * configuration for channel switch
5835 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005836 set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005837 il->switch_channel = cpu_to_le16(ch);
5838 if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005839 clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005840 il->switch_channel = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005841 ieee80211_chswitch_done(ctx->vif, false);
5842 }
5843
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005844out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005845 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005846 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005847}
5848
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005849void
5850il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
5851 unsigned int *total_flags, u64 multicast)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005852{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005853 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005854 __le32 filter_or = 0, filter_nand = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005855
5856#define CHK(test, flag) do { \
5857 if (*total_flags & (test)) \
5858 filter_or |= (flag); \
5859 else \
5860 filter_nand |= (flag); \
5861 } while (0)
5862
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005863 D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
5864 *total_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005865
5866 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
5867 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
5868 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
5869 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
5870
5871#undef CHK
5872
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005873 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005874
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005875 il->staging.filter_flags &= ~filter_nand;
5876 il->staging.filter_flags |= filter_or;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005877
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005878 /*
5879 * Not committing directly because hardware can perform a scan,
5880 * but we'll eventually commit the filter flags change anyway.
5881 */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005882
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005883 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005884
5885 /*
5886 * Receiving all multicast frames is always enabled by the
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005887 * default flags setup in il_connection_init_rx_config()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005888 * since we currently do not support programming multicast
5889 * filters into the device.
5890 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005891 *total_flags &=
5892 FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
5893 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005894}
5895
5896/*****************************************************************************
5897 *
5898 * driver setup and teardown
5899 *
5900 *****************************************************************************/
5901
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005902static void
5903il4965_bg_txpower_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005904{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005905 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005906 txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005907
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005908 mutex_lock(&il->mutex);
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005909
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005910 /* If a scan happened to start before we got here
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005911 * then just return; the stats notification will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005912 * kick off another scheduled work to compensate for
5913 * any temperature delta we missed here. */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005914 if (test_bit(S_EXIT_PENDING, &il->status) ||
5915 test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005916 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005917
5918 /* Regardless of if we are associated, we must reconfigure the
5919 * TX power since frames can be sent on non-radar channels while
5920 * not associated */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005921 il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005922
5923 /* Update last_temperature to keep is_calib_needed from running
5924 * when it isn't needed... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005925 il->last_temperature = il->temperature;
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005926out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005927 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005928}
5929
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005930static void
5931il4965_setup_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005932{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005933 il->workqueue = create_singlethread_workqueue(DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005934
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005935 init_waitqueue_head(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005936
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005937 INIT_WORK(&il->restart, il4965_bg_restart);
5938 INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
5939 INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
5940 INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
5941 INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005942
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005943 il_setup_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005944
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005945 INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005946
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005947 init_timer(&il->stats_periodic);
5948 il->stats_periodic.data = (unsigned long)il;
5949 il->stats_periodic.function = il4965_bg_stats_periodic;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005950
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005951 init_timer(&il->watchdog);
5952 il->watchdog.data = (unsigned long)il;
5953 il->watchdog.function = il_bg_watchdog;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005954
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005955 tasklet_init(&il->irq_tasklet,
5956 (void (*)(unsigned long))il4965_irq_tasklet,
5957 (unsigned long)il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005958}
5959
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005960static void
5961il4965_cancel_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005962{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005963 cancel_work_sync(&il->txpower_work);
5964 cancel_delayed_work_sync(&il->init_alive_start);
5965 cancel_delayed_work(&il->alive_start);
5966 cancel_work_sync(&il->run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005967
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005968 il_cancel_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005969
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005970 del_timer_sync(&il->stats_periodic);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005971}
5972
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005973static void
5974il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005975{
5976 int i;
5977
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005978 for (i = 0; i < RATE_COUNT_LEGACY; i++) {
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02005979 rates[i].bitrate = il_rates[i].ieee * 5;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005980 rates[i].hw_value = i; /* Rate scaling will work on idxes */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005981 rates[i].hw_value_short = i;
5982 rates[i].flags = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005983 if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005984 /*
5985 * If CCK != 1M then set short preamble rate flag.
5986 */
5987 rates[i].flags |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005988 (il_rates[i].plcp ==
5989 RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005990 }
5991 }
5992}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005993
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005994/*
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005995 * Acquire il->lock before calling this function !
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005996 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005997void
5998il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005999{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006000 il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006001 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006002}
6003
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006004void
6005il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
6006 int tx_fifo_id, int scd_retry)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006007{
6008 int txq_id = txq->q.id;
6009
6010 /* Find out whether to activate Tx queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006011 int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006012
6013 /* Set up and activate */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006014 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01006015 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
6016 (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
6017 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
6018 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
6019 IL49_SCD_QUEUE_STTS_REG_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006020
6021 txq->sched_retry = scd_retry;
6022
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006023 D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
6024 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006025}
6026
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006027static int
6028il4965_init_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006029{
6030 int ret;
6031
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006032 spin_lock_init(&il->sta_lock);
6033 spin_lock_init(&il->hcmd_lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006034
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006035 INIT_LIST_HEAD(&il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006036
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006037 mutex_init(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006038
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006039 il->ieee_channels = NULL;
6040 il->ieee_rates = NULL;
6041 il->band = IEEE80211_BAND_2GHZ;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006042
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006043 il->iw_mode = NL80211_IFTYPE_STATION;
6044 il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
6045 il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006046
6047 /* initialize force reset */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006048 il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006049
6050 /* Choose which receivers/antennas to use */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006051 if (il->cfg->ops->hcmd->set_rxon_chain)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006052 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006053
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006054 il_init_scan_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006055
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006056 ret = il_init_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006057 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006058 IL_ERR("initializing regulatory failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006059 goto err;
6060 }
6061
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006062 ret = il_init_geos(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006063 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006064 IL_ERR("initializing geos failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006065 goto err_free_channel_map;
6066 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006067 il4965_init_hw_rates(il, il->ieee_rates);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006068
6069 return 0;
6070
6071err_free_channel_map:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006072 il_free_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006073err:
6074 return ret;
6075}
6076
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006077static void
6078il4965_uninit_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006079{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006080 il4965_calib_free_results(il);
6081 il_free_geos(il);
6082 il_free_channel_map(il);
6083 kfree(il->scan_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006084}
6085
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006086static void
6087il4965_hw_detect(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006088{
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006089 il->hw_rev = _il_rd(il, CSR_HW_REV);
6090 il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006091 il->rev_id = il->pci_dev->revision;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006092 D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006093}
6094
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006095static int
6096il4965_set_hw_params(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006097{
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01006098 il->hw_params.bcast_id = IL4965_BROADCAST_ID;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006099 il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
6100 il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
6101 if (il->cfg->mod_params->amsdu_size_8K)
6102 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006103 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006104 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006105
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006106 il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006107
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006108 if (il->cfg->mod_params->disable_11n)
6109 il->cfg->sku &= ~IL_SKU_N;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006110
6111 /* Device-specific setup */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006112 return il->cfg->ops->lib->set_hw_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006113}
6114
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006115static int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006116il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006117{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006118 int err = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006119 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006120 struct ieee80211_hw *hw;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006121 struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006122 unsigned long flags;
6123 u16 pci_cmd;
6124
6125 /************************
6126 * 1. Allocating HW data
6127 ************************/
6128
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006129 hw = il_alloc_all(cfg);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006130 if (!hw) {
6131 err = -ENOMEM;
6132 goto out;
6133 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006134 il = hw->priv;
6135 /* At this point both hw and il are allocated. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006136
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006137 il->ctx.always_active = true;
6138 il->ctx.is_active = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006139
6140 SET_IEEE80211_DEV(hw, &pdev->dev);
6141
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006142 D_INFO("*** LOAD DRIVER ***\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006143 il->cfg = cfg;
6144 il->pci_dev = pdev;
6145 il->inta_mask = CSR_INI_SET_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006146
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006147 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006148 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006149
6150 /**************************
6151 * 2. Initializing PCI bus
6152 **************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006153 pci_disable_link_state(pdev,
6154 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6155 PCIE_LINK_STATE_CLKPM);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006156
6157 if (pci_enable_device(pdev)) {
6158 err = -ENODEV;
6159 goto out_ieee80211_free_hw;
6160 }
6161
6162 pci_set_master(pdev);
6163
6164 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
6165 if (!err)
6166 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
6167 if (err) {
6168 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6169 if (!err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006170 err =
6171 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006172 /* both attempts failed: */
6173 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006174 IL_WARN("No suitable DMA available.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006175 goto out_pci_disable_device;
6176 }
6177 }
6178
6179 err = pci_request_regions(pdev, DRV_NAME);
6180 if (err)
6181 goto out_pci_disable_device;
6182
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006183 pci_set_drvdata(pdev, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006184
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006185 /***********************
6186 * 3. Read REV register
6187 ***********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006188 il->hw_base = pci_iomap(pdev, 0, 0);
6189 if (!il->hw_base) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006190 err = -ENODEV;
6191 goto out_pci_release_regions;
6192 }
6193
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006194 D_INFO("pci_resource_len = 0x%08llx\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006195 (unsigned long long)pci_resource_len(pdev, 0));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006196 D_INFO("pci_resource_base = %p\n", il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006197
6198 /* these spin locks will be used in apm_ops.init and EEPROM access
6199 * we should init now
6200 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006201 spin_lock_init(&il->reg_lock);
6202 spin_lock_init(&il->lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006203
6204 /*
6205 * stop and reset the on-board processor just in case it is in a
6206 * strange state ... like being left stranded by a primary kernel
6207 * and this is now the kdump kernel trying to start up
6208 */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006209 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006210
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006211 il4965_hw_detect(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006212 IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006213
6214 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6215 * PCI Tx retries from interfering with C3 CPU state */
6216 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
6217
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006218 il4965_prepare_card_hw(il);
6219 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006220 IL_WARN("Failed, HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006221 goto out_iounmap;
6222 }
6223
6224 /*****************
6225 * 4. Read EEPROM
6226 *****************/
6227 /* Read the EEPROM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006228 err = il_eeprom_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006229 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006230 IL_ERR("Unable to init EEPROM\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006231 goto out_iounmap;
6232 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006233 err = il4965_eeprom_check_version(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006234 if (err)
6235 goto out_free_eeprom;
6236
6237 if (err)
6238 goto out_free_eeprom;
6239
6240 /* extract MAC Address */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006241 il4965_eeprom_get_mac(il, il->addresses[0].addr);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006242 D_INFO("MAC address: %pM\n", il->addresses[0].addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006243 il->hw->wiphy->addresses = il->addresses;
6244 il->hw->wiphy->n_addresses = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006245
6246 /************************
6247 * 5. Setup HW constants
6248 ************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006249 if (il4965_set_hw_params(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006250 IL_ERR("failed to set hw parameters\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006251 goto out_free_eeprom;
6252 }
6253
6254 /*******************
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006255 * 6. Setup il
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006256 *******************/
6257
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006258 err = il4965_init_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006259 if (err)
6260 goto out_free_eeprom;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006261 /* At this point both hw and il are initialized. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006262
6263 /********************
6264 * 7. Setup services
6265 ********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006266 spin_lock_irqsave(&il->lock, flags);
6267 il_disable_interrupts(il);
6268 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006269
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006270 pci_enable_msi(il->pci_dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006271
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006272 err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006273 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006274 IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006275 goto out_disable_msi;
6276 }
6277
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006278 il4965_setup_deferred_work(il);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02006279 il4965_setup_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006280
6281 /*********************************************
6282 * 8. Enable interrupts and read RFKILL state
6283 *********************************************/
6284
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02006285 /* enable rfkill interrupt: hw bug w/a */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006286 pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006287 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
6288 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006289 pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006290 }
6291
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006292 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006293
6294 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006295 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006296 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006297 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006298 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006299
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006300 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006301 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006302
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006303 il_power_initialize(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006304
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006305 init_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006306
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006307 err = il4965_request_firmware(il, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006308 if (err)
6309 goto out_destroy_workqueue;
6310
6311 return 0;
6312
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006313out_destroy_workqueue:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006314 destroy_workqueue(il->workqueue);
6315 il->workqueue = NULL;
6316 free_irq(il->pci_dev->irq, il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006317out_disable_msi:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006318 pci_disable_msi(il->pci_dev);
6319 il4965_uninit_drv(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006320out_free_eeprom:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006321 il_eeprom_free(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006322out_iounmap:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006323 pci_iounmap(pdev, il->hw_base);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006324out_pci_release_regions:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006325 pci_set_drvdata(pdev, NULL);
6326 pci_release_regions(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006327out_pci_disable_device:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006328 pci_disable_device(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006329out_ieee80211_free_hw:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006330 il_free_traffic_mem(il);
6331 ieee80211_free_hw(il->hw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006332out:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006333 return err;
6334}
6335
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006336static void __devexit
6337il4965_pci_remove(struct pci_dev *pdev)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006338{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006339 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006340 unsigned long flags;
6341
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006342 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006343 return;
6344
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006345 wait_for_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006346
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006347 D_INFO("*** UNLOAD DRIVER ***\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006348
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006349 il_dbgfs_unregister(il);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006350 sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006351
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006352 /* ieee80211_unregister_hw call wil cause il_mac_stop to
6353 * to be called and il4965_down since we are removing the device
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006354 * we need to set S_EXIT_PENDING bit.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006355 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006356 set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006357
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006358 il_leds_exit(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006359
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006360 if (il->mac80211_registered) {
6361 ieee80211_unregister_hw(il->hw);
6362 il->mac80211_registered = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006363 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006364 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006365 }
6366
6367 /*
6368 * Make sure device is reset to low power before unloading driver.
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006369 * This may be redundant with il4965_down(), but there are paths to
6370 * run il4965_down() without calling apm_ops.stop(), and there are
6371 * paths to avoid running il4965_down() at all before leaving driver.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006372 * This (inexpensive) call *makes sure* device is reset.
6373 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006374 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006375
6376 /* make sure we flush any pending irq or
6377 * tasklet for the driver
6378 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006379 spin_lock_irqsave(&il->lock, flags);
6380 il_disable_interrupts(il);
6381 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006382
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006383 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006384
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006385 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006386
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006387 if (il->rxq.bd)
6388 il4965_rx_queue_free(il, &il->rxq);
6389 il4965_hw_txq_ctx_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006390
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006391 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006392
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006393 /*netif_stop_queue(dev); */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006394 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006395
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006396 /* ieee80211_unregister_hw calls il_mac_stop, which flushes
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006397 * il->workqueue... so we can't take down the workqueue
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006398 * until now... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006399 destroy_workqueue(il->workqueue);
6400 il->workqueue = NULL;
6401 il_free_traffic_mem(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006402
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006403 free_irq(il->pci_dev->irq, il);
6404 pci_disable_msi(il->pci_dev);
6405 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006406 pci_release_regions(pdev);
6407 pci_disable_device(pdev);
6408 pci_set_drvdata(pdev, NULL);
6409
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006410 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006411
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006412 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006413
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006414 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006415}
6416
6417/*
6418 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006419 * must be called under il->lock and mac access
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006420 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006421void
6422il4965_txq_set_sched(struct il_priv *il, u32 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006423{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006424 il_wr_prph(il, IL49_SCD_TXFACT, mask);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006425}
6426
6427/*****************************************************************************
6428 *
6429 * driver and module entry point
6430 *
6431 *****************************************************************************/
6432
6433/* Hardware specific file defines the PCI IDs table for that hardware module */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006434static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006435 {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
6436 {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006437 {0}
6438};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006439MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006440
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006441static struct pci_driver il4965_driver = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006442 .name = DRV_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006443 .id_table = il4965_hw_card_ids,
6444 .probe = il4965_pci_probe,
6445 .remove = __devexit_p(il4965_pci_remove),
6446 .driver.pm = IL_LEGACY_PM_OPS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006447};
6448
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006449static int __init
6450il4965_init(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006451{
6452
6453 int ret;
6454 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
6455 pr_info(DRV_COPYRIGHT "\n");
6456
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006457 ret = il4965_rate_control_register();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006458 if (ret) {
6459 pr_err("Unable to register rate control algorithm: %d\n", ret);
6460 return ret;
6461 }
6462
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006463 ret = pci_register_driver(&il4965_driver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006464 if (ret) {
6465 pr_err("Unable to initialize PCI module\n");
6466 goto error_register;
6467 }
6468
6469 return ret;
6470
6471error_register:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006472 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006473 return ret;
6474}
6475
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006476static void __exit
6477il4965_exit(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006478{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006479 pci_unregister_driver(&il4965_driver);
6480 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006481}
6482
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006483module_exit(il4965_exit);
6484module_init(il4965_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006485
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006486#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02006487module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006488MODULE_PARM_DESC(debug, "debug output mask");
6489#endif
6490
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006491module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006492MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006493module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006494MODULE_PARM_DESC(queues_num, "number of hw queues.");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006495module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006496MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006497module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int,
6498 S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006499MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006500module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006501MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");