blob: e1dfb2c4f832c56f1427a0b3f9c5a6c1d4babb0b [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 Gruszkaa0c1ef32011-12-23 08:13:44 +0100828static void
829il4965_toggle_tx_ant(struct il_priv *il, u8 *ant, u8 valid)
830{
831 int i;
832 u8 ind = *ant;
833
834 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
835 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
836 if (valid & BIT(ind)) {
837 *ant = ind;
838 return;
839 }
840 }
841}
842
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100843int
844il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200845{
846 struct il_host_cmd cmd = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200847 .id = C_SCAN,
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200848 .len = sizeof(struct il_scan_cmd),
849 .flags = CMD_SIZE_HUGE,
850 };
851 struct il_scan_cmd *scan;
852 struct il_rxon_context *ctx = &il->ctx;
853 u32 rate_flags = 0;
854 u16 cmd_len;
855 u16 rx_chain = 0;
856 enum ieee80211_band band;
857 u8 n_probes = 0;
858 u8 rx_ant = il->hw_params.valid_rx_ant;
859 u8 rate;
860 bool is_active = false;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100861 int chan_mod;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200862 u8 active_chains;
863 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
864 int ret;
865
866 lockdep_assert_held(&il->mutex);
867
Greg Dietsche730b4c22011-09-06 17:33:51 -0500868 ctx = il_rxon_ctx_from_vif(vif);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200869
870 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100871 il->scan_cmd =
872 kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
873 GFP_KERNEL);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200874 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100875 D_SCAN("fail to allocate memory for scan\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200876 return -ENOMEM;
877 }
878 }
879 scan = il->scan_cmd;
880 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
881
882 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
883 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
884
885 if (il_is_any_associated(il)) {
886 u16 interval;
887 u32 extra;
888 u32 suspend_time = 100;
889 u32 scan_suspend_time = 100;
890
891 D_INFO("Scanning while associated...\n");
892 interval = vif->bss_conf.beacon_int;
893
894 scan->suspend_time = 0;
895 scan->max_out_time = cpu_to_le32(200 * 1024);
896 if (!interval)
897 interval = suspend_time;
898
899 extra = (suspend_time / interval) << 22;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100900 scan_suspend_time =
901 (extra | ((suspend_time % interval) * 1024));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200902 scan->suspend_time = cpu_to_le32(scan_suspend_time);
903 D_SCAN("suspend_time 0x%X beacon interval %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100904 scan_suspend_time, interval);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200905 }
906
907 if (il->scan_request->n_ssids) {
908 int i, p = 0;
909 D_SCAN("Kicking off active scan\n");
910 for (i = 0; i < il->scan_request->n_ssids; i++) {
911 /* always does wildcard anyway */
912 if (!il->scan_request->ssids[i].ssid_len)
913 continue;
914 scan->direct_scan[p].id = WLAN_EID_SSID;
915 scan->direct_scan[p].len =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100916 il->scan_request->ssids[i].ssid_len;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200917 memcpy(scan->direct_scan[p].ssid,
918 il->scan_request->ssids[i].ssid,
919 il->scan_request->ssids[i].ssid_len);
920 n_probes++;
921 p++;
922 }
923 is_active = true;
924 } else
925 D_SCAN("Start passive scan.\n");
926
927 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
928 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
929 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
930
931 switch (il->scan_band) {
932 case IEEE80211_BAND_2GHZ:
933 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100934 chan_mod =
935 le32_to_cpu(il->ctx.active.
936 flags & RXON_FLG_CHANNEL_MODE_MSK) >>
937 RXON_FLG_CHANNEL_MODE_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200938 if (chan_mod == CHANNEL_MODE_PURE_40) {
939 rate = RATE_6M_PLCP;
940 } else {
941 rate = RATE_1M_PLCP;
942 rate_flags = RATE_MCS_CCK_MSK;
943 }
944 break;
945 case IEEE80211_BAND_5GHZ:
946 rate = RATE_6M_PLCP;
947 break;
948 default:
949 IL_WARN("Invalid scan band\n");
950 return -EIO;
951 }
952
953 /*
954 * If active scanning is requested but a certain channel is
955 * marked passive, we can do active scanning if we detect
956 * transmissions.
957 *
958 * There is an issue with some firmware versions that triggers
959 * a sysassert on a "good CRC threshold" of zero (== disabled),
960 * on a radar channel even though this means that we should NOT
961 * send probes.
962 *
963 * The "good CRC threshold" is the number of frames that we
964 * need to receive during our dwell time on a channel before
965 * sending out probes -- setting this to a huge value will
966 * mean we never reach it, but at the same time work around
967 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
968 * here instead of IL_GOOD_CRC_TH_DISABLED.
969 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100970 scan->good_CRC_th =
971 is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200972
973 band = il->scan_band;
974
975 if (il->cfg->scan_rx_antennas[band])
976 rx_ant = il->cfg->scan_rx_antennas[band];
977
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +0100978 il4965_toggle_tx_ant(il, &il->scan_tx_ant[band], scan_tx_antennas);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200979 rate_flags |= il4965_ant_idx_to_flags(il->scan_tx_ant[band]);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100980 scan->tx_cmd.rate_n_flags =
981 il4965_hw_set_rate_n_flags(rate, rate_flags);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200982
983 /* In power save mode use one chain, otherwise use all chains */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100984 if (test_bit(S_POWER_PMI, &il->status)) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200985 /* rx_ant has been set to all valid chains previously */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100986 active_chains =
987 rx_ant & ((u8) (il->chain_noise_data.active_chains));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200988 if (!active_chains)
989 active_chains = rx_ant;
990
991 D_SCAN("chain_noise_data.active_chains: %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100992 il->chain_noise_data.active_chains);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200993
994 rx_ant = il4965_first_antenna(active_chains);
995 }
996
997 /* MIMO is not used here, but value is required */
998 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
999 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
1000 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
1001 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
1002 scan->rx_chain = cpu_to_le16(rx_chain);
1003
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001004 cmd_len =
1005 il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
1006 vif->addr, il->scan_request->ie,
1007 il->scan_request->ie_len,
1008 IL_MAX_SCAN_SIZE - sizeof(*scan));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001009 scan->tx_cmd.len = cpu_to_le16(cmd_len);
1010
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001011 scan->filter_flags |=
1012 (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001013
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001014 scan->channel_count =
1015 il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
1016 (void *)&scan->data[cmd_len]);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001017 if (scan->channel_count == 0) {
1018 D_SCAN("channel count %d\n", scan->channel_count);
1019 return -EIO;
1020 }
1021
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001022 cmd.len +=
1023 le16_to_cpu(scan->tx_cmd.len) +
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001024 scan->channel_count * sizeof(struct il_scan_channel);
1025 cmd.data = scan;
1026 scan->len = cpu_to_le16(cmd.len);
1027
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001028 set_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001029
1030 ret = il_send_cmd_sync(il, &cmd);
1031 if (ret)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001032 clear_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001033
1034 return ret;
1035}
1036
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001037int
1038il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
1039 bool add)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001040{
1041 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1042
1043 if (add)
1044 return il4965_add_bssid_station(il, vif_priv->ctx,
1045 vif->bss_conf.bssid,
1046 &vif_priv->ibss_bssid_sta_id);
1047 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001048 vif->bss_conf.bssid);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001049}
1050
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001051void
1052il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001053{
1054 lockdep_assert_held(&il->sta_lock);
1055
1056 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1057 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1058 else {
1059 D_TX("free more than tfds_in_queue (%u:%d)\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001060 il->stations[sta_id].tid[tid].tfds_in_queue, freed);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001061 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1062 }
1063}
1064
1065#define IL_TX_QUEUE_MSK 0xfffff
1066
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001067static bool
1068il4965_is_single_rx_stream(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001069{
1070 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001071 il->current_ht_config.single_chain_sufficient;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001072}
1073
1074#define IL_NUM_RX_CHAINS_MULTIPLE 3
1075#define IL_NUM_RX_CHAINS_SINGLE 2
1076#define IL_NUM_IDLE_CHAINS_DUAL 2
1077#define IL_NUM_IDLE_CHAINS_SINGLE 1
1078
1079/*
1080 * Determine how many receiver/antenna chains to use.
1081 *
1082 * More provides better reception via diversity. Fewer saves power
1083 * at the expense of throughput, but only when not in powersave to
1084 * start with.
1085 *
1086 * MIMO (dual stream) requires at least 2, but works better with 3.
1087 * This does not determine *which* chains to use, just how many.
1088 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001089static int
1090il4965_get_active_rx_chain_count(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001091{
1092 /* # of Rx chains to use when expecting MIMO. */
1093 if (il4965_is_single_rx_stream(il))
1094 return IL_NUM_RX_CHAINS_SINGLE;
1095 else
1096 return IL_NUM_RX_CHAINS_MULTIPLE;
1097}
1098
1099/*
1100 * When we are in power saving mode, unless device support spatial
1101 * multiplexing power save, use the active count for rx chain count.
1102 */
1103static int
1104il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1105{
1106 /* # Rx chains when idling, depending on SMPS mode */
1107 switch (il->current_ht_config.smps) {
1108 case IEEE80211_SMPS_STATIC:
1109 case IEEE80211_SMPS_DYNAMIC:
1110 return IL_NUM_IDLE_CHAINS_SINGLE;
1111 case IEEE80211_SMPS_OFF:
1112 return active_cnt;
1113 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001114 WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001115 return active_cnt;
1116 }
1117}
1118
1119/* up to 4 chains */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001120static u8
1121il4965_count_chain_bitmap(u32 chain_bitmap)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001122{
1123 u8 res;
1124 res = (chain_bitmap & BIT(0)) >> 0;
1125 res += (chain_bitmap & BIT(1)) >> 1;
1126 res += (chain_bitmap & BIT(2)) >> 2;
1127 res += (chain_bitmap & BIT(3)) >> 3;
1128 return res;
1129}
1130
1131/**
1132 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1133 *
1134 * Selects how many and which Rx receivers/antennas/chains to use.
1135 * This should not be used for scan command ... it puts data in wrong place.
1136 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001137void
1138il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001139{
1140 bool is_single = il4965_is_single_rx_stream(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001141 bool is_cam = !test_bit(S_POWER_PMI, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001142 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1143 u32 active_chains;
1144 u16 rx_chain;
1145
1146 /* Tell uCode which antennas are actually connected.
1147 * Before first association, we assume all antennas are connected.
1148 * Just after first association, il4965_chain_noise_calibration()
1149 * checks which antennas actually *are* connected. */
1150 if (il->chain_noise_data.active_chains)
1151 active_chains = il->chain_noise_data.active_chains;
1152 else
1153 active_chains = il->hw_params.valid_rx_ant;
1154
1155 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1156
1157 /* How many receivers should we use? */
1158 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1159 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1160
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001161 /* correct rx chain count according hw settings
1162 * and chain noise calibration
1163 */
1164 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1165 if (valid_rx_cnt < active_rx_cnt)
1166 active_rx_cnt = valid_rx_cnt;
1167
1168 if (valid_rx_cnt < idle_rx_cnt)
1169 idle_rx_cnt = valid_rx_cnt;
1170
1171 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001172 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001173
1174 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1175
1176 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
1177 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1178 else
1179 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1180
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001181 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", ctx->staging.rx_chain,
1182 active_rx_cnt, idle_rx_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001183
1184 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1185 active_rx_cnt < idle_rx_cnt);
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 Gruszkaa0c1ef32011-12-23 08:13:44 +01001591 il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001592 rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
1593
1594 /* Set the rate in the TX cmd */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001595 tx_cmd->rate_n_flags =
1596 il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001597}
1598
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001599static void
1600il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
1601 struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
1602 int sta_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001603{
1604 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1605
1606 switch (keyconf->cipher) {
1607 case WLAN_CIPHER_SUITE_CCMP:
1608 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1609 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1610 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1611 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1612 D_TX("tx_cmd with AES hwcrypto\n");
1613 break;
1614
1615 case WLAN_CIPHER_SUITE_TKIP:
1616 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1617 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1618 D_TX("tx_cmd with tkip hwcrypto\n");
1619 break;
1620
1621 case WLAN_CIPHER_SUITE_WEP104:
1622 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1623 /* fall through */
1624 case WLAN_CIPHER_SUITE_WEP40:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001625 tx_cmd->sec_ctl |=
1626 (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
1627 TX_CMD_SEC_SHIFT);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001628
1629 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1630
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001631 D_TX("Configuring packet for WEP encryption " "with key %d\n",
1632 keyconf->keyidx);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001633 break;
1634
1635 default:
1636 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1637 break;
1638 }
1639}
1640
1641/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001642 * start C_TX command process
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001643 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001644int
1645il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001646{
1647 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1648 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1649 struct ieee80211_sta *sta = info->control.sta;
1650 struct il_station_priv *sta_priv = NULL;
1651 struct il_tx_queue *txq;
1652 struct il_queue *q;
1653 struct il_device_cmd *out_cmd;
1654 struct il_cmd_meta *out_meta;
1655 struct il_tx_cmd *tx_cmd;
1656 struct il_rxon_context *ctx = &il->ctx;
1657 int txq_id;
1658 dma_addr_t phys_addr;
1659 dma_addr_t txcmd_phys;
1660 dma_addr_t scratch_phys;
1661 u16 len, firstlen, secondlen;
1662 u16 seq_number = 0;
1663 __le16 fc;
1664 u8 hdr_len;
1665 u8 sta_id;
1666 u8 wait_write_ptr = 0;
1667 u8 tid = 0;
1668 u8 *qc = NULL;
1669 unsigned long flags;
1670 bool is_agg = false;
1671
1672 if (info->control.vif)
1673 ctx = il_rxon_ctx_from_vif(info->control.vif);
1674
1675 spin_lock_irqsave(&il->lock, flags);
1676 if (il_is_rfkill(il)) {
1677 D_DROP("Dropping - RF KILL\n");
1678 goto drop_unlock;
1679 }
1680
1681 fc = hdr->frame_control;
1682
1683#ifdef CONFIG_IWLEGACY_DEBUG
1684 if (ieee80211_is_auth(fc))
1685 D_TX("Sending AUTH frame\n");
1686 else if (ieee80211_is_assoc_req(fc))
1687 D_TX("Sending ASSOC frame\n");
1688 else if (ieee80211_is_reassoc_req(fc))
1689 D_TX("Sending REASSOC frame\n");
1690#endif
1691
1692 hdr_len = ieee80211_hdrlen(fc);
1693
1694 /* For management frames use broadcast id to do not break aggregation */
1695 if (!ieee80211_is_data(fc))
1696 sta_id = ctx->bcast_sta_id;
1697 else {
1698 /* Find idx into station table for destination station */
1699 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1700
1701 if (sta_id == IL_INVALID_STATION) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001702 D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001703 goto drop_unlock;
1704 }
1705 }
1706
1707 D_TX("station Id %d\n", sta_id);
1708
1709 if (sta)
1710 sta_priv = (void *)sta->drv_priv;
1711
1712 if (sta_priv && sta_priv->asleep &&
1713 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1714 /*
1715 * This sends an asynchronous command to the device,
1716 * but we can rely on it being processed before the
1717 * next frame is processed -- and the next frame to
1718 * this station is the one that will consume this
1719 * counter.
1720 * For now set the counter to just 1 since we do not
1721 * support uAPSD yet.
1722 */
1723 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1724 }
1725
1726 /*
1727 * Send this frame after DTIM -- there's a special queue
1728 * reserved for this for contexts that support AP mode.
1729 */
1730 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1731 txq_id = ctx->mcast_queue;
1732 /*
1733 * The microcode will clear the more data
1734 * bit in the last frame it transmits.
1735 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001736 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001737 } else
1738 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
1739
1740 /* irqs already disabled/saved above when locking il->lock */
1741 spin_lock(&il->sta_lock);
1742
1743 if (ieee80211_is_data_qos(fc)) {
1744 qc = ieee80211_get_qos_ctl(hdr);
1745 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1746 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1747 spin_unlock(&il->sta_lock);
1748 goto drop_unlock;
1749 }
1750 seq_number = il->stations[sta_id].tid[tid].seq_number;
1751 seq_number &= IEEE80211_SCTL_SEQ;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001752 hdr->seq_ctrl =
1753 hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001754 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1755 seq_number += 0x10;
1756 /* aggregation is on for this <sta,tid> */
1757 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1758 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1759 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1760 is_agg = true;
1761 }
1762 }
1763
1764 txq = &il->txq[txq_id];
1765 q = &txq->q;
1766
1767 if (unlikely(il_queue_space(q) < q->high_mark)) {
1768 spin_unlock(&il->sta_lock);
1769 goto drop_unlock;
1770 }
1771
1772 if (ieee80211_is_data_qos(fc)) {
1773 il->stations[sta_id].tid[tid].tfds_in_queue++;
1774 if (!ieee80211_has_morefrags(fc))
1775 il->stations[sta_id].tid[tid].seq_number = seq_number;
1776 }
1777
1778 spin_unlock(&il->sta_lock);
1779
1780 /* Set up driver data for this TFD */
1781 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1782 txq->txb[q->write_ptr].skb = skb;
1783 txq->txb[q->write_ptr].ctx = ctx;
1784
1785 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1786 out_cmd = txq->cmd[q->write_ptr];
1787 out_meta = &txq->meta[q->write_ptr];
1788 tx_cmd = &out_cmd->cmd.tx;
1789 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1790 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1791
1792 /*
1793 * Set up the Tx-command (not MAC!) header.
1794 * Store the chosen Tx queue and TFD idx within the sequence field;
1795 * after Tx, uCode's Tx response will return this value so driver can
1796 * locate the frame within the tx queue and do post-tx processing.
1797 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001798 out_cmd->hdr.cmd = C_TX;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001799 out_cmd->hdr.sequence =
1800 cpu_to_le16((u16)
1801 (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001802
1803 /* Copy MAC header from skb into command buffer */
1804 memcpy(tx_cmd->hdr, hdr, hdr_len);
1805
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001806 /* Total # bytes to be transmitted */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001807 len = (u16) skb->len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001808 tx_cmd->len = cpu_to_le16(len);
1809
1810 if (info->control.hw_key)
1811 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1812
1813 /* TODO need this for burst mode later on */
1814 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1815 il_dbg_log_tx_data_frame(il, len, hdr);
1816
1817 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1818
1819 il_update_stats(il, true, fc, len);
1820 /*
1821 * Use the first empty entry in this queue's command buffer array
1822 * to contain the Tx command and MAC header concatenated together
1823 * (payload data will be in another buffer).
1824 * Size of this varies, due to varying MAC header length.
1825 * If end is not dword aligned, we'll have 2 extra bytes at the end
1826 * of the MAC header (device reads on dword boundaries).
1827 * We'll tell device about this padding later.
1828 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001829 len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001830 firstlen = (len + 3) & ~3;
1831
1832 /* Tell NIC about any 2-byte padding after MAC header */
1833 if (firstlen != len)
1834 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1835
1836 /* Physical address of this Tx command's header (not MAC header!),
1837 * within command buffer array. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001838 txcmd_phys =
1839 pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
1840 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001841 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1842 dma_unmap_len_set(out_meta, len, firstlen);
1843 /* Add buffer containing Tx command and MAC(!) header to TFD's
1844 * first entry */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001845 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen,
1846 1, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001847
1848 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1849 txq->need_update = 1;
1850 } else {
1851 wait_write_ptr = 1;
1852 txq->need_update = 0;
1853 }
1854
1855 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1856 * if any (802.11 null frames have no payload). */
1857 secondlen = skb->len - hdr_len;
1858 if (secondlen > 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001859 phys_addr =
1860 pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
1861 PCI_DMA_TODEVICE);
1862 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr,
1863 secondlen, 0, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001864 }
1865
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001866 scratch_phys =
1867 txcmd_phys + sizeof(struct il_cmd_header) +
1868 offsetof(struct il_tx_cmd, scratch);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001869
1870 /* take back ownership of DMA buffer to enable update */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001871 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
1872 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001873 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1874 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1875
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001876 D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001877 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001878 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
1879 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001880
1881 /* Set up entry for this TFD in Tx byte-count array */
1882 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1883 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001884 le16_to_cpu(tx_cmd->
1885 len));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001886
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001887 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
1888 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001889
1890 /* Tell device the write idx *just past* this latest filled TFD */
1891 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1892 il_txq_update_write_ptr(il, txq);
1893 spin_unlock_irqrestore(&il->lock, flags);
1894
1895 /*
1896 * At this point the frame is "transmitted" successfully
1897 * and we will get a TX status notification eventually,
1898 * regardless of the value of ret. "ret" only indicates
1899 * whether or not we should update the write pointer.
1900 */
1901
1902 /*
1903 * Avoid atomic ops if it isn't an associated client.
1904 * Also, if this is a packet for aggregation, don't
1905 * increase the counter because the ucode will stop
1906 * aggregation queues when their respective station
1907 * goes to sleep.
1908 */
1909 if (sta_priv && sta_priv->client && !is_agg)
1910 atomic_inc(&sta_priv->pending_frames);
1911
1912 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1913 if (wait_write_ptr) {
1914 spin_lock_irqsave(&il->lock, flags);
1915 txq->need_update = 1;
1916 il_txq_update_write_ptr(il, txq);
1917 spin_unlock_irqrestore(&il->lock, flags);
1918 } else {
1919 il_stop_queue(il, txq);
1920 }
1921 }
1922
1923 return 0;
1924
1925drop_unlock:
1926 spin_unlock_irqrestore(&il->lock, flags);
1927 return -1;
1928}
1929
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001930static inline int
1931il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001932{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001933 ptr->addr =
1934 dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001935 if (!ptr->addr)
1936 return -ENOMEM;
1937 ptr->size = size;
1938 return 0;
1939}
1940
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001941static inline void
1942il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001943{
1944 if (unlikely(!ptr->addr))
1945 return;
1946
1947 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1948 memset(ptr, 0, sizeof(*ptr));
1949}
1950
1951/**
1952 * il4965_hw_txq_ctx_free - Free TXQ Context
1953 *
1954 * Destroy all TX DMA queues and structures
1955 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001956void
1957il4965_hw_txq_ctx_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001958{
1959 int txq_id;
1960
1961 /* Tx queues */
1962 if (il->txq) {
1963 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1964 if (txq_id == il->cmd_queue)
1965 il_cmd_queue_free(il);
1966 else
1967 il_tx_queue_free(il, txq_id);
1968 }
1969 il4965_free_dma_ptr(il, &il->kw);
1970
1971 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1972
1973 /* free tx queue structure */
1974 il_txq_mem(il);
1975}
1976
1977/**
1978 * il4965_txq_ctx_alloc - allocate TX queue context
1979 * Allocate all Tx DMA structures and initialize them
1980 *
1981 * @param il
1982 * @return error code
1983 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001984int
1985il4965_txq_ctx_alloc(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001986{
1987 int ret;
1988 int txq_id, slots_num;
1989 unsigned long flags;
1990
1991 /* Free all tx/cmd queues and keep-warm buffer */
1992 il4965_hw_txq_ctx_free(il);
1993
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001994 ret =
1995 il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
1996 il->hw_params.scd_bc_tbls_size);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001997 if (ret) {
1998 IL_ERR("Scheduler BC Table allocation failed\n");
1999 goto error_bc_tbls;
2000 }
2001 /* Alloc keep-warm buffer */
2002 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
2003 if (ret) {
2004 IL_ERR("Keep Warm allocation failed\n");
2005 goto error_kw;
2006 }
2007
2008 /* allocate tx queue structure */
2009 ret = il_alloc_txq_mem(il);
2010 if (ret)
2011 goto error;
2012
2013 spin_lock_irqsave(&il->lock, flags);
2014
2015 /* Turn off all Tx DMA fifos */
2016 il4965_txq_set_sched(il, 0);
2017
2018 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002019 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002020
2021 spin_unlock_irqrestore(&il->lock, flags);
2022
2023 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2024 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002025 slots_num =
2026 (txq_id ==
2027 il->cmd_queue) ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2028 ret = il_tx_queue_init(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002029 if (ret) {
2030 IL_ERR("Tx %d queue init failed\n", txq_id);
2031 goto error;
2032 }
2033 }
2034
2035 return ret;
2036
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002037error:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002038 il4965_hw_txq_ctx_free(il);
2039 il4965_free_dma_ptr(il, &il->kw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002040error_kw:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002041 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002042error_bc_tbls:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002043 return ret;
2044}
2045
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002046void
2047il4965_txq_ctx_reset(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002048{
2049 int txq_id, slots_num;
2050 unsigned long flags;
2051
2052 spin_lock_irqsave(&il->lock, flags);
2053
2054 /* Turn off all Tx DMA fifos */
2055 il4965_txq_set_sched(il, 0);
2056
2057 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002058 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002059
2060 spin_unlock_irqrestore(&il->lock, flags);
2061
2062 /* Alloc and init all Tx queues, including the command queue (#4) */
2063 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002064 slots_num =
2065 txq_id == il->cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2066 il_tx_queue_reset(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002067 }
2068}
2069
2070/**
2071 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2072 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002073void
2074il4965_txq_ctx_stop(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002075{
2076 int ch, txq_id;
2077 unsigned long flags;
2078
2079 /* Turn off all Tx DMA fifos */
2080 spin_lock_irqsave(&il->lock, flags);
2081
2082 il4965_txq_set_sched(il, 0);
2083
2084 /* Stop each Tx DMA channel, and wait for it to be idle */
2085 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002086 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2087 if (il_poll_bit
2088 (il, FH49_TSSR_TX_STATUS_REG,
2089 FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002090 IL_ERR("Failing on timeout while stopping"
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002091 " DMA channel %d [0x%08x]", ch,
2092 il_rd(il, FH49_TSSR_TX_STATUS_REG));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002093 }
2094 spin_unlock_irqrestore(&il->lock, flags);
2095
2096 if (!il->txq)
2097 return;
2098
2099 /* Unmap DMA from host system and free skb's */
2100 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2101 if (txq_id == il->cmd_queue)
2102 il_cmd_queue_unmap(il);
2103 else
2104 il_tx_queue_unmap(il, txq_id);
2105}
2106
2107/*
2108 * Find first available (lowest unused) Tx Queue, mark it "active".
2109 * Called only when finding queue for aggregation.
2110 * Should never return anything < 7, because they should already
2111 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2112 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002113static int
2114il4965_txq_ctx_activate_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002115{
2116 int txq_id;
2117
2118 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2119 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2120 return txq_id;
2121 return -1;
2122}
2123
2124/**
2125 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2126 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002127static void
2128il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002129{
2130 /* Simply stop the queue, but don't change any configuration;
2131 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002132 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002133 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
2134 (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002135}
2136
2137/**
2138 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2139 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002140static int
2141il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002142{
2143 u32 tbl_dw_addr;
2144 u32 tbl_dw;
2145 u16 scd_q2ratid;
2146
2147 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2148
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002149 tbl_dw_addr =
2150 il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002151
2152 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2153
2154 if (txq_id & 0x1)
2155 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2156 else
2157 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2158
2159 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2160
2161 return 0;
2162}
2163
2164/**
2165 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2166 *
2167 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2168 * i.e. it must be one of the higher queues used for aggregation
2169 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002170static int
2171il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
2172 int tid, u16 ssn_idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002173{
2174 unsigned long flags;
2175 u16 ra_tid;
2176 int ret;
2177
2178 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2179 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002180 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2181 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002182 txq_id, IL49_FIRST_AMPDU_QUEUE,
2183 IL49_FIRST_AMPDU_QUEUE +
2184 il->cfg->base_params->num_of_ampdu_queues - 1);
2185 return -EINVAL;
2186 }
2187
2188 ra_tid = BUILD_RAxTID(sta_id, tid);
2189
2190 /* Modify device's station table to Tx this TID */
2191 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2192 if (ret)
2193 return ret;
2194
2195 spin_lock_irqsave(&il->lock, flags);
2196
2197 /* Stop this Tx queue before configuring it */
2198 il4965_tx_queue_stop_scheduler(il, txq_id);
2199
2200 /* Map receiver-address / traffic-ID to this queue */
2201 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2202
2203 /* Set this queue as a chain-building queue */
2204 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2205
2206 /* Place first TFD at idx corresponding to start sequence number.
2207 * Assumes that ssn_idx is valid (!= 0xFFF) */
2208 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2209 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2210 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2211
2212 /* Set up Tx win size and frame limit for this queue */
2213 il_write_targ_mem(il,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002214 il->scd_base_addr +
2215 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2216 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
2217 & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002218
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002219 il_write_targ_mem(il,
2220 il->scd_base_addr +
2221 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2222 (SCD_FRAME_LIMIT <<
2223 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
2224 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002225
2226 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2227
2228 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2229 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2230
2231 spin_unlock_irqrestore(&il->lock, flags);
2232
2233 return 0;
2234}
2235
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002236int
2237il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2238 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002239{
2240 int sta_id;
2241 int tx_fifo;
2242 int txq_id;
2243 int ret;
2244 unsigned long flags;
2245 struct il_tid_data *tid_data;
2246
2247 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2248 if (unlikely(tx_fifo < 0))
2249 return tx_fifo;
2250
Greg Dietsche53611e02011-08-28 08:26:16 -05002251 D_HT("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002252
2253 sta_id = il_sta_id(sta);
2254 if (sta_id == IL_INVALID_STATION) {
2255 IL_ERR("Start AGG on invalid station\n");
2256 return -ENXIO;
2257 }
2258 if (unlikely(tid >= MAX_TID_COUNT))
2259 return -EINVAL;
2260
2261 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2262 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2263 return -ENXIO;
2264 }
2265
2266 txq_id = il4965_txq_ctx_activate_free(il);
2267 if (txq_id == -1) {
2268 IL_ERR("No free aggregation queue available\n");
2269 return -ENXIO;
2270 }
2271
2272 spin_lock_irqsave(&il->sta_lock, flags);
2273 tid_data = &il->stations[sta_id].tid[tid];
2274 *ssn = SEQ_TO_SN(tid_data->seq_number);
2275 tid_data->agg.txq_id = txq_id;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002276 il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002277 spin_unlock_irqrestore(&il->sta_lock, flags);
2278
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002279 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002280 if (ret)
2281 return ret;
2282
2283 spin_lock_irqsave(&il->sta_lock, flags);
2284 tid_data = &il->stations[sta_id].tid[tid];
2285 if (tid_data->tfds_in_queue == 0) {
2286 D_HT("HW queue is empty\n");
2287 tid_data->agg.state = IL_AGG_ON;
2288 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2289 } else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002290 D_HT("HW queue is NOT empty: %d packets in HW queue\n",
2291 tid_data->tfds_in_queue);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002292 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2293 }
2294 spin_unlock_irqrestore(&il->sta_lock, flags);
2295 return ret;
2296}
2297
2298/**
2299 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2300 * il->lock must be held by the caller
2301 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002302static int
2303il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002304{
2305 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2306 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002307 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2308 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002309 txq_id, IL49_FIRST_AMPDU_QUEUE,
2310 IL49_FIRST_AMPDU_QUEUE +
2311 il->cfg->base_params->num_of_ampdu_queues - 1);
2312 return -EINVAL;
2313 }
2314
2315 il4965_tx_queue_stop_scheduler(il, txq_id);
2316
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002317 il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002318
2319 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2320 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2321 /* supposes that ssn_idx is valid (!= 0xFFF) */
2322 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2323
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002324 il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002325 il_txq_ctx_deactivate(il, txq_id);
2326 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2327
2328 return 0;
2329}
2330
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002331int
2332il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2333 struct ieee80211_sta *sta, u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002334{
2335 int tx_fifo_id, txq_id, sta_id, ssn;
2336 struct il_tid_data *tid_data;
2337 int write_ptr, read_ptr;
2338 unsigned long flags;
2339
2340 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2341 if (unlikely(tx_fifo_id < 0))
2342 return tx_fifo_id;
2343
2344 sta_id = il_sta_id(sta);
2345
2346 if (sta_id == IL_INVALID_STATION) {
2347 IL_ERR("Invalid station for AGG tid %d\n", tid);
2348 return -ENXIO;
2349 }
2350
2351 spin_lock_irqsave(&il->sta_lock, flags);
2352
2353 tid_data = &il->stations[sta_id].tid[tid];
2354 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2355 txq_id = tid_data->agg.txq_id;
2356
2357 switch (il->stations[sta_id].tid[tid].agg.state) {
2358 case IL_EMPTYING_HW_QUEUE_ADDBA:
2359 /*
2360 * This can happen if the peer stops aggregation
2361 * again before we've had a chance to drain the
2362 * queue we selected previously, i.e. before the
2363 * session was really started completely.
2364 */
2365 D_HT("AGG stop before setup done\n");
2366 goto turn_off;
2367 case IL_AGG_ON:
2368 break;
2369 default:
2370 IL_WARN("Stopping AGG while state not ON or starting\n");
2371 }
2372
2373 write_ptr = il->txq[txq_id].q.write_ptr;
2374 read_ptr = il->txq[txq_id].q.read_ptr;
2375
2376 /* The queue is not empty */
2377 if (write_ptr != read_ptr) {
2378 D_HT("Stopping a non empty AGG HW QUEUE\n");
2379 il->stations[sta_id].tid[tid].agg.state =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002380 IL_EMPTYING_HW_QUEUE_DELBA;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002381 spin_unlock_irqrestore(&il->sta_lock, flags);
2382 return 0;
2383 }
2384
2385 D_HT("HW queue is empty\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002386turn_off:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002387 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2388
2389 /* do not restore/save irqs */
2390 spin_unlock(&il->sta_lock);
2391 spin_lock(&il->lock);
2392
2393 /*
2394 * the only reason this call can fail is queue number out of range,
2395 * which can happen if uCode is reloaded and all the station
2396 * information are lost. if it is outside the range, there is no need
2397 * to deactivate the uCode queue, just return "success" to allow
2398 * mac80211 to clean up it own data.
2399 */
2400 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2401 spin_unlock_irqrestore(&il->lock, flags);
2402
2403 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2404
2405 return 0;
2406}
2407
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002408int
2409il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002410{
2411 struct il_queue *q = &il->txq[txq_id].q;
2412 u8 *addr = il->stations[sta_id].sta.sta.addr;
2413 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2414 struct il_rxon_context *ctx;
2415
2416 ctx = &il->ctx;
2417
2418 lockdep_assert_held(&il->sta_lock);
2419
2420 switch (il->stations[sta_id].tid[tid].agg.state) {
2421 case IL_EMPTYING_HW_QUEUE_DELBA:
2422 /* We are reclaiming the last packet of the */
2423 /* aggregated HW queue */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002424 if (txq_id == tid_data->agg.txq_id &&
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002425 q->read_ptr == q->write_ptr) {
2426 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2427 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002428 D_HT("HW queue empty: continue DELBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002429 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2430 tid_data->agg.state = IL_AGG_OFF;
2431 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2432 }
2433 break;
2434 case IL_EMPTYING_HW_QUEUE_ADDBA:
2435 /* We are reclaiming the last packet of the queue */
2436 if (tid_data->tfds_in_queue == 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002437 D_HT("HW queue empty: continue ADDBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002438 tid_data->agg.state = IL_AGG_ON;
2439 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2440 }
2441 break;
2442 }
2443
2444 return 0;
2445}
2446
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002447static void
2448il4965_non_agg_tx_status(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002449 const u8 *addr1)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002450{
2451 struct ieee80211_sta *sta;
2452 struct il_station_priv *sta_priv;
2453
2454 rcu_read_lock();
2455 sta = ieee80211_find_sta(ctx->vif, addr1);
2456 if (sta) {
2457 sta_priv = (void *)sta->drv_priv;
2458 /* avoid atomic ops if this isn't a client */
2459 if (sta_priv->client &&
2460 atomic_dec_return(&sta_priv->pending_frames) == 0)
2461 ieee80211_sta_block_awake(il->hw, sta, false);
2462 }
2463 rcu_read_unlock();
2464}
2465
2466static void
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002467il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info, bool is_agg)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002468{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002469 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002470
2471 if (!is_agg)
2472 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2473
2474 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2475}
2476
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002477int
2478il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002479{
2480 struct il_tx_queue *txq = &il->txq[txq_id];
2481 struct il_queue *q = &txq->q;
2482 struct il_tx_info *tx_info;
2483 int nfreed = 0;
2484 struct ieee80211_hdr *hdr;
2485
2486 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2487 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002488 "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
2489 q->write_ptr, q->read_ptr);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002490 return 0;
2491 }
2492
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002493 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002494 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2495
2496 tx_info = &txq->txb[txq->q.read_ptr];
2497
2498 if (WARN_ON_ONCE(tx_info->skb == NULL))
2499 continue;
2500
2501 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2502 if (ieee80211_is_data_qos(hdr->frame_control))
2503 nfreed++;
2504
2505 il4965_tx_status(il, tx_info,
2506 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2507 tx_info->skb = NULL;
2508
2509 il->cfg->ops->lib->txq_free_tfd(il, txq);
2510 }
2511 return nfreed;
2512}
2513
2514/**
2515 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2516 *
2517 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2518 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2519 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002520static int
2521il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
2522 struct il_compressed_ba_resp *ba_resp)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002523{
2524 int i, sh, ack;
2525 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2526 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2527 int successes = 0;
2528 struct ieee80211_tx_info *info;
2529 u64 bitmap, sent_bitmap;
2530
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002531 if (unlikely(!agg->wait_for_ba)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002532 if (unlikely(ba_resp->bitmap))
2533 IL_ERR("Received BA when not expected\n");
2534 return -EINVAL;
2535 }
2536
2537 /* Mark that the expected block-ack response arrived */
2538 agg->wait_for_ba = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002539 D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002540
2541 /* Calculate shift to align block-ack bits with our Tx win bits */
2542 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002543 if (sh < 0) /* tbw something is wrong with indices */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002544 sh += 0x100;
2545
2546 if (agg->frame_count > (64 - sh)) {
2547 D_TX_REPLY("more frames than bitmap size");
2548 return -1;
2549 }
2550
2551 /* don't use 64-bit values for now */
2552 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2553
2554 /* check for success or failure according to the
2555 * transmitted bitmap and block-ack bitmap */
2556 sent_bitmap = bitmap & agg->bitmap;
2557
2558 /* For each frame attempted in aggregation,
2559 * update driver's record of tx frame's status. */
2560 i = 0;
2561 while (sent_bitmap) {
2562 ack = sent_bitmap & 1ULL;
2563 successes += ack;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002564 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
2565 i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002566 sent_bitmap >>= 1;
2567 ++i;
2568 }
2569
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002570 D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002571
2572 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2573 memset(&info->status, 0, sizeof(info->status));
2574 info->flags |= IEEE80211_TX_STAT_ACK;
2575 info->flags |= IEEE80211_TX_STAT_AMPDU;
2576 info->status.ampdu_ack_len = successes;
2577 info->status.ampdu_len = agg->frame_count;
2578 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2579
2580 return 0;
2581}
2582
2583/**
2584 * translate ucode response to mac80211 tx status control values
2585 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002586void
2587il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2588 struct ieee80211_tx_info *info)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002589{
2590 struct ieee80211_tx_rate *r = &info->control.rates[0];
2591
2592 info->antenna_sel_tx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002593 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002594 if (rate_n_flags & RATE_MCS_HT_MSK)
2595 r->flags |= IEEE80211_TX_RC_MCS;
2596 if (rate_n_flags & RATE_MCS_GF_MSK)
2597 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2598 if (rate_n_flags & RATE_MCS_HT40_MSK)
2599 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2600 if (rate_n_flags & RATE_MCS_DUP_MSK)
2601 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2602 if (rate_n_flags & RATE_MCS_SGI_MSK)
2603 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2604 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2605}
2606
2607/**
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02002608 * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002609 *
2610 * Handles block-acknowledge notification from device, which reports success
2611 * of frames sent via aggregation.
2612 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002613void
2614il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002615{
2616 struct il_rx_pkt *pkt = rxb_addr(rxb);
2617 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2618 struct il_tx_queue *txq = NULL;
2619 struct il_ht_agg *agg;
2620 int idx;
2621 int sta_id;
2622 int tid;
2623 unsigned long flags;
2624
2625 /* "flow" corresponds to Tx queue */
2626 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2627
2628 /* "ssn" is start of block-ack Tx win, corresponds to idx
2629 * (in Tx queue's circular buffer) of first TFD/frame in win */
2630 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2631
2632 if (scd_flow >= il->hw_params.max_txq_num) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002633 IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002634 return;
2635 }
2636
2637 txq = &il->txq[scd_flow];
2638 sta_id = ba_resp->sta_id;
2639 tid = ba_resp->tid;
2640 agg = &il->stations[sta_id].tid[tid].agg;
2641 if (unlikely(agg->txq_id != scd_flow)) {
2642 /*
2643 * FIXME: this is a uCode bug which need to be addressed,
2644 * log the information and return for now!
2645 * since it is possible happen very often and in order
2646 * not to fill the syslog, don't enable the logging by default
2647 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002648 D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
2649 scd_flow, agg->txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002650 return;
2651 }
2652
2653 /* Find idx just before block-ack win */
2654 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2655
2656 spin_lock_irqsave(&il->sta_lock, flags);
2657
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002658 D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002659 agg->wait_for_ba, (u8 *) &ba_resp->sta_addr_lo32,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002660 ba_resp->sta_id);
2661 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
2662 "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
2663 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2664 ba_resp->scd_flow, ba_resp->scd_ssn);
2665 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
2666 (unsigned long long)agg->bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002667
2668 /* Update driver's record of ACK vs. not for each frame in win */
2669 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2670
2671 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2672 * block-ack win (we assume that they've been successfully
2673 * transmitted ... if not, it's too late anyway). */
2674 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2675 /* calculate mac80211 ampdu sw queue to wake */
2676 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2677 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2678
2679 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2680 il->mac80211_registered &&
2681 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2682 il_wake_queue(il, txq);
2683
2684 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2685 }
2686
2687 spin_unlock_irqrestore(&il->sta_lock, flags);
2688}
2689
2690#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002691const char *
2692il4965_get_tx_fail_reason(u32 status)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002693{
2694#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2695#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2696
2697 switch (status & TX_STATUS_MSK) {
2698 case TX_STATUS_SUCCESS:
2699 return "SUCCESS";
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002700 TX_STATUS_POSTPONE(DELAY);
2701 TX_STATUS_POSTPONE(FEW_BYTES);
2702 TX_STATUS_POSTPONE(QUIET_PERIOD);
2703 TX_STATUS_POSTPONE(CALC_TTAK);
2704 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2705 TX_STATUS_FAIL(SHORT_LIMIT);
2706 TX_STATUS_FAIL(LONG_LIMIT);
2707 TX_STATUS_FAIL(FIFO_UNDERRUN);
2708 TX_STATUS_FAIL(DRAIN_FLOW);
2709 TX_STATUS_FAIL(RFKILL_FLUSH);
2710 TX_STATUS_FAIL(LIFE_EXPIRE);
2711 TX_STATUS_FAIL(DEST_PS);
2712 TX_STATUS_FAIL(HOST_ABORTED);
2713 TX_STATUS_FAIL(BT_RETRY);
2714 TX_STATUS_FAIL(STA_INVALID);
2715 TX_STATUS_FAIL(FRAG_DROPPED);
2716 TX_STATUS_FAIL(TID_DISABLE);
2717 TX_STATUS_FAIL(FIFO_FLUSHED);
2718 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2719 TX_STATUS_FAIL(PASSIVE_NO_RX);
2720 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002721 }
2722
2723 return "UNKNOWN";
2724
2725#undef TX_STATUS_FAIL
2726#undef TX_STATUS_POSTPONE
2727}
2728#endif /* CONFIG_IWLEGACY_DEBUG */
2729
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002730static struct il_link_quality_cmd *
2731il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2732{
2733 int i, r;
2734 struct il_link_quality_cmd *link_cmd;
2735 u32 rate_flags = 0;
2736 __le32 rate_n_flags;
2737
2738 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2739 if (!link_cmd) {
2740 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2741 return NULL;
2742 }
2743 /* Set up the rate scaling to start at selected rate, fall back
2744 * all the way down to 1M in IEEE order, and then spin on 1M */
2745 if (il->band == IEEE80211_BAND_5GHZ)
2746 r = RATE_6M_IDX;
2747 else
2748 r = RATE_1M_IDX;
2749
2750 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2751 rate_flags |= RATE_MCS_CCK_MSK;
2752
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002753 rate_flags |=
2754 il4965_first_antenna(il->hw_params.
2755 valid_tx_ant) << RATE_MCS_ANT_POS;
2756 rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp, rate_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002757 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2758 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2759
2760 link_cmd->general_params.single_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002761 il4965_first_antenna(il->hw_params.valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002762
2763 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002764 il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
2765 valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002766 if (!link_cmd->general_params.dual_stream_ant_msk) {
2767 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2768 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2769 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002770 il->hw_params.valid_tx_ant;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002771 }
2772
2773 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2774 link_cmd->agg_params.agg_time_limit =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002775 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002776
2777 link_cmd->sta_id = sta_id;
2778
2779 return link_cmd;
2780}
2781
2782/*
2783 * il4965_add_bssid_station - Add the special IBSS BSSID station
2784 *
2785 * Function sleeps.
2786 */
2787int
2788il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002789 const u8 *addr, u8 *sta_id_r)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002790{
2791 int ret;
2792 u8 sta_id;
2793 struct il_link_quality_cmd *link_cmd;
2794 unsigned long flags;
2795
2796 if (sta_id_r)
2797 *sta_id_r = IL_INVALID_STATION;
2798
2799 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2800 if (ret) {
2801 IL_ERR("Unable to add station %pM\n", addr);
2802 return ret;
2803 }
2804
2805 if (sta_id_r)
2806 *sta_id_r = sta_id;
2807
2808 spin_lock_irqsave(&il->sta_lock, flags);
2809 il->stations[sta_id].used |= IL_STA_LOCAL;
2810 spin_unlock_irqrestore(&il->sta_lock, flags);
2811
2812 /* Set up default rate scaling table in device's station table */
2813 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2814 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002815 IL_ERR("Unable to initialize rate scaling for station %pM.\n",
2816 addr);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002817 return -ENOMEM;
2818 }
2819
2820 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2821 if (ret)
2822 IL_ERR("Link quality command failed (%d)\n", ret);
2823
2824 spin_lock_irqsave(&il->sta_lock, flags);
2825 il->stations[sta_id].lq = link_cmd;
2826 spin_unlock_irqrestore(&il->sta_lock, flags);
2827
2828 return 0;
2829}
2830
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002831static int
2832il4965_static_wepkey_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2833 bool send_if_empty)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002834{
2835 int i, not_empty = 0;
2836 u8 buff[sizeof(struct il_wep_cmd) +
2837 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2838 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002839 size_t cmd_size = sizeof(struct il_wep_cmd);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002840 struct il_host_cmd cmd = {
2841 .id = ctx->wep_key_cmd,
2842 .data = wep_cmd,
2843 .flags = CMD_SYNC,
2844 };
2845
2846 might_sleep();
2847
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002848 memset(wep_cmd, 0,
2849 cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002850
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002851 for (i = 0; i < WEP_KEYS_MAX; i++) {
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002852 wep_cmd->key[i].key_idx = i;
2853 if (ctx->wep_keys[i].key_size) {
2854 wep_cmd->key[i].key_offset = i;
2855 not_empty = 1;
2856 } else {
2857 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2858 }
2859
2860 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
2861 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002862 ctx->wep_keys[i].key_size);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002863 }
2864
2865 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2866 wep_cmd->num_keys = WEP_KEYS_MAX;
2867
2868 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
2869
2870 cmd.len = cmd_size;
2871
2872 if (not_empty || send_if_empty)
2873 return il_send_cmd(il, &cmd);
2874 else
2875 return 0;
2876}
2877
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002878int
2879il4965_restore_default_wep_keys(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002880{
2881 lockdep_assert_held(&il->mutex);
2882
2883 return il4965_static_wepkey_cmd(il, ctx, false);
2884}
2885
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002886int
2887il4965_remove_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2888 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002889{
2890 int ret;
2891
2892 lockdep_assert_held(&il->mutex);
2893
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002894 D_WEP("Removing default WEP key: idx=%d\n", keyconf->keyidx);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002895
2896 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
2897 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002898 D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002899 /* but keys in device are clear anyway so return success */
2900 return 0;
2901 }
2902 ret = il4965_static_wepkey_cmd(il, ctx, 1);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002903 D_WEP("Remove default WEP key: idx=%d ret=%d\n", keyconf->keyidx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002904
2905 return ret;
2906}
2907
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002908int
2909il4965_set_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2910 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002911{
2912 int ret;
2913
2914 lockdep_assert_held(&il->mutex);
2915
2916 if (keyconf->keylen != WEP_KEY_LEN_128 &&
2917 keyconf->keylen != WEP_KEY_LEN_64) {
2918 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2919 return -EINVAL;
2920 }
2921
2922 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2923 keyconf->hw_key_idx = HW_KEY_DEFAULT;
2924 il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
2925
2926 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
2927 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002928 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002929
2930 ret = il4965_static_wepkey_cmd(il, ctx, false);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002931 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", keyconf->keylen,
2932 keyconf->keyidx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002933
2934 return ret;
2935}
2936
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002937static int
2938il4965_set_wep_dynamic_key_info(struct il_priv *il, struct il_rxon_context *ctx,
2939 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002940{
2941 unsigned long flags;
2942 __le16 key_flags = 0;
2943 struct il_addsta_cmd sta_cmd;
2944
2945 lockdep_assert_held(&il->mutex);
2946
2947 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2948
2949 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2950 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2951 key_flags &= ~STA_KEY_FLG_INVALID;
2952
2953 if (keyconf->keylen == WEP_KEY_LEN_128)
2954 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2955
2956 if (sta_id == ctx->bcast_sta_id)
2957 key_flags |= STA_KEY_MULTICAST_MSK;
2958
2959 spin_lock_irqsave(&il->sta_lock, flags);
2960
2961 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2962 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2963 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2964
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002965 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002966
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002967 memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
2968 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002969
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002970 if ((il->stations[sta_id].sta.key.
2971 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002972 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002973 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002974 /* else, we are overriding an existing key => no need to allocated room
2975 * in uCode. */
2976
2977 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002978 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002979
2980 il->stations[sta_id].sta.key.key_flags = key_flags;
2981 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
2982 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2983
2984 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002985 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002986 spin_unlock_irqrestore(&il->sta_lock, flags);
2987
2988 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2989}
2990
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002991static int
2992il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
2993 struct il_rxon_context *ctx,
2994 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002995{
2996 unsigned long flags;
2997 __le16 key_flags = 0;
2998 struct il_addsta_cmd sta_cmd;
2999
3000 lockdep_assert_held(&il->mutex);
3001
3002 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
3003 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3004 key_flags &= ~STA_KEY_FLG_INVALID;
3005
3006 if (sta_id == ctx->bcast_sta_id)
3007 key_flags |= STA_KEY_MULTICAST_MSK;
3008
3009 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3010
3011 spin_lock_irqsave(&il->sta_lock, flags);
3012 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3013 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
3014
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003015 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003016
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003017 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003018
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003019 if ((il->stations[sta_id].sta.key.
3020 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003021 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003022 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003023 /* else, we are overriding an existing key => no need to allocated room
3024 * in uCode. */
3025
3026 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003027 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003028
3029 il->stations[sta_id].sta.key.key_flags = key_flags;
3030 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3031 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3032
3033 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003034 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003035 spin_unlock_irqrestore(&il->sta_lock, flags);
3036
3037 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3038}
3039
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003040static int
3041il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3042 struct il_rxon_context *ctx,
3043 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003044{
3045 unsigned long flags;
3046 int ret = 0;
3047 __le16 key_flags = 0;
3048
3049 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3050 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3051 key_flags &= ~STA_KEY_FLG_INVALID;
3052
3053 if (sta_id == ctx->bcast_sta_id)
3054 key_flags |= STA_KEY_MULTICAST_MSK;
3055
3056 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3057 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3058
3059 spin_lock_irqsave(&il->sta_lock, flags);
3060
3061 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3062 il->stations[sta_id].keyinfo.keylen = 16;
3063
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003064 if ((il->stations[sta_id].sta.key.
3065 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003066 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003067 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003068 /* else, we are overriding an existing key => no need to allocated room
3069 * in uCode. */
3070
3071 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003072 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003073
3074 il->stations[sta_id].sta.key.key_flags = key_flags;
3075
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003076 /* This copy is acutally not needed: we get the key with each TX */
3077 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3078
3079 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3080
3081 spin_unlock_irqrestore(&il->sta_lock, flags);
3082
3083 return ret;
3084}
3085
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003086void
3087il4965_update_tkip_key(struct il_priv *il, struct il_rxon_context *ctx,
3088 struct ieee80211_key_conf *keyconf,
3089 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003090{
3091 u8 sta_id;
3092 unsigned long flags;
3093 int i;
3094
3095 if (il_scan_cancel(il)) {
3096 /* cancel scan failed, just live w/ bad key and rely
3097 briefly on SW decryption */
3098 return;
3099 }
3100
3101 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3102 if (sta_id == IL_INVALID_STATION)
3103 return;
3104
3105 spin_lock_irqsave(&il->sta_lock, flags);
3106
3107 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3108
3109 for (i = 0; i < 5; i++)
3110 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003111 cpu_to_le16(phase1key[i]);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003112
3113 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3114 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3115
3116 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3117
3118 spin_unlock_irqrestore(&il->sta_lock, flags);
3119
3120}
3121
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003122int
3123il4965_remove_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3124 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003125{
3126 unsigned long flags;
3127 u16 key_flags;
3128 u8 keyidx;
3129 struct il_addsta_cmd sta_cmd;
3130
3131 lockdep_assert_held(&il->mutex);
3132
3133 ctx->key_mapping_keys--;
3134
3135 spin_lock_irqsave(&il->sta_lock, flags);
3136 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3137 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3138
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003139 D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003140
3141 if (keyconf->keyidx != keyidx) {
3142 /* We need to remove a key with idx different that the one
3143 * in the uCode. This means that the key we need to remove has
3144 * been replaced by another one with different idx.
3145 * Don't do anything and return ok
3146 */
3147 spin_unlock_irqrestore(&il->sta_lock, flags);
3148 return 0;
3149 }
3150
3151 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003152 IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
3153 key_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003154 spin_unlock_irqrestore(&il->sta_lock, flags);
3155 return 0;
3156 }
3157
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003158 if (!test_and_clear_bit
3159 (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003160 IL_ERR("idx %d not used in uCode key table.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003161 il->stations[sta_id].sta.key.key_offset);
3162 memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
3163 memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003164 il->stations[sta_id].sta.key.key_flags =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003165 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003166 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3167 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3168 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3169
3170 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003171 D_WEP
3172 ("Not sending C_ADD_STA command because RFKILL enabled.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003173 spin_unlock_irqrestore(&il->sta_lock, flags);
3174 return 0;
3175 }
3176 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003177 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003178 spin_unlock_irqrestore(&il->sta_lock, flags);
3179
3180 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3181}
3182
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003183int
3184il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3185 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003186{
3187 int ret;
3188
3189 lockdep_assert_held(&il->mutex);
3190
3191 ctx->key_mapping_keys++;
3192 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3193
3194 switch (keyconf->cipher) {
3195 case WLAN_CIPHER_SUITE_CCMP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003196 ret =
3197 il4965_set_ccmp_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003198 break;
3199 case WLAN_CIPHER_SUITE_TKIP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003200 ret =
3201 il4965_set_tkip_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003202 break;
3203 case WLAN_CIPHER_SUITE_WEP40:
3204 case WLAN_CIPHER_SUITE_WEP104:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003205 ret = il4965_set_wep_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003206 break;
3207 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003208 IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
3209 keyconf->cipher);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003210 ret = -EINVAL;
3211 }
3212
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003213 D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3214 keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003215
3216 return ret;
3217}
3218
3219/**
3220 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3221 *
3222 * This adds the broadcast station into the driver's station table
3223 * and marks it driver active, so that it will be restored to the
3224 * device at the next best time.
3225 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003226int
3227il4965_alloc_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003228{
3229 struct il_link_quality_cmd *link_cmd;
3230 unsigned long flags;
3231 u8 sta_id;
3232
3233 spin_lock_irqsave(&il->sta_lock, flags);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003234 sta_id = il_prep_station(il, ctx, il_bcast_addr, false, NULL);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003235 if (sta_id == IL_INVALID_STATION) {
3236 IL_ERR("Unable to prepare broadcast station\n");
3237 spin_unlock_irqrestore(&il->sta_lock, flags);
3238
3239 return -EINVAL;
3240 }
3241
3242 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3243 il->stations[sta_id].used |= IL_STA_BCAST;
3244 spin_unlock_irqrestore(&il->sta_lock, flags);
3245
3246 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3247 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003248 IL_ERR
3249 ("Unable to initialize rate scaling for bcast station.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003250 return -ENOMEM;
3251 }
3252
3253 spin_lock_irqsave(&il->sta_lock, flags);
3254 il->stations[sta_id].lq = link_cmd;
3255 spin_unlock_irqrestore(&il->sta_lock, flags);
3256
3257 return 0;
3258}
3259
3260/**
3261 * il4965_update_bcast_station - update broadcast station's LQ command
3262 *
3263 * Only used by iwl4965. Placed here to have all bcast station management
3264 * code together.
3265 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003266static int
3267il4965_update_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003268{
3269 unsigned long flags;
3270 struct il_link_quality_cmd *link_cmd;
3271 u8 sta_id = ctx->bcast_sta_id;
3272
3273 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3274 if (!link_cmd) {
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003275 IL_ERR("Unable to initialize rate scaling for bcast sta.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003276 return -ENOMEM;
3277 }
3278
3279 spin_lock_irqsave(&il->sta_lock, flags);
3280 if (il->stations[sta_id].lq)
3281 kfree(il->stations[sta_id].lq);
3282 else
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003283 D_INFO("Bcast sta rate scaling has not been initialized.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003284 il->stations[sta_id].lq = link_cmd;
3285 spin_unlock_irqrestore(&il->sta_lock, flags);
3286
3287 return 0;
3288}
3289
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003290int
3291il4965_update_bcast_stations(struct il_priv *il)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003292{
3293 return il4965_update_bcast_station(il, &il->ctx);
3294}
3295
3296/**
3297 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3298 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003299int
3300il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003301{
3302 unsigned long flags;
3303 struct il_addsta_cmd sta_cmd;
3304
3305 lockdep_assert_held(&il->mutex);
3306
3307 /* Remove "disable" flag, to enable Tx for this TID */
3308 spin_lock_irqsave(&il->sta_lock, flags);
3309 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3310 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3311 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3312 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003313 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003314 spin_unlock_irqrestore(&il->sta_lock, flags);
3315
3316 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3317}
3318
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003319int
3320il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
3321 u16 ssn)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003322{
3323 unsigned long flags;
3324 int sta_id;
3325 struct il_addsta_cmd sta_cmd;
3326
3327 lockdep_assert_held(&il->mutex);
3328
3329 sta_id = il_sta_id(sta);
3330 if (sta_id == IL_INVALID_STATION)
3331 return -ENXIO;
3332
3333 spin_lock_irqsave(&il->sta_lock, flags);
3334 il->stations[sta_id].sta.station_flags_msk = 0;
3335 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003336 il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003337 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3338 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3339 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003340 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003341 spin_unlock_irqrestore(&il->sta_lock, flags);
3342
3343 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3344}
3345
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003346int
3347il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003348{
3349 unsigned long flags;
3350 int sta_id;
3351 struct il_addsta_cmd sta_cmd;
3352
3353 lockdep_assert_held(&il->mutex);
3354
3355 sta_id = il_sta_id(sta);
3356 if (sta_id == IL_INVALID_STATION) {
3357 IL_ERR("Invalid station for AGG tid %d\n", tid);
3358 return -ENXIO;
3359 }
3360
3361 spin_lock_irqsave(&il->sta_lock, flags);
3362 il->stations[sta_id].sta.station_flags_msk = 0;
3363 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003364 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003365 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3366 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003367 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003368 spin_unlock_irqrestore(&il->sta_lock, flags);
3369
3370 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3371}
3372
3373void
3374il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3375{
3376 unsigned long flags;
3377
3378 spin_lock_irqsave(&il->sta_lock, flags);
3379 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3380 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3381 il->stations[sta_id].sta.sta.modify_mask =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003382 STA_MODIFY_SLEEP_TX_COUNT_MSK;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003383 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3384 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003385 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003386 spin_unlock_irqrestore(&il->sta_lock, flags);
3387
3388}
3389
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003390void
3391il4965_update_chain_flags(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003392{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003393 if (il->cfg->ops->hcmd->set_rxon_chain) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003394 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
3395 if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
3396 il_commit_rxon(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003397 }
3398}
3399
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003400static void
3401il4965_clear_free_frames(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003402{
3403 struct list_head *element;
3404
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003405 D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003406
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003407 while (!list_empty(&il->free_frames)) {
3408 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003409 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003410 kfree(list_entry(element, struct il_frame, list));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003411 il->frames_count--;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003412 }
3413
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003414 if (il->frames_count) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003415 IL_WARN("%d frames still in use. Did we lose one?\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003416 il->frames_count);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003417 il->frames_count = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003418 }
3419}
3420
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003421static struct il_frame *
3422il4965_get_free_frame(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003423{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003424 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003425 struct list_head *element;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003426 if (list_empty(&il->free_frames)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003427 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3428 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003429 IL_ERR("Could not allocate frame!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003430 return NULL;
3431 }
3432
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003433 il->frames_count++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003434 return frame;
3435 }
3436
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003437 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003438 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003439 return list_entry(element, struct il_frame, list);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003440}
3441
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003442static void
3443il4965_free_frame(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003444{
3445 memset(frame, 0, sizeof(*frame));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003446 list_add(&frame->list, &il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003447}
3448
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003449static u32
3450il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
3451 int left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003452{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003453 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003454
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003455 if (!il->beacon_skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003456 return 0;
3457
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003458 if (il->beacon_skb->len > left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003459 return 0;
3460
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003461 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003462
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003463 return il->beacon_skb->len;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003464}
3465
3466/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003467static void
3468il4965_set_beacon_tim(struct il_priv *il,
3469 struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
3470 u32 frame_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003471{
3472 u16 tim_idx;
3473 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3474
3475 /*
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003476 * The idx is relative to frame start but we start looking at the
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003477 * variable-length part of the beacon.
3478 */
3479 tim_idx = mgmt->u.beacon.variable - beacon;
3480
3481 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3482 while ((tim_idx < (frame_size - 2)) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003483 (beacon[tim_idx] != WLAN_EID_TIM))
3484 tim_idx += beacon[tim_idx + 1] + 2;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003485
3486 /* If TIM field was found, set variables */
3487 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3488 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003489 tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003490 } else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003491 IL_WARN("Unable to find TIM Element in beacon\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003492}
3493
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003494static unsigned int
3495il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003496{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003497 struct il_tx_beacon_cmd *tx_beacon_cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003498 u32 frame_size;
3499 u32 rate_flags;
3500 u32 rate;
3501 /*
3502 * We have to set up the TX command, the TX Beacon command, and the
3503 * beacon contents.
3504 */
3505
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003506 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003507
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003508 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003509 IL_ERR("trying to build beacon w/o beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003510 return 0;
3511 }
3512
3513 /* Initialize memory */
3514 tx_beacon_cmd = &frame->u.beacon;
3515 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3516
3517 /* Set up TX beacon contents */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003518 frame_size =
3519 il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
3520 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003521 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3522 return 0;
3523 if (!frame_size)
3524 return 0;
3525
3526 /* Set up TX command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003527 tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003528 tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003529 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003530 tx_beacon_cmd->tx.tx_flags =
3531 TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
3532 TX_CMD_FLG_STA_RATE_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003533
3534 /* Set up TX beacon command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003535 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
3536 frame_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003537
3538 /* Set up packet rate and flags */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003539 rate = il_get_lowest_plcp(il, il->beacon_ctx);
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +01003540 il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003541 rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003542 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003543 rate_flags |= RATE_MCS_CCK_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003544 tx_beacon_cmd->tx.rate_n_flags =
3545 il4965_hw_set_rate_n_flags(rate, rate_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003546
3547 return sizeof(*tx_beacon_cmd) + frame_size;
3548}
3549
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003550int
3551il4965_send_beacon_cmd(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003552{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003553 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003554 unsigned int frame_size;
3555 int rc;
3556
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003557 frame = il4965_get_free_frame(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003558 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003559 IL_ERR("Could not obtain free frame buffer for beacon "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003560 "command.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003561 return -ENOMEM;
3562 }
3563
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003564 frame_size = il4965_hw_get_beacon_cmd(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003565 if (!frame_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003566 IL_ERR("Error configuring the beacon command\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003567 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003568 return -EINVAL;
3569 }
3570
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003571 rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003572
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003573 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003574
3575 return rc;
3576}
3577
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003578static inline dma_addr_t
3579il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003580{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003581 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003582
3583 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3584 if (sizeof(dma_addr_t) > sizeof(u32))
3585 addr |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003586 ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
3587 16;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003588
3589 return addr;
3590}
3591
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003592static inline u16
3593il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003594{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003595 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003596
3597 return le16_to_cpu(tb->hi_n_len) >> 4;
3598}
3599
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003600static inline void
3601il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003602{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003603 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003604 u16 hi_n_len = len << 4;
3605
3606 put_unaligned_le32(addr, &tb->lo);
3607 if (sizeof(dma_addr_t) > sizeof(u32))
3608 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3609
3610 tb->hi_n_len = cpu_to_le16(hi_n_len);
3611
3612 tfd->num_tbs = idx + 1;
3613}
3614
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003615static inline u8
3616il4965_tfd_get_num_tbs(struct il_tfd *tfd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003617{
3618 return tfd->num_tbs & 0x1f;
3619}
3620
3621/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003622 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003623 * @il - driver ilate data
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003624 * @txq - tx queue
3625 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003626 * Does NOT advance any TFD circular buffer read/write idxes
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003627 * Does NOT free the TFD itself (which is within circular buffer)
3628 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003629void
3630il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003631{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003632 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3633 struct il_tfd *tfd;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003634 struct pci_dev *dev = il->pci_dev;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003635 int idx = txq->q.read_ptr;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003636 int i;
3637 int num_tbs;
3638
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003639 tfd = &tfd_tmp[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003640
3641 /* Sanity check on number of chunks */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003642 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003643
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003644 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003645 IL_ERR("Too many chunks: %i\n", num_tbs);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003646 /* @todo issue fatal error, it is quite serious situation */
3647 return;
3648 }
3649
3650 /* Unmap tx_cmd */
3651 if (num_tbs)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003652 pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
3653 dma_unmap_len(&txq->meta[idx], len),
3654 PCI_DMA_BIDIRECTIONAL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003655
3656 /* Unmap chunks, if any. */
3657 for (i = 1; i < num_tbs; i++)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003658 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003659 il4965_tfd_tb_get_len(tfd, i),
3660 PCI_DMA_TODEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003661
3662 /* free SKB */
3663 if (txq->txb) {
3664 struct sk_buff *skb;
3665
3666 skb = txq->txb[txq->q.read_ptr].skb;
3667
3668 /* can be called from irqs-disabled context */
3669 if (skb) {
3670 dev_kfree_skb_any(skb);
3671 txq->txb[txq->q.read_ptr].skb = NULL;
3672 }
3673 }
3674}
3675
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003676int
3677il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
3678 dma_addr_t addr, u16 len, u8 reset, u8 pad)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003679{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003680 struct il_queue *q;
3681 struct il_tfd *tfd, *tfd_tmp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003682 u32 num_tbs;
3683
3684 q = &txq->q;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003685 tfd_tmp = (struct il_tfd *)txq->tfds;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003686 tfd = &tfd_tmp[q->write_ptr];
3687
3688 if (reset)
3689 memset(tfd, 0, sizeof(*tfd));
3690
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003691 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003692
3693 /* Each TFD can point to a maximum 20 Tx buffers */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003694 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003695 IL_ERR("Error can not send more than %d chunks\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003696 IL_NUM_OF_TBS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003697 return -EINVAL;
3698 }
3699
3700 BUG_ON(addr & ~DMA_BIT_MASK(36));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003701 if (unlikely(addr & ~IL_TX_DMA_MASK))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003702 IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003703
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003704 il4965_tfd_set_tb(tfd, num_tbs, addr, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003705
3706 return 0;
3707}
3708
3709/*
3710 * Tell nic where to find circular buffer of Tx Frame Descriptors for
3711 * given Tx queue, and enable the DMA channel used for that queue.
3712 *
3713 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
3714 * channels supported in hardware.
3715 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003716int
3717il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003718{
3719 int txq_id = txq->q.id;
3720
3721 /* Circular buffer (TFD queue in DRAM) physical base address */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003722 il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003723
3724 return 0;
3725}
3726
3727/******************************************************************************
3728 *
3729 * Generic RX handler implementations
3730 *
3731 ******************************************************************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003732static void
3733il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003734{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003735 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003736 struct il_alive_resp *palive;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003737 struct delayed_work *pwork;
3738
3739 palive = &pkt->u.alive_frame;
3740
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003741 D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
3742 palive->is_valid, palive->ver_type, palive->ver_subtype);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003743
3744 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003745 D_INFO("Initialization Alive received.\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003746 memcpy(&il->card_alive_init, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003747 sizeof(struct il_init_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003748 pwork = &il->init_alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003749 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003750 D_INFO("Runtime Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003751 memcpy(&il->card_alive, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003752 sizeof(struct il_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003753 pwork = &il->alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003754 }
3755
3756 /* We delay the ALIVE response by 5ms to
3757 * give the HW RF Kill time to activate... */
3758 if (palive->is_valid == UCODE_VALID_OK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003759 queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003760 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003761 IL_WARN("uCode did not respond OK.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003762}
3763
3764/**
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003765 * il4965_bg_stats_periodic - Timer callback to queue stats
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003766 *
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003767 * This callback is provided in order to send a stats request.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003768 *
3769 * This timer function is continually reset to execute within
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02003770 * REG_RECALIB_PERIOD seconds since the last N_STATS
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003771 * was received. We need to ensure we receive the stats in order
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003772 * to update the temperature used for calibrating the TXPOWER.
3773 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003774static void
3775il4965_bg_stats_periodic(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003776{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003777 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003778
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003779 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003780 return;
3781
3782 /* dont send host command if rf-kill is on */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003783 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003784 return;
3785
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003786 il_send_stats_request(il, CMD_ASYNC, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003787}
3788
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003789static void
3790il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003791{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003792 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003793 struct il4965_beacon_notif *beacon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003794 (struct il4965_beacon_notif *)pkt->u.raw;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003795#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003796 u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003797
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003798 D_RX("beacon status %x retries %d iss %d " "tsf %d %d rate %d\n",
3799 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
3800 beacon->beacon_notify_hdr.failure_frame,
3801 le32_to_cpu(beacon->ibss_mgr_status),
3802 le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003803#endif
3804
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003805 il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003806}
3807
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003808static void
3809il4965_perform_ct_kill_task(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003810{
3811 unsigned long flags;
3812
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003813 D_POWER("Stop all queues\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003814
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003815 if (il->mac80211_registered)
3816 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003817
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003818 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003819 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003820 _il_rd(il, CSR_UCODE_DRV_GP1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003821
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003822 spin_lock_irqsave(&il->reg_lock, flags);
Stanislaw Gruszka13882262011-08-24 15:39:23 +02003823 if (!_il_grab_nic_access(il))
3824 _il_release_nic_access(il);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003825 spin_unlock_irqrestore(&il->reg_lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003826}
3827
3828/* Handle notification from uCode that card's power state is changing
3829 * due to software, hardware, or critical temperature RFKILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003830static void
3831il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003832{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003833 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003834 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003835 unsigned long status = il->status;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003836
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003837 D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003838 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3839 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
3840 (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003841
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003842 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003843
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003844 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003845 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003846
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003847 il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003848
3849 if (!(flags & RXON_CARD_DISABLED)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003850 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003851 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003852 il_wr(il, HBUS_TARG_MBX_C,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003853 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003854 }
3855 }
3856
3857 if (flags & CT_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003858 il4965_perform_ct_kill_task(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003859
3860 if (flags & HW_CARD_DISABLED)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003861 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003862 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003863 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003864
3865 if (!(flags & RXON_CARD_DISABLED))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003866 il_scan_cancel(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003867
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003868 if ((test_bit(S_RF_KILL_HW, &status) !=
3869 test_bit(S_RF_KILL_HW, &il->status)))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003870 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003871 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003872 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003873 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003874}
3875
3876/**
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003877 * il4965_setup_handlers - Initialize Rx handler callbacks
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003878 *
3879 * Setup the RX handlers for each of the reply types sent from the uCode
3880 * to the host.
3881 *
3882 * This function chains into the hardware specific files for them to setup
3883 * any hardware specific handlers as well.
3884 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003885static void
3886il4965_setup_handlers(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003887{
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003888 il->handlers[N_ALIVE] = il4965_hdl_alive;
3889 il->handlers[N_ERROR] = il_hdl_error;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003890 il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003891 il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003892 il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003893 il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003894 il->handlers[N_BEACON] = il4965_hdl_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003895
3896 /*
3897 * The same handler is used for both the REPLY to a discrete
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003898 * stats request from the host as well as for the periodic
3899 * stats notifications (after received beacons) from the uCode.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003900 */
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003901 il->handlers[C_STATS] = il4965_hdl_c_stats;
3902 il->handlers[N_STATS] = il4965_hdl_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003903
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003904 il_setup_rx_scan_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003905
3906 /* status change handler */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003907 il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003908
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003909 il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003910 /* Rx handlers */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003911 il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
3912 il->handlers[N_RX_MPDU] = il4965_hdl_rx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003913 /* block ack */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003914 il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003915 /* Set up hardware specific Rx handlers */
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003916 il->cfg->ops->lib->handler_setup(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003917}
3918
3919/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003920 * il4965_rx_handle - Main entry function for receiving responses from uCode
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003921 *
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003922 * Uses the il->handlers callback function array to invoke
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003923 * the appropriate handlers, including command responses,
3924 * frame-received notifications, and other notifications.
3925 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003926void
3927il4965_rx_handle(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003928{
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003929 struct il_rx_buf *rxb;
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003930 struct il_rx_pkt *pkt;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003931 struct il_rx_queue *rxq = &il->rxq;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003932 u32 r, i;
3933 int reclaim;
3934 unsigned long flags;
3935 u8 fill_rx = 0;
3936 u32 count = 8;
3937 int total_empty;
3938
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003939 /* uCode's read idx (stored in shared DRAM) indicates the last Rx
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003940 * buffer that the driver may process (last buffer filled by ucode). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003941 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003942 i = rxq->read;
3943
3944 /* Rx interrupt, but nothing sent from uCode */
3945 if (i == r)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003946 D_RX("r = %d, i = %d\n", r, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003947
3948 /* calculate total frames need to be restock after handling RX */
3949 total_empty = r - rxq->write_actual;
3950 if (total_empty < 0)
3951 total_empty += RX_QUEUE_SIZE;
3952
3953 if (total_empty > (RX_QUEUE_SIZE / 2))
3954 fill_rx = 1;
3955
3956 while (i != r) {
3957 int len;
3958
3959 rxb = rxq->queue[i];
3960
3961 /* If an RXB doesn't have a Rx queue slot associated with it,
3962 * then a bug has been introduced in the queue refilling
3963 * routines -- catch it here */
3964 BUG_ON(rxb == NULL);
3965
3966 rxq->queue[i] = NULL;
3967
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003968 pci_unmap_page(il->pci_dev, rxb->page_dma,
3969 PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003970 PCI_DMA_FROMDEVICE);
3971 pkt = rxb_addr(rxb);
3972
Stanislaw Gruszkae94a4092011-08-31 13:23:20 +02003973 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003974 len += sizeof(u32); /* account for status word */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003975
3976 /* Reclaim a command buffer only if this packet is a response
3977 * to a (driver-originated) command.
3978 * If the packet (e.g. Rx frame) originated from uCode,
3979 * there is no command buffer to reclaim.
3980 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3981 * but apparently a few don't get set; catch them here. */
3982 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003983 (pkt->hdr.cmd != N_RX_PHY) && (pkt->hdr.cmd != N_RX) &&
3984 (pkt->hdr.cmd != N_RX_MPDU) &&
3985 (pkt->hdr.cmd != N_COMPRESSED_BA) &&
3986 (pkt->hdr.cmd != N_STATS) && (pkt->hdr.cmd != C_TX);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003987
3988 /* Based on type of command response or notification,
3989 * handle those that need handling via function in
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003990 * handlers table. See il4965_setup_handlers() */
3991 if (il->handlers[pkt->hdr.cmd]) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003992 D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
3993 il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003994 il->isr_stats.handlers[pkt->hdr.cmd]++;
3995 il->handlers[pkt->hdr.cmd] (il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003996 } else {
3997 /* No handling needed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003998 D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
3999 i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004000 }
4001
4002 /*
4003 * XXX: After here, we should always check rxb->page
4004 * against NULL before touching it or its virtual
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02004005 * memory (pkt). Because some handler might have
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004006 * already taken or freed the pages.
4007 */
4008
4009 if (reclaim) {
4010 /* Invoke any callbacks, transfer the buffer to caller,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004011 * and fire off the (possibly) blocking il_send_cmd()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004012 * as we reclaim the driver command queue */
4013 if (rxb->page)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004014 il_tx_cmd_complete(il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004015 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004016 IL_WARN("Claim null rxb?\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004017 }
4018
4019 /* Reuse the page if possible. For notification packets and
4020 * SKBs that fail to Rx correctly, add them back into the
4021 * rx_free list for reuse later. */
4022 spin_lock_irqsave(&rxq->lock, flags);
4023 if (rxb->page != NULL) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004024 rxb->page_dma =
4025 pci_map_page(il->pci_dev, rxb->page, 0,
4026 PAGE_SIZE << il->hw_params.
4027 rx_page_order, PCI_DMA_FROMDEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004028 list_add_tail(&rxb->list, &rxq->rx_free);
4029 rxq->free_count++;
4030 } else
4031 list_add_tail(&rxb->list, &rxq->rx_used);
4032
4033 spin_unlock_irqrestore(&rxq->lock, flags);
4034
4035 i = (i + 1) & RX_QUEUE_MASK;
4036 /* If there are a lot of unused frames,
4037 * restock the Rx queue so ucode wont assert. */
4038 if (fill_rx) {
4039 count++;
4040 if (count >= 8) {
4041 rxq->read = i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004042 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004043 count = 0;
4044 }
4045 }
4046 }
4047
4048 /* Backtrack one entry */
4049 rxq->read = i;
4050 if (fill_rx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004051 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004052 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004053 il4965_rx_queue_restock(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004054}
4055
4056/* call this function to flush any scheduled tasklet */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004057static inline void
4058il4965_synchronize_irq(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004059{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004060 /* wait to make sure we flush pending tasklet */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004061 synchronize_irq(il->pci_dev->irq);
4062 tasklet_kill(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004063}
4064
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004065static void
4066il4965_irq_tasklet(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004067{
4068 u32 inta, handled = 0;
4069 u32 inta_fh;
4070 unsigned long flags;
4071 u32 i;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004072#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004073 u32 inta_mask;
4074#endif
4075
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004076 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004077
4078 /* Ack/clear/reset pending uCode interrupts.
4079 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4080 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004081 inta = _il_rd(il, CSR_INT);
4082 _il_wr(il, CSR_INT, inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004083
4084 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4085 * Any new interrupts that happen after this, either while we're
4086 * in this tasklet, or later, will show up in next ISR/tasklet. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004087 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
4088 _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004089
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004090#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004091 if (il_get_debug_level(il) & IL_DL_ISR) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004092 /* just for debug */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004093 inta_mask = _il_rd(il, CSR_INT_MASK);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004094 D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
4095 inta_mask, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004096 }
4097#endif
4098
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004099 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004100
4101 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4102 * atomic, make sure that inta covers all the interrupts that
4103 * we've discovered, even if FH interrupt came in just after
4104 * reading CSR_INT. */
4105 if (inta_fh & CSR49_FH_INT_RX_MASK)
4106 inta |= CSR_INT_BIT_FH_RX;
4107 if (inta_fh & CSR49_FH_INT_TX_MASK)
4108 inta |= CSR_INT_BIT_FH_TX;
4109
4110 /* Now service all interrupt bits discovered above. */
4111 if (inta & CSR_INT_BIT_HW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004112 IL_ERR("Hardware error detected. Restarting.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004113
4114 /* Tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004115 il_disable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004116
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004117 il->isr_stats.hw++;
4118 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004119
4120 handled |= CSR_INT_BIT_HW_ERR;
4121
4122 return;
4123 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004124#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004125 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004126 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4127 if (inta & CSR_INT_BIT_SCD) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004128 D_ISR("Scheduler finished to transmit "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004129 "the frame/frames.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004130 il->isr_stats.sch++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004131 }
4132
4133 /* Alive notification via Rx interrupt will do the real work */
4134 if (inta & CSR_INT_BIT_ALIVE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004135 D_ISR("Alive interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004136 il->isr_stats.alive++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004137 }
4138 }
4139#endif
4140 /* Safely ignore these bits for debug checks below */
4141 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
4142
4143 /* HW RF KILL switch toggled */
4144 if (inta & CSR_INT_BIT_RF_KILL) {
4145 int hw_rf_kill = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004146 if (!
4147 (_il_rd(il, CSR_GP_CNTRL) &
4148 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004149 hw_rf_kill = 1;
4150
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004151 IL_WARN("RF_KILL bit toggled to %s.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004152 hw_rf_kill ? "disable radio" : "enable radio");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004153
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004154 il->isr_stats.rfkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004155
4156 /* driver only loads ucode once setting the interface up.
4157 * the driver allows loading the ucode even if the radio
4158 * is killed. Hence update the killswitch state here. The
4159 * rfkill handler will care about restarting if needed.
4160 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004161 if (!test_bit(S_ALIVE, &il->status)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004162 if (hw_rf_kill)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004163 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004164 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004165 clear_bit(S_RF_KILL_HW, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004166 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004167 }
4168
4169 handled |= CSR_INT_BIT_RF_KILL;
4170 }
4171
4172 /* Chip got too hot and stopped itself */
4173 if (inta & CSR_INT_BIT_CT_KILL) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004174 IL_ERR("Microcode CT kill error detected.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004175 il->isr_stats.ctkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004176 handled |= CSR_INT_BIT_CT_KILL;
4177 }
4178
4179 /* Error detected by uCode */
4180 if (inta & CSR_INT_BIT_SW_ERR) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004181 IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
4182 inta);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004183 il->isr_stats.sw++;
4184 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004185 handled |= CSR_INT_BIT_SW_ERR;
4186 }
4187
4188 /*
4189 * uCode wakes up after power-down sleep.
4190 * Tell device about any new tx or host commands enqueued,
4191 * and about any Rx buffers made available while asleep.
4192 */
4193 if (inta & CSR_INT_BIT_WAKEUP) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004194 D_ISR("Wakeup interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004195 il_rx_queue_update_write_ptr(il, &il->rxq);
4196 for (i = 0; i < il->hw_params.max_txq_num; i++)
4197 il_txq_update_write_ptr(il, &il->txq[i]);
4198 il->isr_stats.wakeup++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004199 handled |= CSR_INT_BIT_WAKEUP;
4200 }
4201
4202 /* All uCode command responses, including Tx command responses,
4203 * Rx "responses" (frame-received notification), and other
4204 * notifications from uCode come through here*/
4205 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004206 il4965_rx_handle(il);
4207 il->isr_stats.rx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004208 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4209 }
4210
4211 /* This "Tx" DMA channel is used only for loading uCode */
4212 if (inta & CSR_INT_BIT_FH_TX) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004213 D_ISR("uCode load interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004214 il->isr_stats.tx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004215 handled |= CSR_INT_BIT_FH_TX;
4216 /* Wake up uCode load routine, now that load is complete */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004217 il->ucode_write_complete = 1;
4218 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004219 }
4220
4221 if (inta & ~handled) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004222 IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004223 il->isr_stats.unhandled++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004224 }
4225
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004226 if (inta & ~(il->inta_mask)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004227 IL_WARN("Disabled INTA bits 0x%08x were pending\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004228 inta & ~il->inta_mask);
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004229 IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004230 }
4231
4232 /* Re-enable all interrupts */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02004233 /* only Re-enable if disabled by irq */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004234 if (test_bit(S_INT_ENABLED, &il->status))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004235 il_enable_interrupts(il);
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02004236 /* Re-enable RF_KILL if it occurred */
4237 else if (handled & CSR_INT_BIT_RF_KILL)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004238 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004239
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004240#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004241 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004242 inta = _il_rd(il, CSR_INT);
4243 inta_mask = _il_rd(il, CSR_INT_MASK);
4244 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004245 D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4246 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004247 }
4248#endif
4249}
4250
4251/*****************************************************************************
4252 *
4253 * sysfs attributes
4254 *
4255 *****************************************************************************/
4256
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004257#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004258
4259/*
4260 * The following adds a new attribute to the sysfs representation
4261 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
4262 * used for controlling the debug level.
4263 *
4264 * See the level definitions in iwl for details.
4265 *
4266 * The debug_level being managed using sysfs below is a per device debug
4267 * level that is used instead of the global debug level if it (the per
4268 * device debug level) is set.
4269 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004270static ssize_t
4271il4965_show_debug_level(struct device *d, struct device_attribute *attr,
4272 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004273{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004274 struct il_priv *il = dev_get_drvdata(d);
4275 return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004276}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004277
4278static ssize_t
4279il4965_store_debug_level(struct device *d, struct device_attribute *attr,
4280 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004281{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004282 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004283 unsigned long val;
4284 int ret;
4285
4286 ret = strict_strtoul(buf, 0, &val);
4287 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004288 IL_ERR("%s is not in hex or decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004289 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004290 il->debug_level = val;
4291 if (il_alloc_traffic_mem(il))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004292 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004293 }
4294 return strnlen(buf, count);
4295}
4296
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004297static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il4965_show_debug_level,
4298 il4965_store_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004299
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004300#endif /* CONFIG_IWLEGACY_DEBUG */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004301
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004302static ssize_t
4303il4965_show_temperature(struct device *d, struct device_attribute *attr,
4304 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004305{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004306 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004307
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004308 if (!il_is_alive(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004309 return -EAGAIN;
4310
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004311 return sprintf(buf, "%d\n", il->temperature);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004312}
4313
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004314static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004315
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004316static ssize_t
4317il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004318{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004319 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004320
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004321 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004322 return sprintf(buf, "off\n");
4323 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004324 return sprintf(buf, "%d\n", il->tx_power_user_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004325}
4326
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004327static ssize_t
4328il4965_store_tx_power(struct device *d, struct device_attribute *attr,
4329 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004330{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004331 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004332 unsigned long val;
4333 int ret;
4334
4335 ret = strict_strtoul(buf, 10, &val);
4336 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004337 IL_INFO("%s is not in decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004338 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004339 ret = il_set_tx_power(il, val, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004340 if (ret)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004341 IL_ERR("failed setting tx power (0x%d).\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004342 else
4343 ret = count;
4344 }
4345 return ret;
4346}
4347
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004348static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il4965_show_tx_power,
4349 il4965_store_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004350
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004351static struct attribute *il_sysfs_entries[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004352 &dev_attr_temperature.attr,
4353 &dev_attr_tx_power.attr,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004354#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004355 &dev_attr_debug_level.attr,
4356#endif
4357 NULL
4358};
4359
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004360static struct attribute_group il_attribute_group = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004361 .name = NULL, /* put in device directory */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004362 .attrs = il_sysfs_entries,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004363};
4364
4365/******************************************************************************
4366 *
4367 * uCode download functions
4368 *
4369 ******************************************************************************/
4370
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004371static void
4372il4965_dealloc_ucode_pci(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004373{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004374 il_free_fw_desc(il->pci_dev, &il->ucode_code);
4375 il_free_fw_desc(il->pci_dev, &il->ucode_data);
4376 il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
4377 il_free_fw_desc(il->pci_dev, &il->ucode_init);
4378 il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
4379 il_free_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004380}
4381
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004382static void
4383il4965_nic_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004384{
4385 /* Remove all resets to allow NIC to operate */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004386 _il_wr(il, CSR_RESET, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004387}
4388
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004389static void il4965_ucode_callback(const struct firmware *ucode_raw,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004390 void *context);
4391static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004392
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004393static int __must_check
4394il4965_request_firmware(struct il_priv *il, bool first)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004395{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004396 const char *name_pre = il->cfg->fw_name_pre;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004397 char tag[8];
4398
4399 if (first) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004400 il->fw_idx = il->cfg->ucode_api_max;
4401 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004402 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004403 il->fw_idx--;
4404 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004405 }
4406
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004407 if (il->fw_idx < il->cfg->ucode_api_min) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004408 IL_ERR("no suitable firmware found!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004409 return -ENOENT;
4410 }
4411
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004412 sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004413
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004414 D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004415
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004416 return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
4417 &il->pci_dev->dev, GFP_KERNEL, il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004418 il4965_ucode_callback);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004419}
4420
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004421struct il4965_firmware_pieces {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004422 const void *inst, *data, *init, *init_data, *boot;
4423 size_t inst_size, data_size, init_size, init_data_size, boot_size;
4424};
4425
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004426static int
4427il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
4428 struct il4965_firmware_pieces *pieces)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004429{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004430 struct il_ucode_header *ucode = (void *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004431 u32 api_ver, hdr_size;
4432 const u8 *src;
4433
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004434 il->ucode_ver = le32_to_cpu(ucode->ver);
4435 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004436
4437 switch (api_ver) {
4438 default:
4439 case 0:
4440 case 1:
4441 case 2:
4442 hdr_size = 24;
4443 if (ucode_raw->size < hdr_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004444 IL_ERR("File size too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004445 return -EINVAL;
4446 }
4447 pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
4448 pieces->data_size = le32_to_cpu(ucode->v1.data_size);
4449 pieces->init_size = le32_to_cpu(ucode->v1.init_size);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004450 pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004451 pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
4452 src = ucode->v1.data;
4453 break;
4454 }
4455
4456 /* Verify size of file vs. image size info in file's header */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004457 if (ucode_raw->size !=
4458 hdr_size + pieces->inst_size + pieces->data_size +
4459 pieces->init_size + pieces->init_data_size + pieces->boot_size) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004460
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004461 IL_ERR("uCode file size %d does not match expected size\n",
4462 (int)ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004463 return -EINVAL;
4464 }
4465
4466 pieces->inst = src;
4467 src += pieces->inst_size;
4468 pieces->data = src;
4469 src += pieces->data_size;
4470 pieces->init = src;
4471 src += pieces->init_size;
4472 pieces->init_data = src;
4473 src += pieces->init_data_size;
4474 pieces->boot = src;
4475 src += pieces->boot_size;
4476
4477 return 0;
4478}
4479
4480/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004481 * il4965_ucode_callback - callback when firmware was loaded
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004482 *
4483 * If loaded successfully, copies the firmware into buffers
4484 * for the card to fetch (via DMA).
4485 */
4486static void
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004487il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004488{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004489 struct il_priv *il = context;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004490 struct il_ucode_header *ucode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004491 int err;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004492 struct il4965_firmware_pieces pieces;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004493 const unsigned int api_max = il->cfg->ucode_api_max;
4494 const unsigned int api_min = il->cfg->ucode_api_min;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004495 u32 api_ver;
4496
4497 u32 max_probe_length = 200;
4498 u32 standard_phy_calibration_size =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004499 IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004500
4501 memset(&pieces, 0, sizeof(pieces));
4502
4503 if (!ucode_raw) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004504 if (il->fw_idx <= il->cfg->ucode_api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004505 IL_ERR("request for firmware file '%s' failed.\n",
4506 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004507 goto try_again;
4508 }
4509
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004510 D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
4511 ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004512
4513 /* Make sure that we got at least the API version number */
4514 if (ucode_raw->size < 4) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004515 IL_ERR("File size way too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004516 goto try_again;
4517 }
4518
4519 /* Data from ucode file: header followed by uCode images */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004520 ucode = (struct il_ucode_header *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004521
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004522 err = il4965_load_firmware(il, ucode_raw, &pieces);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004523
4524 if (err)
4525 goto try_again;
4526
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004527 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004528
4529 /*
4530 * api_ver should match the api version forming part of the
4531 * firmware filename ... but we don't check for that and only rely
4532 * on the API version read from firmware header from here on forward
4533 */
4534 if (api_ver < api_min || api_ver > api_max) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004535 IL_ERR("Driver unable to support your firmware API. "
4536 "Driver supports v%u, firmware is v%u.\n", api_max,
4537 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004538 goto try_again;
4539 }
4540
4541 if (api_ver != api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004542 IL_ERR("Firmware has old API version. Expected v%u, "
4543 "got v%u. New firmware can be obtained "
4544 "from http://www.intellinuxwireless.org.\n", api_max,
4545 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004546
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004547 IL_INFO("loaded firmware version %u.%u.%u.%u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004548 IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
4549 IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004550
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004551 snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
4552 "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
4553 IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004554 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004555
4556 /*
4557 * For any of the failures below (before allocating pci memory)
4558 * we will try to load a version with a smaller API -- maybe the
4559 * user just got a corrupted version of the latest API.
4560 */
4561
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004562 D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
4563 D_INFO("f/w package hdr runtime inst size = %Zd\n", pieces.inst_size);
4564 D_INFO("f/w package hdr runtime data size = %Zd\n", pieces.data_size);
4565 D_INFO("f/w package hdr init inst size = %Zd\n", pieces.init_size);
4566 D_INFO("f/w package hdr init data size = %Zd\n", pieces.init_data_size);
4567 D_INFO("f/w package hdr boot inst size = %Zd\n", pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004568
4569 /* Verify that uCode images will fit in card's SRAM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004570 if (pieces.inst_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004571 IL_ERR("uCode instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004572 pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004573 goto try_again;
4574 }
4575
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004576 if (pieces.data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004577 IL_ERR("uCode data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004578 pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004579 goto try_again;
4580 }
4581
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004582 if (pieces.init_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004583 IL_ERR("uCode init instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004584 pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004585 goto try_again;
4586 }
4587
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004588 if (pieces.init_data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004589 IL_ERR("uCode init data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004590 pieces.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004591 goto try_again;
4592 }
4593
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004594 if (pieces.boot_size > il->hw_params.max_bsm_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004595 IL_ERR("uCode boot instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004596 pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004597 goto try_again;
4598 }
4599
4600 /* Allocate ucode buffers for card's bus-master loading ... */
4601
4602 /* Runtime instructions and 2 copies of data:
4603 * 1) unmodified from disk
4604 * 2) backup cache for save/restore during power-downs */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004605 il->ucode_code.len = pieces.inst_size;
4606 il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004607
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004608 il->ucode_data.len = pieces.data_size;
4609 il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004610
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004611 il->ucode_data_backup.len = pieces.data_size;
4612 il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004613
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004614 if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
4615 !il->ucode_data_backup.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004616 goto err_pci_alloc;
4617
4618 /* Initialization instructions and data */
4619 if (pieces.init_size && pieces.init_data_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004620 il->ucode_init.len = pieces.init_size;
4621 il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004622
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004623 il->ucode_init_data.len = pieces.init_data_size;
4624 il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004625
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004626 if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004627 goto err_pci_alloc;
4628 }
4629
4630 /* Bootstrap (instructions only, no data) */
4631 if (pieces.boot_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004632 il->ucode_boot.len = pieces.boot_size;
4633 il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004634
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004635 if (!il->ucode_boot.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004636 goto err_pci_alloc;
4637 }
4638
4639 /* Now that we can no longer fail, copy information */
4640
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004641 il->sta_key_max_num = STA_KEY_MAX_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004642
4643 /* Copy images into buffers for card's bus-master reads ... */
4644
4645 /* Runtime instructions (first block of data in file) */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004646 D_INFO("Copying (but not loading) uCode instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004647 pieces.inst_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004648 memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004649
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004650 D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004651 il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004652
4653 /*
4654 * Runtime data
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004655 * NOTE: Copy into backup buffer will be done in il_up()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004656 */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004657 D_INFO("Copying (but not loading) uCode data len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004658 pieces.data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004659 memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
4660 memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004661
4662 /* Initialization instructions */
4663 if (pieces.init_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004664 D_INFO("Copying (but not loading) init instr len %Zd\n",
4665 pieces.init_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004666 memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004667 }
4668
4669 /* Initialization data */
4670 if (pieces.init_data_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004671 D_INFO("Copying (but not loading) init data len %Zd\n",
4672 pieces.init_data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004673 memcpy(il->ucode_init_data.v_addr, pieces.init_data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004674 pieces.init_data_size);
4675 }
4676
4677 /* Bootstrap instructions */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004678 D_INFO("Copying (but not loading) boot instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004679 pieces.boot_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004680 memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004681
4682 /*
4683 * figure out the offset of chain noise reset and gain commands
4684 * base on the size of standard phy calibration commands table size
4685 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004686 il->_4965.phy_calib_chain_noise_reset_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004687 standard_phy_calibration_size;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004688 il->_4965.phy_calib_chain_noise_gain_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004689 standard_phy_calibration_size + 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004690
4691 /**************************************************
4692 * This is still part of probe() in a sense...
4693 *
4694 * 9. Setup and register with mac80211 and debugfs
4695 **************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004696 err = il4965_mac_setup_register(il, max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004697 if (err)
4698 goto out_unbind;
4699
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004700 err = il_dbgfs_register(il, DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004701 if (err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004702 IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
4703 err);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004704
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004705 err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004706 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004707 IL_ERR("failed to create sysfs device attributes\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004708 goto out_unbind;
4709 }
4710
4711 /* We have our copies now, allow OS release its copies */
4712 release_firmware(ucode_raw);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004713 complete(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004714 return;
4715
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004716try_again:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004717 /* try next, if any */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004718 if (il4965_request_firmware(il, false))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004719 goto out_unbind;
4720 release_firmware(ucode_raw);
4721 return;
4722
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004723err_pci_alloc:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004724 IL_ERR("failed to allocate pci memory\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004725 il4965_dealloc_ucode_pci(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004726out_unbind:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004727 complete(&il->_4965.firmware_loading_complete);
4728 device_release_driver(&il->pci_dev->dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004729 release_firmware(ucode_raw);
4730}
4731
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004732static const char *const desc_lookup_text[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004733 "OK",
4734 "FAIL",
4735 "BAD_PARAM",
4736 "BAD_CHECKSUM",
4737 "NMI_INTERRUPT_WDG",
4738 "SYSASSERT",
4739 "FATAL_ERROR",
4740 "BAD_COMMAND",
4741 "HW_ERROR_TUNE_LOCK",
4742 "HW_ERROR_TEMPERATURE",
4743 "ILLEGAL_CHAN_FREQ",
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +02004744 "VCC_NOT_STBL",
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004745 "FH49_ERROR",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004746 "NMI_INTERRUPT_HOST",
4747 "NMI_INTERRUPT_ACTION_PT",
4748 "NMI_INTERRUPT_UNKNOWN",
4749 "UCODE_VERSION_MISMATCH",
4750 "HW_ERROR_ABS_LOCK",
4751 "HW_ERROR_CAL_LOCK_FAIL",
4752 "NMI_INTERRUPT_INST_ACTION_PT",
4753 "NMI_INTERRUPT_DATA_ACTION_PT",
4754 "NMI_TRM_HW_ER",
4755 "NMI_INTERRUPT_TRM",
Joe Perches861d9c32011-07-08 23:20:24 -07004756 "NMI_INTERRUPT_BREAK_POINT",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004757 "DEBUG_0",
4758 "DEBUG_1",
4759 "DEBUG_2",
4760 "DEBUG_3",
4761};
4762
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004763static struct {
4764 char *name;
4765 u8 num;
4766} advanced_lookup[] = {
4767 {
4768 "NMI_INTERRUPT_WDG", 0x34}, {
4769 "SYSASSERT", 0x35}, {
4770 "UCODE_VERSION_MISMATCH", 0x37}, {
4771 "BAD_COMMAND", 0x38}, {
4772 "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
4773 "FATAL_ERROR", 0x3D}, {
4774 "NMI_TRM_HW_ERR", 0x46}, {
4775 "NMI_INTERRUPT_TRM", 0x4C}, {
4776 "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
4777 "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
4778 "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
4779 "NMI_INTERRUPT_HOST", 0x66}, {
4780 "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
4781 "NMI_INTERRUPT_UNKNOWN", 0x84}, {
4782 "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
4783"ADVANCED_SYSASSERT", 0},};
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004784
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004785static const char *
4786il4965_desc_lookup(u32 num)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004787{
4788 int i;
4789 int max = ARRAY_SIZE(desc_lookup_text);
4790
4791 if (num < max)
4792 return desc_lookup_text[num];
4793
4794 max = ARRAY_SIZE(advanced_lookup) - 1;
4795 for (i = 0; i < max; i++) {
4796 if (advanced_lookup[i].num == num)
4797 break;
4798 }
4799 return advanced_lookup[i].name;
4800}
4801
4802#define ERROR_START_OFFSET (1 * sizeof(u32))
4803#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4804
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004805void
4806il4965_dump_nic_error_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004807{
4808 u32 data2, line;
4809 u32 desc, time, count, base, data1;
4810 u32 blink1, blink2, ilink1, ilink2;
4811 u32 pc, hcmd;
4812
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004813 if (il->ucode_type == UCODE_INIT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004814 base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004815 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004816 base = le32_to_cpu(il->card_alive.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004817
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004818 if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004819 IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
4820 base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004821 return;
4822 }
4823
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004824 count = il_read_targ_mem(il, base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004825
4826 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004827 IL_ERR("Start IWL Error Log Dump:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004828 IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004829 }
4830
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004831 desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
4832 il->isr_stats.err_code = desc;
4833 pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
4834 blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
4835 blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
4836 ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
4837 ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
4838 data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
4839 data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
4840 line = il_read_targ_mem(il, base + 9 * sizeof(u32));
4841 time = il_read_targ_mem(il, base + 11 * sizeof(u32));
4842 hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004843
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004844 IL_ERR("Desc Time "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004845 "data1 data2 line\n");
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004846 IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004847 il4965_desc_lookup(desc), desc, time, data1, data2, line);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004848 IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004849 IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
4850 blink2, ilink1, ilink2, hcmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004851}
4852
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004853static void
4854il4965_rf_kill_ct_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004855{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004856 struct il_ct_kill_config cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004857 unsigned long flags;
4858 int ret = 0;
4859
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004860 spin_lock_irqsave(&il->lock, flags);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004861 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004862 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004863 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004864
4865 cmd.critical_temperature_R =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004866 cpu_to_le32(il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004867
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004868 ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004869 if (ret)
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004870 IL_ERR("C_CT_KILL_CONFIG failed\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004871 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004872 D_INFO("C_CT_KILL_CONFIG " "succeeded, "
4873 "critical temperature is %d\n",
4874 il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004875}
4876
4877static const s8 default_queue_to_tx_fifo[] = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004878 IL_TX_FIFO_VO,
4879 IL_TX_FIFO_VI,
4880 IL_TX_FIFO_BE,
4881 IL_TX_FIFO_BK,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004882 IL49_CMD_FIFO_NUM,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004883 IL_TX_FIFO_UNUSED,
4884 IL_TX_FIFO_UNUSED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004885};
4886
Stanislaw Gruszkae53aac42011-08-31 11:18:16 +02004887#define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
4888
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004889static int
4890il4965_alive_notify(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004891{
4892 u32 a;
4893 unsigned long flags;
4894 int i, chan;
4895 u32 reg_val;
4896
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004897 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004898
4899 /* Clear 4965's internal Tx Scheduler data base */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004900 il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004901 a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
4902 for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004903 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004904 for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004905 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004906 for (;
4907 a <
4908 il->scd_base_addr +
4909 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
4910 a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004911 il_write_targ_mem(il, a, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004912
4913 /* Tel 4965 where to find Tx byte count tables */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004914 il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004915
4916 /* Enable DMA channel */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004917 for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
4918 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
4919 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
4920 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004921
4922 /* Update FH chicken bits */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004923 reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
4924 il_wr(il, FH49_TX_CHICKEN_BITS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004925 reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004926
4927 /* Disable chain mode for all queues */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004928 il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004929
4930 /* Initialize each Tx queue (including the command queue) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004931 for (i = 0; i < il->hw_params.max_txq_num; i++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004932
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004933 /* TFD circular buffer read/write idxes */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004934 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004935 il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004936
4937 /* Max Tx Window size for Scheduler-ACK mode */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004938 il_write_targ_mem(il,
4939 il->scd_base_addr +
4940 IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
4941 (SCD_WIN_SIZE <<
4942 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
4943 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004944
4945 /* Frame limit */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004946 il_write_targ_mem(il,
4947 il->scd_base_addr +
4948 IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
4949 sizeof(u32),
4950 (SCD_FRAME_LIMIT <<
4951 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
4952 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004953
4954 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004955 il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004956 (1 << il->hw_params.max_txq_num) - 1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004957
4958 /* Activate all Tx DMA/FIFO channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004959 il4965_txq_set_sched(il, IL_MASK(0, 6));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004960
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004961 il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004962
4963 /* make sure all queue are not stopped */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004964 memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004965 for (i = 0; i < 4; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004966 atomic_set(&il->queue_stop_count[i], 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004967
4968 /* reset to 0 to enable all the queue first */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004969 il->txq_ctx_active_msk = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004970 /* Map each Tx/cmd queue to its corresponding fifo */
4971 BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
4972
4973 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
4974 int ac = default_queue_to_tx_fifo[i];
4975
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004976 il_txq_ctx_activate(il, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004977
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004978 if (ac == IL_TX_FIFO_UNUSED)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004979 continue;
4980
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004981 il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004982 }
4983
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004984 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004985
4986 return 0;
4987}
4988
4989/**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004990 * il4965_alive_start - called after N_ALIVE notification received
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004991 * from protocol/runtime uCode (initialization uCode's
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004992 * Alive gets handled by il_init_alive_start()).
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004993 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004994static void
4995il4965_alive_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004996{
4997 int ret = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01004998 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004999
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005000 D_INFO("Runtime Alive received.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005001
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005002 if (il->card_alive.is_valid != UCODE_VALID_OK) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005003 /* We had an error bringing up the hardware, so take it
5004 * all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005005 D_INFO("Alive failed.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005006 goto restart;
5007 }
5008
5009 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5010 * This is a paranoid check, because we would not have gotten the
5011 * "runtime" alive if code weren't properly loaded. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005012 if (il4965_verify_ucode(il)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005013 /* Runtime instruction load was bad;
5014 * take it all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005015 D_INFO("Bad runtime uCode load.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005016 goto restart;
5017 }
5018
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005019 ret = il4965_alive_notify(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005020 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005021 IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005022 goto restart;
5023 }
5024
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005025 /* After the ALIVE response, we can send host commands to the uCode */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005026 set_bit(S_ALIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005027
5028 /* Enable watchdog to monitor the driver tx queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005029 il_setup_watchdog(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005030
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005031 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005032 return;
5033
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005034 ieee80211_wake_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005035
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005036 il->active_rate = RATES_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005037
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005038 if (il_is_associated_ctx(ctx)) {
5039 struct il_rxon_cmd *active_rxon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005040 (struct il_rxon_cmd *)&ctx->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005041 /* apply any changes in staging */
5042 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
5043 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5044 } else {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005045 /* Initialize our rx_config data */
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005046 il_connection_init_rx_config(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005047
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005048 if (il->cfg->ops->hcmd->set_rxon_chain)
5049 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005050 }
5051
5052 /* Configure bluetooth coexistence if enabled */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005053 il_send_bt_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005054
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005055 il4965_reset_run_time_calib(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005056
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005057 set_bit(S_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005058
5059 /* Configure the adapter for unassociated operation */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005060 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005061
5062 /* At this point, the NIC is initialized and operational */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005063 il4965_rf_kill_ct_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005064
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005065 D_INFO("ALIVE processing complete.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005066 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005067
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005068 il_power_update_mode(il, true);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005069 D_INFO("Updated power mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005070
5071 return;
5072
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005073restart:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005074 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005075}
5076
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005077static void il4965_cancel_deferred_work(struct il_priv *il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005078
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005079static void
5080__il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005081{
5082 unsigned long flags;
Stanislaw Gruszkaab42b402011-04-28 11:51:24 +02005083 int exit_pending;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005084
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005085 D_INFO(DRV_NAME " is going down\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005086
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005087 il_scan_cancel_timeout(il, 200);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005088
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005089 exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005090
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005091 /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005092 * to prevent rearm timer */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005093 del_timer_sync(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005094
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005095 il_clear_ucode_stations(il, NULL);
5096 il_dealloc_bcast_stations(il);
5097 il_clear_driver_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005098
5099 /* Unblock any waiting calls */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005100 wake_up_all(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005101
5102 /* Wipe out the EXIT_PENDING status bit if we are not actually
5103 * exiting the module */
5104 if (!exit_pending)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005105 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005106
5107 /* stop and reset the on-board processor */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005108 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005109
5110 /* tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005111 spin_lock_irqsave(&il->lock, flags);
5112 il_disable_interrupts(il);
5113 spin_unlock_irqrestore(&il->lock, flags);
5114 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005115
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005116 if (il->mac80211_registered)
5117 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005118
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005119 /* If we have not previously called il_init() then
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005120 * clear all bits but the RF Kill bit and return */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005121 if (!il_is_init(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005122 il->status =
5123 test_bit(S_RF_KILL_HW,
5124 &il->
5125 status) << S_RF_KILL_HW |
5126 test_bit(S_GEO_CONFIGURED,
5127 &il->
5128 status) << S_GEO_CONFIGURED |
5129 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005130 goto exit;
5131 }
5132
5133 /* ...otherwise clear out all the status bits but the RF Kill
5134 * bit and continue taking the NIC down. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005135 il->status &=
5136 test_bit(S_RF_KILL_HW,
5137 &il->status) << S_RF_KILL_HW | test_bit(S_GEO_CONFIGURED,
5138 &il->
5139 status) <<
5140 S_GEO_CONFIGURED | test_bit(S_FW_ERROR,
5141 &il->
5142 status) << S_FW_ERROR |
5143 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005144
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005145 il4965_txq_ctx_stop(il);
5146 il4965_rxq_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005147
5148 /* Power-down device's busmaster DMA clocks */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02005149 il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005150 udelay(5);
5151
5152 /* Make sure (redundant) we've released our request to stay awake */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005153 il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005154
5155 /* Stop the device, and put it in low power state */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005156 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005157
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005158exit:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005159 memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005160
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005161 dev_kfree_skb(il->beacon_skb);
5162 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005163
5164 /* clear out any free frames */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005165 il4965_clear_free_frames(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005166}
5167
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005168static void
5169il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005170{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005171 mutex_lock(&il->mutex);
5172 __il4965_down(il);
5173 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005174
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005175 il4965_cancel_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005176}
5177
5178#define HW_READY_TIMEOUT (50)
5179
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005180static int
5181il4965_set_hw_ready(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005182{
5183 int ret = 0;
5184
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005185 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005186 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005187
5188 /* See if we got it */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005189 ret =
5190 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5191 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5192 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, HW_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005193 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005194 il->hw_ready = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005195 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005196 il->hw_ready = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005197
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005198 D_INFO("hardware %s\n", (il->hw_ready == 1) ? "ready" : "not ready");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005199 return ret;
5200}
5201
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005202static int
5203il4965_prepare_card_hw(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005204{
5205 int ret = 0;
5206
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005207 D_INFO("il4965_prepare_card_hw enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005208
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005209 ret = il4965_set_hw_ready(il);
5210 if (il->hw_ready)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005211 return ret;
5212
5213 /* If HW is not ready, prepare the conditions to check again */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005214 il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005215
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005216 ret =
5217 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5218 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
5219 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005220
5221 /* HW should be ready by now, check again. */
5222 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005223 il4965_set_hw_ready(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005224
5225 return ret;
5226}
5227
5228#define MAX_HW_RESTARTS 5
5229
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005230static int
5231__il4965_up(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005232{
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005233 int i;
5234 int ret;
5235
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005236 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005237 IL_WARN("Exit pending; will not bring the NIC up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005238 return -EIO;
5239 }
5240
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005241 if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005242 IL_ERR("ucode not available for device bringup\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005243 return -EIO;
5244 }
5245
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005246 ret = il4965_alloc_bcast_station(il, &il->ctx);
5247 if (ret) {
5248 il_dealloc_bcast_stations(il);
5249 return ret;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005250 }
5251
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005252 il4965_prepare_card_hw(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005253
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005254 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005255 IL_WARN("Exit HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005256 return -EIO;
5257 }
5258
5259 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005260 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005261 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005262 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005263 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005264
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005265 if (il_is_rfkill(il)) {
5266 wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005267
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005268 il_enable_interrupts(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005269 IL_WARN("Radio disabled by HW RF Kill switch\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005270 return 0;
5271 }
5272
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005273 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005274
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005275 /* must be initialised before il_hw_nic_init */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005276 il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005277
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005278 ret = il4965_hw_nic_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005279 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005280 IL_ERR("Unable to init nic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005281 return ret;
5282 }
5283
5284 /* make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005285 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005286 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005287
5288 /* clear (again), then enable host interrupts */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005289 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005290 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005291
5292 /* really make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005293 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5294 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005295
5296 /* Copy original ucode data image from disk into backup cache.
5297 * This will be used to initialize the on-board processor's
5298 * data SRAM for a clean start when the runtime program first loads. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005299 memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
5300 il->ucode_data.len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005301
5302 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5303
5304 /* load bootstrap state machine,
5305 * load bootstrap program into processor's memory,
5306 * prepare to load the "initialize" uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005307 ret = il->cfg->ops->lib->load_ucode(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005308
5309 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005310 IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005311 continue;
5312 }
5313
5314 /* start card; "initialize" will load runtime ucode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005315 il4965_nic_start(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005316
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005317 D_INFO(DRV_NAME " is coming up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005318
5319 return 0;
5320 }
5321
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005322 set_bit(S_EXIT_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005323 __il4965_down(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005324 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005325
5326 /* tried to restart and config the device for as long as our
5327 * patience could withstand */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005328 IL_ERR("Unable to initialize device after %d attempts.\n", i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005329 return -EIO;
5330}
5331
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005332/*****************************************************************************
5333 *
5334 * Workqueue callbacks
5335 *
5336 *****************************************************************************/
5337
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005338static void
5339il4965_bg_init_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005340{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005341 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005342 container_of(data, struct il_priv, init_alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005343
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005344 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005345 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005346 goto out;
5347
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005348 il->cfg->ops->lib->init_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005349out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005350 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005351}
5352
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005353static void
5354il4965_bg_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005355{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005356 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005357 container_of(data, struct il_priv, alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005358
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005359 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005360 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005361 goto out;
5362
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005363 il4965_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005364out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005365 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005366}
5367
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005368static void
5369il4965_bg_run_time_calib_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005370{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005371 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005372 run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005373
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005374 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005375
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005376 if (test_bit(S_EXIT_PENDING, &il->status) ||
5377 test_bit(S_SCANNING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005378 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005379 return;
5380 }
5381
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005382 if (il->start_calib) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005383 il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
5384 il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005385 }
5386
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005387 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005388}
5389
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005390static void
5391il4965_bg_restart(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005392{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005393 struct il_priv *il = container_of(data, struct il_priv, restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005394
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005395 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005396 return;
5397
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005398 if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005399 mutex_lock(&il->mutex);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005400 il->ctx.vif = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005401 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005402
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005403 __il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005404
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005405 mutex_unlock(&il->mutex);
5406 il4965_cancel_deferred_work(il);
5407 ieee80211_restart_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005408 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005409 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005410
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005411 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005412 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005413 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005414 return;
5415 }
5416
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005417 __il4965_up(il);
5418 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005419 }
5420}
5421
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005422static void
5423il4965_bg_rx_replenish(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005424{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005425 struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005426
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005427 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005428 return;
5429
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005430 mutex_lock(&il->mutex);
5431 il4965_rx_replenish(il);
5432 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005433}
5434
5435/*****************************************************************************
5436 *
5437 * mac80211 entry point functions
5438 *
5439 *****************************************************************************/
5440
5441#define UCODE_READY_TIMEOUT (4 * HZ)
5442
5443/*
5444 * Not a mac80211 entry point function, but it fits in with all the
5445 * other mac80211 functions grouped here.
5446 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005447static int
5448il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005449{
5450 int ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005451 struct ieee80211_hw *hw = il->hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005452
5453 hw->rate_control_algorithm = "iwl-4965-rs";
5454
5455 /* Tell mac80211 our characteristics */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005456 hw->flags =
5457 IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_AMPDU_AGGREGATION |
5458 IEEE80211_HW_NEED_DTIM_PERIOD | IEEE80211_HW_SPECTRUM_MGMT |
5459 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005460
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005461 if (il->cfg->sku & IL_SKU_N)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005462 hw->flags |=
5463 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
5464 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005465
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005466 hw->sta_data_size = sizeof(struct il_station_priv);
5467 hw->vif_data_size = sizeof(struct il_vif_priv);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005468
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005469 hw->wiphy->interface_modes |= il->ctx.interface_modes;
5470 hw->wiphy->interface_modes |= il->ctx.exclusive_interface_modes;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005471
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005472 hw->wiphy->flags |=
5473 WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005474
5475 /*
5476 * For now, disable PS by default because it affects
5477 * RX performance significantly.
5478 */
5479 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5480
5481 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
5482 /* we create the 802.11 header and a zero-length SSID element */
5483 hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
5484
5485 /* Default value; 4 EDCA QOS priorities */
5486 hw->queues = 4;
5487
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005488 hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005489
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005490 if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
5491 il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005492 &il->bands[IEEE80211_BAND_2GHZ];
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005493 if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
5494 il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005495 &il->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005496
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005497 il_leds_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005498
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005499 ret = ieee80211_register_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005500 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005501 IL_ERR("Failed to register hw (error %d)\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005502 return ret;
5503 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005504 il->mac80211_registered = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005505
5506 return 0;
5507}
5508
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005509int
5510il4965_mac_start(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005511{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005512 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005513 int ret;
5514
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005515 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005516
5517 /* we should be verifying the device is ready to be opened */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005518 mutex_lock(&il->mutex);
5519 ret = __il4965_up(il);
5520 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005521
5522 if (ret)
5523 return ret;
5524
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005525 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005526 goto out;
5527
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005528 D_INFO("Start UP work done.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005529
5530 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5531 * mac80211 will not be run successfully. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005532 ret = wait_event_timeout(il->wait_command_queue,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005533 test_bit(S_READY, &il->status),
5534 UCODE_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005535 if (!ret) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005536 if (!test_bit(S_READY, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005537 IL_ERR("START_ALIVE timeout after %dms.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005538 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5539 return -ETIMEDOUT;
5540 }
5541 }
5542
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005543 il4965_led_enable(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005544
5545out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005546 il->is_open = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005547 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005548 return 0;
5549}
5550
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005551void
5552il4965_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005553{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005554 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005555
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005556 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005557
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005558 if (!il->is_open)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005559 return;
5560
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005561 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005562
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005563 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005564
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005565 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005566
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02005567 /* User space software may expect getting rfkill changes
5568 * even if interface is down */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005569 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005570 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005571
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005572 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005573}
5574
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005575void
5576il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005577{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005578 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005579
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005580 D_MACDUMP("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005581
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005582 D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005583 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005584
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005585 if (il4965_tx_skb(il, skb))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005586 dev_kfree_skb_any(skb);
5587
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005588 D_MACDUMP("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005589}
5590
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005591void
5592il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5593 struct ieee80211_key_conf *keyconf,
5594 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005595{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005596 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005597 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005598
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005599 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005600
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005601 il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta, iv32,
5602 phase1key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005603
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005604 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005605}
5606
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005607int
5608il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5609 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5610 struct ieee80211_key_conf *key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005611{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005612 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005613 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
5614 struct il_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005615 int ret;
5616 u8 sta_id;
5617 bool is_default_wep_key = false;
5618
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005619 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005620
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005621 if (il->cfg->mod_params->sw_crypto) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005622 D_MAC80211("leave - hwcrypto disabled\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005623 return -EOPNOTSUPP;
5624 }
5625
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005626 sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005627 if (sta_id == IL_INVALID_STATION)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005628 return -EINVAL;
5629
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005630 mutex_lock(&il->mutex);
5631 il_scan_cancel_timeout(il, 100);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005632
5633 /*
5634 * If we are getting WEP group key and we didn't receive any key mapping
5635 * so far, we are in legacy wep mode (group key only), otherwise we are
5636 * in 1X mode.
5637 * In legacy wep mode, we use another host command to the uCode.
5638 */
5639 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005640 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005641 if (cmd == SET_KEY)
5642 is_default_wep_key = !ctx->key_mapping_keys;
5643 else
5644 is_default_wep_key =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005645 (key->hw_key_idx == HW_KEY_DEFAULT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005646 }
5647
5648 switch (cmd) {
5649 case SET_KEY:
5650 if (is_default_wep_key)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005651 ret =
5652 il4965_set_default_wep_key(il, vif_priv->ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005653 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005654 ret =
5655 il4965_set_dynamic_key(il, vif_priv->ctx, key,
5656 sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005657
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005658 D_MAC80211("enable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005659 break;
5660 case DISABLE_KEY:
5661 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005662 ret = il4965_remove_default_wep_key(il, ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005663 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005664 ret = il4965_remove_dynamic_key(il, ctx, key, sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005665
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005666 D_MAC80211("disable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005667 break;
5668 default:
5669 ret = -EINVAL;
5670 }
5671
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005672 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005673 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005674
5675 return ret;
5676}
5677
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005678int
5679il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5680 enum ieee80211_ampdu_mlme_action action,
5681 struct ieee80211_sta *sta, u16 tid, u16 * ssn,
5682 u8 buf_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005683{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005684 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005685 int ret = -EINVAL;
5686
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005687 D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005688
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005689 if (!(il->cfg->sku & IL_SKU_N))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005690 return -EACCES;
5691
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005692 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005693
5694 switch (action) {
5695 case IEEE80211_AMPDU_RX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005696 D_HT("start Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005697 ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005698 break;
5699 case IEEE80211_AMPDU_RX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005700 D_HT("stop Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005701 ret = il4965_sta_rx_agg_stop(il, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005702 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005703 ret = 0;
5704 break;
5705 case IEEE80211_AMPDU_TX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005706 D_HT("start Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005707 ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005708 break;
5709 case IEEE80211_AMPDU_TX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005710 D_HT("stop Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005711 ret = il4965_tx_agg_stop(il, vif, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005712 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005713 ret = 0;
5714 break;
5715 case IEEE80211_AMPDU_TX_OPERATIONAL:
5716 ret = 0;
5717 break;
5718 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005719 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005720
5721 return ret;
5722}
5723
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005724int
5725il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5726 struct ieee80211_sta *sta)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005727{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005728 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005729 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
5730 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005731 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
5732 int ret;
5733 u8 sta_id;
5734
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005735 D_INFO("received request to add station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005736 mutex_lock(&il->mutex);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005737 D_INFO("proceeding to add station %pM\n", sta->addr);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005738 sta_priv->common.sta_id = IL_INVALID_STATION;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005739
5740 atomic_set(&sta_priv->pending_frames, 0);
5741
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005742 ret =
5743 il_add_station_common(il, vif_priv->ctx, sta->addr, is_ap, sta,
5744 &sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005745 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005746 IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005747 /* Should we return success if return code is EEXIST ? */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005748 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005749 return ret;
5750 }
5751
5752 sta_priv->common.sta_id = sta_id;
5753
5754 /* Initialize rate scaling */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005755 D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005756 il4965_rs_rate_init(il, sta, sta_id);
5757 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005758
5759 return 0;
5760}
5761
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005762void
5763il4965_mac_channel_switch(struct ieee80211_hw *hw,
5764 struct ieee80211_channel_switch *ch_switch)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005765{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005766 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005767 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005768 struct ieee80211_conf *conf = &hw->conf;
5769 struct ieee80211_channel *channel = ch_switch->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005770 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005771
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005772 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005773 u16 ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005774
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005775 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005776
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005777 mutex_lock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005778
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005779 if (il_is_rfkill(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005780 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005781
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005782 if (test_bit(S_EXIT_PENDING, &il->status) ||
5783 test_bit(S_SCANNING, &il->status) ||
5784 test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005785 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005786
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005787 if (!il_is_associated_ctx(ctx))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005788 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005789
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005790 if (!il->cfg->ops->lib->set_channel_switch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005791 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005792
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005793 ch = channel->hw_value;
5794 if (le16_to_cpu(ctx->active.channel) == ch)
5795 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005796
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005797 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005798 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005799 D_MAC80211("invalid channel\n");
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005800 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005801 }
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005802
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005803 spin_lock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005804
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005805 il->current_ht_config.smps = conf->smps_mode;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005806
5807 /* Configure HT40 channels */
5808 ctx->ht.enabled = conf_is_ht(conf);
5809 if (ctx->ht.enabled) {
5810 if (conf_is_ht40_minus(conf)) {
5811 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005812 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005813 ctx->ht.is_40mhz = true;
5814 } else if (conf_is_ht40_plus(conf)) {
5815 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005816 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005817 ctx->ht.is_40mhz = true;
5818 } else {
5819 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005820 IEEE80211_HT_PARAM_CHA_SEC_NONE;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005821 ctx->ht.is_40mhz = false;
5822 }
5823 } else
5824 ctx->ht.is_40mhz = false;
5825
5826 if ((le16_to_cpu(ctx->staging.channel) != ch))
5827 ctx->staging.flags = 0;
5828
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005829 il_set_rxon_channel(il, channel, ctx);
5830 il_set_rxon_ht(il, ht_conf);
5831 il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005832
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005833 spin_unlock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005834
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005835 il_set_rate(il);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005836 /*
5837 * at this point, staging_rxon has the
5838 * configuration for channel switch
5839 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005840 set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005841 il->switch_channel = cpu_to_le16(ch);
5842 if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005843 clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005844 il->switch_channel = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005845 ieee80211_chswitch_done(ctx->vif, false);
5846 }
5847
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005848out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005849 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005850 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005851}
5852
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005853void
5854il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
5855 unsigned int *total_flags, u64 multicast)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005856{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005857 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005858 __le32 filter_or = 0, filter_nand = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005859
5860#define CHK(test, flag) do { \
5861 if (*total_flags & (test)) \
5862 filter_or |= (flag); \
5863 else \
5864 filter_nand |= (flag); \
5865 } while (0)
5866
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005867 D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
5868 *total_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005869
5870 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
5871 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
5872 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
5873 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
5874
5875#undef CHK
5876
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005877 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005878
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005879 il->ctx.staging.filter_flags &= ~filter_nand;
5880 il->ctx.staging.filter_flags |= filter_or;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005881
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005882 /*
5883 * Not committing directly because hardware can perform a scan,
5884 * but we'll eventually commit the filter flags change anyway.
5885 */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005886
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005887 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005888
5889 /*
5890 * Receiving all multicast frames is always enabled by the
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005891 * default flags setup in il_connection_init_rx_config()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005892 * since we currently do not support programming multicast
5893 * filters into the device.
5894 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005895 *total_flags &=
5896 FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
5897 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005898}
5899
5900/*****************************************************************************
5901 *
5902 * driver setup and teardown
5903 *
5904 *****************************************************************************/
5905
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005906static void
5907il4965_bg_txpower_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005908{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005909 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005910 txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005911
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005912 mutex_lock(&il->mutex);
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005913
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005914 /* If a scan happened to start before we got here
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005915 * then just return; the stats notification will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005916 * kick off another scheduled work to compensate for
5917 * any temperature delta we missed here. */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005918 if (test_bit(S_EXIT_PENDING, &il->status) ||
5919 test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005920 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005921
5922 /* Regardless of if we are associated, we must reconfigure the
5923 * TX power since frames can be sent on non-radar channels while
5924 * not associated */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005925 il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005926
5927 /* Update last_temperature to keep is_calib_needed from running
5928 * when it isn't needed... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005929 il->last_temperature = il->temperature;
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005930out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005931 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005932}
5933
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005934static void
5935il4965_setup_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005936{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005937 il->workqueue = create_singlethread_workqueue(DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005938
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005939 init_waitqueue_head(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005940
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005941 INIT_WORK(&il->restart, il4965_bg_restart);
5942 INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
5943 INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
5944 INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
5945 INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005946
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005947 il_setup_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005948
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005949 INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005950
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005951 init_timer(&il->stats_periodic);
5952 il->stats_periodic.data = (unsigned long)il;
5953 il->stats_periodic.function = il4965_bg_stats_periodic;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005954
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005955 init_timer(&il->watchdog);
5956 il->watchdog.data = (unsigned long)il;
5957 il->watchdog.function = il_bg_watchdog;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005958
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005959 tasklet_init(&il->irq_tasklet,
5960 (void (*)(unsigned long))il4965_irq_tasklet,
5961 (unsigned long)il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005962}
5963
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005964static void
5965il4965_cancel_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005966{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005967 cancel_work_sync(&il->txpower_work);
5968 cancel_delayed_work_sync(&il->init_alive_start);
5969 cancel_delayed_work(&il->alive_start);
5970 cancel_work_sync(&il->run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005971
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005972 il_cancel_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005973
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005974 del_timer_sync(&il->stats_periodic);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005975}
5976
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005977static void
5978il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005979{
5980 int i;
5981
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005982 for (i = 0; i < RATE_COUNT_LEGACY; i++) {
Stanislaw Gruszkad2ddf6212011-08-16 14:17:04 +02005983 rates[i].bitrate = il_rates[i].ieee * 5;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005984 rates[i].hw_value = i; /* Rate scaling will work on idxes */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005985 rates[i].hw_value_short = i;
5986 rates[i].flags = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005987 if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005988 /*
5989 * If CCK != 1M then set short preamble rate flag.
5990 */
5991 rates[i].flags |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005992 (il_rates[i].plcp ==
5993 RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005994 }
5995 }
5996}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005997
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005998/*
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005999 * Acquire il->lock before calling this function !
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006000 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006001void
6002il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006003{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006004 il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006005 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006006}
6007
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006008void
6009il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
6010 int tx_fifo_id, int scd_retry)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006011{
6012 int txq_id = txq->q.id;
6013
6014 /* Find out whether to activate Tx queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006015 int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006016
6017 /* Set up and activate */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006018 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01006019 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
6020 (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
6021 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
6022 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
6023 IL49_SCD_QUEUE_STTS_REG_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006024
6025 txq->sched_retry = scd_retry;
6026
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006027 D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
6028 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006029}
6030
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006031static int
6032il4965_init_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006033{
6034 int ret;
6035
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006036 spin_lock_init(&il->sta_lock);
6037 spin_lock_init(&il->hcmd_lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006038
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006039 INIT_LIST_HEAD(&il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006040
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006041 mutex_init(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006042
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006043 il->ieee_channels = NULL;
6044 il->ieee_rates = NULL;
6045 il->band = IEEE80211_BAND_2GHZ;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006046
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006047 il->iw_mode = NL80211_IFTYPE_STATION;
6048 il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
6049 il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006050
6051 /* initialize force reset */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006052 il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006053
6054 /* Choose which receivers/antennas to use */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006055 if (il->cfg->ops->hcmd->set_rxon_chain)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006056 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006057
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006058 il_init_scan_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006059
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006060 ret = il_init_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006061 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006062 IL_ERR("initializing regulatory failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006063 goto err;
6064 }
6065
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006066 ret = il_init_geos(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006067 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006068 IL_ERR("initializing geos failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006069 goto err_free_channel_map;
6070 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006071 il4965_init_hw_rates(il, il->ieee_rates);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006072
6073 return 0;
6074
6075err_free_channel_map:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006076 il_free_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006077err:
6078 return ret;
6079}
6080
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006081static void
6082il4965_uninit_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006083{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006084 il4965_calib_free_results(il);
6085 il_free_geos(il);
6086 il_free_channel_map(il);
6087 kfree(il->scan_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006088}
6089
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006090static void
6091il4965_hw_detect(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006092{
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006093 il->hw_rev = _il_rd(il, CSR_HW_REV);
6094 il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006095 il->rev_id = il->pci_dev->revision;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006096 D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006097}
6098
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006099static int
6100il4965_set_hw_params(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006101{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006102 il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
6103 il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
6104 if (il->cfg->mod_params->amsdu_size_8K)
6105 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006106 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006107 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006108
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006109 il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006110
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006111 if (il->cfg->mod_params->disable_11n)
6112 il->cfg->sku &= ~IL_SKU_N;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006113
6114 /* Device-specific setup */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006115 return il->cfg->ops->lib->set_hw_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006116}
6117
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006118static const u8 il4965_bss_ac_to_fifo[] = {
6119 IL_TX_FIFO_VO,
6120 IL_TX_FIFO_VI,
6121 IL_TX_FIFO_BE,
6122 IL_TX_FIFO_BK,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006123};
6124
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006125static const u8 il4965_bss_ac_to_queue[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006126 0, 1, 2, 3,
6127};
6128
6129static int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006130il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006131{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006132 int err = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006133 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006134 struct ieee80211_hw *hw;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006135 struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006136 unsigned long flags;
6137 u16 pci_cmd;
6138
6139 /************************
6140 * 1. Allocating HW data
6141 ************************/
6142
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006143 hw = il_alloc_all(cfg);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006144 if (!hw) {
6145 err = -ENOMEM;
6146 goto out;
6147 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006148 il = hw->priv;
6149 /* At this point both hw and il are allocated. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006150
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006151 il->ctx.ctxid = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006152
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006153 il->ctx.always_active = true;
6154 il->ctx.is_active = true;
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02006155 il->ctx.rxon_cmd = C_RXON;
6156 il->ctx.rxon_timing_cmd = C_RXON_TIMING;
6157 il->ctx.rxon_assoc_cmd = C_RXON_ASSOC;
6158 il->ctx.qos_cmd = C_QOS_PARAM;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006159 il->ctx.ap_sta_id = IL_AP_ID;
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02006160 il->ctx.wep_key_cmd = C_WEPKEY;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006161 il->ctx.ac_to_fifo = il4965_bss_ac_to_fifo;
6162 il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006163 il->ctx.exclusive_interface_modes = BIT(NL80211_IFTYPE_ADHOC);
6164 il->ctx.interface_modes = BIT(NL80211_IFTYPE_STATION);
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006165 il->ctx.ap_devtype = RXON_DEV_TYPE_AP;
6166 il->ctx.ibss_devtype = RXON_DEV_TYPE_IBSS;
6167 il->ctx.station_devtype = RXON_DEV_TYPE_ESS;
6168 il->ctx.unused_devtype = RXON_DEV_TYPE_ESS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006169
6170 SET_IEEE80211_DEV(hw, &pdev->dev);
6171
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006172 D_INFO("*** LOAD DRIVER ***\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006173 il->cfg = cfg;
6174 il->pci_dev = pdev;
6175 il->inta_mask = CSR_INI_SET_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006176
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006177 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006178 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006179
6180 /**************************
6181 * 2. Initializing PCI bus
6182 **************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006183 pci_disable_link_state(pdev,
6184 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6185 PCIE_LINK_STATE_CLKPM);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006186
6187 if (pci_enable_device(pdev)) {
6188 err = -ENODEV;
6189 goto out_ieee80211_free_hw;
6190 }
6191
6192 pci_set_master(pdev);
6193
6194 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
6195 if (!err)
6196 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
6197 if (err) {
6198 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6199 if (!err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006200 err =
6201 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006202 /* both attempts failed: */
6203 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006204 IL_WARN("No suitable DMA available.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006205 goto out_pci_disable_device;
6206 }
6207 }
6208
6209 err = pci_request_regions(pdev, DRV_NAME);
6210 if (err)
6211 goto out_pci_disable_device;
6212
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006213 pci_set_drvdata(pdev, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006214
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006215 /***********************
6216 * 3. Read REV register
6217 ***********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006218 il->hw_base = pci_iomap(pdev, 0, 0);
6219 if (!il->hw_base) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006220 err = -ENODEV;
6221 goto out_pci_release_regions;
6222 }
6223
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006224 D_INFO("pci_resource_len = 0x%08llx\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006225 (unsigned long long)pci_resource_len(pdev, 0));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006226 D_INFO("pci_resource_base = %p\n", il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006227
6228 /* these spin locks will be used in apm_ops.init and EEPROM access
6229 * we should init now
6230 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006231 spin_lock_init(&il->reg_lock);
6232 spin_lock_init(&il->lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006233
6234 /*
6235 * stop and reset the on-board processor just in case it is in a
6236 * strange state ... like being left stranded by a primary kernel
6237 * and this is now the kdump kernel trying to start up
6238 */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006239 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006240
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006241 il4965_hw_detect(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006242 IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006243
6244 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6245 * PCI Tx retries from interfering with C3 CPU state */
6246 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
6247
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006248 il4965_prepare_card_hw(il);
6249 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006250 IL_WARN("Failed, HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006251 goto out_iounmap;
6252 }
6253
6254 /*****************
6255 * 4. Read EEPROM
6256 *****************/
6257 /* Read the EEPROM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006258 err = il_eeprom_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006259 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006260 IL_ERR("Unable to init EEPROM\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006261 goto out_iounmap;
6262 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006263 err = il4965_eeprom_check_version(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006264 if (err)
6265 goto out_free_eeprom;
6266
6267 if (err)
6268 goto out_free_eeprom;
6269
6270 /* extract MAC Address */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006271 il4965_eeprom_get_mac(il, il->addresses[0].addr);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006272 D_INFO("MAC address: %pM\n", il->addresses[0].addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006273 il->hw->wiphy->addresses = il->addresses;
6274 il->hw->wiphy->n_addresses = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006275
6276 /************************
6277 * 5. Setup HW constants
6278 ************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006279 if (il4965_set_hw_params(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006280 IL_ERR("failed to set hw parameters\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006281 goto out_free_eeprom;
6282 }
6283
6284 /*******************
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006285 * 6. Setup il
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006286 *******************/
6287
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006288 err = il4965_init_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006289 if (err)
6290 goto out_free_eeprom;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006291 /* At this point both hw and il are initialized. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006292
6293 /********************
6294 * 7. Setup services
6295 ********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006296 spin_lock_irqsave(&il->lock, flags);
6297 il_disable_interrupts(il);
6298 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006299
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006300 pci_enable_msi(il->pci_dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006301
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006302 err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006303 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006304 IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006305 goto out_disable_msi;
6306 }
6307
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006308 il4965_setup_deferred_work(il);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02006309 il4965_setup_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006310
6311 /*********************************************
6312 * 8. Enable interrupts and read RFKILL state
6313 *********************************************/
6314
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02006315 /* enable rfkill interrupt: hw bug w/a */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006316 pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006317 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
6318 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006319 pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006320 }
6321
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006322 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006323
6324 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006325 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006326 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006327 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006328 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006329
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006330 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006331 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006332
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006333 il_power_initialize(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006334
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006335 init_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006336
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006337 err = il4965_request_firmware(il, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006338 if (err)
6339 goto out_destroy_workqueue;
6340
6341 return 0;
6342
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006343out_destroy_workqueue:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006344 destroy_workqueue(il->workqueue);
6345 il->workqueue = NULL;
6346 free_irq(il->pci_dev->irq, il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006347out_disable_msi:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006348 pci_disable_msi(il->pci_dev);
6349 il4965_uninit_drv(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006350out_free_eeprom:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006351 il_eeprom_free(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006352out_iounmap:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006353 pci_iounmap(pdev, il->hw_base);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006354out_pci_release_regions:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006355 pci_set_drvdata(pdev, NULL);
6356 pci_release_regions(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006357out_pci_disable_device:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006358 pci_disable_device(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006359out_ieee80211_free_hw:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006360 il_free_traffic_mem(il);
6361 ieee80211_free_hw(il->hw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006362out:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006363 return err;
6364}
6365
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006366static void __devexit
6367il4965_pci_remove(struct pci_dev *pdev)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006368{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006369 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006370 unsigned long flags;
6371
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006372 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006373 return;
6374
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006375 wait_for_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006376
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006377 D_INFO("*** UNLOAD DRIVER ***\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006378
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006379 il_dbgfs_unregister(il);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006380 sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006381
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006382 /* ieee80211_unregister_hw call wil cause il_mac_stop to
6383 * to be called and il4965_down since we are removing the device
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006384 * we need to set S_EXIT_PENDING bit.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006385 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006386 set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006387
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006388 il_leds_exit(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006389
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006390 if (il->mac80211_registered) {
6391 ieee80211_unregister_hw(il->hw);
6392 il->mac80211_registered = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006393 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006394 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006395 }
6396
6397 /*
6398 * Make sure device is reset to low power before unloading driver.
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006399 * This may be redundant with il4965_down(), but there are paths to
6400 * run il4965_down() without calling apm_ops.stop(), and there are
6401 * paths to avoid running il4965_down() at all before leaving driver.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006402 * This (inexpensive) call *makes sure* device is reset.
6403 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006404 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006405
6406 /* make sure we flush any pending irq or
6407 * tasklet for the driver
6408 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006409 spin_lock_irqsave(&il->lock, flags);
6410 il_disable_interrupts(il);
6411 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006412
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006413 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006414
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006415 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006416
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006417 if (il->rxq.bd)
6418 il4965_rx_queue_free(il, &il->rxq);
6419 il4965_hw_txq_ctx_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006421 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006422
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006423 /*netif_stop_queue(dev); */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006424 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006425
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006426 /* ieee80211_unregister_hw calls il_mac_stop, which flushes
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006427 * il->workqueue... so we can't take down the workqueue
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006428 * until now... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006429 destroy_workqueue(il->workqueue);
6430 il->workqueue = NULL;
6431 il_free_traffic_mem(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006432
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006433 free_irq(il->pci_dev->irq, il);
6434 pci_disable_msi(il->pci_dev);
6435 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006436 pci_release_regions(pdev);
6437 pci_disable_device(pdev);
6438 pci_set_drvdata(pdev, NULL);
6439
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006440 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006441
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006442 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006443
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006444 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006445}
6446
6447/*
6448 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006449 * must be called under il->lock and mac access
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006450 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006451void
6452il4965_txq_set_sched(struct il_priv *il, u32 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006453{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006454 il_wr_prph(il, IL49_SCD_TXFACT, mask);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006455}
6456
6457/*****************************************************************************
6458 *
6459 * driver and module entry point
6460 *
6461 *****************************************************************************/
6462
6463/* Hardware specific file defines the PCI IDs table for that hardware module */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006464static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006465 {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
6466 {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006467 {0}
6468};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006469MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006470
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006471static struct pci_driver il4965_driver = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006472 .name = DRV_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006473 .id_table = il4965_hw_card_ids,
6474 .probe = il4965_pci_probe,
6475 .remove = __devexit_p(il4965_pci_remove),
6476 .driver.pm = IL_LEGACY_PM_OPS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006477};
6478
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006479static int __init
6480il4965_init(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006481{
6482
6483 int ret;
6484 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
6485 pr_info(DRV_COPYRIGHT "\n");
6486
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006487 ret = il4965_rate_control_register();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006488 if (ret) {
6489 pr_err("Unable to register rate control algorithm: %d\n", ret);
6490 return ret;
6491 }
6492
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006493 ret = pci_register_driver(&il4965_driver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006494 if (ret) {
6495 pr_err("Unable to initialize PCI module\n");
6496 goto error_register;
6497 }
6498
6499 return ret;
6500
6501error_register:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006502 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006503 return ret;
6504}
6505
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006506static void __exit
6507il4965_exit(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006508{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006509 pci_unregister_driver(&il4965_driver);
6510 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006511}
6512
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006513module_exit(il4965_exit);
6514module_init(il4965_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006515
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006516#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkad2ddf6212011-08-16 14:17:04 +02006517module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006518MODULE_PARM_DESC(debug, "debug output mask");
6519#endif
6520
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006521module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006522MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006523module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006524MODULE_PARM_DESC(queues_num, "number of hw queues.");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006525module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006526MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006527module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int,
6528 S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006529MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006530module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006531MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");