blob: 731bb8c10b451db7cf499867c59276abf5780947 [file] [log] [blame]
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/init.h>
35#include <linux/pci.h>
36#include <linux/pci-aspm.h>
37#include <linux/slab.h>
38#include <linux/dma-mapping.h>
39#include <linux/delay.h>
40#include <linux/sched.h>
41#include <linux/skbuff.h>
42#include <linux/netdevice.h>
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080043#include <linux/firmware.h>
44#include <linux/etherdevice.h>
45#include <linux/if_arp.h>
46
47#include <net/mac80211.h>
48
49#include <asm/div64.h>
50
51#define DRV_NAME "iwl4965"
52
Stanislaw Gruszka98613be2011-11-15 14:19:34 +010053#include "common.h"
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +020054#include "4965.h"
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080055
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080056/******************************************************************************
57 *
58 * module boiler plate
59 *
60 ******************************************************************************/
61
62/*
63 * module name, copyright, version, etc.
64 */
65#define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
66
Stanislaw Gruszkad3175162011-11-15 11:25:42 +010067#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080068#define VD "d"
69#else
70#define VD
71#endif
72
73#define DRV_VERSION IWLWIFI_VERSION VD
74
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080075MODULE_DESCRIPTION(DRV_DESCRIPTION);
76MODULE_VERSION(DRV_VERSION);
77MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
78MODULE_LICENSE("GPL");
79MODULE_ALIAS("iwl4965");
80
Stanislaw Gruszkae7392362011-11-15 14:45:59 +010081void
82il4965_check_abort_status(struct il_priv *il, u8 frame_count, u32 status)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +020083{
84 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
85 IL_ERR("Tx flush command to flush out all frames\n");
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +010086 if (!test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +020087 queue_work(il->workqueue, &il->tx_flush);
88 }
89}
90
91/*
92 * EEPROM
93 */
94struct il_mod_params il4965_mod_params = {
95 .amsdu_size_8K = 1,
96 .restart_fw = 1,
97 /* the rest are 0 by default */
98};
99
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100100void
101il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200102{
103 unsigned long flags;
104 int i;
105 spin_lock_irqsave(&rxq->lock, flags);
106 INIT_LIST_HEAD(&rxq->rx_free);
107 INIT_LIST_HEAD(&rxq->rx_used);
108 /* Fill the rx_used queue with _all_ of the Rx buffers */
109 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
110 /* In the reset function, these buffers may have been allocated
111 * to an SKB, so we need to unmap and free potential storage */
112 if (rxq->pool[i].page != NULL) {
113 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100114 PAGE_SIZE << il->hw_params.rx_page_order,
115 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200116 __il_free_pages(il, rxq->pool[i].page);
117 rxq->pool[i].page = NULL;
118 }
119 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
120 }
121
122 for (i = 0; i < RX_QUEUE_SIZE; i++)
123 rxq->queue[i] = NULL;
124
125 /* Set us so that we have processed and used all buffers, but have
126 * not restocked the Rx queue with fresh buffers */
127 rxq->read = rxq->write = 0;
128 rxq->write_actual = 0;
129 rxq->free_count = 0;
130 spin_unlock_irqrestore(&rxq->lock, flags);
131}
132
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100133int
134il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200135{
136 u32 rb_size;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100137 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200138 u32 rb_timeout = 0;
139
140 if (il->cfg->mod_params->amsdu_size_8K)
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200141 rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200142 else
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200143 rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200144
145 /* Stop Rx DMA */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200146 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200147
148 /* Reset driver's Rx queue write idx */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200149 il_wr(il, FH49_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200150
151 /* Tell device where to find RBD circular buffer in DRAM */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100152 il_wr(il, FH49_RSCSR_CHNL0_RBDCB_BASE_REG, (u32) (rxq->bd_dma >> 8));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200153
154 /* Tell device where in DRAM to update its Rx status */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100155 il_wr(il, FH49_RSCSR_CHNL0_STTS_WPTR_REG, rxq->rb_stts_dma >> 4);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200156
157 /* Enable Rx DMA
158 * Direct rx interrupts to hosts
159 * Rx buffer size 4 or 8k
160 * RB timeout 0x10
161 * 256 RBDs
162 */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200163 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100164 FH49_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
165 FH49_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +0100166 FH49_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
167 rb_size |
168 (rb_timeout << FH49_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
169 (rfdnlog << FH49_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200170
171 /* Set interrupt coalescing timer to default (2048 usecs) */
172 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
173
174 return 0;
175}
176
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100177static void
178il4965_set_pwr_vmain(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200179{
180/*
181 * (for documentation purposes)
182 * to set power to V_AUX, do:
183
184 if (pci_pme_capable(il->pci_dev, PCI_D3cold))
185 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
186 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
187 ~APMG_PS_CTRL_MSK_PWR_SRC);
188 */
189
190 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100191 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
192 ~APMG_PS_CTRL_MSK_PWR_SRC);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200193}
194
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100195int
196il4965_hw_nic_init(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200197{
198 unsigned long flags;
199 struct il_rx_queue *rxq = &il->rxq;
200 int ret;
201
202 /* nic_init */
203 spin_lock_irqsave(&il->lock, flags);
204 il->cfg->ops->lib->apm_ops.init(il);
205
206 /* Set interrupt coalescing calibration timer to default (512 usecs) */
207 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
208
209 spin_unlock_irqrestore(&il->lock, flags);
210
211 il4965_set_pwr_vmain(il);
212
213 il->cfg->ops->lib->apm_ops.config(il);
214
215 /* Allocate the RX queue, or reset if it is already allocated */
216 if (!rxq->bd) {
217 ret = il_rx_queue_alloc(il);
218 if (ret) {
219 IL_ERR("Unable to initialize Rx queue\n");
220 return -ENOMEM;
221 }
222 } else
223 il4965_rx_queue_reset(il, rxq);
224
225 il4965_rx_replenish(il);
226
227 il4965_rx_init(il, rxq);
228
229 spin_lock_irqsave(&il->lock, flags);
230
231 rxq->need_update = 1;
232 il_rx_queue_update_write_ptr(il, rxq);
233
234 spin_unlock_irqrestore(&il->lock, flags);
235
236 /* Allocate or reset and init all Tx and Command queues */
237 if (!il->txq) {
238 ret = il4965_txq_ctx_alloc(il);
239 if (ret)
240 return ret;
241 } else
242 il4965_txq_ctx_reset(il);
243
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100244 set_bit(S_INIT, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200245
246 return 0;
247}
248
249/**
250 * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
251 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100252static inline __le32
253il4965_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200254{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100255 return cpu_to_le32((u32) (dma_addr >> 8));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200256}
257
258/**
259 * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
260 *
261 * If there are slots in the RX queue that need to be restocked,
262 * and we have free pre-allocated buffers, fill the ranks as much
263 * as we can, pulling from rx_free.
264 *
265 * This moves the 'write' idx forward to catch up with 'processed', and
266 * also updates the memory address in the firmware to reference the new
267 * target buffer.
268 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100269void
270il4965_rx_queue_restock(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200271{
272 struct il_rx_queue *rxq = &il->rxq;
273 struct list_head *element;
274 struct il_rx_buf *rxb;
275 unsigned long flags;
276
277 spin_lock_irqsave(&rxq->lock, flags);
278 while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
279 /* The overwritten rxb must be a used one */
280 rxb = rxq->queue[rxq->write];
281 BUG_ON(rxb && rxb->page);
282
283 /* Get next free Rx buffer, remove from free list */
284 element = rxq->rx_free.next;
285 rxb = list_entry(element, struct il_rx_buf, list);
286 list_del(element);
287
288 /* Point to Rx buffer via next RBD in circular buffer */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100289 rxq->bd[rxq->write] =
290 il4965_dma_addr2rbd_ptr(il, rxb->page_dma);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200291 rxq->queue[rxq->write] = rxb;
292 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
293 rxq->free_count--;
294 }
295 spin_unlock_irqrestore(&rxq->lock, flags);
296 /* If the pre-allocated buffer pool is dropping low, schedule to
297 * refill it */
298 if (rxq->free_count <= RX_LOW_WATERMARK)
299 queue_work(il->workqueue, &il->rx_replenish);
300
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200301 /* If we've added more space for the firmware to place data, tell it.
302 * Increment device's write pointer in multiples of 8. */
303 if (rxq->write_actual != (rxq->write & ~0x7)) {
304 spin_lock_irqsave(&rxq->lock, flags);
305 rxq->need_update = 1;
306 spin_unlock_irqrestore(&rxq->lock, flags);
307 il_rx_queue_update_write_ptr(il, rxq);
308 }
309}
310
311/**
312 * il4965_rx_replenish - Move all used packet from rx_used to rx_free
313 *
314 * When moving to rx_free an SKB is allocated for the slot.
315 *
316 * Also restock the Rx queue via il_rx_queue_restock.
317 * This is called as a scheduled work item (except for during initialization)
318 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100319static void
320il4965_rx_allocate(struct il_priv *il, gfp_t priority)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200321{
322 struct il_rx_queue *rxq = &il->rxq;
323 struct list_head *element;
324 struct il_rx_buf *rxb;
325 struct page *page;
326 unsigned long flags;
327 gfp_t gfp_mask = priority;
328
329 while (1) {
330 spin_lock_irqsave(&rxq->lock, flags);
331 if (list_empty(&rxq->rx_used)) {
332 spin_unlock_irqrestore(&rxq->lock, flags);
333 return;
334 }
335 spin_unlock_irqrestore(&rxq->lock, flags);
336
337 if (rxq->free_count > RX_LOW_WATERMARK)
338 gfp_mask |= __GFP_NOWARN;
339
340 if (il->hw_params.rx_page_order > 0)
341 gfp_mask |= __GFP_COMP;
342
343 /* Alloc a new receive buffer */
344 page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
345 if (!page) {
346 if (net_ratelimit())
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100347 D_INFO("alloc_pages failed, " "order: %d\n",
348 il->hw_params.rx_page_order);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200349
350 if (rxq->free_count <= RX_LOW_WATERMARK &&
351 net_ratelimit())
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100352 IL_ERR("Failed to alloc_pages with %s. "
353 "Only %u free buffers remaining.\n",
354 priority ==
355 GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
356 rxq->free_count);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200357 /* We don't reschedule replenish work here -- we will
358 * call the restock method and if it still needs
359 * more buffers it will schedule replenish */
360 return;
361 }
362
363 spin_lock_irqsave(&rxq->lock, flags);
364
365 if (list_empty(&rxq->rx_used)) {
366 spin_unlock_irqrestore(&rxq->lock, flags);
367 __free_pages(page, il->hw_params.rx_page_order);
368 return;
369 }
370 element = rxq->rx_used.next;
371 rxb = list_entry(element, struct il_rx_buf, list);
372 list_del(element);
373
374 spin_unlock_irqrestore(&rxq->lock, flags);
375
376 BUG_ON(rxb->page);
377 rxb->page = page;
378 /* Get physical address of the RB */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100379 rxb->page_dma =
380 pci_map_page(il->pci_dev, page, 0,
381 PAGE_SIZE << il->hw_params.rx_page_order,
382 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200383 /* dma address must be no more than 36 bits */
384 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
385 /* and also 256 byte aligned! */
386 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
387
388 spin_lock_irqsave(&rxq->lock, flags);
389
390 list_add_tail(&rxb->list, &rxq->rx_free);
391 rxq->free_count++;
392 il->alloc_rxb_page++;
393
394 spin_unlock_irqrestore(&rxq->lock, flags);
395 }
396}
397
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100398void
399il4965_rx_replenish(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200400{
401 unsigned long flags;
402
403 il4965_rx_allocate(il, GFP_KERNEL);
404
405 spin_lock_irqsave(&il->lock, flags);
406 il4965_rx_queue_restock(il);
407 spin_unlock_irqrestore(&il->lock, flags);
408}
409
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100410void
411il4965_rx_replenish_now(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200412{
413 il4965_rx_allocate(il, GFP_ATOMIC);
414
415 il4965_rx_queue_restock(il);
416}
417
418/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
419 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
420 * This free routine walks the list of POOL entries and if SKB is set to
421 * non NULL it is unmapped and freed
422 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100423void
424il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200425{
426 int i;
427 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
428 if (rxq->pool[i].page != NULL) {
429 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100430 PAGE_SIZE << il->hw_params.rx_page_order,
431 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200432 __il_free_pages(il, rxq->pool[i].page);
433 rxq->pool[i].page = NULL;
434 }
435 }
436
437 dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
438 rxq->bd_dma);
439 dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
440 rxq->rb_stts, rxq->rb_stts_dma);
441 rxq->bd = NULL;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100442 rxq->rb_stts = NULL;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200443}
444
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100445int
446il4965_rxq_stop(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200447{
448
449 /* stop Rx DMA */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200450 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
451 il_poll_bit(il, FH49_MEM_RSSR_RX_STATUS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100452 FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200453
454 return 0;
455}
456
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100457int
458il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200459{
460 int idx = 0;
461 int band_offset = 0;
462
463 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
464 if (rate_n_flags & RATE_MCS_HT_MSK) {
465 idx = (rate_n_flags & 0xff);
466 return idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100467 /* Legacy rate format, search for match in table */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200468 } else {
469 if (band == IEEE80211_BAND_5GHZ)
470 band_offset = IL_FIRST_OFDM_RATE;
471 for (idx = band_offset; idx < RATE_COUNT_LEGACY; idx++)
472 if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
473 return idx - band_offset;
474 }
475
476 return -1;
477}
478
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100479static int
480il4965_calc_rssi(struct il_priv *il, struct il_rx_phy_res *rx_resp)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200481{
482 /* data from PHY/DSP regarding signal strength, etc.,
483 * contents are always there, not configurable by host. */
484 struct il4965_rx_non_cfg_phy *ncphy =
485 (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100486 u32 agc =
487 (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK) >>
488 IL49_AGC_DB_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200489
490 u32 valid_antennae =
491 (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100492 >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200493 u8 max_rssi = 0;
494 u32 i;
495
496 /* Find max rssi among 3 possible receivers.
497 * These values are measured by the digital signal processor (DSP).
498 * They should stay fairly constant even as the signal strength varies,
499 * if the radio's automatic gain control (AGC) is working right.
500 * AGC value (see below) will provide the "interesting" info. */
501 for (i = 0; i < 3; i++)
502 if (valid_antennae & (1 << i))
503 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
504
505 D_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
506 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
507 max_rssi, agc);
508
509 /* dBm = max_rssi dB - agc dB - constant.
510 * Higher AGC (higher radio gain) means lower signal. */
511 return max_rssi - agc - IL4965_RSSI_OFFSET;
512}
513
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100514static u32
515il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200516{
517 u32 decrypt_out = 0;
518
519 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100520 RX_RES_STATUS_STATION_FOUND)
521 decrypt_out |=
522 (RX_RES_STATUS_STATION_FOUND |
523 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200524
525 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
526
527 /* packet was not encrypted */
528 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100529 RX_RES_STATUS_SEC_TYPE_NONE)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200530 return decrypt_out;
531
532 /* packet was encrypted with unknown alg */
533 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100534 RX_RES_STATUS_SEC_TYPE_ERR)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200535 return decrypt_out;
536
537 /* decryption was not done in HW */
538 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100539 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200540 return decrypt_out;
541
542 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
543
544 case RX_RES_STATUS_SEC_TYPE_CCMP:
545 /* alg is CCM: check MIC only */
546 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
547 /* Bad MIC */
548 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
549 else
550 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
551
552 break;
553
554 case RX_RES_STATUS_SEC_TYPE_TKIP:
555 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
556 /* Bad TTAK */
557 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
558 break;
559 }
560 /* fall through if TTAK OK */
561 default:
562 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
563 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
564 else
565 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
566 break;
567 }
568
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100569 D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n", decrypt_in, decrypt_out);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200570
571 return decrypt_out;
572}
573
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100574static void
575il4965_pass_packet_to_mac80211(struct il_priv *il, struct ieee80211_hdr *hdr,
576 u16 len, u32 ampdu_status, struct il_rx_buf *rxb,
577 struct ieee80211_rx_status *stats)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200578{
579 struct sk_buff *skb;
580 __le16 fc = hdr->frame_control;
581
582 /* We only process data packets if the interface is open */
583 if (unlikely(!il->is_open)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100584 D_DROP("Dropping packet while interface is not open.\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200585 return;
586 }
587
588 /* In case of HW accelerated crypto and bad decryption, drop */
589 if (!il->cfg->mod_params->sw_crypto &&
590 il_set_decrypted_flag(il, hdr, ampdu_status, stats))
591 return;
592
593 skb = dev_alloc_skb(128);
594 if (!skb) {
595 IL_ERR("dev_alloc_skb failed\n");
596 return;
597 }
598
599 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
600
601 il_update_stats(il, false, fc, len);
602 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
603
604 ieee80211_rx(il->hw, skb);
605 il->alloc_rxb_page--;
606 rxb->page = NULL;
607}
608
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200609/* Called for N_RX (legacy ABG frames), or
610 * N_RX_MPDU (HT high-throughput N frames). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100611void
612il4965_hdl_rx(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200613{
614 struct ieee80211_hdr *header;
615 struct ieee80211_rx_status rx_status;
616 struct il_rx_pkt *pkt = rxb_addr(rxb);
617 struct il_rx_phy_res *phy_res;
618 __le32 rx_pkt_status;
619 struct il_rx_mpdu_res_start *amsdu;
620 u32 len;
621 u32 ampdu_status;
622 u32 rate_n_flags;
623
624 /**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200625 * N_RX and N_RX_MPDU are handled differently.
626 * N_RX: physical layer info is in this buffer
627 * N_RX_MPDU: physical layer info was sent in separate
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200628 * command and cached in il->last_phy_res
629 *
630 * Here we set up local variables depending on which command is
631 * received.
632 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200633 if (pkt->hdr.cmd == N_RX) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200634 phy_res = (struct il_rx_phy_res *)pkt->u.raw;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100635 header =
636 (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res) +
637 phy_res->cfg_phy_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200638
639 len = le16_to_cpu(phy_res->byte_count);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100640 rx_pkt_status =
641 *(__le32 *) (pkt->u.raw + sizeof(*phy_res) +
642 phy_res->cfg_phy_cnt + len);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200643 ampdu_status = le32_to_cpu(rx_pkt_status);
644 } else {
645 if (!il->_4965.last_phy_res_valid) {
646 IL_ERR("MPDU frame without cached PHY data\n");
647 return;
648 }
649 phy_res = &il->_4965.last_phy_res;
650 amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
651 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
652 len = le16_to_cpu(amsdu->byte_count);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100653 rx_pkt_status = *(__le32 *) (pkt->u.raw + sizeof(*amsdu) + len);
654 ampdu_status =
655 il4965_translate_rx_status(il, le32_to_cpu(rx_pkt_status));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200656 }
657
658 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
659 D_DROP("dsp size out of range [0,20]: %d/n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100660 phy_res->cfg_phy_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200661 return;
662 }
663
664 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
665 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100666 D_RX("Bad CRC or FIFO: 0x%08X.\n", le32_to_cpu(rx_pkt_status));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200667 return;
668 }
669
670 /* This will be used in several places later */
671 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
672
673 /* rx_status carries information about the packet to mac80211 */
674 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100675 rx_status.band =
676 (phy_res->
677 phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ? IEEE80211_BAND_2GHZ :
678 IEEE80211_BAND_5GHZ;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200679 rx_status.freq =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100680 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
681 rx_status.band);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200682 rx_status.rate_idx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100683 il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200684 rx_status.flag = 0;
685
686 /* TSF isn't reliable. In order to allow smooth user experience,
687 * this W/A doesn't propagate it to the mac80211 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100688 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU; */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200689
690 il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
691
692 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
693 rx_status.signal = il4965_calc_rssi(il, phy_res);
694
695 il_dbg_log_rx_data_frame(il, len, header);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100696 D_STATS("Rssi %d, TSF %llu\n", rx_status.signal,
697 (unsigned long long)rx_status.mactime);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200698
699 /*
700 * "antenna number"
701 *
702 * It seems that the antenna field in the phy flags value
703 * is actually a bit field. This is undefined by radiotap,
704 * it wants an actual antenna number but I always get "7"
705 * for most legacy frames I receive indicating that the
706 * same frame was received on all three RX chains.
707 *
708 * I think this field should be removed in favor of a
709 * new 802.11n radiotap field "RX chains" that is defined
710 * as a bitmask.
711 */
712 rx_status.antenna =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100713 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK) >>
714 RX_RES_PHY_FLAGS_ANTENNA_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200715
716 /* set the preamble flag if appropriate */
717 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
718 rx_status.flag |= RX_FLAG_SHORTPRE;
719
720 /* Set up the HT phy flags */
721 if (rate_n_flags & RATE_MCS_HT_MSK)
722 rx_status.flag |= RX_FLAG_HT;
723 if (rate_n_flags & RATE_MCS_HT40_MSK)
724 rx_status.flag |= RX_FLAG_40MHZ;
725 if (rate_n_flags & RATE_MCS_SGI_MSK)
726 rx_status.flag |= RX_FLAG_SHORT_GI;
727
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100728 il4965_pass_packet_to_mac80211(il, header, len, ampdu_status, rxb,
729 &rx_status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200730}
731
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200732/* Cache phy data (Rx signal strength, etc) for HT frame (N_RX_PHY).
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +0200733 * This will be used later in il_hdl_rx() for N_RX_MPDU. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100734void
735il4965_hdl_rx_phy(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200736{
737 struct il_rx_pkt *pkt = rxb_addr(rxb);
738 il->_4965.last_phy_res_valid = true;
739 memcpy(&il->_4965.last_phy_res, pkt->u.raw,
740 sizeof(struct il_rx_phy_res));
741}
742
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100743static int
744il4965_get_channels_for_scan(struct il_priv *il, struct ieee80211_vif *vif,
745 enum ieee80211_band band, u8 is_active,
746 u8 n_probes, struct il_scan_channel *scan_ch)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200747{
748 struct ieee80211_channel *chan;
749 const struct ieee80211_supported_band *sband;
750 const struct il_channel_info *ch_info;
751 u16 passive_dwell = 0;
752 u16 active_dwell = 0;
753 int added, i;
754 u16 channel;
755
756 sband = il_get_hw_mode(il, band);
757 if (!sband)
758 return 0;
759
760 active_dwell = il_get_active_dwell_time(il, band, n_probes);
761 passive_dwell = il_get_passive_dwell_time(il, band, vif);
762
763 if (passive_dwell <= active_dwell)
764 passive_dwell = active_dwell + 1;
765
766 for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
767 chan = il->scan_request->channels[i];
768
769 if (chan->band != band)
770 continue;
771
772 channel = chan->hw_value;
773 scan_ch->channel = cpu_to_le16(channel);
774
775 ch_info = il_get_channel_info(il, band, channel);
776 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100777 D_SCAN("Channel %d is INVALID for this band.\n",
778 channel);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200779 continue;
780 }
781
782 if (!is_active || il_is_channel_passive(ch_info) ||
783 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
784 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
785 else
786 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
787
788 if (n_probes)
789 scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
790
791 scan_ch->active_dwell = cpu_to_le16(active_dwell);
792 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
793
794 /* Set txpower levels to defaults */
795 scan_ch->dsp_atten = 110;
796
797 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
798 * power level:
799 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
800 */
801 if (band == IEEE80211_BAND_5GHZ)
802 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
803 else
804 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
805
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100806 D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", channel,
807 le32_to_cpu(scan_ch->type),
808 (scan_ch->
809 type & SCAN_CHANNEL_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
810 (scan_ch->
811 type & SCAN_CHANNEL_TYPE_ACTIVE) ? active_dwell :
812 passive_dwell);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200813
814 scan_ch++;
815 added++;
816 }
817
818 D_SCAN("total channels to scan %d\n", added);
819 return added;
820}
821
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +0100822static void
823il4965_toggle_tx_ant(struct il_priv *il, u8 *ant, u8 valid)
824{
825 int i;
826 u8 ind = *ant;
827
828 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
829 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
830 if (valid & BIT(ind)) {
831 *ant = ind;
832 return;
833 }
834 }
835}
836
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100837int
838il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200839{
840 struct il_host_cmd cmd = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200841 .id = C_SCAN,
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200842 .len = sizeof(struct il_scan_cmd),
843 .flags = CMD_SIZE_HUGE,
844 };
845 struct il_scan_cmd *scan;
846 struct il_rxon_context *ctx = &il->ctx;
847 u32 rate_flags = 0;
848 u16 cmd_len;
849 u16 rx_chain = 0;
850 enum ieee80211_band band;
851 u8 n_probes = 0;
852 u8 rx_ant = il->hw_params.valid_rx_ant;
853 u8 rate;
854 bool is_active = false;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100855 int chan_mod;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200856 u8 active_chains;
857 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
858 int ret;
859
860 lockdep_assert_held(&il->mutex);
861
Greg Dietsche730b4c22011-09-06 17:33:51 -0500862 ctx = il_rxon_ctx_from_vif(vif);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200863
864 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100865 il->scan_cmd =
866 kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
867 GFP_KERNEL);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200868 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100869 D_SCAN("fail to allocate memory for scan\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200870 return -ENOMEM;
871 }
872 }
873 scan = il->scan_cmd;
874 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
875
876 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
877 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
878
879 if (il_is_any_associated(il)) {
880 u16 interval;
881 u32 extra;
882 u32 suspend_time = 100;
883 u32 scan_suspend_time = 100;
884
885 D_INFO("Scanning while associated...\n");
886 interval = vif->bss_conf.beacon_int;
887
888 scan->suspend_time = 0;
889 scan->max_out_time = cpu_to_le32(200 * 1024);
890 if (!interval)
891 interval = suspend_time;
892
893 extra = (suspend_time / interval) << 22;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100894 scan_suspend_time =
895 (extra | ((suspend_time % interval) * 1024));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200896 scan->suspend_time = cpu_to_le32(scan_suspend_time);
897 D_SCAN("suspend_time 0x%X beacon interval %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100898 scan_suspend_time, interval);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200899 }
900
901 if (il->scan_request->n_ssids) {
902 int i, p = 0;
903 D_SCAN("Kicking off active scan\n");
904 for (i = 0; i < il->scan_request->n_ssids; i++) {
905 /* always does wildcard anyway */
906 if (!il->scan_request->ssids[i].ssid_len)
907 continue;
908 scan->direct_scan[p].id = WLAN_EID_SSID;
909 scan->direct_scan[p].len =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100910 il->scan_request->ssids[i].ssid_len;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200911 memcpy(scan->direct_scan[p].ssid,
912 il->scan_request->ssids[i].ssid,
913 il->scan_request->ssids[i].ssid_len);
914 n_probes++;
915 p++;
916 }
917 is_active = true;
918 } else
919 D_SCAN("Start passive scan.\n");
920
921 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Stanislaw Gruszkab16db502012-02-03 17:31:44 +0100922 scan->tx_cmd.sta_id = il->hw_params.bcast_id;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200923 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
924
925 switch (il->scan_band) {
926 case IEEE80211_BAND_2GHZ:
927 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100928 chan_mod =
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +0100929 le32_to_cpu(il->active.flags & RXON_FLG_CHANNEL_MODE_MSK) >>
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100930 RXON_FLG_CHANNEL_MODE_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200931 if (chan_mod == CHANNEL_MODE_PURE_40) {
932 rate = RATE_6M_PLCP;
933 } else {
934 rate = RATE_1M_PLCP;
935 rate_flags = RATE_MCS_CCK_MSK;
936 }
937 break;
938 case IEEE80211_BAND_5GHZ:
939 rate = RATE_6M_PLCP;
940 break;
941 default:
942 IL_WARN("Invalid scan band\n");
943 return -EIO;
944 }
945
946 /*
947 * If active scanning is requested but a certain channel is
948 * marked passive, we can do active scanning if we detect
949 * transmissions.
950 *
951 * There is an issue with some firmware versions that triggers
952 * a sysassert on a "good CRC threshold" of zero (== disabled),
953 * on a radar channel even though this means that we should NOT
954 * send probes.
955 *
956 * The "good CRC threshold" is the number of frames that we
957 * need to receive during our dwell time on a channel before
958 * sending out probes -- setting this to a huge value will
959 * mean we never reach it, but at the same time work around
960 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
961 * here instead of IL_GOOD_CRC_TH_DISABLED.
962 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100963 scan->good_CRC_th =
964 is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200965
966 band = il->scan_band;
967
968 if (il->cfg->scan_rx_antennas[band])
969 rx_ant = il->cfg->scan_rx_antennas[band];
970
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +0100971 il4965_toggle_tx_ant(il, &il->scan_tx_ant[band], scan_tx_antennas);
Stanislaw Gruszka616107e2011-12-23 08:13:45 +0100972 rate_flags |= BIT(il->scan_tx_ant[band]) << RATE_MCS_ANT_POS;
973 scan->tx_cmd.rate_n_flags = cpu_to_le32(rate | rate_flags);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200974
975 /* In power save mode use one chain, otherwise use all chains */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100976 if (test_bit(S_POWER_PMI, &il->status)) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200977 /* rx_ant has been set to all valid chains previously */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100978 active_chains =
979 rx_ant & ((u8) (il->chain_noise_data.active_chains));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200980 if (!active_chains)
981 active_chains = rx_ant;
982
983 D_SCAN("chain_noise_data.active_chains: %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100984 il->chain_noise_data.active_chains);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200985
986 rx_ant = il4965_first_antenna(active_chains);
987 }
988
989 /* MIMO is not used here, but value is required */
990 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
991 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
992 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
993 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
994 scan->rx_chain = cpu_to_le16(rx_chain);
995
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100996 cmd_len =
997 il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
998 vif->addr, il->scan_request->ie,
999 il->scan_request->ie_len,
1000 IL_MAX_SCAN_SIZE - sizeof(*scan));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001001 scan->tx_cmd.len = cpu_to_le16(cmd_len);
1002
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001003 scan->filter_flags |=
1004 (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001005
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001006 scan->channel_count =
1007 il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
1008 (void *)&scan->data[cmd_len]);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001009 if (scan->channel_count == 0) {
1010 D_SCAN("channel count %d\n", scan->channel_count);
1011 return -EIO;
1012 }
1013
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001014 cmd.len +=
1015 le16_to_cpu(scan->tx_cmd.len) +
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001016 scan->channel_count * sizeof(struct il_scan_channel);
1017 cmd.data = scan;
1018 scan->len = cpu_to_le16(cmd.len);
1019
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001020 set_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001021
1022 ret = il_send_cmd_sync(il, &cmd);
1023 if (ret)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001024 clear_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001025
1026 return ret;
1027}
1028
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001029int
1030il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
1031 bool add)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001032{
1033 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1034
1035 if (add)
1036 return il4965_add_bssid_station(il, vif_priv->ctx,
1037 vif->bss_conf.bssid,
1038 &vif_priv->ibss_bssid_sta_id);
1039 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001040 vif->bss_conf.bssid);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001041}
1042
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001043void
1044il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001045{
1046 lockdep_assert_held(&il->sta_lock);
1047
1048 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1049 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1050 else {
1051 D_TX("free more than tfds_in_queue (%u:%d)\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001052 il->stations[sta_id].tid[tid].tfds_in_queue, freed);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001053 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1054 }
1055}
1056
1057#define IL_TX_QUEUE_MSK 0xfffff
1058
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001059static bool
1060il4965_is_single_rx_stream(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001061{
1062 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001063 il->current_ht_config.single_chain_sufficient;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001064}
1065
1066#define IL_NUM_RX_CHAINS_MULTIPLE 3
1067#define IL_NUM_RX_CHAINS_SINGLE 2
1068#define IL_NUM_IDLE_CHAINS_DUAL 2
1069#define IL_NUM_IDLE_CHAINS_SINGLE 1
1070
1071/*
1072 * Determine how many receiver/antenna chains to use.
1073 *
1074 * More provides better reception via diversity. Fewer saves power
1075 * at the expense of throughput, but only when not in powersave to
1076 * start with.
1077 *
1078 * MIMO (dual stream) requires at least 2, but works better with 3.
1079 * This does not determine *which* chains to use, just how many.
1080 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001081static int
1082il4965_get_active_rx_chain_count(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001083{
1084 /* # of Rx chains to use when expecting MIMO. */
1085 if (il4965_is_single_rx_stream(il))
1086 return IL_NUM_RX_CHAINS_SINGLE;
1087 else
1088 return IL_NUM_RX_CHAINS_MULTIPLE;
1089}
1090
1091/*
1092 * When we are in power saving mode, unless device support spatial
1093 * multiplexing power save, use the active count for rx chain count.
1094 */
1095static int
1096il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1097{
1098 /* # Rx chains when idling, depending on SMPS mode */
1099 switch (il->current_ht_config.smps) {
1100 case IEEE80211_SMPS_STATIC:
1101 case IEEE80211_SMPS_DYNAMIC:
1102 return IL_NUM_IDLE_CHAINS_SINGLE;
1103 case IEEE80211_SMPS_OFF:
1104 return active_cnt;
1105 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001106 WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001107 return active_cnt;
1108 }
1109}
1110
1111/* up to 4 chains */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001112static u8
1113il4965_count_chain_bitmap(u32 chain_bitmap)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001114{
1115 u8 res;
1116 res = (chain_bitmap & BIT(0)) >> 0;
1117 res += (chain_bitmap & BIT(1)) >> 1;
1118 res += (chain_bitmap & BIT(2)) >> 2;
1119 res += (chain_bitmap & BIT(3)) >> 3;
1120 return res;
1121}
1122
1123/**
1124 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1125 *
1126 * Selects how many and which Rx receivers/antennas/chains to use.
1127 * This should not be used for scan command ... it puts data in wrong place.
1128 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001129void
1130il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001131{
1132 bool is_single = il4965_is_single_rx_stream(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001133 bool is_cam = !test_bit(S_POWER_PMI, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001134 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1135 u32 active_chains;
1136 u16 rx_chain;
1137
1138 /* Tell uCode which antennas are actually connected.
1139 * Before first association, we assume all antennas are connected.
1140 * Just after first association, il4965_chain_noise_calibration()
1141 * checks which antennas actually *are* connected. */
1142 if (il->chain_noise_data.active_chains)
1143 active_chains = il->chain_noise_data.active_chains;
1144 else
1145 active_chains = il->hw_params.valid_rx_ant;
1146
1147 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1148
1149 /* How many receivers should we use? */
1150 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1151 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1152
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001153 /* correct rx chain count according hw settings
1154 * and chain noise calibration
1155 */
1156 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1157 if (valid_rx_cnt < active_rx_cnt)
1158 active_rx_cnt = valid_rx_cnt;
1159
1160 if (valid_rx_cnt < idle_rx_cnt)
1161 idle_rx_cnt = valid_rx_cnt;
1162
1163 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001164 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001165
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001166 il->staging.rx_chain = cpu_to_le16(rx_chain);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001167
1168 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001169 il->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001170 else
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001171 il->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001172
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01001173 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", il->staging.rx_chain,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001174 active_rx_cnt, idle_rx_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001175
1176 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1177 active_rx_cnt < idle_rx_cnt);
1178}
1179
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001180static const char *
1181il4965_get_fh_string(int cmd)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001182{
1183 switch (cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001184 IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
1185 IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
1186 IL_CMD(FH49_RSCSR_CHNL0_WPTR);
1187 IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
1188 IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
1189 IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
1190 IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
1191 IL_CMD(FH49_TSSR_TX_STATUS_REG);
1192 IL_CMD(FH49_TSSR_TX_ERROR_REG);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001193 default:
1194 return "UNKNOWN";
1195 }
1196}
1197
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001198int
1199il4965_dump_fh(struct il_priv *il, char **buf, bool display)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001200{
1201 int i;
1202#ifdef CONFIG_IWLEGACY_DEBUG
1203 int pos = 0;
1204 size_t bufsz = 0;
1205#endif
1206 static const u32 fh_tbl[] = {
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02001207 FH49_RSCSR_CHNL0_STTS_WPTR_REG,
1208 FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
1209 FH49_RSCSR_CHNL0_WPTR,
1210 FH49_MEM_RCSR_CHNL0_CONFIG_REG,
1211 FH49_MEM_RSSR_SHARED_CTRL_REG,
1212 FH49_MEM_RSSR_RX_STATUS_REG,
1213 FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
1214 FH49_TSSR_TX_STATUS_REG,
1215 FH49_TSSR_TX_ERROR_REG
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001216 };
1217#ifdef CONFIG_IWLEGACY_DEBUG
1218 if (display) {
1219 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
1220 *buf = kmalloc(bufsz, GFP_KERNEL);
1221 if (!*buf)
1222 return -ENOMEM;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001223 pos +=
1224 scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001225 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001226 pos +=
1227 scnprintf(*buf + pos, bufsz - pos,
1228 " %34s: 0X%08x\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001229 il4965_get_fh_string(fh_tbl[i]),
1230 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001231 }
1232 return pos;
1233 }
1234#endif
1235 IL_ERR("FH register values:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001236 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1237 IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
1238 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001239 }
1240 return 0;
1241}
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001242
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001243void
1244il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001245{
1246 struct il_rx_pkt *pkt = rxb_addr(rxb);
1247 struct il_missed_beacon_notif *missed_beacon;
1248
1249 missed_beacon = &pkt->u.missed_beacon;
1250 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
1251 il->missed_beacon_threshold) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001252 D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
1253 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
1254 le32_to_cpu(missed_beacon->total_missed_becons),
1255 le32_to_cpu(missed_beacon->num_recvd_beacons),
1256 le32_to_cpu(missed_beacon->num_expected_beacons));
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001257 if (!test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001258 il4965_init_sensitivity(il);
1259 }
1260}
1261
1262/* Calculate noise level, based on measurements during network silence just
1263 * before arriving beacon. This measurement can be done only if we know
1264 * exactly when to expect beacons, therefore only when we're associated. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001265static void
1266il4965_rx_calc_noise(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001267{
1268 struct stats_rx_non_phy *rx_info;
1269 int num_active_rx = 0;
1270 int total_silence = 0;
1271 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
1272 int last_rx_noise;
1273
1274 rx_info = &(il->_4965.stats.rx.general);
1275 bcn_silence_a =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001276 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001277 bcn_silence_b =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001278 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001279 bcn_silence_c =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001280 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001281
1282 if (bcn_silence_a) {
1283 total_silence += bcn_silence_a;
1284 num_active_rx++;
1285 }
1286 if (bcn_silence_b) {
1287 total_silence += bcn_silence_b;
1288 num_active_rx++;
1289 }
1290 if (bcn_silence_c) {
1291 total_silence += bcn_silence_c;
1292 num_active_rx++;
1293 }
1294
1295 /* Average among active antennas */
1296 if (num_active_rx)
1297 last_rx_noise = (total_silence / num_active_rx) - 107;
1298 else
1299 last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
1300
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001301 D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
1302 bcn_silence_b, bcn_silence_c, last_rx_noise);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001303}
1304
1305#ifdef CONFIG_IWLEGACY_DEBUGFS
1306/*
1307 * based on the assumption of all stats counter are in DWORD
1308 * FIXME: This function is for debugging, do not deal with
1309 * the case of counters roll-over.
1310 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001311static void
1312il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001313{
1314 int i, size;
1315 __le32 *prev_stats;
1316 u32 *accum_stats;
1317 u32 *delta, *max_delta;
1318 struct stats_general_common *general, *accum_general;
1319 struct stats_tx *tx, *accum_tx;
1320
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001321 prev_stats = (__le32 *) &il->_4965.stats;
1322 accum_stats = (u32 *) &il->_4965.accum_stats;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001323 size = sizeof(struct il_notif_stats);
1324 general = &il->_4965.stats.general.common;
1325 accum_general = &il->_4965.accum_stats.general.common;
1326 tx = &il->_4965.stats.tx;
1327 accum_tx = &il->_4965.accum_stats.tx;
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001328 delta = (u32 *) &il->_4965.delta_stats;
1329 max_delta = (u32 *) &il->_4965.max_delta;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001330
1331 for (i = sizeof(__le32); i < size;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001332 i +=
1333 sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
1334 accum_stats++) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001335 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001336 *delta =
1337 (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001338 *accum_stats += *delta;
1339 if (*delta > *max_delta)
1340 *max_delta = *delta;
1341 }
1342 }
1343
1344 /* reset accumulative stats for "no-counter" type stats */
1345 accum_general->temperature = general->temperature;
1346 accum_general->ttl_timestamp = general->ttl_timestamp;
1347}
1348#endif
1349
1350#define REG_RECALIB_PERIOD (60)
1351
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001352void
1353il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001354{
1355 int change;
1356 struct il_rx_pkt *pkt = rxb_addr(rxb);
1357
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001358 D_RX("Statistics notification received (%d vs %d).\n",
1359 (int)sizeof(struct il_notif_stats),
1360 le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001361
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001362 change =
1363 ((il->_4965.stats.general.common.temperature !=
1364 pkt->u.stats.general.common.temperature) ||
1365 ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
1366 (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001367#ifdef CONFIG_IWLEGACY_DEBUGFS
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01001368 il4965_accumulative_stats(il, (__le32 *) &pkt->u.stats);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001369#endif
1370
1371 /* TODO: reading some of stats is unneeded */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001372 memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001373
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001374 set_bit(S_STATS, &il->status);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001375
1376 /* Reschedule the stats timer to occur in
1377 * REG_RECALIB_PERIOD seconds to ensure we get a
1378 * thermal update even if the uCode doesn't give
1379 * us one */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001380 mod_timer(&il->stats_periodic,
1381 jiffies + msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001382
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001383 if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001384 (pkt->hdr.cmd == N_STATS)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001385 il4965_rx_calc_noise(il);
1386 queue_work(il->workqueue, &il->run_time_calib_work);
1387 }
1388 if (il->cfg->ops->lib->temp_ops.temperature && change)
1389 il->cfg->ops->lib->temp_ops.temperature(il);
1390}
1391
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001392void
1393il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001394{
1395 struct il_rx_pkt *pkt = rxb_addr(rxb);
1396
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001397 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001398#ifdef CONFIG_IWLEGACY_DEBUGFS
1399 memset(&il->_4965.accum_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001400 sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001401 memset(&il->_4965.delta_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001402 sizeof(struct il_notif_stats));
1403 memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001404#endif
1405 D_RX("Statistics have been cleared\n");
1406 }
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01001407 il4965_hdl_stats(il, rxb);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001408}
1409
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001410
1411/*
1412 * mac80211 queues, ACs, hardware queues, FIFOs.
1413 *
1414 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
1415 *
1416 * Mac80211 uses the following numbers, which we get as from it
1417 * by way of skb_get_queue_mapping(skb):
1418 *
1419 * VO 0
1420 * VI 1
1421 * BE 2
1422 * BK 3
1423 *
1424 *
1425 * Regular (not A-MPDU) frames are put into hardware queues corresponding
1426 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
1427 * own queue per aggregation session (RA/TID combination), such queues are
1428 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
1429 * order to map frames to the right queue, we also need an AC->hw queue
1430 * mapping. This is implemented here.
1431 *
1432 * Due to the way hw queues are set up (by the hw specific modules like
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +02001433 * 4965.c), the AC->hw queue mapping is the identity
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001434 * mapping.
1435 */
1436
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001437static const u8 tid_to_ac[] = {
1438 IEEE80211_AC_BE,
1439 IEEE80211_AC_BK,
1440 IEEE80211_AC_BK,
1441 IEEE80211_AC_BE,
1442 IEEE80211_AC_VI,
1443 IEEE80211_AC_VI,
1444 IEEE80211_AC_VO,
1445 IEEE80211_AC_VO
1446};
1447
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001448static inline int
1449il4965_get_ac_from_tid(u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001450{
1451 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1452 return tid_to_ac[tid];
1453
1454 /* no support for TIDs 8-15 yet */
1455 return -EINVAL;
1456}
1457
1458static inline int
1459il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
1460{
Stanislaw Gruszkab75b3a72012-02-03 17:31:53 +01001461 const u8 ac_to_fifo[] = {
1462 IL_TX_FIFO_VO,
1463 IL_TX_FIFO_VI,
1464 IL_TX_FIFO_BE,
1465 IL_TX_FIFO_BK,
1466 };
1467
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001468 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
Stanislaw Gruszkab75b3a72012-02-03 17:31:53 +01001469 return ac_to_fifo[tid_to_ac[tid]];
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001470
1471 /* no support for TIDs 8-15 yet */
1472 return -EINVAL;
1473}
1474
1475/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001476 * handle build C_TX command notification.
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001477 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001478static void
1479il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
1480 struct il_tx_cmd *tx_cmd,
1481 struct ieee80211_tx_info *info,
1482 struct ieee80211_hdr *hdr, u8 std_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001483{
1484 __le16 fc = hdr->frame_control;
1485 __le32 tx_flags = tx_cmd->tx_flags;
1486
1487 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1488 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
1489 tx_flags |= TX_CMD_FLG_ACK_MSK;
1490 if (ieee80211_is_mgmt(fc))
1491 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1492 if (ieee80211_is_probe_resp(fc) &&
1493 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1494 tx_flags |= TX_CMD_FLG_TSF_MSK;
1495 } else {
1496 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1497 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1498 }
1499
1500 if (ieee80211_is_back_req(fc))
1501 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1502
1503 tx_cmd->sta_id = std_id;
1504 if (ieee80211_has_morefrags(fc))
1505 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1506
1507 if (ieee80211_is_data_qos(fc)) {
1508 u8 *qc = ieee80211_get_qos_ctl(hdr);
1509 tx_cmd->tid_tspec = qc[0] & 0xf;
1510 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1511 } else {
1512 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1513 }
1514
1515 il_tx_cmd_protection(il, info, fc, &tx_flags);
1516
1517 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1518 if (ieee80211_is_mgmt(fc)) {
1519 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
1520 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
1521 else
1522 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
1523 } else {
1524 tx_cmd->timeout.pm_frame_timeout = 0;
1525 }
1526
1527 tx_cmd->driver_txop = 0;
1528 tx_cmd->tx_flags = tx_flags;
1529 tx_cmd->next_frame_len = 0;
1530}
1531
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001532static void
1533il4965_tx_cmd_build_rate(struct il_priv *il, struct il_tx_cmd *tx_cmd,
1534 struct ieee80211_tx_info *info, __le16 fc)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001535{
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001536 const u8 rts_retry_limit = 60;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001537 u32 rate_flags;
1538 int rate_idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001539 u8 data_retry_limit;
1540 u8 rate_plcp;
1541
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001542 /* Set retry limit on DATA packets and Probe Responses */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001543 if (ieee80211_is_probe_resp(fc))
1544 data_retry_limit = 3;
1545 else
1546 data_retry_limit = IL4965_DEFAULT_TX_RETRY;
1547 tx_cmd->data_retry_limit = data_retry_limit;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001548 /* Set retry limit on RTS packets */
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001549 tx_cmd->rts_retry_limit = min(data_retry_limit, rts_retry_limit);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001550
1551 /* DATA packets will use the uCode station table for rate/antenna
1552 * selection */
1553 if (ieee80211_is_data(fc)) {
1554 tx_cmd->initial_rate_idx = 0;
1555 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1556 return;
1557 }
1558
1559 /**
1560 * If the current TX rate stored in mac80211 has the MCS bit set, it's
1561 * not really a TX rate. Thus, we use the lowest supported rate for
1562 * this band. Also use the lowest supported rate if the stored rate
1563 * idx is invalid.
1564 */
1565 rate_idx = info->control.rates[0].idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001566 if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
1567 || rate_idx > RATE_COUNT_LEGACY)
1568 rate_idx =
1569 rate_lowest_index(&il->bands[info->band],
1570 info->control.sta);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001571 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
1572 if (info->band == IEEE80211_BAND_5GHZ)
1573 rate_idx += IL_FIRST_OFDM_RATE;
1574 /* Get PLCP rate for tx_cmd->rate_n_flags */
1575 rate_plcp = il_rates[rate_idx].plcp;
1576 /* Zero out flags for this packet */
1577 rate_flags = 0;
1578
1579 /* Set CCK flag as needed */
1580 if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
1581 rate_flags |= RATE_MCS_CCK_MSK;
1582
1583 /* Set up antennas */
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +01001584 il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001585 rate_flags |= BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001586
1587 /* Set the rate in the TX cmd */
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01001588 tx_cmd->rate_n_flags = cpu_to_le32(rate_plcp | rate_flags);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001589}
1590
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001591static void
1592il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
1593 struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
1594 int sta_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001595{
1596 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1597
1598 switch (keyconf->cipher) {
1599 case WLAN_CIPHER_SUITE_CCMP:
1600 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1601 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1602 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1603 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1604 D_TX("tx_cmd with AES hwcrypto\n");
1605 break;
1606
1607 case WLAN_CIPHER_SUITE_TKIP:
1608 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1609 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1610 D_TX("tx_cmd with tkip hwcrypto\n");
1611 break;
1612
1613 case WLAN_CIPHER_SUITE_WEP104:
1614 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1615 /* fall through */
1616 case WLAN_CIPHER_SUITE_WEP40:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001617 tx_cmd->sec_ctl |=
1618 (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
1619 TX_CMD_SEC_SHIFT);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001620
1621 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1622
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001623 D_TX("Configuring packet for WEP encryption " "with key %d\n",
1624 keyconf->keyidx);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001625 break;
1626
1627 default:
1628 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1629 break;
1630 }
1631}
1632
1633/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001634 * start C_TX command process
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001635 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001636int
1637il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001638{
1639 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1640 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1641 struct ieee80211_sta *sta = info->control.sta;
1642 struct il_station_priv *sta_priv = NULL;
1643 struct il_tx_queue *txq;
1644 struct il_queue *q;
1645 struct il_device_cmd *out_cmd;
1646 struct il_cmd_meta *out_meta;
1647 struct il_tx_cmd *tx_cmd;
1648 struct il_rxon_context *ctx = &il->ctx;
1649 int txq_id;
1650 dma_addr_t phys_addr;
1651 dma_addr_t txcmd_phys;
1652 dma_addr_t scratch_phys;
1653 u16 len, firstlen, secondlen;
1654 u16 seq_number = 0;
1655 __le16 fc;
1656 u8 hdr_len;
1657 u8 sta_id;
1658 u8 wait_write_ptr = 0;
1659 u8 tid = 0;
1660 u8 *qc = NULL;
1661 unsigned long flags;
1662 bool is_agg = false;
1663
1664 if (info->control.vif)
1665 ctx = il_rxon_ctx_from_vif(info->control.vif);
1666
1667 spin_lock_irqsave(&il->lock, flags);
1668 if (il_is_rfkill(il)) {
1669 D_DROP("Dropping - RF KILL\n");
1670 goto drop_unlock;
1671 }
1672
1673 fc = hdr->frame_control;
1674
1675#ifdef CONFIG_IWLEGACY_DEBUG
1676 if (ieee80211_is_auth(fc))
1677 D_TX("Sending AUTH frame\n");
1678 else if (ieee80211_is_assoc_req(fc))
1679 D_TX("Sending ASSOC frame\n");
1680 else if (ieee80211_is_reassoc_req(fc))
1681 D_TX("Sending REASSOC frame\n");
1682#endif
1683
1684 hdr_len = ieee80211_hdrlen(fc);
1685
1686 /* For management frames use broadcast id to do not break aggregation */
1687 if (!ieee80211_is_data(fc))
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01001688 sta_id = il->hw_params.bcast_id;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001689 else {
1690 /* Find idx into station table for destination station */
1691 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1692
1693 if (sta_id == IL_INVALID_STATION) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001694 D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001695 goto drop_unlock;
1696 }
1697 }
1698
1699 D_TX("station Id %d\n", sta_id);
1700
1701 if (sta)
1702 sta_priv = (void *)sta->drv_priv;
1703
1704 if (sta_priv && sta_priv->asleep &&
1705 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1706 /*
1707 * This sends an asynchronous command to the device,
1708 * but we can rely on it being processed before the
1709 * next frame is processed -- and the next frame to
1710 * this station is the one that will consume this
1711 * counter.
1712 * For now set the counter to just 1 since we do not
1713 * support uAPSD yet.
1714 */
1715 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1716 }
1717
Stanislaw Gruszkad1e14e92012-02-03 17:31:47 +01001718 /* FIXME: remove me ? */
1719 WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
1720
1721 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001722
1723 /* irqs already disabled/saved above when locking il->lock */
1724 spin_lock(&il->sta_lock);
1725
1726 if (ieee80211_is_data_qos(fc)) {
1727 qc = ieee80211_get_qos_ctl(hdr);
1728 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1729 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1730 spin_unlock(&il->sta_lock);
1731 goto drop_unlock;
1732 }
1733 seq_number = il->stations[sta_id].tid[tid].seq_number;
1734 seq_number &= IEEE80211_SCTL_SEQ;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001735 hdr->seq_ctrl =
1736 hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001737 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1738 seq_number += 0x10;
1739 /* aggregation is on for this <sta,tid> */
1740 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1741 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1742 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1743 is_agg = true;
1744 }
1745 }
1746
1747 txq = &il->txq[txq_id];
1748 q = &txq->q;
1749
1750 if (unlikely(il_queue_space(q) < q->high_mark)) {
1751 spin_unlock(&il->sta_lock);
1752 goto drop_unlock;
1753 }
1754
1755 if (ieee80211_is_data_qos(fc)) {
1756 il->stations[sta_id].tid[tid].tfds_in_queue++;
1757 if (!ieee80211_has_morefrags(fc))
1758 il->stations[sta_id].tid[tid].seq_number = seq_number;
1759 }
1760
1761 spin_unlock(&il->sta_lock);
1762
1763 /* Set up driver data for this TFD */
1764 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1765 txq->txb[q->write_ptr].skb = skb;
1766 txq->txb[q->write_ptr].ctx = ctx;
1767
1768 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1769 out_cmd = txq->cmd[q->write_ptr];
1770 out_meta = &txq->meta[q->write_ptr];
1771 tx_cmd = &out_cmd->cmd.tx;
1772 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1773 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1774
1775 /*
1776 * Set up the Tx-command (not MAC!) header.
1777 * Store the chosen Tx queue and TFD idx within the sequence field;
1778 * after Tx, uCode's Tx response will return this value so driver can
1779 * locate the frame within the tx queue and do post-tx processing.
1780 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001781 out_cmd->hdr.cmd = C_TX;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001782 out_cmd->hdr.sequence =
1783 cpu_to_le16((u16)
1784 (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001785
1786 /* Copy MAC header from skb into command buffer */
1787 memcpy(tx_cmd->hdr, hdr, hdr_len);
1788
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001789 /* Total # bytes to be transmitted */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001790 len = (u16) skb->len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001791 tx_cmd->len = cpu_to_le16(len);
1792
1793 if (info->control.hw_key)
1794 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1795
1796 /* TODO need this for burst mode later on */
1797 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1798 il_dbg_log_tx_data_frame(il, len, hdr);
1799
1800 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1801
1802 il_update_stats(il, true, fc, len);
1803 /*
1804 * Use the first empty entry in this queue's command buffer array
1805 * to contain the Tx command and MAC header concatenated together
1806 * (payload data will be in another buffer).
1807 * Size of this varies, due to varying MAC header length.
1808 * If end is not dword aligned, we'll have 2 extra bytes at the end
1809 * of the MAC header (device reads on dword boundaries).
1810 * We'll tell device about this padding later.
1811 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001812 len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001813 firstlen = (len + 3) & ~3;
1814
1815 /* Tell NIC about any 2-byte padding after MAC header */
1816 if (firstlen != len)
1817 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1818
1819 /* Physical address of this Tx command's header (not MAC header!),
1820 * within command buffer array. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001821 txcmd_phys =
1822 pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
1823 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001824 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1825 dma_unmap_len_set(out_meta, len, firstlen);
1826 /* Add buffer containing Tx command and MAC(!) header to TFD's
1827 * first entry */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001828 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen,
1829 1, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001830
1831 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1832 txq->need_update = 1;
1833 } else {
1834 wait_write_ptr = 1;
1835 txq->need_update = 0;
1836 }
1837
1838 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1839 * if any (802.11 null frames have no payload). */
1840 secondlen = skb->len - hdr_len;
1841 if (secondlen > 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001842 phys_addr =
1843 pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
1844 PCI_DMA_TODEVICE);
1845 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr,
1846 secondlen, 0, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001847 }
1848
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001849 scratch_phys =
1850 txcmd_phys + sizeof(struct il_cmd_header) +
1851 offsetof(struct il_tx_cmd, scratch);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001852
1853 /* take back ownership of DMA buffer to enable update */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001854 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
1855 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001856 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1857 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1858
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001859 D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001860 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001861 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
1862 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001863
1864 /* Set up entry for this TFD in Tx byte-count array */
1865 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1866 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001867 le16_to_cpu(tx_cmd->
1868 len));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001869
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001870 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
1871 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001872
1873 /* Tell device the write idx *just past* this latest filled TFD */
1874 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1875 il_txq_update_write_ptr(il, txq);
1876 spin_unlock_irqrestore(&il->lock, flags);
1877
1878 /*
1879 * At this point the frame is "transmitted" successfully
1880 * and we will get a TX status notification eventually,
1881 * regardless of the value of ret. "ret" only indicates
1882 * whether or not we should update the write pointer.
1883 */
1884
1885 /*
1886 * Avoid atomic ops if it isn't an associated client.
1887 * Also, if this is a packet for aggregation, don't
1888 * increase the counter because the ucode will stop
1889 * aggregation queues when their respective station
1890 * goes to sleep.
1891 */
1892 if (sta_priv && sta_priv->client && !is_agg)
1893 atomic_inc(&sta_priv->pending_frames);
1894
1895 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1896 if (wait_write_ptr) {
1897 spin_lock_irqsave(&il->lock, flags);
1898 txq->need_update = 1;
1899 il_txq_update_write_ptr(il, txq);
1900 spin_unlock_irqrestore(&il->lock, flags);
1901 } else {
1902 il_stop_queue(il, txq);
1903 }
1904 }
1905
1906 return 0;
1907
1908drop_unlock:
1909 spin_unlock_irqrestore(&il->lock, flags);
1910 return -1;
1911}
1912
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001913static inline int
1914il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001915{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001916 ptr->addr =
1917 dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001918 if (!ptr->addr)
1919 return -ENOMEM;
1920 ptr->size = size;
1921 return 0;
1922}
1923
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001924static inline void
1925il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001926{
1927 if (unlikely(!ptr->addr))
1928 return;
1929
1930 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1931 memset(ptr, 0, sizeof(*ptr));
1932}
1933
1934/**
1935 * il4965_hw_txq_ctx_free - Free TXQ Context
1936 *
1937 * Destroy all TX DMA queues and structures
1938 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001939void
1940il4965_hw_txq_ctx_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001941{
1942 int txq_id;
1943
1944 /* Tx queues */
1945 if (il->txq) {
1946 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1947 if (txq_id == il->cmd_queue)
1948 il_cmd_queue_free(il);
1949 else
1950 il_tx_queue_free(il, txq_id);
1951 }
1952 il4965_free_dma_ptr(il, &il->kw);
1953
1954 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1955
1956 /* free tx queue structure */
1957 il_txq_mem(il);
1958}
1959
1960/**
1961 * il4965_txq_ctx_alloc - allocate TX queue context
1962 * Allocate all Tx DMA structures and initialize them
1963 *
1964 * @param il
1965 * @return error code
1966 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001967int
1968il4965_txq_ctx_alloc(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001969{
1970 int ret;
1971 int txq_id, slots_num;
1972 unsigned long flags;
1973
1974 /* Free all tx/cmd queues and keep-warm buffer */
1975 il4965_hw_txq_ctx_free(il);
1976
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001977 ret =
1978 il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
1979 il->hw_params.scd_bc_tbls_size);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001980 if (ret) {
1981 IL_ERR("Scheduler BC Table allocation failed\n");
1982 goto error_bc_tbls;
1983 }
1984 /* Alloc keep-warm buffer */
1985 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
1986 if (ret) {
1987 IL_ERR("Keep Warm allocation failed\n");
1988 goto error_kw;
1989 }
1990
1991 /* allocate tx queue structure */
1992 ret = il_alloc_txq_mem(il);
1993 if (ret)
1994 goto error;
1995
1996 spin_lock_irqsave(&il->lock, flags);
1997
1998 /* Turn off all Tx DMA fifos */
1999 il4965_txq_set_sched(il, 0);
2000
2001 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002002 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002003
2004 spin_unlock_irqrestore(&il->lock, flags);
2005
2006 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2007 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002008 slots_num =
2009 (txq_id ==
2010 il->cmd_queue) ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2011 ret = il_tx_queue_init(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002012 if (ret) {
2013 IL_ERR("Tx %d queue init failed\n", txq_id);
2014 goto error;
2015 }
2016 }
2017
2018 return ret;
2019
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002020error:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002021 il4965_hw_txq_ctx_free(il);
2022 il4965_free_dma_ptr(il, &il->kw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002023error_kw:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002024 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002025error_bc_tbls:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002026 return ret;
2027}
2028
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002029void
2030il4965_txq_ctx_reset(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002031{
2032 int txq_id, slots_num;
2033 unsigned long flags;
2034
2035 spin_lock_irqsave(&il->lock, flags);
2036
2037 /* Turn off all Tx DMA fifos */
2038 il4965_txq_set_sched(il, 0);
2039
2040 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002041 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002042
2043 spin_unlock_irqrestore(&il->lock, flags);
2044
2045 /* Alloc and init all Tx queues, including the command queue (#4) */
2046 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002047 slots_num =
2048 txq_id == il->cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2049 il_tx_queue_reset(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002050 }
2051}
2052
2053/**
2054 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2055 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002056void
2057il4965_txq_ctx_stop(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002058{
2059 int ch, txq_id;
2060 unsigned long flags;
2061
2062 /* Turn off all Tx DMA fifos */
2063 spin_lock_irqsave(&il->lock, flags);
2064
2065 il4965_txq_set_sched(il, 0);
2066
2067 /* Stop each Tx DMA channel, and wait for it to be idle */
2068 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002069 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2070 if (il_poll_bit
2071 (il, FH49_TSSR_TX_STATUS_REG,
2072 FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002073 IL_ERR("Failing on timeout while stopping"
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002074 " DMA channel %d [0x%08x]", ch,
2075 il_rd(il, FH49_TSSR_TX_STATUS_REG));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002076 }
2077 spin_unlock_irqrestore(&il->lock, flags);
2078
2079 if (!il->txq)
2080 return;
2081
2082 /* Unmap DMA from host system and free skb's */
2083 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2084 if (txq_id == il->cmd_queue)
2085 il_cmd_queue_unmap(il);
2086 else
2087 il_tx_queue_unmap(il, txq_id);
2088}
2089
2090/*
2091 * Find first available (lowest unused) Tx Queue, mark it "active".
2092 * Called only when finding queue for aggregation.
2093 * Should never return anything < 7, because they should already
2094 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2095 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002096static int
2097il4965_txq_ctx_activate_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002098{
2099 int txq_id;
2100
2101 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2102 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2103 return txq_id;
2104 return -1;
2105}
2106
2107/**
2108 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2109 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002110static void
2111il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002112{
2113 /* Simply stop the queue, but don't change any configuration;
2114 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002115 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002116 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
2117 (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002118}
2119
2120/**
2121 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2122 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002123static int
2124il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002125{
2126 u32 tbl_dw_addr;
2127 u32 tbl_dw;
2128 u16 scd_q2ratid;
2129
2130 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2131
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002132 tbl_dw_addr =
2133 il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002134
2135 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2136
2137 if (txq_id & 0x1)
2138 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2139 else
2140 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2141
2142 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2143
2144 return 0;
2145}
2146
2147/**
2148 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2149 *
2150 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2151 * i.e. it must be one of the higher queues used for aggregation
2152 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002153static int
2154il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
2155 int tid, u16 ssn_idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002156{
2157 unsigned long flags;
2158 u16 ra_tid;
2159 int ret;
2160
2161 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2162 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002163 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2164 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002165 txq_id, IL49_FIRST_AMPDU_QUEUE,
2166 IL49_FIRST_AMPDU_QUEUE +
2167 il->cfg->base_params->num_of_ampdu_queues - 1);
2168 return -EINVAL;
2169 }
2170
2171 ra_tid = BUILD_RAxTID(sta_id, tid);
2172
2173 /* Modify device's station table to Tx this TID */
2174 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2175 if (ret)
2176 return ret;
2177
2178 spin_lock_irqsave(&il->lock, flags);
2179
2180 /* Stop this Tx queue before configuring it */
2181 il4965_tx_queue_stop_scheduler(il, txq_id);
2182
2183 /* Map receiver-address / traffic-ID to this queue */
2184 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2185
2186 /* Set this queue as a chain-building queue */
2187 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2188
2189 /* Place first TFD at idx corresponding to start sequence number.
2190 * Assumes that ssn_idx is valid (!= 0xFFF) */
2191 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2192 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2193 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2194
2195 /* Set up Tx win size and frame limit for this queue */
2196 il_write_targ_mem(il,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002197 il->scd_base_addr +
2198 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2199 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
2200 & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002201
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002202 il_write_targ_mem(il,
2203 il->scd_base_addr +
2204 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2205 (SCD_FRAME_LIMIT <<
2206 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
2207 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002208
2209 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2210
2211 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2212 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2213
2214 spin_unlock_irqrestore(&il->lock, flags);
2215
2216 return 0;
2217}
2218
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002219int
2220il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2221 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002222{
2223 int sta_id;
2224 int tx_fifo;
2225 int txq_id;
2226 int ret;
2227 unsigned long flags;
2228 struct il_tid_data *tid_data;
2229
2230 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2231 if (unlikely(tx_fifo < 0))
2232 return tx_fifo;
2233
Greg Dietsche53611e02011-08-28 08:26:16 -05002234 D_HT("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002235
2236 sta_id = il_sta_id(sta);
2237 if (sta_id == IL_INVALID_STATION) {
2238 IL_ERR("Start AGG on invalid station\n");
2239 return -ENXIO;
2240 }
2241 if (unlikely(tid >= MAX_TID_COUNT))
2242 return -EINVAL;
2243
2244 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2245 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2246 return -ENXIO;
2247 }
2248
2249 txq_id = il4965_txq_ctx_activate_free(il);
2250 if (txq_id == -1) {
2251 IL_ERR("No free aggregation queue available\n");
2252 return -ENXIO;
2253 }
2254
2255 spin_lock_irqsave(&il->sta_lock, flags);
2256 tid_data = &il->stations[sta_id].tid[tid];
2257 *ssn = SEQ_TO_SN(tid_data->seq_number);
2258 tid_data->agg.txq_id = txq_id;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002259 il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002260 spin_unlock_irqrestore(&il->sta_lock, flags);
2261
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002262 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002263 if (ret)
2264 return ret;
2265
2266 spin_lock_irqsave(&il->sta_lock, flags);
2267 tid_data = &il->stations[sta_id].tid[tid];
2268 if (tid_data->tfds_in_queue == 0) {
2269 D_HT("HW queue is empty\n");
2270 tid_data->agg.state = IL_AGG_ON;
2271 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2272 } else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002273 D_HT("HW queue is NOT empty: %d packets in HW queue\n",
2274 tid_data->tfds_in_queue);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002275 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2276 }
2277 spin_unlock_irqrestore(&il->sta_lock, flags);
2278 return ret;
2279}
2280
2281/**
2282 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2283 * il->lock must be held by the caller
2284 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002285static int
2286il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002287{
2288 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2289 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002290 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2291 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002292 txq_id, IL49_FIRST_AMPDU_QUEUE,
2293 IL49_FIRST_AMPDU_QUEUE +
2294 il->cfg->base_params->num_of_ampdu_queues - 1);
2295 return -EINVAL;
2296 }
2297
2298 il4965_tx_queue_stop_scheduler(il, txq_id);
2299
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002300 il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002301
2302 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2303 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2304 /* supposes that ssn_idx is valid (!= 0xFFF) */
2305 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2306
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002307 il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002308 il_txq_ctx_deactivate(il, txq_id);
2309 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2310
2311 return 0;
2312}
2313
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002314int
2315il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2316 struct ieee80211_sta *sta, u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002317{
2318 int tx_fifo_id, txq_id, sta_id, ssn;
2319 struct il_tid_data *tid_data;
2320 int write_ptr, read_ptr;
2321 unsigned long flags;
2322
2323 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2324 if (unlikely(tx_fifo_id < 0))
2325 return tx_fifo_id;
2326
2327 sta_id = il_sta_id(sta);
2328
2329 if (sta_id == IL_INVALID_STATION) {
2330 IL_ERR("Invalid station for AGG tid %d\n", tid);
2331 return -ENXIO;
2332 }
2333
2334 spin_lock_irqsave(&il->sta_lock, flags);
2335
2336 tid_data = &il->stations[sta_id].tid[tid];
2337 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2338 txq_id = tid_data->agg.txq_id;
2339
2340 switch (il->stations[sta_id].tid[tid].agg.state) {
2341 case IL_EMPTYING_HW_QUEUE_ADDBA:
2342 /*
2343 * This can happen if the peer stops aggregation
2344 * again before we've had a chance to drain the
2345 * queue we selected previously, i.e. before the
2346 * session was really started completely.
2347 */
2348 D_HT("AGG stop before setup done\n");
2349 goto turn_off;
2350 case IL_AGG_ON:
2351 break;
2352 default:
2353 IL_WARN("Stopping AGG while state not ON or starting\n");
2354 }
2355
2356 write_ptr = il->txq[txq_id].q.write_ptr;
2357 read_ptr = il->txq[txq_id].q.read_ptr;
2358
2359 /* The queue is not empty */
2360 if (write_ptr != read_ptr) {
2361 D_HT("Stopping a non empty AGG HW QUEUE\n");
2362 il->stations[sta_id].tid[tid].agg.state =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002363 IL_EMPTYING_HW_QUEUE_DELBA;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002364 spin_unlock_irqrestore(&il->sta_lock, flags);
2365 return 0;
2366 }
2367
2368 D_HT("HW queue is empty\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002369turn_off:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002370 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2371
2372 /* do not restore/save irqs */
2373 spin_unlock(&il->sta_lock);
2374 spin_lock(&il->lock);
2375
2376 /*
2377 * the only reason this call can fail is queue number out of range,
2378 * which can happen if uCode is reloaded and all the station
2379 * information are lost. if it is outside the range, there is no need
2380 * to deactivate the uCode queue, just return "success" to allow
2381 * mac80211 to clean up it own data.
2382 */
2383 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2384 spin_unlock_irqrestore(&il->lock, flags);
2385
2386 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2387
2388 return 0;
2389}
2390
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002391int
2392il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002393{
2394 struct il_queue *q = &il->txq[txq_id].q;
2395 u8 *addr = il->stations[sta_id].sta.sta.addr;
2396 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2397 struct il_rxon_context *ctx;
2398
2399 ctx = &il->ctx;
2400
2401 lockdep_assert_held(&il->sta_lock);
2402
2403 switch (il->stations[sta_id].tid[tid].agg.state) {
2404 case IL_EMPTYING_HW_QUEUE_DELBA:
2405 /* We are reclaiming the last packet of the */
2406 /* aggregated HW queue */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002407 if (txq_id == tid_data->agg.txq_id &&
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002408 q->read_ptr == q->write_ptr) {
2409 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2410 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002411 D_HT("HW queue empty: continue DELBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002412 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2413 tid_data->agg.state = IL_AGG_OFF;
2414 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2415 }
2416 break;
2417 case IL_EMPTYING_HW_QUEUE_ADDBA:
2418 /* We are reclaiming the last packet of the queue */
2419 if (tid_data->tfds_in_queue == 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002420 D_HT("HW queue empty: continue ADDBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002421 tid_data->agg.state = IL_AGG_ON;
2422 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2423 }
2424 break;
2425 }
2426
2427 return 0;
2428}
2429
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002430static void
2431il4965_non_agg_tx_status(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002432 const u8 *addr1)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002433{
2434 struct ieee80211_sta *sta;
2435 struct il_station_priv *sta_priv;
2436
2437 rcu_read_lock();
2438 sta = ieee80211_find_sta(ctx->vif, addr1);
2439 if (sta) {
2440 sta_priv = (void *)sta->drv_priv;
2441 /* avoid atomic ops if this isn't a client */
2442 if (sta_priv->client &&
2443 atomic_dec_return(&sta_priv->pending_frames) == 0)
2444 ieee80211_sta_block_awake(il->hw, sta, false);
2445 }
2446 rcu_read_unlock();
2447}
2448
2449static void
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002450il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info, bool is_agg)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002451{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002452 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002453
2454 if (!is_agg)
2455 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2456
2457 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2458}
2459
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002460int
2461il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002462{
2463 struct il_tx_queue *txq = &il->txq[txq_id];
2464 struct il_queue *q = &txq->q;
2465 struct il_tx_info *tx_info;
2466 int nfreed = 0;
2467 struct ieee80211_hdr *hdr;
2468
2469 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2470 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002471 "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
2472 q->write_ptr, q->read_ptr);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002473 return 0;
2474 }
2475
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002476 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002477 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2478
2479 tx_info = &txq->txb[txq->q.read_ptr];
2480
2481 if (WARN_ON_ONCE(tx_info->skb == NULL))
2482 continue;
2483
2484 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2485 if (ieee80211_is_data_qos(hdr->frame_control))
2486 nfreed++;
2487
2488 il4965_tx_status(il, tx_info,
2489 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2490 tx_info->skb = NULL;
2491
2492 il->cfg->ops->lib->txq_free_tfd(il, txq);
2493 }
2494 return nfreed;
2495}
2496
2497/**
2498 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2499 *
2500 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2501 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2502 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002503static int
2504il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
2505 struct il_compressed_ba_resp *ba_resp)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002506{
2507 int i, sh, ack;
2508 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2509 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2510 int successes = 0;
2511 struct ieee80211_tx_info *info;
2512 u64 bitmap, sent_bitmap;
2513
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002514 if (unlikely(!agg->wait_for_ba)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002515 if (unlikely(ba_resp->bitmap))
2516 IL_ERR("Received BA when not expected\n");
2517 return -EINVAL;
2518 }
2519
2520 /* Mark that the expected block-ack response arrived */
2521 agg->wait_for_ba = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002522 D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002523
2524 /* Calculate shift to align block-ack bits with our Tx win bits */
2525 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002526 if (sh < 0) /* tbw something is wrong with indices */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002527 sh += 0x100;
2528
2529 if (agg->frame_count > (64 - sh)) {
2530 D_TX_REPLY("more frames than bitmap size");
2531 return -1;
2532 }
2533
2534 /* don't use 64-bit values for now */
2535 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2536
2537 /* check for success or failure according to the
2538 * transmitted bitmap and block-ack bitmap */
2539 sent_bitmap = bitmap & agg->bitmap;
2540
2541 /* For each frame attempted in aggregation,
2542 * update driver's record of tx frame's status. */
2543 i = 0;
2544 while (sent_bitmap) {
2545 ack = sent_bitmap & 1ULL;
2546 successes += ack;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002547 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
2548 i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002549 sent_bitmap >>= 1;
2550 ++i;
2551 }
2552
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002553 D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002554
2555 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2556 memset(&info->status, 0, sizeof(info->status));
2557 info->flags |= IEEE80211_TX_STAT_ACK;
2558 info->flags |= IEEE80211_TX_STAT_AMPDU;
2559 info->status.ampdu_ack_len = successes;
2560 info->status.ampdu_len = agg->frame_count;
2561 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2562
2563 return 0;
2564}
2565
2566/**
2567 * translate ucode response to mac80211 tx status control values
2568 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002569void
2570il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2571 struct ieee80211_tx_info *info)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002572{
2573 struct ieee80211_tx_rate *r = &info->control.rates[0];
2574
2575 info->antenna_sel_tx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002576 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002577 if (rate_n_flags & RATE_MCS_HT_MSK)
2578 r->flags |= IEEE80211_TX_RC_MCS;
2579 if (rate_n_flags & RATE_MCS_GF_MSK)
2580 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2581 if (rate_n_flags & RATE_MCS_HT40_MSK)
2582 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2583 if (rate_n_flags & RATE_MCS_DUP_MSK)
2584 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2585 if (rate_n_flags & RATE_MCS_SGI_MSK)
2586 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2587 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2588}
2589
2590/**
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02002591 * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002592 *
2593 * Handles block-acknowledge notification from device, which reports success
2594 * of frames sent via aggregation.
2595 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002596void
2597il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002598{
2599 struct il_rx_pkt *pkt = rxb_addr(rxb);
2600 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2601 struct il_tx_queue *txq = NULL;
2602 struct il_ht_agg *agg;
2603 int idx;
2604 int sta_id;
2605 int tid;
2606 unsigned long flags;
2607
2608 /* "flow" corresponds to Tx queue */
2609 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2610
2611 /* "ssn" is start of block-ack Tx win, corresponds to idx
2612 * (in Tx queue's circular buffer) of first TFD/frame in win */
2613 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2614
2615 if (scd_flow >= il->hw_params.max_txq_num) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002616 IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002617 return;
2618 }
2619
2620 txq = &il->txq[scd_flow];
2621 sta_id = ba_resp->sta_id;
2622 tid = ba_resp->tid;
2623 agg = &il->stations[sta_id].tid[tid].agg;
2624 if (unlikely(agg->txq_id != scd_flow)) {
2625 /*
2626 * FIXME: this is a uCode bug which need to be addressed,
2627 * log the information and return for now!
2628 * since it is possible happen very often and in order
2629 * not to fill the syslog, don't enable the logging by default
2630 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002631 D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
2632 scd_flow, agg->txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002633 return;
2634 }
2635
2636 /* Find idx just before block-ack win */
2637 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2638
2639 spin_lock_irqsave(&il->sta_lock, flags);
2640
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002641 D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002642 agg->wait_for_ba, (u8 *) &ba_resp->sta_addr_lo32,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002643 ba_resp->sta_id);
2644 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
2645 "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
2646 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2647 ba_resp->scd_flow, ba_resp->scd_ssn);
2648 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
2649 (unsigned long long)agg->bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002650
2651 /* Update driver's record of ACK vs. not for each frame in win */
2652 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2653
2654 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2655 * block-ack win (we assume that they've been successfully
2656 * transmitted ... if not, it's too late anyway). */
2657 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2658 /* calculate mac80211 ampdu sw queue to wake */
2659 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2660 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2661
2662 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2663 il->mac80211_registered &&
2664 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2665 il_wake_queue(il, txq);
2666
2667 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2668 }
2669
2670 spin_unlock_irqrestore(&il->sta_lock, flags);
2671}
2672
2673#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002674const char *
2675il4965_get_tx_fail_reason(u32 status)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002676{
2677#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2678#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2679
2680 switch (status & TX_STATUS_MSK) {
2681 case TX_STATUS_SUCCESS:
2682 return "SUCCESS";
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002683 TX_STATUS_POSTPONE(DELAY);
2684 TX_STATUS_POSTPONE(FEW_BYTES);
2685 TX_STATUS_POSTPONE(QUIET_PERIOD);
2686 TX_STATUS_POSTPONE(CALC_TTAK);
2687 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2688 TX_STATUS_FAIL(SHORT_LIMIT);
2689 TX_STATUS_FAIL(LONG_LIMIT);
2690 TX_STATUS_FAIL(FIFO_UNDERRUN);
2691 TX_STATUS_FAIL(DRAIN_FLOW);
2692 TX_STATUS_FAIL(RFKILL_FLUSH);
2693 TX_STATUS_FAIL(LIFE_EXPIRE);
2694 TX_STATUS_FAIL(DEST_PS);
2695 TX_STATUS_FAIL(HOST_ABORTED);
2696 TX_STATUS_FAIL(BT_RETRY);
2697 TX_STATUS_FAIL(STA_INVALID);
2698 TX_STATUS_FAIL(FRAG_DROPPED);
2699 TX_STATUS_FAIL(TID_DISABLE);
2700 TX_STATUS_FAIL(FIFO_FLUSHED);
2701 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2702 TX_STATUS_FAIL(PASSIVE_NO_RX);
2703 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002704 }
2705
2706 return "UNKNOWN";
2707
2708#undef TX_STATUS_FAIL
2709#undef TX_STATUS_POSTPONE
2710}
2711#endif /* CONFIG_IWLEGACY_DEBUG */
2712
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002713static struct il_link_quality_cmd *
2714il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2715{
2716 int i, r;
2717 struct il_link_quality_cmd *link_cmd;
2718 u32 rate_flags = 0;
2719 __le32 rate_n_flags;
2720
2721 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2722 if (!link_cmd) {
2723 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2724 return NULL;
2725 }
2726 /* Set up the rate scaling to start at selected rate, fall back
2727 * all the way down to 1M in IEEE order, and then spin on 1M */
2728 if (il->band == IEEE80211_BAND_5GHZ)
2729 r = RATE_6M_IDX;
2730 else
2731 r = RATE_1M_IDX;
2732
2733 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2734 rate_flags |= RATE_MCS_CCK_MSK;
2735
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002736 rate_flags |=
2737 il4965_first_antenna(il->hw_params.
2738 valid_tx_ant) << RATE_MCS_ANT_POS;
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01002739 rate_n_flags = cpu_to_le32(il_rates[r].plcp | rate_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002740 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2741 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2742
2743 link_cmd->general_params.single_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002744 il4965_first_antenna(il->hw_params.valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002745
2746 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002747 il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
2748 valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002749 if (!link_cmd->general_params.dual_stream_ant_msk) {
2750 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2751 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2752 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002753 il->hw_params.valid_tx_ant;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002754 }
2755
2756 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2757 link_cmd->agg_params.agg_time_limit =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002758 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002759
2760 link_cmd->sta_id = sta_id;
2761
2762 return link_cmd;
2763}
2764
2765/*
2766 * il4965_add_bssid_station - Add the special IBSS BSSID station
2767 *
2768 * Function sleeps.
2769 */
2770int
2771il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01002772 const u8 *addr, u8 *sta_id_r)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002773{
2774 int ret;
2775 u8 sta_id;
2776 struct il_link_quality_cmd *link_cmd;
2777 unsigned long flags;
2778
2779 if (sta_id_r)
2780 *sta_id_r = IL_INVALID_STATION;
2781
2782 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2783 if (ret) {
2784 IL_ERR("Unable to add station %pM\n", addr);
2785 return ret;
2786 }
2787
2788 if (sta_id_r)
2789 *sta_id_r = sta_id;
2790
2791 spin_lock_irqsave(&il->sta_lock, flags);
2792 il->stations[sta_id].used |= IL_STA_LOCAL;
2793 spin_unlock_irqrestore(&il->sta_lock, flags);
2794
2795 /* Set up default rate scaling table in device's station table */
2796 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2797 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002798 IL_ERR("Unable to initialize rate scaling for station %pM.\n",
2799 addr);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002800 return -ENOMEM;
2801 }
2802
2803 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2804 if (ret)
2805 IL_ERR("Link quality command failed (%d)\n", ret);
2806
2807 spin_lock_irqsave(&il->sta_lock, flags);
2808 il->stations[sta_id].lq = link_cmd;
2809 spin_unlock_irqrestore(&il->sta_lock, flags);
2810
2811 return 0;
2812}
2813
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002814static int
2815il4965_static_wepkey_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2816 bool send_if_empty)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002817{
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002818 int i;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002819 u8 buff[sizeof(struct il_wep_cmd) +
2820 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2821 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002822 size_t cmd_size = sizeof(struct il_wep_cmd);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002823 struct il_host_cmd cmd = {
Stanislaw Gruszkad98e2942012-02-03 17:31:42 +01002824 .id = C_WEPKEY,
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002825 .data = wep_cmd,
2826 .flags = CMD_SYNC,
2827 };
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002828 bool not_empty = false;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002829
2830 might_sleep();
2831
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002832 memset(wep_cmd, 0,
2833 cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002834
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002835 for (i = 0; i < WEP_KEYS_MAX; i++) {
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002836 u8 key_size = il->_4965.wep_keys[i].key_size;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002837
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002838 wep_cmd->key[i].key_idx = i;
2839 if (key_size) {
2840 wep_cmd->key[i].key_offset = i;
2841 not_empty = true;
2842 } else
2843 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2844
2845 wep_cmd->key[i].key_size = key_size;
2846 memcpy(&wep_cmd->key[i].key[3], il->_4965.wep_keys[i].key, key_size);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002847 }
2848
2849 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2850 wep_cmd->num_keys = WEP_KEYS_MAX;
2851
2852 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002853 cmd.len = cmd_size;
2854
2855 if (not_empty || send_if_empty)
2856 return il_send_cmd(il, &cmd);
2857 else
2858 return 0;
2859}
2860
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002861int
2862il4965_restore_default_wep_keys(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002863{
2864 lockdep_assert_held(&il->mutex);
2865
2866 return il4965_static_wepkey_cmd(il, ctx, false);
2867}
2868
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002869int
2870il4965_remove_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2871 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002872{
2873 int ret;
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002874 int idx = keyconf->keyidx;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002875
2876 lockdep_assert_held(&il->mutex);
2877
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002878 D_WEP("Removing default WEP key: idx=%d\n", idx);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002879
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002880 memset(&il->_4965.wep_keys[idx], 0, sizeof(struct il_wep_key));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002881 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002882 D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002883 /* but keys in device are clear anyway so return success */
2884 return 0;
2885 }
2886 ret = il4965_static_wepkey_cmd(il, ctx, 1);
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002887 D_WEP("Remove default WEP key: idx=%d ret=%d\n", idx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002888
2889 return ret;
2890}
2891
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002892int
2893il4965_set_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2894 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002895{
2896 int ret;
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002897 int len = keyconf->keylen;
2898 int idx = keyconf->keyidx;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002899
2900 lockdep_assert_held(&il->mutex);
2901
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002902 if (len != WEP_KEY_LEN_128 && len != WEP_KEY_LEN_64) {
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002903 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2904 return -EINVAL;
2905 }
2906
2907 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2908 keyconf->hw_key_idx = HW_KEY_DEFAULT;
Stanislaw Gruszka8f9e5642012-02-03 17:31:43 +01002909 il->stations[IL_AP_ID].keyinfo.cipher = keyconf->cipher;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002910
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002911 il->_4965.wep_keys[idx].key_size = len;
2912 memcpy(&il->_4965.wep_keys[idx].key, &keyconf->key, len);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002913
2914 ret = il4965_static_wepkey_cmd(il, ctx, false);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002915
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01002916 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", len, idx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002917 return ret;
2918}
2919
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002920static int
2921il4965_set_wep_dynamic_key_info(struct il_priv *il, struct il_rxon_context *ctx,
2922 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002923{
2924 unsigned long flags;
2925 __le16 key_flags = 0;
2926 struct il_addsta_cmd sta_cmd;
2927
2928 lockdep_assert_held(&il->mutex);
2929
2930 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2931
2932 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2933 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2934 key_flags &= ~STA_KEY_FLG_INVALID;
2935
2936 if (keyconf->keylen == WEP_KEY_LEN_128)
2937 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2938
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01002939 if (sta_id == il->hw_params.bcast_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002940 key_flags |= STA_KEY_MULTICAST_MSK;
2941
2942 spin_lock_irqsave(&il->sta_lock, flags);
2943
2944 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2945 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2946 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2947
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002948 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002949
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002950 memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
2951 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002952
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002953 if ((il->stations[sta_id].sta.key.
2954 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002955 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002956 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002957 /* else, we are overriding an existing key => no need to allocated room
2958 * in uCode. */
2959
2960 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002961 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002962
2963 il->stations[sta_id].sta.key.key_flags = key_flags;
2964 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
2965 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2966
2967 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002968 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002969 spin_unlock_irqrestore(&il->sta_lock, flags);
2970
2971 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2972}
2973
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002974static int
2975il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
2976 struct il_rxon_context *ctx,
2977 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002978{
2979 unsigned long flags;
2980 __le16 key_flags = 0;
2981 struct il_addsta_cmd sta_cmd;
2982
2983 lockdep_assert_held(&il->mutex);
2984
2985 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
2986 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2987 key_flags &= ~STA_KEY_FLG_INVALID;
2988
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01002989 if (sta_id == il->hw_params.bcast_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002990 key_flags |= STA_KEY_MULTICAST_MSK;
2991
2992 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2993
2994 spin_lock_irqsave(&il->sta_lock, flags);
2995 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2996 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2997
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002998 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002999
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003000 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003001
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003002 if ((il->stations[sta_id].sta.key.
3003 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003004 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003005 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003006 /* else, we are overriding an existing key => no need to allocated room
3007 * in uCode. */
3008
3009 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003010 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003011
3012 il->stations[sta_id].sta.key.key_flags = key_flags;
3013 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3014 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3015
3016 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003017 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003018 spin_unlock_irqrestore(&il->sta_lock, flags);
3019
3020 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3021}
3022
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003023static int
3024il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3025 struct il_rxon_context *ctx,
3026 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003027{
3028 unsigned long flags;
3029 int ret = 0;
3030 __le16 key_flags = 0;
3031
3032 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3033 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3034 key_flags &= ~STA_KEY_FLG_INVALID;
3035
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01003036 if (sta_id == il->hw_params.bcast_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003037 key_flags |= STA_KEY_MULTICAST_MSK;
3038
3039 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3040 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3041
3042 spin_lock_irqsave(&il->sta_lock, flags);
3043
3044 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3045 il->stations[sta_id].keyinfo.keylen = 16;
3046
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003047 if ((il->stations[sta_id].sta.key.
3048 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003049 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003050 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003051 /* else, we are overriding an existing key => no need to allocated room
3052 * in uCode. */
3053
3054 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003055 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003056
3057 il->stations[sta_id].sta.key.key_flags = key_flags;
3058
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003059 /* This copy is acutally not needed: we get the key with each TX */
3060 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3061
3062 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3063
3064 spin_unlock_irqrestore(&il->sta_lock, flags);
3065
3066 return ret;
3067}
3068
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003069void
3070il4965_update_tkip_key(struct il_priv *il, struct il_rxon_context *ctx,
3071 struct ieee80211_key_conf *keyconf,
3072 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003073{
3074 u8 sta_id;
3075 unsigned long flags;
3076 int i;
3077
3078 if (il_scan_cancel(il)) {
3079 /* cancel scan failed, just live w/ bad key and rely
3080 briefly on SW decryption */
3081 return;
3082 }
3083
3084 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3085 if (sta_id == IL_INVALID_STATION)
3086 return;
3087
3088 spin_lock_irqsave(&il->sta_lock, flags);
3089
3090 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3091
3092 for (i = 0; i < 5; i++)
3093 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003094 cpu_to_le16(phase1key[i]);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003095
3096 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3097 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3098
3099 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3100
3101 spin_unlock_irqrestore(&il->sta_lock, flags);
3102
3103}
3104
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003105int
3106il4965_remove_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3107 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003108{
3109 unsigned long flags;
3110 u16 key_flags;
3111 u8 keyidx;
3112 struct il_addsta_cmd sta_cmd;
3113
3114 lockdep_assert_held(&il->mutex);
3115
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01003116 il->_4965.key_mapping_keys--;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003117
3118 spin_lock_irqsave(&il->sta_lock, flags);
3119 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3120 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3121
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003122 D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003123
3124 if (keyconf->keyidx != keyidx) {
3125 /* We need to remove a key with idx different that the one
3126 * in the uCode. This means that the key we need to remove has
3127 * been replaced by another one with different idx.
3128 * Don't do anything and return ok
3129 */
3130 spin_unlock_irqrestore(&il->sta_lock, flags);
3131 return 0;
3132 }
3133
3134 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003135 IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
3136 key_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003137 spin_unlock_irqrestore(&il->sta_lock, flags);
3138 return 0;
3139 }
3140
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003141 if (!test_and_clear_bit
3142 (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003143 IL_ERR("idx %d not used in uCode key table.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003144 il->stations[sta_id].sta.key.key_offset);
3145 memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
3146 memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003147 il->stations[sta_id].sta.key.key_flags =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003148 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003149 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3150 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3151 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3152
3153 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003154 D_WEP
3155 ("Not sending C_ADD_STA command because RFKILL enabled.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003156 spin_unlock_irqrestore(&il->sta_lock, flags);
3157 return 0;
3158 }
3159 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003160 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003161 spin_unlock_irqrestore(&il->sta_lock, flags);
3162
3163 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3164}
3165
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003166int
3167il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3168 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003169{
3170 int ret;
3171
3172 lockdep_assert_held(&il->mutex);
3173
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01003174 il->_4965.key_mapping_keys++;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003175 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3176
3177 switch (keyconf->cipher) {
3178 case WLAN_CIPHER_SUITE_CCMP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003179 ret =
3180 il4965_set_ccmp_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003181 break;
3182 case WLAN_CIPHER_SUITE_TKIP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003183 ret =
3184 il4965_set_tkip_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003185 break;
3186 case WLAN_CIPHER_SUITE_WEP40:
3187 case WLAN_CIPHER_SUITE_WEP104:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003188 ret = il4965_set_wep_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003189 break;
3190 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003191 IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
3192 keyconf->cipher);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003193 ret = -EINVAL;
3194 }
3195
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003196 D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3197 keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003198
3199 return ret;
3200}
3201
3202/**
3203 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3204 *
3205 * This adds the broadcast station into the driver's station table
3206 * and marks it driver active, so that it will be restored to the
3207 * device at the next best time.
3208 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003209int
3210il4965_alloc_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003211{
3212 struct il_link_quality_cmd *link_cmd;
3213 unsigned long flags;
3214 u8 sta_id;
3215
3216 spin_lock_irqsave(&il->sta_lock, flags);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003217 sta_id = il_prep_station(il, ctx, il_bcast_addr, false, NULL);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003218 if (sta_id == IL_INVALID_STATION) {
3219 IL_ERR("Unable to prepare broadcast station\n");
3220 spin_unlock_irqrestore(&il->sta_lock, flags);
3221
3222 return -EINVAL;
3223 }
3224
3225 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3226 il->stations[sta_id].used |= IL_STA_BCAST;
3227 spin_unlock_irqrestore(&il->sta_lock, flags);
3228
3229 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3230 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003231 IL_ERR
3232 ("Unable to initialize rate scaling for bcast station.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003233 return -ENOMEM;
3234 }
3235
3236 spin_lock_irqsave(&il->sta_lock, flags);
3237 il->stations[sta_id].lq = link_cmd;
3238 spin_unlock_irqrestore(&il->sta_lock, flags);
3239
3240 return 0;
3241}
3242
3243/**
3244 * il4965_update_bcast_station - update broadcast station's LQ command
3245 *
3246 * Only used by iwl4965. Placed here to have all bcast station management
3247 * code together.
3248 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003249static int
3250il4965_update_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003251{
3252 unsigned long flags;
3253 struct il_link_quality_cmd *link_cmd;
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01003254 u8 sta_id = il->hw_params.bcast_id;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003255
3256 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3257 if (!link_cmd) {
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003258 IL_ERR("Unable to initialize rate scaling for bcast sta.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003259 return -ENOMEM;
3260 }
3261
3262 spin_lock_irqsave(&il->sta_lock, flags);
3263 if (il->stations[sta_id].lq)
3264 kfree(il->stations[sta_id].lq);
3265 else
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01003266 D_INFO("Bcast sta rate scaling has not been initialized.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003267 il->stations[sta_id].lq = link_cmd;
3268 spin_unlock_irqrestore(&il->sta_lock, flags);
3269
3270 return 0;
3271}
3272
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003273int
3274il4965_update_bcast_stations(struct il_priv *il)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003275{
3276 return il4965_update_bcast_station(il, &il->ctx);
3277}
3278
3279/**
3280 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3281 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003282int
3283il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003284{
3285 unsigned long flags;
3286 struct il_addsta_cmd sta_cmd;
3287
3288 lockdep_assert_held(&il->mutex);
3289
3290 /* Remove "disable" flag, to enable Tx for this TID */
3291 spin_lock_irqsave(&il->sta_lock, flags);
3292 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3293 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3294 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3295 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003296 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003297 spin_unlock_irqrestore(&il->sta_lock, flags);
3298
3299 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3300}
3301
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003302int
3303il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
3304 u16 ssn)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003305{
3306 unsigned long flags;
3307 int sta_id;
3308 struct il_addsta_cmd sta_cmd;
3309
3310 lockdep_assert_held(&il->mutex);
3311
3312 sta_id = il_sta_id(sta);
3313 if (sta_id == IL_INVALID_STATION)
3314 return -ENXIO;
3315
3316 spin_lock_irqsave(&il->sta_lock, flags);
3317 il->stations[sta_id].sta.station_flags_msk = 0;
3318 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003319 il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003320 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3321 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3322 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003323 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003324 spin_unlock_irqrestore(&il->sta_lock, flags);
3325
3326 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3327}
3328
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003329int
3330il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003331{
3332 unsigned long flags;
3333 int sta_id;
3334 struct il_addsta_cmd sta_cmd;
3335
3336 lockdep_assert_held(&il->mutex);
3337
3338 sta_id = il_sta_id(sta);
3339 if (sta_id == IL_INVALID_STATION) {
3340 IL_ERR("Invalid station for AGG tid %d\n", tid);
3341 return -ENXIO;
3342 }
3343
3344 spin_lock_irqsave(&il->sta_lock, flags);
3345 il->stations[sta_id].sta.station_flags_msk = 0;
3346 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003347 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003348 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3349 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003350 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003351 spin_unlock_irqrestore(&il->sta_lock, flags);
3352
3353 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3354}
3355
3356void
3357il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3358{
3359 unsigned long flags;
3360
3361 spin_lock_irqsave(&il->sta_lock, flags);
3362 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3363 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3364 il->stations[sta_id].sta.sta.modify_mask =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003365 STA_MODIFY_SLEEP_TX_COUNT_MSK;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003366 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3367 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003368 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003369 spin_unlock_irqrestore(&il->sta_lock, flags);
3370
3371}
3372
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003373void
3374il4965_update_chain_flags(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003375{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003376 if (il->cfg->ops->hcmd->set_rxon_chain) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003377 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01003378 if (il->active.rx_chain != il->staging.rx_chain)
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003379 il_commit_rxon(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003380 }
3381}
3382
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003383static void
3384il4965_clear_free_frames(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003385{
3386 struct list_head *element;
3387
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003388 D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003389
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003390 while (!list_empty(&il->free_frames)) {
3391 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003392 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003393 kfree(list_entry(element, struct il_frame, list));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003394 il->frames_count--;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003395 }
3396
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003397 if (il->frames_count) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003398 IL_WARN("%d frames still in use. Did we lose one?\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003399 il->frames_count);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003400 il->frames_count = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003401 }
3402}
3403
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003404static struct il_frame *
3405il4965_get_free_frame(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003406{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003407 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003408 struct list_head *element;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003409 if (list_empty(&il->free_frames)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003410 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3411 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003412 IL_ERR("Could not allocate frame!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003413 return NULL;
3414 }
3415
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003416 il->frames_count++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003417 return frame;
3418 }
3419
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003420 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003421 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003422 return list_entry(element, struct il_frame, list);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003423}
3424
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003425static void
3426il4965_free_frame(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003427{
3428 memset(frame, 0, sizeof(*frame));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003429 list_add(&frame->list, &il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003430}
3431
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003432static u32
3433il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
3434 int left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003435{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003436 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003437
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003438 if (!il->beacon_skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003439 return 0;
3440
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003441 if (il->beacon_skb->len > left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003442 return 0;
3443
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003444 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003445
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003446 return il->beacon_skb->len;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003447}
3448
3449/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003450static void
3451il4965_set_beacon_tim(struct il_priv *il,
3452 struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
3453 u32 frame_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003454{
3455 u16 tim_idx;
3456 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3457
3458 /*
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003459 * The idx is relative to frame start but we start looking at the
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003460 * variable-length part of the beacon.
3461 */
3462 tim_idx = mgmt->u.beacon.variable - beacon;
3463
3464 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3465 while ((tim_idx < (frame_size - 2)) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003466 (beacon[tim_idx] != WLAN_EID_TIM))
3467 tim_idx += beacon[tim_idx + 1] + 2;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003468
3469 /* If TIM field was found, set variables */
3470 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3471 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003472 tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003473 } else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003474 IL_WARN("Unable to find TIM Element in beacon\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003475}
3476
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003477static unsigned int
3478il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003479{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003480 struct il_tx_beacon_cmd *tx_beacon_cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003481 u32 frame_size;
3482 u32 rate_flags;
3483 u32 rate;
3484 /*
3485 * We have to set up the TX command, the TX Beacon command, and the
3486 * beacon contents.
3487 */
3488
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003489 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003490
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003491 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003492 IL_ERR("trying to build beacon w/o beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003493 return 0;
3494 }
3495
3496 /* Initialize memory */
3497 tx_beacon_cmd = &frame->u.beacon;
3498 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3499
3500 /* Set up TX beacon contents */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003501 frame_size =
3502 il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
3503 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003504 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3505 return 0;
3506 if (!frame_size)
3507 return 0;
3508
3509 /* Set up TX command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003510 tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01003511 tx_beacon_cmd->tx.sta_id = il->hw_params.bcast_id;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003512 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003513 tx_beacon_cmd->tx.tx_flags =
3514 TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
3515 TX_CMD_FLG_STA_RATE_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003516
3517 /* Set up TX beacon command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003518 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
3519 frame_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003520
3521 /* Set up packet rate and flags */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003522 rate = il_get_lowest_plcp(il, il->beacon_ctx);
Stanislaw Gruszkaa0c1ef32011-12-23 08:13:44 +01003523 il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01003524 rate_flags = BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003525 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003526 rate_flags |= RATE_MCS_CCK_MSK;
Stanislaw Gruszka616107e2011-12-23 08:13:45 +01003527 tx_beacon_cmd->tx.rate_n_flags = cpu_to_le32(rate | rate_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003528
3529 return sizeof(*tx_beacon_cmd) + frame_size;
3530}
3531
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003532int
3533il4965_send_beacon_cmd(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003534{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003535 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003536 unsigned int frame_size;
3537 int rc;
3538
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003539 frame = il4965_get_free_frame(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003540 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003541 IL_ERR("Could not obtain free frame buffer for beacon "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003542 "command.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003543 return -ENOMEM;
3544 }
3545
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003546 frame_size = il4965_hw_get_beacon_cmd(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003547 if (!frame_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003548 IL_ERR("Error configuring the beacon command\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003549 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003550 return -EINVAL;
3551 }
3552
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003553 rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003554
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003555 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003556
3557 return rc;
3558}
3559
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003560static inline dma_addr_t
3561il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003562{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003563 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003564
3565 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3566 if (sizeof(dma_addr_t) > sizeof(u32))
3567 addr |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003568 ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
3569 16;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003570
3571 return addr;
3572}
3573
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003574static inline u16
3575il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003576{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003577 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003578
3579 return le16_to_cpu(tb->hi_n_len) >> 4;
3580}
3581
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003582static inline void
3583il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003584{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003585 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003586 u16 hi_n_len = len << 4;
3587
3588 put_unaligned_le32(addr, &tb->lo);
3589 if (sizeof(dma_addr_t) > sizeof(u32))
3590 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3591
3592 tb->hi_n_len = cpu_to_le16(hi_n_len);
3593
3594 tfd->num_tbs = idx + 1;
3595}
3596
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003597static inline u8
3598il4965_tfd_get_num_tbs(struct il_tfd *tfd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003599{
3600 return tfd->num_tbs & 0x1f;
3601}
3602
3603/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003604 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003605 * @il - driver ilate data
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003606 * @txq - tx queue
3607 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003608 * Does NOT advance any TFD circular buffer read/write idxes
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003609 * Does NOT free the TFD itself (which is within circular buffer)
3610 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003611void
3612il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003613{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003614 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3615 struct il_tfd *tfd;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003616 struct pci_dev *dev = il->pci_dev;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003617 int idx = txq->q.read_ptr;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003618 int i;
3619 int num_tbs;
3620
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003621 tfd = &tfd_tmp[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003622
3623 /* Sanity check on number of chunks */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003624 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003625
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003626 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003627 IL_ERR("Too many chunks: %i\n", num_tbs);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003628 /* @todo issue fatal error, it is quite serious situation */
3629 return;
3630 }
3631
3632 /* Unmap tx_cmd */
3633 if (num_tbs)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003634 pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
3635 dma_unmap_len(&txq->meta[idx], len),
3636 PCI_DMA_BIDIRECTIONAL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003637
3638 /* Unmap chunks, if any. */
3639 for (i = 1; i < num_tbs; i++)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003640 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003641 il4965_tfd_tb_get_len(tfd, i),
3642 PCI_DMA_TODEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003643
3644 /* free SKB */
3645 if (txq->txb) {
3646 struct sk_buff *skb;
3647
3648 skb = txq->txb[txq->q.read_ptr].skb;
3649
3650 /* can be called from irqs-disabled context */
3651 if (skb) {
3652 dev_kfree_skb_any(skb);
3653 txq->txb[txq->q.read_ptr].skb = NULL;
3654 }
3655 }
3656}
3657
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003658int
3659il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
3660 dma_addr_t addr, u16 len, u8 reset, u8 pad)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003661{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003662 struct il_queue *q;
3663 struct il_tfd *tfd, *tfd_tmp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003664 u32 num_tbs;
3665
3666 q = &txq->q;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003667 tfd_tmp = (struct il_tfd *)txq->tfds;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003668 tfd = &tfd_tmp[q->write_ptr];
3669
3670 if (reset)
3671 memset(tfd, 0, sizeof(*tfd));
3672
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003673 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003674
3675 /* Each TFD can point to a maximum 20 Tx buffers */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003676 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003677 IL_ERR("Error can not send more than %d chunks\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003678 IL_NUM_OF_TBS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003679 return -EINVAL;
3680 }
3681
3682 BUG_ON(addr & ~DMA_BIT_MASK(36));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003683 if (unlikely(addr & ~IL_TX_DMA_MASK))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003684 IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003685
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003686 il4965_tfd_set_tb(tfd, num_tbs, addr, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003687
3688 return 0;
3689}
3690
3691/*
3692 * Tell nic where to find circular buffer of Tx Frame Descriptors for
3693 * given Tx queue, and enable the DMA channel used for that queue.
3694 *
3695 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
3696 * channels supported in hardware.
3697 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003698int
3699il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003700{
3701 int txq_id = txq->q.id;
3702
3703 /* Circular buffer (TFD queue in DRAM) physical base address */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003704 il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003705
3706 return 0;
3707}
3708
3709/******************************************************************************
3710 *
3711 * Generic RX handler implementations
3712 *
3713 ******************************************************************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003714static void
3715il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003716{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003717 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003718 struct il_alive_resp *palive;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003719 struct delayed_work *pwork;
3720
3721 palive = &pkt->u.alive_frame;
3722
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003723 D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
3724 palive->is_valid, palive->ver_type, palive->ver_subtype);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003725
3726 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003727 D_INFO("Initialization Alive received.\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003728 memcpy(&il->card_alive_init, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003729 sizeof(struct il_init_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003730 pwork = &il->init_alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003731 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003732 D_INFO("Runtime Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003733 memcpy(&il->card_alive, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003734 sizeof(struct il_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003735 pwork = &il->alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003736 }
3737
3738 /* We delay the ALIVE response by 5ms to
3739 * give the HW RF Kill time to activate... */
3740 if (palive->is_valid == UCODE_VALID_OK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003741 queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003742 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003743 IL_WARN("uCode did not respond OK.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003744}
3745
3746/**
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003747 * il4965_bg_stats_periodic - Timer callback to queue stats
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003748 *
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003749 * This callback is provided in order to send a stats request.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003750 *
3751 * This timer function is continually reset to execute within
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02003752 * REG_RECALIB_PERIOD seconds since the last N_STATS
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003753 * was received. We need to ensure we receive the stats in order
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003754 * to update the temperature used for calibrating the TXPOWER.
3755 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003756static void
3757il4965_bg_stats_periodic(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003758{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003759 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003760
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003761 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003762 return;
3763
3764 /* dont send host command if rf-kill is on */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003765 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003766 return;
3767
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003768 il_send_stats_request(il, CMD_ASYNC, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003769}
3770
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003771static void
3772il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003773{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003774 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003775 struct il4965_beacon_notif *beacon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003776 (struct il4965_beacon_notif *)pkt->u.raw;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003777#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003778 u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003779
Stanislaw Gruszka5bf0dac2011-12-23 08:13:47 +01003780 D_RX("beacon status %x retries %d iss %d tsf:0x%.8x%.8x rate %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003781 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
3782 beacon->beacon_notify_hdr.failure_frame,
3783 le32_to_cpu(beacon->ibss_mgr_status),
3784 le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003785#endif
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003786 il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003787}
3788
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003789static void
3790il4965_perform_ct_kill_task(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003791{
3792 unsigned long flags;
3793
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003794 D_POWER("Stop all queues\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003795
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003796 if (il->mac80211_registered)
3797 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003798
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003799 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003800 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003801 _il_rd(il, CSR_UCODE_DRV_GP1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003802
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003803 spin_lock_irqsave(&il->reg_lock, flags);
Stanislaw Gruszka13882262011-08-24 15:39:23 +02003804 if (!_il_grab_nic_access(il))
3805 _il_release_nic_access(il);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003806 spin_unlock_irqrestore(&il->reg_lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003807}
3808
3809/* Handle notification from uCode that card's power state is changing
3810 * due to software, hardware, or critical temperature RFKILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003811static void
3812il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003813{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003814 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003815 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003816 unsigned long status = il->status;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003817
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003818 D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003819 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3820 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
3821 (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003822
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003823 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003824
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003825 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003826 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003827
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003828 il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003829
3830 if (!(flags & RXON_CARD_DISABLED)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003831 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003832 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003833 il_wr(il, HBUS_TARG_MBX_C,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003834 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003835 }
3836 }
3837
3838 if (flags & CT_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003839 il4965_perform_ct_kill_task(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003840
3841 if (flags & HW_CARD_DISABLED)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003842 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003843 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003844 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003845
3846 if (!(flags & RXON_CARD_DISABLED))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003847 il_scan_cancel(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003848
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003849 if ((test_bit(S_RF_KILL_HW, &status) !=
3850 test_bit(S_RF_KILL_HW, &il->status)))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003851 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003852 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003853 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003854 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003855}
3856
3857/**
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003858 * il4965_setup_handlers - Initialize Rx handler callbacks
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003859 *
3860 * Setup the RX handlers for each of the reply types sent from the uCode
3861 * to the host.
3862 *
3863 * This function chains into the hardware specific files for them to setup
3864 * any hardware specific handlers as well.
3865 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003866static void
3867il4965_setup_handlers(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003868{
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003869 il->handlers[N_ALIVE] = il4965_hdl_alive;
3870 il->handlers[N_ERROR] = il_hdl_error;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003871 il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003872 il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003873 il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003874 il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003875 il->handlers[N_BEACON] = il4965_hdl_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003876
3877 /*
3878 * The same handler is used for both the REPLY to a discrete
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003879 * stats request from the host as well as for the periodic
3880 * stats notifications (after received beacons) from the uCode.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003881 */
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003882 il->handlers[C_STATS] = il4965_hdl_c_stats;
3883 il->handlers[N_STATS] = il4965_hdl_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003884
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003885 il_setup_rx_scan_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003886
3887 /* status change handler */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003888 il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003889
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003890 il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003891 /* Rx handlers */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003892 il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
3893 il->handlers[N_RX_MPDU] = il4965_hdl_rx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003894 /* block ack */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003895 il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003896 /* Set up hardware specific Rx handlers */
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003897 il->cfg->ops->lib->handler_setup(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003898}
3899
3900/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003901 * il4965_rx_handle - Main entry function for receiving responses from uCode
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003902 *
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003903 * Uses the il->handlers callback function array to invoke
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003904 * the appropriate handlers, including command responses,
3905 * frame-received notifications, and other notifications.
3906 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003907void
3908il4965_rx_handle(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003909{
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003910 struct il_rx_buf *rxb;
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003911 struct il_rx_pkt *pkt;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003912 struct il_rx_queue *rxq = &il->rxq;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003913 u32 r, i;
3914 int reclaim;
3915 unsigned long flags;
3916 u8 fill_rx = 0;
3917 u32 count = 8;
3918 int total_empty;
3919
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003920 /* uCode's read idx (stored in shared DRAM) indicates the last Rx
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003921 * buffer that the driver may process (last buffer filled by ucode). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003922 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003923 i = rxq->read;
3924
3925 /* Rx interrupt, but nothing sent from uCode */
3926 if (i == r)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003927 D_RX("r = %d, i = %d\n", r, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003928
3929 /* calculate total frames need to be restock after handling RX */
3930 total_empty = r - rxq->write_actual;
3931 if (total_empty < 0)
3932 total_empty += RX_QUEUE_SIZE;
3933
3934 if (total_empty > (RX_QUEUE_SIZE / 2))
3935 fill_rx = 1;
3936
3937 while (i != r) {
3938 int len;
3939
3940 rxb = rxq->queue[i];
3941
3942 /* If an RXB doesn't have a Rx queue slot associated with it,
3943 * then a bug has been introduced in the queue refilling
3944 * routines -- catch it here */
3945 BUG_ON(rxb == NULL);
3946
3947 rxq->queue[i] = NULL;
3948
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003949 pci_unmap_page(il->pci_dev, rxb->page_dma,
3950 PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003951 PCI_DMA_FROMDEVICE);
3952 pkt = rxb_addr(rxb);
3953
Stanislaw Gruszkae94a4092011-08-31 13:23:20 +02003954 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003955 len += sizeof(u32); /* account for status word */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003956
3957 /* Reclaim a command buffer only if this packet is a response
3958 * to a (driver-originated) command.
3959 * If the packet (e.g. Rx frame) originated from uCode,
3960 * there is no command buffer to reclaim.
3961 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3962 * but apparently a few don't get set; catch them here. */
3963 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003964 (pkt->hdr.cmd != N_RX_PHY) && (pkt->hdr.cmd != N_RX) &&
3965 (pkt->hdr.cmd != N_RX_MPDU) &&
3966 (pkt->hdr.cmd != N_COMPRESSED_BA) &&
3967 (pkt->hdr.cmd != N_STATS) && (pkt->hdr.cmd != C_TX);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003968
3969 /* Based on type of command response or notification,
3970 * handle those that need handling via function in
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003971 * handlers table. See il4965_setup_handlers() */
3972 if (il->handlers[pkt->hdr.cmd]) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003973 D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
3974 il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003975 il->isr_stats.handlers[pkt->hdr.cmd]++;
3976 il->handlers[pkt->hdr.cmd] (il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003977 } else {
3978 /* No handling needed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003979 D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
3980 i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003981 }
3982
3983 /*
3984 * XXX: After here, we should always check rxb->page
3985 * against NULL before touching it or its virtual
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003986 * memory (pkt). Because some handler might have
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003987 * already taken or freed the pages.
3988 */
3989
3990 if (reclaim) {
3991 /* Invoke any callbacks, transfer the buffer to caller,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003992 * and fire off the (possibly) blocking il_send_cmd()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003993 * as we reclaim the driver command queue */
3994 if (rxb->page)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003995 il_tx_cmd_complete(il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003996 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003997 IL_WARN("Claim null rxb?\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003998 }
3999
4000 /* Reuse the page if possible. For notification packets and
4001 * SKBs that fail to Rx correctly, add them back into the
4002 * rx_free list for reuse later. */
4003 spin_lock_irqsave(&rxq->lock, flags);
4004 if (rxb->page != NULL) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004005 rxb->page_dma =
4006 pci_map_page(il->pci_dev, rxb->page, 0,
4007 PAGE_SIZE << il->hw_params.
4008 rx_page_order, PCI_DMA_FROMDEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004009 list_add_tail(&rxb->list, &rxq->rx_free);
4010 rxq->free_count++;
4011 } else
4012 list_add_tail(&rxb->list, &rxq->rx_used);
4013
4014 spin_unlock_irqrestore(&rxq->lock, flags);
4015
4016 i = (i + 1) & RX_QUEUE_MASK;
4017 /* If there are a lot of unused frames,
4018 * restock the Rx queue so ucode wont assert. */
4019 if (fill_rx) {
4020 count++;
4021 if (count >= 8) {
4022 rxq->read = i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004023 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004024 count = 0;
4025 }
4026 }
4027 }
4028
4029 /* Backtrack one entry */
4030 rxq->read = i;
4031 if (fill_rx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004032 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004033 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004034 il4965_rx_queue_restock(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004035}
4036
4037/* call this function to flush any scheduled tasklet */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004038static inline void
4039il4965_synchronize_irq(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004040{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004041 /* wait to make sure we flush pending tasklet */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004042 synchronize_irq(il->pci_dev->irq);
4043 tasklet_kill(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004044}
4045
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004046static void
4047il4965_irq_tasklet(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004048{
4049 u32 inta, handled = 0;
4050 u32 inta_fh;
4051 unsigned long flags;
4052 u32 i;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004053#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004054 u32 inta_mask;
4055#endif
4056
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004057 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004058
4059 /* Ack/clear/reset pending uCode interrupts.
4060 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4061 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004062 inta = _il_rd(il, CSR_INT);
4063 _il_wr(il, CSR_INT, inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004064
4065 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4066 * Any new interrupts that happen after this, either while we're
4067 * in this tasklet, or later, will show up in next ISR/tasklet. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004068 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
4069 _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004070
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004071#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004072 if (il_get_debug_level(il) & IL_DL_ISR) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004073 /* just for debug */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004074 inta_mask = _il_rd(il, CSR_INT_MASK);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004075 D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
4076 inta_mask, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004077 }
4078#endif
4079
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004080 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004081
4082 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4083 * atomic, make sure that inta covers all the interrupts that
4084 * we've discovered, even if FH interrupt came in just after
4085 * reading CSR_INT. */
4086 if (inta_fh & CSR49_FH_INT_RX_MASK)
4087 inta |= CSR_INT_BIT_FH_RX;
4088 if (inta_fh & CSR49_FH_INT_TX_MASK)
4089 inta |= CSR_INT_BIT_FH_TX;
4090
4091 /* Now service all interrupt bits discovered above. */
4092 if (inta & CSR_INT_BIT_HW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004093 IL_ERR("Hardware error detected. Restarting.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004094
4095 /* Tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004096 il_disable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004097
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004098 il->isr_stats.hw++;
4099 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004100
4101 handled |= CSR_INT_BIT_HW_ERR;
4102
4103 return;
4104 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004105#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004106 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004107 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4108 if (inta & CSR_INT_BIT_SCD) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004109 D_ISR("Scheduler finished to transmit "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004110 "the frame/frames.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004111 il->isr_stats.sch++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004112 }
4113
4114 /* Alive notification via Rx interrupt will do the real work */
4115 if (inta & CSR_INT_BIT_ALIVE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004116 D_ISR("Alive interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004117 il->isr_stats.alive++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004118 }
4119 }
4120#endif
4121 /* Safely ignore these bits for debug checks below */
4122 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
4123
4124 /* HW RF KILL switch toggled */
4125 if (inta & CSR_INT_BIT_RF_KILL) {
4126 int hw_rf_kill = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004127 if (!
4128 (_il_rd(il, CSR_GP_CNTRL) &
4129 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004130 hw_rf_kill = 1;
4131
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004132 IL_WARN("RF_KILL bit toggled to %s.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004133 hw_rf_kill ? "disable radio" : "enable radio");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004134
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004135 il->isr_stats.rfkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004136
4137 /* driver only loads ucode once setting the interface up.
4138 * the driver allows loading the ucode even if the radio
4139 * is killed. Hence update the killswitch state here. The
4140 * rfkill handler will care about restarting if needed.
4141 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004142 if (!test_bit(S_ALIVE, &il->status)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004143 if (hw_rf_kill)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004144 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004145 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004146 clear_bit(S_RF_KILL_HW, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004147 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004148 }
4149
4150 handled |= CSR_INT_BIT_RF_KILL;
4151 }
4152
4153 /* Chip got too hot and stopped itself */
4154 if (inta & CSR_INT_BIT_CT_KILL) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004155 IL_ERR("Microcode CT kill error detected.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004156 il->isr_stats.ctkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004157 handled |= CSR_INT_BIT_CT_KILL;
4158 }
4159
4160 /* Error detected by uCode */
4161 if (inta & CSR_INT_BIT_SW_ERR) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004162 IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
4163 inta);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004164 il->isr_stats.sw++;
4165 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004166 handled |= CSR_INT_BIT_SW_ERR;
4167 }
4168
4169 /*
4170 * uCode wakes up after power-down sleep.
4171 * Tell device about any new tx or host commands enqueued,
4172 * and about any Rx buffers made available while asleep.
4173 */
4174 if (inta & CSR_INT_BIT_WAKEUP) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004175 D_ISR("Wakeup interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004176 il_rx_queue_update_write_ptr(il, &il->rxq);
4177 for (i = 0; i < il->hw_params.max_txq_num; i++)
4178 il_txq_update_write_ptr(il, &il->txq[i]);
4179 il->isr_stats.wakeup++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004180 handled |= CSR_INT_BIT_WAKEUP;
4181 }
4182
4183 /* All uCode command responses, including Tx command responses,
4184 * Rx "responses" (frame-received notification), and other
4185 * notifications from uCode come through here*/
4186 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004187 il4965_rx_handle(il);
4188 il->isr_stats.rx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004189 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4190 }
4191
4192 /* This "Tx" DMA channel is used only for loading uCode */
4193 if (inta & CSR_INT_BIT_FH_TX) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004194 D_ISR("uCode load interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004195 il->isr_stats.tx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004196 handled |= CSR_INT_BIT_FH_TX;
4197 /* Wake up uCode load routine, now that load is complete */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004198 il->ucode_write_complete = 1;
4199 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004200 }
4201
4202 if (inta & ~handled) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004203 IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004204 il->isr_stats.unhandled++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004205 }
4206
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004207 if (inta & ~(il->inta_mask)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004208 IL_WARN("Disabled INTA bits 0x%08x were pending\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004209 inta & ~il->inta_mask);
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004210 IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004211 }
4212
4213 /* Re-enable all interrupts */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02004214 /* only Re-enable if disabled by irq */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004215 if (test_bit(S_INT_ENABLED, &il->status))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004216 il_enable_interrupts(il);
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02004217 /* Re-enable RF_KILL if it occurred */
4218 else if (handled & CSR_INT_BIT_RF_KILL)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004219 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004220
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004221#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004222 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004223 inta = _il_rd(il, CSR_INT);
4224 inta_mask = _il_rd(il, CSR_INT_MASK);
4225 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004226 D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4227 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004228 }
4229#endif
4230}
4231
4232/*****************************************************************************
4233 *
4234 * sysfs attributes
4235 *
4236 *****************************************************************************/
4237
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004238#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004239
4240/*
4241 * The following adds a new attribute to the sysfs representation
4242 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
4243 * used for controlling the debug level.
4244 *
4245 * See the level definitions in iwl for details.
4246 *
4247 * The debug_level being managed using sysfs below is a per device debug
4248 * level that is used instead of the global debug level if it (the per
4249 * device debug level) is set.
4250 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004251static ssize_t
4252il4965_show_debug_level(struct device *d, struct device_attribute *attr,
4253 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004254{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004255 struct il_priv *il = dev_get_drvdata(d);
4256 return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004257}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004258
4259static ssize_t
4260il4965_store_debug_level(struct device *d, struct device_attribute *attr,
4261 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004262{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004263 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004264 unsigned long val;
4265 int ret;
4266
4267 ret = strict_strtoul(buf, 0, &val);
4268 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004269 IL_ERR("%s is not in hex or decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004270 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004271 il->debug_level = val;
4272 if (il_alloc_traffic_mem(il))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004273 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004274 }
4275 return strnlen(buf, count);
4276}
4277
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004278static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il4965_show_debug_level,
4279 il4965_store_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004280
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004281#endif /* CONFIG_IWLEGACY_DEBUG */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004282
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004283static ssize_t
4284il4965_show_temperature(struct device *d, struct device_attribute *attr,
4285 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004286{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004287 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004288
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004289 if (!il_is_alive(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004290 return -EAGAIN;
4291
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004292 return sprintf(buf, "%d\n", il->temperature);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004293}
4294
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004295static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004296
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004297static ssize_t
4298il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004299{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004300 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004301
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004302 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004303 return sprintf(buf, "off\n");
4304 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004305 return sprintf(buf, "%d\n", il->tx_power_user_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004306}
4307
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004308static ssize_t
4309il4965_store_tx_power(struct device *d, struct device_attribute *attr,
4310 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004311{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004312 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004313 unsigned long val;
4314 int ret;
4315
4316 ret = strict_strtoul(buf, 10, &val);
4317 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004318 IL_INFO("%s is not in decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004319 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004320 ret = il_set_tx_power(il, val, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004321 if (ret)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004322 IL_ERR("failed setting tx power (0x%d).\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004323 else
4324 ret = count;
4325 }
4326 return ret;
4327}
4328
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004329static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il4965_show_tx_power,
4330 il4965_store_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004331
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004332static struct attribute *il_sysfs_entries[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004333 &dev_attr_temperature.attr,
4334 &dev_attr_tx_power.attr,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004335#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004336 &dev_attr_debug_level.attr,
4337#endif
4338 NULL
4339};
4340
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004341static struct attribute_group il_attribute_group = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004342 .name = NULL, /* put in device directory */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004343 .attrs = il_sysfs_entries,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004344};
4345
4346/******************************************************************************
4347 *
4348 * uCode download functions
4349 *
4350 ******************************************************************************/
4351
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004352static void
4353il4965_dealloc_ucode_pci(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004354{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004355 il_free_fw_desc(il->pci_dev, &il->ucode_code);
4356 il_free_fw_desc(il->pci_dev, &il->ucode_data);
4357 il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
4358 il_free_fw_desc(il->pci_dev, &il->ucode_init);
4359 il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
4360 il_free_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004361}
4362
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004363static void
4364il4965_nic_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004365{
4366 /* Remove all resets to allow NIC to operate */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004367 _il_wr(il, CSR_RESET, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004368}
4369
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004370static void il4965_ucode_callback(const struct firmware *ucode_raw,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004371 void *context);
4372static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004373
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004374static int __must_check
4375il4965_request_firmware(struct il_priv *il, bool first)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004376{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004377 const char *name_pre = il->cfg->fw_name_pre;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004378 char tag[8];
4379
4380 if (first) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004381 il->fw_idx = il->cfg->ucode_api_max;
4382 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004383 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004384 il->fw_idx--;
4385 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004386 }
4387
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004388 if (il->fw_idx < il->cfg->ucode_api_min) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004389 IL_ERR("no suitable firmware found!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004390 return -ENOENT;
4391 }
4392
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004393 sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004394
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004395 D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004396
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004397 return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
4398 &il->pci_dev->dev, GFP_KERNEL, il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004399 il4965_ucode_callback);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004400}
4401
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004402struct il4965_firmware_pieces {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004403 const void *inst, *data, *init, *init_data, *boot;
4404 size_t inst_size, data_size, init_size, init_data_size, boot_size;
4405};
4406
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004407static int
4408il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
4409 struct il4965_firmware_pieces *pieces)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004410{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004411 struct il_ucode_header *ucode = (void *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004412 u32 api_ver, hdr_size;
4413 const u8 *src;
4414
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004415 il->ucode_ver = le32_to_cpu(ucode->ver);
4416 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004417
4418 switch (api_ver) {
4419 default:
4420 case 0:
4421 case 1:
4422 case 2:
4423 hdr_size = 24;
4424 if (ucode_raw->size < hdr_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004425 IL_ERR("File size too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004426 return -EINVAL;
4427 }
4428 pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
4429 pieces->data_size = le32_to_cpu(ucode->v1.data_size);
4430 pieces->init_size = le32_to_cpu(ucode->v1.init_size);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004431 pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004432 pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
4433 src = ucode->v1.data;
4434 break;
4435 }
4436
4437 /* Verify size of file vs. image size info in file's header */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004438 if (ucode_raw->size !=
4439 hdr_size + pieces->inst_size + pieces->data_size +
4440 pieces->init_size + pieces->init_data_size + pieces->boot_size) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004441
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004442 IL_ERR("uCode file size %d does not match expected size\n",
4443 (int)ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004444 return -EINVAL;
4445 }
4446
4447 pieces->inst = src;
4448 src += pieces->inst_size;
4449 pieces->data = src;
4450 src += pieces->data_size;
4451 pieces->init = src;
4452 src += pieces->init_size;
4453 pieces->init_data = src;
4454 src += pieces->init_data_size;
4455 pieces->boot = src;
4456 src += pieces->boot_size;
4457
4458 return 0;
4459}
4460
4461/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004462 * il4965_ucode_callback - callback when firmware was loaded
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004463 *
4464 * If loaded successfully, copies the firmware into buffers
4465 * for the card to fetch (via DMA).
4466 */
4467static void
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004468il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004469{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004470 struct il_priv *il = context;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004471 struct il_ucode_header *ucode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004472 int err;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004473 struct il4965_firmware_pieces pieces;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004474 const unsigned int api_max = il->cfg->ucode_api_max;
4475 const unsigned int api_min = il->cfg->ucode_api_min;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004476 u32 api_ver;
4477
4478 u32 max_probe_length = 200;
4479 u32 standard_phy_calibration_size =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004480 IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004481
4482 memset(&pieces, 0, sizeof(pieces));
4483
4484 if (!ucode_raw) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004485 if (il->fw_idx <= il->cfg->ucode_api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004486 IL_ERR("request for firmware file '%s' failed.\n",
4487 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004488 goto try_again;
4489 }
4490
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004491 D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
4492 ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004493
4494 /* Make sure that we got at least the API version number */
4495 if (ucode_raw->size < 4) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004496 IL_ERR("File size way too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004497 goto try_again;
4498 }
4499
4500 /* Data from ucode file: header followed by uCode images */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004501 ucode = (struct il_ucode_header *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004502
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004503 err = il4965_load_firmware(il, ucode_raw, &pieces);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004504
4505 if (err)
4506 goto try_again;
4507
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004508 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004509
4510 /*
4511 * api_ver should match the api version forming part of the
4512 * firmware filename ... but we don't check for that and only rely
4513 * on the API version read from firmware header from here on forward
4514 */
4515 if (api_ver < api_min || api_ver > api_max) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004516 IL_ERR("Driver unable to support your firmware API. "
4517 "Driver supports v%u, firmware is v%u.\n", api_max,
4518 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004519 goto try_again;
4520 }
4521
4522 if (api_ver != api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004523 IL_ERR("Firmware has old API version. Expected v%u, "
4524 "got v%u. New firmware can be obtained "
4525 "from http://www.intellinuxwireless.org.\n", api_max,
4526 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004527
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004528 IL_INFO("loaded firmware version %u.%u.%u.%u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004529 IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
4530 IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004531
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004532 snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
4533 "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
4534 IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004535 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004536
4537 /*
4538 * For any of the failures below (before allocating pci memory)
4539 * we will try to load a version with a smaller API -- maybe the
4540 * user just got a corrupted version of the latest API.
4541 */
4542
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004543 D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
4544 D_INFO("f/w package hdr runtime inst size = %Zd\n", pieces.inst_size);
4545 D_INFO("f/w package hdr runtime data size = %Zd\n", pieces.data_size);
4546 D_INFO("f/w package hdr init inst size = %Zd\n", pieces.init_size);
4547 D_INFO("f/w package hdr init data size = %Zd\n", pieces.init_data_size);
4548 D_INFO("f/w package hdr boot inst size = %Zd\n", pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004549
4550 /* Verify that uCode images will fit in card's SRAM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004551 if (pieces.inst_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004552 IL_ERR("uCode instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004553 pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004554 goto try_again;
4555 }
4556
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004557 if (pieces.data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004558 IL_ERR("uCode data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004559 pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004560 goto try_again;
4561 }
4562
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004563 if (pieces.init_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004564 IL_ERR("uCode init instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004565 pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004566 goto try_again;
4567 }
4568
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004569 if (pieces.init_data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004570 IL_ERR("uCode init data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004571 pieces.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004572 goto try_again;
4573 }
4574
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004575 if (pieces.boot_size > il->hw_params.max_bsm_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004576 IL_ERR("uCode boot instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004577 pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004578 goto try_again;
4579 }
4580
4581 /* Allocate ucode buffers for card's bus-master loading ... */
4582
4583 /* Runtime instructions and 2 copies of data:
4584 * 1) unmodified from disk
4585 * 2) backup cache for save/restore during power-downs */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004586 il->ucode_code.len = pieces.inst_size;
4587 il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004588
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004589 il->ucode_data.len = pieces.data_size;
4590 il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004591
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004592 il->ucode_data_backup.len = pieces.data_size;
4593 il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004594
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004595 if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
4596 !il->ucode_data_backup.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004597 goto err_pci_alloc;
4598
4599 /* Initialization instructions and data */
4600 if (pieces.init_size && pieces.init_data_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004601 il->ucode_init.len = pieces.init_size;
4602 il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004603
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004604 il->ucode_init_data.len = pieces.init_data_size;
4605 il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004606
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004607 if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004608 goto err_pci_alloc;
4609 }
4610
4611 /* Bootstrap (instructions only, no data) */
4612 if (pieces.boot_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004613 il->ucode_boot.len = pieces.boot_size;
4614 il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004615
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004616 if (!il->ucode_boot.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004617 goto err_pci_alloc;
4618 }
4619
4620 /* Now that we can no longer fail, copy information */
4621
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004622 il->sta_key_max_num = STA_KEY_MAX_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004623
4624 /* Copy images into buffers for card's bus-master reads ... */
4625
4626 /* Runtime instructions (first block of data in file) */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004627 D_INFO("Copying (but not loading) uCode instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004628 pieces.inst_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004629 memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004630
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004631 D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004632 il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004633
4634 /*
4635 * Runtime data
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004636 * NOTE: Copy into backup buffer will be done in il_up()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004637 */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004638 D_INFO("Copying (but not loading) uCode data len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004639 pieces.data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004640 memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
4641 memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004642
4643 /* Initialization instructions */
4644 if (pieces.init_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004645 D_INFO("Copying (but not loading) init instr len %Zd\n",
4646 pieces.init_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004647 memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004648 }
4649
4650 /* Initialization data */
4651 if (pieces.init_data_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004652 D_INFO("Copying (but not loading) init data len %Zd\n",
4653 pieces.init_data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004654 memcpy(il->ucode_init_data.v_addr, pieces.init_data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004655 pieces.init_data_size);
4656 }
4657
4658 /* Bootstrap instructions */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004659 D_INFO("Copying (but not loading) boot instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004660 pieces.boot_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004661 memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004662
4663 /*
4664 * figure out the offset of chain noise reset and gain commands
4665 * base on the size of standard phy calibration commands table size
4666 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004667 il->_4965.phy_calib_chain_noise_reset_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004668 standard_phy_calibration_size;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004669 il->_4965.phy_calib_chain_noise_gain_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004670 standard_phy_calibration_size + 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004671
4672 /**************************************************
4673 * This is still part of probe() in a sense...
4674 *
4675 * 9. Setup and register with mac80211 and debugfs
4676 **************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004677 err = il4965_mac_setup_register(il, max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004678 if (err)
4679 goto out_unbind;
4680
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004681 err = il_dbgfs_register(il, DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004682 if (err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004683 IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
4684 err);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004685
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004686 err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004687 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004688 IL_ERR("failed to create sysfs device attributes\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004689 goto out_unbind;
4690 }
4691
4692 /* We have our copies now, allow OS release its copies */
4693 release_firmware(ucode_raw);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004694 complete(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004695 return;
4696
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004697try_again:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004698 /* try next, if any */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004699 if (il4965_request_firmware(il, false))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004700 goto out_unbind;
4701 release_firmware(ucode_raw);
4702 return;
4703
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004704err_pci_alloc:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004705 IL_ERR("failed to allocate pci memory\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004706 il4965_dealloc_ucode_pci(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004707out_unbind:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004708 complete(&il->_4965.firmware_loading_complete);
4709 device_release_driver(&il->pci_dev->dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004710 release_firmware(ucode_raw);
4711}
4712
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004713static const char *const desc_lookup_text[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004714 "OK",
4715 "FAIL",
4716 "BAD_PARAM",
4717 "BAD_CHECKSUM",
4718 "NMI_INTERRUPT_WDG",
4719 "SYSASSERT",
4720 "FATAL_ERROR",
4721 "BAD_COMMAND",
4722 "HW_ERROR_TUNE_LOCK",
4723 "HW_ERROR_TEMPERATURE",
4724 "ILLEGAL_CHAN_FREQ",
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +02004725 "VCC_NOT_STBL",
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004726 "FH49_ERROR",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004727 "NMI_INTERRUPT_HOST",
4728 "NMI_INTERRUPT_ACTION_PT",
4729 "NMI_INTERRUPT_UNKNOWN",
4730 "UCODE_VERSION_MISMATCH",
4731 "HW_ERROR_ABS_LOCK",
4732 "HW_ERROR_CAL_LOCK_FAIL",
4733 "NMI_INTERRUPT_INST_ACTION_PT",
4734 "NMI_INTERRUPT_DATA_ACTION_PT",
4735 "NMI_TRM_HW_ER",
4736 "NMI_INTERRUPT_TRM",
Joe Perches861d9c32011-07-08 23:20:24 -07004737 "NMI_INTERRUPT_BREAK_POINT",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004738 "DEBUG_0",
4739 "DEBUG_1",
4740 "DEBUG_2",
4741 "DEBUG_3",
4742};
4743
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004744static struct {
4745 char *name;
4746 u8 num;
4747} advanced_lookup[] = {
4748 {
4749 "NMI_INTERRUPT_WDG", 0x34}, {
4750 "SYSASSERT", 0x35}, {
4751 "UCODE_VERSION_MISMATCH", 0x37}, {
4752 "BAD_COMMAND", 0x38}, {
4753 "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
4754 "FATAL_ERROR", 0x3D}, {
4755 "NMI_TRM_HW_ERR", 0x46}, {
4756 "NMI_INTERRUPT_TRM", 0x4C}, {
4757 "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
4758 "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
4759 "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
4760 "NMI_INTERRUPT_HOST", 0x66}, {
4761 "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
4762 "NMI_INTERRUPT_UNKNOWN", 0x84}, {
4763 "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
4764"ADVANCED_SYSASSERT", 0},};
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004765
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004766static const char *
4767il4965_desc_lookup(u32 num)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004768{
4769 int i;
4770 int max = ARRAY_SIZE(desc_lookup_text);
4771
4772 if (num < max)
4773 return desc_lookup_text[num];
4774
4775 max = ARRAY_SIZE(advanced_lookup) - 1;
4776 for (i = 0; i < max; i++) {
4777 if (advanced_lookup[i].num == num)
4778 break;
4779 }
4780 return advanced_lookup[i].name;
4781}
4782
4783#define ERROR_START_OFFSET (1 * sizeof(u32))
4784#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4785
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004786void
4787il4965_dump_nic_error_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004788{
4789 u32 data2, line;
4790 u32 desc, time, count, base, data1;
4791 u32 blink1, blink2, ilink1, ilink2;
4792 u32 pc, hcmd;
4793
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004794 if (il->ucode_type == UCODE_INIT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004795 base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01004796 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004797 base = le32_to_cpu(il->card_alive.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004798
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004799 if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004800 IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
4801 base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004802 return;
4803 }
4804
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004805 count = il_read_targ_mem(il, base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004806
4807 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004808 IL_ERR("Start IWL Error Log Dump:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004809 IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004810 }
4811
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004812 desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
4813 il->isr_stats.err_code = desc;
4814 pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
4815 blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
4816 blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
4817 ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
4818 ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
4819 data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
4820 data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
4821 line = il_read_targ_mem(il, base + 9 * sizeof(u32));
4822 time = il_read_targ_mem(il, base + 11 * sizeof(u32));
4823 hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004824
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004825 IL_ERR("Desc Time "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004826 "data1 data2 line\n");
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004827 IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004828 il4965_desc_lookup(desc), desc, time, data1, data2, line);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004829 IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004830 IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
4831 blink2, ilink1, ilink2, hcmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004832}
4833
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004834static void
4835il4965_rf_kill_ct_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004836{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004837 struct il_ct_kill_config cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004838 unsigned long flags;
4839 int ret = 0;
4840
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004841 spin_lock_irqsave(&il->lock, flags);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004842 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004843 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004844 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004845
4846 cmd.critical_temperature_R =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004847 cpu_to_le32(il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004848
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004849 ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004850 if (ret)
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004851 IL_ERR("C_CT_KILL_CONFIG failed\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004852 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004853 D_INFO("C_CT_KILL_CONFIG " "succeeded, "
4854 "critical temperature is %d\n",
4855 il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004856}
4857
4858static const s8 default_queue_to_tx_fifo[] = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004859 IL_TX_FIFO_VO,
4860 IL_TX_FIFO_VI,
4861 IL_TX_FIFO_BE,
4862 IL_TX_FIFO_BK,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004863 IL49_CMD_FIFO_NUM,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004864 IL_TX_FIFO_UNUSED,
4865 IL_TX_FIFO_UNUSED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004866};
4867
Stanislaw Gruszkae53aac42011-08-31 11:18:16 +02004868#define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
4869
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004870static int
4871il4965_alive_notify(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004872{
4873 u32 a;
4874 unsigned long flags;
4875 int i, chan;
4876 u32 reg_val;
4877
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004878 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004879
4880 /* Clear 4965's internal Tx Scheduler data base */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004881 il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004882 a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
4883 for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004884 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004885 for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004886 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004887 for (;
4888 a <
4889 il->scd_base_addr +
4890 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
4891 a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004892 il_write_targ_mem(il, a, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004893
4894 /* Tel 4965 where to find Tx byte count tables */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004895 il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004896
4897 /* Enable DMA channel */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004898 for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
4899 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
4900 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
4901 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004902
4903 /* Update FH chicken bits */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004904 reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
4905 il_wr(il, FH49_TX_CHICKEN_BITS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004906 reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004907
4908 /* Disable chain mode for all queues */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004909 il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004910
4911 /* Initialize each Tx queue (including the command queue) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004912 for (i = 0; i < il->hw_params.max_txq_num; i++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004913
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004914 /* TFD circular buffer read/write idxes */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004915 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004916 il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004917
4918 /* Max Tx Window size for Scheduler-ACK mode */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004919 il_write_targ_mem(il,
4920 il->scd_base_addr +
4921 IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
4922 (SCD_WIN_SIZE <<
4923 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
4924 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004925
4926 /* Frame limit */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004927 il_write_targ_mem(il,
4928 il->scd_base_addr +
4929 IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
4930 sizeof(u32),
4931 (SCD_FRAME_LIMIT <<
4932 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
4933 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004934
4935 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004936 il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004937 (1 << il->hw_params.max_txq_num) - 1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004938
4939 /* Activate all Tx DMA/FIFO channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004940 il4965_txq_set_sched(il, IL_MASK(0, 6));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004941
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004942 il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004943
4944 /* make sure all queue are not stopped */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004945 memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004946 for (i = 0; i < 4; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004947 atomic_set(&il->queue_stop_count[i], 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004948
4949 /* reset to 0 to enable all the queue first */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004950 il->txq_ctx_active_msk = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004951 /* Map each Tx/cmd queue to its corresponding fifo */
4952 BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
4953
4954 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
4955 int ac = default_queue_to_tx_fifo[i];
4956
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004957 il_txq_ctx_activate(il, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004958
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004959 if (ac == IL_TX_FIFO_UNUSED)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004960 continue;
4961
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004962 il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004963 }
4964
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004965 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004966
4967 return 0;
4968}
4969
4970/**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004971 * il4965_alive_start - called after N_ALIVE notification received
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004972 * from protocol/runtime uCode (initialization uCode's
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004973 * Alive gets handled by il_init_alive_start()).
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004974 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004975static void
4976il4965_alive_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004977{
4978 int ret = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01004979 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004980
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004981 D_INFO("Runtime Alive received.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004982
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004983 if (il->card_alive.is_valid != UCODE_VALID_OK) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004984 /* We had an error bringing up the hardware, so take it
4985 * all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004986 D_INFO("Alive failed.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004987 goto restart;
4988 }
4989
4990 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4991 * This is a paranoid check, because we would not have gotten the
4992 * "runtime" alive if code weren't properly loaded. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004993 if (il4965_verify_ucode(il)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004994 /* Runtime instruction load was bad;
4995 * take it all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004996 D_INFO("Bad runtime uCode load.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004997 goto restart;
4998 }
4999
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005000 ret = il4965_alive_notify(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005001 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005002 IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005003 goto restart;
5004 }
5005
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005006 /* After the ALIVE response, we can send host commands to the uCode */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005007 set_bit(S_ALIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005008
5009 /* Enable watchdog to monitor the driver tx queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005010 il_setup_watchdog(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005011
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005012 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005013 return;
5014
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005015 ieee80211_wake_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005016
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005017 il->active_rate = RATES_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005018
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005019 if (il_is_associated(il)) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005020 struct il_rxon_cmd *active_rxon =
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005021 (struct il_rxon_cmd *)&il->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005022 /* apply any changes in staging */
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005023 il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005024 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5025 } else {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005026 /* Initialize our rx_config data */
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005027 il_connection_init_rx_config(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005028
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005029 if (il->cfg->ops->hcmd->set_rxon_chain)
5030 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005031 }
5032
5033 /* Configure bluetooth coexistence if enabled */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005034 il_send_bt_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005035
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005036 il4965_reset_run_time_calib(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005037
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005038 set_bit(S_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005039
5040 /* Configure the adapter for unassociated operation */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005041 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005042
5043 /* At this point, the NIC is initialized and operational */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005044 il4965_rf_kill_ct_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005045
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005046 D_INFO("ALIVE processing complete.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005047 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005048
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005049 il_power_update_mode(il, true);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005050 D_INFO("Updated power mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005051
5052 return;
5053
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005054restart:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005055 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005056}
5057
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005058static void il4965_cancel_deferred_work(struct il_priv *il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005059
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005060static void
5061__il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005062{
5063 unsigned long flags;
Stanislaw Gruszkaab42b402011-04-28 11:51:24 +02005064 int exit_pending;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005065
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005066 D_INFO(DRV_NAME " is going down\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005067
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005068 il_scan_cancel_timeout(il, 200);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005069
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005070 exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005071
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005072 /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005073 * to prevent rearm timer */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005074 del_timer_sync(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005075
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005076 il_clear_ucode_stations(il, NULL);
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01005077
5078 /* FIXME: race conditions ? */
5079 spin_lock_irq(&il->sta_lock);
5080 /*
5081 * Remove all key information that is not stored as part
5082 * of station information since mac80211 may not have had
5083 * a chance to remove all the keys. When device is
5084 * reconfigured by mac80211 after an error all keys will
5085 * be reconfigured.
5086 */
5087 memset(il->_4965.wep_keys, 0, sizeof(il->_4965.wep_keys));
5088 il->_4965.key_mapping_keys = 0;
5089 spin_unlock_irq(&il->sta_lock);
5090
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005091 il_dealloc_bcast_stations(il);
5092 il_clear_driver_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005093
5094 /* Unblock any waiting calls */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005095 wake_up_all(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005096
5097 /* Wipe out the EXIT_PENDING status bit if we are not actually
5098 * exiting the module */
5099 if (!exit_pending)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005100 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005101
5102 /* stop and reset the on-board processor */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005103 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005104
5105 /* tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005106 spin_lock_irqsave(&il->lock, flags);
5107 il_disable_interrupts(il);
5108 spin_unlock_irqrestore(&il->lock, flags);
5109 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005110
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005111 if (il->mac80211_registered)
5112 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005113
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005114 /* If we have not previously called il_init() then
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005115 * clear all bits but the RF Kill bit and return */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005116 if (!il_is_init(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005117 il->status =
5118 test_bit(S_RF_KILL_HW,
5119 &il->
5120 status) << S_RF_KILL_HW |
5121 test_bit(S_GEO_CONFIGURED,
5122 &il->
5123 status) << S_GEO_CONFIGURED |
5124 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005125 goto exit;
5126 }
5127
5128 /* ...otherwise clear out all the status bits but the RF Kill
5129 * bit and continue taking the NIC down. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005130 il->status &=
5131 test_bit(S_RF_KILL_HW,
5132 &il->status) << S_RF_KILL_HW | test_bit(S_GEO_CONFIGURED,
5133 &il->
5134 status) <<
5135 S_GEO_CONFIGURED | test_bit(S_FW_ERROR,
5136 &il->
5137 status) << S_FW_ERROR |
5138 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005139
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005140 il4965_txq_ctx_stop(il);
5141 il4965_rxq_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005142
5143 /* Power-down device's busmaster DMA clocks */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02005144 il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005145 udelay(5);
5146
5147 /* Make sure (redundant) we've released our request to stay awake */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005148 il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005149
5150 /* Stop the device, and put it in low power state */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005151 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005152
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005153exit:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005154 memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005155
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005156 dev_kfree_skb(il->beacon_skb);
5157 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005158
5159 /* clear out any free frames */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005160 il4965_clear_free_frames(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005161}
5162
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005163static void
5164il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005165{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005166 mutex_lock(&il->mutex);
5167 __il4965_down(il);
5168 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005169
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005170 il4965_cancel_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005171}
5172
5173#define HW_READY_TIMEOUT (50)
5174
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005175static int
5176il4965_set_hw_ready(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005177{
5178 int ret = 0;
5179
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005180 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005181 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005182
5183 /* See if we got it */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005184 ret =
5185 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5186 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5187 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, HW_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005188 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005189 il->hw_ready = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005190 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005191 il->hw_ready = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005192
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005193 D_INFO("hardware %s\n", (il->hw_ready == 1) ? "ready" : "not ready");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005194 return ret;
5195}
5196
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005197static int
5198il4965_prepare_card_hw(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005199{
5200 int ret = 0;
5201
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005202 D_INFO("il4965_prepare_card_hw enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005203
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005204 ret = il4965_set_hw_ready(il);
5205 if (il->hw_ready)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005206 return ret;
5207
5208 /* If HW is not ready, prepare the conditions to check again */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005209 il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005210
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005211 ret =
5212 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5213 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
5214 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005215
5216 /* HW should be ready by now, check again. */
5217 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005218 il4965_set_hw_ready(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005219
5220 return ret;
5221}
5222
5223#define MAX_HW_RESTARTS 5
5224
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005225static int
5226__il4965_up(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005227{
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005228 int i;
5229 int ret;
5230
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005231 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005232 IL_WARN("Exit pending; will not bring the NIC up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005233 return -EIO;
5234 }
5235
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005236 if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005237 IL_ERR("ucode not available for device bringup\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005238 return -EIO;
5239 }
5240
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005241 ret = il4965_alloc_bcast_station(il, &il->ctx);
5242 if (ret) {
5243 il_dealloc_bcast_stations(il);
5244 return ret;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005245 }
5246
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005247 il4965_prepare_card_hw(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005248
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005249 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005250 IL_WARN("Exit HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005251 return -EIO;
5252 }
5253
5254 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005255 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005256 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005257 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005258 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005259
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005260 if (il_is_rfkill(il)) {
5261 wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005262
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005263 il_enable_interrupts(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005264 IL_WARN("Radio disabled by HW RF Kill switch\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005265 return 0;
5266 }
5267
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005268 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005269
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005270 /* must be initialised before il_hw_nic_init */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005271 il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005272
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005273 ret = il4965_hw_nic_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005274 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005275 IL_ERR("Unable to init nic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005276 return ret;
5277 }
5278
5279 /* make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005280 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005281 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005282
5283 /* clear (again), then enable host interrupts */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005284 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005285 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005286
5287 /* really make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005288 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5289 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005290
5291 /* Copy original ucode data image from disk into backup cache.
5292 * This will be used to initialize the on-board processor's
5293 * data SRAM for a clean start when the runtime program first loads. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005294 memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
5295 il->ucode_data.len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005296
5297 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5298
5299 /* load bootstrap state machine,
5300 * load bootstrap program into processor's memory,
5301 * prepare to load the "initialize" uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005302 ret = il->cfg->ops->lib->load_ucode(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005303
5304 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005305 IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005306 continue;
5307 }
5308
5309 /* start card; "initialize" will load runtime ucode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005310 il4965_nic_start(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005311
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005312 D_INFO(DRV_NAME " is coming up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005313
5314 return 0;
5315 }
5316
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005317 set_bit(S_EXIT_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005318 __il4965_down(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005319 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005320
5321 /* tried to restart and config the device for as long as our
5322 * patience could withstand */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005323 IL_ERR("Unable to initialize device after %d attempts.\n", i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005324 return -EIO;
5325}
5326
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005327/*****************************************************************************
5328 *
5329 * Workqueue callbacks
5330 *
5331 *****************************************************************************/
5332
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005333static void
5334il4965_bg_init_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005335{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005336 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005337 container_of(data, struct il_priv, init_alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005338
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005339 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005340 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005341 goto out;
5342
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005343 il->cfg->ops->lib->init_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005344out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005345 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005346}
5347
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005348static void
5349il4965_bg_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005350{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005351 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005352 container_of(data, struct il_priv, alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005353
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005354 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005355 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005356 goto out;
5357
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005358 il4965_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005359out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005360 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005361}
5362
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005363static void
5364il4965_bg_run_time_calib_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005365{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005366 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005367 run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005368
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005369 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005370
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005371 if (test_bit(S_EXIT_PENDING, &il->status) ||
5372 test_bit(S_SCANNING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005373 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005374 return;
5375 }
5376
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005377 if (il->start_calib) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005378 il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
5379 il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005380 }
5381
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005382 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005383}
5384
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005385static void
5386il4965_bg_restart(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005387{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005388 struct il_priv *il = container_of(data, struct il_priv, restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005389
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005390 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005391 return;
5392
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005393 if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005394 mutex_lock(&il->mutex);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005395 il->ctx.vif = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005396 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005397
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005398 __il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005399
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005400 mutex_unlock(&il->mutex);
5401 il4965_cancel_deferred_work(il);
5402 ieee80211_restart_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005403 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005404 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005405
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005406 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005407 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005408 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005409 return;
5410 }
5411
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005412 __il4965_up(il);
5413 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005414 }
5415}
5416
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005417static void
5418il4965_bg_rx_replenish(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005419{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005420 struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005421
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005422 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005423 return;
5424
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005425 mutex_lock(&il->mutex);
5426 il4965_rx_replenish(il);
5427 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005428}
5429
5430/*****************************************************************************
5431 *
5432 * mac80211 entry point functions
5433 *
5434 *****************************************************************************/
5435
5436#define UCODE_READY_TIMEOUT (4 * HZ)
5437
5438/*
5439 * Not a mac80211 entry point function, but it fits in with all the
5440 * other mac80211 functions grouped here.
5441 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005442static int
5443il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005444{
5445 int ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005446 struct ieee80211_hw *hw = il->hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005447
5448 hw->rate_control_algorithm = "iwl-4965-rs";
5449
5450 /* Tell mac80211 our characteristics */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005451 hw->flags =
5452 IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_AMPDU_AGGREGATION |
5453 IEEE80211_HW_NEED_DTIM_PERIOD | IEEE80211_HW_SPECTRUM_MGMT |
5454 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005455
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005456 if (il->cfg->sku & IL_SKU_N)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005457 hw->flags |=
5458 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
5459 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005460
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005461 hw->sta_data_size = sizeof(struct il_station_priv);
5462 hw->vif_data_size = sizeof(struct il_vif_priv);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005463
Stanislaw Gruszka8c9c48d2012-02-03 17:31:50 +01005464 hw->wiphy->interface_modes =
5465 BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005466
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005467 hw->wiphy->flags |=
5468 WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005469
5470 /*
5471 * For now, disable PS by default because it affects
5472 * RX performance significantly.
5473 */
5474 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5475
5476 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
5477 /* we create the 802.11 header and a zero-length SSID element */
5478 hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
5479
5480 /* Default value; 4 EDCA QOS priorities */
5481 hw->queues = 4;
5482
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005483 hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005484
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005485 if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
5486 il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005487 &il->bands[IEEE80211_BAND_2GHZ];
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005488 if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
5489 il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005490 &il->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005491
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005492 il_leds_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005493
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005494 ret = ieee80211_register_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005495 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005496 IL_ERR("Failed to register hw (error %d)\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005497 return ret;
5498 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005499 il->mac80211_registered = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005500
5501 return 0;
5502}
5503
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005504int
5505il4965_mac_start(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005506{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005507 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005508 int ret;
5509
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005510 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005511
5512 /* we should be verifying the device is ready to be opened */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005513 mutex_lock(&il->mutex);
5514 ret = __il4965_up(il);
5515 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005516
5517 if (ret)
5518 return ret;
5519
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005520 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005521 goto out;
5522
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005523 D_INFO("Start UP work done.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005524
5525 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5526 * mac80211 will not be run successfully. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005527 ret = wait_event_timeout(il->wait_command_queue,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005528 test_bit(S_READY, &il->status),
5529 UCODE_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005530 if (!ret) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005531 if (!test_bit(S_READY, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005532 IL_ERR("START_ALIVE timeout after %dms.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005533 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5534 return -ETIMEDOUT;
5535 }
5536 }
5537
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005538 il4965_led_enable(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005539
5540out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005541 il->is_open = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005542 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005543 return 0;
5544}
5545
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005546void
5547il4965_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005548{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005549 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005550
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005551 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005552
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005553 if (!il->is_open)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005554 return;
5555
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005556 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005557
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005558 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005559
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005560 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005561
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02005562 /* User space software may expect getting rfkill changes
5563 * even if interface is down */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005564 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005565 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005566
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005567 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005568}
5569
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005570void
5571il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005572{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005573 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005574
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005575 D_MACDUMP("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005576
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005577 D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005578 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005579
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005580 if (il4965_tx_skb(il, skb))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005581 dev_kfree_skb_any(skb);
5582
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005583 D_MACDUMP("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005584}
5585
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005586void
5587il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5588 struct ieee80211_key_conf *keyconf,
5589 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005590{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005591 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005592 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005593
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005594 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005595
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005596 il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta, iv32,
5597 phase1key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005598
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005599 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005600}
5601
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005602int
5603il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5604 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5605 struct ieee80211_key_conf *key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005606{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005607 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005608 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
5609 struct il_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005610 int ret;
5611 u8 sta_id;
5612 bool is_default_wep_key = false;
5613
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005614 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005615
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005616 if (il->cfg->mod_params->sw_crypto) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005617 D_MAC80211("leave - hwcrypto disabled\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005618 return -EOPNOTSUPP;
5619 }
5620
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005621 sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005622 if (sta_id == IL_INVALID_STATION)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005623 return -EINVAL;
5624
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005625 mutex_lock(&il->mutex);
5626 il_scan_cancel_timeout(il, 100);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005627
5628 /*
5629 * If we are getting WEP group key and we didn't receive any key mapping
5630 * so far, we are in legacy wep mode (group key only), otherwise we are
5631 * in 1X mode.
5632 * In legacy wep mode, we use another host command to the uCode.
5633 */
5634 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005635 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005636 if (cmd == SET_KEY)
Stanislaw Gruszkad735f922012-02-03 17:31:48 +01005637 is_default_wep_key = !il->_4965.key_mapping_keys;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005638 else
5639 is_default_wep_key =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005640 (key->hw_key_idx == HW_KEY_DEFAULT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005641 }
5642
5643 switch (cmd) {
5644 case SET_KEY:
5645 if (is_default_wep_key)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005646 ret =
5647 il4965_set_default_wep_key(il, vif_priv->ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005648 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005649 ret =
5650 il4965_set_dynamic_key(il, vif_priv->ctx, key,
5651 sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005652
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005653 D_MAC80211("enable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005654 break;
5655 case DISABLE_KEY:
5656 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005657 ret = il4965_remove_default_wep_key(il, ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005658 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005659 ret = il4965_remove_dynamic_key(il, ctx, key, sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005660
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005661 D_MAC80211("disable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005662 break;
5663 default:
5664 ret = -EINVAL;
5665 }
5666
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005667 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005668 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005669
5670 return ret;
5671}
5672
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005673int
5674il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5675 enum ieee80211_ampdu_mlme_action action,
5676 struct ieee80211_sta *sta, u16 tid, u16 * ssn,
5677 u8 buf_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005678{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005679 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005680 int ret = -EINVAL;
5681
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005682 D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005683
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005684 if (!(il->cfg->sku & IL_SKU_N))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005685 return -EACCES;
5686
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005687 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005688
5689 switch (action) {
5690 case IEEE80211_AMPDU_RX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005691 D_HT("start Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005692 ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005693 break;
5694 case IEEE80211_AMPDU_RX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005695 D_HT("stop Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005696 ret = il4965_sta_rx_agg_stop(il, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005697 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005698 ret = 0;
5699 break;
5700 case IEEE80211_AMPDU_TX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005701 D_HT("start Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005702 ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005703 break;
5704 case IEEE80211_AMPDU_TX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005705 D_HT("stop Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005706 ret = il4965_tx_agg_stop(il, vif, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005707 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005708 ret = 0;
5709 break;
5710 case IEEE80211_AMPDU_TX_OPERATIONAL:
5711 ret = 0;
5712 break;
5713 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005714 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005715
5716 return ret;
5717}
5718
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005719int
5720il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5721 struct ieee80211_sta *sta)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005722{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005723 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005724 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
5725 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005726 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
5727 int ret;
5728 u8 sta_id;
5729
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005730 D_INFO("received request to add station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005731 mutex_lock(&il->mutex);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005732 D_INFO("proceeding to add station %pM\n", sta->addr);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005733 sta_priv->common.sta_id = IL_INVALID_STATION;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005734
5735 atomic_set(&sta_priv->pending_frames, 0);
5736
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005737 ret =
5738 il_add_station_common(il, vif_priv->ctx, sta->addr, is_ap, sta,
5739 &sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005740 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005741 IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005742 /* Should we return success if return code is EEXIST ? */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005743 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005744 return ret;
5745 }
5746
5747 sta_priv->common.sta_id = sta_id;
5748
5749 /* Initialize rate scaling */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005750 D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005751 il4965_rs_rate_init(il, sta, sta_id);
5752 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005753
5754 return 0;
5755}
5756
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005757void
5758il4965_mac_channel_switch(struct ieee80211_hw *hw,
5759 struct ieee80211_channel_switch *ch_switch)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005760{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005761 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005762 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005763 struct ieee80211_conf *conf = &hw->conf;
5764 struct ieee80211_channel *channel = ch_switch->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005765 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005766
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005767 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005768 u16 ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005769
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005770 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005771
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005772 mutex_lock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005773
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005774 if (il_is_rfkill(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005775 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005776
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005777 if (test_bit(S_EXIT_PENDING, &il->status) ||
5778 test_bit(S_SCANNING, &il->status) ||
5779 test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005780 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005781
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005782 if (!il_is_associated(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005783 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005784
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005785 if (!il->cfg->ops->lib->set_channel_switch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005786 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005787
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005788 ch = channel->hw_value;
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005789 if (le16_to_cpu(il->active.channel) == ch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005790 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005791
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005792 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005793 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005794 D_MAC80211("invalid channel\n");
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005795 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005796 }
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005797
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005798 spin_lock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005799
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005800 il->current_ht_config.smps = conf->smps_mode;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005801
5802 /* Configure HT40 channels */
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005803 il->ht.enabled = conf_is_ht(conf);
5804 if (il->ht.enabled) {
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005805 if (conf_is_ht40_minus(conf)) {
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005806 il->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005807 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005808 il->ht.is_40mhz = true;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005809 } else if (conf_is_ht40_plus(conf)) {
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005810 il->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005811 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005812 il->ht.is_40mhz = true;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005813 } else {
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005814 il->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005815 IEEE80211_HT_PARAM_CHA_SEC_NONE;
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005816 il->ht.is_40mhz = false;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005817 }
5818 } else
Stanislaw Gruszka1c03c462012-02-03 17:31:52 +01005819 il->ht.is_40mhz = false;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005820
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005821 if ((le16_to_cpu(il->staging.channel) != ch))
5822 il->staging.flags = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005823
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005824 il_set_rxon_channel(il, channel, ctx);
5825 il_set_rxon_ht(il, ht_conf);
5826 il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005827
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005828 spin_unlock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005829
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005830 il_set_rate(il);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005831 /*
5832 * at this point, staging_rxon has the
5833 * configuration for channel switch
5834 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005835 set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005836 il->switch_channel = cpu_to_le16(ch);
5837 if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005838 clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005839 il->switch_channel = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005840 ieee80211_chswitch_done(ctx->vif, false);
5841 }
5842
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005843out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005844 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005845 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005846}
5847
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005848void
5849il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
5850 unsigned int *total_flags, u64 multicast)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005851{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005852 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005853 __le32 filter_or = 0, filter_nand = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005854
5855#define CHK(test, flag) do { \
5856 if (*total_flags & (test)) \
5857 filter_or |= (flag); \
5858 else \
5859 filter_nand |= (flag); \
5860 } while (0)
5861
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005862 D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
5863 *total_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005864
5865 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
5866 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
5867 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
5868 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
5869
5870#undef CHK
5871
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005872 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005873
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +01005874 il->staging.filter_flags &= ~filter_nand;
5875 il->staging.filter_flags |= filter_or;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005876
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005877 /*
5878 * Not committing directly because hardware can perform a scan,
5879 * but we'll eventually commit the filter flags change anyway.
5880 */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005881
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005882 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005883
5884 /*
5885 * Receiving all multicast frames is always enabled by the
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005886 * default flags setup in il_connection_init_rx_config()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005887 * since we currently do not support programming multicast
5888 * filters into the device.
5889 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005890 *total_flags &=
5891 FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
5892 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005893}
5894
5895/*****************************************************************************
5896 *
5897 * driver setup and teardown
5898 *
5899 *****************************************************************************/
5900
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005901static void
5902il4965_bg_txpower_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005903{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005904 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005905 txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005906
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005907 mutex_lock(&il->mutex);
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005908
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005909 /* If a scan happened to start before we got here
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005910 * then just return; the stats notification will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005911 * kick off another scheduled work to compensate for
5912 * any temperature delta we missed here. */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005913 if (test_bit(S_EXIT_PENDING, &il->status) ||
5914 test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005915 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005916
5917 /* Regardless of if we are associated, we must reconfigure the
5918 * TX power since frames can be sent on non-radar channels while
5919 * not associated */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005920 il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005921
5922 /* Update last_temperature to keep is_calib_needed from running
5923 * when it isn't needed... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005924 il->last_temperature = il->temperature;
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005925out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005926 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005927}
5928
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005929static void
5930il4965_setup_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005931{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005932 il->workqueue = create_singlethread_workqueue(DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005933
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005934 init_waitqueue_head(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005935
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005936 INIT_WORK(&il->restart, il4965_bg_restart);
5937 INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
5938 INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
5939 INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
5940 INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005941
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005942 il_setup_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005943
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005944 INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005945
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005946 init_timer(&il->stats_periodic);
5947 il->stats_periodic.data = (unsigned long)il;
5948 il->stats_periodic.function = il4965_bg_stats_periodic;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005949
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005950 init_timer(&il->watchdog);
5951 il->watchdog.data = (unsigned long)il;
5952 il->watchdog.function = il_bg_watchdog;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005953
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005954 tasklet_init(&il->irq_tasklet,
5955 (void (*)(unsigned long))il4965_irq_tasklet,
5956 (unsigned long)il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005957}
5958
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005959static void
5960il4965_cancel_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005961{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005962 cancel_work_sync(&il->txpower_work);
5963 cancel_delayed_work_sync(&il->init_alive_start);
5964 cancel_delayed_work(&il->alive_start);
5965 cancel_work_sync(&il->run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005966
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005967 il_cancel_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005968
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005969 del_timer_sync(&il->stats_periodic);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005970}
5971
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005972static void
5973il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005974{
5975 int i;
5976
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005977 for (i = 0; i < RATE_COUNT_LEGACY; i++) {
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02005978 rates[i].bitrate = il_rates[i].ieee * 5;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005979 rates[i].hw_value = i; /* Rate scaling will work on idxes */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005980 rates[i].hw_value_short = i;
5981 rates[i].flags = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005982 if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005983 /*
5984 * If CCK != 1M then set short preamble rate flag.
5985 */
5986 rates[i].flags |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005987 (il_rates[i].plcp ==
5988 RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005989 }
5990 }
5991}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005992
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005993/*
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005994 * Acquire il->lock before calling this function !
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005995 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005996void
5997il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005998{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005999 il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006000 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006001}
6002
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006003void
6004il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
6005 int tx_fifo_id, int scd_retry)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006006{
6007 int txq_id = txq->q.id;
6008
6009 /* Find out whether to activate Tx queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006010 int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006011
6012 /* Set up and activate */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006013 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +01006014 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
6015 (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
6016 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
6017 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
6018 IL49_SCD_QUEUE_STTS_REG_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006019
6020 txq->sched_retry = scd_retry;
6021
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006022 D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
6023 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006024}
6025
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006026static int
6027il4965_init_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006028{
6029 int ret;
6030
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006031 spin_lock_init(&il->sta_lock);
6032 spin_lock_init(&il->hcmd_lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006033
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006034 INIT_LIST_HEAD(&il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006035
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006036 mutex_init(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006037
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006038 il->ieee_channels = NULL;
6039 il->ieee_rates = NULL;
6040 il->band = IEEE80211_BAND_2GHZ;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006041
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006042 il->iw_mode = NL80211_IFTYPE_STATION;
6043 il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
6044 il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006045
6046 /* initialize force reset */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006047 il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006048
6049 /* Choose which receivers/antennas to use */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006050 if (il->cfg->ops->hcmd->set_rxon_chain)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006051 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006052
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006053 il_init_scan_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006054
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006055 ret = il_init_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006056 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006057 IL_ERR("initializing regulatory failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006058 goto err;
6059 }
6060
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006061 ret = il_init_geos(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006062 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006063 IL_ERR("initializing geos failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006064 goto err_free_channel_map;
6065 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006066 il4965_init_hw_rates(il, il->ieee_rates);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006067
6068 return 0;
6069
6070err_free_channel_map:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006071 il_free_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006072err:
6073 return ret;
6074}
6075
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006076static void
6077il4965_uninit_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006078{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006079 il4965_calib_free_results(il);
6080 il_free_geos(il);
6081 il_free_channel_map(il);
6082 kfree(il->scan_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006083}
6084
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006085static void
6086il4965_hw_detect(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006087{
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006088 il->hw_rev = _il_rd(il, CSR_HW_REV);
6089 il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006090 il->rev_id = il->pci_dev->revision;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006091 D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006092}
6093
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006094static int
6095il4965_set_hw_params(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006096{
Stanislaw Gruszkab16db502012-02-03 17:31:44 +01006097 il->hw_params.bcast_id = IL4965_BROADCAST_ID;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006098 il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
6099 il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
6100 if (il->cfg->mod_params->amsdu_size_8K)
6101 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006102 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006103 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006104
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006105 il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006106
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006107 if (il->cfg->mod_params->disable_11n)
6108 il->cfg->sku &= ~IL_SKU_N;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006109
6110 /* Device-specific setup */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006111 return il->cfg->ops->lib->set_hw_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006112}
6113
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006114static const u8 il4965_bss_ac_to_queue[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006115 0, 1, 2, 3,
6116};
6117
6118static int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006119il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006120{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006121 int err = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006122 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006123 struct ieee80211_hw *hw;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006124 struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006125 unsigned long flags;
6126 u16 pci_cmd;
6127
6128 /************************
6129 * 1. Allocating HW data
6130 ************************/
6131
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006132 hw = il_alloc_all(cfg);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006133 if (!hw) {
6134 err = -ENOMEM;
6135 goto out;
6136 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006137 il = hw->priv;
6138 /* At this point both hw and il are allocated. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006139
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006140 il->ctx.always_active = true;
6141 il->ctx.is_active = true;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006142 il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006143
6144 SET_IEEE80211_DEV(hw, &pdev->dev);
6145
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006146 D_INFO("*** LOAD DRIVER ***\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006147 il->cfg = cfg;
6148 il->pci_dev = pdev;
6149 il->inta_mask = CSR_INI_SET_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006150
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006151 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006152 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006153
6154 /**************************
6155 * 2. Initializing PCI bus
6156 **************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006157 pci_disable_link_state(pdev,
6158 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6159 PCIE_LINK_STATE_CLKPM);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006160
6161 if (pci_enable_device(pdev)) {
6162 err = -ENODEV;
6163 goto out_ieee80211_free_hw;
6164 }
6165
6166 pci_set_master(pdev);
6167
6168 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
6169 if (!err)
6170 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
6171 if (err) {
6172 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6173 if (!err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006174 err =
6175 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006176 /* both attempts failed: */
6177 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006178 IL_WARN("No suitable DMA available.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006179 goto out_pci_disable_device;
6180 }
6181 }
6182
6183 err = pci_request_regions(pdev, DRV_NAME);
6184 if (err)
6185 goto out_pci_disable_device;
6186
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006187 pci_set_drvdata(pdev, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006188
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006189 /***********************
6190 * 3. Read REV register
6191 ***********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006192 il->hw_base = pci_iomap(pdev, 0, 0);
6193 if (!il->hw_base) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006194 err = -ENODEV;
6195 goto out_pci_release_regions;
6196 }
6197
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006198 D_INFO("pci_resource_len = 0x%08llx\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006199 (unsigned long long)pci_resource_len(pdev, 0));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006200 D_INFO("pci_resource_base = %p\n", il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006201
6202 /* these spin locks will be used in apm_ops.init and EEPROM access
6203 * we should init now
6204 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006205 spin_lock_init(&il->reg_lock);
6206 spin_lock_init(&il->lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006207
6208 /*
6209 * stop and reset the on-board processor just in case it is in a
6210 * strange state ... like being left stranded by a primary kernel
6211 * and this is now the kdump kernel trying to start up
6212 */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006213 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006214
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006215 il4965_hw_detect(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006216 IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006217
6218 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6219 * PCI Tx retries from interfering with C3 CPU state */
6220 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
6221
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006222 il4965_prepare_card_hw(il);
6223 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006224 IL_WARN("Failed, HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006225 goto out_iounmap;
6226 }
6227
6228 /*****************
6229 * 4. Read EEPROM
6230 *****************/
6231 /* Read the EEPROM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006232 err = il_eeprom_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006233 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006234 IL_ERR("Unable to init EEPROM\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006235 goto out_iounmap;
6236 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006237 err = il4965_eeprom_check_version(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006238 if (err)
6239 goto out_free_eeprom;
6240
6241 if (err)
6242 goto out_free_eeprom;
6243
6244 /* extract MAC Address */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006245 il4965_eeprom_get_mac(il, il->addresses[0].addr);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006246 D_INFO("MAC address: %pM\n", il->addresses[0].addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006247 il->hw->wiphy->addresses = il->addresses;
6248 il->hw->wiphy->n_addresses = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006249
6250 /************************
6251 * 5. Setup HW constants
6252 ************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006253 if (il4965_set_hw_params(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006254 IL_ERR("failed to set hw parameters\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006255 goto out_free_eeprom;
6256 }
6257
6258 /*******************
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006259 * 6. Setup il
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006260 *******************/
6261
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006262 err = il4965_init_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006263 if (err)
6264 goto out_free_eeprom;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006265 /* At this point both hw and il are initialized. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006266
6267 /********************
6268 * 7. Setup services
6269 ********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006270 spin_lock_irqsave(&il->lock, flags);
6271 il_disable_interrupts(il);
6272 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006273
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006274 pci_enable_msi(il->pci_dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006275
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006276 err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006277 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006278 IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006279 goto out_disable_msi;
6280 }
6281
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006282 il4965_setup_deferred_work(il);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02006283 il4965_setup_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006284
6285 /*********************************************
6286 * 8. Enable interrupts and read RFKILL state
6287 *********************************************/
6288
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02006289 /* enable rfkill interrupt: hw bug w/a */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006290 pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006291 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
6292 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006293 pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006294 }
6295
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006296 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006297
6298 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006299 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006300 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006301 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006302 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006303
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006304 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006305 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006306
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006307 il_power_initialize(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006308
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006309 init_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006310
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006311 err = il4965_request_firmware(il, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006312 if (err)
6313 goto out_destroy_workqueue;
6314
6315 return 0;
6316
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006317out_destroy_workqueue:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006318 destroy_workqueue(il->workqueue);
6319 il->workqueue = NULL;
6320 free_irq(il->pci_dev->irq, il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006321out_disable_msi:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006322 pci_disable_msi(il->pci_dev);
6323 il4965_uninit_drv(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006324out_free_eeprom:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006325 il_eeprom_free(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006326out_iounmap:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006327 pci_iounmap(pdev, il->hw_base);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006328out_pci_release_regions:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006329 pci_set_drvdata(pdev, NULL);
6330 pci_release_regions(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006331out_pci_disable_device:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006332 pci_disable_device(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006333out_ieee80211_free_hw:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006334 il_free_traffic_mem(il);
6335 ieee80211_free_hw(il->hw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006336out:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006337 return err;
6338}
6339
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006340static void __devexit
6341il4965_pci_remove(struct pci_dev *pdev)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006342{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006343 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006344 unsigned long flags;
6345
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006346 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006347 return;
6348
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006349 wait_for_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006350
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006351 D_INFO("*** UNLOAD DRIVER ***\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006352
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006353 il_dbgfs_unregister(il);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006354 sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006355
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006356 /* ieee80211_unregister_hw call wil cause il_mac_stop to
6357 * to be called and il4965_down since we are removing the device
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006358 * we need to set S_EXIT_PENDING bit.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006359 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006360 set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006361
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006362 il_leds_exit(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006363
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006364 if (il->mac80211_registered) {
6365 ieee80211_unregister_hw(il->hw);
6366 il->mac80211_registered = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006367 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006368 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006369 }
6370
6371 /*
6372 * Make sure device is reset to low power before unloading driver.
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006373 * This may be redundant with il4965_down(), but there are paths to
6374 * run il4965_down() without calling apm_ops.stop(), and there are
6375 * paths to avoid running il4965_down() at all before leaving driver.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006376 * This (inexpensive) call *makes sure* device is reset.
6377 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006378 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006379
6380 /* make sure we flush any pending irq or
6381 * tasklet for the driver
6382 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006383 spin_lock_irqsave(&il->lock, flags);
6384 il_disable_interrupts(il);
6385 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006386
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006387 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006388
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006389 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006390
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006391 if (il->rxq.bd)
6392 il4965_rx_queue_free(il, &il->rxq);
6393 il4965_hw_txq_ctx_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006394
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006395 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006396
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006397 /*netif_stop_queue(dev); */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006398 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006399
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006400 /* ieee80211_unregister_hw calls il_mac_stop, which flushes
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006401 * il->workqueue... so we can't take down the workqueue
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006402 * until now... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006403 destroy_workqueue(il->workqueue);
6404 il->workqueue = NULL;
6405 il_free_traffic_mem(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006406
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006407 free_irq(il->pci_dev->irq, il);
6408 pci_disable_msi(il->pci_dev);
6409 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006410 pci_release_regions(pdev);
6411 pci_disable_device(pdev);
6412 pci_set_drvdata(pdev, NULL);
6413
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006414 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006415
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006416 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006417
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006418 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006419}
6420
6421/*
6422 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006423 * must be called under il->lock and mac access
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006424 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006425void
6426il4965_txq_set_sched(struct il_priv *il, u32 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006427{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006428 il_wr_prph(il, IL49_SCD_TXFACT, mask);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006429}
6430
6431/*****************************************************************************
6432 *
6433 * driver and module entry point
6434 *
6435 *****************************************************************************/
6436
6437/* Hardware specific file defines the PCI IDs table for that hardware module */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006438static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006439 {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
6440 {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006441 {0}
6442};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006443MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006444
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006445static struct pci_driver il4965_driver = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006446 .name = DRV_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006447 .id_table = il4965_hw_card_ids,
6448 .probe = il4965_pci_probe,
6449 .remove = __devexit_p(il4965_pci_remove),
6450 .driver.pm = IL_LEGACY_PM_OPS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006451};
6452
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006453static int __init
6454il4965_init(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006455{
6456
6457 int ret;
6458 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
6459 pr_info(DRV_COPYRIGHT "\n");
6460
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006461 ret = il4965_rate_control_register();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006462 if (ret) {
6463 pr_err("Unable to register rate control algorithm: %d\n", ret);
6464 return ret;
6465 }
6466
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006467 ret = pci_register_driver(&il4965_driver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006468 if (ret) {
6469 pr_err("Unable to initialize PCI module\n");
6470 goto error_register;
6471 }
6472
6473 return ret;
6474
6475error_register:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006476 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006477 return ret;
6478}
6479
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006480static void __exit
6481il4965_exit(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006482{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006483 pci_unregister_driver(&il4965_driver);
6484 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006485}
6486
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006487module_exit(il4965_exit);
6488module_init(il4965_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006489
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006490#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02006491module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006492MODULE_PARM_DESC(debug, "debug output mask");
6493#endif
6494
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006495module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006496MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006497module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006498MODULE_PARM_DESC(queues_num, "number of hw queues.");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006499module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006500MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006501module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int,
6502 S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006503MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006504module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006505MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");