blob: 4aaef4135564efc67bd382b2437489ed0a430d73 [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 Gruszkae7392362011-11-15 14:45:59 +0100822static inline u32
823il4965_ant_idx_to_flags(u8 ant_idx)
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +0200824{
825 return BIT(ant_idx) << RATE_MCS_ANT_POS;
826}
827
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100828int
829il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200830{
831 struct il_host_cmd cmd = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200832 .id = C_SCAN,
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200833 .len = sizeof(struct il_scan_cmd),
834 .flags = CMD_SIZE_HUGE,
835 };
836 struct il_scan_cmd *scan;
837 struct il_rxon_context *ctx = &il->ctx;
838 u32 rate_flags = 0;
839 u16 cmd_len;
840 u16 rx_chain = 0;
841 enum ieee80211_band band;
842 u8 n_probes = 0;
843 u8 rx_ant = il->hw_params.valid_rx_ant;
844 u8 rate;
845 bool is_active = false;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100846 int chan_mod;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200847 u8 active_chains;
848 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
849 int ret;
850
851 lockdep_assert_held(&il->mutex);
852
Greg Dietsche730b4c22011-09-06 17:33:51 -0500853 ctx = il_rxon_ctx_from_vif(vif);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200854
855 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100856 il->scan_cmd =
857 kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
858 GFP_KERNEL);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200859 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100860 D_SCAN("fail to allocate memory for scan\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200861 return -ENOMEM;
862 }
863 }
864 scan = il->scan_cmd;
865 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
866
867 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
868 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
869
870 if (il_is_any_associated(il)) {
871 u16 interval;
872 u32 extra;
873 u32 suspend_time = 100;
874 u32 scan_suspend_time = 100;
875
876 D_INFO("Scanning while associated...\n");
877 interval = vif->bss_conf.beacon_int;
878
879 scan->suspend_time = 0;
880 scan->max_out_time = cpu_to_le32(200 * 1024);
881 if (!interval)
882 interval = suspend_time;
883
884 extra = (suspend_time / interval) << 22;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100885 scan_suspend_time =
886 (extra | ((suspend_time % interval) * 1024));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200887 scan->suspend_time = cpu_to_le32(scan_suspend_time);
888 D_SCAN("suspend_time 0x%X beacon interval %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100889 scan_suspend_time, interval);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200890 }
891
892 if (il->scan_request->n_ssids) {
893 int i, p = 0;
894 D_SCAN("Kicking off active scan\n");
895 for (i = 0; i < il->scan_request->n_ssids; i++) {
896 /* always does wildcard anyway */
897 if (!il->scan_request->ssids[i].ssid_len)
898 continue;
899 scan->direct_scan[p].id = WLAN_EID_SSID;
900 scan->direct_scan[p].len =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100901 il->scan_request->ssids[i].ssid_len;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200902 memcpy(scan->direct_scan[p].ssid,
903 il->scan_request->ssids[i].ssid,
904 il->scan_request->ssids[i].ssid_len);
905 n_probes++;
906 p++;
907 }
908 is_active = true;
909 } else
910 D_SCAN("Start passive scan.\n");
911
912 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
913 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
914 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
915
916 switch (il->scan_band) {
917 case IEEE80211_BAND_2GHZ:
918 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100919 chan_mod =
920 le32_to_cpu(il->ctx.active.
921 flags & RXON_FLG_CHANNEL_MODE_MSK) >>
922 RXON_FLG_CHANNEL_MODE_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200923 if (chan_mod == CHANNEL_MODE_PURE_40) {
924 rate = RATE_6M_PLCP;
925 } else {
926 rate = RATE_1M_PLCP;
927 rate_flags = RATE_MCS_CCK_MSK;
928 }
929 break;
930 case IEEE80211_BAND_5GHZ:
931 rate = RATE_6M_PLCP;
932 break;
933 default:
934 IL_WARN("Invalid scan band\n");
935 return -EIO;
936 }
937
938 /*
939 * If active scanning is requested but a certain channel is
940 * marked passive, we can do active scanning if we detect
941 * transmissions.
942 *
943 * There is an issue with some firmware versions that triggers
944 * a sysassert on a "good CRC threshold" of zero (== disabled),
945 * on a radar channel even though this means that we should NOT
946 * send probes.
947 *
948 * The "good CRC threshold" is the number of frames that we
949 * need to receive during our dwell time on a channel before
950 * sending out probes -- setting this to a huge value will
951 * mean we never reach it, but at the same time work around
952 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
953 * here instead of IL_GOOD_CRC_TH_DISABLED.
954 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100955 scan->good_CRC_th =
956 is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200957
958 band = il->scan_band;
959
960 if (il->cfg->scan_rx_antennas[band])
961 rx_ant = il->cfg->scan_rx_antennas[band];
962
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100963 il->scan_tx_ant[band] =
964 il4965_toggle_tx_ant(il, il->scan_tx_ant[band], scan_tx_antennas);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200965 rate_flags |= il4965_ant_idx_to_flags(il->scan_tx_ant[band]);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100966 scan->tx_cmd.rate_n_flags =
967 il4965_hw_set_rate_n_flags(rate, rate_flags);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200968
969 /* In power save mode use one chain, otherwise use all chains */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100970 if (test_bit(S_POWER_PMI, &il->status)) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200971 /* rx_ant has been set to all valid chains previously */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100972 active_chains =
973 rx_ant & ((u8) (il->chain_noise_data.active_chains));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200974 if (!active_chains)
975 active_chains = rx_ant;
976
977 D_SCAN("chain_noise_data.active_chains: %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100978 il->chain_noise_data.active_chains);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200979
980 rx_ant = il4965_first_antenna(active_chains);
981 }
982
983 /* MIMO is not used here, but value is required */
984 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
985 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
986 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
987 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
988 scan->rx_chain = cpu_to_le16(rx_chain);
989
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100990 cmd_len =
991 il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
992 vif->addr, il->scan_request->ie,
993 il->scan_request->ie_len,
994 IL_MAX_SCAN_SIZE - sizeof(*scan));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200995 scan->tx_cmd.len = cpu_to_le16(cmd_len);
996
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100997 scan->filter_flags |=
998 (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200999
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001000 scan->channel_count =
1001 il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
1002 (void *)&scan->data[cmd_len]);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001003 if (scan->channel_count == 0) {
1004 D_SCAN("channel count %d\n", scan->channel_count);
1005 return -EIO;
1006 }
1007
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001008 cmd.len +=
1009 le16_to_cpu(scan->tx_cmd.len) +
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001010 scan->channel_count * sizeof(struct il_scan_channel);
1011 cmd.data = scan;
1012 scan->len = cpu_to_le16(cmd.len);
1013
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001014 set_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001015
1016 ret = il_send_cmd_sync(il, &cmd);
1017 if (ret)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001018 clear_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001019
1020 return ret;
1021}
1022
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001023int
1024il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
1025 bool add)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001026{
1027 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1028
1029 if (add)
1030 return il4965_add_bssid_station(il, vif_priv->ctx,
1031 vif->bss_conf.bssid,
1032 &vif_priv->ibss_bssid_sta_id);
1033 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001034 vif->bss_conf.bssid);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001035}
1036
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001037void
1038il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001039{
1040 lockdep_assert_held(&il->sta_lock);
1041
1042 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1043 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1044 else {
1045 D_TX("free more than tfds_in_queue (%u:%d)\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001046 il->stations[sta_id].tid[tid].tfds_in_queue, freed);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001047 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1048 }
1049}
1050
1051#define IL_TX_QUEUE_MSK 0xfffff
1052
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001053static bool
1054il4965_is_single_rx_stream(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001055{
1056 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001057 il->current_ht_config.single_chain_sufficient;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001058}
1059
1060#define IL_NUM_RX_CHAINS_MULTIPLE 3
1061#define IL_NUM_RX_CHAINS_SINGLE 2
1062#define IL_NUM_IDLE_CHAINS_DUAL 2
1063#define IL_NUM_IDLE_CHAINS_SINGLE 1
1064
1065/*
1066 * Determine how many receiver/antenna chains to use.
1067 *
1068 * More provides better reception via diversity. Fewer saves power
1069 * at the expense of throughput, but only when not in powersave to
1070 * start with.
1071 *
1072 * MIMO (dual stream) requires at least 2, but works better with 3.
1073 * This does not determine *which* chains to use, just how many.
1074 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001075static int
1076il4965_get_active_rx_chain_count(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001077{
1078 /* # of Rx chains to use when expecting MIMO. */
1079 if (il4965_is_single_rx_stream(il))
1080 return IL_NUM_RX_CHAINS_SINGLE;
1081 else
1082 return IL_NUM_RX_CHAINS_MULTIPLE;
1083}
1084
1085/*
1086 * When we are in power saving mode, unless device support spatial
1087 * multiplexing power save, use the active count for rx chain count.
1088 */
1089static int
1090il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1091{
1092 /* # Rx chains when idling, depending on SMPS mode */
1093 switch (il->current_ht_config.smps) {
1094 case IEEE80211_SMPS_STATIC:
1095 case IEEE80211_SMPS_DYNAMIC:
1096 return IL_NUM_IDLE_CHAINS_SINGLE;
1097 case IEEE80211_SMPS_OFF:
1098 return active_cnt;
1099 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001100 WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001101 return active_cnt;
1102 }
1103}
1104
1105/* up to 4 chains */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001106static u8
1107il4965_count_chain_bitmap(u32 chain_bitmap)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001108{
1109 u8 res;
1110 res = (chain_bitmap & BIT(0)) >> 0;
1111 res += (chain_bitmap & BIT(1)) >> 1;
1112 res += (chain_bitmap & BIT(2)) >> 2;
1113 res += (chain_bitmap & BIT(3)) >> 3;
1114 return res;
1115}
1116
1117/**
1118 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1119 *
1120 * Selects how many and which Rx receivers/antennas/chains to use.
1121 * This should not be used for scan command ... it puts data in wrong place.
1122 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001123void
1124il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001125{
1126 bool is_single = il4965_is_single_rx_stream(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001127 bool is_cam = !test_bit(S_POWER_PMI, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001128 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1129 u32 active_chains;
1130 u16 rx_chain;
1131
1132 /* Tell uCode which antennas are actually connected.
1133 * Before first association, we assume all antennas are connected.
1134 * Just after first association, il4965_chain_noise_calibration()
1135 * checks which antennas actually *are* connected. */
1136 if (il->chain_noise_data.active_chains)
1137 active_chains = il->chain_noise_data.active_chains;
1138 else
1139 active_chains = il->hw_params.valid_rx_ant;
1140
1141 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1142
1143 /* How many receivers should we use? */
1144 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1145 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1146
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001147 /* correct rx chain count according hw settings
1148 * and chain noise calibration
1149 */
1150 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1151 if (valid_rx_cnt < active_rx_cnt)
1152 active_rx_cnt = valid_rx_cnt;
1153
1154 if (valid_rx_cnt < idle_rx_cnt)
1155 idle_rx_cnt = valid_rx_cnt;
1156
1157 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001158 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001159
1160 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1161
1162 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
1163 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1164 else
1165 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1166
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001167 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", ctx->staging.rx_chain,
1168 active_rx_cnt, idle_rx_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001169
1170 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1171 active_rx_cnt < idle_rx_cnt);
1172}
1173
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001174u8
1175il4965_toggle_tx_ant(struct il_priv *il, u8 ant, u8 valid)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001176{
1177 int i;
1178 u8 ind = ant;
1179
1180 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001181 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001182 if (valid & BIT(ind))
1183 return ind;
1184 }
1185 return ant;
1186}
1187
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001188static const char *
1189il4965_get_fh_string(int cmd)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001190{
1191 switch (cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001192 IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
1193 IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
1194 IL_CMD(FH49_RSCSR_CHNL0_WPTR);
1195 IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
1196 IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
1197 IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
1198 IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
1199 IL_CMD(FH49_TSSR_TX_STATUS_REG);
1200 IL_CMD(FH49_TSSR_TX_ERROR_REG);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001201 default:
1202 return "UNKNOWN";
1203 }
1204}
1205
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001206int
1207il4965_dump_fh(struct il_priv *il, char **buf, bool display)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001208{
1209 int i;
1210#ifdef CONFIG_IWLEGACY_DEBUG
1211 int pos = 0;
1212 size_t bufsz = 0;
1213#endif
1214 static const u32 fh_tbl[] = {
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02001215 FH49_RSCSR_CHNL0_STTS_WPTR_REG,
1216 FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
1217 FH49_RSCSR_CHNL0_WPTR,
1218 FH49_MEM_RCSR_CHNL0_CONFIG_REG,
1219 FH49_MEM_RSSR_SHARED_CTRL_REG,
1220 FH49_MEM_RSSR_RX_STATUS_REG,
1221 FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
1222 FH49_TSSR_TX_STATUS_REG,
1223 FH49_TSSR_TX_ERROR_REG
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001224 };
1225#ifdef CONFIG_IWLEGACY_DEBUG
1226 if (display) {
1227 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
1228 *buf = kmalloc(bufsz, GFP_KERNEL);
1229 if (!*buf)
1230 return -ENOMEM;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001231 pos +=
1232 scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001233 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001234 pos +=
1235 scnprintf(*buf + pos, bufsz - pos,
1236 " %34s: 0X%08x\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001237 il4965_get_fh_string(fh_tbl[i]),
1238 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001239 }
1240 return pos;
1241 }
1242#endif
1243 IL_ERR("FH register values:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001244 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1245 IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
1246 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001247 }
1248 return 0;
1249}
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001250
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001251void
1252il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001253{
1254 struct il_rx_pkt *pkt = rxb_addr(rxb);
1255 struct il_missed_beacon_notif *missed_beacon;
1256
1257 missed_beacon = &pkt->u.missed_beacon;
1258 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
1259 il->missed_beacon_threshold) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001260 D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
1261 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
1262 le32_to_cpu(missed_beacon->total_missed_becons),
1263 le32_to_cpu(missed_beacon->num_recvd_beacons),
1264 le32_to_cpu(missed_beacon->num_expected_beacons));
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001265 if (!test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001266 il4965_init_sensitivity(il);
1267 }
1268}
1269
1270/* Calculate noise level, based on measurements during network silence just
1271 * before arriving beacon. This measurement can be done only if we know
1272 * exactly when to expect beacons, therefore only when we're associated. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001273static void
1274il4965_rx_calc_noise(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001275{
1276 struct stats_rx_non_phy *rx_info;
1277 int num_active_rx = 0;
1278 int total_silence = 0;
1279 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
1280 int last_rx_noise;
1281
1282 rx_info = &(il->_4965.stats.rx.general);
1283 bcn_silence_a =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001284 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001285 bcn_silence_b =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001286 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001287 bcn_silence_c =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001288 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001289
1290 if (bcn_silence_a) {
1291 total_silence += bcn_silence_a;
1292 num_active_rx++;
1293 }
1294 if (bcn_silence_b) {
1295 total_silence += bcn_silence_b;
1296 num_active_rx++;
1297 }
1298 if (bcn_silence_c) {
1299 total_silence += bcn_silence_c;
1300 num_active_rx++;
1301 }
1302
1303 /* Average among active antennas */
1304 if (num_active_rx)
1305 last_rx_noise = (total_silence / num_active_rx) - 107;
1306 else
1307 last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
1308
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001309 D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
1310 bcn_silence_b, bcn_silence_c, last_rx_noise);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001311}
1312
1313#ifdef CONFIG_IWLEGACY_DEBUGFS
1314/*
1315 * based on the assumption of all stats counter are in DWORD
1316 * FIXME: This function is for debugging, do not deal with
1317 * the case of counters roll-over.
1318 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001319static void
1320il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001321{
1322 int i, size;
1323 __le32 *prev_stats;
1324 u32 *accum_stats;
1325 u32 *delta, *max_delta;
1326 struct stats_general_common *general, *accum_general;
1327 struct stats_tx *tx, *accum_tx;
1328
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001329 prev_stats = (__le32 *) &il->_4965.stats;
1330 accum_stats = (u32 *) &il->_4965.accum_stats;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001331 size = sizeof(struct il_notif_stats);
1332 general = &il->_4965.stats.general.common;
1333 accum_general = &il->_4965.accum_stats.general.common;
1334 tx = &il->_4965.stats.tx;
1335 accum_tx = &il->_4965.accum_stats.tx;
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001336 delta = (u32 *) &il->_4965.delta_stats;
1337 max_delta = (u32 *) &il->_4965.max_delta;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001338
1339 for (i = sizeof(__le32); i < size;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001340 i +=
1341 sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
1342 accum_stats++) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001343 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001344 *delta =
1345 (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001346 *accum_stats += *delta;
1347 if (*delta > *max_delta)
1348 *max_delta = *delta;
1349 }
1350 }
1351
1352 /* reset accumulative stats for "no-counter" type stats */
1353 accum_general->temperature = general->temperature;
1354 accum_general->ttl_timestamp = general->ttl_timestamp;
1355}
1356#endif
1357
1358#define REG_RECALIB_PERIOD (60)
1359
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001360void
1361il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001362{
1363 int change;
1364 struct il_rx_pkt *pkt = rxb_addr(rxb);
1365
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001366 D_RX("Statistics notification received (%d vs %d).\n",
1367 (int)sizeof(struct il_notif_stats),
1368 le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001369
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001370 change =
1371 ((il->_4965.stats.general.common.temperature !=
1372 pkt->u.stats.general.common.temperature) ||
1373 ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
1374 (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001375#ifdef CONFIG_IWLEGACY_DEBUGFS
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001376 il4965_accumulative_stats(il, (__le32 *) &pkt->u.stats);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001377#endif
1378
1379 /* TODO: reading some of stats is unneeded */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001380 memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001381
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001382 set_bit(S_STATS, &il->status);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001383
1384 /* Reschedule the stats timer to occur in
1385 * REG_RECALIB_PERIOD seconds to ensure we get a
1386 * thermal update even if the uCode doesn't give
1387 * us one */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001388 mod_timer(&il->stats_periodic,
1389 jiffies + msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001390
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001391 if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001392 (pkt->hdr.cmd == N_STATS)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001393 il4965_rx_calc_noise(il);
1394 queue_work(il->workqueue, &il->run_time_calib_work);
1395 }
1396 if (il->cfg->ops->lib->temp_ops.temperature && change)
1397 il->cfg->ops->lib->temp_ops.temperature(il);
1398}
1399
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001400void
1401il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001402{
1403 struct il_rx_pkt *pkt = rxb_addr(rxb);
1404
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001405 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001406#ifdef CONFIG_IWLEGACY_DEBUGFS
1407 memset(&il->_4965.accum_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001408 sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001409 memset(&il->_4965.delta_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001410 sizeof(struct il_notif_stats));
1411 memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001412#endif
1413 D_RX("Statistics have been cleared\n");
1414 }
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01001415 il4965_hdl_stats(il, rxb);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001416}
1417
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001418
1419/*
1420 * mac80211 queues, ACs, hardware queues, FIFOs.
1421 *
1422 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
1423 *
1424 * Mac80211 uses the following numbers, which we get as from it
1425 * by way of skb_get_queue_mapping(skb):
1426 *
1427 * VO 0
1428 * VI 1
1429 * BE 2
1430 * BK 3
1431 *
1432 *
1433 * Regular (not A-MPDU) frames are put into hardware queues corresponding
1434 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
1435 * own queue per aggregation session (RA/TID combination), such queues are
1436 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
1437 * order to map frames to the right queue, we also need an AC->hw queue
1438 * mapping. This is implemented here.
1439 *
1440 * Due to the way hw queues are set up (by the hw specific modules like
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +02001441 * 4965.c), the AC->hw queue mapping is the identity
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001442 * mapping.
1443 */
1444
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001445static const u8 tid_to_ac[] = {
1446 IEEE80211_AC_BE,
1447 IEEE80211_AC_BK,
1448 IEEE80211_AC_BK,
1449 IEEE80211_AC_BE,
1450 IEEE80211_AC_VI,
1451 IEEE80211_AC_VI,
1452 IEEE80211_AC_VO,
1453 IEEE80211_AC_VO
1454};
1455
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001456static inline int
1457il4965_get_ac_from_tid(u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001458{
1459 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1460 return tid_to_ac[tid];
1461
1462 /* no support for TIDs 8-15 yet */
1463 return -EINVAL;
1464}
1465
1466static inline int
1467il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
1468{
1469 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1470 return ctx->ac_to_fifo[tid_to_ac[tid]];
1471
1472 /* no support for TIDs 8-15 yet */
1473 return -EINVAL;
1474}
1475
1476/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001477 * handle build C_TX command notification.
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001478 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001479static void
1480il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
1481 struct il_tx_cmd *tx_cmd,
1482 struct ieee80211_tx_info *info,
1483 struct ieee80211_hdr *hdr, u8 std_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001484{
1485 __le16 fc = hdr->frame_control;
1486 __le32 tx_flags = tx_cmd->tx_flags;
1487
1488 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1489 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
1490 tx_flags |= TX_CMD_FLG_ACK_MSK;
1491 if (ieee80211_is_mgmt(fc))
1492 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1493 if (ieee80211_is_probe_resp(fc) &&
1494 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1495 tx_flags |= TX_CMD_FLG_TSF_MSK;
1496 } else {
1497 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1498 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1499 }
1500
1501 if (ieee80211_is_back_req(fc))
1502 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1503
1504 tx_cmd->sta_id = std_id;
1505 if (ieee80211_has_morefrags(fc))
1506 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1507
1508 if (ieee80211_is_data_qos(fc)) {
1509 u8 *qc = ieee80211_get_qos_ctl(hdr);
1510 tx_cmd->tid_tspec = qc[0] & 0xf;
1511 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1512 } else {
1513 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1514 }
1515
1516 il_tx_cmd_protection(il, info, fc, &tx_flags);
1517
1518 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1519 if (ieee80211_is_mgmt(fc)) {
1520 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
1521 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
1522 else
1523 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
1524 } else {
1525 tx_cmd->timeout.pm_frame_timeout = 0;
1526 }
1527
1528 tx_cmd->driver_txop = 0;
1529 tx_cmd->tx_flags = tx_flags;
1530 tx_cmd->next_frame_len = 0;
1531}
1532
1533#define RTS_DFAULT_RETRY_LIMIT 60
1534
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001535static void
1536il4965_tx_cmd_build_rate(struct il_priv *il, struct il_tx_cmd *tx_cmd,
1537 struct ieee80211_tx_info *info, __le16 fc)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001538{
1539 u32 rate_flags;
1540 int rate_idx;
1541 u8 rts_retry_limit;
1542 u8 data_retry_limit;
1543 u8 rate_plcp;
1544
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001545 /* Set retry limit on DATA packets and Probe Responses */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001546 if (ieee80211_is_probe_resp(fc))
1547 data_retry_limit = 3;
1548 else
1549 data_retry_limit = IL4965_DEFAULT_TX_RETRY;
1550 tx_cmd->data_retry_limit = data_retry_limit;
1551
1552 /* Set retry limit on RTS packets */
1553 rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
1554 if (data_retry_limit < rts_retry_limit)
1555 rts_retry_limit = data_retry_limit;
1556 tx_cmd->rts_retry_limit = rts_retry_limit;
1557
1558 /* DATA packets will use the uCode station table for rate/antenna
1559 * selection */
1560 if (ieee80211_is_data(fc)) {
1561 tx_cmd->initial_rate_idx = 0;
1562 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1563 return;
1564 }
1565
1566 /**
1567 * If the current TX rate stored in mac80211 has the MCS bit set, it's
1568 * not really a TX rate. Thus, we use the lowest supported rate for
1569 * this band. Also use the lowest supported rate if the stored rate
1570 * idx is invalid.
1571 */
1572 rate_idx = info->control.rates[0].idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001573 if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
1574 || rate_idx > RATE_COUNT_LEGACY)
1575 rate_idx =
1576 rate_lowest_index(&il->bands[info->band],
1577 info->control.sta);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001578 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
1579 if (info->band == IEEE80211_BAND_5GHZ)
1580 rate_idx += IL_FIRST_OFDM_RATE;
1581 /* Get PLCP rate for tx_cmd->rate_n_flags */
1582 rate_plcp = il_rates[rate_idx].plcp;
1583 /* Zero out flags for this packet */
1584 rate_flags = 0;
1585
1586 /* Set CCK flag as needed */
1587 if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
1588 rate_flags |= RATE_MCS_CCK_MSK;
1589
1590 /* Set up antennas */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001591 il->mgmt_tx_ant =
1592 il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
1593 il->hw_params.valid_tx_ant);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001594
1595 rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
1596
1597 /* Set the rate in the TX cmd */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001598 tx_cmd->rate_n_flags =
1599 il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001600}
1601
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001602static void
1603il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
1604 struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
1605 int sta_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001606{
1607 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1608
1609 switch (keyconf->cipher) {
1610 case WLAN_CIPHER_SUITE_CCMP:
1611 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1612 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1613 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1614 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1615 D_TX("tx_cmd with AES hwcrypto\n");
1616 break;
1617
1618 case WLAN_CIPHER_SUITE_TKIP:
1619 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1620 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1621 D_TX("tx_cmd with tkip hwcrypto\n");
1622 break;
1623
1624 case WLAN_CIPHER_SUITE_WEP104:
1625 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1626 /* fall through */
1627 case WLAN_CIPHER_SUITE_WEP40:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001628 tx_cmd->sec_ctl |=
1629 (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
1630 TX_CMD_SEC_SHIFT);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001631
1632 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1633
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001634 D_TX("Configuring packet for WEP encryption " "with key %d\n",
1635 keyconf->keyidx);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001636 break;
1637
1638 default:
1639 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1640 break;
1641 }
1642}
1643
1644/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001645 * start C_TX command process
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001646 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001647int
1648il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001649{
1650 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1651 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1652 struct ieee80211_sta *sta = info->control.sta;
1653 struct il_station_priv *sta_priv = NULL;
1654 struct il_tx_queue *txq;
1655 struct il_queue *q;
1656 struct il_device_cmd *out_cmd;
1657 struct il_cmd_meta *out_meta;
1658 struct il_tx_cmd *tx_cmd;
1659 struct il_rxon_context *ctx = &il->ctx;
1660 int txq_id;
1661 dma_addr_t phys_addr;
1662 dma_addr_t txcmd_phys;
1663 dma_addr_t scratch_phys;
1664 u16 len, firstlen, secondlen;
1665 u16 seq_number = 0;
1666 __le16 fc;
1667 u8 hdr_len;
1668 u8 sta_id;
1669 u8 wait_write_ptr = 0;
1670 u8 tid = 0;
1671 u8 *qc = NULL;
1672 unsigned long flags;
1673 bool is_agg = false;
1674
1675 if (info->control.vif)
1676 ctx = il_rxon_ctx_from_vif(info->control.vif);
1677
1678 spin_lock_irqsave(&il->lock, flags);
1679 if (il_is_rfkill(il)) {
1680 D_DROP("Dropping - RF KILL\n");
1681 goto drop_unlock;
1682 }
1683
1684 fc = hdr->frame_control;
1685
1686#ifdef CONFIG_IWLEGACY_DEBUG
1687 if (ieee80211_is_auth(fc))
1688 D_TX("Sending AUTH frame\n");
1689 else if (ieee80211_is_assoc_req(fc))
1690 D_TX("Sending ASSOC frame\n");
1691 else if (ieee80211_is_reassoc_req(fc))
1692 D_TX("Sending REASSOC frame\n");
1693#endif
1694
1695 hdr_len = ieee80211_hdrlen(fc);
1696
1697 /* For management frames use broadcast id to do not break aggregation */
1698 if (!ieee80211_is_data(fc))
1699 sta_id = ctx->bcast_sta_id;
1700 else {
1701 /* Find idx into station table for destination station */
1702 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1703
1704 if (sta_id == IL_INVALID_STATION) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001705 D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001706 goto drop_unlock;
1707 }
1708 }
1709
1710 D_TX("station Id %d\n", sta_id);
1711
1712 if (sta)
1713 sta_priv = (void *)sta->drv_priv;
1714
1715 if (sta_priv && sta_priv->asleep &&
1716 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1717 /*
1718 * This sends an asynchronous command to the device,
1719 * but we can rely on it being processed before the
1720 * next frame is processed -- and the next frame to
1721 * this station is the one that will consume this
1722 * counter.
1723 * For now set the counter to just 1 since we do not
1724 * support uAPSD yet.
1725 */
1726 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1727 }
1728
1729 /*
1730 * Send this frame after DTIM -- there's a special queue
1731 * reserved for this for contexts that support AP mode.
1732 */
1733 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1734 txq_id = ctx->mcast_queue;
1735 /*
1736 * The microcode will clear the more data
1737 * bit in the last frame it transmits.
1738 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001739 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001740 } else
1741 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
1742
1743 /* irqs already disabled/saved above when locking il->lock */
1744 spin_lock(&il->sta_lock);
1745
1746 if (ieee80211_is_data_qos(fc)) {
1747 qc = ieee80211_get_qos_ctl(hdr);
1748 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1749 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1750 spin_unlock(&il->sta_lock);
1751 goto drop_unlock;
1752 }
1753 seq_number = il->stations[sta_id].tid[tid].seq_number;
1754 seq_number &= IEEE80211_SCTL_SEQ;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001755 hdr->seq_ctrl =
1756 hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001757 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1758 seq_number += 0x10;
1759 /* aggregation is on for this <sta,tid> */
1760 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1761 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1762 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1763 is_agg = true;
1764 }
1765 }
1766
1767 txq = &il->txq[txq_id];
1768 q = &txq->q;
1769
1770 if (unlikely(il_queue_space(q) < q->high_mark)) {
1771 spin_unlock(&il->sta_lock);
1772 goto drop_unlock;
1773 }
1774
1775 if (ieee80211_is_data_qos(fc)) {
1776 il->stations[sta_id].tid[tid].tfds_in_queue++;
1777 if (!ieee80211_has_morefrags(fc))
1778 il->stations[sta_id].tid[tid].seq_number = seq_number;
1779 }
1780
1781 spin_unlock(&il->sta_lock);
1782
1783 /* Set up driver data for this TFD */
1784 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1785 txq->txb[q->write_ptr].skb = skb;
1786 txq->txb[q->write_ptr].ctx = ctx;
1787
1788 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1789 out_cmd = txq->cmd[q->write_ptr];
1790 out_meta = &txq->meta[q->write_ptr];
1791 tx_cmd = &out_cmd->cmd.tx;
1792 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1793 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1794
1795 /*
1796 * Set up the Tx-command (not MAC!) header.
1797 * Store the chosen Tx queue and TFD idx within the sequence field;
1798 * after Tx, uCode's Tx response will return this value so driver can
1799 * locate the frame within the tx queue and do post-tx processing.
1800 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001801 out_cmd->hdr.cmd = C_TX;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001802 out_cmd->hdr.sequence =
1803 cpu_to_le16((u16)
1804 (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001805
1806 /* Copy MAC header from skb into command buffer */
1807 memcpy(tx_cmd->hdr, hdr, hdr_len);
1808
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001809 /* Total # bytes to be transmitted */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001810 len = (u16) skb->len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001811 tx_cmd->len = cpu_to_le16(len);
1812
1813 if (info->control.hw_key)
1814 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1815
1816 /* TODO need this for burst mode later on */
1817 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1818 il_dbg_log_tx_data_frame(il, len, hdr);
1819
1820 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1821
1822 il_update_stats(il, true, fc, len);
1823 /*
1824 * Use the first empty entry in this queue's command buffer array
1825 * to contain the Tx command and MAC header concatenated together
1826 * (payload data will be in another buffer).
1827 * Size of this varies, due to varying MAC header length.
1828 * If end is not dword aligned, we'll have 2 extra bytes at the end
1829 * of the MAC header (device reads on dword boundaries).
1830 * We'll tell device about this padding later.
1831 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001832 len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001833 firstlen = (len + 3) & ~3;
1834
1835 /* Tell NIC about any 2-byte padding after MAC header */
1836 if (firstlen != len)
1837 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1838
1839 /* Physical address of this Tx command's header (not MAC header!),
1840 * within command buffer array. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001841 txcmd_phys =
1842 pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
1843 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001844 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1845 dma_unmap_len_set(out_meta, len, firstlen);
1846 /* Add buffer containing Tx command and MAC(!) header to TFD's
1847 * first entry */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001848 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen,
1849 1, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001850
1851 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1852 txq->need_update = 1;
1853 } else {
1854 wait_write_ptr = 1;
1855 txq->need_update = 0;
1856 }
1857
1858 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1859 * if any (802.11 null frames have no payload). */
1860 secondlen = skb->len - hdr_len;
1861 if (secondlen > 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001862 phys_addr =
1863 pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
1864 PCI_DMA_TODEVICE);
1865 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr,
1866 secondlen, 0, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001867 }
1868
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001869 scratch_phys =
1870 txcmd_phys + sizeof(struct il_cmd_header) +
1871 offsetof(struct il_tx_cmd, scratch);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001872
1873 /* take back ownership of DMA buffer to enable update */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001874 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
1875 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001876 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1877 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1878
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001879 D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001880 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001881 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
1882 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001883
1884 /* Set up entry for this TFD in Tx byte-count array */
1885 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1886 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001887 le16_to_cpu(tx_cmd->
1888 len));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001889
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001890 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
1891 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001892
1893 /* Tell device the write idx *just past* this latest filled TFD */
1894 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1895 il_txq_update_write_ptr(il, txq);
1896 spin_unlock_irqrestore(&il->lock, flags);
1897
1898 /*
1899 * At this point the frame is "transmitted" successfully
1900 * and we will get a TX status notification eventually,
1901 * regardless of the value of ret. "ret" only indicates
1902 * whether or not we should update the write pointer.
1903 */
1904
1905 /*
1906 * Avoid atomic ops if it isn't an associated client.
1907 * Also, if this is a packet for aggregation, don't
1908 * increase the counter because the ucode will stop
1909 * aggregation queues when their respective station
1910 * goes to sleep.
1911 */
1912 if (sta_priv && sta_priv->client && !is_agg)
1913 atomic_inc(&sta_priv->pending_frames);
1914
1915 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1916 if (wait_write_ptr) {
1917 spin_lock_irqsave(&il->lock, flags);
1918 txq->need_update = 1;
1919 il_txq_update_write_ptr(il, txq);
1920 spin_unlock_irqrestore(&il->lock, flags);
1921 } else {
1922 il_stop_queue(il, txq);
1923 }
1924 }
1925
1926 return 0;
1927
1928drop_unlock:
1929 spin_unlock_irqrestore(&il->lock, flags);
1930 return -1;
1931}
1932
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001933static inline int
1934il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001935{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001936 ptr->addr =
1937 dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001938 if (!ptr->addr)
1939 return -ENOMEM;
1940 ptr->size = size;
1941 return 0;
1942}
1943
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001944static inline void
1945il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001946{
1947 if (unlikely(!ptr->addr))
1948 return;
1949
1950 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1951 memset(ptr, 0, sizeof(*ptr));
1952}
1953
1954/**
1955 * il4965_hw_txq_ctx_free - Free TXQ Context
1956 *
1957 * Destroy all TX DMA queues and structures
1958 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001959void
1960il4965_hw_txq_ctx_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001961{
1962 int txq_id;
1963
1964 /* Tx queues */
1965 if (il->txq) {
1966 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1967 if (txq_id == il->cmd_queue)
1968 il_cmd_queue_free(il);
1969 else
1970 il_tx_queue_free(il, txq_id);
1971 }
1972 il4965_free_dma_ptr(il, &il->kw);
1973
1974 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1975
1976 /* free tx queue structure */
1977 il_txq_mem(il);
1978}
1979
1980/**
1981 * il4965_txq_ctx_alloc - allocate TX queue context
1982 * Allocate all Tx DMA structures and initialize them
1983 *
1984 * @param il
1985 * @return error code
1986 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001987int
1988il4965_txq_ctx_alloc(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001989{
1990 int ret;
1991 int txq_id, slots_num;
1992 unsigned long flags;
1993
1994 /* Free all tx/cmd queues and keep-warm buffer */
1995 il4965_hw_txq_ctx_free(il);
1996
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001997 ret =
1998 il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
1999 il->hw_params.scd_bc_tbls_size);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002000 if (ret) {
2001 IL_ERR("Scheduler BC Table allocation failed\n");
2002 goto error_bc_tbls;
2003 }
2004 /* Alloc keep-warm buffer */
2005 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
2006 if (ret) {
2007 IL_ERR("Keep Warm allocation failed\n");
2008 goto error_kw;
2009 }
2010
2011 /* allocate tx queue structure */
2012 ret = il_alloc_txq_mem(il);
2013 if (ret)
2014 goto error;
2015
2016 spin_lock_irqsave(&il->lock, flags);
2017
2018 /* Turn off all Tx DMA fifos */
2019 il4965_txq_set_sched(il, 0);
2020
2021 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002022 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002023
2024 spin_unlock_irqrestore(&il->lock, flags);
2025
2026 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2027 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002028 slots_num =
2029 (txq_id ==
2030 il->cmd_queue) ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2031 ret = il_tx_queue_init(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002032 if (ret) {
2033 IL_ERR("Tx %d queue init failed\n", txq_id);
2034 goto error;
2035 }
2036 }
2037
2038 return ret;
2039
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002040error:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002041 il4965_hw_txq_ctx_free(il);
2042 il4965_free_dma_ptr(il, &il->kw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002043error_kw:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002044 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002045error_bc_tbls:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002046 return ret;
2047}
2048
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002049void
2050il4965_txq_ctx_reset(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002051{
2052 int txq_id, slots_num;
2053 unsigned long flags;
2054
2055 spin_lock_irqsave(&il->lock, flags);
2056
2057 /* Turn off all Tx DMA fifos */
2058 il4965_txq_set_sched(il, 0);
2059
2060 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002061 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002062
2063 spin_unlock_irqrestore(&il->lock, flags);
2064
2065 /* Alloc and init all Tx queues, including the command queue (#4) */
2066 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002067 slots_num =
2068 txq_id == il->cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2069 il_tx_queue_reset(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002070 }
2071}
2072
2073/**
2074 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2075 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002076void
2077il4965_txq_ctx_stop(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002078{
2079 int ch, txq_id;
2080 unsigned long flags;
2081
2082 /* Turn off all Tx DMA fifos */
2083 spin_lock_irqsave(&il->lock, flags);
2084
2085 il4965_txq_set_sched(il, 0);
2086
2087 /* Stop each Tx DMA channel, and wait for it to be idle */
2088 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002089 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2090 if (il_poll_bit
2091 (il, FH49_TSSR_TX_STATUS_REG,
2092 FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002093 IL_ERR("Failing on timeout while stopping"
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002094 " DMA channel %d [0x%08x]", ch,
2095 il_rd(il, FH49_TSSR_TX_STATUS_REG));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002096 }
2097 spin_unlock_irqrestore(&il->lock, flags);
2098
2099 if (!il->txq)
2100 return;
2101
2102 /* Unmap DMA from host system and free skb's */
2103 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2104 if (txq_id == il->cmd_queue)
2105 il_cmd_queue_unmap(il);
2106 else
2107 il_tx_queue_unmap(il, txq_id);
2108}
2109
2110/*
2111 * Find first available (lowest unused) Tx Queue, mark it "active".
2112 * Called only when finding queue for aggregation.
2113 * Should never return anything < 7, because they should already
2114 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2115 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002116static int
2117il4965_txq_ctx_activate_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002118{
2119 int txq_id;
2120
2121 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2122 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2123 return txq_id;
2124 return -1;
2125}
2126
2127/**
2128 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2129 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002130static void
2131il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002132{
2133 /* Simply stop the queue, but don't change any configuration;
2134 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002135 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002136 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
2137 (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002138}
2139
2140/**
2141 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2142 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002143static int
2144il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002145{
2146 u32 tbl_dw_addr;
2147 u32 tbl_dw;
2148 u16 scd_q2ratid;
2149
2150 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2151
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002152 tbl_dw_addr =
2153 il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002154
2155 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2156
2157 if (txq_id & 0x1)
2158 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2159 else
2160 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2161
2162 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2163
2164 return 0;
2165}
2166
2167/**
2168 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2169 *
2170 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2171 * i.e. it must be one of the higher queues used for aggregation
2172 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002173static int
2174il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
2175 int tid, u16 ssn_idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002176{
2177 unsigned long flags;
2178 u16 ra_tid;
2179 int ret;
2180
2181 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2182 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002183 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2184 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002185 txq_id, IL49_FIRST_AMPDU_QUEUE,
2186 IL49_FIRST_AMPDU_QUEUE +
2187 il->cfg->base_params->num_of_ampdu_queues - 1);
2188 return -EINVAL;
2189 }
2190
2191 ra_tid = BUILD_RAxTID(sta_id, tid);
2192
2193 /* Modify device's station table to Tx this TID */
2194 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2195 if (ret)
2196 return ret;
2197
2198 spin_lock_irqsave(&il->lock, flags);
2199
2200 /* Stop this Tx queue before configuring it */
2201 il4965_tx_queue_stop_scheduler(il, txq_id);
2202
2203 /* Map receiver-address / traffic-ID to this queue */
2204 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2205
2206 /* Set this queue as a chain-building queue */
2207 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2208
2209 /* Place first TFD at idx corresponding to start sequence number.
2210 * Assumes that ssn_idx is valid (!= 0xFFF) */
2211 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2212 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2213 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2214
2215 /* Set up Tx win size and frame limit for this queue */
2216 il_write_targ_mem(il,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002217 il->scd_base_addr +
2218 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2219 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
2220 & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002221
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002222 il_write_targ_mem(il,
2223 il->scd_base_addr +
2224 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2225 (SCD_FRAME_LIMIT <<
2226 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
2227 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002228
2229 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2230
2231 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2232 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2233
2234 spin_unlock_irqrestore(&il->lock, flags);
2235
2236 return 0;
2237}
2238
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002239int
2240il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2241 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002242{
2243 int sta_id;
2244 int tx_fifo;
2245 int txq_id;
2246 int ret;
2247 unsigned long flags;
2248 struct il_tid_data *tid_data;
2249
2250 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2251 if (unlikely(tx_fifo < 0))
2252 return tx_fifo;
2253
Greg Dietsche53611e02011-08-28 08:26:16 -05002254 D_HT("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002255
2256 sta_id = il_sta_id(sta);
2257 if (sta_id == IL_INVALID_STATION) {
2258 IL_ERR("Start AGG on invalid station\n");
2259 return -ENXIO;
2260 }
2261 if (unlikely(tid >= MAX_TID_COUNT))
2262 return -EINVAL;
2263
2264 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2265 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2266 return -ENXIO;
2267 }
2268
2269 txq_id = il4965_txq_ctx_activate_free(il);
2270 if (txq_id == -1) {
2271 IL_ERR("No free aggregation queue available\n");
2272 return -ENXIO;
2273 }
2274
2275 spin_lock_irqsave(&il->sta_lock, flags);
2276 tid_data = &il->stations[sta_id].tid[tid];
2277 *ssn = SEQ_TO_SN(tid_data->seq_number);
2278 tid_data->agg.txq_id = txq_id;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002279 il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002280 spin_unlock_irqrestore(&il->sta_lock, flags);
2281
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002282 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002283 if (ret)
2284 return ret;
2285
2286 spin_lock_irqsave(&il->sta_lock, flags);
2287 tid_data = &il->stations[sta_id].tid[tid];
2288 if (tid_data->tfds_in_queue == 0) {
2289 D_HT("HW queue is empty\n");
2290 tid_data->agg.state = IL_AGG_ON;
2291 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2292 } else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002293 D_HT("HW queue is NOT empty: %d packets in HW queue\n",
2294 tid_data->tfds_in_queue);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002295 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2296 }
2297 spin_unlock_irqrestore(&il->sta_lock, flags);
2298 return ret;
2299}
2300
2301/**
2302 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2303 * il->lock must be held by the caller
2304 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002305static int
2306il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002307{
2308 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2309 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002310 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2311 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002312 txq_id, IL49_FIRST_AMPDU_QUEUE,
2313 IL49_FIRST_AMPDU_QUEUE +
2314 il->cfg->base_params->num_of_ampdu_queues - 1);
2315 return -EINVAL;
2316 }
2317
2318 il4965_tx_queue_stop_scheduler(il, txq_id);
2319
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002320 il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002321
2322 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2323 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2324 /* supposes that ssn_idx is valid (!= 0xFFF) */
2325 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2326
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002327 il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002328 il_txq_ctx_deactivate(il, txq_id);
2329 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2330
2331 return 0;
2332}
2333
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002334int
2335il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2336 struct ieee80211_sta *sta, u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002337{
2338 int tx_fifo_id, txq_id, sta_id, ssn;
2339 struct il_tid_data *tid_data;
2340 int write_ptr, read_ptr;
2341 unsigned long flags;
2342
2343 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2344 if (unlikely(tx_fifo_id < 0))
2345 return tx_fifo_id;
2346
2347 sta_id = il_sta_id(sta);
2348
2349 if (sta_id == IL_INVALID_STATION) {
2350 IL_ERR("Invalid station for AGG tid %d\n", tid);
2351 return -ENXIO;
2352 }
2353
2354 spin_lock_irqsave(&il->sta_lock, flags);
2355
2356 tid_data = &il->stations[sta_id].tid[tid];
2357 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2358 txq_id = tid_data->agg.txq_id;
2359
2360 switch (il->stations[sta_id].tid[tid].agg.state) {
2361 case IL_EMPTYING_HW_QUEUE_ADDBA:
2362 /*
2363 * This can happen if the peer stops aggregation
2364 * again before we've had a chance to drain the
2365 * queue we selected previously, i.e. before the
2366 * session was really started completely.
2367 */
2368 D_HT("AGG stop before setup done\n");
2369 goto turn_off;
2370 case IL_AGG_ON:
2371 break;
2372 default:
2373 IL_WARN("Stopping AGG while state not ON or starting\n");
2374 }
2375
2376 write_ptr = il->txq[txq_id].q.write_ptr;
2377 read_ptr = il->txq[txq_id].q.read_ptr;
2378
2379 /* The queue is not empty */
2380 if (write_ptr != read_ptr) {
2381 D_HT("Stopping a non empty AGG HW QUEUE\n");
2382 il->stations[sta_id].tid[tid].agg.state =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002383 IL_EMPTYING_HW_QUEUE_DELBA;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002384 spin_unlock_irqrestore(&il->sta_lock, flags);
2385 return 0;
2386 }
2387
2388 D_HT("HW queue is empty\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002389turn_off:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002390 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2391
2392 /* do not restore/save irqs */
2393 spin_unlock(&il->sta_lock);
2394 spin_lock(&il->lock);
2395
2396 /*
2397 * the only reason this call can fail is queue number out of range,
2398 * which can happen if uCode is reloaded and all the station
2399 * information are lost. if it is outside the range, there is no need
2400 * to deactivate the uCode queue, just return "success" to allow
2401 * mac80211 to clean up it own data.
2402 */
2403 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2404 spin_unlock_irqrestore(&il->lock, flags);
2405
2406 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2407
2408 return 0;
2409}
2410
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002411int
2412il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002413{
2414 struct il_queue *q = &il->txq[txq_id].q;
2415 u8 *addr = il->stations[sta_id].sta.sta.addr;
2416 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2417 struct il_rxon_context *ctx;
2418
2419 ctx = &il->ctx;
2420
2421 lockdep_assert_held(&il->sta_lock);
2422
2423 switch (il->stations[sta_id].tid[tid].agg.state) {
2424 case IL_EMPTYING_HW_QUEUE_DELBA:
2425 /* We are reclaiming the last packet of the */
2426 /* aggregated HW queue */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002427 if (txq_id == tid_data->agg.txq_id &&
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002428 q->read_ptr == q->write_ptr) {
2429 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2430 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002431 D_HT("HW queue empty: continue DELBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002432 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2433 tid_data->agg.state = IL_AGG_OFF;
2434 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2435 }
2436 break;
2437 case IL_EMPTYING_HW_QUEUE_ADDBA:
2438 /* We are reclaiming the last packet of the queue */
2439 if (tid_data->tfds_in_queue == 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002440 D_HT("HW queue empty: continue ADDBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002441 tid_data->agg.state = IL_AGG_ON;
2442 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2443 }
2444 break;
2445 }
2446
2447 return 0;
2448}
2449
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002450static void
2451il4965_non_agg_tx_status(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002452 const u8 *addr1)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002453{
2454 struct ieee80211_sta *sta;
2455 struct il_station_priv *sta_priv;
2456
2457 rcu_read_lock();
2458 sta = ieee80211_find_sta(ctx->vif, addr1);
2459 if (sta) {
2460 sta_priv = (void *)sta->drv_priv;
2461 /* avoid atomic ops if this isn't a client */
2462 if (sta_priv->client &&
2463 atomic_dec_return(&sta_priv->pending_frames) == 0)
2464 ieee80211_sta_block_awake(il->hw, sta, false);
2465 }
2466 rcu_read_unlock();
2467}
2468
2469static void
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002470il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info, bool is_agg)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002471{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002472 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002473
2474 if (!is_agg)
2475 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2476
2477 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2478}
2479
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002480int
2481il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002482{
2483 struct il_tx_queue *txq = &il->txq[txq_id];
2484 struct il_queue *q = &txq->q;
2485 struct il_tx_info *tx_info;
2486 int nfreed = 0;
2487 struct ieee80211_hdr *hdr;
2488
2489 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2490 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002491 "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
2492 q->write_ptr, q->read_ptr);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002493 return 0;
2494 }
2495
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002496 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002497 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2498
2499 tx_info = &txq->txb[txq->q.read_ptr];
2500
2501 if (WARN_ON_ONCE(tx_info->skb == NULL))
2502 continue;
2503
2504 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2505 if (ieee80211_is_data_qos(hdr->frame_control))
2506 nfreed++;
2507
2508 il4965_tx_status(il, tx_info,
2509 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2510 tx_info->skb = NULL;
2511
2512 il->cfg->ops->lib->txq_free_tfd(il, txq);
2513 }
2514 return nfreed;
2515}
2516
2517/**
2518 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2519 *
2520 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2521 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2522 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002523static int
2524il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
2525 struct il_compressed_ba_resp *ba_resp)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002526{
2527 int i, sh, ack;
2528 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2529 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2530 int successes = 0;
2531 struct ieee80211_tx_info *info;
2532 u64 bitmap, sent_bitmap;
2533
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002534 if (unlikely(!agg->wait_for_ba)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002535 if (unlikely(ba_resp->bitmap))
2536 IL_ERR("Received BA when not expected\n");
2537 return -EINVAL;
2538 }
2539
2540 /* Mark that the expected block-ack response arrived */
2541 agg->wait_for_ba = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002542 D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002543
2544 /* Calculate shift to align block-ack bits with our Tx win bits */
2545 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002546 if (sh < 0) /* tbw something is wrong with indices */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002547 sh += 0x100;
2548
2549 if (agg->frame_count > (64 - sh)) {
2550 D_TX_REPLY("more frames than bitmap size");
2551 return -1;
2552 }
2553
2554 /* don't use 64-bit values for now */
2555 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2556
2557 /* check for success or failure according to the
2558 * transmitted bitmap and block-ack bitmap */
2559 sent_bitmap = bitmap & agg->bitmap;
2560
2561 /* For each frame attempted in aggregation,
2562 * update driver's record of tx frame's status. */
2563 i = 0;
2564 while (sent_bitmap) {
2565 ack = sent_bitmap & 1ULL;
2566 successes += ack;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002567 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
2568 i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002569 sent_bitmap >>= 1;
2570 ++i;
2571 }
2572
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002573 D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002574
2575 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2576 memset(&info->status, 0, sizeof(info->status));
2577 info->flags |= IEEE80211_TX_STAT_ACK;
2578 info->flags |= IEEE80211_TX_STAT_AMPDU;
2579 info->status.ampdu_ack_len = successes;
2580 info->status.ampdu_len = agg->frame_count;
2581 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2582
2583 return 0;
2584}
2585
2586/**
2587 * translate ucode response to mac80211 tx status control values
2588 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002589void
2590il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2591 struct ieee80211_tx_info *info)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002592{
2593 struct ieee80211_tx_rate *r = &info->control.rates[0];
2594
2595 info->antenna_sel_tx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002596 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002597 if (rate_n_flags & RATE_MCS_HT_MSK)
2598 r->flags |= IEEE80211_TX_RC_MCS;
2599 if (rate_n_flags & RATE_MCS_GF_MSK)
2600 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2601 if (rate_n_flags & RATE_MCS_HT40_MSK)
2602 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2603 if (rate_n_flags & RATE_MCS_DUP_MSK)
2604 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2605 if (rate_n_flags & RATE_MCS_SGI_MSK)
2606 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2607 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2608}
2609
2610/**
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02002611 * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002612 *
2613 * Handles block-acknowledge notification from device, which reports success
2614 * of frames sent via aggregation.
2615 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002616void
2617il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002618{
2619 struct il_rx_pkt *pkt = rxb_addr(rxb);
2620 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2621 struct il_tx_queue *txq = NULL;
2622 struct il_ht_agg *agg;
2623 int idx;
2624 int sta_id;
2625 int tid;
2626 unsigned long flags;
2627
2628 /* "flow" corresponds to Tx queue */
2629 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2630
2631 /* "ssn" is start of block-ack Tx win, corresponds to idx
2632 * (in Tx queue's circular buffer) of first TFD/frame in win */
2633 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2634
2635 if (scd_flow >= il->hw_params.max_txq_num) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002636 IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002637 return;
2638 }
2639
2640 txq = &il->txq[scd_flow];
2641 sta_id = ba_resp->sta_id;
2642 tid = ba_resp->tid;
2643 agg = &il->stations[sta_id].tid[tid].agg;
2644 if (unlikely(agg->txq_id != scd_flow)) {
2645 /*
2646 * FIXME: this is a uCode bug which need to be addressed,
2647 * log the information and return for now!
2648 * since it is possible happen very often and in order
2649 * not to fill the syslog, don't enable the logging by default
2650 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002651 D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
2652 scd_flow, agg->txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002653 return;
2654 }
2655
2656 /* Find idx just before block-ack win */
2657 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2658
2659 spin_lock_irqsave(&il->sta_lock, flags);
2660
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002661 D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002662 agg->wait_for_ba, (u8 *) &ba_resp->sta_addr_lo32,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002663 ba_resp->sta_id);
2664 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
2665 "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
2666 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2667 ba_resp->scd_flow, ba_resp->scd_ssn);
2668 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
2669 (unsigned long long)agg->bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002670
2671 /* Update driver's record of ACK vs. not for each frame in win */
2672 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2673
2674 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2675 * block-ack win (we assume that they've been successfully
2676 * transmitted ... if not, it's too late anyway). */
2677 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2678 /* calculate mac80211 ampdu sw queue to wake */
2679 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2680 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2681
2682 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2683 il->mac80211_registered &&
2684 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2685 il_wake_queue(il, txq);
2686
2687 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2688 }
2689
2690 spin_unlock_irqrestore(&il->sta_lock, flags);
2691}
2692
2693#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002694const char *
2695il4965_get_tx_fail_reason(u32 status)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002696{
2697#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2698#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2699
2700 switch (status & TX_STATUS_MSK) {
2701 case TX_STATUS_SUCCESS:
2702 return "SUCCESS";
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002703 TX_STATUS_POSTPONE(DELAY);
2704 TX_STATUS_POSTPONE(FEW_BYTES);
2705 TX_STATUS_POSTPONE(QUIET_PERIOD);
2706 TX_STATUS_POSTPONE(CALC_TTAK);
2707 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2708 TX_STATUS_FAIL(SHORT_LIMIT);
2709 TX_STATUS_FAIL(LONG_LIMIT);
2710 TX_STATUS_FAIL(FIFO_UNDERRUN);
2711 TX_STATUS_FAIL(DRAIN_FLOW);
2712 TX_STATUS_FAIL(RFKILL_FLUSH);
2713 TX_STATUS_FAIL(LIFE_EXPIRE);
2714 TX_STATUS_FAIL(DEST_PS);
2715 TX_STATUS_FAIL(HOST_ABORTED);
2716 TX_STATUS_FAIL(BT_RETRY);
2717 TX_STATUS_FAIL(STA_INVALID);
2718 TX_STATUS_FAIL(FRAG_DROPPED);
2719 TX_STATUS_FAIL(TID_DISABLE);
2720 TX_STATUS_FAIL(FIFO_FLUSHED);
2721 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2722 TX_STATUS_FAIL(PASSIVE_NO_RX);
2723 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002724 }
2725
2726 return "UNKNOWN";
2727
2728#undef TX_STATUS_FAIL
2729#undef TX_STATUS_POSTPONE
2730}
2731#endif /* CONFIG_IWLEGACY_DEBUG */
2732
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002733static struct il_link_quality_cmd *
2734il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2735{
2736 int i, r;
2737 struct il_link_quality_cmd *link_cmd;
2738 u32 rate_flags = 0;
2739 __le32 rate_n_flags;
2740
2741 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2742 if (!link_cmd) {
2743 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2744 return NULL;
2745 }
2746 /* Set up the rate scaling to start at selected rate, fall back
2747 * all the way down to 1M in IEEE order, and then spin on 1M */
2748 if (il->band == IEEE80211_BAND_5GHZ)
2749 r = RATE_6M_IDX;
2750 else
2751 r = RATE_1M_IDX;
2752
2753 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2754 rate_flags |= RATE_MCS_CCK_MSK;
2755
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002756 rate_flags |=
2757 il4965_first_antenna(il->hw_params.
2758 valid_tx_ant) << RATE_MCS_ANT_POS;
2759 rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp, rate_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002760 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2761 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2762
2763 link_cmd->general_params.single_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002764 il4965_first_antenna(il->hw_params.valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002765
2766 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002767 il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
2768 valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002769 if (!link_cmd->general_params.dual_stream_ant_msk) {
2770 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2771 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2772 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002773 il->hw_params.valid_tx_ant;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002774 }
2775
2776 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2777 link_cmd->agg_params.agg_time_limit =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002778 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002779
2780 link_cmd->sta_id = sta_id;
2781
2782 return link_cmd;
2783}
2784
2785/*
2786 * il4965_add_bssid_station - Add the special IBSS BSSID station
2787 *
2788 * Function sleeps.
2789 */
2790int
2791il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002792 const u8 *addr, u8 *sta_id_r)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002793{
2794 int ret;
2795 u8 sta_id;
2796 struct il_link_quality_cmd *link_cmd;
2797 unsigned long flags;
2798
2799 if (sta_id_r)
2800 *sta_id_r = IL_INVALID_STATION;
2801
2802 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2803 if (ret) {
2804 IL_ERR("Unable to add station %pM\n", addr);
2805 return ret;
2806 }
2807
2808 if (sta_id_r)
2809 *sta_id_r = sta_id;
2810
2811 spin_lock_irqsave(&il->sta_lock, flags);
2812 il->stations[sta_id].used |= IL_STA_LOCAL;
2813 spin_unlock_irqrestore(&il->sta_lock, flags);
2814
2815 /* Set up default rate scaling table in device's station table */
2816 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2817 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002818 IL_ERR("Unable to initialize rate scaling for station %pM.\n",
2819 addr);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002820 return -ENOMEM;
2821 }
2822
2823 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2824 if (ret)
2825 IL_ERR("Link quality command failed (%d)\n", ret);
2826
2827 spin_lock_irqsave(&il->sta_lock, flags);
2828 il->stations[sta_id].lq = link_cmd;
2829 spin_unlock_irqrestore(&il->sta_lock, flags);
2830
2831 return 0;
2832}
2833
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002834static int
2835il4965_static_wepkey_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2836 bool send_if_empty)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002837{
2838 int i, not_empty = 0;
2839 u8 buff[sizeof(struct il_wep_cmd) +
2840 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2841 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002842 size_t cmd_size = sizeof(struct il_wep_cmd);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002843 struct il_host_cmd cmd = {
2844 .id = ctx->wep_key_cmd,
2845 .data = wep_cmd,
2846 .flags = CMD_SYNC,
2847 };
2848
2849 might_sleep();
2850
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002851 memset(wep_cmd, 0,
2852 cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002853
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002854 for (i = 0; i < WEP_KEYS_MAX; i++) {
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002855 wep_cmd->key[i].key_idx = i;
2856 if (ctx->wep_keys[i].key_size) {
2857 wep_cmd->key[i].key_offset = i;
2858 not_empty = 1;
2859 } else {
2860 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2861 }
2862
2863 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
2864 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002865 ctx->wep_keys[i].key_size);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002866 }
2867
2868 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2869 wep_cmd->num_keys = WEP_KEYS_MAX;
2870
2871 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
2872
2873 cmd.len = cmd_size;
2874
2875 if (not_empty || send_if_empty)
2876 return il_send_cmd(il, &cmd);
2877 else
2878 return 0;
2879}
2880
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002881int
2882il4965_restore_default_wep_keys(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002883{
2884 lockdep_assert_held(&il->mutex);
2885
2886 return il4965_static_wepkey_cmd(il, ctx, false);
2887}
2888
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002889int
2890il4965_remove_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2891 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002892{
2893 int ret;
2894
2895 lockdep_assert_held(&il->mutex);
2896
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002897 D_WEP("Removing default WEP key: idx=%d\n", keyconf->keyidx);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002898
2899 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
2900 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002901 D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002902 /* but keys in device are clear anyway so return success */
2903 return 0;
2904 }
2905 ret = il4965_static_wepkey_cmd(il, ctx, 1);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002906 D_WEP("Remove default WEP key: idx=%d ret=%d\n", keyconf->keyidx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002907
2908 return ret;
2909}
2910
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002911int
2912il4965_set_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2913 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002914{
2915 int ret;
2916
2917 lockdep_assert_held(&il->mutex);
2918
2919 if (keyconf->keylen != WEP_KEY_LEN_128 &&
2920 keyconf->keylen != WEP_KEY_LEN_64) {
2921 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2922 return -EINVAL;
2923 }
2924
2925 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2926 keyconf->hw_key_idx = HW_KEY_DEFAULT;
2927 il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
2928
2929 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
2930 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002931 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002932
2933 ret = il4965_static_wepkey_cmd(il, ctx, false);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002934 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", keyconf->keylen,
2935 keyconf->keyidx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002936
2937 return ret;
2938}
2939
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002940static int
2941il4965_set_wep_dynamic_key_info(struct il_priv *il, struct il_rxon_context *ctx,
2942 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002943{
2944 unsigned long flags;
2945 __le16 key_flags = 0;
2946 struct il_addsta_cmd sta_cmd;
2947
2948 lockdep_assert_held(&il->mutex);
2949
2950 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2951
2952 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2953 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2954 key_flags &= ~STA_KEY_FLG_INVALID;
2955
2956 if (keyconf->keylen == WEP_KEY_LEN_128)
2957 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2958
2959 if (sta_id == ctx->bcast_sta_id)
2960 key_flags |= STA_KEY_MULTICAST_MSK;
2961
2962 spin_lock_irqsave(&il->sta_lock, flags);
2963
2964 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2965 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2966 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2967
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002968 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002969
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002970 memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
2971 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002972
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002973 if ((il->stations[sta_id].sta.key.
2974 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002975 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002976 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002977 /* else, we are overriding an existing key => no need to allocated room
2978 * in uCode. */
2979
2980 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002981 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002982
2983 il->stations[sta_id].sta.key.key_flags = key_flags;
2984 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
2985 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2986
2987 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002988 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002989 spin_unlock_irqrestore(&il->sta_lock, flags);
2990
2991 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2992}
2993
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002994static int
2995il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
2996 struct il_rxon_context *ctx,
2997 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002998{
2999 unsigned long flags;
3000 __le16 key_flags = 0;
3001 struct il_addsta_cmd sta_cmd;
3002
3003 lockdep_assert_held(&il->mutex);
3004
3005 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
3006 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3007 key_flags &= ~STA_KEY_FLG_INVALID;
3008
3009 if (sta_id == ctx->bcast_sta_id)
3010 key_flags |= STA_KEY_MULTICAST_MSK;
3011
3012 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3013
3014 spin_lock_irqsave(&il->sta_lock, flags);
3015 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3016 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
3017
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003018 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003019
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003020 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003021
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003022 if ((il->stations[sta_id].sta.key.
3023 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003024 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003025 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003026 /* else, we are overriding an existing key => no need to allocated room
3027 * in uCode. */
3028
3029 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003030 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003031
3032 il->stations[sta_id].sta.key.key_flags = key_flags;
3033 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3034 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3035
3036 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003037 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003038 spin_unlock_irqrestore(&il->sta_lock, flags);
3039
3040 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3041}
3042
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003043static int
3044il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3045 struct il_rxon_context *ctx,
3046 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003047{
3048 unsigned long flags;
3049 int ret = 0;
3050 __le16 key_flags = 0;
3051
3052 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3053 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3054 key_flags &= ~STA_KEY_FLG_INVALID;
3055
3056 if (sta_id == ctx->bcast_sta_id)
3057 key_flags |= STA_KEY_MULTICAST_MSK;
3058
3059 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3060 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3061
3062 spin_lock_irqsave(&il->sta_lock, flags);
3063
3064 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3065 il->stations[sta_id].keyinfo.keylen = 16;
3066
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003067 if ((il->stations[sta_id].sta.key.
3068 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003069 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003070 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003071 /* else, we are overriding an existing key => no need to allocated room
3072 * in uCode. */
3073
3074 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003075 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003076
3077 il->stations[sta_id].sta.key.key_flags = key_flags;
3078
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003079 /* This copy is acutally not needed: we get the key with each TX */
3080 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3081
3082 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3083
3084 spin_unlock_irqrestore(&il->sta_lock, flags);
3085
3086 return ret;
3087}
3088
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003089void
3090il4965_update_tkip_key(struct il_priv *il, struct il_rxon_context *ctx,
3091 struct ieee80211_key_conf *keyconf,
3092 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003093{
3094 u8 sta_id;
3095 unsigned long flags;
3096 int i;
3097
3098 if (il_scan_cancel(il)) {
3099 /* cancel scan failed, just live w/ bad key and rely
3100 briefly on SW decryption */
3101 return;
3102 }
3103
3104 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3105 if (sta_id == IL_INVALID_STATION)
3106 return;
3107
3108 spin_lock_irqsave(&il->sta_lock, flags);
3109
3110 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3111
3112 for (i = 0; i < 5; i++)
3113 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003114 cpu_to_le16(phase1key[i]);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003115
3116 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3117 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3118
3119 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3120
3121 spin_unlock_irqrestore(&il->sta_lock, flags);
3122
3123}
3124
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003125int
3126il4965_remove_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3127 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003128{
3129 unsigned long flags;
3130 u16 key_flags;
3131 u8 keyidx;
3132 struct il_addsta_cmd sta_cmd;
3133
3134 lockdep_assert_held(&il->mutex);
3135
3136 ctx->key_mapping_keys--;
3137
3138 spin_lock_irqsave(&il->sta_lock, flags);
3139 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3140 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3141
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003142 D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003143
3144 if (keyconf->keyidx != keyidx) {
3145 /* We need to remove a key with idx different that the one
3146 * in the uCode. This means that the key we need to remove has
3147 * been replaced by another one with different idx.
3148 * Don't do anything and return ok
3149 */
3150 spin_unlock_irqrestore(&il->sta_lock, flags);
3151 return 0;
3152 }
3153
3154 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003155 IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
3156 key_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003157 spin_unlock_irqrestore(&il->sta_lock, flags);
3158 return 0;
3159 }
3160
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003161 if (!test_and_clear_bit
3162 (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003163 IL_ERR("idx %d not used in uCode key table.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003164 il->stations[sta_id].sta.key.key_offset);
3165 memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
3166 memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003167 il->stations[sta_id].sta.key.key_flags =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003168 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003169 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3170 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3171 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3172
3173 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003174 D_WEP
3175 ("Not sending C_ADD_STA command because RFKILL enabled.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003176 spin_unlock_irqrestore(&il->sta_lock, flags);
3177 return 0;
3178 }
3179 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003180 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003181 spin_unlock_irqrestore(&il->sta_lock, flags);
3182
3183 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3184}
3185
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003186int
3187il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3188 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003189{
3190 int ret;
3191
3192 lockdep_assert_held(&il->mutex);
3193
3194 ctx->key_mapping_keys++;
3195 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3196
3197 switch (keyconf->cipher) {
3198 case WLAN_CIPHER_SUITE_CCMP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003199 ret =
3200 il4965_set_ccmp_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003201 break;
3202 case WLAN_CIPHER_SUITE_TKIP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003203 ret =
3204 il4965_set_tkip_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003205 break;
3206 case WLAN_CIPHER_SUITE_WEP40:
3207 case WLAN_CIPHER_SUITE_WEP104:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003208 ret = il4965_set_wep_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003209 break;
3210 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003211 IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
3212 keyconf->cipher);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003213 ret = -EINVAL;
3214 }
3215
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003216 D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3217 keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003218
3219 return ret;
3220}
3221
3222/**
3223 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3224 *
3225 * This adds the broadcast station into the driver's station table
3226 * and marks it driver active, so that it will be restored to the
3227 * device at the next best time.
3228 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003229int
3230il4965_alloc_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003231{
3232 struct il_link_quality_cmd *link_cmd;
3233 unsigned long flags;
3234 u8 sta_id;
3235
3236 spin_lock_irqsave(&il->sta_lock, flags);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003237 sta_id = il_prep_station(il, ctx, il_bcast_addr, false, NULL);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003238 if (sta_id == IL_INVALID_STATION) {
3239 IL_ERR("Unable to prepare broadcast station\n");
3240 spin_unlock_irqrestore(&il->sta_lock, flags);
3241
3242 return -EINVAL;
3243 }
3244
3245 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3246 il->stations[sta_id].used |= IL_STA_BCAST;
3247 spin_unlock_irqrestore(&il->sta_lock, flags);
3248
3249 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3250 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003251 IL_ERR
3252 ("Unable to initialize rate scaling for bcast station.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003253 return -ENOMEM;
3254 }
3255
3256 spin_lock_irqsave(&il->sta_lock, flags);
3257 il->stations[sta_id].lq = link_cmd;
3258 spin_unlock_irqrestore(&il->sta_lock, flags);
3259
3260 return 0;
3261}
3262
3263/**
3264 * il4965_update_bcast_station - update broadcast station's LQ command
3265 *
3266 * Only used by iwl4965. Placed here to have all bcast station management
3267 * code together.
3268 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003269static int
3270il4965_update_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003271{
3272 unsigned long flags;
3273 struct il_link_quality_cmd *link_cmd;
3274 u8 sta_id = ctx->bcast_sta_id;
3275
3276 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3277 if (!link_cmd) {
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003278 IL_ERR("Unable to initialize rate scaling for bcast sta.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003279 return -ENOMEM;
3280 }
3281
3282 spin_lock_irqsave(&il->sta_lock, flags);
3283 if (il->stations[sta_id].lq)
3284 kfree(il->stations[sta_id].lq);
3285 else
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003286 D_INFO("Bcast sta rate scaling has not been initialized.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003287 il->stations[sta_id].lq = link_cmd;
3288 spin_unlock_irqrestore(&il->sta_lock, flags);
3289
3290 return 0;
3291}
3292
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003293int
3294il4965_update_bcast_stations(struct il_priv *il)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003295{
3296 return il4965_update_bcast_station(il, &il->ctx);
3297}
3298
3299/**
3300 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3301 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003302int
3303il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003304{
3305 unsigned long flags;
3306 struct il_addsta_cmd sta_cmd;
3307
3308 lockdep_assert_held(&il->mutex);
3309
3310 /* Remove "disable" flag, to enable Tx for this TID */
3311 spin_lock_irqsave(&il->sta_lock, flags);
3312 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3313 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3314 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3315 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003316 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003317 spin_unlock_irqrestore(&il->sta_lock, flags);
3318
3319 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3320}
3321
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003322int
3323il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
3324 u16 ssn)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003325{
3326 unsigned long flags;
3327 int sta_id;
3328 struct il_addsta_cmd sta_cmd;
3329
3330 lockdep_assert_held(&il->mutex);
3331
3332 sta_id = il_sta_id(sta);
3333 if (sta_id == IL_INVALID_STATION)
3334 return -ENXIO;
3335
3336 spin_lock_irqsave(&il->sta_lock, flags);
3337 il->stations[sta_id].sta.station_flags_msk = 0;
3338 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003339 il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003340 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3341 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3342 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003343 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003344 spin_unlock_irqrestore(&il->sta_lock, flags);
3345
3346 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3347}
3348
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003349int
3350il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003351{
3352 unsigned long flags;
3353 int sta_id;
3354 struct il_addsta_cmd sta_cmd;
3355
3356 lockdep_assert_held(&il->mutex);
3357
3358 sta_id = il_sta_id(sta);
3359 if (sta_id == IL_INVALID_STATION) {
3360 IL_ERR("Invalid station for AGG tid %d\n", tid);
3361 return -ENXIO;
3362 }
3363
3364 spin_lock_irqsave(&il->sta_lock, flags);
3365 il->stations[sta_id].sta.station_flags_msk = 0;
3366 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003367 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003368 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3369 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003370 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003371 spin_unlock_irqrestore(&il->sta_lock, flags);
3372
3373 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3374}
3375
3376void
3377il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3378{
3379 unsigned long flags;
3380
3381 spin_lock_irqsave(&il->sta_lock, flags);
3382 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3383 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3384 il->stations[sta_id].sta.sta.modify_mask =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003385 STA_MODIFY_SLEEP_TX_COUNT_MSK;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003386 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3387 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003388 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003389 spin_unlock_irqrestore(&il->sta_lock, flags);
3390
3391}
3392
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003393void
3394il4965_update_chain_flags(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003395{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003396 if (il->cfg->ops->hcmd->set_rxon_chain) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003397 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
3398 if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
3399 il_commit_rxon(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003400 }
3401}
3402
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003403static void
3404il4965_clear_free_frames(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003405{
3406 struct list_head *element;
3407
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003408 D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003409
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003410 while (!list_empty(&il->free_frames)) {
3411 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003412 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003413 kfree(list_entry(element, struct il_frame, list));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003414 il->frames_count--;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003415 }
3416
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003417 if (il->frames_count) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003418 IL_WARN("%d frames still in use. Did we lose one?\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003419 il->frames_count);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003420 il->frames_count = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003421 }
3422}
3423
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003424static struct il_frame *
3425il4965_get_free_frame(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003426{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003427 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003428 struct list_head *element;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003429 if (list_empty(&il->free_frames)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003430 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3431 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003432 IL_ERR("Could not allocate frame!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003433 return NULL;
3434 }
3435
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003436 il->frames_count++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003437 return frame;
3438 }
3439
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003440 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003441 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003442 return list_entry(element, struct il_frame, list);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003443}
3444
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003445static void
3446il4965_free_frame(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003447{
3448 memset(frame, 0, sizeof(*frame));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003449 list_add(&frame->list, &il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003450}
3451
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003452static u32
3453il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
3454 int left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003455{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003456 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003457
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003458 if (!il->beacon_skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003459 return 0;
3460
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003461 if (il->beacon_skb->len > left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003462 return 0;
3463
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003464 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003465
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003466 return il->beacon_skb->len;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003467}
3468
3469/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003470static void
3471il4965_set_beacon_tim(struct il_priv *il,
3472 struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
3473 u32 frame_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003474{
3475 u16 tim_idx;
3476 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3477
3478 /*
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003479 * The idx is relative to frame start but we start looking at the
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003480 * variable-length part of the beacon.
3481 */
3482 tim_idx = mgmt->u.beacon.variable - beacon;
3483
3484 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3485 while ((tim_idx < (frame_size - 2)) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003486 (beacon[tim_idx] != WLAN_EID_TIM))
3487 tim_idx += beacon[tim_idx + 1] + 2;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003488
3489 /* If TIM field was found, set variables */
3490 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3491 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003492 tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003493 } else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003494 IL_WARN("Unable to find TIM Element in beacon\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003495}
3496
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003497static unsigned int
3498il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003499{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003500 struct il_tx_beacon_cmd *tx_beacon_cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003501 u32 frame_size;
3502 u32 rate_flags;
3503 u32 rate;
3504 /*
3505 * We have to set up the TX command, the TX Beacon command, and the
3506 * beacon contents.
3507 */
3508
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003509 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003510
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003511 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003512 IL_ERR("trying to build beacon w/o beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003513 return 0;
3514 }
3515
3516 /* Initialize memory */
3517 tx_beacon_cmd = &frame->u.beacon;
3518 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3519
3520 /* Set up TX beacon contents */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003521 frame_size =
3522 il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
3523 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003524 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3525 return 0;
3526 if (!frame_size)
3527 return 0;
3528
3529 /* Set up TX command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003530 tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003531 tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003532 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003533 tx_beacon_cmd->tx.tx_flags =
3534 TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
3535 TX_CMD_FLG_STA_RATE_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003536
3537 /* Set up TX beacon command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003538 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
3539 frame_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003540
3541 /* Set up packet rate and flags */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003542 rate = il_get_lowest_plcp(il, il->beacon_ctx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003543 il->mgmt_tx_ant =
3544 il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
3545 il->hw_params.valid_tx_ant);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003546 rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003547 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003548 rate_flags |= RATE_MCS_CCK_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003549 tx_beacon_cmd->tx.rate_n_flags =
3550 il4965_hw_set_rate_n_flags(rate, rate_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003551
3552 return sizeof(*tx_beacon_cmd) + frame_size;
3553}
3554
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003555int
3556il4965_send_beacon_cmd(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003557{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003558 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003559 unsigned int frame_size;
3560 int rc;
3561
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003562 frame = il4965_get_free_frame(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003563 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003564 IL_ERR("Could not obtain free frame buffer for beacon "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003565 "command.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003566 return -ENOMEM;
3567 }
3568
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003569 frame_size = il4965_hw_get_beacon_cmd(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003570 if (!frame_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003571 IL_ERR("Error configuring the beacon command\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003572 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003573 return -EINVAL;
3574 }
3575
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003576 rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003577
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003578 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003579
3580 return rc;
3581}
3582
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003583static inline dma_addr_t
3584il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
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
3588 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3589 if (sizeof(dma_addr_t) > sizeof(u32))
3590 addr |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003591 ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
3592 16;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003593
3594 return addr;
3595}
3596
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003597static inline u16
3598il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003599{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003600 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003601
3602 return le16_to_cpu(tb->hi_n_len) >> 4;
3603}
3604
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003605static inline void
3606il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003607{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003608 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003609 u16 hi_n_len = len << 4;
3610
3611 put_unaligned_le32(addr, &tb->lo);
3612 if (sizeof(dma_addr_t) > sizeof(u32))
3613 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3614
3615 tb->hi_n_len = cpu_to_le16(hi_n_len);
3616
3617 tfd->num_tbs = idx + 1;
3618}
3619
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003620static inline u8
3621il4965_tfd_get_num_tbs(struct il_tfd *tfd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003622{
3623 return tfd->num_tbs & 0x1f;
3624}
3625
3626/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003627 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003628 * @il - driver ilate data
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003629 * @txq - tx queue
3630 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003631 * Does NOT advance any TFD circular buffer read/write idxes
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003632 * Does NOT free the TFD itself (which is within circular buffer)
3633 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003634void
3635il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003636{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003637 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3638 struct il_tfd *tfd;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003639 struct pci_dev *dev = il->pci_dev;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003640 int idx = txq->q.read_ptr;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003641 int i;
3642 int num_tbs;
3643
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003644 tfd = &tfd_tmp[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003645
3646 /* Sanity check on number of chunks */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003647 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003648
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003649 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003650 IL_ERR("Too many chunks: %i\n", num_tbs);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003651 /* @todo issue fatal error, it is quite serious situation */
3652 return;
3653 }
3654
3655 /* Unmap tx_cmd */
3656 if (num_tbs)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003657 pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
3658 dma_unmap_len(&txq->meta[idx], len),
3659 PCI_DMA_BIDIRECTIONAL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003660
3661 /* Unmap chunks, if any. */
3662 for (i = 1; i < num_tbs; i++)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003663 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003664 il4965_tfd_tb_get_len(tfd, i),
3665 PCI_DMA_TODEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003666
3667 /* free SKB */
3668 if (txq->txb) {
3669 struct sk_buff *skb;
3670
3671 skb = txq->txb[txq->q.read_ptr].skb;
3672
3673 /* can be called from irqs-disabled context */
3674 if (skb) {
3675 dev_kfree_skb_any(skb);
3676 txq->txb[txq->q.read_ptr].skb = NULL;
3677 }
3678 }
3679}
3680
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003681int
3682il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
3683 dma_addr_t addr, u16 len, u8 reset, u8 pad)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003684{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003685 struct il_queue *q;
3686 struct il_tfd *tfd, *tfd_tmp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003687 u32 num_tbs;
3688
3689 q = &txq->q;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003690 tfd_tmp = (struct il_tfd *)txq->tfds;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003691 tfd = &tfd_tmp[q->write_ptr];
3692
3693 if (reset)
3694 memset(tfd, 0, sizeof(*tfd));
3695
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003696 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003697
3698 /* Each TFD can point to a maximum 20 Tx buffers */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003699 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003700 IL_ERR("Error can not send more than %d chunks\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003701 IL_NUM_OF_TBS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003702 return -EINVAL;
3703 }
3704
3705 BUG_ON(addr & ~DMA_BIT_MASK(36));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003706 if (unlikely(addr & ~IL_TX_DMA_MASK))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003707 IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003708
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003709 il4965_tfd_set_tb(tfd, num_tbs, addr, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003710
3711 return 0;
3712}
3713
3714/*
3715 * Tell nic where to find circular buffer of Tx Frame Descriptors for
3716 * given Tx queue, and enable the DMA channel used for that queue.
3717 *
3718 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
3719 * channels supported in hardware.
3720 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003721int
3722il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003723{
3724 int txq_id = txq->q.id;
3725
3726 /* Circular buffer (TFD queue in DRAM) physical base address */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003727 il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003728
3729 return 0;
3730}
3731
3732/******************************************************************************
3733 *
3734 * Generic RX handler implementations
3735 *
3736 ******************************************************************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003737static void
3738il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003739{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003740 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003741 struct il_alive_resp *palive;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003742 struct delayed_work *pwork;
3743
3744 palive = &pkt->u.alive_frame;
3745
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003746 D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
3747 palive->is_valid, palive->ver_type, palive->ver_subtype);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003748
3749 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003750 D_INFO("Initialization Alive received.\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003751 memcpy(&il->card_alive_init, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003752 sizeof(struct il_init_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003753 pwork = &il->init_alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003754 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003755 D_INFO("Runtime Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003756 memcpy(&il->card_alive, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003757 sizeof(struct il_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003758 pwork = &il->alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003759 }
3760
3761 /* We delay the ALIVE response by 5ms to
3762 * give the HW RF Kill time to activate... */
3763 if (palive->is_valid == UCODE_VALID_OK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003764 queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003765 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003766 IL_WARN("uCode did not respond OK.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003767}
3768
3769/**
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003770 * il4965_bg_stats_periodic - Timer callback to queue stats
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003771 *
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003772 * This callback is provided in order to send a stats request.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003773 *
3774 * This timer function is continually reset to execute within
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02003775 * REG_RECALIB_PERIOD seconds since the last N_STATS
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003776 * was received. We need to ensure we receive the stats in order
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003777 * to update the temperature used for calibrating the TXPOWER.
3778 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003779static void
3780il4965_bg_stats_periodic(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003781{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003782 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003783
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003784 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003785 return;
3786
3787 /* dont send host command if rf-kill is on */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003788 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003789 return;
3790
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003791 il_send_stats_request(il, CMD_ASYNC, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003792}
3793
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003794static void
3795il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003796{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003797 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003798 struct il4965_beacon_notif *beacon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003799 (struct il4965_beacon_notif *)pkt->u.raw;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003800#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003801 u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003802
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003803 D_RX("beacon status %x retries %d iss %d " "tsf %d %d rate %d\n",
3804 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
3805 beacon->beacon_notify_hdr.failure_frame,
3806 le32_to_cpu(beacon->ibss_mgr_status),
3807 le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003808#endif
3809
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003810 il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003811}
3812
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003813static void
3814il4965_perform_ct_kill_task(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003815{
3816 unsigned long flags;
3817
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003818 D_POWER("Stop all queues\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003819
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003820 if (il->mac80211_registered)
3821 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003822
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003823 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003824 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003825 _il_rd(il, CSR_UCODE_DRV_GP1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003826
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003827 spin_lock_irqsave(&il->reg_lock, flags);
Stanislaw Gruszka13882262011-08-24 15:39:23 +02003828 if (!_il_grab_nic_access(il))
3829 _il_release_nic_access(il);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003830 spin_unlock_irqrestore(&il->reg_lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003831}
3832
3833/* Handle notification from uCode that card's power state is changing
3834 * due to software, hardware, or critical temperature RFKILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003835static void
3836il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003837{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003838 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003839 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003840 unsigned long status = il->status;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003841
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003842 D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003843 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3844 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
3845 (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003846
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003847 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003848
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003849 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003850 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003851
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003852 il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003853
3854 if (!(flags & RXON_CARD_DISABLED)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003855 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003856 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003857 il_wr(il, HBUS_TARG_MBX_C,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003858 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003859 }
3860 }
3861
3862 if (flags & CT_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003863 il4965_perform_ct_kill_task(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003864
3865 if (flags & HW_CARD_DISABLED)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003866 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003867 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003868 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003869
3870 if (!(flags & RXON_CARD_DISABLED))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003871 il_scan_cancel(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003872
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003873 if ((test_bit(S_RF_KILL_HW, &status) !=
3874 test_bit(S_RF_KILL_HW, &il->status)))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003875 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003876 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003877 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003878 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003879}
3880
3881/**
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003882 * il4965_setup_handlers - Initialize Rx handler callbacks
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003883 *
3884 * Setup the RX handlers for each of the reply types sent from the uCode
3885 * to the host.
3886 *
3887 * This function chains into the hardware specific files for them to setup
3888 * any hardware specific handlers as well.
3889 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003890static void
3891il4965_setup_handlers(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003892{
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003893 il->handlers[N_ALIVE] = il4965_hdl_alive;
3894 il->handlers[N_ERROR] = il_hdl_error;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003895 il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003896 il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003897 il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003898 il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003899 il->handlers[N_BEACON] = il4965_hdl_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003900
3901 /*
3902 * The same handler is used for both the REPLY to a discrete
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003903 * stats request from the host as well as for the periodic
3904 * stats notifications (after received beacons) from the uCode.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003905 */
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003906 il->handlers[C_STATS] = il4965_hdl_c_stats;
3907 il->handlers[N_STATS] = il4965_hdl_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003908
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003909 il_setup_rx_scan_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003910
3911 /* status change handler */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003912 il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003913
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003914 il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003915 /* Rx handlers */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003916 il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
3917 il->handlers[N_RX_MPDU] = il4965_hdl_rx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003918 /* block ack */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003919 il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003920 /* Set up hardware specific Rx handlers */
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003921 il->cfg->ops->lib->handler_setup(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003922}
3923
3924/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003925 * il4965_rx_handle - Main entry function for receiving responses from uCode
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003926 *
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003927 * Uses the il->handlers callback function array to invoke
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003928 * the appropriate handlers, including command responses,
3929 * frame-received notifications, and other notifications.
3930 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003931void
3932il4965_rx_handle(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003933{
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003934 struct il_rx_buf *rxb;
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003935 struct il_rx_pkt *pkt;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003936 struct il_rx_queue *rxq = &il->rxq;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003937 u32 r, i;
3938 int reclaim;
3939 unsigned long flags;
3940 u8 fill_rx = 0;
3941 u32 count = 8;
3942 int total_empty;
3943
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003944 /* uCode's read idx (stored in shared DRAM) indicates the last Rx
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003945 * buffer that the driver may process (last buffer filled by ucode). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003946 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003947 i = rxq->read;
3948
3949 /* Rx interrupt, but nothing sent from uCode */
3950 if (i == r)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003951 D_RX("r = %d, i = %d\n", r, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003952
3953 /* calculate total frames need to be restock after handling RX */
3954 total_empty = r - rxq->write_actual;
3955 if (total_empty < 0)
3956 total_empty += RX_QUEUE_SIZE;
3957
3958 if (total_empty > (RX_QUEUE_SIZE / 2))
3959 fill_rx = 1;
3960
3961 while (i != r) {
3962 int len;
3963
3964 rxb = rxq->queue[i];
3965
3966 /* If an RXB doesn't have a Rx queue slot associated with it,
3967 * then a bug has been introduced in the queue refilling
3968 * routines -- catch it here */
3969 BUG_ON(rxb == NULL);
3970
3971 rxq->queue[i] = NULL;
3972
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003973 pci_unmap_page(il->pci_dev, rxb->page_dma,
3974 PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003975 PCI_DMA_FROMDEVICE);
3976 pkt = rxb_addr(rxb);
3977
Stanislaw Gruszkae94a4092011-08-31 13:23:20 +02003978 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003979 len += sizeof(u32); /* account for status word */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003980
3981 /* Reclaim a command buffer only if this packet is a response
3982 * to a (driver-originated) command.
3983 * If the packet (e.g. Rx frame) originated from uCode,
3984 * there is no command buffer to reclaim.
3985 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3986 * but apparently a few don't get set; catch them here. */
3987 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003988 (pkt->hdr.cmd != N_RX_PHY) && (pkt->hdr.cmd != N_RX) &&
3989 (pkt->hdr.cmd != N_RX_MPDU) &&
3990 (pkt->hdr.cmd != N_COMPRESSED_BA) &&
3991 (pkt->hdr.cmd != N_STATS) && (pkt->hdr.cmd != C_TX);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003992
3993 /* Based on type of command response or notification,
3994 * handle those that need handling via function in
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003995 * handlers table. See il4965_setup_handlers() */
3996 if (il->handlers[pkt->hdr.cmd]) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003997 D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
3998 il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003999 il->isr_stats.handlers[pkt->hdr.cmd]++;
4000 il->handlers[pkt->hdr.cmd] (il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004001 } else {
4002 /* No handling needed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004003 D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
4004 i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004005 }
4006
4007 /*
4008 * XXX: After here, we should always check rxb->page
4009 * against NULL before touching it or its virtual
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02004010 * memory (pkt). Because some handler might have
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004011 * already taken or freed the pages.
4012 */
4013
4014 if (reclaim) {
4015 /* Invoke any callbacks, transfer the buffer to caller,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004016 * and fire off the (possibly) blocking il_send_cmd()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004017 * as we reclaim the driver command queue */
4018 if (rxb->page)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004019 il_tx_cmd_complete(il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004020 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004021 IL_WARN("Claim null rxb?\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004022 }
4023
4024 /* Reuse the page if possible. For notification packets and
4025 * SKBs that fail to Rx correctly, add them back into the
4026 * rx_free list for reuse later. */
4027 spin_lock_irqsave(&rxq->lock, flags);
4028 if (rxb->page != NULL) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004029 rxb->page_dma =
4030 pci_map_page(il->pci_dev, rxb->page, 0,
4031 PAGE_SIZE << il->hw_params.
4032 rx_page_order, PCI_DMA_FROMDEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004033 list_add_tail(&rxb->list, &rxq->rx_free);
4034 rxq->free_count++;
4035 } else
4036 list_add_tail(&rxb->list, &rxq->rx_used);
4037
4038 spin_unlock_irqrestore(&rxq->lock, flags);
4039
4040 i = (i + 1) & RX_QUEUE_MASK;
4041 /* If there are a lot of unused frames,
4042 * restock the Rx queue so ucode wont assert. */
4043 if (fill_rx) {
4044 count++;
4045 if (count >= 8) {
4046 rxq->read = i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004047 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004048 count = 0;
4049 }
4050 }
4051 }
4052
4053 /* Backtrack one entry */
4054 rxq->read = i;
4055 if (fill_rx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004056 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004057 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004058 il4965_rx_queue_restock(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004059}
4060
4061/* call this function to flush any scheduled tasklet */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004062static inline void
4063il4965_synchronize_irq(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004064{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004065 /* wait to make sure we flush pending tasklet */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004066 synchronize_irq(il->pci_dev->irq);
4067 tasklet_kill(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004068}
4069
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004070static void
4071il4965_irq_tasklet(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004072{
4073 u32 inta, handled = 0;
4074 u32 inta_fh;
4075 unsigned long flags;
4076 u32 i;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004077#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004078 u32 inta_mask;
4079#endif
4080
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004081 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004082
4083 /* Ack/clear/reset pending uCode interrupts.
4084 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4085 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004086 inta = _il_rd(il, CSR_INT);
4087 _il_wr(il, CSR_INT, inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004088
4089 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4090 * Any new interrupts that happen after this, either while we're
4091 * in this tasklet, or later, will show up in next ISR/tasklet. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004092 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
4093 _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004094
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004095#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004096 if (il_get_debug_level(il) & IL_DL_ISR) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004097 /* just for debug */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004098 inta_mask = _il_rd(il, CSR_INT_MASK);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004099 D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
4100 inta_mask, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004101 }
4102#endif
4103
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004104 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004105
4106 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4107 * atomic, make sure that inta covers all the interrupts that
4108 * we've discovered, even if FH interrupt came in just after
4109 * reading CSR_INT. */
4110 if (inta_fh & CSR49_FH_INT_RX_MASK)
4111 inta |= CSR_INT_BIT_FH_RX;
4112 if (inta_fh & CSR49_FH_INT_TX_MASK)
4113 inta |= CSR_INT_BIT_FH_TX;
4114
4115 /* Now service all interrupt bits discovered above. */
4116 if (inta & CSR_INT_BIT_HW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004117 IL_ERR("Hardware error detected. Restarting.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004118
4119 /* Tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004120 il_disable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004121
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004122 il->isr_stats.hw++;
4123 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004124
4125 handled |= CSR_INT_BIT_HW_ERR;
4126
4127 return;
4128 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004129#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004130 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004131 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4132 if (inta & CSR_INT_BIT_SCD) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004133 D_ISR("Scheduler finished to transmit "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004134 "the frame/frames.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004135 il->isr_stats.sch++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004136 }
4137
4138 /* Alive notification via Rx interrupt will do the real work */
4139 if (inta & CSR_INT_BIT_ALIVE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004140 D_ISR("Alive interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004141 il->isr_stats.alive++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004142 }
4143 }
4144#endif
4145 /* Safely ignore these bits for debug checks below */
4146 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
4147
4148 /* HW RF KILL switch toggled */
4149 if (inta & CSR_INT_BIT_RF_KILL) {
4150 int hw_rf_kill = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004151 if (!
4152 (_il_rd(il, CSR_GP_CNTRL) &
4153 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004154 hw_rf_kill = 1;
4155
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004156 IL_WARN("RF_KILL bit toggled to %s.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004157 hw_rf_kill ? "disable radio" : "enable radio");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004158
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004159 il->isr_stats.rfkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004160
4161 /* driver only loads ucode once setting the interface up.
4162 * the driver allows loading the ucode even if the radio
4163 * is killed. Hence update the killswitch state here. The
4164 * rfkill handler will care about restarting if needed.
4165 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004166 if (!test_bit(S_ALIVE, &il->status)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004167 if (hw_rf_kill)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004168 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004169 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004170 clear_bit(S_RF_KILL_HW, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004171 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004172 }
4173
4174 handled |= CSR_INT_BIT_RF_KILL;
4175 }
4176
4177 /* Chip got too hot and stopped itself */
4178 if (inta & CSR_INT_BIT_CT_KILL) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004179 IL_ERR("Microcode CT kill error detected.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004180 il->isr_stats.ctkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004181 handled |= CSR_INT_BIT_CT_KILL;
4182 }
4183
4184 /* Error detected by uCode */
4185 if (inta & CSR_INT_BIT_SW_ERR) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004186 IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
4187 inta);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004188 il->isr_stats.sw++;
4189 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004190 handled |= CSR_INT_BIT_SW_ERR;
4191 }
4192
4193 /*
4194 * uCode wakes up after power-down sleep.
4195 * Tell device about any new tx or host commands enqueued,
4196 * and about any Rx buffers made available while asleep.
4197 */
4198 if (inta & CSR_INT_BIT_WAKEUP) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004199 D_ISR("Wakeup interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004200 il_rx_queue_update_write_ptr(il, &il->rxq);
4201 for (i = 0; i < il->hw_params.max_txq_num; i++)
4202 il_txq_update_write_ptr(il, &il->txq[i]);
4203 il->isr_stats.wakeup++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004204 handled |= CSR_INT_BIT_WAKEUP;
4205 }
4206
4207 /* All uCode command responses, including Tx command responses,
4208 * Rx "responses" (frame-received notification), and other
4209 * notifications from uCode come through here*/
4210 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004211 il4965_rx_handle(il);
4212 il->isr_stats.rx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004213 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4214 }
4215
4216 /* This "Tx" DMA channel is used only for loading uCode */
4217 if (inta & CSR_INT_BIT_FH_TX) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004218 D_ISR("uCode load interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004219 il->isr_stats.tx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004220 handled |= CSR_INT_BIT_FH_TX;
4221 /* Wake up uCode load routine, now that load is complete */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004222 il->ucode_write_complete = 1;
4223 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004224 }
4225
4226 if (inta & ~handled) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004227 IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004228 il->isr_stats.unhandled++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004229 }
4230
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004231 if (inta & ~(il->inta_mask)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004232 IL_WARN("Disabled INTA bits 0x%08x were pending\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004233 inta & ~il->inta_mask);
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004234 IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004235 }
4236
4237 /* Re-enable all interrupts */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02004238 /* only Re-enable if disabled by irq */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004239 if (test_bit(S_INT_ENABLED, &il->status))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004240 il_enable_interrupts(il);
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02004241 /* Re-enable RF_KILL if it occurred */
4242 else if (handled & CSR_INT_BIT_RF_KILL)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004243 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004244
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004245#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004246 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004247 inta = _il_rd(il, CSR_INT);
4248 inta_mask = _il_rd(il, CSR_INT_MASK);
4249 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004250 D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4251 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004252 }
4253#endif
4254}
4255
4256/*****************************************************************************
4257 *
4258 * sysfs attributes
4259 *
4260 *****************************************************************************/
4261
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004262#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004263
4264/*
4265 * The following adds a new attribute to the sysfs representation
4266 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
4267 * used for controlling the debug level.
4268 *
4269 * See the level definitions in iwl for details.
4270 *
4271 * The debug_level being managed using sysfs below is a per device debug
4272 * level that is used instead of the global debug level if it (the per
4273 * device debug level) is set.
4274 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004275static ssize_t
4276il4965_show_debug_level(struct device *d, struct device_attribute *attr,
4277 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004278{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004279 struct il_priv *il = dev_get_drvdata(d);
4280 return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004281}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004282
4283static ssize_t
4284il4965_store_debug_level(struct device *d, struct device_attribute *attr,
4285 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004286{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004287 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004288 unsigned long val;
4289 int ret;
4290
4291 ret = strict_strtoul(buf, 0, &val);
4292 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004293 IL_ERR("%s is not in hex or decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004294 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004295 il->debug_level = val;
4296 if (il_alloc_traffic_mem(il))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004297 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004298 }
4299 return strnlen(buf, count);
4300}
4301
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004302static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il4965_show_debug_level,
4303 il4965_store_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004304
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004305#endif /* CONFIG_IWLEGACY_DEBUG */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004306
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004307static ssize_t
4308il4965_show_temperature(struct device *d, struct device_attribute *attr,
4309 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004310{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004311 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004312
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004313 if (!il_is_alive(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004314 return -EAGAIN;
4315
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004316 return sprintf(buf, "%d\n", il->temperature);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004317}
4318
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004319static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004320
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004321static ssize_t
4322il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004323{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004324 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004325
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004326 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004327 return sprintf(buf, "off\n");
4328 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004329 return sprintf(buf, "%d\n", il->tx_power_user_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004330}
4331
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004332static ssize_t
4333il4965_store_tx_power(struct device *d, struct device_attribute *attr,
4334 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004335{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004336 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004337 unsigned long val;
4338 int ret;
4339
4340 ret = strict_strtoul(buf, 10, &val);
4341 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004342 IL_INFO("%s is not in decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004343 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004344 ret = il_set_tx_power(il, val, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004345 if (ret)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004346 IL_ERR("failed setting tx power (0x%d).\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004347 else
4348 ret = count;
4349 }
4350 return ret;
4351}
4352
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004353static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il4965_show_tx_power,
4354 il4965_store_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004355
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004356static struct attribute *il_sysfs_entries[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004357 &dev_attr_temperature.attr,
4358 &dev_attr_tx_power.attr,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004359#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004360 &dev_attr_debug_level.attr,
4361#endif
4362 NULL
4363};
4364
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004365static struct attribute_group il_attribute_group = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004366 .name = NULL, /* put in device directory */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004367 .attrs = il_sysfs_entries,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004368};
4369
4370/******************************************************************************
4371 *
4372 * uCode download functions
4373 *
4374 ******************************************************************************/
4375
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004376static void
4377il4965_dealloc_ucode_pci(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004378{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004379 il_free_fw_desc(il->pci_dev, &il->ucode_code);
4380 il_free_fw_desc(il->pci_dev, &il->ucode_data);
4381 il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
4382 il_free_fw_desc(il->pci_dev, &il->ucode_init);
4383 il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
4384 il_free_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004385}
4386
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004387static void
4388il4965_nic_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004389{
4390 /* Remove all resets to allow NIC to operate */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004391 _il_wr(il, CSR_RESET, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004392}
4393
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004394static void il4965_ucode_callback(const struct firmware *ucode_raw,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004395 void *context);
4396static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004397
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004398static int __must_check
4399il4965_request_firmware(struct il_priv *il, bool first)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004400{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004401 const char *name_pre = il->cfg->fw_name_pre;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004402 char tag[8];
4403
4404 if (first) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004405 il->fw_idx = il->cfg->ucode_api_max;
4406 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004407 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004408 il->fw_idx--;
4409 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004410 }
4411
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004412 if (il->fw_idx < il->cfg->ucode_api_min) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004413 IL_ERR("no suitable firmware found!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004414 return -ENOENT;
4415 }
4416
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004417 sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004418
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004419 D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004421 return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
4422 &il->pci_dev->dev, GFP_KERNEL, il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004423 il4965_ucode_callback);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004424}
4425
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004426struct il4965_firmware_pieces {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004427 const void *inst, *data, *init, *init_data, *boot;
4428 size_t inst_size, data_size, init_size, init_data_size, boot_size;
4429};
4430
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004431static int
4432il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
4433 struct il4965_firmware_pieces *pieces)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004434{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004435 struct il_ucode_header *ucode = (void *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004436 u32 api_ver, hdr_size;
4437 const u8 *src;
4438
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004439 il->ucode_ver = le32_to_cpu(ucode->ver);
4440 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004441
4442 switch (api_ver) {
4443 default:
4444 case 0:
4445 case 1:
4446 case 2:
4447 hdr_size = 24;
4448 if (ucode_raw->size < hdr_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004449 IL_ERR("File size too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004450 return -EINVAL;
4451 }
4452 pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
4453 pieces->data_size = le32_to_cpu(ucode->v1.data_size);
4454 pieces->init_size = le32_to_cpu(ucode->v1.init_size);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004455 pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004456 pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
4457 src = ucode->v1.data;
4458 break;
4459 }
4460
4461 /* Verify size of file vs. image size info in file's header */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004462 if (ucode_raw->size !=
4463 hdr_size + pieces->inst_size + pieces->data_size +
4464 pieces->init_size + pieces->init_data_size + pieces->boot_size) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004465
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004466 IL_ERR("uCode file size %d does not match expected size\n",
4467 (int)ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004468 return -EINVAL;
4469 }
4470
4471 pieces->inst = src;
4472 src += pieces->inst_size;
4473 pieces->data = src;
4474 src += pieces->data_size;
4475 pieces->init = src;
4476 src += pieces->init_size;
4477 pieces->init_data = src;
4478 src += pieces->init_data_size;
4479 pieces->boot = src;
4480 src += pieces->boot_size;
4481
4482 return 0;
4483}
4484
4485/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004486 * il4965_ucode_callback - callback when firmware was loaded
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004487 *
4488 * If loaded successfully, copies the firmware into buffers
4489 * for the card to fetch (via DMA).
4490 */
4491static void
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004492il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004493{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004494 struct il_priv *il = context;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004495 struct il_ucode_header *ucode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004496 int err;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004497 struct il4965_firmware_pieces pieces;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004498 const unsigned int api_max = il->cfg->ucode_api_max;
4499 const unsigned int api_min = il->cfg->ucode_api_min;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004500 u32 api_ver;
4501
4502 u32 max_probe_length = 200;
4503 u32 standard_phy_calibration_size =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004504 IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004505
4506 memset(&pieces, 0, sizeof(pieces));
4507
4508 if (!ucode_raw) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004509 if (il->fw_idx <= il->cfg->ucode_api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004510 IL_ERR("request for firmware file '%s' failed.\n",
4511 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004512 goto try_again;
4513 }
4514
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004515 D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
4516 ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004517
4518 /* Make sure that we got at least the API version number */
4519 if (ucode_raw->size < 4) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004520 IL_ERR("File size way too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004521 goto try_again;
4522 }
4523
4524 /* Data from ucode file: header followed by uCode images */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004525 ucode = (struct il_ucode_header *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004526
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004527 err = il4965_load_firmware(il, ucode_raw, &pieces);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004528
4529 if (err)
4530 goto try_again;
4531
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004532 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004533
4534 /*
4535 * api_ver should match the api version forming part of the
4536 * firmware filename ... but we don't check for that and only rely
4537 * on the API version read from firmware header from here on forward
4538 */
4539 if (api_ver < api_min || api_ver > api_max) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004540 IL_ERR("Driver unable to support your firmware API. "
4541 "Driver supports v%u, firmware is v%u.\n", api_max,
4542 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004543 goto try_again;
4544 }
4545
4546 if (api_ver != api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004547 IL_ERR("Firmware has old API version. Expected v%u, "
4548 "got v%u. New firmware can be obtained "
4549 "from http://www.intellinuxwireless.org.\n", api_max,
4550 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004551
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004552 IL_INFO("loaded firmware version %u.%u.%u.%u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004553 IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
4554 IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004555
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004556 snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
4557 "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
4558 IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004559 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004560
4561 /*
4562 * For any of the failures below (before allocating pci memory)
4563 * we will try to load a version with a smaller API -- maybe the
4564 * user just got a corrupted version of the latest API.
4565 */
4566
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004567 D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
4568 D_INFO("f/w package hdr runtime inst size = %Zd\n", pieces.inst_size);
4569 D_INFO("f/w package hdr runtime data size = %Zd\n", pieces.data_size);
4570 D_INFO("f/w package hdr init inst size = %Zd\n", pieces.init_size);
4571 D_INFO("f/w package hdr init data size = %Zd\n", pieces.init_data_size);
4572 D_INFO("f/w package hdr boot inst size = %Zd\n", pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004573
4574 /* Verify that uCode images will fit in card's SRAM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004575 if (pieces.inst_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004576 IL_ERR("uCode instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004577 pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004578 goto try_again;
4579 }
4580
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004581 if (pieces.data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004582 IL_ERR("uCode data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004583 pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004584 goto try_again;
4585 }
4586
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004587 if (pieces.init_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004588 IL_ERR("uCode init instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004589 pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004590 goto try_again;
4591 }
4592
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004593 if (pieces.init_data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004594 IL_ERR("uCode init data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004595 pieces.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004596 goto try_again;
4597 }
4598
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004599 if (pieces.boot_size > il->hw_params.max_bsm_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004600 IL_ERR("uCode boot instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004601 pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004602 goto try_again;
4603 }
4604
4605 /* Allocate ucode buffers for card's bus-master loading ... */
4606
4607 /* Runtime instructions and 2 copies of data:
4608 * 1) unmodified from disk
4609 * 2) backup cache for save/restore during power-downs */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004610 il->ucode_code.len = pieces.inst_size;
4611 il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004612
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004613 il->ucode_data.len = pieces.data_size;
4614 il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004615
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004616 il->ucode_data_backup.len = pieces.data_size;
4617 il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004618
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004619 if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
4620 !il->ucode_data_backup.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004621 goto err_pci_alloc;
4622
4623 /* Initialization instructions and data */
4624 if (pieces.init_size && pieces.init_data_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004625 il->ucode_init.len = pieces.init_size;
4626 il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004627
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004628 il->ucode_init_data.len = pieces.init_data_size;
4629 il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004630
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004631 if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004632 goto err_pci_alloc;
4633 }
4634
4635 /* Bootstrap (instructions only, no data) */
4636 if (pieces.boot_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004637 il->ucode_boot.len = pieces.boot_size;
4638 il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004639
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004640 if (!il->ucode_boot.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004641 goto err_pci_alloc;
4642 }
4643
4644 /* Now that we can no longer fail, copy information */
4645
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004646 il->sta_key_max_num = STA_KEY_MAX_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004647
4648 /* Copy images into buffers for card's bus-master reads ... */
4649
4650 /* Runtime instructions (first block of data in file) */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004651 D_INFO("Copying (but not loading) uCode instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004652 pieces.inst_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004653 memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004654
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004655 D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004656 il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004657
4658 /*
4659 * Runtime data
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004660 * NOTE: Copy into backup buffer will be done in il_up()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004661 */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004662 D_INFO("Copying (but not loading) uCode data len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004663 pieces.data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004664 memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
4665 memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004666
4667 /* Initialization instructions */
4668 if (pieces.init_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004669 D_INFO("Copying (but not loading) init instr len %Zd\n",
4670 pieces.init_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004671 memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004672 }
4673
4674 /* Initialization data */
4675 if (pieces.init_data_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004676 D_INFO("Copying (but not loading) init data len %Zd\n",
4677 pieces.init_data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004678 memcpy(il->ucode_init_data.v_addr, pieces.init_data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004679 pieces.init_data_size);
4680 }
4681
4682 /* Bootstrap instructions */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004683 D_INFO("Copying (but not loading) boot instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004684 pieces.boot_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004685 memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004686
4687 /*
4688 * figure out the offset of chain noise reset and gain commands
4689 * base on the size of standard phy calibration commands table size
4690 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004691 il->_4965.phy_calib_chain_noise_reset_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004692 standard_phy_calibration_size;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004693 il->_4965.phy_calib_chain_noise_gain_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004694 standard_phy_calibration_size + 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004695
4696 /**************************************************
4697 * This is still part of probe() in a sense...
4698 *
4699 * 9. Setup and register with mac80211 and debugfs
4700 **************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004701 err = il4965_mac_setup_register(il, max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004702 if (err)
4703 goto out_unbind;
4704
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004705 err = il_dbgfs_register(il, DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004706 if (err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004707 IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
4708 err);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004709
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004710 err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004711 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004712 IL_ERR("failed to create sysfs device attributes\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004713 goto out_unbind;
4714 }
4715
4716 /* We have our copies now, allow OS release its copies */
4717 release_firmware(ucode_raw);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004718 complete(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004719 return;
4720
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004721try_again:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004722 /* try next, if any */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004723 if (il4965_request_firmware(il, false))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004724 goto out_unbind;
4725 release_firmware(ucode_raw);
4726 return;
4727
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004728err_pci_alloc:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004729 IL_ERR("failed to allocate pci memory\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004730 il4965_dealloc_ucode_pci(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004731out_unbind:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004732 complete(&il->_4965.firmware_loading_complete);
4733 device_release_driver(&il->pci_dev->dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004734 release_firmware(ucode_raw);
4735}
4736
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004737static const char *const desc_lookup_text[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004738 "OK",
4739 "FAIL",
4740 "BAD_PARAM",
4741 "BAD_CHECKSUM",
4742 "NMI_INTERRUPT_WDG",
4743 "SYSASSERT",
4744 "FATAL_ERROR",
4745 "BAD_COMMAND",
4746 "HW_ERROR_TUNE_LOCK",
4747 "HW_ERROR_TEMPERATURE",
4748 "ILLEGAL_CHAN_FREQ",
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +02004749 "VCC_NOT_STBL",
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004750 "FH49_ERROR",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004751 "NMI_INTERRUPT_HOST",
4752 "NMI_INTERRUPT_ACTION_PT",
4753 "NMI_INTERRUPT_UNKNOWN",
4754 "UCODE_VERSION_MISMATCH",
4755 "HW_ERROR_ABS_LOCK",
4756 "HW_ERROR_CAL_LOCK_FAIL",
4757 "NMI_INTERRUPT_INST_ACTION_PT",
4758 "NMI_INTERRUPT_DATA_ACTION_PT",
4759 "NMI_TRM_HW_ER",
4760 "NMI_INTERRUPT_TRM",
Joe Perches861d9c32011-07-08 23:20:24 -07004761 "NMI_INTERRUPT_BREAK_POINT",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004762 "DEBUG_0",
4763 "DEBUG_1",
4764 "DEBUG_2",
4765 "DEBUG_3",
4766};
4767
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004768static struct {
4769 char *name;
4770 u8 num;
4771} advanced_lookup[] = {
4772 {
4773 "NMI_INTERRUPT_WDG", 0x34}, {
4774 "SYSASSERT", 0x35}, {
4775 "UCODE_VERSION_MISMATCH", 0x37}, {
4776 "BAD_COMMAND", 0x38}, {
4777 "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
4778 "FATAL_ERROR", 0x3D}, {
4779 "NMI_TRM_HW_ERR", 0x46}, {
4780 "NMI_INTERRUPT_TRM", 0x4C}, {
4781 "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
4782 "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
4783 "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
4784 "NMI_INTERRUPT_HOST", 0x66}, {
4785 "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
4786 "NMI_INTERRUPT_UNKNOWN", 0x84}, {
4787 "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
4788"ADVANCED_SYSASSERT", 0},};
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004789
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004790static const char *
4791il4965_desc_lookup(u32 num)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004792{
4793 int i;
4794 int max = ARRAY_SIZE(desc_lookup_text);
4795
4796 if (num < max)
4797 return desc_lookup_text[num];
4798
4799 max = ARRAY_SIZE(advanced_lookup) - 1;
4800 for (i = 0; i < max; i++) {
4801 if (advanced_lookup[i].num == num)
4802 break;
4803 }
4804 return advanced_lookup[i].name;
4805}
4806
4807#define ERROR_START_OFFSET (1 * sizeof(u32))
4808#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4809
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004810void
4811il4965_dump_nic_error_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004812{
4813 u32 data2, line;
4814 u32 desc, time, count, base, data1;
4815 u32 blink1, blink2, ilink1, ilink2;
4816 u32 pc, hcmd;
4817
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004818 if (il->ucode_type == UCODE_INIT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004819 base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004820 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004821 base = le32_to_cpu(il->card_alive.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004822
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004823 if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004824 IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
4825 base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004826 return;
4827 }
4828
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004829 count = il_read_targ_mem(il, base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004830
4831 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004832 IL_ERR("Start IWL Error Log Dump:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004833 IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004834 }
4835
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004836 desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
4837 il->isr_stats.err_code = desc;
4838 pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
4839 blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
4840 blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
4841 ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
4842 ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
4843 data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
4844 data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
4845 line = il_read_targ_mem(il, base + 9 * sizeof(u32));
4846 time = il_read_targ_mem(il, base + 11 * sizeof(u32));
4847 hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004848
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004849 IL_ERR("Desc Time "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004850 "data1 data2 line\n");
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004851 IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004852 il4965_desc_lookup(desc), desc, time, data1, data2, line);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004853 IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004854 IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
4855 blink2, ilink1, ilink2, hcmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004856}
4857
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004858static void
4859il4965_rf_kill_ct_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004860{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004861 struct il_ct_kill_config cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004862 unsigned long flags;
4863 int ret = 0;
4864
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004865 spin_lock_irqsave(&il->lock, flags);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004866 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004867 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004868 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004869
4870 cmd.critical_temperature_R =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004871 cpu_to_le32(il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004872
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004873 ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004874 if (ret)
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004875 IL_ERR("C_CT_KILL_CONFIG failed\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004876 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004877 D_INFO("C_CT_KILL_CONFIG " "succeeded, "
4878 "critical temperature is %d\n",
4879 il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004880}
4881
4882static const s8 default_queue_to_tx_fifo[] = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004883 IL_TX_FIFO_VO,
4884 IL_TX_FIFO_VI,
4885 IL_TX_FIFO_BE,
4886 IL_TX_FIFO_BK,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004887 IL49_CMD_FIFO_NUM,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004888 IL_TX_FIFO_UNUSED,
4889 IL_TX_FIFO_UNUSED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004890};
4891
Stanislaw Gruszkae53aac42011-08-31 11:18:16 +02004892#define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
4893
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004894static int
4895il4965_alive_notify(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004896{
4897 u32 a;
4898 unsigned long flags;
4899 int i, chan;
4900 u32 reg_val;
4901
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004902 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004903
4904 /* Clear 4965's internal Tx Scheduler data base */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004905 il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004906 a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
4907 for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004908 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004909 for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004910 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004911 for (;
4912 a <
4913 il->scd_base_addr +
4914 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
4915 a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004916 il_write_targ_mem(il, a, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004917
4918 /* Tel 4965 where to find Tx byte count tables */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004919 il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004920
4921 /* Enable DMA channel */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004922 for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
4923 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
4924 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
4925 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004926
4927 /* Update FH chicken bits */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004928 reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
4929 il_wr(il, FH49_TX_CHICKEN_BITS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004930 reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004931
4932 /* Disable chain mode for all queues */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004933 il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004934
4935 /* Initialize each Tx queue (including the command queue) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004936 for (i = 0; i < il->hw_params.max_txq_num; i++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004937
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004938 /* TFD circular buffer read/write idxes */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004939 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004940 il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004941
4942 /* Max Tx Window size for Scheduler-ACK mode */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004943 il_write_targ_mem(il,
4944 il->scd_base_addr +
4945 IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
4946 (SCD_WIN_SIZE <<
4947 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
4948 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004949
4950 /* Frame limit */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004951 il_write_targ_mem(il,
4952 il->scd_base_addr +
4953 IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
4954 sizeof(u32),
4955 (SCD_FRAME_LIMIT <<
4956 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
4957 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004958
4959 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004960 il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004961 (1 << il->hw_params.max_txq_num) - 1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004962
4963 /* Activate all Tx DMA/FIFO channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004964 il4965_txq_set_sched(il, IL_MASK(0, 6));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004965
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004966 il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004967
4968 /* make sure all queue are not stopped */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004969 memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004970 for (i = 0; i < 4; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004971 atomic_set(&il->queue_stop_count[i], 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004972
4973 /* reset to 0 to enable all the queue first */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004974 il->txq_ctx_active_msk = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004975 /* Map each Tx/cmd queue to its corresponding fifo */
4976 BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
4977
4978 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
4979 int ac = default_queue_to_tx_fifo[i];
4980
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004981 il_txq_ctx_activate(il, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004982
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004983 if (ac == IL_TX_FIFO_UNUSED)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004984 continue;
4985
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004986 il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004987 }
4988
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004989 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004990
4991 return 0;
4992}
4993
4994/**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004995 * il4965_alive_start - called after N_ALIVE notification received
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004996 * from protocol/runtime uCode (initialization uCode's
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004997 * Alive gets handled by il_init_alive_start()).
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004998 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004999static void
5000il4965_alive_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005001{
5002 int ret = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005003 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005004
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005005 D_INFO("Runtime Alive received.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005006
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005007 if (il->card_alive.is_valid != UCODE_VALID_OK) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005008 /* We had an error bringing up the hardware, so take it
5009 * all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005010 D_INFO("Alive failed.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005011 goto restart;
5012 }
5013
5014 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5015 * This is a paranoid check, because we would not have gotten the
5016 * "runtime" alive if code weren't properly loaded. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005017 if (il4965_verify_ucode(il)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005018 /* Runtime instruction load was bad;
5019 * take it all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005020 D_INFO("Bad runtime uCode load.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005021 goto restart;
5022 }
5023
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005024 ret = il4965_alive_notify(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005025 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005026 IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005027 goto restart;
5028 }
5029
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005030 /* After the ALIVE response, we can send host commands to the uCode */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005031 set_bit(S_ALIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005032
5033 /* Enable watchdog to monitor the driver tx queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005034 il_setup_watchdog(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005035
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005036 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005037 return;
5038
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005039 ieee80211_wake_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005040
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005041 il->active_rate = RATES_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005042
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005043 if (il_is_associated_ctx(ctx)) {
5044 struct il_rxon_cmd *active_rxon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005045 (struct il_rxon_cmd *)&ctx->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005046 /* apply any changes in staging */
5047 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
5048 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5049 } else {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005050 /* Initialize our rx_config data */
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005051 il_connection_init_rx_config(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005052
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005053 if (il->cfg->ops->hcmd->set_rxon_chain)
5054 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005055 }
5056
5057 /* Configure bluetooth coexistence if enabled */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005058 il_send_bt_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005059
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005060 il4965_reset_run_time_calib(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005061
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005062 set_bit(S_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005063
5064 /* Configure the adapter for unassociated operation */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005065 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005066
5067 /* At this point, the NIC is initialized and operational */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005068 il4965_rf_kill_ct_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005069
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005070 D_INFO("ALIVE processing complete.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005071 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005072
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005073 il_power_update_mode(il, true);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005074 D_INFO("Updated power mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005075
5076 return;
5077
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005078restart:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005079 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005080}
5081
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005082static void il4965_cancel_deferred_work(struct il_priv *il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005083
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005084static void
5085__il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005086{
5087 unsigned long flags;
Stanislaw Gruszkaab42b402011-04-28 11:51:24 +02005088 int exit_pending;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005089
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005090 D_INFO(DRV_NAME " is going down\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005091
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005092 il_scan_cancel_timeout(il, 200);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005093
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005094 exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005095
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005096 /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005097 * to prevent rearm timer */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005098 del_timer_sync(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005099
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005100 il_clear_ucode_stations(il, NULL);
5101 il_dealloc_bcast_stations(il);
5102 il_clear_driver_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005103
5104 /* Unblock any waiting calls */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005105 wake_up_all(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005106
5107 /* Wipe out the EXIT_PENDING status bit if we are not actually
5108 * exiting the module */
5109 if (!exit_pending)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005110 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005111
5112 /* stop and reset the on-board processor */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005113 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005114
5115 /* tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005116 spin_lock_irqsave(&il->lock, flags);
5117 il_disable_interrupts(il);
5118 spin_unlock_irqrestore(&il->lock, flags);
5119 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005120
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005121 if (il->mac80211_registered)
5122 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005123
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005124 /* If we have not previously called il_init() then
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005125 * clear all bits but the RF Kill bit and return */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005126 if (!il_is_init(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005127 il->status =
5128 test_bit(S_RF_KILL_HW,
5129 &il->
5130 status) << S_RF_KILL_HW |
5131 test_bit(S_GEO_CONFIGURED,
5132 &il->
5133 status) << S_GEO_CONFIGURED |
5134 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005135 goto exit;
5136 }
5137
5138 /* ...otherwise clear out all the status bits but the RF Kill
5139 * bit and continue taking the NIC down. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005140 il->status &=
5141 test_bit(S_RF_KILL_HW,
5142 &il->status) << S_RF_KILL_HW | test_bit(S_GEO_CONFIGURED,
5143 &il->
5144 status) <<
5145 S_GEO_CONFIGURED | test_bit(S_FW_ERROR,
5146 &il->
5147 status) << S_FW_ERROR |
5148 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005149
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005150 il4965_txq_ctx_stop(il);
5151 il4965_rxq_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005152
5153 /* Power-down device's busmaster DMA clocks */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02005154 il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005155 udelay(5);
5156
5157 /* Make sure (redundant) we've released our request to stay awake */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005158 il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005159
5160 /* Stop the device, and put it in low power state */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005161 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005162
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005163exit:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005164 memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005165
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005166 dev_kfree_skb(il->beacon_skb);
5167 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005168
5169 /* clear out any free frames */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005170 il4965_clear_free_frames(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005171}
5172
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005173static void
5174il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005175{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005176 mutex_lock(&il->mutex);
5177 __il4965_down(il);
5178 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005179
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005180 il4965_cancel_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005181}
5182
5183#define HW_READY_TIMEOUT (50)
5184
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005185static int
5186il4965_set_hw_ready(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005187{
5188 int ret = 0;
5189
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005190 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005191 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005192
5193 /* See if we got it */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005194 ret =
5195 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5196 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5197 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, HW_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005198 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005199 il->hw_ready = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005200 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005201 il->hw_ready = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005202
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005203 D_INFO("hardware %s\n", (il->hw_ready == 1) ? "ready" : "not ready");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005204 return ret;
5205}
5206
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005207static int
5208il4965_prepare_card_hw(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005209{
5210 int ret = 0;
5211
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005212 D_INFO("il4965_prepare_card_hw enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005213
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005214 ret = il4965_set_hw_ready(il);
5215 if (il->hw_ready)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005216 return ret;
5217
5218 /* If HW is not ready, prepare the conditions to check again */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005219 il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005220
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005221 ret =
5222 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5223 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
5224 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005225
5226 /* HW should be ready by now, check again. */
5227 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005228 il4965_set_hw_ready(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005229
5230 return ret;
5231}
5232
5233#define MAX_HW_RESTARTS 5
5234
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005235static int
5236__il4965_up(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005237{
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005238 int i;
5239 int ret;
5240
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005241 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005242 IL_WARN("Exit pending; will not bring the NIC up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005243 return -EIO;
5244 }
5245
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005246 if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005247 IL_ERR("ucode not available for device bringup\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005248 return -EIO;
5249 }
5250
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005251 ret = il4965_alloc_bcast_station(il, &il->ctx);
5252 if (ret) {
5253 il_dealloc_bcast_stations(il);
5254 return ret;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005255 }
5256
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005257 il4965_prepare_card_hw(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005258
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005259 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005260 IL_WARN("Exit HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005261 return -EIO;
5262 }
5263
5264 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005265 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005266 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005267 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005268 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005269
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005270 if (il_is_rfkill(il)) {
5271 wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005272
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005273 il_enable_interrupts(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005274 IL_WARN("Radio disabled by HW RF Kill switch\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005275 return 0;
5276 }
5277
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005278 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005279
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005280 /* must be initialised before il_hw_nic_init */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005281 il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005282
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005283 ret = il4965_hw_nic_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005284 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005285 IL_ERR("Unable to init nic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005286 return ret;
5287 }
5288
5289 /* make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005290 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005291 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005292
5293 /* clear (again), then enable host interrupts */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005294 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005295 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005296
5297 /* really make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005298 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5299 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005300
5301 /* Copy original ucode data image from disk into backup cache.
5302 * This will be used to initialize the on-board processor's
5303 * data SRAM for a clean start when the runtime program first loads. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005304 memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
5305 il->ucode_data.len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005306
5307 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5308
5309 /* load bootstrap state machine,
5310 * load bootstrap program into processor's memory,
5311 * prepare to load the "initialize" uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005312 ret = il->cfg->ops->lib->load_ucode(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005313
5314 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005315 IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005316 continue;
5317 }
5318
5319 /* start card; "initialize" will load runtime ucode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005320 il4965_nic_start(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005321
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005322 D_INFO(DRV_NAME " is coming up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005323
5324 return 0;
5325 }
5326
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005327 set_bit(S_EXIT_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005328 __il4965_down(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005329 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005330
5331 /* tried to restart and config the device for as long as our
5332 * patience could withstand */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005333 IL_ERR("Unable to initialize device after %d attempts.\n", i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005334 return -EIO;
5335}
5336
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005337/*****************************************************************************
5338 *
5339 * Workqueue callbacks
5340 *
5341 *****************************************************************************/
5342
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005343static void
5344il4965_bg_init_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005345{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005346 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005347 container_of(data, struct il_priv, init_alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005348
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005349 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005350 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005351 goto out;
5352
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005353 il->cfg->ops->lib->init_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005354out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005355 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005356}
5357
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005358static void
5359il4965_bg_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005360{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005361 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005362 container_of(data, struct il_priv, alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005363
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005364 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005365 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005366 goto out;
5367
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005368 il4965_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005369out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005370 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005371}
5372
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005373static void
5374il4965_bg_run_time_calib_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005375{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005376 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005377 run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005378
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005379 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005380
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005381 if (test_bit(S_EXIT_PENDING, &il->status) ||
5382 test_bit(S_SCANNING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005383 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005384 return;
5385 }
5386
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005387 if (il->start_calib) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005388 il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
5389 il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005390 }
5391
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005392 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005393}
5394
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005395static void
5396il4965_bg_restart(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005397{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005398 struct il_priv *il = container_of(data, struct il_priv, restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005399
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005400 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005401 return;
5402
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005403 if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005404 mutex_lock(&il->mutex);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005405 il->ctx.vif = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005406 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005407
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005408 __il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005409
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005410 mutex_unlock(&il->mutex);
5411 il4965_cancel_deferred_work(il);
5412 ieee80211_restart_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005413 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005414 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005415
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005416 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005417 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005418 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005419 return;
5420 }
5421
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005422 __il4965_up(il);
5423 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005424 }
5425}
5426
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005427static void
5428il4965_bg_rx_replenish(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005429{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005430 struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005431
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005432 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005433 return;
5434
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005435 mutex_lock(&il->mutex);
5436 il4965_rx_replenish(il);
5437 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005438}
5439
5440/*****************************************************************************
5441 *
5442 * mac80211 entry point functions
5443 *
5444 *****************************************************************************/
5445
5446#define UCODE_READY_TIMEOUT (4 * HZ)
5447
5448/*
5449 * Not a mac80211 entry point function, but it fits in with all the
5450 * other mac80211 functions grouped here.
5451 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005452static int
5453il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005454{
5455 int ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005456 struct ieee80211_hw *hw = il->hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005457
5458 hw->rate_control_algorithm = "iwl-4965-rs";
5459
5460 /* Tell mac80211 our characteristics */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005461 hw->flags =
5462 IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_AMPDU_AGGREGATION |
5463 IEEE80211_HW_NEED_DTIM_PERIOD | IEEE80211_HW_SPECTRUM_MGMT |
5464 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005465
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005466 if (il->cfg->sku & IL_SKU_N)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005467 hw->flags |=
5468 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
5469 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005470
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005471 hw->sta_data_size = sizeof(struct il_station_priv);
5472 hw->vif_data_size = sizeof(struct il_vif_priv);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005473
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005474 hw->wiphy->interface_modes |= il->ctx.interface_modes;
5475 hw->wiphy->interface_modes |= il->ctx.exclusive_interface_modes;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005476
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005477 hw->wiphy->flags |=
5478 WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005479
5480 /*
5481 * For now, disable PS by default because it affects
5482 * RX performance significantly.
5483 */
5484 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5485
5486 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
5487 /* we create the 802.11 header and a zero-length SSID element */
5488 hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
5489
5490 /* Default value; 4 EDCA QOS priorities */
5491 hw->queues = 4;
5492
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005493 hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005494
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005495 if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
5496 il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005497 &il->bands[IEEE80211_BAND_2GHZ];
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005498 if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
5499 il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005500 &il->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005501
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005502 il_leds_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005503
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005504 ret = ieee80211_register_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005505 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005506 IL_ERR("Failed to register hw (error %d)\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005507 return ret;
5508 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005509 il->mac80211_registered = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005510
5511 return 0;
5512}
5513
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005514int
5515il4965_mac_start(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005516{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005517 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005518 int ret;
5519
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005520 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005521
5522 /* we should be verifying the device is ready to be opened */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005523 mutex_lock(&il->mutex);
5524 ret = __il4965_up(il);
5525 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005526
5527 if (ret)
5528 return ret;
5529
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005530 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005531 goto out;
5532
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005533 D_INFO("Start UP work done.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005534
5535 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5536 * mac80211 will not be run successfully. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005537 ret = wait_event_timeout(il->wait_command_queue,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005538 test_bit(S_READY, &il->status),
5539 UCODE_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005540 if (!ret) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005541 if (!test_bit(S_READY, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005542 IL_ERR("START_ALIVE timeout after %dms.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005543 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5544 return -ETIMEDOUT;
5545 }
5546 }
5547
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005548 il4965_led_enable(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005549
5550out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005551 il->is_open = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005552 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005553 return 0;
5554}
5555
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005556void
5557il4965_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005558{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005559 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005560
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005561 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005562
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005563 if (!il->is_open)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005564 return;
5565
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005566 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005567
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005568 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005569
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005570 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005571
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02005572 /* User space software may expect getting rfkill changes
5573 * even if interface is down */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005574 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005575 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005576
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005577 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005578}
5579
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005580void
5581il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005582{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005583 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005584
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005585 D_MACDUMP("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005586
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005587 D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005588 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005589
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005590 if (il4965_tx_skb(il, skb))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005591 dev_kfree_skb_any(skb);
5592
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005593 D_MACDUMP("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005594}
5595
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005596void
5597il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5598 struct ieee80211_key_conf *keyconf,
5599 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005600{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005601 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005602 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005603
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005604 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005605
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005606 il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta, iv32,
5607 phase1key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005608
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005609 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005610}
5611
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005612int
5613il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5614 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5615 struct ieee80211_key_conf *key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005616{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005617 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005618 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
5619 struct il_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005620 int ret;
5621 u8 sta_id;
5622 bool is_default_wep_key = false;
5623
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005624 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005625
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005626 if (il->cfg->mod_params->sw_crypto) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005627 D_MAC80211("leave - hwcrypto disabled\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005628 return -EOPNOTSUPP;
5629 }
5630
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005631 sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005632 if (sta_id == IL_INVALID_STATION)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005633 return -EINVAL;
5634
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005635 mutex_lock(&il->mutex);
5636 il_scan_cancel_timeout(il, 100);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005637
5638 /*
5639 * If we are getting WEP group key and we didn't receive any key mapping
5640 * so far, we are in legacy wep mode (group key only), otherwise we are
5641 * in 1X mode.
5642 * In legacy wep mode, we use another host command to the uCode.
5643 */
5644 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005645 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005646 if (cmd == SET_KEY)
5647 is_default_wep_key = !ctx->key_mapping_keys;
5648 else
5649 is_default_wep_key =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005650 (key->hw_key_idx == HW_KEY_DEFAULT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005651 }
5652
5653 switch (cmd) {
5654 case SET_KEY:
5655 if (is_default_wep_key)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005656 ret =
5657 il4965_set_default_wep_key(il, vif_priv->ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005658 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005659 ret =
5660 il4965_set_dynamic_key(il, vif_priv->ctx, key,
5661 sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005662
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005663 D_MAC80211("enable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005664 break;
5665 case DISABLE_KEY:
5666 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005667 ret = il4965_remove_default_wep_key(il, ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005668 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005669 ret = il4965_remove_dynamic_key(il, ctx, key, sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005670
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005671 D_MAC80211("disable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005672 break;
5673 default:
5674 ret = -EINVAL;
5675 }
5676
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005677 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005678 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005679
5680 return ret;
5681}
5682
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005683int
5684il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5685 enum ieee80211_ampdu_mlme_action action,
5686 struct ieee80211_sta *sta, u16 tid, u16 * ssn,
5687 u8 buf_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005688{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005689 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005690 int ret = -EINVAL;
5691
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005692 D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005693
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005694 if (!(il->cfg->sku & IL_SKU_N))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005695 return -EACCES;
5696
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005697 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005698
5699 switch (action) {
5700 case IEEE80211_AMPDU_RX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005701 D_HT("start Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005702 ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005703 break;
5704 case IEEE80211_AMPDU_RX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005705 D_HT("stop Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005706 ret = il4965_sta_rx_agg_stop(il, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005707 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005708 ret = 0;
5709 break;
5710 case IEEE80211_AMPDU_TX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005711 D_HT("start Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005712 ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005713 break;
5714 case IEEE80211_AMPDU_TX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005715 D_HT("stop Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005716 ret = il4965_tx_agg_stop(il, vif, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005717 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005718 ret = 0;
5719 break;
5720 case IEEE80211_AMPDU_TX_OPERATIONAL:
5721 ret = 0;
5722 break;
5723 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005724 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005725
5726 return ret;
5727}
5728
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005729int
5730il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5731 struct ieee80211_sta *sta)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005732{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005733 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005734 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
5735 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005736 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
5737 int ret;
5738 u8 sta_id;
5739
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005740 D_INFO("received request to add station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005741 mutex_lock(&il->mutex);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005742 D_INFO("proceeding to add station %pM\n", sta->addr);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005743 sta_priv->common.sta_id = IL_INVALID_STATION;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005744
5745 atomic_set(&sta_priv->pending_frames, 0);
5746
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005747 ret =
5748 il_add_station_common(il, vif_priv->ctx, sta->addr, is_ap, sta,
5749 &sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005750 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005751 IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005752 /* Should we return success if return code is EEXIST ? */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005753 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005754 return ret;
5755 }
5756
5757 sta_priv->common.sta_id = sta_id;
5758
5759 /* Initialize rate scaling */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005760 D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005761 il4965_rs_rate_init(il, sta, sta_id);
5762 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005763
5764 return 0;
5765}
5766
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005767void
5768il4965_mac_channel_switch(struct ieee80211_hw *hw,
5769 struct ieee80211_channel_switch *ch_switch)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005770{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005771 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005772 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005773 struct ieee80211_conf *conf = &hw->conf;
5774 struct ieee80211_channel *channel = ch_switch->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005775 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005776
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005777 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005778 u16 ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005779
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005780 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005781
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005782 mutex_lock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005783
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005784 if (il_is_rfkill(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005785 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005786
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005787 if (test_bit(S_EXIT_PENDING, &il->status) ||
5788 test_bit(S_SCANNING, &il->status) ||
5789 test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005790 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005791
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005792 if (!il_is_associated_ctx(ctx))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005793 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005794
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005795 if (!il->cfg->ops->lib->set_channel_switch)
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 ch = channel->hw_value;
5799 if (le16_to_cpu(ctx->active.channel) == ch)
5800 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005801
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005802 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005803 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005804 D_MAC80211("invalid channel\n");
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005805 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005806 }
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005807
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005808 spin_lock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005809
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005810 il->current_ht_config.smps = conf->smps_mode;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005811
5812 /* Configure HT40 channels */
5813 ctx->ht.enabled = conf_is_ht(conf);
5814 if (ctx->ht.enabled) {
5815 if (conf_is_ht40_minus(conf)) {
5816 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005817 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005818 ctx->ht.is_40mhz = true;
5819 } else if (conf_is_ht40_plus(conf)) {
5820 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005821 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005822 ctx->ht.is_40mhz = true;
5823 } else {
5824 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005825 IEEE80211_HT_PARAM_CHA_SEC_NONE;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005826 ctx->ht.is_40mhz = false;
5827 }
5828 } else
5829 ctx->ht.is_40mhz = false;
5830
5831 if ((le16_to_cpu(ctx->staging.channel) != ch))
5832 ctx->staging.flags = 0;
5833
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005834 il_set_rxon_channel(il, channel, ctx);
5835 il_set_rxon_ht(il, ht_conf);
5836 il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005837
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005838 spin_unlock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005839
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005840 il_set_rate(il);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005841 /*
5842 * at this point, staging_rxon has the
5843 * configuration for channel switch
5844 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005845 set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005846 il->switch_channel = cpu_to_le16(ch);
5847 if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005848 clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005849 il->switch_channel = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005850 ieee80211_chswitch_done(ctx->vif, false);
5851 }
5852
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005853out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005854 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005855 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005856}
5857
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005858void
5859il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
5860 unsigned int *total_flags, u64 multicast)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005861{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005862 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005863 __le32 filter_or = 0, filter_nand = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005864
5865#define CHK(test, flag) do { \
5866 if (*total_flags & (test)) \
5867 filter_or |= (flag); \
5868 else \
5869 filter_nand |= (flag); \
5870 } while (0)
5871
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005872 D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
5873 *total_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005874
5875 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
5876 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
5877 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
5878 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
5879
5880#undef CHK
5881
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005882 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005883
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005884 il->ctx.staging.filter_flags &= ~filter_nand;
5885 il->ctx.staging.filter_flags |= filter_or;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005886
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005887 /*
5888 * Not committing directly because hardware can perform a scan,
5889 * but we'll eventually commit the filter flags change anyway.
5890 */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005891
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005892 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005893
5894 /*
5895 * Receiving all multicast frames is always enabled by the
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005896 * default flags setup in il_connection_init_rx_config()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005897 * since we currently do not support programming multicast
5898 * filters into the device.
5899 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005900 *total_flags &=
5901 FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
5902 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005903}
5904
5905/*****************************************************************************
5906 *
5907 * driver setup and teardown
5908 *
5909 *****************************************************************************/
5910
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005911static void
5912il4965_bg_txpower_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005913{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005914 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005915 txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005916
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005917 mutex_lock(&il->mutex);
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005918
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005919 /* If a scan happened to start before we got here
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005920 * then just return; the stats notification will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005921 * kick off another scheduled work to compensate for
5922 * any temperature delta we missed here. */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005923 if (test_bit(S_EXIT_PENDING, &il->status) ||
5924 test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005925 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005926
5927 /* Regardless of if we are associated, we must reconfigure the
5928 * TX power since frames can be sent on non-radar channels while
5929 * not associated */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005930 il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005931
5932 /* Update last_temperature to keep is_calib_needed from running
5933 * when it isn't needed... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005934 il->last_temperature = il->temperature;
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005935out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005936 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005937}
5938
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005939static void
5940il4965_setup_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005941{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005942 il->workqueue = create_singlethread_workqueue(DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005943
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005944 init_waitqueue_head(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005945
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005946 INIT_WORK(&il->restart, il4965_bg_restart);
5947 INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
5948 INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
5949 INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
5950 INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005951
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005952 il_setup_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005953
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005954 INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005955
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005956 init_timer(&il->stats_periodic);
5957 il->stats_periodic.data = (unsigned long)il;
5958 il->stats_periodic.function = il4965_bg_stats_periodic;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005959
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005960 init_timer(&il->watchdog);
5961 il->watchdog.data = (unsigned long)il;
5962 il->watchdog.function = il_bg_watchdog;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005963
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005964 tasklet_init(&il->irq_tasklet,
5965 (void (*)(unsigned long))il4965_irq_tasklet,
5966 (unsigned long)il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005967}
5968
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005969static void
5970il4965_cancel_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005971{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005972 cancel_work_sync(&il->txpower_work);
5973 cancel_delayed_work_sync(&il->init_alive_start);
5974 cancel_delayed_work(&il->alive_start);
5975 cancel_work_sync(&il->run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005976
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005977 il_cancel_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005978
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005979 del_timer_sync(&il->stats_periodic);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005980}
5981
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005982static void
5983il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005984{
5985 int i;
5986
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005987 for (i = 0; i < RATE_COUNT_LEGACY; i++) {
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02005988 rates[i].bitrate = il_rates[i].ieee * 5;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005989 rates[i].hw_value = i; /* Rate scaling will work on idxes */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005990 rates[i].hw_value_short = i;
5991 rates[i].flags = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005992 if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005993 /*
5994 * If CCK != 1M then set short preamble rate flag.
5995 */
5996 rates[i].flags |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005997 (il_rates[i].plcp ==
5998 RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005999 }
6000 }
6001}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006002
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006003/*
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006004 * Acquire il->lock before calling this function !
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006005 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006006void
6007il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006008{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006009 il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006010 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006011}
6012
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006013void
6014il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
6015 int tx_fifo_id, int scd_retry)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006016{
6017 int txq_id = txq->q.id;
6018
6019 /* Find out whether to activate Tx queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006020 int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006021
6022 /* Set up and activate */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006023 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01006024 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
6025 (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
6026 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
6027 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
6028 IL49_SCD_QUEUE_STTS_REG_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006029
6030 txq->sched_retry = scd_retry;
6031
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006032 D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
6033 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006034}
6035
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006036static int
6037il4965_init_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006038{
6039 int ret;
6040
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006041 spin_lock_init(&il->sta_lock);
6042 spin_lock_init(&il->hcmd_lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006043
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006044 INIT_LIST_HEAD(&il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006045
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006046 mutex_init(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006047
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006048 il->ieee_channels = NULL;
6049 il->ieee_rates = NULL;
6050 il->band = IEEE80211_BAND_2GHZ;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006051
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006052 il->iw_mode = NL80211_IFTYPE_STATION;
6053 il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
6054 il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006055
6056 /* initialize force reset */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006057 il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006058
6059 /* Choose which receivers/antennas to use */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006060 if (il->cfg->ops->hcmd->set_rxon_chain)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006061 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006062
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006063 il_init_scan_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006064
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006065 ret = il_init_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006066 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006067 IL_ERR("initializing regulatory failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006068 goto err;
6069 }
6070
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006071 ret = il_init_geos(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006072 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006073 IL_ERR("initializing geos failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006074 goto err_free_channel_map;
6075 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006076 il4965_init_hw_rates(il, il->ieee_rates);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006077
6078 return 0;
6079
6080err_free_channel_map:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006081 il_free_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006082err:
6083 return ret;
6084}
6085
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006086static void
6087il4965_uninit_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006088{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006089 il4965_calib_free_results(il);
6090 il_free_geos(il);
6091 il_free_channel_map(il);
6092 kfree(il->scan_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006093}
6094
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006095static void
6096il4965_hw_detect(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006097{
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006098 il->hw_rev = _il_rd(il, CSR_HW_REV);
6099 il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006100 il->rev_id = il->pci_dev->revision;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006101 D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006102}
6103
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006104static int
6105il4965_set_hw_params(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006106{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006107 il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
6108 il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
6109 if (il->cfg->mod_params->amsdu_size_8K)
6110 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006111 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006112 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006113
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006114 il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006115
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006116 if (il->cfg->mod_params->disable_11n)
6117 il->cfg->sku &= ~IL_SKU_N;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006118
6119 /* Device-specific setup */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006120 return il->cfg->ops->lib->set_hw_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006121}
6122
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006123static const u8 il4965_bss_ac_to_fifo[] = {
6124 IL_TX_FIFO_VO,
6125 IL_TX_FIFO_VI,
6126 IL_TX_FIFO_BE,
6127 IL_TX_FIFO_BK,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006128};
6129
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006130static const u8 il4965_bss_ac_to_queue[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006131 0, 1, 2, 3,
6132};
6133
6134static int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006135il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006136{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006137 int err = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006138 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006139 struct ieee80211_hw *hw;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006140 struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006141 unsigned long flags;
6142 u16 pci_cmd;
6143
6144 /************************
6145 * 1. Allocating HW data
6146 ************************/
6147
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006148 hw = il_alloc_all(cfg);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006149 if (!hw) {
6150 err = -ENOMEM;
6151 goto out;
6152 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006153 il = hw->priv;
6154 /* At this point both hw and il are allocated. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006155
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006156 il->ctx.ctxid = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006157
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006158 il->ctx.always_active = true;
6159 il->ctx.is_active = true;
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02006160 il->ctx.rxon_cmd = C_RXON;
6161 il->ctx.rxon_timing_cmd = C_RXON_TIMING;
6162 il->ctx.rxon_assoc_cmd = C_RXON_ASSOC;
6163 il->ctx.qos_cmd = C_QOS_PARAM;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006164 il->ctx.ap_sta_id = IL_AP_ID;
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02006165 il->ctx.wep_key_cmd = C_WEPKEY;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006166 il->ctx.ac_to_fifo = il4965_bss_ac_to_fifo;
6167 il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006168 il->ctx.exclusive_interface_modes = BIT(NL80211_IFTYPE_ADHOC);
6169 il->ctx.interface_modes = BIT(NL80211_IFTYPE_STATION);
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006170 il->ctx.ap_devtype = RXON_DEV_TYPE_AP;
6171 il->ctx.ibss_devtype = RXON_DEV_TYPE_IBSS;
6172 il->ctx.station_devtype = RXON_DEV_TYPE_ESS;
6173 il->ctx.unused_devtype = RXON_DEV_TYPE_ESS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006174
6175 SET_IEEE80211_DEV(hw, &pdev->dev);
6176
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006177 D_INFO("*** LOAD DRIVER ***\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006178 il->cfg = cfg;
6179 il->pci_dev = pdev;
6180 il->inta_mask = CSR_INI_SET_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006181
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006182 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006183 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006184
6185 /**************************
6186 * 2. Initializing PCI bus
6187 **************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006188 pci_disable_link_state(pdev,
6189 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6190 PCIE_LINK_STATE_CLKPM);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006191
6192 if (pci_enable_device(pdev)) {
6193 err = -ENODEV;
6194 goto out_ieee80211_free_hw;
6195 }
6196
6197 pci_set_master(pdev);
6198
6199 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
6200 if (!err)
6201 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
6202 if (err) {
6203 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6204 if (!err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006205 err =
6206 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006207 /* both attempts failed: */
6208 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006209 IL_WARN("No suitable DMA available.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006210 goto out_pci_disable_device;
6211 }
6212 }
6213
6214 err = pci_request_regions(pdev, DRV_NAME);
6215 if (err)
6216 goto out_pci_disable_device;
6217
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006218 pci_set_drvdata(pdev, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006219
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006220 /***********************
6221 * 3. Read REV register
6222 ***********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006223 il->hw_base = pci_iomap(pdev, 0, 0);
6224 if (!il->hw_base) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006225 err = -ENODEV;
6226 goto out_pci_release_regions;
6227 }
6228
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006229 D_INFO("pci_resource_len = 0x%08llx\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006230 (unsigned long long)pci_resource_len(pdev, 0));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006231 D_INFO("pci_resource_base = %p\n", il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006232
6233 /* these spin locks will be used in apm_ops.init and EEPROM access
6234 * we should init now
6235 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006236 spin_lock_init(&il->reg_lock);
6237 spin_lock_init(&il->lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006238
6239 /*
6240 * stop and reset the on-board processor just in case it is in a
6241 * strange state ... like being left stranded by a primary kernel
6242 * and this is now the kdump kernel trying to start up
6243 */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006244 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006245
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006246 il4965_hw_detect(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006247 IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006248
6249 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6250 * PCI Tx retries from interfering with C3 CPU state */
6251 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
6252
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006253 il4965_prepare_card_hw(il);
6254 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006255 IL_WARN("Failed, HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006256 goto out_iounmap;
6257 }
6258
6259 /*****************
6260 * 4. Read EEPROM
6261 *****************/
6262 /* Read the EEPROM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006263 err = il_eeprom_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006264 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006265 IL_ERR("Unable to init EEPROM\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006266 goto out_iounmap;
6267 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006268 err = il4965_eeprom_check_version(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006269 if (err)
6270 goto out_free_eeprom;
6271
6272 if (err)
6273 goto out_free_eeprom;
6274
6275 /* extract MAC Address */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006276 il4965_eeprom_get_mac(il, il->addresses[0].addr);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006277 D_INFO("MAC address: %pM\n", il->addresses[0].addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006278 il->hw->wiphy->addresses = il->addresses;
6279 il->hw->wiphy->n_addresses = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006280
6281 /************************
6282 * 5. Setup HW constants
6283 ************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006284 if (il4965_set_hw_params(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006285 IL_ERR("failed to set hw parameters\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006286 goto out_free_eeprom;
6287 }
6288
6289 /*******************
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006290 * 6. Setup il
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006291 *******************/
6292
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006293 err = il4965_init_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006294 if (err)
6295 goto out_free_eeprom;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006296 /* At this point both hw and il are initialized. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006297
6298 /********************
6299 * 7. Setup services
6300 ********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006301 spin_lock_irqsave(&il->lock, flags);
6302 il_disable_interrupts(il);
6303 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006304
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006305 pci_enable_msi(il->pci_dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006306
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006307 err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006308 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006309 IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006310 goto out_disable_msi;
6311 }
6312
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006313 il4965_setup_deferred_work(il);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02006314 il4965_setup_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006315
6316 /*********************************************
6317 * 8. Enable interrupts and read RFKILL state
6318 *********************************************/
6319
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02006320 /* enable rfkill interrupt: hw bug w/a */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006321 pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006322 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
6323 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006324 pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006325 }
6326
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006327 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006328
6329 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006330 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006331 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006332 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006333 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006334
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006335 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006336 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006337
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006338 il_power_initialize(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006339
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006340 init_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006341
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006342 err = il4965_request_firmware(il, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006343 if (err)
6344 goto out_destroy_workqueue;
6345
6346 return 0;
6347
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006348out_destroy_workqueue:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006349 destroy_workqueue(il->workqueue);
6350 il->workqueue = NULL;
6351 free_irq(il->pci_dev->irq, il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006352out_disable_msi:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006353 pci_disable_msi(il->pci_dev);
6354 il4965_uninit_drv(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006355out_free_eeprom:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006356 il_eeprom_free(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006357out_iounmap:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006358 pci_iounmap(pdev, il->hw_base);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006359out_pci_release_regions:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006360 pci_set_drvdata(pdev, NULL);
6361 pci_release_regions(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006362out_pci_disable_device:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006363 pci_disable_device(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006364out_ieee80211_free_hw:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006365 il_free_traffic_mem(il);
6366 ieee80211_free_hw(il->hw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006367out:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006368 return err;
6369}
6370
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006371static void __devexit
6372il4965_pci_remove(struct pci_dev *pdev)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006373{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006374 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006375 unsigned long flags;
6376
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006377 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006378 return;
6379
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006380 wait_for_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006381
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006382 D_INFO("*** UNLOAD DRIVER ***\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006383
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006384 il_dbgfs_unregister(il);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006385 sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006386
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006387 /* ieee80211_unregister_hw call wil cause il_mac_stop to
6388 * to be called and il4965_down since we are removing the device
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006389 * we need to set S_EXIT_PENDING bit.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006390 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006391 set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006392
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006393 il_leds_exit(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006394
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006395 if (il->mac80211_registered) {
6396 ieee80211_unregister_hw(il->hw);
6397 il->mac80211_registered = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006398 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006399 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006400 }
6401
6402 /*
6403 * Make sure device is reset to low power before unloading driver.
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006404 * This may be redundant with il4965_down(), but there are paths to
6405 * run il4965_down() without calling apm_ops.stop(), and there are
6406 * paths to avoid running il4965_down() at all before leaving driver.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006407 * This (inexpensive) call *makes sure* device is reset.
6408 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006409 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006410
6411 /* make sure we flush any pending irq or
6412 * tasklet for the driver
6413 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006414 spin_lock_irqsave(&il->lock, flags);
6415 il_disable_interrupts(il);
6416 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006417
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006418 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006419
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006420 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006421
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006422 if (il->rxq.bd)
6423 il4965_rx_queue_free(il, &il->rxq);
6424 il4965_hw_txq_ctx_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006425
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006426 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006427
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006428 /*netif_stop_queue(dev); */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006429 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006430
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006431 /* ieee80211_unregister_hw calls il_mac_stop, which flushes
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006432 * il->workqueue... so we can't take down the workqueue
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006433 * until now... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006434 destroy_workqueue(il->workqueue);
6435 il->workqueue = NULL;
6436 il_free_traffic_mem(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006437
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006438 free_irq(il->pci_dev->irq, il);
6439 pci_disable_msi(il->pci_dev);
6440 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006441 pci_release_regions(pdev);
6442 pci_disable_device(pdev);
6443 pci_set_drvdata(pdev, NULL);
6444
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006445 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006446
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006447 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006448
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006449 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006450}
6451
6452/*
6453 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006454 * must be called under il->lock and mac access
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006455 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006456void
6457il4965_txq_set_sched(struct il_priv *il, u32 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006458{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006459 il_wr_prph(il, IL49_SCD_TXFACT, mask);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006460}
6461
6462/*****************************************************************************
6463 *
6464 * driver and module entry point
6465 *
6466 *****************************************************************************/
6467
6468/* Hardware specific file defines the PCI IDs table for that hardware module */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006469static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006470 {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
6471 {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006472 {0}
6473};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006474MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006475
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006476static struct pci_driver il4965_driver = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006477 .name = DRV_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006478 .id_table = il4965_hw_card_ids,
6479 .probe = il4965_pci_probe,
6480 .remove = __devexit_p(il4965_pci_remove),
6481 .driver.pm = IL_LEGACY_PM_OPS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006482};
6483
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006484static int __init
6485il4965_init(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006486{
6487
6488 int ret;
6489 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
6490 pr_info(DRV_COPYRIGHT "\n");
6491
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006492 ret = il4965_rate_control_register();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006493 if (ret) {
6494 pr_err("Unable to register rate control algorithm: %d\n", ret);
6495 return ret;
6496 }
6497
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006498 ret = pci_register_driver(&il4965_driver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006499 if (ret) {
6500 pr_err("Unable to initialize PCI module\n");
6501 goto error_register;
6502 }
6503
6504 return ret;
6505
6506error_register:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006507 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006508 return ret;
6509}
6510
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006511static void __exit
6512il4965_exit(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006513{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006514 pci_unregister_driver(&il4965_driver);
6515 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006516}
6517
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006518module_exit(il4965_exit);
6519module_init(il4965_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006520
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006521#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02006522module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006523MODULE_PARM_DESC(debug, "debug output mask");
6524#endif
6525
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006526module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006527MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006528module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006529MODULE_PARM_DESC(queues_num, "number of hw queues.");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006530module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006531MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006532module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int,
6533 S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006534MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006535module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006536MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");